Kokkos Array Conversions#

group ArrayUtilities

Code for converting between different array types. Often used in bindings to other languages.

Functions

template<typename ScalarType, typename MemorySpace = Kokkos::HostSpace>
inline Kokkos::View<ScalarType*, MemorySpace> ToKokkos(ScalarType *ptr, unsigned int dim)#

Converts a pointer to a 1d unmanaged Kokkos view.

Creates a Kokkos unmanaged view around a preallocated block of memory. The unmanaged view will not free the memory so all allocations and deallocations need to be handled manually (or via another object like an Eigen::Matrix). Currently only works for memory on the Host device.

Usage Examples

Conversion from a std::vector of doubles to a a Kokkos array.

std::vector<double> array;
// fill in array here....

Kokkos::View<double*> view = ToKokkos<double>(&array[0], array.size());

Parameters:
  • ptr[in] A pointer to a block of memory defining the array. Note

  • dim[in] The length of the array.

Template Parameters:

ScalarType – The scalar type, typically double, int, or unsigned int.

Returns:

A Kokkos view wrapping around the memory pointed to by ptr.

template<typename ScalarType, typename LayoutType = Kokkos::LayoutLeft, typename MemorySpace = Kokkos::HostSpace>
inline Kokkos::View<ScalarType**, LayoutType, MemorySpace> ToKokkos(ScalarType *ptr, unsigned int rows, unsigned int cols)#

Converts a pointer to a 2d unmanaged Kokkos view.

Creates a Kokkos unmanaged view around a preallocated block of memory. The unmanaged view will not free the memory so all allocations and deallocations need to be handled manually (or via another object like an Eigen::Matrix). Currently only works for memory on the Host device.

Usage Examples

Conversion from a block of memory containing a \(N\times M\) matrix (in a column-major layout):

unsigned int N = 10;
unsigned int M = 20;
std::vector<double> array(N*M);
// fill in matrix here....

Kokkos::View<double*> view = ToKokkos<double>(&array[0], N, M);

It is also possible to specify the layout of the data (e.g., row major) by adding an additional template argument. Here is the conversion from a block of memory containing a \(N\times M\) row-major matrix:

unsigned int N = 10;
unsigned int M = 20;
std::vector<double> array(N*M);
// fill in matrix here....

Kokkos::View<double*> view = ToKokkos<double,Kokkos::LayoutRight>(&array[0], N, M);

Parameters:
  • ptr[in] A pointer to a block of memory defining the array. Note that this array must have at least rows*cols allocated after this pointer or a segfault is likely.

  • rows[in] The number of rows in the matrix.

  • cols[in] The number of columns in the matrix.

Template Parameters:
  • LayoutType – A kokkos layout type dictating whether the memory in ptr is organized in column major format or row major format. If LayoutType is Kokkos::LayoutRight, the data is treated in row major form. If LayoutType is Kokkos::LayoutLeft, the data is treated in column major form. Defaults to Kokkos::LayoutLeft.

  • ScalarType – The scalar type, typically double, int, or unsigned int.

Returns:

A 2D Kokkos view wrapping around the memory pointed to by ptr.

template<typename ScalarType, typename MemorySpace = Kokkos::HostSpace>
inline StridedMatrix<ScalarType, MemorySpace> MatToKokkos(Eigen::Ref<Eigen::Matrix<ScalarType, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>, 0, Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic>> ref)#

Converts a column major 2d Eigen::Ref of a matrix to an unmanaged Kokkos view.

Creates a Kokkos unmanaged view around an existing Eigen object. Currently only works with objects on the Host.

Note that this function returns a Kokkos::View with strided layout. This is different than the typical Kokkos::LayoutLeft and Kokkos::LayoutRight layouts typically used by default. A list of admissable conversions between layout types can be found in the Kokkos documentation.

Usage examples:

Eigen::MatrixXd A;
// Fill in A... ;

// Create a 2d view to the matrix
auto view = MatToKokkos<double>(A);
Eigen::MatrxXd A;
// Fill in A... ;

// Create a 2d view to a 10x10 block of the matrix
auto view = MatToKokkos<double>( A.block(1,2,10,10) );
Parameters:

ref[in] The reference to the eigen reference.

Template Parameters:

ScalarType – The scalar type, typically double, int, or unsigned int.

Returns:

A 2D Kokkos unmanaged view wrapping the same memory as the eigen ref and using the same strides as the eigen object.

template<typename ScalarType, typename MemorySpace = Kokkos::HostSpace>
inline StridedMatrix<ScalarType, MemorySpace> MatToKokkos(Eigen::Ref<Eigen::Matrix<ScalarType, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>, 0, Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic>> ref)#

Converts a row major 2d Eigen::Ref of a matrix to an unmanaged Kokkos view.

Creates a Kokkos unmanaged view around an existing Eigen object. Currently only works with objects on the Host.

Usage examples:

Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic,Eigen::RowMajor> A;
// Fill in A... ;

// Create a 2d view to the matrix
auto view = MatToKokkos<double>(A);
Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic,Eigen::RowMajor> A;
// Fill in A... ;

// Create a 2d view to a 10x10 block of the matrix
auto view = MatToKokkos<double>( A.block(1,2,10,10) );
Eigen::MatrixXd A;
// Fill in A... ;

// Create a 2d view to the transpose of the matrix
auto view = MatToKokkos<double>( A.transpose() );
Parameters:

ref[in] The reference to the eigen reference.

Template Parameters:

ScalarType – The scalar type, typically double, int, or unsigned int.

Returns:

A 2D Kokkos unmanaged view wrapping the same memory as the eigen ref and using the same strides as the eigen object.

template<typename ScalarType, typename MemorySpace = Kokkos::HostSpace>
inline Kokkos::View<ScalarType*, Kokkos::LayoutStride, MemorySpace> VecToKokkos(Eigen::Ref<Eigen::Matrix<ScalarType, Eigen::Dynamic, 1>, 0, Eigen::InnerStride<Eigen::Dynamic>> ref)#

Converts a 1d Eigen::Ref of a vector to an unmanaged Kokkos view.

Creates a Kokkos unmanaged view around an existing Eigen object. Currently only works with objects on the Host.

Usage examples:

Eigen::VectorXd x;
// Fill in x... ;

// Create a 1d view to the vector
auto view = VecToKokkos<double>(x);
Eigen::MatrxXd A;
// Fill in x... ;

// Create a 1d view to the first row of the matrix
auto view = VecToKokkos<double>(A.row(0));
Parameters:

ref[in] The reference to the eigen reference.

Template Parameters:

ScalarType – The scalar type, typically double, int, or unsigned int.

Returns:

A 1d Kokkos unmanaged view wrapping the same memory as the eigen ref and using the same stride as the eigen object.

template<typename ScalarType, typename ...OtherTraits>
inline Eigen::Map<Eigen::Matrix<ScalarType, Eigen::Dynamic, 1>> KokkosToVec(Kokkos::View<ScalarType*, OtherTraits...> view)#

Converts a 1d Kokkos view with contiguous memory to an Eigen::Map.

Template Parameters:
  • ScalarType – The scalar type stored in the view, typically double, int, or unsigned int.

  • OtherTraits – Additional traits, like the memory space, used to define the view.

Parameters:

view – The Kokkos::View we wish to wrap.

Returns:

Eigen::Map<Eigen::VectorXd> A map wrapped around the memory stored by the view. Note that this map will only be valid as long as the view’s memory is being managed. Segfaults could occur if the map is accessed after the view goes out of scope. To avoid this, copy the map into a vector or use the CopyKokkosToVec function.

template<typename ScalarType, typename ...OtherTraits>
inline Eigen::Map<Eigen::Matrix<ScalarType, Eigen::Dynamic, 1>, 0, Eigen::InnerStride<>> KokkosToVec(Kokkos::View<ScalarType*, Kokkos::LayoutStride, OtherTraits...> view)#

Converts a 1d Kokkos view with strided memory access to an Eigen::Map.

Template Parameters:
  • ScalarType – The scalar type stored in the view, typically double, int, or unsigned int.

  • OtherTraits – Additional traits, like the memory space, used to define the view.

Parameters:

view – The Kokkos::View we wish to wrap.

Returns:

Eigen::Map<Eigen::VectorXd> A map wrapped around the memory stored by the view. Note that this map will only be valid as long as the view’s memory is being managed. Segfaults could occur if the map is accessed after the view goes out of scope. To avoid this, copy the map into a vector or use the CopyKokkosToVec function.

template<typename ScalarType, typename ...OtherTraits>
inline Eigen::Matrix<typename std::remove_const<ScalarType>::type, Eigen::Dynamic, 1> CopyKokkosToVec(Kokkos::View<ScalarType*, OtherTraits...> view)#

Copies memory in a 1d Kokkos to an Eigen::VectorXd.

Template Parameters:
  • ScalarType – The scalar type stored in the view, typically double, int, or unsigned int.

  • OtherTraits – Additional traits, like the memory space, used to define the view.

Parameters:

view – The Kokkos::View we wish to copy.

Returns:

Eigen::Matrix<ScalarType,Eigen::Dynamic,1> An Eigen vector with a copy of the contents in the Kokkos view.