-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q9.cu
197 lines (158 loc) · 4.83 KB
/
Q9.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
#define _CRT_SECURE_NO_WARNINGS
#define RUN_COUNT 10
#include <cuda_runtime.h>
#include <stdlib.h>
#include <stdio.h>
#include <omp.h>
#include <time.h>
double starttime, elapsedtime;
double times_sum = 0;
int *allocateVector(int size);
void fillVector(int * v, size_t n);
void printVector(int * v, size_t n);
void serialAdd(int *a, int *b, int *c, int vectorSize);
void cpuAdd(int *a, int *b, int *c, int vectorSize);
cudaError_t gpuAdd(int *h_a, int *h_b, int *h_c, int vectorSize);
int main()
{
#ifndef _OPENMP
printf("OpenMP is not supported.\n");
return 0;
#endif
const int vectorSize = 100000000;
int *a, *b, *c;
srand(time(NULL));
for (int i = 0; i < RUN_COUNT; i++)
{
a = allocateVector(vectorSize);
b = allocateVector(vectorSize);
c = allocateVector(vectorSize);
fillVector(a, vectorSize);
fillVector(b, vectorSize);
// serialAdd(a, b, c, vectorSize);
// cpuAdd(a, b, c, vectorSize);
gpuAdd(a, b, c, vectorSize);
// printVector(c, vectorSize);
free(a);
free(b);
free(c);
// report elapsed time
printf("[-] Time Elapsed: %f Secs\n", elapsedtime);
times_sum += elapsedtime;
}
printf("\n[-] The average running time was: %lf\n", times_sum / RUN_COUNT);
return EXIT_SUCCESS;
}
// Allocates vector in host
int *allocateVector(int size) {
return (int *) malloc(sizeof(int) * size);
}
// Fills a vector with data
void fillVector(int * v, size_t n) {
int i;
#pragma omp parallel for
for (i = 0; i < n; i++) {
v[i] = rand() % 100;
}
}
// Prints a vector to the stdout.
void printVector(int * v, size_t n) {
int i;
printf("[-] Vector elements: ");
for (i = 0; i < n; i++) {
printf("%d, ", v[i]);
}
printf("\b\b \n");
}
void serialAdd(int *a, int *b, int *c, int vectorSize) {
// get starting time
starttime = omp_get_wtime();
int i;
for (i = 0; i < vectorSize; i++) {
c[i] = a[i] + b[i];
}
// get ending time and use it to determine elapsed time
elapsedtime = omp_get_wtime() - starttime;
}
void cpuAdd(int *a, int *b, int *c, int vectorSize) {
// get starting time
starttime = omp_get_wtime();
int i;
#pragma omp parallel for
for (i = 0; i < vectorSize; i++) {
c[i] = a[i] + b[i];
}
// get ending time and use it to determine elapsed time
elapsedtime = omp_get_wtime() - starttime;
}
__global__ void addKernel(int *a, int *b, int *c, int vectorSize, int elements_per_thread) {
int start = (blockIdx.x * blockDim.x + threadIdx.x) * elements_per_thread;
for (int i = start; i - start < elements_per_thread && (i < vectorSize); i++) {
c[i] = a[i] + b[i];
}
}
cudaError_t gpuAdd(int *h_a, int *h_b, int *h_c, int vectorSize) {
cudaError_t cudaStatus;
// Choose which GPU to run on, change this on a multi-GPU system.
cudaStatus = cudaSetDevice(0);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaSetDevice failed! Do you have a CUDA-capable GPU installed?");
goto Error;
}
int *dev_a, *dev_b, *dev_c;
cudaStatus = cudaMalloc(&dev_a, vectorSize * sizeof(int));
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMalloc failed!");
goto Error;
}
cudaStatus = cudaMalloc(&dev_b, vectorSize * sizeof(int));
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMalloc failed!");
goto Error;
}
cudaStatus = cudaMalloc(&dev_c, vectorSize * sizeof(int));
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMalloc failed!");
goto Error;
}
cudaMemcpy(dev_a, h_a, vectorSize * sizeof(int), cudaMemcpyHostToDevice);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMemcpy failed!");
goto Error;
}
cudaMemcpy(dev_b, h_b, vectorSize * sizeof(int), cudaMemcpyHostToDevice);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMemcpy failed!");
goto Error;
}
int ELEMENTS_PER_THREAD = 1;
dim3 NUM_THREADS(512, 1, 1); // Threads per block
dim3 NUM_BLOCKS((vectorSize + (NUM_THREADS.x * ELEMENTS_PER_THREAD) - 1) / (NUM_THREADS.x * ELEMENTS_PER_THREAD), 1, 1);
printf("threads per blocks: %d, blocks: %d\n", NUM_THREADS.x, NUM_BLOCKS.x);
// get starting time
starttime = omp_get_wtime();
// Kernel call
addKernel<<<NUM_BLOCKS, NUM_THREADS>>>(dev_a, dev_b, dev_c, vectorSize, ELEMENTS_PER_THREAD);
cudaDeviceSynchronize();
// get ending time and use it to determine elapsed time
elapsedtime = omp_get_wtime() - starttime;
cudaMemcpy(h_c, dev_c, vectorSize * sizeof(int), cudaMemcpyDeviceToHost);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMemcpy failed!");
goto Error;
}
// Check results
int i;
#pragma omp parallel for
for (i = 0; i < vectorSize; i++) {
if (h_c[i] != h_a[i] + h_b[i]) {
fprintf(stderr, "wrong addition\n");
exit(1);
}
}
Error:
cudaFree(dev_c);
cudaFree(dev_a);
cudaFree(dev_b);
return cudaStatus;
}