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

rusty1s's avatar
rusty1s committed
3
#define THGreedy_ TH_CONCAT_3(TH,Real,Greedy)
rusty1s's avatar
rusty1s committed
4
#define DATA(TENSOR) TENSOR->storage->data + TENSOR->storageOffset
rusty1s's avatar
rusty1s committed
5
6

#define TH_GREEDY_CLUSTER(cluster, row, col, deg, SELECT) { \
rusty1s's avatar
rusty1s committed
7
8
9
10
11
  THLongTensor_fill(cluster, -1); \
  int64_t *clusterData = DATA(cluster); \
  int64_t *rowData = DATA(row); \
  int64_t *colData = DATA(col); \
  int64_t *degData = DATA(deg); \
rusty1s's avatar
rusty1s committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  ptrdiff_t rowIdx = 0, neighborIdx; \
  int64_t rowValue, colValue, clusterValue, tmp; \
  while(rowIdx < THLongTensor_nElement(row)) { \
    rowValue = rowData[rowIdx]; \
    if (clusterData[rowValue] < 0) { \
      colValue = rowValue; \
      SELECT \
      clusterValue = rowValue < colValue ? rowValue : colValue; \
      clusterData[rowValue] = clusterValue; \
      clusterData[colValue] = clusterValue; \
    } \
    rowIdx += degData[rowValue]; \
  } \
}

rusty1s's avatar
rusty1s committed
27
void THGreedy(THLongTensor *cluster, THLongTensor *row, THLongTensor *col, THLongTensor *deg) {
rusty1s's avatar
rusty1s committed
28
29
30
31
32
33
34
35
36
37
38
39
40
  TH_GREEDY_CLUSTER(cluster, row, col, deg,
    for (neighborIdx = rowIdx; neighborIdx < rowIdx + degData[rowValue]; neighborIdx++) {
      tmp = colData[neighborIdx];
      if (clusterData[tmp] < 0) {
        colValue = tmp;
        break;
      }
    }
  )
}

#include "generic/THGreedy.c"
#include "THGenerateAllTypes.h"