Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix cuda on 2.2 #678

Merged
merged 6 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/arraymancer/tensor/backend/cuda.nim
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ proc cudaMalloc*[T](size: Natural): ptr UncheckedArray[T] {.noSideEffect, inline

proc newCudaStorage*[T: SomeFloat](length: int): CudaStorage[T] {.noSideEffect.}=
result.Flen = length
new(result.Fref_tracking, deallocCuda)
new result.Fref_tracking
result.Fdata = cast[ptr UncheckedArray[T]](cudaMalloc[T](result.Flen))
result.Fref_tracking.value = result.Fdata

Expand Down Expand Up @@ -88,9 +88,16 @@ type
len*: cint # Number of elements allocated in memory


proc deallocCuda*(p: CudaLayoutArray) {.noSideEffect.}=
if not p.value.isNil:
check cudaFree(p.value)
when NimMajor == 1:
proc `=destroy`*(p: var CudaLayoutArrayObj) {.noSideEffect.}=
if not p.value.isNil:
discard cudaFree(p.value)
else:
proc `=destroy`*(p: CudaLayoutArrayObj) {.noSideEffect.}=
if not p.value.isNil:
discard cudaFree(p.value)



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

new result.shape, deallocCuda
new result.strides, deallocCuda
new result.shape
new result.strides

result.shape.value = cudaMalloc[cint](MAXRANK)
result.strides.value = cudaMalloc[cint](MAXRANK)
Expand Down
12 changes: 8 additions & 4 deletions src/arraymancer/tensor/data_structure.nim
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,14 @@ when defined(cuda):
offset*: int
storage*: CudaStorage[T]

proc deallocCuda*[T](p: CudaTensorRefTracker[T]) {.noSideEffect.}=
if not p.value.isNil:
check cudaFree(p.value)
when NimMajor == 1:
proc `=destroy`*[T](p: var CudaTensorRefTrackerObj[T]) {.noSideEffect.}=
if not p.value.isNil:
discard cudaFree(p.value)
else:
proc `=destroy`*[T](p: CudaTensorRefTrackerObj[T]) {.noSideEffect.}=
if not p.value.isNil:
discard cudaFree(p.value)

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




# ###############
# Field accessors
# ###############
Expand Down
Loading