All Projects → lloda → ra-ra

lloda / ra-ra

Licence: LGPL-3.0 License
A C++20 array / expression template library with some J/APL features

Programming Languages

C++
36643 projects - #6 most used programming language
python
139335 projects - #7 most used programming language
CMake
9771 projects

Projects that are alternatives of or similar to ra-ra

Massiv
Efficient Haskell Arrays featuring Parallel computation
Stars: ✭ 328 (+1390.91%)
Mutual labels:  stencil, array, multidimensional-arrays
Arrayy
🗃 Array manipulation library for PHP, called Arrayy!
Stars: ✭ 363 (+1550%)
Mutual labels:  array, multidimensional-arrays
Pyopencl
OpenCL integration for Python, plus shiny features
Stars: ✭ 790 (+3490.91%)
Mutual labels:  array, multidimensional-arrays
Loopy
A code generator for array-based code on CPUs and GPUs
Stars: ✭ 367 (+1568.18%)
Mutual labels:  array, multidimensional-arrays
Pycuda
CUDA integration for Python, plus shiny features
Stars: ✭ 1,112 (+4954.55%)
Mutual labels:  array, multidimensional-arrays
solar-components
Web Components Implementation of Solar Design System
Stars: ✭ 16 (-27.27%)
Mutual labels:  stencil
array
Pack of advanced PHP array functions
Stars: ✭ 45 (+104.55%)
Mutual labels:  array
yask
YASK--Yet Another Stencil Kit: a domain-specific language and framework to create high-performance stencil code for implementing finite-difference methods and similar applications.
Stars: ✭ 81 (+268.18%)
Mutual labels:  stencil
array-sort-by
Powerful mechanism to sort arrays or array of objects by one or more properties. You can also specify a custom comparer function.
Stars: ✭ 37 (+68.18%)
Mutual labels:  array
arraync
Async Array methods polyfills
Stars: ✭ 16 (-27.27%)
Mutual labels:  array
ss-search
The most basic, yet powerful text search.
Stars: ✭ 41 (+86.36%)
Mutual labels:  array
domy
Custom Elements Storage
Stars: ✭ 77 (+250%)
Mutual labels:  stencil
lazy-arr
Arrays that look just like regular JavaScript arrays, but are computed lazily.
Stars: ✭ 67 (+204.55%)
Mutual labels:  array
stock-prices
Analyzing stock prices with Fortran arrays
Stars: ✭ 24 (+9.09%)
Mutual labels:  array
stencil-snippets
An extension to add some snippets on vs code
Stars: ✭ 21 (-4.55%)
Mutual labels:  stencil
linqjs
use linq and lambda in javascript on es6, can use linq function in an Object or an Array or a String value | 一个方便对数组、字典、树形数据进行操作、筛选等操作的工具库
Stars: ✭ 17 (-22.73%)
Mutual labels:  array
prototyped.js
Some common Typescript prototypes
Stars: ✭ 22 (+0%)
Mutual labels:  array
ustd
Micro-standard-library providing minimal and portable array, queue and map for attiny avr, arduinos, esp8266/32 and linux, mac
Stars: ✭ 14 (-36.36%)
Mutual labels:  array
utils.js
👷 🔧 zero dependencies vanilla JavaScript utils.
Stars: ✭ 14 (-36.36%)
Mutual labels:  array
nuxt-stencil
Easy Stencil.js component library integration with Nuxt.js.
Stars: ✭ 16 (-27.27%)
Mutual labels:  stencil

ra-ra (travis build status)

ra-ra is a C++20, header-only multidimensional array library in the spirit of Blitz++.

Multidimensional arrays are containers that can be indexed in multiple dimensions. For example, vectors are arrays of rank 1 and matrices are arrays of rank 2. C has built-in multidimensional array types, but even in modern C++ there's very little you can do with those, and a separate library is required for any practical endeavor.

ra-ra implements expression templates. This is a C++ technique (pioneered by Blitz++) to delay the execution of expressions involving large array operands, and in this way avoid the unnecessary creation of large temporary array objects.

ra-ra tries to distinguish itself from established C++ libraries in this space (such as Eigen or Boost.MultiArray) by being more APLish, more general, smaller, and more hackable.

In this example (examples/readme.cc), we add each element of a vector to each row of a matrix, and then print the result.

#include "ra/ra.hh"
#include <iostream>

int main()
{
  ra::Big<float, 2> A {{1, 2}, {3, 4}};  // compile-time rank, dynamic shape
  A += std::vector<float> {10, 20};      // rank-extending op with STL object
  std::cout << "A: " << A << std::endl;  // shape is dynamic, so it will be printed
}

