From a333764916136a93e1b477fa1d41b6bbba58667c Mon Sep 17 00:00:00 2001 From: Preston Waters Date: Mon, 26 Feb 2024 16:06:46 -0500 Subject: [PATCH] clang formatting --- src/CabanaController.hpp | 255 +++++++++++++------------- src/KokkosController.hpp | 283 ++++++++++++++-------------- src/MeshField.hpp | 203 ++++++++++---------- src/MeshField_Utility.hpp | 47 ++--- src/test.hpp | 130 +++++++------ test/test.cpp | 37 ++-- test/testCabana.cpp | 218 ++++++++++++---------- test/testKokkos.cpp | 377 ++++++++++++++++++++++---------------- test/testMain.cpp | 214 ++++++++++++---------- 9 files changed, 924 insertions(+), 840 deletions(-) diff --git a/src/CabanaController.hpp b/src/CabanaController.hpp index 2589a64..92d1e12 100644 --- a/src/CabanaController.hpp +++ b/src/CabanaController.hpp @@ -1,34 +1,33 @@ #ifndef cabanaslicewrapper_hpp #define cabanaslicewrapper_hpp -#include #include +#include #include namespace Controller { -template -struct CabanaSliceWrapper { - +template struct CabanaSliceWrapper { + static const int MAX_RANK = 4; static const std::size_t RANK = Kokkos::View::rank + 1; int dimensions[MAX_RANK]; SliceType slice; typedef T Type; - CabanaSliceWrapper( SliceType slice_in, int* sizes ) : slice(slice_in) { - for( int i = 0; i < MAX_RANK; i++ ) { + CabanaSliceWrapper(SliceType slice_in, int *sizes) : slice(slice_in) { + for (int i = 0; i < MAX_RANK; i++) { dimensions[i] = sizes[i]; } } - CabanaSliceWrapper( ) {} + CabanaSliceWrapper() {} // TODO change size to extent KOKKOS_INLINE_FUNCTION - auto size( int i ) const { - assert( i >= 0 ); - assert( i <= MAX_RANK ); - return dimensions[i]; + auto size(int i) const { + assert(i >= 0); + assert(i <= MAX_RANK); + return dimensions[i]; } KOKKOS_INLINE_FUNCTION @@ -39,37 +38,36 @@ struct CabanaSliceWrapper { auto &operator()(const int i) const { auto s = SliceType::index_type::s(i); auto a = SliceType::index_type::a(i); - return slice.access(s,a); + return slice.access(s, a); } KOKKOS_INLINE_FUNCTION auto &operator()(int i, int j) const { auto s = SliceType::index_type::s(i); auto a = SliceType::index_type::a(i); - return slice.access(s,a,j); + return slice.access(s, a, j); } KOKKOS_INLINE_FUNCTION auto &operator()(int i, int j, int k) const { auto s = SliceType::index_type::s(i); auto a = SliceType::index_type::a(i); - return slice.access(s,a,j,k); + return slice.access(s, a, j, k); } KOKKOS_INLINE_FUNCTION - auto &operator()(int i, int j, int k, int l) const { + auto &operator()(int i, int j, int k, int l) const { auto s = SliceType::index_type::s(i); auto a = SliceType::index_type::a(i); - return slice.access(s,a,j,k,l); + return slice.access(s, a, j, k, l); } KOKKOS_INLINE_FUNCTION auto &operator()(int i, int j, int k, int l, int m) const { auto s = SliceType::index_type::s(i); auto a = SliceType::index_type::a(i); - return slice.access(s,a,j,k,l,m); + return slice.access(s, a, j, k, l, m); } - }; using namespace Cabana; @@ -81,10 +79,11 @@ class CabanaController { using TypeTuple = std::tuple; using DeviceType = Kokkos::Device; using DataTypes = Cabana::MemberTypes; - + public: typedef ExecutionSpace exe; - static const int MAX_RANK = 4; // Including num_tuples -> so 3 additional extents + static const int MAX_RANK = + 4; // Including num_tuples -> so 3 additional extents static constexpr int vecLen = Cabana::AoSoA::vector_length; @@ -103,40 +102,40 @@ class CabanaController { template using member_slice_t = - Cabana::Slice; + Cabana::Slice; template using wrapper_slice_t = CabanaSliceWrapper, T>; - template< typename T1, typename... Tx > - void construct_sizes() { + template void construct_sizes() { // There are no dynamic ranks w/ cabana controller. // So we only need to know extent. // Cabana SoA only supports up to 3 additional ranks... std::size_t rank = std::rank{}; - assert( rank < MAX_RANK ); - switch( rank ) { - case 3: - extent_sizes[theta][3] = (int)std::extent::value; - case 2: - extent_sizes[theta][2] = (int)std::extent::value; - case 1: - extent_sizes[theta][1] = (int)std::extent::value; - default: - for( int i = MAX_RANK -1; i > rank; i-- ) { - // Since extent_sizes 2nd dimension - // is explicitly MAX_RANK, we fill the - // remaining space w/ 0 - extent_sizes[theta][i] = 0; - } - break; + assert(rank < MAX_RANK); + switch (rank) { + case 3: + extent_sizes[theta][3] = (int)std::extent::value; + case 2: + extent_sizes[theta][2] = (int)std::extent::value; + case 1: + extent_sizes[theta][1] = (int)std::extent::value; + default: + for (int i = MAX_RANK - 1; i > rank; i--) { + // Since extent_sizes 2nd dimension + // is explicitly MAX_RANK, we fill the + // remaining space w/ 0 + extent_sizes[theta][i] = 0; + } + break; } extent_sizes[theta][0] = num_tuples; - this->theta+=1; - if constexpr (sizeof...(Tx) != 0 ) construct_sizes(); + this->theta += 1; + if constexpr (sizeof...(Tx) != 0) + construct_sizes(); } - // member vaiables Cabana::AoSoA aosoa; const int num_tuples; @@ -144,31 +143,27 @@ class CabanaController { int extent_sizes[sizeof...(Ts)][MAX_RANK]; public: - CabanaController() : num_tuples(0) { - static_assert( sizeof...(Ts) != 0 ); + static_assert(sizeof...(Ts) != 0); construct_sizes(); } - CabanaController(int n) - : aosoa("sliceAoSoA", n), num_tuples(n) { - static_assert( sizeof...(Ts) != 0 ); + CabanaController(int n) : aosoa("sliceAoSoA", n), num_tuples(n) { + static_assert(sizeof...(Ts) != 0); construct_sizes(); } - int size(int i, int j) const { + int size(int i, int j) const { // returns slice dimensions. - assert( j >= 0 ); - assert( j <= MAX_RANK ); - if( j == 0 ) return this->tuples(); - else return extent_sizes[i][j]; - } - - int tuples() const { - return num_tuples; + assert(j >= 0); + assert(j <= MAX_RANK); + if (j == 0) + return this->tuples(); + else + return extent_sizes[i][j]; } - + int tuples() const { return num_tuples; } template auto makeSlice() { // Creates wrapper object w/ meta data about the slice. @@ -176,87 +171,99 @@ class CabanaController { const int stride = sizeof(soa_t) / sizeof(member_value_t); auto slice = Cabana::slice(aosoa); int sizes[MAX_RANK]; - for( int i = 0; i < MAX_RANK; i++ ) sizes[i] = this->size(index,i); - return wrapper_slice_t(std::move(slice),sizes); + for (int i = 0; i < MAX_RANK; i++) + sizes[i] = this->size(index, i); + return wrapper_slice_t(std::move(slice), sizes); } - template - typename std::enable_if<1==MeshFieldUtil::function_traits::arity>::type - simd_for( Fn& kernel, const Kokkos::Array& start, - const Kokkos::Array& end, - std::string tag) { - Cabana::SimdPolicy policy( start[0], end[0] ); - Cabana::simd_parallel_for( policy, KOKKOS_LAMBDA( const int& s, const int& a ) { - const std::size_t i = Cabana::Impl::Index::i(s,a); - kernel(i); - }, tag ); + template + typename std::enable_if<1 == MeshFieldUtil::function_traits::arity>::type + simd_for(Fn &kernel, const Kokkos::Array &start, + const Kokkos::Array &end, std::string tag) { + Cabana::SimdPolicy policy(start[0], end[0]); + Cabana::simd_parallel_for( + policy, + KOKKOS_LAMBDA(const int &s, const int &a) { + const std::size_t i = Cabana::Impl::Index::i(s, a); + kernel(i); + }, + tag); } - template - typename std::enable_if<2==MeshFieldUtil::function_traits::arity>::type - simd_for( Fn& kernel, const Kokkos::Array& start, - const Kokkos::Array& end, - std::string tag) { - Cabana::SimdPolicy policy( start[0], end[0] ); + template + typename std::enable_if<2 == MeshFieldUtil::function_traits::arity>::type + simd_for(Fn &kernel, const Kokkos::Array &start, + const Kokkos::Array &end, std::string tag) { + Cabana::SimdPolicy policy(start[0], end[0]); const int64_t s1 = start[1]; const int64_t e1 = end[1]; - Cabana::simd_parallel_for( policy, KOKKOS_LAMBDA( const int& s, const int& a ) { - const std::size_t i = Cabana::Impl::Index::i(s,a); - for( int j = s1; j < e1; j++ ) kernel(i,j); - }, tag ); + Cabana::simd_parallel_for( + policy, + KOKKOS_LAMBDA(const int &s, const int &a) { + const std::size_t i = Cabana::Impl::Index::i(s, a); + for (int j = s1; j < e1; j++) + kernel(i, j); + }, + tag); } - template - typename std::enable_if<3==MeshFieldUtil::function_traits::arity>::type - simd_for( Fn& kernel, const Kokkos::Array& start, - const Kokkos::Array& end, - std::string tag) { - Cabana::SimdPolicy policy( start[0], end[0] ); - const int s1=start[1],s2=start[2]; - const int e1=end[1],e2=end[2]; - Cabana::simd_parallel_for( policy, KOKKOS_LAMBDA( const int& s, const int& a ) { - const std::size_t i = Cabana::Impl::Index::i(s,a); - for( int j = s1; j < e1; j++ ) { - for( int k = s2; k < e2; k++ ) { - kernel(i,j,k); - } - } - }, tag ); + template + typename std::enable_if<3 == MeshFieldUtil::function_traits::arity>::type + simd_for(Fn &kernel, const Kokkos::Array &start, + const Kokkos::Array &end, std::string tag) { + Cabana::SimdPolicy policy(start[0], end[0]); + const int s1 = start[1], s2 = start[2]; + const int e1 = end[1], e2 = end[2]; + Cabana::simd_parallel_for( + policy, + KOKKOS_LAMBDA(const int &s, const int &a) { + const std::size_t i = Cabana::Impl::Index::i(s, a); + for (int j = s1; j < e1; j++) { + for (int k = s2; k < e2; k++) { + kernel(i, j, k); + } + } + }, + tag); } - template - typename std::enable_if<4==MeshFieldUtil::function_traits::arity>::type - simd_for( Fn& kernel, const Kokkos::Array& start, - const Kokkos::Array& end, - std::string tag) { - Cabana::SimdPolicy policy( start[0], end[0] ); - const int s1=start[1],s2=start[2],s3=start[3]; - const int e1=end[1],e2=end[2],e3=end[3]; - Cabana::simd_parallel_for( policy, KOKKOS_LAMBDA( const int& s, const int& a ) { - const std::size_t i = Cabana::Impl::Index::i(s,a); - for( int j = s1; j < e1; j++ ) { - for( int k = s2; k < e2; k++ ) { - for( int l = s3; l < e3; l++ ) { - kernel(i,j,k,l); + template + typename std::enable_if<4 == MeshFieldUtil::function_traits::arity>::type + simd_for(Fn &kernel, const Kokkos::Array &start, + const Kokkos::Array &end, std::string tag) { + Cabana::SimdPolicy policy(start[0], end[0]); + const int s1 = start[1], s2 = start[2], s3 = start[3]; + const int e1 = end[1], e2 = end[2], e3 = end[3]; + Cabana::simd_parallel_for( + policy, + KOKKOS_LAMBDA(const int &s, const int &a) { + const std::size_t i = Cabana::Impl::Index::i(s, a); + for (int j = s1; j < e1; j++) { + for (int k = s2; k < e2; k++) { + for (int l = s3; l < e3; l++) { + kernel(i, j, k, l); + } + } } - } - } - }, tag ); + }, + tag); } template - void parallel_for(const std::initializer_list& start_init, - const std::initializer_list& end_init, - FunctorType &vectorKernel, - std::string tag) { - static_assert( std::is_integral::value, "Integral required\n" ); - static_assert( std::is_integral::value, "Integral required\n" ); - constexpr std::size_t RANK = MeshFieldUtil::function_traits::arity; - assert( start_init.size() >= RANK ); - assert( end_init.size() >= RANK ); - Kokkos::Array a_start = MeshFieldUtil::to_kokkos_array(start_init); - Kokkos::Array a_end = MeshFieldUtil::to_kokkos_array(end_init); - simd_for(vectorKernel,a_start,a_end,tag); + void parallel_for(const std::initializer_list &start_init, + const std::initializer_list &end_init, + FunctorType &vectorKernel, std::string tag) { + static_assert(std::is_integral::value, "Integral required\n"); + static_assert(std::is_integral::value, "Integral required\n"); + constexpr std::size_t RANK = + MeshFieldUtil::function_traits::arity; + assert(start_init.size() >= RANK); + assert(end_init.size() >= RANK); + Kokkos::Array a_start = + MeshFieldUtil::to_kokkos_array(start_init); + Kokkos::Array a_end = + MeshFieldUtil::to_kokkos_array(end_init); + simd_for(vectorKernel, a_start, a_end, tag); } }; } // namespace Controller diff --git a/src/KokkosController.hpp b/src/KokkosController.hpp index b228a6c..a81fa76 100644 --- a/src/KokkosController.hpp +++ b/src/KokkosController.hpp @@ -1,242 +1,237 @@ #ifndef kokkosslicewrapper_hpp #define kokkosslicewrapper_hpp -#include -#include -#include #include +#include #include -#include // typeid +#include +#include #include -#include +#include // typeid +#include -#include -#include "MeshField_Utility.hpp" #include "MeshField_Macros.hpp" - +#include "MeshField_Utility.hpp" +#include namespace Controller { -template -struct KokkosSliceWrapper{ - +template struct KokkosSliceWrapper { + SliceType slice; int dimensions[5]; typedef T Type; static const int MAX_RANK = 5; static const std::size_t RANK = Kokkos::View::rank; - KokkosSliceWrapper(SliceType slice_in, int* sizes) : slice(slice_in) { - for( int i = 0; i < MAX_RANK; i++ ) dimensions[i] = sizes[i]; + KokkosSliceWrapper(SliceType slice_in, int *sizes) : slice(slice_in) { + for (int i = 0; i < MAX_RANK; i++) + dimensions[i] = sizes[i]; } KokkosSliceWrapper() {} - + KOKKOS_INLINE_FUNCTION - auto size(int i) const { - assert( i >= 0 ); - assert( i < MAX_RANK ); - return dimensions[i]; + auto size(int i) const { + assert(i >= 0); + assert(i < MAX_RANK); + return dimensions[i]; } /* 1D Access */ KOKKOS_INLINE_FUNCTION - auto &operator()( int s ) const { return slice(s); } - + auto &operator()(int s) const { return slice(s); } + KOKKOS_INLINE_FUNCTION - auto &operator()( int s, int a ) const { return slice(s,a); } + auto &operator()(int s, int a) const { return slice(s, a); } KOKKOS_INLINE_FUNCTION - auto &operator()( int s, int a, int i ) const { return slice(s,a,i); } + auto &operator()(int s, int a, int i) const { return slice(s, a, i); } KOKKOS_INLINE_FUNCTION - auto &operator()( int s, int a, int i, int j ) - const { return slice(s,a,i,j); } - + auto &operator()(int s, int a, int i, int j) const { + return slice(s, a, i, j); + } + KOKKOS_INLINE_FUNCTION - auto &operator()( int s, int a, int i, int j, int k ) - const { return slice(s,a,i,j,k); } + auto &operator()(int s, int a, int i, int j, int k) const { + return slice(s, a, i, j, k); + } }; - template class KokkosController { - + // type definitions using TypeTuple = std::tuple; private: // all the type defenitions that are needed us to get the type of the slice // returned by the underlying AoSoA - template - using member_data_t = + template + using member_data_t = typename std::tuple_element>::type; template using member_value_t = typename std::remove_all_extents>::type; - template - using member_slice_t = - Kokkos::View; + template using member_slice_t = Kokkos::View; template using wrapper_slice_t = KokkosSliceWrapper, T>; - template - auto construct( std::vector &dims ) { + template auto construct(std::vector &dims) { return std::tuple...>{create_view("view", dims)...}; } - - template - Kokkos::View - create_view( std::string tag, std::vector &dims ) { + + template + Kokkos::View create_view(std::string tag, + std::vector &dims) { int rank = Kokkos::View::rank; int dynamic = Kokkos::View::rank_dynamic; - assert( rank <= MAX_RANK ); - assert( rank >= 0 ); - - if( rank == 0 ) return Kokkos::View(tag); - Kokkos::View rt; - switch( dynamic ) { - case 1: - rt = Kokkos::View(tag, dims[0] ); - break; - case 2: - rt = Kokkos::View(tag, dims[0], - dims[1] ); - break; - case 3: - rt = Kokkos::View(tag, dims[0], - dims[1], - dims[2]); - break; - case 4: - rt = Kokkos::View(tag, dims[0], - dims[1], - dims[2], - dims[3]); - break; - case 5: - rt = Kokkos::View(tag, dims[0], - dims[1], - dims[2], - dims[3], - dims[4]); - break; - default: - rt = Kokkos::View(tag); - break; + assert(rank <= MAX_RANK); + assert(rank >= 0); + + if (rank == 0) + return Kokkos::View(tag); + Kokkos::View rt; + switch (dynamic) { + case 1: + rt = Kokkos::View(tag, dims[0]); + break; + case 2: + rt = Kokkos::View(tag, dims[0], dims[1]); + break; + case 3: + rt = Kokkos::View(tag, dims[0], dims[1], dims[2]); + break; + case 4: + rt = Kokkos::View(tag, dims[0], dims[1], dims[2], + dims[3]); + break; + case 5: + rt = Kokkos::View(tag, dims[0], dims[1], dims[2], + dims[3], dims[4]); + break; + default: + rt = Kokkos::View(tag); + break; } - + // Places all of the dyanmic ranks into the extent_sizes - for( int i = 0; i < dynamic; i++ ) { + for (int i = 0; i < dynamic; i++) { this->extent_sizes[theta][i] = dims[i]; } - this->theta+=1; - dims.erase( dims.begin(), dims.begin()+dynamic ); + this->theta += 1; + dims.erase(dims.begin(), dims.begin() + dynamic); return rt; } - - template< typename T1, typename... Tx> - void construct_sizes() { + + template void construct_sizes() { int total_rank = Kokkos::View::rank; int dynamic_rank = Kokkos::View::rank_dynamic; const int static_rank = total_rank - dynamic_rank; - assert( total_rank <= MAX_RANK ); - assert( total_rank >= 0 ); - - switch( static_rank ) { - case 1: - extent_sizes[delta][dynamic_rank] = (int)std::extent::value; - break; - case 2: - extent_sizes[delta][dynamic_rank] = (int)std::extent::value; - extent_sizes[delta][dynamic_rank+1] = (int)std::extent::value; - break; - case 3: - extent_sizes[delta][dynamic_rank] = (int)std::extent::value; - extent_sizes[delta][dynamic_rank+1] = (int)std::extent::value; - extent_sizes[delta][dynamic_rank+2] = (int)std::extent::value; - break; - case 4: - extent_sizes[delta][dynamic_rank] = (int)std::extent::value; - extent_sizes[delta][dynamic_rank+1] = (int)std::extent::value; - extent_sizes[delta][dynamic_rank+2] = (int)std::extent::value; - extent_sizes[delta][dynamic_rank+3] = (int)std::extent::value; - break; - case 5: - extent_sizes[delta][0] = (int)std::extent::value; - extent_sizes[delta][1] = (int)std::extent::value; - extent_sizes[delta][2] = (int)std::extent::value; - extent_sizes[delta][3] = (int)std::extent::value; - extent_sizes[delta][4] = (int)std::extent::value; - break; - case 0: - break; - default: - break; + assert(total_rank <= MAX_RANK); + assert(total_rank >= 0); + + switch (static_rank) { + case 1: + extent_sizes[delta][dynamic_rank] = (int)std::extent::value; + break; + case 2: + extent_sizes[delta][dynamic_rank] = (int)std::extent::value; + extent_sizes[delta][dynamic_rank + 1] = (int)std::extent::value; + break; + case 3: + extent_sizes[delta][dynamic_rank] = (int)std::extent::value; + extent_sizes[delta][dynamic_rank + 1] = (int)std::extent::value; + extent_sizes[delta][dynamic_rank + 2] = (int)std::extent::value; + break; + case 4: + extent_sizes[delta][dynamic_rank] = (int)std::extent::value; + extent_sizes[delta][dynamic_rank + 1] = (int)std::extent::value; + extent_sizes[delta][dynamic_rank + 2] = (int)std::extent::value; + extent_sizes[delta][dynamic_rank + 3] = (int)std::extent::value; + break; + case 5: + extent_sizes[delta][0] = (int)std::extent::value; + extent_sizes[delta][1] = (int)std::extent::value; + extent_sizes[delta][2] = (int)std::extent::value; + extent_sizes[delta][3] = (int)std::extent::value; + extent_sizes[delta][4] = (int)std::extent::value; + break; + case 0: + break; + default: + break; } - this->delta+=1; - if constexpr ( sizeof...(Tx) >= 1 ) construct_sizes(); + this->delta += 1; + if constexpr (sizeof...(Tx) >= 1) + construct_sizes(); } // member vaiables const int num_types = sizeof...(Ts); unsigned short delta = 0; - unsigned short theta = 0; + unsigned short theta = 0; int extent_sizes[sizeof...(Ts)][5]; - std::tuple...> values_; + std::tuple...> values_; public: - typedef ExecutionSpace exe; static const int MAX_RANK = 5; - + KokkosController() { - if constexpr ( sizeof...(Ts) >= 1 ) { construct_sizes(); } + if constexpr (sizeof...(Ts) >= 1) { + construct_sizes(); + } std::vector obj; values_ = construct(obj); } KokkosController(const std::initializer_list items) { - if constexpr ( sizeof...(Ts) >= 1 ) { construct_sizes(); } + if constexpr (sizeof...(Ts) >= 1) { + construct_sizes(); + } std::vector obj(items); values_ = construct(obj); } - + ~KokkosController() = default; - - int size( int type_index, int dimension_index ) const { + + int size(int type_index, int dimension_index) const { assert(type_index >= 0); assert(type_index < num_types); assert(dimension_index >= 0); - assert(dimension_index < MAX_RANK ); - return extent_sizes[type_index][dimension_index]; + assert(dimension_index < MAX_RANK); + return extent_sizes[type_index][dimension_index]; } - - - template auto makeSlice() { + + template auto makeSlice() { using type = std::tuple_element_t; int slice_dims[5]; - for( int i = 0; i < 5; i++ ) slice_dims[i] = this->extent_sizes[index][i]; - return wrapper_slice_t(std::get(values_),slice_dims); + for (int i = 0; i < 5; i++) + slice_dims[i] = this->extent_sizes[index][i]; + return wrapper_slice_t(std::get(values_), slice_dims); } - + template - void parallel_for(const std::initializer_list& start, - const std::initializer_list& end, - FunctorType &vectorKernel, - std::string tag ) { + void parallel_for(const std::initializer_list &start, + const std::initializer_list &end, + FunctorType &vectorKernel, std::string tag) { constexpr auto RANK = MeshFieldUtil::function_traits::arity; - assert( RANK >= 1 ); - Kokkos::Array a_start = MeshFieldUtil::to_kokkos_array( start ); - Kokkos::Array a_end = MeshFieldUtil::to_kokkos_array( end ); - if constexpr ( RANK == 1 ) { + assert(RANK >= 1); + Kokkos::Array a_start = + MeshFieldUtil::to_kokkos_array(start); + Kokkos::Array a_end = + MeshFieldUtil::to_kokkos_array(end); + if constexpr (RANK == 1) { Kokkos::RangePolicy p(a_start[0], a_end[0]); - Kokkos::parallel_for(tag,p,vectorKernel); + Kokkos::parallel_for(tag, p, vectorKernel); } else { - Kokkos::MDRangePolicy, ExecutionSpace> policy(a_start,a_end); - Kokkos::parallel_for( tag, policy, vectorKernel ); + Kokkos::MDRangePolicy, ExecutionSpace> policy(a_start, + a_end); + Kokkos::parallel_for(tag, policy, vectorKernel); } } }; diff --git a/src/MeshField.hpp b/src/MeshField.hpp index a3b8f6b..71b9c8b 100644 --- a/src/MeshField.hpp +++ b/src/MeshField.hpp @@ -1,14 +1,14 @@ #ifndef meshfield_hpp #define meshfield_hpp -#include -#include // std::same_v #include +#include #include +#include // std::same_v +#include "MeshField_Utility.hpp" #include #include -#include "MeshField_Utility.hpp" namespace MeshField { template class Field { @@ -17,12 +17,11 @@ template class Field { typedef typename Slice::Type Type; public: - static const int MAX_RANK = Slice::MAX_RANK; static const int RANK = Slice::RANK; Field(Slice s) : slice(s) {} - + KOKKOS_INLINE_FUNCTION auto size(int i) const { return slice.size(i); } @@ -50,143 +49,143 @@ template class MeshField { Controller sliceController; public: - MeshField(Controller controller) : sliceController(std::move(controller)) {} - - int size(int type_index, int dimension_index) { + + int size(int type_index, int dimension_index) { // given a Controller w/ types Tx = // size(i,j) will return dimension size from Tx[i,j]; // So in the above 'Tx' example, size(0,2) == 3. - return sliceController.size(type_index,dimension_index); + return sliceController.size(type_index, dimension_index); } template auto makeField() { auto slice = sliceController.template makeSlice(); return Field(std::move(slice)); - } - - template - void setFieldRankOne( Field& field, View& view ) { + } + + template + void setFieldRankOne(Field &field, View &view) { using EXE_SPACE = typename Controller::exe; - Kokkos::RangePolicy p(0,field.size(0)); - Kokkos::parallel_for(p, KOKKOS_LAMBDA (const int& i){ field(i) = view(i);}); + Kokkos::RangePolicy p(0, field.size(0)); + Kokkos::parallel_for( + p, KOKKOS_LAMBDA(const int &i) { field(i) = view(i); }); } - template - void setFieldRankTwo( Field& field, View& view ) { + template + void setFieldRankTwo(Field &field, View &view) { using EXE_SPACE = typename Controller::exe; - Kokkos::Array a = MeshFieldUtil::to_kokkos_array({0,0}); - Kokkos::Array b = MeshFieldUtil::to_kokkos_array({field.size(0), - field.size(1)}); - Kokkos::MDRangePolicy,EXE_SPACE> p(a,b); - Kokkos::parallel_for(p, - KOKKOS_LAMBDA (const int& i,const int& j){ - field(i,j) = view(i,j); - }); + Kokkos::Array a = MeshFieldUtil::to_kokkos_array({0, 0}); + Kokkos::Array b = MeshFieldUtil::to_kokkos_array( + {field.size(0), field.size(1)}); + Kokkos::MDRangePolicy, EXE_SPACE> p(a, b); + Kokkos::parallel_for( + p, KOKKOS_LAMBDA(const int &i, const int &j) { + field(i, j) = view(i, j); + }); } - template - void setFieldRankThree( Field& field, View& view ) { + template + void setFieldRankThree(Field &field, View &view) { using EXE_SPACE = typename Controller::exe; - Kokkos::Array a = MeshFieldUtil::to_kokkos_array({0,0,0}); - Kokkos::Array b = MeshFieldUtil::to_kokkos_array({field.size(0), - field.size(1), - field.size(2)}); - Kokkos::MDRangePolicy,EXE_SPACE> p(a,b); - Kokkos::parallel_for(p, - KOKKOS_LAMBDA (const int& i,const int& j,const int& k){ - field(i,j,k) = view(i,j,k); - }); + Kokkos::Array a = MeshFieldUtil::to_kokkos_array({0, 0, 0}); + Kokkos::Array b = MeshFieldUtil::to_kokkos_array( + {field.size(0), field.size(1), field.size(2)}); + Kokkos::MDRangePolicy, EXE_SPACE> p(a, b); + Kokkos::parallel_for( + p, KOKKOS_LAMBDA(const int &i, const int &j, const int &k) { + field(i, j, k) = view(i, j, k); + }); } - template - void setFieldRankFour( Field& field, View& view ) { + template + void setFieldRankFour(Field &field, View &view) { using EXE_SPACE = typename Controller::exe; - Kokkos::Array a = MeshFieldUtil::to_kokkos_array({0,0,0,0}); - Kokkos::Array b = MeshFieldUtil::to_kokkos_array({field.size(0), - field.size(1), - field.size(2), - field.size(3)}); - Kokkos::MDRangePolicy,EXE_SPACE> p(a,b); - Kokkos::parallel_for(p, - KOKKOS_LAMBDA (const int& i,const int& j,const int& k, const int& l){ - field(i,j,k,l) = view(i,j,k,l); - }); + Kokkos::Array a = MeshFieldUtil::to_kokkos_array({0, 0, 0, 0}); + Kokkos::Array b = MeshFieldUtil::to_kokkos_array( + {field.size(0), field.size(1), field.size(2), field.size(3)}); + Kokkos::MDRangePolicy, EXE_SPACE> p(a, b); + Kokkos::parallel_for( + p, + KOKKOS_LAMBDA(const int &i, const int &j, const int &k, const int &l) { + field(i, j, k, l) = view(i, j, k, l); + }); } - template - void setFieldRankFive( Field& field, View& view ) { + template + void setFieldRankFive(Field &field, View &view) { using EXE_SPACE = typename Controller::exe; - Kokkos::Array a = MeshFieldUtil::to_kokkos_array({0,0,0,0,0}); - Kokkos::Array b = MeshFieldUtil::to_kokkos_array({field.size(0), - field.size(1), - field.size(2), - field.size(3), - field.size(4)}); - Kokkos::MDRangePolicy,EXE_SPACE> p(a,b); - Kokkos::parallel_for(p, - KOKKOS_LAMBDA (const int& i,const int& j,const int& k, const int& l, const int& m){ - field(i,j,k,l,m) = view(i,j,k,l,m); - }); + Kokkos::Array a = + MeshFieldUtil::to_kokkos_array({0, 0, 0, 0, 0}); + Kokkos::Array b = MeshFieldUtil::to_kokkos_array( + {field.size(0), field.size(1), field.size(2), field.size(3), + field.size(4)}); + Kokkos::MDRangePolicy, EXE_SPACE> p(a, b); + Kokkos::parallel_for( + p, KOKKOS_LAMBDA(const int &i, const int &j, const int &k, const int &l, + const int &m) { + field(i, j, k, l, m) = view(i, j, k, l, m); + }); } - template - void setField(FieldT &field, View &view) { + template void setField(FieldT &field, View &view) { // Accepts a Field object and Kokkos::View // -> sets field(i,j,...) = view(i,j,...) for all i,j,... // up to rank 5. constexpr std::size_t view_rank = View::rank; constexpr std::size_t field_rank = FieldT::RANK; - static_assert( field_rank <= FieldT::MAX_RANK ); - static_assert( view_rank == field_rank ); - - if constexpr( field_rank == 1 ) { setFieldRankOne(field,view); } - else if constexpr( field_rank == 2 ) { setFieldRankTwo(field,view); } - else if constexpr( field_rank == 3 ) { setFieldRankThree(field,view); } - else if constexpr( field_rank == 4 ) { setFieldRankFour(field,view); } - else if constexpr( field_rank == 5 ) { setFieldRankFive(field,view); } - else { fprintf(stderr, "setField error: Invalid Field Rank\n"); } + static_assert(field_rank <= FieldT::MAX_RANK); + static_assert(view_rank == field_rank); + + if constexpr (field_rank == 1) { + setFieldRankOne(field, view); + } else if constexpr (field_rank == 2) { + setFieldRankTwo(field, view); + } else if constexpr (field_rank == 3) { + setFieldRankThree(field, view); + } else if constexpr (field_rank == 4) { + setFieldRankFour(field, view); + } else if constexpr (field_rank == 5) { + setFieldRankFive(field, view); + } else { + fprintf(stderr, "setField error: Invalid Field Rank\n"); + } } - + template - void parallel_for(const std::initializer_list& start, - const std::initializer_list& end, - FunctorType &vectorKernel, - std::string tag) { - sliceController.parallel_for(start,end, vectorKernel, tag); + void parallel_for(const std::initializer_list &start, + const std::initializer_list &end, + FunctorType &vectorKernel, std::string tag) { + sliceController.parallel_for(start, end, vectorKernel, tag); } - + template - void parallel_reduce(std::string tag, - const std::initializer_list& start, - const std::initializer_list& end, - FunctorType &reductionKernel, - ReducerType &reducer) { + void parallel_reduce(std::string tag, const std::initializer_list &start, + const std::initializer_list &end, + FunctorType &reductionKernel, ReducerType &reducer) { /* TODO: infinite reducers */ - /* Number of arguements to lambda should be equal to number of ranks + number of reducers + /* Number of arguements to lambda should be equal to number of ranks + + * number of reducers * -> adjust 'RANK' accordingly */ constexpr std::size_t reducer_count = 1; - constexpr auto RANK = MeshFieldUtil::function_traits::arity - reducer_count; - + constexpr auto RANK = + MeshFieldUtil::function_traits::arity - reducer_count; + using EXE_SPACE = typename Controller::exe; - assert( start.size() == end.size() ); - if constexpr ( RANK <= 1 ) { - Kokkos::RangePolicy policy((*start.begin()), (*end.begin()) ); - Kokkos::parallel_reduce( tag, policy, reductionKernel, reducer ); + assert(start.size() == end.size()); + if constexpr (RANK <= 1) { + Kokkos::RangePolicy policy((*start.begin()), (*end.begin())); + Kokkos::parallel_reduce(tag, policy, reductionKernel, reducer); } else { - auto a_start = MeshFieldUtil::to_kokkos_array( start ); - auto a_end = MeshFieldUtil::to_kokkos_array( end ); - Kokkos::MDRangePolicy, EXE_SPACE> policy(a_start, a_end); - Kokkos::parallel_reduce( tag, policy, reductionKernel, reducer ); + auto a_start = MeshFieldUtil::to_kokkos_array(start); + auto a_end = MeshFieldUtil::to_kokkos_array(end); + Kokkos::MDRangePolicy, EXE_SPACE> policy(a_start, + a_end); + Kokkos::parallel_reduce(tag, policy, reductionKernel, reducer); } - } template - void parallel_scan(std::string tag, - int64_t start_index, - int64_t end_index, - KernelType &scanKernel, - resultant &result) { - static_assert( std::is_pod::value ); + void parallel_scan(std::string tag, int64_t start_index, int64_t end_index, + KernelType &scanKernel, resultant &result) { + static_assert(std::is_pod::value); Kokkos::RangePolicy> p(start_index, end_index); Kokkos::parallel_scan(tag, p, scanKernel, result); } diff --git a/src/MeshField_Utility.hpp b/src/MeshField_Utility.hpp index d73e50e..ec750ab 100644 --- a/src/MeshField_Utility.hpp +++ b/src/MeshField_Utility.hpp @@ -1,61 +1,52 @@ #ifndef MESHFIELD_UTILITY_HPP #define MESHFIELD_UTILITY_HPP -#include #include -#include +#include #include +#include /* stackoverflow.com/questions/7943525/is-it-possible-to-figure-out-the-parameter-type-and-return-type-of-a-lambda */ -namespace MeshFieldUtil -{ +namespace MeshFieldUtil { template -struct function_traits - : public function_traits -{}; +struct function_traits : public function_traits {}; -template< typename ClassType, typename ReturnType, typename... Args> -struct function_traits { +template +struct function_traits { // arity is used to find the number of arguements // used in a lambda function. Refer to stackoverflow link above. static constexpr std::size_t arity = sizeof...(Args); typedef ReturnType result_type; - template struct arg { - typedef typename std::tuple_element>::type type; + template struct arg { + typedef typename std::tuple_element>::type type; }; }; -template< std::size_t RANK, class T > -Kokkos::Array -to_kokkos_array( const std::initializer_list& item) { +template +Kokkos::Array +to_kokkos_array(const std::initializer_list &item) { // Convert initializer_list to a Kokkos::Array - assert( std::is_integral::value ); + assert(std::is_integral::value); Kokkos::Array rt{}; auto x = item.begin(); - for( std::size_t i = 0; i < RANK; i++ ) { + for (std::size_t i = 0; i < RANK; i++) { rt[i] = (*x); x++; } return rt; } -template -struct identity { +template struct identity { using type = T; }; -template -struct remove_all_pointers : std::conditional_t< - std::is_pointer_v, - remove_all_pointers< - std::remove_pointer_t - >, - identity -> -{}; +template +struct remove_all_pointers + : std::conditional_t, + remove_all_pointers>, + identity> {}; } // END NAMESPACE MeshFieldUtil - #endif // #ifndef MESHFIELD_UTILITY_HPP diff --git a/src/test.hpp b/src/test.hpp index a36a948..d4b97be 100644 --- a/src/test.hpp +++ b/src/test.hpp @@ -1,61 +1,54 @@ #ifndef _test_hpp_ #define _test_hpp_ +#include "CabanaController.hpp" +#include "MeshField.hpp" +#include "MeshField_Utility.hpp" +#include #include -#include -#include #include +#include +#include #include #include -#include -#include "MeshField_Utility.hpp" -#include "MeshField.hpp" -#include "CabanaController.hpp" template -struct function_traits - : public function_traits -{}; +struct function_traits : public function_traits {}; -template< typename ClassType, typename ReturnType, typename... Args> -struct function_traits { +template +struct function_traits { static constexpr std::size_t arity = sizeof...(Args); typedef ReturnType result_type; - template struct arg { - typedef typename std::tuple_element>::type type; + template struct arg { + typedef typename std::tuple_element>::type type; }; }; -template -struct impl{ - impl(){} - virtual bool x( std::vector& start, - std::vector& end) { +template struct impl { + impl() {} + virtual bool x(std::vector &start, std::vector &end) { return false; } }; -template -struct impl<1,FunctorType> { - impl( FunctorType& t) : z(t) {} - bool x( std::vector& start, - std::vector& end ) { - assert( start.size() >= 1 && end.size() >= 1 ); - for( int i = start[0]; i < end[0]; i++ ) z(i); +template struct impl<1, FunctorType> { + impl(FunctorType &t) : z(t) {} + bool x(std::vector &start, std::vector &end) { + assert(start.size() >= 1 && end.size() >= 1); + for (int i = start[0]; i < end[0]; i++) + z(i); return true; } FunctorType z; }; -template -struct impl<2,FunctorType> { - impl( FunctorType& t) : z(t) {} - bool x( std::vector& start, - std::vector& end) { - assert( start.size() >= 2 && end.size() >= 2); - for( int i = start[0]; i < end[0]; i++ ) { - for( int j =(start[1]); j < (end[1]); j++ ) { - z(i,j); +template struct impl<2, FunctorType> { + impl(FunctorType &t) : z(t) {} + bool x(std::vector &start, std::vector &end) { + assert(start.size() >= 2 && end.size() >= 2); + for (int i = start[0]; i < end[0]; i++) { + for (int j = (start[1]); j < (end[1]); j++) { + z(i, j); } } return true; @@ -66,56 +59,59 @@ struct impl<2,FunctorType> { /* template typename std::enable_if< 2 == function_traits::arity>::type -parallel_for(FunctorType& kernel, const std::initializer_list& start, const std::initializer_list& end) { - Kokkos::Array a_start = MeshFieldUtil::to_kokkos_array<2>(start); - Kokkos::Array a_end = MeshFieldUtil::to_kokkos_array<2>(end); - Kokkos::MDRangePolicy> p(a_start,a_end); - Kokkos::parallel_for( "rank_2", p, kernel ); +parallel_for(FunctorType& kernel, const std::initializer_list& start, const +std::initializer_list& end) { Kokkos::Array a_start = +MeshFieldUtil::to_kokkos_array<2>(start); Kokkos::Array a_end = +MeshFieldUtil::to_kokkos_array<2>(end); Kokkos::MDRangePolicy> +p(a_start,a_end); Kokkos::parallel_for( "rank_2", p, kernel ); } */ /* template typename std::enable_if< 1 == function_traits::arity>::type -parallel_for(FunctorType& kernel, const std::initializer_list& start, const std::initializer_list& end) { - Kokkos::RangePolicy p(*start.begin(),*end.begin()); - Kokkos::parallel_for( "rank_1", p, kernel ); +parallel_for(FunctorType& kernel, const std::initializer_list& start, const +std::initializer_list& end) { Kokkos::RangePolicy +p(*start.begin(),*end.begin()); Kokkos::parallel_for( "rank_1", p, kernel ); } */ -template -typename std::enable_if<1==function_traits::arity>::type -simd_for( Fn kernel, const std::initializer_list& start, - const std::initializer_list& end ) { +template +typename std::enable_if<1 == function_traits::arity>::type +simd_for(Fn kernel, const std::initializer_list &start, + const std::initializer_list &end) { printf("rank1\n"); - Kokkos::Array a_start = MeshFieldUtil::to_kokkos_array<1>( start ); - Kokkos::Array a_end = MeshFieldUtil::to_kokkos_array<1>( end ); - assert( a_start[0] >= 0 ); - assert( a_end[0] >= 0 && a_end[0] > a_start[0]); + Kokkos::Array a_start = MeshFieldUtil::to_kokkos_array<1>(start); + Kokkos::Array a_end = MeshFieldUtil::to_kokkos_array<1>(end); + assert(a_start[0] >= 0); + assert(a_end[0] >= 0 && a_end[0] > a_start[0]); /* Cabana::SimdPolicy policy( *start.begin(), *start.end() ); - Cabana::simd_parallel_for( policy, KOKKOS_LAMBDA( const int& s, const int& a ) { - const int i = s*vectorLen+a; // TODO: use impel_index - kernel(i); + Cabana::simd_parallel_for( policy, KOKKOS_LAMBDA( const int& s, const int& a ) + { const int i = s*vectorLen+a; // TODO: use impel_index kernel(i); }, "simd_for (rank 1)"); */ } -template -typename std::enable_if<2==function_traits::arity>::type -simd_for( Fn kernel, const std::initializer_list& start, - const std::initializer_list& end ) { - Kokkos::Array a_start = MeshFieldUtil::to_kokkos_array<2>( start ); - Kokkos::Array a_end = MeshFieldUtil::to_kokkos_array<2>( end ); - assert( a_start.size() >= 2 && a_end.size() >= 2 ); - assert( a_start[0] >= 0 ); - assert( a_end[0] >= 0 && a_end[0] > a_start[0]); +template +typename std::enable_if<2 == function_traits::arity>::type +simd_for(Fn kernel, const std::initializer_list &start, + const std::initializer_list &end) { + Kokkos::Array a_start = MeshFieldUtil::to_kokkos_array<2>(start); + Kokkos::Array a_end = MeshFieldUtil::to_kokkos_array<2>(end); + assert(a_start.size() >= 2 && a_end.size() >= 2); + assert(a_start[0] >= 0); + assert(a_end[0] >= 0 && a_end[0] > a_start[0]); - Cabana::SimdPolicy policy( *start.begin(), *start.end() ); + Cabana::SimdPolicy policy(*start.begin(), *start.end()); const int s1 = a_start[1]; const int e1 = a_end[1]; - Cabana::simd_parallel_for( policy, KOKKOS_LAMBDA( const int& s, const int& a ) { - const int i = s*vectorLen+a; // TODO: use impel_index - for( int j = s1; j < e1; j++ ) kernel(i,j); - }, "simd_for (rank 2)"); + Cabana::simd_parallel_for( + policy, + KOKKOS_LAMBDA(const int &s, const int &a) { + const int i = s * vectorLen + a; // TODO: use impel_index + for (int j = s1; j < e1; j++) + kernel(i, j); + }, + "simd_for (rank 2)"); } /* template< typename FunctorType> diff --git a/test/test.cpp b/test/test.cpp index 9410e34..303a427 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -1,16 +1,15 @@ -#include -#include -#include #include "test.hpp" +#include "CabanaController.hpp" #include "MeshField.hpp" #include "MeshField_Macros.hpp" -#include "CabanaController.hpp" +#include +#include +#include using ExecutionSpace = Kokkos::DefaultExecutionSpace; using MemorySpace = Kokkos::DefaultExecutionSpace::memory_space; - -int main( int argc, char** argv) { +int main(int argc, char **argv) { Kokkos::ScopeGuard scope_gaurd(argc, argv); /* auto rank1 = KOKKOS_LAMBDA(const int& i) { @@ -20,23 +19,23 @@ int main( int argc, char** argv) { printf("I am rank 2: (%d,%d)\n",i,j); }; */ - //parallel_for2(rank1); - //parallel_for2(rank2); + // parallel_for2(rank1); + // parallel_for2(rank2); - //parallel_for( rank2, {0,0}, {5,5} ); - //parallel_for( rank1, {0}, {5} ); - //simd_for( rank1, {0}, {10} ); - //simd_for( rank2, {0,0}, {10,10} ); + // parallel_for( rank2, {0,0}, {5,5} ); + // parallel_for( rank1, {0}, {5} ); + // simd_for( rank1, {0}, {10} ); + // simd_for( rank2, {0,0}, {10,10} ); const int x = 100; - using ctrl = Controller::CabanaController; + using ctrl = + Controller::CabanaController; ctrl c1(x); MeshField::MeshField mf(c1); auto field0 = mf.makeField<0>(); - auto vKernel = KOKKOS_LAMBDA( const int& i, const int& j ) { - printf("vKernel -> (%d,%d)\n", i,j); - field0(i,j) = i + j; - assert(field0(i,j) == i + j ); + auto vKernel = KOKKOS_LAMBDA(const int &i, const int &j) { + printf("vKernel -> (%d,%d)\n", i, j); + field0(i, j) = i + j; + assert(field0(i, j) == i + j); }; - simd_for(vKernel, {0,0}, {x,x}); - + simd_for(vKernel, {0, 0}, {x, x}); } diff --git a/test/testCabana.cpp b/test/testCabana.cpp index 0827b99..1e4ff84 100644 --- a/test/testCabana.cpp +++ b/test/testCabana.cpp @@ -1,20 +1,19 @@ -#include "MeshField.hpp" #include "CabanaController.hpp" #include "KokkosController.hpp" +#include "MeshField.hpp" #include "MeshField_Macros.hpp" #include "MeshField_Utility.hpp" #include #include -#include -#include #include +#include #include +#include #define TOLERANCE 1e-10; - // helper testing functions // compare doubles within a tolerance @@ -27,18 +26,19 @@ bool doubleCompare(double d1, double d2) { using ExecutionSpace = Kokkos::DefaultExecutionSpace; using MemorySpace = Kokkos::DefaultExecutionSpace::memory_space; -void testMakeSliceCabana( int num_tuples ) { +void testMakeSliceCabana(int num_tuples) { printf("== START testMakeSliceCabana ==\n"); - using Ctrl = Controller::CabanaController; - Ctrl c( num_tuples ); + using Ctrl = + Controller::CabanaController; + Ctrl c(num_tuples); MeshField::MeshField cabanaMeshField(c); auto field0 = cabanaMeshField.makeField<0>(); - auto testKernel = KOKKOS_LAMBDA( const int x ) { + auto testKernel = KOKKOS_LAMBDA(const int x) { double gamma = (double)x; field0(x) = gamma; - assert(doubleCompare(field0(x),gamma)); + assert(doubleCompare(field0(x), gamma)); }; Kokkos::parallel_for("testMakeSliceCabana()", num_tuples, testKernel); @@ -47,28 +47,32 @@ void testMakeSliceCabana( int num_tuples ) { void testParallelReduceCabana() { printf("== START testParallelReduceCabana ==\n"); - using Ctrl = Controller::CabanaController; + using Ctrl = Controller::CabanaController; const int N = 9; - Ctrl c( N ); + Ctrl c(N); MeshField::MeshField cabanaMeshField(c); - + { double result = 0, verify = 0; - auto reduce_kernel = KOKKOS_LAMBDA( const int &i, double& lsum ) { + auto reduce_kernel = KOKKOS_LAMBDA(const int &i, double &lsum) { lsum += i * 1.0; }; - cabanaMeshField.parallel_reduce("CabanaReduceTest1", {0}, {N}, reduce_kernel, result ); - for( int i = 0; i < N; i++ ) verify+=i*1.0; + cabanaMeshField.parallel_reduce("CabanaReduceTest1", {0}, {N}, + reduce_kernel, result); + for (int i = 0; i < N; i++) + verify += i * 1.0; assert(doubleCompare(verify, result)); } { double result = 0, verify = 0; - auto reduce_kernel = KOKKOS_LAMBDA( const int &i, const int& j, double& lsum ) { + auto reduce_kernel = + KOKKOS_LAMBDA(const int &i, const int &j, double &lsum) { lsum += i * j; }; - cabanaMeshField.parallel_reduce("CabanaReduceTest2", {0,0}, {N,N}, reduce_kernel, result ); - for( int i = 0; i < N; i++ ) { - for( int j = 0; j < N; j++ ) { + cabanaMeshField.parallel_reduce("CabanaReduceTest2", {0, 0}, {N, N}, + reduce_kernel, result); + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) { verify += i * j; } } @@ -76,14 +80,16 @@ void testParallelReduceCabana() { } { double result = 0, verify = 0; - auto reduce_kernel = KOKKOS_LAMBDA( const int &i, const int& j, const int& k, double& lsum ) { + auto reduce_kernel = + KOKKOS_LAMBDA(const int &i, const int &j, const int &k, double &lsum) { lsum += i * j * k; }; - cabanaMeshField.parallel_reduce("CabanaReduceTest3", {0,0,0}, {N,N,N}, reduce_kernel, result ); - for( int i = 0; i < N; i++ ) { - for( int j = 0; j < N; j++ ) { - for( int k = 0; k < N; k++ ) { - verify += i * j * k; + cabanaMeshField.parallel_reduce("CabanaReduceTest3", {0, 0, 0}, {N, N, N}, + reduce_kernel, result); + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) { + for (int k = 0; k < N; k++) { + verify += i * j * k; } } } @@ -92,15 +98,17 @@ void testParallelReduceCabana() { } { double result = 0, verify = 0; - auto reduce_kernel = KOKKOS_LAMBDA( const int &i, const int& j, const int& k, const int& l, double& lsum ) { + auto reduce_kernel = KOKKOS_LAMBDA(const int &i, const int &j, const int &k, + const int &l, double &lsum) { lsum += i * j * k * l; }; - cabanaMeshField.parallel_reduce("CabanaReduceTest4", {0,0,0,0}, {N,N,N,N}, reduce_kernel, result ); - for( int i = 0; i < N; i++ ) { - for( int j = 0; j < N; j++ ) { - for( int k = 0; k < N; k++ ) { - for( int l = 0; l < N; l++ ) { - verify += i * j * k * l; + cabanaMeshField.parallel_reduce("CabanaReduceTest4", {0, 0, 0, 0}, + {N, N, N, N}, reduce_kernel, result); + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) { + for (int k = 0; k < N; k++) { + for (int l = 0; l < N; l++) { + verify += i * j * k * l; } } } @@ -114,13 +122,18 @@ void testCabanaControllerSize() { printf("== START testCabanaControllerSize ==\n"); - const int a=6,b=5,c=4,d=3; - const int psi[4] = {a,b,c,d}; + const int a = 6, b = 5, c = 4, d = 3; + const int psi[4] = {a, b, c, d}; - using simple = Controller::CabanaController; - using multi = Controller::CabanaController; - using varied = Controller::CabanaController; - using empty = Controller::CabanaController; + using simple = + Controller::CabanaController; + using multi = + Controller::CabanaController; + using varied = + Controller::CabanaController; + using empty = Controller::CabanaController; simple c1(a); multi c2(a); @@ -131,26 +144,31 @@ void testCabanaControllerSize() { MeshField::MeshField multi_kok(c2); MeshField::MeshField varied_kok(c3); MeshField::MeshField empty_kok(c4); - + // simple_kok - for( int i = 0; i < 2; i++ ) { assert( simple_kok.size(0,i) == psi[i] ); } - + for (int i = 0; i < 2; i++) { + assert(simple_kok.size(0, i) == psi[i]); + } + // multi_kok - for( int i = 0; i < 3; i++ ) { - for( int j = 0; j < 4; j++ ) { - assert( multi_kok.size(i,j) == psi[j] ); + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 4; j++) { + assert(multi_kok.size(i, j) == psi[j]); } - } + } // varied_kok - for( int i = 0; i < 3; i++ ) assert( varied_kok.size(0,i) == psi[i] ); - assert( varied_kok.size(1,0) == psi[0] ); - for( int i = 0; i < 4; i++ ) assert( varied_kok.size(2,i) == psi[i] ); - for( int i = 0; i < 2; i++ ) assert( varied_kok.size(3,i) == psi[i] ); - + for (int i = 0; i < 3; i++) + assert(varied_kok.size(0, i) == psi[i]); + assert(varied_kok.size(1, 0) == psi[0]); + for (int i = 0; i < 4; i++) + assert(varied_kok.size(2, i) == psi[i]); + for (int i = 0; i < 2; i++) + assert(varied_kok.size(3, i) == psi[i]); + // empty_kok - for( int i = 0; i < 4; i++ ) { - assert( empty_kok.size(0,i) == 0 ); + for (int i = 0; i < 4; i++) { + assert(empty_kok.size(0, i) == 0); } printf("== END testCabanaControllerSize ==\n"); } @@ -158,13 +176,18 @@ void testCabanaControllerSize() { void testCabanaFieldSize() { printf("== START testCabanaFieldSize ==\n"); - const int a=6,b=5,c=4,d=3; - const int psi[4] = {a,b,c,d}; - - using simple = Controller::CabanaController; - using multi = Controller::CabanaController; - using varied = Controller::CabanaController; - using empty = Controller::CabanaController; + const int a = 6, b = 5, c = 4, d = 3; + const int psi[4] = {a, b, c, d}; + + using simple = + Controller::CabanaController; + using multi = + Controller::CabanaController; + using varied = + Controller::CabanaController; + using empty = Controller::CabanaController; simple c1(a); multi c2(a); varied c3(a); @@ -173,22 +196,23 @@ void testCabanaFieldSize() { MeshField::MeshField multi_kok(c2); MeshField::MeshField varied_kok(c3); MeshField::MeshField empty_kok(c4); - + const int MAX_RANK = 4; { // simple_kok auto field0 = simple_kok.makeField<0>(); - for( int i = 0; i < 2; i++ ) assert( field0.size(i) == psi[i] ); + for (int i = 0; i < 2; i++) + assert(field0.size(i) == psi[i]); } { // multi_kok auto field0 = multi_kok.makeField<0>(); auto field1 = multi_kok.makeField<1>(); auto field2 = multi_kok.makeField<2>(); - for( int i = 0; i < MAX_RANK; i++ ) { - assert( field0.size(i) == psi[i] ); - assert( field1.size(i) == psi[i] ); - assert( field2.size(i) == psi[i] ); + for (int i = 0; i < MAX_RANK; i++) { + assert(field0.size(i) == psi[i]); + assert(field1.size(i) == psi[i]); + assert(field2.size(i) == psi[i]); } } @@ -198,24 +222,35 @@ void testCabanaFieldSize() { auto field2 = varied_kok.makeField<2>(); auto field3 = varied_kok.makeField<3>(); - for( int i = 0; i < 3; i++ ) { assert( field0.size(i) == psi[i] ); } - for( int i = 0; i < 1; i++ ) { assert( field1.size(i) == psi[i] ); } - for( int i = 0; i < 4; i++ ) { assert( field2.size(i) == psi[i] ); } - for( int i = 0; i < 2; i++ ) { assert( field3.size(i) == psi[i] ); } + for (int i = 0; i < 3; i++) { + assert(field0.size(i) == psi[i]); + } + for (int i = 0; i < 1; i++) { + assert(field1.size(i) == psi[i]); + } + for (int i = 0; i < 4; i++) { + assert(field2.size(i) == psi[i]); + } + for (int i = 0; i < 2; i++) { + assert(field3.size(i) == psi[i]); + } } { // empty_kok auto field0 = empty_kok.makeField<0>(); - for( int i = 0; i < MAX_RANK; i++ ) { assert( field0.size(i) == 0 ); } + for (int i = 0; i < MAX_RANK; i++) { + assert(field0.size(i) == 0); + } } printf("== END testCabanaFieldSize ==\n"); } void testCabanaParallelFor() { printf("== START testCabanaParallelFor() ==\n"); - const int x=10,y=9,z=8,a=7; - { - using simd_ctrlr = Controller::CabanaController; + const int x = 10, y = 9, z = 8, a = 7; + { + using simd_ctrlr = + Controller::CabanaController; simd_ctrlr c1(x); MeshField::MeshField mf(c1); auto field0 = mf.makeField<0>(); @@ -223,32 +258,31 @@ void testCabanaParallelFor() { auto field2 = mf.makeField<2>(); auto field3 = mf.makeField<3>(); - auto vectorKernel = KOKKOS_LAMBDA( const int& i ) { + auto vectorKernel = KOKKOS_LAMBDA(const int &i) { field0(i) = i; assert(field0(i) == i); }; - mf.parallel_for( {0},{x}, vectorKernel, "simple_loop"); - - auto vectorKernel2 = KOKKOS_LAMBDA( const int& i, const int& j ) { - field1(i,j) = i+j; - assert(field1(i,j) == i+j); - }; - mf.parallel_for( {0,0},{x,y}, vectorKernel2, "simple_loop"); + mf.parallel_for({0}, {x}, vectorKernel, "simple_loop"); - auto vectorKernel3 = KOKKOS_LAMBDA( const int& i, const int& j, const int& k ) { - field2(i,j,k) = i+j+k; - assert( field2(i,j,k) == i+j+k ); + auto vectorKernel2 = KOKKOS_LAMBDA(const int &i, const int &j) { + field1(i, j) = i + j; + assert(field1(i, j) == i + j); }; - mf.parallel_for( {0,0,0},{x,y,z}, vectorKernel3, "simple_loop"); + mf.parallel_for({0, 0}, {x, y}, vectorKernel2, "simple_loop"); + auto vectorKernel3 = + KOKKOS_LAMBDA(const int &i, const int &j, const int &k) { + field2(i, j, k) = i + j + k; + assert(field2(i, j, k) == i + j + k); + }; + mf.parallel_for({0, 0, 0}, {x, y, z}, vectorKernel3, "simple_loop"); - auto vectorKernel4 = KOKKOS_LAMBDA( const int& i, const int& j, const int& k, - const int& l) { - field3(i,j,k,l) = i+j+k+l; - assert( field3(i,j,k,l) == i+j+k+l ); + auto vectorKernel4 = + KOKKOS_LAMBDA(const int &i, const int &j, const int &k, const int &l) { + field3(i, j, k, l) = i + j + k + l; + assert(field3(i, j, k, l) == i + j + k + l); }; - mf.parallel_for( {0,0,0,0},{x,y,z,a}, vectorKernel4, "simple_loop"); - + mf.parallel_for({0, 0, 0, 0}, {x, y, z, a}, vectorKernel4, "simple_loop"); } printf("== END testCabanaParallelFor() ==\n"); diff --git a/test/testKokkos.cpp b/test/testKokkos.cpp index be102ff..6edac71 100644 --- a/test/testKokkos.cpp +++ b/test/testKokkos.cpp @@ -1,17 +1,17 @@ -#include "MeshField.hpp" #include "CabanaController.hpp" #include "KokkosController.hpp" +#include "MeshField.hpp" #include "MeshField_Macros.hpp" #include "MeshField_Utility.hpp" #include #include -#include -#include +#include #include +#include #include -#include +#include #define TOLERANCE 1e-10; @@ -30,41 +30,46 @@ using MemorySpace = Kokkos::DefaultExecutionSpace::memory_space; void testMakeSliceKokkos() { printf("== START testMakeSliceKokkos ==\n"); int N = 10; - using Ctrlr = Controller::KokkosController; + using Ctrlr = + Controller::KokkosController; Ctrlr c({10}); MeshField::MeshField kokkosMeshField(c); auto field0 = kokkosMeshField.makeField<0>(); - - auto testKernel = KOKKOS_LAMBDA( const int x ) { + + auto testKernel = KOKKOS_LAMBDA(const int x) { double gamma = (double)x; field0(x) = gamma; - assert(doubleCompare(field0(x),gamma)); + assert(doubleCompare(field0(x), gamma)); }; Kokkos::parallel_for("testMakeSliceKokkos()", N, testKernel); printf("== END testMakeSliceKokkos ==\n"); } -void testKokkosConstructor( int num_tuples ) { +void testKokkosConstructor(int num_tuples) { printf("== START testKokkosConstructor ==\n"); { - using Ctrlr = Controller::KokkosController; - Ctrlr c({num_tuples,num_tuples}); + using Ctrlr = + Controller::KokkosController; + Ctrlr c({num_tuples, num_tuples}); MeshField::MeshField kok(c); } { - using Ctrlr = Controller::KokkosController; + using Ctrlr = + Controller::KokkosController; Ctrlr c; MeshField::MeshField kok(c); } { - using Ctrlr = Controller::KokkosController; - Ctrlr c({10,10,10,10,10}); + using Ctrlr = + Controller::KokkosController; + Ctrlr c({10, 10, 10, 10, 10}); MeshField::MeshField kok(c); } { - using Ctrlr = Controller::KokkosController; + using Ctrlr = + Controller::KokkosController; Ctrlr c; MeshField::MeshField kok(c); } @@ -72,72 +77,73 @@ void testKokkosConstructor( int num_tuples ) { printf("== END testKokkosConstructor ==\n"); } - void testingStufffs() { printf("== START testingStufffs ==\n"); - using Ctrlr = Controller::KokkosController; + using Ctrlr = + Controller::KokkosController; Ctrlr c({10}); MeshField::MeshField kok(c); - + auto field0 = kok.makeField<0>(); - auto vectorKernel = KOKKOS_LAMBDA(const int &s) { - field0(s) = 3; - }; - Kokkos::parallel_for("tag", 10, vectorKernel ); + auto vectorKernel = KOKKOS_LAMBDA(const int &s) { field0(s) = 3; }; + Kokkos::parallel_for("tag", 10, vectorKernel); printf("== END testingStufffs ==\n"); } - void testKokkosParallelFor() { printf("== START testKokkosParallelFor ==\n"); - const int a=10; - const int b=9; - const int c=8; - const int d=7; - const int e=6; + const int a = 10; + const int b = 9; + const int c = 8; + const int d = 7; + const int e = 6; { - using Ctrlr = Controller::KokkosController; + using Ctrlr = + Controller::KokkosController; Ctrlr c; MeshField::MeshField kok(c); - + auto rk1 = kok.makeField<0>(); auto rk2 = kok.makeField<1>(); auto rk3 = kok.makeField<2>(); auto rk4 = kok.makeField<3>(); auto rk5 = kok.makeField<4>(); - - auto k1 = KOKKOS_LAMBDA( const int& i ) { + + auto k1 = KOKKOS_LAMBDA(const int &i) { rk1(i) = i; assert(rk1(i) == i); }; - auto k2 = KOKKOS_LAMBDA (const int i, const int j) { - rk2(i,j) = i+j; - assert(rk2(i,j) == i+j); + auto k2 = KOKKOS_LAMBDA(const int i, const int j) { + rk2(i, j) = i + j; + assert(rk2(i, j) == i + j); }; - auto k3 = KOKKOS_LAMBDA(int i,int j,int k) { - rk3(i,j,k) = i + j + k; - assert( rk3(i,j,k) == i+j+k); + auto k3 = KOKKOS_LAMBDA(int i, int j, int k) { + rk3(i, j, k) = i + j + k; + assert(rk3(i, j, k) == i + j + k); }; - auto k4 = KOKKOS_LAMBDA(int i,int j,int k,int l) { - rk4(i,j,k,l) = i+j+k+l; - assert(rk4(i,j,k,l) == i+j+k+l); + auto k4 = KOKKOS_LAMBDA(int i, int j, int k, int l) { + rk4(i, j, k, l) = i + j + k + l; + assert(rk4(i, j, k, l) == i + j + k + l); }; - auto k5 = KOKKOS_LAMBDA(int i,int j,int k,int l, int m) { - rk5(i,j,k,l,m) = i+j+k+l+m; - assert( rk5(i,j,k,l,m) == i+j+k+l+m ); + auto k5 = KOKKOS_LAMBDA(int i, int j, int k, int l, int m) { + rk5(i, j, k, l, m) = i + j + k + l + m; + assert(rk5(i, j, k, l, m) == i + j + k + l + m); }; // idk why I cant use c,d,e to reference the extents // of each index but it doesn't compile if I use them. // -> github issue - kok.parallel_for({0},{a},k1, "testKokkosParallelFor(rank1)"); - kok.parallel_for({0,0},{a,b},k2, "testKokkosParallelFor(rank2)"); - kok.parallel_for({0,0,0},{a,b,8},k3, "testKokkosParallelFor(rank3)"); - kok.parallel_for({0,0,0,0},{a,b,8,7},k4, "testKokkosParallelFor(rank4)"); - kok.parallel_for({0,0,0,0,0},{a,b,8,7,6},k5, "testKokkosParallelFor(rank5)"); + kok.parallel_for({0}, {a}, k1, "testKokkosParallelFor(rank1)"); + kok.parallel_for({0, 0}, {a, b}, k2, "testKokkosParallelFor(rank2)"); + kok.parallel_for({0, 0, 0}, {a, b, 8}, k3, "testKokkosParallelFor(rank3)"); + kok.parallel_for({0, 0, 0, 0}, {a, b, 8, 7}, k4, + "testKokkosParallelFor(rank4)"); + kok.parallel_for({0, 0, 0, 0, 0}, {a, b, 8, 7, 6}, k5, + "testKokkosParallelFor(rank5)"); } printf("== END testKokkosParallelFor ==\n"); @@ -145,12 +151,13 @@ void testKokkosParallelFor() { void kokkosDocumentationLiesTest() { // They dont... printf("== START kokkosDocumentationLiesTest ==\n"); - Kokkos::Array start = {0,0,0}; - Kokkos::Array end = {2,2,2}; - Kokkos::parallel_for("0_0", Kokkos::MDRangePolicy< Kokkos::Rank<3> >(start,end), - KOKKOS_LAMBDA (const int c, const int f, const int p) { - printf("Kokkos documentation lies!!!: c:%d f:%d p:%d\n",c,f,p); - }); + Kokkos::Array start = {0, 0, 0}; + Kokkos::Array end = {2, 2, 2}; + Kokkos::parallel_for( + "0_0", Kokkos::MDRangePolicy>(start, end), + KOKKOS_LAMBDA(const int c, const int f, const int p) { + printf("Kokkos documentation lies!!!: c:%d f:%d p:%d\n", c, f, p); + }); printf("== END kokkosDocumentationLiesTest ==\n"); } @@ -159,78 +166,82 @@ void kokkosParallelReduceTest() { * https://kokkos.github.io/kokkos-core-wiki/API/core/parallel-dispatch/parallel_reduce.html?highlight=parallel_reduce*/ printf("== START kokkosParallelReduceTest ==\n"); - using Ctrlr = Controller::KokkosController; + using Ctrlr = + Controller::KokkosController; Ctrlr c1({10}); MeshField::MeshField kok(c1); { double result; int N = 10; - auto kernel = KOKKOS_LAMBDA( const int& i, double& lsum ) { + auto kernel = KOKKOS_LAMBDA(const int &i, double &lsum) { lsum += 1.0 * i; }; - kok.parallel_reduce("ReduceTest",{0},{N},kernel,result); + kok.parallel_reduce("ReduceTest", {0}, {N}, kernel, result); double result_verify = 0; - for( int i = 0; i < N; i++ ) { - result_verify += 1.0*i; + for (int i = 0; i < N; i++) { + result_verify += 1.0 * i; } - assert( result_verify == result ); - printf("Reduce test 1-D Result: %d %.2lf\n",N,result); + assert(result_verify == result); + printf("Reduce test 1-D Result: %d %.2lf\n", N, result); } { double result; int N = 10; - auto kernel = KOKKOS_LAMBDA( const int& i, const int&j, double& lsum ) { + auto kernel = KOKKOS_LAMBDA(const int &i, const int &j, double &lsum) { lsum += i * j; }; - kok.parallel_reduce("ReduceTest2",{0,0},{N,N},kernel,result); + kok.parallel_reduce("ReduceTest2", {0, 0}, {N, N}, kernel, result); double result_verify = 0; - for( int i = 0; i < N; i++ ) { - for( int j = 0; j < N; j++ ) { - result_verify += i*j; + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) { + result_verify += i * j; } } - assert( result_verify == result ); + assert(result_verify == result); - printf("Reduce test 2-D Result: %d %.2lf\n",N,result); + printf("Reduce test 2-D Result: %d %.2lf\n", N, result); } { double result; int N = 10; - auto kernel = KOKKOS_LAMBDA( const int& i, const int& j, const int& k, double& lsum ) { + auto kernel = + KOKKOS_LAMBDA(const int &i, const int &j, const int &k, double &lsum) { lsum += i * j * k; }; - kok.parallel_reduce("ReduceTest3",{0,0,0},{N,N,N},kernel,result); + kok.parallel_reduce("ReduceTest3", {0, 0, 0}, {N, N, N}, kernel, result); double result_verify = 0; - for( int i = 0; i < N; i++ ) { - for( int j = 0; j < N; j++ ) { - for( int k = 0; k < N; k++ ) { - result_verify += i*j*k; + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) { + for (int k = 0; k < N; k++) { + result_verify += i * j * k; } } } - assert( result_verify == result ); - printf("Reduce test 3-D Result: %d %.2lf\n",N,result); + assert(result_verify == result); + printf("Reduce test 3-D Result: %d %.2lf\n", N, result); } { double result; int N = 10; - auto kernel = KOKKOS_LAMBDA( const int& i, const int& j, const int& k, const int& l, double& lsum ) { + auto kernel = KOKKOS_LAMBDA(const int &i, const int &j, const int &k, + const int &l, double &lsum) { lsum += i * j * k * l; }; - kok.parallel_reduce("ReduceTest4",{0,0,0,0},{N,N,N,N},kernel,result); + kok.parallel_reduce("ReduceTest4", {0, 0, 0, 0}, {N, N, N, N}, kernel, + result); double result_verify = 0; - for( int i = 0; i < N; i++ ) { - for( int j = 0; j < N; j++ ) { - for( int k = 0; k < N; k++ ) { - for( int l = 0; l < N; l++ ) { - result_verify += i*j*k*l; + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) { + for (int k = 0; k < N; k++) { + for (int l = 0; l < N; l++) { + result_verify += i * j * k * l; } } } } - assert( result_verify == result ); - printf("Reduce test 4-D Result: %d %.2lf\n",N,result); + assert(result_verify == result); + printf("Reduce test 4-D Result: %d %.2lf\n", N, result); } printf("== END kokkosParallelReduceTest ==\n"); @@ -238,20 +249,25 @@ void kokkosParallelReduceTest() { void kokkosControllerSizeTest() { printf("== START kokkosControllerSizeTest ==\n"); - + const int a = 5; const int b = 4; const int c = 3; const int d = 2; const int e = 1; - const int psi[5] = {a,b,c,d,e}; + const int psi[5] = {a, b, c, d, e}; { /* BEGIN STATIC DIMENSIONS TESTS */ - using simple_static = Controller::KokkosController; - using large_simple_static = Controller::KokkosController; - using multi_static = Controller::KokkosController; - + using simple_static = + Controller::KokkosController; + using large_simple_static = + Controller::KokkosController; + using multi_static = + Controller::KokkosController; + simple_static c1; large_simple_static c2; multi_static c3; @@ -260,116 +276,143 @@ void kokkosControllerSizeTest() { MeshField::MeshField large_kok(c2); MeshField::MeshField multi_kok(c3); - assert(simple_kok.size(0,0) == a); + assert(simple_kok.size(0, 0) == a); - for( int i = 0; i < 5; i++ ) { - assert(large_kok.size(0,i) == psi[i]); + for (int i = 0; i < 5; i++) { + assert(large_kok.size(0, i) == psi[i]); } - for(int i = 0; i < 2; i++ ) { - for(int j = 0; j < 3;j++ ) { - int foo = multi_kok.size(i,j); - assert( ( foo == psi[j]) ); + for (int i = 0; i < 2; i++) { + for (int j = 0; j < 3; j++) { + int foo = multi_kok.size(i, j); + assert((foo == psi[j])); } } - + /* END STATIC DIMENSIONS TESTS */ } { /* BEGIN DYNAMIC DIMENSION TESTS */ - using simple_dynamic = Controller::KokkosController; - using large_simple_dynamic = Controller::KokkosController; - using multi_dynamic = Controller::KokkosController; + using simple_dynamic = + Controller::KokkosController; + using large_simple_dynamic = + Controller::KokkosController; + using multi_dynamic = + Controller::KokkosController; simple_dynamic c1({5}); - large_simple_dynamic c2({5,4,3,2,1}); - multi_dynamic c3({5,4,3,5,4,3}); + large_simple_dynamic c2({5, 4, 3, 2, 1}); + multi_dynamic c3({5, 4, 3, 5, 4, 3}); MeshField::MeshField simple_kok(c1); MeshField::MeshField large_kok(c2); MeshField::MeshField multi_kok(c3); - - assert( simple_kok.size(0,0) == a ); - for( int i = 0; i < 5; i++ ) - assert( large_kok.size(0,i) == psi[i] ); + assert(simple_kok.size(0, 0) == a); + + for (int i = 0; i < 5; i++) + assert(large_kok.size(0, i) == psi[i]); - for( int i = 0; i < 2; i++ ) - for( int j = 0; j < 3; j++ ) - assert( multi_kok.size(i,j) == psi[j] ); + for (int i = 0; i < 2; i++) + for (int j = 0; j < 3; j++) + assert(multi_kok.size(i, j) == psi[j]); /* END DYNAMIC DIMENSION TESTS */ } { /* BEGIN MIXED DIMENSION TESTS */ - using simple_mixed = Controller::KokkosController; - using large_simple_mixed = Controller::KokkosController; - using multi_mixed = Controller::KokkosController; - using complex_multi_mixed = Controller::KokkosController; + using simple_mixed = + Controller::KokkosController; + using large_simple_mixed = + Controller::KokkosController; + using multi_mixed = + Controller::KokkosController; + using complex_multi_mixed = + Controller::KokkosController; simple_mixed c1({5}); - large_simple_mixed c2({5,4}); - multi_mixed c3({5,4,3,5,4}); - complex_multi_mixed c4({5,4,3,5,4,5,4,3,2,5}); + large_simple_mixed c2({5, 4}); + multi_mixed c3({5, 4, 3, 5, 4}); + complex_multi_mixed c4({5, 4, 3, 5, 4, 5, 4, 3, 2, 5}); MeshField::MeshField simple_kok(c1); MeshField::MeshField large_kok(c2); MeshField::MeshField multi_kok(c3); MeshField::MeshField complex_kok(c4); - - assert( simple_kok.size(0,0) == a ); - assert( simple_kok.size(0,1) == b ); - for( int i = 0; i < 5; i++ ) - assert( large_kok.size(0,i) == psi[i] ); + assert(simple_kok.size(0, 0) == a); + assert(simple_kok.size(0, 1) == b); + + for (int i = 0; i < 5; i++) + assert(large_kok.size(0, i) == psi[i]); - for( int i = 0; i < 2; i++ ) { - for( int j = 0; j < 3; j++ ) { - assert( multi_kok.size(i,j) == psi[j] ); + for (int i = 0; i < 2; i++) { + for (int j = 0; j < 3; j++) { + assert(multi_kok.size(i, j) == psi[j]); } } - for( int i = 0; i < 4; i++ ) { - for( int j = 0; j < 5; j++ ) { - assert( complex_kok.size(i,j) == psi[j]); + for (int i = 0; i < 4; i++) { + for (int j = 0; j < 5; j++) { + assert(complex_kok.size(i, j) == psi[j]); } } /* END MIXED DIMENSION TESTS */ } - + printf("== END kokkosControllerSizeTest ==\n"); } void kokkosFieldSizeTest() { printf("== START kokkosFieldSizeTest ==\n"); - const int a=5,b=4,c=3,d=2,e=1; - const int psi[5] = {a,b,c,d,e}; + const int a = 5, b = 4, c = 3, d = 2, e = 1; + const int psi[5] = {a, b, c, d, e}; { - using simple_static = Controller::KokkosController; - using simple_dynamic = Controller::KokkosController; - using mixed = Controller::KokkosController; + using simple_static = + Controller::KokkosController; + using simple_dynamic = + Controller::KokkosController; + using mixed = Controller::KokkosController< + MemorySpace, ExecutionSpace, int *[b][c][d][e], char **[c][d][e], + double ***[d][e], bool ****[e], long *****>; simple_static c1; - simple_dynamic c2({5,5,4,5,4,3,5,4,3,2,5,4,3,2,1}); - mixed c3({5,5,4,5,4,3,5,4,3,2,5,4,3,2,1}); - + simple_dynamic c2({5, 5, 4, 5, 4, 3, 5, 4, 3, 2, 5, 4, 3, 2, 1}); + mixed c3({5, 5, 4, 5, 4, 3, 5, 4, 3, 2, 5, 4, 3, 2, 1}); + MeshField::MeshField simple_kok(c1); MeshField::MeshField dynamic_kok(c2); MeshField::MeshField mixed_kok(c3); - + { auto field0 = simple_kok.makeField<0>(); auto field1 = simple_kok.makeField<1>(); auto field2 = simple_kok.makeField<2>(); auto field3 = simple_kok.makeField<3>(); auto field4 = simple_kok.makeField<4>(); - - assert( field0.size(0) == a ); - for( int i = 0; i < 2; i++ ) { assert( field1.size(i) == psi[i]); } - for( int i = 0; i < 3; i++ ) { assert( field2.size(i) == psi[i]); } - for( int i = 0; i < 4; i++ ) { assert( field3.size(i) == psi[i]); } - for( int i = 0; i < 5; i++ ) { assert( field4.size(i) == psi[i]); } - + + assert(field0.size(0) == a); + for (int i = 0; i < 2; i++) { + assert(field1.size(i) == psi[i]); + } + for (int i = 0; i < 3; i++) { + assert(field2.size(i) == psi[i]); + } + for (int i = 0; i < 4; i++) { + assert(field3.size(i) == psi[i]); + } + for (int i = 0; i < 5; i++) { + assert(field4.size(i) == psi[i]); + } } { auto field0 = dynamic_kok.makeField<0>(); @@ -377,12 +420,20 @@ void kokkosFieldSizeTest() { auto field2 = dynamic_kok.makeField<2>(); auto field3 = dynamic_kok.makeField<3>(); auto field4 = dynamic_kok.makeField<4>(); - - assert( field0.size(0) == a ); - for( int i = 0; i < 2; i++ ) { assert( field1.size(i) == psi[i]); } - for( int i = 0; i < 3; i++ ) { assert( field2.size(i) == psi[i]); } - for( int i = 0; i < 4; i++ ) { assert( field3.size(i) == psi[i]); } - for( int i = 0; i < 5; i++ ) { assert( field4.size(i) == psi[i]); } + + assert(field0.size(0) == a); + for (int i = 0; i < 2; i++) { + assert(field1.size(i) == psi[i]); + } + for (int i = 0; i < 3; i++) { + assert(field2.size(i) == psi[i]); + } + for (int i = 0; i < 4; i++) { + assert(field3.size(i) == psi[i]); + } + for (int i = 0; i < 5; i++) { + assert(field4.size(i) == psi[i]); + } } { auto field0 = mixed_kok.makeField<0>(); @@ -391,12 +442,12 @@ void kokkosFieldSizeTest() { auto field3 = mixed_kok.makeField<3>(); auto field4 = mixed_kok.makeField<4>(); - for( int i = 0; i < 5; i++ ) { - assert( field0.size(i) == psi[i]); - assert( field1.size(i) == psi[i]); - assert( field2.size(i) == psi[i]); - assert( field3.size(i) == psi[i]); - assert( field4.size(i) == psi[i]); + for (int i = 0; i < 5; i++) { + assert(field0.size(i) == psi[i]); + assert(field1.size(i) == psi[i]); + assert(field2.size(i) == psi[i]); + assert(field3.size(i) == psi[i]); + assert(field4.size(i) == psi[i]); } } } @@ -407,14 +458,14 @@ void kokkosFieldSizeTest() { int main(int argc, char *argv[]) { int num_tuples = (argc < 2) ? (1000) : (atoi(argv[1])); Kokkos::ScopeGuard scope_guard(argc, argv); - + testKokkosConstructor(num_tuples); testKokkosParallelFor(); kokkosParallelReduceTest(); testMakeSliceKokkos(); kokkosControllerSizeTest(); - kokkosFieldSizeTest(); + kokkosFieldSizeTest(); - //kokkosDocumentationLiesTest(); + // kokkosDocumentationLiesTest(); return 0; } diff --git a/test/testMain.cpp b/test/testMain.cpp index 11102b5..2c3f247 100644 --- a/test/testMain.cpp +++ b/test/testMain.cpp @@ -1,16 +1,16 @@ -#include "MeshField.hpp" #include "CabanaController.hpp" #include "KokkosController.hpp" +#include "MeshField.hpp" #include "MeshField_Macros.hpp" #include "MeshField_Utility.hpp" #include #include -#include -#include #include +#include #include +#include #define TOLERANCE 1e-10; @@ -25,9 +25,7 @@ bool doubleCompare(double d1, double d2) { // returns (x-1)*(x/2) = 1 + 2 + 3 + 4 + ... + x KOKKOS_INLINE_FUNCTION -int seriesSum( int x ) { - return (int)(((double)x-1.0))*(((double)x/2.0)); -} +int seriesSum(int x) { return (int)(((double)x - 1.0)) * (((double)x / 2.0)); } using ExecutionSpace = Kokkos::DefaultExecutionSpace; using MemorySpace = Kokkos::DefaultExecutionSpace::memory_space; @@ -39,43 +37,51 @@ void testParallelScan() { printf("-- start testParallelScan on Kokkos --\n"); { - using s_kok = Controller::KokkosController; - s_kok c1({N,N}); + using s_kok = + Controller::KokkosController; + s_kok c1({N, N}); MeshField::MeshField mfk(c1); auto pre = mfk.makeField<0>(); auto post = mfk.makeField<1>(); - - auto scan_kernel = KOKKOS_LAMBDA(int i, int& partial_sum, const bool is_final) { - if( is_final ) pre(i) = partial_sum; + + auto scan_kernel = + KOKKOS_LAMBDA(int i, int &partial_sum, const bool is_final) { + if (is_final) + pre(i) = partial_sum; partial_sum += i; - if( is_final ) post(i) = partial_sum; + if (is_final) + post(i) = partial_sum; }; - - for( int i = 1; i <= N; i++ ) { + + for (int i = 1; i <= N; i++) { int result; - mfk.parallel_scan("default", 0, i, scan_kernel, result ); - assert( result == seriesSum(i) ); + mfk.parallel_scan("default", 0, i, scan_kernel, result); + assert(result == seriesSum(i)); } } printf("-- end testParallelScan on Kokkos --\n"); printf("-- start testParallelScan on Cabana --\n"); { - using s_cab = Controller::CabanaController; + using s_cab = + Controller::CabanaController; s_cab c1(N); MeshField::MeshField mfc(c1); auto pre = mfc.makeField<0>(); auto post = mfc.makeField<1>(); - - auto scan_kernel = KOKKOS_LAMBDA(int i, int& partial_sum, const bool is_final) { - if( is_final ) pre(i) = partial_sum; + + auto scan_kernel = + KOKKOS_LAMBDA(int i, int &partial_sum, const bool is_final) { + if (is_final) + pre(i) = partial_sum; partial_sum += i; - if( is_final ) post(i) = partial_sum; + if (is_final) + post(i) = partial_sum; }; - - for( int i = 1; i <= N; i++ ) { + + for (int i = 1; i <= N; i++) { int result; - mfc.parallel_scan("default", 0, i, scan_kernel, result ); - assert( result == seriesSum(i) ); + mfc.parallel_scan("default", 0, i, scan_kernel, result); + assert(result == seriesSum(i)); } } printf("-- end testParallelScan on Cabana --\n"); @@ -87,8 +93,10 @@ void testSetField() { { // KOKKOS FIELD const int N = 10; - using kok1 = Controller::KokkosController; - kok1 c1({N,N,N,N,N,N,N,N,N,N,N,N,N,N,N}); + using kok1 = + Controller::KokkosController; + kok1 c1({N, N, N, N, N, N, N, N, N, N, N, N, N, N, N}); MeshField::MeshField mf(c1); auto f1 = mf.makeField<0>(); auto f2 = mf.makeField<1>(); @@ -96,43 +104,48 @@ void testSetField() { auto f4 = mf.makeField<3>(); auto f5 = mf.makeField<4>(); - Kokkos::View v1("1",N); - Kokkos::View v2("2",N,N); - Kokkos::View v3("3",N,N,N); - Kokkos::View v4("4",N,N,N,N); - Kokkos::View v5("5",N,N,N,N,N); - - Kokkos::Array start = MeshFieldUtil::to_kokkos_array<5>({0,0,0,0,0}); - Kokkos::Array end = MeshFieldUtil::to_kokkos_array<5>({N,N,N,N,N}); - Kokkos::MDRangePolicy> p(start,end); - - Kokkos::parallel_for( "",p,KOKKOS_LAMBDA(const int& i,const int& j, const int& k, const int& l, const int& m){ - v1(i) += i; - v2(i,j) += i+j; - v3(i,j,k) += i+j+k; - v4(i,j,k,l) += i+j+k+l; - v5(i,j,k,l,m) += i+j+k+l+m; - }); - - mf.setField(f1,v1); - mf.setField(f2,v2); - mf.setField(f3,v3); - mf.setField(f4,v4); - mf.setField(f5,v5); - - - Kokkos::parallel_for( "",p,KOKKOS_LAMBDA(const int& i,const int& j, const int& k, const int& l, const int& m){ - assert( f1(i) == v1(i) ); - assert( f2(i,j) == v2(i,j) ); - assert( f3(i,j,k) == v3(i,j,k) ); - assert( f4(i,j,k,l) == v4(i,j,k,l) ); - assert( f5(i,j,k,l,m) == v5(i,j,k,l,m) ); - - }); + Kokkos::View v1("1", N); + Kokkos::View v2("2", N, N); + Kokkos::View v3("3", N, N, N); + Kokkos::View v4("4", N, N, N, N); + Kokkos::View v5("5", N, N, N, N, N); + + Kokkos::Array start = MeshFieldUtil::to_kokkos_array<5>({0, 0, 0, 0, 0}); + Kokkos::Array end = MeshFieldUtil::to_kokkos_array<5>({N, N, N, N, N}); + Kokkos::MDRangePolicy> p(start, end); + + Kokkos::parallel_for( + "", p, + KOKKOS_LAMBDA(const int &i, const int &j, const int &k, const int &l, + const int &m) { + v1(i) += i; + v2(i, j) += i + j; + v3(i, j, k) += i + j + k; + v4(i, j, k, l) += i + j + k + l; + v5(i, j, k, l, m) += i + j + k + l + m; + }); + + mf.setField(f1, v1); + mf.setField(f2, v2); + mf.setField(f3, v3); + mf.setField(f4, v4); + mf.setField(f5, v5); + + Kokkos::parallel_for( + "", p, + KOKKOS_LAMBDA(const int &i, const int &j, const int &k, const int &l, + const int &m) { + assert(f1(i) == v1(i)); + assert(f2(i, j) == v2(i, j)); + assert(f3(i, j, k) == v3(i, j, k)); + assert(f4(i, j, k, l) == v4(i, j, k, l)); + assert(f5(i, j, k, l, m) == v5(i, j, k, l, m)); + }); } { // CABANA FIELD const int N = 10; - using cab1 = Controller::CabanaController; + using cab1 = Controller::CabanaController; cab1 c1(N); MeshField::MeshField mf(c1); auto f1 = mf.makeField<0>(); @@ -140,58 +153,57 @@ void testSetField() { auto f3 = mf.makeField<2>(); auto f4 = mf.makeField<3>(); - Kokkos::View v1("1",N); - Kokkos::View v2("2",N,N); - Kokkos::View v3("3",N,N,N); - Kokkos::View v4("4",N,N,N,N); - - Kokkos::Array start = MeshFieldUtil::to_kokkos_array<4>({0,0,0,0}); - Kokkos::Array end = MeshFieldUtil::to_kokkos_array<4>({N,N,N,N}); - Kokkos::MDRangePolicy> p(start,end); - - Kokkos::parallel_for( "",p,KOKKOS_LAMBDA(const int& i,const int& j, const int& k, const int& l){ - v1(i) += i; - v2(i,j) += i+j; - v3(i,j,k) += i+j+k; - v4(i,j,k,l) += i+j+k+l; - }); - - mf.setField(f1,v1); - mf.setField(f2,v2); - mf.setField(f3,v3); - mf.setField(f4,v4); - - - Kokkos::parallel_for( "",p,KOKKOS_LAMBDA(const int& i,const int& j, const int& k, const int& l){ - assert( f1(i) == v1(i) ); - assert( f2(i,j) == v2(i,j) ); - assert( f3(i,j,k) == v3(i,j,k) ); - assert( f4(i,j,k,l) == v4(i,j,k,l) ); - - }); + Kokkos::View v1("1", N); + Kokkos::View v2("2", N, N); + Kokkos::View v3("3", N, N, N); + Kokkos::View v4("4", N, N, N, N); + + Kokkos::Array start = MeshFieldUtil::to_kokkos_array<4>({0, 0, 0, 0}); + Kokkos::Array end = MeshFieldUtil::to_kokkos_array<4>({N, N, N, N}); + Kokkos::MDRangePolicy> p(start, end); + + Kokkos::parallel_for( + "", p, + KOKKOS_LAMBDA(const int &i, const int &j, const int &k, const int &l) { + v1(i) += i; + v2(i, j) += i + j; + v3(i, j, k) += i + j + k; + v4(i, j, k, l) += i + j + k + l; + }); + + mf.setField(f1, v1); + mf.setField(f2, v2); + mf.setField(f3, v3); + mf.setField(f4, v4); + + Kokkos::parallel_for( + "", p, + KOKKOS_LAMBDA(const int &i, const int &j, const int &k, const int &l) { + assert(f1(i) == v1(i)); + assert(f2(i, j) == v2(i, j)); + assert(f3(i, j, k) == v3(i, j, k)); + assert(f4(i, j, k, l) == v4(i, j, k, l)); + }); } - printf("== END testSetField ==\n"); } void testSetCorrect() { - + printf("== START testSetCorrect ==\n"); const int N = 10; - using kok1 = Controller::KokkosController; - kok1 c1({N,1, - N,N,N, - N,N,15,N, - N,N,N,N,6}); + using kok1 = Controller::KokkosController; + kok1 c1({N, 1, N, N, N, N, N, 15, N, N, N, N, N, 6}); // Checking that sizes are loaded correctly - assert(c1.size(0,1) == 1); - assert(c1.size(2,2) == 15); - assert(c1.size(3,4) == 6); - + assert(c1.size(0, 1) == 1); + assert(c1.size(2, 2) == 15); + assert(c1.size(3, 4) == 6); + printf("== END testSetCorrect ==\n"); }