diff --git a/CMakeLists.txt b/CMakeLists.txt index 3935c360..5c8a0bfb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,13 +24,6 @@ endif(NOT CMAKE_BUILD_TYPE) # Macros and packages set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake/Modules/") -# --- custom targets: --- -if (NOT TARGET distclean) - # Only include distclean if it has not already been defined (by ELEMENTS or - # any other package that defines distclean and uses MATAR as a submodule) - INCLUDE(cmake/Modules/TargetDistclean.cmake OPTIONAL) -endif (NOT TARGET distclean) - find_package(Vector) if (CMAKE_VECTOR_NOVEC) set(VECTOR_C_FLAGS "${VECTOR_NOVEC_C_FLAGS}") @@ -62,8 +55,18 @@ if(Matar_ENABLE_KOKKOS) else() find_package(Kokkos REQUIRED) endif() + if (Matar_ENABLE_MPI) + find_package(MPI REQUIRED) + add_definitions(-DHAVE_MPI=1) + target_link_libraries(matar INTERFACE Kokkos::kokkos MPI::MPI_CXX) + else() target_link_libraries(matar INTERFACE Kokkos::kokkos) + endif() add_definitions(-DHAVE_KOKKOS=1) +elseif(Matar_ENABLE_MPI) + find_package(MPI REQUIRED) + target_link_libraries(matar INTERFACE MPI::MPI_CXX) + add_definitions(-DHAVE_MPI=1) endif() include(CMakePackageConfigHelpers) diff --git a/cmake/Modules/TargetDistclean.cmake b/cmake/Modules/TargetDistclean.cmake index c43a18df..90434028 100644 --- a/cmake/Modules/TargetDistclean.cmake +++ b/cmake/Modules/TargetDistclean.cmake @@ -31,11 +31,6 @@ IF (UNIX) */*/cmake_install.cmake install_manifest.txt */install_manifest.txt - Kokkos - kokkos - src - test - mtest ) ADD_CUSTOM_COMMAND( diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 040ef875..050cf586 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,65 +1,92 @@ cmake_minimum_required(VERSION 3.8) +# --- custom targets: --- + set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) -find_package(Matar REQUIRED) +if (NOT TARGET distclean) + # Only include distclean if it has not already been defined (by any + # other package that defines distclean and uses MATAR as a submodule) + INCLUDE(../cmake/Modules/TargetDistclean.cmake OPTIONAL) +endif (NOT TARGET distclean) -add_executable(mtest main.cpp) -target_link_libraries(mtest matar) +set(LINKING_LIBRARIES matar) + +find_package(Matar REQUIRED) +if (MPI) + find_package(MPI REQUIRED) + add_definitions(-DHAVE_MPI=1) + list(APPEND LINKING_LIBRARIES MPI::MPI_CXX) + if (GPU_AWARE_MPI) + add_definitions(-DHAVE_GPU_AWARE_MPI) +# elseif (GPU_SHARED_MEM) +# add_definitions(-DHAVE_GPU_SHARED_MEM) + endif() +endif() if (NOT KOKKOS) + add_executable(mtest main.cpp) + target_link_libraries(mtest ${LINKING_LIBRARIES}) + add_executable(test_for test_for.cpp) - target_link_libraries(test_for matar) + target_link_libraries(test_for ${LINKING_LIBRARIES}) add_executable(test_shared_ptr test_shared_ptr.cpp) - target_link_libraries(test_shared_ptr matar) + target_link_libraries(test_shared_ptr ${LINKING_LIBRARIES}) endif() if (KOKKOS) find_package(Kokkos REQUIRED) #new + list(APPEND LINKING_LIBRARIES Kokkos::kokkos) + + add_definitions(-DHAVE_KOKKOS=1) + + if (CUDA) + add_definitions(-DHAVE_CUDA=1) + elseif (HIP) + add_definitions(-DHAVE_HIP=1) + elseif (OPENMP) + add_definitions(-DHAVE_OPENMP=1) + elseif (THREADS) + add_definitions(-DHAVE_THREADS=1) + endif() + add_executable(mtestkokkos main_kokkos.cpp) - target_link_libraries(mtestkokkos matar Kokkos::kokkos) + target_link_libraries(mtestkokkos ${LINKING_LIBRARIES}) add_executable(test_kokkos_for kokkos_for.cpp) - target_link_libraries(test_kokkos_for matar Kokkos::kokkos) + target_link_libraries(test_kokkos_for ${LINKING_LIBRARIES}) add_executable(test_dual_types test_dual_types.cpp) - target_link_libraries(test_dual_types matar Kokkos::kokkos) - - add_definitions(-DHAVE_KOKKOS=1) + target_link_libraries(test_dual_types ${LINKING_LIBRARIES}) add_executable(kokkos_csr CSRKokkos.cpp) - target_link_libraries(kokkos_csr matar Kokkos::kokkos) + target_link_libraries(kokkos_csr ${LINKING_LIBRARIES}) add_executable(kokkos_csc CSCKokkos.cpp) - target_link_libraries(kokkos_csc matar Kokkos::kokkos) + target_link_libraries(kokkos_csc ${LINKING_LIBRARIES}) add_executable(mtr_kokkos-simple mtr-kokkos-simple.cpp) - target_link_libraries(mtr_kokkos-simple matar Kokkos::kokkos) - + target_link_libraries(mtr_kokkos-simple ${LINKING_LIBRARIES}) - if (CUDA) - add_definitions(-DHAVE_CUDA=1) - elseif (HIP) - add_definitions(-DHAVE_HIP=1) - elseif (OPENMP) - add_definitions(-DHAVE_OPENMP=1) + if (OPENMP) add_executable(parallel_hello_world parallel_hello_world.cpp) - target_link_libraries(parallel_hello_world matar Kokkos::kokkos) - elseif (THREADS) - add_definitions(-DHAVE_THREADS=1) + target_link_libraries(parallel_hello_world ${LINKING_LIBRARIES}) endif() endif() - ### HIP Linking error, will add back in after fixed if (NOT HIP) include_directories(virtualFcnKokkos) add_subdirectory(virtualFcnKokkos) endif() +# In testing, not working +#include_directories(gArrayofgArrays) +#add_subdirectory(gArrayofgArrays) + include_directories(virtualFcnMATAR) add_subdirectory(virtualFcnMATAR) @@ -72,8 +99,8 @@ add_subdirectory(halfspace_cooling) include_directories(watt-graph) add_subdirectory(watt-graph) -include_directories(matar_fortran) -add_subdirectory(matar_fortran) +#include_directories(matar_fortran) +#add_subdirectory(matar_fortran) include_directories(sparsetests) add_subdirectory(sparsetests) @@ -81,8 +108,8 @@ add_subdirectory(sparsetests) include_directories(test_rocm) add_subdirectory(test_rocm) -#include_directories(laplaceMPI) -#add_subdirectory(laplaceMPI) +include_directories(laplaceMPI) +add_subdirectory(laplaceMPI) #include_directories(phaseField/srcKokkosVerbose) #add_subdirectory(phaseField/srcKokkosVerbose) diff --git a/examples/gArrayofgArrays/CMakeLists.txt b/examples/gArrayofgArrays/CMakeLists.txt new file mode 100644 index 00000000..acbfb6fa --- /dev/null +++ b/examples/gArrayofgArrays/CMakeLists.txt @@ -0,0 +1,23 @@ +cmake_minimum_required(VERSION 3.1.3) + +find_package(Matar REQUIRED) + +if (KOKKOS) + add_definitions(-DHAVE_KOKKOS=1) + find_package(Kokkos REQUIRED) + + add_definitions(-DHAVE_KOKKOS=1) + if (CUDA) + add_definitions(-DHAVE_CUDA=1) + elseif (HIP) + add_definitions(-DHAVE_HIP=1) + elseif (OPENMP) + add_definitions(-DHAVE_OPENMP=1) + elseif (THREADS) + add_definitions(-DHAVE_THREADS=1) + endif() + + add_executable(viewofview main.cpp) + target_link_libraries(viewofview matar Kokkos::kokkos) + +endif() diff --git a/src/matar.h b/examples/gArrayofgArrays/main.cpp similarity index 50% rename from src/matar.h rename to examples/gArrayofgArrays/main.cpp index 7566ded2..3e829cf4 100644 --- a/src/matar.h +++ b/examples/gArrayofgArrays/main.cpp @@ -1,7 +1,5 @@ -#ifndef MATAR_H -#define MATAR_H /********************************************************************************************** - © 2020. Triad National Security, LLC. All rights reserved. + © 2020. Triad National Security, LLC. All rights reserved. This program was produced under U.S. Government contract 89233218CNA000001 for Los Alamos National Laboratory (LANL), which is operated by Triad National Security, LLC for the U.S. Department of Energy/National Nuclear Security Administration. All rights in the program are @@ -13,14 +11,11 @@ This program is open source under the BSD-3 License. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. @@ -36,52 +31,80 @@ OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. **********************************************************************************************/ +#include +#include +#include "matar.h" +using namespace mtr; -// ---- -// Standard data structures (host types) -// 1. FArray -// 2. ViewFArray -// 3. FMatrix -// 4. ViewFMatrix -// 5. CArray -// 6. ViewCArray -// 7. CMatrix -// 8. ViewCMatrix -// 9. RaggedRightArray -// 10. RaggedDownArray -// 11. DynamicRaggedRightArray -// 12. DynamicRaggedDownArray -// 13. CSRArray -// 14. CSCArray +struct thing +{ + int a; + DCArrayKokkos inner; +}; -// ---- -// Kokkos Data structures (device and dual types) -// 15. FArrayKokkos -// 16. ViewFArrayKokkos -// 17. FMatrixKokkos -// 18. ViewFMatrixKokkos -// 19. CArrayKokkos -// 20. ViewCArrayKokkos -// 21. CMatrixKokkos -// 22. ViewCMatrixKokkos -// 23. RaggedRightArrayKokkos -// 24. RaggedDownArrayKokkos -// 25. DynamicRaggedRightArrayKokkos -// 26. DynamicRaggedDownArrayKokkos -// 27. CSRArrayKokkos -// 28. CSCArrayKokkos -// 29. DViewCArrayKokkos -// 30. DViewCMatrixKokkos -// 31. DViewFArrayKokkos -// 32. DViewFMatrixKokkos +int main(int argc, char* argv[]) +{ + Kokkos::initialize(argc, argv); + { // kokkos scope + const size_t num_things = 4; + auto array_of_things = DCArrayKokkos (num_things); + for (size_t i = 0; i < num_things; i++) { + array_of_things.host(i).inner = (DCArrayKokkos*)Kokkos::kokkos_malloc(sizeof(DCArrayKokkos)); + } + // Update device side of array of memory location on GPU + array_of_things.update_device(); -#include "macros.h" -#include "host_types.h" -#include "kokkos_types.h" -#include "aliases.h" +/* + // Create shapes using `placement new`. Even=Circle, Odd=Square. Radius=i, Length=i. + FOR_ALL(i, 0, num_things, { + new ((DCArrayKokkos*)array_of_things(i).inner) DCArrayKokkos(8); + }); + Kokkos::fence(); + // Calculate Area + DCArrayKokkos area_array(num_shapes); + FOR_ALL(i, 0, num_shapes, { + area_array(i) = shape_array(i).shape->area(); + }); + Kokkos::fence(); + area_array.update_host(); + // Check result + for (size_t i = 0; i < num_shapes; i++) { + double area; + if (i % 2 == 0) { + area = atan(1) * 4 * i * i; + if (area != area_array.host(i)) { + printf("Circle radius=%.3f, calc_area=%.3f, actual_area=%.3f\n", i, area_array.host(i), area); + } + } + else { + area = i * i; + if (area != area_array.host(i)) { + printf("Square length=%.3f, calc_area=%.3f, actual_area=%.3f\n", i, area_array.host(i), area); + } + } + if (area != area_array.host(i)) { + throw std::runtime_error("calculated area NOT EQUAL actual area"); + } + } + // Destroy shapes + FOR_ALL(i, 0, num_things, { + array_of_things(i).inner->~DCArrayKokkos(); + }); + Kokkos::fence(); -#endif // MATAR_H + // Free GPU memory + for (size_t i = 0; i < num_shapes; i++) { + Kokkos::kokkos_free(array_of_things.host(i).inner); + } +*/ + + printf("COMPLETED SUCCESSFULLY!!!\n"); + } // end kokkos scope + Kokkos::finalize(); + + return 0; +} diff --git a/examples/halfspace_cooling/CMakeLists.txt b/examples/halfspace_cooling/CMakeLists.txt index 07c9f930..5714039a 100644 --- a/examples/halfspace_cooling/CMakeLists.txt +++ b/examples/halfspace_cooling/CMakeLists.txt @@ -9,5 +9,5 @@ if (KOKKOS) add_definitions(-DHAVE_KOKKOS=1) - target_link_libraries(halfspace_cooling matar Kokkos::kokkos) + target_link_libraries(halfspace_cooling ${LINKING_LIBRARIES}) endif() diff --git a/examples/kokkos_for.cpp b/examples/kokkos_for.cpp index 9effb331..0026211a 100644 --- a/examples/kokkos_for.cpp +++ b/examples/kokkos_for.cpp @@ -47,14 +47,14 @@ int main() // Kokkos::View arr("ARR", 10); CArrayKokkos arr(10); FOR_ALL(i, 0, 10, { - arr(i) = 314; + arr(i) = i; }); // Kokkos::View arr_2D("ARR_2D", 10,10); CArrayKokkos arr_2D(10, 10); FOR_ALL(i, 0, 10, j, 0, 10, { - arr_2D(i, j) = 314; + arr_2D(i, j) = j * 10 + i; }); // Kokkos::View arr_3D("ARR_3D", 10,10,10); @@ -62,7 +62,7 @@ int main() FOR_ALL(i, 0, 10, j, 0, 10, k, 0, 10, { - arr_3D(i, j, k) = 314; + arr_3D(i, j, k) = k * 10 * 10 + j * 10 + i; }); int loc_sum = 0; diff --git a/examples/laplace/CMakeLists.txt b/examples/laplace/CMakeLists.txt index c0ab2af4..45d5fbd4 100644 --- a/examples/laplace/CMakeLists.txt +++ b/examples/laplace/CMakeLists.txt @@ -8,10 +8,10 @@ if (NOT KOKKOS) add_executable(farray_right main_farray_right.cpp) add_executable(farray_wrong main_farray_wrong.cpp) - target_link_libraries(carray_right matar) - target_link_libraries(carray_wrong matar) - target_link_libraries(farray_right matar) - target_link_libraries(farray_wrong matar) + target_link_libraries(carray_right ${LINKING_LIBRARIES}) + target_link_libraries(carray_wrong ${LINKING_LIBRARIES}) + target_link_libraries(farray_right ${LINKING_LIBRARIES}) + target_link_libraries(farray_wrong ${LINKING_LIBRARIES}) endif() if (KOKKOS) @@ -25,9 +25,9 @@ if (KOKKOS) add_definitions(-DHAVE_KOKKOS=1) - target_link_libraries(kokkosview matar Kokkos::kokkos) - target_link_libraries(carraykokkos_default_indexing matar Kokkos::kokkos) - target_link_libraries(carraykokkos_c_indexing matar Kokkos::kokkos) - target_link_libraries(farraykokkos_default_indexing matar Kokkos::kokkos) - target_link_libraries(farraykokkos_f_indexing matar Kokkos::kokkos) + target_link_libraries(kokkosview ${LINKING_LIBRARIES}) + target_link_libraries(carraykokkos_default_indexing ${LINKING_LIBRARIES}) + target_link_libraries(carraykokkos_c_indexing ${LINKING_LIBRARIES}) + target_link_libraries(farraykokkos_default_indexing ${LINKING_LIBRARIES}) + target_link_libraries(farraykokkos_f_indexing ${LINKING_LIBRARIES}) endif() diff --git a/examples/laplaceMPI/CMakeLists.txt b/examples/laplaceMPI/CMakeLists.txt index 80b987c1..cedc01b2 100644 --- a/examples/laplaceMPI/CMakeLists.txt +++ b/examples/laplaceMPI/CMakeLists.txt @@ -4,7 +4,11 @@ if (KOKKOS) find_package(Kokkos REQUIRED) #new find_package(MPI REQUIRED) - add_executable(laplace_mpi laplace_mpi.cpp) + #add_executable(laplace_mpi laplace_mpi.cpp) + #add_executable(laplace_mpi simple_mpi.cpp) + #add_executable(laplace_mpi mpi_mesh_test.cpp) + add_executable(laplace_mpi simple_halo.cpp) + add_definitions(-DHAVE_MPI=1) add_definitions(-DHAVE_KOKKOS=1) if (CUDA) @@ -21,5 +25,5 @@ if (KOKKOS) endif() - target_link_libraries(laplace_mpi matar MPI::MPI_CXX) + target_link_libraries(laplace_mpi ${LINKING_LIBRARIES}) endif() diff --git a/examples/laplaceMPI/mpi_mesh_test.cpp b/examples/laplaceMPI/mpi_mesh_test.cpp new file mode 100644 index 00000000..d76bd3ea --- /dev/null +++ b/examples/laplaceMPI/mpi_mesh_test.cpp @@ -0,0 +1,211 @@ +#include +#include +#include +#include +#include +#include +#include + +// Dont change ROOT +#define ROOT 0 +//---------------- +#define N 20 + +using namespace mtr; // matar namespace + int get_id(int i, int j, int k, int num_i, int num_j) + { + return i + j * num_i + k * num_i * num_j; + } + + void build_3d_box(int world_size, int rank) + { + printf("Rank %d Creating a 3D box mesh \n", rank); + + const int num_dim = 3; + + const double lx = pow(2.0, (double) N); + const double ly = pow(2.0, (double) N); + const double lz = pow(2.0, (double) N); + + const double num_elems_i = N; + const double num_elems_j = N; + const double num_elems_k = N; + + const int num_points_i = num_elems_i + 1; // num points in x + const int num_points_j = num_elems_j + 1; // num points in y + const int num_points_k = num_elems_k + 1; // num points in y + + const int num_nodes = num_points_i * num_points_j * num_points_k; + + const double dx = lx / ((double)num_elems_i); // len/(num_elems_i) + const double dy = ly / ((double)num_elems_j); // len/(num_elems_j) + const double dz = lz / ((double)num_elems_k); // len/(num_elems_k) + + const int num_elems = num_elems_i * num_elems_j * num_elems_k; + std::vector origin(num_dim); + for (int i = 0; i < num_dim; i++) { origin[i] = 0; } + + // --- 3D parameters --- + // const int num_faces_in_elem = 6; // number of faces in elem + // const int num_points_in_elem = 8; // number of points in elem + // const int num_points_in_face = 4; // number of points in a face + // const int num_edges_in_elem = 12; // number of edges in a elem + + // --- mesh node ordering --- + // Convert ijk index system to the finite element numbering convention + // for vertices in elem + auto convert_point_number_in_Hex = CArray(8); + convert_point_number_in_Hex(0) = 0; + convert_point_number_in_Hex(1) = 1; + convert_point_number_in_Hex(2) = 3; + convert_point_number_in_Hex(3) = 2; + convert_point_number_in_Hex(4) = 4; + convert_point_number_in_Hex(5) = 5; + convert_point_number_in_Hex(6) = 7; + convert_point_number_in_Hex(7) = 6; + + int rk_num_bins = 1; + + // intialize node variables + + // extra math for mpi decomposition + int grid_world_size, krem, jrem, col, row, kadd, jadd, ksub, jsub, kchunk, jchunk, kstart, kend, jstart, jend; + grid_world_size = sqrt(world_size); + krem = num_points_k % grid_world_size; // how many extra points we have after a division of ranks + jrem = num_points_j % grid_world_size; + col = rank % grid_world_size; + row = rank / grid_world_size; + kadd = col / (grid_world_size - krem); // 1 if you have an extra piece, 0 otherwise + jadd = row / (grid_world_size - jrem); // 1 if you have an extra piece, 0 otherwise + ksub = kadd * (grid_world_size - krem); // subtraction from your start based on how many other ranks have an extra piece + jsub = jadd * (grid_world_size - jrem); + kchunk = num_points_k / grid_world_size + kadd; + jchunk = num_points_j / grid_world_size + jadd; + kstart = col * kchunk - ksub; + kend = kstart + kchunk; + jstart = row * jchunk - jsub; + jend = jstart + jchunk; + + // --- Build nodes --- + auto coords = MPIArrayKokkos (rk_num_bins, num_points_i * kchunk * jchunk, num_dim); + // populate the point data structures + for (int k = kstart, k_loc = 0; k < kend; k++, k_loc++) { + for (int j = jstart, j_loc = 0; j < jend; j++, j_loc++) { + for (int i = 0; i < num_points_i; i++) { + int node_gid = get_id(i, j_loc, k_loc, num_points_i, jchunk); + + // store the point coordinates + coords.host(0, node_gid, 0) = origin[0] + (double)i * dx; + coords.host(0, node_gid, 1) = origin[1] + (double)j * dy; + coords.host(0, node_gid, 2) = origin[2] + (double)k * dz; + } // end for i + } // end for j + } // end for k + + for (int rk_level = 1; rk_level < rk_num_bins; rk_level++) { + for (int node_gid = 0; node_gid < num_points_i * kchunk * jchunk; node_gid++) { + coords.host(rk_level, node_gid, 0) = coords.host(0, node_gid, 0); + coords.host(rk_level, node_gid, 1) = coords.host(0, node_gid, 1); + coords.host(rk_level, node_gid, 2) = coords.host(0, node_gid, 2); + } + } + coords.update_device(); + + // intialize elem variables + + krem = (int) num_elems_k % grid_world_size; // how many extra points we have after a division of ranks + jrem = (int) num_elems_j % grid_world_size; + col = rank % grid_world_size; + row = rank / grid_world_size; + kadd = col / (grid_world_size - krem); // 1 if you have an extra piece, 0 otherwise + jadd = row / (grid_world_size - jrem); // 1 if you have an extra piece, 0 otherwise + ksub = kadd * (grid_world_size - krem); // subtraction from your start based on how many other ranks have an extra piece + jsub = jadd * (grid_world_size - jrem); + kchunk = (int) num_elems_k / grid_world_size + kadd; + jchunk = (int) num_elems_j / grid_world_size + jadd; + kstart = col * kchunk - ksub; + kend = kstart + kchunk; + jstart = row * jchunk - jsub; + jend = jstart + jchunk; + // --- Build elems --- + + auto nodes_in_elem = MPIArrayKokkos (num_elems_i * kchunk * jchunk, 8); + // populate the elem center data structures + for (int k = kstart, k_loc = 0; k < kend / world_size; k++, k_loc++) { + for (int j = kstart, j_loc = 0; j < jend; j++, j_loc++) { + for (int i = 0; i < num_elems_i; i++) { + int elem_gid = get_id(i, j_loc, k_loc, num_elems_i, jchunk); + + // store the point IDs for this elem where the range is + // (i:i+1, j:j+1, k:k+1) for a linear hexahedron + int this_point = 0; + for (int kcount = k; kcount <= k + 1; kcount++) { + for (int jcount = j; jcount <= j + 1; jcount++) { + for (int icount = i; icount <= i + 1; icount++) { + int node_gid = get_id(icount, jcount, kcount, + num_points_i, num_points_j); + + // convert this_point index to the FE index convention + int this_index = convert_point_number_in_Hex(this_point); + + // store the points in this elem according the the finite + // element numbering convention + nodes_in_elem.host(elem_gid, this_index) = node_gid; + + // increment the point counting index + this_point = this_point + 1; + } // end for icount + } // end for jcount + } // end for kcount + } // end for i + } // end for j + } // end for k + // update device side + nodes_in_elem.update_device(); + // intialize corner variables + //corner.initialize(num_corners, num_dim); + + // Build connectivity + //mesh.build_connectivity(); +} + +int main(int argc, char *argv[]) +{ + + //MPI_Init(&argc, &argv); + //Kokkos::initialize(argc, argv); + MATAR_MPI_INIT + MATAR_KOKKOS_INIT + { // kokkos scope + + //double begin_time_total = MPI_Wtime(); + double begin_time_total = MATAR_MPI_TIME + + int world_size, + rank; + + // get world_size and rank + MPI_Comm_size(MPI_COMM_WORLD, &world_size); + MPI_Comm_rank(MPI_COMM_WORLD, &rank); + + build_3d_box(world_size, rank); + + // stop timing + //double end_time = MPI_Wtime(); + double end_time = MATAR_MPI_TIME + + if (rank == ROOT) { + printf("\n"); + printf("Number of MPI processes = %d\n", world_size); + printf("Total code time was %10.6e seconds.\n", end_time-begin_time_total); + //printf("Main loop time was %10.6e seconds.\n", end_time-begin_time_main_loop); + } + + + } // end kokkos scope + MATAR_KOKKOS_FINALIZE + MATAR_MPI_FINALIZE + //Kokkos::finalize(); + //MPI_Finalize(); + return 0; +} diff --git a/examples/laplaceMPI/simple_halo.cpp b/examples/laplaceMPI/simple_halo.cpp new file mode 100644 index 00000000..4aa38718 --- /dev/null +++ b/examples/laplaceMPI/simple_halo.cpp @@ -0,0 +1,302 @@ +#include +#include +#include +#include +#include +#include +#include + +// Dont change ROOT +#define ROOT 0 +//---------------- +// For overlapping async calls with setup +#define FAST + +using namespace mtr; // matar namespace + +int width = 1000; +int height = 1000; +int max_num_iterations = 1000; +double temp_tolerance = 0.01; + + +// Example for DENSE halo sending and receiving +// Multiple, non uniform halo communication - assumes 4 ranks for this example +// Ranks + (size-1) = # of sends +// 0 sends to 1,2,3 and receives from no one - 1 sends to 2,3 and receives from 0 - 2 sends to 3 and receives from 0,1, 3 sends to no one receives from all +// Done asynchronously +void example_nonuniform_halo_comms(int world_size, int rank) { + int send_rank = rank + 1; + int recv_rank = ROOT; + int tag = 99 + 3 * (rank + send_rank); + int r_tag = 99 + 3 * (recv_rank + rank); + int size = 5 + rank; + int in_size = 5 + recv_rank; + + MPIArrayKokkos myhalo; + MPIArrayKokkos halo0; + MPIArrayKokkos halo1; + MPIArrayKokkos halo2; + + myhalo = MPIArrayKokkos (size); + if (rank == 0) { + // send rank needs to be updated for each rank + myhalo.mpi_setup(send_rank, tag, MPI_COMM_WORLD); +#ifdef FAST + myhalo.halo_isend(); + tag = 99 + 3 * (++send_rank + rank); + myhalo.mpi_set_rank(send_rank); + myhalo.mpi_set_tag(tag); + myhalo.halo_isend(); + tag = 99 + 3 * (++send_rank + rank); + myhalo.mpi_set_rank(send_rank); + myhalo.mpi_set_tag(tag); + myhalo.halo_isend(); + // Waits + myhalo.wait_send(); + tag = 99 + 3 * (--send_rank + rank); + myhalo.mpi_set_rank(send_rank); + myhalo.mpi_set_tag(tag); + myhalo.wait_send(); + tag = 99 + 3 * (--send_rank + rank); + myhalo.mpi_set_rank(send_rank); + myhalo.mpi_set_tag(tag); + myhalo.wait_send(); +#endif + } + else if (rank == 1) { + myhalo.mpi_setup(send_rank, tag, MPI_COMM_WORLD); +#ifdef FAST + // Sends + myhalo.halo_isend(); + tag = 99 + 3 * (++send_rank + rank); + myhalo.mpi_set_rank(send_rank); + myhalo.mpi_set_tag(tag); + myhalo.halo_isend(); +#endif + halo0 = MPIArrayKokkos (in_size); + halo0.mpi_setup(recv_rank, r_tag, MPI_COMM_WORLD); +#ifdef FAST + // Recvs + halo0.halo_irecv(); + // Waits + halo0.wait_recv(); + myhalo.wait_send(); + tag = 99 + 3 * (--send_rank + rank); + myhalo.mpi_set_rank(send_rank); + myhalo.mpi_set_tag(tag); + myhalo.wait_send(); +#endif + } + else if (rank == 2) { + myhalo.mpi_setup(send_rank, tag, MPI_COMM_WORLD); +#ifdef FAST + myhalo.halo_isend(); +#endif + halo0 = MPIArrayKokkos (in_size); + halo0.mpi_setup(recv_rank, r_tag, MPI_COMM_WORLD); +#ifdef FAST + halo0.halo_irecv(); +#endif + halo1 = MPIArrayKokkos (in_size+1); + r_tag = 99 + 3 * (++recv_rank + rank); + halo1.mpi_setup(recv_rank, r_tag, MPI_COMM_WORLD); +#ifdef FAST + // Recvs + halo1.halo_irecv(); + // Waits + halo0.wait_recv(); + halo1.wait_recv(); + myhalo.wait_send(); +#endif + } + else if (rank == 3) { + myhalo.mpi_setup(send_rank, tag, MPI_COMM_WORLD); + halo0 = MPIArrayKokkos (in_size); + halo0.mpi_setup(recv_rank, r_tag, MPI_COMM_WORLD); +#ifdef FAST + halo0.halo_irecv(); +#endif + halo1 = MPIArrayKokkos (in_size+1); + r_tag = 99 + 3 * (++recv_rank + rank); + halo1.mpi_setup(recv_rank, r_tag, MPI_COMM_WORLD); +#ifdef FAST + halo1.halo_irecv(); +#endif + halo2 = MPIArrayKokkos (in_size+2); + r_tag = 99 + 3 * (++recv_rank + rank); + halo2.mpi_setup(recv_rank, r_tag, MPI_COMM_WORLD); +#ifdef FAST + halo2.halo_irecv(); + // Waits + halo0.wait_recv(); + halo1.wait_recv(); + halo2.wait_recv(); +#endif + } + else { + printf("size is too big, rank %d will not do work\n", rank); + } + + FOR_ALL(idx, 0, size, { + myhalo(idx) = idx * rank; + }); + Kokkos::fence(); + +#ifndef FAST + if (rank == 0) { + // Sends + //printf("Rank %d sending message to %d with tag %d\n", rank, myhalo.get_rank(), myhalo.get_tag()); + myhalo.halo_isend(); + tag = 99 + 3 * (++send_rank + rank); + myhalo.mpi_set_rank(send_rank); + myhalo.mpi_set_tag(tag); + myhalo.halo_isend(); + tag = 99 + 3 * (++send_rank + rank); + myhalo.mpi_set_rank(send_rank); + myhalo.mpi_set_tag(tag); + myhalo.halo_isend(); + // Waits + myhalo.wait_send(); + tag = 99 + 3 * (--send_rank + rank); + myhalo.mpi_set_rank(send_rank); + myhalo.mpi_set_tag(tag); + myhalo.wait_send(); + tag = 99 + 3 * (--send_rank + rank); + myhalo.mpi_set_rank(send_rank); + myhalo.mpi_set_tag(tag); + myhalo.wait_send(); + } + else if (rank == 1) { + // Sends + myhalo.halo_isend(); + tag = 99 + 3 * (++send_rank + rank); + myhalo.mpi_set_rank(send_rank); + myhalo.mpi_set_tag(tag); + myhalo.halo_isend(); + // Recvs + halo0.halo_irecv(); + // Waits + halo0.wait_recv(); + myhalo.wait_send(); + tag = 99 + 3 * (--send_rank + rank); + myhalo.mpi_set_rank(send_rank); + myhalo.mpi_set_tag(tag); + myhalo.wait_send(); + } + else if (rank == 2) { + // Sends + myhalo.halo_isend(); + // Recvs + halo0.halo_irecv(); + halo1.halo_irecv(); + // Waits + halo0.wait_recv(); + halo1.wait_recv(); + myhalo.wait_send(); + } + else if (rank == 3) { + // Recvs + halo0.halo_irecv(); + halo1.halo_irecv(); + halo2.halo_irecv(); + // Waits + halo0.wait_recv(); + halo1.wait_recv(); + halo2.wait_recv(); + } + else { + printf("size is too big, rank %d will not do work\n", rank); + } +#endif + + if (rank > 1) { + // Print statement just to show it copied up to GPU. + FOR_ALL(idx, 0, in_size+1, { + printf("Rank %d) idx %d with value %d\n", rank, idx, halo1(idx)); + }); + Kokkos::fence(); + } + +} + +// Example for DENSE halo sending and receiving +// Only 1 halo communication +// Done asynchrnously +void example_halo_comms(int world_size, int rank) { + int size = 10; + int s_neighbor = (rank + 1) % world_size; + int r_neighbor = (rank + (world_size - 1)) % world_size; + int s_tag = 10 * rank + 20 * s_neighbor; + int r_tag = 20 * rank + 10 * r_neighbor; + + MPIArrayKokkos myhalo = MPIArrayKokkos (size); + MPIArrayKokkos neibhalo = MPIArrayKokkos (size); + + //printf("Rank %d rneighb %d rtag %d sneighb %d stag %d\n", rank, r_neighbor, r_tag, s_neighbor, s_tag); + + myhalo.mpi_setup(s_neighbor, s_tag, MPI_COMM_WORLD); + neibhalo.mpi_setup(r_neighbor, r_tag, MPI_COMM_WORLD); + FOR_ALL(idx, 0, size, { + myhalo(idx) = idx * rank; + }); + Kokkos::fence(); + + // Send halos + myhalo.halo_isend(); + // Receiv halos + //neibhalo.halo_recv(); + neibhalo.halo_irecv(); + // Block until I have received + neibhalo.wait_recv(); + // Block until that send has been received + myhalo.wait_send(); + // The asynch receive is not mandatory + // Could simply asynch send, then have a blocking receive + // Asynch send is necessary to not get a communication block + +///* + FOR_ALL(idx, 0, size, { + printf("Rank %d) idx %d with value %d\n", rank, idx, neibhalo(idx)); + }); + Kokkos::fence(); +//*/ +} + +int main(int argc, char *argv[]) +{ + + MPI_Init(&argc, &argv); + Kokkos::initialize(argc, argv); + { // kokkos scope + + double begin_time_total = MPI_Wtime(); + + int world_size, + rank; + + // get world_size and rank + MPI_Comm_size(MPI_COMM_WORLD, &world_size); + MPI_Comm_rank(MPI_COMM_WORLD, &rank); + + //example_halo_comms(world_size, rank); + example_nonuniform_halo_comms(world_size, rank); + + // stop timing + double end_time = MPI_Wtime(); + + if (rank == ROOT) { + printf("\n"); + printf("Number of MPI processes = %d\n", world_size); + printf("Total code time was %10.6e seconds.\n", end_time-begin_time_total); + //printf("Main loop time was %10.6e seconds.\n", end_time-begin_time_main_loop); + } + + + } // end kokkos scope + Kokkos::finalize(); + MPI_Finalize(); + return 0; +} + + diff --git a/examples/laplaceMPI/simple_mpi.cpp b/examples/laplaceMPI/simple_mpi.cpp new file mode 100644 index 00000000..decd72b3 --- /dev/null +++ b/examples/laplaceMPI/simple_mpi.cpp @@ -0,0 +1,204 @@ +#include +#include +#include +#include +#include +#include +#include + +// Dont change ROOT +#define ROOT 0 +//---------------- + +// Change to 0 or 1 as needed +#define TRACK_PROGRESS 0 + +#if defined HAVE_CUDA || defined HAVE_HIP + #define GPU 1 +#else + #define GPU 0 +#endif + +using namespace mtr; // matar namespace + +int width = 1000; +int height = 1000; +int max_num_iterations = 1000; +double temp_tolerance = 0.01; + +// Example for the scatter and gather functions +// All it does is take an array, split it up, +// half the value of each chunk, and send those +// back to be grouped again by the root +void example_gather(int world_size, int rank) { + int size = 12; + int sub_size = size / world_size; + MPIArrayKokkos arrAll = MPIArrayKokkos (MPI_FLOAT, size); + MPIArrayKokkos arrSub = MPIArrayKokkos (MPI_FLOAT, sub_size); + + if (rank == ROOT) { + FOR_ALL(idx, 0, size, { + arrAll(idx) = idx+1; + printf("%d) %f\n", idx, arrAll(idx)); + }); + Kokkos::fence(); + printf("------------\n"); + } + + arrAll.scatter(sub_size, arrSub, sub_size, ROOT, MPI_COMM_WORLD); + + FOR_ALL(idx, 0, sub_size, { + arrSub(idx) = arrSub(idx) / 2; + }); + Kokkos::fence(); + + // Gather + /* + arrSub.gather(sub_size, arrAll, sub_size, ROOT, MPI_COMM_WORLD); + + if (rank == ROOT) { + FOR_ALL(idx, 0, size, { + printf("%d) %f\n", idx, arrAll(idx)); + }); + Kokkos::fence(); + } + */ + // Allgather + arrSub.allgather(sub_size, arrAll, sub_size, MPI_COMM_WORLD); + + // A rank other than Root + if (rank == 1) { + FOR_ALL(idx, 0, size, { + printf("%d) %f\n", idx, arrAll(idx)); + }); + Kokkos::fence(); + } +} + +// Example for halo sending and receiving +void example_halo_comms(int world_size, int rank) { + int size = 10; + int tag = 99; + + MPIArrayKokkos mca_s = MPIArrayKokkos (size); + MPIArrayKokkos mca_r = MPIArrayKokkos (size); + + if (rank == ROOT) { + FOR_ALL(idx, 0, size, { + mca_s(idx) = idx+1; + }); + Kokkos::fence(); + mca_s.mpi_setup(1, tag, MPI_COMM_WORLD); + } + else { + mca_r.mpi_setup(ROOT, tag, MPI_COMM_WORLD); + } + + if (rank == ROOT) { + mca_s.halo_send(); + } + else { + mca_r.halo_recv(); + } + + if (rank != ROOT) { + FOR_ALL(idx, 0, size, { + printf("idx %d with value %d\n", idx, mca_r(idx)); + }); + Kokkos::fence(); + } + +} + +// Example for sending, receiving, and broadcasting +void example_simple_comms(int world_size, int rank) { + int size = 10; + int tag = 99; + MPIArrayKokkos mca_s = MPIArrayKokkos (size); + MPIArrayKokkos mca_r = MPIArrayKokkos (size); + + if (rank == ROOT) { + FOR_ALL(idx, 0, size, { + mca_s(idx) = idx+1; + }); + Kokkos::fence(); + } + + if (rank == ROOT) { + mca_s.isend(size, 1, tag, MPI_COMM_WORLD); + mca_s.wait_send(); + } + else { + mca_r.irecv(size, 0, tag, MPI_COMM_WORLD); + mca_r.wait_recv(); + } + MATAR_MPI_BARRIER + + if (rank != ROOT) { + FOR_ALL(idx, 0, size, { + printf("idx %d with value %d\n", idx, mca_r(idx)); + }); + Kokkos::fence(); + } + + if (rank == ROOT) { + FOR_ALL(idx, 0, size, { + mca_s(idx) = mca_s(idx) * 2; + }); + Kokkos::fence(); + } + + mca_s.broadcast(size, ROOT, MPI_COMM_WORLD); + + if (rank != ROOT) { + FOR_ALL(idx, 0, size, { + printf("idx %d with value %d\n", idx, mca_s(idx)); + }); + Kokkos::fence(); + } +} + +int main(int argc, char *argv[]) +{ + + //MPI_Init(&argc, &argv); + //Kokkos::initialize(argc, argv); + MATAR_MPI_INIT + MATAR_KOKKOS_INIT + { // kokkos scope + + //double begin_time_total = MPI_Wtime(); + double begin_time_total = MATAR_MPI_TIME + + int world_size, + rank; + + // get world_size and rank + MPI_Comm_size(MPI_COMM_WORLD, &world_size); + MPI_Comm_rank(MPI_COMM_WORLD, &rank); + + //example_gather(world_size, rank); + //example_simple_comms(world_size, rank); + example_halo_comms(world_size, rank); + + // stop timing + //double end_time = MPI_Wtime(); + double end_time = MATAR_MPI_TIME + + if (rank == ROOT) { + printf("\n"); + printf("Number of MPI processes = %d\n", world_size); + printf("Total code time was %10.6e seconds.\n", end_time-begin_time_total); + //printf("Main loop time was %10.6e seconds.\n", end_time-begin_time_main_loop); + } + + + } // end kokkos scope + MATAR_KOKKOS_FINALIZE + MATAR_MPI_FINALIZE + //Kokkos::finalize(); + //MPI_Finalize(); + return 0; +} + + diff --git a/examples/matar_fortran/CMakeLists.txt b/examples/matar_fortran/CMakeLists.txt index 43594a83..f99bc326 100644 --- a/examples/matar_fortran/CMakeLists.txt +++ b/examples/matar_fortran/CMakeLists.txt @@ -18,5 +18,5 @@ if (KOKKOS) endif() add_executable(matar-fortran main.f90 matar_functions.cpp) - target_link_libraries(matar-fortran matar) + target_link_libraries(matar-fortran ${LINKING_LIBRARIES}) endif(KOKKOS) diff --git a/examples/sparsetests/CMakeLists.txt b/examples/sparsetests/CMakeLists.txt index 72695e45..f17d7f5f 100644 --- a/examples/sparsetests/CMakeLists.txt +++ b/examples/sparsetests/CMakeLists.txt @@ -8,10 +8,10 @@ if (KOKKOS) add_executable(powerIter powerIter.cpp) add_executable(spPower sparsePowerIter.cpp) add_executable(spatVec sparseMatVec.cpp) - target_link_libraries(spatVec matar Kokkos::kokkos) - target_link_libraries(matVec matar Kokkos::kokkos) - target_link_libraries(powerIter matar Kokkos::kokkos) - target_link_libraries(spPower matar Kokkos::kokkos) + target_link_libraries(spatVec ${LINKING_LIBRARIES}) + target_link_libraries(matVec ${LINKING_LIBRARIES}) + target_link_libraries(powerIter ${LINKING_LIBRARIES}) + target_link_libraries(spPower ${LINKING_LIBRARIES}) if (CUDA) add_definitions(-DHAVE_CUDA=1) diff --git a/examples/test_rocm/CMakeLists.txt b/examples/test_rocm/CMakeLists.txt index 7fb845d7..39d9e61d 100644 --- a/examples/test_rocm/CMakeLists.txt +++ b/examples/test_rocm/CMakeLists.txt @@ -24,5 +24,5 @@ if (KOKKOS) endif() - target_link_libraries(test_rocm matar Kokkos::kokkos) + target_link_libraries(test_rocm ${LINKING_LIBRARIES}) endif() diff --git a/examples/virtualFcnKokkos/CMakeLists.txt b/examples/virtualFcnKokkos/CMakeLists.txt index 8aeb1750..0af21ba3 100644 --- a/examples/virtualFcnKokkos/CMakeLists.txt +++ b/examples/virtualFcnKokkos/CMakeLists.txt @@ -11,5 +11,5 @@ if (KOKKOS) add_definitions(-DHAVE_KOKKOS=1) - target_link_libraries(virttestkokkos matar Kokkos::kokkos) + target_link_libraries(virttestkokkos ${LINKING_LIBRARIES}) endif() diff --git a/examples/virtualFcnMATAR/CMakeLists.txt b/examples/virtualFcnMATAR/CMakeLists.txt index d669da5e..27a32df7 100644 --- a/examples/virtualFcnMATAR/CMakeLists.txt +++ b/examples/virtualFcnMATAR/CMakeLists.txt @@ -18,6 +18,6 @@ if (KOKKOS) endif() add_executable(virtual_fcn_test main.cpp classes.cpp) - target_link_libraries(virtual_fcn_test matar Kokkos::kokkos) + target_link_libraries(virtual_fcn_test ${LINKING_LIBRARIES}) endif() diff --git a/examples/watt-graph/CMakeLists.txt b/examples/watt-graph/CMakeLists.txt index 0c922a2b..9491b38d 100644 --- a/examples/watt-graph/CMakeLists.txt +++ b/examples/watt-graph/CMakeLists.txt @@ -1,16 +1,17 @@ cmake_minimum_required(VERSION 3.1.3) -add_executable(test_floyd floyd.cpp) - -target_link_libraries(test_floyd matar) +if (NOT KOKKOS) + add_executable(test_floyd floyd.cpp) + target_link_libraries(test_floyd ${LINKING_LIBRARIES}) +endif() if (KOKKOS) find_package(Kokkos REQUIRED) add_definitions(-DHAVE_KOKKOS=1) add_executable(test_kokkos_floyd kokkos_floyd.cpp) - target_link_libraries(test_kokkos_floyd matar Kokkos::kokkos) + target_link_libraries(test_kokkos_floyd ${LINKING_LIBRARIES}) if (CUDA) add_definitions(-DHAVE_CUDA=1) diff --git a/scripts/build-matar.sh b/scripts/build-matar.sh index 0aa79a11..adadf4ad 100755 --- a/scripts/build-matar.sh +++ b/scripts/build-matar.sh @@ -3,7 +3,7 @@ show_help() { echo "Usage: source $(basename "$BASH_SOURCE") [OPTION]" echo "Valid options:" echo " --execution=. Default is 'all'" - echo " --kokkos_build_type=. Default is 'serial'" + echo " --kokkos_build_type=. Default is 'serial'" echo " --build_action=. Default is 'full-app'" echo " --machine=. Default is 'linux'" echo " --build_cores=. Default is set 1" @@ -33,6 +33,10 @@ show_help() { echo " pthreads pthreads Kokkos backend" echo " cuda Cuda Kokkos backend" echo " hip HIP Kokkos backend" + echo " serial_mpi Serial Kokkos backendi plus MPI" + echo " openmp_mpi OpenMP Kokkos backendi plus MPI" + echo " cuda_mpi Cuda Kokkos backendi plus MPI" + echo " hip_mpi HIP Kokkos backendi plus MPI" echo " " echo " --machine The machine you are building for. The default is 'linux'" echo " " @@ -54,7 +58,7 @@ build_cores="1" # Define arrays of valid options valid_build_action=("full-app" "set-env" "install-matar" "install-kokkos" "matar") valid_execution=("examples" "test" "benchmark") -valid_kokkos_build_types=("none" "serial" "openmp" "pthreads" "cuda" "hip") +valid_kokkos_build_types=("none" "serial" "openmp" "pthreads" "cuda" "hip" "serial_mpi" "openmp_mpi" "cuda_mpi" "hip_mpi") valid_machines=("darwin" "chicoma" "linux" "mac") # Parse command line arguments @@ -154,6 +158,11 @@ source setup-env.sh ${machine} ${kokkos_build_type} ${build_cores} # Next, do action based on args if [ "$build_action" = "full-app" ]; then + rm -rf ${builddir} + if [ ! -d "${builddir}" ] + then + mkdir -p ${builddir} + fi source kokkos-install.sh ${kokkos_build_type} source matar-install.sh ${kokkos_build_type} source cmake_build_${execution}.sh ${kokkos_build_type} @@ -162,6 +171,8 @@ elif [ "$build_action" = "install-kokkos" ]; then elif [ "$build_action" = "install-matar" ]; then source matar-install.sh ${kokkos_build_type} elif [ "$build_action" = "matar" ]; then + # Clean build directory (assumes there is a stale build) + make -C "${EXAMPLE_BUILD_DIR}" distclean source cmake_build_${execution}.sh ${kokkos_build_type} else echo "No build action, only setup the environment." diff --git a/scripts/cmake_build_examples.sh b/scripts/cmake_build_examples.sh index 101d3f6c..fa8a4586 100644 --- a/scripts/cmake_build_examples.sh +++ b/scripts/cmake_build_examples.sh @@ -23,6 +23,12 @@ else ) fi +if [[ "$kokkos_build_type" = *"mpi"* ]]; then + cmake_options+=( + -D MPI=ON + ) +fi + # Print CMake options for reference echo "CMake Options: ${cmake_options[@]}" diff --git a/scripts/kokkos-install.sh b/scripts/kokkos-install.sh index d5b6e36d..990888af 100644 --- a/scripts/kokkos-install.sh +++ b/scripts/kokkos-install.sh @@ -59,6 +59,10 @@ if [ "$kokkos_build_type" = "openmp" ]; then cmake_options+=( ${OPENMP_ADDITIONS[@]} ) +elif [ "$kokkos_build_type" = "openmp_mpi" ]; then + cmake_options+=( + ${OPENMP_ADDITIONS[@]} + ) elif [ "$kokkos_build_type" = "pthreads" ]; then cmake_options+=( ${PTHREADS_ADDITIONS[@]} @@ -67,10 +71,18 @@ elif [ "$kokkos_build_type" = "cuda" ]; then cmake_options+=( ${CUDA_ADDITIONS[@]} ) +elif [ "$kokkos_build_type" = "cuda_mpi" ]; then + cmake_options+=( + ${CUDA_ADDITIONS[@]} + ) elif [ "$kokkos_build_type" = "hip" ]; then cmake_options+=( ${HIP_ADDITIONS[@]} ) +elif [ "$kokkos_build_type" = "hip_mpi" ]; then + cmake_options+=( + ${HIP_ADDITIONS[@]} + ) fi # Print CMake options for reference diff --git a/scripts/machines/darwin-env.sh b/scripts/machines/darwin-env.sh index 5c0cf8f7..e020e86e 100644 --- a/scripts/machines/darwin-env.sh +++ b/scripts/machines/darwin-env.sh @@ -13,11 +13,13 @@ myrocm="rocm" mympi="openmpi/3.1.6-gcc_9.4.0" module purge -module load ${mympi} -if [ "$kokkos_build_type" = "cuda" ]; then +if [[ "$kokkos_build_type" = *"mpi"* ]]; then + module load ${mympi} +fi +if [[ "$kokkos_build_type" = *"cuda"* ]]; then module load ${mygcc} module load ${mycuda} -elif [ "$kokkos_build_type" = "hip" ]; then +elif [[ "$kokkos_build_type" = *"hip"* ]]; then module load ${mygcc} module load ${myrocm} else diff --git a/scripts/matar-install.sh b/scripts/matar-install.sh index 6ec12073..ad2b0bf6 100644 --- a/scripts/matar-install.sh +++ b/scripts/matar-install.sh @@ -20,6 +20,13 @@ else ) fi +if [[ "$kokkos_build_type" = *"mpi"* ]]; then + cmake_options+=( + -D MPI=ON + ) +fi + + # Print CMake options for reference echo "CMake Options: ${cmake_options[@]}" diff --git a/scripts/setup-env.sh b/scripts/setup-env.sh index 368e0388..fa97861a 100644 --- a/scripts/setup-env.sh +++ b/scripts/setup-env.sh @@ -38,8 +38,6 @@ export MATAR_INSTALL_DIR=${installdir}/matar export MATAR_BUILD_CORES=$build_cores # Clean stale build and install(s) -rm -rf ${builddir} -mkdir -p ${builddir} cd $scriptdir diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt deleted file mode 100644 index 61a96552..00000000 --- a/src/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -cmake_minimum_required(VERSION 3.12) -set (CMAKE_CXX_STANDARD 17) - diff --git a/src/aliases.h b/src/aliases.h deleted file mode 100644 index 3bdca874..00000000 --- a/src/aliases.h +++ /dev/null @@ -1,178 +0,0 @@ -#ifndef ALIASES_H -#define ALIASES_H -/********************************************************************************************** - © 2020. Triad National Security, LLC. All rights reserved. - This program was produced under U.S. Government contract 89233218CNA000001 for Los Alamos - National Laboratory (LANL), which is operated by Triad National Security, LLC for the U.S. - Department of Energy/National Nuclear Security Administration. All rights in the program are - reserved by Triad National Security, LLC, and the U.S. Department of Energy/National Nuclear - Security Administration. The Government is granted for itself and others acting on its behalf a - nonexclusive, paid-up, irrevocable worldwide license in this material to reproduce, prepare - derivative works, distribute copies to the public, perform publicly and display publicly, and - to permit others to do so. - This program is open source under the BSD-3 License. - Redistribution and use in source and binary forms, with or without modification, are permitted - provided that the following conditions are met: - - 1. Redistributions of source code must retain the above copyright notice, this list of - conditions and the following disclaimer. - - 2. Redistributions in binary form must reproduce the above copyright notice, this list of - conditions and the following disclaimer in the documentation and/or other materials - provided with the distribution. - - 3. Neither the name of the copyright holder nor the names of its contributors may be used - to endorse or promote products derived from this software without specific prior - written permission. - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS - IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - **********************************************************************************************/ - -#include "host_types.h" -#include "kokkos_types.h" - - -using real_t = double; -using u_int = unsigned int; - - -// simple data type names -namespace mtr -{ - // dense host (only cpu) types - template - using CArrayHost = CArray ; - - template - using CMatrixHost = CMatrix ; - - template - using FArrayHost = FArray ; - - template - using FMatrixHost = FMatrix ; - - template - using ViewCArrayHost = ViewCArray ; - - template - using ViewCMatrixHost = ViewCMatrix ; - - template - using ViewFArrayHost = ViewFArray ; - - template - using ViewFMatrixHost = ViewFMatrix ; - - - // ragged and sparse host (only cpu) types - template - using RaggedCArrayHost = RaggedRightArray ; - - template - using RaggedFArrayHost = RaggedDownArray ; - - template - using DynamicRaggedCArrayHost = DynamicRaggedRightArray ; - - template - using DynamicRaggedFArrayHost = DynamicRaggedDownArray ; - - template - using CSRArrayHost = CSRArray ; - - template - using CSCArrayHost = CSCArray ; - - -} // end namespace - - -#ifdef HAVE_KOKKOS -namespace mtr -{ - // dense device types - template - using CArrayDevice = CArrayKokkos ; - - template - using CMatrixDevice = CMatrixKokkos ; - - template - using FArrayDevice = FArrayKokkos ; - - template - using FMatrixDevice = FMatrixKokkos ; - - template - using ViewCArrayDevice = ViewCArrayKokkos ; - - template - using ViewCMatrixDevice = ViewCMatrixKokkos ; - - template - using ViewFArrayDevice = ViewFArrayKokkos ; - - template - using ViewFMatrixDevice = ViewFMatrixKokkos ; - - - // ragged and sparse device types - template - using RaggedCArrayDevice = RaggedRightArrayKokkos ; - - template - using RaggedFArrayDevice = RaggedDownArrayKokkos ; - - template - using DynamicRaggedCArrayDevice = DynamicRaggedRightArrayKokkos ; - - template - using DynamicRaggedFArrayDevice = DynamicRaggedDownArrayKokkos ; - - template - using CSRArrayDevice = CSRArrayKokkos ; - - template - using CSCArrayDevice = CSCArrayKokkos ; - - - // dual dense types - template - using CArrayDual = DCArrayKokkos ; - - template - using CMatrixDual = DCMatrixKokkos ; - - template - using FArrayDual = DFArrayKokkos ; - - template - using FMatrixDual = DFMatrixKokkos ; - - template - using ViewCArrayDual = DViewCArrayKokkos ; - - template - using ViewCMatrixDual = DViewCMatrixKokkos ; - - template - using ViewFArrayDual = DViewFArrayKokkos ; - - template - using ViewFMatrixDual = DViewFMatrixKokkos ; - - -} // end namespace -#endif // end if have Kokkos for simple data type names - -#endif // ALIASES_H diff --git a/src/host_types.h b/src/host_types.h deleted file mode 100644 index fb8f4be1..00000000 --- a/src/host_types.h +++ /dev/null @@ -1,5219 +0,0 @@ -#ifndef HOST_TYPES_H -#define HOST_TYPES_H -/********************************************************************************************** - © 2020. Triad National Security, LLC. All rights reserved. - This program was produced under U.S. Government contract 89233218CNA000001 for Los Alamos - National Laboratory (LANL), which is operated by Triad National Security, LLC for the U.S. - Department of Energy/National Nuclear Security Administration. All rights in the program are - reserved by Triad National Security, LLC, and the U.S. Department of Energy/National Nuclear - Security Administration. The Government is granted for itself and others acting on its behalf a - nonexclusive, paid-up, irrevocable worldwide license in this material to reproduce, prepare - derivative works, distribute copies to the public, perform publicly and display publicly, and - to permit others to do so. - This program is open source under the BSD-3 License. - Redistribution and use in source and binary forms, with or without modification, are permitted - provided that the following conditions are met: - - 1. Redistributions of source code must retain the above copyright notice, this list of - conditions and the following disclaimer. - - 2. Redistributions in binary form must reproduce the above copyright notice, this list of - conditions and the following disclaimer in the documentation and/or other materials - provided with the distribution. - - 3. Neither the name of the copyright holder nor the names of its contributors may be used - to endorse or promote products derived from this software without specific prior - written permission. - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS - IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - **********************************************************************************************/ -#include -#include -#include -#include -#include // for shared_ptr - - -namespace mtr -{ - - -//---Begin Standard Data Structures--- - -//1. FArray -// indicies are [0:N-1] -template -class FArray { - -private: - size_t dims_[7]; - size_t length_; - size_t order_; // tensor order (rank) - std::shared_ptr array_; - -public: - - // default constructor - FArray (); - - //overload constructors from 1D to 7D - - FArray(size_t dim0); - - FArray(size_t dim0, - size_t dim1); - - FArray(size_t dim0, - size_t dim1, - size_t dim2); - - FArray(size_t dim0, - size_t dim1, - size_t dim2, - size_t dim3); - - FArray(size_t dim0, - size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4); - - FArray(size_t dim0, - size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4, - size_t dim5); - - FArray(size_t dim0, - size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4, - size_t dim5, - size_t dim6); - - FArray (const FArray& temp); - - // overload operator() to access data as array(i,....,n); - T& operator()(size_t i) const; - - T& operator()(size_t i, - size_t j) const; - - T& operator()(size_t i, - size_t j, - size_t k) const; - - T& operator()(size_t i, - size_t j, - size_t k, - size_t l) const; - - T& operator()(size_t i, - size_t j, - size_t k, - size_t l, - size_t m) const; - - T& operator()(size_t i, - size_t j, - size_t k, - size_t l, - size_t m, - size_t n) const; - T& operator()(size_t i, - size_t j, - size_t k, - size_t l, - size_t m, - size_t n, - size_t o) const; - - //overload = operator - FArray& operator=(const FArray& temp); - - //return array size - size_t size() const; - - // return array dims - size_t dims(size_t i) const; - - // return array order (rank) - size_t order() const; - - //return pointer - T* pointer() const; - - // deconstructor - ~FArray (); - -}; // end of f_array_t - -//---FArray class definnitions---- - -//constructors -template -FArray::FArray(){ - array_ = NULL; - length_ = order_ = 0; - for (int i = 0; i < 7; i++) { - dims_[i] = 0; - } -} - -//1D -template -FArray::FArray(size_t dim0) -{ - dims_[0] = dim0; - length_ = dim0; - order_ = 1; - array_ = std::shared_ptr (new T[length_]); -} - -template -FArray::FArray(size_t dim0, - size_t dim1) -{ - dims_[0] = dim0; - dims_[1] = dim1; - order_ = 2; - length_ = dim0*dim1; - array_ = std::shared_ptr (new T[length_]); -} - -//3D -template -FArray::FArray(size_t dim0, - size_t dim1, - size_t dim2) -{ - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - order_ = 3; - length_ = dim0*dim1*dim2; - array_ = std::shared_ptr (new T[length_]); -} - -//4D -template -FArray::FArray(size_t dim0, - size_t dim1, - size_t dim2, - size_t dim3) -{ - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - order_ = 4; - length_ = dim0*dim1*dim2*dim3; - array_ = std::shared_ptr (new T[length_]); -} - -//5D -template -FArray::FArray(size_t dim0, - size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4) -{ - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - dims_[4] = dim4; - order_ = 5; - length_ = dim0*dim1*dim2*dim3*dim4; - array_ = std::shared_ptr (new T[length_]); -} - -//6D -template -FArray::FArray(size_t dim0, - size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4, - size_t dim5) -{ - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - dims_[4] = dim4; - dims_[5] = dim5; - order_ = 6; - length_ = dim0*dim1*dim2*dim3*dim4*dim5; - array_ = std::shared_ptr (new T[length_]); -} - - -//7D -template -FArray::FArray(size_t dim0, - size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4, - size_t dim5, - size_t dim6) -{ - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - dims_[4] = dim4; - dims_[5] = dim5; - dims_[6] = dim6; - order_ = 7; - length_ = dim0*dim1*dim2*dim3*dim4*dim5*dim6; - array_ = std::shared_ptr (new T[length_]); - -} - -//Copy constructor - -template -FArray::FArray(const FArray& temp) { - - // Do nothing if the assignment is of the form x = x - - if (this != &temp) { - for (int iter = 0; iter < temp.order_; iter++){ - dims_[iter] = temp.dims_[iter]; - } // end for - - order_ = temp.order_; - length_ = temp.length_; - array_ = temp.array_; - } // end if - -} // end constructor - -//overload operator () for 1D to 7D -//indices are from [0:N-1] - -//1D -template -T& FArray::operator()(size_t i) const -{ - assert(order_ == 1 && "Tensor order (rank) does not match constructor in FArray 1D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in FArray 1D!"); - return array_[i]; -} - -//2D -template -T& FArray::operator()(size_t i, - size_t j) const -{ - assert(order_ == 2 && "Tensor order (rank) does not match constructor in FArray 2D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in FArray 2D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in FArray 2D!"); - return array_[i + j*dims_[0]]; -} - -//3D -template -T& FArray::operator()(size_t i, - size_t j, - size_t k) const -{ - assert(order_ == 3 && "Tensor order (rank) does not match constructor in FArray 3D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in FArray 3D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in Farray 3D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in FArray 3D!"); - return array_[i + j*dims_[0] - + k*dims_[0]*dims_[1]]; -} - -//4D -template -T& FArray::operator()(size_t i, - size_t j, - size_t k, - size_t l) const -{ - assert(order_ == 4 && "Tensor order (rank) does not match constructor in FArray 4D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in FArray 4D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in FArray 4D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in FArray 4D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in FArray 4D!"); - return array_[i + j*dims_[0] - + k*dims_[0]*dims_[1] - + l*dims_[0]*dims_[1]*dims_[2]]; -} - -//5D -template -T& FArray::operator()(size_t i, - size_t j, - size_t k, - size_t l, - size_t m) const -{ - assert(order_ == 5 && "Tensor order (rank) does not match constructor in FArray 5D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in FArray 5D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in FArray 5D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in FArray 5D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in FArray 5D!"); - assert(m >= 0 && m < dims_[4] && "m is out of bounds in FArray 5D!"); - return array_[i + j*dims_[0] - + k*dims_[0]*dims_[1] - + l*dims_[0]*dims_[1]*dims_[2] - + m*dims_[0]*dims_[1]*dims_[2]*dims_[3]]; -} - -//6D -template -T& FArray::operator()(size_t i, - size_t j, - size_t k, - size_t l, - size_t m, - size_t n) const -{ - assert(order_ == 6 && "Tensor order (rank) does not match constructor in FArray 6D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in FArray 6D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in FArray 6D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in FArray 6D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in FArray 6D!"); - assert(m >= 0 && m < dims_[4] && "m is out of bounds in FArray 6D!"); - assert(n >= 0 && n < dims_[5] && "n is out of bounds in FArray 6D!"); - return array_[i + j*dims_[0] - + k*dims_[0]*dims_[1] - + l*dims_[0]*dims_[1]*dims_[2] - + m*dims_[0]*dims_[1]*dims_[2]*dims_[3] - + n*dims_[0]*dims_[1]*dims_[2]*dims_[3]*dims_[4]]; -} - -//7D -template -T& FArray::operator()(size_t i, - size_t j, - size_t k, - size_t l, - size_t m, - size_t n, - size_t o) const -{ - assert(order_ == 7 && "Tensor order (rank) does not match constructor in FArray 7D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in FArray 7D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in FArray 7D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in FArray 7D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in FArray 7D!"); - assert(m >= 0 && m < dims_[4] && "m is out of bounds in FArray 7D!"); - assert(n >= 0 && n < dims_[5] && "n is out of bounds in FArray 7D!"); - assert(o >= 0 && o < dims_[6] && "o is out of bounds in FArray 7D!"); - return array_[i + j*dims_[0] - + k*dims_[0]*dims_[1] - + l*dims_[0]*dims_[1]*dims_[2] - + m*dims_[0]*dims_[1]*dims_[2]*dims_[3] - + n*dims_[0]*dims_[1]*dims_[2]*dims_[3]*dims_[4] - + o*dims_[0]*dims_[1]*dims_[2]*dims_[3]*dims_[4]*dims_[5]]; -} - -// = operator -//THIS = FArray <> TEMP(n,m,...) -template -FArray& FArray::operator= (const FArray& temp) -{ - if(this != & temp) { - for (int iter = 0; iter < temp.order_; iter++){ - dims_[iter] = temp.dims_[iter]; - } // end for - - order_ = temp.order_; - length_ = temp.length_; - array_ = temp.array_; - } - return *this; -} - -template -inline size_t FArray::size() const { - return length_; -} - -template -inline size_t FArray::dims(size_t i) const { - assert(i < order_ && "FArray order (rank) does not match constructor, dim[i] does not exist!"); - assert(i >= 0 && dims_[i]>0 && "Access to FArray dims is out of bounds!"); - return dims_[i]; -} - -template -inline size_t FArray::order() const { - return order_; -} - - -template -inline T* FArray::pointer() const { - return array_.get(); -} - -//delete FArray -template -FArray::~FArray(){} - -//---end of FArray class definitions---- - - -//2. ViewFArray -// indicies are [0:N-1] -template -class ViewFArray { - -private: - size_t dims_[7]; - size_t length_; // Length of 1D array - size_t order_; // tensor order (rank) - T * array_; - -public: - - // default constructor - ViewFArray (); - - //---1D to 7D array --- - ViewFArray(T *array, - size_t dim0); - - ViewFArray (T *array, - size_t dim0, - size_t dim1); - - ViewFArray (T *array, - size_t dim0, - size_t dim1, - size_t dim2); - - ViewFArray (T *array, - size_t dim0, - size_t dim1, - size_t dim2, - size_t dim3); - - ViewFArray (T *array, - size_t dim0, - size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4); - - ViewFArray (T *array, - size_t dim0, - size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4, - size_t dim5); - - ViewFArray (T *array, - size_t dim0, - size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4, - size_t dim5, - size_t dim6); - - T& operator()(size_t i) const; - - T& operator()(size_t i, - size_t j) const; - - T& operator()(size_t i, - size_t j, - size_t k) const; - - T& operator()(size_t i, - size_t j, - size_t k, - size_t l) const; - - T& operator()(size_t i, - size_t j, - size_t k, - size_t l, - size_t m) const; - - T& operator()(size_t i, - size_t j, - size_t k, - size_t l, - size_t m, - size_t n) const; - - T& operator()(size_t i, - size_t j, - size_t k, - size_t l, - size_t m, - size_t n, - size_t o) const; - - // calculate C = math(A,B) - template - void operator=(M do_this_math); - - //return array size - size_t size() const; - - //return array dims - size_t dims(size_t i) const; - - // return array order (rank) - size_t order() const; - - // return pointer - T* pointer() const; - -}; // end of viewFArray - -//class definitions for viewFArray - -//~~~~constructors for viewFArray for 1D to 7D~~~~~~~ - -//no dimension -template -ViewFArray::ViewFArray(){ - array_ = NULL; - length_ = order_ = 0; - for (int i = 0; i < 7; i++) { - dims_[i] = 0; - } -} - -//1D -template -ViewFArray::ViewFArray(T *array, - size_t dim0) -{ - dims_[0] = dim0; - order_ = 1; - length_ = dim0; - array_ = array; -} - -//2D -template -ViewFArray::ViewFArray(T *array, - size_t dim0, - size_t dim1) -{ - dims_[0] = dim0; - dims_[1] = dim1; - order_ = 2; - length_ = dim0*dim1; - array_ = array; -} - -//3D -template -ViewFArray::ViewFArray(T *array, - size_t dim0, - size_t dim1, - size_t dim2) -{ - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - order_ = 3; - length_ = dim0*dim1*dim2; - array_ = array; -} - -//4D -template -ViewFArray::ViewFArray(T *array, - size_t dim0, - size_t dim1, - size_t dim2, - size_t dim3) -{ - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - order_ = 4; - length_ = dim0*dim1*dim2*dim3; - array_ = array; -} - -//5D -template -ViewFArray::ViewFArray(T *array, - size_t dim0, - size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4) -{ - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - dims_[4] = dim4; - order_ = 5; - length_ = dim0*dim1*dim2*dim3*dim4; - array_ = array; -} - -//6D -template -ViewFArray::ViewFArray(T *array, - size_t dim0, - size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4, - size_t dim5) -{ - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - dims_[4] = dim4; - dims_[5] = dim5; - order_ = 6; - length_ = dim0*dim1*dim2*dim3*dim4*dim5; - array_ = array; -} - -//7D -template -ViewFArray::ViewFArray(T *array, - size_t dim0, - size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4, - size_t dim5, - size_t dim6) -{ - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - dims_[4] = dim4; - dims_[5] = dim5; - dims_[6] = dim6; - order_ = 7; - length_ = dim0*dim1*dim2*dim3*dim4*dim5*dim6; - array_ = array; -} - -//~~~~~~operator () overload -//for dimensions 1D to 7D -//indices for array are from 0...N-1 - -//1D -template -T& ViewFArray::operator()(size_t i) const -{ - assert(order_ == 1 && "Tensor order (rank) does not match constructor in ViewFArray 1D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in ViewFArray 1D!"); - return array_[i]; -} - -//2D -template -T& ViewFArray::operator()(size_t i, - size_t j) const -{ - assert(order_ == 2 && "Tensor order (rank) does not match constructor in ViewFArray 2D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in ViewFArray 2D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in ViewFArray 2D!"); - return array_[i + j*dims_[0]]; -} - -//3D -template -T& ViewFArray::operator()(size_t i, - size_t j, - size_t k) const -{ - assert(order_ == 3 && "Tensor order (rank) does not match constructor in ViewFArray 3D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in ViewFArray 3D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in ViewFArray 3D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in ViewFArray 3D!"); - return array_[i + j*dims_[0] - + k*dims_[0]*dims_[1]]; -} - -//4D -template -T& ViewFArray::operator()(size_t i, - size_t j, - size_t k, - size_t l) const -{ - assert(order_ == 4 && "Tensor order (rank) does not match constructor in ViewFArray 4D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in ViewFArray 4D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in ViewFArray 4D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in ViewFArray 4D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in ViewFArray 4D!"); - return array_[i + j*dims_[0] - + k*dims_[0]*dims_[1] - + l*dims_[0]*dims_[1]*dims_[2]]; -} - -//5D -template -T& ViewFArray::operator()(size_t i, - size_t j, - size_t k, - size_t l, - size_t m) const -{ - assert(order_ == 5 && "Tensor order (rank) does not match constructor in ViewFArray 5D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in ViewFArray 5D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in ViewFArray 5D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in ViewFArray 5D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in ViewFArray 5D!"); - assert(m >= 0 && m < dims_[4] && "m is out of bounds in ViewFArray 5D!"); - return array_[i + j*dims_[0] - + k*dims_[0]*dims_[1] - + l*dims_[0]*dims_[1]*dims_[2] - + m*dims_[0]*dims_[1]*dims_[2]*dims_[3]]; -} - -//6D -template -T& ViewFArray:: operator()(size_t i, - size_t j, - size_t k, - size_t l, - size_t m, - size_t n) const -{ - assert(order_ == 6 && "Tensor order (rank) does not match constructor in ViewFArray 6D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in ViewFArray 6D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in ViewFArray 6D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in ViewFArray 6D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in ViewFArray 6D!"); - assert(m >= 0 && m < dims_[4] && "m is out of bounds in ViewFArray 6D!"); - assert(n >= 0 && n < dims_[5] && "n is out of bounds in ViewFArray 6D!"); - return array_[i + j*dims_[0] - + k*dims_[0]*dims_[1] - + l*dims_[0]*dims_[1]*dims_[2] - + m*dims_[0]*dims_[1]*dims_[2]*dims_[3] - + n*dims_[0]*dims_[1]*dims_[2]*dims_[3]*dims_[4]]; -} - -//7D -template -T& ViewFArray:: operator()(size_t i, - size_t j, - size_t k, - size_t l, - size_t m, - size_t n, - size_t o) const -{ - assert(order_ == 7 && "Tensor order (rank) does not match constructor in ViewFArray 7D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in ViewFArray 7D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in ViewFArray 7D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in ViewFArray 7D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in ViewFArray 7D!"); - assert(m >= 0 && m < dims_[4] && "m is out of bounds in ViewFArray 7D!"); - assert(n >= 0 && n < dims_[5] && "n is out of bounds in ViewFArray 7D!"); - assert(o >= 0 && o < dims_[6] && "n is out of bounds in ViewFArray 7D!"); - return array_[i + j*dims_[0] - + k*dims_[0]*dims_[1] - + l*dims_[0]*dims_[1]*dims_[2] - + m*dims_[0]*dims_[1]*dims_[2]*dims_[3] - + n*dims_[0]*dims_[1]*dims_[2]*dims_[3]*dims_[4] - + o*dims_[0]*dims_[1]*dims_[2]*dims_[3]*dims_[4]*dims_[5]]; -} - -// calculate this ViewFArray object = math(A,B) -template -template -void ViewFArray::operator=(M do_this_math){ - do_this_math(*this); // pass in this ViewFArray object -}// end of math opperation - -template -inline size_t ViewFArray::dims(size_t i) const { - assert(i < order_ && "ViewFArray order (rank) does not match constructor, dim[i] does not exist!"); - assert(i >= 0 && dims_[i]>0 && "Access to ViewFArray dims is out of bounds!"); - return dims_[i]; -} - -template -inline size_t ViewFArray::order() const { - return order_; -} - -template -inline size_t ViewFArray::size() const { - return length_; -} - -template -inline T* ViewFArray::pointer() const { - return array_; -} - -//---end of ViewFArray class definitions--- - - -//3. FMatrix -// indicies are [1:N] -template -class FMatrix { -private: - size_t dims_[7]; - size_t length_; // Length of 1D array - size_t order_; // tensor order (rank) - std::shared_ptr matrix_; - -public: - // Default constructor - FMatrix (); - - //---1D to 7D matrix --- - FMatrix (size_t dim1); - - FMatrix (size_t dim1, - size_t dim2); - - FMatrix (size_t dim1, - size_t dim2, - size_t dim3); - - FMatrix (size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4); - - FMatrix (size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4, - size_t dim5); - - FMatrix (size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4, - size_t dim5, - size_t dim6); - - FMatrix (size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4, - size_t dim5, - size_t dim6, - size_t dim7); - - FMatrix (const FMatrix& temp); - - T& operator() (size_t i) const; - - T& operator() (size_t i, - size_t j) const; - - T& operator() (size_t i, - size_t j, - size_t k) const; - - T& operator() (size_t i, - size_t j, - size_t k, - size_t l) const; - - T& operator() (size_t i, - size_t j, - size_t k, - size_t l, - size_t m) const; - - T& operator() (size_t i, - size_t j, - size_t k, - size_t l, - size_t m, - size_t n) const; - - T& operator() (size_t i, - size_t j, - size_t k, - size_t l, - size_t m, - size_t n, - size_t o) const; - - - // Overload copy assignment operator - FMatrix& operator=(const FMatrix& temp); - - // the length of the 1D storage array - size_t size() const; - - // matrix dims - size_t dims(size_t i) const; - - // return matrix order (rank) - size_t order() const; - - //return pointer - T* pointer() const; - - // Deconstructor - ~FMatrix (); - -}; // End of FMatrix - -//---FMatrix class definitions--- - -//constructors -template -FMatrix::FMatrix(){ - matrix_ = NULL; - length_ = order_ = 0; - for (int i = 0; i < 7; i++) { - dims_[i] = 0; - } -} - -//1D -template -FMatrix::FMatrix(size_t dim1) -{ - dims_[0] = dim1; - order_ = 1; - length_ = dim1; - matrix_ = std::shared_ptr (new T[length_]); -} - -//2D -template -FMatrix::FMatrix(size_t dim1, - size_t dim2) -{ - dims_[0] = dim1; - dims_[1] = dim2; - order_ = 2; - length_ = dim1 * dim2; - matrix_ = std::shared_ptr (new T[length_]); -} - -//3D -template -FMatrix::FMatrix(size_t dim1, - size_t dim2, - size_t dim3) -{ - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - order_ = 3; - length_ = dim1 * dim2 * dim3; - matrix_ = std::shared_ptr (new T[length_]); -} - -//4D -template -FMatrix::FMatrix(size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4) -{ - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - order_ = 4; - length_ = dim1 * dim2 * dim3 * dim4; - matrix_ = std::shared_ptr (new T[length_]); -} - -//5D -template -FMatrix::FMatrix(size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4, - size_t dim5) -{ - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - dims_[4] = dim5; - order_ = 5; - length_ = dim1 * dim2 * dim3 * dim4 * dim5; - matrix_ = std::shared_ptr (new T[length_]); -} - -//6D -template -FMatrix::FMatrix(size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4, - size_t dim5, - size_t dim6) -{ - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - dims_[4] = dim5; - dims_[5] = dim6; - order_ = 6; - length_ = dim1 * dim2 * dim3 * dim4 * dim5 * dim6; - matrix_ = std::shared_ptr (new T[length_]); - -} - -template -FMatrix::FMatrix(size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4, - size_t dim5, - size_t dim6, - size_t dim7) -{ - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - dims_[4] = dim5; - dims_[5] = dim6; - dims_[6] = dim7; - order_ = 7; - length_ = dim1 * dim2 * dim3 * dim4 * dim5 * dim6 * dim7; - matrix_ = std::shared_ptr (new T[length_]); - -} - -template -FMatrix::FMatrix(const FMatrix& temp) { - - // Do nothing if the assignment is of the form x = x - - if (this != &temp) { - for (int iter = 0; iter < temp.order_; iter++){ - dims_[iter] = temp.dims_[iter]; - } // end for - - order_ = temp.order_; - length_ = temp.length_; - matrix_ = temp.matrix_; - } // end if - -} // end constructor - - -//overload operators - -//1D -template -inline T& FMatrix::operator() (size_t i) const -{ - assert(order_ == 1 && "Tensor order (rank) does not match constructor in FMatrix 1D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in FMatrix 1D!"); - return matrix_[i - 1]; -} - -//2D -template -inline T& FMatrix::operator() (size_t i, - size_t j) const -{ - assert(order_ == 2 && "Tensor order (rank) does not match constructor in FMatrix 2D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in FMatrix 2D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in FMatrix 2D!"); - return matrix_[(i - 1) + ((j - 1) * dims_[0])]; -} - -//3D -template -inline T& FMatrix::operator() (size_t i, - size_t j, - size_t k) const -{ - assert(order_ == 3 && "Tensor order (rank) does not match constructor in FMatrix 3D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in FMatrix 3D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in FMatrix 3D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in FMatrix 3D!"); - return matrix_[(i - 1) + ((j - 1) * dims_[0]) - + ((k - 1) * dims_[0] * dims_[1])]; -} - -//4D -template -inline T& FMatrix::operator() (size_t i, - size_t j, - size_t k, - size_t l) const -{ - assert(order_ == 4 && "Tensor order (rank) does not match constructor in FMatrix 4D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in FMatrix 4D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in FMatrix 4D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in FMatrix 4D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in FMatrix 4D!"); - return matrix_[(i - 1) + ((j - 1) * dims_[0]) - + ((k - 1) * dims_[0] * dims_[1]) - + ((l - 1) * dims_[0] * dims_[1] * dims_[2])]; -} - -//5D -template -inline T& FMatrix::operator() (size_t i, - size_t j, - size_t k, - size_t l, - size_t m) const -{ - assert(order_ == 5 && "Tensor order (rank) does not match constructor in FMatrix 5D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in FMatrix 5D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in FMatrix 5D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in FMatrix 5D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in FMatrix 5D!"); - assert(m >= 1 && m <= dims_[4] && "m is out of bounds in FMatrix 5D!"); - return matrix_[(i - 1) + ((j - 1) * dims_[0]) - + ((k - 1) * dims_[0] * dims_[1]) - + ((l - 1) * dims_[0] * dims_[1] * dims_[2]) - + ((m - 1) * dims_[0] * dims_[1] * dims_[2] * dims_[3])]; -} - -//6D -template -inline T& FMatrix::operator() (size_t i, - size_t j, - size_t k, - size_t l, - size_t m, - size_t n) const -{ - assert(order_ == 6 && "Tensor order (rank) does not match constructor in FMatrix 6D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in FMatrix 6D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in FMatrix 6D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in FMatrix 6D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in FMatrix 6D!"); - assert(m >= 1 && m <= dims_[4] && "m is out of bounds in FMatrix 6D!"); - assert(n >= 1 && n <= dims_[5] && "n is out of bounds in FMatrix 6D!"); - return matrix_[(i - 1) + ((j - 1) * dims_[0]) - + ((k - 1) * dims_[0] * dims_[1]) - + ((l - 1) * dims_[0] * dims_[1] * dims_[2]) - + ((m - 1) * dims_[0] * dims_[1] * dims_[2] * dims_[3]) - + ((n - 1) * dims_[0] * dims_[1] * dims_[2] * dims_[3] * dims_[4])]; -} - -//7D -template -inline T& FMatrix::operator() (size_t i, - size_t j, - size_t k, - size_t l, - size_t m, - size_t n, - size_t o) const -{ - assert(order_ == 7 && "Tensor order (rank) does not match constructor in FMatrix 7D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in FMatrix 7D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in FMatrix 7D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in FMatrix 7D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in FMatrix 7D!"); - assert(m >= 1 && m <= dims_[4] && "m is out of bounds in FMatrix 7D!"); - assert(n >= 1 && n <= dims_[5] && "n is out of bounds in FMatrix 7D!"); - assert(o >= 1 && o <= dims_[6] && "o is out of bounds in FMatrix 7D!"); - return matrix_[(i - 1) + ((j - 1) * dims_[0]) - + ((k - 1) * dims_[0] * dims_[1]) - + ((l - 1) * dims_[0] * dims_[1] * dims_[2]) - + ((m - 1) * dims_[0] * dims_[1] * dims_[2] * dims_[3]) - + ((n - 1) * dims_[0] * dims_[1] * dims_[2] * dims_[3] * dims_[4]) - + ((o - 1) * dims_[0] * dims_[1] * dims_[2] * dims_[3] * dims_[4] * dims_[5])]; -} - - -template -inline FMatrix& FMatrix::operator= (const FMatrix& temp) -{ - // Do nothing if assignment is of the form x = x - if (this != &temp) { - for (int iter = 0; iter < temp.order_; iter++){ - dims_[iter] = temp.dims_[iter]; - } // end for - - order_ = temp.order_; - length_ = temp.length_; - matrix_ = temp.matrix_; - } - - return *this; -} - -template -inline size_t FMatrix::size() const { - return length_; -} - -template -inline size_t FMatrix::dims(size_t i) const { - i--; // i starts at 1 - assert(i < order_ && "FMatrix order (rank) does not match constructor, dim[i] does not exist!"); - assert(i >= 0 && dims_[i]>0 && "Access to FMatrix dims is out of bounds!"); - return dims_[i]; -} - -template -inline size_t FMatrix::order() const { - return order_; -} - -template -inline T* FMatrix::pointer() const{ - return matrix_.get(); -} - -template -FMatrix::~FMatrix() {} - -//----end of FMatrix class definitions---- - - -//4. ViewFMatrix -// indices are [1:N] -template -class ViewFMatrix { - -private: - size_t dims_[7]; - size_t length_; // Length of 1D array - size_t order_; // tensor order (rank) - T * matrix_; - -public: - - // Default constructor - ViewFMatrix (); - - //--- 1D to 7D matrix --- - - ViewFMatrix(T *matrix, - size_t dim1); - - ViewFMatrix(T *some_matrix, - size_t dim1, - size_t dim2); - - ViewFMatrix(T *matrix, - size_t dim1, - size_t dim2, - size_t dim3); - - ViewFMatrix(T *matrix, - size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4); - - ViewFMatrix (T *matrix, - size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4, - size_t dim5); - - ViewFMatrix (T *matrix, - size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4, - size_t dim5, - size_t dim6); - - ViewFMatrix (T *matrix, - size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4, - size_t dim5, - size_t dim6, - size_t dim7); - - T& operator()(size_t i) const; - - T& operator()(size_t i, - size_t j) const; - - T& operator()(size_t i, - size_t j, - size_t k) const; - - T& operator()(size_t i, - size_t j, - size_t k, - size_t l) const; - - T& operator() (size_t i, - size_t j, - size_t k, - size_t l, - size_t m) const; - - T& operator()(size_t i, - size_t j, - size_t k, - size_t l, - size_t m, - size_t n) const; - - T& operator()(size_t i, - size_t j, - size_t k, - size_t l, - size_t m, - size_t n, - size_t o) const; - - // calculate C = math(A,B) - template - void operator=(M do_this_math); - - // length of 1D array - size_t size() const; - - // matrix dims - size_t dims(size_t i) const; - - // return matrix order (rank) - size_t order() const; - - // return pointer - T* pointer() const; - -}; // end of ViewFMatrix - -//constructors - -//no dimension -template -ViewFMatrix::ViewFMatrix() { - matrix_ = NULL; - length_ = order_ = 0; - for (int i = 0; i < 7; i++) { - dims_[i] = 0; - } -} - -//1D -template -ViewFMatrix::ViewFMatrix(T *matrix, - size_t dim1) -{ - dims_[0] = dim1; - order_ = 1; - length_ = dim1; - matrix_ = matrix; -} - -//2D -template -ViewFMatrix::ViewFMatrix(T *matrix, - size_t dim1, - size_t dim2) -{ - dims_[0] = dim1; - dims_[1] = dim2; - order_ = 2; - length_ = dim1 * dim2; - matrix_ = matrix; -} - -//3D -template -ViewFMatrix::ViewFMatrix (T *matrix, - size_t dim1, - size_t dim2, - size_t dim3) -{ - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - order_ = 3; - length_ = dim1 * dim2 * dim3; - matrix_ = matrix; -} - -//4D -template -ViewFMatrix::ViewFMatrix(T *matrix, - size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4) -{ - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - order_ = 4; - length_ = dim1 * dim2 * dim3 * dim4; - matrix_ = matrix; -} - -//5D -template -ViewFMatrix::ViewFMatrix(T *matrix, - size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4, - size_t dim5) -{ - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - dims_[4] = dim5; - order_ = 5; - length_ = dim1 * dim2 * dim3 * dim4 * dim5; - matrix_ = matrix; -} - -//6D -template -ViewFMatrix::ViewFMatrix(T *matrix, - size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4, - size_t dim5, - size_t dim6) -{ - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - dims_[4] = dim5; - dims_[5] = dim6; - order_ = 6; - length_ = dim1 * dim2 * dim3 * dim4 * dim5 * dim6; - matrix_ = matrix; -} - -//6D -template -ViewFMatrix::ViewFMatrix(T *matrix, - size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4, - size_t dim5, - size_t dim6, - size_t dim7) -{ - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - dims_[4] = dim5; - dims_[5] = dim6; - dims_[6] = dim7; - order_ = 7; - length_ = dim1 * dim2 * dim3 * dim4 * dim5 * dim6 * dim7; - matrix_ = matrix; -} - - -//overload operator () - -//1D -template -inline T& ViewFMatrix::operator()(size_t i) const -{ - assert(order_ == 1 && "Tensor order (rank) does not match constructor in ViewFMatrix 1D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in ViewFMatrix 1D"); // die if >= dim1 - - return matrix_[(i - 1)]; -} - -//2D -template -inline T& ViewFMatrix::operator()(size_t i, - size_t j) const -{ - assert(order_ == 2 && "Tensor order (rank) does not match constructor in ViewFMatrix 2D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in ViewFMatrix 2D"); // die if >= dim1 - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in ViewFMatrix 2D"); // die if >= dim2 - - return matrix_[(i - 1) + ((j - 1) * dims_[0])]; -} - -//3D -template -inline T& ViewFMatrix::operator()(size_t i, - size_t j, - size_t k) const -{ - assert(order_ == 3 && "Tensor order (rank) does not match constructor in ViewFMatrix 3D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in ViewFMatrix 3D"); // die if >= dim1 - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in ViewFMatrix 3D"); // die if >= dim2 - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in ViewFMatrix 3D"); // die if >= dim3 - - return matrix_[(i - 1) + ((j - 1) * dims_[0]) - + ((k - 1) * dims_[0] * dims_[1])]; -} - -//4D -template -inline T& ViewFMatrix::operator()(size_t i, - size_t j, - size_t k, - size_t l) const -{ - assert(order_ == 4 && "Tensor order (rank) does not match constructor in ViewFMatrix 4D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in ViewFMatrix 4D"); // die if >= dim1 - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in ViewFMatrix 4D"); // die if >= dim2 - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in ViewFMatrix 4D"); // die if >= dim3 - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in ViewFMatrix 4D"); // die if >= dim4 - - return matrix_[(i - 1) + ((j - 1) * dims_[0]) - + ((k - 1) * dims_[0] * dims_[1]) - + ((l - 1) * dims_[0] * dims_[1] * dims_[2])]; -} - -//5D -template -inline T& ViewFMatrix::operator()(size_t i, - size_t j, - size_t k, - size_t l, - size_t m) const -{ - assert(order_ == 5 && "Tensor order (rank) does not match constructor in ViewFMatrix 5D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in ViewFMatrix 5D"); // die if >= dim1 - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in ViewFMatrix 5D"); // die if >= dim2 - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in ViewFMatrix 5D"); // die if >= dim3 - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in ViewFMatrix 5D"); // die if >= dim4 - assert(m >= 1 && m <= dims_[4] && "m is out of bounds in ViewFMatrix 5D"); // die if >= dim5 - - return matrix_[(i - 1) + ((j - 1) * dims_[0]) - + ((k - 1) * dims_[0] * dims_[1]) - + ((l - 1) * dims_[0] * dims_[1] * dims_[2]) - + ((m - 1) * dims_[0] * dims_[1] * dims_[2] * dims_[3])]; -} - -//6D -template -inline T& ViewFMatrix::operator()(size_t i, - size_t j, - size_t k, - size_t l, - size_t m, - size_t n) const -{ - assert(order_ == 6 && "Tensor order (rank) does not match constructor in ViewFMatrix 6D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in ViewFMatrix 6D"); // die if >= dim1 - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in ViewFMatrix 6D"); // die if >= dim2 - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in ViewFMatrix 6D"); // die if >= dim3 - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in ViewFMatrix 6D"); // die if >= dim4 - assert(m >= 1 && m <= dims_[4] && "m is out of bounds in ViewFMatrix 6D"); // die if >= dim5 - assert(n >= 1 && n <= dims_[5] && "n is out of bounds in ViewFMatrix 6D"); // die if >= dim6 - return matrix_[(i - 1) + ((j - 1) * dims_[0]) - + ((k - 1) * dims_[0] * dims_[1]) - + ((l - 1) * dims_[0] * dims_[1] * dims_[2]) - + ((m - 1) * dims_[0] * dims_[1] * dims_[2] * dims_[3]) - + ((n - 1) * dims_[0] * dims_[1] * dims_[2] * dims_[3] * dims_[4])]; -} - -//6D -template -inline T& ViewFMatrix::operator()(size_t i, - size_t j, - size_t k, - size_t l, - size_t m, - size_t n, - size_t o) const -{ - assert(order_ == 7 && "Tensor order (rank) does not match constructor in ViewFMatrix 7D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in ViewFMatrix 7D"); // die if >= dim1 - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in ViewFMatrix 7D"); // die if >= dim2 - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in ViewFMatrix 7D"); // die if >= dim3 - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in ViewFMatrix 7D"); // die if >= dim4 - assert(m >= 1 && m <= dims_[4] && "m is out of bounds in ViewFMatrix 7D"); // die if >= dim5 - assert(n >= 1 && n <= dims_[5] && "n is out of bounds in ViewFMatrix 7D"); // die if >= dim6 - assert(o >= 1 && o <= dims_[6] && "o is out of bounds in ViewFMatrix 7D"); // die if >= dim7 - - return matrix_[(i - 1) + ((j - 1) * dims_[0]) - + ((k - 1) * dims_[0] * dims_[1]) - + ((l - 1) * dims_[0] * dims_[1] * dims_[2]) - + ((m - 1) * dims_[0] * dims_[1] * dims_[2] * dims_[3]) - + ((n - 1) * dims_[0] * dims_[1] * dims_[2] * dims_[3] * dims_[4]) - + ((o - 1) * dims_[0] * dims_[1] * dims_[2] * dims_[3] * dims_[4] * dims_[5])]; -} - -// calculate this ViewFMatrix object = math(A,B) -template -template -void ViewFMatrix::operator=(M do_this_math){ - do_this_math(*this); // pass in this ViewFArray object -}// end of math opperation - -template -inline size_t ViewFMatrix::dims(size_t i) const { - i--; // i starts at 1 - assert(i < order_ && "ViewFMatrix order (rank) does not match constructor, dim[i] does not exist!"); - assert(i >= 0 && dims_[i]>0 && "Access to ViewFMatrix dims is out of bounds!"); - return dims_[i]; -} - -template -inline size_t ViewFMatrix::order() const { - return order_; -} - -template -inline T* ViewFMatrix::pointer() const { - return matrix_; -} -//-----end ViewFMatrix----- - - -//5. CArray -// indicies are [0:N-1] -template -class CArray { - -private: - size_t dims_[7]; - size_t length_; // Length of 1D array - size_t order_; // tensor order (rank) - std::shared_ptr array_; - -public: - // Default constructor - CArray (); - - // --- 1D to 7D array --- - - CArray (size_t dim0); - - CArray (size_t dim0, - size_t dim1); - - CArray (size_t dim0, - size_t dim1, - size_t dim2); - - CArray (size_t dim0, - size_t dim1, - size_t dim2, - size_t dim3); - - CArray (size_t dim0, - size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4); - - CArray (size_t dim0, - size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4, - size_t dim5); - - CArray (size_t dim0, - size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4, - size_t dim5, - size_t dim6); - - CArray (const CArray& temp); - - // Overload operator() - T& operator() (size_t i) const; - - T& operator() (size_t i, - size_t j) const; - - T& operator() (size_t i, - size_t j, - size_t k) const; - - T& operator() (size_t i, - size_t j, - size_t k, - size_t l) const; - - T& operator() (size_t i, - size_t j, - size_t k, - size_t l, - size_t m) const; - - T& operator() (size_t i, - size_t j, - size_t k, - size_t l, - size_t m, - size_t n) const; - - T& operator() (size_t i, - size_t j, - size_t k, - size_t l, - size_t m, - size_t n, - size_t o) const; - - // Overload copy assignment operator - CArray& operator= (const CArray& temp); - - //return array size - size_t size() const; - - // return array dims - size_t dims(size_t i) const; - - // return array order (rank) - size_t order() const; - - //return pointer - T* pointer() const; - - // Deconstructor - ~CArray (); - -}; // End of CArray - -//---carray class declarations--- - -//constructors - -//no dim -template -CArray::CArray() { - array_ = NULL; - length_ = order_ = 0; - for (int i = 0; i < 7; i++) { - dims_[i] = 0; - } -} - -//1D -template -CArray::CArray(size_t dim0) -{ - dims_[0] = dim0; - order_ = 1; - length_ = dim0; - array_ = std::shared_ptr (new T[length_]); -} - -//2D -template -CArray::CArray(size_t dim0, - size_t dim1) -{ - dims_[0] = dim0; - dims_[1] = dim1; - order_ = 2; - length_ = dim0 * dim1; - array_ = std::shared_ptr (new T[length_]); -} - -//3D -template -CArray::CArray(size_t dim0, - size_t dim1, - size_t dim2) -{ - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - order_ = 3; - length_ = dim0 * dim1 * dim2; - array_ = std::shared_ptr (new T[length_]); -} - -//4D -template -CArray::CArray(size_t dim0, - size_t dim1, - size_t dim2, - size_t dim3) -{ - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - order_ = 4; - length_ = dim0 * dim1 * dim2 * dim3; - array_ = std::shared_ptr (new T[length_]); -} - -//5D -template -CArray::CArray(size_t dim0, - size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4) { - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - dims_[4] = dim4; - order_ = 5; - length_ = dim0 * dim1 * dim2 * dim3 * dim4; - array_ = std::shared_ptr (new T[length_]); -} - -//6D -template -CArray::CArray(size_t dim0, - size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4, - size_t dim5) { - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - dims_[4] = dim4; - dims_[5] = dim5; - order_ = 6; - length_ = dim0 * dim1 * dim2 * dim3 * dim4 * dim5; - array_ = std::shared_ptr (new T[length_]); -} - -//7D -template -CArray::CArray(size_t dim0, - size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4, - size_t dim5, - size_t dim6) { - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - dims_[4] = dim4; - dims_[5] = dim5; - dims_[6] = dim6; - order_ = 7; - length_ = dim0 * dim1 * dim2 * dim3 * dim4 * dim5 * dim6; - array_ = std::shared_ptr (new T[length_]); -} - -//Copy constructor - -template -CArray::CArray(const CArray& temp) { - - // Do nothing if the assignment is of the form x = x - - if (this != &temp) { - for (int iter = 0; iter < temp.order_; iter++){ - dims_[iter] = temp.dims_[iter]; - } // end for - - order_ = temp.order_; - length_ = temp.length_; - array_ = temp.array_; - } // end if - -} // end constructor - - -//overload () operator - -//1D -template -inline T& CArray::operator() (size_t i) const -{ - assert(order_ == 1 && "Tensor order (rank) does not match constructor in CArray 1D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in CArray 1D!"); - - return array_[i]; -} - -//2D -template -inline T& CArray::operator() (size_t i, - size_t j) const -{ - assert(order_ == 2 && "Tensor order (rank) does not match constructor in CArray 2D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in CArray 2D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in CArray 2D!"); - - return array_[j + (i * dims_[1])]; -} - -//3D -template -inline T& CArray::operator() (size_t i, - size_t j, - size_t k) const -{ - assert(order_ == 3 && "Tensor order (rank) does not match constructor in CArray 3D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in CArray 3D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in Carray 3D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in CArray 3D!"); - - return array_[k + (j * dims_[2]) - + (i * dims_[2] * dims_[1])]; -} - -//4D -template -inline T& CArray::operator() (size_t i, - size_t j, - size_t k, - size_t l) const -{ - assert(order_ == 4 && "Tensor order (rank) does not match constructor in CArray 4D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in CArray 4D"); // die if >= dim0 - assert(j >= 0 && j < dims_[1] && "j is out of bounds in CArray 4D"); // die if >= dim1 - assert(k >= 0 && k < dims_[2] && "k is out of bounds in CArray 4D"); // die if >= dim2 - assert(l >= 0 && l < dims_[3] && "l is out of bounds in CArray 4D"); // die if >= dim3 - - return array_[l + (k * dims_[3]) - + (j * dims_[3] * dims_[2]) - + (i * dims_[3] * dims_[2] * dims_[1])]; -} - -//5D -template -inline T& CArray::operator() (size_t i, - size_t j, - size_t k, - size_t l, - size_t m) const -{ - assert(order_ == 5 && "Tensor order (rank) does not match constructor in CArray 5D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in CArray 5D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in CArray 5D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in CArray 5D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in CArray 5D!"); - assert(m >= 0 && m < dims_[4] && "m is out of bounds in CArray 5D!"); - - return array_[m + (l * dims_[4]) - + (k * dims_[4] * dims_[3]) - + (j * dims_[4] * dims_[3] * dims_[2]) - + (i * dims_[4] * dims_[3] * dims_[2] * dims_[1])]; -} - -//6D -template -inline T& CArray::operator() (size_t i, - size_t j, - size_t k, - size_t l, - size_t m, - size_t n) const -{ - assert(order_ == 6 && "Tensor order (rank) does not match constructor in CArray 6D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in CArray 6D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in CArray 6D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in CArray 6D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in CArray 6D!"); - assert(m >= 0 && m < dims_[4] && "m is out of bounds in CArray 6D!"); - assert(n >= 0 && n < dims_[5] && "n is out of bounds in CArray 6D!"); - - return array_[n + (m * dims_[5]) - + (l * dims_[5] * dims_[4]) - + (k * dims_[5] * dims_[4] * dims_[3]) - + (j * dims_[5] * dims_[4] * dims_[3] * dims_[2]) - + (i * dims_[5] * dims_[4] * dims_[3] * dims_[2] * dims_[1])]; -} - -//7D -template -inline T& CArray::operator() (size_t i, - size_t j, - size_t k, - size_t l, - size_t m, - size_t n, - size_t o) const -{ - assert(order_ == 7 && "Tensor order (rank) does not match constructor in CArray 7D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in CArray 7D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in CArray 7D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in CArray 7D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in CArray 7D!"); - assert(m >= 0 && m < dims_[4] && "m is out of bounds in CArray 7D!"); - assert(n >= 0 && n < dims_[5] && "n is out of bounds in CArray 7D!"); - assert(o >= 0 && o < dims_[6] && "o is out of bounds in CArray 7D!"); - - return array_[o + (n * dims_[6]) - + (m * dims_[6] * dims_[5]) - + (l * dims_[6] * dims_[5] * dims_[4]) - + (k * dims_[6] * dims_[5] * dims_[4] * dims_[3]) - + (j * dims_[6] * dims_[5] * dims_[4] * dims_[3] * dims_[2]) - + (i * dims_[6] * dims_[5] * dims_[4] * dims_[3] * dims_[2] * dims_[1])]; - -} - - -//overload = operator -template -inline CArray& CArray::operator= (const CArray& temp) -{ - - // Do nothing if the assignment is of the form x = x - if (this != &temp) { - for (int iter = 0; iter < temp.order_; iter++){ - dims_[iter] = temp.dims_[iter]; - } // end for - - order_ = temp.order_; - length_ = temp.length_; - array_ = temp.array_; - } - return *this; -} - - - -//return size -template -inline size_t CArray::size() const { - return length_; -} - -template -inline size_t CArray::dims(size_t i) const { - assert(i < order_ && "CArray order (rank) does not match constructor, dim[i] does not exist!"); - assert(i >= 0 && dims_[i]>0 && "Access to CArray dims is out of bounds!"); - return dims_[i]; -} - -template -inline size_t CArray::order() const { - return order_; -} - - -template -inline T* CArray::pointer() const{ - return array_.get(); -} - -//destructor -template -CArray::~CArray() {} - -//----endof carray class definitions---- - - -//6. ViewCArray -// indicies are [0:N-1] -template -class ViewCArray { - -private: - size_t dims_[7]; - size_t length_; // Length of 1D array - size_t order_; // tensor order (rank) - T * array_; - -public: - - // Default constructor - ViewCArray (); - - //--- 1D to 7D array --- - ViewCArray(T *array, - size_t dim0); - - ViewCArray(T *array, - size_t dim0, - size_t dim1); - - ViewCArray(T *some_array, - size_t dim0, - size_t dim1, - size_t dim2); - - ViewCArray(T *some_array, - size_t dim0, - size_t dim1, - size_t dim2, - size_t dim3); - - ViewCArray (T *some_array, - size_t dim0, - size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4); - - ViewCArray (T *some_array, - size_t dim0, - size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4, - size_t dim5); - - ViewCArray (T *some_array, - size_t dim0, - size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4, - size_t dim5, - size_t dim6); - - T& operator()(size_t i) const; - - T& operator()(size_t i, - size_t j) const; - - T& operator()(size_t i, - size_t j, - size_t k) const; - - T& operator()(size_t i, - size_t j, - size_t k, - size_t l) const; - T& operator()(size_t i, - size_t j, - size_t k, - size_t l, - size_t m) const; - - T& operator()(size_t i, - size_t j, - size_t k, - size_t l, - size_t m, - size_t n) const; - - T& operator()(size_t i, - size_t j, - size_t k, - size_t l, - size_t m, - size_t n, - size_t o) const; - - // calculate C = math(A,B) - template - void operator=(M do_this_math); - - //return array size - size_t size() const; - - // return array dims - size_t dims(size_t i) const; - - // return array order (rank) - size_t order() const; - - // return pointer - T* pointer() const; - -}; // end of ViewCArray - -//class definitions - -//constructors - -//no dim -template -ViewCArray::ViewCArray() { - array_ = NULL; - length_ = order_ = 0; - for (int i = 0; i < 7; i++) { - dims_[i] = 0; - } -} - -//1D -template -ViewCArray::ViewCArray(T *array, - size_t dim0) -{ - dims_[0] = dim0; - order_ = 1; - length_ = dim0; - array_ = array; -} - -//2D -template -ViewCArray::ViewCArray(T *array, - size_t dim0, - size_t dim1) -{ - dims_[0] = dim0; - dims_[1] = dim1; - order_ = 2; - length_ = dim0 * dim1; - array_ = array; -} - -//3D -template -ViewCArray::ViewCArray (T *array, - size_t dim0, - size_t dim1, - size_t dim2) -{ - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - order_ = 3; - length_ = dim0 * dim1 * dim2; - array_ = array; -} - -//4D -template -ViewCArray::ViewCArray(T *array, - size_t dim0, - size_t dim1, - size_t dim2, - size_t dim3) -{ - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - order_ = 4; - length_ = dim0 * dim1 * dim2 * dim3; - array_ = array; -} - -//5D -template -ViewCArray::ViewCArray(T *array, - size_t dim0, - size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4) -{ - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - dims_[4] = dim4; - order_ = 5; - length_ = dim0 * dim1 * dim2 * dim3 * dim4; - array_ = array; -} - -//6D -template -ViewCArray::ViewCArray(T *array, - size_t dim0, - size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4, - size_t dim5) -{ - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - dims_[4] = dim4; - dims_[5] = dim5; - order_ = 6; - length_ = dim0 * dim1 * dim2 * dim3 * dim4 * dim5; - array_ = array; -} - -//7D -template -ViewCArray::ViewCArray(T *array, - size_t dim0, - size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4, - size_t dim5, - size_t dim6) -{ - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - dims_[4] = dim4; - dims_[5] = dim5; - dims_[6] = dim6; - order_ = 7; - length_ = dim0 * dim1 * dim2 * dim3 * dim4 * dim5 * dim6; - array_ = array; -} - -//overload () operator - -//1D -template -inline T& ViewCArray::operator()(size_t i) const -{ - assert(order_ == 1 && "Tensor order (rank) does not match constructor in ViewCArray 1D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in ViewCArray 1D!"); - - return array_[i]; -} - -/* -//specification for CArray type -//1D -template -inline T& ViewCArray>::operator()(size_t i) const -{ - assert(i < dim1_ && "i is out of bounds in c_array 1D"); // die if >= dim1 - - return (*this_array_)(i); -} -*/ - -//2D -template -inline T& ViewCArray::operator()(size_t i, - size_t j) const -{ - - assert(order_ == 2 && "Tensor order (rank) does not match constructor in ViewCArray 2D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in ViewCArray 2D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in ViewCArray 2D!"); - - return array_[j + (i * dims_[1])]; -} - -//3D -template -inline T& ViewCArray::operator()(size_t i, - size_t j, - size_t k) const -{ - assert(order_ == 3 && "Tensor order (rank) does not match constructor in ViewCArray 3D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in ViewCArray 3D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in ViewCarray 3D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in ViewCArray 3D!"); - - return array_[k + (j * dims_[2]) - + (i * dims_[2] * dims_[1])]; -} - -//4D -template -inline T& ViewCArray::operator()(size_t i, - size_t j, - size_t k, - size_t l) const -{ - assert(order_ == 4 && "Tensor order (rank) does not match constructor in ViewCArray 4D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in ViewCArray 4D"); // die if >= dim0 - assert(j >= 0 && j < dims_[1] && "j is out of bounds in ViewCArray 4D"); // die if >= dim1 - assert(k >= 0 && k < dims_[2] && "k is out of bounds in ViewCArray 4D"); // die if >= dim2 - assert(l >= 0 && l < dims_[3] && "l is out of bounds in ViewCArray 4D"); // die if >= dim3 - - return array_[l + (k * dims_[3]) - + (j * dims_[3] * dims_[2]) - + (i * dims_[3] * dims_[2] * dims_[1])]; -} - -//5D -template -inline T& ViewCArray::operator()(size_t i, - size_t j, - size_t k, - size_t l, - size_t m) const -{ - assert(order_ == 5 && "Tensor order (rank) does not match constructor in ViewCArray 5D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in ViewCArray 5D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in ViewCArray 5D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in ViewCArray 5D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in ViewCArray 5D!"); - assert(m >= 0 && m < dims_[4] && "m is out of bounds in ViewCArray 5D!"); - - return array_[m + (l * dims_[4]) - + (k * dims_[4] * dims_[3]) - + (j * dims_[4] * dims_[3] * dims_[2]) - + (i * dims_[4] * dims_[3] * dims_[2] * dims_[1])]; -} - -//6D -template -inline T& ViewCArray::operator()(size_t i, - size_t j, - size_t k, - size_t l, - size_t m, - size_t n) const -{ - assert(order_ == 6 && "Tensor order (rank) does not match constructor in ViewCArray 6D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in ViewCArray 6D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in ViewCArray 6D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in ViewCArray 6D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in ViewCArray 6D!"); - assert(m >= 0 && m < dims_[4] && "m is out of bounds in ViewCArray 6D!"); - assert(n >= 0 && n < dims_[5] && "n is out of bounds in ViewCArray 6D!"); - - return array_[n + (m * dims_[5]) - + (l * dims_[5] * dims_[4]) - + (k * dims_[5] * dims_[4] * dims_[3]) - + (j * dims_[5] * dims_[4] * dims_[3] * dims_[2]) - + (i * dims_[5] * dims_[4] * dims_[3] * dims_[2] * dims_[1])]; -} - -//7D -template -inline T& ViewCArray::operator()(size_t i, - size_t j, - size_t k, - size_t l, - size_t m, - size_t n, - size_t o) const -{ - assert(order_ == 7 && "Tensor order (rank) does not match constructor in ViewCArray 7D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in ViewCArray 7D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in ViewCArray 7D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in ViewCArray 7D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in ViewCArray 7D!"); - assert(m >= 0 && m < dims_[4] && "m is out of bounds in ViewCArray 7D!"); - assert(n >= 0 && n < dims_[5] && "n is out of bounds in ViewCArray 7D!"); - assert(o >= 0 && o < dims_[6] && "o is out of bounds in ViewCArray 7D!"); - - return array_[o + (n * dims_[6]) - + (m * dims_[6] * dims_[5]) - + (l * dims_[6] * dims_[5] * dims_[4]) - + (k * dims_[6] * dims_[5] * dims_[4] * dims_[3]) - + (j * dims_[6] * dims_[5] * dims_[4] * dims_[3] * dims_[2]) - + (i * dims_[6] * dims_[5] * dims_[4] * dims_[3] * dims_[2] * dims_[1])]; -} - - -// calculate this ViewFArray object = math(A,B) -template -template -void ViewCArray::operator=(M do_this_math){ - do_this_math(*this); // pass in this ViewFArray object -}// end of math opperation - -//return size -template -inline size_t ViewCArray::size() const { - return length_; -} - -template -inline size_t ViewCArray::dims(size_t i) const { - assert(i < order_ && "ViewCArray order (rank) does not match constructor, dim[i] does not exist!"); - assert(i >= 0 && dims_[i]>0 && "Access to ViewCArray dims is out of bounds!"); - return dims_[i]; -} - -template -inline size_t ViewCArray::order() const { - return order_; -} - -template -inline T* ViewCArray::pointer() const { - return array_; -} - -//---end of ViewCArray class definitions---- - - -//7. CMatrix -template -class CMatrix { - -private: - size_t dims_[7]; - size_t length_; // Length of 1D array - size_t order_; // tensor order (rank) - std::shared_ptr matrix_; - -public: - - // default constructor - CMatrix(); - - CMatrix(size_t dim1); - - CMatrix(size_t dim1, - size_t dim2); - - CMatrix(size_t dim1, - size_t dim2, - size_t dim3); - - CMatrix(size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4); - - CMatrix(size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4, - size_t dim5); - - CMatrix (size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4, - size_t dim5, - size_t dim6); - - CMatrix (size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4, - size_t dim5, - size_t dim6, - size_t dim7); - - CMatrix(const CMatrix& temp); - - //overload operators to access data - T& operator()(size_t i) const; - - T& operator()(size_t i, - size_t j) const; - - T& operator()(size_t i, - size_t j, - size_t k) const; - - T& operator()(size_t i, - size_t j, - size_t k, - size_t l) const; - - T& operator()(size_t i, - size_t j, - size_t k, - size_t l, - size_t m) const; - - T& operator()(size_t i, - size_t j, - size_t k, - size_t l, - size_t m, - size_t n) const; - - T& operator()(size_t i, - size_t j, - size_t k, - size_t l, - size_t m, - size_t n, - size_t o) const; - - //overload = operator - CMatrix& operator= (const CMatrix &temp); - - //return array size - size_t size() const; - - // return array dims - size_t dims(size_t i) const; - - // return array order (rank) - size_t order() const; - - //return pointer - T* pointer() const; - - // deconstructor - ~CMatrix( ); - -}; // end of CMatrix - -// CMatrix class definitions - -//constructors - -//no dim - -//1D -template -CMatrix::CMatrix() { - matrix_ = NULL; - length_ = order_ = 0; - for (int i = 0; i < 7; i++) { - dims_[i] = 0; - } -} - -//1D -template -CMatrix::CMatrix(size_t dim1) -{ - dims_[0] = dim1; - order_ = 1; - length_ = dim1; - matrix_ = std::shared_ptr (new T[length_]); -} - -//2D -template -CMatrix::CMatrix(size_t dim1, - size_t dim2) -{ - dims_[0] = dim1; - dims_[1] = dim2; - order_ = 2; - length_ = dim1 * dim2; - matrix_ = std::shared_ptr (new T[length_]); -} - -//3D -template -CMatrix::CMatrix(size_t dim1, - size_t dim2, - size_t dim3) -{ - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - order_ = 3; - length_ = dim1 * dim2 * dim3; - matrix_ = std::shared_ptr (new T[length_]); -} - -//4D -template -CMatrix::CMatrix(size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4) -{ - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - order_ = 4; - length_ = dim1 * dim2 * dim3 * dim4; - matrix_ = std::shared_ptr (new T[length_]); -} - -//5D -template -CMatrix::CMatrix(size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4, - size_t dim5) -{ - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - dims_[4] = dim5; - order_ = 5; - length_ = dim1 * dim2 * dim3 * dim4 * dim5; - matrix_ = std::shared_ptr (new T[length_]); -} - -//6D -template -CMatrix::CMatrix(size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4, - size_t dim5, - size_t dim6) -{ - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - dims_[4] = dim5; - dims_[5] = dim6; - order_ = 6; - length_ = dim1 * dim2 * dim3 * dim4 * dim5 * dim6; - matrix_ = std::shared_ptr (new T[length_]); -} - -//7D -template -CMatrix::CMatrix(size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4, - size_t dim5, - size_t dim6, - size_t dim7) -{ - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - dims_[4] = dim5; - dims_[5] = dim6; - dims_[6] = dim7; - order_ = 7; - length_ = dim1 * dim2 * dim3 * dim4 * dim5 * dim6 * dim7; - matrix_ = std::shared_ptr (new T[length_]); -} - -template -CMatrix::CMatrix(const CMatrix& temp) { - - // Do nothing if the assignment is of the form x = x - - if (this != &temp) { - for (int iter = 0; iter < temp.order_; iter++){ - dims_[iter] = temp.dims_[iter]; - } // end for - - order_ = temp.order_; - length_ = temp.length_; - matrix_ = temp.matrix_; - } // end if - -} // end constructor - -//overload () operator - -//1D -template -T& CMatrix::operator()(size_t i) const -{ - assert(order_ == 1 && "Tensor order (rank) does not match constructor in CMatrix 1D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in CMatrix 1D!"); - - return matrix_[i-1]; -} - -//2D -template -T& CMatrix::operator()(size_t i, - size_t j) const -{ - assert(order_ == 2 && "Tensor order (rank) does not match constructor in CMatrix 2D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in CMatrix 2D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in CMatrix 2D!"); - - return matrix_[(j-1) + (i-1)*dims_[1]]; -} - -//3D -template -T& CMatrix::operator()(size_t i, - size_t j, - size_t k) const -{ - assert(order_ == 3 && "Tensor order (rank) does not match constructor in CMatrix 3D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in CMatrix 3D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in CMatrix 3D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in CMatrix 3D!"); - - return matrix_[(k-1) + (j-1)*dims_[2] - + (i-1)*dims_[2]*dims_[1]]; -} - -//4D -template -T& CMatrix::operator()(size_t i, - size_t j, - size_t k, - size_t l) const -{ - assert(order_ == 4 && "Tensor order (rank) does not match constructor in CMatrix 4D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in CMatrix 4D"); // die if >= dim0 - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in CMatrix 4D"); // die if >= dim1 - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in CMatrix 4D"); // die if >= dim2 - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in CMatrix 4D"); // die if >= dim3 - - return matrix_[(l-1) + (k-1)*dims_[3] - + (j-1)*dims_[3]*dims_[2] - + (i-1)*dims_[3]*dims_[2]*dims_[1]]; -} - -//5D -template -T& CMatrix::operator()(size_t i, - size_t j, - size_t k, - size_t l, - size_t m) const -{ - assert(order_ == 5 && "Tensor order (rank) does not match constructor in CMatrix 5D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in CMatrix 5D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in CMatrix 5D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in CMatrix 5D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in CMatrix 5D!"); - assert(m >= 1 && m <= dims_[4] && "m is out of bounds in CMatrix 5D!"); - - return matrix_[(m-1) + (l-1)*dims_[4] - + (k-1)*dims_[4]*dims_[3] - + (j-1)*dims_[4]*dims_[3]*dims_[2] - + (i-1)*dims_[4]*dims_[3]*dims_[2]*dims_[1]]; -} - -//6D -template -T& CMatrix::operator()(size_t i, - size_t j, - size_t k, - size_t l, - size_t m, - size_t n) const -{ - assert(order_ == 6 && "Tensor order (rank) does not match constructor in CMatrix 6D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in CMatrix 6D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in CMatrix 6D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in CMatrix 6D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in CMatrix 6D!"); - assert(m >= 1 && m <= dims_[4] && "m is out of bounds in CMatrix 6D!"); - assert(n >= 1 && n <= dims_[5] && "n is out of bounds in CMatrix 6D!"); - - return matrix_[ (n-1) + (m-1)*dims_[5] - + (l-1)*dims_[5]*dims_[4] - + (k-1)*dims_[5]*dims_[4]*dims_[3] - + (j-1)*dims_[5]*dims_[4]*dims_[3]*dims_[2] - + (i-1)*dims_[5]*dims_[4]*dims_[3]*dims_[2]*dims_[1]]; -} - -//7D -template -T& CMatrix::operator()(size_t i, - size_t j, - size_t k, - size_t l, - size_t m, - size_t n, - size_t o) const -{ - assert(order_ == 7 && "Tensor order (rank) does not match constructor in CMatrix 7D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in CMatrix 7D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in CMatrix 7D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in CMatrix 7D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in CMatrix 7D!"); - assert(m >= 1 && m <= dims_[4] && "m is out of bounds in CMatrix 7D!"); - assert(n >= 1 && n <= dims_[5] && "n is out of bounds in CMatrix 7D!"); - assert(o >= 1 && o <= dims_[6] && "o is out of bounds in CMatrix 7D!"); - - return matrix_[(o-1) + (n-1)*dims_[6] - + (m-1)*dims_[6]*dims_[5] - + (l-1)*dims_[6]*dims_[5]*dims_[4] - + (k-1)*dims_[6]*dims_[5]*dims_[4]*dims_[3] - + (j-1)*dims_[6]*dims_[5]*dims_[4]*dims_[3]*dims_[2] - + (i-1)*dims_[6]*dims_[5]*dims_[4]*dims_[3]*dims_[2]*dims_[1]]; -} - -//overload = operator -//THIS = CMatrix<> temp -template -CMatrix &CMatrix::operator= (const CMatrix &temp) { - if(this != &temp) { - for (int iter = 0; iter < temp.order_; iter++){ - dims_[iter] = temp.dims_[iter]; - } // end for - - order_ = temp.order_; - length_ = temp.length_; - matrix_ = temp.matrix_; - } - return *this; -} - -template -inline size_t CMatrix::size() const { - return length_; -} - -template -inline size_t CMatrix::dims(size_t i) const { - i--; // i starts at 1 - assert(i < order_ && "CMatrix order (rank) does not match constructor, dim[i] does not exist!"); - assert(i >= 0 && dims_[i]>0 && "Access to CMatrix dims is out of bounds!"); - return dims_[i]; -} - -template -inline size_t CMatrix::order() const { - return order_; -} - -template -inline T* CMatrix::pointer() const{ - return matrix_.get(); -} - -// Destructor -template -CMatrix::~CMatrix(){} - -//----end of CMatrix class definitions---- - - -//8. ViewCMatrix -// indices [1:N] -template -class ViewCMatrix { - -private: - size_t dims_[7]; - size_t length_; // Length of 1D array - size_t order_; // tensor order (rank) - T * matrix_; - -public: - - // default constructor - ViewCMatrix(); - - - //--- 1D array --- - // overloaded constructor - ViewCMatrix (T *matrix, - size_t dim1); - - ViewCMatrix (T *matrix, - size_t dim1, - size_t dim2); - - ViewCMatrix (T *matrix, - size_t dim1, - size_t dim2, - size_t dim3); - - ViewCMatrix (T *matrix, - size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4); - - ViewCMatrix (T *matrix, - size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4, - size_t dim5); - - ViewCMatrix (T *matrix, - size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4, - size_t dim5, - size_t dim6); - - ViewCMatrix (T *matrix, - size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4, - size_t dim5, - size_t dim6, - size_t dim7); - - T& operator() (size_t i) const; - - T& operator() (size_t i, - size_t j) const; - - T& operator() (size_t i, - size_t j, - size_t k) const; - - T& operator() (size_t i, - size_t j, - size_t k, - size_t l) const; - - T& operator() (size_t i, - size_t j, - size_t k, - size_t l, - size_t m) const; - - T& operator() (size_t i, - size_t j, - size_t k, - size_t l, - size_t m, - size_t n) const; - T& operator() (size_t i, - size_t j, - size_t k, - size_t l, - size_t m, - size_t n, - size_t o) const; - - // calculate C = math(A,B) - template - void operator=(M do_this_math); - - //return array size - size_t size() const; - - // return array dims - size_t dims(size_t i) const; - - // return array order (rank) - size_t order() const; - - // return pointer - T* pointer() const; - -}; // end of ViewCMatrix - -//class definitions - -//constructors - -//no dim -template -ViewCMatrix::ViewCMatrix(){ - matrix_ = NULL; - length_ = order_ = 0; - for (int i = 0; i < 7; i++) { - dims_[i] = 0; - } -} - -//1D -template -ViewCMatrix::ViewCMatrix(T *matrix, - size_t dim1) -{ - dims_[0] = dim1; - order_ = 1; - length_ = dim1; - matrix_ = matrix; -} - -//2D -template -ViewCMatrix::ViewCMatrix(T *matrix, - size_t dim1, - size_t dim2) -{ - dims_[0] = dim1; - dims_[1] = dim2; - order_ = 2; - length_ = dim1 * dim2; - matrix_ = matrix; -} - -//3D -template -ViewCMatrix::ViewCMatrix(T *matrix, - size_t dim1, - size_t dim2, - size_t dim3) -{ - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - order_ = 3; - length_ = dim1 * dim2 * dim3; - matrix_ = matrix; -} - -//4D -template -ViewCMatrix::ViewCMatrix(T *matrix, - size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4) -{ - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - order_ = 4; - length_ = dim1 * dim2 * dim3 * dim4; - matrix_ = matrix; -} - -//5D -template -ViewCMatrix::ViewCMatrix(T *matrix, - size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4, - size_t dim5) -{ - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - dims_[4] = dim5; - order_ = 5; - length_ = dim1 * dim2 * dim3 * dim4 * dim5; - matrix_ = matrix; -} - -//6D -template -ViewCMatrix::ViewCMatrix(T *matrix, - size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4, - size_t dim5, - size_t dim6) { - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - dims_[4] = dim5; - dims_[5] = dim6; - order_ = 6; - length_ = dim1 * dim2 * dim3 * dim4 * dim5 * dim6; - matrix_ = matrix; -} - -//7D -template -ViewCMatrix::ViewCMatrix(T *matrix, - size_t dim1, - size_t dim2, - size_t dim3, - size_t dim4, - size_t dim5, - size_t dim6, - size_t dim7) { - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - dims_[4] = dim5; - dims_[5] = dim6; - dims_[6] = dim7; - order_ = 7; - length_ = dim1 * dim2 * dim3 * dim4 * dim5 * dim6 * dim7; - matrix = matrix_; -} - -//overload () operator - -//1D -template -T& ViewCMatrix:: operator() (size_t i) const -{ - assert(order_ == 1 && "Tensor order (rank) does not match constructor in ViewCMatrix 1D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in ViewCMatrix 1D!"); - - return matrix_[i-1]; -} - -//2D -template -T& ViewCMatrix::operator() (size_t i, - size_t j) const -{ - assert(order_ == 2 && "Tensor order (rank) does not match constructor in ViewCMatrix 2D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in ViewCMatrix 2D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in ViewCMatrix 2D!"); - - return matrix_[(j-1) + (i-1)*dims_[1]]; -} - -//3D -template -T& ViewCMatrix::operator () (size_t i, - size_t j, - size_t k) const -{ - assert(order_ == 3 && "Tensor order (rank) does not match constructor in ViewCMatrix 3D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in ViewCMatrix 3D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in ViewCMatrix 3D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in ViewCMatrix 3D!"); - - return matrix_[(k-1) + (j-1)*dims_[2] - + (i-1)*dims_[2]*dims_[1]]; -} - -//4D -template -T& ViewCMatrix::operator()(size_t i, - size_t j, - size_t k, - size_t l) const -{ - assert(order_ == 4 && "Tensor order (rank) does not match constructor in ViewCMatrix 4D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in ViewCMatrix 4D"); // die if >= dim0 - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in ViewCMatrix 4D"); // die if >= dim1 - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in ViewCMatrix 4D"); // die if >= dim2 - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in ViewCMatrix 4D"); // die if >= dim3 - - return matrix_[(l-1) + (k-1)*dims_[3] - + (j-1)*dims_[3]*dims_[2] - + (i-1)*dims_[3]*dims_[2]*dims_[1]]; -} - -//5D -template -T& ViewCMatrix::operator()(size_t i, - size_t j, - size_t k, - size_t l, - size_t m) const -{ - assert(order_ == 5 && "Tensor order (rank) does not match constructor in ViewCMatrix 5D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in ViewCMatrix 5D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in ViewCMatrix 5D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in ViewCMatrix 5D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in ViewCMatrix 5D!"); - assert(m >= 1 && m <= dims_[4] && "m is out of bounds in ViewCMatrix 5D!"); - - return matrix_[(m-1) + (l-1)*dims_[4] - + (k-1)*dims_[4]*dims_[3] - + (j-1)*dims_[4]*dims_[3]*dims_[2] - + (i-1)*dims_[4]*dims_[3]*dims_[2]*dims_[1]]; -} - -//6D -template -T& ViewCMatrix::operator()(size_t i, - size_t j, - size_t k, - size_t l, - size_t m, - size_t n) const -{ - assert(order_ == 6 && "Tensor order (rank) does not match constructor in ViewCMatrix 6D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in ViewCMatrix 6D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in ViewCMatrix 6D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in ViewCMatrix 6D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in ViewCMatrix 6D!"); - assert(m >= 1 && m <= dims_[4] && "m is out of bounds in ViewCMatrix 6D!"); - assert(n >= 1 && n <= dims_[5] && "n is out of bounds in ViewCMatrix 6D!"); - - return matrix_[(n-1) + (m-1)*dims_[5] - + (l-1)*dims_[5]*dims_[4] - + (k-1)*dims_[5]*dims_[4]*dims_[3] - + (j-1)*dims_[5]*dims_[4]*dims_[3]*dims_[2] - + (i-1)*dims_[5]*dims_[4]*dims_[3]*dims_[2]*dims_[1]]; -} - -//7D -template -T& ViewCMatrix::operator()(size_t i, - size_t j, - size_t k, - size_t l, - size_t m, - size_t n, - size_t o) const -{ - assert(order_ == 7 && "Tensor order (rank) does not match constructor in ViewCMatrix 7D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in ViewCMatrix 7D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in ViewCMatrix 7D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in ViewCMatrix 7D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in ViewCMatrix 7D!"); - assert(m >= 1 && m <= dims_[4] && "m is out of bounds in ViewCMatrix 7D!"); - assert(n >= 1 && n <= dims_[5] && "n is out of bounds in ViewCMatrix 7D!"); - assert(o >= 1 && o <= dims_[6] && "o is out of bounds in ViewCMatrix 7D!"); - - return matrix_[(o-1) + (n-1)*dims_[6] - + (m-1)*dims_[6]*dims_[5] - + (l-1)*dims_[6]*dims_[5]*dims_[4] - + (k-1)*dims_[6]*dims_[5]*dims_[4]*dims_[3] - + (j-1)*dims_[6]*dims_[5]*dims_[4]*dims_[3]*dims_[2] - + (i-1)*dims_[6]*dims_[5]*dims_[4]*dims_[3]*dims_[2]*dims_[1]]; -} - -// calculate this ViewFArray object = math(A,B) -template -template -void ViewCMatrix::operator=(M do_this_math){ - do_this_math(*this); // pass in this ViewFArray object -}// end of math opperation - -template -inline size_t ViewCMatrix::size() const { - return length_; -} - -template -inline size_t ViewCMatrix::dims(size_t i) const { - i--; // i starts at 1 - assert(i < order_ && "ViewCMatrix order (rank) does not match constructor, dim[i] does not exist!"); - assert(i >= 0 && dims_[i]>0 && "Access to ViewCMatrix dims is out of bounds!"); - return dims_[i]; -} - -template -inline size_t ViewCMatrix::order() const { - return order_; -} - -template -inline T* ViewCMatrix::pointer() const { - return matrix_; -} - - -//----end of ViewCMatrix class definitions---- - -//9. RaggedRightArray -template -class RaggedRightArray { -private: - std::shared_ptr start_index_; - std::shared_ptr array_; - - size_t dim1_, length_; - size_t num_saved_; // the number saved in the 1D array - -public: - // Default constructor - RaggedRightArray (); - - //--- 2D array access of a ragged right array --- - - // Overload constructor for a CArray - RaggedRightArray (CArray &strides_array); - - // Overload constructor for a ViewCArray - RaggedRightArray (ViewCArray &strides_array); - - // Overloaded constructor for a traditional array - RaggedRightArray (size_t *strides_array, size_t some_dim1); - - // Overload constructor for a RaggedRightArray to - // support a dynamically built stride_array - RaggedRightArray (size_t some_dim1, size_t buffer); - - // Copy constructor - RaggedRightArray (const RaggedRightArray& temp); - - // A method to return the stride size - size_t stride(size_t i) const; - - // A method to increase the number of column entries, i.e., - // the stride size. Used with the constructor for building - // the stride_array dynamically. - // DO NOT USE with the constructures with a strides_array - void push_back(size_t i); - - // Overload operator() to access data as array(i,j) - // where i=[0:N-1], j=[stride(i)] - T& operator()(size_t i, size_t j) const; - - // method to return total size - size_t size() const; - - //return pointer - T* pointer() const; - - //get row starts array - size_t* get_starts() const; - - RaggedRightArray& operator+= (const size_t i); - - RaggedRightArray& operator= (const RaggedRightArray &temp); - - // Destructor - ~RaggedRightArray ( ); -}; // End of RaggedRightArray - -// Default constructor -template -RaggedRightArray::RaggedRightArray () { - array_ = NULL; - start_index_ = NULL; - length_ = dim1_ = num_saved_ = 0; -} - - -// Overloaded constructor with CArray -template -RaggedRightArray::RaggedRightArray (CArray &strides_array){ - // The length of the stride array is some_dim1; - dim1_ = strides_array.size(); - - // Create and initialize the starting index of the entries in the 1D array - start_index_ = std::shared_ptr (new size_t[(dim1_ + 1)]); // note the dim1+1 - start_index_[0] = 0; // the 1D array starts at 0 - - // Loop over to find the total length of the 1D array to - // represent the ragged-right array and set the starting 1D index - size_t count = 0; - for (size_t i = 0; i < dim1_; i++){ - count += strides_array(i); - start_index_[(i + 1)] = count; - } // end for i - length_ = count; - - array_ = std::shared_ptr (new T[length_]); -} // End constructor - -// Overloaded constructor with a view c array -template -RaggedRightArray::RaggedRightArray (ViewCArray &strides_array) { - // The length of the stride array is some_dim1; - dim1_ = strides_array.size(); - - // Create and initialize the starting index of the entries in the 1D array - start_index_ = std::shared_ptr (new size_t[(dim1_ + 1)]); // note the dim1+1 - start_index_[0] = 0; // the 1D array starts at 0 - - // Loop over to find the total length of the 1D array to - // represent the ragged-right array and set the starting 1D index - size_t count = 0; - for (size_t i = 0; i < dim1_; i++){ - count += strides_array(i); - start_index_[(i + 1)] = count; - } // end for i - length_ = count; - - array_ = std::shared_ptr (new T[length_]); -} // End constructor - -// Overloaded constructor with a regular cpp array -template -RaggedRightArray::RaggedRightArray (size_t *strides_array, size_t dim1){ - // The length of the stride array is some_dim1; - dim1_ = dim1; - - // Create and initialize the starting index of the entries in the 1D array - start_index_ = std::shared_ptr (new size_t[(dim1_ + 1)]); // note the dim1+1 - start_index_[0] = 0; // the 1D array starts at 0 - - // Loop over to find the total length of the 1D array to - // represent the ragged-right array and set the starting 1D index - size_t count = 0; - for (size_t i = 0; i < dim1_; i++){ - count += strides_array[i]; - start_index_[(i + 1)] = count; - } // end for i - length_ = count; - - array_ = std::shared_ptr (new T[length_]); -} // End constructor - -// overloaded constructor for a dynamically built strides_array. -// buffer is the max number of columns needed -template -RaggedRightArray::RaggedRightArray (size_t some_dim1, size_t buffer){ - - dim1_ = some_dim1; - - // create and initialize the starting index of the entries in the 1D array - start_index_ = std::shared_ptr (new size_t[(dim1_ + 1)]); // note the dim1+1 - //start_index_[0] = 0; // the 1D array starts at 0 - - num_saved_ = 0; - - length_ = some_dim1*buffer; - array_ = std::shared_ptr (new T[length_]); - -} // end constructor - -// Copy constructor -template -RaggedRightArray::RaggedRightArray (const RaggedRightArray& temp) { - - if (this != &temp) { - dim1_ = temp.dim1_; - length_ = temp.length_; - num_saved_ = temp.num_saved_; - - // shared_ptr - start_index_ = temp.start_index_; - array_ = temp.array_; - } -} - -// A method to return the stride size -template -inline size_t RaggedRightArray::stride(size_t i) const { - // Ensure that i is within bounds - assert(i < dim1_ && "i is greater than dim1_ in RaggedRightArray"); - - return start_index_[(i + 1)] - start_index_[i]; -} - -// A method to increase the stride size, in other words, -// this is used to build the stride array dynamically -// DO NOT USE with constructors that are given a stride array -template -void RaggedRightArray::push_back(size_t i){ - num_saved_ ++; - start_index_[i+1] = num_saved_; -} - -// Overload operator() to access data as array(i,j) -// where i=[0:N-1], j=[0:stride(i)] -template -inline T& RaggedRightArray::operator()(size_t i, size_t j) const { - // get the 1D array index - size_t start = start_index_[i]; - - // asserts - assert(i < dim1_ && "i is out of dim1 bounds in RaggedRightArray"); // die if >= dim1 - //assert(j < stride(i) && "j is out of stride bounds in RaggedRightArray"); // die if >= stride - assert(j+start < length_ && "j+start is out of bounds in RaggedRightArray"); // die if >= 1D array length) - - return array_[j + start]; -} // End operator() - -//return size -template -size_t RaggedRightArray::size() const { - return length_; -} - -template -RaggedRightArray & RaggedRightArray::operator+= (const size_t i) { - this->num_saved_ ++; - this->start_index_[i+1] = num_saved_; - return *this; -} - -//overload = operator -template -RaggedRightArray & RaggedRightArray::operator= (const RaggedRightArray &temp) { - - if( this != &temp) { - dim1_ = temp.dim1_; - length_ = temp.length_; - num_saved_ = temp.num_saved_; - - // shared_ptr - start_index_ = temp.start_index_; - array_ = temp.array_; - } - - return *this; -} - -template -inline T* RaggedRightArray::pointer() const{ - return array_.get(); -} - -template -inline size_t* RaggedRightArray::get_starts() const{ - return start_index_.get(); -} - -// Destructor -template -RaggedRightArray::~RaggedRightArray () {} - -//----end of RaggedRightArray class definitions---- - -//9. RaggedRightArrayofVectors -template -class RaggedRightArrayofVectors { -private: - std::shared_ptr start_index_; - std::shared_ptr array_; - - size_t dim1_, length_, vector_dim_; - size_t num_saved_; // the number saved in the 1D array - -public: - // Default constructor - RaggedRightArrayofVectors (); - - //--- 3D array access of a ragged right array storing a vector of size vector_dim_ at each (i,j)--- - - // Overload constructor for a CArray - RaggedRightArrayofVectors (CArray &strides_array, size_t vector_dim); - - // Overload constructor for a ViewCArray - RaggedRightArrayofVectors (ViewCArray &strides_array, size_t vector_dim); - - // Overloaded constructor for a traditional array - RaggedRightArrayofVectors (size_t *strides_array, size_t some_dim1, size_t vector_dim); - - // Overload constructor for a RaggedRightArray to - // support a dynamically built stride_array - RaggedRightArrayofVectors (size_t some_dim1, size_t buffer, size_t vector_dim); - - // Copy constructor - RaggedRightArrayofVectors (const RaggedRightArrayofVectors& temp); - - // A method to return the stride size - size_t stride(size_t i) const; - - // A method to return the vector dim - size_t vector_dim() const; - - // A method to increase the number of column entries, i.e., - // the stride size. Used with the constructor for building - // the stride_array dynamically. - // DO NOT USE with the constructures with a strides_array - void push_back(size_t i); - - // Overload operator() to access data as array(i,j) - // where i=[0:N-1], j=[stride(i)], k=[0,vector_dim_] - T& operator()(size_t i, size_t j, size_t k) const; - - // method to return total size - size_t size() const; - - //return pointer - T* pointer() const; - - //get row starts array - size_t* get_starts() const; - - RaggedRightArrayofVectors& operator+= (const size_t i); - - RaggedRightArrayofVectors& operator= (const RaggedRightArrayofVectors &temp); - - // Destructor - ~RaggedRightArrayofVectors ( ); -}; // End of RaggedRightArray - -// Default constructor -template -RaggedRightArrayofVectors::RaggedRightArrayofVectors () { - array_ = NULL; - start_index_ = NULL; - length_ = dim1_ = vector_dim_ = num_saved_ = 0; -} - - -// Overloaded constructor with CArray -template -RaggedRightArrayofVectors::RaggedRightArrayofVectors (CArray &strides_array, size_t vector_dim){ - // The length of the stride array is some_dim1; - dim1_ = strides_array.size(); - vector_dim_ = vector_dim; - - // Create and initialize the starting index of the entries in the 1D array - start_index_ = std::shared_ptr (new size_t[(dim1_ + 1)]); // note the dim1+1 - start_index_[0] = 0; // the 1D array starts at 0 - - // Loop over to find the total length of the 1D array to - // represent the ragged-right array and set the starting 1D index - size_t count = 0; - for (size_t i = 0; i < dim1_; i++){ - count += strides_array(i)*vector_dim_; - start_index_[(i + 1)] = count; - } // end for i - length_ = count; - - array_ = std::shared_ptr (new T[length_]); -} // End constructor - -// Overloaded constructor with a view c array -template -RaggedRightArrayofVectors::RaggedRightArrayofVectors (ViewCArray &strides_array, size_t vector_dim) { - // The length of the stride array is some_dim1; - dim1_ = strides_array.size(); - vector_dim_ = vector_dim; - - // Create and initialize the starting index of the entries in the 1D array - start_index_ = std::shared_ptr (new size_t[(dim1_ + 1)]); // note the dim1+1 - start_index_[0] = 0; // the 1D array starts at 0 - - // Loop over to find the total length of the 1D array to - // represent the ragged-right array and set the starting 1D index - size_t count = 0; - for (size_t i = 0; i < dim1_; i++){ - count += strides_array(i)*vector_dim_; - start_index_[(i + 1)] = count; - } // end for i - length_ = count; - - array_ = std::shared_ptr (new T[length_]); -} // End constructor - -// Overloaded constructor with a regular cpp array -template -RaggedRightArrayofVectors::RaggedRightArrayofVectors (size_t *strides_array, size_t dim1, size_t vector_dim){ - // The length of the stride array is some_dim1; - dim1_ = dim1; - vector_dim_ = vector_dim; - - // Create and initialize the starting index of the entries in the 1D array - start_index_ = std::shared_ptr (new size_t[(dim1_ + 1)]); // note the dim1+1 - start_index_[0] = 0; // the 1D array starts at 0 - - // Loop over to find the total length of the 1D array to - // represent the ragged-right array of vectors and set the starting 1D index - size_t count = 0; - for (size_t i = 0; i < dim1_; i++){ - count += strides_array[i]*vector_dim_; - start_index_[(i + 1)] = count; - } // end for i - length_ = count; - - array_ = std::shared_ptr (new T[length_]); -} // End constructor - -// overloaded constructor for a dynamically built strides_array. -// buffer is the max number of columns needed -template -RaggedRightArrayofVectors::RaggedRightArrayofVectors (size_t some_dim1, size_t buffer, size_t vector_dim){ - - dim1_ = some_dim1; - vector_dim_ = vector_dim; - - // create and initialize the starting index of the entries in the 1D array - start_index_ = std::shared_ptr (new size_t[(dim1_ + 1)]); // note the dim1+1 - //start_index_[0] = 0; // the 1D array starts at 0 - - num_saved_ = 0; - - length_ = some_dim1*buffer*vector_dim; - array_ = std::shared_ptr (new T[some_dim1*buffer]); - -} // end constructor - -// Copy constructor -template -RaggedRightArrayofVectors::RaggedRightArrayofVectors (const RaggedRightArrayofVectors& temp) { - - if( this != &temp) { - dim1_ = temp.dim1_; - vector_dim_ = temp.vector_dim_; - length_ = temp.length_; - num_saved_ = temp.num_saved_; - - // shared pointer - start_index_ = temp.start_index_; - array_ = temp.start_index_; - } -} // end copy constructor - -// A method to return the stride size -template -inline size_t RaggedRightArrayofVectors::stride(size_t i) const { - // Ensure that i is within bounds - assert(i < dim1_ && "i is greater than dim1_ in RaggedRightArray"); - - return (start_index_[(i + 1)] - start_index_[i])/vector_dim_; -} - -// A method to increase the stride size, in other words, -// this is used to build the stride array dynamically -// DO NOT USE with constructors that are given a stride array -template -void RaggedRightArrayofVectors::push_back(size_t i){ - num_saved_ += vector_dim_; - start_index_[i+1] = num_saved_; -} - -// Overload operator() to access data as array(i,j,k) -// where i=[0:N-1], j=[0:stride(i)], k=[0:vector_dim_] -template -inline T& RaggedRightArrayofVectors::operator()(size_t i, size_t j, size_t k) const { - // get the 1D array index - size_t start = start_index_[i]; - - // asserts - assert(i < dim1_ && "i is out of dim1 bounds in RaggedRightArray"); // die if >= dim1 - //assert(j < stride(i) && "j is out of stride bounds in RaggedRightArray"); // die if >= stride - assert(j*vector_dim_+start + k < length_ && "j+start is out of bounds in RaggedRightArray"); // die if >= 1D array length) - - return array_[j*vector_dim_ + start + k]; -} // End operator() - -//return size -template -size_t RaggedRightArrayofVectors::size() const { - return length_; -} - -template -RaggedRightArrayofVectors & RaggedRightArrayofVectors::operator+= (const size_t i) { - this->num_saved_ += vector_dim_; - this->start_index_[i+1] = num_saved_; - return *this; -} - -//overload = operator -template -RaggedRightArrayofVectors & RaggedRightArrayofVectors::operator= (const RaggedRightArrayofVectors &temp) { - - if( this != &temp) { - dim1_ = temp.dim1_; - vector_dim_ = temp.vector_dim_; - length_ = temp.length_; - num_saved_ = temp.num_saved_; - - // shared pointer - start_index_ = temp.start_index_; - array_ = temp.start_index_; - } - - return *this; -} - -template -inline T* RaggedRightArrayofVectors::pointer() const{ - return array_.get(); -} - -template -inline size_t* RaggedRightArrayofVectors::get_starts() const{ - return start_index_.get(); -} - -// Destructor -template -RaggedRightArrayofVectors::~RaggedRightArrayofVectors () {} - -//----end of RaggedRightArrayofVectors class definitions---- - -//10. RaggedDownArray -template -class RaggedDownArray { -private: - std::shared_ptr start_index_; - std::shared_ptr array_; - - size_t dim2_; - size_t length_; - size_t num_saved_; // the number saved in the 1D array - -public: - //default constructor - RaggedDownArray() ; - - //~~~~2D`~~~~ - //overload constructor with CArray - RaggedDownArray(CArray &strides_array); - - //overload with ViewCArray - RaggedDownArray(ViewCArray &strides_array); - - //overload with traditional array - RaggedDownArray(size_t *strides_array, size_t dome_dim1); - - // Overload constructor for a RaggedDownArray to - // support a dynamically built stride_array - RaggedDownArray (size_t some_dim2, size_t buffer); - - // Copy constructor - RaggedDownArray (const RaggedDownArray& temp); - - //method to return stride size - size_t stride(size_t j); - - // A method to increase the number of column entries, i.e., - // the stride size. Used with the constructor for building - // the stride_array dynamically. - // DO NOT USE with the constructures with a strides_array - void push_back(size_t j); - - //overload () operator to access data as array (i,j) - T& operator()(size_t i, size_t j); - - // method to return total size - size_t size(); - - //return pointer - T* pointer() const; - - //get row starts array - size_t* get_starts() const; - - //overload = operator - RaggedDownArray& operator= (const RaggedDownArray &temp); - - //destructor - ~RaggedDownArray(); - -}; //~~~~~end of RaggedDownArray class declarations~~~~~~~~ - -//no dims -template -RaggedDownArray::RaggedDownArray() { - array_ = NULL; - start_index_ = NULL; - length_ = dim2_ = num_saved_ = 0; -} - -//overload constructor with CArray -template -RaggedDownArray::RaggedDownArray( CArray &strides_array) { - // Length of stride array - //dim2_ = strides_array.size(); - - // Create and initialize startding indices - start_index_ = std::shared_ptr (new size_t[(dim2_ + 1)]); // note the dim2+1 - start_index_[0] = 0; //1D array starts at 0 - - - //length of strides - dim2_ = strides_array.size(); - - // Loop to find total length of 1D array - size_t count = 0; - for(size_t j = 0; j < dim2_ ; j++) { - count += strides_array(j); - start_index_[j+1] = count; - } - length_ = count; - - array_ = std::shared_ptr (new T[length_]); - -} // End constructor - -// Overload constructor with ViewCArray -template -RaggedDownArray::RaggedDownArray( ViewCArray &strides_array) { - // Length of strides - //dim2_ = strides_array.size(); - - //create array for holding start indices - start_index_ = std::shared_ptr (new size_t[(dim2_ + 1)]); // note the dim2+1 - start_index_[0] = 0; - - size_t count = 0; - // Loop over to get total length of 1D array - for(size_t j = 0; j < dim2_ ;j++ ) { - count += strides_array(j); - start_index_[j+1] = count; - } - length_ = count; - array_ = std::shared_ptr (new T[length_]); - -} // End constructor - -// Overload constructor with regualar array -template -RaggedDownArray::RaggedDownArray( size_t *strides_array, size_t dim2){ - // Length of stride array - dim2_ = dim2; - - // Create and initialize starting index of entries - start_index_ = std::shared_ptr (new size_t[(dim2_ + 1)]); // note the dim2+1 - start_index_[0] = 0; - - // Loop over to find length of 1D array - // Represent ragged down array and set 1D index - size_t count = 0; - for(size_t j = 0; j < dim2_; j++) { - count += strides_array[j]; - start_index_[j+1] = count; - } - length_ = count; - array_ = std::shared_ptr (new T[length_]); - -} //end construnctor - -// overloaded constructor for a dynamically built strides_array. -// buffer is the max number of columns needed -template -RaggedDownArray::RaggedDownArray (size_t some_dim2, size_t buffer){ - - dim2_ = some_dim2; - - // create and initialize the starting index of the entries in the 1D array - start_index_ = std::shared_ptr (new size_t[(dim2_ + 1)]); // note the dim2+1 - //start_index_[0] = 0; // the 1D array starts at 0 - - num_saved_ = 0; - - length_ = some_dim2*buffer; - array_ = std::shared_ptr (new T[length_]); - -} // end constructor - -// Copy constructor -template -RaggedDownArray::RaggedDownArray (const RaggedDownArray& temp) { - if( this != &temp) { - dim2_ = temp.dim2_; - length_ = temp.length_; - num_saved_ = temp.num_saved_; - - // shared pointer - start_index_ = temp.start_index_; - array_ = temp.array_; - } -} // end copy constructor - -// Check the stride size -template -size_t RaggedDownArray::stride(size_t j) { - assert(j < dim2_ && "j is greater than dim2_ in RaggedDownArray"); - - return start_index_[j+1] - start_index_[j]; -} - -// A method to increase the stride size, in other words, -// this is used to build the stride array dynamically -// DO NOT USE with constructors that are given a stride array -template -void RaggedDownArray::push_back(size_t j){ - num_saved_ ++; - start_index_[j+1] = num_saved_; -} - -//return size -template -size_t RaggedDownArray::size() { - return length_; -} - -// overload operator () to access data as an array(i,j) -// Note: i = 0:stride(j), j = 0:N-1 -template -T& RaggedDownArray::operator()(size_t i, size_t j) { - // Where is the array starting? - // look at start index - size_t start = start_index_[j]; - - // Make sure we are within array bounds - assert(i < stride(j) && "i is out of bounds in RaggedDownArray"); - assert(j < dim2_ && "j is out of dim2_ bounds in RaggedDownArray"); - assert(i+start < length_ && "i+start is out of bounds in RaggedDownArray"); // die if >= 1D array length) - - return array_[i + start]; - -} // End () operator - -//overload = operator -template -RaggedDownArray & RaggedDownArray::operator= (const RaggedDownArray &temp) { - - if( this != &temp) { - dim2_ = temp.dim2_; - length_ = temp.length_; - num_saved_ = temp.num_saved_; - - // shared pointer - start_index_ = temp.start_index_; - array_ = temp.array_; - } - - return *this; -} - -template -inline T* RaggedDownArray::pointer() const{ - return array_.get(); -} - - -template -inline size_t* RaggedDownArray::get_starts() const{ - return start_index_.get(); -} - -// Destructor -template -RaggedDownArray::~RaggedDownArray() {} -// End destructor - - -//----end of RaggedDownArray---- - - -//11. DynamicRaggedRightArray - -template -class DynamicRaggedRightArray { -private: - std::shared_ptr stride_; - std::shared_ptr array_; - - size_t dim1_; - size_t dim2_; - size_t length_; - -public: - // Default constructor - DynamicRaggedRightArray (); - - //--- 2D array access of a ragged right array --- - - // overload constructor - DynamicRaggedRightArray (size_t dim1, size_t dim2); - - // Copy constructor - DynamicRaggedRightArray (const DynamicRaggedRightArray& temp); - - // A method to return or set the stride size - size_t& stride(size_t i) const; - - // A method to return the size - size_t size() const; - - //return pointer - T* pointer() const; - - // Overload operator() to access data as array(i,j), - // where i=[0:N-1], j=[stride(i)] - T& operator()(size_t i, size_t j) const; - - // Overload copy assignment operator - DynamicRaggedRightArray& operator= (const DynamicRaggedRightArray &temp); - - // Destructor - ~DynamicRaggedRightArray (); -}; - -//nothing -template -DynamicRaggedRightArray::DynamicRaggedRightArray () { - array_ = NULL; - stride_ = NULL; - length_ = dim1_ = dim2_ = 0; -} - -// Overloaded constructor -template -DynamicRaggedRightArray::DynamicRaggedRightArray (size_t dim1, size_t dim2) { - // The dimensions of the array; - dim1_ = dim1; - dim2_ = dim2; - length_ = dim1*dim2; - - // Create memory on the heap for the values - array_ = std::shared_ptr (new T[dim1*dim2]); - - // Create memory for the stride size in each row - stride_ = std::shared_ptr (new size_t[dim1]); - - // Initialize the stride - for (int i=0; i -DynamicRaggedRightArray::DynamicRaggedRightArray (const DynamicRaggedRightArray& temp) { - if( this != &temp) { - dim1_ = temp.dim1_; - dim2_ = temp.dim2_; - length_ = temp.length_; - - // shared pointer - stride_ = temp.stride_; - array_ = temp.array_; - } -} // end copy constructor - -// A method to set the stride size for row i -template -size_t& DynamicRaggedRightArray::stride(size_t i) const { - return stride_[i]; -} - -//return size -template -size_t DynamicRaggedRightArray::size() const{ - return length_; -} - -// Overload operator() to access data as array(i,j), -// where i=[0:N-1], j=[0:stride(i)] -template -inline T& DynamicRaggedRightArray::operator()(size_t i, size_t j) const { - // Asserts - assert(i < dim1_ && "i is out of dim1 bounds in DynamicRaggedRight"); // die if >= dim1 - assert(j < dim2_ && "j is out of dim2 bounds in DynamicRaggedRight"); // die if >= dim2 - assert(j < stride_[i] && "j is out of stride bounds in DynamicRaggedRight"); // die if >= stride - - return array_[j + i*dim2_]; -} - -//overload = operator -template -inline DynamicRaggedRightArray& DynamicRaggedRightArray::operator= (const DynamicRaggedRightArray &temp) -{ - - if( this != &temp) { - dim1_ = temp.dim1_; - dim2_ = temp.dim2_; - length_ = temp.length_; - - // shared pointer - stride_ = temp.stride_; - array_ = temp.array_; - } - - return *this; -} - -template -inline T* DynamicRaggedRightArray::pointer() const{ - return array_.get(); -} - -// Destructor -template -DynamicRaggedRightArray::~DynamicRaggedRightArray() {} - - - - -//----end DynamicRaggedRightArray class definitions---- - - -//12. DynamicRaggedDownArray - -template -class DynamicRaggedDownArray { -private: - std::shared_ptr stride_; - std::shared_ptr array_; - - size_t dim1_; - size_t dim2_; - size_t length_; - -public: - // Default constructor - DynamicRaggedDownArray (); - - //--- 2D array access of a ragged right array --- - - // overload constructor - DynamicRaggedDownArray (size_t dim1, size_t dim2); - - // Copy constructor - DynamicRaggedDownArray (const DynamicRaggedDownArray& temp); - - // A method to return or set the stride size - size_t& stride(size_t j) const; - - // A method to return the size - size_t size() const; - - // Overload operator() to access data as array(i,j), - // where i=[stride(j)], j=[0:N-1] - T& operator()(size_t i, size_t j) const; - - // Overload copy assignment operator - DynamicRaggedDownArray& operator= (const DynamicRaggedDownArray &temp); - - //return pointer - T* pointer() const; - - // Destructor - ~DynamicRaggedDownArray (); -}; - -//nothing -template -DynamicRaggedDownArray::DynamicRaggedDownArray () { - array_ = NULL; - stride_ = NULL; - length_ = dim1_ = dim2_ = 0; -} - -// Overloaded constructor -template -DynamicRaggedDownArray::DynamicRaggedDownArray (size_t dim1, size_t dim2) { - // The dimensions of the array; - dim1_ = dim1; - dim2_ = dim2; - length_ = dim1*dim2; - - // Create memory on the heap for the values - array_ = std::shared_ptr (new T[dim1*dim2]); - - // Create memory for the stride size in each row - stride_ = std::shared_ptr (new size_t[dim2]); - - // Initialize the stride - for (int j=0; j -DynamicRaggedDownArray::DynamicRaggedDownArray (const DynamicRaggedDownArray& temp) { - if( this != &temp) { - dim1_ = temp.dim1_; - dim2_ = temp.dim2_; - length_ = temp.length_; - - // shared pointer - stride_ = temp.stride_; - array_ = temp.array_; - } -} // end copy constructor - - -// A method to set the stride size for column j -template -size_t& DynamicRaggedDownArray::stride(size_t j) const { - return stride_[j]; -} - -//return size -template -size_t DynamicRaggedDownArray::size() const{ - return length_; -} - -// overload operator () to access data as an array(i,j) -// Note: i = 0:stride(j), j = 0:N-1 - -template -inline T& DynamicRaggedDownArray::operator()(size_t i, size_t j) const { - // Asserts - assert(i < dim1_ && "i is out of dim1 bounds in DynamicRaggedDownArray"); // die if >= dim1 - assert(j < dim2_ && "j is out of dim2 bounds in DynamicRaggedDownArray"); // die if >= dim2 - assert(i < stride_[j] && "i is out of stride bounds in DynamicRaggedDownArray"); // die if >= stride - - return array_[i + j*dim1_]; -} - -//overload = operator -template -inline DynamicRaggedDownArray& DynamicRaggedDownArray::operator= (const DynamicRaggedDownArray &temp) -{ - - if( this != &temp) { - dim1_ = temp.dim1_; - dim2_ = temp.dim2_; - length_ = temp.length_; - - // shared pointer - stride_ = temp.stride_; - array_ = temp.array_; - } - - return *this; -} - -template -inline T* DynamicRaggedDownArray::pointer() const{ - return array_.get(); -} - -// Destructor -template -DynamicRaggedDownArray::~DynamicRaggedDownArray() {} - -//----end of DynamicRaggedDownArray class definitions----- - - -// 15CSRArrayy -template -class CSRArray { - private: // What ought to be private ? - size_t dim1_, dim2_; // dim1_ is number of rows dim2_ is number of columns - size_t nnz_; - std::shared_ptr array_; - std::shared_ptr column_index_; - std::shared_ptr start_index_; - - public: - - /** - * @brief Construct a new Sparse Row Array object - * - */ - CSRArray(); - - - /** - * @brief Construct a new Sparse Row Array object - * - * @param array 1d array of data values in order read left to right top to bottom - * @param column_index_ 1d array specifying for each data value what column it is found - * @param start_index_ 1d array that marks the index for array and column where each row starts - * @param dim1 number of rows - * @param dim2 number of columns - */ - CSRArray(CArray array, CArray column_index, CArray start_index, size_t dim1, size_t dim2); - - /** - * @brief Constructor for a new Sparse Row Array object using the data from a dense matrix - * - * @param dense matrix to get the value from - */ - CSRArray(CArray dense); - - /** - * @brief Copy a new Sparse Row Array object - * - * @param temp array to copy - */ - CSRArray(const CSRArray &temp); - - /** - * @brief Access A(i,j). Returns a dummy address with value 0 if A(i,j) is not alocated - * - * @param i row - * @param j column - * @return T& - */ - T& operator()(size_t i, size_t j) const; - - /** - * @brief Same functionality as operator(). Used for compatibility with other matar types - * - * @param i Row - * @param j Column - * @return T& - */ - T& value(size_t i, size_t j) const; - - /** - * @brief Assignment operator. Uses shared pointers to the data of temp instead of making a fresh copy - * - * @param temp CSRArray to copy values of - * @return CSRArray& - */ - CSRArray& operator=(const CSRArray &temp); - - /** - * @brief get start of array_ data - * - * @return T* - */ - T* pointer() const; - - /** - * @brief Get the start_index_ object - * - * @return size_t* - */ - size_t* get_starts() const; - - void printer(); //debugging tool - - /** - * @brief Iterator notation to go through the non zero values of row i. - * - * @param i row - * @return T* pointer to first non zero element of row i - */ - T* begin(size_t i); - - /** - * @brief Iterator notation to go through the non zeros values of row i - * - * @param i row - * @return T* pointer to first non zero element of row i+1 - */ - T* end(size_t i); - - /** - * @brief Return number of non zero elements in row i. Included for compatibility with other matar types - * - * @param i row - */ - size_t stride(size_t i) const; - - /** - * @brief Number of rows - * - */ - - size_t dim1() const; - /** - * @brief Number of Columns - * - */ - size_t dim2() const; - - // iterator for the raw data at row i - // i.e. return the index each element is the index in the 1 array - // This as the use of providing a reasonable way to get the column - // index and data value in the case you need both - size_t begin_index(size_t i); - size_t end_index(size_t i); - - // Get number of non zero elements in row i - size_t nnz(size_t i); - // Get total number of non zero elements - size_t nnz(); - - // underscore stuff - // Use the index into the 1d array to get what value is stored there and what is the corresponding row - T& get_val_flat(size_t k); - size_t get_col_flat(size_t k); - // reverse map function from A(i,j) to what element of data/col_pt_ it corersponds to - size_t flat_index(size_t i, size_t j); - // Convertor - int toCSC(CArray &array, CArray &start_index, CArray &row_index); - - void to_dense(CArray& A); - //destructor - ~CSRArray(); - - -}; - -template -CSRArray::CSRArray(){ - dim1_ = dim2_ = nnz_ = 0; - array_ = NULL; - column_index_ = start_index_ = NULL; -} - -template -CSRArray::CSRArray(CArray array, CArray column_index, CArray start_index, size_t dim1, size_t dim2 ){ - dim1_ = dim1; - dim2_ = dim2; - size_t nnz = array.size(); - start_index_ = std::shared_ptr (new size_t[dim1_ + 1]); - array_ = std::shared_ptr (new T[nnz+1]); - column_index_ = std::shared_ptr (new size_t[nnz]); - size_t i ; - for(i = 0; i < nnz; i++){ - array_[i] = array(i); - column_index_[i] = column_index(i); - } - for(i = 0; i < dim1_ + 1; i++){ - start_index_[i] = start_index(i); - } - nnz_ = nnz; -} - -template -CSRArray::CSRArray(const CSRArray &temp){ - if(this != temp) { - nnz_ = temp.nnz_; - dim1_ = temp.dim1_; - dim2_ = temp.dim2_; - - start_index_ = temp.start_index_; - column_index_ = temp.column_index_; - array_ = temp.array_; - } -} - -template -CSRArray::CSRArray(CArray dense){ - dim1_ = dense.dims(0); - dim2_ = dense.dims(1); - nnz_ = dense.size(); - start_index_ = std::shared_ptr (new size_t[dim1_ + 1]); - array_ = std::shared_ptr (new T[nnz_ + 1]); - column_index_ = std::shared_ptr (new size_t[nnz_]); - size_t i,j; - size_t cur = 0; - for(i = 0; i < dim1_; i++){ - for(j = 0; i < dim2_; j++){ - if(j != 0){ - start_index_[i+1] += 1; - column_index_[cur] = j; - array_[cur] = dense(i,j); - cur++; - } - } - } - start_index_[0] = 0; - for(i = 1; i <= dim1_; i++){ - start_index_[i] = start_index_[i] + start_index_[i+1]; - } - - -} - -template -T& CSRArray::operator()(size_t i, size_t j) const { - size_t row_start = start_index_[i]; - size_t row_end = start_index_[i+1]; - size_t k; - for(k = 0; k < row_end - row_start; k++){ - if(column_index_[row_start + k] == j){ - return array_[row_start + k]; - } - } - array_[nnz_] = (T) NULL; - return array_[nnz_]; -} - - -template -T& CSRArray::value(size_t i, size_t j) const { - size_t row_start = start_index_[i]; - size_t row_end = start_index_[i+1]; - size_t k; - for(k = 0; k < row_end - row_start; k++){ - if(column_index_[row_start + k] == j){ - return array_[row_start + k]; - } - } - array_[nnz_] = (T) NULL; - return array_[nnz_]; -} - -template -T* CSRArray::pointer() const{ - return array_.get(); -} - -template -size_t* CSRArray::get_starts() const { - return start_index_.get(); -} - -template -CSRArray& CSRArray::operator=(const CSRArray &temp){ - if(this != temp) { - nnz_ = temp.nnz_; - dim1_ = temp.dim1_; - dim2_ = temp.dim2_; - - start_index_ = temp.start_index_; - column_index_ = temp.column_index_; - array_ = temp.array_; - } - return *this; -} - -//debugging tool primarily -template -void CSRArray::printer(){ - size_t i,j; - for(i = 0; i < dim1_; i++){ - for(j = 0; j < dim2_; j++){ - printf(" %d ", (*this)(i,j)); - } - printf("\n"); - } -} - -template -void CSRArray::to_dense(CArray& A){ - size_t i,j; - for(i = 0; i < dim1_; i++){ - for(j = 0; j < dim2_; j++){ - A(i,j) = (*this)(i,j); - } - } - -} - -template -size_t CSRArray::stride(size_t i) const { - assert(i <= dim1_ && "Index i out of bounds in CSRArray.stride()"); - return start_index_[i+i] - start_index_[i]; - -} - - -template -size_t CSRArray::dim1() const { - return dim1_; -} - -template -size_t CSRArray::dim2() const { - return dim2_; -} - - -template -T* CSRArray::begin(size_t i){ - assert(i <= dim1_ && "i is out of bounds in CSRArray.begin()"); - size_t row_start = start_index_[i]; - return &array_[row_start]; -} - -template -T* CSRArray::end(size_t i){ - assert(i <= dim1_ && "i is out of bounds in CSRArray.end()"); - size_t row_start = start_index_[i+1]; - return &array_[row_start]; -} - -template -size_t CSRArray::begin_index(size_t i){ - assert(i <= dim1_ && "i is out of bounds in CSRArray.begin_index()"); - return start_index_[i]; -} - -template -size_t CSRArray::end_index(size_t i){ - assert(i <= dim1_ && "i is out of bounds in CSRArray.begin_index()"); - return start_index_[i+1]; -} - -template -size_t CSRArray::nnz(){ - return nnz_; -} - -template -size_t CSRArray::nnz(size_t i){ - assert(i <= dim1_ && "Index i out of bounds in CSRArray.stride()"); - return start_index_[i+1] - start_index_[i]; -} - - -template -T& CSRArray::get_val_flat(size_t k){ - assert(k < nnz_ && "Index k is out of bounds in CSRArray.get_val_flat()"); - return array_[k]; -} - -template -size_t CSRArray::get_col_flat(size_t k){ - assert(k < nnz_ && "Index k is out of bounds in CSRArray.get_col_lat()"); - return column_index_[k]; -} - - -template -size_t CSRArray::flat_index(size_t i, size_t j){ - size_t k; - size_t row_start = start_index_[i]; - size_t row_end = start_index_[i+1]; - for(k = 0; k < row_end - row_start; k++){ - if(column_index_[row_start+k] == j){ - return row_start+k; - } - } - return nnz_; -} - -// Assumes that data, col_ptrs, and row_ptrs -// have been allocated size already before this call -// Returns the data in this csr format but as represented as the appropriatte vectors -// for a csc format -template -int CSRArray::toCSC(CArray &data, CArray &col_ptrs, CArray &row_ptrs ){ - int nnz_cols[dim2_ + 1]; - int col_counts[dim2_]; - int i = 0; - // How many elements are each column - for(i =0 ; i < dim2_; i++){ - nnz_cols[i] = 0; - col_counts[i] = 0; - } - nnz_cols[dim2_] = 0; - col_ptrs(0) = 0; - for(i =0; i < nnz_; i++){ - nnz_cols[column_index_[i] + 1] += 1; - } - // What we actually care about is how many elements are - // in all the columns preceeding this column. - for(i = 1; i <= dim2_; i++){ - nnz_cols[i] = nnz_cols[i-1] + nnz_cols[i]; - col_ptrs(i) = nnz_cols[i]; - } - size_t row = 1; - // if b is at A(i,j) stored in csr format - // it needs to go where the where the ith column starts - // + how many things we have put in the "window" - // we allocated for this column already - // For row we simply keep track of what row we are currently in - // as we scan through the 1d array of data. - for(i = 0; i < nnz_; i++){ - if(i >= start_index_[row]){ - row++; - } - int idx = nnz_cols[column_index_[i]] + col_counts[column_index_[i]]; - col_counts[column_index_[i]] += 1; - data(idx) = array_[i]; - row_ptrs(idx) = row - 1; - } - // I return an int because I thought I might need to return an error code - // Not sure that is true - return 0; -} - -template -CSRArray::~CSRArray() {} - -// EndCSRArrayy - -// 16 CSCArray -template -class CSCArray -{ -private: // What ought to be private ? - size_t dim1_, dim2_; - size_t nnz_; - std::shared_ptr array_; - std::shared_ptr start_index_; - std::shared_ptr row_index_; - - public: - - /** - * @brief Construct a new empty Sparse Col Array object - * - */ - CSCArray(); - - /** - * @brief Construct a new Sparse Col Array object - * - * @param array: 1d array of data values in order as read top to bottom, left to right - * @param row_index: 1d array that marks what row each element is in - * @param start_index: 1d array that marks where the first element of each column starts - * @param dim1: number of rows the matrix should have - * @param dim2: number of columns the matrix should have - */ - CSCArray(CArray array, CArray row_index, CArray start_index, size_t dim1, size_t dim2); - - /** - * @brief Access A(i,j). Returns a dummy address with value 0 if A(i,j) is not allocated - * - * @param i : row - * @param j : column - * @return T& : address of array_ that corresponds to A(i,j) - */ - T &operator()(size_t i, size_t j) const; - - /** - * @brief Overloaded copy operator - * - * @param temp : Array to copy - * @return CSCArray& - */ - CSCArray &operator=(const CSCArray &temp); - - T *pointer() const; - - /** - * @brief Same functionality as nnz(i) included for compatibility with the rest of matar - * - * @param i : row - * @return size_t - */ - size_t stride(size_t i) const; - - /** - * @brief Same functionality as operator() included for compatibility with the rest of matar - * - * @param i: row - * @param j: column - * @return T& - */ - T &value(size_t i, size_t j) const; - - /** - * @brief Get the start_index array - * - * @return size_t* : returns start_index_ - */ - size_t *get_starts() const; - - /** - * @brief Get number of rows - * - * @return size_t number of rows - */ - size_t dim1() const; - - /** - * @brief Get number of columns - * - * @return size_t number of columns - */ - size_t dim2() const; - - /** - * @brief iterator notation for iterating through the non zeros values of row i. - * - * @param i : row - * @return T* - */ - T *begin(size_t i); - - /** - * @brief iterator notation for iterating through the non zeros values of row i. - * - * @param i : row - * @return T* - */ - T *end(size_t i); - - // iterator for the raw data at row i - // i.e. return the index each element is the index in the 1 array - // This as the use of providing a reasonable way to get the column - // index and data value in the case you need both - size_t begin_index(size_t i); - size_t end_index (size_t i); - - /** - * @brief Get the number of non zero elements in row i - * - * @param i : row to get - * @return size_t : size of row - */ - size_t nnz(size_t i); - /** - * @brief Get number of non zero elements total in array - * - * @return size_t - */ - size_t nnz(); - - // Use the index into the 1d array to get what value is stored there and what is the corresponding row - T &get_val_flat(size_t k); - size_t get_row_flat(size_t k); - // reverse map function from A(i,j) to what element of data/col_pt_ it corersponds to - int flat_index(size_t i, size_t j); - // Convertor - int toCSR(CArray &data, CArray &row_ptrs, CArray &col_ptrs); - void to_dense(FArray &A); - // destructor - ~CSCArray(); -}; - -template -CSCArray::CSCArray(){ - dim1_ = dim2_ = nnz_ = 0; - array_ = NULL; - row_index_ = start_index_ = NULL; -} - -template -CSCArray::CSCArray(CArray array, CArray row_index, CArray start_index, size_t dim1, size_t dim2 ){ - dim1_ = dim1; - dim2_ = dim2; - size_t nnz = array.size(); - start_index_ = std::shared_ptr (new size_t[dim2_ + 1]); - array_ = std::shared_ptr (new T[nnz+1]); - row_index_ = std::shared_ptr (new size_t[nnz]); - size_t i ; - for(i = 0; i < nnz; i++){ - array_[i] = array(i); - row_index_[i] = row_index(i); - } - for(i = 0; i < dim2_ + 1; i++){ - start_index_[i] = start_index(i); - } - nnz_ = nnz; -} - - -template -T& CSCArray::operator()(size_t i, size_t j) const { - size_t col_start = start_index_[j]; - size_t col_end = start_index_[j + 1]; - size_t k; - for(k =0; k < col_end - col_start;k++){ - if(row_index_[col_start + k] == i){ - return array_[col_start + k]; - } - } - array_[nnz_] = (T) NULL; - return array_[nnz_]; -} - -template -T* CSCArray::pointer() const { - return array_.get(); -} - -template -T& CSCArray::value(size_t i, size_t j) const { - size_t col_start = start_index_[j]; - size_t col_end = start_index_[j + 1]; - size_t k; - for(k =0; k < col_end - col_start;k++){ - if(row_index_[col_start + k] == i){ - return array_[col_start + k]; - } - } - array_[nnz_] = (T) NULL; - return array_[nnz_]; -} - -template -size_t* CSCArray::get_starts() const{ - return &start_index_[0]; -} - -template -CSCArray& CSCArray::operator=(const CSCArray &temp){ - if(this != temp) { - nnz_ = temp.nnz_; - dim2_ = temp.dim2_; - dim1_ = temp.dim1_;; - - start_index_ = temp.start_index_; - row_index_ = temp.row_row_index_; - array_ = temp.array_; - } - return *this; -} - -template -size_t CSCArray::stride(size_t i) const{ - assert(i < dim2_ && "i is out of bounds in CSCArray.stride()"); - return start_index_[i+1] - start_index_[i]; -} - - -template -void CSCArray::to_dense(FArray& A){ - size_t i,j; - for (j = 0; j < dim2_; j++) - { - for(i = 0; i < dim1_; i++){ - A(i,j) = (*this)(i,j); - } - } -} - -template -size_t CSCArray::dim1() const { - return dim1_; -} - -template -size_t CSCArray::dim2() const{ - return dim2_; -} - -template -T* CSCArray::begin(size_t i){ - assert(i <= dim2_ && "index i out of bounds at CSCArray.begin()"); - size_t col_start = start_index_[i]; - return &array_[col_start]; -} - -template -T* CSCArray::end(size_t i){ - assert(i <= dim2_ && "index i out of bounds at CSCArray.endt()"); - size_t col_start = start_index_[i+1]; - return &array_[col_start]; -} - -template -size_t CSCArray::begin_index(size_t i){ - assert(i <= dim2_ && "index i out of bounds at CSCArray.begin_index()"); - return start_index_[i]; -} - -template -size_t CSCArray::end_index(size_t i){ - assert(i <= dim2_ && "index i out of bounds at CSCArray.end_index()"); - return start_index_[i + 1]; -} - -template -size_t CSCArray::nnz(){ - return nnz_; -} - -template -size_t CSCArray::nnz(size_t i){ - return start_index_[i+1] - start_index_[i]; -} - -template -T& CSCArray::get_val_flat(size_t k){ - return array_[k]; -} - -template -size_t CSCArray::get_row_flat(size_t k){ - return row_index_[k]; -} - -template -int CSCArray::flat_index(size_t i, size_t j){ - size_t col_start = start_index_[j]; - size_t col_end = start_index_[j+1]; - size_t k; - for (k = 0; k < col_end - col_start; k++) - { - if(row_index_[col_start + k] == i){ - return col_start + k; - } - } - return -1; -} - -// Assumes that data, col_ptrs, and row_ptrs -// have been allocated size already before this call -// Returns the data in this csr format but as represented as the appropriatte vectors -// for a csc format -template -int CSCArray::toCSR(CArray &data, CArray &col_ptrs, CArray &row_ptrs ){ - int nnz_rows[dim1_ + 1]; - int row_counts[dim1_]; - int i = 0; - // How many elements are each column - for(i =0 ; i < dim1_; i++){ - nnz_rows[i] = 0; - row_counts[i] = 0; - } - nnz_rows[dim1_] = 0; - row_ptrs(i) = 0 ; - for(i =0; i < nnz_; i++){ - nnz_rows[row_index_[i] + 1] += 1; - } - // What we actually care about is how many elements are - // in all the columns preceeding this column. - for(i = 1; i < dim1_; i++){ - nnz_rows[i] = nnz_rows[i-1] + nnz_rows[i]; - row_ptrs(i) = nnz_rows[i]; - } - size_t col = 1; - // if b is at A(i,j) stored in csr format - // it needs to go where the where the ith column starts - // + how many things we have put in the "window" - // we allocated for this column already - // For row we simply keep track of what row we are currently in - // as we scan through the 1d array of data. - for(i = 0; i < nnz_; i++){ - if(i >= start_index_[col]){ - col++; - } - int idx = nnz_rows[row_index_[i]] + row_counts[row_index_[i]]; - row_counts[row_index_[i]] += 1; - data(idx) = array_[i]; - col_ptrs(idx) = col - 1; - } - // I return an int because I thought I might need to return an error code - // Not sure that is true - return 0; -} - -template -CSCArray::~CSCArray() {} - -// End of CSCArray - - -//======================================================================= -// end of standard MATAR data-types -//======================================================================== - - -} // end namespace - - - -#endif // HOST_TYPES_H diff --git a/src/include/kokkos_types.h b/src/include/kokkos_types.h index 3a3b78a7..9eb66aec 100644 --- a/src/include/kokkos_types.h +++ b/src/include/kokkos_types.h @@ -107,6 +107,8 @@ using SArray5D = Kokkos::View; using SHArray1D = Kokkos::View; #endif +//To disable asserts, uncomment the following line +//#define NDEBUG @@ -1988,6 +1990,7 @@ class DViewFArrayKokkos { using TArray1DHost = Kokkos::View; // this is manage using TArray1D = Kokkos::View; + typename ExecSpace::memory_space memspace; private: size_t dims_[7]; @@ -2000,24 +2003,24 @@ class DViewFArrayKokkos { public: DViewFArrayKokkos(); - DViewFArrayKokkos(T * inp_array, size_t dim0); + DViewFArrayKokkos(T * inp_array, size_t dim0, const std::string& tag_string = DEFAULTSTRINGARRAY); - DViewFArrayKokkos(T * inp_array, size_t dim0, size_t dim1); + DViewFArrayKokkos(T * inp_array, size_t dim0, size_t dim1, const std::string& tag_string = DEFAULTSTRINGARRAY); - DViewFArrayKokkos(T * inp_array, size_t dim0, size_t dim1, size_t dim2); + DViewFArrayKokkos(T * inp_array, size_t dim0, size_t dim1, size_t dim2, const std::string& tag_string = DEFAULTSTRINGARRAY); DViewFArrayKokkos(T * inp_array, size_t dim0, size_t dim1, size_t dim2, - size_t dim3); + size_t dim3, const std::string& tag_string = DEFAULTSTRINGARRAY); DViewFArrayKokkos(T * inp_array, size_t dim0, size_t dim1, size_t dim2, - size_t dim3, size_t dim4); + size_t dim3, size_t dim4, const std::string& tag_string = DEFAULTSTRINGARRAY); DViewFArrayKokkos(T * inp_array, size_t dim0, size_t dim1, size_t dim2, - size_t dim3, size_t dim4, size_t dim5); + size_t dim3, size_t dim4, size_t dim5, const std::string& tag_string = DEFAULTSTRINGARRAY); DViewFArrayKokkos(T * inp_array, size_t dim0, size_t dim1, size_t dim2, size_t dim3, size_t dim4, size_t dim5, - size_t dim6); + size_t dim6, const std::string& tag_string = DEFAULTSTRINGARRAY); KOKKOS_INLINE_FUNCTION T& operator()(size_t i) const; @@ -2096,7 +2099,7 @@ DViewFArrayKokkos::DViewFArrayKokkos() { // Overloaded 1D constructor template -DViewFArrayKokkos::DViewFArrayKokkos(T * inp_array, size_t dim0) { +DViewFArrayKokkos::DViewFArrayKokkos(T * inp_array, size_t dim0, const std::string& tag_string) { //using TArray1DHost = Kokkos::View; //using TArray1D = Kokkos::View; @@ -2108,14 +2111,14 @@ DViewFArrayKokkos::DViewFArrayKokkos(T * inp_ar // Assign temp point to inp_array pointer that is passed in temp_inp_array_ = inp_array; // Create a device copy of that host view - this_array_ = create_mirror_view_and_copy(ExecSpace(), this_array_host_); + this_array_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_array_host_); // Create host ViewFArray. Note: inp_array and this_array_host_.data() are the same pointer host = ViewFArray (inp_array, dim0); } // Overloaded 2D constructor template -DViewFArrayKokkos::DViewFArrayKokkos(T * inp_array, size_t dim0, size_t dim1) { +DViewFArrayKokkos::DViewFArrayKokkos(T * inp_array, size_t dim0, size_t dim1, const std::string& tag_string) { //using TArray1DHost = Kokkos::View; //using TArray1D = Kokkos::View; //using TArray1Dtemp = TArray1D::HostMirror; @@ -2129,14 +2132,14 @@ DViewFArrayKokkos::DViewFArrayKokkos(T * inp_ar // Assign temp point to inp_array pointer that is passed in temp_inp_array_ = inp_array; // Create a device copy of that host view - this_array_ = create_mirror_view_and_copy(ExecSpace(), this_array_host_); + this_array_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_array_host_); // Create host ViewFArray host = ViewFArray (inp_array, dim0, dim1); } template DViewFArrayKokkos::DViewFArrayKokkos(T * inp_array, size_t dim0, size_t dim1, - size_t dim2) { + size_t dim2, const std::string& tag_string) { //using TArray1D = Kokkos::View; dims_[0] = dim0; @@ -2149,14 +2152,14 @@ DViewFArrayKokkos::DViewFArrayKokkos(T * inp_ar // Assign temp point to inp_array pointer that is passed in temp_inp_array_ = inp_array; // Create a device copy of that host view - this_array_ = create_mirror_view_and_copy(ExecSpace(), this_array_host_); + this_array_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_array_host_); // Create host ViewFArray host = ViewFArray (inp_array, dim0, dim1, dim2); } template DViewFArrayKokkos::DViewFArrayKokkos(T * inp_array, size_t dim0, size_t dim1, - size_t dim2, size_t dim3) { + size_t dim2, size_t dim3, const std::string& tag_string) { //using TArray1D = Kokkos::View; dims_[0] = dim0; @@ -2170,15 +2173,15 @@ DViewFArrayKokkos::DViewFArrayKokkos(T * inp_ar // Assign temp point to inp_array pointer that is passed in temp_inp_array_ = inp_array; // Create a device copy of that host view - this_array_ = create_mirror_view_and_copy(ExecSpace(), this_array_host_); + this_array_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_array_host_); // Create host ViewFArray host = ViewFArray (inp_array, dim0, dim1, dim2, dim3); } template DViewFArrayKokkos::DViewFArrayKokkos(T * inp_array, size_t dim0, size_t dim1, - size_t dim2, size_t dim3, - size_t dim4) { + size_t dim2, size_t dim3, + size_t dim4, const std::string& tag_string) { //using TArray1D = Kokkos::View; @@ -2194,15 +2197,15 @@ DViewFArrayKokkos::DViewFArrayKokkos(T * inp_ar // Assign temp point to inp_array pointer that is passed in temp_inp_array_ = inp_array; // Create a device copy of that host view - this_array_ = create_mirror_view_and_copy(ExecSpace(), this_array_host_); + this_array_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_array_host_); // Create host ViewFArray host = ViewFArray (inp_array, dim0, dim1, dim2, dim3, dim4); } template DViewFArrayKokkos::DViewFArrayKokkos(T * inp_array, size_t dim0, size_t dim1, - size_t dim2, size_t dim3, - size_t dim4, size_t dim5) { + size_t dim2, size_t dim3, + size_t dim4, size_t dim5, const std::string& tag_string) { //using TArray1D = Kokkos::View; dims_[0] = dim0; @@ -2218,7 +2221,7 @@ DViewFArrayKokkos::DViewFArrayKokkos(T * inp_ar // Assign temp point to inp_array pointer that is passed in temp_inp_array_ = inp_array; // Create a device copy of that host view - this_array_ = create_mirror_view_and_copy(ExecSpace(), this_array_host_); + this_array_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_array_host_); // Create host ViewFArray host = ViewFArray (inp_array, dim0, dim1, dim2, dim3, dim4, dim5); } @@ -2227,7 +2230,7 @@ template ::DViewFArrayKokkos(T * inp_array, size_t dim0, size_t dim1, size_t dim2, size_t dim3, size_t dim4, size_t dim5, - size_t dim6) { + size_t dim6, const std::string& tag_string) { //using TArray1D = Kokkos::View; dims_[0] = dim0; @@ -2244,7 +2247,7 @@ DViewFArrayKokkos::DViewFArrayKokkos(T * inp_ar // Assign temp point to inp_array pointer that is passed in temp_inp_array_ = inp_array; // Create a device copy of that host view - this_array_ = create_mirror_view_and_copy(ExecSpace(), this_array_host_); + this_array_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_array_host_); // Create host ViewFArray host = ViewFArray (inp_array, dim0, dim1, dim2, dim3, dim4, dim5, dim6); } @@ -2825,6 +2828,7 @@ class DViewFMatrixKokkos { using TArray1DHost = Kokkos::View; // this is manage using TArray1D = Kokkos::View; + typename ExecSpace::memory_space memspace; private: size_t dims_[7]; @@ -2837,24 +2841,24 @@ class DViewFMatrixKokkos { public: DViewFMatrixKokkos(); - DViewFMatrixKokkos(T * inp_matrix, size_t dim1); + DViewFMatrixKokkos(T * inp_matrix, size_t dim1, const std::string& tag_string = DEFAULTSTRINGMATRIX); - DViewFMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2); + DViewFMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, const std::string& tag_string = DEFAULTSTRINGMATRIX); - DViewFMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, size_t dim3); + DViewFMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, size_t dim3, const std::string& tag_string = DEFAULTSTRINGMATRIX); DViewFMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, size_t dim3, - size_t dim4); + size_t dim4, const std::string& tag_string = DEFAULTSTRINGMATRIX); DViewFMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, size_t dim3, - size_t dim4, size_t dim5); + size_t dim4, size_t dim5, const std::string& tag_string = DEFAULTSTRINGMATRIX); DViewFMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, size_t dim3, - size_t dim4, size_t dim5, size_t dim6); + size_t dim4, size_t dim5, size_t dim6, const std::string& tag_string = DEFAULTSTRINGMATRIX); DViewFMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, size_t dim3, size_t dim4, size_t dim5, size_t dim6, - size_t dim7); + size_t dim7, const std::string& tag_string = DEFAULTSTRINGMATRIX); KOKKOS_INLINE_FUNCTION T& operator()(size_t i) const; @@ -2933,7 +2937,7 @@ DViewFMatrixKokkos::DViewFMatrixKokkos() { // Overloaded 1D constructor template -DViewFMatrixKokkos::DViewFMatrixKokkos(T * inp_matrix, size_t dim1) { +DViewFMatrixKokkos::DViewFMatrixKokkos(T * inp_matrix, size_t dim1, const std::string& tag_string) { dims_[0] = dim1; order_ = 1; @@ -2943,14 +2947,14 @@ DViewFMatrixKokkos::DViewFMatrixKokkos(T * inp_ // Assign temp point to inp_matrix pointer that is passed in temp_inp_matrix_ = inp_matrix; // Create a device copy of that host view - this_matrix_ = create_mirror_view_and_copy(ExecSpace(), this_matrix_host_); + this_matrix_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_matrix_host_); // Create host ViewFMatrix. Note: inp_matrix and this_matrix_host_.data() are the same pointer host = ViewFMatrix (inp_matrix, dim1); } // Overloaded 2D constructor template -DViewFMatrixKokkos::DViewFMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2) { +DViewFMatrixKokkos::DViewFMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, const std::string& tag_string) { dims_[0] = dim1; dims_[1] = dim2; @@ -2961,14 +2965,14 @@ DViewFMatrixKokkos::DViewFMatrixKokkos(T * inp_ // Assign temp point to inp_matrix pointer that is passed in temp_inp_matrix_ = inp_matrix; // Create a device copy of that host view - this_matrix_ = create_mirror_view_and_copy(ExecSpace(), this_matrix_host_); + this_matrix_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_matrix_host_); // Create host ViewFMatrix host = ViewFMatrix (inp_matrix, dim1, dim2); } template DViewFMatrixKokkos::DViewFMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, - size_t dim3) { + size_t dim3, const std::string& tag_string) { dims_[0] = dim1; dims_[1] = dim2; @@ -2980,14 +2984,14 @@ DViewFMatrixKokkos::DViewFMatrixKokkos(T * inp_ // Assign temp point to inp_matrix pointer that is passed in temp_inp_matrix_ = inp_matrix; // Create a device copy of that host view - this_matrix_ = create_mirror_view_and_copy(ExecSpace(), this_matrix_host_); + this_matrix_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_matrix_host_); // Create host ViewFMatrix host = ViewFMatrix (inp_matrix, dim1, dim2, dim3); } template DViewFMatrixKokkos::DViewFMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, - size_t dim3, size_t dim4) { + size_t dim3, size_t dim4, const std::string& tag_string) { dims_[0] = dim1; dims_[1] = dim2; @@ -3000,15 +3004,15 @@ DViewFMatrixKokkos::DViewFMatrixKokkos(T * inp_ // Assign temp point to inp_matrix pointer that is passed in temp_inp_matrix_ = inp_matrix; // Create a device copy of that host view - this_matrix_ = create_mirror_view_and_copy(ExecSpace(), this_matrix_host_); + this_matrix_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_matrix_host_); // Create host ViewFMatrix host = ViewFMatrix (inp_matrix, dim1, dim2, dim3, dim4); } template DViewFMatrixKokkos::DViewFMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, - size_t dim3, size_t dim4, - size_t dim5) { + size_t dim3, size_t dim4, + size_t dim5, const std::string& tag_string) { dims_[0] = dim1; dims_[1] = dim2; @@ -3022,15 +3026,15 @@ DViewFMatrixKokkos::DViewFMatrixKokkos(T * inp_ // Assign temp point to inp_matrix pointer that is passed in temp_inp_matrix_ = inp_matrix; // Create a device copy of that host view - this_matrix_ = create_mirror_view_and_copy(ExecSpace(), this_matrix_host_); + this_matrix_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_matrix_host_); // Create host ViewFMatrix host = ViewFMatrix (inp_matrix, dim1, dim2, dim3, dim4, dim5); } template DViewFMatrixKokkos::DViewFMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, - size_t dim3, size_t dim4, - size_t dim5, size_t dim6) { + size_t dim3, size_t dim4, + size_t dim5, size_t dim6, const std::string& tag_string) { dims_[0] = dim1; dims_[1] = dim2; @@ -3045,7 +3049,7 @@ DViewFMatrixKokkos::DViewFMatrixKokkos(T * inp_ // Assign temp point to inp_matrix pointer that is passed in temp_inp_matrix_ = inp_matrix; // Create a device copy of that host view - this_matrix_ = create_mirror_view_and_copy(ExecSpace(), this_matrix_host_); + this_matrix_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_matrix_host_); // Create host ViewFMatrix host = ViewFMatrix (inp_matrix, dim1, dim2, dim3, dim4, dim5, dim6); } @@ -3054,7 +3058,7 @@ template ::DViewFMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, size_t dim3, size_t dim4, size_t dim5, size_t dim6, - size_t dim7) { + size_t dim7, const std::string& tag_string) { dims_[0] = dim1; dims_[1] = dim2; @@ -3070,7 +3074,7 @@ DViewFMatrixKokkos::DViewFMatrixKokkos(T * inp_ // Assign temp point to inp_matrix pointer that is passed in temp_inp_matrix_ = inp_matrix; // Create a device copy of that host view - this_matrix_ = create_mirror_view_and_copy(ExecSpace(), this_matrix_host_); + this_matrix_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_matrix_host_); // Create host ViewFMatrix host = ViewFMatrix (inp_matrix, dim1, dim2, dim3, dim4, dim5, dim6, dim7); } @@ -3185,7 +3189,7 @@ DViewFMatrixKokkos& DViewFMatrixKokkos; // this is manage using TArray1D = Kokkos::View; + typename ExecSpace::memory_space memspace; private: size_t dims_[7]; @@ -5073,24 +5078,24 @@ class DViewCArrayKokkos { public: DViewCArrayKokkos(); - DViewCArrayKokkos(T * inp_array, size_t dim0); + DViewCArrayKokkos(T * inp_array, size_t dim0, const std::string& tag_string = DEFAULTSTRINGARRAY); - DViewCArrayKokkos(T * inp_array, size_t dim0, size_t dim1); + DViewCArrayKokkos(T * inp_array, size_t dim0, size_t dim1, const std::string& tag_string = DEFAULTSTRINGARRAY); - DViewCArrayKokkos(T * inp_array, size_t dim0, size_t dim1, size_t dim2); + DViewCArrayKokkos(T * inp_array, size_t dim0, size_t dim1, size_t dim2, const std::string& tag_string = DEFAULTSTRINGARRAY); DViewCArrayKokkos(T * inp_array, size_t dim0, size_t dim1, size_t dim2, - size_t dim3); + size_t dim3, const std::string& tag_string = DEFAULTSTRINGARRAY); DViewCArrayKokkos(T * inp_array, size_t dim0, size_t dim1, size_t dim2, - size_t dim3, size_t dim4); + size_t dim3, size_t dim4, const std::string& tag_string = DEFAULTSTRINGARRAY); DViewCArrayKokkos(T * inp_array, size_t dim0, size_t dim1, size_t dim2, - size_t dim3, size_t dim4, size_t dim5); + size_t dim3, size_t dim4, size_t dim5, const std::string& tag_string = DEFAULTSTRINGARRAY); DViewCArrayKokkos(T * inp_array, size_t dim0, size_t dim1, size_t dim2, size_t dim3, size_t dim4, size_t dim5, - size_t dim6); + size_t dim6, const std::string& tag_string = DEFAULTSTRINGARRAY); KOKKOS_INLINE_FUNCTION T& operator()(size_t i) const; @@ -5108,7 +5113,7 @@ class DViewCArrayKokkos { T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m) const; KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m, + T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m, size_t n) const; KOKKOS_INLINE_FUNCTION @@ -5169,7 +5174,7 @@ DViewCArrayKokkos::DViewCArrayKokkos() { // Overloaded 1D constructor template -DViewCArrayKokkos::DViewCArrayKokkos(T * inp_array, size_t dim0) { +DViewCArrayKokkos::DViewCArrayKokkos(T * inp_array, size_t dim0, const std::string& tag_string) { //using TArray1DHost = Kokkos::View; //using TArray1D = Kokkos::View; @@ -5181,14 +5186,16 @@ DViewCArrayKokkos::DViewCArrayKokkos(T * inp_ar // Assign temp point to inp_array pointer that is passed in temp_inp_array_ = inp_array; // Create a device copy of that host view - this_array_ = create_mirror_view_and_copy(ExecSpace(), this_array_host_); - // Create host ViewCArray. Note: inp_array and this_array_host_.data() are the same pointer + //this_array_ = create_mirror_view_and_copy(ExecSpace(), this_array_host_); + this_array_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_array_host_); + + // Create host ViewCArray. Note: inp_array and this_array_host_.data() are the same pointer host = ViewCArray (inp_array, dim0); } // Overloaded 2D constructor template -DViewCArrayKokkos::DViewCArrayKokkos(T * inp_array, size_t dim0, size_t dim1) { +DViewCArrayKokkos::DViewCArrayKokkos(T * inp_array, size_t dim0, size_t dim1, const std::string& tag_string) { //using TArray1DHost = Kokkos::View; //using TArray1D = Kokkos::View; //using TArray1Dtemp = TArray1D::HostMirror; @@ -5201,15 +5208,15 @@ DViewCArrayKokkos::DViewCArrayKokkos(T * inp_ar this_array_host_ = TArray1DHost(inp_array, length_); // Assign temp point to inp_array pointer that is passed in temp_inp_array_ = inp_array; - // Create a device copy of that host view - this_array_ = create_mirror_view_and_copy(ExecSpace(), this_array_host_); + //this_array_ = create_mirror_view_and_copy(ExecSpace(), this_array_host_); + this_array_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_array_host_); // Create host ViewCArray host = ViewCArray (inp_array, dim0, dim1); } template DViewCArrayKokkos::DViewCArrayKokkos(T * inp_array, size_t dim0, size_t dim1, - size_t dim2) { + size_t dim2, const std::string& tag_string) { //using TArray1D = Kokkos::View; dims_[0] = dim0; @@ -5222,14 +5229,15 @@ DViewCArrayKokkos::DViewCArrayKokkos(T * inp_ar // Assign temp point to inp_array pointer that is passed in temp_inp_array_ = inp_array; // Create a device copy of that host view - this_array_ = create_mirror_view_and_copy(ExecSpace(), this_array_host_); + //this_array_ = create_mirror_view_and_copy(ExecSpace(), this_array_host_); + this_array_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_array_host_); // Create host ViewCArray host = ViewCArray (inp_array, dim0, dim1, dim2); } template DViewCArrayKokkos::DViewCArrayKokkos(T * inp_array, size_t dim0, size_t dim1, - size_t dim2, size_t dim3) { + size_t dim2, size_t dim3, const std::string& tag_string) { //using TArray1D = Kokkos::View; dims_[0] = dim0; @@ -5243,15 +5251,16 @@ DViewCArrayKokkos::DViewCArrayKokkos(T * inp_ar // Assign temp point to inp_array pointer that is passed in temp_inp_array_ = inp_array; // Create a device copy of that host view - this_array_ = create_mirror_view_and_copy(ExecSpace(), this_array_host_); + //this_array_ = create_mirror_view_and_copy(ExecSpace(), this_array_host_); + this_array_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_array_host_); // Create host ViewCArray host = ViewCArray (inp_array, dim0, dim1, dim2, dim3); } template DViewCArrayKokkos::DViewCArrayKokkos(T * inp_array, size_t dim0, size_t dim1, - size_t dim2, size_t dim3, - size_t dim4) { + size_t dim2, size_t dim3, + size_t dim4, const std::string& tag_string) { //using TArray1D = Kokkos::View; @@ -5267,15 +5276,16 @@ DViewCArrayKokkos::DViewCArrayKokkos(T * inp_ar // Assign temp point to inp_array pointer that is passed in temp_inp_array_ = inp_array; // Create a device copy of that host view - this_array_ = create_mirror_view_and_copy(ExecSpace(), this_array_host_); + //this_array_ = create_mirror_view_and_copy(ExecSpace(), this_array_host_); + this_array_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_array_host_); // Create host ViewCArray host = ViewCArray (inp_array, dim0, dim1, dim2, dim3, dim4); } template DViewCArrayKokkos::DViewCArrayKokkos(T * inp_array, size_t dim0, size_t dim1, - size_t dim2, size_t dim3, - size_t dim4, size_t dim5) { + size_t dim2, size_t dim3, + size_t dim4, size_t dim5, const std::string& tag_string) { //using TArray1D = Kokkos::View; dims_[0] = dim0; @@ -5291,7 +5301,8 @@ DViewCArrayKokkos::DViewCArrayKokkos(T * inp_ar // Assign temp point to inp_array pointer that is passed in temp_inp_array_ = inp_array; // Create a device copy of that host view - this_array_ = create_mirror_view_and_copy(ExecSpace(), this_array_host_); + //this_array_ = create_mirror_view_and_copy(ExecSpace(), this_array_host_); + this_array_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_array_host_); // Create host ViewCArray host = ViewCArray (inp_array, dim0, dim1, dim2, dim3, dim4, dim5); } @@ -5300,7 +5311,7 @@ template ::DViewCArrayKokkos(T * inp_array, size_t dim0, size_t dim1, size_t dim2, size_t dim3, size_t dim4, size_t dim5, - size_t dim6) { + size_t dim6, const std::string& tag_string) { //using TArray1D = Kokkos::View; dims_[0] = dim0; @@ -5316,8 +5327,8 @@ DViewCArrayKokkos::DViewCArrayKokkos(T * inp_ar this_array_host_ = TArray1DHost(inp_array, length_); // Assign temp point to inp_array pointer that is passed in temp_inp_array_ = inp_array; - // Create a device copy of that host view - this_array_ = create_mirror_view_and_copy(ExecSpace(), this_array_host_); + //this_array_ = create_mirror_view_and_copy(ExecSpace(), this_array_host_); + this_array_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_array_host_); // Create host ViewCArray host = ViewCArray (inp_array, dim0, dim1, dim2, dim3, dim4, dim5, dim6); } @@ -5900,6 +5911,7 @@ class DViewCMatrixKokkos { using TArray1DHost = Kokkos::View; // this is manage using TArray1D = Kokkos::View; + typename ExecSpace::memory_space memspace; private: size_t dims_[7]; @@ -5912,24 +5924,24 @@ class DViewCMatrixKokkos { public: DViewCMatrixKokkos(); - DViewCMatrixKokkos(T * inp_matrix, size_t dim1); + DViewCMatrixKokkos(T * inp_matrix, size_t dim1, const std::string& tag_string = DEFAULTSTRINGMATRIX); - DViewCMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2); + DViewCMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, const std::string& tag_string = DEFAULTSTRINGMATRIX); - DViewCMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, size_t dim3); + DViewCMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, size_t dim3, const std::string& tag_string = DEFAULTSTRINGMATRIX); DViewCMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, size_t dim3, - size_t dim4); + size_t dim4, const std::string& tag_string = DEFAULTSTRINGMATRIX); DViewCMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, size_t dim3, - size_t dim4, size_t dim5); + size_t dim4, size_t dim5, const std::string& tag_string = DEFAULTSTRINGMATRIX); DViewCMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, size_t dim3, - size_t dim4, size_t dim5, size_t dim6); + size_t dim4, size_t dim5, size_t dim6, const std::string& tag_string = DEFAULTSTRINGMATRIX); DViewCMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, size_t dim3, size_t dim4, size_t dim5, size_t dim6, - size_t dim7); + size_t dim7, const std::string& tag_string = DEFAULTSTRINGMATRIX); KOKKOS_INLINE_FUNCTION T& operator()(size_t i) const; @@ -6008,7 +6020,7 @@ DViewCMatrixKokkos::DViewCMatrixKokkos() { // Overloaded 1D constructor template -DViewCMatrixKokkos::DViewCMatrixKokkos(T * inp_matrix, size_t dim1) { +DViewCMatrixKokkos::DViewCMatrixKokkos(T * inp_matrix, size_t dim1, const std::string& tag_string) { dims_[0] = dim1; order_ = 1; @@ -6018,14 +6030,14 @@ DViewCMatrixKokkos::DViewCMatrixKokkos(T * inp_ // Assign temp point to inp_matrix pointer that is passed in temp_inp_matrix_ = inp_matrix; // Create a device copy of that host view - this_matrix_ = create_mirror_view_and_copy(ExecSpace(), this_matrix_host_); + this_matrix_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_matrix_host_); // Create host ViewCMatrix. Note: inp_matrix and this_matrix_host_.data() are the same pointer host = ViewCMatrix (inp_matrix, dim1); } // Overloaded 2D constructor template -DViewCMatrixKokkos::DViewCMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2) { +DViewCMatrixKokkos::DViewCMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, const std::string& tag_string) { dims_[0] = dim1; dims_[1] = dim2; @@ -6036,14 +6048,14 @@ DViewCMatrixKokkos::DViewCMatrixKokkos(T * inp_ // Assign temp point to inp_matrix pointer that is passed in temp_inp_matrix_ = inp_matrix; // Create a device copy of that host view - this_matrix_ = create_mirror_view_and_copy(ExecSpace(), this_matrix_host_); + this_matrix_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_matrix_host_); // Create host ViewCMatrix host = ViewCMatrix (inp_matrix, dim1, dim2); } template DViewCMatrixKokkos::DViewCMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, - size_t dim3) { + size_t dim3, const std::string& tag_string) { dims_[0] = dim1; dims_[1] = dim2; @@ -6055,14 +6067,14 @@ DViewCMatrixKokkos::DViewCMatrixKokkos(T * inp_ // Assign temp point to inp_matrix pointer that is passed in temp_inp_matrix_ = inp_matrix; // Create a device copy of that host view - this_matrix_ = create_mirror_view_and_copy(ExecSpace(), this_matrix_host_); + this_matrix_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_matrix_host_); // Create host ViewCMatrix host = ViewCMatrix (inp_matrix, dim1, dim2, dim3); } template DViewCMatrixKokkos::DViewCMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, - size_t dim3, size_t dim4) { + size_t dim3, size_t dim4, const std::string& tag_string) { dims_[0] = dim1; dims_[1] = dim2; @@ -6075,15 +6087,15 @@ DViewCMatrixKokkos::DViewCMatrixKokkos(T * inp_ // Assign temp point to inp_matrix pointer that is passed in temp_inp_matrix_ = inp_matrix; // Create a device copy of that host view - this_matrix_ = create_mirror_view_and_copy(ExecSpace(), this_matrix_host_); + this_matrix_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_matrix_host_); // Create host ViewCMatrix host = ViewCMatrix (inp_matrix, dim1, dim2, dim3, dim4); } template DViewCMatrixKokkos::DViewCMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, - size_t dim3, size_t dim4, - size_t dim5) { + size_t dim3, size_t dim4, + size_t dim5, const std::string& tag_string) { dims_[0] = dim1; dims_[1] = dim2; @@ -6097,15 +6109,15 @@ DViewCMatrixKokkos::DViewCMatrixKokkos(T * inp_ // Assign temp point to inp_matrix pointer that is passed in temp_inp_matrix_ = inp_matrix; // Create a device copy of that host view - this_matrix_ = create_mirror_view_and_copy(ExecSpace(), this_matrix_host_); + this_matrix_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_matrix_host_); // Create host ViewCMatrix host = ViewCMatrix (inp_matrix, dim1, dim2, dim3, dim4, dim5); } template DViewCMatrixKokkos::DViewCMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, - size_t dim3, size_t dim4, - size_t dim5, size_t dim6) { + size_t dim3, size_t dim4, + size_t dim5, size_t dim6, const std::string& tag_string) { dims_[0] = dim1; dims_[1] = dim2; @@ -6120,7 +6132,7 @@ DViewCMatrixKokkos::DViewCMatrixKokkos(T * inp_ // Assign temp point to inp_matrix pointer that is passed in temp_inp_matrix_ = inp_matrix; // Create a device copy of that host view - this_matrix_ = create_mirror_view_and_copy(ExecSpace(), this_matrix_host_); + this_matrix_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_matrix_host_); // Create host ViewCMatrix host = ViewCMatrix (inp_matrix, dim1, dim2, dim3, dim4, dim5, dim6); } @@ -6129,7 +6141,7 @@ template ::DViewCMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, size_t dim3, size_t dim4, size_t dim5, size_t dim6, - size_t dim7) { + size_t dim7, const std::string& tag_string) { dims_[0] = dim1; dims_[1] = dim2; @@ -6145,7 +6157,7 @@ DViewCMatrixKokkos::DViewCMatrixKokkos(T * inp_ // Assign temp point to inp_matrix pointer that is passed in temp_inp_matrix_ = inp_matrix; // Create a device copy of that host view - this_matrix_ = create_mirror_view_and_copy(ExecSpace(), this_matrix_host_); + this_matrix_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_matrix_host_); // Create host ViewCMatrix host = ViewCMatrix (inp_matrix, dim1, dim2, dim3, dim4, dim5, dim6, dim7); } @@ -7023,7 +7035,7 @@ T& RaggedRightArrayofVectorsKokkos::ope // asserts assert(i < dim1_ && "i is out of dim1 bounds in RaggedRightArrayKokkos"); // die if >= dim1 assert(j < stride(i) && "j is out of stride bounds in RaggedRightArrayKokkos"); // die if >= stride - assert(j < vector_dim_ && "k is out of vector_dim bounds in RaggedRightArrayKokkos"); // die if >= vector_dim + assert(k < vector_dim_ && "k is out of vector_dim bounds in RaggedRightArrayKokkos"); // die if >= vector_dim return array_(j*vector_dim_ + start + k); } // End operator() @@ -7510,7 +7522,7 @@ DynamicRaggedRightArrayKokkos::DynamicRaggedRig stride_ = SArray1D(strides_tag_string, dim1_); #ifdef HAVE_CLASS_LAMBDA Kokkos::parallel_for("StridesInit", dim1_, KOKKOS_CLASS_LAMBDA(const int i) { - strides_((i) = 0; + strides_(i) = 0; }); #else set_strides_functor execution_functor(0, stride_); @@ -7687,7 +7699,7 @@ DynamicRaggedDownArrayKokkos::DynamicRaggedDown stride_ = SArray1D(strides_tag_string, dim2_); #ifdef HAVE_CLASS_LAMBDA Kokkos::parallel_for("StridesInit", dim2_, KOKKOS_CLASS_LAMBDA(const int i) { - strides_((i) = 0; + strides_(i) = 0; }); #else set_strides_functor execution_functor(0, stride_); diff --git a/src/include/macros.h b/src/include/macros.h index aa3a2bd1..4b40204d 100644 --- a/src/include/macros.h +++ b/src/include/macros.h @@ -463,6 +463,16 @@ Kokkos::parallel_for( \ Kokkos::ThreadVectorRange( teamMember, z0, z1 ), [&] ( const int (k) ) \ {fcn} ) +//Kokkos Initialize +#define \ + MATAR_KOKKOS_INIT \ + Kokkos::initialize(argc, argv); + +//Kokkos Finalize +#define \ + MATAR_KOKKOS_FINALIZE \ + Kokkos::finalize(); + #endif @@ -983,6 +993,29 @@ void reduce_max (int i_start, int i_end, #endif // if not kokkos +#ifdef HAVE_MPI + +// MPI Init +#define \ + MATAR_MPI_INIT \ + MPI_Init(&argc, &argv); + +// MPI Finalize +#define \ + MATAR_MPI_FINALIZE \ + MPI_Finalize(); + +// MPI Wall time +#define \ + MATAR_MPI_TIME \ + MPI_Wtime(); + +// MPI Barrier +#define \ + MATAR_MPI_BARRIER \ + MPI_Barrier(MPI_COMM_WORLD); + +#endif #endif // MACROS_H diff --git a/src/include/matar.h b/src/include/matar.h index 7566ded2..51b8eb6c 100644 --- a/src/include/matar.h +++ b/src/include/matar.h @@ -81,6 +81,7 @@ #include "host_types.h" #include "kokkos_types.h" #include "aliases.h" +#include "mpi_types.h" diff --git a/src/include/mpi_types.h b/src/include/mpi_types.h new file mode 100644 index 00000000..fe1258b0 --- /dev/null +++ b/src/include/mpi_types.h @@ -0,0 +1,784 @@ +#ifndef MPI_TYPES_H +#define MPI_TYPES_H +/********************************************************************************************** + © 2020. Triad National Security, LLC. All rights reserved. + This program was produced under U.S. Government contract 89233218CNA000001 for Los Alamos + National Laboratory (LANL), which is operated by Triad National Security, LLC for the U.S. + Department of Energy/National Nuclear Security Administration. All rights in the program are + reserved by Triad National Security, LLC, and the U.S. Department of Energy/National Nuclear + Security Administration. The Government is granted for itself and others acting on its behalf a + nonexclusive, paid-up, irrevocable worldwide license in this material to reproduce, prepare + derivative works, distribute copies to the public, perform publicly and display publicly, and + to permit others to do so. + This program is open source under the BSD-3 License. + Redistribution and use in source and binary forms, with or without modification, are permitted + provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, this list of + conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright notice, this list of + conditions and the following disclaimer in the documentation and/or other materials + provided with the distribution. + + 3. Neither the name of the copyright holder nor the names of its contributors may be used + to endorse or promote products derived from this software without specific prior + written permission. + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + **********************************************************************************************/ + +#include "host_types.h" +#include "kokkos_types.h" +#include +#ifdef HAVE_MPI +#include + +namespace mtr +{ + +///////////////////////// +// MPIArrayKokkos: Dual type for managing data on both CPU and GPU. +///////////////////////// +template +class MPIArrayKokkos { + + // this is manage + using TArray1D = Kokkos::DualView ; + +private: + size_t dims_[7]; + size_t length_; + size_t order_; // tensor order (rank) + int mpi_recv_rank_; + int mpi_tag_; + MPI_Comm mpi_comm_; + MPI_Status mpi_status_; + MPI_Datatype mpi_datatype_; + MPI_Request mpi_request_; + TArray1D this_array_; + + void set_mpi_type(); + +public: + // Data member to access host view + ViewCArray host; + + MPIArrayKokkos(); + + MPIArrayKokkos(size_t dim0, const std::string& tag_string = DEFAULTSTRINGARRAY); + + MPIArrayKokkos(size_t dim0, size_t dim1, const std::string& tag_string = DEFAULTSTRINGARRAY); + + MPIArrayKokkos(size_t dim0, size_t dim1, size_t dim2, const std::string& tag_string = DEFAULTSTRINGARRAY); + + MPIArrayKokkos(size_t dim0, size_t dim1, size_t dim2, + size_t dim3, const std::string& tag_string = DEFAULTSTRINGARRAY); + + MPIArrayKokkos(size_t dim0, size_t dim1, size_t dim2, + size_t dim3, size_t dim4, const std::string& tag_string = DEFAULTSTRINGARRAY); + + MPIArrayKokkos(size_t dim0, size_t dim1, size_t dim2, + size_t dim3, size_t dim4, size_t dim5, const std::string& tag_string = DEFAULTSTRINGARRAY); + + MPIArrayKokkos(size_t dim0, size_t dim1, size_t dim2, + size_t dim3, size_t dim4, size_t dim5, + size_t dim6, const std::string& tag_string = DEFAULTSTRINGARRAY); + + // These functions can setup the data needed for halo send/receives + // Not necessary for standard MPI comms + void mpi_setup(); + + void mpi_setup(int recv_rank); + + void mpi_setup(int recv_rank, int tag); + + void mpi_setup(int recv_rank, int tag, MPI_Comm comm); + + void mpi_set_rank(int recv_rank); + + void mpi_set_tag(int tag); + + void mpi_set_comm(MPI_Comm comm); + + int get_rank(); + + int get_tag(); + + MPI_Comm get_comm(); + + KOKKOS_INLINE_FUNCTION + T& operator()(size_t i) const; + + KOKKOS_INLINE_FUNCTION + T& operator()(size_t i, size_t j) const; + + KOKKOS_INLINE_FUNCTION + T& operator()(size_t i, size_t j, size_t k) const; + + KOKKOS_INLINE_FUNCTION + T& operator()(size_t i, size_t j, size_t k, size_t l) const; + + KOKKOS_INLINE_FUNCTION + T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m) const; + + KOKKOS_INLINE_FUNCTION + T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m, + size_t n) const; + + KOKKOS_INLINE_FUNCTION + T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m, + size_t n, size_t o) const; + + KOKKOS_INLINE_FUNCTION + MPIArrayKokkos& operator=(const MPIArrayKokkos& temp); + + // GPU Method + // Method that returns size + KOKKOS_INLINE_FUNCTION + size_t size() const; + + // Host Method + // Method that returns size + KOKKOS_INLINE_FUNCTION + size_t extent() const; + + KOKKOS_INLINE_FUNCTION + size_t dims(size_t i) const; + + KOKKOS_INLINE_FUNCTION + size_t order() const; + + // Method returns the raw device pointer of the Kokkos DualView + KOKKOS_INLINE_FUNCTION + T* device_pointer() const; + + // Method returns the raw host pointer of the Kokkos DualView + KOKKOS_INLINE_FUNCTION + T* host_pointer() const; + + // Method returns kokkos dual view + KOKKOS_INLINE_FUNCTION + TArray1D get_kokkos_dual_view() const; + + // Method that update host view + void update_host(); + + // Method that update device view + void update_device(); + + // MPI send wrapper + void send(size_t count, int dest, int tag, MPI_Comm comm); + + // MPI recieve wrapper + void recv(size_t count, int dest, int tag, MPI_Comm comm); + + // MPI broadcast wrapper + void broadcast(size_t count, int root, MPI_Comm comm); + + // MPI scatter wrapper + void scatter(size_t send_count, MPIArrayKokkos recv_buffer, size_t recv_count, int root, MPI_Comm comm); + + // MPI gather wrapper + void gather(size_t send_count, MPIArrayKokkos recv_buffer, size_t recv_count, int root, MPI_Comm comm); + + // MPI allgather wrapper + void allgather(size_t send_count, MPIArrayKokkos recv_buffer, size_t recv_count, MPI_Comm comm); + + // MPI send wrapper + void isend(size_t count, int dest, int tag, MPI_Comm comm); + + // MPI recieve wrapper + void irecv(size_t count, int dest, int tag, MPI_Comm comm); + + // MPI wait wrapper for sender + void wait_send(); + + // MPI wait wrapper for receiver + void wait_recv(); + + // MPI barrier wrapper + //void barrier(MPI_Comm comm); + + // MPI send wrapper + void halo_send(); + + // MPI recieve wrapper + void halo_recv(); + + // MPI send wrapper + void halo_isend(); + + // MPI recieve wrapper + void halo_irecv(); + + // Deconstructor + KOKKOS_INLINE_FUNCTION + ~MPIArrayKokkos (); +}; // End of MPIArrayKokkos + + +// Default constructor +template +MPIArrayKokkos::MPIArrayKokkos() { + length_ = order_ = 0; + for (int i = 0; i < 7; i++) { + dims_[i] = 0; + } +} + +// Overloaded 1D constructor +template +MPIArrayKokkos::MPIArrayKokkos(size_t dim0, const std::string& tag_string) { + + dims_[0] = dim0; + order_ = 1; + length_ = dim0; + this_array_ = TArray1D(tag_string, length_); + // Create host ViewCArray + host = ViewCArray (this_array_.h_view.data(), dim0); + set_mpi_type(); +} + +// Overloaded 2D constructor +template +MPIArrayKokkos::MPIArrayKokkos(size_t dim0, size_t dim1, const std::string& tag_string) { + + dims_[0] = dim0; + dims_[1] = dim1; + order_ = 2; + length_ = (dim0 * dim1); + this_array_ = TArray1D(tag_string, length_); + // Create host ViewCArray + host = ViewCArray (this_array_.h_view.data(), dim0, dim1); + set_mpi_type(); +} + +template +MPIArrayKokkos::MPIArrayKokkos(size_t dim0, size_t dim1, + size_t dim2, const std::string& tag_string) { + + dims_[0] = dim0; + dims_[1] = dim1; + dims_[2] = dim2; + order_ = 3; + length_ = (dim0 * dim1 * dim2); + this_array_ = TArray1D(tag_string, length_); + // Create host ViewCArray + host = ViewCArray (this_array_.h_view.data(), dim0, dim1, dim2); + set_mpi_type(); +} + +template +MPIArrayKokkos::MPIArrayKokkos(size_t dim0, size_t dim1, + size_t dim2, size_t dim3, const std::string& tag_string) { + + dims_[0] = dim0; + dims_[1] = dim1; + dims_[2] = dim2; + dims_[3] = dim3; + order_ = 4; + length_ = (dim0 * dim1 * dim2 * dim3); + this_array_ = TArray1D(tag_string, length_); + // Create host ViewCArray + host = ViewCArray (this_array_.h_view.data(), dim0, dim1, dim2, dim3); + set_mpi_type(); +} + +template +MPIArrayKokkos::MPIArrayKokkos(size_t dim0, size_t dim1, + size_t dim2, size_t dim3, + size_t dim4, const std::string& tag_string) { + + dims_[0] = dim0; + dims_[1] = dim1; + dims_[2] = dim2; + dims_[3] = dim3; + dims_[4] = dim4; + order_ = 5; + length_ = (dim0 * dim1 * dim2 * dim3 * dim4); + this_array_ = TArray1D(tag_string, length_); + // Create host ViewCArray + host = ViewCArray (this_array_.h_view.data(), dim0, dim1, dim2, dim3, dim4); + set_mpi_type(); +} + +template +MPIArrayKokkos::MPIArrayKokkos(size_t dim0, size_t dim1, + size_t dim2, size_t dim3, + size_t dim4, size_t dim5, const std::string& tag_string) { + + dims_[0] = dim0; + dims_[1] = dim1; + dims_[2] = dim2; + dims_[3] = dim3; + dims_[4] = dim4; + dims_[5] = dim5; + order_ = 6; + length_ = (dim0 * dim1 * dim2 * dim3 * dim4 * dim5); + this_array_ = TArray1D(tag_string, length_); + // Create host ViewCArray + host = ViewCArray (this_array_.h_view.data(), dim0, dim1, dim2, dim3, dim4, dim5); + set_mpi_type(); +} + +template +MPIArrayKokkos::MPIArrayKokkos(size_t dim0, size_t dim1, + size_t dim2, size_t dim3, + size_t dim4, size_t dim5, + size_t dim6, const std::string& tag_string) { + + dims_[0] = dim0; + dims_[1] = dim1; + dims_[2] = dim2; + dims_[3] = dim3; + dims_[4] = dim4; + dims_[5] = dim5; + dims_[6] = dim6; + order_ = 7; + length_ = (dim0 * dim1 * dim2 * dim3 * dim4 * dim5 * dim6); + this_array_ = TArray1D(tag_string, length_); + // Create host ViewCArray + host = ViewCArray (this_array_.h_view.data(), dim0, dim1, dim2, dim3, dim4, dim5, dim6); + set_mpi_type(); +} + +template +void MPIArrayKokkos::set_mpi_type() { + if (typeid(T).name() == typeid(bool).name()) { + mpi_datatype_ = MPI_C_BOOL; + } + else if (typeid(T).name() == typeid(int).name()) { + mpi_datatype_ = MPI_INT; + } + else if (typeid(T).name() == typeid(long int).name()) { + mpi_datatype_ = MPI_LONG; + } + else if (typeid(T).name() == typeid(long long int).name()) { + mpi_datatype_ = MPI_LONG_LONG_INT; + } + else if (typeid(T).name() == typeid(float).name()) { + mpi_datatype_ = MPI_FLOAT; + } + else if (typeid(T).name() == typeid(double).name()) { + mpi_datatype_ = MPI_DOUBLE; + } + else { + printf("Your entered MPIArrayKokkos type is not a supported type for MPI communications and is being set to int\n"); + mpi_datatype_ = MPI_INT; + } +} + +template +KOKKOS_INLINE_FUNCTION +T& MPIArrayKokkos::operator()(size_t i) const { + assert(order_ == 1 && "Tensor order (rank) does not match constructor in MPIArrayKokkos 1D!"); + assert(i >= 0 && i < dims_[0] && "i is out of bounds in MPIArrayKokkos 1D!"); + return this_array_.d_view(i); +} + +template +KOKKOS_INLINE_FUNCTION +T& MPIArrayKokkos::operator()(size_t i, size_t j) const { + assert(order_ == 2 && "Tensor order (rank) does not match constructor in MPIArrayKokkos 2D!"); + assert(i >= 0 && i < dims_[0] && "i is out of bounds in MPIArrayKokkos 2D!"); + assert(j >= 0 && j < dims_[1] && "j is out of bounds in MPIArrayKokkos 2D!"); + return this_array_.d_view(j + (i * dims_[1])); +} + +template +KOKKOS_INLINE_FUNCTION +T& MPIArrayKokkos::operator()(size_t i, size_t j, size_t k) const { + assert(order_ == 3 && "Tensor order (rank) does not match constructor in MPIArrayKokkos 3D!"); + assert(i >= 0 && i < dims_[0] && "i is out of bounds in MPIArrayKokkos 3D!"); + assert(j >= 0 && j < dims_[1] && "j is out of bounds in MPIArrayKokkos 3D!"); + assert(k >= 0 && k < dims_[2] && "k is out of bounds in MPIArrayKokkos 3D!"); + return this_array_.d_view(k + (j * dims_[2]) + + (i * dims_[2] * dims_[1])); +} + +template +KOKKOS_INLINE_FUNCTION +T& MPIArrayKokkos::operator()(size_t i, size_t j, size_t k, size_t l) const { + assert(order_ == 4 && "Tensor order (rank) does not match constructor in MPIArrayKokkos 4D!"); + assert(i >= 0 && i < dims_[0] && "i is out of bounds in MPIArrayKokkos 4D!"); + assert(j >= 0 && j < dims_[1] && "j is out of bounds in MPIArrayKokkos 4D!"); + assert(k >= 0 && k < dims_[2] && "k is out of bounds in MPIArrayKokkos 4D!"); + assert(l >= 0 && l < dims_[3] && "l is out of bounds in MPIArrayKokkos 4D!"); + return this_array_.d_view(l + (k * dims_[3]) + + (j * dims_[3] * dims_[2]) + + (i * dims_[3] * dims_[2] * dims_[1])); +} + +template +KOKKOS_INLINE_FUNCTION +T& MPIArrayKokkos::operator()(size_t i, size_t j, size_t k, size_t l, + size_t m) const { + assert(order_ == 5 && "Tensor order (rank) does not match constructor in MPIArrayKokkos 5D!"); + assert(i >= 0 && i < dims_[0] && "i is out of bounds in MPIArrayKokkos 5D!"); + assert(j >= 0 && j < dims_[1] && "j is out of bounds in MPIArrayKokkos 5D!"); + assert(k >= 0 && k < dims_[2] && "k is out of bounds in MPIArrayKokkos 5D!"); + assert(l >= 0 && l < dims_[3] && "l is out of bounds in MPIArrayKokkos 5D!"); + assert(m >= 0 && m < dims_[4] && "m is out of bounds in MPIArrayKokkos 5D!"); + return this_array_.d_view(m + (l * dims_[4]) + + (k * dims_[4] * dims_[3]) + + (j * dims_[4] * dims_[3] * dims_[2]) + + (i * dims_[4] * dims_[3] * dims_[2] * dims_[1])); +} + +template +KOKKOS_INLINE_FUNCTION +T& MPIArrayKokkos::operator()(size_t i, size_t j, size_t k, size_t l, + size_t m, size_t n) const { + assert(order_ == 6 && "Tensor order (rank) does not match constructor in MPIArrayKokkos 6D!"); + assert(i >= 0 && i < dims_[0] && "i is out of bounds in MPIArrayKokkos 6D!"); + assert(j >= 0 && j < dims_[1] && "j is out of bounds in MPIArrayKokkos 6D!"); + assert(k >= 0 && k < dims_[2] && "k is out of bounds in MPIArrayKokkos 6D!"); + assert(l >= 0 && l < dims_[3] && "l is out of bounds in MPIArrayKokkos 6D!"); + assert(m >= 0 && m < dims_[4] && "m is out of bounds in MPIArrayKokkos 6D!"); + assert(n >= 0 && n < dims_[5] && "n is out of bounds in MPIArrayKokkos 6D!"); + return this_array_.d_view(n + (m * dims_[5]) + + (l * dims_[5] * dims_[4]) + + (k * dims_[5] * dims_[4] * dims_[3]) + + (j * dims_[5] * dims_[4] * dims_[3] * dims_[2]) + + (i * dims_[5] * dims_[4] * dims_[3] * dims_[2] * dims_[1])); +} + +template +KOKKOS_INLINE_FUNCTION +T& MPIArrayKokkos::operator()(size_t i, size_t j, size_t k, size_t l, + size_t m, size_t n, size_t o) const { + assert(order_ == 7 && "Tensor order (rank) does not match constructor in MPIArrayKokkos 7D!"); + assert(i >= 0 && i < dims_[0] && "i is out of bounds in MPIArrayKokkos 7D!"); + assert(j >= 0 && j < dims_[1] && "j is out of bounds in MPIArrayKokkos 7D!"); + assert(k >= 0 && k < dims_[2] && "k is out of bounds in MPIArrayKokkos 7D!"); + assert(l >= 0 && l < dims_[3] && "l is out of bounds in MPIArrayKokkos 7D!"); + assert(m >= 0 && m < dims_[4] && "m is out of bounds in MPIArrayKokkos 7D!"); + assert(n >= 0 && n < dims_[5] && "n is out of bounds in MPIArrayKokkos 7D!"); + assert(o >= 0 && o < dims_[6] && "o is out of bounds in MPIArrayKokkos 7D!"); + return this_array_.d_view(o + (n * dims_[6]) + + (m * dims_[6] * dims_[5]) + + (l * dims_[6] * dims_[5] * dims_[4]) + + (k * dims_[6] * dims_[5] * dims_[4] * dims_[3]) + + (j * dims_[6] * dims_[5] * dims_[4] * dims_[3] * dims_[2]) + + (i * dims_[6] * dims_[5] * dims_[4] * dims_[3] * dims_[2] * dims_[1])); +} + +template +KOKKOS_INLINE_FUNCTION +MPIArrayKokkos& MPIArrayKokkos::operator= (const MPIArrayKokkos& temp) { + + // Do nothing if the assignment is of the form x = x + if (this != &temp) { + for (int iter = 0; iter < temp.order_; iter++){ + dims_[iter] = temp.dims_[iter]; + } // end for + + order_ = temp.order_; + length_ = temp.length_; + this_array_ = temp.this_array_; + host = temp.host; + mpi_recv_rank_ = temp.mpi_recv_rank_; + mpi_tag_ = temp.mpi_tag_; + mpi_comm_ = temp.mpi_comm_; + mpi_status_ = temp.mpi_status_; + mpi_datatype_ = temp.mpi_datatype_; + mpi_request_ = temp.mpi_request_; + } + + return *this; +} + +// Return size +template +KOKKOS_INLINE_FUNCTION +size_t MPIArrayKokkos::size() const { + return length_; +} + +template +KOKKOS_INLINE_FUNCTION +size_t MPIArrayKokkos::extent() const { + return length_; +} + +template +KOKKOS_INLINE_FUNCTION +size_t MPIArrayKokkos::dims(size_t i) const { + assert(i < order_ && "MPIArrayKokkos order (rank) does not match constructor, dim[i] does not exist!"); + assert(i >= 0 && dims_[i]>0 && "Access to MPIArrayKokkos dims is out of bounds!"); + return dims_[i]; +} + +template +KOKKOS_INLINE_FUNCTION +size_t MPIArrayKokkos::order() const { + return order_; +} + +template +KOKKOS_INLINE_FUNCTION +T* MPIArrayKokkos::device_pointer() const { + return this_array_.d_view.data(); +} + +template +KOKKOS_INLINE_FUNCTION +T* MPIArrayKokkos::host_pointer() const { + return this_array_.h_view.data(); +} + +template +KOKKOS_INLINE_FUNCTION +Kokkos::DualView MPIArrayKokkos::get_kokkos_dual_view() const { + return this_array_; +} + +template +void MPIArrayKokkos::update_host() { + + this_array_.template modify(); + this_array_.template sync(); +} + +template +void MPIArrayKokkos::update_device() { + + this_array_.template modify(); + this_array_.template sync(); +} + +// a default setup, should not be used except for testing +template +void MPIArrayKokkos::mpi_setup() { + mpi_recv_rank_ = 1; + mpi_tag_ = 99; + mpi_comm_ = MPI_COMM_WORLD; +} + +template +void MPIArrayKokkos::mpi_setup(int recv_rank) { + mpi_recv_rank_ = recv_rank; +} + +template +void MPIArrayKokkos::mpi_setup(int recv_rank, int tag) { + mpi_recv_rank_ = recv_rank; + mpi_tag_ = tag; +} + +template +void MPIArrayKokkos::mpi_setup(int recv_rank, int tag, MPI_Comm comm) { + mpi_recv_rank_ = recv_rank; + mpi_tag_ = tag; + mpi_comm_ = comm; +} + +template +void MPIArrayKokkos::mpi_set_rank(int recv_rank) { + mpi_recv_rank_ = recv_rank; +} + +template +void MPIArrayKokkos::mpi_set_tag(int tag) { + mpi_tag_ = tag; +} + +template +void MPIArrayKokkos::mpi_set_comm(MPI_Comm comm) { + mpi_comm_ = comm; +} + +template +int MPIArrayKokkos::get_rank() { + return mpi_recv_rank_; +} + +template +int MPIArrayKokkos::get_tag() { + return mpi_tag_; +} + +template +MPI_Comm MPIArrayKokkos::get_comm() { + return mpi_comm_; +} + +//MPI_Send wrapper +template +void MPIArrayKokkos::send(size_t count, int dest, int tag, MPI_Comm comm) { +#ifdef HAVE_GPU_AWARE_MPI + MPI_Send(device_pointer(), count, mpi_datatype_, dest, tag, comm); +#else + update_host(); + MPI_Send(host_pointer(), count, mpi_datatype_, dest, tag, comm); +#endif +} + +//MPI_Recv wrapper +template +void MPIArrayKokkos::recv(size_t count, int source, int tag, MPI_Comm comm) { +#ifdef HAVE_GPU_AWARE_MPI + MPI_Recv(device_pointer(), count, mpi_datatype_, source, tag, comm, &mpi_status_); +#else + MPI_Recv(host_pointer(), count, mpi_datatype_, source, tag, comm, &mpi_status_); + update_device(); +#endif +} + +//MPI_Send halo wrapper +template +void MPIArrayKokkos::halo_send() { +#ifdef HAVE_GPU_AWARE_MPI + MPI_Send(device_pointer(), size(), mpi_datatype_, mpi_recv_rank_, mpi_tag_, mpi_comm_); +#else + update_host(); + MPI_Send(host_pointer(), size(), mpi_datatype_, mpi_recv_rank_, mpi_tag_, mpi_comm_); +#endif +} + +//MPI_Recv halo wrapper +template +void MPIArrayKokkos::halo_recv() { +#ifdef HAVE_GPU_AWARE_MPI + MPI_Recv(device_pointer(), size(), mpi_datatype_, mpi_recv_rank_, mpi_tag_, mpi_comm_, &mpi_status_); +#else + MPI_Recv(host_pointer(), size(), mpi_datatype_, mpi_recv_rank_, mpi_tag_, mpi_comm_, &mpi_status_); + update_device(); +#endif +} + +//MPI_iSend halo wrapper +template +void MPIArrayKokkos::halo_isend() { +#ifdef HAVE_GPU_AWARE_MPI + MPI_Isend(device_pointer(), size(), mpi_datatype_, mpi_recv_rank_, mpi_tag_, mpi_comm_, &mpi_request_); +#else + update_host(); + MPI_Isend(host_pointer(), size(), mpi_datatype_, mpi_recv_rank_, mpi_tag_, mpi_comm_, &mpi_request_); +#endif +} + +//MPI_iRecv halo wrapper +template +void MPIArrayKokkos::halo_irecv() { +#ifdef HAVE_GPU_AWARE_MPI + MPI_Irecv(device_pointer(), size(), mpi_datatype_, mpi_recv_rank_, mpi_tag_, mpi_comm_, &mpi_request_); +#else + MPI_Irecv(host_pointer(), size(), mpi_datatype_, mpi_recv_rank_, mpi_tag_, mpi_comm_, &mpi_request_); +#endif +} + +//MPI_Bcast wrapper +template +void MPIArrayKokkos::broadcast(size_t count, int root, MPI_Comm comm) { +#ifdef HAVE_GPU_AWARE_MPI + MPI_Bcast(device_pointer(), count, mpi_datatype_, root, comm); +#else + update_host(); + MPI_Bcast(host_pointer(), count, mpi_datatype_, root, comm); + update_device(); +#endif +} + +//MPI_Scatter wrapper +template +void MPIArrayKokkos::scatter(size_t send_count, MPIArrayKokkos recv_buffer, size_t recv_count, int root, MPI_Comm comm) { +#ifdef HAVE_GPU_AWARE_MPI + MPI_Scatter(device_pointer(), send_count, mpi_datatype_, recv_buffer.device_pointer(), recv_count, mpi_datatype_, root, comm); +#else + update_host(); + MPI_Scatter(host_pointer(), send_count, mpi_datatype_, recv_buffer.host_pointer(), recv_count, mpi_datatype_, root, comm); + recv_buffer.update_device(); +#endif +} + +//MPI_Gather wrapper +template +void MPIArrayKokkos::gather(size_t send_count, MPIArrayKokkos recv_buffer, size_t recv_count, int root, MPI_Comm comm) { +#ifdef HAVE_GPU_AWARE_MPI + MPI_Gather(device_pointer(), send_count, mpi_datatype_, recv_buffer.device_pointer(), recv_count, mpi_datatype_, root, comm); +#else + update_host(); + MPI_Gather(host_pointer(), send_count, mpi_datatype_, recv_buffer.host_pointer(), recv_count, mpi_datatype_, root, comm); + recv_buffer.update_device(); +#endif +} + +//MPI_AllGather wrapper +template +void MPIArrayKokkos::allgather(size_t send_count, MPIArrayKokkos recv_buffer, size_t recv_count, MPI_Comm comm) { +#ifdef HAVE_GPU_AWARE_MPI + MPI_Allgather(device_pointer(), send_count, mpi_datatype_, recv_buffer.device_pointer(), recv_count, mpi_datatype_, comm); +#else + update_host(); + MPI_Allgather(host_pointer(), send_count, mpi_datatype_, recv_buffer.host_pointer(), recv_count, mpi_datatype_, comm); + recv_buffer.update_device(); +#endif +} + +//MPI_Isend wrapper +template +void MPIArrayKokkos::isend(size_t count, int dest, int tag, MPI_Comm comm) { +#ifdef HAVE_GPU_AWARE_MPI + MPI_Isend(device_pointer(), count, mpi_datatype_, dest, tag, comm, &mpi_request_); +#else + update_host(); + MPI_Isend(host_pointer(), count, mpi_datatype_, dest, tag, comm, &mpi_request_); +#endif +} + +//MPI_Irecv wrapper +template +void MPIArrayKokkos::irecv(size_t count, int source, int tag, MPI_Comm comm) { +#ifdef HAVE_GPU_AWARE_MPI + MPI_Irecv(device_pointer(), count, mpi_datatype_, source, tag, comm, &mpi_request_); +#else + MPI_Irecv(host_pointer(), count, mpi_datatype_, source, tag, comm, &mpi_request_); +#endif +} + +//MPI_Wait wrapper for the sender +template +void MPIArrayKokkos::wait_send() { + MPI_Wait(&mpi_request_, &mpi_status_); +} + +//MPI_Wait wrapper for the receiver +template +void MPIArrayKokkos::wait_recv() { + MPI_Wait(&mpi_request_, &mpi_status_); +#ifndef HAVE_GPU_AWARE_MPI + update_device(); +#endif +} + +//MPI_Barrier wrapper +//template +//void MPIArrayKokkos::barrier(MPI_Comm comm) { +// MPI_Barrier(comm); +//} + +template +KOKKOS_INLINE_FUNCTION +MPIArrayKokkos::~MPIArrayKokkos() {} + +//////////////////////////////////////////////////////////////////////////////// +// End of MPIArrayKokkos +//////////////////////////////////////////////////////////////////////////////// + +} // end namespace + +#endif // end if have MPI + +#endif // MPI_TYPES_H + diff --git a/src/kokkos_types.h b/src/kokkos_types.h deleted file mode 100644 index 06283914..00000000 --- a/src/kokkos_types.h +++ /dev/null @@ -1,8814 +0,0 @@ -#ifndef KOKKOS_TYPES_H -#define KOKKOS_TYPES_H -/********************************************************************************************** - © 2020. Triad National Security, LLC. All rights reserved. - This program was produced under U.S. Government contract 89233218CNA000001 for Los Alamos - National Laboratory (LANL), which is operated by Triad National Security, LLC for the U.S. - Department of Energy/National Nuclear Security Administration. All rights in the program are - reserved by Triad National Security, LLC, and the U.S. Department of Energy/National Nuclear - Security Administration. The Government is granted for itself and others acting on its behalf a - nonexclusive, paid-up, irrevocable worldwide license in this material to reproduce, prepare - derivative works, distribute copies to the public, perform publicly and display publicly, and - to permit others to do so. - This program is open source under the BSD-3 License. - Redistribution and use in source and binary forms, with or without modification, are permitted - provided that the following conditions are met: - - 1. Redistributions of source code must retain the above copyright notice, this list of - conditions and the following disclaimer. - - 2. Redistributions in binary form must reproduce the above copyright notice, this list of - conditions and the following disclaimer in the documentation and/or other materials - provided with the distribution. - - 3. Neither the name of the copyright holder nor the names of its contributors may be used - to endorse or promote products derived from this software without specific prior - written permission. - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS - IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - **********************************************************************************************/ - -#include "host_types.h" - - -#ifdef HAVE_KOKKOS -#include -#include - -using HostSpace = Kokkos::HostSpace; -using MemoryUnmanaged = Kokkos::MemoryUnmanaged; - -#ifdef HAVE_CUDA -//using UVMMemSpace = Kokkos::CudaUVMSpace; -using DefaultMemSpace = Kokkos::CudaSpace; -using DefaultExecSpace = Kokkos::Cuda; -using DefaultLayout = Kokkos::LayoutLeft; -#elif HAVE_OPENMP -using DefaultMemSpace = Kokkos::HostSpace; -using DefaultExecSpace = Kokkos::OpenMP; -using DefaultLayout = Kokkos::LayoutRight; -#elif HAVE_THREADS -using DefaultMemSpace = Kokkos::HostSpace; -using DefaultExecSpace = Kokkos::Threads; -using DefaultLayout = Kokkos::LayoutLeft; -#elif HAVE_HIP -using DefaultMemSpace = Kokkos::Experimental::HIPSpace; -using DefaultExecSpace = Kokkos::Experimental::HIP; -using DefaultLayout = Kokkos::LayoutLeft; -#else -using DefaultExecSpace = Kokkos::DefaultExecutionSpace; -using DefaultMemSpace = Kokkos::DefaultExecutionSpace::memory_space; -using DefaultLayout = Kokkos::LayoutLeft; -#endif - - -//MACROS to make the code less scary -#define kmalloc(size) ( Kokkos::kokkos_malloc(size) ) -#define kfree(pnt) ( Kokkos::kokkos_free(pnt) ) -#define ProfileRegionStart ( Kokkos::Profiling::pushRegion ) -#define ProfileRegionEnd ( Kokkos::Profiling::popRegion ) -#define DEFAULTSTRINGARRAY "array_" -#define DEFAULTSTRINGMATRIX "matrix_" - -using policy1D = Kokkos::RangePolicy; -using policy2D = Kokkos::MDRangePolicy< Kokkos::Rank<2> >; -using policy3D = Kokkos::MDRangePolicy< Kokkos::Rank<3> >; -using policy4D = Kokkos::MDRangePolicy< Kokkos::Rank<4> >; - -using TeamPolicy = Kokkos::TeamPolicy; -//using mdrange_policy2 = Kokkos::MDRangePolicy>; -//using mdrange_policy3 = Kokkos::MDRangePolicy>; - -using RMatrix1D = Kokkos::View; -using RMatrix2D = Kokkos::View; -using RMatrix3D = Kokkos::View; -using RMatrix4D = Kokkos::View; -using RMatrix5D = Kokkos::View; -using IMatrix1D = Kokkos::View; -using IMatrix2D = Kokkos::View; -using IMatrix3D = Kokkos::View; -using IMatrix4D = Kokkos::View; -using IMatrix5D = Kokkos::View; -using SVar = Kokkos::View; -using SArray2D = Kokkos::View; -using SArray3D = Kokkos::View; -using SArray4D = Kokkos::View; -using SArray5D = Kokkos::View; - -using SHArray1D = Kokkos::View; -#endif - -//To disable asserts, uncomment the following line -//#define NDEBUG - - - - - -#ifdef HAVE_KOKKOS -namespace mtr -{ - -/*! \brief Kokkos version of the serial FArray class. - * - * This is the Kokkos version of the serial FArray class. - * Its usage is analagous to that of the serial FArr5class, and it is to be - * used in Kokkos-specific code. - */ - - -template -class FArrayKokkos { - - using TArray1D = Kokkos::View; - -private: - size_t dims_[7]; - size_t order_; - size_t length_; - TArray1D this_array_; - -public: - - /*! - * \brief Default constructor - */ - FArrayKokkos(); - - /*! - * \brief An overloaded constructor used to construct an 1D FArrayKokkos - object. - - \param dim0 the length of the first dimension - */ - FArrayKokkos(size_t dim0, const std::string& tag_string = DEFAULTSTRINGARRAY); - - /*! - * \brief An overloaded constructor used to construct a 2D FArrayKokkos - object. - - \param dim0 the length of the first dimension - \param dim1 the length of the second dimension - */ - FArrayKokkos(size_t dim0, size_t dim1, const std::string& tag_string = DEFAULTSTRINGARRAY); - - /*! - * \brief An overloaded constructor used to construct a 3D FArrayKokkos - object. - - \param dim0 the length of the first dimension - \param dim1 the length of the second dimension - \param dim2 the length of the third dimension - */ - FArrayKokkos(size_t dim0, size_t dim1, size_t dim2, const std::string& tag_string = DEFAULTSTRINGARRAY); - - FArrayKokkos(size_t dim0, size_t dim1, size_t dim2, - size_t dim3, const std::string& tag_string = DEFAULTSTRINGARRAY); - - FArrayKokkos(size_t dim0, size_t dim1, size_t dim2, - size_t dim3, size_t dim4, const std::string& tag_string = DEFAULTSTRINGARRAY); - - FArrayKokkos(size_t dim0, size_t sone_dim2, size_t dim2, - size_t dim3, size_t dim4, size_t dim5, const std::string& tag_string = DEFAULTSTRINGARRAY); - - FArrayKokkos(size_t dim0, size_t sone_dim2, size_t dim2, - size_t dim3, size_t dim4, size_t dim5, - size_t dim6, const std::string& tag_string = DEFAULTSTRINGARRAY); - - // Overload operator() to acces data - // from 1D to 6D - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j) const; - - KOKKOS_INLINE_FUNCTION - T& operator() (size_t i, size_t j, size_t k) const; - - KOKKOS_INLINE_FUNCTION - T& operator() (size_t i, size_t j, size_t k, - size_t l) const; - - KOKKOS_INLINE_FUNCTION - T& operator() (size_t i, size_t j, size_t k, - size_t l, size_t m) const; - - KOKKOS_INLINE_FUNCTION - T& operator() (size_t i, size_t j, size_t k, - size_t l, size_t m, size_t n) const; - - KOKKOS_INLINE_FUNCTION - T& operator() (size_t i, size_t j, size_t k, - size_t l, size_t m, size_t n, size_t o) const; - - // Overload = operator - KOKKOS_INLINE_FUNCTION - FArrayKokkos& operator= (const FArrayKokkos &temp); - - KOKKOS_INLINE_FUNCTION - size_t size() const; - - KOKKOS_INLINE_FUNCTION - size_t extent() const; - - KOKKOS_INLINE_FUNCTION - size_t dims(size_t i) const; - - KOKKOS_INLINE_FUNCTION - size_t order() const; - - KOKKOS_INLINE_FUNCTION - T* pointer() const; - - //return kokkos view - KOKKOS_INLINE_FUNCTION - TArray1D get_kokkos_view() const; - - // Destructor - KOKKOS_INLINE_FUNCTION - ~FArrayKokkos(); - -}; //end of FArrayKokkos declarations - -// Default constructor -template -FArrayKokkos::FArrayKokkos() { - length_ = order_ = 0; - for (int i = 0; i < 7; i++) { - dims_[i] = 0; - } -} - -// Overloaded 1D constructor -template -FArrayKokkos::FArrayKokkos(size_t dim0, const std::string& tag_string){ - using TArray1D = Kokkos::View; - dims_[0] = dim0; - order_ = 1; - length_ = dim0; - this_array_ = TArray1D(tag_string, length_); -} - -// Overloaded 2D constructor -template -FArrayKokkos::FArrayKokkos(size_t dim0, size_t dim1, const std::string& tag_string) { - - using TArray1D = Kokkos::View; - - dims_[0] = dim0; - dims_[1] = dim1; - order_ = 2; - length_ = (dim0 * dim1); - this_array_ = TArray1D(tag_string, length_); -} - -// Overloaded 3D constructor -template -FArrayKokkos::FArrayKokkos(size_t dim0, size_t dim1, - size_t dim2, const std::string& tag_string) { - - using TArray1D = Kokkos::View; - - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - order_ = 3; - length_ = (dim0 * dim1 * dim2); - this_array_ = TArray1D(tag_string, length_); -} - -// Overloaded 4D constructor -template -FArrayKokkos::FArrayKokkos(size_t dim0, size_t dim1, - size_t dim2, size_t dim3, const std::string& tag_string) { - - using TArray1D = Kokkos::View; - - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - order_ = 4; - length_ = (dim0 * dim1 * dim2 * dim3); - this_array_ = TArray1D(tag_string, length_); -} - -// Overloaded 5D constructor -template -FArrayKokkos::FArrayKokkos(size_t dim0, size_t dim1, - size_t dim2, size_t dim3, - size_t dim4, const std::string& tag_string) { - - using TArray1D = Kokkos::View; - - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - dims_[4] = dim4; - order_ = 5; - length_ = (dim0 * dim1 * dim2 * dim3 * dim4); - this_array_ = TArray1D(tag_string, length_); -} - -// Overloaded 6D constructor -template -FArrayKokkos::FArrayKokkos(size_t dim0, size_t dim1, - size_t dim2, size_t dim3, - size_t dim4, size_t dim5, const std::string& tag_string) { - - using TArray1D = Kokkos::View; - - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - dims_[4] = dim4; - dims_[5] = dim5; - order_ = 6; - length_ = (dim0 * dim1 * dim2 * dim3 * dim4 * dim5); - this_array_ = TArray1D(tag_string, length_); -} - -// Overloaded 7D constructor -template -FArrayKokkos::FArrayKokkos(size_t dim0, size_t dim1, - size_t dim2, size_t dim3, - size_t dim4, size_t dim5, - size_t dim6, const std::string& tag_string) { - - using TArray1D = Kokkos::View; - - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - dims_[4] = dim4; - dims_[5] = dim5; - dims_[6] = dim6; - order_ = 7; - length_ = (dim0 * dim1 * dim2 * dim3 * dim4 * dim5 * dim6); - this_array_ = TArray1D(tag_string, length_); -} - -// Definitions of overload operator() -// for 1D to 7D -// Note: the indices for array all start at 0 - -// 1D -template -KOKKOS_INLINE_FUNCTION -T& FArrayKokkos::operator()( size_t i) const { - assert(order_ == 1 && "Tensor order (rank) does not match constructor in FArrayKokkos 1D!"); - assert( i >= 0 && i < dims_[0] && "i is out of bounds in FArrayKokkos 1D!"); - return this_array_(i); -} - -// 2D -template -KOKKOS_INLINE_FUNCTION -T& FArrayKokkos::operator()(size_t i, size_t j) const { - assert(order_ == 2 && "Tensor order (rank) does not match constructor in FArrayKokkos 2D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in FArrayKokkos 2D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in FArrayKokkos 2D!"); - return this_array_(i + (j * dims_[0])); -} - -// 3D -template -KOKKOS_INLINE_FUNCTION -T& FArrayKokkos::operator()(size_t i, size_t j, size_t k) const { - assert(order_ == 3 && "Tensor order (rank) does not match constructor in FArrayKokkos 3D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in FArrayKokkos 3D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in FArrayKokkos 3D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in FArrayKokkos 3D!"); - return this_array_(i + (j * dims_[0]) - + (k * dims_[0] * dims_[1])); -} - -// 4D -template -KOKKOS_INLINE_FUNCTION -T& FArrayKokkos::operator()(size_t i, size_t j, size_t k, size_t l) const { - assert(order_ == 4 && "Tensor order (rank) does not match constructor in FArrayKokkos 4D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in FArrayKokkos 4D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in FArrayKokkos 4D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in FArrayKokkos 4D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in FArrayKokkos 4D!"); - return this_array_(i + (j * dims_[0]) - + (k * dims_[0] * dims_[1]) - + (l * dims_[0] * dims_[1] * dims_[2])); -} - -// 5D -template -KOKKOS_INLINE_FUNCTION -T& FArrayKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m) const { - assert(order_ == 5 && "Tensor order (rank) does not match constructor in FArrayKokkos 5D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in FArrayKokkos 5D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in FArrayKokkos 5D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in FArrayKokkos 5D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in FArrayKokkos 5D!"); - assert(m >= 0 && m < dims_[4] && "m is out of bounds in FArrayKokkos 5D!"); - return this_array_(i + (j * dims_[0]) - + (k * dims_[0] * dims_[1]) - + (l * dims_[0] * dims_[1] * dims_[2]) - + (m * dims_[0] * dims_[1] * dims_[2] * dims_[3])); -} - -// 6D -template -KOKKOS_INLINE_FUNCTION -T& FArrayKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m, size_t n) const { - assert(order_ == 6 && "Tensor order (rank) does not match constructor in FArrayKokkos 6D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in FArrayKokkos 6D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in FArrayKokkos 6D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in FArrayKokkos 6D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in FArrayKokkos 6D!"); - assert(m >= 0 && m < dims_[4] && "m is out of bounds in FArrayKokkos 6D!"); - assert(n >= 0 && n < dims_[5] && "n is out of bounds in FArrayKokkos 6D!"); - return this_array_(i + (j * dims_[0]) - + (k * dims_[0] * dims_[1]) - + (l * dims_[0] * dims_[1] * dims_[2]) - + (m * dims_[0] * dims_[1] * dims_[2] * dims_[3]) - + (n * dims_[0] * dims_[1] * dims_[2] * dims_[3] * dims_[4])); -} - -// 7D -template -KOKKOS_INLINE_FUNCTION -T& FArrayKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m, size_t n, size_t o) const { - assert(order_ == 7 && "Tensor order (rank) does not match constructor in FArrayKokkos 7D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in FArrayKokkos 7D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in FArrayKokkos 7D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in FArrayKokkos 7D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in FArrayKokkos 7D!"); - assert(m >= 0 && m < dims_[4] && "m is out of bounds in FArrayKokkos 7D!"); - assert(n >= 0 && n < dims_[5] && "n is out of bounds in FArrayKokkos 7D!"); - assert(o >= 0 && o < dims_[6] && "o is out of bounds in FArrayKokkos 7D!"); - return this_array_(i + (j * dims_[0]) - + (k * dims_[0] * dims_[1]) - + (l * dims_[0] * dims_[1] * dims_[2]) - + (m * dims_[0] * dims_[1] * dims_[2] * dims_[3]) - + (n * dims_[0] * dims_[1] * dims_[2] * dims_[3] * dims_[4]) - + (o * dims_[0] * dims_[1] * dims_[2] * dims_[3] * dims_[4] * dims_[5])); -} - -// Overload = operator -// for object assingment THIS = FArrayKokkos<> TEMP(n,m,,,,) -template -KOKKOS_INLINE_FUNCTION -FArrayKokkos& FArrayKokkos::operator= (const FArrayKokkos& temp) { - using TArray1D = Kokkos::View; - - if (this != &temp) { - for (int iter = 0; iter < temp.order_; iter++){ - dims_[iter] = temp.dims_[iter]; - } // end for - - order_ = temp.order_; - length_ = temp.length_; - this_array_ = temp.this_array_; - } - return *this; -} - -template -KOKKOS_INLINE_FUNCTION -size_t FArrayKokkos::size() const { - return length_; -} - -template -KOKKOS_INLINE_FUNCTION -size_t FArrayKokkos::extent() const { - return length_; -} - -template -KOKKOS_INLINE_FUNCTION -size_t FArrayKokkos::dims(size_t i) const { - assert(i < order_ && "FArrayKokkos order (rank) does not match constructor, dim[i] does not exist!"); - assert(i >= 0 && dims_[i]>0 && "Access to FArrayKokkos dims is out of bounds!"); - return dims_[i]; -} - -template -KOKKOS_INLINE_FUNCTION -size_t FArrayKokkos::order() const { - return order_; -} - -template -KOKKOS_INLINE_FUNCTION -T* FArrayKokkos::pointer() const { - return this_array_.data(); -} - -//return the stored Kokkos view -template -KOKKOS_INLINE_FUNCTION -Kokkos::View FArrayKokkos::get_kokkos_view() const { - return this_array_; -} - -// Destructor -template -KOKKOS_INLINE_FUNCTION -FArrayKokkos::~FArrayKokkos() {} - -//////////////////////////////////////////////////////////////////////////////// -// End of FArrayKokkos -//////////////////////////////////////////////////////////////////////////////// - -/*! \brief Kokkos version of the serial ViewFArray class. - * - */ -template -class ViewFArrayKokkos { - -private: - size_t dims_[7]; - size_t order_; - size_t length_; - T* this_array_; - -public: - KOKKOS_INLINE_FUNCTION - ViewFArrayKokkos(); - - KOKKOS_INLINE_FUNCTION - ViewFArrayKokkos(T* some_array, size_t dim0); - - KOKKOS_INLINE_FUNCTION - ViewFArrayKokkos(T* some_array, size_t dim0, size_t dim1); - - KOKKOS_INLINE_FUNCTION - ViewFArrayKokkos(T* some_array, size_t dim0, size_t dim1, size_t dim2); - - KOKKOS_INLINE_FUNCTION - ViewFArrayKokkos(T* some_array, size_t dim0, size_t dim1, size_t dim2, - size_t dim3); - - KOKKOS_INLINE_FUNCTION - ViewFArrayKokkos(T* some_array, size_t dim0, size_t dim1, size_t dim2, - size_t dim3, size_t dim4); - - KOKKOS_INLINE_FUNCTION - ViewFArrayKokkos(T* some_array, size_t dim0, size_t dim1, size_t dim2, - size_t dim3, size_t dim4, size_t dim5); - - KOKKOS_INLINE_FUNCTION - ViewFArrayKokkos(T* some_array, size_t dim0, size_t dim1, size_t dim2, - size_t dim3, size_t dim4, size_t dim5, size_t dim6); - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, - size_t l) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, - size_t l, size_t m) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, - size_t l, size_t m, size_t n) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, - size_t l, size_t m, size_t n, size_t o) const; - - - KOKKOS_INLINE_FUNCTION - size_t size() const; - - KOKKOS_INLINE_FUNCTION - size_t extent() const; - - KOKKOS_INLINE_FUNCTION - size_t dims(size_t i) const; - - KOKKOS_INLINE_FUNCTION - size_t order() const; - - KOKKOS_INLINE_FUNCTION - T* pointer() const; - - KOKKOS_INLINE_FUNCTION - ~ViewFArrayKokkos(); - -}; // End of ViewFArrayKokkos declarations - -// Default constructor -template -KOKKOS_INLINE_FUNCTION -ViewFArrayKokkos::ViewFArrayKokkos() { - length_ = order_ = 0; - for (int i = 0; i < 7; i++) { - dims_[i] = 0; - } -} - -// Overloaded 1D constructor -template -KOKKOS_INLINE_FUNCTION -ViewFArrayKokkos::ViewFArrayKokkos(T *some_array, size_t dim0) { - dims_[0] = dim0; - order_ = 1; - length_ = dim0; - this_array_ = some_array; -} - -// Overloaded 2D constructor -template -KOKKOS_INLINE_FUNCTION -ViewFArrayKokkos::ViewFArrayKokkos(T *some_array, size_t dim0, size_t dim1) { - dims_[0] = dim0; - dims_[1] = dim1; - order_ = 2; - length_ = (dim0 * dim1); - this_array_ = some_array; -} - -// Overloaded 3D constructor -template -KOKKOS_INLINE_FUNCTION -ViewFArrayKokkos::ViewFArrayKokkos(T *some_array, size_t dim0, size_t dim1, - size_t dim2) { - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - order_ = 3; - length_ = (dim0 * dim1 * dim2); - this_array_ = some_array; -} - -// Overloaded 4D constructor -template -KOKKOS_INLINE_FUNCTION -ViewFArrayKokkos::ViewFArrayKokkos(T *some_array, size_t dim0, size_t dim1, - size_t dim2, size_t dim3) { - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - order_ = 4; - length_ = (dim0 * dim1 * dim2 * dim3); - this_array_ = some_array; -} - -// Overloaded 5D constructor -template -KOKKOS_INLINE_FUNCTION -ViewFArrayKokkos::ViewFArrayKokkos(T *some_array, size_t dim0, size_t dim1, - size_t dim2, size_t dim3, size_t dim4) { - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - dims_[4] = dim4; - order_ = 5; - length_ = (dim0 * dim1 * dim2 * dim3 * dim4); - this_array_ = some_array; -} - -// Overloaded 6D constructor -template -KOKKOS_INLINE_FUNCTION -ViewFArrayKokkos::ViewFArrayKokkos(T *some_array, size_t dim0, size_t dim1, - size_t dim2, size_t dim3, size_t dim4, - size_t dim5) { - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - dims_[4] = dim4; - dims_[5] = dim5; - order_ = 6; - length_ = (dim0 * dim1 * dim2 * dim3 * dim4 * dim5); - this_array_ = some_array; -} - -// Overloaded 7D constructor -template -KOKKOS_INLINE_FUNCTION -ViewFArrayKokkos::ViewFArrayKokkos(T *some_array, size_t dim0, size_t dim1, - size_t dim2, size_t dim3, size_t dim4, - size_t dim5, size_t dim6) { - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - dims_[4] = dim4; - dims_[5] = dim5; - dims_[6] = dim6; - order_ = 7; - length_ = (dim0 * dim1 * dim2 * dim3 * dim4 * dim5 * dim6); - this_array_ = some_array; -} - -// Overloaded operator() for 1D array access -template -KOKKOS_INLINE_FUNCTION -T& ViewFArrayKokkos::operator()(size_t i) const { - assert(order_ == 1 && "Tensor order (rank) does not match constructor in ViewFArrayKokkos 1D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in ViewFArrayKokkos 1D!"); - return this_array_[i]; -} - -//2D -template -KOKKOS_INLINE_FUNCTION -T& ViewFArrayKokkos::operator()(size_t i, size_t j) const { - assert(order_ == 2 && "Tensor order (rank) does not match constructor in ViewFArrayKokkos 2D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in ViewFArrayKokkos 2D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in ViewFArrayKokkos 2D!"); - return this_array_[i + (j * dims_[0])]; -} - -//3D -template -KOKKOS_INLINE_FUNCTION -T& ViewFArrayKokkos::operator()(size_t i, size_t j, size_t k) const { - assert(order_ == 3 && "Tensor order (rank) does not match constructor in ViewFArrayKokkos 3D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in ViewFArrayKokkos 3D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in ViewFArrayKokkos 3D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in ViewFArrayKokkos 3D!"); - return this_array_[i + (j * dims_[0]) - + (k * dims_[0] * dims_[1])]; -} - -//4D -template -KOKKOS_INLINE_FUNCTION -T& ViewFArrayKokkos::operator()(size_t i, size_t j, size_t k, - size_t l) const { - assert(order_ == 4 && "Tensor order (rank) does not match constructor in ViewFArrayKokkos 4D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in ViewFArrayKokkos 4D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in ViewFArrayKokkos 4D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in ViewFArrayKokkos 4D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in ViewFArrayKokkos 4D!"); - return this_array_[i + (j * dims_[0]) - + (k * dims_[0] * dims_[1]) - + (l * dims_[0] * dims_[1] *dims_[2])]; -} - -//5D -template -KOKKOS_INLINE_FUNCTION -T& ViewFArrayKokkos::operator()(size_t i, size_t j, size_t k, - size_t l, size_t m) const { - assert(order_ == 5 && "Tensor order (rank) does not match constructor in ViewFArrayKokkos 5D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in ViewFArrayKokkos 5D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in ViewFArrayKokkos 5D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in ViewFArrayKokkos 5D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in ViewFArrayKokkos 5D!"); - assert(m >= 0 && m < dims_[4] && "m is out of bounds in ViewFArrayKokkos 5D!"); - return this_array_[i + (j * dims_[0]) - + (k * dims_[0] * dims_[1]) - + (l * dims_[0] * dims_[1] * dims_[2]) - + (m * dims_[0] * dims_[1] * dims_[2] * dims_[3])]; -} - -//6D -template -KOKKOS_INLINE_FUNCTION -T& ViewFArrayKokkos::operator()(size_t i, size_t j, size_t k, - size_t l, size_t m, size_t n) const { - assert(order_ == 6 && "Tensor order (rank) does not match constructor in ViewFArrayKokkos 6D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in ViewFArrayKokkos 6D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in ViewFArrayKokkos 6D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in ViewFArrayKokkos 6D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in ViewFArrayKokkos 6D!"); - assert(m >= 0 && m < dims_[4] && "m is out of bounds in ViewFArrayKokkos 6D!"); - assert(n >= 0 && n < dims_[5] && "n is out of bounds in ViewFArrayKokkos 6D!"); - return this_array_[i + (j * dims_[0]) - + (k * dims_[0] * dims_[1]) - + (l * dims_[0] * dims_[1] * dims_[2]) - + (m * dims_[0] * dims_[1] * dims_[2] * dims_[3]) - + (n * dims_[0] * dims_[1] * dims_[2] * dims_[3] * dims_[4])]; -} - -//7D -template -KOKKOS_INLINE_FUNCTION -T& ViewFArrayKokkos::operator()(size_t i, size_t j, size_t k, - size_t l, size_t m, size_t n, - size_t o) const { - assert(order_ == 7 && "Tensor order (rank) does not match constructor in ViewFArrayKokkos 7D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in ViewFArrayKokkos 7D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in ViewFArrayKokkos 7D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in ViewFArrayKokkos 7D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in ViewFArrayKokkos 7D!"); - assert(m >= 0 && m < dims_[4] && "m is out of bounds in ViewFArrayKokkos 7D!"); - assert(n >= 0 && n < dims_[5] && "n is out of bounds in ViewFArrayKokkos 7D!"); - assert(o >= 0 && o < dims_[6] && "o is out of bounds in ViewFArrayKokkos 7D!"); - return this_array_[i + (j * dims_[0]) - + (k * dims_[0] * dims_[1]) - + (l * dims_[0] * dims_[1] * dims_[2]) - + (m * dims_[0] * dims_[1] * dims_[2] * dims_[3]) - + (n * dims_[0] * dims_[1] * dims_[2] * dims_[3] * dims_[4]) - + (o * dims_[0] * dims_[1] * dims_[2] * dims_[3] * dims_[4] * dims_[5])]; -} - -template -KOKKOS_INLINE_FUNCTION -size_t ViewFArrayKokkos::size() const { - return length_; -} - -template -KOKKOS_INLINE_FUNCTION -size_t ViewFArrayKokkos::extent() const { - return length_; -} - -template -KOKKOS_INLINE_FUNCTION -size_t ViewFArrayKokkos::dims(size_t i) const { - assert(i < order_ && "ViewFArrayKokkos order (rank) does not match constructor, dim[i] does not exist!"); - assert(i >= 0 && dims_[i]>0 && "Access to ViewFArrayKokkos dims is out of bounds!"); - return dims_[i]; -} - -template -KOKKOS_INLINE_FUNCTION -size_t ViewFArrayKokkos::order() const { - return order_; -} - -template -KOKKOS_INLINE_FUNCTION -T* ViewFArrayKokkos::pointer() const { - return this_array_; -} - -template -KOKKOS_INLINE_FUNCTION -ViewFArrayKokkos::~ViewFArrayKokkos() {} - -//////////////////////////////////////////////////////////////////////////////// -// End of ViewFArrayKokkos -//////////////////////////////////////////////////////////////////////////////// - -/*! \brief Kokkos version of the serial FMatrix class. - * - */ -template -class FMatrixKokkos { - - using TArray1D = Kokkos::View; - -private: - - size_t dims_[7]; - size_t order_; - size_t length_; - TArray1D this_matrix_; - -public: - FMatrixKokkos(); - - FMatrixKokkos(size_t dim1, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - FMatrixKokkos(size_t dim1, size_t dim2, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - FMatrixKokkos(size_t dim1, size_t dim2, size_t dim3, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - FMatrixKokkos(size_t dim1, size_t dim2, size_t dim3, - size_t dim4, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - FMatrixKokkos(size_t dim1, size_t dim2, size_t dim3, - size_t dim4, size_t dim5, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - FMatrixKokkos(size_t dim1, size_t dim2, size_t dim3, - size_t dim4, size_t dim5, size_t dim6, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - FMatrixKokkos(size_t dim1, size_t dim2, size_t dim3, - size_t dim4, size_t dim5, size_t dim6, - size_t dim7, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m, - size_t n) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m, - size_t n, size_t o) const; - - KOKKOS_INLINE_FUNCTION - FMatrixKokkos& operator=(const FMatrixKokkos& temp); - - KOKKOS_INLINE_FUNCTION - size_t size() const; - - KOKKOS_INLINE_FUNCTION - size_t extent() const; - - KOKKOS_INLINE_FUNCTION - size_t dims(size_t i) const; - - KOKKOS_INLINE_FUNCTION - size_t order() const; - - KOKKOS_INLINE_FUNCTION - T* pointer() const; - - //return kokkos view - KOKKOS_INLINE_FUNCTION - TArray1D get_kokkos_view() const; - - KOKKOS_INLINE_FUNCTION - ~FMatrixKokkos(); - -}; // End of FMatrixKokkos - -// Default constructor -template -FMatrixKokkos::FMatrixKokkos() { - length_ = order_ = 0; - for (int i = 0; i < 7; i++) { - dims_[i] = 0; - } -} - -// Overloaded 1D constructor -template -FMatrixKokkos::FMatrixKokkos(size_t dim1, const std::string& tag_string) { - using TArray1D = Kokkos::View; - - dims_[0] = dim1; - order_ = 1; - length_ = dim1; - this_matrix_ = TArray1D(tag_string, length_); -} - -// Overloaded 2D constructor -template -FMatrixKokkos::FMatrixKokkos(size_t dim1, size_t dim2, const std::string& tag_string) { - using TArray1D = Kokkos::View; - - dims_[0] = dim1; - dims_[1] = dim2; - order_ = 2; - length_ = (dim1 * dim2); - this_matrix_ = TArray1D(tag_string, length_); -} - -// Overloaded 3D constructor -template -FMatrixKokkos::FMatrixKokkos(size_t dim1, size_t dim2, - size_t dim3, const std::string& tag_string) { - using TArray1D = Kokkos::View; - - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - order_ = 3; - length_ = (dim1 * dim2 * dim3); - this_matrix_ = TArray1D(tag_string, length_); -} - -// Overloaded 4D constructor -template -FMatrixKokkos::FMatrixKokkos(size_t dim1, size_t dim2, - size_t dim3, size_t dim4, const std::string& tag_string) { - using TArray1D = Kokkos::View; - - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - order_ = 4; - length_ = (dim1 * dim2 * dim3 * dim4); - this_matrix_ = TArray1D(tag_string, length_); -} - -// Overloaded 5D constructor -template -FMatrixKokkos::FMatrixKokkos(size_t dim1, size_t dim2, - size_t dim3, size_t dim4, - size_t dim5, const std::string& tag_string) { - using TArray1D = Kokkos::View; - - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - dims_[4] = dim5; - order_ = 5; - length_ = (dim1 * dim2 * dim3 * dim4 * dim5); - this_matrix_ = TArray1D(tag_string, length_); -} - -// Overloaded 5D constructor -template -FMatrixKokkos::FMatrixKokkos(size_t dim1, size_t dim2, - size_t dim3, size_t dim4, - size_t dim5, size_t dim6, const std::string& tag_string) { - using TArray1D = Kokkos::View; - - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - dims_[4] = dim5; - dims_[5] = dim6; - order_ = 6; - length_ = (dim1 * dim2 * dim3 * dim4 * dim5 * dim6); - this_matrix_ = TArray1D(tag_string, length_); -} - -// Overloaded 5D constructor -template -FMatrixKokkos::FMatrixKokkos(size_t dim1, size_t dim2, - size_t dim3, size_t dim4, - size_t dim5, size_t dim6, - size_t dim7, const std::string& tag_string) { - using TArray1D = Kokkos::View; - - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - dims_[4] = dim5; - dims_[5] = dim6; - dims_[6] = dim7; - order_ = 7; - length_ = (dim1 * dim2 * dim3 * dim4 * dim5 * dim6 * dim7); - this_matrix_ = TArray1D(tag_string, length_); -} - -template -KOKKOS_INLINE_FUNCTION -T& FMatrixKokkos::operator()(size_t i) const { - assert(order_ == 1 && "Tensor order (rank) does not match constructor in FMatrixKokkos 1D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in FMatrixKokkos in 1D!"); - return this_matrix_((i - 1)); -} - -template -KOKKOS_INLINE_FUNCTION -T& FMatrixKokkos::operator()(size_t i, size_t j) const { - assert(order_ == 2 && "Tensor order (rank) does not match constructor in FMatrixKokkos 2D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in FMatrixKokkos in 2D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in FMatrixKokkos in 2D!"); - return this_matrix_((i - 1) + ((j - 1) * dims_[0])); -} - -template -KOKKOS_INLINE_FUNCTION -T& FMatrixKokkos::operator()(size_t i, size_t j, size_t k) const { - assert(order_ == 3 && "Tensor order (rank) does not match constructor in FMatrixKokkos 3D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in FMatrixKokkos in 3D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in FMatrixKokkos in 3D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in FMatrixKokkos in 3D!"); - return this_matrix_((i - 1) + ((j - 1) * dims_[0]) - + ((k - 1) * dims_[0] * dims_[1])); -} - -template -KOKKOS_INLINE_FUNCTION -T& FMatrixKokkos::operator()(size_t i, size_t j, size_t k, size_t l) const { - assert(order_ == 4 && "Tensor order (rank) does not match constructor in FMatrixKokkos 4D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in FMatrixKokkos in 4D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in FMatrixKokkos in 4D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in FMatrixKokkos in 4D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in FMatrixKokkos in 4D!"); - return this_matrix_((i - 1) + ((j - 1) * dims_[0]) - + ((k - 1) * dims_[0] * dims_[1]) - + ((l - 1) * dims_[0] * dims_[1] * dims_[2])); -} - -template -KOKKOS_INLINE_FUNCTION -T& FMatrixKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m) const { - assert(order_ == 5 && "Tensor order (rank) does not match constructor in FMatrixKokkos 5D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in FMatrixKokkos in 5D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in FMatrixKokkos in 5D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in FMatrixKokkos in 5D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in FMatrixKokkos in 5D!"); - assert(m >= 1 && m <= dims_[4] && "m is out of bounds in FMatrixKokkos in 5D!"); - return this_matrix_((i - 1) + ((j - 1) * dims_[0]) - + ((k - 1) * dims_[0] * dims_[1]) - + ((l - 1) * dims_[0] * dims_[1] * dims_[2]) - + ((m - 1) * dims_[0] * dims_[1] * dims_[2] * dims_[3])); -} - -template -KOKKOS_INLINE_FUNCTION -T& FMatrixKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m, size_t n) const { - assert(order_ == 6 && "Tensor order (rank) does not match constructor in FMatrixKokkos 6D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in FMatrixKokkos in 6D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in FMatrixKokkos in 6D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in FMatrixKokkos in 6D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in FMatrixKokkos in 6D!"); - assert(m >= 1 && m <= dims_[4] && "m is out of bounds in FMatrixKokkos in 6D!"); - assert(n >= 1 && n <= dims_[5] && "n is out of bounds in FMatrixKokkos in 6D!"); - return this_matrix_[(i - 1) + ((j - 1) * dims_[0]) - + ((k - 1) * dims_[0] * dims_[1]) - + ((l - 1) * dims_[0] * dims_[1] * dims_[2]) - + ((m - 1) * dims_[0] * dims_[1] * dims_[2] * dims_[3]) - + ((n - 1) * dims_[0] * dims_[1] * dims_[2] * dims_[3] * dims_[4])]; -} - -template -KOKKOS_INLINE_FUNCTION -T& FMatrixKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m, size_t n, size_t o) const { - assert(order_ == 7 && "Tensor order (rank) does not match constructor in FMatrixKokkos 7D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in FMatrixKokkos in 7D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in FMatrixKokkos in 7D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in FMatrixKokkos in 7D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in FMatrixKokkos in 7D!"); - assert(m >= 1 && m <= dims_[4] && "m is out of bounds in FMatrixKokkos in 7D!"); - assert(n >= 1 && n <= dims_[5] && "n is out of bounds in FMatrixKokkos in 7D!"); - assert(o >= 1 && o <= dims_[6] && "o is out of bounds in FMatrixKokkos in 7D!"); - return this_matrix_[(i - 1) + ((j - 1) * dims_[0]) - + ((k - 1) * dims_[0] * dims_[1]) - + ((l - 1) * dims_[0] * dims_[1] * dims_[2]) - + ((m - 1) * dims_[0] * dims_[1] * dims_[2] * dims_[3]) - + ((n - 1) * dims_[0] * dims_[1] * dims_[2] * dims_[3] * dims_[4]) - + ((o - 1) * dims_[0] * dims_[1] * dims_[2] * dims_[3] * dims_[4] * dims_[5])]; -} - -template -KOKKOS_INLINE_FUNCTION -FMatrixKokkos& FMatrixKokkos::operator=(const FMatrixKokkos& temp) { - // Do nothing if the assignment is of the form x = x - if (this != &temp) { - for (int iter = 0; iter < temp.order_; iter++){ - dims_[iter] = temp.dims_[iter]; - } // end for - - order_ = temp.order_; - length_ = temp.length_; - this_matrix_ = temp.this_matrix_; - } - return *this; -} - - - -template -KOKKOS_INLINE_FUNCTION -size_t FMatrixKokkos::size() const { - return length_; -} - -template -KOKKOS_INLINE_FUNCTION -size_t FMatrixKokkos::extent() const { - return length_; -} - -template -KOKKOS_INLINE_FUNCTION -size_t FMatrixKokkos::dims(size_t i) const { - i--; - assert(i < order_ && "FMatrixKokkos order (rank) does not match constructor, dim[i] does not exist!"); - assert(i >= 0 && dims_[i]>0 && "Access to FMatrixKokkos dims is out of bounds!"); - return dims_[i]; -} - -template -KOKKOS_INLINE_FUNCTION -size_t FMatrixKokkos::order() const { - return order_; -} - -template -KOKKOS_INLINE_FUNCTION -T* FMatrixKokkos::pointer() const { - return this_matrix_.data(); -} - -//return the stored Kokkos view -template -KOKKOS_INLINE_FUNCTION -Kokkos::View FMatrixKokkos::get_kokkos_view() const { - return this_matrix_; -} - -template -KOKKOS_INLINE_FUNCTION -FMatrixKokkos::~FMatrixKokkos() {} - -//////////////////////////////////////////////////////////////////////////////// -// End of FMatrixKokkos -//////////////////////////////////////////////////////////////////////////////// - -/*! \brief Kokkos version of the serial ViewFMatrix class. - * - */ -template -class ViewFMatrixKokkos { - -private: - - size_t dims_[7]; - size_t order_; - size_t length_; - T* this_matrix_; - -public: - - KOKKOS_INLINE_FUNCTION - ViewFMatrixKokkos(); - - KOKKOS_INLINE_FUNCTION - ViewFMatrixKokkos(T* some_matrix, size_t dim1); - - KOKKOS_INLINE_FUNCTION - ViewFMatrixKokkos(T* some_matrix, size_t dim1, size_t dim2); - - KOKKOS_INLINE_FUNCTION - ViewFMatrixKokkos(T* some_matrix, size_t dim1, size_t dim2, - size_t dim3); - - KOKKOS_INLINE_FUNCTION - ViewFMatrixKokkos(T* some_matrix, size_t dim1, size_t dim2, - size_t dim3, size_t dim4); - - KOKKOS_INLINE_FUNCTION - ViewFMatrixKokkos(T* some_matrix, size_t dim1, size_t dim2, - size_t dim3, size_t dim4, size_t dim5); - - KOKKOS_INLINE_FUNCTION - ViewFMatrixKokkos(T* some_matrix, size_t dim1, size_t dim2, - size_t dim3, size_t dim4, size_t dim5, - size_t dim6); - - KOKKOS_INLINE_FUNCTION - ViewFMatrixKokkos(T* some_matrix, size_t dim1, size_t dim2, - size_t dim3, size_t dim4, size_t dim5, - size_t dim6, size_t dim7); - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m, - size_t n) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m, - size_t n, size_t o) const; - - KOKKOS_INLINE_FUNCTION - size_t size() const; - - KOKKOS_INLINE_FUNCTION - size_t extent() const; - - KOKKOS_INLINE_FUNCTION - size_t dims(size_t i) const; - - KOKKOS_INLINE_FUNCTION - size_t order() const; - - KOKKOS_INLINE_FUNCTION - T* pointer() const; - - KOKKOS_INLINE_FUNCTION - ~ViewFMatrixKokkos(); - -}; // end of ViewFMatrixKokkos - -// Default constructor -template -KOKKOS_INLINE_FUNCTION -ViewFMatrixKokkos::ViewFMatrixKokkos() { - length_ = order_ = 0; - for (int i = 0; i < 7; i++) { - dims_[i] = 0; - } -} - -// Overloaded 1D constructor -template -KOKKOS_INLINE_FUNCTION -ViewFMatrixKokkos::ViewFMatrixKokkos(T* some_matrix, size_t dim1) { - dims_[0] = dim1; - order_ = 1; - length_ = dim1; - this_matrix_ = some_matrix; -} - -// Overloaded 2D constructor -template -KOKKOS_INLINE_FUNCTION -ViewFMatrixKokkos::ViewFMatrixKokkos(T* some_matrix, size_t dim1, - size_t dim2) { - dims_[0] = dim1; - dims_[1] = dim2; - order_ = 2; - length_ = (dim1 * dim2); - this_matrix_ = some_matrix; -} - -// Overloaded 3D constructor -template -KOKKOS_INLINE_FUNCTION -ViewFMatrixKokkos::ViewFMatrixKokkos(T* some_matrix, size_t dim1, - size_t dim2, size_t dim3) { - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - order_ = 3; - length_ = (dim1 * dim2 * dim3); - this_matrix_ = some_matrix; -} - -// Overloaded 4D constructor -template -KOKKOS_INLINE_FUNCTION -ViewFMatrixKokkos::ViewFMatrixKokkos(T* some_matrix, size_t dim1, - size_t dim2, size_t dim3, - size_t dim4) { - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - order_ = 4; - length_ = (dim1 * dim2 * dim3 * dim4); - this_matrix_ = some_matrix; -} - -// Overloaded 5D constructor -template -KOKKOS_INLINE_FUNCTION -ViewFMatrixKokkos::ViewFMatrixKokkos(T* some_matrix, size_t dim1, - size_t dim2, size_t dim3, - size_t dim4, size_t dim5) { - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - dims_[4] = dim5; - order_ = 5; - length_ = (dim1 * dim2 * dim3 * dim4 * dim5); - this_matrix_ = some_matrix; -} - -// Overloaded 6D constructor -template -KOKKOS_INLINE_FUNCTION -ViewFMatrixKokkos::ViewFMatrixKokkos(T* some_matrix, size_t dim1, - size_t dim2, size_t dim3, - size_t dim4, size_t dim5, - size_t dim6) { - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - dims_[4] = dim5; - dims_[5] = dim6; - order_ = 6; - length_ = (dim1 * dim2 * dim3 * dim4 * dim5 * dim6); - this_matrix_ = some_matrix; -} - -// Overloaded 6D constructor -template -KOKKOS_INLINE_FUNCTION -ViewFMatrixKokkos::ViewFMatrixKokkos(T* some_matrix, size_t dim1, - size_t dim2, size_t dim3, - size_t dim4, size_t dim5, - size_t dim6, size_t dim7) { - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - dims_[4] = dim5; - dims_[5] = dim6; - dims_[6] = dim7; - order_ = 7; - length_ = (dim1 * dim2 * dim3 * dim4 * dim5 * dim6 * dim7); - this_matrix_ = some_matrix; -} - - -template -KOKKOS_INLINE_FUNCTION -T& ViewFMatrixKokkos::operator()(size_t i) const { - assert(order_ == 1 && "Tensor order (rank) does not match constructor in ViewFMatrixKokkos 1D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in ViewFMatrixKokkos 1D!"); - return this_matrix_[(i - 1)]; -} - -template -KOKKOS_INLINE_FUNCTION -T& ViewFMatrixKokkos::operator()(size_t i, size_t j) const { - assert(order_ == 2 && "Tensor order (rank) does not match constructor in ViewFMatrixKokkos 2D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in ViewFMatrixKokkos 2D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in ViewFMatrixKokkos 2D!"); - return this_matrix_[(i - 1) + ((j - 1) * dims_[0])]; -} - -template -KOKKOS_INLINE_FUNCTION -T& ViewFMatrixKokkos::operator()(size_t i, size_t j, size_t k) const -{ - assert(order_ == 3 && "Tensor order (rank) does not match constructor in ViewFMatrixKokkos 3D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in ViewFMatrixKokkos 3D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in ViewFMatrixKokkos 3D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in ViewFMatrixKokkos 3D!"); - - return this_matrix_[(i - 1) + ((j - 1) * dims_[0]) - + ((k - 1) * dims_[0] * dims_[1])]; -} - -template -KOKKOS_INLINE_FUNCTION -T& ViewFMatrixKokkos::operator()(size_t i, size_t j, size_t k, - size_t l) const { - assert(order_ == 4 && "Tensor order (rank) does not match constructor in ViewFMatrixKokkos 4D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in ViewFMatrixKokkos 4D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in ViewFMatrixKokkos 4D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in ViewFMatrixKokkos 4D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in ViewFMatrixKokkos 4D!"); - return this_matrix_[(i - 1) + ((j - 1) * dims_[0]) - + ((k - 1) * dims_[0] * dims_[1]) - + ((l - 1) * dims_[0] * dims_[1] * dims_[2])]; -} - -template -KOKKOS_INLINE_FUNCTION -T& ViewFMatrixKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m) const { - assert(order_ == 5 && "Tensor order (rank) does not match constructor in ViewFMatrixKokkos 5D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in ViewFMatrixKokkos 5D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in ViewFMatrixKokkos 5D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in ViewFMatrixKokkos 5D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in ViewFMatrixKokkos 5D!"); - assert(m >= 1 && m <= dims_[4] && "m is out of bounds in ViewFMatrixKokkos 5D!"); - return this_matrix_[(i - 1) + ((j - 1) * dims_[0]) - + ((k - 1) * dims_[0] * dims_[1]) - + ((l - 1) * dims_[0] * dims_[1] * dims_[2]) - + ((m - 1) * dims_[0] * dims_[1] * dims_[2] * dims_[3])]; -} - -template -KOKKOS_INLINE_FUNCTION -T& ViewFMatrixKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m, size_t n) const -{ - assert(order_ == 6 && "Tensor order (rank) does not match constructor in ViewFMatrixKokkos 6D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in ViewFMatrixKokkos 6D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in ViewFMatrixKokkos 6D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in ViewFMatrixKokkos 6D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in ViewFMatrixKokkos 6D!"); - assert(m >= 1 && m <= dims_[4] && "m is out of bounds in ViewFMatrixKokkos 6D!"); - assert(n >= 1 && n <= dims_[5] && "n is out of bounds in ViewFMatrixKokkos 6D!"); - return this_matrix_[(i - 1) + ((j - 1) * dims_[0]) - + ((k - 1) * dims_[0] * dims_[1]) - + ((l - 1) * dims_[0] * dims_[1] * dims_[2]) - + ((m - 1) * dims_[0] * dims_[1] * dims_[2] * dims_[3]) - + ((n - 1) * dims_[0] * dims_[1] * dims_[2] * dims_[3] * dims_[4])]; -} - -template -KOKKOS_INLINE_FUNCTION -T& ViewFMatrixKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m, size_t n, size_t o) const -{ - assert(order_ == 7 && "Tensor order (rank) does not match constructor in ViewFMatrixKokkos 7D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in ViewFMatrixKokkos 7D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in ViewFMatrixKokkos 7D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in ViewFMatrixKokkos 7D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in ViewFMatrixKokkos 7D!"); - assert(m >= 1 && m <= dims_[4] && "m is out of bounds in ViewFMatrixKokkos 7D!"); - assert(n >= 1 && n <= dims_[5] && "n is out of bounds in ViewFMatrixKokkos 7D!"); - assert(o >= 1 && o <= dims_[6] && "o is out of bounds in ViewFMatrixKokkos 7D!"); - return this_matrix_[(i - 1) + ((j - 1) * dims_[0]) - + ((k - 1) * dims_[0] * dims_[1]) - + ((l - 1) * dims_[0] * dims_[1] * dims_[2]) - + ((m - 1) * dims_[0] * dims_[1] * dims_[2] * dims_[3]) - + ((n - 1) * dims_[0] * dims_[1] * dims_[2] * dims_[3] * dims_[4]) - + ((o - 1) * dims_[0] * dims_[1] * dims_[2] * dims_[3] * dims_[4] * dims_[5])]; -} - -template -KOKKOS_INLINE_FUNCTION -size_t ViewFMatrixKokkos::size() const { - return length_; -} - -template -KOKKOS_INLINE_FUNCTION -size_t ViewFMatrixKokkos::extent() const { - return length_; -} - -template -KOKKOS_INLINE_FUNCTION -size_t ViewFMatrixKokkos::dims(size_t i) const { - i--; - assert(i < order_ && "ViewFMatrixKokkos order (rank) does not match constructor, dim[i] does not exist!"); - assert(i >= 0 && dims_[i]>0 && "Access to ViewFMatrixKokkos dims is out of bounds!"); - return dims_[i]; -} - -template -KOKKOS_INLINE_FUNCTION -size_t ViewFMatrixKokkos::order() const { - return order_; -} - -template -KOKKOS_INLINE_FUNCTION -T* ViewFMatrixKokkos::pointer() const { - return this_matrix_; -} - -template -KOKKOS_INLINE_FUNCTION -ViewFMatrixKokkos::~ViewFMatrixKokkos() {} - -//////////////////////////////////////////////////////////////////////////////// -// End of ViewFMatrixKokkos -//////////////////////////////////////////////////////////////////////////////// - - -///////////////////////// -// DFArrayKokkos: Dual type for managing data on both CPU and GPU. -///////////////////////// -template -class DFArrayKokkos { - - // this is manage - using TArray1D = Kokkos::DualView; - -private: - size_t dims_[7]; - size_t length_; - size_t order_; // tensor order (rank) - TArray1D this_array_; - -public: - // Data member to access host view - ViewFArray host; - - DFArrayKokkos(); - - DFArrayKokkos(size_t dim0, const std::string& tag_string = DEFAULTSTRINGARRAY); - - DFArrayKokkos(size_t dim0, size_t dim1, const std::string& tag_string = DEFAULTSTRINGARRAY); - - DFArrayKokkos (size_t dim0, size_t dim1, size_t dim2, const std::string& tag_string = DEFAULTSTRINGARRAY); - - DFArrayKokkos(size_t dim0, size_t dim1, size_t dim2, - size_t dim3, const std::string& tag_string = DEFAULTSTRINGARRAY); - - DFArrayKokkos(size_t dim0, size_t dim1, size_t dim2, - size_t dim3, size_t dim4, const std::string& tag_string = DEFAULTSTRINGARRAY); - - DFArrayKokkos(size_t dim0, size_t dim1, size_t dim2, - size_t dim3, size_t dim4, size_t dim5, const std::string& tag_string = DEFAULTSTRINGARRAY); - - DFArrayKokkos(size_t dim0, size_t dim1, size_t dim2, - size_t dim3, size_t dim4, size_t dim5, - size_t dim6, const std::string& tag_string = DEFAULTSTRINGARRAY); - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m, - size_t n) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m, - size_t n, size_t o) const; - - KOKKOS_INLINE_FUNCTION - DFArrayKokkos& operator=(const DFArrayKokkos& temp); - - // GPU Method - // Method that returns size - KOKKOS_INLINE_FUNCTION - size_t size() const; - - // Host Method - // Method that returns size - KOKKOS_INLINE_FUNCTION - size_t extent() const; - - KOKKOS_INLINE_FUNCTION - size_t dims(size_t i) const; - - KOKKOS_INLINE_FUNCTION - size_t order() const; - - // Method returns the raw device pointer of the Kokkos DualView - KOKKOS_INLINE_FUNCTION - T* device_pointer() const; - - // Method returns the raw host pointer of the Kokkos DualView - KOKKOS_INLINE_FUNCTION - T* host_pointer() const; - - // Method that update host view - void update_host(); - - // Method that update device view - void update_device(); - - // Deconstructor - KOKKOS_INLINE_FUNCTION - ~DFArrayKokkos (); - -}; // End of DFArrayKokkos declarations - -// Default constructor -template -DFArrayKokkos::DFArrayKokkos() { - length_ = order_ = 0; - for (int i = 0; i < 7; i++) { - dims_[i] = 0; - } -} - -// Overloaded 1D constructor -template -DFArrayKokkos::DFArrayKokkos(size_t dim0, const std::string& tag_string) { - - dims_[0] = dim0; - order_ = 1; - length_ = dim0; - this_array_ = TArray1D(tag_string, length_); - // Create host ViewFArray - host = ViewFArray (this_array_.h_view.data(), dim0); -} - -// Overloaded 2D constructor -template -DFArrayKokkos::DFArrayKokkos(size_t dim0, size_t dim1, const std::string& tag_string) { - - dims_[0] = dim0; - dims_[1] = dim1; - order_ = 2; - length_ = (dim0 * dim1); - this_array_ = TArray1D(tag_string, length_); - // Create host ViewFArray - host = ViewFArray (this_array_.h_view.data(), dim0, dim1); -} - -template -DFArrayKokkos::DFArrayKokkos(size_t dim0, size_t dim1, - size_t dim2, const std::string& tag_string) { - - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - order_ = 3; - length_ = (dim0 * dim1 * dim2); - this_array_ = TArray1D(tag_string, length_); - // Create host ViewFArray - host = ViewFArray (this_array_.h_view.data(), dim0, dim1, dim2); -} - -template -DFArrayKokkos::DFArrayKokkos(size_t dim0, size_t dim1, - size_t dim2, size_t dim3, const std::string& tag_string) { - - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - order_ = 4; - length_ = (dim0 * dim1 * dim2 * dim3); - this_array_ = TArray1D(tag_string, length_); - // Create host ViewFArray - host = ViewFArray (this_array_.h_view.data(), dim0, dim1, dim2, dim3); -} - -template -DFArrayKokkos::DFArrayKokkos(size_t dim0, size_t dim1, - size_t dim2, size_t dim3, - size_t dim4, const std::string& tag_string) { - - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - dims_[4] = dim4; - order_ = 5; - length_ = (dim0 * dim1 * dim2 * dim3 * dim4); - this_array_ = TArray1D(tag_string, length_); - // Create host ViewFArray - host = ViewFArray (this_array_.h_view.data(), dim0, dim1, dim2, dim3, dim4); -} - -template -DFArrayKokkos::DFArrayKokkos(size_t dim0, size_t dim1, - size_t dim2, size_t dim3, - size_t dim4, size_t dim5, const std::string& tag_string) { - - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - dims_[4] = dim4; - dims_[5] = dim5; - order_ = 6; - length_ = (dim0 * dim1 * dim2 * dim3 * dim4 * dim5); - this_array_ = TArray1D(tag_string, length_); - // Create host ViewFArray - host = ViewFArray (this_array_.h_view.data(), dim0, dim1, dim2, dim3, dim4, dim5); -} - -template -DFArrayKokkos::DFArrayKokkos(size_t dim0, size_t dim1, - size_t dim2, size_t dim3, - size_t dim4, size_t dim5, - size_t dim6, const std::string& tag_string) { - - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - dims_[4] = dim4; - dims_[5] = dim5; - dims_[6] = dim6; - order_ = 7; - length_ = (dim0 * dim1 * dim2 * dim3 * dim4 * dim5 * dim6); - this_array_ = TArray1D(tag_string, length_); - // Create host ViewFArray - host = ViewFArray (this_array_.h_view.data(), dim0, dim1, dim2, dim3, dim4, dim5, dim6); -} - -template -KOKKOS_INLINE_FUNCTION -T& DFArrayKokkos::operator()(size_t i) const { - assert(order_ == 1 && "Tensor order (rank) does not match constructor in DFArrayKokkos 1D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in DFArrayKokkos 1D!"); - return this_array_.d_view(i); -} - -template -KOKKOS_INLINE_FUNCTION -T& DFArrayKokkos::operator()(size_t i, size_t j) const { - assert(order_ == 2 && "Tensor order (rank) does not match constructor in DFArrayKokkos 2D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in DFArrayKokkos 2D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in DFArrayKokkos 2D!"); - return this_array_.d_view(i + (j * dims_[0])); -} - -template -KOKKOS_INLINE_FUNCTION -T& DFArrayKokkos::operator()(size_t i, size_t j, size_t k) const { - assert(order_ == 3 && "Tensor order (rank) does not match constructor in DFArrayKokkos 3D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in DFArrayKokkos 3D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in DFArrayKokkos 3D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in DFArrayKokkos 3D!"); - return this_array_.d_view(i + (j * dims_[0]) - + (k * dims_[0] * dims_[1])); -} - -template -KOKKOS_INLINE_FUNCTION -T& DFArrayKokkos::operator()(size_t i, size_t j, size_t k, size_t l) const { - assert(order_ == 4 && "Tensor order (rank) does not match constructor in DFArrayKokkos 4D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in DFArrayKokkos 4D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in DFArrayKokkos 4D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in DFArrayKokkos 4D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in DFArrayKokkos 4D!"); - return this_array_.d_view(i + (j * dims_[0]) - + (k * dims_[0] * dims_[1]) - + (l * dims_[0] * dims_[1] * dims_[2])); -} - -template -KOKKOS_INLINE_FUNCTION -T& DFArrayKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m) const { - assert(order_ == 5 && "Tensor order (rank) does not match constructor in DFArrayKokkos 5D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in DFArrayKokkos 5D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in DFArrayKokkos 5D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in DFArrayKokkos 5D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in DFArrayKokkos 5D!"); - assert(m >= 0 && m < dims_[4] && "m is out of bounds in DFArrayKokkos 5D!"); - return this_array_.d_view(i + (j * dims_[0]) - + (k * dims_[0] * dims_[1]) - + (l * dims_[0] * dims_[1] * dims_[2]) - + (m * dims_[0] * dims_[1] * dims_[2] * dims_[3])); -} - -template -KOKKOS_INLINE_FUNCTION -T& DFArrayKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m, size_t n) const { - assert(order_ == 6 && "Tensor order (rank) does not match constructor in DFArrayKokkos 6D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in DFArrayKokkos 6D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in DFArrayKokkos 6D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in DFArrayKokkos 6D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in DFArrayKokkos 6D!"); - assert(m >= 0 && m < dims_[4] && "m is out of bounds in DFArrayKokkos 6D!"); - assert(n >= 0 && n < dims_[5] && "n is out of bounds in DFArrayKokkos 6D!"); - return this_array_.d_view(i + (j * dims_[0]) - + (k * dims_[0] * dims_[1]) - + (l * dims_[0] * dims_[1] * dims_[2]) - + (m * dims_[0] * dims_[1] * dims_[2] * dims_[3]) - + (n * dims_[0] * dims_[1] * dims_[2] * dims_[3] * dims_[4])); -} - -template -KOKKOS_INLINE_FUNCTION -T& DFArrayKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m, size_t n, size_t o) const { - assert(order_ == 7 && "Tensor order (rank) does not match constructor in DFArrayKokkos 7D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in DFArrayKokkos 7D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in DFArrayKokkos 7D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in DFArrayKokkos 7D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in DFArrayKokkos 7D!"); - assert(m >= 0 && m < dims_[4] && "m is out of bounds in DFArrayKokkos 7D!"); - assert(n >= 0 && n < dims_[5] && "n is out of bounds in DFArrayKokkos 7D!"); - assert(o >= 0 && o < dims_[6] && "o is out of bounds in DFArrayKokkos 7D!"); - return this_array_.d_view(i + (j * dims_[0]) - + (k * dims_[0] * dims_[1]) - + (l * dims_[0] * dims_[1] * dims_[2]) - + (m * dims_[0] * dims_[1] * dims_[2] * dims_[3]) - + (n * dims_[0] * dims_[1] * dims_[2] * dims_[3] * dims_[4]) - + (o * dims_[0] * dims_[1] * dims_[2] * dims_[3] * dims_[4] * dims_[5])); -} - -template -KOKKOS_INLINE_FUNCTION -DFArrayKokkos& DFArrayKokkos::operator= (const DFArrayKokkos& temp) { - - // Do nothing if the assignment is of the form x = x - if (this != &temp) { - for (int iter = 0; iter < temp.order_; iter++){ - dims_[iter] = temp.dims_[iter]; - } // end for - - order_ = temp.order_; - length_ = temp.length_; - this_array_ = temp.this_array_; - host = temp.host; - } - - return *this; -} - -// Return size -template -KOKKOS_INLINE_FUNCTION -size_t DFArrayKokkos::size() const { - return length_; -} - -template -KOKKOS_INLINE_FUNCTION -size_t DFArrayKokkos::extent() const { - return length_; -} - -template -KOKKOS_INLINE_FUNCTION -size_t DFArrayKokkos::dims(size_t i) const { - assert(i < order_ && "DFArrayKokkos order (rank) does not match constructor, dim[i] does not exist!"); - assert(i >= 0 && dims_[i]>0 && "Access to DFArrayKokkos dims is out of bounds!"); - return dims_[i]; -} - -template -KOKKOS_INLINE_FUNCTION -size_t DFArrayKokkos::order() const { - return order_; -} - -template -KOKKOS_INLINE_FUNCTION -T* DFArrayKokkos::device_pointer() const { - return this_array_.d_view.data(); -} - -template -KOKKOS_INLINE_FUNCTION -T* DFArrayKokkos::host_pointer() const { - return this_array_.h_view.data(); -} - -template -void DFArrayKokkos::update_host() { - - this_array_.template modify(); - this_array_.template sync(); -} - -template -void DFArrayKokkos::update_device() { - - this_array_.template modify(); - this_array_.template sync(); -} - -template -KOKKOS_INLINE_FUNCTION -DFArrayKokkos::~DFArrayKokkos() {} -// End DFArrayKokkos - - -///////////////////////// -// DViewFArrayKokkos: The DView means dual view of the data, where data is on both CPU and GPU. -// -// This MATAR type is for accepting a pointer to data on the CPU via the constructor and then it copies the data -// data to the GPU where the member functions and overloads access the data on the GPU. The corresponding -// FArrayKokkos type creates memory on the GPU; likewise, the viewFArrayKokkos accesses data already on the GPU. -// To emphasize, the data must be on the CPU prior to calling the constructor for the DView data type. -///////////////////////// -template -class DViewFArrayKokkos { - - // this is always unmanaged - using TArray1DHost = Kokkos::View; - // this is manage - using TArray1D = Kokkos::View; - typename ExecSpace::memory_space memspace; - -private: - size_t dims_[7]; - size_t length_; - size_t order_; // tensor order (rank) - TArray1D this_array_; - TArray1DHost this_array_host_; - T * temp_inp_array_; - -public: - DViewFArrayKokkos(); - - DViewFArrayKokkos(T * inp_array, size_t dim0, const std::string& tag_string = DEFAULTSTRINGARRAY); - - DViewFArrayKokkos(T * inp_array, size_t dim0, size_t dim1, const std::string& tag_string = DEFAULTSTRINGARRAY); - - DViewFArrayKokkos(T * inp_array, size_t dim0, size_t dim1, size_t dim2, const std::string& tag_string = DEFAULTSTRINGARRAY); - - DViewFArrayKokkos(T * inp_array, size_t dim0, size_t dim1, size_t dim2, - size_t dim3, const std::string& tag_string = DEFAULTSTRINGARRAY); - - DViewFArrayKokkos(T * inp_array, size_t dim0, size_t dim1, size_t dim2, - size_t dim3, size_t dim4, const std::string& tag_string = DEFAULTSTRINGARRAY); - - DViewFArrayKokkos(T * inp_array, size_t dim0, size_t dim1, size_t dim2, - size_t dim3, size_t dim4, size_t dim5, const std::string& tag_string = DEFAULTSTRINGARRAY); - - DViewFArrayKokkos(T * inp_array, size_t dim0, size_t dim1, size_t dim2, - size_t dim3, size_t dim4, size_t dim5, - size_t dim6, const std::string& tag_string = DEFAULTSTRINGARRAY); - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m, - size_t n) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m, - size_t n, size_t o) const; - - KOKKOS_INLINE_FUNCTION - DViewFArrayKokkos& operator=(const DViewFArrayKokkos& temp); - - // GPU Method - // Method that returns size - KOKKOS_INLINE_FUNCTION - size_t size() const; - - // Host Method - // Method that returns size - KOKKOS_INLINE_FUNCTION - size_t extent() const; - - KOKKOS_INLINE_FUNCTION - size_t dims(size_t i) const; - - KOKKOS_INLINE_FUNCTION - size_t order() const; - - // Method returns the raw device pointer of the Kokkos View - KOKKOS_INLINE_FUNCTION - T* device_pointer() const; - - // Method returns the raw host pointer of the Kokkos View - KOKKOS_INLINE_FUNCTION - T* host_pointer() const; - - // Data member to access host view - ViewFArray host; - - // Method that update host view - void update_host(); - - // Method that update device view - void update_device(); - - // Deconstructor - KOKKOS_INLINE_FUNCTION - ~DViewFArrayKokkos (); -}; // End of DViewFArrayKokkos - - -// Default constructor -template -DViewFArrayKokkos::DViewFArrayKokkos() { - length_ = order_ = 0; - temp_inp_array_ = NULL; - for (int i = 0; i < 7; i++) { - dims_[i] = 0; - } -} - -// Overloaded 1D constructor -template -DViewFArrayKokkos::DViewFArrayKokkos(T * inp_array, size_t dim0, const std::string& tag_string) { - //using TArray1DHost = Kokkos::View; - //using TArray1D = Kokkos::View; - - dims_[0] = dim0; - order_ = 1; - length_ = dim0; - // Create a 1D host view of the external allocation - this_array_host_ = TArray1DHost(inp_array, length_); - // Assign temp point to inp_array pointer that is passed in - temp_inp_array_ = inp_array; - // Create a device copy of that host view - this_array_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_array_host_); - // Create host ViewFArray. Note: inp_array and this_array_host_.data() are the same pointer - host = ViewFArray (inp_array, dim0); -} - -// Overloaded 2D constructor -template -DViewFArrayKokkos::DViewFArrayKokkos(T * inp_array, size_t dim0, size_t dim1, const std::string& tag_string) { - //using TArray1DHost = Kokkos::View; - //using TArray1D = Kokkos::View; - //using TArray1Dtemp = TArray1D::HostMirror; - - dims_[0] = dim0; - dims_[1] = dim1; - order_ = 2; - length_ = (dim0 * dim1); - // Create a 1D host view of the external allocation - this_array_host_ = TArray1DHost(inp_array, length_); - // Assign temp point to inp_array pointer that is passed in - temp_inp_array_ = inp_array; - // Create a device copy of that host view - this_array_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_array_host_); - // Create host ViewFArray - host = ViewFArray (inp_array, dim0, dim1); -} - -template -DViewFArrayKokkos::DViewFArrayKokkos(T * inp_array, size_t dim0, size_t dim1, - size_t dim2, const std::string& tag_string) { - //using TArray1D = Kokkos::View; - - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - order_ = 3; - length_ = (dim0 * dim1 * dim2); - // Create a 1D host view of the external allocation - this_array_host_ = TArray1DHost(inp_array, length_); - // Assign temp point to inp_array pointer that is passed in - temp_inp_array_ = inp_array; - // Create a device copy of that host view - this_array_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_array_host_); - // Create host ViewFArray - host = ViewFArray (inp_array, dim0, dim1, dim2); -} - -template -DViewFArrayKokkos::DViewFArrayKokkos(T * inp_array, size_t dim0, size_t dim1, - size_t dim2, size_t dim3, const std::string& tag_string) { - //using TArray1D = Kokkos::View; - - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - order_ = 4; - length_ = (dim0 * dim1 * dim2 * dim3); - // Create a 1D host view of the external allocation - this_array_host_ = TArray1DHost(inp_array, length_); - // Assign temp point to inp_array pointer that is passed in - temp_inp_array_ = inp_array; - // Create a device copy of that host view - this_array_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_array_host_); - // Create host ViewFArray - host = ViewFArray (inp_array, dim0, dim1, dim2, dim3); -} - -template -DViewFArrayKokkos::DViewFArrayKokkos(T * inp_array, size_t dim0, size_t dim1, - size_t dim2, size_t dim3, - size_t dim4, const std::string& tag_string) { - - //using TArray1D = Kokkos::View; - - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - dims_[4] = dim4; - order_ = 5; - length_ = (dim0 * dim1 * dim2 * dim3 * dim4); - // Create a 1D host view of the external allocation - this_array_host_ = TArray1DHost(inp_array, length_); - // Assign temp point to inp_array pointer that is passed in - temp_inp_array_ = inp_array; - // Create a device copy of that host view - this_array_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_array_host_); - // Create host ViewFArray - host = ViewFArray (inp_array, dim0, dim1, dim2, dim3, dim4); -} - -template -DViewFArrayKokkos::DViewFArrayKokkos(T * inp_array, size_t dim0, size_t dim1, - size_t dim2, size_t dim3, - size_t dim4, size_t dim5, const std::string& tag_string) { - //using TArray1D = Kokkos::View; - - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - dims_[4] = dim4; - dims_[5] = dim5; - order_ = 6; - length_ = (dim0 * dim1 * dim2 * dim3 * dim4 * dim5); - // Create a 1D host view of the external allocation - this_array_host_ = TArray1DHost(inp_array, length_); - // Assign temp point to inp_array pointer that is passed in - temp_inp_array_ = inp_array; - // Create a device copy of that host view - this_array_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_array_host_); - // Create host ViewFArray - host = ViewFArray (inp_array, dim0, dim1, dim2, dim3, dim4, dim5); -} - -template -DViewFArrayKokkos::DViewFArrayKokkos(T * inp_array, size_t dim0, size_t dim1, - size_t dim2, size_t dim3, - size_t dim4, size_t dim5, - size_t dim6, const std::string& tag_string) { - //using TArray1D = Kokkos::View; - - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - dims_[4] = dim4; - dims_[5] = dim5; - dims_[6] = dim6; - order_ = 7; - length_ = (dim0 * dim1 * dim2 * dim3 * dim4 * dim5 * dim6); - // Create a 1D host view of the external allocation - this_array_host_ = TArray1DHost(inp_array, length_); - // Assign temp point to inp_array pointer that is passed in - temp_inp_array_ = inp_array; - // Create a device copy of that host view - this_array_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_array_host_); - // Create host ViewFArray - host = ViewFArray (inp_array, dim0, dim1, dim2, dim3, dim4, dim5, dim6); -} - -template -KOKKOS_INLINE_FUNCTION -T& DViewFArrayKokkos::operator()(size_t i) const { - assert(order_ == 1 && "Tensor order (rank) does not match constructor in DViewFArrayKokkos 1D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in DViewFArrayKokkos 1D!"); - return this_array_(i); -} - -template -KOKKOS_INLINE_FUNCTION -T& DViewFArrayKokkos::operator()(size_t i, size_t j) const { - assert(order_ == 2 && "Tensor order (rank) does not match constructor in DViewFArrayKokkos 2D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in DViewFArrayKokkos 2D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in DViewFArrayKokkos 2D!"); - return this_array_(i + (j * dims_[0])); -} - -template -KOKKOS_INLINE_FUNCTION -T& DViewFArrayKokkos::operator()(size_t i, size_t j, size_t k) const { - assert(order_ == 3 && "Tensor order (rank) does not match constructor in DViewFArrayKokkos 3D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in DViewFArrayKokkos 3D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in DViewFArrayKokkos 3D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in DViewFArrayKokkos 3D!"); - return this_array_(i + (j * dims_[0]) - + (k * dims_[0] * dims_[1])); -} - -template -KOKKOS_INLINE_FUNCTION -T& DViewFArrayKokkos::operator()(size_t i, size_t j, size_t k, size_t l) const { - assert(order_ == 4 && "Tensor order (rank) does not match constructor in DViewFArrayKokkos 4D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in DViewFArrayKokkos 4D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in DViewFArrayKokkos 4D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in DViewFArrayKokkos 4D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in DViewFArrayKokkos 4D!"); - return this_array_(i + (j * dims_[0]) - + (k * dims_[0] * dims_[1]) - + (l * dims_[0] * dims_[1] * dims_[2])); -} - -template -KOKKOS_INLINE_FUNCTION -T& DViewFArrayKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m) const { - assert(order_ == 5 && "Tensor order (rank) does not match constructor in DViewFArrayKokkos 5D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in DViewFArrayKokkos 5D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in DViewFArrayKokkos 5D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in DViewFArrayKokkos 5D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in DViewFArrayKokkos 5D!"); - assert(m >= 0 && m < dims_[4] && "m is out of bounds in DViewFArrayKokkos 5D!"); - return this_array_(i + (j * dims_[0]) - + (k * dims_[0] * dims_[1]) - + (l * dims_[0] * dims_[1] * dims_[2]) - + (m * dims_[0] * dims_[1] * dims_[2] * dims_[3])); -} - -template -KOKKOS_INLINE_FUNCTION -T& DViewFArrayKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m, size_t n) const { - assert(order_ == 6 && "Tensor order (rank) does not match constructor in DViewFArrayKokkos 6D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in DViewFArrayKokkos 6D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in DViewFArrayKokkos 6D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in DViewFArrayKokkos 6D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in DViewFArrayKokkos 6D!"); - assert(m >= 0 && m < dims_[4] && "m is out of bounds in DViewFArrayKokkos 6D!"); - assert(n >= 0 && n < dims_[5] && "n is out of bounds in DViewFArrayKokkos 6D!"); - return this_array_(i + (j * dims_[0]) - + (k * dims_[0] * dims_[1]) - + (l * dims_[0] * dims_[1] * dims_[2]) - + (m * dims_[0] * dims_[1] * dims_[2] * dims_[3]) - + (n * dims_[0] * dims_[1] * dims_[2] * dims_[3] * dims_[4])); -} - -template -KOKKOS_INLINE_FUNCTION -T& DViewFArrayKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m, size_t n, size_t o) const { - assert(order_ == 7 && "Tensor order (rank) does not match constructor in DViewFArrayKokkos 7D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in DViewFArrayKokkos 7D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in DViewFArrayKokkos 7D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in DViewFArrayKokkos 7D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in DViewFArrayKokkos 7D!"); - assert(m >= 0 && m < dims_[4] && "m is out of bounds in DViewFArrayKokkos 7D!"); - assert(n >= 0 && n < dims_[5] && "n is out of bounds in DViewFArrayKokkos 7D!"); - assert(o >= 0 && o < dims_[6] && "o is out of bounds in DViewFArrayKokkos 7D!"); - return this_array_(i + (j * dims_[0]) - + (k * dims_[0] * dims_[1]) - + (l * dims_[0] * dims_[1] * dims_[2]) - + (m * dims_[0] * dims_[1] * dims_[2] * dims_[3]) - + (n * dims_[0] * dims_[1] * dims_[2] * dims_[3] * dims_[4]) - + (o * dims_[0] * dims_[1] * dims_[2] * dims_[3] * dims_[4] * dims_[5])); -} - -template -KOKKOS_INLINE_FUNCTION -DViewFArrayKokkos& DViewFArrayKokkos::operator= (const DViewFArrayKokkos& temp) { - - // Do nothing if the assignment is of the form x = x - if (this != &temp) { - for (int iter = 0; iter < temp.order_; iter++){ - dims_[iter] = temp.dims_[iter]; - } // end for - - order_ = temp.order_; - length_ = temp.length_; - temp_inp_array_ = temp.temp_inp_array_; - this_array_host_ = temp.this_array_host_; - this_array_ = temp.this_array_; - host = temp.host; - } - - return *this; -} - -// Return size -template -KOKKOS_INLINE_FUNCTION -size_t DViewFArrayKokkos::size() const { - return length_; -} - -template -KOKKOS_INLINE_FUNCTION -size_t DViewFArrayKokkos::extent() const { - return length_; -} - -template -KOKKOS_INLINE_FUNCTION -size_t DViewFArrayKokkos::dims(size_t i) const { - assert(i < order_ && "DViewFArrayKokkos order (rank) does not match constructor, dim[i] does not exist!"); - assert(i >= 0 && dims_[i]>0 && "Access to DViewFArrayKokkos dims is out of bounds!"); - return dims_[i]; -} - -template -KOKKOS_INLINE_FUNCTION -size_t DViewFArrayKokkos::order() const { - return order_; -} - -template -KOKKOS_INLINE_FUNCTION -T* DViewFArrayKokkos::device_pointer() const { - return this_array_.data(); -} - -template -KOKKOS_INLINE_FUNCTION -T* DViewFArrayKokkos::host_pointer() const { - return this_array_host_.data(); -} - -template -void DViewFArrayKokkos::update_host() { - // Deep copy of device view to host view - deep_copy(this_array_host_, this_array_); -} - -template -void DViewFArrayKokkos::update_device() { - // Deep copy of host view to device view - deep_copy(this_array_, this_array_host_); -} - -template -KOKKOS_INLINE_FUNCTION -DViewFArrayKokkos::~DViewFArrayKokkos() {} -// End DViewFArrayKokkos - - -///////////////////////// -// DFMatrixKokkos -///////////////////////// -template -class DFMatrixKokkos { - - // this is manage - using TArray1D = Kokkos::DualView; - -private: - size_t dims_[7]; - size_t length_; - size_t order_; // tensor order (rank) - TArray1D this_matrix_; - -public: - DFMatrixKokkos(); - - DFMatrixKokkos(size_t dim1, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - DFMatrixKokkos(size_t dim1, size_t dim2, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - DFMatrixKokkos (size_t dim1, size_t dim2, size_t dim3, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - DFMatrixKokkos(size_t dim1, size_t dim2, size_t dim3, - size_t dim4, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - DFMatrixKokkos(size_t dim1, size_t dim2, size_t dim3, - size_t dim4, size_t dim5, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - DFMatrixKokkos(size_t dim1, size_t dim2, size_t dim3, - size_t dim4, size_t dim5, size_t dim6, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - DFMatrixKokkos(size_t dim1, size_t dim2, size_t dim3, - size_t dim4, size_t dim5, size_t dim6, - size_t dim7, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m, - size_t n) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m, - size_t n, size_t o) const; - - KOKKOS_INLINE_FUNCTION - DFMatrixKokkos& operator=(const DFMatrixKokkos& temp); - - // GPU Method - // Method that returns size - KOKKOS_INLINE_FUNCTION - size_t size() const; - - // Host Method - // Method that returns size - KOKKOS_INLINE_FUNCTION - size_t extent() const; - - KOKKOS_INLINE_FUNCTION - size_t dims(size_t i) const; - - KOKKOS_INLINE_FUNCTION - size_t order() const; - - // Method returns the raw device pointer of the Kokkos DualView - KOKKOS_INLINE_FUNCTION - T* device_pointer() const; - - // Method returns the raw host pointer of the Kokkos DualView - KOKKOS_INLINE_FUNCTION - T* host_pointer() const; - - // Data member to access host view - ViewFMatrix host; - - // Method that update host view - void update_host(); - - // Method that update device view - void update_device(); - - // Deconstructor - KOKKOS_INLINE_FUNCTION - ~DFMatrixKokkos (); -}; // End of DFMatrixKokkos declarations - -// Default constructor -template -DFMatrixKokkos::DFMatrixKokkos() { - length_ = order_ = 0; - for (int i = 0; i < 7; i++) { - dims_[i] = 0; - } -} - -// Overloaded 1D constructor -template -DFMatrixKokkos::DFMatrixKokkos(size_t dim1, const std::string& tag_string) { - - dims_[0] = dim1; - order_ = 1; - length_ = dim1; - this_matrix_ = TArray1D(tag_string, length_); - // Create host ViewFMatrix - host = ViewFMatrix (this_matrix_.h_view.data(), dim1); -} - -// Overloaded 2D constructor -template -DFMatrixKokkos::DFMatrixKokkos(size_t dim1, size_t dim2, const std::string& tag_string) { - - dims_[0] = dim1; - dims_[1] = dim2; - order_ = 2; - length_ = (dim1 * dim2); - this_matrix_ = TArray1D(tag_string, length_); - // Create host ViewFMatrix - host = ViewFMatrix (this_matrix_.h_view.data(), dim1, dim2); -} - -template -DFMatrixKokkos::DFMatrixKokkos(size_t dim1, size_t dim2, - size_t dim3, const std::string& tag_string) { - - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - order_ = 3; - length_ = (dim1 * dim2 * dim3); - this_matrix_ = TArray1D(tag_string, length_); - // Create host ViewFMatrix - host = ViewFMatrix (this_matrix_.h_view.data(), dim1, dim2, dim3); -} - -template -DFMatrixKokkos::DFMatrixKokkos(size_t dim1, size_t dim2, - size_t dim3, size_t dim4, const std::string& tag_string) { - - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - order_ = 4; - length_ = (dim1 * dim2 * dim3 * dim4); - this_matrix_ = TArray1D(tag_string, length_); - // Create host ViewFMatrix - host = ViewFMatrix (this_matrix_.h_view.data(), dim1, dim2, dim3, dim4); -} - -template -DFMatrixKokkos::DFMatrixKokkos(size_t dim1, size_t dim2, - size_t dim3, size_t dim4, - size_t dim5, const std::string& tag_string) { - - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - dims_[4] = dim5; - order_ = 5; - length_ = (dim1 * dim2 * dim3 * dim4 * dim5); - this_matrix_ = TArray1D(tag_string, length_); - // Create host ViewFMatrix - host = ViewFMatrix (this_matrix_.h_view.data(), dim1, dim2, dim3, dim4, dim5); -} - -template -DFMatrixKokkos::DFMatrixKokkos(size_t dim1, size_t dim2, - size_t dim3, size_t dim4, - size_t dim5, size_t dim6, const std::string& tag_string) { - - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - dims_[4] = dim5; - dims_[5] = dim6; - order_ = 6; - length_ = (dim1 * dim2 * dim3 * dim4 * dim5 * dim6); - this_matrix_ = TArray1D(tag_string, length_); - // Create host ViewFMatrix - host = ViewFMatrix (this_matrix_.h_view.data(), dim1, dim2, dim3, dim4, dim5, dim6); -} - -template -DFMatrixKokkos::DFMatrixKokkos(size_t dim1, size_t dim2, - size_t dim3, size_t dim4, - size_t dim5, size_t dim6, - size_t dim7, const std::string& tag_string) { - - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - dims_[4] = dim5; - dims_[5] = dim6; - dims_[6] = dim7; - order_ = 7; - length_ = (dim1 * dim2 * dim3 * dim4 * dim5 * dim6 * dim7); - this_matrix_ = TArray1D(tag_string, length_); - // Create host ViewFMatrix - host = ViewFMatrix (this_matrix_.h_view.data(), dim1, dim2, dim3, dim4, dim5, dim6, dim7); -} - -template -KOKKOS_INLINE_FUNCTION -T& DFMatrixKokkos::operator()(size_t i) const { - assert(order_ == 1 && "Tensor order (rank) does not match constructor in DFMatrixKokkos 1D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in DFMatrixKokkos 1D!"); - return this_matrix_.d_view((i - 1)); -} - -template -KOKKOS_INLINE_FUNCTION -T& DFMatrixKokkos::operator()(size_t i, size_t j) const { - assert(order_ == 2 && "Tensor order (rank) does not match constructor in DFMatrixKokkos 2D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in DFMatrixKokkos 2D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in DFMatrixKokkos 2D!"); - return this_matrix_.d_view((i - 1) + ((j - 1) * dims_[0])); -} - -template -KOKKOS_INLINE_FUNCTION -T& DFMatrixKokkos::operator()(size_t i, size_t j, size_t k) const { - assert(order_ == 3 && "Tensor order (rank) does not match constructor in DFMatrixKokkos 3D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in DFMatrixKokkos 3D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in DFMatrixKokkos 3D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in DFMatrixKokkos 3D!"); - return this_matrix_.d_view((i - 1) + ((j - 1) * dims_[0]) - + ((k - 1) * dims_[0] * dims_[1])); -} - -template -KOKKOS_INLINE_FUNCTION -T& DFMatrixKokkos::operator()(size_t i, size_t j, size_t k, size_t l) const { - assert(order_ == 4 && "Tensor order (rank) does not match constructor in DFMatrixKokkos 4D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in DFMatrixKokkos 4D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in DFMatrixKokkos 4D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in DFMatrixKokkos 4D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in DFMatrixKokkos 4D!"); - return this_matrix_.d_view((i - 1) + ((j - 1) * dims_[0]) - + ((k - 1) * dims_[0] * dims_[1]) - + ((l - 1) * dims_[0] * dims_[1] * dims_[2])); -} - -template -KOKKOS_INLINE_FUNCTION -T& DFMatrixKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m) const { - assert(order_ == 5 && "Tensor order (rank) does not match constructor in DFMatrixKokkos 5D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in DFMatrixKokkos 5D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in DFMatrixKokkos 5D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in DFMatrixKokkos 5D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in DFMatrixKokkos 5D!"); - assert(m >= 1 && m <= dims_[4] && "m is out of bounds in DFMatrixKokkos 5D!"); - return this_matrix_.d_view((i - 1) + ((j - 1) * dims_[0]) - + ((k - 1) * dims_[0] * dims_[1]) - + ((l - 1) * dims_[0] * dims_[1] * dims_[2]) - + ((m - 1) * dims_[0] * dims_[1] * dims_[2] * dims_[3])); -} - -template -KOKKOS_INLINE_FUNCTION -T& DFMatrixKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m, size_t n) const { - assert(order_ == 6 && "Tensor order (rank) does not match constructor in DFMatrixKokkos 6D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in DFMatrixKokkos 6D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in DFMatrixKokkos 6D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in DFMatrixKokkos 6D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in DFMatrixKokkos 6D!"); - assert(m >= 1 && m <= dims_[4] && "m is out of bounds in DFMatrixKokkos 6D!"); - assert(n >= 1 && n <= dims_[5] && "n is out of bounds in DFMatrixKokkos 6D!"); - return this_matrix_.d_view((i - 1) + ((j - 1) * dims_[0]) - + ((k - 1) * dims_[0] * dims_[1]) - + ((l - 1) * dims_[0] * dims_[1] * dims_[2]) - + ((m - 1) * dims_[0] * dims_[1] * dims_[2] * dims_[3]) - + ((n - 1) * dims_[0] * dims_[1] * dims_[2] * dims_[3] * dims_[4])); -} - -template -KOKKOS_INLINE_FUNCTION -T& DFMatrixKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m, size_t n, size_t o) const { - assert(order_ == 7 && "Tensor order (rank) does not match constructor in DFMatrixKokkos 7D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in DFMatrixKokkos 7D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in DFMatrixKokkos 7D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in DFMatrixKokkos 7D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in DFMatrixKokkos 7D!"); - assert(m >= 1 && m <= dims_[4] && "m is out of bounds in DFMatrixKokkos 7D!"); - assert(n >= 1 && n <= dims_[5] && "n is out of bounds in DFMatrixKokkos 7D!"); - assert(o >= 1 && o <= dims_[6] && "o is out of bounds in DFMatrixKokkos 7D!"); - return this_matrix_.d_view((i - 1) + ((j - 1) * dims_[0]) - + ((k - 1) * dims_[0] * dims_[1]) - + ((l - 1) * dims_[0] * dims_[1] * dims_[2]) - + ((m - 1) * dims_[0] * dims_[1] * dims_[2] * dims_[3]) - + ((n - 1) * dims_[0] * dims_[1] * dims_[2] * dims_[3] * dims_[4]) - + ((o - 1) * dims_[0] * dims_[1] * dims_[2] * dims_[3] * dims_[4] * dims_[5])); -} - -template -KOKKOS_INLINE_FUNCTION -DFMatrixKokkos& DFMatrixKokkos::operator= (const DFMatrixKokkos& temp) { - - // Do nothing if the assignment is of the form x = x - if (this != &temp) { - for (int iter = 0; iter < temp.order_; iter++){ - dims_[iter] = temp.dims_[iter]; - } // end for - - order_ = temp.order_; - length_ = temp.length_; - this_matrix_ = temp.this_matrix_; - host = temp.host; - } - - return *this; -} - -// Return size -template -KOKKOS_INLINE_FUNCTION -size_t DFMatrixKokkos::size() const { - return length_; -} - -template -KOKKOS_INLINE_FUNCTION -size_t DFMatrixKokkos::extent() const { - return length_; -} - -template -KOKKOS_INLINE_FUNCTION -size_t DFMatrixKokkos::dims(size_t i) const { - i--; - assert(i < order_ && "DFMatrixKokkos order (rank) does not match constructor, dim[i] does not exist!"); - assert(i >= 0 && dims_[i]>0 && "Access to DFMatrixKokkos dims is out of bounds!"); - return dims_[i]; -} - -template -KOKKOS_INLINE_FUNCTION -size_t DFMatrixKokkos::order() const { - return order_; -} - -template -KOKKOS_INLINE_FUNCTION -T* DFMatrixKokkos::device_pointer() const { - return this_matrix_.d_view.data(); -} - -template -KOKKOS_INLINE_FUNCTION -T* DFMatrixKokkos::host_pointer() const { - return this_matrix_.h_view.data(); -} - -template -void DFMatrixKokkos::update_host() { - - this_matrix_.template modify(); - this_matrix_.template sync(); -} - -template -void DFMatrixKokkos::update_device() { - - this_matrix_.template modify(); - this_matrix_.template sync(); -} - -template -KOKKOS_INLINE_FUNCTION -DFMatrixKokkos::~DFMatrixKokkos() {} -// End DFMatrixKokkos - - -///////////////////////// -// DViewFMatrixKokkos -///////////////////////// -template -class DViewFMatrixKokkos { - - // this is always unmanaged - using TArray1DHost = Kokkos::View; - // this is manage - using TArray1D = Kokkos::View; - typename ExecSpace::memory_space memspace; - -private: - size_t dims_[7]; - size_t length_; - size_t order_; // tensor order (rank) - TArray1D this_matrix_; - TArray1DHost this_matrix_host_; - T * temp_inp_matrix_; - -public: - DViewFMatrixKokkos(); - - DViewFMatrixKokkos(T * inp_matrix, size_t dim1, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - DViewFMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - DViewFMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, size_t dim3, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - DViewFMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, size_t dim3, - size_t dim4, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - DViewFMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, size_t dim3, - size_t dim4, size_t dim5, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - DViewFMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, size_t dim3, - size_t dim4, size_t dim5, size_t dim6, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - DViewFMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, size_t dim3, - size_t dim4, size_t dim5, size_t dim6, - size_t dim7, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m, - size_t n) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m, - size_t n, size_t o) const; - - KOKKOS_INLINE_FUNCTION - DViewFMatrixKokkos& operator=(const DViewFMatrixKokkos& temp); - - // GPU Method - // Method that returns size - KOKKOS_INLINE_FUNCTION - size_t size() const; - - // Host Method - // Method that returns size - KOKKOS_INLINE_FUNCTION - size_t extent() const; - - KOKKOS_INLINE_FUNCTION - size_t dims(size_t i) const; - - KOKKOS_INLINE_FUNCTION - size_t order() const; - - // Method returns the raw device pointer of the Kokkos View - KOKKOS_INLINE_FUNCTION - T* device_pointer() const; - - // Method returns the raw host pointer of the Kokkos View - KOKKOS_INLINE_FUNCTION - T* host_pointer() const; - - // Data member to access host view - ViewFMatrix host; - - // Method that update host view - void update_host(); - - // Method that update device view - void update_device(); - - // Deconstructor - KOKKOS_INLINE_FUNCTION - ~DViewFMatrixKokkos (); -}; // End of DViewFMatrixKokkos - - -// Default constructor -template -DViewFMatrixKokkos::DViewFMatrixKokkos() { - length_ = order_ = 0; - temp_inp_matrix_ = NULL; - for (int i = 0; i < 7; i++) { - dims_[i] = 0; - } -} - -// Overloaded 1D constructor -template -DViewFMatrixKokkos::DViewFMatrixKokkos(T * inp_matrix, size_t dim1, const std::string& tag_string) { - - dims_[0] = dim1; - order_ = 1; - length_ = dim1; - // Create a 1D host view of the external allocation - this_matrix_host_ = TArray1DHost(inp_matrix, length_); - // Assign temp point to inp_matrix pointer that is passed in - temp_inp_matrix_ = inp_matrix; - // Create a device copy of that host view - this_matrix_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_matrix_host_); - // Create host ViewFMatrix. Note: inp_matrix and this_matrix_host_.data() are the same pointer - host = ViewFMatrix (inp_matrix, dim1); -} - -// Overloaded 2D constructor -template -DViewFMatrixKokkos::DViewFMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, const std::string& tag_string) { - - dims_[0] = dim1; - dims_[1] = dim2; - order_ = 2; - length_ = (dim1 * dim2); - // Create a 1D host view of the external allocation - this_matrix_host_ = TArray1DHost(inp_matrix, length_); - // Assign temp point to inp_matrix pointer that is passed in - temp_inp_matrix_ = inp_matrix; - // Create a device copy of that host view - this_matrix_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_matrix_host_); - // Create host ViewFMatrix - host = ViewFMatrix (inp_matrix, dim1, dim2); -} - -template -DViewFMatrixKokkos::DViewFMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, - size_t dim3, const std::string& tag_string) { - - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - order_ = 3; - length_ = (dim1 * dim2 * dim3); - // Create a 1D host view of the external allocation - this_matrix_host_ = TArray1DHost(inp_matrix, length_); - // Assign temp point to inp_matrix pointer that is passed in - temp_inp_matrix_ = inp_matrix; - // Create a device copy of that host view - this_matrix_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_matrix_host_); - // Create host ViewFMatrix - host = ViewFMatrix (inp_matrix, dim1, dim2, dim3); -} - -template -DViewFMatrixKokkos::DViewFMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, - size_t dim3, size_t dim4, const std::string& tag_string) { - - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - order_ = 4; - length_ = (dim1 * dim2 * dim3 * dim4); - // Create a 1D host view of the external allocation - this_matrix_host_ = TArray1DHost(inp_matrix, length_); - // Assign temp point to inp_matrix pointer that is passed in - temp_inp_matrix_ = inp_matrix; - // Create a device copy of that host view - this_matrix_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_matrix_host_); - // Create host ViewFMatrix - host = ViewFMatrix (inp_matrix, dim1, dim2, dim3, dim4); -} - -template -DViewFMatrixKokkos::DViewFMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, - size_t dim3, size_t dim4, - size_t dim5, const std::string& tag_string) { - - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - dims_[4] = dim5; - order_ = 5; - length_ = (dim1 * dim2 * dim3 * dim4 * dim5); - // Create a 1D host view of the external allocation - this_matrix_host_ = TArray1DHost(inp_matrix, length_); - // Assign temp point to inp_matrix pointer that is passed in - temp_inp_matrix_ = inp_matrix; - // Create a device copy of that host view - this_matrix_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_matrix_host_); - // Create host ViewFMatrix - host = ViewFMatrix (inp_matrix, dim1, dim2, dim3, dim4, dim5); -} - -template -DViewFMatrixKokkos::DViewFMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, - size_t dim3, size_t dim4, - size_t dim5, size_t dim6, const std::string& tag_string) { - - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - dims_[4] = dim5; - dims_[5] = dim6; - order_ = 6; - length_ = (dim1 * dim2 * dim3 * dim4 * dim5 * dim6); - // Create a 1D host view of the external allocation - this_matrix_host_ = TArray1DHost(inp_matrix, length_); - // Assign temp point to inp_matrix pointer that is passed in - temp_inp_matrix_ = inp_matrix; - // Create a device copy of that host view - this_matrix_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_matrix_host_); - // Create host ViewFMatrix - host = ViewFMatrix (inp_matrix, dim1, dim2, dim3, dim4, dim5, dim6); -} - -template -DViewFMatrixKokkos::DViewFMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, - size_t dim3, size_t dim4, - size_t dim5, size_t dim6, - size_t dim7, const std::string& tag_string) { - - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - dims_[4] = dim5; - dims_[5] = dim6; - dims_[6] = dim7; - order_ = 7; - length_ = (dim1 * dim2 * dim3 * dim4 * dim5 * dim6 * dim7); - // Create a 1D host view of the external allocation - this_matrix_host_ = TArray1DHost(inp_matrix, length_); - // Assign temp point to inp_matrix pointer that is passed in - temp_inp_matrix_ = inp_matrix; - // Create a device copy of that host view - this_matrix_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_matrix_host_); - // Create host ViewFMatrix - host = ViewFMatrix (inp_matrix, dim1, dim2, dim3, dim4, dim5, dim6, dim7); -} - -template -KOKKOS_INLINE_FUNCTION -T& DViewFMatrixKokkos::operator()(size_t i) const { - assert(order_ == 1 && "Tensor order (rank) does not match constructor in DViewFMatrixKokkos 1D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in DViewFMatrixKokkos 1D!"); - return this_matrix_((i - 1)); -} - -template -KOKKOS_INLINE_FUNCTION -T& DViewFMatrixKokkos::operator()(size_t i, size_t j) const { - assert(order_ == 2 && "Tensor order (rank) does not match constructor in DViewFMatrixKokkos 2D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in DViewFMatrixKokkos 2D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in DViewFMatrixKokkos 2D!"); - return this_matrix_((i - 1) + ((j - 1) * dims_[0])); -} - -template -KOKKOS_INLINE_FUNCTION -T& DViewFMatrixKokkos::operator()(size_t i, size_t j, size_t k) const { - assert(order_ == 3 && "Tensor order (rank) does not match constructor in DViewFMatrixKokkos 3D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in DViewFMatrixKokkos 3D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in DViewFMatrixKokkos 3D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in DViewFMatrixKokkos 3D!"); - return this_matrix_((i - 1) + ((j - 1) * dims_[0]) - + ((k - 1) * dims_[0] * dims_[1])); -} - -template -KOKKOS_INLINE_FUNCTION -T& DViewFMatrixKokkos::operator()(size_t i, size_t j, size_t k, size_t l) const { - assert(order_ == 4 && "Tensor order (rank) does not match constructor in DViewFMatrixKokkos 4D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in DViewFMatrixKokkos 4D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in DViewFMatrixKokkos 4D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in DViewFMatrixKokkos 4D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in DViewFMatrixKokkos 4D!"); - return this_matrix_((i - 1) + ((j - 1) * dims_[0]) - + ((k - 1) * dims_[0] * dims_[1]) - + ((l - 1) * dims_[0] * dims_[1] * dims_[2])); -} - -template -KOKKOS_INLINE_FUNCTION -T& DViewFMatrixKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m) const { - assert(order_ == 5 && "Tensor order (rank) does not match constructor in DViewFMatrixKokkos 5D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in DViewFMatrixKokkos 5D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in DViewFMatrixKokkos 5D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in DViewFMatrixKokkos 5D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in DViewFMatrixKokkos 5D!"); - assert(m >= 1 && m <= dims_[4] && "m is out of bounds in DViewFMatrixKokkos 5D!"); - return this_matrix_((i - 1) + ((j - 1) * dims_[0]) - + ((k - 1) * dims_[0] * dims_[1]) - + ((l - 1) * dims_[0] * dims_[1] * dims_[2]) - + ((m - 1) * dims_[0] * dims_[1] * dims_[2] * dims_[3])); -} - -template -KOKKOS_INLINE_FUNCTION -T& DViewFMatrixKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m, size_t n) const { - assert(order_ == 6 && "Tensor order (rank) does not match constructor in DViewFMatrixKokkos 6D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in DViewFMatrixKokkos 6D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in DViewFMatrixKokkos 6D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in DViewFMatrixKokkos 6D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in DViewFMatrixKokkos 6D!"); - assert(m >= 1 && m <= dims_[4] && "m is out of bounds in DViewFMatrixKokkos 6D!"); - assert(n >= 1 && n <= dims_[5] && "n is out of bounds in DViewFMatrixKokkos 6D!"); - return this_matrix_((i - 1) + ((j - 1) * dims_[0]) - + ((k - 1) * dims_[0] * dims_[1]) - + ((l - 1) * dims_[0] * dims_[1] * dims_[2]) - + ((m - 1) * dims_[0] * dims_[1] * dims_[2] * dims_[3]) - + ((n - 1) * dims_[0] * dims_[1] * dims_[2] * dims_[3] * dims_[4])); -} - -template -KOKKOS_INLINE_FUNCTION -T& DViewFMatrixKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m, size_t n, size_t o) const { - assert(order_ == 7 && "Tensor order (rank) does not match constructor in DViewFMatrixKokkos 7D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in DViewFMatrixKokkos 7D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in DViewFMatrixKokkos 7D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in DViewFMatrixKokkos 7D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in DViewFMatrixKokkos 7D!"); - assert(m >= 1 && m <= dims_[4] && "m is out of bounds in DViewFMatrixKokkos 7D!"); - assert(n >= 1 && n <= dims_[5] && "n is out of bounds in DViewFMatrixKokkos 7D!"); - assert(o >= 1 && o <= dims_[6] && "o is out of bounds in DViewFMatrixKokkos 7D!"); - return this_matrix_((i - 1) + ((j - 1) * dims_[0]) - + ((k - 1) * dims_[0] * dims_[1]) - + ((l - 1) * dims_[0] * dims_[1] * dims_[2]) - + ((m - 1) * dims_[0] * dims_[1] * dims_[2] * dims_[3]) - + ((n - 1) * dims_[0] * dims_[1] * dims_[2] * dims_[3] * dims_[4]) - + ((o - 1) * dims_[0] * dims_[1] * dims_[2] * dims_[3] * dims_[4] * dims_[5])); -} - -template -KOKKOS_INLINE_FUNCTION -DViewFMatrixKokkos& DViewFMatrixKokkos::operator= (const DViewFMatrixKokkos& temp) { - - // Do nothing if the assignment is of the form x = x - if (this != &temp) { - for (int iter = 0; iter < temp.order_; iter++){ - dims_[iter] = temp.dims_[iter]; - } // end for - - order_ = temp.order_; - length_ = temp.length_; - temp_inp_matrix_ = temp.temp_inp_matrix_; - this_matrix_host_ = temp.this_matrix_host_; - this_matrix_ = temp.this_matrix_; - host = temp.host; - } - - return *this; -} - -// Return size -template -KOKKOS_INLINE_FUNCTION -size_t DViewFMatrixKokkos::size() const { - return length_; -} - -template -KOKKOS_INLINE_FUNCTION -size_t DViewFMatrixKokkos::extent() const { - return length_; -} - -template -KOKKOS_INLINE_FUNCTION -size_t DViewFMatrixKokkos::dims(size_t i) const { - i--; - assert(i < order_ && "DViewFMatrixKokkos order (rank) does not match constructor, dim[i] does not exist!"); - assert(i >= 0 && dims_[i]>0 && "Access to DViewFMatrixKokkos dims is out of bounds!"); - return dims_[i]; -} - -template -KOKKOS_INLINE_FUNCTION -size_t DViewFMatrixKokkos::order() const { - return order_; -} - -template -KOKKOS_INLINE_FUNCTION -T* DViewFMatrixKokkos::device_pointer() const { - return this_matrix_.data(); -} - -template -KOKKOS_INLINE_FUNCTION -T* DViewFMatrixKokkos::host_pointer() const { - return this_matrix_host_.data(); -} - -template -void DViewFMatrixKokkos::update_host() { - // Deep copy of device view to host view - deep_copy(this_matrix_host_, this_matrix_); -} - -template -void DViewFMatrixKokkos::update_device() { - // Deep copy of host view to device view - deep_copy(this_matrix_, this_matrix_host_); -} - -template -KOKKOS_INLINE_FUNCTION -DViewFMatrixKokkos::~DViewFMatrixKokkos() {} -// End DViewFMatrixKokkos - - -/*! \brief Kokkos version of the serial CArray class. - * - */ -template -class CArrayKokkos { - - using TArray1D = Kokkos::View; - -private: - size_t dims_[7]; - size_t order_; - size_t length_; - TArray1D this_array_; - -public: - CArrayKokkos(); - - CArrayKokkos(size_t dim0, const std::string& tag_string = DEFAULTSTRINGARRAY); - - CArrayKokkos(size_t dim0, size_t dim1, const std::string& tag_string = DEFAULTSTRINGARRAY); - - CArrayKokkos (size_t dim0, size_t dim1, size_t dim2, const std::string& tag_string = DEFAULTSTRINGARRAY); - - CArrayKokkos(size_t dim0, size_t dim1, size_t dim2, - size_t dim3, const std::string& tag_string = DEFAULTSTRINGARRAY); - - CArrayKokkos(size_t dim0, size_t dim1, size_t dim2, - size_t dim3, size_t dim4, const std::string& tag_string = DEFAULTSTRINGARRAY); - - CArrayKokkos(size_t dim0, size_t dim1, size_t dim2, - size_t dim3, size_t dim4, size_t dim5, const std::string& tag_string = DEFAULTSTRINGARRAY); - - CArrayKokkos(size_t dim0, size_t dim1, size_t dim2, - size_t dim3, size_t dim4, size_t dim5, - size_t dim6, const std::string& tag_string = DEFAULTSTRINGARRAY); - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m, - size_t n) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m, - size_t n, size_t o) const; - - KOKKOS_INLINE_FUNCTION - CArrayKokkos& operator=(const CArrayKokkos& temp); - - // GPU Method - // Method that returns size - KOKKOS_INLINE_FUNCTION - size_t size() const; - - // Host Method - // Method that returns size - KOKKOS_INLINE_FUNCTION - size_t extent() const; - - KOKKOS_INLINE_FUNCTION - size_t dims(size_t i) const; - - KOKKOS_INLINE_FUNCTION - size_t order() const; - - // Methods returns the raw pointer (most likely GPU) of the Kokkos View - KOKKOS_INLINE_FUNCTION - T* pointer() const; - - //return the view - KOKKOS_INLINE_FUNCTION - TArray1D get_kokkos_view() const; - - // Deconstructor - KOKKOS_INLINE_FUNCTION - ~CArrayKokkos (); -}; // End of CArrayKokkos - -// Default constructor -template -CArrayKokkos::CArrayKokkos() { - length_ = order_ = 0; - for (int i = 0; i < 7; i++) { - dims_[i] = 0; - } -} - -// Overloaded 1D constructor -template -CArrayKokkos::CArrayKokkos(size_t dim0, const std::string& tag_string) { - using TArray1D = Kokkos::View; - - dims_[0] = dim0; - order_ = 1; - length_ = dim0; - this_array_ = TArray1D(tag_string, length_); -} - -// Overloaded 2D constructor -template -CArrayKokkos::CArrayKokkos(size_t dim0, size_t dim1, const std::string& tag_string) { - using TArray1D = Kokkos::View; - - dims_[0] = dim0; - dims_[1] = dim1; - order_ = 2; - length_ = (dim0 * dim1); - this_array_ = TArray1D(tag_string, length_); -} - -template -CArrayKokkos::CArrayKokkos(size_t dim0, size_t dim1, - size_t dim2, const std::string& tag_string) { - using TArray1D = Kokkos::View; - - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - order_ = 3; - length_ = (dim0 * dim1 * dim2); - this_array_ = TArray1D(tag_string, length_); -} - -template -CArrayKokkos::CArrayKokkos(size_t dim0, size_t dim1, - size_t dim2, size_t dim3, const std::string& tag_string) { - using TArray1D = Kokkos::View; - - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - order_ = 4; - length_ = (dim0 * dim1 * dim2 * dim3); - this_array_ = TArray1D(tag_string, length_); -} - -template -CArrayKokkos::CArrayKokkos(size_t dim0, size_t dim1, - size_t dim2, size_t dim3, - size_t dim4, const std::string& tag_string) { - - using TArray1D = Kokkos::View; - - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - dims_[4] = dim4; - order_ = 5; - length_ = (dim0 * dim1 * dim2 * dim3 * dim4); - this_array_ = TArray1D(tag_string, length_); -} - -template -CArrayKokkos::CArrayKokkos(size_t dim0, size_t dim1, - size_t dim2, size_t dim3, - size_t dim4, size_t dim5, const std::string& tag_string) { - using TArray1D = Kokkos::View; - - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - dims_[4] = dim4; - dims_[5] = dim5; - order_ = 6; - length_ = (dim0 * dim1 * dim2 * dim3 * dim4 * dim5); - this_array_ = TArray1D(tag_string, length_); -} - -template -CArrayKokkos::CArrayKokkos(size_t dim0, size_t dim1, - size_t dim2, size_t dim3, - size_t dim4, size_t dim5, - size_t dim6, const std::string& tag_string) { - using TArray1D = Kokkos::View; - - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - dims_[4] = dim4; - dims_[5] = dim5; - dims_[6] = dim6; - order_ = 7; - length_ = (dim0 * dim1 * dim2 * dim3 * dim4 * dim5 * dim6); - this_array_ = TArray1D(tag_string, length_); -} - -template -KOKKOS_INLINE_FUNCTION -T& CArrayKokkos::operator()(size_t i) const { - assert(order_ == 1 && "Tensor order (rank) does not match constructor in CArrayKokkos 1D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in CArrayKokkos 1D!"); - return this_array_(i); -} - -template -KOKKOS_INLINE_FUNCTION -T& CArrayKokkos::operator()(size_t i, size_t j) const { - assert(order_ == 2 && "Tensor order (rank) does not match constructor in CArrayKokkos 2D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in CArrayKokkos 2D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in CArrayKokkos 2D!"); - return this_array_(j + (i * dims_[1])); -} - -template -KOKKOS_INLINE_FUNCTION -T& CArrayKokkos::operator()(size_t i, size_t j, size_t k) const { - assert(order_ == 3 && "Tensor order (rank) does not match constructor in CArrayKokkos 3D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in CArrayKokkos 3D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in CArrayKokkos 3D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in CArrayKokkos 3D!"); - return this_array_(k + (j * dims_[2]) - + (i * dims_[2] * dims_[1])); -} - -template -KOKKOS_INLINE_FUNCTION -T& CArrayKokkos::operator()(size_t i, size_t j, size_t k, size_t l) const { - assert(order_ == 4 && "Tensor order (rank) does not match constructor in CArrayKokkos 4D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in CArrayKokkos 4D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in CArrayKokkos 4D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in CArrayKokkos 4D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in CArrayKokkos 4D!"); - return this_array_(l + (k * dims_[3]) - + (j * dims_[3] * dims_[2]) - + (i * dims_[3] * dims_[2] * dims_[1])); -} - -template -KOKKOS_INLINE_FUNCTION -T& CArrayKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m) const { - assert(order_ == 5 && "Tensor order (rank) does not match constructor in CArrayKokkos 5D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in CArrayKokkos 5D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in CArrayKokkos 5D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in CArrayKokkos 5D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in CArrayKokkos 5D!"); - assert(m >= 0 && m < dims_[4] && "m is out of bounds in CArrayKokkos 5D!"); - return this_array_(m + (l * dims_[4]) - + (k * dims_[4] * dims_[3]) - + (j * dims_[4] * dims_[3] * dims_[2]) - + (i * dims_[4] * dims_[3] * dims_[2] * dims_[1])); -} - -template -KOKKOS_INLINE_FUNCTION -T& CArrayKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m, size_t n) const { - assert(order_ == 6 && "Tensor order (rank) does not match constructor in CArrayKokkos 6D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in CArrayKokkos 6D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in CArrayKokkos 6D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in CArrayKokkos 6D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in CArrayKokkos 6D!"); - assert(m >= 0 && m < dims_[4] && "m is out of bounds in CArrayKokkos 6D!"); - assert(n >= 0 && n < dims_[5] && "n is out of bounds in CArrayKokkos 6D!"); - return this_array_(n + (m * dims_[5]) - + (l * dims_[5] * dims_[4]) - + (k * dims_[5] * dims_[4] * dims_[3]) - + (j * dims_[5] * dims_[4] * dims_[3] * dims_[2]) - + (i * dims_[5] * dims_[4] * dims_[3] * dims_[2] * dims_[1])); -} - -template -KOKKOS_INLINE_FUNCTION -T& CArrayKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m, size_t n, size_t o) const { - assert(order_ == 7 && "Tensor order (rank) does not match constructor in CArrayKokkos 7D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in CArrayKokkos 7D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in CArrayKokkos 7D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in CArrayKokkos 7D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in CArrayKokkos 7D!"); - assert(m >= 0 && m < dims_[4] && "m is out of bounds in CArrayKokkos 7D!"); - assert(n >= 0 && n < dims_[5] && "n is out of bounds in CArrayKokkos 7D!"); - assert(o >= 0 && o < dims_[6] && "o is out of bounds in CArrayKokkos 7D!"); - return this_array_(o + (n * dims_[6]) - + (m * dims_[6] * dims_[5]) - + (l * dims_[6] * dims_[5] * dims_[4]) - + (k * dims_[6] * dims_[5] * dims_[4] * dims_[3]) - + (j * dims_[6] * dims_[5] * dims_[4] * dims_[3] * dims_[2]) - + (i * dims_[6] * dims_[5] * dims_[4] * dims_[3] * dims_[2] * dims_[1])); -} - -template -KOKKOS_INLINE_FUNCTION -CArrayKokkos& CArrayKokkos::operator= (const CArrayKokkos& temp) { - using TArray1D = Kokkos::View; - - // Do nothing if the assignment is of the form x = x - if (this != &temp) { - for (int iter = 0; iter < temp.order_; iter++){ - dims_[iter] = temp.dims_[iter]; - } // end for - - order_ = temp.order_; - length_ = temp.length_; - this_array_ = temp.this_array_; - } - - return *this; -} - -// Return size -template -KOKKOS_INLINE_FUNCTION -size_t CArrayKokkos::size() const { - return length_; -} - -template -KOKKOS_INLINE_FUNCTION -size_t CArrayKokkos::extent() const { - return length_; -} - -template -KOKKOS_INLINE_FUNCTION -size_t CArrayKokkos::dims(size_t i) const { - assert(i < order_ && "CArrayKokkos order (rank) does not match constructor, dim[i] does not exist!"); - assert(i >= 0 && dims_[i]>0 && "Access to CArrayKokkos dims is out of bounds!"); - return dims_[i]; -} - -template -KOKKOS_INLINE_FUNCTION -size_t CArrayKokkos::order() const { - return order_; -} - -template -KOKKOS_INLINE_FUNCTION -T* CArrayKokkos::pointer() const { - return this_array_.data(); -} - -//return the stored Kokkos view -template -KOKKOS_INLINE_FUNCTION -Kokkos::View CArrayKokkos::get_kokkos_view() const { - return this_array_; -} - -template -KOKKOS_INLINE_FUNCTION -CArrayKokkos::~CArrayKokkos() {} - -//////////////////////////////////////////////////////////////////////////////// -// End of CArrayKokkos -//////////////////////////////////////////////////////////////////////////////// - -/*! \brief Kokkos version of the serial ViewCArray class. - * - */ -template -class ViewCArrayKokkos { - -private: - size_t dims_[7]; - size_t order_; - size_t length_; // Length of 1D array - T* this_array_; - -public: - KOKKOS_INLINE_FUNCTION - ViewCArrayKokkos(); - - KOKKOS_INLINE_FUNCTION - ViewCArrayKokkos(T* some_array, size_t dim0); - - KOKKOS_INLINE_FUNCTION - ViewCArrayKokkos(T* some_array, size_t dim0, size_t dim1); - - KOKKOS_INLINE_FUNCTION - ViewCArrayKokkos(T* some_array, size_t dim0, size_t dim1, - size_t dim2); - - KOKKOS_INLINE_FUNCTION - ViewCArrayKokkos(T* some_array, size_t dim0, size_t dim1, - size_t dim2, size_t dim3); - - KOKKOS_INLINE_FUNCTION - ViewCArrayKokkos(T* some_array, size_t dim0, size_t dim1, - size_t dim2, size_t dim3, size_t dim4); - - KOKKOS_INLINE_FUNCTION - ViewCArrayKokkos(T* some_array, size_t dim0, size_t dim1, - size_t dim2, size_t dim3, size_t dim4, - size_t dim5); - - KOKKOS_INLINE_FUNCTION - ViewCArrayKokkos(T* some_array, size_t dim0, size_t dim1, - size_t dim2, size_t dim3, size_t dim4, - size_t dim5, size_t dim6);; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l) const; - - KOKKOS_INLINE_FUNCTION - T& operator() (size_t i, size_t j, size_t k, size_t l, size_t m) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m, - size_t n) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m, - size_t n, size_t o) const; - - KOKKOS_INLINE_FUNCTION - size_t size() const; - - KOKKOS_INLINE_FUNCTION - size_t extent() const; - - KOKKOS_INLINE_FUNCTION - size_t dims(size_t i) const; - - KOKKOS_INLINE_FUNCTION - size_t order() const; - - KOKKOS_INLINE_FUNCTION - T* pointer() const; - - KOKKOS_INLINE_FUNCTION - ~ViewCArrayKokkos(); - -}; // end of ViewCArrayKokkos - -// Default constructor -template -KOKKOS_INLINE_FUNCTION -ViewCArrayKokkos::ViewCArrayKokkos() { - length_ = order_ = 0; - this_array_ = NULL; - for (int i = 0; i < 7; i++) { - dims_[i] = 0; - } -} - -// Overloaded 1D constructor -template -KOKKOS_INLINE_FUNCTION -ViewCArrayKokkos::ViewCArrayKokkos(T* some_array, size_t dim0) { - dims_[0] = dim0; - order_ = 1; - length_ = dim0; - this_array_ = some_array; -} - -// Overloaded 2D constructor -template -KOKKOS_INLINE_FUNCTION -ViewCArrayKokkos::ViewCArrayKokkos(T* some_array, size_t dim0, - size_t dim1) { - dims_[0] = dim0; - dims_[1] = dim1; - order_ = 2; - length_ = (dim0 * dim1); - this_array_ = some_array; -} - -// Overloaded 3D constructor -template -KOKKOS_INLINE_FUNCTION -ViewCArrayKokkos::ViewCArrayKokkos(T* some_array, size_t dim0, - size_t dim1, size_t dim2) { - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - order_ = 3; - length_ = (dim0 * dim1 * dim2); - this_array_ = some_array; -} - -// Overloaded 4D constructor -template -KOKKOS_INLINE_FUNCTION -ViewCArrayKokkos::ViewCArrayKokkos(T* some_array, size_t dim0, - size_t dim1, size_t dim2, - size_t dim3) { - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - order_ = 4; - length_ = (dim0 * dim1 * dim2 * dim3); - this_array_ = some_array; -} - -// Overloaded 5D constructor -template -KOKKOS_INLINE_FUNCTION -ViewCArrayKokkos::ViewCArrayKokkos(T* some_array, size_t dim0, - size_t dim1, size_t dim2, - size_t dim3, size_t dim4) { - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - dims_[4] = dim4; - order_ = 5; - length_ = (dim0 * dim1 * dim2 * dim3 * dim4); - this_array_ = some_array; -} - -// Overloaded 6D constructor -template -KOKKOS_INLINE_FUNCTION -ViewCArrayKokkos::ViewCArrayKokkos(T* some_array, size_t dim0, - size_t dim1, size_t dim2, - size_t dim3, size_t dim4, - size_t dim5) { - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - dims_[4] = dim4; - dims_[5] = dim5; - order_ = 6; - length_ = (dim0 * dim1 * dim2 * dim3 * dim4 * dim5); - this_array_ = some_array; -} - -// Overloaded 7D constructor -template -KOKKOS_INLINE_FUNCTION -ViewCArrayKokkos::ViewCArrayKokkos(T* some_array, size_t dim0, - size_t dim1, size_t dim2, - size_t dim3, size_t dim4, - size_t dim5, size_t dim6) { - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - dims_[4] = dim4; - dims_[5] = dim5; - dims_[6] = dim6; - order_ = 7; - length_ = (dim0 * dim1 * dim2 * dim3 * dim4 * dim5 * dim6); - this_array_ = some_array; -} - -template -KOKKOS_INLINE_FUNCTION -T& ViewCArrayKokkos::operator()(size_t i) const { - assert(order_ == 1 && "Tensor order (rank) does not match constructor in ViewCArrayKokkos 1D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in ViewCArrayKokkos 1D!"); - return this_array_[i]; -} - -template -KOKKOS_INLINE_FUNCTION -T& ViewCArrayKokkos::operator()(size_t i, size_t j) const { - assert(order_ == 2 && "Tensor order (rank) does not match constructor in ViewCArrayKokkos 2D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in ViewCArrayKokkos 2D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in ViewCArrayKokkos 2D!"); - return this_array_[j + (i * dims_[1])]; -} - -template -KOKKOS_INLINE_FUNCTION -T& ViewCArrayKokkos::operator()(size_t i, size_t j, size_t k) const { - assert(order_ == 3 && "Tensor order (rank) does not match constructor in ViewCArrayKokkos 3D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in ViewCArrayKokkos 3D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in ViewCArrayKokkos 3D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in ViewCArrayKokkos 3D!"); - return this_array_[k + (j * dims_[2]) - + (i * dims_[2] * dims_[1])]; -} - -template -KOKKOS_INLINE_FUNCTION -T& ViewCArrayKokkos::operator()(size_t i, size_t j, size_t k, - size_t l) const { - assert(order_ == 4 && "Tensor order (rank) does not match constructor in ViewCArrayKokkos 4D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in ViewCArrayKokkos 4D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in ViewCArrayKokkos 4D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in ViewCArrayKokkos 4D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in ViewCArrayKokkos 4D!"); - return this_array_[l + (k * dims_[3]) - + (j * dims_[3] * dims_[2]) - + (i * dims_[3] * dims_[2] * dims_[1])]; -} - -template -KOKKOS_INLINE_FUNCTION -T& ViewCArrayKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m) const { - assert(order_ == 5 && "Tensor order (rank) does not match constructor in ViewCArrayKokkos 5D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in ViewCArrayKokkos 5D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in ViewCArrayKokkos 5D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in ViewCArrayKokkos 5D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in ViewCArrayKokkos 5D!"); - assert(m >= 0 && m < dims_[4] && "m is out of bounds in ViewCArrayKokkos 5D!"); - return this_array_[m + (l * dims_[4]) - + (k * dims_[4] * dims_[3]) - + (j * dims_[4] * dims_[3] * dims_[2]) - + (i * dims_[4] * dims_[3] * dims_[2] * dims_[1])]; -} - -template -KOKKOS_INLINE_FUNCTION -T& ViewCArrayKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m, size_t n) const { - assert(order_ == 6 && "Tensor order (rank) does not match constructor in ViewCArrayKokkos 6D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in ViewCArrayKokkos 6D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in ViewCArrayKokkos 6D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in ViewCArrayKokkos 6D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in ViewCArrayKokkos 6D!"); - assert(m >= 0 && m < dims_[4] && "m is out of bounds in ViewCArrayKokkos 6D!"); - assert(n >= 0 && n < dims_[5] && "n is out of bounds in ViewCArrayKokkos 6D!"); - return this_array_[n + (m * dims_[5]) - + (l * dims_[5] * dims_[4]) - + (k * dims_[5] * dims_[4] * dims_[3]) - + (j * dims_[5] * dims_[4] * dims_[3] * dims_[2]) - + (i * dims_[5] * dims_[4] * dims_[3] * dims_[2] * dims_[1])]; -} - -template -KOKKOS_INLINE_FUNCTION -T& ViewCArrayKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m, size_t n, size_t o) const { - assert(order_ == 7 && "Tensor order (rank) does not match constructor in ViewCArrayKokkos 7D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in ViewCArrayKokkos 7D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in ViewCArrayKokkos 7D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in ViewCArrayKokkos 7D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in ViewCArrayKokkos 7D!"); - assert(m >= 0 && m < dims_[4] && "m is out of bounds in ViewCArrayKokkos 7D!"); - assert(n >= 0 && n < dims_[5] && "n is out of bounds in ViewCArrayKokkos 7D!"); - assert(o >= 0 && o < dims_[6] && "o is out of bounds in ViewCArrayKokkos 7D!"); - return this_array_[o + (n * dims_[6]) - + (m * dims_[6] * dims_[5]) - + (l * dims_[6] * dims_[5] * dims_[4]) - + (k * dims_[6] * dims_[5] * dims_[4] * dims_[3]) - + (j * dims_[6] * dims_[5] * dims_[4] * dims_[3] * dims_[2]) - + (i * dims_[6] * dims_[5] * dims_[4] * dims_[3] * dims_[2] * dims_[1])]; -} - -template -KOKKOS_INLINE_FUNCTION -size_t ViewCArrayKokkos::size() const { - return length_; -} - -template -KOKKOS_INLINE_FUNCTION -size_t ViewCArrayKokkos::extent() const { - return length_; -} - -template -KOKKOS_INLINE_FUNCTION -size_t ViewCArrayKokkos::dims(size_t i) const { - assert(i < order_ && "ViewCArrayKokkos order (rank) does not match constructor, dim[i] does not exist!"); - assert(i >= 0 && dims_[i]>0 && "Access to ViewCArrayKokkos dims is out of bounds!"); - return dims_[i]; -} - -template -KOKKOS_INLINE_FUNCTION -size_t ViewCArrayKokkos::order() const { - return order_; -} - -template -KOKKOS_INLINE_FUNCTION -T* ViewCArrayKokkos::pointer() const { - return this_array_; -} - -template -KOKKOS_INLINE_FUNCTION -ViewCArrayKokkos::~ViewCArrayKokkos() {} - -//////////////////////////////////////////////////////////////////////////////// -// End of ViewCArrayKokkos -//////////////////////////////////////////////////////////////////////////////// - -/*! \brief Kokkos version of the serial CMatrix class. - * - */ -template -class CMatrixKokkos { - - using TArray1D = Kokkos::View; - -private: - size_t dims_[7]; - size_t order_; - size_t length_; - TArray1D this_matrix_; - -public: - CMatrixKokkos(); - - CMatrixKokkos(size_t dim1, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - CMatrixKokkos(size_t dim1, size_t dim2, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - CMatrixKokkos(size_t dim1, size_t dim2, size_t dim3, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - CMatrixKokkos(size_t dim1, size_t dim2, size_t dim3, - size_t dim4, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - CMatrixKokkos(size_t dim1, size_t dim2, size_t dim3, - size_t dim4, size_t dim5, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - CMatrixKokkos(size_t dim1, size_t dim2, size_t dim3, - size_t dim4, size_t dim5, size_t dim6, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - CMatrixKokkos(size_t dim1, size_t dim2, size_t dim3, - size_t dim4, size_t dim5, size_t dim6, - size_t dim7, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m, - size_t n) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m, - size_t n, size_t o) const; - - KOKKOS_INLINE_FUNCTION - CMatrixKokkos& operator=(const CMatrixKokkos &temp); - - KOKKOS_INLINE_FUNCTION - size_t size() const; - - KOKKOS_INLINE_FUNCTION - size_t extent() const; - - KOKKOS_INLINE_FUNCTION - size_t dims(size_t i) const; - - KOKKOS_INLINE_FUNCTION - size_t order() const; - - KOKKOS_INLINE_FUNCTION - T* pointer() const; - - //return the view - KOKKOS_INLINE_FUNCTION - TArray1D get_kokkos_view() const; - - KOKKOS_INLINE_FUNCTION - ~CMatrixKokkos(); - -}; // End of CMatrixKokkos - -// Default constructor -template -CMatrixKokkos::CMatrixKokkos() { - length_ = order_ = 0; - for (int i = 0; i < 7; i++) { - dims_[i] = 0; - } -} - -// Overloaded 1D constructor -template -CMatrixKokkos::CMatrixKokkos(size_t dim1, const std::string& tag_string) { - using TArray1D = Kokkos::View; - - dims_[0] = dim1; - order_ = 1; - length_ = dim1; - this_matrix_ = TArray1D(tag_string, length_); -} - -// Overloaded 2D constructor -template -CMatrixKokkos::CMatrixKokkos(size_t dim1, size_t dim2, const std::string& tag_string) { - using TArray1D = Kokkos::View; - - dims_[0] = dim1; - dims_[1] = dim2; - order_ = 2; - length_ = (dim1 * dim2); - this_matrix_ = TArray1D(tag_string, length_); -} - -// Overloaded 3D constructor -template -CMatrixKokkos::CMatrixKokkos(size_t dim1, size_t dim2, - size_t dim3, const std::string& tag_string) { - using TArray1D = Kokkos::View; - - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - order_ = 3; - length_ = (dim1 * dim2 * dim3); - this_matrix_ = TArray1D(tag_string, length_); -} - -// Overloaded 4D constructor -template -CMatrixKokkos::CMatrixKokkos(size_t dim1, size_t dim2, - size_t dim3, size_t dim4, const std::string& tag_string) { - using TArray1D = Kokkos::View; - - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - order_ = 4; - length_ = (dim1 * dim2 * dim3 * dim4); - this_matrix_ = TArray1D(tag_string, length_); -} - -// Overloaded 5D constructor -template -CMatrixKokkos::CMatrixKokkos(size_t dim1, size_t dim2, - size_t dim3, size_t dim4, - size_t dim5, const std::string& tag_string) { - - using TArray1D = Kokkos::View; - - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - dims_[4] = dim5; - order_ = 5; - length_ = (dim1 * dim2 * dim3 * dim4 * dim5); - this_matrix_ = TArray1D(tag_string, length_); -} - -// Overloaded 6D constructor -template -CMatrixKokkos::CMatrixKokkos(size_t dim1, size_t dim2, - size_t dim3, size_t dim4, - size_t dim5, size_t dim6, const std::string& tag_string) { - using TArray1D = Kokkos::View; - - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - dims_[4] = dim5; - dims_[5] = dim6; - order_ = 6; - length_ = (dim1 * dim2 * dim3 * dim4 * dim5 * dim6); - this_matrix_ = TArray1D(tag_string, length_); -} - -// Overloaded 7D constructor -template -CMatrixKokkos::CMatrixKokkos(size_t dim1, size_t dim2, - size_t dim3, size_t dim4, - size_t dim5, size_t dim6, - size_t dim7, const std::string& tag_string) { - using TArray1D = Kokkos::View; - - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - dims_[4] = dim5; - dims_[5] = dim6; - dims_[6] = dim7; - order_ = 7; - length_ = (dim1 * dim2 * dim3 * dim4 * dim5 * dim6 * dim7); - this_matrix_ = TArray1D(tag_string, length_); -} - -template -KOKKOS_INLINE_FUNCTION -T& CMatrixKokkos::operator()(size_t i) const { - assert(order_ == 1 && "Tensor order (rank) does not match constructor in CMatrixKokkos 1D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in CMatrixKokkos 1D!"); - return this_matrix_((i - 1)); -} - -template -KOKKOS_INLINE_FUNCTION -T& CMatrixKokkos::operator()(size_t i, size_t j) const { - assert(order_ == 2 && "Tensor order (rank) does not match constructor in CMatrixKokkos 2D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in CMatrixKokkos 2D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in CMatrixKokkos 2D!"); - return this_matrix_((j - 1) + ((i - 1) * dims_[1])); -} - -template -KOKKOS_INLINE_FUNCTION -T& CMatrixKokkos::operator()(size_t i, size_t j, size_t k) const { - assert(order_ == 3 && "Tensor order (rank) does not match constructor in CMatrixKokkos 3D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in CMatrixKokkos 3D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in CMatrixKokkos 3D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in CMatrixKokkos 3D!"); - return this_matrix_((k - 1) + ((j - 1) * dims_[2]) - + ((i - 1) * dims_[2] * dims_[1])); -} - -template -KOKKOS_INLINE_FUNCTION -T& CMatrixKokkos::operator()(size_t i, size_t j, size_t k, size_t l) const { - assert(order_ == 4 && "Tensor order (rank) does not match constructor in CMatrixKokkos 4D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in CMatrixKokkos 4D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in CMatrixKokkos 4D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in CMatrixKokkos 4D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in CMatrixKokkos 4D!"); - return this_matrix_((l - 1) + ((k - 1) * dims_[3]) - + ((j - 1) * dims_[3] * dims_[2]) - + ((i - 1) * dims_[3] * dims_[2] * dims_[1])); -} - -template -KOKKOS_INLINE_FUNCTION -T& CMatrixKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m) const { - assert(order_ == 5 && "Tensor order (rank) does not match constructor in CMatrixKokkos 5D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in CMatrixKokkos 5D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in CMatrixKokkos 5D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in CMatrixKokkos 5D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in CMatrixKokkos 5D!"); - assert(m >= 1 && m <= dims_[4] && "m is out of bounds in CMatrixKokkos 5D!"); - return this_matrix_((m - 1) + ((l - 1) * dims_[4]) - + ((k - 1) * dims_[4] * dims_[3]) - + ((j - 1) * dims_[4] * dims_[3] * dims_[2]) - + ((i - 1) * dims_[4] * dims_[3] * dims_[2] * dims_[1])); -} - -template -KOKKOS_INLINE_FUNCTION -T& CMatrixKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m, size_t n) const { - assert(order_ == 6 && "Tensor order (rank) does not match constructor in CMatrixKokkos 6D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in CMatrixKokkos 6D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in CMatrixKokkos 6D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in CMatrixKokkos 6D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in CMatrixKokkos 6D!"); - assert(m >= 1 && m <= dims_[4] && "m is out of bounds in CMatrixKokkos 6D!"); - assert(n >= 1 && n <= dims_[5] && "n is out of bounds in CMatrixKokkos 6D!"); - return this_matrix_((n - 1) + ((m - 1) * dims_[5]) - + ((l - 1) * dims_[5] * dims_[4]) - + ((k - 1) * dims_[5] * dims_[4] * dims_[3]) - + ((j - 1) * dims_[5] * dims_[4] * dims_[3] * dims_[2]) - + ((i - 1) * dims_[5] * dims_[4] * dims_[3] * dims_[2] * dims_[1])); -} - -template -KOKKOS_INLINE_FUNCTION -T& CMatrixKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m, size_t n, size_t o) const { - assert(order_ == 7 && "Tensor order (rank) does not match constructor in CMatrixKokkos 7D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in CMatrixKokkos 7D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in CMatrixKokkos 7D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in CMatrixKokkos 7D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in CMatrixKokkos 7D!"); - assert(m >= 1 && m <= dims_[4] && "m is out of bounds in CMatrixKokkos 7D!"); - assert(n >= 1 && n <= dims_[5] && "n is out of bounds in CMatrixKokkos 7D!"); - assert(o >= 1 && o <= dims_[6] && "o is out of bounds in CMatrixKokkos 7D!"); - return this_matrix_((o-1) + ((n - 1) * dims_[6]) - + ((m - 1) * dims_[6] * dims_[5]) - + ((l - 1) * dims_[6] * dims_[5] * dims_[4]) - + ((k - 1) * dims_[6] * dims_[5] * dims_[4] * dims_[3]) - + ((j - 1) * dims_[6] * dims_[5] * dims_[4] * dims_[3] * dims_[2]) - + ((i - 1) * dims_[6] * dims_[5] * dims_[4] * dims_[3] * dims_[2] * dims_[1])); -} - -// Overload = operator -// for object assignment THIS = CMatrixKokkos <> temp -template -KOKKOS_INLINE_FUNCTION -CMatrixKokkos & CMatrixKokkos::operator=(const CMatrixKokkos &temp) { - using TArray1D = Kokkos::View; - - if( this != &temp) { - for (int iter = 0; iter < temp.order_; iter++){ - dims_[iter] = temp.dims_[iter]; - } // end for - - order_ = temp.order_; - length_ = temp.length_; - this_matrix_ = temp.this_matrix_; - } - - return *this; -} - -template -KOKKOS_INLINE_FUNCTION -size_t CMatrixKokkos::size() const { - return length_; -} - -template -KOKKOS_INLINE_FUNCTION -size_t CMatrixKokkos::extent() const { - return length_; -} - -template -KOKKOS_INLINE_FUNCTION -size_t CMatrixKokkos::dims(size_t i) const { - i--; - assert(i < order_ && "CMatrixKokkos order (rank) does not match constructor, dim[i] does not exist!"); - assert(i >= 0 && dims_[i]>0 && "Access to CMatrixKokkos dims is out of bounds!"); - return dims_[i]; -} - -template -KOKKOS_INLINE_FUNCTION -size_t CMatrixKokkos::order() const { - return order_; -} - -template -KOKKOS_INLINE_FUNCTION -T* CMatrixKokkos::pointer() const { - return this_matrix_.data(); -} - -//return the stored Kokkos view -template -KOKKOS_INLINE_FUNCTION -Kokkos::View CMatrixKokkos::get_kokkos_view() const { - return this_matrix_; -} - -// Deconstructor -template -KOKKOS_INLINE_FUNCTION -CMatrixKokkos::~CMatrixKokkos() {} - -//////////////////////////////////////////////////////////////////////////////// -// End of CMatrixKokkos -//////////////////////////////////////////////////////////////////////////////// - -/*! \brief Kokkos version of the serial ViewCMatrix class. - * - */ -template -class ViewCMatrixKokkos { - -private: - size_t dims_[7]; - size_t order_; - size_t length_; - T* this_matrix_; - -public: - KOKKOS_INLINE_FUNCTION - ViewCMatrixKokkos(); - - KOKKOS_INLINE_FUNCTION - ViewCMatrixKokkos(T* some_matrix, size_t dim1); - - KOKKOS_INLINE_FUNCTION - ViewCMatrixKokkos(T* some_matrix, size_t dim1, size_t dim2); - - KOKKOS_INLINE_FUNCTION - ViewCMatrixKokkos(T* some_matrix, size_t dim1, size_t dim2, size_t dim3); - - KOKKOS_INLINE_FUNCTION - ViewCMatrixKokkos(T* some_matrix, size_t dim1, size_t dim2, size_t dim3, - size_t dim4); - - KOKKOS_INLINE_FUNCTION - ViewCMatrixKokkos(T* some_matrix, size_t dim1, size_t dim2, size_t dim3, - size_t dim4, size_t dim5); - - KOKKOS_INLINE_FUNCTION - ViewCMatrixKokkos(T* some_matrix, size_t dim1, size_t dim2, size_t dim3, - size_t dim4, size_t dim5, size_t dim6); - - KOKKOS_INLINE_FUNCTION - ViewCMatrixKokkos(T* some_matrix, size_t dim1, size_t dim2, size_t dim3, - size_t dim4, size_t dim5, size_t dim6, size_t dim7); - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j , size_t k) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k , size_t l) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m, size_t n) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m, size_t n, size_t o) const; - - KOKKOS_INLINE_FUNCTION - size_t size() const; - - KOKKOS_INLINE_FUNCTION - size_t extent() const; - - KOKKOS_INLINE_FUNCTION - size_t dims(size_t i) const; - - KOKKOS_INLINE_FUNCTION - size_t order() const; - - KOKKOS_INLINE_FUNCTION - T* pointer() const; - - KOKKOS_INLINE_FUNCTION - ~ViewCMatrixKokkos(); - -}; // End of ViewCMatrixKokkos - -// Default constructor -template -KOKKOS_INLINE_FUNCTION -ViewCMatrixKokkos::ViewCMatrixKokkos(){ - length_ = order_ = 0; - this_matrix_ = NULL; - for (int i = 0; i < 7; i++) { - dims_[i] = 0; - } -} - -// Overloaded 1D constructor -template -KOKKOS_INLINE_FUNCTION -ViewCMatrixKokkos::ViewCMatrixKokkos(T* some_matrix, size_t dim1) { - dims_[0] = dim1; - order_ = 1; - length_ = dim1; - this_matrix_ = some_matrix; -} - -// Overloaded 2D constructor -template -KOKKOS_INLINE_FUNCTION -ViewCMatrixKokkos::ViewCMatrixKokkos(T* some_matrix, size_t dim1, - size_t dim2) { - dims_[0] = dim1; - dims_[1] = dim2; - order_ = 2; - length_ = (dim1 * dim2); - this_matrix_ = some_matrix; -} - -// Overloaded 3D constructor -template -KOKKOS_INLINE_FUNCTION -ViewCMatrixKokkos::ViewCMatrixKokkos(T* some_matrix, size_t dim1, size_t dim2, - size_t dim3) { - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - order_ = 3; - length_ = (dim1 * dim2 * dim3); - this_matrix_ = some_matrix; -} - -// Overloaded 4D constructor -template -KOKKOS_INLINE_FUNCTION -ViewCMatrixKokkos::ViewCMatrixKokkos(T* some_matrix, size_t dim1, size_t dim2, - size_t dim3, size_t dim4) { - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - order_ = 4; - length_ = (dim1 * dim2 * dim3 * dim4); - this_matrix_ = some_matrix; -} - -// Overloaded 5D constructor -template -KOKKOS_INLINE_FUNCTION -ViewCMatrixKokkos::ViewCMatrixKokkos(T* some_matrix, size_t dim1, size_t dim2, - size_t dim3, size_t dim4, size_t dim5) { - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - dims_[4] = dim5; - order_ = 5; - length_ = (dim1 * dim2 * dim3 * dim4 * dim5); - this_matrix_ = some_matrix; -} - -// Overloaded 6D constructor -template -KOKKOS_INLINE_FUNCTION -ViewCMatrixKokkos::ViewCMatrixKokkos(T* some_matrix, size_t dim1, size_t dim2, - size_t dim3, size_t dim4, size_t dim5, - size_t dim6) { - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - dims_[4] = dim5; - dims_[5] = dim6; - order_ = 6; - length_ = (dim1 * dim2 * dim3 * dim4 * dim5 * dim6); - this_matrix_ = some_matrix; -} - -// Overloaded 7D constructor -template -KOKKOS_INLINE_FUNCTION -ViewCMatrixKokkos::ViewCMatrixKokkos(T* some_matrix, size_t dim1, size_t dim2, - size_t dim3, size_t dim4, size_t dim5, - size_t dim6, size_t dim7) { - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - dims_[4] = dim5; - dims_[5] = dim6; - dims_[6] = dim7; - order_ = 7; - length_ = (dim1 * dim2 * dim3 * dim4 * dim5 * dim6 * dim7); - this_matrix_ = some_matrix; -} - -template -KOKKOS_INLINE_FUNCTION -T& ViewCMatrixKokkos::operator()(size_t i) const { - assert(order_ == 1 && "Tensor order (rank) does not match constructor in ViewCMatrixKokkos 1D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in ViewCMatrixKokkos 1D!"); - return this_matrix_[(i - 1)]; -} - -template -KOKKOS_INLINE_FUNCTION -T& ViewCMatrixKokkos::operator()(size_t i, size_t j) const { - assert(order_ == 2 && "Tensor order (rank) does not match constructor in ViewCMatrixKokkos 2D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in ViewCMatrixKokkos 2D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in ViewCMatrixKokkos 2D!"); - return this_matrix_[(j - 1) + ((i - 1) * dims_[1])]; -} - -template -KOKKOS_INLINE_FUNCTION -T& ViewCMatrixKokkos::operator()(size_t i, size_t j, size_t k) const { - assert(order_ == 3 && "Tensor order (rank) does not match constructor in ViewCMatrixKokkos 3D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in ViewCMatrixKokkos 3D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in ViewCMatrixKokkos 3D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in ViewCMatrixKokkos 3D!"); - return this_matrix_[(k - 1) + ((j - 1) * dims_[2]) - + ((i - 1) * dims_[2] * dims_[1])]; -} - -template -KOKKOS_INLINE_FUNCTION -T& ViewCMatrixKokkos::operator()(size_t i, size_t j , size_t k, size_t l) const { - assert(order_ == 4 && "Tensor order (rank) does not match constructor in ViewCMatrixKokkos 4D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in ViewCMatrixKokkos 4D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in ViewCMatrixKokkos 4D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in ViewCMatrixKokkos 4D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in ViewCMatrixKokkos 4D!"); - return this_matrix_[(l - 1) + ((k - 1) * dims_[3]) - + ((j - 1) * dims_[3] * dims_[2]) - + ((i - 1) * dims_[3] * dims_[2] * dims_[1])]; -} - -template -KOKKOS_INLINE_FUNCTION -T& ViewCMatrixKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m) const { - assert(order_ == 5 && "Tensor order (rank) does not match constructor in ViewCMatrixKokkos 5D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds for ViewCMatrixKokkos 5D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds for ViewCMatrixKokkos 5D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds for ViewCMatrixKokkos 5D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds for ViewCMatrixKokkos 5D!"); - assert(m >= 1 && m <= dims_[4] && "m is out of bounds for ViewCMatrixKokkos 5D!"); - return this_matrix_[(m - 1) + ((l - 1) * dims_[4]) - + ((k - 1) * dims_[4] * dims_[3]) - + ((j - 1) * dims_[4] * dims_[3] * dims_[2]) - + ((i - 1) * dims_[4] * dims_[3] * dims_[2] * dims_[1])]; -} - -template -KOKKOS_INLINE_FUNCTION -T& ViewCMatrixKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m, size_t n) const { - assert(order_ == 6 && "Tensor order (rank) does not match constructor in ViewCMatrixKokkos 6D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds for ViewCMatrixKokkos 6D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds for ViewCMatrixKokkos 6D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds for ViewCMatrixKokkos 6D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds for ViewCMatrixKokkos 6D!"); - assert(m >= 1 && m <= dims_[4] && "m is out of bounds for ViewCMatrixKokkos 6D!"); - assert(n >= 1 && n <= dims_[5] && "n is out of bounds for ViewCMatrixKokkos 6D!"); - return this_matrix_[(n - 1) + ((m - 1) * dims_[5]) - + ((l - 1) * dims_[5] * dims_[4]) - + ((k - 1) * dims_[5] * dims_[4] * dims_[3]) - + ((j - 1) * dims_[5] * dims_[4] * dims_[3] * dims_[2]) - + ((i - 1) * dims_[5] * dims_[4] * dims_[3] * dims_[2] * dims_[1])]; -} - -template -KOKKOS_INLINE_FUNCTION -T& ViewCMatrixKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m, size_t n, size_t o) const { - assert(order_ == 7 && "Tensor order (rank) does not match constructor in ViewCMatrixKokkos 7D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds for ViewCMatrixKokkos 7D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds for ViewCMatrixKokkos 7D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds for ViewCMatrixKokkos 7D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds for ViewCMatrixKokkos 7D!"); - assert(m >= 1 && m <= dims_[4] && "m is out of bounds for ViewCMatrixKokkos 7D!"); - assert(n >= 1 && n <= dims_[5] && "n is out of bounds for ViewCMatrixKokkos 7D!"); - assert(o >= 1 && o <= dims_[6] && "o is out of bounds for ViewCMatrixKokkos 7D!"); - return this_matrix_[o + ((n - 1) * dims_[6]) - + ((m - 1) * dims_[6] * dims_[5]) - + ((l - 1) * dims_[6] * dims_[5] * dims_[4]) - + ((k - 1) * dims_[6] * dims_[5] * dims_[4] * dims_[3]) - + ((j - 1) * dims_[6] * dims_[5] * dims_[4] * dims_[3] * dims_[2]) - + ((i - 1) * dims_[6] * dims_[5] * dims_[4] * dims_[3] * dims_[2] * dims_[1])]; -} - - -template -KOKKOS_INLINE_FUNCTION -size_t ViewCMatrixKokkos::size() const { - return length_; -} - -template -KOKKOS_INLINE_FUNCTION -size_t ViewCMatrixKokkos::extent() const { - return length_; -} - -template -KOKKOS_INLINE_FUNCTION -size_t ViewCMatrixKokkos::dims(size_t i) const { - i--; - assert(i < order_ && "ViewCMatrixKokkos order (rank) does not match constructor, dim[i] does not exist!"); - assert(i >= 0 && dims_[i]>0 && "Access to ViewCMatrixKokkos dims is out of bounds!"); - return dims_[i]; -} - -template -KOKKOS_INLINE_FUNCTION -size_t ViewCMatrixKokkos::order() const { - return order_; -} - -template -KOKKOS_INLINE_FUNCTION -T* ViewCMatrixKokkos::pointer() const { - return this_matrix_; -} - -template -KOKKOS_INLINE_FUNCTION -ViewCMatrixKokkos::~ViewCMatrixKokkos() {} - -//////////////////////////////////////////////////////////////////////////////// -// End of ViewCMatrixKokkos -//////////////////////////////////////////////////////////////////////////////// - -///////////////////////// -// DCArrayKokkos: Dual type for managing data on both CPU and GPU. -///////////////////////// -template -class DCArrayKokkos { - - // this is manage - using TArray1D = Kokkos::DualView ; - -private: - size_t dims_[7]; - size_t length_; - size_t order_; // tensor order (rank) - TArray1D this_array_; - -public: - // Data member to access host view - ViewCArray host; - - DCArrayKokkos(); - - DCArrayKokkos(size_t dim0, const std::string& tag_string = DEFAULTSTRINGARRAY); - - DCArrayKokkos(size_t dim0, size_t dim1, const std::string& tag_string = DEFAULTSTRINGARRAY); - - DCArrayKokkos (size_t dim0, size_t dim1, size_t dim2, const std::string& tag_string = DEFAULTSTRINGARRAY); - - DCArrayKokkos(size_t dim0, size_t dim1, size_t dim2, - size_t dim3, const std::string& tag_string = DEFAULTSTRINGARRAY); - - DCArrayKokkos(size_t dim0, size_t dim1, size_t dim2, - size_t dim3, size_t dim4, const std::string& tag_string = DEFAULTSTRINGARRAY); - - DCArrayKokkos(size_t dim0, size_t dim1, size_t dim2, - size_t dim3, size_t dim4, size_t dim5, const std::string& tag_string = DEFAULTSTRINGARRAY); - - DCArrayKokkos(size_t dim0, size_t dim1, size_t dim2, - size_t dim3, size_t dim4, size_t dim5, - size_t dim6, const std::string& tag_string = DEFAULTSTRINGARRAY); - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m, - size_t n) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m, - size_t n, size_t o) const; - - KOKKOS_INLINE_FUNCTION - DCArrayKokkos& operator=(const DCArrayKokkos& temp); - - // GPU Method - // Method that returns size - KOKKOS_INLINE_FUNCTION - size_t size() const; - - // Host Method - // Method that returns size - KOKKOS_INLINE_FUNCTION - size_t extent() const; - - KOKKOS_INLINE_FUNCTION - size_t dims(size_t i) const; - - KOKKOS_INLINE_FUNCTION - size_t order() const; - - // Method returns the raw device pointer of the Kokkos DualView - KOKKOS_INLINE_FUNCTION - T* device_pointer() const; - - // Method returns the raw host pointer of the Kokkos DualView - KOKKOS_INLINE_FUNCTION - T* host_pointer() const; - - // Method returns kokkos dual view - KOKKOS_INLINE_FUNCTION - TArray1D get_kokkos_dual_view() const; - - // Method that update host view - void update_host(); - - // Method that update device view - void update_device(); - - // Deconstructor - KOKKOS_INLINE_FUNCTION - ~DCArrayKokkos (); -}; // End of DCArrayKokkos - - -// Default constructor -template -DCArrayKokkos::DCArrayKokkos() { - length_ = order_ = 0; - for (int i = 0; i < 7; i++) { - dims_[i] = 0; - } -} - -// Overloaded 1D constructor -template -DCArrayKokkos::DCArrayKokkos(size_t dim0, const std::string& tag_string) { - - dims_[0] = dim0; - order_ = 1; - length_ = dim0; - this_array_ = TArray1D(tag_string, length_); - // Create host ViewCArray - host = ViewCArray (this_array_.h_view.data(), dim0); -} - -// Overloaded 2D constructor -template -DCArrayKokkos::DCArrayKokkos(size_t dim0, size_t dim1, const std::string& tag_string) { - - dims_[0] = dim0; - dims_[1] = dim1; - order_ = 2; - length_ = (dim0 * dim1); - this_array_ = TArray1D(tag_string, length_); - // Create host ViewCArray - host = ViewCArray (this_array_.h_view.data(), dim0, dim1); -} - -template -DCArrayKokkos::DCArrayKokkos(size_t dim0, size_t dim1, - size_t dim2, const std::string& tag_string) { - - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - order_ = 3; - length_ = (dim0 * dim1 * dim2); - this_array_ = TArray1D(tag_string, length_); - // Create host ViewCArray - host = ViewCArray (this_array_.h_view.data(), dim0, dim1, dim2); -} - -template -DCArrayKokkos::DCArrayKokkos(size_t dim0, size_t dim1, - size_t dim2, size_t dim3, const std::string& tag_string) { - - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - order_ = 4; - length_ = (dim0 * dim1 * dim2 * dim3); - this_array_ = TArray1D(tag_string, length_); - // Create host ViewCArray - host = ViewCArray (this_array_.h_view.data(), dim0, dim1, dim2, dim3); -} - -template -DCArrayKokkos::DCArrayKokkos(size_t dim0, size_t dim1, - size_t dim2, size_t dim3, - size_t dim4, const std::string& tag_string) { - - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - dims_[4] = dim4; - order_ = 5; - length_ = (dim0 * dim1 * dim2 * dim3 * dim4); - this_array_ = TArray1D(tag_string, length_); - // Create host ViewCArray - host = ViewCArray (this_array_.h_view.data(), dim0, dim1, dim2, dim3, dim4); -} - -template -DCArrayKokkos::DCArrayKokkos(size_t dim0, size_t dim1, - size_t dim2, size_t dim3, - size_t dim4, size_t dim5, const std::string& tag_string) { - - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - dims_[4] = dim4; - dims_[5] = dim5; - order_ = 6; - length_ = (dim0 * dim1 * dim2 * dim3 * dim4 * dim5); - this_array_ = TArray1D(tag_string, length_); - // Create host ViewCArray - host = ViewCArray (this_array_.h_view.data(), dim0, dim1, dim2, dim3, dim4, dim5); -} - -template -DCArrayKokkos::DCArrayKokkos(size_t dim0, size_t dim1, - size_t dim2, size_t dim3, - size_t dim4, size_t dim5, - size_t dim6, const std::string& tag_string) { - - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - dims_[4] = dim4; - dims_[5] = dim5; - dims_[6] = dim6; - order_ = 7; - length_ = (dim0 * dim1 * dim2 * dim3 * dim4 * dim5 * dim6); - this_array_ = TArray1D(tag_string, length_); - // Create host ViewCArray - host = ViewCArray (this_array_.h_view.data(), dim0, dim1, dim2, dim3, dim4, dim5, dim6); -} - -template -KOKKOS_INLINE_FUNCTION -T& DCArrayKokkos::operator()(size_t i) const { - assert(order_ == 1 && "Tensor order (rank) does not match constructor in DCArrayKokkos 1D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in DCArrayKokkos 1D!"); - return this_array_.d_view(i); -} - -template -KOKKOS_INLINE_FUNCTION -T& DCArrayKokkos::operator()(size_t i, size_t j) const { - assert(order_ == 2 && "Tensor order (rank) does not match constructor in DCArrayKokkos 2D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in DCArrayKokkos 2D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in DCArrayKokkos 2D!"); - return this_array_.d_view(j + (i * dims_[1])); -} - -template -KOKKOS_INLINE_FUNCTION -T& DCArrayKokkos::operator()(size_t i, size_t j, size_t k) const { - assert(order_ == 3 && "Tensor order (rank) does not match constructor in DCArrayKokkos 3D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in DCArrayKokkos 3D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in DCArrayKokkos 3D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in DCArrayKokkos 3D!"); - return this_array_.d_view(k + (j * dims_[2]) - + (i * dims_[2] * dims_[1])); -} - -template -KOKKOS_INLINE_FUNCTION -T& DCArrayKokkos::operator()(size_t i, size_t j, size_t k, size_t l) const { - assert(order_ == 4 && "Tensor order (rank) does not match constructor in DCArrayKokkos 4D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in DCArrayKokkos 4D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in DCArrayKokkos 4D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in DCArrayKokkos 4D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in DCArrayKokkos 4D!"); - return this_array_.d_view(l + (k * dims_[3]) - + (j * dims_[3] * dims_[2]) - + (i * dims_[3] * dims_[2] * dims_[1])); -} - -template -KOKKOS_INLINE_FUNCTION -T& DCArrayKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m) const { - assert(order_ == 5 && "Tensor order (rank) does not match constructor in DCArrayKokkos 5D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in DCArrayKokkos 5D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in DCArrayKokkos 5D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in DCArrayKokkos 5D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in DCArrayKokkos 5D!"); - assert(m >= 0 && m < dims_[4] && "m is out of bounds in DCArrayKokkos 5D!"); - return this_array_.d_view(m + (l * dims_[4]) - + (k * dims_[4] * dims_[3]) - + (j * dims_[4] * dims_[3] * dims_[2]) - + (i * dims_[4] * dims_[3] * dims_[2] * dims_[1])); -} - -template -KOKKOS_INLINE_FUNCTION -T& DCArrayKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m, size_t n) const { - assert(order_ == 6 && "Tensor order (rank) does not match constructor in DCArrayKokkos 6D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in DCArrayKokkos 6D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in DCArrayKokkos 6D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in DCArrayKokkos 6D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in DCArrayKokkos 6D!"); - assert(m >= 0 && m < dims_[4] && "m is out of bounds in DCArrayKokkos 6D!"); - assert(n >= 0 && n < dims_[5] && "n is out of bounds in DCArrayKokkos 6D!"); - return this_array_.d_view(n + (m * dims_[5]) - + (l * dims_[5] * dims_[4]) - + (k * dims_[5] * dims_[4] * dims_[3]) - + (j * dims_[5] * dims_[4] * dims_[3] * dims_[2]) - + (i * dims_[5] * dims_[4] * dims_[3] * dims_[2] * dims_[1])); -} - -template -KOKKOS_INLINE_FUNCTION -T& DCArrayKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m, size_t n, size_t o) const { - assert(order_ == 7 && "Tensor order (rank) does not match constructor in DCArrayKokkos 7D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in DCArrayKokkos 7D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in DCArrayKokkos 7D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in DCArrayKokkos 7D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in DCArrayKokkos 7D!"); - assert(m >= 0 && m < dims_[4] && "m is out of bounds in DCArrayKokkos 7D!"); - assert(n >= 0 && n < dims_[5] && "n is out of bounds in DCArrayKokkos 7D!"); - assert(o >= 0 && o < dims_[6] && "o is out of bounds in DCArrayKokkos 7D!"); - return this_array_.d_view(o + (n * dims_[6]) - + (m * dims_[6] * dims_[5]) - + (l * dims_[6] * dims_[5] * dims_[4]) - + (k * dims_[6] * dims_[5] * dims_[4] * dims_[3]) - + (j * dims_[6] * dims_[5] * dims_[4] * dims_[3] * dims_[2]) - + (i * dims_[6] * dims_[5] * dims_[4] * dims_[3] * dims_[2] * dims_[1])); -} - -template -KOKKOS_INLINE_FUNCTION -DCArrayKokkos& DCArrayKokkos::operator= (const DCArrayKokkos& temp) { - - // Do nothing if the assignment is of the form x = x - if (this != &temp) { - for (int iter = 0; iter < temp.order_; iter++){ - dims_[iter] = temp.dims_[iter]; - } // end for - - order_ = temp.order_; - length_ = temp.length_; - this_array_ = temp.this_array_; - host = temp.host; - } - - return *this; -} - -// Return size -template -KOKKOS_INLINE_FUNCTION -size_t DCArrayKokkos::size() const { - return length_; -} - -template -KOKKOS_INLINE_FUNCTION -size_t DCArrayKokkos::extent() const { - return length_; -} - -template -KOKKOS_INLINE_FUNCTION -size_t DCArrayKokkos::dims(size_t i) const { - assert(i < order_ && "DCArrayKokkos order (rank) does not match constructor, dim[i] does not exist!"); - assert(i >= 0 && dims_[i]>0 && "Access to DCArrayKokkos dims is out of bounds!"); - return dims_[i]; -} - -template -KOKKOS_INLINE_FUNCTION -size_t DCArrayKokkos::order() const { - return order_; -} - -template -KOKKOS_INLINE_FUNCTION -T* DCArrayKokkos::device_pointer() const { - return this_array_.d_view.data(); -} - -template -KOKKOS_INLINE_FUNCTION -T* DCArrayKokkos::host_pointer() const { - return this_array_.h_view.data(); -} - -template -KOKKOS_INLINE_FUNCTION -Kokkos::DualView DCArrayKokkos::get_kokkos_dual_view() const { - return this_array_; -} - -template -void DCArrayKokkos::update_host() { - - this_array_.template modify(); - this_array_.template sync(); -} - -template -void DCArrayKokkos::update_device() { - - this_array_.template modify(); - this_array_.template sync(); -} - -template -KOKKOS_INLINE_FUNCTION -DCArrayKokkos::~DCArrayKokkos() {} -// End DCArrayKokkos - - -///////////////////////// -// DViewCArrayKokkos -///////////////////////// -template -class DViewCArrayKokkos { - - // this is always unmanaged - using TArray1DHost = Kokkos::View; - // this is manage - using TArray1D = Kokkos::View; - typename ExecSpace::memory_space memspace; - -private: - size_t dims_[7]; - size_t length_; - size_t order_; // tensor order (rank) - TArray1D this_array_; - TArray1DHost this_array_host_; - T * temp_inp_array_; - //typename Kokkos::View::HostMirror h_this_array_; - -public: - DViewCArrayKokkos(); - - DViewCArrayKokkos(T * inp_array, size_t dim0, const std::string& tag_string = DEFAULTSTRINGARRAY); - - DViewCArrayKokkos(T * inp_array, size_t dim0, size_t dim1, const std::string& tag_string = DEFAULTSTRINGARRAY); - - DViewCArrayKokkos(T * inp_array, size_t dim0, size_t dim1, size_t dim2, const std::string& tag_string = DEFAULTSTRINGARRAY); - - DViewCArrayKokkos(T * inp_array, size_t dim0, size_t dim1, size_t dim2, - size_t dim3, const std::string& tag_string = DEFAULTSTRINGARRAY); - - DViewCArrayKokkos(T * inp_array, size_t dim0, size_t dim1, size_t dim2, - size_t dim3, size_t dim4, const std::string& tag_string = DEFAULTSTRINGARRAY); - - DViewCArrayKokkos(T * inp_array, size_t dim0, size_t dim1, size_t dim2, - size_t dim3, size_t dim4, size_t dim5, const std::string& tag_string = DEFAULTSTRINGARRAY); - - DViewCArrayKokkos(T * inp_array, size_t dim0, size_t dim1, size_t dim2, - size_t dim3, size_t dim4, size_t dim5, - size_t dim6, const std::string& tag_string = DEFAULTSTRINGARRAY); - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m, - size_t n) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m, - size_t n, size_t o) const; - - KOKKOS_INLINE_FUNCTION - DViewCArrayKokkos& operator=(const DViewCArrayKokkos& temp); - - // GPU Method - // Method that returns size - KOKKOS_INLINE_FUNCTION - size_t size() const; - - // Host Method - // Method that returns size - KOKKOS_INLINE_FUNCTION - size_t extent() const; - - KOKKOS_INLINE_FUNCTION - size_t dims(size_t i) const; - - KOKKOS_INLINE_FUNCTION - size_t order() const; - - // Method returns the raw device pointer of the Kokkos View - KOKKOS_INLINE_FUNCTION - T* device_pointer() const; - - // Method returns the raw host pointer of the Kokkos View - KOKKOS_INLINE_FUNCTION - T* host_pointer() const; - - // Data member to access host view - ViewCArray host; - - // Method that update host view - void update_host(); - - // Method that update device view - void update_device(); - - // Deconstructor - KOKKOS_INLINE_FUNCTION - ~DViewCArrayKokkos (); -}; // End of DViewCArrayKokkos - - -// Default constructor -template -DViewCArrayKokkos::DViewCArrayKokkos() { - length_ = order_ = 0; - temp_inp_array_ = NULL; - for (int i = 0; i < 7; i++) { - dims_[i] = 0; - } -} - -// Overloaded 1D constructor -template -DViewCArrayKokkos::DViewCArrayKokkos(T * inp_array, size_t dim0, const std::string& tag_string) { - //using TArray1DHost = Kokkos::View; - //using TArray1D = Kokkos::View; - - dims_[0] = dim0; - order_ = 1; - length_ = dim0; - // Create a 1D host view of the external allocation - this_array_host_ = TArray1DHost(inp_array, length_); - // Assign temp point to inp_array pointer that is passed in - temp_inp_array_ = inp_array; - // Create a device copy of that host view - //this_array_ = create_mirror_view_and_copy(ExecSpace(), this_array_host_); - this_array_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_array_host_); - - // Create host ViewCArray. Note: inp_array and this_array_host_.data() are the same pointer - host = ViewCArray (inp_array, dim0); -} - -// Overloaded 2D constructor -template -DViewCArrayKokkos::DViewCArrayKokkos(T * inp_array, size_t dim0, size_t dim1, const std::string& tag_string) { - //using TArray1DHost = Kokkos::View; - //using TArray1D = Kokkos::View; - //using TArray1Dtemp = TArray1D::HostMirror; - - dims_[0] = dim0; - dims_[1] = dim1; - order_ = 2; - length_ = (dim0 * dim1); - // Create a 1D host view of the external allocation - this_array_host_ = TArray1DHost(inp_array, length_); - // Assign temp point to inp_array pointer that is passed in - temp_inp_array_ = inp_array; - //this_array_ = create_mirror_view_and_copy(ExecSpace(), this_array_host_); - this_array_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_array_host_); - // Create host ViewCArray - host = ViewCArray (inp_array, dim0, dim1); -} - -template -DViewCArrayKokkos::DViewCArrayKokkos(T * inp_array, size_t dim0, size_t dim1, - size_t dim2, const std::string& tag_string) { - //using TArray1D = Kokkos::View; - - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - order_ = 3; - length_ = (dim0 * dim1 * dim2); - // Create a 1D host view of the external allocation - this_array_host_ = TArray1DHost(inp_array, length_); - // Assign temp point to inp_array pointer that is passed in - temp_inp_array_ = inp_array; - // Create a device copy of that host view - //this_array_ = create_mirror_view_and_copy(ExecSpace(), this_array_host_); - this_array_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_array_host_); - // Create host ViewCArray - host = ViewCArray (inp_array, dim0, dim1, dim2); -} - -template -DViewCArrayKokkos::DViewCArrayKokkos(T * inp_array, size_t dim0, size_t dim1, - size_t dim2, size_t dim3, const std::string& tag_string) { - //using TArray1D = Kokkos::View; - - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - order_ = 4; - length_ = (dim0 * dim1 * dim2 * dim3); - // Create a 1D host view of the external allocation - this_array_host_ = TArray1DHost(inp_array, length_); - // Assign temp point to inp_array pointer that is passed in - temp_inp_array_ = inp_array; - // Create a device copy of that host view - //this_array_ = create_mirror_view_and_copy(ExecSpace(), this_array_host_); - this_array_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_array_host_); - // Create host ViewCArray - host = ViewCArray (inp_array, dim0, dim1, dim2, dim3); -} - -template -DViewCArrayKokkos::DViewCArrayKokkos(T * inp_array, size_t dim0, size_t dim1, - size_t dim2, size_t dim3, - size_t dim4, const std::string& tag_string) { - - //using TArray1D = Kokkos::View; - - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - dims_[4] = dim4; - order_ = 5; - length_ = (dim0 * dim1 * dim2 * dim3 * dim4); - // Create a 1D host view of the external allocation - this_array_host_ = TArray1DHost(inp_array, length_); - // Assign temp point to inp_array pointer that is passed in - temp_inp_array_ = inp_array; - // Create a device copy of that host view - //this_array_ = create_mirror_view_and_copy(ExecSpace(), this_array_host_); - this_array_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_array_host_); - // Create host ViewCArray - host = ViewCArray (inp_array, dim0, dim1, dim2, dim3, dim4); -} - -template -DViewCArrayKokkos::DViewCArrayKokkos(T * inp_array, size_t dim0, size_t dim1, - size_t dim2, size_t dim3, - size_t dim4, size_t dim5, const std::string& tag_string) { - //using TArray1D = Kokkos::View; - - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - dims_[4] = dim4; - dims_[5] = dim5; - order_ = 6; - length_ = (dim0 * dim1 * dim2 * dim3 * dim4 * dim5); - // Create a 1D host view of the external allocation - this_array_host_ = TArray1DHost(inp_array, length_); - // Assign temp point to inp_array pointer that is passed in - temp_inp_array_ = inp_array; - // Create a device copy of that host view - //this_array_ = create_mirror_view_and_copy(ExecSpace(), this_array_host_); - this_array_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_array_host_); - // Create host ViewCArray - host = ViewCArray (inp_array, dim0, dim1, dim2, dim3, dim4, dim5); -} - -template -DViewCArrayKokkos::DViewCArrayKokkos(T * inp_array, size_t dim0, size_t dim1, - size_t dim2, size_t dim3, - size_t dim4, size_t dim5, - size_t dim6, const std::string& tag_string) { - //using TArray1D = Kokkos::View; - - dims_[0] = dim0; - dims_[1] = dim1; - dims_[2] = dim2; - dims_[3] = dim3; - dims_[4] = dim4; - dims_[5] = dim5; - dims_[6] = dim6; - order_ = 7; - length_ = (dim0 * dim1 * dim2 * dim3 * dim4 * dim5 * dim6); - // Create a 1D host view of the external allocation - this_array_host_ = TArray1DHost(inp_array, length_); - // Assign temp point to inp_array pointer that is passed in - temp_inp_array_ = inp_array; - //this_array_ = create_mirror_view_and_copy(ExecSpace(), this_array_host_); - this_array_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_array_host_); - // Create host ViewCArray - host = ViewCArray (inp_array, dim0, dim1, dim2, dim3, dim4, dim5, dim6); -} - -template -KOKKOS_INLINE_FUNCTION -T& DViewCArrayKokkos::operator()(size_t i) const { - assert(order_ == 1 && "Tensor order (rank) does not match constructor in DViewCArrayKokkos 1D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in DViewCArrayKokkos 1D!"); - return this_array_(i); -} - -template -KOKKOS_INLINE_FUNCTION -T& DViewCArrayKokkos::operator()(size_t i, size_t j) const { - assert(order_ == 2 && "Tensor order (rank) does not match constructor in DViewCArrayKokkos 2D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in DViewCArrayKokkos 2D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in DViewCArrayKokkos 2D!"); - return this_array_(j + (i * dims_[1])); -} - -template -KOKKOS_INLINE_FUNCTION -T& DViewCArrayKokkos::operator()(size_t i, size_t j, size_t k) const { - assert(order_ == 3 && "Tensor order (rank) does not match constructor in DViewCArrayKokkos 3D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in DViewCArrayKokkos 3D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in DViewCArrayKokkos 3D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in DViewCArrayKokkos 3D!"); - return this_array_(k + (j * dims_[2]) - + (i * dims_[2] * dims_[1])); -} - -template -KOKKOS_INLINE_FUNCTION -T& DViewCArrayKokkos::operator()(size_t i, size_t j, size_t k, size_t l) const { - assert(order_ == 4 && "Tensor order (rank) does not match constructor in DViewCArrayKokkos 4D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in DViewCArrayKokkos 4D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in DViewCArrayKokkos 4D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in DViewCArrayKokkos 4D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in DViewCArrayKokkos 4D!"); - return this_array_(l + (k * dims_[3]) - + (j * dims_[3] * dims_[2]) - + (i * dims_[3] * dims_[2] * dims_[1])); -} - -template -KOKKOS_INLINE_FUNCTION -T& DViewCArrayKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m) const { - assert(order_ == 5 && "Tensor order (rank) does not match constructor in DViewCArrayKokkos 5D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in DViewCArrayKokkos 5D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in DViewCArrayKokkos 5D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in DViewCArrayKokkos 5D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in DViewCArrayKokkos 5D!"); - assert(m >= 0 && m < dims_[4] && "m is out of bounds in DViewCArrayKokkos 5D!"); - return this_array_(m + (l * dims_[4]) - + (k * dims_[4] * dims_[3]) - + (j * dims_[4] * dims_[3] * dims_[2]) - + (i * dims_[4] * dims_[3] * dims_[2] * dims_[1])); -} - -template -KOKKOS_INLINE_FUNCTION -T& DViewCArrayKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m, size_t n) const { - assert(order_ == 6 && "Tensor order (rank) does not match constructor in DViewCArrayKokkos 6D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in DViewCArrayKokkos 6D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in DViewCArrayKokkos 6D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in DViewCArrayKokkos 6D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in DViewCArrayKokkos 6D!"); - assert(m >= 0 && m < dims_[4] && "m is out of bounds in DViewCArrayKokkos 6D!"); - assert(n >= 0 && n < dims_[5] && "n is out of bounds in DViewCArrayKokkos 6D!"); - return this_array_(n + (m * dims_[5]) - + (l * dims_[5] * dims_[4]) - + (k * dims_[5] * dims_[4] * dims_[3]) - + (j * dims_[5] * dims_[4] * dims_[3] * dims_[2]) - + (i * dims_[5] * dims_[4] * dims_[3] * dims_[2] * dims_[1])); -} - -template -KOKKOS_INLINE_FUNCTION -T& DViewCArrayKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m, size_t n, size_t o) const { - assert(order_ == 7 && "Tensor order (rank) does not match constructor in DViewCArrayKokkos 7D!"); - assert(i >= 0 && i < dims_[0] && "i is out of bounds in DViewCArrayKokkos 7D!"); - assert(j >= 0 && j < dims_[1] && "j is out of bounds in DViewCArrayKokkos 7D!"); - assert(k >= 0 && k < dims_[2] && "k is out of bounds in DViewCArrayKokkos 7D!"); - assert(l >= 0 && l < dims_[3] && "l is out of bounds in DViewCArrayKokkos 7D!"); - assert(m >= 0 && m < dims_[4] && "m is out of bounds in DViewCArrayKokkos 7D!"); - assert(n >= 0 && n < dims_[5] && "n is out of bounds in DViewCArrayKokkos 7D!"); - assert(o >= 0 && o < dims_[6] && "o is out of bounds in DViewCArrayKokkos 7D!"); - return this_array_(o + (n * dims_[6]) - + (m * dims_[6] * dims_[5]) - + (l * dims_[6] * dims_[5] * dims_[4]) - + (k * dims_[6] * dims_[5] * dims_[4] * dims_[3]) - + (j * dims_[6] * dims_[5] * dims_[4] * dims_[3] * dims_[2]) - + (i * dims_[6] * dims_[5] * dims_[4] * dims_[3] * dims_[2] * dims_[1])); -} - -template -KOKKOS_INLINE_FUNCTION -DViewCArrayKokkos& DViewCArrayKokkos::operator= (const DViewCArrayKokkos& temp) { - //using TArray1D = Kokkos::View; - - // Do nothing if the assignment is of the form x = x - if (this != &temp) { - for (int iter = 0; iter < temp.order_; iter++){ - dims_[iter] = temp.dims_[iter]; - } // end for - - order_ = temp.order_; - length_ = temp.length_; - temp_inp_array_ = temp.temp_inp_array_; - this_array_host_ = temp.this_array_host_; - this_array_ = temp.this_array_; - host = temp.host; - } - - return *this; -} - -// Return size -template -KOKKOS_INLINE_FUNCTION -size_t DViewCArrayKokkos::size() const { - return length_; -} - -template -KOKKOS_INLINE_FUNCTION -size_t DViewCArrayKokkos::extent() const { - return length_; -} - -template -KOKKOS_INLINE_FUNCTION -size_t DViewCArrayKokkos::dims(size_t i) const { - assert(i < order_ && "DViewCArrayKokkos order (rank) does not match constructor, dim[i] does not exist!"); - assert(i >= 0 && dims_[i]>0 && "Access to DViewCArrayKokkos dims is out of bounds!"); - return dims_[i]; -} - -template -KOKKOS_INLINE_FUNCTION -size_t DViewCArrayKokkos::order() const { - return order_; -} - -template -KOKKOS_INLINE_FUNCTION -T* DViewCArrayKokkos::device_pointer() const { - return this_array_.data(); -} - -template -KOKKOS_INLINE_FUNCTION -T* DViewCArrayKokkos::host_pointer() const { - return this_array_host_.data(); -} - -template -void DViewCArrayKokkos::update_host() { - // Deep copy of device view to host view - deep_copy(this_array_host_, this_array_); -} - -template -void DViewCArrayKokkos::update_device() { - // Deep copy of host view to device view - deep_copy(this_array_, this_array_host_); -} - -template -KOKKOS_INLINE_FUNCTION -DViewCArrayKokkos::~DViewCArrayKokkos() {} -// End DViewCArrayKokkos - - -///////////////////////// -// DCMatrixKokkos -///////////////////////// -template -class DCMatrixKokkos { - - // this is manage - using TArray1D = Kokkos::DualView; - -private: - size_t dims_[7]; - size_t length_; - size_t order_; // tensor order (rank) - TArray1D this_matrix_; - -public: - // Data member to access host view - ViewCMatrix host; - - DCMatrixKokkos(); - - DCMatrixKokkos(size_t dim1, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - DCMatrixKokkos(size_t dim1, size_t dim2, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - DCMatrixKokkos (size_t dim1, size_t dim2, size_t dim3, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - DCMatrixKokkos(size_t dim1, size_t dim2, size_t dim3, - size_t dim4, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - DCMatrixKokkos(size_t dim1, size_t dim2, size_t dim3, - size_t dim4, size_t dim5, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - DCMatrixKokkos(size_t dim1, size_t dim2, size_t dim3, - size_t dim4, size_t dim5, size_t dim6, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - DCMatrixKokkos(size_t dim1, size_t dim2, size_t dim3, - size_t dim4, size_t dim5, size_t dim6, - size_t dim7, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m, - size_t n) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m, - size_t n, size_t o) const; - - KOKKOS_INLINE_FUNCTION - DCMatrixKokkos& operator=(const DCMatrixKokkos& temp); - - // GPU Method - // Method that returns size - KOKKOS_INLINE_FUNCTION - size_t size() const; - - // Host Method - // Method that returns size - KOKKOS_INLINE_FUNCTION - size_t extent() const; - - KOKKOS_INLINE_FUNCTION - size_t dims(size_t i) const; - - KOKKOS_INLINE_FUNCTION - size_t order() const; - - // Method returns the raw device pointer of the Kokkos DualView - KOKKOS_INLINE_FUNCTION - T* device_pointer() const; - - // Method returns the raw host pointer of the Kokkos DualView - KOKKOS_INLINE_FUNCTION - T* host_pointer() const; - - // Method that update host view - void update_host(); - - // Method that update device view - void update_device(); - - // Deconstructor - KOKKOS_INLINE_FUNCTION - ~DCMatrixKokkos (); - -}; // End of DCMatrixKokkos declarations - -// Default constructor -template -DCMatrixKokkos::DCMatrixKokkos() { - length_ = order_ = 0; - for (int i = 0; i < 7; i++) { - dims_[i] = 0; - } -} - -// Overloaded 1D constructor -template -DCMatrixKokkos::DCMatrixKokkos(size_t dim1, const std::string& tag_string) { - - dims_[0] = dim1; - order_ = 1; - length_ = dim1; - this_matrix_ = TArray1D(tag_string, length_); - // Create host ViewCMatrix - host = ViewCMatrix (this_matrix_.h_view.data(), dim1); -} - -// Overloaded 2D constructor -template -DCMatrixKokkos::DCMatrixKokkos(size_t dim1, size_t dim2, const std::string& tag_string) { - - dims_[0] = dim1; - dims_[1] = dim2; - order_ = 2; - length_ = (dim1 * dim2); - this_matrix_ = TArray1D(tag_string, length_); - // Create host ViewCMatrix - host = ViewCMatrix (this_matrix_.h_view.data(), dim1, dim2); -} - -template -DCMatrixKokkos::DCMatrixKokkos(size_t dim1, size_t dim2, - size_t dim3, const std::string& tag_string) { - - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - order_ = 3; - length_ = (dim1 * dim2 * dim3); - this_matrix_ = TArray1D(tag_string, length_); - // Create host ViewCMatrix - host = ViewCMatrix (this_matrix_.h_view.data(), dim1, dim2, dim3); -} - -template -DCMatrixKokkos::DCMatrixKokkos(size_t dim1, size_t dim2, - size_t dim3, size_t dim4, const std::string& tag_string) { - - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - order_ = 4; - length_ = (dim1 * dim2 * dim3 * dim4); - this_matrix_ = TArray1D(tag_string, length_); - // Create host ViewCMatrix - host = ViewCMatrix (this_matrix_.h_view.data(), dim1, dim2, dim3, dim4); -} - -template -DCMatrixKokkos::DCMatrixKokkos(size_t dim1, size_t dim2, - size_t dim3, size_t dim4, - size_t dim5, const std::string& tag_string) { - - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - dims_[4] = dim5; - order_ = 5; - length_ = (dim1 * dim2 * dim3 * dim4 * dim5); - this_matrix_ = TArray1D(tag_string, length_); - // Create host ViewCMatrix - host = ViewCMatrix (this_matrix_.h_view.data(), dim1, dim2, dim3, dim4, dim5); -} - -template -DCMatrixKokkos::DCMatrixKokkos(size_t dim1, size_t dim2, - size_t dim3, size_t dim4, - size_t dim5, size_t dim6, const std::string& tag_string) { - - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - dims_[4] = dim5; - dims_[5] = dim6; - order_ = 6; - length_ = (dim1 * dim2 * dim3 * dim4 * dim5 * dim6); - this_matrix_ = TArray1D(tag_string, length_); - // Create host ViewCMatrix - host = ViewCMatrix (this_matrix_.h_view.data(), dim1, dim2, dim3, dim4, dim5, dim6); -} - -template -DCMatrixKokkos::DCMatrixKokkos(size_t dim1, size_t dim2, - size_t dim3, size_t dim4, - size_t dim5, size_t dim6, - size_t dim7, const std::string& tag_string) { - - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - dims_[4] = dim5; - dims_[5] = dim6; - dims_[6] = dim7; - order_ = 7; - length_ = (dim1 * dim2 * dim3 * dim4 * dim5 * dim6 * dim7); - this_matrix_ = TArray1D(tag_string, length_); - // Create host ViewCMatrix - host = ViewCMatrix (this_matrix_.h_view.data(), dim1, dim2, dim3, dim4, dim5, dim6, dim7); -} - -template -KOKKOS_INLINE_FUNCTION -T& DCMatrixKokkos::operator()(size_t i) const { - assert(order_ == 1 && "Tensor order (rank) does not match constructor in DCMatrixKokkos 1D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in DCMatrixKokkos 1D!"); - return this_matrix_.d_view((i - 1)); -} - -template -KOKKOS_INLINE_FUNCTION -T& DCMatrixKokkos::operator()(size_t i, size_t j) const { - assert(order_ == 2 && "Tensor order (rank) does not match constructor in DCMatrixKokkos 2D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in DCMatrixKokkos 2D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in DCMatrixKokkos 2D!"); - return this_matrix_.d_view((j - 1) + ((i - 1) * dims_[1])); -} - -template -KOKKOS_INLINE_FUNCTION -T& DCMatrixKokkos::operator()(size_t i, size_t j, size_t k) const { - assert(order_ == 3 && "Tensor order (rank) does not match constructor in DCMatrixKokkos 3D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in DCMatrixKokkos 3D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in DCMatrixKokkos 3D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in DCMatrixKokkos 3D!"); - return this_matrix_.d_view((k - 1) + ((j - 1) * dims_[2]) - + ((i - 1) * dims_[2] * dims_[1])); -} - -template -KOKKOS_INLINE_FUNCTION -T& DCMatrixKokkos::operator()(size_t i, size_t j, size_t k, size_t l) const { - assert(order_ == 4 && "Tensor order (rank) does not match constructor in DCMatrixKokkos 4D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in DCMatrixKokkos 4D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in DCMatrixKokkos 4D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in DCMatrixKokkos 4D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in DCMatrixKokkos 4D!"); - return this_matrix_.d_view((l - 1) + ((k - 1) * dims_[3]) - + ((j - 1) * dims_[3] * dims_[2]) - + ((i - 1) * dims_[3] * dims_[2] * dims_[1])); -} - -template -KOKKOS_INLINE_FUNCTION -T& DCMatrixKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m) const { - assert(order_ == 5 && "Tensor order (rank) does not match constructor in DCMatrixKokkos 5D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in DCMatrixKokkos 5D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in DCMatrixKokkos 5D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in DCMatrixKokkos 5D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in DCMatrixKokkos 5D!"); - assert(m >= 1 && m <= dims_[4] && "m is out of bounds in DCMatrixKokkos 5D!"); - return this_matrix_.d_view((m - 1) + ((l - 1) * dims_[4]) - + ((k - 1) * dims_[4] * dims_[3]) - + ((j - 1) * dims_[4] * dims_[3] * dims_[2]) - + ((i - 1) * dims_[4] * dims_[3] * dims_[2] * dims_[1])); -} - -template -KOKKOS_INLINE_FUNCTION -T& DCMatrixKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m, size_t n) const { - assert(order_ == 6 && "Tensor order (rank) does not match constructor in DCMatrixKokkos 6D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in DCMatrixKokkos 6D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in DCMatrixKokkos 6D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in DCMatrixKokkos 6D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in DCMatrixKokkos 6D!"); - assert(m >= 1 && m <= dims_[4] && "m is out of bounds in DCMatrixKokkos 6D!"); - assert(n >= 1 && n <= dims_[5] && "n is out of bounds in DCMatrixKokkos 6D!"); - return this_matrix_.d_view((n - 1) + ((m - 1) * dims_[5]) - + ((l - 1) * dims_[5] * dims_[4]) - + ((k - 1) * dims_[5] * dims_[4] * dims_[3]) - + ((j - 1) * dims_[5] * dims_[4] * dims_[3] * dims_[2]) - + ((i - 1) * dims_[5] * dims_[4] * dims_[3] * dims_[2] * dims_[1])); -} - -template -KOKKOS_INLINE_FUNCTION -T& DCMatrixKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m, size_t n, size_t o) const { - assert(order_ == 7 && "Tensor order (rank) does not match constructor in DCMatrixKokkos 7D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in DCMatrixKokkos 7D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in DCMatrixKokkos 7D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in DCMatrixKokkos 7D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in DCMatrixKokkos 7D!"); - assert(m >= 1 && m <= dims_[4] && "m is out of bounds in DCMatrixKokkos 7D!"); - assert(n >= 1 && n <= dims_[5] && "n is out of bounds in DCMatrixKokkos 7D!"); - assert(o >= 1 && o <= dims_[6] && "o is out of bounds in DCMatrixKokkos 7D!"); - return this_matrix_.d_view((o-1) + ((n - 1) * dims_[6]) - + ((m - 1) * dims_[6] * dims_[5]) - + ((l - 1) * dims_[6] * dims_[5] * dims_[4]) - + ((k - 1) * dims_[6] * dims_[5] * dims_[4] * dims_[3]) - + ((j - 1) * dims_[6] * dims_[5] * dims_[4] * dims_[3] * dims_[2]) - + ((i - 1) * dims_[6] * dims_[5] * dims_[4] * dims_[3] * dims_[2] * dims_[1])); -} - -template -KOKKOS_INLINE_FUNCTION -DCMatrixKokkos& DCMatrixKokkos::operator= (const DCMatrixKokkos& temp) { - - // Do nothing if the assignment is of the form x = x - if (this != &temp) { - for (int iter = 0; iter < temp.order_; iter++){ - dims_[iter] = temp.dims_[iter]; - } // end for - - order_ = temp.order_; - length_ = temp.length_; - this_matrix_ = temp.this_matrix_; - host = temp.host; - } - - return *this; -} - -// Return size -template -KOKKOS_INLINE_FUNCTION -size_t DCMatrixKokkos::size() const { - return length_; -} - -template -KOKKOS_INLINE_FUNCTION -size_t DCMatrixKokkos::extent() const { - return length_; -} - -template -KOKKOS_INLINE_FUNCTION -size_t DCMatrixKokkos::dims(size_t i) const { - i--; - assert(i < order_ && "DCMatrixKokkos order (rank) does not match constructor, dim[i] does not exist!"); - assert(i >= 0 && dims_[i]>0 && "Access to DCMatrixKokkos dims is out of bounds!"); - return dims_[i]; -} - -template -KOKKOS_INLINE_FUNCTION -size_t DCMatrixKokkos::order() const { - return order_; -} - -template -KOKKOS_INLINE_FUNCTION -T* DCMatrixKokkos::device_pointer() const { - return this_matrix_.d_view.data(); -} - -template -KOKKOS_INLINE_FUNCTION -T* DCMatrixKokkos::host_pointer() const { - return this_matrix_.h_view.data(); -} - -template -void DCMatrixKokkos::update_host() { - - this_matrix_.template modify(); - this_matrix_.template sync(); -} - -template -void DCMatrixKokkos::update_device() { - - this_matrix_.template modify(); - this_matrix_.template sync(); -} - -template -KOKKOS_INLINE_FUNCTION -DCMatrixKokkos::~DCMatrixKokkos() {} -// End DCMatrixKokkos - - -///////////////////////// -// DViewCMatrixKokkos -///////////////////////// -template -class DViewCMatrixKokkos { - - // this is always unmanaged - using TArray1DHost = Kokkos::View; - // this is manage - using TArray1D = Kokkos::View; - typename ExecSpace::memory_space memspace; - -private: - size_t dims_[7]; - size_t length_; - size_t order_; // tensor order (rank) - TArray1D this_matrix_; - TArray1DHost this_matrix_host_; - T * temp_inp_matrix_; - -public: - DViewCMatrixKokkos(); - - DViewCMatrixKokkos(T * inp_matrix, size_t dim1, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - DViewCMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - DViewCMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, size_t dim3, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - DViewCMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, size_t dim3, - size_t dim4, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - DViewCMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, size_t dim3, - size_t dim4, size_t dim5, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - DViewCMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, size_t dim3, - size_t dim4, size_t dim5, size_t dim6, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - DViewCMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, size_t dim3, - size_t dim4, size_t dim5, size_t dim6, - size_t dim7, const std::string& tag_string = DEFAULTSTRINGMATRIX); - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m, - size_t n) const; - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m, - size_t n, size_t o) const; - - KOKKOS_INLINE_FUNCTION - DViewCMatrixKokkos& operator=(const DViewCMatrixKokkos& temp); - - // GPU Method - // Method that returns size - KOKKOS_INLINE_FUNCTION - size_t size() const; - - // Host Method - // Method that returns size - KOKKOS_INLINE_FUNCTION - size_t extent() const; - - KOKKOS_INLINE_FUNCTION - size_t dims(size_t i) const; - - KOKKOS_INLINE_FUNCTION - size_t order() const; - - // Method returns the raw device pointer of the Kokkos View - KOKKOS_INLINE_FUNCTION - T* device_pointer() const; - - // Method returns the raw host pointer of the Kokkos View - KOKKOS_INLINE_FUNCTION - T* host_pointer() const; - - // Data member to access host view - ViewCMatrix host; - - // Method that update host view - void update_host(); - - // Method that update device view - void update_device(); - - // Deconstructor - KOKKOS_INLINE_FUNCTION - ~DViewCMatrixKokkos (); -}; // End of DViewCMatrixKokkos - - -// Default constructor -template -DViewCMatrixKokkos::DViewCMatrixKokkos() { - length_ = order_ = 0; - temp_inp_matrix_ = NULL; - for (int i = 0; i < 7; i++) { - dims_[i] = 0; - } -} - -// Overloaded 1D constructor -template -DViewCMatrixKokkos::DViewCMatrixKokkos(T * inp_matrix, size_t dim1, const std::string& tag_string) { - - dims_[0] = dim1; - order_ = 1; - length_ = dim1; - // Create a 1D host view of the external allocation - this_matrix_host_ = TArray1DHost(inp_matrix, length_); - // Assign temp point to inp_matrix pointer that is passed in - temp_inp_matrix_ = inp_matrix; - // Create a device copy of that host view - this_matrix_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_matrix_host_); - // Create host ViewCMatrix. Note: inp_matrix and this_matrix_host_.data() are the same pointer - host = ViewCMatrix (inp_matrix, dim1); -} - -// Overloaded 2D constructor -template -DViewCMatrixKokkos::DViewCMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, const std::string& tag_string) { - - dims_[0] = dim1; - dims_[1] = dim2; - order_ = 2; - length_ = (dim1 * dim2); - // Create a 1D host view of the external allocation - this_matrix_host_ = TArray1DHost(inp_matrix, length_); - // Assign temp point to inp_matrix pointer that is passed in - temp_inp_matrix_ = inp_matrix; - // Create a device copy of that host view - this_matrix_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_matrix_host_); - // Create host ViewCMatrix - host = ViewCMatrix (inp_matrix, dim1, dim2); -} - -template -DViewCMatrixKokkos::DViewCMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, - size_t dim3, const std::string& tag_string) { - - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - order_ = 3; - length_ = (dim1 * dim2 * dim3); - // Create a 1D host view of the external allocation - this_matrix_host_ = TArray1DHost(inp_matrix, length_); - // Assign temp point to inp_matrix pointer that is passed in - temp_inp_matrix_ = inp_matrix; - // Create a device copy of that host view - this_matrix_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_matrix_host_); - // Create host ViewCMatrix - host = ViewCMatrix (inp_matrix, dim1, dim2, dim3); -} - -template -DViewCMatrixKokkos::DViewCMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, - size_t dim3, size_t dim4, const std::string& tag_string) { - - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - order_ = 4; - length_ = (dim1 * dim2 * dim3 * dim4); - // Create a 1D host view of the external allocation - this_matrix_host_ = TArray1DHost(inp_matrix, length_); - // Assign temp point to inp_matrix pointer that is passed in - temp_inp_matrix_ = inp_matrix; - // Create a device copy of that host view - this_matrix_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_matrix_host_); - // Create host ViewCMatrix - host = ViewCMatrix (inp_matrix, dim1, dim2, dim3, dim4); -} - -template -DViewCMatrixKokkos::DViewCMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, - size_t dim3, size_t dim4, - size_t dim5, const std::string& tag_string) { - - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - dims_[4] = dim5; - order_ = 5; - length_ = (dim1 * dim2 * dim3 * dim4 * dim5); - // Create a 1D host view of the external allocation - this_matrix_host_ = TArray1DHost(inp_matrix, length_); - // Assign temp point to inp_matrix pointer that is passed in - temp_inp_matrix_ = inp_matrix; - // Create a device copy of that host view - this_matrix_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_matrix_host_); - // Create host ViewCMatrix - host = ViewCMatrix (inp_matrix, dim1, dim2, dim3, dim4, dim5); -} - -template -DViewCMatrixKokkos::DViewCMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, - size_t dim3, size_t dim4, - size_t dim5, size_t dim6, const std::string& tag_string) { - - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - dims_[4] = dim5; - dims_[5] = dim6; - order_ = 6; - length_ = (dim1 * dim2 * dim3 * dim4 * dim5 * dim6); - // Create a 1D host view of the external allocation - this_matrix_host_ = TArray1DHost(inp_matrix, length_); - // Assign temp point to inp_matrix pointer that is passed in - temp_inp_matrix_ = inp_matrix; - // Create a device copy of that host view - this_matrix_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_matrix_host_); - // Create host ViewCMatrix - host = ViewCMatrix (inp_matrix, dim1, dim2, dim3, dim4, dim5, dim6); -} - -template -DViewCMatrixKokkos::DViewCMatrixKokkos(T * inp_matrix, size_t dim1, size_t dim2, - size_t dim3, size_t dim4, - size_t dim5, size_t dim6, - size_t dim7, const std::string& tag_string) { - - dims_[0] = dim1; - dims_[1] = dim2; - dims_[2] = dim3; - dims_[3] = dim4; - dims_[4] = dim5; - dims_[5] = dim6; - dims_[6] = dim7; - order_ = 7; - length_ = (dim1 * dim2 * dim3 * dim4 * dim5 * dim6 * dim7); - // Create a 1D host view of the external allocation - this_matrix_host_ = TArray1DHost(inp_matrix, length_); - // Assign temp point to inp_matrix pointer that is passed in - temp_inp_matrix_ = inp_matrix; - // Create a device copy of that host view - this_matrix_ = create_mirror_view_and_copy(Kokkos::view_alloc(memspace, tag_string), this_matrix_host_); - // Create host ViewCMatrix - host = ViewCMatrix (inp_matrix, dim1, dim2, dim3, dim4, dim5, dim6, dim7); -} - -template -KOKKOS_INLINE_FUNCTION -T& DViewCMatrixKokkos::operator()(size_t i) const { - assert(order_ == 1 && "Tensor order (rank) does not match constructor in DViewCMatrixKokkos 1D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in DViewCMatrixKokkos 1D!"); - return this_matrix_((i - 1)); -} - -template -KOKKOS_INLINE_FUNCTION -T& DViewCMatrixKokkos::operator()(size_t i, size_t j) const { - assert(order_ == 2 && "Tensor order (rank) does not match constructor in DViewCMatrixKokkos 2D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in DViewCMatrixKokkos 2D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in DViewCMatrixKokkos 2D!"); - return this_matrix_((j - 1) + ((i - 1) * dims_[1])); -} - -template -KOKKOS_INLINE_FUNCTION -T& DViewCMatrixKokkos::operator()(size_t i, size_t j, size_t k) const { - assert(order_ == 3 && "Tensor order (rank) does not match constructor in DViewCMatrixKokkos 3D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in DViewCMatrixKokkos 3D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in DViewCMatrixKokkos 3D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in DViewCMatrixKokkos 3D!"); - return this_matrix_((k - 1) + ((j - 1) * dims_[2]) - + ((i - 1) * dims_[2] * dims_[1])); -} - -template -KOKKOS_INLINE_FUNCTION -T& DViewCMatrixKokkos::operator()(size_t i, size_t j, size_t k, size_t l) const { - assert(order_ == 4 && "Tensor order (rank) does not match constructor in DViewCMatrixKokkos 4D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in DViewCMatrixKokkos 4D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in DViewCMatrixKokkos 4D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in DViewCMatrixKokkos 4D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in DViewCMatrixKokkos 4D!"); - return this_matrix_((l - 1) + ((k - 1) * dims_[3]) - + ((j - 1) * dims_[3] * dims_[2]) - + ((i - 1) * dims_[3] * dims_[2] * dims_[1])); -} - -template -KOKKOS_INLINE_FUNCTION -T& DViewCMatrixKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m) const { - assert(order_ == 5 && "Tensor order (rank) does not match constructor in DViewCMatrixKokkos 5D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in DViewCMatrixKokkos 5D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in DViewCMatrixKokkos 5D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in DViewCMatrixKokkos 5D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in DViewCMatrixKokkos 5D!"); - assert(m >= 1 && m <= dims_[4] && "m is out of bounds in DViewCMatrixKokkos 5D!"); - return this_matrix_((m - 1) + ((l - 1) * dims_[4]) - + ((k - 1) * dims_[4] * dims_[3]) - + ((j - 1) * dims_[4] * dims_[3] * dims_[2]) - + ((i - 1) * dims_[4] * dims_[3] * dims_[2] * dims_[1])); -} - -template -KOKKOS_INLINE_FUNCTION -T& DViewCMatrixKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m, size_t n) const { - assert(order_ == 6 && "Tensor order (rank) does not match constructor in DViewCMatrixKokkos 6D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in DViewCMatrixKokkos 6D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in DViewCMatrixKokkos 6D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in DViewCMatrixKokkos 6D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in DViewCMatrixKokkos 6D!"); - assert(m >= 1 && m <= dims_[4] && "m is out of bounds in DViewCMatrixKokkos 6D!"); - assert(n >= 1 && n <= dims_[5] && "n is out of bounds in DViewCMatrixKokkos 6D!"); - return this_matrix_((n - 1) + ((m - 1) * dims_[5]) - + ((l - 1) * dims_[5] * dims_[4]) - + ((k - 1) * dims_[5] * dims_[4] * dims_[3]) - + ((j - 1) * dims_[5] * dims_[4] * dims_[3] * dims_[2]) - + ((i - 1) * dims_[5] * dims_[4] * dims_[3] * dims_[2] * dims_[1])); -} - -template -KOKKOS_INLINE_FUNCTION -T& DViewCMatrixKokkos::operator()(size_t i, size_t j, size_t k, size_t l, - size_t m, size_t n, size_t o) const { - assert(order_ == 7 && "Tensor order (rank) does not match constructor in DViewCMatrixKokkos 7D!"); - assert(i >= 1 && i <= dims_[0] && "i is out of bounds in DViewCMatrixKokkos 7D!"); - assert(j >= 1 && j <= dims_[1] && "j is out of bounds in DViewCMatrixKokkos 7D!"); - assert(k >= 1 && k <= dims_[2] && "k is out of bounds in DViewCMatrixKokkos 7D!"); - assert(l >= 1 && l <= dims_[3] && "l is out of bounds in DViewCMatrixKokkos 7D!"); - assert(m >= 1 && m <= dims_[4] && "m is out of bounds in DViewCMatrixKokkos 7D!"); - assert(n >= 1 && n <= dims_[5] && "n is out of bounds in DViewCMatrixKokkos 7D!"); - assert(o >= 1 && o <= dims_[6] && "o is out of bounds in DViewCMatrixKokkos 7D!"); - return this_matrix_(o + ((n - 1) * dims_[6]) - + ((m - 1) * dims_[6] * dims_[5]) - + ((l - 1) * dims_[6] * dims_[5] * dims_[4]) - + ((k - 1) * dims_[6] * dims_[5] * dims_[4] * dims_[3]) - + ((j - 1) * dims_[6] * dims_[5] * dims_[4] * dims_[3] * dims_[2]) - + ((i - 1) * dims_[6] * dims_[5] * dims_[4] * dims_[3] * dims_[2] * dims_[1])); -} - -template -KOKKOS_INLINE_FUNCTION -DViewCMatrixKokkos& DViewCMatrixKokkos::operator= (const DViewCMatrixKokkos& temp) { - - // Do nothing if the assignment is of the form x = x - if (this != &temp) { - for (int iter = 0; iter < temp.order_; iter++){ - dims_[iter] = temp.dims_[iter]; - } // end for - - order_ = temp.order_; - length_ = temp.length_; - temp_inp_matrix_ = temp.temp_inp_matrix_; - this_matrix_host_ = temp.this_matrix_host_; - this_matrix_ = temp.this_matrix_; - host = temp.host; - } - - return *this; -} - -// Return size -template -KOKKOS_INLINE_FUNCTION -size_t DViewCMatrixKokkos::size() const { - return length_; -} - -template -KOKKOS_INLINE_FUNCTION -size_t DViewCMatrixKokkos::extent() const { - return length_; -} - -template -KOKKOS_INLINE_FUNCTION -size_t DViewCMatrixKokkos::dims(size_t i) const { - i--; - assert(i < order_ && "DViewCMatrixKokkos order (rank) does not match constructor, dim[i] does not exist!"); - assert(i >= 0 && dims_[i]>0 && "Access to DViewCMatrixKokkos dims is out of bounds!"); - return dims_[i]; -} - -template -KOKKOS_INLINE_FUNCTION -size_t DViewCMatrixKokkos::order() const { - return order_; -} - -template -KOKKOS_INLINE_FUNCTION -T* DViewCMatrixKokkos::device_pointer() const { - return this_matrix_.data(); -} - -template -KOKKOS_INLINE_FUNCTION -T* DViewCMatrixKokkos::host_pointer() const { - return this_matrix_host_.data(); -} - -template -void DViewCMatrixKokkos::update_host() { - // Deep copy of device view to host view - deep_copy(this_matrix_host_, this_matrix_); -} - -template -void DViewCMatrixKokkos::update_device() { - // Deep copy of host view to device view - deep_copy(this_matrix_, this_matrix_host_); -} - -template -KOKKOS_INLINE_FUNCTION -DViewCMatrixKokkos::~DViewCMatrixKokkos() {} -// End DViewCMatrixKokkos - - -/*! \brief Kokkos version of the serial RaggedRightArray class. - * - */ -template -class RaggedRightArrayKokkos { - - using TArray1D = Kokkos::View; - using SArray1D = Kokkos::View; - using Strides1D = Kokkos::View; - -private: - TArray1D array_; - - size_t dim1_; - size_t length_; - -public: - // Default constructor - RaggedRightArrayKokkos(); - - //--- 2D array access of a ragged right array --- - - // Overload constructor for a CArrayKokkos - RaggedRightArrayKokkos(CArrayKokkos &strides_array, const std::string& tag_string = DEFAULTSTRINGARRAY); - - // Overload constructor for a DCArrayKokkos - RaggedRightArrayKokkos(DCArrayKokkos &strides_array, const std::string& tag_string = DEFAULTSTRINGARRAY); - - // Overload constructor for a ViewCArray - RaggedRightArrayKokkos(ViewCArray &strides_array, const std::string& tag_string = DEFAULTSTRINGARRAY); - - // Overloaded constructor for a traditional array - RaggedRightArrayKokkos(size_t* strides_array, size_t some_dim1, const std::string& tag_string = DEFAULTSTRINGARRAY); - - // A method to return the stride size - KOKKOS_INLINE_FUNCTION - size_t stride(size_t i) const; - - // Host method to return the stride size - size_t stride_host(size_t i) const; - - // A method to increase the number of column entries, i.e., - // the stride size. Used with the constructor for building - // the stride_array dynamically. - // DO NOT USE with the constructures with a strides_array - KOKKOS_INLINE_FUNCTION - size_t& build_stride(const size_t i) const; - - KOKKOS_INLINE_FUNCTION - void stride_finalize() const; - - // Overload operator() to access data as array(i,j) - // where i=[0:N-1], j=[stride(i)] - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j) const; - - // method to return total size - KOKKOS_INLINE_FUNCTION - size_t size(){ - return length_; - } - - //setup start indices - void data_setup(const std::string& tag_string); - - KOKKOS_INLINE_FUNCTION - T* pointer(); - - //return the view - KOKKOS_INLINE_FUNCTION - TArray1D get_kokkos_view(); - - // Kokkos views of strides and start indices - Strides1D mystrides_; - SArray1D start_index_; - - KOKKOS_INLINE_FUNCTION - RaggedRightArrayKokkos& operator= (const RaggedRightArrayKokkos &temp); - - //initialize start indices view - class init_start_indices_functor{ - public: - SArray1D mystart_index_; - init_start_indices_functor(SArray1D tempstart_index_){ - mystart_index_ = tempstart_index_; - } - KOKKOS_INLINE_FUNCTION void operator()(const int index) const { - mystart_index_(index) = 0; - } - }; - - //setup start indices view - class setup_start_indices_functor{ - public: - SArray1D mystart_index_; - Strides1D mytemp_strides_; - setup_start_indices_functor(SArray1D tempstart_index_, Strides1D temp_strides_){ - mystart_index_ = tempstart_index_; - mytemp_strides_ = temp_strides_; - } - KOKKOS_INLINE_FUNCTION void operator()(const int index, int& update, bool final) const { - // Load old value in case we update it before accumulating - const size_t count = mytemp_strides_(index); - update += count; - if (final) { - mystart_index_((index+1)) = update; - } - } - }; - - //setup length of view - class setup_length_functor{ - public: - //kokkos needs this typedef named - typedef size_t value_type; - // This is helpful for determining the right index type, - // especially if you expect to need a 64-bit index. - //typedef Kokkos::View::size_type size_type; - Strides1D mytemp_strides_; - setup_length_functor(Strides1D temp_strides_){ - mytemp_strides_ = temp_strides_; - } - KOKKOS_INLINE_FUNCTION void operator()(const int index, size_t& update) const { - //const size_t count = mytemp_strides_(index); - update += mytemp_strides_(index); - } - }; - - //sets final 1D array size - class finalize_stride_functor{ - public: - SArray1D mystart_index_; - finalize_stride_functor(SArray1D tempstart_index_){ - mystart_index_ = tempstart_index_; - } - KOKKOS_INLINE_FUNCTION void operator()(const int index, int& update, bool final) const { - // Load old value in case we update it before accumulating - const size_t count = mystart_index_(index+1); - update += count; - if (final) { - mystart_index_((index+1)) = update; - } - } - }; - - // Destructor - KOKKOS_INLINE_FUNCTION - ~RaggedRightArrayKokkos ( ); -}; // End of RaggedRightArray - -template -RaggedRightArrayKokkos::RaggedRightArrayKokkos() { - dim1_ = length_ = 0; -} - -// Overloaded constructor -template -RaggedRightArrayKokkos::RaggedRightArrayKokkos(CArrayKokkos &strides_array, - const std::string& tag_string) { - mystrides_ = strides_array.get_kokkos_view(); - dim1_ = strides_array.extent(); - data_setup(tag_string); -} // End constructor - -// Overloaded constructor -template -RaggedRightArrayKokkos::RaggedRightArrayKokkos(DCArrayKokkos &strides_array, - const std::string& tag_string) { - mystrides_ = strides_array.get_kokkos_dual_view().d_view; - dim1_ = strides_array.extent(); - data_setup(tag_string); -} // End constructor - -// Overloaded constructor -template -RaggedRightArrayKokkos::RaggedRightArrayKokkos(ViewCArray &strides_array, - const std::string& tag_string) { -} // End constructor - -// Overloaded constructor -template -RaggedRightArrayKokkos::RaggedRightArrayKokkos(size_t* strides_array, size_t some_dim1, - const std::string& tag_string) { - mystrides_.assign_data(strides_array); - dim1_ = some_dim1; - data_setup(tag_string); -} // End constructor - -//setup start indices -template -void RaggedRightArrayKokkos::data_setup(const std::string& tag_string) { - //allocate start indices - std::string append_indices_string("start_indices"); - std::string append_array_string("array"); - std::string temp_copy_string = tag_string; - std::string start_index_tag_string = temp_copy_string.append(append_indices_string); - temp_copy_string = tag_string; - std::string array_tag_string = temp_copy_string.append(append_array_string); - - start_index_ = SArray1D(start_index_tag_string,dim1_ + 1); - #ifdef HAVE_CLASS_LAMBDA - Kokkos::parallel_for("StartValuesInit", dim1_+1, KOKKOS_CLASS_LAMBDA(const int i) { - start_index_(i) = 0; - }); - #else - init_start_indices_functor execution_functor(start_index_); - Kokkos::parallel_for("StartValuesInit", dim1_+1,execution_functor); - #endif - - #ifdef HAVE_CLASS_LAMBDA - Kokkos::parallel_scan("StartValuesSetup", dim1_, KOKKOS_CLASS_LAMBDA(const int i, int& update, const bool final) { - // Load old value in case we update it before accumulating - const size_t count = mystrides_(i); - update += count; - if (final) { - start_index_((i+1)) = update; - } - - }); - #else - setup_start_indices_functor setup_execution_functor(start_index_, mystrides_); - Kokkos::parallel_scan("StartValuesSetup", dim1_,setup_execution_functor); - #endif - - //compute length of the storage - #ifdef HAVE_CLASS_LAMBDA - Kokkos::parallel_reduce("LengthSetup", dim1_, KOKKOS_CLASS_LAMBDA(const int i, int& update) { - // Load old value in case we update it before accumulating - update += mystrides_(i); - }, length_); - #else - setup_length_functor length_functor(mystrides_); - Kokkos::parallel_reduce("LengthSetup", dim1_, length_functor, length_); - #endif - - //allocate view - array_ = TArray1D(array_tag_string, length_); -} - -// A method to return the stride size -template -KOKKOS_INLINE_FUNCTION -size_t RaggedRightArrayKokkos::stride(size_t i) const { - // Ensure that i is within bounds - assert(i < (dim1_) && "i is greater than dim1_ in RaggedRightArray"); - return mystrides_(i); -} - -// Method to build the stride (non-Kokkos push back) -template -KOKKOS_INLINE_FUNCTION -size_t& RaggedRightArrayKokkos::build_stride(const size_t i) const { - return start_index_(i+1); -} - -// Method to finalize stride -template -KOKKOS_INLINE_FUNCTION -void RaggedRightArrayKokkos::stride_finalize() const { - - #ifdef HAVE_CLASS_LAMBDA - Kokkos::parallel_scan("StartValues", dim1_, KOKKOS_CLASS_LAMBDA(const int i, int& update, const bool final) { - // Load old value in case we update it before accumulating - const size_t count = start_index_(i+1); - update += count; - if (final) { - start_index_((i+1)) = update; - } - - }); - #else - finalize_stride_functor execution_functor(start_index_); - Kokkos::parallel_scan("StartValues", dim1_,execution_functor); - #endif - Kokkos::fence(); -} - - -// Overload operator() to access data as array(i,j) -// where i=[0:N-1], j=[0:stride(i)] -template -KOKKOS_INLINE_FUNCTION -T& RaggedRightArrayKokkos::operator()(size_t i, size_t j) const { - // Get the 1D array index - size_t start = start_index_(i); - - // asserts - assert(i < dim1_ && "i is out of dim1 bounds in RaggedRightArrayKokkos"); // die if >= dim1 - assert(j < stride(i) && "j is out of stride bounds in RaggedRightArrayKokkos"); // die if >= stride - - return array_(j + start); -} // End operator() - -template -KOKKOS_INLINE_FUNCTION -T* RaggedRightArrayKokkos::pointer() { - return array_.data(); -} - - -template -KOKKOS_INLINE_FUNCTION -RaggedRightArrayKokkos & RaggedRightArrayKokkos:: - operator= (const RaggedRightArrayKokkos &temp) { - - if (this != &temp) { - /* - SArray1D tempdim = SArray1D("tempdim", 1); - auto h_tempdim = HostMirror(tempdim); - Kokkos::parallel_for("StrideDim", 1, KOKKOS_CLASS_LAMBDA(const int&) { - tempdim(0) = strides_array.size(); - //dim1_ = strides_array.size(); - }); - Kokkos::fence(); - deep_copy(h_tempdim, tempdim); - dim1_ = h_tempdim(0); - */ - dim1_ = temp.dim1_; - - // Create and initialize the starting index of the entries in the 1D array - start_index_ = temp.start_index_; - //start_index_(0) = 0; // the 1D array starts at 0 - - /* - size_t * h_start_index = new size_t [dim1_+1]; - h_start_index[0] = 0; - size_t * herenow = new size_t [2]; - herenow[0] = 1; - herenow[1] = 2; - size_t count = 0; - for (size_t i = 0; i < dim1_; i++){ - count += herenow[i]; - h_start_index[(i + 1)] = count; - printf("%d) Start check %ld\n", i, h_start_index[i]); - } // end for i - */ - /* - SArray1D templen = SArray1D("templen", 1); - auto h_templen = Kokkos::create_mirror_view(templen); - #ifdef HAVE_CLASS_LAMBDA - Kokkos::parallel_for("ArrayLength", 1, KOKKOS_CLASS_LAMBDA(const int&) { - templen(0) = start_index_(dim1_); - //length_ = start_index_(dim1_); - }); - #else - templen_functor templen_execution_functor(templen); - Kokkos::parallel_for("ArrayLength", 1, templen_execution_functor); - #endif - Kokkos::fence(); - Kokkos::deep_copy(h_templen, templen); - if (h_templen(0) != 0) - length_ = h_templen(0); - else - */ - length_ = temp.length_; - - - //printf("Length %ld\n", length_); - - //Kokkos::parallel_for("StartCheck", dim1_+1, KOKKOS_CLASS_LAMBDA(const int i) { - // printf("%d) Start %ld\n", i, start_index_(i)); - // }); - //Kokkos::fence(); - - array_ = temp.array_; - mystrides_ = temp.mystrides_; - - /* - dim1_ = temp.dim1_; - length_ = temp.length_; - start_index_ = SArray1D("start_index_", dim1_ + 1); - Kokkos::parallel_for("EqualOperator", dim1_+1, KOKKOS_CLASS_LAMBDA(const int j) { - start_index_(j) = temp.start_index_(j); - }); - //for (int j = 0; j < dim1_; j++) { - // start_index_(j) = temp.start_index_(j); - //} - array_ = TArray1D("array_", length_); - */ - } - - return *this; -} - -//return the stored Kokkos view -template -KOKKOS_INLINE_FUNCTION -Kokkos::View RaggedRightArrayKokkos::get_kokkos_view() { - return array_; -} - -// Destructor -template -KOKKOS_INLINE_FUNCTION -RaggedRightArrayKokkos::~RaggedRightArrayKokkos() { } - -//////////////////////////////////////////////////////////////////////////////// -// End of RaggedRightArrayKokkos -//////////////////////////////////////////////////////////////////////////////// - -/*! \brief Kokkos version of the RaggedRightArray class. - * - */ -template -class RaggedRightArrayofVectorsKokkos { - - using TArray1D = Kokkos::View; - using SArray1D = Kokkos::View; - using Strides1D = Kokkos::View; - -private: - TArray1D array_; - - size_t dim1_, vector_dim_; - size_t length_; - -public: - // Default constructor - RaggedRightArrayofVectorsKokkos(); - - //--- 2D array access of a ragged right array --- - - // Overload constructor for a CArrayKokkos - RaggedRightArrayofVectorsKokkos(CArrayKokkos &strides_array, size_t vector_dim, - const std::string& tag_string = DEFAULTSTRINGARRAY ); - - // Overload constructor for a ViewCArray - RaggedRightArrayofVectorsKokkos(ViewCArray &strides_array, size_t vector_dim, const std::string& tag_string = DEFAULTSTRINGARRAY); - - // Overloaded constructor for a traditional array - RaggedRightArrayofVectorsKokkos(size_t* strides_array, size_t some_dim1, size_t vector_dim, const std::string& tag_string = DEFAULTSTRINGARRAY); - - // A method to return the stride size - KOKKOS_INLINE_FUNCTION - size_t stride(size_t i) const; - - // A method to increase the number of column entries, i.e., - // the stride size. Used with the constructor for building - // the stride_array dynamically. - // DO NOT USE with the constructures with a strides_array - KOKKOS_INLINE_FUNCTION - size_t& build_stride(const size_t i) const; - - KOKKOS_INLINE_FUNCTION - void stride_finalize() const; - - // Overload operator() to access data as array(i,j) - // where i=[0:N-1], j=[stride(i)] - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j, size_t k) const; - - // method to return total size - KOKKOS_INLINE_FUNCTION - size_t size(){ - return length_; - } - - //setup start indices - void data_setup(const std::string& tag_string); - - KOKKOS_INLINE_FUNCTION - T* pointer(); - - //return the view - KOKKOS_INLINE_FUNCTION - TArray1D get_kokkos_view(); - - // Kokkos views of strides and start indices - Strides1D mystrides_; - SArray1D start_index_; - - KOKKOS_INLINE_FUNCTION - RaggedRightArrayofVectorsKokkos& operator= (const RaggedRightArrayofVectorsKokkos &temp); - - //functors for kokkos execution policies - // initialize start indices view - class init_start_indices_functor{ - public: - SArray1D mystart_index_; - init_start_indices_functor(SArray1D tempstart_index_){ - mystart_index_ = tempstart_index_; - } - KOKKOS_INLINE_FUNCTION void operator()(const int index) const { - mystart_index_(index) = 0; - } - }; - - //setup start indices view - class setup_start_indices_functor{ - public: - SArray1D mystart_index_; - Strides1D mytemp_strides_; - size_t myvector_dim_; - setup_start_indices_functor(SArray1D tempstart_index_, Strides1D temp_strides_, size_t myvector_dim){ - mystart_index_ = tempstart_index_; - mytemp_strides_ = temp_strides_; - myvector_dim_ = myvector_dim; - } - KOKKOS_INLINE_FUNCTION void operator()(const int index, int& update, bool final) const { - // Load old value in case we update it before accumulating - const size_t count = mytemp_strides_(index)*myvector_dim_; - update += count; - if (final) { - mystart_index_((index+1)) = update; - } - } - }; - - //setup length of view - class setup_length_functor{ - public: - //kokkos needs this typedef named - typedef size_t value_type; - // This is helpful for determining the right index type, - // especially if you expect to need a 64-bit index. - //typedef Kokkos::View::size_type size_type; - - Strides1D mytemp_strides_; - size_t myvector_dim_; - - setup_length_functor(Strides1D temp_strides_, size_t myvector_dim){ - mytemp_strides_ = temp_strides_; - myvector_dim_ = myvector_dim; - } - KOKKOS_INLINE_FUNCTION void operator()(const int index, size_t& update) const { - //const size_t count = mytemp_strides_(index)*myvector_dim_; - update += mytemp_strides_(index)*myvector_dim_;; - } - }; - - //sets final 1D array size - class finalize_stride_functor{ - public: - SArray1D mystart_index_; - finalize_stride_functor(SArray1D tempstart_index_){ - mystart_index_ = tempstart_index_; - } - KOKKOS_INLINE_FUNCTION void operator()(const int index, int& update, bool final) const { - // Load old value in case we update it before accumulating - const size_t count = mystart_index_(index+1); - update += count; - if (final) { - mystart_index_((index+1)) = update; - } - } - }; - - // Destructor - KOKKOS_INLINE_FUNCTION - ~RaggedRightArrayofVectorsKokkos ( ); -}; // End of RaggedRightArrayofVectorsKokkos - -template -RaggedRightArrayofVectorsKokkos::RaggedRightArrayofVectorsKokkos() { - dim1_ = length_ = vector_dim_ = 0; -} - -// Overloaded constructor -template -RaggedRightArrayofVectorsKokkos::RaggedRightArrayofVectorsKokkos(CArrayKokkos - &strides_array, size_t vector_dim, - const std::string& tag_string) { - //mystrides_.assign_data(strides_array.pointer()); - vector_dim_ = vector_dim; - mystrides_ = strides_array.get_kokkos_view(); - dim1_ = strides_array.extent(); - data_setup(tag_string); -} // End constructor - -/* -template -RaggedRightArrayofVectorsKokkos::RaggedRightArrayofVectorsKokkos(CArrayKokkos - &strides_array, size_t vector_dim) { - //mystrides_.assign_data(strides_array.pointer()); - vector_dim_ = vector_dim; - mystrides_ = strides_array; - dim1_ = strides_array.extent(); -} // End constructor -*/ - -// Overloaded constructor -template -RaggedRightArrayofVectorsKokkos::RaggedRightArrayofVectorsKokkos(ViewCArray &strides_array, size_t vector_dim, - const std::string& tag_string) { -} // End constructor - -// Overloaded constructor -template -RaggedRightArrayofVectorsKokkos::RaggedRightArrayofVectorsKokkos(size_t* strides_array, size_t some_dim1, size_t vector_dim, - const std::string& tag_string) { - vector_dim_ = vector_dim; - mystrides_.assign_data(strides_array); - dim1_ = some_dim1; - data_setup(tag_string); -} // End constructor - -//setup start indices -template -void RaggedRightArrayofVectorsKokkos::data_setup(const std::string& tag_string) { - - //allocate start indices - std::string append_indices_string("start_indices"); - std::string append_array_string("array"); - std::string temp_copy_string = tag_string; - std::string start_index_tag_string = temp_copy_string.append(append_indices_string); - temp_copy_string = tag_string; - std::string array_tag_string = temp_copy_string.append(append_array_string); - - start_index_ = SArray1D(start_index_tag_string,dim1_ + 1); - #ifdef HAVE_CLASS_LAMBDA - Kokkos::parallel_for("StartValuesInit", dim1_+1, KOKKOS_CLASS_LAMBDA(const int i) { - start_index_(i) = 0; - }); - #else - init_start_indices_functor execution_functor(start_index_); - Kokkos::parallel_for("StartValuesInit", dim1_+1,execution_functor); - #endif - - #ifdef HAVE_CLASS_LAMBDA - Kokkos::parallel_scan("StartValuesSetup", dim1_, KOKKOS_CLASS_LAMBDA(const int i, int& update, const bool final) { - // Load old value in case we update it before accumulating - const size_t count = mystrides_(i)*vector_dim_; - update += count; - if (final) { - start_index_((i+1)) = update; - } - - }); - #else - setup_start_indices_functor setup_execution_functor(start_index_, mystrides_, vector_dim_); - Kokkos::parallel_scan("StartValuesSetup", dim1_,setup_execution_functor); - #endif - - //compute length of the storage - #ifdef HAVE_CLASS_LAMBDA - Kokkos::parallel_reduce("LengthSetup", dim1_, KOKKOS_CLASS_LAMBDA(const int i, int& update) { - // Load old value in case we update it before accumulating - update += mystrides_(i)*vector_dim_; - }, length_); - #else - setup_length_functor length_functor(mystrides_, vector_dim_); - Kokkos::parallel_reduce("LengthSetup", dim1_, length_functor,length_); - #endif - - //allocate view - array_ = TArray1D(array_tag_string, length_); -} - -// A method to return the stride size -template -KOKKOS_INLINE_FUNCTION -size_t RaggedRightArrayofVectorsKokkos::stride(size_t i) const { - // Ensure that i is within bounds - assert(i < (dim1_) && "i is greater than dim1_ in RaggedRightArray"); - return mystrides_(i); -} - -// Method to build the stride (non-Kokkos push back) -template -KOKKOS_INLINE_FUNCTION -size_t& RaggedRightArrayofVectorsKokkos::build_stride(const size_t i) const { - return start_index_(i+1); -} - -// Method to finalize stride -template -KOKKOS_INLINE_FUNCTION -void RaggedRightArrayofVectorsKokkos::stride_finalize() const { - - #ifdef HAVE_CLASS_LAMBDA - Kokkos::parallel_scan("StartValues", dim1_, KOKKOS_CLASS_LAMBDA(const int i, int& update, const bool final) { - // Load old value in case we update it before accumulating - const size_t count = start_index_(i+1); - update += count; - if (final) { - start_index_((i+1)) = update; - } - - }); - #else - finalize_stride_functor execution_functor(start_index_); - Kokkos::parallel_scan("StartValues", dim1_,execution_functor); - #endif - Kokkos::fence(); -} - - -// Overload operator() to access data as array(i,j) -// where i=[0:N-1], j=[0:stride(i)] -template -KOKKOS_INLINE_FUNCTION -T& RaggedRightArrayofVectorsKokkos::operator()(size_t i, size_t j, size_t k) const { - // Get the 1D array index - size_t start = start_index_(i); - - // asserts - assert(i < dim1_ && "i is out of dim1 bounds in RaggedRightArrayKokkos"); // die if >= dim1 - assert(j < stride(i) && "j is out of stride bounds in RaggedRightArrayKokkos"); // die if >= stride - assert(k < vector_dim_ && "k is out of vector_dim bounds in RaggedRightArrayKokkos"); // die if >= vector_dim - - return array_(j*vector_dim_ + start + k); -} // End operator() - -template -KOKKOS_INLINE_FUNCTION -T* RaggedRightArrayofVectorsKokkos::pointer() { - return array_.data(); -} - - -template -KOKKOS_INLINE_FUNCTION -RaggedRightArrayofVectorsKokkos & RaggedRightArrayofVectorsKokkos:: - operator= (const RaggedRightArrayofVectorsKokkos &temp) { - - if (this != &temp) { - dim1_ = temp.dim1_; - vector_dim_ = temp.vector_dim_; - - // Create and initialize the starting index of the entries in the 1D array - start_index_ = temp.start_index_; - length_ = temp.length_; - - array_ = temp.array_; - mystrides_ = temp.mystrides_; - } - - return *this; -} - -//return the stored Kokkos view -template -KOKKOS_INLINE_FUNCTION -Kokkos::View RaggedRightArrayofVectorsKokkos::get_kokkos_view() { - return array_; -} - -// Destructor -template -KOKKOS_INLINE_FUNCTION -RaggedRightArrayofVectorsKokkos::~RaggedRightArrayofVectorsKokkos() { } - -//////////////////////////////////////////////////////////////////////////////// -// End of RaggedRightArrayofVectorsKokkos -//////////////////////////////////////////////////////////////////////////////// - -/*! \brief Kokkos version of the serial RaggedDownArray class. - * - */ -template -class RaggedDownArrayKokkos { - - using TArray1D = Kokkos::View; - using SArray1D = Kokkos::View; - using Strides1D = Kokkos::View; - -private: - TArray1D array_; - - size_t dim2_; - size_t length_; - -public: - // Default constructor - RaggedDownArrayKokkos(); - - //--- 2D array access of a ragged right array --- - - // Overload constructor for a CArray - RaggedDownArrayKokkos(CArrayKokkos &strides_array, const std::string& tag_string = DEFAULTSTRINGARRAY); - - // Overload constructor for a ViewCArray - RaggedDownArrayKokkos(ViewCArray &strides_array, const std::string& tag_string = DEFAULTSTRINGARRAY); - - // Overloaded constructor for a traditional array - RaggedDownArrayKokkos(size_t* strides_array, size_t some_dim2, const std::string& tag_string = DEFAULTSTRINGARRAY); - - // A method to return the stride size - KOKKOS_INLINE_FUNCTION - size_t stride(size_t j) const; - - //setup start indices - void data_setup(const std::string& tag_string); - - // Overload operator() to access data as array(i,j) - // where i=[0:N-1], j=[stride(i)] - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j) const; - - KOKKOS_INLINE_FUNCTION - T* pointer(); - - //return the view - KOKKOS_INLINE_FUNCTION - TArray1D get_kokkos_view(); - - KOKKOS_INLINE_FUNCTION - RaggedDownArrayKokkos& operator= (const RaggedDownArrayKokkos &temp); - - // Kokkos views of strides and start indices - Strides1D mystrides_; - SArray1D start_index_; - - //functors for kokkos execution policies - //initialize start indices view - class init_start_indices_functor{ - public: - SArray1D mystart_index_; - init_start_indices_functor(SArray1D tempstart_index_){ - mystart_index_ = tempstart_index_; - } - KOKKOS_INLINE_FUNCTION void operator()(const int index) const { - mystart_index_(index) = 0; - } - }; - - //setup start indices view - class setup_start_indices_functor{ - public: - SArray1D mystart_index_; - Strides1D mytemp_strides_; - setup_start_indices_functor(SArray1D tempstart_index_, Strides1D temp_strides_){ - mystart_index_ = tempstart_index_; - mytemp_strides_ = temp_strides_; - } - KOKKOS_INLINE_FUNCTION void operator()(const int index, int& update, bool final) const { - // Load old value in case we update it before accumulating - const size_t count = mytemp_strides_(index); - update += count; - if (final) { - mystart_index_((index+1)) = update; - } - } - }; - - //setup length of view - class setup_length_functor{ - public: - //kokkos needs this typedef named - typedef size_t value_type; - // This is helpful for determining the right index type, - // especially if you expect to need a 64-bit index. - //typedef Kokkos::View::size_type size_type; - Strides1D mytemp_strides_; - setup_length_functor(Strides1D temp_strides_){ - mytemp_strides_ = temp_strides_; - } - KOKKOS_INLINE_FUNCTION void operator()(const int index, size_t& update) const { - //const size_t count = mytemp_strides_(index); - update += mytemp_strides_(index); - } - }; - - // Destructor - KOKKOS_INLINE_FUNCTION - ~RaggedDownArrayKokkos ( ); -}; // End of RaggedDownArray - -template -RaggedDownArrayKokkos::RaggedDownArrayKokkos() { - dim2_ = length_ = 0; -} - -// Overloaded constructor -template -RaggedDownArrayKokkos::RaggedDownArrayKokkos(CArrayKokkos &strides_array, - const std::string& tag_string) { - mystrides_ = strides_array.get_kokkos_view(); - dim2_ = strides_array.extent(); - data_setup(tag_string); -} // End constructor - -// Overloaded constructor -template -RaggedDownArrayKokkos::RaggedDownArrayKokkos(ViewCArray &strides_array, const std::string& tag_string) { -} // End constructor - -// Overloaded constructor -template -RaggedDownArrayKokkos::RaggedDownArrayKokkos(size_t* strides_array, size_t some_dim2, - const std::string& tag_string) { - mystrides_.assign_data(strides_array); - dim2_ = some_dim2; - data_setup(tag_string); -} // End constructor - - -//setup start indices -template -void RaggedDownArrayKokkos::data_setup(const std::string& tag_string) { - //allocate start indices - std::string append_indices_string("start_indices"); - std::string append_array_string("array"); - std::string temp_copy_string = tag_string; - std::string start_index_tag_string = temp_copy_string.append(append_indices_string); - temp_copy_string = tag_string; - std::string array_tag_string = temp_copy_string.append(append_array_string); - - start_index_ = SArray1D(start_index_tag_string,dim2_ + 1); - #ifdef HAVE_CLASS_LAMBDA - Kokkos::parallel_for("StartValuesInit", dim2_+1, KOKKOS_CLASS_LAMBDA(const int i) { - start_index_(i) = 0; - }); - #else - init_start_indices_functor execution_functor(start_index_); - Kokkos::parallel_for("StartValuesInit", dim2_+1,execution_functor); - #endif - - #ifdef HAVE_CLASS_LAMBDA - Kokkos::parallel_scan("StartValuesSetup", dim2_, KOKKOS_CLASS_LAMBDA(const int i, int& update, const bool final) { - // Load old value in case we update it before accumulating - const size_t count = mystrides_(i); - update += count; - if (final) { - start_index_((i+1)) = update; - } - - }); - #else - setup_start_indices_functor setup_execution_functor(start_index_, mystrides_); - Kokkos::parallel_scan("StartValuesSetup", dim2_,setup_execution_functor); - #endif - - //compute length of the storage - #ifdef HAVE_CLASS_LAMBDA - Kokkos::parallel_reduce("LengthSetup", dim2_, KOKKOS_CLASS_LAMBDA(const int i, int& update) { - // Load old value in case we update it before accumulating - update += mystrides_(i); - }, length_); - #else - setup_length_functor length_functor(mystrides_); - Kokkos::parallel_reduce("LengthSetup", dim2_, length_functor, length_); - #endif - - //allocate view - array_ = TArray1D(array_tag_string, length_); -} - -// A method to return the stride size -template -KOKKOS_INLINE_FUNCTION -size_t RaggedDownArrayKokkos::stride(size_t j) const { - // Ensure that j is within bounds - assert(j < (dim2_) && "j is greater than dim1_ in RaggedDownArray"); - - return mystrides_(j); -} - -// Overload operator() to access data as array(i,j) -// where i=[0:N-1], j=[0:stride(i)] -template -KOKKOS_INLINE_FUNCTION -T& RaggedDownArrayKokkos::operator()(size_t i, size_t j) const { - // Get the 1D array index - size_t start = start_index_(j); - - // asserts - assert(i < stride(j) && "i is out of stride bounds in RaggedDownArrayKokkos"); // die if >= stride - assert(j < dim2_ && "j is out of dim1 bounds in RaggedDownArrayKokkos"); // die if >= dim1 - - return array_(i + start); -} // End operator() - -template -KOKKOS_INLINE_FUNCTION -RaggedDownArrayKokkos& RaggedDownArrayKokkos:: -operator= (const RaggedDownArrayKokkos &temp) { - - if (this != &temp) { - /* - SArray1D tempdim = SArray1D("tempdim", 1); - auto h_tempdim = HostMirror(tempdim); - Kokkos::parallel_for("StrideDim", 1, KOKKOS_CLASS_LAMBDA(const int&) { - tempdim(0) = strides_array.size(); - //dim1_ = strides_array.size(); - }); - Kokkos::fence(); - deep_copy(h_tempdim, tempdim); - dim1_ = h_tempdim(0); - */ - dim2_ = temp.dim2_; - - // Create and initialize the starting index of the entries in the 1D array - start_index_ = temp.start_index_; - /* - //start_index_(0) = 0; // the 1D array starts at 0 - #ifdef HAVE_CLASS_LAMBDA - Kokkos::parallel_for("StartFirst", 1, KOKKOS_CLASS_LAMBDA(const int&) { - start_index_(0) = 0; - }); - #else - assignment_init_functor init_execution_functor; - Kokkos::parallel_for("StartFirst", 1, init_execution_functor); - #endif - Kokkos::fence(); - - // Loop over to find the total length of the 1D array to - // represent the ragged-right array and set the starting 1D index - #ifdef HAVE_CLASS_LAMBDA - Kokkos::parallel_scan("StartValues", dim2_, KOKKOS_CLASS_LAMBDA(const int j, double& update, const bool final) { - // Load old value in case we update it before accumulating - const size_t count = temp.mystrides_[j]; - update += count; - if (final) { - start_index_((j+1)) = update; - } - - }); - #else - assignment_scan_functor scan_execution_functor(temp); - Kokkos::parallel_scan("StartValues", dim2_, scan_execution_functor); - #endif - Kokkos::fence(); - */ - /* - size_t * h_start_index = new size_t [dim1_+1]; - h_start_index[0] = 0; - size_t * herenow = new size_t [2]; - herenow[0] = 1; - herenow[1] = 2; - size_t count = 0; - for (size_t i = 0; i < dim1_; i++){ - count += herenow[i]; - h_start_index[(i + 1)] = count; - printf("%d) Start check %ld\n", i, h_start_index[i]); - } // end for i - */ - /* - SArray1D templen = SArray1D("templen", 1); - auto h_templen = Kokkos::create_mirror_view(templen); - #ifdef HAVE_CLASS_LAMBDA - Kokkos::parallel_for("ArrayLength", 1, KOKKOS_CLASS_LAMBDA(const int&) { - templen(0) = start_index_(dim2_); - //length_ = start_index_(dim2_); - }); - #else - templen_functor templen_execution_functor(templen); - Kokkos::parallel_for("ArrayLength", 1, templen_execution_functor); - #endif - Kokkos::fence(); - deep_copy(h_templen, templen); - length_ = h_templen(0); - - printf("Length %ld\n", length_); - - #ifdef HAVE_CLASS_LAMBDA - Kokkos::parallel_for("StartCheck", dim2_+1, KOKKOS_CLASS_LAMBDA(const int j) { - printf("%d) Start %ld\n", j, start_index_(j)); - }); - #else - stride_check_functor check_execution_functor; - Kokkos::parallel_for("StartCheck", dim2_+1, check_execution_functor); - #endif - Kokkos::fence(); - */ - length_ = temp.length_; - array_ = temp.length_; - mystrides_ = temp.mystrides_; - - /* - dim1_ = temp.dim1_; - length_ = temp.length_; - start_index_ = SArray1D("start_index_", dim1_ + 1); - Kokkos::parallel_for("EqualOperator", dim1_+1, KOKKOS_CLASS_LAMBDA(const int j) { - start_index_(j) = temp.start_index_(j); - }); - //for (int j = 0; j < dim1_; j++) { - // start_index_(j) = temp.start_index_(j); - //} - array_ = TArray1D("array_", length_); - */ - } - - return *this; -} - -//return the stored Kokkos view -template -KOKKOS_INLINE_FUNCTION -Kokkos::View RaggedDownArrayKokkos::get_kokkos_view() { - return array_; -} - -// Destructor -template -KOKKOS_INLINE_FUNCTION -RaggedDownArrayKokkos::~RaggedDownArrayKokkos() { } - -//////////////////////////////////////////////////////////////////////////////// -// End of RaggedDownArrayKokkos -//////////////////////////////////////////////////////////////////////////////// - -//11. DynamicRaggedRightArray -template -class DynamicRaggedRightArrayKokkos { - - using TArray1D = Kokkos::View; - using SArray1D = Kokkos::View; - -private: - // THIS WILL BE A GPU POINTER! - SArray1D stride_; - TArray1D array_; - - size_t dim1_; - size_t dim2_; - size_t length_; - -public: - // Default constructor - DynamicRaggedRightArrayKokkos (); - - //--- 2D array access of a ragged right array --- - - // overload constructor - DynamicRaggedRightArrayKokkos (size_t dim1, size_t dim2, const std::string& tag_string = DEFAULTSTRINGARRAY); - - // A method to return or set the stride size - KOKKOS_INLINE_FUNCTION - size_t& stride(size_t i) const; - - // A method to return the size - KOKKOS_INLINE_FUNCTION - size_t size() const; - - //return the view - KOKKOS_INLINE_FUNCTION - TArray1D get_kokkos_view(); - - // Overload operator() to access data as array(i,j), - // where i=[0:N-1], j=[stride(i)] - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j) const; - - // Overload copy assignment operator - KOKKOS_INLINE_FUNCTION - DynamicRaggedRightArrayKokkos& operator= (const DynamicRaggedRightArrayKokkos &temp); - - //kokkos policy functors - - //functors for kokkos execution policies - //set strides to a constant value - class set_strides_functor{ - public: - SArray1D functor_strides_; - size_t init_stride_; - set_strides_functor(size_t init_stride, SArray1D temp_strides_){ - init_stride_ = init_stride; - functor_strides_ = temp_strides_; - } - KOKKOS_INLINE_FUNCTION void operator()(const int index) const { - functor_strides_(index) = init_stride_; - } - }; - - // Destructor - KOKKOS_INLINE_FUNCTION - ~DynamicRaggedRightArrayKokkos (); -}; - -//nothing -template -DynamicRaggedRightArrayKokkos::DynamicRaggedRightArrayKokkos () { - dim1_ = dim2_ = length_ = 0; -} - -// Overloaded constructor -template -DynamicRaggedRightArrayKokkos::DynamicRaggedRightArrayKokkos (size_t dim1, size_t dim2, const std::string& tag_string) { - // The dimensions of the array; - dim1_ = dim1; - dim2_ = dim2; - length_ = dim1*dim2; - - std::string append_stride_string("strides"); - std::string append_array_string("array"); - std::string temp_copy_string = tag_string; - std::string strides_tag_string = temp_copy_string.append(append_stride_string); - temp_copy_string = tag_string; - std::string array_tag_string = temp_copy_string.append(append_array_string); - - stride_ = SArray1D(strides_tag_string, dim1_); - #ifdef HAVE_CLASS_LAMBDA - Kokkos::parallel_for("StridesInit", dim1_, KOKKOS_CLASS_LAMBDA(const int i) { - strides_(i) = 0; - }); - #else - set_strides_functor execution_functor(0, stride_); - Kokkos::parallel_for("StridesInit", dim1_,execution_functor); - #endif - - //allocate view - array_ = TArray1D(array_tag_string, length_); -} - -// A method to set the stride size for row i -template -KOKKOS_INLINE_FUNCTION -size_t& DynamicRaggedRightArrayKokkos::stride(size_t i) const { - return stride_(i); -} - -//return size -template -KOKKOS_INLINE_FUNCTION -size_t DynamicRaggedRightArrayKokkos::size() const{ - return length_; -} - -// Overload operator() to access data as array(i,j), -// where i=[0:N-1], j=[0:stride(i)] -template -KOKKOS_INLINE_FUNCTION -T& DynamicRaggedRightArrayKokkos::operator()(size_t i, size_t j) const { - // Asserts - assert(i < dim1_ && "i is out of dim1 bounds in DynamicRaggedRight"); // die if >= dim1 - assert(j < stride_(i) && "j is out of stride bounds in DynamicRaggedRight"); // die if >= dim2 - // Cannot assert on Kokkos View - //assert(j < stride_[i] && "j is out of stride bounds in DynamicRaggedRight"); // die if >= stride - - return array_(j + i*dim2_); -} - -//overload = operator -template -KOKKOS_INLINE_FUNCTION -DynamicRaggedRightArrayKokkos& - DynamicRaggedRightArrayKokkos::operator= (const DynamicRaggedRightArrayKokkos &temp) -{ - - if( this != &temp) { - dim1_ = temp.dim1_; - dim2_ = temp.dim2_; - length_ = temp.length_; - stride_ = temp.stride_; - array_ = temp.array_; - /* - #ifdef HAVE_CLASS_LAMBDA - Kokkos::parallel_for("StrideZeroOut", dim1_, KOKKOS_CLASS_LAMBDA(const int i) { - stride_(i) = 0; - }); - #else - stride_zero_functor execution_functor; - Kokkos::parallel_for("StrideZeroOut", dim1_, execution_functor); - #endif - */ - } - - return *this; -} - -//return the stored Kokkos view -template -KOKKOS_INLINE_FUNCTION -Kokkos::View DynamicRaggedRightArrayKokkos::get_kokkos_view() { - return array_; -} - -// Destructor -template -KOKKOS_INLINE_FUNCTION -DynamicRaggedRightArrayKokkos::~DynamicRaggedRightArrayKokkos() { -} - - - - -//----end DynamicRaggedRightArray class definitions---- - - -//12. DynamicRaggedDownArray - -template -class DynamicRaggedDownArrayKokkos { - - using TArray1D = Kokkos::View; - using SArray1D = Kokkos::View; - -private: - SArray1D stride_; - TArray1D array_; - - size_t dim1_; - size_t dim2_; - size_t length_; - -public: - // Default constructor - DynamicRaggedDownArrayKokkos (); - - //--- 2D array access of a ragged right array --- - - // overload constructor - DynamicRaggedDownArrayKokkos (size_t dim1, size_t dim2, const std::string& tag_string = DEFAULTSTRINGARRAY); - - // A method to return or set the stride size - KOKKOS_INLINE_FUNCTION - size_t& stride(size_t j) const; - - // A method to return the size - KOKKOS_INLINE_FUNCTION - size_t size() const; - - //return the view - KOKKOS_INLINE_FUNCTION - TArray1D get_kokkos_view(); - - // Overload operator() to access data as array(i,j), - // where i=[stride(j)], j=[0:N-1] - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j) const; - - // Overload copy assignment operator - KOKKOS_INLINE_FUNCTION - DynamicRaggedDownArrayKokkos& operator= (const DynamicRaggedDownArrayKokkos &temp); - - //kokkos policy functors - //set strides to 0 functor - //set strides to a constant value - class set_strides_functor{ - public: - SArray1D functor_strides_; - size_t init_stride_; - set_strides_functor(size_t init_stride, SArray1D temp_strides_){ - init_stride_ = init_stride; - functor_strides_ = temp_strides_; - } - KOKKOS_INLINE_FUNCTION void operator()(const int index) const { - functor_strides_(index) = init_stride_; - } - }; - - // Destructor - KOKKOS_INLINE_FUNCTION - ~DynamicRaggedDownArrayKokkos (); -}; - -//nothing -template -DynamicRaggedDownArrayKokkos::DynamicRaggedDownArrayKokkos () { - dim1_ = dim2_ = length_ = 0; -} - -// Overloaded constructor -template -DynamicRaggedDownArrayKokkos::DynamicRaggedDownArrayKokkos (size_t dim1, size_t dim2, const std::string& tag_string) { - // The dimensions of the array; - dim1_ = dim1; - dim2_ = dim2; - length_ = dim1*dim2; - - std::string append_stride_string("strides"); - std::string append_array_string("array"); - std::string temp_copy_string = tag_string; - std::string strides_tag_string = temp_copy_string.append(append_stride_string); - temp_copy_string = tag_string; - std::string array_tag_string = temp_copy_string.append(append_array_string); - - stride_ = SArray1D(strides_tag_string, dim2_); - #ifdef HAVE_CLASS_LAMBDA - Kokkos::parallel_for("StridesInit", dim2_, KOKKOS_CLASS_LAMBDA(const int i) { - strides_(i) = 0; - }); - #else - set_strides_functor execution_functor(0, stride_); - Kokkos::parallel_for("StridesInit", dim2_,execution_functor); - #endif - - //allocate view - array_ = TArray1D(array_tag_string, length_); -} - -// A method to set the stride size for column j -template -KOKKOS_INLINE_FUNCTION -size_t& DynamicRaggedDownArrayKokkos::stride(size_t j) const { - return stride_(j); -} - -//return size -template -KOKKOS_INLINE_FUNCTION -size_t DynamicRaggedDownArrayKokkos::size() const{ - return length_; -} - -// overload operator () to access data as an array(i,j) -// Note: i = 0:stride(j), j = 0:N-1 - -template -KOKKOS_INLINE_FUNCTION -T& DynamicRaggedDownArrayKokkos::operator()(size_t i, size_t j) const { - // Asserts - assert(j < dim2_ && "j is out of dim2 bounds in DynamicRaggedDownArrayKokkos"); // die if >= dim2 - assert(i < stride(j) && "i is out of stride bounds in DynamicRaggedDownArrayKokkos"); // die if >= stride(j) - // Can't do this assert with a Kokkos View - //assert(i < stride_[j] && "i is out of stride bounds in DynamicRaggedDownArrayKokkos"); // die if >= stride - - return array_(i + j*dim1_); -} - -//overload = operator -template -KOKKOS_INLINE_FUNCTION -DynamicRaggedDownArrayKokkos& - DynamicRaggedDownArrayKokkos::operator= (const DynamicRaggedDownArrayKokkos &temp) -{ - - if( this != &temp) { - dim1_ = temp.dim1_; - dim2_ = temp.dim2_; - length_ = temp.length_; - stride_ = temp.stride_; - array_ = temp.array_; - /* - #ifdef HAVE_CLASS_LAMBDA - Kokkos::parallel_for("StrideZeroOut", dim2_, KOKKOS_CLASS_LAMBDA(const int j) { - stride_(j) = 0; - }); - #else - stride_zero_functor execution_functor; - Kokkos::parallel_for("StrideZeroOut", dim2_, execution_functor); - #endif - */ - } - - return *this; -} - -//return the stored Kokkos view -template -KOKKOS_INLINE_FUNCTION -Kokkos::View DynamicRaggedDownArrayKokkos::get_kokkos_view() { - return array_; -} - -// Destructor -template -KOKKOS_INLINE_FUNCTION -DynamicRaggedDownArrayKokkos::~DynamicRaggedDownArrayKokkos() { -} - -////// END DynamicRaggedDownArrayKokkos - -// KokkosCSRArray -template -class CSRArrayKokkos { - - using TArray1D = Kokkos::View; - using SArray1D = Kokkos::View; - - private: // What ought to be private ? - size_t dim1_, dim2_; - size_t nnz_; - TArray1D array_; - SArray1D column_index_; - SArray1D start_index_; - TArray1D miss_; - public: - - /** - * @brief Construct a new Sparse Row Array Kokkos object - * - */ - CSRArrayKokkos(); - //CSRArray(CArray data, CArray col_ptrs, CArray row_ptrs, size_t rows, size_t cols); - - CSRArrayKokkos( - CArrayKokkos &array, - CArrayKokkos &start_index, - CArrayKokkos &colum_index, - size_t dim1, size_t dim2, const std::string & tag_string = DEFAULTSTRINGARRAY); - - - /** - * @brief Constructor takes in dense matrix - */ - //KOKKOS_INLINE_FUNCTION - CSRArrayKokkos(const CArrayKokkos &dense, const size_t dim1, const size_t dim2); - - void data_setup(const std::string& tag_string); - /** - * @brief Access method to A(i,j) returns a dummy value of 0 if value is not allocated - * - * @param i row - * @param j column - * @return KOKKOS_INLINE_FUNCTION& - */ - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t j) const; - - /** - * @brief Same functionality as operator. Included for compatibility with other matar types - * - * @param i - * @param j - * @return KOKKOS_INLINE_FUNCTION& - */ - KOKKOS_INLINE_FUNCTION - T& value(size_t i, size_t j) const; - - /** - * @brief Copy operator - * - * @param temp - */ - KOKKOS_INLINE_FUNCTION - CSRArrayKokkos& operator=(const CSRArrayKokkos &temp); - - - /** - * @brief Pointer to start of array_ data - * - */ - KOKKOS_INLINE_FUNCTION - T* pointer() const; - - /** - * @brief Get the beginning of the start_index_ array - * - */ - KOKKOS_INLINE_FUNCTION - size_t* get_starts() const; - - - /** - * @brief Number of columns - * - * @return KOKKOS_INLINE_FUNCTION - */ - KOKKOS_INLINE_FUNCTION - size_t dim2() const ; - - /** - * @brief Number of rows - * - * @return KOKKOS_INLINE_FUNCTION - */ - KOKKOS_INLINE_FUNCTION - size_t dim1() const; - - /** - * @brief iterator notation to access the non zero elements of row i. Returns pointer to first element in row i - * - * @param i row - * @return KOKKOS_INLINE_FUNCTION* - */ - KOKKOS_INLINE_FUNCTION - T* begin(size_t i); - - /** - * @brief Iteator notation to access the non zero elements of row i. Returns pointer first element of the next row - * - * @param i - * @return KOKKOS_INLINE_FUNCTION* - */ - KOKKOS_INLINE_FUNCTION - T* end(size_t i); - - /** - * @brief Get the size of row i. Same functionality as nnz(i) but included for compatiblity. - * - * @param i - * @return KOKKOS_INLINE_FUNCTION - */ - KOKKOS_INLINE_FUNCTION - size_t stride(size_t i) const; - - - /* - * @brief get values from dense array - */ - void from_dense(CArrayKokkos &starts, - CArrayKokkos &columns, - CArrayKokkos &array); - - /* - * iterator for the raw data at row i - * i.e. return the index each element is the index in the 1 array - * This as the use of providing a reasonable way to get the column - * index and data value in the case you need both - */ - KOKKOS_INLINE_FUNCTION - size_t begin_index(size_t i) const; - KOKKOS_INLINE_FUNCTION - size_t end_index(size_t i) const; - - /** - * @brief get the number of non zero elements in row i - */ - KOKKOS_INLINE_FUNCTION - size_t nnz(size_t i); - - /** - * @brief get the total number of non zero elements - */ - KOKKOS_INLINE_FUNCTION - size_t nnz() const ; - - // Use the index into the 1d array to get what value is stored there and what is the corresponding row - KOKKOS_INLINE_FUNCTION - T& get_val_flat(size_t k) const; - KOKKOS_INLINE_FUNCTION - size_t get_col_flat(size_t k) const; - // reverse map function from A(i,j) to what element of data/col_pt_ it corersponds to - int flat_index(size_t i, size_t j); - // Convertor - - // int toCSC(CArray &data, CArray &col_ptrs, CArray &row_ptrs); - - void to_dense(CArrayKokkos& A); - - class init_start_indices_functor{ - public: - SArray1D mystart_index_; - init_start_indices_functor(SArray1D tempstart_index_){ - mystart_index_ = tempstart_index_; - } - KOKKOS_INLINE_FUNCTION void operator()(const int index) const { - mystart_index_(index) = 0; - } - }; - //destructor - KOKKOS_INLINE_FUNCTION - ~CSRArrayKokkos(); - - -}; - -template -CSRArrayKokkos::CSRArrayKokkos() { - dim1_ = dim2_ = nnz_ = 0; -} - -template -CSRArrayKokkos::CSRArrayKokkos( - CArrayKokkos &array, - CArrayKokkos &start_index, - CArrayKokkos &colum_index, - size_t dim1, size_t dim2, const std::string & tag_string){ - dim1_ = dim1; - dim2_ = dim2; - start_index_ = start_index.get_kokkos_view(); - array_ = array.get_kokkos_view(); - column_index_ = colum_index.get_kokkos_view(); - nnz_ = colum_index.extent(); - miss_ = TArray1D("miss", 1); -} - -/* -template -KOKKOS_INLINE_FUNCTION -CSRArrayKokkos::CSRArrayKokkos(const CArrayKokkos &dense, const size_t dim1, const size_t dim2){ - dim1_ = dim1; - dim2_ = dim2; - miss_ = TArray1D("miss",1); - start_index_ = Kokkos::View("start indices", dim1 + 1); - nnz_ = 0; - - - start_index_(0) = 0; - // TODO MAKE parallel - for(size_t i = 0; i < dim1_; i++){ - start_index_(i+1) = 0; - for(size_t j =0; j < dim2_; j++){ - if(dense(i,j) != 0){ - start_index_(i+1) ++; - nnz_++; - } - } - } - - - for(size_t i = 1; i < dim1_ + 1; i++){ - start_index_(i) = start_index_[i] + start_index_[i-1]; - } - - column_index_ = Kokkos::View("column Indices", nnz_); - array_ = Kokkos::View("array elements", nnz_); - size_t next = 0 ; - for(size_t i = 0; i < dim1_; i++){ - for(size_t j =0 ; j < dim2_; j++){ - if(dense(i,j) != 0){ - // column_index_(next) = j; - // array_(next) = dense(i,j); - next++; - } - } - } -} -*/ - -//setup start indices -template -void CSRArrayKokkos::data_setup(const std::string& tag_string) { - //allocate start indices - std::string append_indices_string("start_indices"); - std::string temp_copy_string = tag_string; - std::string start_index_tag_string = temp_copy_string.append(append_indices_string); - temp_copy_string = tag_string; - - start_index_ = SArray1D(start_index_tag_string, dim1_ + 1); - #ifdef HAVE_CLASS_LAMBDA - Kokkos::parallel_for("StartValuesInit", dim1_+1, KOKKOS_CLASS_LAMBDA(const int i) { - start_index_(i) = 0; - }); - #else - init_start_indices_functor execution_functor(start_index_); - Kokkos::parallel_for("StartValuesInit", dim1_+1,execution_functor); - #endif - -} - -template -KOKKOS_INLINE_FUNCTION -T& CSRArrayKokkos::operator()(size_t i, size_t j) const { - size_t row_start = start_index_[i]; - size_t row_end = start_index_[i+1]; - size_t k; - for(k = 0; k < row_end - row_start; k++){ - if(column_index_[row_start + k] == j){ - return array_.data()[row_start + k]; - } - } - miss_[0] = (T) NULL; - return miss_[0]; -} - - -template -KOKKOS_INLINE_FUNCTION -T& CSRArrayKokkos::value(size_t i, size_t j) const { - size_t row_start = start_index_[i]; - size_t row_end = start_index_[i+1]; - size_t k; - for(k = 0; k < row_end - row_start; k++){ - if(column_index_[row_start + k] == j){ - return array_.data()[row_start + k]; - } - } - miss_[0] = (T) NULL; - return miss_[0]; -} - -template -KOKKOS_INLINE_FUNCTION -T* CSRArrayKokkos::pointer() const{ - return array_.data(); -} - -template -KOKKOS_INLINE_FUNCTION -size_t* CSRArrayKokkos::get_starts() const { - return start_index_.data(); -} - -template -KOKKOS_INLINE_FUNCTION -CSRArrayKokkos& CSRArrayKokkos::operator=(const CSRArrayKokkos &temp){ - if(this != temp) { - nnz_ = temp.nnz_; - dim1_ = temp.dim1_; - dim2_ = temp.dim2_; - - start_index_ = temp.start_index_; - column_index_ = temp.column_index_; - array_ = temp.array_; - } - return *this; -} - -template -void CSRArrayKokkos::to_dense(CArrayKokkos& A){ - size_t i,j; - for(i = 0; i < dim1_; i++){ - for(j = 0; j < dim2_; j++){ - A(i,j) = (*this)(i,j); - } - } -} - -template -KOKKOS_INLINE_FUNCTION -size_t CSRArrayKokkos::stride(size_t i) const { - assert(i <= dim1_ && "Index i out of bounds in CSRArray.stride()"); - return start_index_.data()[i+i] - start_index_.data()[i]; -} - - -template -KOKKOS_INLINE_FUNCTION -size_t CSRArrayKokkos::dim2() const { - return dim2_; -} - -template -KOKKOS_INLINE_FUNCTION -size_t CSRArrayKokkos::dim1() const{ - return dim1_; -} - -template -KOKKOS_INLINE_FUNCTION -T* CSRArrayKokkos::begin(size_t i){ - assert(i <= dim1_ && "i is out of bounds in CSRArray.begin()"); - size_t row_start = start_index_.data()[i]; - return &array_.data()[row_start]; -} -template -KOKKOS_INLINE_FUNCTION -T* CSRArrayKokkos::end(size_t i){ - assert(i <= dim1_ && "i is out of bounds in CSRArray.end()"); - size_t row_start = start_index_.data()[i+1]; - return &array_.data()[row_start]; -} - -template -KOKKOS_INLINE_FUNCTION -size_t CSRArrayKokkos::begin_index(size_t i) const{ - assert(i <= dim1_ && "i is out of bounds in CSRArray.begin_index()"); - return start_index_.data()[i]; -} - -template -KOKKOS_INLINE_FUNCTION -size_t CSRArrayKokkos::end_index(size_t i) const{ - assert(i <= dim1_ && "i is out of bounds in CSRArray.begin_index()"); - return start_index_.data()[i+1]; -} - -template -KOKKOS_INLINE_FUNCTION -size_t CSRArrayKokkos::nnz() const{ - return nnz_; -} - -template -KOKKOS_INLINE_FUNCTION -size_t CSRArrayKokkos::nnz(size_t i){ - assert(i <= dim1_ && "Index i out of bounds in CSRArray.stride()"); - return start_index_.data()[i+1] - start_index_.data()[i]; -} - -template -KOKKOS_INLINE_FUNCTION -T& CSRArrayKokkos::get_val_flat(size_t k) const{ - assert(k < nnz_ && "Index k is out of bounds in CSRArray.get_val_flat()"); - return array_.data()[k]; -} - -template -KOKKOS_INLINE_FUNCTION -size_t CSRArrayKokkos::get_col_flat(size_t k) const{ - assert(k < nnz_ && "Index k is out of bounds in CSRArray.get_col_lat()"); - return column_index_.data()[k]; -} - - -template -int CSRArrayKokkos::flat_index(size_t i, size_t j){ - size_t k; - size_t row_start = start_index_.data()[i]; - size_t row_end = start_index_.data()[i+1]; - for(k = 0; k < row_end - row_start; k++){ - if(column_index_.data()[row_start+k] == j){ - return row_start+k; - } - } - return -1; -} - -//template -//void CSRArrrayKokkos::from_dense(CArrayKokkos &starts, -// CArrayKokkos &columns, -// CArrayKokkos &array); - - /* -int CSRArray::toCSC(CArray &data, CArray &col_ptrs, CArray &row_ptrs ){ - int nnz_cols[ncols_ + 1]; - int col_counts[ncols_]; - int i = 0; - // How many elements are each column - for(i =0 ; i < ncols_; i++){ - nnz_cols[i] = 0; - col_counts[i] = 0; - } - nnz_cols[ncols_] = 0; - col_ptrs(0) = 0; - for(i =0; i < nnz_; i++){ - nnz_cols[column_index_[i] + 1] += 1; - } - // What we actually care about is how many elements are - // in all the columns preceeding this column. - for(i = 1; i <= ncols_; i++){ - nnz_cols[i] = nnz_cols[i-1] + nnz_cols[i]; - col_ptrs(i) = nnz_cols[i]; - } - size_t row = 1; - // if b is at A(i,j) stored in csr format - // it needs to go where the where the ith column starts - // + how many things we have put in the "window" - // we allocated for this column already - // For row we simply keep track of what row we are currently in - // as we scan through the 1d array of data. - for(i = 0; i < nnz_; i++){ - if(i >= start_index_[row]){ - row++; - } - int idx = nnz_cols[column_index_[i]] + col_counts[column_index_[i]]; - col_counts[column_index_[i]] += 1; - data(idx) = array_[i]; - row_ptrs(idx) = row - 1; - } - // I return an int because I thought I might need to return an error code - // Not sure that is true - return 0; -} -*/ - -template -CSRArrayKokkos::~CSRArrayKokkos() {} - -// 16 CSCArrayKokkos -template -class CSCArrayKokkos -{ - - using TArray1D = Kokkos::View; - using SArray1D = Kokkos::View; -private: // What ought to be private ? - size_t dim1_, dim2_; - size_t nnz_; - TArray1D array_; - TArray1D miss_; - SArray1D start_index_; - SArray1D row_index_; - - public: - - /** - * @brief Construct a new empty Sparse Col Array object - * - */ - CSCArrayKokkos(); - - /** - * @brief Construct a new Sparse Col Array object - * - * @param array: 1d array of data values in order as read top to bottom, left to right - * @param row_index: 1d array that marks what row each element is in - * @param start_index: 1d array that marks where the first element of each column starts - * @param dim1: number of rows the matrix should have - * @param dim2: number of columns the matrix should have - */ - CSCArrayKokkos( - CArrayKokkos &array, - CArrayKokkos &start_index, - CArrayKokkos &row_index, - size_t dim1, size_t dim2, const std::string & tag_string = DEFAULTSTRINGARRAY); - - - /** - * @brief Access A(i,j). Returns a dummy address with value 0 if A(i,j) is not allocated - * - * @param i : row - * @param j : column - * @return T& : address of array_ that corresponds to A(i,j) - */ - KOKKOS_INLINE_FUNCTION - T &operator()(size_t i, size_t j) const; - - /** - * @brief Overloaded copy operator - * - * @param temp : Array to copy - * @return CSCArray& - */ - KOKKOS_INLINE_FUNCTION - CSCArrayKokkos &operator=(const CSCArrayKokkos &temp); - - /** - * @brief returns pointer to array_ - * - */ - KOKKOS_INLINE_FUNCTION - T *pointer() const; - - /** - * @brief Same functionality as nnz(i) included for compatibility with the rest of matar - * - * @param i : row - * @return size_t - */ - KOKKOS_INLINE_FUNCTION - size_t stride(size_t i) const; - - /** - * @brief Same functionality as operator() included for compatibility with the rest of matar - * - * @param i: row - * @param j: column - * @return T& - */ - KOKKOS_INLINE_FUNCTION - T &value(size_t i, size_t j) const; - - /** - * @brief Get the start_index array - * - * @return size_t* : returns start_index_ - */ - KOKKOS_INLINE_FUNCTION - size_t *get_starts() const; - - /** - * @brief Get number of rows - * - * @return size_t number of rows - */ - KOKKOS_INLINE_FUNCTION - size_t dim1() const; - - /** - * @brief Get number of columns - * - * @return size_t number of columns - */ - KOKKOS_INLINE_FUNCTION - size_t dim2() const; - - /** - * @brief iterator notation for iterating through the non zeros values of row i. - * - * @param i : row - * @return T* - */ - KOKKOS_INLINE_FUNCTION - T *begin(size_t i); - - /** - * @brief iterator notation for iterating through the non zeros values of row i. - * - * @param i : row - * @return T* - */ - KOKKOS_INLINE_FUNCTION - T *end(size_t i); - - // iterator for the raw data at row i - // i.e. return the index each element is the index in the 1 array - // This as the use of providing a reasonable way to get the column - // index and data value in the case you need both - KOKKOS_INLINE_FUNCTION - size_t begin_index(size_t i); - - KOKKOS_INLINE_FUNCTION - size_t end_index(size_t i); - - /** - * @brief Get the number of non zero elements in row i - * - * @param i : row to get - * @return size_t : size of row - */ - KOKKOS_INLINE_FUNCTION - size_t nnz(size_t i); - - /** - * @brief Get number of non zero elements total in array - * - * @return size_t - */ - KOKKOS_INLINE_FUNCTION - size_t nnz() const; - - // Use the index into the 1d array to get what value is stored there and what is the corresponding row - KOKKOS_INLINE_FUNCTION - T &get_val_flat(size_t k); - - KOKKOS_INLINE_FUNCTION - size_t get_row_flat(size_t k); - - // reverse map function from A(i,j) to what element of data/col_pt_ it corersponds to - KOKKOS_INLINE_FUNCTION - int flat_index(size_t i, size_t j); - - // Convertor - //int toCSR(CArray &data, CArray &row_ptrs, CArray &col_ptrs); - //void to_dense(FArray &A); - // destructor - KOKKOS_INLINE_FUNCTION - ~CSCArrayKokkos(); -}; - -template -CSCArrayKokkos::CSCArrayKokkos() { - dim1_ = dim2_ = nnz_ = 0; -} - - -template -CSCArrayKokkos::CSCArrayKokkos( - CArrayKokkos &array, - CArrayKokkos &start_index, - CArrayKokkos &row_index, - size_t dim1, size_t dim2, const std::string & tag_string){ - - dim1_ = dim1; - dim2_ = dim2; - start_index_ = start_index.get_kokkos_view(); - array_ = array.get_kokkos_view(); - row_index_ = row_index.get_kokkos_view(); - nnz_ = row_index.extent(); - miss_ = TArray1D("miss", 1); -} - - -template -KOKKOS_INLINE_FUNCTION -T& CSCArrayKokkos::operator()(size_t i, size_t j) const { - size_t col_start = start_index_[j]; - size_t col_end = start_index_[j + 1]; - size_t k; - for(k =0; k < col_end - col_start;k++){ - if(row_index_[col_start + k] == i){ - return array_.data()[col_start + k]; - } - } - //return array_.data()[nnz_]; - miss_[0] = (T) NULL; - return miss_[0]; -} - -template -KOKKOS_INLINE_FUNCTION -T* CSCArrayKokkos::pointer() const { - return array_.data(); -} - - -template -KOKKOS_INLINE_FUNCTION -T& CSCArrayKokkos::value(size_t i, size_t j) const { - size_t col_start = start_index_.data()[j]; - size_t col_end = start_index_.data()[j + 1]; - size_t k; - for(k =0; k < col_end - col_start;k++){ - if(row_index_.data()[col_start + k] == i){ - return array_.data()[col_start + k]; - } - } - miss_[0] = (T) NULL; - return miss_[0]; -} - -template -KOKKOS_INLINE_FUNCTION -size_t* CSCArrayKokkos::get_starts() const{ - return &start_index_.data()[0]; -} - -template -KOKKOS_INLINE_FUNCTION -CSCArrayKokkos& CSCArrayKokkos::operator=(const CSCArrayKokkos &temp){ - if(this != temp) { - nnz_ = temp.nnz_; - dim2_ = temp.dim2_; - dim1_ = temp.dim1_;; - - start_index_ = temp.start_index_; - row_index_ = temp.row_row_index_; - array_ = temp.array_; - } - return *this; -} - -template -KOKKOS_INLINE_FUNCTION -size_t CSCArrayKokkos::stride(size_t i) const{ - assert(i < dim2_ && "i is out of bounds in CSCArray.stride()"); - return start_index_.data()[i+1] - start_index_.data()[i]; -} - - -template -KOKKOS_INLINE_FUNCTION -size_t CSCArrayKokkos::dim1() const { - return dim1_; -} - -template -KOKKOS_INLINE_FUNCTION -size_t CSCArrayKokkos::dim2() const{ - return dim2_; -} - -template -KOKKOS_INLINE_FUNCTION -T* CSCArrayKokkos::begin(size_t i){ - assert(i <= dim2_ && "index i out of bounds at CSCArray.begin()"); - size_t col_start = start_index_.data()[i]; - return &array_.data()[col_start]; -} - -template -KOKKOS_INLINE_FUNCTION -T* CSCArrayKokkos::end(size_t i){ - assert(i <= dim2_ && "index i out of bounds at CSCArray.endt()"); - size_t col_start = start_index_.data()[i+1]; - return &array_.data()[col_start]; -} - -template -KOKKOS_INLINE_FUNCTION -size_t CSCArrayKokkos::begin_index(size_t i){ - assert(i <= dim2_ && "index i out of bounds at CSCArray.begin_index()"); - return start_index_.data()[i]; -} - -template -KOKKOS_INLINE_FUNCTION -size_t CSCArrayKokkos::end_index(size_t i){ - assert(i <= dim2_ && "index i out of bounds at CSCArray.end_index()"); - return start_index_.data()[i + 1]; -} - -template -KOKKOS_INLINE_FUNCTION -size_t CSCArrayKokkos::nnz() const{ - return nnz_; -} - -template -KOKKOS_INLINE_FUNCTION -size_t CSCArrayKokkos::nnz(size_t i){ - return start_index_.data()[i+1] - start_index_.data()[i]; -} - -template -KOKKOS_INLINE_FUNCTION -T& CSCArrayKokkos::get_val_flat(size_t k){ - return array_.data()[k]; -} - -template -KOKKOS_INLINE_FUNCTION -size_t CSCArrayKokkos::get_row_flat(size_t k){ - return row_index_.data()[k]; -} - -template -KOKKOS_INLINE_FUNCTION -int CSCArrayKokkos::flat_index(size_t i, size_t j){ - size_t col_start = start_index_.data()[j]; - size_t col_end = start_index_.data()[j+1]; - size_t k; - for (k = 0; k < col_end - col_start; k++) - { - if(row_index_.data()[col_start + k] == i){ - return col_start + k; - } - } - return -1; -} - -// Assumes that data, col_ptrs, and row_ptrs -// have been allocated size already before this call -// Returns the data in this csr format but as represented as the appropriatte vectors -// for a csc format -/*template -int CSCArray::toCSR(CArray &data, CArray &col_ptrs, CArray &row_ptrs ){ - int nnz_rows[dim1_ + 1]; - int row_counts[dim1_]; - int i = 0; - // How many elements are each column - for(i =0 ; i < dim1_; i++){ - nnz_rows[i] = 0; - row_counts[i] = 0; - } - nnz_rows[dim1_] = 0; - row_ptrs(i) = 0 ; - for(i =0; i < nnz_; i++){ - nnz_rows[row_index_[i] + 1] += 1; - } - // What we actually care about is how many elements are - // in all the columns preceeding this column. - for(i = 1; i < dim1_; i++){ - nnz_rows[i] = nnz_rows[i-1] + nnz_rows[i]; - row_ptrs(i) = nnz_rows[i]; - } - size_t col = 1; - // if b is at A(i,j) stored in csr format - // it needs to go where the where the ith column starts - // + how many things we have put in the "window" - // we allocated for this column already - // For row we simply keep track of what row we are currently in - // as we scan through the 1d array of data. - for(i = 0; i < nnz_; i++){ - if(i >= start_index_[col]){ - col++; - } - int idx = nnz_rows[row_index_[i]] + row_counts[row_index_[i]]; - row_counts[row_index_[i]] += 1; - data(idx) = array_[i]; - col_ptrs(idx) = col - 1; - } - // I return an int because I thought I might need to return an error code - // Not sure that is true - return 0; -} -*/ - -template -CSCArrayKokkos::~CSCArrayKokkos() {} - -////////////////////////// -// Inherited Class Array -////////////////////////// - -/* -//template -template -class InheritedArray2L { - - using TArray1D = Kokkos::View; - -private: - size_t dim1_, length_; - -public: - TArray1D this_array_; - typename Kokkos::View::HostMirror h_this_array_; - - InheritedArray2L(); - - InheritedArray2L(size_t some_dim1); - - KOKKOS_INLINE_FUNCTION - T& operator()(size_t i, size_t dest) const; - - template - void AllocateHost(size_t size, U *obj); - - void AllocateGPU(); - - template - void InitModels(U *obj, V input); - - template - void ClearModels(U obj); - - InheritedArray2L& operator=(const InheritedArray2L& temp); - - // GPU Method - // Method that returns size - KOKKOS_INLINE_FUNCTION - size_t size(); - - // Host Method - // Method that returns size - size_t extent(); - - // Methods returns the raw pointer (most likely GPU) of the Kokkos View - T* pointer(); - - // Deconstructor - KOKKOS_INLINE_FUNCTION - ~InheritedArray2L (); -}; // End of InheritedArray2L - -// Default constructor -template -InheritedArray2L::InheritedArray2L() {} - -// Overloaded 1D constructor -template -InheritedArray2L::InheritedArray2L(size_t some_dim1) { - using TArray1D = Kokkos::View; - - dim1_ = some_dim1; - length_ = dim1_; - this_array_ = TArray1D("this_array_", length_); - h_this_array_ = Kokkos::create_mirror_view(this_array_); -} - -template -KOKKOS_INLINE_FUNCTION -T& InheritedArray2L::operator()(size_t i, size_t dest) const { - assert(i < dim1_ && "i is out of bounds in InheritedArray2L 1D!"); - assert(dest < 2 && "dest is out of bounds in InheritedArray2L 1D!"); - if (dest == 0) - return h_this_array_(i); - else - return this_array_(i); -} - -template -template -void InheritedArray2L::AllocateHost(size_t size, U *obj) { - obj = (U *) kmalloc(size); -} - -template -void InheritedArray2L::AllocateGPU() { - Kokkos::deep_copy(this_array_, h_this_array_); -} - -template -template -void InheritedArray2L::InitModels(U *obj, V input) { - Kokkos::parallel_for( - "CreateObjects", 1, KOKKOS_CLASS_LAMBDA(const int&) { - new ((V *)obj) V{input}; - }); -} - -template -template -void InheritedArray2L::ClearModels(U obj) { - Kokkos::parallel_for( - "DestroyObjects", 1, KOKKOS_LAMBDA(const int&) { - this_array_(0).obj->~U(); - this_array_(1).obj->~U(); - }); -} - -template -InheritedArray2L& InheritedArray2L::operator= (const InheritedArray2L& temp) { - using TArray1D = Kokkos::View; - - // Do nothing if the assignment is of the form x = x - if (this != &temp) { - dim1_ = temp.dim1_; - length_ = temp.length_; - this_array_ = TArray1D("this_array_", length_); - } - - return *this; -} - -// Return size -template -KOKKOS_INLINE_FUNCTION -size_t InheritedArray2L::size() { - return length_; -} - -template -size_t InheritedArray2L::extent() { - return length_; -} - -template -T* InheritedArray2L::pointer() { - return this_array_.data(); -} - -template -KOKKOS_INLINE_FUNCTION -InheritedArray2L::~InheritedArray2L() {} -*/ - -//////////////////////////////////////////////////////////////////////////////// -// End of InheritedArray2L -//////////////////////////////////////////////////////////////////////////////// - - -} // end namespace - -#endif // end if have Kokkos - - -#endif // KOKKOS_TYPES_H diff --git a/src/macros.h b/src/macros.h deleted file mode 100644 index fe29d70c..00000000 --- a/src/macros.h +++ /dev/null @@ -1,962 +0,0 @@ -#ifndef MACROS_H -#define MACROS_H -/********************************************************************************************** - © 2020. Triad National Security, LLC. All rights reserved. - This program was produced under U.S. Government contract 89233218CNA000001 for Los Alamos - National Laboratory (LANL), which is operated by Triad National Security, LLC for the U.S. - Department of Energy/National Nuclear Security Administration. All rights in the program are - reserved by Triad National Security, LLC, and the U.S. Department of Energy/National Nuclear - Security Administration. The Government is granted for itself and others acting on its behalf a - nonexclusive, paid-up, irrevocable worldwide license in this material to reproduce, prepare - derivative works, distribute copies to the public, perform publicly and display publicly, and - to permit others to do so. - This program is open source under the BSD-3 License. - Redistribution and use in source and binary forms, with or without modification, are permitted - provided that the following conditions are met: - - 1. Redistributions of source code must retain the above copyright notice, this list of - conditions and the following disclaimer. - - 2. Redistributions in binary form must reproduce the above copyright notice, this list of - conditions and the following disclaimer in the documentation and/or other materials - provided with the distribution. - - 3. Neither the name of the copyright holder nor the names of its contributors may be used - to endorse or promote products derived from this software without specific prior - written permission. - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS - IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - **********************************************************************************************/ - -/********************************************************************************************** - This file has suite of MACROS to build serial and parallel loops that are more readable and - are written with the same syntax. The parallel loops use kokkos (i.e., the MACROS hide the - complexity) and the serial loops are done using functions located in this file. The goal is to - help users add kokkos to their code projects for performance portability across architectures. - - The loop order with the MACRO enforces the inner loop varies the fastest and the outer most - loop varies the slowest. Optiminal performance will be achieved by ensureing the loop indices - align with the access pattern of the MATAR datatype. - - 1. The syntax to use the FOR_ALL MACRO is as follows: - - // parallelization over a single loop - FOR_ALL(k, 0, 10, - { loop contents is here }); - - // parallellization over two loops - FOR_ALL(m, 0, 3, - n, 0, 3, - { loop contents is here }); - - // parallellization over two loops - FOR_ALL(i, 0, 3, - j, 0, 3, - k, 0, 3, - { loop contents is here }); - - 2. The syntax to use the FOR_REDUCE is as follows: - - // reduce over a single loop - REDUCE_SUM(i, 0, 100, - local_answer, - { loop contents is here }, answer); - - REDUCE_SUM(i, 0, 100, - j, 0, 100, - local_answer, - { loop contents is here }, answer); - - REDUCE_SUM(i, 0, 100, - j, 0, 100, - k, 0, 100, - local_answer, - { loop contents is here }, answer); - - // other reduces are: RDUCE_MAX and REDUCE_MIN - **********************************************************************************************/ - - -#include -#include - - - - - -// ----------------------------------------- -// MACROS used with both Kokkos and non-kokkos versions -// ----------------------------------------- -// a macro to select the name of a macro based on the number of inputs -#define \ - GET_MACRO(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, NAME,...) NAME - - -// ----------------------------------------- -// MACROS for kokkos -// ----------------------------------------- - -#ifdef HAVE_KOKKOS - -// CArray nested loop convention use Right, use Left for outermost loop first -#define LOOP_ORDER Kokkos::Iterate::Right - -// FArray nested loop convention use Right -#define F_LOOP_ORDER Kokkos::Iterate::Right - - -// run once on the device -#define \ - RUN(fcn) \ - Kokkos::parallel_for( Kokkos::RangePolicy<> ( 0, 1), \ - KOKKOS_LAMBDA(const int ijkabc){fcn} ) - -// run once on the device inside a class -#define \ - RUN_CLASS(fcn) \ - Kokkos::parallel_for( Kokkos::RangePolicy<> ( 0, 1), \ - KOKKOS_CLASS_LAMBDA(const int ijkabc){fcn} ) - - -// the FOR_ALL loop -#define \ - FOR1D(i, x0, x1,fcn) \ - Kokkos::parallel_for( Kokkos::RangePolicy<> ( (x0), (x1)), \ - KOKKOS_LAMBDA( const int (i) ){fcn} ) - -#define \ - FOR2D(i, x0, x1, j, y0, y1,fcn) \ - Kokkos::parallel_for( \ - Kokkos::MDRangePolicy< Kokkos::Rank<2,LOOP_ORDER,LOOP_ORDER> > ( {(x0), (y0)}, {(x1), (y1)} ), \ - KOKKOS_LAMBDA( const int (i), const int (j) ){fcn} ) - -#define \ - FOR3D(i, x0, x1, j, y0, y1, k, z0, z1, fcn) \ - Kokkos::parallel_for( \ - Kokkos::MDRangePolicy< Kokkos::Rank<3,LOOP_ORDER,LOOP_ORDER> > ( {(x0), (y0), (z0)}, {(x1), (y1), (z1)} ), \ - KOKKOS_LAMBDA( const int (i), const int (j), const int (k) ) {fcn} ) - -#define \ - FOR_ALL(...) \ - GET_MACRO(__VA_ARGS__, _13, _12, _11, FOR3D, _9, _8, FOR2D, _6, _5, FOR1D)(__VA_ARGS__) - - -// the DO_ALL loop -#define \ - DO1D(i, x0, x1,fcn) \ - Kokkos::parallel_for( Kokkos::RangePolicy<> ( (x0), (x1)+1), \ - KOKKOS_LAMBDA( const int (i) ){fcn} ) - -#define \ - DO2D(i, x0, x1, j, y0, y1,fcn) \ - Kokkos::parallel_for( \ - Kokkos::MDRangePolicy< Kokkos::Rank<2,F_LOOP_ORDER, F_LOOP_ORDER> > ( {(x0), (y0)}, {(x1)+1, (y1)+1} ), \ - KOKKOS_LAMBDA( const int (i), const int (j) ){fcn} ) - -#define \ - DO3D(i, x0, x1, j, y0, y1, k, z0, z1, fcn) \ - Kokkos::parallel_for( \ - Kokkos::MDRangePolicy< Kokkos::Rank<3,F_LOOP_ORDER,F_LOOP_ORDER> > ( {(x0), (y0), (z0)}, {(x1)+1, (y1)+1, (z1)+1} ), \ - KOKKOS_LAMBDA( const int (i), const int (j), const int (k) ) {fcn} ) - -#define \ - DO_ALL(...) \ - GET_MACRO(__VA_ARGS__, _13, _12, _11, DO3D, _9, _8, DO2D, _6, _5, DO1D)(__VA_ARGS__) - - -// the REDUCE SUM loop -#define \ - RSUM1D(i, x0, x1, var, fcn, result) \ - Kokkos::parallel_reduce( Kokkos::RangePolicy<> ( (x0), (x1) ), \ - KOKKOS_LAMBDA(const int (i), decltype(var) &(var)){fcn}, (result)) - -#define \ - RSUM2D(i, x0, x1, j, y0, y1, var, fcn, result) \ - Kokkos::parallel_reduce( \ - Kokkos::MDRangePolicy< Kokkos::Rank<2,LOOP_ORDER,LOOP_ORDER> > ( {(x0), (y0)}, {(x1), (y1)} ), \ - KOKKOS_LAMBDA( const int (i),const int (j), decltype(var) &(var) ){fcn}, \ - (result) ) - -#define \ - RSUM3D(i, x0, x1, j, y0, y1, k, z0, z1, var, fcn, result) \ - Kokkos::parallel_reduce( \ - Kokkos::MDRangePolicy< Kokkos::Rank<3,LOOP_ORDER,LOOP_ORDER> > ( {(x0), (y0), (z0)}, {(x1), (y1), (z1)} ), \ - KOKKOS_LAMBDA( const int (i), const int (j), const int (k), decltype(var) &(var) ){fcn}, \ - (result) ) - -#define \ - REDUCE_SUM(...) \ - GET_MACRO(__VA_ARGS__, _13, RSUM3D, _11, _10, RSUM2D, _8, _7, RSUM1D)(__VA_ARGS__) - - -// the DO_REDUCE_SUM loop -#define \ - DO_RSUM1D(i, x0, x1, var, fcn, result) \ - Kokkos::parallel_reduce( Kokkos::RangePolicy<> ( (x0), (x1)+1 ), \ - KOKKOS_LAMBDA(const int (i), decltype(var) &(var)){fcn}, (result)) - -#define \ - DO_RSUM2D(i, x0, x1, j, y0, y1, var, fcn, result) \ - Kokkos::parallel_reduce( \ - Kokkos::MDRangePolicy< Kokkos::Rank<2,F_LOOP_ORDER,F_LOOP_ORDER> > ( {(x0), (y0)}, {(x1)+1, (y1)+1} ), \ - KOKKOS_LAMBDA( const int (i),const int (j), decltype(var) &(var) ){fcn}, \ - (result) ) - -#define \ - DO_RSUM3D(i, x0, x1, j, y0, y1, k, z0, z1, var, fcn, result) \ - Kokkos::parallel_reduce( \ - Kokkos::MDRangePolicy< Kokkos::Rank<3,F_LOOP_ORDER,F_LOOP_ORDER> > ( {(x0), (y0), (z0)}, {(x1)+1, (y1)+1, (z1)+1} ), \ - KOKKOS_LAMBDA( const int (i), const int (j), const int (k), decltype(var) &(var) ){fcn}, \ - (result) ) - -#define \ - DO_REDUCE_SUM(...) \ - GET_MACRO(__VA_ARGS__, _13, DO_RSUM3D, _11, _10, DO_RSUM2D, _8, _7, DO_RSUM1D)(__VA_ARGS__) - - -// the REDUCE MAX loop -#define \ - RMAX1D(i, x0, x1, var, fcn, result) \ - Kokkos::parallel_reduce( \ - Kokkos::RangePolicy<> ( (x0), (x1) ), \ - KOKKOS_LAMBDA(const int (i), decltype(var) &(var)){fcn}, \ - Kokkos::Max< decltype(result) > ( (result) ) ) - -#define \ - RMAX2D(i, x0, x1, j, y0, y1, var, fcn, result) \ - Kokkos::parallel_reduce( \ - Kokkos::MDRangePolicy< Kokkos::Rank<2,LOOP_ORDER,LOOP_ORDER> > ( {(x0), (y0)}, {(x1), (y1)} ), \ - KOKKOS_LAMBDA( const int (i),const int (j), decltype(var) &(var) ){fcn}, \ - Kokkos::Max< decltype(result) > ( (result) ) ) - -#define \ - RMAX3D(i, x0, x1, j, y0, y1, k, z0, z1, var, fcn, result) \ - Kokkos::parallel_reduce( \ - Kokkos::MDRangePolicy< Kokkos::Rank<3,LOOP_ORDER,LOOP_ORDER> > ( {(x0), (y0), (z0)}, {(x1), (y1), (z1)} ), \ - KOKKOS_LAMBDA( const int (i), const int (j), const int (k), decltype(var) &(var) ){fcn}, \ - Kokkos::Max< decltype(result) > ( (result) ) ) - -#define \ - REDUCE_MAX(...) \ - GET_MACRO(__VA_ARGS__, _13, RMAX3D, _11, _10, RMAX2D, _8, _7, RMAX1D)(__VA_ARGS__) - - -// the DO_REDUCE_MAX loop -#define \ - DO_RMAX1D(i, x0, x1, var, fcn, result) \ - Kokkos::parallel_reduce( \ - Kokkos::RangePolicy<> ( (x0), (x1)+1 ), \ - KOKKOS_LAMBDA(const int (i), decltype(var) &(var)){fcn}, \ - Kokkos::Max< decltype(result) > ( (result) ) ) - -#define \ - DO_RMAX2D(i, x0, x1, j, y0, y1, var, fcn, result) \ - Kokkos::parallel_reduce( \ - Kokkos::MDRangePolicy< Kokkos::Rank<2,F_LOOP_ORDER,F_LOOP_ORDER> > ( {(x0), (y0)}, {(x1)+1, (y1)+1} ), \ - KOKKOS_LAMBDA( const int (i),const int (j), decltype(var) &(var) ){fcn}, \ - Kokkos::Max< decltype(result) > ( (result) ) ) - -#define \ - DO_RMAX3D(i, x0, x1, j, y0, y1, k, z0, z1, var, fcn, result) \ - Kokkos::parallel_reduce( \ - Kokkos::MDRangePolicy< Kokkos::Rank<3,F_LOOP_ORDER,F_LOOP_ORDER> > ( {(x0), (y0), (z0)}, {(x1)+1, (y1)+1, (z1)+1} ), \ - KOKKOS_LAMBDA( const int (i), const int (j), const int (k), decltype(var) &(var) ){fcn}, \ - Kokkos::Max< decltype(result) > ( (result) ) ) - -#define \ - DO_REDUCE_MAX(...) \ - GET_MACRO(__VA_ARGS__, _13, DO_RMAX3D, _11, _10, DO_RMAX2D, _8, _7, DO_RMAX1D)(__VA_ARGS__) - - - -// the REDUCE MIN loop -#define \ - RMIN1D(i, x0, x1, var, fcn, result) \ - Kokkos::parallel_reduce( \ - Kokkos::RangePolicy<> ( (x0), (x1) ), \ - KOKKOS_LAMBDA( const int (i), decltype(var) &(var) ){fcn}, \ - Kokkos::Min< decltype(result) >(result)) - -#define \ - RMIN2D(i, x0, x1, j, y0, y1, var, fcn, result) \ - Kokkos::parallel_reduce( \ - Kokkos::MDRangePolicy< Kokkos::Rank<2,LOOP_ORDER,LOOP_ORDER> > ( {(x0), (y0)}, {(x1), (y1)} ), \ - KOKKOS_LAMBDA( const int (i),const int (j), decltype(var) &(var) ){fcn}, \ - Kokkos::Min< decltype(result) >(result) ) - -#define \ - RMIN3D(i, x0, x1, j, y0, y1, k, z0, z1, var, fcn, result) \ - Kokkos::parallel_reduce( \ - Kokkos::MDRangePolicy< Kokkos::Rank<3,LOOP_ORDER,LOOP_ORDER> > ( {(x0), (y0), (z0)}, {(x1), (y1), (z1)} ), \ - KOKKOS_LAMBDA( const int (i), const int (j), const int (k), decltype(var) &(var) ){fcn}, \ - Kokkos::Min< decltype(result) >(result) ) - -#define \ - REDUCE_MIN(...) \ - GET_MACRO(__VA_ARGS__, _13, RMIN3D, _11, _10, RMIN2D, _8, _7, RMIN1D)(__VA_ARGS__) - - -// the DO_REDUCE MIN loop -#define \ - DO_RMIN1D(i, x0, x1, var, fcn, result) \ - Kokkos::parallel_reduce( \ - Kokkos::RangePolicy<> ( (x0), (x1)+1 ), \ - KOKKOS_LAMBDA( const int (i), decltype(var) &(var) ){fcn}, \ - Kokkos::Min< decltype(result) >(result)) - -#define \ - DO_RMIN2D(i, x0, x1, j, y0, y1, var, fcn, result) \ - Kokkos::parallel_reduce( \ - Kokkos::MDRangePolicy< Kokkos::Rank<2,F_LOOP_ORDER,F_LOOP_ORDER> > ( {(x0), (y0)}, {(x1)+1, (y1)+1} ), \ - KOKKOS_LAMBDA( const int (i),const int (j), decltype(var) &(var) ){fcn}, \ - Kokkos::Min< decltype(result) >(result) ) - -#define \ - DO_RMIN3D(i, x0, x1, j, y0, y1, k, z0, z1, var, fcn, result) \ - Kokkos::parallel_reduce( \ - Kokkos::MDRangePolicy< Kokkos::Rank<3,F_LOOP_ORDER,F_LOOP_ORDER> > ( {(x0), (y0), (z0)}, {(x1)+1, (y1)+1, (z1)+1} ), \ - KOKKOS_LAMBDA( const int (i), const int (j), const int (k), decltype(var) &(var) ){fcn}, \ - Kokkos::Min< decltype(result) >(result) ) - -#define \ - DO_REDUCE_MIN(...) \ - GET_MACRO(__VA_ARGS__, _13, DO_RMIN3D, _11, _10, DO_RMIN2D, _8, _7, DO_RMIN1D)(__VA_ARGS__) - - - -// the FOR_ALL loop with variables in a class -#define \ -FORCLASS1D(i, x0, x1,fcn) \ -Kokkos::parallel_for( Kokkos::RangePolicy<> ( (x0), (x1)), \ - KOKKOS_CLASS_LAMBDA( const int (i) ){fcn} ) - -#define \ -FORCLASS2D(i, x0, x1, j, y0, y1,fcn) \ -Kokkos::parallel_for( \ - Kokkos::MDRangePolicy< Kokkos::Rank<2,LOOP_ORDER,LOOP_ORDER> > ( {(x0), (y0)}, {(x1), (y1)} ), \ - KOKKOS_CLASS_LAMBDA( const int (i), const int (j) ){fcn} ) - -#define \ -FORCLASS3D(i, x0, x1, j, y0, y1, k, z0, z1, fcn) \ -Kokkos::parallel_for( \ - Kokkos::MDRangePolicy< Kokkos::Rank<3,LOOP_ORDER,LOOP_ORDER> > ( {(x0), (y0), (z0)}, {(x1), (y1), (z1)} ), \ - KOKKOS_CLASS_LAMBDA( const int (i), const int (j), const int (k) ) {fcn} ) - -#define \ -FOR_ALL_CLASS(...) \ -GET_MACRO(__VA_ARGS__, _13, _12, _11, FORCLASS3D, _9, _8, FORCLASS2D, _6, _5, FORCLASS1D)(__VA_ARGS__) - - -// the REDUCE SUM loop -#define \ -RSUMCLASS1D(i, x0, x1, var, fcn, result) \ -Kokkos::parallel_reduce( Kokkos::RangePolicy<> ( (x0), (x1) ), \ - KOKKOS_CLASS_LAMBDA(const int (i), decltype(var) &(var)){fcn}, (result)) - -#define \ -RSUMCLASS2D(i, x0, x1, j, y0, y1, var, fcn, result) \ -Kokkos::parallel_reduce( \ - Kokkos::MDRangePolicy< Kokkos::Rank<2,LOOP_ORDER,LOOP_ORDER> > ( {(x0), (y0)}, {(x1), (y1)} ), \ - KOKKOS_CLASS_LAMBDA( const int (i),const int (j), decltype(var) &(var) ){fcn}, \ - (result) ) - -#define \ -RSUMCLASS3D(i, x0, x1, j, y0, y1, k, z0, z1, var, fcn, result) \ -Kokkos::parallel_reduce( \ - Kokkos::MDRangePolicy< Kokkos::Rank<3,LOOP_ORDER,LOOP_ORDER> > ( {(x0), (y0), (z0)}, {(x1), (y1), (z1)} ), \ - KOKKOS_CLASS_LAMBDA( const int (i), const int (j), const int (k), decltype(var) &(var) ){fcn}, \ - (result) ) - -#define \ -REDUCE_SUM_CLASS(...) \ -GET_MACRO(__VA_ARGS__, _13, RSUMCLASS3D, _11, _10, RSUMCLASS2D, _8, _7, RSUMCLASS1D)(__VA_ARGS__) - - - -// the REDUCE MAX loop with variables in a class - -#define \ -RMAXCLASS1D(i, x0, x1, var, fcn, result) \ -Kokkos::parallel_reduce( \ - Kokkos::RangePolicy<> ( (x0), (x1) ), \ - KOKKOS_CLASS_LAMBDA(const int (i), decltype(var) &(var)){fcn}, \ - Kokkos::Max< decltype(result) > ( (result) ) ) - -#define \ -RMAXCLASS2D(i, x0, x1, j, y0, y1, var, fcn, result) \ -Kokkos::parallel_reduce( \ - Kokkos::MDRangePolicy< Kokkos::Rank<2,LOOP_ORDER,LOOP_ORDER> > ( {(x0), (y0)}, {(x1), (y1)} ), \ - KOKKOS_CLASS_LAMBDA( const int (i),const int (j), decltype(var) &(var) ){fcn}, \ - Kokkos::Max< decltype(result) > ( (result) ) ) - -#define \ -RMAXCLASS3D(i, x0, x1, j, y0, y1, k, z0, z1, var, fcn, result) \ -Kokkos::parallel_reduce( \ - Kokkos::MDRangePolicy< Kokkos::Rank<3,LOOP_ORDER,LOOP_ORDER> > ( {(x0), (y0), (z0)}, {(x1), (y1), (z1)} ), \ - KOKKOS_CLASS_LAMBDA( const int (i), const int (j), const int (k), decltype(var) &(var) ){fcn}, \ - Kokkos::Max< decltype(result) > ( (result) ) ) - -#define \ -REDUCE_MAX_CLASS(...) \ -GET_MACRO(__VA_ARGS__, _13, RMAXCLASS3D, _11, _10, RMAXCLASS2D, _8, _7, RMAXCLASS1D)(__VA_ARGS__) - - -// the REDUCE MIN loop with variables in a class -#define \ -RMINCLASS1D(i, x0, x1, var, fcn, result) \ -Kokkos::parallel_reduce( \ - Kokkos::RangePolicy<> ( (x0), (x1) ), \ - KOKKOS_CLASS_LAMBDA( const int (i), decltype(var) &(var) ){fcn}, \ - Kokkos::Min< decltype(result) >(result)) - -#define \ -RMINCLASS2D(i, x0, x1, j, y0, y1, var, fcn, result) \ -Kokkos::parallel_reduce( \ - Kokkos::MDRangePolicy< Kokkos::Rank<2,LOOP_ORDER,LOOP_ORDER> > ( {(x0), (y0)}, {(x1), (y1)} ), \ - KOKKOS_CLASS_LAMBDA( const int (i),const int (j), decltype(var) &(var) ){fcn}, \ - Kokkos::Min< decltype(result) >(result) ) - -#define \ -RMINCLASS3D(i, x0, x1, j, y0, y1, k, z0, z1, var, fcn, result) \ -Kokkos::parallel_reduce( \ - Kokkos::MDRangePolicy< Kokkos::Rank<3,LOOP_ORDER,LOOP_ORDER> > ( {(x0), (y0), (z0)}, {(x1), (y1), (z1)} ), \ - KOKKOS_CLASS_LAMBDA( const int (i), const int (j), const int (k), decltype(var) &(var) ){fcn}, \ - Kokkos::Min< decltype(result) >(result) ) - -#define \ -REDUCE_MIN_CLASS(...) \ -GET_MACRO(__VA_ARGS__, _13, RMINCLASS3D, _11, _10, RMINCLASS2D, _8, _7, RMINCLASS1D)(__VA_ARGS__) - -#endif - - -// end of KOKKOS routines - - - - -// ----------------------------------------- -// The for_all is used for serial loops and -// with the non-kokkos MACROS -// ----------------------------------------- - -template -void for_all (int i_start, int i_end, - const F &lambda_fcn){ - - for (int i=i_start; i -void for_all (int i_start, int i_end, - int j_start, int j_end, - const F &lambda_fcn){ - - for (int i=i_start; i -void for_all (int i_start, int i_end, - int j_start, int j_end, - int k_start, int k_end, - const F &lambda_fcn){ - - for (int i=i_start; i -void for_all_delta (int i_start, int i_end, int i_delta, - const F &lambda_fcn){ - - for (int i=i_start; i -void for_all_delta (int i_start, int i_end, int i_delta, - int j_start, int j_end, int j_delta, - const F &lambda_fcn){ - - for (int i=i_start; i -void for_all_delta (int i_start, int i_end, int i_delta, - int j_start, int j_end, int j_delta, - int k_start, int k_end, int k_delta, - const F &lambda_fcn){ - - for (int i=i_start; i // for the max and min values of a int, double, etc. - -// SUM -template -void reduce_sum (int i_start, int i_end, - T var, - const F &lambda_fcn, T &result){ - var = 0; - for (int i=i_start; i -void reduce_sum (int i_start, int i_end, - int j_start, int j_end, - T var, - const F &lambda_fcn, T &result){ - var = 0; - for (int i=i_start; i -void reduce_sum (int i_start, int i_end, - int j_start, int j_end, - int k_start, int k_end, - T var, - const F &lambda_fcn, T &result){ - var = 0; - for (int i=i_start; i -void reduce_min (int i_start, int i_end, - T var, - const F &lambda_fcn, T &result){ - var = std::numeric_limits::max(); //2147483647; - for (int i=i_start; i -void reduce_min (int i_start, int i_end, - int j_start, int j_end, - T var, - const F &lambda_fcn, T &result){ - var = std::numeric_limits::max(); //2147483647; - for (int i=i_start; i -void reduce_min (int i_start, int i_end, - int j_start, int j_end, - int k_start, int k_end, - T var, - const F &lambda_fcn, T &result){ - var = std::numeric_limits::max(); //2147483647; - for (int i=i_start; i -void reduce_max (int i_start, int i_end, - T var, - const F &lambda_fcn, T &result){ - var = std::numeric_limits::min(); // -2147483647 - 1; - for (int i=i_start; i -void reduce_max (int i_start, int i_end, - int j_start, int j_end, - T var, - const F &lambda_fcn, T &result){ - var = std::numeric_limits::min(); //-2147483647 - 1; - for (int i=i_start; i -void reduce_max (int i_start, int i_end, - int j_start, int j_end, - int k_start, int k_end, - T var, - const F &lambda_fcn, T &result){ - var = std::numeric_limits::min(); // -2147483647 - 1; - for (int i=i_start; i