File size: 8,478 Bytes
5cb6c4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#ifndef _ORDERGRAPH_KERNEL_H_
#define _ORDERGRAPH_KERNEL_H_

#include <stdio.h>

#ifdef DATA_25
#include "data25.cu"
#endif
#ifdef DATA_30
#include "data30.cu"
#endif
#ifdef DATA_40
#include "data40.cu"
#endif
#ifdef DATA_45
#include "data45.cu"
#endif
#ifdef DATA_50
#include "data50.cu"
#endif
#ifdef DATA_125
#include "data125.cu"
#endif
;

#include <cooperative_groups.h>
#include <cooperative_groups/memcpy_async.h>

using namespace nvcuda::experimental;

#define PREFETCH_COUNT 2

#define BLOCK_SIZE 256
#define MAX_NBLOCKS 1024
#define MIN_NBATCHES 16


__device__ void Dincr(int *bit, int n);
__device__ void DincrS(int *bit, int n);
__device__ bool D_getState(int parN, int *sta, int time);
__device__ void D_findComb(int *comb, int l, int n);
__device__ int D_findindex(int *arr, int size);
__device__ int D_C(int n, int a);

__global__ void genScoreKernel(int sizepernode, float *D_localscore,
                               int *D_data, float *D_LG) {
  int id = blockIdx.x * BLOCK_SIZE + threadIdx.x;
  int node, index;
  bool flag;
  int parent[5] = {0};
  int pre[NODE_N] = {0};
  int state[5] = {0};
  int i, j, parN = 0, tmp, t;
  int t1 = 0, t2 = 0;
  float ls = 0;
  int Nij[STATE_N] = {0};

  if (id < sizepernode) {

    D_findComb(parent, id, NODE_N - 1);

    for (i = 0; i < 4; i++) {
      if (parent[i] > 0)
        parN++;
    }

    for (node = 0; node < NODE_N; node++) {

      j = 1;
      for (i = 0; i < NODE_N; i++) {
        if (i != node)
          pre[j++] = i;
      }

      for (tmp = 0; tmp < parN; tmp++)
        state[tmp] = 0;

      index = sizepernode * node + id;

      // priors
      /*
      for(tmp=1;tmp<=4;tmp++){
          localscore[index]+=100*(prior[node][pre[parent[tmp]]]-0.5)*(prior[node][pre[parent[tmp]]]-0.5)*(prior[node][pre[parent[tmp]]]-0.5);
      }
      */
      t = 0;
      while (D_getState(parN, state, t++)) { // for get state
                                             // printf("test %u\n",id);
        ls = 0;
        for (tmp = 0; tmp < STATE_N; tmp++)
          Nij[tmp] = 0;

        for (t1 = 0; t1 < DATA_N; t1++) {
          flag = true;
          for (t2 = 0; t2 < parN; t2++) {
            if (D_data[t1 * NODE_N + pre[parent[t2]]] != state[t2]) {
              flag = false;
              break;
            }
          }
          if (!flag)
            continue;

          Nij[D_data[t1 * NODE_N + node]]++;
        }

        tmp = STATE_N - 1;

        for (t1 = 0; t1 < STATE_N; t1++) {
          ls += D_LG[Nij[t1]];
          tmp += Nij[t1];
        }

        ls -= D_LG[tmp];
        ls += D_LG[STATE_N - 1];

        D_localscore[index] += ls;
      }
    }
  }
}

