[Boost.] Multi
Disclosure: This is not an official or accepted Boost library and is unrelated to the std::mdspan proposal. It is in the process of being proposed for inclusion in Boost and it doesn't depend on Boost libraries.
© Alfredo A. Correa, 2018-2026
Multi is a modern C++ library that provides manipulation and access of data in multidimensional arrays for both CPU and GPU memory.
#include <cassert> // for assert
#include <multi/array.hpp> // from https://gitlab.com/correaa/boost-multi or https://gitlab.com/correaa/boost-multi
namespace multi = boost::multi;
int main() {
multi::array<int, 2> A = { // 2D array of integers
{1, 2, 3},
{4, 5, 6}
};
assert(A.size() == 2); // the array has 2 rows
assert(A.size() == A.end() - A.begin()); // interators to rows
assert(A[1][1] == 5); // element access through indexing
assert(A.elements().size() == 2 * 3); // array has 6 elements
assert(A.elements()[4] == 5); // elements gives "flat" sequences
using multi::_; // wildcard
auto&& col1 = A(_, 1); // second column
assert( col1.size() == 2 );
auto&& row1 = A(1, _); // second row
assert( row1.size() == 3 );
auto&& block = A({0, 2}, {0, 2}); // a 2x2 block
assert( block.elements().size() == 4 );
}Before installing the library, you can try it online through the Godbolt's Compiler Explorer.
Alternatively, the core of the library can be downloaded from https://correaa.gitlab.io/boost-multi/boost-multi.hpp as a single (amalgamated) header, and used locally with #include <DIR/boost-multi.hpp>,
Multi has no external dependencies and can be used immediately after downloading.
git clone https://gitlab.com/correaa/boost-multi.gitMulti doesn't require installation since it is a header-only library, and including the main header brings the code library:
// c++ -I multi_root_dir/include main.cpp
#include <boost/multi.hpp>
int main() { ... }The library can be also installed with CMake.
The header (and CMake) files will be installed in the chosen prefix location (by default, /usr/local/include/multi and /usr/local/share/multi).
cd boost-multi
mkdir -p build && cd build
cmake . -B ./build # --install-prefix=$HOME/.local
cmake --install ./build # or sudo ...Testing the library requires Boost.Core (headers), installed for example, via sudo apt install cmake git g++ libboost-test-dev make or sudo dnf install boost-devel cmake gcc-c++ git.
A CMake build system is provided to compile and run basic tests.
ctest -C ./buildOnce installed, other CMake projects (targets) can depend on Multi by adding a simple add_subdirectory(my_multi_path) or by find_package:
find_package(multi) # see https://gitlab.com/correaa/boost-multiAlternatively, the library can be fetched on demand:
include(FetchContent)
FetchContent_Declare(multi GIT_REPOSITORY https://gitlab.com/correaa/boost-multi.git)
FetchContent_MakeAvailable(multi)
...
target_link_libraries(my_target PUBLIC multi)- File a Gitlab issue or Github issue.
- Join the #boost-multi discussion group at cpplang.slack.com