From 8031bb85f5943fb587d27ca66c0caf4809e12765 Mon Sep 17 00:00:00 2001 From: pjongy Date: Thu, 19 Oct 2023 03:31:22 +0000 Subject: [PATCH 1/2] MINOR: Move runtime.SetFinalizer into freeXXX --- ts/scalar.go | 12 ++++++++---- ts/tensor.go | 14 ++++++++------ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/ts/scalar.go b/ts/scalar.go index 33da8c9..c548637 100644 --- a/ts/scalar.go +++ b/ts/scalar.go @@ -17,8 +17,10 @@ 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) + } lock.Lock() delete(ExistingScalars, x.name) lock.Unlock() @@ -53,8 +55,10 @@ 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) + } lock.Lock() ExistingScalars[x.name] = struct{}{} lock.Unlock() diff --git a/ts/tensor.go b/ts/tensor.go index 33f6666..dc8e554 100644 --- a/ts/tensor.go +++ b/ts/tensor.go @@ -67,8 +67,10 @@ 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) + } lock.Lock() if _, ok := ExistingTensors[name]; ok { name = fmt.Sprintf("%s_%09d", name, TensorCount) @@ -170,6 +172,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 +1196,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) } From 6f7d68572bde448faa4dab821f220416a91897bf Mon Sep 17 00:00:00 2001 From: pjongy Date: Thu, 19 Oct 2023 04:18:26 +0000 Subject: [PATCH 2/2] Fix build error --- ts/scalar.go | 12 ++++-------- ts/tensor.go | 6 ++---- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/ts/scalar.go b/ts/scalar.go index c548637..e3c1ab9 100644 --- a/ts/scalar.go +++ b/ts/scalar.go @@ -20,6 +20,8 @@ func freeCScalar(x *Scalar) error { 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) @@ -30,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 } @@ -58,15 +56,13 @@ func newScalar(cscalar lib.Cscalar, nameOpt ...string) *Scalar { 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() 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 dc8e554..6e5216e 100644 --- a/ts/tensor.go +++ b/ts/tensor.go @@ -70,6 +70,8 @@ func newTensor(ctensor lib.Ctensor, nameOpt ...string) *Tensor { 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() if _, ok := ExistingTensors[name]; ok { @@ -80,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)