-
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
8 changed files
with
187 additions
and
68 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,73 @@ | ||
/* Copyright 2021-2023 The AMReX Community | ||
* | ||
* Authors: Axel Huebl | ||
* License: BSD-3-Clause-LBNL | ||
*/ | ||
#pragma once | ||
|
||
#include "pyAMReX.H" | ||
|
||
#include <AMReX_Array4.H> | ||
#include <AMReX_BLassert.H> | ||
#include <AMReX_IntVect.H> | ||
|
||
#include <cstdint> | ||
#include <sstream> | ||
#include <type_traits> | ||
|
||
|
||
namespace pyAMReX | ||
{ | ||
using namespace amrex; | ||
|
||
/** CPU: __array_interface__ v3 | ||
* | ||
* https://numpy.org/doc/stable/reference/arrays.interface.html | ||
*/ | ||
template<typename T> | ||
py::dict | ||
array_interface(Array4<T> const & a4) | ||
{ | ||
auto d = py::dict(); | ||
auto const len = length(a4); | ||
// F->C index conversion here | ||
// p[(i-begin.x)+(j-begin.y)*jstride+(k-begin.z)*kstride+n*nstride]; | ||
// Buffer dimensions: zero-size shall not skip dimension | ||
auto shape = py::make_tuple( | ||
py::ssize_t(a4.ncomp), | ||
py::ssize_t(len.z <= 0 ? 1 : len.z), | ||
py::ssize_t(len.y <= 0 ? 1 : len.y), | ||
py::ssize_t(len.x <= 0 ? 1 : len.x) // fastest varying index | ||
); | ||
// buffer protocol strides are in bytes, AMReX strides are elements | ||
auto const strides = py::make_tuple( | ||
py::ssize_t(sizeof(T) * a4.nstride), | ||
py::ssize_t(sizeof(T) * a4.kstride), | ||
py::ssize_t(sizeof(T) * a4.jstride), | ||
py::ssize_t(sizeof(T)) // fastest varying index | ||
); | ||
bool const read_only = false; | ||
d["data"] = py::make_tuple(std::intptr_t(a4.dataPtr()), read_only); | ||
// note: if we want to keep the same global indexing with non-zero | ||
// box small_end as in AMReX, then we can explore playing with | ||
// this offset as well | ||
//d["offset"] = 0; // default | ||
//d["mask"] = py::none(); // default | ||
|
||
d["shape"] = shape; | ||
// we could also set this after checking the strides are C-style contiguous: | ||
//if (is_contiguous<T>(shape, strides)) | ||
// d["strides"] = py::none(); // C-style contiguous | ||
//else | ||
d["strides"] = strides; | ||
|
||
// type description | ||
// for more complicated types, e.g., tuples/structs | ||
//d["descr"] = ...; | ||
// we currently only need this | ||
d["typestr"] = py::format_descriptor<T>::format(); | ||
|
||
d["version"] = 3; | ||
return d; | ||
} | ||
} |
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
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
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