2015-04-22 14:23:55 -04:00
|
|
|
#ifndef included_ArrayClass
|
|
|
|
|
#define included_ArrayClass
|
2013-10-08 22:29:21 -04:00
|
|
|
|
2016-05-24 10:34:33 -04:00
|
|
|
#include <vector>
|
|
|
|
|
#include <array>
|
|
|
|
|
#include <functional>
|
2013-10-08 22:29:21 -04:00
|
|
|
#include <iostream>
|
2016-05-24 10:34:33 -04:00
|
|
|
#include <stdexcept>
|
2015-04-22 14:23:55 -04:00
|
|
|
#include <memory>
|
2016-05-24 10:34:33 -04:00
|
|
|
#include <iostream>
|
|
|
|
|
|
2013-10-08 22:29:21 -04:00
|
|
|
|
2016-05-24 10:34:33 -04:00
|
|
|
#define ARRAY_NDIM_MAX 5 // Maximum number of dimensions supported
|
2015-04-16 10:54:58 -04:00
|
|
|
|
2016-05-24 10:34:33 -04:00
|
|
|
|
|
|
|
|
#define GET_ARRAY_INDEX3D( N, i1, i2, i3 ) i1 + N[0] * ( i2 + N[1] * i3 )
|
|
|
|
|
#define GET_ARRAY_INDEX4D( N, i1, i2, i3, i4 ) i1 + N[0] * ( i2 + N[1] * ( i3 + N[2] * i4 ) )
|
|
|
|
|
#define GET_ARRAY_INDEX5D( N, i1, i2, i3, i4, i5 ) i1 + N[0] * ( i2 + N[1] * ( i3 + N[2] * ( i4 + N[3] * i5 ) ) )
|
|
|
|
|
|
|
|
|
|
#if defined( DEBUG ) || defined( _DEBUG )
|
|
|
|
|
#define CHECK_ARRAY_INDEX3D( N, i1, i2, i3 ) \
|
|
|
|
|
if ( GET_ARRAY_INDEX3D( N, i1, i2, i3 ) < 0 || GET_ARRAY_INDEX3D( N, i1, i2, i3 ) >= d_length ) \
|
|
|
|
|
throw std::logic_error( "Index exceeds array bounds" );
|
|
|
|
|
#define CHECK_ARRAY_INDEX4D( N, i1, i2, i3, i4 ) \
|
|
|
|
|
if ( GET_ARRAY_INDEX4D( N, i1, i2, i3, i4 ) < 0 || \
|
|
|
|
|
GET_ARRAY_INDEX4D( N, i1, i2, i3, i4 ) >= d_length ) \
|
|
|
|
|
throw std::logic_error( "Index exceeds array bounds" );
|
2015-04-16 10:54:58 -04:00
|
|
|
#else
|
2016-05-24 10:34:33 -04:00
|
|
|
#define CHECK_ARRAY_INDEX3D( N, i1, i2, i3 )
|
|
|
|
|
#define CHECK_ARRAY_INDEX4D( N, i1, i2, i3, i4 )
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if defined( __CUDA_ARCH__ )
|
|
|
|
|
#include <cuda.h>
|
|
|
|
|
#define HOST_DEVICE __host__ __device__
|
|
|
|
|
#else
|
|
|
|
|
#define HOST_DEVICE
|
2015-04-16 10:54:58 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
2015-04-22 14:23:55 -04:00
|
|
|
/*!
|
2016-05-24 10:34:33 -04:00
|
|
|
* Class Array is a multi-dimensional array class written by Mark Berrill
|
2013-10-08 22:29:21 -04:00
|
|
|
*/
|
2016-05-24 10:34:33 -04:00
|
|
|
template <class TYPE>
|
2015-04-22 14:23:55 -04:00
|
|
|
class Array
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
/*!
|
|
|
|
|
* Create a new empty Array
|
|
|
|
|
*/
|
2016-05-24 10:34:33 -04:00
|
|
|
Array();
|
2015-04-22 14:23:55 -04:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Create a new 1D Array with the given number of elements
|
|
|
|
|
* @param N Number of elements in the array
|
|
|
|
|
*/
|
2016-05-24 10:34:33 -04:00
|
|
|
explicit Array( size_t N );
|
2015-04-22 14:23:55 -04:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Create a new 2D Array with the given number of rows and columns
|
|
|
|
|
* @param N_rows Number of rows
|
|
|
|
|
* @param N_columns Number of columns
|
|
|
|
|
*/
|
2016-05-24 10:34:33 -04:00
|
|
|
explicit Array( size_t N_rows, size_t N_columns );
|
2015-04-22 14:23:55 -04:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Create a new 3D Array with the given number of rows and columns
|
|
|
|
|
* @param N1 Number of rows
|
|
|
|
|
* @param N2 Number of columns
|
|
|
|
|
* @param N3 Number of elements in the third dimension
|
|
|
|
|
*/
|
2016-05-24 10:34:33 -04:00
|
|
|
explicit Array( size_t N1, size_t N2, size_t N3 );
|
2015-04-22 14:23:55 -04:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Create a multi-dimensional Array with the given number of elements
|
|
|
|
|
* @param N Number of elements in each dimension
|
2016-05-24 10:34:33 -04:00
|
|
|
* @param data Optional raw array to copy the src data
|
2015-04-22 14:23:55 -04:00
|
|
|
*/
|
2016-05-24 10:34:33 -04:00
|
|
|
explicit Array( const std::vector<size_t> &N, const TYPE *data = NULL );
|
2015-04-22 14:23:55 -04:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Copy constructor
|
|
|
|
|
* @param rhs Array to copy
|
|
|
|
|
*/
|
2016-05-24 10:34:33 -04:00
|
|
|
Array( const Array &rhs );
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Move constructor
|
|
|
|
|
* @param rhs Array to copy
|
|
|
|
|
*/
|
|
|
|
|
Array( Array &&rhs );
|
2015-04-22 14:23:55 -04:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Assignment operator
|
|
|
|
|
* @param rhs Array to copy
|
|
|
|
|
*/
|
2016-05-24 10:34:33 -04:00
|
|
|
Array &operator=( const Array &rhs );
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Move assignment operator
|
|
|
|
|
* @param rhs Array to copy
|
|
|
|
|
*/
|
|
|
|
|
Array &operator=( Array &&rhs );
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Assignment operator
|
|
|
|
|
* @param rhs std::vector to copy
|
|
|
|
|
*/
|
|
|
|
|
Array &operator=( const std::vector<TYPE> &rhs );
|
|
|
|
|
|
|
|
|
|
|
2015-04-22 14:23:55 -04:00
|
|
|
/*!
|
|
|
|
|
* Create a 1D Array view to a raw block of data
|
|
|
|
|
* @param N Number of elements in the array
|
|
|
|
|
* @param data Pointer to the data
|
|
|
|
|
*/
|
2016-05-24 10:34:33 -04:00
|
|
|
static std::shared_ptr<Array> view( size_t N, std::shared_ptr<TYPE> const &data );
|
2015-04-22 14:23:55 -04:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Create a new 2D Array with the given number of rows and columns
|
|
|
|
|
* @param N_rows Number of rows
|
|
|
|
|
* @param N_columns Number of columns
|
|
|
|
|
* @param data Pointer to the data
|
|
|
|
|
*/
|
2016-05-24 10:34:33 -04:00
|
|
|
static std::shared_ptr<Array> view(
|
|
|
|
|
size_t N_rows, size_t N_columns, std::shared_ptr<TYPE> const &data );
|
2015-04-22 14:23:55 -04:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Create a new 3D Array view to a raw block of data
|
|
|
|
|
* @param N1 Number of rows
|
|
|
|
|
* @param N2 Number of columns
|
|
|
|
|
* @param N3 Number of elements in the third dimension
|
|
|
|
|
* @param data Pointer to the data
|
|
|
|
|
*/
|
2016-05-24 10:34:33 -04:00
|
|
|
static std::shared_ptr<Array> view(
|
|
|
|
|
size_t N1, size_t N2, size_t N3, std::shared_ptr<TYPE> const &data );
|
2015-04-22 14:23:55 -04:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Create a multi-dimensional Array view to a raw block of data
|
|
|
|
|
* @param N Number of elements in each dimension
|
2016-05-24 10:34:33 -04:00
|
|
|
* @param data Pointer to the data
|
2015-04-22 14:23:55 -04:00
|
|
|
*/
|
2016-05-24 10:34:33 -04:00
|
|
|
static std::shared_ptr<Array> view(
|
|
|
|
|
const std::vector<size_t> &N, std::shared_ptr<TYPE> const &data );
|
|
|
|
|
|
2015-04-22 14:23:55 -04:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Create a 1D Array view to a raw block of data
|
|
|
|
|
* @param N Number of elements in the array
|
|
|
|
|
* @param data Pointer to the data
|
|
|
|
|
*/
|
2016-05-24 10:34:33 -04:00
|
|
|
static std::shared_ptr<const Array> constView(
|
|
|
|
|
size_t N, std::shared_ptr<const TYPE> const &data );
|
2015-04-22 14:23:55 -04:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Create a new 2D Array with the given number of rows and columns
|
|
|
|
|
* @param N_rows Number of rows
|
|
|
|
|
* @param N_columns Number of columns
|
|
|
|
|
* @param data Pointer to the data
|
|
|
|
|
*/
|
2016-05-24 10:34:33 -04:00
|
|
|
static std::shared_ptr<const Array> constView(
|
|
|
|
|
size_t N_rows, size_t N_columns, std::shared_ptr<const TYPE> const &data );
|
2015-04-22 14:23:55 -04:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Create a new 3D Array view to a raw block of data
|
|
|
|
|
* @param N1 Number of rows
|
|
|
|
|
* @param N2 Number of columns
|
|
|
|
|
* @param N3 Number of elements in the third dimension
|
|
|
|
|
* @param data Pointer to the data
|
|
|
|
|
*/
|
2016-05-24 10:34:33 -04:00
|
|
|
static std::shared_ptr<const Array> constView(
|
|
|
|
|
size_t N1, size_t N2, size_t N3, std::shared_ptr<const TYPE> const &data );
|
2015-04-22 14:23:55 -04:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Create a multi-dimensional Array view to a raw block of data
|
|
|
|
|
* @param N Number of elements in each dimension
|
2016-05-24 10:34:33 -04:00
|
|
|
* @param data Pointer to the data
|
|
|
|
|
*/
|
|
|
|
|
static std::shared_ptr<const Array> constView(
|
|
|
|
|
const std::vector<size_t> &N, std::shared_ptr<const TYPE> const &data );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Make this object a view of the src
|
|
|
|
|
* @param src Source vector to take the view of
|
|
|
|
|
*/
|
|
|
|
|
void view2( Array &src );
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Make this object a view of the data
|
|
|
|
|
* @param N Number of elements in each dimension
|
|
|
|
|
* @param data Pointer to the data
|
|
|
|
|
*/
|
|
|
|
|
void view2( const std::vector<size_t> &N, std::shared_ptr<TYPE> const &data );
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Make this object a view of the raw data (expert use only).
|
|
|
|
|
* Use view2( N, std::shared_ptr(data,[](TYPE*){}) ) instead.
|
|
|
|
|
* Note: this interface is not recommended as it does not protect from
|
|
|
|
|
* the src data being deleted while still being used by the Array.
|
|
|
|
|
* Additionally for maximum performance it does not set the internal shared_ptr
|
|
|
|
|
* so functions like getPtr and resize will not work correctly.
|
|
|
|
|
* @param N Number of elements in each dimension
|
|
|
|
|
* @param data Pointer to the data
|
2015-04-22 14:23:55 -04:00
|
|
|
*/
|
2016-05-24 10:34:33 -04:00
|
|
|
void viewRaw( const std::initializer_list<size_t> &N, TYPE *data );
|
2015-04-22 14:23:55 -04:00
|
|
|
|
2016-05-24 10:34:33 -04:00
|
|
|
/*!
|
|
|
|
|
* Make this object a view of the raw data (expert use only).
|
|
|
|
|
* Use view2( N, std::shared_ptr(data,[](TYPE*){}) ) instead.
|
|
|
|
|
* Note: this interface is not recommended as it does not protect from
|
|
|
|
|
* the src data being deleted while still being used by the Array.
|
|
|
|
|
* Additionally for maximum performance it does not set the internal shared_ptr
|
|
|
|
|
* so functions like getPtr and resize will not work correctly.
|
|
|
|
|
* @param N Number of elements in each dimension
|
|
|
|
|
* @param data Pointer to the data
|
|
|
|
|
*/
|
|
|
|
|
void viewRaw( const std::vector<size_t> &N, TYPE *data );
|
2015-04-22 14:23:55 -04:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Convert an array of one type to another. This may or may not allocate new memory.
|
|
|
|
|
* @param array Input array
|
|
|
|
|
*/
|
2016-05-24 10:34:33 -04:00
|
|
|
template <class TYPE2>
|
|
|
|
|
static std::shared_ptr<Array<TYPE2>> convert( std::shared_ptr<Array<TYPE>> array );
|
2015-04-22 14:23:55 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Convert an array of one type to another. This may or may not allocate new memory.
|
|
|
|
|
* @param array Input array
|
|
|
|
|
*/
|
2016-05-24 10:34:33 -04:00
|
|
|
template <class TYPE2>
|
|
|
|
|
static std::shared_ptr<const Array<TYPE2>> convert( std::shared_ptr<const Array<TYPE>> array );
|
2015-04-22 14:23:55 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Copy and convert data from another array to this array
|
|
|
|
|
* @param array Source array
|
|
|
|
|
*/
|
2016-05-24 10:34:33 -04:00
|
|
|
template <class TYPE2>
|
|
|
|
|
void copy( const Array<TYPE2> &array );
|
2015-04-22 14:23:55 -04:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Copy and convert data from a raw vector to this array.
|
|
|
|
|
* Note: The current array must be allocated to the proper size first.
|
|
|
|
|
* @param array Source array
|
|
|
|
|
*/
|
2016-05-24 10:34:33 -04:00
|
|
|
template <class TYPE2>
|
|
|
|
|
void copy( const TYPE2 *array );
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Copy and convert data from this array to a raw vector.
|
|
|
|
|
* @param array Source array
|
|
|
|
|
*/
|
|
|
|
|
template <class TYPE2>
|
|
|
|
|
void copyTo( TYPE2 *array ) const;
|
|
|
|
|
|
2015-04-22 14:23:55 -04:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Fill the array with the given value
|
|
|
|
|
* @param value Value to fill
|
|
|
|
|
*/
|
2016-05-24 10:34:33 -04:00
|
|
|
void fill( const TYPE &value );
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Scale the array by the given value
|
|
|
|
|
* @param scale Value to scale by
|
|
|
|
|
*/
|
|
|
|
|
void scale( const TYPE &scale );
|
2015-04-22 14:23:55 -04:00
|
|
|
|
2016-06-15 09:24:59 -04:00
|
|
|
/*!
|
|
|
|
|
* Set the values of this array to pow(base, exp)
|
|
|
|
|
* @param base Base array
|
|
|
|
|
* @param exp Exponent value
|
|
|
|
|
*/
|
|
|
|
|
void pow( const Array<TYPE> &baseArray, const TYPE &exp );
|
2015-04-22 14:23:55 -04:00
|
|
|
|
|
|
|
|
//! Destructor
|
2016-05-24 10:34:33 -04:00
|
|
|
~Array();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//! Clear the data in the array
|
|
|
|
|
void clear();
|
2015-04-22 14:23:55 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
//! Return the size of the Array
|
2016-05-24 10:34:33 -04:00
|
|
|
inline int ndim() const { return d_ndim; }
|
2015-04-22 14:23:55 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
//! Return the size of the Array
|
2016-06-15 09:24:59 -04:00
|
|
|
inline std::vector<size_t> size() const { return std::vector<size_t>( d_N, d_N + d_ndim ); }
|
2015-04-22 14:23:55 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
//! Return the size of the Array
|
|
|
|
|
inline size_t size( int d ) const { return d_N[d]; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//! Return the size of the Array
|
2016-05-24 10:34:33 -04:00
|
|
|
inline size_t length() const { return d_length; }
|
2015-04-22 14:23:55 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
//! Return true if the Array is empty
|
2016-05-24 10:34:33 -04:00
|
|
|
inline bool empty() const { return d_length == 0; }
|
2015-04-22 14:23:55 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Resize the Array
|
|
|
|
|
* @param N NUmber of elements
|
|
|
|
|
*/
|
|
|
|
|
void resize( size_t N );
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Resize the Array
|
|
|
|
|
* @param N_rows Number of rows
|
|
|
|
|
* @param N_columns Number of columns
|
|
|
|
|
*/
|
|
|
|
|
void resize( size_t N_rows, size_t N_columns );
|
2013-10-08 22:29:21 -04:00
|
|
|
|
2015-04-22 14:23:55 -04:00
|
|
|
/*!
|
|
|
|
|
* Resize the Array
|
|
|
|
|
* @param N1 Number of rows
|
|
|
|
|
* @param N2 Number of columns
|
|
|
|
|
* @param N3 Number of elements in the third dimension
|
|
|
|
|
*/
|
|
|
|
|
void resize( size_t N1, size_t N2, size_t N3 );
|
2013-10-08 22:29:21 -04:00
|
|
|
|
2015-04-22 14:23:55 -04:00
|
|
|
/*!
|
|
|
|
|
* Resize the Array
|
|
|
|
|
* @param N Number of elements in each dimension
|
|
|
|
|
*/
|
2016-05-24 10:34:33 -04:00
|
|
|
void resize( const std::vector<size_t> &N );
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Resize the given dimension of the array
|
|
|
|
|
* @param dim The dimension to resize
|
|
|
|
|
* @param N Number of elements for the given dimension
|
|
|
|
|
* @param value Value to initialize new elements
|
|
|
|
|
*/
|
|
|
|
|
void resizeDim( int dim, size_t N, const TYPE &value );
|
2013-10-08 22:29:21 -04:00
|
|
|
|
|
|
|
|
|
2015-04-22 14:23:55 -04:00
|
|
|
/*!
|
|
|
|
|
* Reshape the Array (total size of array will not change)
|
|
|
|
|
* @param N Number of elements in each dimension
|
|
|
|
|
*/
|
2016-05-24 10:34:33 -04:00
|
|
|
void reshape( const std::vector<size_t> &N );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Subset the Array (total size of array will not change)
|
|
|
|
|
* @param index Index to subset (imin,imax,jmin,jmax,kmin,kmax,...)
|
|
|
|
|
*/
|
|
|
|
|
template<class TYPE2=TYPE>
|
|
|
|
|
Array<TYPE2> subset( const std::vector<size_t> &index ) const;
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Copy data from an array into a subset of this array
|
|
|
|
|
* @param index Index of the subset (imin,imax,jmin,jmax,kmin,kmax,...)
|
|
|
|
|
* @param subset The subset array to copy from
|
|
|
|
|
*/
|
|
|
|
|
template <class TYPE2>
|
|
|
|
|
void copySubset( const std::vector<size_t> &index, const Array<TYPE2> &subset );
|
2013-10-08 22:29:21 -04:00
|
|
|
|
2016-06-15 09:24:59 -04:00
|
|
|
/*!
|
|
|
|
|
* Add data from an array into a subset of this array
|
|
|
|
|
* @param index Index of the subset (imin,imax,jmin,jmax,kmin,kmax,...)
|
|
|
|
|
* @param subset The subset array to add from
|
|
|
|
|
*/
|
|
|
|
|
void addSubset( const std::vector<size_t> &index, const Array<TYPE> &subset );
|
|
|
|
|
|
2013-10-08 22:29:21 -04:00
|
|
|
|
2015-04-22 14:23:55 -04:00
|
|
|
/*!
|
|
|
|
|
* Access the desired element
|
|
|
|
|
* @param i The row index
|
|
|
|
|
*/
|
2016-05-24 10:34:33 -04:00
|
|
|
HOST_DEVICE inline TYPE &operator()( size_t i )
|
|
|
|
|
{
|
|
|
|
|
CHECK_ARRAY_INDEX3D( d_N, i, 0, 0 ) return d_data[i];
|
|
|
|
|
}
|
2013-10-08 22:29:21 -04:00
|
|
|
|
2015-04-22 14:23:55 -04:00
|
|
|
/*!
|
|
|
|
|
* Access the desired element
|
|
|
|
|
* @param i The row index
|
|
|
|
|
*/
|
2016-05-24 10:34:33 -04:00
|
|
|
HOST_DEVICE inline const TYPE &operator()( size_t i ) const
|
|
|
|
|
{
|
|
|
|
|
CHECK_ARRAY_INDEX3D( d_N, i, 0, 0 ) return d_data[i];
|
|
|
|
|
}
|
2013-10-08 22:29:21 -04:00
|
|
|
|
2015-04-22 14:23:55 -04:00
|
|
|
/*!
|
|
|
|
|
* Access the desired element
|
|
|
|
|
* @param i The row index
|
|
|
|
|
* @param j The column index
|
|
|
|
|
*/
|
2016-05-24 10:34:33 -04:00
|
|
|
HOST_DEVICE inline TYPE &operator()( size_t i, size_t j )
|
|
|
|
|
{
|
|
|
|
|
CHECK_ARRAY_INDEX3D( d_N, i, j, 0 ) return d_data[i + j * d_N[0]];
|
|
|
|
|
}
|
2013-10-08 22:29:21 -04:00
|
|
|
|
2015-04-22 14:23:55 -04:00
|
|
|
/*!
|
|
|
|
|
* Access the desired element
|
|
|
|
|
* @param i The row index
|
|
|
|
|
* @param j The column index
|
|
|
|
|
*/
|
2016-05-24 10:34:33 -04:00
|
|
|
HOST_DEVICE inline const TYPE &operator()( size_t i, size_t j ) const
|
|
|
|
|
{
|
|
|
|
|
CHECK_ARRAY_INDEX3D( d_N, i, j, 0 ) return d_data[i + j * d_N[0]];
|
|
|
|
|
}
|
2013-10-08 22:29:21 -04:00
|
|
|
|
2015-04-22 14:23:55 -04:00
|
|
|
/*!
|
|
|
|
|
* Access the desired element
|
|
|
|
|
* @param i The row index
|
|
|
|
|
* @param j The column index
|
2016-05-24 10:34:33 -04:00
|
|
|
* @param k The third index
|
2015-04-22 14:23:55 -04:00
|
|
|
*/
|
2016-05-24 10:34:33 -04:00
|
|
|
HOST_DEVICE inline TYPE &operator()( size_t i, size_t j, size_t k )
|
|
|
|
|
{
|
|
|
|
|
CHECK_ARRAY_INDEX3D( d_N, i, j, k ) return d_data[GET_ARRAY_INDEX3D( d_N, i, j, k )];
|
|
|
|
|
}
|
2013-10-08 22:29:21 -04:00
|
|
|
|
2015-04-22 14:23:55 -04:00
|
|
|
/*!
|
|
|
|
|
* Access the desired element
|
|
|
|
|
* @param i The row index
|
|
|
|
|
* @param j The column index
|
2016-05-24 10:34:33 -04:00
|
|
|
* @param k The third index
|
2015-04-22 14:23:55 -04:00
|
|
|
*/
|
2016-05-24 10:34:33 -04:00
|
|
|
HOST_DEVICE inline const TYPE &operator()( size_t i, size_t j, size_t k ) const
|
|
|
|
|
{
|
|
|
|
|
CHECK_ARRAY_INDEX3D( d_N, i, j, k ) return d_data[GET_ARRAY_INDEX3D( d_N, i, j, k )];
|
|
|
|
|
}
|
2015-04-28 11:34:57 -04:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Access the desired element
|
|
|
|
|
* @param i The row index
|
|
|
|
|
* @param j The column index
|
2016-05-24 10:34:33 -04:00
|
|
|
* @param k The third index
|
|
|
|
|
* @param l The fourth index
|
2015-04-28 11:34:57 -04:00
|
|
|
*/
|
2016-05-24 10:34:33 -04:00
|
|
|
HOST_DEVICE inline TYPE &operator()( size_t i, size_t j, size_t k, size_t l )
|
|
|
|
|
{
|
|
|
|
|
CHECK_ARRAY_INDEX4D( d_N, i, j, k, l ) return d_data[GET_ARRAY_INDEX4D( d_N, i, j, k, l )];
|
|
|
|
|
}
|
2015-04-28 11:34:57 -04:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Access the desired element
|
|
|
|
|
* @param i The row index
|
|
|
|
|
* @param j The column index
|
2016-05-24 10:34:33 -04:00
|
|
|
* @param k The third index
|
|
|
|
|
* @param l The fourth index
|
2015-04-28 11:34:57 -04:00
|
|
|
*/
|
2016-05-24 10:34:33 -04:00
|
|
|
HOST_DEVICE inline const TYPE &operator()( size_t i, size_t j, size_t k, size_t l ) const
|
|
|
|
|
{
|
|
|
|
|
CHECK_ARRAY_INDEX4D( d_N, i, j, k, l ) return d_data[GET_ARRAY_INDEX4D( d_N, i, j, k, l )];
|
|
|
|
|
}
|
2013-10-08 22:29:21 -04:00
|
|
|
|
2016-05-24 10:34:33 -04:00
|
|
|
//! Check if two matrices are equal
|
|
|
|
|
// Equality means the dimensions and data have to be identical
|
|
|
|
|
bool operator==( const Array &rhs ) const;
|
2013-10-08 22:29:21 -04:00
|
|
|
|
2016-05-24 10:34:33 -04:00
|
|
|
//! Check if two matrices are not equal
|
|
|
|
|
inline bool operator!=( const Array &rhs ) const { return !this->operator==( rhs ); }
|
2013-10-08 22:29:21 -04:00
|
|
|
|
|
|
|
|
|
2015-04-22 14:23:55 -04:00
|
|
|
//! Return the pointer to the raw data
|
2016-05-24 10:34:33 -04:00
|
|
|
inline std::shared_ptr<TYPE> getPtr() { return d_ptr; }
|
2015-04-22 14:23:55 -04:00
|
|
|
|
|
|
|
|
//! Return the pointer to the raw data
|
2016-05-24 10:34:33 -04:00
|
|
|
inline std::shared_ptr<const TYPE> getPtr() const { return d_ptr; }
|
2015-04-22 14:23:55 -04:00
|
|
|
|
|
|
|
|
//! Return the pointer to the raw data
|
2016-05-24 10:34:33 -04:00
|
|
|
HOST_DEVICE inline TYPE *data() { return d_data; }
|
2015-04-22 14:23:55 -04:00
|
|
|
|
|
|
|
|
//! Return the pointer to the raw data
|
2016-05-24 10:34:33 -04:00
|
|
|
HOST_DEVICE inline const TYPE *data() const { return d_data; }
|
2015-04-22 14:23:55 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
//! Return true if NaNs are present
|
2016-05-24 10:34:33 -04:00
|
|
|
inline bool NaNs() const;
|
2015-04-22 14:23:55 -04:00
|
|
|
|
|
|
|
|
//! Return the smallest value
|
2016-05-24 10:34:33 -04:00
|
|
|
inline TYPE min() const;
|
2015-04-22 14:23:55 -04:00
|
|
|
|
|
|
|
|
//! Return the largest value
|
2016-05-24 10:34:33 -04:00
|
|
|
inline TYPE max() const;
|
2015-04-22 14:23:55 -04:00
|
|
|
|
|
|
|
|
//! Return the sum of all elements
|
2016-05-24 10:34:33 -04:00
|
|
|
inline TYPE sum() const;
|
|
|
|
|
|
|
|
|
|
//! Return the mean of all elements
|
|
|
|
|
inline TYPE mean() const;
|
|
|
|
|
|
|
|
|
|
//! Return the min of all elements in a given direction
|
|
|
|
|
Array<TYPE> min( int dir ) const;
|
|
|
|
|
|
|
|
|
|
//! Return the max of all elements in a given direction
|
|
|
|
|
Array<TYPE> max( int dir ) const;
|
2015-04-22 14:23:55 -04:00
|
|
|
|
|
|
|
|
//! Return the sum of all elements in a given direction
|
2016-05-24 10:34:33 -04:00
|
|
|
Array<TYPE> sum( int dir ) const;
|
|
|
|
|
|
|
|
|
|
//! Return the smallest value
|
|
|
|
|
inline TYPE min( const std::vector<size_t> &index ) const;
|
|
|
|
|
|
|
|
|
|
//! Return the largest value
|
|
|
|
|
inline TYPE max( const std::vector<size_t> &index ) const;
|
|
|
|
|
|
|
|
|
|
//! Return the sum of all elements
|
|
|
|
|
inline TYPE sum( const std::vector<size_t> &index ) const;
|
|
|
|
|
|
|
|
|
|
//! Return the mean of all elements
|
|
|
|
|
inline TYPE mean( const std::vector<size_t> &index ) const;
|
|
|
|
|
|
|
|
|
|
//! Find all elements that match the operator
|
|
|
|
|
std::vector<size_t> find(
|
|
|
|
|
const TYPE &value, std::function<bool( const TYPE &, const TYPE & )> compare ) const;
|
|
|
|
|
|
|
|
|
|
//! Add another array
|
|
|
|
|
Array &operator+=( const Array &rhs );
|
2015-04-22 14:23:55 -04:00
|
|
|
|
2016-05-24 10:34:33 -04:00
|
|
|
//! Subtract another array
|
|
|
|
|
Array &operator-=( const Array &rhs );
|
|
|
|
|
|
|
|
|
|
//! Add a scalar
|
|
|
|
|
Array &operator+=( const TYPE &rhs );
|
|
|
|
|
|
|
|
|
|
//! Subtract a scalar
|
|
|
|
|
Array &operator-=( const TYPE &rhs );
|
|
|
|
|
|
|
|
|
|
//! Print an array
|
|
|
|
|
void print( std::ostream& os, const std::string& name="A", const std::string& prefix="" ) const;
|
|
|
|
|
|
|
|
|
|
//! Multiply two arrays
|
|
|
|
|
static Array multiply( const Array& a, const Array& b );
|
|
|
|
|
|
|
|
|
|
//! Transpose an array
|
|
|
|
|
Array<TYPE> reverseDim( ) const;
|
|
|
|
|
|
|
|
|
|
//! Coarsen an array using the given filter
|
|
|
|
|
Array<TYPE> coarsen( const Array<TYPE>& filter ) const;
|
2015-04-22 14:23:55 -04:00
|
|
|
|
2016-06-15 09:24:59 -04:00
|
|
|
//! Coarsen an array using the given filter
|
|
|
|
|
Array<TYPE> coarsen( const std::vector<size_t>& ratio, std::function<TYPE(const Array<TYPE>&)> filter ) const;
|
|
|
|
|
|
2015-04-22 14:23:55 -04:00
|
|
|
private:
|
2016-05-24 10:34:33 -04:00
|
|
|
int d_ndim; // Number of dimensions in array
|
|
|
|
|
size_t d_N[ARRAY_NDIM_MAX]; // Size of each dimension
|
|
|
|
|
size_t d_length; // Total length of array
|
|
|
|
|
TYPE *d_data; // Raw pointer to data in array
|
|
|
|
|
std::shared_ptr<TYPE> d_ptr; // Shared pointer to data in array
|
|
|
|
|
void allocate( const std::vector<size_t> &N );
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
template<class TYPE2>
|
|
|
|
|
inline bool sizeMatch( const Array<TYPE2>& rhs ) const;
|
|
|
|
|
inline void checkSubsetIndex( const std::vector<size_t> &index ) const;
|
|
|
|
|
inline std::array<size_t, 5> getDimArray() const;
|
|
|
|
|
static inline void getSubsetArrays( const std::vector<size_t> &index,
|
|
|
|
|
std::array<size_t, 5> &first, std::array<size_t, 5> &last, std::array<size_t, 5> &N );
|
2015-04-22 14:23:55 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typedef Array<double> DoubleArray;
|
2016-05-24 10:34:33 -04:00
|
|
|
typedef Array<float> FloatArray;
|
|
|
|
|
typedef Array<int> IntArray;
|
2015-04-22 14:23:55 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "common/Array.hpp"
|
2013-10-08 22:29:21 -04:00
|
|
|
|
|
|
|
|
#endif
|