Skip to content

Commit

Permalink
Add unsafe_raw_data (#499)
Browse files Browse the repository at this point in the history
* Add unsafe_raw_data

* update comment

* update changelog and nimble version

* fix casual missing ptr keyword

* rename unsafe_raw_data -> toUnsafeView and move it next to fromBuffer as its counterpart

* unsafe_raw_data -> toUnsafeView in docstring & changelog
  • Loading branch information
Clonkk authored Mar 22, 2021
1 parent 9a575de commit 6118841
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion arraymancer.nimble
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
### Package
version = "0.6.2"
version = "0.6.3"
author = "Mamy André-Ratsimbazafy"
description = "A n-dimensional tensor (ndarray) library"
license = "Apache License 2.0"
Expand Down
10 changes: 8 additions & 2 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
Arraymancer v0.x.x
Arraymancer v0.6.X
=====================================================
Changes :
- Add ``toUnsafeView`` as replacement of ``dataArray`` to return a ``ptr UncheckedArray``

Changes (TODO):

Arraymancer v0.6.2 Dec. 22 2020
=====================================================

Changes :
- Fancy Indexing (#434)
- ``argmax_max`` obsolete assert to 1D/2D removed. It refered to issues #183 and merged PR #171

Expand Down
12 changes: 12 additions & 0 deletions src/arraymancer/laser/tensor/initialization.nim
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ proc fromBuffer*[T](rawBuffer: ptr UncheckedArray[T], shape: varargs[int]): Tens
## If you type cast a raw `pointer` to `ptr UncheckedArray[T]` before handing it to this
## proc, make sure to cast to the correct type as we cannot check the validity of
## the type!
##
## Its counterpart ``toUnsafeView`` can be used to obtain ``ptr UncheckedArray`` from a Tensor.
var size: int
initTensorMetadata(result, size, shape)
cpuStorageFromBuffer(result.storage, rawBuffer, size)
Expand All @@ -242,6 +244,16 @@ proc fromBuffer*[T](rawBuffer: pointer, shape: varargs[int]): Tensor[T] =
## Creates a `Tensor[T]` from a raw `pointer`. Make sure that the explicit type
## given to this proc actually matches the data stored behind the pointer!
## The size derived from the given shape must match the size of the buffer!
##
## Its counterpart ``toUnsafeView`` can be used to obtain ``ptr UncheckedArray`` from a Tensor.
var size: int
initTensorMetadata(result, size, shape)
cpuStorageFromBuffer(result.storage, rawBuffer, size)

func toUnsafeView*[T: KnownSupportsCopyMem](t: Tensor[T], aligned: static bool = true): ptr UncheckedArray[T] {.inline.} =
## Returns an unsafe view of the valid data as a ``ptr UncheckedArray``.
## Its counterpart ``fromBuffer`` can be used to create a Tensor from``ptr UncheckedArray``.
##
## Unsafe: the pointer can outlive the input tensor.
unsafe_raw_offset(t, aligned).distinctBase()

2 changes: 1 addition & 1 deletion src/arraymancer/tensor/data_structure.nim
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ proc get_offset_ptr*[T](t: CudaTensor[T] or ClTensor[T]): ptr T {.noSideEffect,
## - A pointer to the offset start of its data
t.storage.Fdata[t.offset].unsafeAddr

proc dataArray*[T: KnownSupportsCopyMem](t: Tensor[T]): ptr UncheckedArray[T] {.noSideEffect, inline, deprecated: "Use unsafe_raw_offset instead".}=
proc dataArray*[T: KnownSupportsCopyMem](t: Tensor[T]): ptr UncheckedArray[T] {.noSideEffect, inline, deprecated: "Use toUnsafeView instead".}=
## Input:
## - A tensor
## Returns:
Expand Down

0 comments on commit 6118841

Please sign in to comment.