serial_cpu.c 1.2 KB
Newer Older
rusty1s's avatar
rusty1s committed
1
2
#include <TH/TH.h>

rusty1s's avatar
rusty1s committed
3
void serial_cluster(THLongTensor *output, THLongTensor *row, THLongTensor *col, THLongTensor *degree) {
rusty1s's avatar
rusty1s committed
4
5
6
7
8
  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
9
  int64_t e = 0, row_value, col_value, i, value;
rusty1s's avatar
rusty1s committed
10
11
12
13

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

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

rusty1s's avatar
rusty1s committed
25
      // Set cluster output for new matched nodes (one or two).
rusty1s's avatar
rusty1s committed
26
27
28
29
30
31
32
33
34
      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
35
36

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

rusty1s's avatar
rusty1s committed
41