-
Notifications
You must be signed in to change notification settings - Fork 1
/
conv_demag.go
198 lines (172 loc) · 6.07 KB
/
conv_demag.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
package cuda
import (
"github.com/mumax/3/cuda/cu"
"github.com/mumax/3/data"
"github.com/mumax/3/util"
)
// Stores the necessary state to perform FFT-accelerated convolution
// with magnetostatic kernel (or other kernel of same symmetry).
type DemagConvolution struct {
inputSize [3]int // 3D size of the input/output data
realKernSize [3]int // Size of kernel and logical FFT size.
fftKernLogicSize [3]int // logic size FFTed kernel, real parts only, we store less
fftRBuf [3]*data.Slice // FFT input buf; 2D: Z shares storage with X.
fftCBuf [3]*data.Slice // FFT output buf; 2D: Z shares storage with X.
kern [3][3]*data.Slice // FFT kernel on device
fwPlan fft3DR2CPlan // Forward FFT (1 component)
bwPlan fft3DC2RPlan // Backward FFT (1 component)
}
// Initializes a convolution to evaluate the demag field for the given mesh geometry.
// Sanity-checked if test == true (slow-ish for large meshes).
func NewDemag(inputSize, PBC [3]int, kernel [3][3]*data.Slice, test bool) *DemagConvolution {
c := new(DemagConvolution)
c.inputSize = inputSize
c.realKernSize = kernel[X][X].Size()
c.init(kernel)
if test {
testConvolution(c, PBC, kernel)
}
return c
}
// Calculate the demag field of m * vol * Bsat, store result in B.
// m: magnetization normalized to unit length
// vol: unitless mask used to scale m's length, may be nil
// Bsat: saturation magnetization in Tesla
// B: resulting demag field, in Tesla
func (c *DemagConvolution) Exec(B, m, vol *data.Slice, Bsat LUTPtr, regions *Bytes) {
util.Argument(B.Size() == c.inputSize && m.Size() == c.inputSize)
if c.is2D() {
c.exec2D(B, m, vol, Bsat, regions)
} else {
c.exec3D(B, m, vol, Bsat, regions)
}
}
func (c *DemagConvolution) exec3D(outp, inp, vol *data.Slice, Bsat LUTPtr, regions *Bytes) {
for i := 0; i < 3; i++ { // FW FFT
c.fwFFT(i, inp, vol, Bsat, regions)
}
// kern mul
kernMulRSymm3D_async(c.fftCBuf,
c.kern[X][X], c.kern[Y][Y], c.kern[Z][Z],
c.kern[Y][Z], c.kern[X][Z], c.kern[X][Y],
c.fftKernLogicSize[X], c.fftKernLogicSize[Y], c.fftKernLogicSize[Z])
for i := 0; i < 3; i++ { // BW FFT
c.bwFFT(i, outp)
}
}
func (c *DemagConvolution) exec2D(outp, inp, vol *data.Slice, Bsat LUTPtr, regions *Bytes) {
// Convolution is separated into
// a 1D convolution for z and a 2D convolution for xy.
// So only 2 FFT buffers are needed at the same time.
Nx, Ny := c.fftKernLogicSize[X], c.fftKernLogicSize[Y]
// Z
c.fwFFT(Z, inp, vol, Bsat, regions)
kernMulRSymm2Dz_async(c.fftCBuf[Z], c.kern[Z][Z], Nx, Ny)
c.bwFFT(Z, outp)
// XY
c.fwFFT(X, inp, vol, Bsat, regions)
c.fwFFT(Y, inp, vol, Bsat, regions)
kernMulRSymm2Dxy_async(c.fftCBuf[X], c.fftCBuf[Y],
c.kern[X][X], c.kern[Y][Y], c.kern[X][Y], Nx, Ny)
c.bwFFT(X, outp)
c.bwFFT(Y, outp)
}
func (c *DemagConvolution) is2D() bool {
return c.inputSize[Z] == 1
}
// zero 1-component slice
func zero1_async(dst *data.Slice) {
cu.MemsetD32Async(cu.DevicePtr(uintptr(dst.DevPtr(0))), 0, int64(dst.Len()), stream0)
}
// forward FFT component i
func (c *DemagConvolution) fwFFT(i int, inp, vol *data.Slice, Bsat LUTPtr, regions *Bytes) {
zero1_async(c.fftRBuf[i])
in := inp.Comp(i)
copyPadMul(c.fftRBuf[i], in, vol, c.realKernSize, c.inputSize, Bsat, regions)
c.fwPlan.ExecAsync(c.fftRBuf[i], c.fftCBuf[i])
}
// backward FFT component i
func (c *DemagConvolution) bwFFT(i int, outp *data.Slice) {
c.bwPlan.ExecAsync(c.fftCBuf[i], c.fftRBuf[i])
out := outp.Comp(i)
copyUnPad(out, c.fftRBuf[i], c.inputSize, c.realKernSize)
}
func (c *DemagConvolution) init(realKern [3][3]*data.Slice) {
// init device buffers
// 2D re-uses fftBuf[X] as fftBuf[Z], 3D needs all 3 fftBufs.
nc := fftR2COutputSizeFloats(c.realKernSize)
c.fftCBuf[X] = NewSlice(1, nc)
c.fftCBuf[Y] = NewSlice(1, nc)
if c.is2D() {
c.fftCBuf[Z] = c.fftCBuf[X]
} else {
c.fftCBuf[Z] = NewSlice(1, nc)
}
c.fftRBuf[X] = NewSlice(1, c.realKernSize)
c.fftRBuf[Y] = NewSlice(1, c.realKernSize)
if c.is2D() {
c.fftRBuf[Z] = c.fftRBuf[X]
} else {
c.fftRBuf[Z] = NewSlice(1, c.realKernSize)
}
// init FFT plans
c.fwPlan = newFFT3DR2C(c.realKernSize[X], c.realKernSize[Y], c.realKernSize[Z])
c.bwPlan = newFFT3DC2R(c.realKernSize[X], c.realKernSize[Y], c.realKernSize[Z])
// init FFT kernel
// logic size of FFT(kernel): store real parts only
c.fftKernLogicSize = fftR2COutputSizeFloats(c.realKernSize)
util.Assert(c.fftKernLogicSize[X]%2 == 0)
c.fftKernLogicSize[X] /= 2
// physical size of FFT(kernel): store only non-redundant part exploiting Y, Z mirror symmetry
// X mirror symmetry already exploited: FFT(kernel) is purely real.
physKSize := [3]int{c.fftKernLogicSize[X], c.fftKernLogicSize[Y]/2 + 1, c.fftKernLogicSize[Z]/2 + 1}
output := c.fftCBuf[0]
input := c.fftRBuf[0]
fftKern := data.NewSlice(1, physKSize)
kfull := data.NewSlice(1, output.Size()) // not yet exploiting symmetry
kfulls := kfull.Scalars()
kCSize := physKSize
kCSize[X] *= 2 // size of kernel after removing Y,Z redundant parts, but still complex
kCmplx := data.NewSlice(1, kCSize) // not yet exploiting X symmetry
kc := kCmplx.Scalars()
for i := 0; i < 3; i++ {
for j := i; j < 3; j++ { // upper triangular part
if realKern[i][j] != nil { // ignore 0's
// FW FFT
data.Copy(input, realKern[i][j])
c.fwPlan.ExecAsync(input, output)
data.Copy(kfull, output)
// extract non-redundant part (Y,Z symmetry)
for iz := 0; iz < kCSize[Z]; iz++ {
for iy := 0; iy < kCSize[Y]; iy++ {
for ix := 0; ix < kCSize[X]; ix++ {
kc[iz][iy][ix] = kfulls[iz][iy][ix]
}
}
}
// extract real parts (X symmetry)
scaleRealParts(fftKern, kCmplx, 1/float32(c.fwPlan.InputLen()))
c.kern[i][j] = GPUCopy(fftKern)
}
}
}
}
func (c *DemagConvolution) Free() {
if c == nil {
return
}
c.inputSize = [3]int{}
c.realKernSize = [3]int{}
for i := 0; i < 3; i++ {
c.fftCBuf[i].Free()
c.fftRBuf[i].Free()
c.fftCBuf[i] = nil
c.fftRBuf[i] = nil
for j := 0; j < 3; j++ {
c.kern[i][j].Free()
c.kern[i][j] = nil
}
c.fwPlan.Free()
c.bwPlan.Free()
}
}