Skip to content

Commit 2e25e65

Browse files
committed
Attach Destroy as method to types
1 parent fd78d7d commit 2e25e65

9 files changed

+40
-55
lines changed

api.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func StreamPriorityRange() (leastPriority int, greatestPriority int, err error)
175175
return
176176
}
177177

178-
func Unload(hmod Module) (err error) {
178+
func (hmod Module) Unload() (err error) {
179179
Chmod := hmod.c()
180180
return result(C.cuModuleUnload(Chmod))
181181
}
@@ -520,7 +520,7 @@ func (hArray Array) Descriptor() (pArrayDescriptor ArrayDesc, err error) {
520520
return
521521
}
522522

523-
func DestroyArray(hArray Array) (err error) {
523+
func (hArray Array) Destroy() (err error) {
524524
ChArray := hArray.c()
525525
return result(C.cuArrayDestroy(ChArray))
526526
}

batch_test.go

+8-10
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ loop:
9494
}
9595
}
9696

97-
Unload(mod)
98-
DestroyContext(&cuctx)
97+
mod.Unload()
98+
cuctx.Destroy()
9999
}
100100

101101
func TestLargeBatch(t *testing.T) {
@@ -211,8 +211,8 @@ loop:
211211
if afterFree != beforeFree {
212212
t.Errorf("Before: Freemem: %v. After %v | Diff %v", beforeFree, afterFree, (beforeFree-afterFree)/1024)
213213
}
214-
Unload(mod)
215-
DestroyContext(&cuctx)
214+
mod.Unload()
215+
cuctx.Destroy()
216216
}
217217

218218
func BenchmarkNoBatching(bench *testing.B) {
@@ -288,9 +288,8 @@ func BenchmarkNoBatching(bench *testing.B) {
288288
}
289289
MemFree(memA)
290290
MemFree(memB)
291-
Unload(mod)
292-
DestroyContext(&ctx)
293-
291+
mod.Unload()
292+
ctx.Destroy()
294293
}
295294

296295
func BenchmarkBatching(bench *testing.B) {
@@ -364,7 +363,6 @@ func BenchmarkBatching(bench *testing.B) {
364363

365364
MemFree(memA)
366365
MemFree(memB)
367-
Unload(mod)
368-
DestroyContext(&cuctx)
369-
366+
mod.Unload()
367+
cuctx.Destroy()
370368
}

batchedPatterns_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TestAttributes(t *testing.T) {
4646
t.Errorf("Expected ComputeCapabilityMinor to be %v. Got %v instead", min, attrs[2])
4747
}
4848

49-
DestroyContext(&ctx)
49+
ctx.Destroy()
5050
}
5151

