本文整理匯總了Golang中github.com/mumax/3/data.Copy函數的典型用法代碼示例。如果您正苦於以下問題:Golang Copy函數的具體用法?Golang Copy怎麽用?Golang Copy使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Copy函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestCpy
func TestCpy(t *testing.T) {
Init(0)
N0, N1, N2 := 2, 4, 32
N := N0 * N1 * N2
mesh := [3]int{N0, N1, N2}
h1 := make([]float32, N)
for i := range h1 {
h1[i] = float32(i)
}
hs := sliceFromList([][]float32{h1}, mesh)
d := NewSlice(1, mesh)
data.Copy(d, hs)
d2 := NewSlice(1, mesh)
data.Copy(d2, d)
h2 := data.NewSlice(1, mesh)
data.Copy(h2, d2)
res := h2.Host()[0]
for i := range res {
if res[i] != h1[i] {
t.Fail()
}
}
}
示例2: Step
func (mini *Minimizer) Step() {
m := M.Buffer()
size := m.Size()
k := mini.k
h := mini.h
// save original magnetization
m0 := cuda.Buffer(3, size)
defer cuda.Recycle(m0)
data.Copy(m0, m)
// make descent
cuda.Minimize(m, m0, k, h)
// calculate new torque for next step
k0 := cuda.Buffer(3, size)
defer cuda.Recycle(k0)
data.Copy(k0, k)
torqueFn(k)
setMaxTorque(k) // report to user
// just to make the following readable
dm := m0
dk := k0
// calculate step difference of m and k
cuda.Madd2(dm, m, m0, 1., -1.)
cuda.Madd2(dk, k, k0, -1., 1.) // reversed due to LLNoPrecess sign
// get maxdiff and add to list
max_dm := cuda.MaxVecNorm(dm)
mini.lastDm.Add(max_dm)
setLastErr(mini.lastDm.Max()) // report maxDm to user as LastErr
// adjust next time step
var nom, div float32
if NSteps%2 == 0 {
nom = cuda.Dot(dm, dm)
div = cuda.Dot(dm, dk)
} else {
nom = cuda.Dot(dm, dk)
div = cuda.Dot(dk, dk)
}
if div != 0. {
mini.h = nom / div
} else { // in case of division by zero
mini.h = 1e-4
}
M.normalize()
// as a convention, time does not advance during relax
NSteps++
}
示例3: shift
func (g *geom) shift(dx int) {
// empty mask, nothing to do
if g == nil || g.buffer.IsNil() {
return
}
// allocated mask: shift
s := g.buffer
s2 := cuda.Buffer(1, g.Mesh().Size())
defer cuda.Recycle(s2)
newv := float32(1) // initially fill edges with 1's
cuda.ShiftX(s2, s, dx, newv, newv)
data.Copy(s, s2)
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
if !g.shape(r[X], r[Y], r[Z]) {
cuda.SetCell(g.buffer, 0, ix, iy, iz, 0) // a bit slowish, but hardly reached
}
}
}
}
}
示例4: SetArray
func (b *magnetization) SetArray(src *data.Slice) {
if src.Size() != b.Mesh().Size() {
src = data.Resample(src, b.Mesh().Size())
}
data.Copy(b.Buffer(), src)
M.normalize()
}
示例5: toGPU
func toGPU(list []float32) *data.Slice {
mesh := [3]int{1, 1, len(list)}
h := sliceFromList([][]float32{list}, mesh)
d := NewSlice(1, mesh)
data.Copy(d, h)
return d
}
示例6: resize
func (m *magnetization) resize() {
backup := m.Buffer().HostCopy()
s2 := Mesh().Size()
resized := data.Resample(backup, s2)
m.buffer_.Free()
m.buffer_ = cuda.NewSlice(VECTOR, s2)
data.Copy(m.buffer_, resized)
}
示例7: Step
// Euler method, can be used as solver.Step.
func (s *BackwardEuler) Step() {
util.AssertMsg(MaxErr > 0, "Backward euler solver requires MaxErr > 0")
t0 := Time
y := M.Buffer()
y0 := cuda.Buffer(VECTOR, y.Size())
defer cuda.Recycle(y0)
data.Copy(y0, y)
dy0 := cuda.Buffer(VECTOR, y.Size())
defer cuda.Recycle(dy0)
if s.dy1 == nil {
s.dy1 = cuda.Buffer(VECTOR, y.Size())
}
dy1 := s.dy1
Dt_si = FixDt
dt := float32(Dt_si * GammaLL)
util.AssertMsg(dt > 0, "Backward Euler solver requires fixed time step > 0")
// Fist guess
Time = t0 + 0.5*Dt_si // 0.5 dt makes it implicit midpoint method
// with temperature, previous torque cannot be used as predictor
if Temp.isZero() {
cuda.Madd2(y, y0, dy1, 1, dt) // predictor euler step with previous torque
M.normalize()
}
torqueFn(dy0)
cuda.Madd2(y, y0, dy0, 1, dt) // y = y0 + dt * dy
M.normalize()
// One iteration
torqueFn(dy1)
cuda.Madd2(y, y0, dy1, 1, dt) // y = y0 + dt * dy1
M.normalize()
Time = t0 + Dt_si
err := cuda.MaxVecDiff(dy0, dy1) * float64(dt)
// adjust next time step
//if err < MaxErr || Dt_si <= MinDt || FixDt != 0 { // mindt check to avoid infinite loop
// step OK
NSteps++
setLastErr(err)
setMaxTorque(dy1)
//} else {
// undo bad step
// util.Assert(FixDt == 0)
// Time = t0
// data.Copy(y, y0)
// NUndone++
//}
}
示例8: shiftMag
func shiftMag(m *data.Slice, dx int) {
m2 := cuda.Buffer(1, m.Size())
defer cuda.Recycle(m2)
for c := 0; c < m.NComp(); c++ {
comp := m.Comp(c)
cuda.ShiftX(m2, comp, dx, float32(ShiftMagL[c]), float32(ShiftMagR[c]))
data.Copy(comp, m2) // str0 ?
}
}
示例9: initFFTKern3D
func (c *MFMConvolution) initFFTKern3D() {
c.fftKernSize = fftR2COutputSizeFloats(c.kernSize)
for i := 0; i < 3; i++ {
zero1_async(c.fftRBuf)
data.Copy(c.fftRBuf, c.kern[i])
c.fwPlan.ExecAsync(c.fftRBuf, c.fftCBuf)
scale := 2 / float32(c.fwPlan.InputLen()) // ??
zero1_async(c.gpuFFTKern[i])
Madd2(c.gpuFFTKern[i], c.gpuFFTKern[i], c.fftCBuf, 0, scale)
}
}
示例10: testConvolution
// Compares FFT-accelerated convolution against brute-force on sparse data.
// This is not really needed but very quickly uncovers newly introduced bugs.
func testConvolution(c *DemagConvolution, PBC [3]int, realKern [3][3]*data.Slice) {
if PBC != [3]int{0, 0, 0} || prod(c.inputSize) > 512*512 {
// the brute-force method does not work for pbc,
// and for large simulations it gets just too slow.
util.Log("skipping convolution self-test")
return
}
//fmt.Print("convolution test ")
inhost := data.NewSlice(3, c.inputSize)
initConvTestInput(inhost.Vectors())
gpu := NewSlice(3, c.inputSize)
defer gpu.Free()
data.Copy(gpu, inhost)
regions := NewBytes(prod(c.inputSize))
defer regions.Free()
Bsat := NewSlice(1, [3]int{1, 1, 256})
defer Bsat.Free()
Memset(Bsat, 1)
BsatLUT := LUTPtr(Bsat.DevPtr(0))
vol := data.NilSlice(1, c.inputSize)
c.Exec(gpu, gpu, vol, BsatLUT, regions)
output := gpu.HostCopy()
brute := data.NewSlice(3, c.inputSize)
bruteConv(inhost.Vectors(), brute.Vectors(), realKern)
a, b := output.Host(), brute.Host()
err := float32(0)
for c := range a {
for i := range a[c] {
if fabs(a[c][i]-b[c][i]) > err {
err = fabs(a[c][i] - b[c][i])
}
}
}
if err > CONV_TOLERANCE {
util.Fatal("convolution self-test tolerance: ", err, " FAIL")
}
}
示例11: testConvolution
// Compares FFT-accelerated convolution against brute-force on sparse data.
// This is not really needed but very quickly uncovers newly introduced bugs.
func testConvolution(c *DemagConvolution, PBC [3]int, realKern [3][3]*data.Slice) {
if PBC != [3]int{0, 0, 0} {
// the brute-force method does not work for pbc.
util.Log("skipping convolution self-test for PBC")
return
}
util.Log("//convolution self-test...")
inhost := data.NewSlice(3, c.inputSize)
initConvTestInput(inhost.Vectors())
gpu := NewSlice(3, c.inputSize)
defer gpu.Free()
data.Copy(gpu, inhost)
Msat := NewSlice(1, [3]int{1, 1, 256})
defer Msat.Free()
Memset(Msat, 1)
vol := data.NilSlice(1, c.inputSize)
c.Exec(gpu, gpu, vol, ToMSlice(Msat))
output := gpu.HostCopy()
brute := data.NewSlice(3, c.inputSize)
bruteConv(inhost.Vectors(), brute.Vectors(), realKern)
a, b := output.Host(), brute.Host()
err := float32(0)
for c := range a {
for i := range a[c] {
if fabs(a[c][i]-b[c][i]) > err {
err = fabs(a[c][i] - b[c][i])
}
}
}
if err > CONV_TOLERANCE {
util.Fatal("convolution self-test tolerance: ", err, " FAIL")
}
}
示例12: setMaskedDemagField
// Sets dst to the demag field, but cells where NoDemagSpins != 0 do not generate nor recieve field.
func setMaskedDemagField(dst *data.Slice) {
// No-demag spins: mask-out geometry with zeros where NoDemagSpins is set,
// so these spins do not generate a field
buf := cuda.Buffer(SCALAR, geometry.Gpu().Size()) // masked-out geometry
defer cuda.Recycle(buf)
// obtain a copy of the geometry mask, which we can overwrite
geom, r := geometry.Slice()
if r {
defer cuda.Recycle(geom)
}
data.Copy(buf, geom)
// mask-out
cuda.ZeroMask(buf, NoDemagSpins.gpuLUT1(), regions.Gpu())
// convolution with masked-out cells.
demagConv().Exec(dst, M.Buffer(), buf, Bsat.gpuLUT1(), regions.Gpu())
// After convolution, mask-out the field in the NoDemagSpins cells
// so they don't feel the field generated by others.
cuda.ZeroMask(dst, NoDemagSpins.gpuLUT1(), regions.Gpu())
}
示例13: GPUCopy
// Returns a copy of in, allocated on GPU.
func GPUCopy(in *data.Slice) *data.Slice {
s := NewSlice(in.NComp(), in.Size())
data.Copy(s, in)
return s
}
示例14: Step
func (rk *RK23) Step() {
m := M.Buffer()
size := m.Size()
if FixDt != 0 {
Dt_si = FixDt
}
// upon resize: remove wrongly sized k1
if rk.k1.Size() != m.Size() {
rk.Free()
}
// first step ever: one-time k1 init and eval
if rk.k1 == nil {
rk.k1 = cuda.NewSlice(3, size)
torqueFn(rk.k1)
}
// FSAL cannot be used with temperature
if !Temp.isZero() {
torqueFn(rk.k1)
}
t0 := Time
// backup magnetization
m0 := cuda.Buffer(3, size)
defer cuda.Recycle(m0)
data.Copy(m0, m)
k2, k3, k4 := cuda.Buffer(3, size), cuda.Buffer(3, size), cuda.Buffer(3, size)
defer cuda.Recycle(k2)
defer cuda.Recycle(k3)
defer cuda.Recycle(k4)
h := float32(Dt_si * GammaLL) // internal time step = Dt * gammaLL
// there is no explicit stage 1: k1 from previous step
// stage 2
Time = t0 + (1./2.)*Dt_si
cuda.Madd2(m, m, rk.k1, 1, (1./2.)*h) // m = m*1 + k1*h/2
M.normalize()
torqueFn(k2)
// stage 3
Time = t0 + (3./4.)*Dt_si
cuda.Madd2(m, m0, k2, 1, (3./4.)*h) // m = m0*1 + k2*3/4
M.normalize()
torqueFn(k3)
// 3rd order solution
madd4(m, m0, rk.k1, k2, k3, 1, (2./9.)*h, (1./3.)*h, (4./9.)*h)
M.normalize()
// error estimate
Time = t0 + Dt_si
torqueFn(k4)
Err := k2 // re-use k2 as error
// difference of 3rd and 2nd order torque without explicitly storing them first
madd4(Err, rk.k1, k2, k3, k4, (7./24.)-(2./9.), (1./4.)-(1./3.), (1./3.)-(4./9.), (1. / 8.))
// determine error
err := cuda.MaxVecNorm(Err) * float64(h)
// adjust next time step
if err < MaxErr || Dt_si <= MinDt || FixDt != 0 { // mindt check to avoid infinite loop
// step OK
setLastErr(err)
setMaxTorque(k4)
NSteps++
Time = t0 + Dt_si
adaptDt(math.Pow(MaxErr/err, 1./3.))
data.Copy(rk.k1, k4) // FSAL
} else {
// undo bad step
//util.Println("Bad step at t=", t0, ", err=", err)
util.Assert(FixDt == 0)
Time = t0
data.Copy(m, m0)
NUndone++
adaptDt(math.Pow(MaxErr/err, 1./4.))
}
}
示例15: download
// rescale and download quantity, save in rescaleBuf
func (ren *render) download() {
InjectAndWait(func() {
if ren.quant == nil { // not yet set, default = m
ren.quant = &M
}
quant := ren.quant
size := quant.Mesh().Size()
// don't slice out of bounds
renderLayer := ren.layer
if renderLayer >= size[Z] {
renderLayer = size[Z] - 1
}
if renderLayer < 0 {
renderLayer = 0
}
// scaling sanity check
if ren.scale < 1 {
ren.scale = 1
}
if ren.scale > maxScale {
ren.scale = maxScale
}
// Don't render too large images or we choke
for size[X]/ren.scale > maxImgSize {
ren.scale++
}
for size[Y]/ren.scale > maxImgSize {
ren.scale++
}
for i := range size {
size[i] /= ren.scale
if size[i] == 0 {
size[i] = 1
}
}
size[Z] = 1 // selects one layer
// make sure buffers are there
if ren.imgBuf.Size() != size {
ren.imgBuf = data.NewSlice(3, size) // always 3-comp, may be re-used
}
buf, r := quant.Slice()
if r {
defer cuda.Recycle(buf)
}
if !buf.GPUAccess() {
ren.imgBuf = Download(quant) // fallback (no zoom)
return
}
// make sure buffers are there (in CUDA context)
if ren.rescaleBuf.Size() != size {
ren.rescaleBuf.Free()
ren.rescaleBuf = cuda.NewSlice(1, size)
}
for c := 0; c < quant.NComp(); c++ {
cuda.Resize(ren.rescaleBuf, buf.Comp(c), renderLayer)
data.Copy(ren.imgBuf.Comp(c), ren.rescaleBuf)
}
})
}