A: 2 2
11 12
23 24

Please check the manual online at lloda.github.io/ra-ra, or have a look at the examples/ folder.

ra-ra offers:

  • Array types with arbitrary compile time or runtime rank, and compile time or runtime shape.
  • Memory owning types as well as views over any piece of memory.
  • Rank extension by prefix matching, as in APL/J, for functions of any number of arguments.
  • Compatibility with builtin arrays and with the STL.
  • Transparent memory layout, for interoperability with other libraries and/or languages.
  • Iterators over cells (slices/subarrays) of any rank.
  • Rank conjunction as in J, with some limitations.
  • Slicing with indices of arbitrary rank, beating of linear range indices, index skipping and elision.
  • Outer product operation.
  • Tensor index object.
  • Short-circuiting logical operators.
  • Argument list selection operators (where with bool selector, or pick with integer selector).
  • Axis insertion (e.g. for broadcasting).
  • Reshape, transpose, reverse, collapse/explode, stencils.
  • Arbitrary types as array elements, or as scalar operands.
  • Many predefined array operations. Adding yours is trivial.

There is some constexpr support for the compile time size types. For example, this works:

constexpr ra::Small<int, 3> a = { 1, 2, 3 };
using T = std::integral_constant<int, ra::sum(a)>;
static_assert(T::value==6);

Performance is competitive with hand written scalar (element by element) loops, but probably not with cache-tuned code such as your platform BLAS, or with code using SIMD. Please have a look at the benchmarks in bench/.

Building the tests and the benchmarks

The library itself is header-only and has no dependencies other than a C++20 compiler and the standard library.

The test suite in test/ runs under either SCons (CXXFLAGS=-O3 scons) or CMake (CXXFLAGS=-O3 cmake . && make && make test). Running the test suite will also build and run the examples (examples/) and the benchmarks (bench/), although you can also build each of these separately. None of them has any dependencies, but some of the benchmarks will try to use BLAS if you have RA_USE_BLAS=1 in the environment.

The tests pass under gcc 10.3/11.2 (earlier versions don't support -std=c++20 or have bugs). Remember to pass -O2 or -O3 to the compiler, otherwise some of the tests will take a very long time to run. Clang 10 doesn't currently work (I'll keep trying) but the code is meant to be standard C++.

Notes

  • Both index and size types are signed. Index base is 0.
  • Default array order is C or row-major (last dimension changes fastest). You can make array views with other orders, but newly created arrays use C-order.
  • The selection (subscripting) operator is (). [] means exactly the same as (). It's unfortunate that [] was wasted on subscripting when () works perfectly well for that...
  • Indices are checked by default. This can be disabled with a compilation flag.
  • ra-ra doesn't itself use exceptions, but it provides a hook so you can throw your own exceptions on ra-ra errors. See ‘Error handling’ in the manual.

Bugs & defects

  • Lack of good reduction mechanisms.
  • Operations that require allocation, such as concatenation or search, are mostly absent.
  • Traversal of arrays is naive (just unrolling of inner dimensions).
  • Handling of nested (‘ragged’) arrays is inconsistent.
  • No SIMD to speak of.

Please have a look at TODO for a concrete list of known bugs.

Out of scope

  • Parallelization (closer to wish...).
  • GPU / calls to external libraries.
  • Linear algebra, quaternions, etc. Those things belong in other libraries, and calling them with ra-ra objects is trivial.
  • Sparse arrays. You'd still want to mix & match with dense arrays, so maybe at some point.

Motivation

I do numerical work in C++, so I need a library of this kind. Most C++ array libraries seem to support only vectors and matrices, or small objects for low-dimensional vector algebra. Blitz++ was a great early generic array library (even though the focus was numerical) and it hasn't really been replaced as far as I can tell.

It was a heroic feat to write a library such as Blitz++ in C++ in the late 90s, even discounting the fragmented compiler landscape and the patchy support for the standard at that time. Variadic templates, lambdas, rvalue arguments, etc. make things much simpler, for the library writer as well as for the user.

From APL and J I've taken the rank extension mechanism, and perhaps an inclination for carrying each feature to its logical end.

ra-ra wants to remain a simple library. I try not to second-guess the compiler and I don't stress performance as much as Blitz++ did. However, I'm wary of adding features that could become an obstacle if I ever tried to make things fast(er). I believe that the implementation of new traversal methods, or perhaps the optimization of specific expression patterns, should be possible without having to turn the library inside out.

Other C++ array libraries

Links

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].