Merge pull request #112 from pjongy/minor_cleanup_memory
MINOR: Move runtime.SetFinalizer into freeXXX
This commit is contained in:
commit
e0fce090d5
24
ts/scalar.go
24
ts/scalar.go
|
@ -17,8 +17,12 @@ type Scalar struct {
|
||||||
|
|
||||||
// free releases C allocated memory.
|
// free releases C allocated memory.
|
||||||
func freeCScalar(x *Scalar) error {
|
func freeCScalar(x *Scalar) error {
|
||||||
nbytes := x.nbytes()
|
if gotch.Debug {
|
||||||
atomic.AddInt64(&AllocatedMem, -nbytes)
|
nbytes := x.nbytes()
|
||||||
|
atomic.AddInt64(&AllocatedMem, -nbytes)
|
||||||
|
|
||||||
|
log.Printf("INFO: Released scalar %q - C memory: %d bytes.\n", x.name, nbytes)
|
||||||
|
}
|
||||||
lock.Lock()
|
lock.Lock()
|
||||||
delete(ExistingScalars, x.name)
|
delete(ExistingScalars, x.name)
|
||||||
lock.Unlock()
|
lock.Unlock()
|
||||||
|
@ -28,10 +32,6 @@ func freeCScalar(x *Scalar) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if gotch.Debug {
|
|
||||||
log.Printf("INFO: Released scalar %q - C memory: %d bytes.\n", x.name, nbytes)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,17 +52,17 @@ func newScalar(cscalar lib.Cscalar, nameOpt ...string) *Scalar {
|
||||||
}
|
}
|
||||||
|
|
||||||
atomic.AddInt64(&ScalarCount, 1)
|
atomic.AddInt64(&ScalarCount, 1)
|
||||||
nbytes := x.nbytes()
|
if gotch.Debug {
|
||||||
atomic.AddInt64(&AllocatedMem, nbytes)
|
nbytes := x.nbytes()
|
||||||
|
atomic.AddInt64(&AllocatedMem, nbytes)
|
||||||
|
|
||||||
|
log.Printf("INFO: scalar %q added - Allocated memory (%d bytes).\n", x.name, nbytes)
|
||||||
|
}
|
||||||
lock.Lock()
|
lock.Lock()
|
||||||
x.name = newName(nameOpt...)
|
x.name = newName(nameOpt...)
|
||||||
ExistingScalars[x.name] = struct{}{}
|
ExistingScalars[x.name] = struct{}{}
|
||||||
lock.Unlock()
|
lock.Unlock()
|
||||||
|
|
||||||
if gotch.Debug {
|
|
||||||
log.Printf("INFO: scalar %q added - Allocated memory (%d bytes).\n", x.name, nbytes)
|
|
||||||
}
|
|
||||||
|
|
||||||
runtime.SetFinalizer(x, freeCScalar)
|
runtime.SetFinalizer(x, freeCScalar)
|
||||||
|
|
||||||
return x
|
return x
|
||||||
|
|
20
ts/tensor.go
20
ts/tensor.go
|
@ -66,8 +66,12 @@ func newTensor(ctensor lib.Ctensor, nameOpt ...string) *Tensor {
|
||||||
x.d = new(bigStruct)
|
x.d = new(bigStruct)
|
||||||
|
|
||||||
atomic.AddInt64(&TensorCount, 1)
|
atomic.AddInt64(&TensorCount, 1)
|
||||||
nbytes := x.nbytes()
|
if gotch.Debug {
|
||||||
atomic.AddInt64(&AllocatedMem, nbytes)
|
nbytes := x.nbytes()
|
||||||
|
atomic.AddInt64(&AllocatedMem, nbytes)
|
||||||
|
|
||||||
|
log.Printf("INFO: Added tensor %q - Allocated memory: %d bytes.\n", x.name, nbytes)
|
||||||
|
}
|
||||||
lock.Lock()
|
lock.Lock()
|
||||||
name := newName(nameOpt...)
|
name := newName(nameOpt...)
|
||||||
if _, ok := ExistingTensors[name]; ok {
|
if _, ok := ExistingTensors[name]; ok {
|
||||||
|
@ -78,10 +82,6 @@ func newTensor(ctensor lib.Ctensor, nameOpt ...string) *Tensor {
|
||||||
|
|
||||||
x.name = name
|
x.name = name
|
||||||
|
|
||||||
if gotch.Debug {
|
|
||||||
log.Printf("INFO: Added tensor %q - Allocated memory: %d bytes.\n", x.name, nbytes)
|
|
||||||
}
|
|
||||||
|
|
||||||
x.calledFrom = "newTensor()"
|
x.calledFrom = "newTensor()"
|
||||||
|
|
||||||
runtime.SetFinalizer(x, freeCTensor)
|
runtime.SetFinalizer(x, freeCTensor)
|
||||||
|
@ -170,6 +170,10 @@ func freeCTensor(ts *Tensor) error {
|
||||||
// IMPORTANT. make it nil so won't double free.
|
// IMPORTANT. make it nil so won't double free.
|
||||||
ts.ctensor = nil
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1190,10 +1194,6 @@ func (ts *Tensor) Drop() error {
|
||||||
return nil
|
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()"
|
ts.calledFrom = "ts.Drop()"
|
||||||
return freeCTensor(ts)
|
return freeCTensor(ts)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user