-
Notifications
You must be signed in to change notification settings - Fork 1
/
regions.go
205 lines (178 loc) · 4.82 KB
/
regions.go
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package engine
import (
"github.com/mumax/3/cuda"
"github.com/mumax/3/data"
"github.com/mumax/3/util"
"log"
)
var regions = Regions{info: Info(1, "regions", "")} // global regions map
const NREGION = 256 // maximum number of regions. (!) duplicated in CUDA
func init() {
DeclFunc("DefRegion", DefRegion, "Define a material region with given index (0-255) and shape")
DeclFunc("DefRegionCell", DefRegionCell, "Set a material region in one cell by index")
DeclROnly("regions", ®ions, "Outputs the region index for each cell")
}
// stores the region index for each cell
type Regions struct {
gpuCache *cuda.Bytes // TODO: rename: buffer
hist []func(x, y, z float64) int // history of region set operations
info
}
func (r *Regions) alloc() {
log.Println("regions.alloc")
mesh := r.Mesh()
r.gpuCache = cuda.NewBytes(mesh.NCell())
DefRegion(0, universe)
}
func (r *Regions) resize() {
newSize := Mesh().Size()
r.gpuCache.Free()
r.gpuCache = cuda.NewBytes(prod(newSize))
log.Println("regions.re-alloc")
for _, f := range r.hist {
r.render(f)
}
}
// Define a region with id (0-255) to be inside the Shape.
func DefRegion(id int, s Shape) {
defRegionId(id)
f := func(x, y, z float64) int {
if s(x, y, z) {
return id
} else {
return -1
}
}
regions.render(f)
regions.hist = append(regions.hist, f)
}
// renders (rasterizes) shape, filling it with region number #id, between x1 and x2
// TODO: a tidbit expensive
func (r *Regions) render(f func(x, y, z float64) int) {
n := Mesh().Size()
l := r.HostList() // need to start from previous state
arr := reshapeBytes(l, r.Mesh().Size())
for iz := 0; iz < n[Z]; iz++ {
for iy := 0; iy < n[Y]; iy++ {
for ix := 0; ix < n[X]; ix++ {
r := Index2Coord(ix, iy, iz)
region := f(r[X], r[Y], r[Z])
if region >= 0 {
arr[iz][iy][ix] = byte(region)
}
}
}
}
log.Print("regions.upload")
r.gpuCache.Upload(l)
}
// get the region for position R based on the history
func (r *Regions) get(R data.Vector) int {
// reverse order, last one set wins.
for i := len(r.hist) - 1; i >= 0; i-- {
f := r.hist[i]
region := f(R[X], R[Y], R[Z])
if region >= 0 {
return region
}
}
return 0
}
func (r *Regions) HostArray() [][][]byte {
return reshapeBytes(r.HostList(), r.Mesh().Size())
}
func (r *Regions) HostList() []byte {
regionsList := make([]byte, r.Mesh().NCell())
regions.gpuCache.Download(regionsList)
return regionsList
}
func DefRegionCell(id int, x, y, z int) {
defRegionId(id)
index := data.Index(Mesh().Size(), z, y, x)
regions.gpuCache.Set(index, byte(id))
}
// Set the region of one cell
// TODO dedup
func (r *Regions) SetCell(ix, iy, iz int, region int) {
size := Mesh().Size()
i := data.Index(size, ix, iy, iz)
r.gpuCache.Set(i, byte(region))
}
func defRegionId(id int) {
if id < 0 || id > NREGION {
util.Fatalf("region id should be 0 -%v, have: %v", NREGION, id)
}
checkMesh()
}
// normalized volume (0..1) of region.
// TODO: a tidbit too expensive
func (r *Regions) volume(region_ int) float64 {
region := byte(region_)
vol := 0
list := r.HostList()
for _, reg := range list {
if reg == region {
vol++
}
}
V := float64(vol) / float64(r.Mesh().NCell())
return V
}
// Get the region data on GPU
func (r *Regions) Gpu() *cuda.Bytes {
return r.gpuCache
}
var unitMap inputParam // unit map used to output regions quantity
func init() {
unitMap.init(1, "unit", "", nil)
for r := 0; r < NREGION; r++ {
unitMap.setRegion(r, []float64{float64(r)})
}
}
// Get returns the regions as a slice of floats, so it can be output.
func (r *Regions) Slice() (*data.Slice, bool) {
buf := cuda.Buffer(1, r.Mesh().Size())
cuda.RegionDecode(buf, unitMap.gpuLUT1(), regions.Gpu())
return buf, true
}
// Re-interpret a contiguous array as a multi-dimensional array of given size.
func reshapeBytes(array []byte, size [3]int) [][][]byte {
Nx, Ny, Nz := size[X], size[Y], size[Z]
util.Argument(Nx*Ny*Nz == len(array))
sliced := make([][][]byte, Nz)
for i := range sliced {
sliced[i] = make([][]byte, Ny)
}
for i := range sliced {
for j := range sliced[i] {
sliced[i][j] = array[(i*Ny+j)*Nx+0 : (i*Ny+j)*Nx+Nx]
}
}
return sliced
}
func (b *Regions) shift(dx int) {
// TODO: return if no regions defined
r1 := b.Gpu()
r2 := cuda.NewBytes(b.Mesh().NCell()) // TODO: somehow recycle
defer r2.Free()
newreg := byte(0) // new region at edge
cuda.ShiftBytes(r2, r1, b.Mesh(), dx, newreg)
r1.Copy(r2)
n := Mesh().Size()
x1, x2 := shiftDirtyRange(dx)
for iz := 0; iz < n[Z]; iz++ {
for iy := 0; iy < n[Y]; iy++ {
for ix := x1; ix < x2; ix++ {
r := Index2Coord(ix, iy, iz) // includes shift
reg := b.get(r)
if reg != 0 {
b.SetCell(ix, iy, iz, reg) // a bit slowish, but hardly reached
}
}
}
}
}
func (r *Regions) Mesh() *data.Mesh { return Mesh() }
func prod(s [3]int) int {
return s[0] * s[1] * s[2]
}