5252
func TestLaunchAndSync(t *testing.T) {
@@ -122,8 +122,8 @@ func TestLaunchAndSync(t *testing.T) {
122122

123123
MemFree(memA)
124124
MemFree(memB)
125-
Unload(mod)
126-
DestroyContext(&ctx)
125+
mod.Unload()
126+
ctx.Destroy()
127127
}
128128

129129
func TestAllocAndCopy(t *testing.T) {
@@ -168,5 +168,5 @@ func TestAllocAndCopy(t *testing.T) {
168168
}
169169

170170
MemFree(mem)
171-
DestroyContext(&ctx)
171+
ctx.Destroy()
172172
}

cucontext.go

+8-15
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@ func makeContext(ctx C.CUcontext) CUContext { return CUContext{ctx} }
1919
func (ctx CUContext) c() C.CUcontext { return ctx.ctx }
2020

2121
func (d Device) MakeContext(flags ContextFlags) (CUContext, error) {
22-
var ctx C.CUcontext
23-
if err := result(C.cuCtxCreate(&ctx, C.uint(flags), C.CUdevice(d))); err != nil {
24-
return CUContext{}, err
25-
}
26-
return CUContext{ctx}, nil
22+
var ctx CUContext
23+
err := result(C.cuCtxCreate(&ctx.ctx, C.uint(flags), C.CUdevice(d)))
24+
return ctx, err
2725
}
2826

2927
// Lock ties the calling goroutine to an OS thread, then ties the CUDA context to the thread.
@@ -64,15 +62,11 @@ func (ctx CUContext) Unlock() error {
6462
return nil
6563
}
6664

67-
// DestroyContext destroys the context. It returns an error if it wasn't properly destroyed
65+
// Destroy destroys the context. It returns an error if it wasn't properly destroyed
6866
//
6967
// Wrapper over cuCtxDestroy: http://docs.nvidia.com/cuda/cuda-driver-api/group__CUDA__CTX.html#group__CUDA__CTX_1g27a365aebb0eb548166309f58a1e8b8e
70-
func DestroyContext(ctx *CUContext) error {
71-
if err := result(C.cuCtxDestroy(ctx.ctx)); err != nil {
72-
return err
73-
}
74-
*ctx = CUContext{}
75-
return nil
68+
func (ctx *CUContext) Destroy() error {
69+
return result(C.cuCtxDestroy(ctx.ctx))
7670
}
7771

7872
// RetainPrimaryCtx retains the primary context on the GPU, creating it if necessary, increasing its usage count.
@@ -85,9 +79,8 @@ func DestroyContext(ctx *CUContext) error {
8579
// The nvidia-smi tool can be used to set the compute mode for devices. Documentation for nvidia-smi can be obtained by passing a -h option to it.
8680
// Please note that the primary context always supports pinned allocations. Other flags can be specified by cuDevicePrimaryCtxSetFlags().
8781
func (d Device) RetainPrimaryCtx() (primaryContext CUContext, err error) {
88-
var ctx C.CUcontext
89-
if err = result(C.cuDevicePrimaryCtxRetain(&ctx, C.CUdevice(d))); err != nil {
82+
if err = result(C.cuDevicePrimaryCtxRetain(&primaryContext.ctx, C.CUdevice(d))); err != nil {
9083
return
9184
}
92-
return CUContext{ctx}, nil
85+
return primaryContext, nil
9386
}

cucontext_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@ func TestCUContext(t *testing.T) {
110110
}
111111

112112
// finally we destroy the context
113-
if err = DestroyContext(&ctx); err != nil {
113+
if err = ctx.Destroy(); err != nil {
114114
t.Error(err)
115115
}
116116

117-
if ctx != 0 {
117+
if (ctx != CUContext{}) {
118118
t.Error("expected ctx to be set to 0")
119119
}
120120
}

memory_test.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func TestArray(t *testing.T) {
2323
Format: Float32,
2424
}
2525
ctx, _ := Device(0).MakeContext(SchedAuto)
26-
defer DestroyContext(&ctx)
26+
defer ctx.Destroy()
2727

2828
arr, err := Make3DArray(desc)
2929
assert.Nil(err)
@@ -39,7 +39,7 @@ func TestArray(t *testing.T) {
3939
assert.Equal(desc3.Height, desc.Height)
4040
assert.Equal(desc3.NumChannels, desc.NumChannels)
4141

42-
err = DestroyArray(arr)
42+
err = arr.Destroy()
4343
assert.Nil(err)
4444
}
4545

@@ -50,7 +50,7 @@ func TestMalloc(t *testing.T) {
5050
return
5151
}
5252
ctx, _ := Device(0).MakeContext(SchedAuto)
53-
defer DestroyContext(&ctx)
53+
defer ctx.Destroy()
5454

5555
for i := 0; i < 1024; i++ {
5656
pointer, err := MemAlloc(16 * 1024 * 1024)
@@ -70,7 +70,7 @@ func TestDevicePtr_AddressRange(t *testing.T) {
7070
return
7171
}
7272
ctx, _ := Device(0).MakeContext(SchedAuto)
73-
defer DestroyContext(&ctx)
73+
defer ctx.Destroy()
7474

7575
// actual test
7676

@@ -95,7 +95,7 @@ func TestMemInfo(t *testing.T) {
9595
return
9696
}
9797
ctx, _ := Device(0).MakeContext(SchedAuto)
98-
defer DestroyContext(&ctx)
98+
defer ctx.Destroy()
9999

100100
// actual test starts
101101

@@ -120,7 +120,7 @@ func TestMemcpy(t *testing.T) {
120120
return
121121
}
122122
ctx, _ := Device(0).MakeContext(SchedAuto)
123-
defer DestroyContext(&ctx)
123+
defer ctx.Destroy()
124124

125125
// actual test starts
126126

@@ -165,7 +165,7 @@ func TestMemcpyAsync(t *testing.T) {
165165
return
166166
}
167167
ctx, _ := Device(0).MakeContext(SchedAuto)
168-
defer DestroyContext(&ctx)
168+
defer ctx.Destroy()
169169

170170
// actual test starts
171171

@@ -208,7 +208,7 @@ func TestMemset(t *testing.T) {
208208
return
209209
}
210210
ctx, _ := Device(0).MakeContext(SchedAuto)
211-
defer DestroyContext(&ctx)
211+
defer ctx.Destroy()
212212

213213
var err error
214214
var dev1 DevicePtr
@@ -258,7 +258,7 @@ func BenchmarkMallocFree1B(b *testing.B) {
258258
return
259259
}
260260
ctx, _ := Device(0).MakeContext(SchedAuto)
261-
defer DestroyContext(&ctx)
261+
defer ctx.Destroy()
262262

263263
var m DevicePtr
264264
var err error
@@ -282,7 +282,7 @@ func BenchmarkMallocFree1kB(b *testing.B) {
282282
return
283283
}
284284
ctx, _ := Device(0).MakeContext(SchedAuto)
285-
defer DestroyContext(&ctx)
285+
defer ctx.Destroy()
286286

287287
var m DevicePtr
288288
var err error
@@ -307,7 +307,7 @@ func BenchmarkMallocFree1MB(b *testing.B) {
307307
return
308308
}
309309
ctx, _ := Device(0).MakeContext(SchedAuto)
310-
defer DestroyContext(&ctx)
310+
defer ctx.Destroy()
311311

312312
var m DevicePtr
313313
var err error
@@ -338,7 +338,7 @@ func BenchmarkMemcpy(b *testing.B) {
338338
if ctx, err = Device(0).MakeContext(SchedAuto); err != nil {
339339
b.Fatal(err)
340340
}
341-
defer DestroyContext(&ctx)
341+
defer ctx.Destroy()
342342

343343
b.StopTimer()
344344
N := int64(32 * 1024 * 1024)

module.go

-5
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,6 @@ func (m Module) Global(name string) (DevicePtr, int64, error) {
5959
return DevicePtr(d), int64(size), nil
6060
}
6161

62-
// Unload unloads the module
63-
func (m Module) Unload() error {
64-
return result(C.cuModuleUnload(m.mod))
65-
}
66-
6762
func (ctx *Ctx) Load(name string) (m Module, err error) {
6863
var mod C.CUmodule
6964
f := func() error { return result(C.cuModuleLoad(&mod, C.CString(name))) }

module_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func TestModule(t *testing.T) {
1212
return
1313
}
1414
ctx, _ := Device(0).MakeContext(SchedAuto)
15-
defer DestroyContext(&ctx)
15+
defer ctx.Destroy()
1616

1717
var mod Module
1818
var f Function

test_test.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,8 @@ func testSetup() (dev Device, ctx CUContext, err error) {
8484
}
8585

8686
func testTeardown(ctx CUContext, mod Module) {
87-
if mod != 0 {
88-
Unload(mod)
87+
if (mod != Module{}) {
88+
mod.Unload()
8989
}
90-
91-
DestroyContext(&ctx)
90+
ctx.Destroy()
9291
}

0 commit comments

Comments
 (0)