-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
131 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
""" | ||
This file is part of pyAMReX | ||
Copyright 2022 AMReX community | ||
Authors: Axel Huebl | ||
License: BSD-3-Clause-LBNL | ||
""" | ||
|
||
def array4_to_numpy(self, copy=False, order="F"): | ||
""" | ||
Provide a Numpy view into an Array4. | ||
Note on the order of indices: | ||
By default, this is as in AMReX in Fortran contiguous order, indexing as | ||
x,y,z. This has performance implications for use in external libraries such | ||
as cupy. | ||
The order="C" option will index as z,y,x and perform better with cupy. | ||
https://github.com/AMReX-Codes/pyamrex/issues/55#issuecomment-1579610074 | ||
Parameters | ||
---------- | ||
self : amrex.Array4_* | ||
An Array4 class in pyAMReX | ||
copy : bool, optional | ||
Copy the data if true, otherwise create a view (default). | ||
order : string, optional | ||
F order (default) or C. C is faster with external libraries. | ||
Returns | ||
------- | ||
np.array | ||
A numpy n-dimensional array. | ||
""" | ||
import numpy as np | ||
|
||
if order == "F": | ||
return np.array(self, copy=copy).T | ||
elif order == "C": | ||
return np.array(self, copy=copy) | ||
else: | ||
raise ValueError("The order argument must be F or C.") | ||
|
||
|
||
def array4_to_cupy(self, copy=False, order="F"): | ||
""" | ||
Provide a Cupy view into an Array4. | ||
Note on the order of indices: | ||
By default, this is as in AMReX in Fortran contiguous order, indexing as | ||
x,y,z. This has performance implications for use in external libraries such | ||
as cupy. | ||
The order="C" option will index as z,y,x and perform better with cupy. | ||
https://github.com/AMReX-Codes/pyamrex/issues/55#issuecomment-1579610074 | ||
Parameters | ||
---------- | ||
self : amrex.Array4_* | ||
An Array4 class in pyAMReX | ||
copy : bool, optional | ||
Copy the data if true, otherwise create a view (default). | ||
order : string, optional | ||
F order (default) or C. C is faster with external libraries. | ||
Returns | ||
------- | ||
cupy.array | ||
A numpy n-dimensional array. | ||
Raises | ||
------ | ||
ImportError | ||
Raises an exception if cupy is not installed | ||
""" | ||
import cupy as cp | ||
|
||
if order == "F": | ||
return cp.array(self, copy=copy).T | ||
elif order == "C": | ||
return cp.array(self, copy=copy) | ||
else: | ||
raise ValueError("The order argument must be F or C.") |
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