fastscape-lem / fastscapelib

A C++/Python library of efficient algorithms for landscape evolution modeling
http://fastscapelib.readthedocs.io
GNU General Public License v3.0
36 stars 6 forks source link

Create a container API for structured grids #155

Closed adriendelsalle closed 4 months ago

adriendelsalle commented 6 months ago

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 grids and aliases are also interverted to better reflect the design:

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:

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.

benbovy commented 6 months ago

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.

adriendelsalle commented 4 months ago

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.

It also means setting a default raster connect template argument?

benbovy commented 4 months ago

It also means setting a default raster connect template argument?

Yes, in 90% of cases we use the queen connectivity.

benbovy commented 4 months ago

Merged! Thanks a lot @adriendelsalle