forked from Xtra-Computing/Medusa
/
APIKernelWrappers.h
221 lines (187 loc) · 4.96 KB
/
APIKernelWrappers.h
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
#ifndef APIWRAPPER_H
#define APIWRAPPER_H
#include "../Algorithm/DeviceDataStructure.h"
#include "../Algorithm/Configuration.h"
template<class O>
__global__ void vertexProcWrapper(int super_step, O op, int total_thread_num)
{
int tid = blockIdx.x*blockDim.x + threadIdx.x;
for(; tid < GRAPH_STORAGE_GPU::d_vertexArray.size; tid += total_thread_num)
{
D_Vertex vertex(tid);
op(vertex, super_step);
}
}
template<class O>
__global__ void OOvertexProcWrapper(int super_step, O op, int offset, int total_thread_num)
{
int tid = blockIdx.x*blockDim.x + threadIdx.x;
for(; tid < offset; tid += total_thread_num)
{
D_Vertex vertex(tid);
op(vertex, super_step);
}
}
template<class O>
__global__ void edgeProcWrapper(int super_step, O op, int total_thread_num)
{
int tid = blockDim.x*blockIdx.x + threadIdx.x;
for(; tid < GRAPH_STORAGE_GPU::d_edgeArray.size; tid += total_thread_num)
{
D_Edge edge(tid);
op(edge, super_step);
}
}
/*
* Queue version of edgeProcWrapper
* Work on edges (either edgeArray or edge queue), generate edge queu
* Code for enqueue operation is added before and after op operator.
* The op operator will call SetActive(int vertexID)
*/
#if defined MBFS
#define SetActive(vertexID)\
{\
activeVertexID = vertexID;\
}\
//The number of threads should be not less than edgeQueueAlphaSize
#if defined(WARPBASED) or defined (SCANBASED)
#define WARPS 8
#define WSIZE 32
//#define ATOMIC
__global__ void EdgeQueue2EdgeQueue(int super_step, int *edgeQueuePtr, int *edgeQueueAlpha, int edgeQueueAlphaSize, int *edgeQueueBeta)
{
int tid = blockDim.x*(blockIdx.x*gridDim.y + blockIdx.y) + threadIdx.x;
#if defined WARPBASED
volatile __shared__ int comm[WARPS][3];
int warp_id = threadIdx.x >> 5;
int lane_id = threadIdx.x & 31;
int activeVertexID = -1;
#endif
if(tid < edgeQueueAlphaSize)
{
//----------------------generated code------------------------------------
int dstVertexID = edgeQueueAlpha[tid];
D_Vertex dst_V(dstVertexID);
#if defined ATOMIC
int dstLevel = atomicCAS(&GRAPH_STORAGE_GPU::d_vertexArray.d_level[dstVertexID], MVT_Init_Value, super_step + 1);
if(dstLevel == MVT_Init_Value)
{
SetActive(dstVertexID);
}
#else
if(dst_V.get_level() == MVT_Init_Value)
{
dst_V.set_level(super_step + 1);
SetActive(dstVertexID);
}
#endif
//--------------------end of generated code--------------------------------
}
#if defined WARPBASED
//enqueue the edges
int r = 0, r_end = 0;
if(activeVertexID != -1)
{
r = GRAPH_STORAGE_GPU::d_vertexArray.d_edge_index[activeVertexID];
r_end = GRAPH_STORAGE_GPU::d_vertexArray.d_edge_index[activeVertexID + 1];
}
//printf("%d\n", activeVertexID);
while(__any(r_end - r > 0))
{
//vie for control of warp
if(r_end - r)
{
comm[warp_id][0] = lane_id;
}
// winner describes adjlist
if (comm[warp_id][0] == lane_id)
{
comm[warp_id][1] = r;
comm[warp_id][2] = r_end;
//get enqueue offset of this edgelist
comm[warp_id][0] = atomicAdd(edgeQueuePtr, r_end - r);
r_end = r;
}
//strip-mine winner's adjlist
int r_gather = comm[warp_id][1] + lane_id;
int r_gather_end = comm[warp_id][2];
int r_scatter = comm[warp_id][0] + lane_id;
while(r_gather < r_gather_end)
{
edgeQueueBeta[r_scatter] = GRAPH_STORAGE_GPU::d_edgeArray.d_dstVertexID[r_gather];
r_gather += WSIZE;
r_scatter += WSIZE;
}
}
#endif
}
#endif //defined(WARPBASED) or defined (SCANBASED)
#endif //defined MBFS
/**
*
* Edge processor wrapper for original and core
* @param -
* @return
* @note
*
*/
template<class O>
__global__ void OOEdgeProcWrapper(int super_step, O op, int offset, int total_thread_num)
{
int tid = blockIdx.x*blockDim.x + threadIdx.x;
for(; tid < offset; tid += total_thread_num)
{
D_Edge edge(tid);
op(edge, super_step);
}
}
/**
*
* Edge processor wrapper for replica
* @param -
* @return
* @note
*
*/
template<class O>
__global__ void REdgeProcWrapper(int super_step, O op, int offset, int total_thread_num)
{
int tid = blockIdx.x*blockDim.x + threadIdx.x + offset;
for(;tid < GRAPH_STORAGE_GPU::d_edgeArray.size; tid += total_thread_num)
{
D_Edge edge(tid);
op(edge, super_step);
}
}
template<class O>
__global__ void edgeProcWrapperHT(int super_step, O op, int total_thread_num)
{
int tid = blockIdx.x*blockDim.x + threadIdx.x;
for( ; tid < GRAPH_STORAGE_GPU::d_edgeArray.size; tid += total_thread_num)
{
D_Edge edge(tid);
op(edge, super_step);
}
}
template<class O>
__global__ void edgeListProcWrapper(int super_step, O op, int total_thread_num)
{
int tid = blockIdx.x*blockDim.x + threadIdx.x;
for(; tid < GRAPH_STORAGE_GPU::d_vertexArray.size; tid += total_thread_num)
{
EdgeList edgeList(tid);
D_Vertex vertex(tid);
op(vertex, edgeList, super_step);
}
}
template<class O>
__global__ void messageProcWrapper(int super_step, O op, int total_thread_num)
{
int tid = blockIdx.x*blockDim.x + threadIdx.x;
for(; tid < GRAPH_STORAGE_GPU::d_edgeArray.size; tid += total_thread_num)
{
D_Message message(tid);
op(message, super_step);
}
}
#endif