-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_device_alloc.t
57 lines (48 loc) · 1.52 KB
/
test_device_alloc.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
-- SPDX-FileCopyrightText: 2024 René Hiemstra <[email protected]>
-- SPDX-FileCopyrightText: 2024 Torsten Keßler <[email protected]>
--
-- SPDX-License-Identifier: MIT
if not terralib.cudalib then
os.exit(0)
end
local cuda = require("cuda")
local dvector = require("dvector")
local io = terralib.includec("stdio.h")
local Alloc = cuda.DeviceAllocator{Managed = true}
local Vec = dvector.DynamicVector(double)
local terra kernel(x: Vec)
var idx = cuda.threadIdx.x() + cuda.blockIdx.x() * cuda.blockDim.x()
cuda.printf("idx = %d with entry %g\n", idx, x(idx))
for xx in x do
cuda.printf("Iterate over %g\n", xx)
end
if idx < x:size() then
x(idx) = x:size() - idx
end
end
if not __silent__ then --only run when silent mode is off
kernel:printpretty()
-- cudacompile initializes cuda runtime.
-- TODO Is there a better way?
local R = terralib.cudacompile{kernel = kernel}
terra main()
var alloc: Alloc
var x = Vec.from(&alloc, 5, 7, 9)
io.printf("Before:\n")
for xx in x do
io.printf("%g\n", xx)
end
io.printf("\n")
var launch = terralib.CUDAParams {1, 1, 1,
3, 1, 1,
0, nil}
R.kernel(&launch, x)
-- Managed memory is only copied after a synchronization
cuda.StreamSynchronize(nil)
io.printf("After:\n")
for xx in x do
io.printf("%g\n", xx)
end
end
main()
end