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

Create a container API for structured grids #155

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

adriendelsalle
Copy link
Contributor

@adriendelsalle adriendelsalle commented Apr 22, 2024

Description

This PR aims to refactor the container API of the grids, to next make it easier to refactor the container API of the flow related classes (graph, operators, etc.).
For most operation on the grids, there is no reason to rely on xtensor except for trivial broadcasting or selection that could be performed directly on STL containers.

It is proposed in this PR to adopt a container API unrelated to the underlying container implementation and interface the implementation using a template specialization:

  • the selection of the implementation is performed using container_selection and a type selector (typically xt_selector)
  • the wrapping of the implementation to be used in fastscapelib is exposed using container_impl
  • using declaration refering to xtensor are renamed to be agnostic: xt_ndims -> container_ndims, xt_selector -> container_selector, etc.

The grids and aliases are also interverted to better reflect the design:

  • raster_grid and profile_grid are the templated classes
  • raster_grid_xt and profile_grid_xt are specialization relying on xtensor with cache logic (and raster_connect::queen for raster grid)

At few places, STL containers are used to store info (neighbors_offsets_type was defined as xt::xtensor<xt::xtensor_fixed<std::ptrdiff_t, xt::xshape<2>>, 1> and proposed to be simplified as std::vector<std::array<std::ptrdiff_t, 2>>).

The global performance is:

  • unchanged or slightly improved when using a neighbors' cache
  • improved by ~2x when running without caching. the changes made in node_neighbors_offsets to operate over std::arrays instead of xt::xtensor is probably what gives the main impact

Also proposed in this PR is to remove hard dependency to xtensor in cmake files, and make it possible to dowstream project to select what implementation they need. This option could probably added later in another PR when the library will be completely independent from xtensor (not only the structured grids).

This refactoring is still not finished on the trimesh for which the only working container types are still the xtensor's ones.

remove unecessary references to xtensor
make the API more generic to allow future use of different container types
add container apis in xtensor_utils for now
update tests and benchs

make the grids almost independant from xtensor containers
rename typedefs to ref to container type, shape, size, etc.
add generic selectors to rely on a specific container type
add `container_impl` to allow specialization of functions required to use containers in fastscapelib grids
remove xtensor from linked libraries
pass xtensor and or eigen as deps in cmakelists files for testing, benchmarking or for python bindings
rename `raster_grid_xt` `raster_grid` and vice versa to better reflect the design
@benbovy
Copy link
Member

benbovy commented Apr 23, 2024

I've made a quick pass over the proposed changes and overall this looks great, thanks @adriendelsalle !

The grids and aliases are also interverted to better reflect the design

On one hand I find raster_grid better than raster_grid_xt as a name for the default container types (i.e., using xtensor containers) ; it is also consistent with RasterGrid in the Python bindings (where the container type is fixed). On the other hand I agree the switched class name / alias better reflect the design.

Alternatively, could we get rid of the type aliases and set a default value for all template parameters of each grid class? This way we can still use raster_grid by default (C++17) or it is fine using raster_grid<> for C++14.

STL containers are used to store info

Yes that makes sense, and the gain in performance for non-cached neighbors is very welcome (the cache can become a big waste of memory for large grids).

PR is to remove hard dependency to xtensor in cmake files, and make it possible to dowstream project to select what implementation they need.

Agreed that may be useful. We'll likely keep xtensor as the default implementation for quite some time, though.

This refactoring is still not finished on the trimesh for which the only working container types are still the xtensor's ones.

Yes this might require some effort to refactor .set_nodes_areas(), which currently relies on a lot of advanced vectorized functions available in xtensor. This can be done later.

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.

None yet

2 participants