-
Notifications
You must be signed in to change notification settings - Fork 23
Helpers: to_numpy/cupy #88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| """ | ||
| This file is part of pyAMReX | ||
| Copyright 2023 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 | ||
|
Comment on lines
+37
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Follow-up: support AMReX-side device-to-host copies. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added directly to MultiFab in #192 |
||
| 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 cupy 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.") | ||
|
|
||
|
|
||
| def register_Array4_extension(amr): | ||
| """Array4 helper methods""" | ||
| import inspect | ||
| import sys | ||
|
|
||
| # register member functions for every Array4_* type | ||
| for _, Array4_type in inspect.getmembers( | ||
| sys.modules[amr.__name__], | ||
| lambda member: inspect.isclass(member) | ||
| and member.__module__ == amr.__name__ | ||
| and member.__name__.startswith("Array4_"), | ||
| ): | ||
| Array4_type.to_numpy = array4_to_numpy | ||
| Array4_type.to_cupy = array4_to_cupy | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| """ | ||
| This file is part of pyAMReX | ||
|
|
||
| Copyright 2023 AMReX community | ||
| Authors: Axel Huebl | ||
| License: BSD-3-Clause-LBNL | ||
| """ | ||
| from collections import namedtuple | ||
|
|
||
|
|
||
| def aos_to_numpy(self, copy=False): | ||
| """ | ||
| Provide Numpy views into a ArrayOfStructs. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| self : amrex.ArrayOfStructs_* | ||
| An ArrayOfStructs class in pyAMReX | ||
| copy : bool, optional | ||
| Copy the data if true, otherwise create a view (default). | ||
|
|
||
| Returns | ||
| ------- | ||
| namedtuple | ||
| A tuple with real and int components that are each lists | ||
| of 1D numpy arrays. | ||
| """ | ||
| import numpy as np | ||
|
|
||
| if self.size() == 0: | ||
| raise ValueError("AoS is empty.") | ||
|
|
||
| if copy: | ||
| # This supports a device-to-host copy. | ||
| # | ||
| # todo: validate of the to_host() returned object | ||
| # lifetime is always managed correctly by | ||
| # Python's GC - otherwise copy twice via copy=True | ||
| return np.array(self.to_host(), copy=False) | ||
| else: | ||
| return np.array(self, copy=False) | ||
|
|
||
|
|
||
| def aos_to_cupy(self, copy=False): | ||
| """ | ||
| Provide Cupy views into a ArrayOfStructs. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| self : amrex.ArrayOfStructs_* | ||
| An ArrayOfStructs class in pyAMReX | ||
| copy : bool, optional | ||
| Copy the data if true, otherwise create a view (default). | ||
|
|
||
| Returns | ||
| ------- | ||
| namedtuple | ||
| A tuple with real and int components that are each lists | ||
| of 1D numpy arrays. | ||
|
|
||
| Raises | ||
| ------ | ||
| ImportError | ||
| Raises an exception if cupy is not installed | ||
| """ | ||
| import cupy as cp | ||
|
|
||
| SoA_cp = namedtuple(type(self).__name__ + "_cp", ["real", "int"]) | ||
|
|
||
| soa_view = SoA_cp([], []) | ||
Check noticeCode scanning / CodeQL Unused local variable
Variable soa_view is not used.
|
||
|
|
||
| if self.size() == 0: | ||
| raise ValueError("AoS is empty.") | ||
|
|
||
| return cp.array(self, copy=copy) | ||
|
|
||
|
|
||
| def register_AoS_extension(amr): | ||
| """ArrayOfStructs helper methods""" | ||
| import inspect | ||
| import sys | ||
|
|
||
| # register member functions for every ArrayOfStructs_* type | ||
| for _, AoS_type in inspect.getmembers( | ||
| sys.modules[amr.__name__], | ||
| lambda member: inspect.isclass(member) | ||
| and member.__module__ == amr.__name__ | ||
| and member.__name__.startswith("ArrayOfStructs_"), | ||
| ): | ||
| AoS_type.to_numpy = aos_to_numpy | ||
| AoS_type.to_cupy = aos_to_cupy | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| """ | ||
| This file is part of pyAMReX | ||
|
|
||
| Copyright 2023 AMReX community | ||
| Authors: Axel Huebl | ||
| License: BSD-3-Clause-LBNL | ||
| """ | ||
|
|
||
|
|
||
| def mf_to_numpy(self, copy=False, order="F"): | ||
| """ | ||
| Provide a Numpy view into a MultiFab. | ||
|
|
||
| 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.MultiFab | ||
| A MultiFab 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 | ||
| ------- | ||
| list of np.array | ||
| A list of numpy n-dimensional arrays, for each local block in the | ||
| MultiFab. | ||
| """ | ||
| views = [] | ||
| for mfi in self: | ||
| views.append(self.array(mfi).to_numpy(copy, order)) | ||
|
|
||
| return views | ||
|
|
||
|
|
||
| def mf_to_cupy(self, copy=False, order="F"): | ||
| """ | ||
| Provide a Cupy view into a MultiFab. | ||
|
|
||
| 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.MultiFab | ||
| A MultiFab 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 | ||
| ------- | ||
| list of cupy.array | ||
| A list of cupy n-dimensional arrays, for each local block in the | ||
| MultiFab. | ||
|
|
||
| Raises | ||
| ------ | ||
| ImportError | ||
| Raises an exception if cupy is not installed | ||
| """ | ||
| views = [] | ||
| for mfi in self: | ||
| views.append(self.array(mfi).to_cupy(copy, order)) | ||
|
|
||
| return views | ||
|
|
||
|
|
||
| def register_MultiFab_extension(amr): | ||
| """MultiFab helper methods""" | ||
| import inspect | ||
| import sys | ||
|
|
||
| # register member functions for every MultiFab* type | ||
| for _, MultiFab_type in inspect.getmembers( | ||
| sys.modules[amr.__name__], | ||
| lambda member: inspect.isclass(member) | ||
| and member.__module__ == amr.__name__ | ||
| and member.__name__.startswith("MultiFab"), | ||
| ): | ||
| MultiFab_type.to_numpy = mf_to_numpy | ||
| MultiFab_type.to_cupy = mf_to_cupy |
Uh oh!
There was an error while loading. Please reload this page.