forked from terralang/terra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blocking2.t
81 lines (71 loc) · 1.58 KB
/
blocking2.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
terra min(a : int, b : int)
return terralib.select(a < b, a, b)
end
function blockedloop(N,blocksizes,bodyfn)
local function generatelevel(n,ii,jj,bb)
if n > #blocksizes then
return bodyfn(ii,jj)
end
local blocksize = blocksizes[n]
return quote
for i = ii,min(ii+bb,N),blocksize do
for j = jj,min(jj+bb,N),blocksize do
[ generatelevel(n+1,i,j,blocksize) ]
end
end
end
end
return generatelevel(1,0,0,N)
end
IO = terralib.includec("stdio.h")
stdlib = terralib.includec("stdlib.h")
terra main()
var a : int[8][8]
var c = 0
var N = 8
[blockedloop(N, {4,2,1}, function(i,j)
return quote
--IO.printf("%d %d\n",i,j)
a[i][j] = c
c = c + 1
end
end)]
for i = 0,N do
for j = 0,N do
IO.printf("%d\t",a[i][j])
end
IO.printf("\n")
end
end
main()
function Image(Spectrum)
local struct ImageImpl {
data : &Spectrum,
N : int
}
terra ImageImpl:init(N : int)
self.data = [&float](stdlib.malloc(N*N*sizeof(Spectrum)))
self.N = N
end
ImageImpl.methods.pixel = macro(function(self,x,y)
return `self.data[x*self.N + y]
end)
return ImageImpl
end
GreyScaleImage = Image(float)
terra laplace(input : &GreyScaleImage, output : &GreyScaleImage)
var newN = input.N - 2 --shrink result since we do not calculate boundaries
output:init(newN);
[blockedloop(newN,{32,1},function(i,j)
return quote
output:pixel(i,j) =
input:pixel(i+0,j+1)
+ input:pixel(i+2,j+1)
+ input:pixel(i+1,j+2)
+ input:pixel(i+1,j+0)
- 4 * input:pixel(i+1,j+1)
end
end)]
end
laplace:compile()
laplace:printpretty()