Skip to content
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

MultiFab & Array4 #19

Merged
merged 43 commits into from
Mar 26, 2022
Merged

MultiFab & Array4 #19

merged 43 commits into from
Mar 26, 2022

Conversation

ax3l
Copy link
Member

@ax3l ax3l commented Mar 18, 2021

Reimplementation of the successful MFiter concepts from: https://github.com/sayerhs/pyamrex for MultiFab, avoiding copies. Reimplementation of yield continuations with an iterator.

Start new concepts for Array4 - first via __array_interface__: #17 / #9 (comment)

  • we will make Array4(Real/Int) createable only by MultiFabs (amrex::Box indexing semantics by default for everything: index -1 is ghost)
  • add explicit asarray / as_numpy / as_cupy functions for non-owning data from it: (numpy/Python Buffer Interface semantics for everything: index -1 is end of array)
  • make data copies explicit or fail if coming from device (check also pytorch & tensorflow interfaces for good vibes)

Refs.:

# assume a MultiFab mfab with 3 components
ngv = mfab.nGrowVect

# Looping
for mfi in mfab:
    bx = mfi.tilebox().grow(ngv)
    marr = mfab.array(mfi) # Array4: added __array_interface__ here

    # index by index assignment
    # - this is AMReX Array4, F-order indices
    # - even though we iterate by fastest varying index, such loops are naturally very slow in Python
    for i, j, k in bx:
        mar[i, j, k, 0] = 10.0 * i
        mar[i, j, k, 1] = 10.0 * j
        mar[i, j, k, 2] = 10.0 * k

    # numpy representation: non-copying view, including the
    # guard/ghost region
    #   note: in numpy, indices are in C-order!
    marr_np = np.array(marr, copy=False)

    # all components at all indices set at once to 42
    marr_np[:, :, :, :] = 42.
    # first component at all indices set at once to 12
    marr_np[0, :, :, :] = 12.

    # check the values at start/end are the same between MultiFab and numpy view in it: first component
    assert(marr_np[0, 0, 0, 0] == marr[bx.small_end])
    assert(marr_np[0, -1, -1, -1] == marr[bx.big_end])

@ax3l ax3l requested a review from sayerhs March 18, 2021 21:32
@sayerhs sayerhs marked this pull request as draft March 18, 2021 21:33
@ax3l ax3l mentioned this pull request Oct 5, 2021
7 tasks
@ax3l
Copy link
Member Author

ax3l commented Oct 8, 2021

A comment by @WeiqunZhang

MultiFab contains a vector of ints. These ints are global box index that can be used to access Fab, MulitiFab::operator[](int K). There is a public function that returns the vector. Maybe this can be used to build an iterator in python without involving MFIter.
FabArrayBase::indexArray() if I recall it correctly.

@ax3l ax3l mentioned this pull request Feb 28, 2022
@ax3l ax3l force-pushed the topic-MultiFab branch 3 times, most recently from 24584b0 to 97b9875 Compare March 9, 2022 05:25
@ax3l ax3l marked this pull request as ready for review March 9, 2022 18:03
@ax3l
Copy link
Member Author

ax3l commented Mar 25, 2022

Windows memory violation: Cannot find any issue on Linux with address + undefined sanitizers so far... (besides #29)

update: I used a too-small integer type to store the pointer: numpy/numpy#21248

@ax3l

This comment was marked as resolved.

@ax3l ax3l force-pushed the topic-MultiFab branch 2 times, most recently from a56a873 to 165f854 Compare March 25, 2022 16:07
tests/test_array4.py Outdated Show resolved Hide resolved
@ax3l

This comment was marked as resolved.

ax3l and others added 11 commits March 25, 2022 15:03
Co-authored-by: Shreyas Ananthan <shreyas@umd.edu>
Forgot to return in custom lambda methods
Still fails with double free, probably because MFIter survives MultiFab
or so
Try again with a newer pybind11 version later?
Implement the `__array_interface__` in Array for.
@ax3l ax3l force-pushed the topic-MultiFab branch 2 times, most recently from 28c886b to 4ac3dde Compare March 25, 2022 22:07
3D == no component: stride should not matter
@ax3l

This comment was marked as resolved.

@ax3l ax3l changed the title [Draft] MultiFab MultiFab Mar 26, 2022
@ax3l ax3l changed the title MultiFab MultiFab & Array4 Mar 26, 2022
@ax3l ax3l merged commit e430ab8 into AMReX-Codes:development Mar 26, 2022
@ax3l ax3l deleted the topic-MultiFab branch March 26, 2022 21:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant