|
| 1 | +""" |
| 2 | +This file is part of pyAMReX |
| 3 | +
|
| 4 | +Copyright 2023 AMReX community |
| 5 | +Authors: Axel Huebl |
| 6 | +License: BSD-3-Clause-LBNL |
| 7 | +""" |
| 8 | + |
| 9 | + |
| 10 | +def array4_to_numpy(self, copy=False, order="F"): |
| 11 | + """ |
| 12 | + Provide a Numpy view into an Array4. |
| 13 | +
|
| 14 | + Note on the order of indices: |
| 15 | + By default, this is as in AMReX in Fortran contiguous order, indexing as |
| 16 | + x,y,z. This has performance implications for use in external libraries such |
| 17 | + as cupy. |
| 18 | + The order="C" option will index as z,y,x and perform better with cupy. |
| 19 | + https://github.com/AMReX-Codes/pyamrex/issues/55#issuecomment-1579610074 |
| 20 | +
|
| 21 | + Parameters |
| 22 | + ---------- |
| 23 | + self : amrex.Array4_* |
| 24 | + An Array4 class in pyAMReX |
| 25 | + copy : bool, optional |
| 26 | + Copy the data if true, otherwise create a view (default). |
| 27 | + order : string, optional |
| 28 | + F order (default) or C. C is faster with external libraries. |
| 29 | +
|
| 30 | + Returns |
| 31 | + ------- |
| 32 | + np.array |
| 33 | + A numpy n-dimensional array. |
| 34 | + """ |
| 35 | + import numpy as np |
| 36 | + |
| 37 | + if order == "F": |
| 38 | + return np.array(self, copy=copy).T |
| 39 | + elif order == "C": |
| 40 | + return np.array(self, copy=copy) |
| 41 | + else: |
| 42 | + raise ValueError("The order argument must be F or C.") |
| 43 | + |
| 44 | + |
| 45 | +def array4_to_cupy(self, copy=False, order="F"): |
| 46 | + """ |
| 47 | + Provide a Cupy view into an Array4. |
| 48 | +
|
| 49 | + Note on the order of indices: |
| 50 | + By default, this is as in AMReX in Fortran contiguous order, indexing as |
| 51 | + x,y,z. This has performance implications for use in external libraries such |
| 52 | + as cupy. |
| 53 | + The order="C" option will index as z,y,x and perform better with cupy. |
| 54 | + https://github.com/AMReX-Codes/pyamrex/issues/55#issuecomment-1579610074 |
| 55 | +
|
| 56 | + Parameters |
| 57 | + ---------- |
| 58 | + self : amrex.Array4_* |
| 59 | + An Array4 class in pyAMReX |
| 60 | + copy : bool, optional |
| 61 | + Copy the data if true, otherwise create a view (default). |
| 62 | + order : string, optional |
| 63 | + F order (default) or C. C is faster with external libraries. |
| 64 | +
|
| 65 | + Returns |
| 66 | + ------- |
| 67 | + cupy.array |
| 68 | + A cupy n-dimensional array. |
| 69 | +
|
| 70 | + Raises |
| 71 | + ------ |
| 72 | + ImportError |
| 73 | + Raises an exception if cupy is not installed |
| 74 | + """ |
| 75 | + import cupy as cp |
| 76 | + |
| 77 | + if order == "F": |
| 78 | + return cp.array(self, copy=copy).T |
| 79 | + elif order == "C": |
| 80 | + return cp.array(self, copy=copy) |
| 81 | + else: |
| 82 | + raise ValueError("The order argument must be F or C.") |
| 83 | + |
| 84 | + |
| 85 | +def register_Array4_extension(amr): |
| 86 | + """Array4 helper methods""" |
| 87 | + import inspect |
| 88 | + import sys |
| 89 | + |
| 90 | + # register member functions for every Array4_* type |
| 91 | + for _, Array4_type in inspect.getmembers( |
| 92 | + sys.modules[amr.__name__], |
| 93 | + lambda member: inspect.isclass(member) |
| 94 | + and member.__module__ == amr.__name__ |
| 95 | + and member.__name__.startswith("Array4_"), |
| 96 | + ): |
| 97 | + Array4_type.to_numpy = array4_to_numpy |
| 98 | + Array4_type.to_cupy = array4_to_cupy |
0 commit comments