-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKernel.cu
More file actions
49 lines (33 loc) · 1.07 KB
/
Kernel.cu
File metadata and controls
49 lines (33 loc) · 1.07 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
#include <iostream>
#include <cuda_runtime.h>
#include <device_launch_parameters.h>
// Preallocated memory for result
__global__ void dot(int a_x, int a_y, int b_x, int b_y, int* result) {
*result = a_x * b_x + a_y * b_y;
}
__global__ void add(int a, int b, int* result) {
*result = a + b;
}
int dotProduct(int a_x, int a_y, int b_x, int b_y) {
int result;
int* d_result = nullptr;
cudaMallocManaged(&d_result, 4);
dot << <1, 256 >> > (a_x, a_y, b_x, b_y, d_result);
//cudaDeviceSynchronize();
cudaMemcpy(&result, d_result, 4, cudaMemcpyDeviceToHost);
//cudaFree(d_result); // Free memory immediately after use
cudaFree(d_result);
return result;
//cudaFree(&result);
}
int addNum(int a, int b) {
int result;
int* d_result = nullptr;
cudaMallocManaged(&d_result, 4);
add << <1, 256 >> > (a,b, d_result);
cudaMemcpy(&result, d_result, 4, cudaMemcpyDeviceToHost);
//cudaDeviceSynchronize();
cudaFree(d_result);
return result;
//cudaFree(&result);
}