Skip to content

Commit 84af537

Browse files
authored
Fix cuda on 2.2 (#678)
* new destroy hook form for cuda tensor pointers * new form for hook for unified memory as well * remove bad form of error throwing on destructors. worst case we'll leak. * nim 1.x.x compat * move destructor hooks to match #676
1 parent 9bff6f1 commit 84af537

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

Diff for: src/arraymancer/tensor/backend/cuda.nim

+13-6
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ proc cudaMalloc*[T](size: Natural): ptr UncheckedArray[T] {.noSideEffect, inline
3333

3434
proc newCudaStorage*[T: SomeFloat](length: int): CudaStorage[T] {.noSideEffect.}=
3535
result.Flen = length
36-
new(result.Fref_tracking, deallocCuda)
36+
new result.Fref_tracking
3737
result.Fdata = cast[ptr UncheckedArray[T]](cudaMalloc[T](result.Flen))
3838
result.Fref_tracking.value = result.Fdata
3939

@@ -88,9 +88,16 @@ type
8888
len*: cint # Number of elements allocated in memory
8989

9090

91-
proc deallocCuda*(p: CudaLayoutArray) {.noSideEffect.}=
92-
if not p.value.isNil:
93-
check cudaFree(p.value)
91+
when NimMajor == 1:
92+
proc `=destroy`*(p: var CudaLayoutArrayObj) {.noSideEffect.}=
93+
if not p.value.isNil:
94+
discard cudaFree(p.value)
95+
else:
96+
proc `=destroy`*(p: CudaLayoutArrayObj) {.noSideEffect.}=
97+
if not p.value.isNil:
98+
discard cudaFree(p.value)
99+
100+
94101

95102
proc layoutOnDevice*[T:SomeFloat](t: CudaTensor[T]): CudaTensorLayout[T] {.noSideEffect.}=
96103
## Store a CudaTensor shape, strides, etc information on the GPU
@@ -104,8 +111,8 @@ proc layoutOnDevice*[T:SomeFloat](t: CudaTensor[T]): CudaTensorLayout[T] {.noSid
104111
result.data = t.get_data_ptr
105112
result.len = t.size.cint
106113

107-
new result.shape, deallocCuda
108-
new result.strides, deallocCuda
114+
new result.shape
115+
new result.strides
109116

110117
result.shape.value = cudaMalloc[cint](MAXRANK)
111118
result.strides.value = cudaMalloc[cint](MAXRANK)

Diff for: src/arraymancer/tensor/data_structure.nim

+8-4
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,14 @@ when defined(cuda):
5757
offset*: int
5858
storage*: CudaStorage[T]
5959

60-
proc deallocCuda*[T](p: CudaTensorRefTracker[T]) {.noSideEffect.}=
61-
if not p.value.isNil:
62-
check cudaFree(p.value)
60+
when NimMajor == 1:
61+
proc `=destroy`*[T](p: var CudaTensorRefTrackerObj[T]) {.noSideEffect.}=
62+
if not p.value.isNil:
63+
discard cudaFree(p.value)
64+
else:
65+
proc `=destroy`*[T](p: CudaTensorRefTrackerObj[T]) {.noSideEffect.}=
66+
if not p.value.isNil:
67+
discard cudaFree(p.value)
6368

6469
when defined(opencl):
6570
type
@@ -98,7 +103,6 @@ type GpuTensor[T] = AnyTensor[T] and not Tensor[T]
98103

99104

100105

101-
102106
# ###############
103107
# Field accessors
104108
# ###############

0 commit comments

Comments
 (0)