-
Notifications
You must be signed in to change notification settings - Fork 0
/
cuda_version.cu
245 lines (190 loc) · 4.8 KB
/
cuda_version.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
float max_norm(float *matrix, int m, int n);
float frob_norm(float *matrix, int m, int n);
float one_norm(float *matrix, int m, int n);
float inf_norm(float *matrix, int m, int n);
// #threads
int block_size = 32;
//================================================================
// One global function for each norm, which will
// be called by curly brackets within a function, say,
// 'calculate_norm_on_gpu'
__global__ void add_arrays_gpu(float *matrix, int m, int n){
// idx is a unique ID for each thread
// ( block index * threads ) + thread index
int idx = blockIdx.x*blockDim.x + threadIdx.x;
int k;
int max = 0;
if(idx<m) // For each row
for(k=0; k<n; k++){
if( fabsf(matrix_d[idx*m + k]) > max ){
max = fabsf(matrix_d[idx*m + k]);
}
}
// Store local max somewhere
}
//================================================================
double max_norm_gpu(int m, int n ){
// On device
float *matrix_d
cudaMalloc ((void **) &matrix_d, sizeof(float)*m*n);
// Copy data from host memory to device
cudaMemcpy(matrix_d, matrix, sizeof(float)*n*m, cudaMemcpyHostToDevice);
// Configuring the grid
dim3 dimBlock(block_size); // One argument = 1D
// N/32 blocks, or one extra with an uneven amount
dim3 dimGrid ( (N/dimBlock.x) + (!(N%dimBlock.x)?0:1) );
// Matrix for local results
float max[m];
// Error Checks
// Call global function
compute_maxnorm_gpu<<dimGrid,dimBlock>>(matrix_d,m,n);
// Run through max[m]
cudaMemcpy(max[idx],max,sizeof(float),cudaMemcpyDeviceToHost);
// return norm
}
int main(int argc, char **argv ){
int c,m,n,i,j;
int tflag=0,mflag=0,nflag=0;
m=n=10;
srand48(123456);
struct timeval start,end;
long long time_elasped;
while((c = getopt(argc,argv,"rtmn")) != -1)
switch(c){
case 'r':
srand48(time(NULL));
break;
case 't':
tflag = 1;
break;
case 'm':
mflag=1;
break;
case 'n':
nflag=1;
break;
case '?':
if(isprint (optopt))
fprintf(stderr, "Unknown option `-%c.\n",optopt);
else
fprintf(stderr,"Unknown option chacracter `\\x%x'.\n",optopt);
return 1;
default:
abort();
}
if(mflag)
m = atoi(argv[optind]);
if(nflag)
n = atoi(argv[optind+1]);
printf("m = %d, n = %d\n",m,n);
//====================================================
// Allocate memory
// On host
float *matrix;
matrix = malloc(n*m*sizeof(float));
//====================================================
// Initialise matrix
for(i=0;i<n*m;i++){
matrix[i] = drand48();
}
float norm;
// Testing time
/*
gettimeofday(&start, NULL);
sleep(5);
gettimeofday(&end, NULL);
printf("%f\n", end.tv_sec - start.tv_sec);
printf("%f\n", end.tv_usec - start.tv_usec);
*/
// Calculating norms
// Measuring time in microseconds
gettimeofday(&start, NULL);
norm = max_norm(matrix,m,n);
gettimeofday(&end, NULL);
printf("Max norm %f\n",norm);
if(tflag){
time_elasped = (end.tv_sec-start.tv_sec)*1000000 + (end.tv_usec-start.tv_usec);
printf("\t%lld microseconds \n",time_elasped);
}
gettimeofday(&start, NULL);
norm = frob_norm(matrix,m,n);
gettimeofday(&end, NULL);
printf("Frobenius norm %f\n",norm);
if(tflag){
time_elasped = (end.tv_sec-start.tv_sec)*1000000 + (end.tv_usec-start.tv_usec);
printf("\t%lld microseconds \n",time_elasped);
}
gettimeofday(&start, NULL);
norm = one_norm(matrix,m,n);
gettimeofday(&end, NULL);
printf("One norm %f\n",norm);
if(tflag){
time_elasped = (end.tv_sec-start.tv_sec)*1000000 + (end.tv_usec-start.tv_usec);
printf("\t%lld microseconds \n",time_elasped);
}
gettimeofday(&start, NULL);
norm = inf_norm(matrix,m,n);
gettimeofday(&end, NULL);
printf("Infinity norm %f\n",norm);
if(tflag){
time_elasped = (end.tv_sec-start.tv_sec)*1000000 + (end.tv_usec-start.tv_usec);
printf("\t%lld microseconds \n",time_elasped);
}
free(matrix);
return 0;
}
float max_norm(float *matrix, int m, int n){
int i, I;
float max = 0.0;
for(i=0;i<(n*m);i++){
if( matrix[i]*matrix[i] > max ){
max = matrix[i]*matrix[i];
I = i;
}
}
return fabsf(matrix[I]);
}
float frob_norm(float *matrix, int m, int n){
int i;
float sum;
for(i=0;i<(m*n);i++)
sum+=matrix[i]*matrix[i];
return sqrt(sum);
}
float one_norm(float *matrix, int m, int n){
// m rows
// n columns
// Access (i.j) entry by matrix[i*n + j]
float sum,max=0;
int i,j;
// Loop over columns
for(j=0;j<n;j++){
sum=0;
// Add all column entries
for(i=0;i<m;i++){
sum+=fabsf(matrix[i*n+j]);
}
if( sum > max )
max = sum;
}
return max;
}
float inf_norm(float *matrix,int m, int n){
float sum,max=0;
int i,j;
// Loop over rows
for(i=0;i<m;i++){
sum=0;
// Add all entries in row
for(j=0;j<n;j++){
sum+=fabsf(matrix[i*n+j]);
}
if( sum > max )
max = sum;
}
return max;
}