diff --git a/ts/scalar.go b/ts/scalar.go index 8abf69d..d147d69 100644 --- a/ts/scalar.go +++ b/ts/scalar.go @@ -17,8 +17,12 @@ type Scalar struct { // free releases C allocated memory. func freeCScalar(x *Scalar) error { - nbytes := x.nbytes() - atomic.AddInt64(&AllocatedMem, -nbytes) + if gotch.Debug { + nbytes := x.nbytes() + atomic.AddInt64(&AllocatedMem, -nbytes) + + log.Printf("INFO: Released scalar %q - C memory: %d bytes.\n", x.name, nbytes) + } lock.Lock() delete(ExistingScalars, x.name) lock.Unlock() @@ -28,10 +32,6 @@ func freeCScalar(x *Scalar) error { return err } - if gotch.Debug { - log.Printf("INFO: Released scalar %q - C memory: %d bytes.\n", x.name, nbytes) - } - return nil } @@ -52,17 +52,17 @@ func newScalar(cscalar lib.Cscalar, nameOpt ...string) *Scalar { } atomic.AddInt64(&ScalarCount, 1) - nbytes := x.nbytes() - atomic.AddInt64(&AllocatedMem, nbytes) + if gotch.Debug { + nbytes := x.nbytes() + atomic.AddInt64(&AllocatedMem, nbytes) + + log.Printf("INFO: scalar %q added - Allocated memory (%d bytes).\n", x.name, nbytes) + } lock.Lock() x.name = newName(nameOpt...) ExistingScalars[x.name] = struct{}{} lock.Unlock() - if gotch.Debug { - log.Printf("INFO: scalar %q added - Allocated memory (%d bytes).\n", x.name, nbytes) - } - runtime.SetFinalizer(x, freeCScalar) return x diff --git a/ts/tensor.go b/ts/tensor.go index 4e5531d..2bc73da 100644 --- a/ts/tensor.go +++ b/ts/tensor.go @@ -66,8 +66,12 @@ func newTensor(ctensor lib.Ctensor, nameOpt ...string) *Tensor { x.d = new(bigStruct) atomic.AddInt64(&TensorCount, 1) - nbytes := x.nbytes() - atomic.AddInt64(&AllocatedMem, nbytes) + if gotch.Debug { + nbytes := x.nbytes() + atomic.AddInt64(&AllocatedMem, nbytes) + + log.Printf("INFO: Added tensor %q - Allocated memory: %d bytes.\n", x.name, nbytes) + } lock.Lock() name := newName(nameOpt...) if _, ok := ExistingTensors[name]; ok { @@ -78,10 +82,6 @@ func newTensor(ctensor lib.Ctensor, nameOpt ...string) *Tensor { x.name = name - if gotch.Debug { - log.Printf("INFO: Added tensor %q - Allocated memory: %d bytes.\n", x.name, nbytes) - } - x.calledFrom = "newTensor()" runtime.SetFinalizer(x, freeCTensor) @@ -170,6 +170,10 @@ func freeCTensor(ts *Tensor) error { // IMPORTANT. make it nil so won't double free. ts.ctensor = nil + // Clear SetFinalizer on ts so no double free tensor. + // Ref. https://pkg.go.dev/runtime#SetFinalizer + runtime.SetFinalizer(ts, nil) + return nil } @@ -1190,10 +1194,6 @@ func (ts *Tensor) Drop() error { return nil } - // Clear SetFinalizer on ts so no double free tensor. - // Ref. https://pkg.go.dev/runtime#SetFinalizer - runtime.SetFinalizer(ts, nil) - ts.calledFrom = "ts.Drop()" return freeCTensor(ts) }