-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid.t
138 lines (116 loc) · 2.72 KB
/
grid.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
local m = require("mem")
local templatize = require("templatize")
local util = require("util")
local Vector = require("vector")
local C = terralib.includecstring [[
#include <stdlib.h>
]]
-- A simple 2D grid of values, like a matrix, but without
-- any linear algebra operations defined.
-- Useful as an interchange format for e.g. Eigen
local Grid2D
Grid2D = templatize(function(valueType)
local struct GridT
{
rows: int,
cols: int,
data: &valueType
}
GridT.metamethods.__typename = function(self)
return string.format("Grid2D(%s)", tostring(valueType))
end
GridT.metamethods.__apply = macro(function(self, i, j)
return `self.data[i*self.cols + j]
end)
terra GridT:__construct() : {}
self.rows = 0
self.cols = 0
self.data = nil
end
terra GridT:__construct(r: int, c: int) : {}
self:__construct()
self.rows = r
self.cols = c
if r*c > 0 then
self.data = [&valueType](C.malloc(r*c*sizeof(valueType)))
for i=0,r do
for j=0,c do
m.init(self(i,j))
end
end
end
end
terra GridT:__construct(r: int, c: int, fillVal: valueType) : {}
self:__construct(r, c)
for i=0,r do
for j=0,c do
self(i,j) = m.copy(fillVal)
end
end
end
terra GridT:__copy(other: &GridT)
self.rows = other.rows
self.cols = other.cols
self.data = [&valueType](C.malloc(self.rows*self.cols*sizeof(valueType)))
for i=0,self.rows do
for j=0,self.cols do
self(i,j) = m.copy(other(i,j))
end
end
end
GridT.__templatecopy = templatize(function(valueType2)
return terra(self: &GridT, other: &Grid2D(valueType2))
self.rows = other.rows
self.cols = other.cols
self.data = [&valueType](C.malloc(self.rows*self.cols*sizeof(valueType)))
for i=0,self.rows do
for j=0,self.cols do
self(i,j) = [m.templatecopy(valueType)](other(i,j))
end
end
end
end)
terra GridT:__destruct()
if self.data ~= nil then
for i=0,self.rows do
for j=0,self.cols do
m.destruct(self(i,j))
end
end
C.free(self.data)
end
end
-- Completely wipes all the stored data
terra GridT:resize(r: int, c: int)
if r ~= self.rows or c ~= self.cols then
self:__destruct()
self:__construct(r, c)
end
end
terra GridT:mult(invec: &Vector(valueType), outvec: &Vector(valueType))
outvec:resize(self.rows)
for i=0,self.rows do
var sum = valueType(0.0)
for j=0,self.cols do
sum = sum + self(i,j)*invec(j)
end
outvec(i) = sum
end
end
terra GridT:transposeMult(invec: &Vector(valueType), outvec: &Vector(valueType))
outvec:resize(self.cols)
for i=0,self.cols do
var sum = valueType(0.0)
for j=0,self.rows do
sum = sum + self(j,i)*invec(j)
end
outvec(i) = sum
end
end
m.addConstructors(GridT)
return GridT
end)
return
{
Grid2D = Grid2D
}