-
Notifications
You must be signed in to change notification settings - Fork 0
/
addVectors.cpp
84 lines (62 loc) · 2.1 KB
/
addVectors.cpp
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
#include <iostream>
#include <hip/hip_runtime.h>
#include <vector>
#define HIP_CHECK(command){ \
hipError_t status = command ; \
if(status!=hipSuccess){ \
std::cerr << "Error: HIP reports " << hipGetErrorString(status) << std::endl; \
std::abort();}}
__global__ void addVectors(int* a, int* b, int* result, int n){
int tid=threadIdx.x+ blockIdx.x * blockDim.x;
if(tid <n){
result[tid]= a[tid] + b[tid];
}
}
bool check_result(const std::vector<int>& result, const std::vector<int>& A, std::vector<int>& B){
for(int i=0; i<result.size(); i++){
if(result[i] != A[i]+B[i]){
return false;
}
}
return true;
}
int main() {
const int n=10000;
// Define the number of blocks and threads per block
int numBlocks = (n+256-1)/256;
int threadsPerBlock = 256;
// host array A, B and results array C
std::vector<int> hostA(n);
std::vector<int> hostB(n);
std::vector<int> hostC(n);
// host array init
for(int i=0; i<n; i++){
hostA[i]=i;
hostB[i]=i;
}
// Allocate device arrays
int* deviceA;
int* deviceB;
int* deviceC;
HIP_CHECK(hipMalloc(&deviceA, n*sizeof(int)));
HIP_CHECK(hipMalloc(&deviceB, n*sizeof(int)));
HIP_CHECK(hipMalloc(&deviceC, n*sizeof(int)));
HIP_CHECK(hipMemcpy(deviceA, hostA.data(), n*sizeof(int), hipMemcpyHostToDevice));
HIP_CHECK(hipMemcpy(deviceB, hostB.data(), n*sizeof(int), hipMemcpyHostToDevice));
// Launch the HIP kernel
hipLaunchKernelGGL(addVectors, dim3(numBlocks), dim3(threadsPerBlock), 0, 0, deviceA, deviceB, deviceC, n);
addVectors<<< dim3(numBlocks), dim3(threadsPerBlock)>>>(deviceA, deviceB, deviceC, n);
// Wait for the kernel to finish
hipDeviceSynchronize();
HIP_CHECK(hipMemcpy(hostC.data(), deviceC, n*sizeof(n), hipMemcpyDeviceToHost));
if(check_result(hostC, hostA, hostB)){
std::cout << "result match between host and device" << std::endl;
}
else{
std::cout << "result do not match between host and device"<< std::endl;
}
hipFree(deviceA);
hipFree(deviceB);
hipFree(deviceC);
return 0;
}