-
Notifications
You must be signed in to change notification settings - Fork 5
/
saxpy.cu
90 lines (61 loc) · 1.71 KB
/
saxpy.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
#include <iostream>
// blockDim.x - num threads in a block, .x indicates 1D block labelling
// blockIdx.x - thread index number
// multiplying the above two variables gives start of block
// then add the threadIdx.x offset for the particular thread
__global__ void saxpy_parallel(int n, float a, float *x, float *y)
{
int i = blockIdx.x*blockDim.x + threadIdx.x;
if (i<n) y[i] = a*x[i] + y[i];
}
int main()
{
int N =10;
// allocate vectors on host
int size = N * sizeof(float);
float* h_x = (float*)malloc(size);
float* h_y = (float*)malloc(size);
// allocate device memory
float* d_x; float* d_y;
cudaMalloc((void**) &d_x, size);
cudaMalloc((void**) &d_y, size);
cudaMemcpy(d_x, h_x, size, cudaMemcpyHostToDevice);
cudaMemcpy(d_y, h_y, size, cudaMemcpyHostToDevice);
// put values in h_x and h_y
for (int i = 0;i<=N-1;i++)
{
h_x[i]=2;
h_y[i]=2;
}
for (int i = 0;i<=N-1;i++)
{
std::cout << i << " " << h_y[i] << std::endl;
}
// calculate number of blocks needed for N
int nblocks = (N+255)/256;
// call
saxpy_parallel<<<nblocks,256>>>(N,2.0,d_x,d_y);
// Copy results back from device memory to host memory
// implicty waits for threads to excute
cudaMemcpy(h_y, d_y, size, cudaMemcpyDeviceToHost);
// Check for any CUDA errors
checkCUDAError("cudaMemcpy calls");
for (int i = 0;i<=N-1;i++)
{
std::cout << i << " " << d_y[i] << std::endl;
}
cudaFree(d_x);
cudaFree(d_y);
free(h_x);
free(h_y);
return 0;
}
void checkCUDAError(const char *msg)
{
cudaError_t err = cudaGetLastError();
if( cudaSuccess != err)
{
fprintf(stderr, "Cuda error: %s: %s.\n", msg, cudaGetErrorString( err) );
exit(-1);
}
}