forked from terralang/terra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cudaatomic.t
41 lines (32 loc) · 1.01 KB
/
cudaatomic.t
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
if not terralib.cudacompile then
print("CUDA not enabled, not performing test...")
return
end
if os.getenv("CI") then
print("Running in CI environment without a GPU, not performing test...")
return
end
local tid = cudalib.nvvm_read_ptx_sreg_tid_x--terralib.intrinsic("llvm.nvvm.read.ptx.sreg.tid.x",{} -> int)
foo = terra(result : &int)
var t = tid()
terralib.asm(terralib.types.unit,"red.global.max.u32 [$0], $1;","l,r",true,result,t)
end
local C = terralib.includecstring [[
#include "cuda_runtime.h"
#include <stdlib.h>
#include <stdio.h>
]]
sync = terralib.externfunction("cudaThreadSynchronize", {} -> int)
local R = terralib.cudacompile({ bar = foo },true)
terra doit(N : int)
var data = 0
var location : &int
C.cudaMalloc([&&opaque](&location),sizeof(int))
C.cudaMemcpy(location,&data,sizeof(int),1)
var launch = terralib.CUDAParams { 1,1,1, N,1,1, 0, nil }
R.bar(&launch,location)
var data2 = -1
C.cudaMemcpy(&data2,location,sizeof(int),2)
return data2
end
assert(doit(32) == 31)