-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- deprecate `vector` - add tests
- Loading branch information
Showing
5 changed files
with
58 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
"""Useful helper functions.""" | ||
from collections.abc import Sequence | ||
|
||
import numpy as np | ||
|
||
from .pycuvec import vec_types | ||
|
||
|
||
def zeros(shape, dtype="float32"): | ||
""" | ||
Returns a new `Vector_*` of the specified shape and data type | ||
(`cuvec` equivalent of `numpy.zeros`). | ||
""" | ||
return vec_types[np.dtype(dtype)](shape if isinstance(shape, Sequence) else (shape,)) | ||
|
||
|
||
def from_numpy(arr): | ||
""" | ||
Returns a new `Vector_*` of the specified shape and data type | ||
(`cuvec` equivalent of `numpy.copy`). | ||
""" | ||
res = zeros(arr.shape, arr.dtype) | ||
np.asarray(res)[:] = arr[:] | ||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import numpy as np | ||
from pytest import mark | ||
|
||
import cuvec | ||
|
||
|
||
@mark.parametrize("spec,result", [("i", np.int32), ("d", np.float64)]) | ||
def test_zeros(spec, result): | ||
shape = 127, 344, 344 | ||
a = np.asarray(cuvec.zeros(shape, spec)) | ||
assert a.dtype == result | ||
assert a.shape == shape | ||
assert not a.any() | ||
|
||
|
||
def test_from_numpy(): | ||
shape = 127, 344, 344 | ||
a = np.random.random(shape) | ||
b = np.asarray(cuvec.from_numpy(a)) | ||
assert a.shape == b.shape | ||
assert a.dtype == b.dtype | ||
assert (a == b).all() |