blob: 691b18c95d88e47ae76da5fb22b2dc968fbb501d (
plain)
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
|
#include <cstdio>
void logDevices() {
int count;
if (cudaGetDeviceCount(&count) != cudaSuccess) {
printf("No CUDA devices found\n");
return;
}
printf("CUDA devices found: %d\n", count);
for (int i = 0; i < count; ++i) {
cudaDeviceProp properties;
if (cudaGetDeviceProperties(&properties, i) == cudaSuccess) {
printf("Device [%d]: %s\n", i, properties.name);
}
}
}
__global__ void kernel(int* array, int N) {
for (int i = 0; i < N; ++i) {
array[i] = i;
}
}
int main() {
logDevices();
constexpr int N = 100;
int* host_array = new int[N];
int* device_array = nullptr;
bool success = false;
if (cudaMalloc(&device_array, N * sizeof(int)) != cudaSuccess) {
goto cleanup;
}
kernel<<<1, 1>>>(device_array, N);
if (cudaMemcpy(
host_array, device_array, N * sizeof(int), cudaMemcpyDeviceToHost) !=
cudaSuccess) {
goto cleanup;
}
for (int i = 0; i < N; ++i) {
printf("%d ", host_array[i]);
}
printf("\n");
success = true;
cleanup:
delete[] host_array;
if (device_array != nullptr) {
cudaFree(device_array);
}
return success ? 0 : 1;
}
|