-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cu
64 lines (51 loc) · 1.34 KB
/
main.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
#include <stdio.h>
#include <math.h>
__global__ void kernel(double *n, bool *result, double *count) {
*result = true;
while (*n != 1)
{
// perform modulo
// a - (a / b) * b => a % b
if ((*n - (*n / 2) * 2) == 0)
{
*n = *n / 2;
}
else
{
*n = *n * 3;
*n = *n + 1;
}
*count = *count + 1;
}
}
int main() {
// Host copies
double n;
bool result;
double count;
// CUDA Device copies
double *n_ptr;
bool *result_ptr;
double *count_ptr;
cudaMalloc(&n_ptr, sizeof(double));
cudaMalloc(&result_ptr, sizeof(bool));
cudaMalloc(&count_ptr, sizeof(double));
// nums to add
n = 4;
// copy to device
cudaMemcpy(n_ptr, &n, sizeof(double1), cudaMemcpyHostToDevice);
// start
kernel<<<1, 1>>>(n_ptr, result_ptr, count_ptr);
// copy result from device
cudaMemcpy(&result, result_ptr, sizeof(bool), cudaMemcpyDeviceToHost);
cudaMemcpy(&count, count_ptr, sizeof(double), cudaMemcpyDeviceToHost);
printf("%lf: %d | with %lf operations\n", n, result, count);
cudaDeviceSynchronize();
cudaError_t error = cudaGetLastError();
if(error != cudaSuccess)
{
fprintf(stderr,"ERROR: %s\n", cudaGetErrorString(error) );
exit(-1);
}
return 0;
}