-
Notifications
You must be signed in to change notification settings - Fork 0
/
sub.cu
57 lines (45 loc) · 1.39 KB
/
sub.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
// FILE: add.cu
#include <iostream>
#include <cuda_runtime.h>
__global__ void add(int *a, int *b, int *c, int size) {
int index = blockIdx.x * blockDim.x + threadIdx.x;
if (index < size) {
c[index] = a[index] - b[index];
}
}
int main() {
cudaSetDevice(1);
const int arraySize = 1000000; // 增加数组大小
int *a = new int[arraySize];
int *b = new int[arraySize];
int *c = new int[arraySize];
// 初始化数组
for (int i = 0; i < arraySize; i++) {
a[i] = i;
b[i] = i * 2;
}
int *d_a, *d_b, *d_c;
cudaMalloc((void**)&d_a, arraySize * sizeof(int));
cudaMalloc((void**)&d_b, arraySize * sizeof(int));
cudaMalloc((void**)&d_c, arraySize * sizeof(int));
cudaMemcpy(d_a, a, arraySize * sizeof(int), cudaMemcpyHostToDevice);
cudaMemcpy(d_b, b, arraySize * sizeof(int), cudaMemcpyHostToDevice);
int blockSize = 256;
int numBlocks = (arraySize + blockSize - 1) / blockSize;
// 启动 CUDA 内核
add<<<numBlocks, blockSize>>>(d_a, d_b, d_c, arraySize);
cudaMemcpy(c, d_c, arraySize * sizeof(int), cudaMemcpyDeviceToHost);
// 输出部分结果
std::cout << "Result: ";
for (int i = 0; i < 10; i++) {
std::cout << c[i] << " ";
}
std::cout << std::endl;
cudaFree(d_a);
cudaFree(d_b);
cudaFree(d_c);
delete[] a;
delete[] b;
delete[] c;
return 0;
}