// shift dst by shx cells (positive or negative) along X-axis. // new edge value is clampL at left edge or clampR at right edge. func ShiftX(dst, src *data.Slice, shiftX int, clampL, clampR float32) { util.Argument(dst.NComp() == 1 && src.NComp() == 1) util.Assert(dst.Len() == src.Len()) N := dst.Size() cfg := make3DConf(N) k_shiftx_async(dst.DevPtr(0), src.DevPtr(0), N[X], N[Y], N[Z], shiftX, clampL, clampR, cfg) }
// dst += LUT[region], for vectors. Used to add terms to excitation. func RegionAddV(dst *data.Slice, lut LUTPtrs, regions *Bytes) { util.Argument(dst.NComp() == 3) N := dst.Len() cfg := make1DConf(N) k_regionaddv_async(dst.DevPtr(X), dst.DevPtr(Y), dst.DevPtr(Z), lut[X], lut[Y], lut[Z], regions.Ptr, N, cfg) }
// Sets vector dst to zero where mask != 0. func ZeroMask(dst *data.Slice, mask LUTPtr, regions *Bytes) { N := dst.Len() cfg := make1DConf(N) for c := 0; c < dst.NComp(); c++ { k_zeromask_async(dst.DevPtr(c), unsafe.Pointer(mask), regions.Ptr, N, cfg) } }
// Landau-Lifshitz torque with precession disabled. // Used by engine.Relax(). func LLNoPrecess(torque, m, B *data.Slice) { N := torque.Len() cfg := make1DConf(N) k_llnoprecess_async(torque.DevPtr(X), torque.DevPtr(Y), torque.DevPtr(Z), m.DevPtr(X), m.DevPtr(Y), m.DevPtr(Z), B.DevPtr(X), B.DevPtr(Y), B.DevPtr(Z), N, cfg) }
// Set Bth to thermal noise (Brown). // see temperature.cu func SetTemperature(Bth, noise *data.Slice, temp_red LUTPtr, k2mu0_VgammaDt float64, regions *Bytes) { util.Argument(Bth.NComp() == 1 && noise.NComp() == 1) N := Bth.Len() cfg := make1DConf(N) k_settemperature_async(Bth.DevPtr(0), noise.DevPtr(0), float32(k2mu0_VgammaDt), unsafe.Pointer(temp_red), regions.Ptr, N, cfg) }
// m = 1 / (4 + τ²(m x H)²) [{4 - τ²(m x H)²} m - 4τ(m x m x H)] // note: torque from LLNoPrecess has negative sign func Minimize(m, m0, torque *data.Slice, dt float32) { N := m.Len() cfg := make1DConf(N) k_minimize_async(m.DevPtr(X), m.DevPtr(Y), m.DevPtr(Z), m0.DevPtr(X), m0.DevPtr(Y), m0.DevPtr(Z), torque.DevPtr(X), torque.DevPtr(Y), torque.DevPtr(Z), dt, N, cfg) }
// select the part of src within the specified region, set 0's everywhere else. func RegionSelect(dst, src *data.Slice, regions *Bytes, region byte) { util.Argument(dst.NComp() == src.NComp()) N := dst.Len() cfg := make1DConf(N) for c := 0; c < dst.NComp(); c++ { k_regionselect_async(dst.DevPtr(c), src.DevPtr(c), regions.Ptr, region, N, cfg) } }
// Landau-Lifshitz torque divided by gamma0: // - 1/(1+α²) [ m x B + α m x (m x B) ] // torque in Tesla // m normalized // B in Tesla // see lltorque.cu func LLTorque(torque, m, B *data.Slice, alpha LUTPtr, regions *Bytes) { N := torque.Len() cfg := make1DConf(N) k_lltorque_async(torque.DevPtr(X), torque.DevPtr(Y), torque.DevPtr(Z), m.DevPtr(X), m.DevPtr(Y), m.DevPtr(Z), B.DevPtr(X), B.DevPtr(Y), B.DevPtr(Z), unsafe.Pointer(alpha), regions.Ptr, N, cfg) }
// Landau-Lifshitz torque divided by gamma0: // - 1/(1+α²) [ m x B + α m x (m x B) ] // torque in Tesla // m normalized // B in Tesla // see lltorque.cu func LLTorque(torque, m, B *data.Slice, alpha MSlice) { N := torque.Len() cfg := make1DConf(N) k_lltorque2_async(torque.DevPtr(X), torque.DevPtr(Y), torque.DevPtr(Z), m.DevPtr(X), m.DevPtr(Y), m.DevPtr(Z), B.DevPtr(X), B.DevPtr(Y), B.DevPtr(Z), alpha.DevPtr(0), alpha.Mul(0), N, cfg) }
// Copies src (larger) into dst (smaller). // Used to extract demag field after convolution on padded m. func copyUnPad(dst, src *data.Slice, dstsize, srcsize [3]int) { util.Argument(dst.NComp() == 1 && src.NComp() == 1) util.Argument(dst.Len() == prod(dstsize) && src.Len() == prod(srcsize)) cfg := make3DConf(dstsize) k_copyunpad_async(dst.DevPtr(0), dstsize[X], dstsize[Y], dstsize[Z], src.DevPtr(0), srcsize[X], srcsize[Y], srcsize[Z], cfg) }
// Copies src into dst, which is larger, and multiplies by vol*Bsat. // The remainder of dst is not filled with zeros. // Used to zero-pad magnetization before convolution and in the meanwhile multiply m by its length. func copyPadMul(dst, src, vol *data.Slice, dstsize, srcsize [3]int, Bsat LUTPtr, regions *Bytes) { util.Argument(dst.NComp() == 1 && src.NComp() == 1) util.Assert(dst.Len() == prod(dstsize) && src.Len() == prod(srcsize)) cfg := make3DConf(srcsize) k_copypadmul_async(dst.DevPtr(0), dstsize[X], dstsize[Y], dstsize[Z], src.DevPtr(0), vol.DevPtr(0), srcsize[X], srcsize[Y], srcsize[Z], unsafe.Pointer(Bsat), regions.Ptr, cfg) }
// Dot product. func Dot(a, b *data.Slice) float32 { nComp := a.NComp() util.Argument(nComp == b.NComp()) out := reduceBuf(0) // not async over components for c := 0; c < nComp; c++ { k_reducedot_async(a.DevPtr(c), b.DevPtr(c), out, 0, a.Len(), reducecfg) // all components add to out } return copyback(out) }
// Copies src into dst, which is larger, and multiplies by vol*Bsat. // The remainder of dst is not filled with zeros. // Used to zero-pad magnetization before convolution and in the meanwhile multiply m by its length. func copyPadMul(dst, src, vol *data.Slice, dstsize, srcsize [3]int, Msat MSlice) { util.Argument(dst.NComp() == 1 && src.NComp() == 1) util.Assert(dst.Len() == prod(dstsize) && src.Len() == prod(srcsize)) cfg := make3DConf(srcsize) k_copypadmul2_async(dst.DevPtr(0), dstsize[X], dstsize[Y], dstsize[Z], src.DevPtr(0), srcsize[X], srcsize[Y], srcsize[Z], Msat.DevPtr(0), Msat.Mul(0), vol.DevPtr(0), cfg) }
// dst += prefactor * dot(a, b), as used for energy density func AddDotProduct(dst *data.Slice, prefactor float32, a, b *data.Slice) { util.Argument(dst.NComp() == 1 && a.NComp() == 3 && b.NComp() == 3) util.Argument(dst.Len() == a.Len() && dst.Len() == b.Len()) N := dst.Len() cfg := make1DConf(N) k_dotproduct_async(dst.DevPtr(0), prefactor, a.DevPtr(X), a.DevPtr(Y), a.DevPtr(Z), b.DevPtr(X), b.DevPtr(Y), b.DevPtr(Z), N, cfg) }
// Add uniaxial magnetocrystalline anisotropy field to Beff. // see uniaxialanisotropy.cu func AddUniaxialAnisotropy(Beff, m *data.Slice, k1_red, k2_red LUTPtr, u LUTPtrs, regions *Bytes) { util.Argument(Beff.Size() == m.Size()) N := Beff.Len() cfg := make1DConf(N) k_adduniaxialanisotropy_async(Beff.DevPtr(X), Beff.DevPtr(Y), Beff.DevPtr(Z), m.DevPtr(X), m.DevPtr(Y), m.DevPtr(Z), unsafe.Pointer(k1_red), unsafe.Pointer(k2_red), u[X], u[Y], u[Z], regions.Ptr, N, cfg) }
// Set Bth to thermal noise (Brown). // see temperature.cu func SetTemperature(Bth, noise *data.Slice, k2mu0_Mu0VgammaDt float64, Msat, Temp, Alpha MSlice) { util.Argument(Bth.NComp() == 1 && noise.NComp() == 1) N := Bth.Len() cfg := make1DConf(N) k_settemperature2_async(Bth.DevPtr(0), noise.DevPtr(0), float32(k2mu0_Mu0VgammaDt), Msat.DevPtr(0), Msat.Mul(0), Temp.DevPtr(0), Temp.Mul(0), Alpha.DevPtr(0), Alpha.Mul(0), N, cfg) }
// Add Slonczewski ST torque to torque (Tesla). // see slonczewski.cu func AddSlonczewskiTorque(torque, m, J, fixedP *data.Slice, Msat, alpha, pol, λ, ε_prime LUTPtr, regions *Bytes, mesh *data.Mesh) { N := torque.Len() cfg := make1DConf(N) thickness := float32(mesh.WorldSize()[Z]) k_addslonczewskitorque_async(torque.DevPtr(X), torque.DevPtr(Y), torque.DevPtr(Z), m.DevPtr(X), m.DevPtr(Y), m.DevPtr(Z), J.DevPtr(Z), fixedP.DevPtr(X), fixedP.DevPtr(Y), fixedP.DevPtr(Z), unsafe.Pointer(Msat), unsafe.Pointer(alpha), thickness, unsafe.Pointer(pol), unsafe.Pointer(λ), unsafe.Pointer(ε_prime), regions.Ptr, N, cfg) }
// Memset sets the Slice's components to the specified values. // To be carefully used on unified slice (need sync) func Memset(s *data.Slice, val ...float32) { if Synchronous { // debug Sync() timer.Start("memset") } util.Argument(len(val) == s.NComp()) for c, v := range val { cu.MemsetD32Async(cu.DevicePtr(uintptr(s.DevPtr(c))), math.Float32bits(v), int64(s.Len()), stream0) } if Synchronous { //debug Sync() timer.Stop("memset") } }
// Adds cubic anisotropy field to Beff. // see cubicanisotropy.cu func AddCubicAnisotropy(Beff, m *data.Slice, k1_red, k2_red, k3_red LUTPtr, c1, c2 LUTPtrs, regions *Bytes) { util.Argument(Beff.Size() == m.Size()) N := Beff.Len() cfg := make1DConf(N) k_addcubicanisotropy_async( Beff.DevPtr(X), Beff.DevPtr(Y), Beff.DevPtr(Z), m.DevPtr(X), m.DevPtr(Y), m.DevPtr(Z), unsafe.Pointer(k1_red), unsafe.Pointer(k2_red), unsafe.Pointer(k3_red), c1[X], c1[Y], c1[Z], c2[X], c2[Y], c2[Z], regions.Ptr, N, cfg) }
// Returns a buffer obtained from GetBuffer to the pool. func Recycle(s *data.Slice) { if Synchronous { Sync() } N := s.Len() pool := buf_pool[N] // put each component buffer back on the stack for i := 0; i < s.NComp(); i++ { ptr := s.DevPtr(i) if _, ok := buf_check[ptr]; !ok { log.Panic("recyle: was not obtained with getbuffer") } pool = append(pool, ptr) } s.Disable() // make it unusable, protect against accidental use after recycle buf_pool[N] = pool }
// multiply-add: dst[i] = src1[i] * factor1 + src2[i] * factor2 + src3 * factor3 func Madd3(dst, src1, src2, src3 *data.Slice, factor1, factor2, factor3 float32) { N := dst.Len() nComp := dst.NComp() util.Assert(src1.Len() == N && src2.Len() == N && src3.Len() == N) util.Assert(src1.NComp() == nComp && src2.NComp() == nComp && src3.NComp() == nComp) cfg := make1DConf(N) for c := 0; c < nComp; c++ { k_madd3_async(dst.DevPtr(c), src1.DevPtr(c), factor1, src2.DevPtr(c), factor2, src3.DevPtr(c), factor3, N, cfg) } }
// Add uniaxial magnetocrystalline anisotropy field to Beff. // see uniaxialanisotropy.cu func AddUniaxialAnisotropy2(Beff, m *data.Slice, Msat, k1, k2, u MSlice) { util.Argument(Beff.Size() == m.Size()) checkSize(Beff, m, k1, k2, u, Msat) N := Beff.Len() cfg := make1DConf(N) k_adduniaxialanisotropy2_async( Beff.DevPtr(X), Beff.DevPtr(Y), Beff.DevPtr(Z), m.DevPtr(X), m.DevPtr(Y), m.DevPtr(Z), Msat.DevPtr(0), Msat.Mul(0), k1.DevPtr(0), k1.Mul(0), k2.DevPtr(0), k2.Mul(0), u.DevPtr(X), u.Mul(X), u.DevPtr(Y), u.Mul(Y), u.DevPtr(Z), u.Mul(Z), N, cfg) }
// Add uniaxial magnetocrystalline anisotropy field to Beff. // see uniaxialanisotropy.cu func AddCubicAnisotropy2(Beff, m *data.Slice, Msat, k1, k2, k3, c1, c2 MSlice) { util.Argument(Beff.Size() == m.Size()) N := Beff.Len() cfg := make1DConf(N) k_addcubicanisotropy2_async( Beff.DevPtr(X), Beff.DevPtr(Y), Beff.DevPtr(Z), m.DevPtr(X), m.DevPtr(Y), m.DevPtr(Z), Msat.DevPtr(0), Msat.Mul(0), k1.DevPtr(0), k1.Mul(0), k2.DevPtr(0), k2.Mul(0), k3.DevPtr(0), k3.Mul(0), c1.DevPtr(X), c1.Mul(X), c1.DevPtr(Y), c1.Mul(Y), c1.DevPtr(Z), c1.Mul(Z), c2.DevPtr(X), c2.Mul(X), c2.DevPtr(Y), c2.Mul(Y), c2.DevPtr(Z), c2.Mul(Z), N, cfg) }
// Add Slonczewski ST torque to torque (Tesla). // see slonczewski.cu func AddSlonczewskiTorque2(torque, m *data.Slice, Msat, J, fixedP, alpha, pol, λ, ε_prime MSlice, mesh *data.Mesh) { N := torque.Len() cfg := make1DConf(N) flt := float32(mesh.WorldSize()[Z]) k_addslonczewskitorque2_async( torque.DevPtr(X), torque.DevPtr(Y), torque.DevPtr(Z), m.DevPtr(X), m.DevPtr(Y), m.DevPtr(Z), Msat.DevPtr(0), Msat.Mul(0), J.DevPtr(Z), J.Mul(Z), fixedP.DevPtr(X), fixedP.Mul(X), fixedP.DevPtr(Y), fixedP.Mul(Y), fixedP.DevPtr(Z), fixedP.Mul(Z), alpha.DevPtr(0), alpha.Mul(0), pol.DevPtr(0), pol.Mul(0), λ.DevPtr(0), λ.Mul(0), ε_prime.DevPtr(0), ε_prime.Mul(0), unsafe.Pointer(uintptr(0)), flt, N, cfg) }
// Maximum of the norms of the difference between all vectors (x1,y1,z1) and (x2,y2,z2) // (dx, dy, dz) = (x1, y1, z1) - (x2, y2, z2) // max_i sqrt( dx[i]*dx[i] + dy[i]*dy[i] + dz[i]*dz[i] ) func MaxVecDiff(x, y *data.Slice) float64 { util.Argument(x.Len() == y.Len()) out := reduceBuf(0) k_reducemaxvecdiff2_async(x.DevPtr(0), x.DevPtr(1), x.DevPtr(2), y.DevPtr(0), y.DevPtr(1), y.DevPtr(2), out, 0, x.Len(), reducecfg) return math.Sqrt(float64(copyback(out))) }
// multiply: dst[i] = a[i] * b[i] // a and b must have the same number of components func Mul(dst, a, b *data.Slice) { N := dst.Len() nComp := dst.NComp() util.Assert(a.Len() == N && a.NComp() == nComp && b.Len() == N && b.NComp() == nComp) cfg := make1DConf(N) for c := 0; c < nComp; c++ { k_mul_async(dst.DevPtr(c), a.DevPtr(c), b.DevPtr(c), N, cfg) } }
// Execute the FFT plan, asynchronous. // src and dst are 3D arrays stored 1D arrays. func (p *fft3DC2RPlan) ExecAsync(src, dst *data.Slice) { if Synchronous { Sync() timer.Start("fft") } oksrclen := p.InputLenFloats() if src.Len() != oksrclen { panic(fmt.Errorf("fft size mismatch: expecting src len %v, got %v", oksrclen, src.Len())) } okdstlen := p.OutputLenFloats() if dst.Len() != okdstlen { panic(fmt.Errorf("fft size mismatch: expecting dst len %v, got %v", okdstlen, dst.Len())) } p.handle.ExecC2R(cu.DevicePtr(uintptr(src.DevPtr(0))), cu.DevicePtr(uintptr(dst.DevPtr(0)))) if Synchronous { Sync() timer.Stop("fft") } }
// Execute the FFT plan, asynchronous. // src and dst are 3D arrays stored 1D arrays. func (p *fft3DR2CPlan) ExecAsync(src, dst *data.Slice) { if Synchronous { Sync() timer.Start("fft") } util.Argument(src.NComp() == 1 && dst.NComp() == 1) oksrclen := p.InputLen() if src.Len() != oksrclen { log.Panicf("fft size mismatch: expecting src len %v, got %v", oksrclen, src.Len()) } okdstlen := p.OutputLen() if dst.Len() != okdstlen { log.Panicf("fft size mismatch: expecting dst len %v, got %v", okdstlen, dst.Len()) } p.handle.ExecR2C(cu.DevicePtr(uintptr(src.DevPtr(0))), cu.DevicePtr(uintptr(dst.DevPtr(0)))) if Synchronous { Sync() timer.Stop("fft") } }
// Extract real parts, copy them from src to dst. // In the meanwhile, check if imaginary parts are nearly zero // and scale the kernel to compensate for unnormalized FFTs. // scale = 1/N, with N the FFT logical size. func scaleRealParts(dst, src *data.Slice, scale float32) { util.Argument(2*dst.Len() == src.Len()) util.Argument(dst.NComp() == 1 && src.NComp() == 1) srcList := src.Host()[0] dstList := dst.Host()[0] // Normally, the FFT'ed kernel is purely real because of symmetry, // so we only store the real parts... maximg := float32(0.) for i := 0; i < src.Len()/2; i++ { dstList[i] = srcList[2*i] * scale if fabs(srcList[2*i+1]) > maximg { maximg = fabs(srcList[2*i+1]) } } maximg *= float32(math.Sqrt(float64(scale))) // after 1 FFT, normalization is sqrt(N) // ...however, we check that the imaginary parts are nearly zero, // just to be sure we did not make a mistake during kernel creation. if maximg > FFT_IMAG_TOLERANCE { log.Fatalf("FFT kernel imaginary part: %v\n", maximg) } }
// Normalize vec to unit length, unless length or vol are zero. func Normalize(vec, vol *data.Slice) { util.Argument(vol == nil || vol.NComp() == 1) N := vec.Len() cfg := make1DConf(N) k_normalize_async(vec.DevPtr(X), vec.DevPtr(Y), vec.DevPtr(Z), vol.DevPtr(0), N, cfg) }