-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsparse.cu
275 lines (240 loc) · 11.8 KB
/
sparse.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <stdlib.h>
#include <math.h>
#include "./include/library.h"
#include "./include/helper_cuda.h"
#include <cuda_runtime.h>
#include <cusparse.h>
#define NDEVICE 4
#define TIMER_DEF struct timeval temp_1, temp_2
#define TIMER_START gettimeofday(&temp_1, (struct timezone*)0)
#define TIMER_STOP gettimeofday(&temp_2, (struct timezone*)0)
#define TIMER_ELAPSED ((temp_2.tv_sec-temp_1.tv_sec)+(temp_2.tv_usec-temp_1.tv_usec)/1000000.0)
int main(int argc, char *argv[]) {
//The paths of our benchmark matrices
const char* path[] = {
"./dataset/1138_bus/1138_bus.mtx",
"./dataset/Maragal_3/Maragal_3.mtx",
"./dataset/photogrammetry/photogrammetry.mtx",
"./dataset/plbuckle/plbuckle.mtx",
"./dataset/bcsstk17/bcsstk17.mtx",
"./dataset/filter2D/filter2D.mtx",
"./dataset/SiH4/SiH4.mtx",
"./dataset/linverse/linverse.mtx",
"./dataset/t2dah_a/t2dah_a.mtx",
"./dataset/bcsstk35/bcsstk35.mtx"
};
//Stats of my problem
srand(time(NULL));
int array_length = sizeof(path) / sizeof(path[0]);
int blocksize = 16;
int gridsize = 7;
printf("==============================================================\n");
printf("STATS OF MY PROBLEM\n");
printf("block size = %d \n", blocksize);
printf("grid size = %d \n", gridsize);
dim3 block_size(blocksize, blocksize, 1);
dim3 grid_size(gridsize, gridsize, 1);
printf("%d: block_size = (%d, %d), grid_size = (%d, %d)\n", __LINE__, block_size.x, block_size.y, grid_size.x, grid_size.y);
int sharedMemSize = sizeof(dtype) * block_size.x * block_size.y * 2;
int *number, m, n, nnz;
int nnz_counter;
//Timer definitions
TIMER_DEF;
float times[NDEVICE] = {0};
char filename[556];
FILE *csvtime[array_length];
//Print device properties for unitn cluster
FILE *file = fopen("warp.txt", "r");
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
printf("==============================================================\n");
printf("DEVICE PROPERTIES\n");
char ch;
while ((ch = fgetc(file)) != EOF) {
printf("%c", ch);
}
fclose(file);
for(int k = 0; k < array_length; k++){
//Initialize all the stuff we need
dtype *matrix = NULL;
number = (int *)malloc(3 * sizeof(int));
read_mtx(path[k], matrix, number);
nnz_counter = 0;
//Assign number of rows, columns and non zero elements
m = number[0];
n = number[1];
nnz = number[2];
//Initialize kernel
dummyKernel<<<grid_size, block_size>>>();
checkCudaErrors(cudaGetLastError());
checkCudaErrors(cudaDeviceSynchronize());
//Cusparse handle and stream
cusparseHandle_t handle;
cusparseCreate(&handle);
checkCudaErrors(cudaGetLastError());
cudaStream_t stream;
checkCudaErrors(cudaStreamCreate(&stream));
//Allocate and initialize host memory
int *h_csrRowPtr = (int *)malloc((m + 1) * sizeof(int));
int *h_csrColInd = (int *)malloc(nnz * sizeof(int));
dtype *h_csrVal = (dtype *)malloc(nnz * sizeof(dtype));
if (h_csrRowPtr == NULL || h_csrColInd == NULL || h_csrVal == NULL) {
fprintf(stderr, "Error allocating host memory\n");
return 1;
}
//Allocate device memory
int *d_csrRowPtr, *d_csrColInd, *d_cscRowInd, *d_cscColPtr;
dtype *d_csrVal, *d_cscVal;
checkCudaErrors(cudaMalloc((void **)&d_csrRowPtr, (m + 1) * sizeof(int)));
checkCudaErrors(cudaMalloc((void **)&d_csrColInd, nnz * sizeof(int)));
checkCudaErrors(cudaMalloc((void **)&d_csrVal, nnz * sizeof(dtype)));
checkCudaErrors(cudaMalloc((void **)&d_cscRowInd, nnz * sizeof(int)));
checkCudaErrors(cudaMalloc((void **)&d_cscColPtr, (n + 1) * sizeof(int)));
checkCudaErrors(cudaMalloc((void **)&d_cscVal, nnz * sizeof(dtype)));
//Assign values host memory
h_csrRowPtr[0] = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i + j * m] != 0) {
if (nnz_counter >= nnz) {
fprintf(stderr, "Error: nnz_counter (%d) exceeded nnz (%d) at i=%d, j=%d\n", nnz_counter, nnz, i, j);
exit(EXIT_FAILURE);
}
h_csrColInd[nnz_counter] = j;
h_csrVal[nnz_counter] = matrix[i + j * m];
nnz_counter++;
}
}
if (i + 1 > m) {
fprintf(stderr, "Error: i+1 (%d) exceeded m (%d)\n", i + 1, m);
exit(EXIT_FAILURE);
}
h_csrRowPtr[i + 1] = nnz_counter;
}
//Copy data to device
checkCudaErrors(cudaMemcpyAsync(d_csrRowPtr, h_csrRowPtr, (m + 1) * sizeof(int), cudaMemcpyHostToDevice, stream));
checkCudaErrors(cudaMemcpyAsync(d_csrColInd, h_csrColInd, nnz * sizeof(int), cudaMemcpyHostToDevice, stream));
checkCudaErrors(cudaMemcpyAsync(d_csrVal, h_csrVal, nnz * sizeof(dtype), cudaMemcpyHostToDevice, stream));
//Perform sparse matrix transpose with cusparse
void *buffer;
cusparseBuffer(handle, m, n, nnz, d_csrVal, d_csrRowPtr, d_csrColInd,
d_cscVal, d_cscColPtr, d_cscRowInd, buffer);
TIMER_START;
cusparseTranspose(handle, m, n, nnz, d_csrVal, d_csrRowPtr, d_csrColInd,
d_cscVal, d_cscColPtr, d_cscRowInd, buffer);
TIMER_STOP;
times[0] = TIMER_ELAPSED;
//Copy the transposed matrix back to host
checkCudaErrors(cudaMemcpy(h_csrRowPtr, d_cscColPtr, (n + 1) * sizeof(int), cudaMemcpyDeviceToHost));
checkCudaErrors(cudaMemcpy(h_csrColInd, d_cscRowInd, nnz * sizeof(int), cudaMemcpyDeviceToHost));
checkCudaErrors(cudaMemcpy(h_csrVal, d_cscVal, nnz * sizeof(dtype), cudaMemcpyDeviceToHost));
//Perform a normal matrix transpose with kernels from homework 2
dtype *transpose = NULL, *transposeShared = NULL, *d_matrix = NULL;
checkCudaErrors(cudaMallocManaged(&d_matrix, sizeof(dtype) * m * n ));
checkCudaErrors(cudaMallocManaged(&transpose, sizeof(dtype) * m * n));
checkCudaErrors(cudaMallocManaged(&transposeShared, sizeof(dtype) * m * n));
checkCudaErrors(cudaMemcpy(d_matrix, matrix, sizeof(dtype) * m * n , cudaMemcpyHostToDevice));
//Global matrix transpose
TIMER_START;
transposeGlobalMatrix<<<grid_size, block_size, sharedMemSize, stream>>>(d_matrix, transpose, m, n);
checkCudaErrors(cudaGetLastError());
checkCudaErrors(cudaDeviceSynchronize());
checkCudaErrors(cudaStreamSynchronize(stream));
TIMER_STOP;
times[1] = TIMER_ELAPSED;
//Shared matrix transpose
TIMER_START;
transposeSharedMatrix<<<grid_size, block_size, sharedMemSize, stream>>>(d_matrix, transposeShared, m, n);
checkCudaErrors(cudaGetLastError());
checkCudaErrors(cudaDeviceSynchronize());
checkCudaErrors(cudaStreamSynchronize(stream));
TIMER_STOP;
times[2] = TIMER_ELAPSED;
//Copy back to host for debug purposes
dtype *h_transpose = NULL, *h_transposeShared = NULL;
h_transpose = (dtype*)malloc(m * n * sizeof(dtype));
h_transposeShared = (dtype*)malloc(m * n * sizeof(dtype));
checkCudaErrors(cudaMemcpy(h_transpose, transpose, m * n * sizeof(dtype), cudaMemcpyDeviceToHost));
checkCudaErrors(cudaMemcpy(h_transposeShared, transposeShared, m * n * sizeof(dtype), cudaMemcpyDeviceToHost));
//Perform a transpose with kernel adapted for sparse matrices
int *d_my_csrRowPtr, *d_my_csrColInd, *d_my_cscRowInd, *d_my_cscColPtr;
dtype *d_my_csrVal, *d_my_cscVal;
checkCudaErrors(cudaMalloc((void **)&d_my_csrRowPtr, (m + 1) * sizeof(int)));
checkCudaErrors(cudaMalloc((void **)&d_my_csrColInd, nnz * sizeof(int)));
checkCudaErrors(cudaMalloc((void **)&d_my_csrVal, nnz * sizeof(dtype)));
checkCudaErrors(cudaMalloc((void **)&d_my_cscRowInd, nnz * sizeof(int)));
checkCudaErrors(cudaMalloc((void **)&d_my_cscColPtr, (n + 1) * sizeof(int)));
checkCudaErrors(cudaMalloc((void **)&d_my_cscVal, nnz * sizeof(dtype)));
checkCudaErrors(cudaMemcpy(d_my_csrRowPtr, h_csrRowPtr, (m + 1) * sizeof(int), cudaMemcpyHostToDevice));
checkCudaErrors(cudaMemcpy(d_my_csrColInd, h_csrColInd, nnz * sizeof(int), cudaMemcpyHostToDevice));
checkCudaErrors(cudaMemcpy(d_my_csrVal, h_csrVal, nnz * sizeof(dtype), cudaMemcpyHostToDevice));
TIMER_START;
sparseMatrixTranspose(m, n, nnz, d_my_csrVal, d_my_csrRowPtr, d_csrColInd, d_my_cscVal, d_my_cscColPtr, d_my_cscRowInd, sharedMemSize, stream);
TIMER_STOP;
times[3] = TIMER_ELAPSED;
//Print effective Bandwidth
printf("==============================================================\n");
printf("STATS of %s\n", path[k]);
printf("Sparse Matrix Transpose With Cusparse Effective Bandwidth(GB/s): %f\n", (2 * nnz * sizeof(dtype)) / (1e9 * times[0]));
printf("Global Matrix Transpose Effective Bandwidth(GB/s): %f\n", (2 * nnz * sizeof(dtype)) / (1e9 * times[1]));
printf("Shared Matrix Transpose Effective Bandwidth(GB/s): %f\n", (2 * nnz * sizeof(dtype)) / (1e9 * times[2]));
printf("My Sparse Matrix Transpose Effective Bandwidth(GB/s): %f\n", (2 * nnz * sizeof(dtype)) / (1e9 * times[3]));
//Produce output files
sprintf(filename, "output/Matrix%d.csv", k);
csvtime[k] = fopen(filename, "w");
if (csvtime[k] == NULL) {
printf("Error opening file!\n");
return 1;
}
printf("%c", filename[k]);
fprintf(csvtime[k], "TheRowsAre,Legend,Cusparse,Global,Shared,MySparse\n");
fprintf(csvtime[k], "Bandwidth,Rows,Columns,NonZeros\n");
for (int i = 0; i < 4; i++) {
fprintf(csvtime[k], "%f,%d,%d,%d\n", (2 * nnz * sizeof(dtype)) / (1e9 * times[i]), m, n, nnz);
}
fclose(csvtime[k]);
//Lines for debug purposes
//printMatrix(matrix, m, n, "Matrix");
//printSparseMatrix(h_csrRowPtr, h_csrColInd, h_csrVal, n, nnz, "Cusparse Transposed Matrix");
//printMatrix(h_transpose, m, n, "Transpose");
//printMatrix(h_transposeShared, m, n, "Transpose Shared");
//printDeviceData<<<1, 1, 0, stream>>>(d_cscColPtr, d_cscRowInd, transposeShared, m, nnz);
//Destroy everything
checkCudaErrors(cudaFree(d_csrRowPtr));
checkCudaErrors(cudaFree(d_csrColInd));
checkCudaErrors(cudaFree(d_csrVal));
checkCudaErrors(cudaFree(d_cscRowInd));
checkCudaErrors(cudaFree(d_cscColPtr));
checkCudaErrors(cudaFree(d_cscVal));
checkCudaErrors(cudaFree(d_matrix));
cusparseDestroy(handle);
checkCudaErrors(cudaFree(transpose));
checkCudaErrors(cudaFree(transposeShared));
checkCudaErrors(cudaStreamDestroy(stream));
checkCudaErrors(cudaFree(d_my_csrRowPtr));
checkCudaErrors(cudaFree(d_my_csrColInd));
checkCudaErrors(cudaFree(d_my_csrVal));
checkCudaErrors(cudaFree(d_my_cscRowInd));
checkCudaErrors(cudaFree(d_my_cscColPtr));
checkCudaErrors(cudaFree(d_my_cscVal));
free(h_csrRowPtr);
free(h_csrColInd);
free(h_csrVal);
free(matrix);
free(h_transpose);
free(h_transposeShared);
free(number);
checkCudaErrors(cudaGetLastError());
checkCudaErrors(cudaDeviceReset());
//Reset times array
for (int i = 0; i < NDEVICE; i++) {
times[i] = 0.0;
}
}
return 0;
}