-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkernel_gpu1.cu
252 lines (212 loc) · 9.21 KB
/
kernel_gpu1.cu
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
#include <assert.h>
#include <stdio.h>
#include "kernel.h"
#include "matrix.h"
#include "timer.h"
#define THRESHOLD 0.000001
#define YMAX 32
#define BLOCK_DIM 16
__global__ void spmspm(COOMatrix *result, CSRMatrix *A, CSCMatrix *B, float bias) {
unsigned int r = blockIdx.y*blockDim.y + threadIdx.y;
unsigned int c = blockIdx.x*blockDim.x + threadIdx.x;
unsigned int temp = 0;
if(r < A->numRows && c < B->numCols) {
unsigned int rowPtrA = A->rowPtrs[r]; // Index of the current rowPtrs element
unsigned int nnzA = A->rowPtrs[r + 1] - rowPtrA; // Number of non zero elements in A
if(nnzA > 0){
unsigned int *colIdxsA = A->colIdxs + rowPtrA;
float *valueA = A->values + rowPtrA;
// Loop over B columns
unsigned int colPtrB = B->colPtrs[c];
unsigned int nnzB = B->colPtrs[c + 1] - colPtrB;
if(nnzB > 0){
unsigned int *rowIdxsB = B->rowIdxs + colPtrB;
float *valueB = B->values + colPtrB;
// Loop and find intersection
float sum = 0;
unsigned int ia = 0;
unsigned int ib = 0;
// Loop over segment of non zero elements in the row of A and col of B
while(ia < nnzA && ib < nnzB){
unsigned int colIdx = colIdxsA[ia];
unsigned int rowIdx = rowIdxsB[ib];
if(colIdx < rowIdx) {
ia++;
} else if(colIdx > rowIdx) {
ib++;
} else {
sum += valueA[ia] * valueB[ib];
ia++;
ib++;
}
}
// Sync threads
// Write to Result
if(sum > THRESHOLD || sum < -THRESHOLD) {
sum += bias;
//Remove negative and zero values
if(sum > 0) {
if(sum>YMAX) {
sum = YMAX;
}
temp = atomicAdd(&result->nnz, 1);
result->colIdxs[temp] = c;
result->values[temp] = sum;
result->rowIdxs[temp] = r;
}
}
}
}
}
}
void findNonzeroRows(Vector* v, CSRMatrix* A) {
unsigned int nnz = 0;
for(unsigned int r = 0; r < A->numRows; ++r) {
unsigned int rowPtrA = A->rowPtrs[r];
unsigned int nnzA = A->rowPtrs[r + 1] - rowPtrA;
if(nnzA > 0) {
if(nnz >= v->capacity) {
expandVectorCapacity(v, 2*v->capacity);
}
v->data[nnz] = r;
++nnz;
}
}
v->nnz = nnz;
}
COOMatrix* createEmptyCOO_d(unsigned int numRows, unsigned int numCols, unsigned int capacity) {
COOMatrix cooShadow;
cooShadow.numRows = numRows;
cooShadow.numCols = numCols;
cooShadow.nnz = 0;
cooShadow.capacity = capacity;
cudaMalloc((void**) &cooShadow.rowIdxs, capacity*sizeof(unsigned int));
cudaMalloc((void**) &cooShadow.colIdxs, capacity*sizeof(unsigned int));
cudaMalloc((void**) &cooShadow.values, capacity*sizeof(float));
COOMatrix* coo_d;
cudaMalloc((void**) &coo_d, sizeof(COOMatrix));
cudaMemcpy(coo_d, &cooShadow, sizeof(COOMatrix), cudaMemcpyHostToDevice);
cudaDeviceSynchronize();
return coo_d;
}
void copyCOOfromGPU(COOMatrix* coo_d, COOMatrix* coo) {
COOMatrix cooShadow;
cudaMemcpy(&cooShadow, coo_d, sizeof(COOMatrix), cudaMemcpyDeviceToHost);
assert(coo->numRows == cooShadow.numRows);
assert(coo->numCols == cooShadow.numCols);
assert(coo->capacity >= cooShadow.nnz);
coo->nnz = cooShadow.nnz;
cudaMemcpy(coo->rowIdxs, cooShadow.rowIdxs, cooShadow.nnz*sizeof(unsigned int), cudaMemcpyDeviceToHost);
cudaMemcpy(coo->colIdxs, cooShadow.colIdxs, cooShadow.nnz*sizeof(unsigned int), cudaMemcpyDeviceToHost);
cudaMemcpy(coo->values, cooShadow.values, cooShadow.nnz*sizeof(float), cudaMemcpyDeviceToHost);
cudaDeviceSynchronize();
}
CSRMatrix* createEmptyCSR_d(unsigned int numRows, unsigned int numCols, unsigned int capacity) {
CSRMatrix csrShadow;
csrShadow.numRows = numRows;
csrShadow.numCols = numCols;
csrShadow.nnz = 0;
csrShadow.capacity = capacity;
cudaMalloc((void**) &csrShadow.rowPtrs, (numRows + 1)*sizeof(unsigned int));
cudaMalloc((void**) &csrShadow.colIdxs, capacity*sizeof(unsigned int));
cudaMalloc((void**) &csrShadow.values, capacity*sizeof(float));
CSRMatrix* csr_d;
cudaMalloc((void**) &csr_d, sizeof(CSRMatrix));
cudaMemcpy(csr_d, &csrShadow, sizeof(CSRMatrix), cudaMemcpyHostToDevice);
cudaDeviceSynchronize();
return csr_d;
}
void copyCSRtoGPU(CSRMatrix* csr, CSRMatrix* csr_d) {
CSRMatrix csrShadow;
cudaMemcpy(&csrShadow, csr_d, sizeof(CSRMatrix), cudaMemcpyDeviceToHost);
assert(csrShadow.numRows == csr->numRows);
assert(csrShadow.numCols == csr->numCols);
assert(csrShadow.capacity >= csr->nnz);
csrShadow.nnz = csr->nnz;
cudaMemcpy(csrShadow.rowPtrs, csr->rowPtrs, (csr->numRows + 1)*sizeof(unsigned int), cudaMemcpyHostToDevice);
cudaMemcpy(csrShadow.colIdxs, csr->colIdxs, csr->nnz*sizeof(unsigned int), cudaMemcpyHostToDevice);
cudaMemcpy(csrShadow.values, csr->values, csr->nnz*sizeof(float), cudaMemcpyHostToDevice);
cudaDeviceSynchronize();
}
CSCMatrix* createCSCfromCSC_d(CSCMatrix* csc) {
CSCMatrix cscShadow;
cscShadow.numRows = csc->numRows;
cscShadow.numCols = csc->numCols;
cscShadow.nnz = csc->nnz;
cscShadow.capacity = csc->capacity;
cudaMalloc((void**) &cscShadow.colPtrs, (csc->numCols + 1)*sizeof(unsigned int));
cudaMalloc((void**) &cscShadow.rowIdxs, csc->capacity*sizeof(unsigned int));
cudaMalloc((void**) &cscShadow.values, csc->capacity*sizeof(float));
cudaMemcpy(cscShadow.colPtrs, csc->colPtrs, (csc->numCols + 1)*sizeof(unsigned int), cudaMemcpyHostToDevice);
cudaMemcpy(cscShadow.rowIdxs, csc->rowIdxs, csc->capacity*sizeof(unsigned int), cudaMemcpyHostToDevice);
cudaMemcpy(cscShadow.values, csc->values, csc->capacity*sizeof(float), cudaMemcpyHostToDevice);
CSCMatrix* csc_d;
cudaMalloc((void**) &csc_d, sizeof(CSCMatrix));
cudaMemcpy(csc_d, &cscShadow, sizeof(CSCMatrix), cudaMemcpyHostToDevice);
cudaDeviceSynchronize();
return csc_d;
}
void sparseNN(Vector* result, COOMatrix* featureVectors, COOMatrix** layerWeights, float bias, unsigned int numLayers) {
Timer timer;
// Convert featureVectors to CSR
startTime(&timer);
CSRMatrix* Y0 = createEmptyCSR(featureVectors->numRows, featureVectors->numCols, 4*featureVectors->nnz); // Assuming 4*nnz is enough for all Y vectors
convertCOOtoCSR(featureVectors, Y0);
CSRMatrix* Y0_d = createEmptyCSR_d(featureVectors->numRows, featureVectors->numCols, 4*featureVectors->nnz); // Assuming 4*nnz is enough for all Y vectors
stopTimeAndPrint(&timer, "Convert feature vectors to CSR");
// Convert layer weights to CSC
startTime(&timer);
CSCMatrix* W[numLayers];
CSCMatrix* W_d[numLayers];
for(unsigned int layer = 0; layer < numLayers; ++layer) {
W[layer] = createCSCfromCOO(layerWeights[layer]);
W_d[layer] = createCSCfromCSC_d(W[layer]);
}
stopTimeAndPrint(&timer, "Convert weights to CSR");
// Temporary buffer
startTime(&timer);
COOMatrix *tmp = createEmptyCOO(Y0->numRows, Y0->numCols, Y0->capacity);
COOMatrix *tmp_d = createEmptyCOO_d(Y0->numRows, Y0->numCols, Y0->capacity);
stopTimeAndPrint(&timer, "Allocate temporary buffer");
// Loop over layers
CSRMatrix *Yin = Y0;
COOMatrix *Yout = tmp;
CSRMatrix *Yin_d = Y0_d;
COOMatrix *Yout_d = tmp_d;
// Configurations
dim3 threadsPerBlock(BLOCK_DIM,BLOCK_DIM);
dim3 blocksPerGrid((threadsPerBlock.x + Y0->numCols - 1)/threadsPerBlock.x,(threadsPerBlock.y + Y0->numRows - 1)/threadsPerBlock.y);
for(unsigned int layer = 0; layer < numLayers; ++layer) {
printf("Computing layer %u (SpMSpM)\n", layer);
// Copy to GPU
startTime(&timer);
copyCSRtoGPU(Yin, Yin_d);
cudaMemset(&Yout_d->nnz, 0, sizeof(unsigned int));
stopTimeAndPrint(&timer, " Copy CSR to GPU and clear COO");
// SpMSpM
startTime(&timer);
spmspm <<< blocksPerGrid, threadsPerBlock >>> (Yout_d, Yin_d, W_d[layer], bias);
cudaDeviceSynchronize();
stopTimeAndPrint(&timer, " SpMSpM");
// Copy from GPU
startTime(&timer);
copyCOOfromGPU(Yout_d, Yout);
stopTimeAndPrint(&timer, " Copy COO from GPU");
// Convert COO to CSR
startTime(&timer);
convertCOOtoCSR(Yout, Yin);
stopTimeAndPrint(&timer, " Converting COO to CSR");
}
// Find nonzero rows
startTime(&timer);
findNonzeroRows(result, Yin);
stopTimeAndPrint(&timer, "Find nonzero rows");
// Free buffers
startTime(&timer);
freeCSR(Y0);
for(unsigned int layer = 0; layer < numLayers; ++layer) {
freeCSC(W[layer]);
}
freeCOO(tmp);
stopTimeAndPrint(&timer, "Deallocate memory");
}