/
dot.go
60 lines (49 loc) · 1.08 KB
/
dot.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
package engine
import (
"github.com/mumax/3/cuda"
"github.com/mumax/3/data"
)
func init() {
DeclFunc("DotProduct", DotProduct, "Dot product of two vector quantities")
}
type dotProduct struct {
a, b outputField
}
// DotProduct creates a new quantity that is the dot product of
// quantities a and b. E.g.:
// DotProct(&M, &B_ext)
func DotProduct(a, b outputField) *dotProduct {
return &dotProduct{a, b}
}
func (d *dotProduct) Mesh() *data.Mesh {
return d.a.Mesh()
}
func (d *dotProduct) NComp() int {
return 1
}
func (d *dotProduct) Name() string {
return d.a.Name() + "_dot_" + d.b.Name()
}
func (d *dotProduct) Unit() string {
return d.a.Unit() + d.b.Unit()
}
func (d *dotProduct) Slice() (*data.Slice, bool) {
slice := cuda.Buffer(d.NComp(), d.Mesh().Size())
cuda.Zero(slice)
A, r := d.a.Slice()
if r {
defer cuda.Recycle(A)
}
B, r := d.b.Slice()
if r {
defer cuda.Recycle(B)
}
cuda.AddDotProduct(slice, 1, A, B)
return slice, true
}
func (d *dotProduct) average() []float64 {
return qAverageUniverse(d)
}
func (d *dotProduct) Average() float64 {
return d.average()[0]
}