Skip to content

Commit

Permalink
Cleaned up pointer semantics (#55)
Browse files Browse the repository at this point in the history
* Cleaned up pointer semantics so that all pointer arithmetics are correctly done now

* Fixed travis yml
  • Loading branch information
chewxy authored Dec 8, 2019
1 parent cbf3fe5 commit ef164d5
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 30 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ go:
- 1.10.x
- 1.11.x
- 1.12.x
- 1.13.x
- tip

env:
Expand Down
12 changes: 5 additions & 7 deletions array.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ func arrayFromSlice(x interface{}) array {
elT := xT.Elem()

xV := reflect.ValueOf(x)
ptr := xV.Pointer()
uptr := unsafe.Pointer(ptr)
uptr := unsafe.Pointer(xV.Pointer())

return array{
Header: storage.Header{
Expand All @@ -71,8 +70,8 @@ func (a *array) fromSlice(x interface{}) {
}
elT := xT.Elem()
xV := reflect.ValueOf(x)
ptr := xV.Pointer()
uptr := unsafe.Pointer(ptr)
uptr := unsafe.Pointer(xV.Pointer())

a.Ptr = uptr
a.L = xV.Len()
a.C = xV.Cap()
Expand Down Expand Up @@ -127,7 +126,6 @@ func (a array) byteSlice() []byte {
// sliceInto creates a slice. Instead of returning an array, which would cause a lot of reallocations, sliceInto expects a array to
// already have been created. This allows repetitive actions to be done without having to have many pointless allocation
func (a *array) sliceInto(i, j int, res *array) {
base := uintptr(a.Ptr)
c := a.C

if i < 0 || j < i || j > c {
Expand All @@ -138,10 +136,10 @@ func (a *array) sliceInto(i, j int, res *array) {
res.C = c - i

if c-1 > 0 {
res.Ptr = storage.ElementAt(i, unsafe.Pointer(base), a.t.Size())
res.Ptr = storage.ElementAt(i, a.Ptr, a.t.Size())
} else {
// don't advance pointer
res.Ptr = unsafe.Pointer(base)
res.Ptr = a.Ptr
}
res.fix()
}
Expand Down
7 changes: 3 additions & 4 deletions array_getset.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions consopt.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ func FromScalar(x interface{}, argMask ...[]bool) ConsOpt {
xv := reflect.New(xt)
xvi := reflect.Indirect(xv)
xvi.Set(reflect.ValueOf(x))
ptr := xv.Pointer()
uptr := unsafe.Pointer(ptr)
uptr := unsafe.Pointer(xv.Pointer())

tt.array.Ptr = uptr
tt.array.L = 1
Expand Down
13 changes: 6 additions & 7 deletions genlib2/array_getset.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ func (a *array) Get(i int) interface{} {
{{end -}}
{{end -}}
default:
at := uintptr(a.Ptr) + uintptr(i) * a.t.Size()
val := reflect.NewAt(a.t.Type, unsafe.Pointer(at))
at := unsafe.Pointer(uintptr(a.Ptr) + uintptr(i) * a.t.Size())
val := reflect.NewAt(a.t.Type, at)
val = reflect.Indirect(val)
return val.Interface()
}
}
`
const setRaw = `// Set sets the value of the underlying array at the index i.
const setRaw = `// Set sets the value of the underlying array at the index i.
func (a *array) Set(i int, x interface{}) {
switch a.t.Kind() {
{{range .Kinds -}}
Expand All @@ -47,8 +47,7 @@ func (a *array) Set(i int, x interface{}) {
{{end -}}
default:
xv := reflect.ValueOf(x)
ptr := uintptr(a.Ptr)
want := ptr + uintptr(i)*a.t.Size()
want := unsafe.Pointer(uintptr(a.Ptr) + uintptr(i)*a.t.Size())
val := reflect.NewAt(a.t.Type, unsafe.Pointer(want))
val = reflect.Indirect(val)
val.Set(xv)
Expand All @@ -75,7 +74,7 @@ func (a *array) Memset(x interface{}) error {
{{end -}}
{{end -}}
}
xv := reflect.ValueOf(x)
ptr := uintptr(a.Ptr)
for i := 0; i < a.L; i++ {
Expand Down Expand Up @@ -192,7 +191,7 @@ func (t *array) memsetIter(x interface{}, it Iterator) (err error) {
}
data := t.{{sliceOf .}}
for i, err = it.Next(); err == nil; i, err = it.Next(){
data[i] = xv
data[i] = xv
}
err = handleNoOp(err)
{{end -}}
Expand Down
20 changes: 10 additions & 10 deletions genlib2/dense_io.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ func (r *binaryReader) Err() error {
// http://docs.scipy.org/doc/numpy/neps/npy-format.html
//
// Gorgonia specifically uses Version 1.0, as 65535 bytes should be more than enough for the headers.
// The values are written in little endian order, because let's face it -
// The values are written in little endian order, because let's face it -
// 90% of the world's computers are running on x86+ processors.
//
// This method does not close the writer. Closing (if needed) is deferred to the caller
// If tensor is masked, invalid values are replaced by the default fill value.
func (t *Dense) WriteNpy(w io.Writer) (err error) {
var npdt string
if npdt, err = t.t.numpyDtype(); err != nil{
return
return
}
header := "{'descr': '<%v', 'fortran_order': False, 'shape': %v}"
Expand All @@ -86,7 +86,7 @@ func (t *Dense) WriteNpy(w io.Writer) (err error) {
bw.seq = 0
if t.IsMasked(){
fillval:=t.FillValue()
it := FlatMaskedIteratorFromDense(t)
it := FlatMaskedIteratorFromDense(t)
for i, err := it.Next(); err == nil; i, err = it.Next() {
if t.mask[i] {
bw.w(fillval)
Expand Down Expand Up @@ -119,7 +119,7 @@ func (t *Dense) WriteCSV(w io.Writer, formats ...string) (err error) {
}
cw := csv.NewWriter(w)
it := IteratorFromDense(t)
it := IteratorFromDense(t)
coord := it.Coord()
// rows := t.Shape()[0]
Expand Down Expand Up @@ -195,7 +195,7 @@ func (t *Dense) GobEncode() (p []byte, err error){
if err = encoder.Encode(&data); err != nil {
return
}
return buf.Bytes(), err
}
`
Expand All @@ -205,7 +205,7 @@ func (t *Dense) GobDecode(p []byte) (err error){
buf := bytes.NewBuffer(p)
decoder := gob.NewDecoder(buf)
var shape Shape
if err = decoder.Decode(&shape); err != nil {
return
Expand All @@ -232,12 +232,12 @@ func (t *Dense) GobDecode(p []byte) (err error){
if err = decoder.Decode(&mask); err != nil {
return
}
var data interface{}
if err = decoder.Decode(&data); err != nil {
return
}
t.fromSlice(data)
t.addMask(mask)
t.fix()
Expand Down Expand Up @@ -298,7 +298,7 @@ func (t *Dense) ReadNpy(r io.Reader) (err error){
return errors.New("No shape information found in npy file")
}
sizesStr := strings.Split(string(match[1]), ",")
var shape Shape
for _, s := range sizesStr {
Expand Down Expand Up @@ -339,7 +339,7 @@ func (t *Dense) ReadNpy(r io.Reader) (err error){
}
`

const readCSVRaw = `// convFromStrs converts a []string to a slice of the Dtype provided. It takes a provided backing slice.
const readCSVRaw = `// convFromStrs converts a []string to a slice of the Dtype provided. It takes a provided backing slice.
// If into is nil, then a backing slice will be created.
func convFromStrs(to Dtype, record []string, into interface{}) (interface{}, error) {
var err error
Expand Down
10 changes: 10 additions & 0 deletions testutils_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tensor

import (
"bytes"
"errors"
"math"
"math/cmplx"
Expand Down Expand Up @@ -562,3 +563,12 @@ func qcEqCheck(t *testing.T, dt Dtype, willFailEq bool, correct, got interface{}
}
return true
}

// DummyState is a dummy fmt.State, used to debug things
type DummyState struct {
*bytes.Buffer
}

func (d *DummyState) Width() (int, bool) { return 0, false }
func (d *DummyState) Precision() (int, bool) { return 0, false }
func (d *DummyState) Flag(c int) bool { return false }

0 comments on commit ef164d5

Please sign in to comment.