-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.cpp
65 lines (50 loc) · 1.37 KB
/
example.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
//
// Created by egi on 1/18/20.
//
#include <cuda_jit.h>
#include <cuda_runtime.h>
#include "map.h"
int main ()
{
jit (saxpy,
{
const size_t tid = threadIdx.x;
if (tid < {{ N }})
{
// Test comment
out[tid] = a * x[tid] + y[tid];
}
}, (int, a), (const int *, x), (const int *, y), (int *, out));
size_t n = 1024;
nlohmann::json data;
data["N"] = n;
int a = 2;
int *x {};
int *y {};
int *out {};
cudaMalloc (&x, n * sizeof (int));
cudaMalloc (&y, n * sizeof (int));
cudaMalloc (&out, n * sizeof (int));
std::unique_ptr<int[]> h_x (new int[n]);
std::unique_ptr<int[]> h_y (new int[n]);
std::unique_ptr<int[]> h_out (new int[n]);
for (size_t i = 0; i < n; i++)
{
h_x[i] = 1;
h_y[i] = 2;
}
cudaMemcpy (x, h_x.get (), n * sizeof (float), cudaMemcpyHostToDevice);
cudaMemcpy (y, h_y.get (), n * sizeof (float), cudaMemcpyHostToDevice);
auto saxpy_kernel = saxpy.compile (data);
saxpy_kernel.launch (1, 1024, a, x, y, out);
cudaMemcpy (h_out.get (), out, n * sizeof (float), cudaMemcpyDeviceToHost);
for (size_t i = 0; i < n; i++)
{
int target_value = a * h_x[i] + h_y[i];
if (target_value != h_out[i])
std::cerr << "Error in out[" << i << "] = " << h_out[i] << " != " << target_value << "\n";
}
cudaFree (x);
cudaFree (y);
cudaFree (out);
}