Repository navigation
Set up sorting boxes on the CuPy backend #736
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3399211
Set up sorting boxes on the CuPy backend
max-models 2579a99
Merge devel into 726-sorting-boxes-cupy
max-models 185db66
Merge devel into 726-sorting-boxes-cupy
max-models 0e212ae
Merge branch 'devel' into 726-sorting-boxes-cupy
max-models 565d0e3
Merge branch 'devel' into 726-sorting-boxes-cupy
max-models 2395b8e
Address review of #736: box kernels in kernel folders, one host round…
max-models 1be48d1
Merge remote-tracking branch 'origin/devel' into 726-sorting-boxes-cupy
max-models 44c996b
Box kernels: import sorting_kernels as a module, not its names
max-models 992917e
Tests: stubs expose _domain_array_host, count the pic.kernels catalog
max-models File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,10 @@ | ||
| """Box sorting kernels of :class:`~struphy.pic.base.Particles`, one folder per kernel (see ``CUDA_STRATEGY.md``). | ||
|
|
||
| Each folder ``<name>`` holds the pyccel kernel ``<name>_kernels.py`` and, once ported (#694), its CUDA version | ||
| ``<name>_cuda.cu``. The folder's ``__init__.py`` declares the kernel, a :class:`cunumpy.kernels.Kernel` that runs the | ||
| version of the active backend:: | ||
|
|
||
| from struphy.pic.kernels.box_particles import box_particles | ||
|
|
||
| The helpers they call are in :mod:`struphy.pic.sorting_kernels`. | ||
| """ |
16 changes: 16 additions & 0 deletions
16
src/struphy/pic/kernels/box_and_sort_particles/__init__.py
This file contains hidden or 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,16 @@ | ||
| """The kernel of this folder: pyccel in ``<name>_kernels.py``, CUDA in ``<name>_cuda.cu`` once ported. | ||
|
|
||
| No CUDA version yet (#694): on the CuPy backend the pyccel kernel runs on host copies of the arrays (a warning is | ||
| emitted once), and only the arrays in ``OUTPUTS`` are copied back to the device. | ||
| """ | ||
|
|
||
| from cunumpy.kernels import Kernel | ||
|
|
||
| from struphy.utils.cuda_arguments import CUDA_STRUCTS | ||
|
|
||
| # the arguments the kernel writes to, by position: markers, holes, boxes, next_index, cumul_next_index | ||
| OUTPUTS = (0, 1, 6, 7, 8) | ||
|
|
||
| box_and_sort_particles = Kernel.from_folder( | ||
| __name__, structs=CUDA_STRUCTS, missing_cuda="fallback", host_options={"outputs": OUTPUTS} | ||
| ) |
43 changes: 43 additions & 0 deletions
43
src/struphy/pic/kernels/box_and_sort_particles/box_and_sort_particles_kernels.py
This file contains hidden or 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,43 @@ | ||
| """Assign boxes, sort by box and rebuild the box array in one call, see :meth:`~struphy.pic.base.Particles.do_sort`.""" | ||
|
|
||
| # module import (not names): the module may still be an older compiled version until `struphy compile` runs | ||
| import struphy.pic.sorting_kernels as sorting_kernels | ||
|
|
||
|
|
||
| def box_and_sort_particles( | ||
| markers: "float[:,:]", | ||
| holes: "bool[:]", | ||
| nx: "int", | ||
| ny: "int", | ||
| nz: "int", | ||
| domain_array: "float[:]", | ||
| boxes: "int[:,:]", | ||
| next_index: "int[:]", | ||
| cumul_next_index: "int[:]", | ||
| swap_line_1: "float[:]", | ||
| swap_line_2: "float[:]", | ||
| box_index: "int" = -2, | ||
| ) -> "int": | ||
| """``box_particles`` followed by ``sort_and_fill_boxes``: write the box of each particle into column ``box_index``, | ||
| count the particles per box, reorder the marker rows by box, then update ``holes`` and fill ``boxes`` for the | ||
| sorted rows. On the CuPy backend the markers cross between device and host once in each direction. | ||
|
|
||
| Returns the largest number of particles in a box; if it is ``>= boxes.shape[1]``, nothing is sorted, ``boxes`` is | ||
| left untouched and the caller must raise. | ||
| """ | ||
| sorting_kernels.assign_box_to_each_particle(markers, holes, nx, ny, nz, domain_array, box_index) | ||
| max_in_box = sorting_kernels.count_particles_in_boxes(markers, holes, next_index, box_index) | ||
| if max_in_box < boxes.shape[1]: | ||
| sorting_kernels.sort_boxed_particles( | ||
| markers, | ||
| swap_line_1, | ||
| swap_line_2, | ||
| next_index.shape[0], | ||
| next_index, | ||
| cumul_next_index, | ||
| box_index, | ||
| ) | ||
| sorting_kernels.find_holes(markers, holes) | ||
| # the number of particles per box is unchanged, so they still fit into the box array | ||
| sorting_kernels.assign_particles_to_boxes(markers, holes, boxes, next_index, box_index) | ||
| return max_in_box |
This file contains hidden or 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,16 @@ | ||
| """The kernel of this folder: pyccel in ``<name>_kernels.py``, CUDA in ``<name>_cuda.cu`` once ported. | ||
|
|
||
| No CUDA version yet (#694): on the CuPy backend the pyccel kernel runs on host copies of the arrays (a warning is | ||
| emitted once), and only the arrays in ``OUTPUTS`` are copied back to the device. | ||
| """ | ||
|
|
||
| from cunumpy.kernels import Kernel | ||
|
|
||
| from struphy.utils.cuda_arguments import CUDA_STRUCTS | ||
|
|
||
| # the arguments the kernel writes to, by position: markers, boxes, next_index | ||
| OUTPUTS = (0, 6, 7) | ||
|
|
||
| box_particles = Kernel.from_folder( | ||
| __name__, structs=CUDA_STRUCTS, missing_cuda="fallback", host_options={"outputs": OUTPUTS} | ||
| ) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.