Repository navigation
Expand file tree
/
Copy path01-vector-addition_profile.cu
More file actions
152 lines (125 loc) · 4.35 KB
/
Copy path01-vector-addition_profile.cu
File metadata and controls
152 lines (125 loc) · 4.35 KB
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
/**
* CUDA Basic Example - Vector Addition
*
* This program demonstrates the fundamental concepts of CUDA programming:
* 1. Allocating memory on the GPU
* 2. Copying data between CPU and GPU
* 3. Executing a kernel function on the GPU
* 4. Synchronization between CPU and GPU
* 5. Cleaning up resources
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include <limits.h>
// CUDA kernel function to add two vectors
__global__ void vectorAdd(const float *A, const float *B, float *C, int numElements) {
// Get the unique thread ID, which is the index in the vector
int i = (blockDim.x * blockIdx.x + threadIdx.x) * 2;
// Make sure we don't go out of bounds
if (i < numElements) {
C[i] = A[i] + B[i];
}
}
void vectorAddCPU(const float *A, const float *B, float *C, int numElements) {
for (int i = 0; i < numElements; i++) {
C[i] = A[i] + B[i];
}
}
int parsePositiveInt(const char *text) {
char *end;
errno = 0;
long value = strtol(text, &end, 10);
if (errno != 0 || end == text || *end != '\0' || value <= 0 || value > INT_MAX) {
fprintf(stderr, "Invalid positive integer: %s\n", text);
exit(EXIT_FAILURE);
}
return (int)value;
}
int main(int argc, char **argv) {
if (argc > 3) {
fprintf(stderr, "Usage: %s [threadsPerBlock] [numElements]\n", argv[0]);
return EXIT_FAILURE;
}
// Set threadsPerBlock
int threadsPerBlock = argc > 1 ? parsePositiveInt(argv[1]) : 256;
if (threadsPerBlock > 1024) {
fprintf(stderr, "threadsPerBlock must be between 1 and 1024\n");
return EXIT_FAILURE;
}
// Vector size and memory size
int numElements = 5000000;
size_t size = numElements * sizeof(float);
printf("Vector addition of %d elements\n", numElements);
// Allocate host memory
float *h_A = (float *)malloc(size);
float *h_B = (float *)malloc(size);
float *h_C = (float *)malloc(size);
// Initialize host arrays with random data
for (int i = 0; i < numElements; ++i) {
h_A[i] = rand() / (float)RAND_MAX;
h_B[i] = rand() / (float)RAND_MAX;
}
// Allocate device memory
float *d_A = NULL;
float *d_B = NULL;
float *d_C = NULL;
cudaMalloc((void **)&d_A, size);
cudaMalloc((void **)&d_B, size);
cudaMalloc((void **)&d_C, size);
// Copy data from host to device
cudaMemcpy(d_A, h_A, size, cudaMemcpyHostToDevice);
cudaMemcpy(d_B, h_B, size, cudaMemcpyHostToDevice);
// Launch the CUDA kernel
int blocksPerGrid = (numElements + threadsPerBlock - 1) / threadsPerBlock;
printf("CUDA kernel launch with %d blocks of %d threads\n", blocksPerGrid, threadsPerBlock);
// Measure GPU time
cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);
cudaEventRecord(start);
vectorAdd<<<blocksPerGrid, threadsPerBlock>>>(d_A, d_B, d_C, numElements);
vectorAdd<<<blocksPerGrid, threadsPerBlock>>>(d_A + 1, d_B + 1, d_C + 1, numElements);
cudaEventRecord(stop);
cudaEventSynchronize(stop);
float milliseconds = 0;
cudaEventElapsedTime(&milliseconds, start, stop);
printf("Kernel execution time: %.3f ms\n", milliseconds);
cudaEventDestroy(start);
cudaEventDestroy(stop);
/*
// Measure CPU time
clock_t cpuStart = clock();
vectorAddCPU(h_A, h_B, h_C, numElements);
clock_t cpuStop = clock();
float cpuMilliseconds = 1000.0 * (cpuStop - cpuStart) / CLOCKS_PER_SEC;
printf("CPU execution time: %.3f ms\n", cpuMilliseconds);
*/
// Check for errors in kernel launch
cudaError_t err = cudaGetLastError();
if (err != cudaSuccess) {
fprintf(stderr, "Failed to launch kernel: %s\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
// Copy result back to host
cudaMemcpy(h_C, d_C, size, cudaMemcpyDeviceToHost);
// Verify the result
for (int i = 0; i < numElements; ++i) {
if (fabs(h_A[i] + h_B[i] - h_C[i]) > 1e-5) {
fprintf(stderr, "Result verification failed at element %d!\n", i);
exit(EXIT_FAILURE);
}
}
printf("Test PASSED\n");
// Free device memory
cudaFree(d_A);
cudaFree(d_B);
cudaFree(d_C);
// Free host memory
free(h_A);
free(h_B);
free(h_C);
printf("Done\n");
return 0;
}