Skip to content

Commit

Permalink
Added more resource management stuff.
Browse files Browse the repository at this point in the history
Temporarily removed the JITTargetOption - older computes are no longer supported
  • Loading branch information
chewxy committed Jul 8, 2018
1 parent 42667b6 commit 820b28d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 26 deletions.
10 changes: 5 additions & 5 deletions array.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ type Array3Desc struct {

func (desc Array3Desc) c() *C.CUDA_ARRAY3D_DESCRIPTOR {
return &C.CUDA_ARRAY3D_DESCRIPTOR{
Width: C.ulonglong(desc.Width),
Height: C.ulonglong(desc.Height),
Depth: C.ulonglong(desc.Depth),
Width: C.ulong(desc.Width),
Height: C.ulong(desc.Height),
Depth: C.ulong(desc.Depth),
Format: C.CUarray_format(desc.Format),
NumChannels: C.uint(desc.NumChannels),
Flags: C.uint(desc.Flags),
Expand Down Expand Up @@ -70,8 +70,8 @@ type ArrayDesc struct {

func (desc ArrayDesc) c() *C.CUDA_ARRAY_DESCRIPTOR {
return &C.CUDA_ARRAY_DESCRIPTOR{
Width: C.ulonglong(desc.Width),
Height: C.ulonglong(desc.Height),
Width: C.ulong(desc.Width),
Height: C.ulong(desc.Height),
Format: C.CUarray_format(desc.Format),
NumChannels: C.uint(desc.NumChannels),
}
Expand Down
1 change: 1 addition & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Context interface {
Do(fn func() error) error
Work() <-chan func() error
ErrChan() chan<- error
Close() error // Close closes all resources associated with the context

// actual methods
Address(hTexRef TexRef) (pdptr DevicePtr, err error)
Expand Down
39 changes: 23 additions & 16 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,31 @@ func newContext(c CUContext) *Ctx {
}
runtime.SetFinalizer(ctx, finalizeCtx)
return ctx
}

// Close destroys the CUDA context and associated resources that has been created. Additionally, all channels of communications will be closed.
func (ctx *Ctx) Close() error {
var empty C.CUcontext
if ctx.CUContext.ctx == empty {
return nil
}

if ctx.errChan != nil {
close(ctx.errChan)
ctx.errChan = nil
}

if ctx.work != nil {
close(ctx.work)
ctx.work = nil
}

err := result(C.cuCtxDestroy(C.CUcontext(unsafe.Pointer(ctx.CUContext.ctx))))
ctx.CUContext.ctx = empty
return err
}

// Do does one function at a time.
func (ctx *Ctx) Do(fn func() error) error {
ctx.work <- fn
return <-ctx.errChan
Expand Down Expand Up @@ -149,21 +171,6 @@ func (ctx *Ctx) Run(errChan chan error) error {
return nil
}

func finalizeCtx(ctx *Ctx) {
if (ctx.CUContext == CUContext{}) {
close(ctx.errChan)
close(ctx.work)
return
}

f := func() error {
return result(C.cuCtxDestroy(C.CUcontext(unsafe.Pointer(&ctx.CUContext))))
}
if err := ctx.Do(f); err != nil {
panic(err)
}
close(ctx.errChan)
close(ctx.work)
}
func finalizeCtx(ctx *Ctx) { ctx.Close() }

/* Manually Written Methods */
18 changes: 17 additions & 1 deletion dnn/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type Memory interface {
// Context represents the context in which cuDNN operations are performed in.
//
// Internally, the Context holds a cudnnHandle_t
//
// Once the context has been finished, do remember to call `Close` on the context.
type Context struct {
internal C.cudnnHandle_t
}
Expand All @@ -33,4 +35,18 @@ func NewContext() (retVal *Context) {
return retVal
}

func destroyContext(obj *Context) { C.cudnnDestroy(obj.internal) }
// Close destroys the underlying context.
func (ctx *Context) Close() error {
var empty C.cudnnHandle_t
if ctx.internal == empty {
return nil
}

if err := result(C.cudnnDestroy(ctx.internal)); err != nil {
return err
}
ctx.internal = empty
return nil
}

func destroyContext(obj *Context) { obj.Close() }
8 changes: 4 additions & 4 deletions jit.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ func jitBooleanOption(b bool) uintptr {
type JITTargetOption uint64

const (
JITTarget10 JITTargetOption = C.CU_TARGET_COMPUTE_10
JITTarget11 JITTargetOption = C.CU_TARGET_COMPUTE_11
JITTarget12 JITTargetOption = C.CU_TARGET_COMPUTE_12
JITTarget13 JITTargetOption = C.CU_TARGET_COMPUTE_13
// JITTarget10 JITTargetOption = C.CU_TARGET_COMPUTE_10
// JITTarget11 JITTargetOption = C.CU_TARGET_COMPUTE_11
// JITTarget12 JITTargetOption = C.CU_TARGET_COMPUTE_12
// JITTarget13 JITTargetOption = C.CU_TARGET_COMPUTE_13
JITTarget20 JITTargetOption = C.CU_TARGET_COMPUTE_20
JITTarget21 JITTargetOption = C.CU_TARGET_COMPUTE_21
JITTarget30 JITTargetOption = C.CU_TARGET_COMPUTE_30
Expand Down

0 comments on commit 820b28d

Please sign in to comment.