cpu.c 1.32 KB
Newer Older
rusty1s's avatar
rusty1s committed
1
2
3
4
#include <TH/TH.h>

#define cluster_(NAME) TH_CONCAT_4(cluster_, NAME, _, Real)

5
void cluster_random(THLongTensor *output, THLongTensor *row, THLongTensor *col, THLongTensor *degree) {
rusty1s's avatar
rusty1s committed
6
7
8
9
10
  int64_t *output_data = output->storage->data + output->storageOffset;
  int64_t *row_data = row->storage->data + row->storageOffset;
  int64_t *col_data = col->storage->data + col->storageOffset;
  int64_t *degree_data = degree->storage->data + degree->storageOffset;

rusty1s's avatar
rusty1s committed
11
  int64_t e = 0, row_value, col_value, i, value;
rusty1s's avatar
rusty1s committed
12
13
14
15

  while(e < THLongTensor_nElement(row)) {
    row_value = row_data[e];
    if (output_data[row_value] < 0) { // Node is unmatched.
rusty1s's avatar
rusty1s committed
16

rusty1s's avatar
rusty1s committed
17
18
19
      // Find next unmatched neighbor.
      col_value = -1;
      for (i = 0; i < degree_data[row_value]; i++) {
rusty1s's avatar
rusty1s committed
20
21
22
23
24
        value = col_data[e + i];
        if (output_data[value] < 0) { // Neighbor found. Save and abort.
          col_value = value;
          break;
        }
rusty1s's avatar
rusty1s committed
25
26
      }

rusty1s's avatar
rusty1s committed
27
      // Set cluster output for new matched nodes (one or two).
rusty1s's avatar
rusty1s committed
28
29
30
31
32
33
34
35
36
      if (col_value < 0) {
        output_data[row_value] = row_value;
      }
      else {
        i = row_value < col_value ? row_value : col_value;
        output_data[row_value] = i;
        output_data[col_value] = i;
      }
    }
rusty1s's avatar
rusty1s committed
37
38

    // Jump to next row.
rusty1s's avatar
rusty1s committed
39
40
    e += degree_data[row_value];
  }
41
42
}

rusty1s's avatar
rusty1s committed
43
44
#include "generic/cpu.c"
#include "THGenerateAllTypes.h"