__global__ void computeKernel(int taskperthr, int sizepernode,
                              float *D_localscore, bool *D_parent, int node,
                              int total, float *D_Score, int *D_resP,
                              int nbatches) {
  cooperative_groups::thread_block block =
      cooperative_groups::this_thread_block();
  pipeline pipe;
  __shared__ float lsinblock[PREFETCH_COUNT][BLOCK_SIZE];

  int fetch = 0;
  int end_tile = fetch + nbatches;
  int bestparent[4] = {0}, parent[5] = {-1};

  for (int compute = fetch; compute < end_tile; compute++) {
    for (; fetch < end_tile && fetch < compute + PREFETCH_COUNT; fetch++) {
      unsigned int bid = blockIdx.x * nbatches + fetch;
      unsigned int tid = threadIdx.x;
      unsigned int id = bid * (BLOCK_SIZE * nbatches) + tid;

      int posN = 1, i, index, tmp;
      int pre[NODE_N] = {0};
      int parN = 0;

      float bestls = -999999999999999, ls;

      for (i = 0; i < NODE_N; i++) {
        if (D_parent[i] == 1) {
          pre[posN++] = i;
        }
      }

      for (i = 0; i < taskperthr && ((id * taskperthr + i) < total); i++) {

        D_findComb(parent, id * taskperthr + i, posN);

        for (parN = 0; parN < 4; parN++) {
          if (parent[parN] < 0)
            break;
          if (pre[parent[parN]] > node)
            parent[parN] = pre[parent[parN]];
          else
            parent[parN] = pre[parent[parN]] + 1;
        }

        for (tmp = parN; tmp > 0; tmp--) {
          parent[tmp] = parent[tmp - 1];
        }
        parent[0] = 0;

        index = D_findindex(parent, parN);
        index += sizepernode * node;

        ls = D_localscore[index];

        if (ls > bestls) {
          bestls = ls;
          for (tmp = 0; tmp < 4; tmp++)
            bestparent[tmp] = parent[tmp + 1];
        }
      }

      memcpy_async(lsinblock[fetch % PREFETCH_COUNT][tid], bestls, pipe);
      pipe.commit();
    }
    if (fetch == end_tile) {
      for (int i = 0; i < PREFETCH_COUNT - 1; ++i) {
        pipe.commit();
      }
      ++fetch;
    }
    pipe.wait_prior<PREFETCH_COUNT - 1>();
    block.sync();

    int i, t;
    unsigned int bid = blockIdx.x * nbatches + compute;
    unsigned int tid = threadIdx.x;
    // unsigned int id = bid * (BLOCK_SIZE * nbatches) + tid;

    for (i = BLOCK_SIZE / 2; i >= 1; i /= 2) {
      if (tid < i) {
        if (lsinblock[compute % PREFETCH_COUNT][tid + i] >
                lsinblock[compute % PREFETCH_COUNT][tid] &&
            lsinblock[compute % PREFETCH_COUNT][tid + i] < 0) {
          lsinblock[compute % PREFETCH_COUNT][tid] =
              lsinblock[compute % PREFETCH_COUNT][tid + i];
          lsinblock[compute % PREFETCH_COUNT][tid + i] = (float)(tid + i);
        } else if (lsinblock[compute % PREFETCH_COUNT][tid + i] <
                       lsinblock[compute % PREFETCH_COUNT][tid] &&
                   lsinblock[compute % PREFETCH_COUNT][tid] < 0) {
          lsinblock[compute % PREFETCH_COUNT][tid + i] = (float)tid;
        } else if (lsinblock[tid] > 0 && lsinblock[tid + i] < 0) {
          lsinblock[compute % PREFETCH_COUNT][tid] =
              lsinblock[compute % PREFETCH_COUNT][tid + i];
          lsinblock[compute % PREFETCH_COUNT][tid + i] = (float)(tid + i);
        } else if (lsinblock[compute % PREFETCH_COUNT][tid] < 0 &&
                   lsinblock[compute % PREFETCH_COUNT][tid + i] > 0) {
          lsinblock[compute % PREFETCH_COUNT][tid + i] = (float)tid;
        }
      }
      block.sync();
    }
    block.sync();

    if (tid == 0) {
      D_Score[bid] = lsinblock[compute % PREFETCH_COUNT][0];
      t = 0;
      for (i = 0; i < 7 && t < 128 && t >= 0; i++) {
        t = (int)lsinblock[compute % PREFETCH_COUNT][(int)powf(2.0, i) + t];
      }
      lsinblock[compute % PREFETCH_COUNT][0] = (float)t;
    }
    block.sync();

    if (tid == (int)lsinblock[compute % PREFETCH_COUNT][0]) {
      for (i = 0; i < 4; i++) {
        D_resP[bid * 4 + i] = bestparent[i];
      }
    }
  }
}

__device__ void Dincr(int *bit, int n) {

  while (n <= NODE_N) {
    bit[n]++;
    if (bit[n] >= 2) {
      bit[n] = 0;
      n++;
    } else {
      break;
    }
  }

  return;
}

__device__ void DincrS(int *bit, int n) {

  bit[n]++;
  if (bit[n] >= STATE_N) {
    bit[n] = 0;
    Dincr(bit, n + 1);
  }

  return;
}

__device__ bool D_getState(int parN, int *sta, int time) {
  int i, j = 1;

  for (i = 0; i < parN; i++) {
    j *= STATE_N;
  }
  j--;
  if (time > j)
    return false;

  if (time >= 1)
    DincrS(sta, 0);

  return true;
}

__device__ void D_findComb(int *comb, int l, int n) {
  const int len = 4;
  if (l == 0) {
    for (int i = 0; i < len; i++)
      comb[i] = -1;
    return;
  }
  int sum = 0;
  int k = 1;

  while (sum < l)
    sum += D_C(n, k++);
  l -= sum - D_C(n, --k);
  int low = 0;
  int pos = 0;
  while (k > 1) {
    sum = 0;
    int s = 1;
    while (sum < l)
      sum += D_C(n - s++, k - 1);
    l -= sum - D_C(n - (--s), --k);
    low += s;
    comb[pos++] = low;
    n -= s;
  }
  comb[pos] = low + l;
  for (int i = pos + 1; i < 4; i++)
    comb[i] = -1;
}

__device__ int D_findindex(int *arr,
                           int size) { // reminder: arr[0] has to be 0 && size
                                       // == array size-1 && index start from 0
  int i, j, index = 0;

  for (i = 1; i < size; i++) {
    index += D_C(NODE_N - 1, i);
  }

  for (i = 1; i <= size - 1; i++) {
    for (j = arr[i - 1] + 1; j <= arr[i] - 1; j++) {
      index += D_C(NODE_N - 1 - j, size - i);
    }
  }

  index += arr[size] - arr[size - 1];

  return index;
}

__device__ int D_C(int n, int a) {
  int i, res = 1, atmp = a;

  for (i = 0; i < atmp; i++) {
    res *= n;
    n--;
  }

  for (i = 0; i < atmp; i++) {
    res /= a;
    a--;
  }

  return res;
}

#endif