mirror of
https://github.com/KevinMidboe/linguist.git
synced 2026-01-21 08:45:32 +00:00
samples: add Terra samples
source: https://github.com/zdevito/terra/tree/master/tests
This commit is contained in:
39
samples/Terra/cudaatomic.t
Normal file
39
samples/Terra/cudaatomic.t
Normal file
@@ -0,0 +1,39 @@
|
||||
if not terralib.cudacompile then
|
||||
print("CUDA not enabled, 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
|
||||
|
||||
terralib.includepath = terralib.includepath..";/usr/local/cuda/include"
|
||||
|
||||
|
||||
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)
|
||||
Reference in New Issue
Block a user