Newer
Older
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
/*
* A cuda implementation of the Matrix-Vector multiplication
*
* Author: Petros Anastasiadis(panastas@cslab.ece.ntua.gr)
*
* The device code for the GPU implemntations can be found in the 'dmv_gpu.cu' file
*
* For more info about coalesced memmory access and shmem, see https://cvw.cac.cornell.edu/gpu/coalesced
*/
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <cuda_runtime.h>
#include <cublas_v2.h>
#include <cusparse_v2.h>
#include "dmv_gpu.h"
/* Need to include External_Functions for these */
#include "matrix_op.h"
#include "util.h"
#include "input.h"
#include "gpu_util.h"
int main(int argc, char **argv)
{
/* Initializations */
int i, j, n, m;
int *I, *cooCol, n_z, sparse=0;
double *cooVal, timer;
/* File Input to COO */
if (argc < 2) error("Too few Arguments");
else if ( argc == 2) /* ./Program Input_File */
{
if(!mtx_read(&I, &cooCol, &cooVal, &n, &m, &n_z, argv[1])) error("input and/or COO convertion failed");
sparse = 1;
}
else if ( argc == 3) { /*./Program N M */
n = atoi(argv[1]);
m = atoi(argv[2]);
}
else error("Too many Arguments");
int block_size = 256; /* Number of GPU threads per block */
int grid_size = (n-1)/block_size + 1;
size_t shmem_size = 0;
dim3 gpu_block(block_size, 1);
dim3 gpu_grid(grid_size, 1);
/* Allocate space */
double *x = (double *) malloc(m * sizeof(*x));
double *y = (double *) malloc(n * sizeof(*y));
double *M = (double *) malloc(n * m * sizeof(*M));
if( !y || !x || !M ) error("memory allocation failed");
/* Initialize matrices */
if (sparse) {
; //regenerate_matrix_coo(M, I, cooCol, cooVal, n, m, n_z); /* Sparse matrices read from .mtx format */
}
else ser_matrix_init_rand(M, n, m, 1.0); /* Normal matrices generated randomly */
/* Initialize vectors */
vec_init_rand(x, m, 1.0);
vec_init(y, n, 0.0);
/* Initialize cuda variables */
int device_num=0;
cudaGetDeviceCount(&device_num);
if (!device_num) printf("No available Cuda Devices");
else {
cudaSetDevice(0);
printf("Single GPU CUDA Version(N=%d, M=%d): ", n, m);
double *A, * y, *x_c;
/* Initialize Unified memmory visible and accesible from both CPU and GPU */
cudaMallocManaged(&A, m*n * sizeof(double));
cudaMallocManaged(&y, n * sizeof(double));
cudaMallocManaged(&x_c, m * sizeof(double));
cudaDeviceSynchronize();
cudaCheckErrors("Unified Alloc failed");
if ( !A || !y || !x_c) error("unified alloc failed");
for (i = 0; i < m; i++) x_c[i] = x[i];
/* First naive kernel */
for ( i = 0; i < n*m; i++) A[i] = M[i] ;
timer=csecond();
for (j = 0; j < NR_ITER; ++j) {
dmv_gpu_naive<<<gpu_grid,gpu_block,shmem_size>>>(A, x_c, y, m);
cudaDeviceSynchronize();
}
timer = csecond() - timer;
cudaCheckErrors("naive kernel failed");
report_results(timer);
/* Second kernel, using coalesced memmory accesses in the GPU by transposing the matrix. */
printf("Single GPU CUDA Coalesced Version(N=%d, M=%d): ", n, m);
matrix_col_major(M, A, n, m); /* We transpose the matrix to better match the GPU coalesced memmory access logic */
timer=csecond();
for (j = 0; j < NR_ITER; ++j) {
dmv_gpu_coalesced<<<gpu_grid,gpu_block,shmem_size>>>(A, x_c, y, m);
cudaDeviceSynchronize();
}
timer = csecond() - timer;
cudaCheckErrors("coalesced kernel failed");
report_results(timer);
/* Third and final kernel further improves memmory access speed by using block exclusive shmem */
printf("Single GPU CUDA shmem Version(N=%d, M=%d): ", n, m);
shmem_size= block_size*sizeof(float);
timer=csecond();
for (j = 0; j < NR_ITER; ++j) {
dmv_gpu_shmem<<<gpu_grid,gpu_block,shmem_size>>>(A, x_c, y, m);
cudaDeviceSynchronize();
}
timer = csecond() - timer;
cudaCheckErrors("shmem kernel failed");
#ifdef _DEBUG_
/* Output y vector to a file for debugging */
FILE * fp;
char filename[] = "CUDA.debug" ; /* Common directory for all implementations, change if needed */
if(( fp = fopen( filename, "w")) == NULL) error("Output file creation failed\n");
for (i = 0; i < n; ++i) fprintf(fp, "%lf ", y[i]) ;
fclose(fp) ;
#endif
report_results(timer);
}
return 0;
}