diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..df51ef6 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,6 @@ +[submodule "tpls/kokkos"] + path = tpls/kokkos + url = https://github.com/kokkos/kokkos.git +[submodule "tpls/googletest"] + path = tpls/googletest + url = https://github.com/google/googletest.git diff --git a/CMakeLists.txt b/CMakeLists.txt index abfbc87..7fd2e31 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,21 +1,39 @@ -cmake_minimum_required(VERSION 3.21) -project(kokkos_internal CXX) -set(CMAKE_CXX_STANDARD 20 CACHE STRING "The C++ standard to use") -set(CMAKE_CXX_STANDARD_REQUIRED ON) -message(STATUS "Kokkos is building with C++ Standard: ${CMAKE_CXX_STANDARD}") -set(DESUL_ENABLE_FOR_TEST ON CACHE BOOL "Enable Desul test mode (used by subprojects via FetchContent)" FORCE) -include(FetchContent) -set(KOKKOS_FETCH_TAG "5.1.1" CACHE STRING "Kokkos git tag to fetch") -find_package(Kokkos 5.1.1 QUIET) -if (NOT Kokkos_FOUND) - message(STATUS "Kokkos not found locally - fetching tag " "${KOKKOS_FETCH_TAG} via FetchContent") - FetchContent_Declare(kokkos GIT_REPOSITORY https://github.com/kokkos/kokkos.git GIT_TAG ${KOKKOS_FETCH_TAG} GIT_SHALLOW ON) - FetchContent_MakeAvailable(kokkos) +cmake_minimum_required(VERSION 3.22) +project(Kokkos_Nums VERSION 0.0.1 LANGUAGES CXX) +list(PREPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/tpls/kokkos/cmake") +set(Kokkos_Nums_VERSION ${PROJECT_VERSION}) +set(Kokkos_Nums_VERSION_MAJOR ${PROJECT_VERSION_MAJOR}) +set(Kokkos_Nums_VERSION_MINOR ${PROJECT_VERSION_MINOR}) +set(Kokkos_Nums_VERSION_PATCH ${PROJECT_VERSION_PATCH}) + +set(KOKKOS_REQUIRED_VERSION 5.2) +option(Kokkos_Nums_ENABLE_TESTS "Enable tests" ON) +option(Kokkos_Nums_ENABLE_INTERNAL_KOKKOS "Enable internal Kokkos build" OFF) +option(Kokkos_Nums_USE_find_package "Use find_package to locate Kokkos" OFF) +option(Kokkos_Nums_GYSELALIBXX_USE "Use GyselaLibxx" ON) +if(Kokkos_Nums_USE_find_package) + if(NOT TARGET Kokkos::kokkos) + find_package(Kokkos ${KOKKOS_REQUIRED_VERSION} REQUIRED) + endif() else() - message(STATUS "Using pre-installed Kokkos: ${Kokkos_DIR}") + add_subdirectory(tpls/kokkos) endif() -FetchContent_Declare( - googletest GIT_REPOSITORY https://github.com/google/googletest.git GIT_TAG v1.17.0) -FetchContent_MakeAvailable(googletest) -enable_testing() -add_subdirectory(kokkos_tree) +message(STATUS "Kokkos_Nums_ENABLE_TESTS: ${Kokkos_Nums_ENABLE_TESTS}") +message(STATUS "Kokkos_Nums_ENABLE_INTERNAL_KOKKOS: ${Kokkos_Nums_ENABLE_INTERNAL_KOKKOS}") +message(STATUS "Kokkos version: ${Kokkos_VERSION}") +message(STATUS "Kokkos_Nums version major: ${Kokkos_Nums_VERSION_MAJOR}") +message(STATUS "Kokkos_Nums version minor: ${Kokkos_Nums_VERSION_MINOR}") +message(STATUS "Kokkos_Nums version patch: ${Kokkos_Nums_VERSION_PATCH}") +add_library(kokkos_num INTERFACE) +target_link_libraries(kokkos_num INTERFACE Kokkos::kokkos) +target_include_directories(kokkos_num INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}) +if(Kokkos_Nums_ENABLE_TESTS) + include(CTest) + find_package(GTest CONFIG) + if(NOT GTest_FOUND) + add_subdirectory(tpls/googletest) + endif() +endif() +add_subdirectory(Tests) +add_subdirectory(src) + diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt new file mode 100644 index 0000000..f8b07a5 --- /dev/null +++ b/Tests/CMakeLists.txt @@ -0,0 +1,8 @@ +enable_testing() +add_executable(Kokkos_qun_test test_dyn_rank.cpp) +target_link_libraries(Kokkos_qun_test PRIVATE Kokkos::kokkos GTest::gtest) + +add_test( + NAME Kokkos_qun_test + COMMAND Kokkos_qun_test +) diff --git a/kokkos_tree/desul/desul_test.hpp b/Tests/desul/desul_test.hpp similarity index 100% rename from kokkos_tree/desul/desul_test.hpp rename to Tests/desul/desul_test.hpp diff --git a/Tests/kokkos_core/AnalyzeExecPolicyUserMatch.hpp b/Tests/kokkos_core/AnalyzeExecPolicyUserMatch.hpp new file mode 100644 index 0000000..deadffb --- /dev/null +++ b/Tests/kokkos_core/AnalyzeExecPolicyUserMatch.hpp @@ -0,0 +1,29 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using Policy = Kokkos::Impl::AnalyzeExecPolicy; //searching ExecutionSpaceTrait +namespace Test{ +TEST(AnalyzeExecPolicyUserMatch,line_52_AnalyzeP_file){ + using analyzeExecPolicyVoid = Kokkos::Impl::AnalyzeExecPolicy; + static_assert(analyzeExecPolicyVoid::execution_space_is_defaulted, "Not Ok"); + using analyzedPolicyExec = Kokkos::Impl::AnalyzeExecPolicy; + //auto recv = analyzedPolicyExec::show_execution_space_error_in_compilation_message; + static_assert(std::is_same_v,"NOT SAME"); + using analyzedPolicyExecSch = Kokkos::Impl::AnalyzeExecPolicy>; + static_assert(std::is_same_v>,"NOT SAME"); + +} + +} \ No newline at end of file diff --git a/kokkos_tree/kokkos_core/README.md b/Tests/kokkos_core/README.md similarity index 100% rename from kokkos_tree/kokkos_core/README.md rename to Tests/kokkos_core/README.md diff --git a/kokkos_tree/kokkos_core/View_data_analysis_kokkos_investigation.hpp b/Tests/kokkos_core/View_data_analysis_kokkos_investigation.hpp similarity index 100% rename from kokkos_tree/kokkos_core/View_data_analysis_kokkos_investigation.hpp rename to Tests/kokkos_core/View_data_analysis_kokkos_investigation.hpp diff --git a/kokkos_tree/kokkos_core/kokkos_pair_investigation.hpp b/Tests/kokkos_core/kokkos_pair_investigation.hpp similarity index 100% rename from kokkos_tree/kokkos_core/kokkos_pair_investigation.hpp rename to Tests/kokkos_core/kokkos_pair_investigation.hpp diff --git a/kokkos_tree/kokkos_core/test_drank.hpp b/Tests/kokkos_core/test_drank.hpp similarity index 100% rename from kokkos_tree/kokkos_core/test_drank.hpp rename to Tests/kokkos_core/test_drank.hpp diff --git a/kokkos_tree/mdspan/README.md b/Tests/mdspan/README.md similarity index 100% rename from kokkos_tree/mdspan/README.md rename to Tests/mdspan/README.md diff --git a/kokkos_tree/mdspan/kokkos_extent_inv.hpp b/Tests/mdspan/kokkos_extent_inv.hpp similarity index 100% rename from kokkos_tree/mdspan/kokkos_extent_inv.hpp rename to Tests/mdspan/kokkos_extent_inv.hpp diff --git a/kokkos_tree/mdspan/kokkos_raw_mdspan.hpp b/Tests/mdspan/kokkos_raw_mdspan.hpp similarity index 95% rename from kokkos_tree/mdspan/kokkos_raw_mdspan.hpp rename to Tests/mdspan/kokkos_raw_mdspan.hpp index 8174005..5b6ed45 100644 --- a/kokkos_tree/mdspan/kokkos_raw_mdspan.hpp +++ b/Tests/mdspan/kokkos_raw_mdspan.hpp @@ -31,9 +31,9 @@ TEST(mdspan_static_array_impl, line_79_100) { using Stat_arr = mdspan_detail::static_array_impl<0, size_t, 10, 20, 30>; static_assert(Stat_arr::get(0) == 10, "Should be 10"); // It's for runtime static_assert(Stat_arr::get(1) == 20, "Should be 20"); // same for this; - static_assert(Stat_arr::get<2>() == 30, "Should be 30"); + //static_assert(Stat_arr::get<2>() == 30, "Should be 30"); using Stat_arr2 = mdspan_detail::static_array_impl<2, size_t, 1, 2, 3>; - static_assert(Stat_arr2::get<2>() == 1, "Should be 1"); + //static_assert(Stat_arr2::get<2>() == 1, "Should be 1"); // static_assert(Stat_arr2::get<1>()==1,"Should be 1"); auto val = Stat_arr2::get(2); std::cout << val << std::endl; @@ -42,7 +42,7 @@ TEST(mdspan_static_array_impl, line_79_100) { using stat_arr = mdspan_detail::static_array; constexpr auto va = stat_arr::size(); static_assert(va == 3, "Should match 3"); - EXPECT_EQ(stat_arr::get<2>(), 30); + //EXPECT_EQ(stat_arr::get<2>(), 30); using ind_seq = mdspan_detail::index_sequence_scan_impl<0, 1, 2, 3>; auto constexpr val_in = ind_seq::get(2); EXPECT_EQ(val_in, 3); @@ -62,8 +62,8 @@ TEST(maybe_static_array, line_206_start) { mdspan_detail::maybe_static_array; using static_array_impl_may_bug = mdspan_detail::static_array_impl<0,int,15,25,35>; - auto constexpr val = static_array_impl_may_bug::get<1>(); - EXPECT_EQ(val, 25); + // auto constexpr val = static_array_impl_may_bug::get<1>(); + //EXPECT_EQ(val, 25); // HybridArray my_extents{50}; // using static_val_type = HybridArray::static_vals_t; // using value_type = static_val_type::value_type; inaccessible as private diff --git a/kokkos_tree/test_dyn_rank.cpp b/Tests/test_dyn_rank.cpp similarity index 91% rename from kokkos_tree/test_dyn_rank.cpp rename to Tests/test_dyn_rank.cpp index 54965f8..2835bd7 100644 --- a/kokkos_tree/test_dyn_rank.cpp +++ b/Tests/test_dyn_rank.cpp @@ -1,6 +1,6 @@ #include "desul/desul_test.hpp" #include "kokkos_core/kokkos_pair_investigation.hpp" -#include "mdspan/kokkos_raw_mdspan.hpp" +//#include "mdspan/kokkos_raw_mdspan.hpp" #include "kokkos_core/test_drank.hpp" #include "kokkos_core/View_data_analysis_kokkos_investigation.hpp" #include "mdspan/kokkos_extent_inv.hpp" diff --git a/kokkos_tree/CMakeLists.txt b/kokkos_tree/CMakeLists.txt deleted file mode 100644 index 738823a..0000000 --- a/kokkos_tree/CMakeLists.txt +++ /dev/null @@ -1,17 +0,0 @@ -cmake_minimum_required(VERSION 3.21) -include(GoogleTest) -if(NOT PROJECT_NAME) - project(test_dyn_rank CXX) - set(CMAKE_CXX_STANDARD 20) - find_package(Kokkos 5.1.1 REQUIRED) - - enable_testing() -endif() - -add_executable(Kokkos_qun_test test_dyn_rank.cpp) -target_link_libraries(Kokkos_qun_test PRIVATE Kokkos::kokkos GTest::gtest) - -add_test( - NAME Kokkos_qun_test - COMMAND Kokkos_qun_test -) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..0cc2d56 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,2 @@ +add_subdirectory(vector) +add_subdirectory(View) \ No newline at end of file diff --git a/src/View/CMakeLists.txt b/src/View/CMakeLists.txt new file mode 100644 index 0000000..f4eb52c --- /dev/null +++ b/src/View/CMakeLists.txt @@ -0,0 +1,5 @@ + +add_executable(kokkos_num_exe1 view.cpp) +target_compile_features(kokkos_num_exe1 PUBLIC cxx_std_20) +#target_compile_definitions(kokkos_num_exe1 PUBLIC KOKKOS_NUMS_ENABLE_TESTS) +target_link_libraries(kokkos_num_exe1 PRIVATE kokkos_num) diff --git a/src/View/view.cpp b/src/View/view.cpp new file mode 100644 index 0000000..2809b44 --- /dev/null +++ b/src/View/view.cpp @@ -0,0 +1,8 @@ +#include "view.hpp" + +int main(int argc,char* argv[]){ + Kokkos::initialize(argc,argv); + create_required_span_rank> (1000000*8,"view1",1000000); + Kokkos::finalize(); + return 0; +} \ No newline at end of file diff --git a/src/View/view.hpp b/src/View/view.hpp new file mode 100644 index 0000000..8853160 --- /dev/null +++ b/src/View/view.hpp @@ -0,0 +1,86 @@ +#include +#include +template +KOKKOS_FUNCTION void launch_parallel_kernel(ViewType& view) +{ + Kokkos::parallel_for("",Kokkos::RangePolicy<>(0, view.size()), KOKKOS_LAMBDA(int i){ + view(i) = static_cast(i); + }); + auto host_view = Kokkos::create_mirror_view(view); + Kokkos::deep_copy(host_view, view); + for(size_t i = 0; i < host_view.size(); i++) + { + if(i%20 == 0){ + printf("view(%zu) = %f\n", i, static_cast(host_view(i))); + } + } +} +template +void create_required_span_rank(size_t expected_size, std::string label, Sizes... sizes) +{ + ViewType view(label,sizes...); + size_t req_allocation_size = ViewType::required_allocation_size(sizes...); //this goes to Kokkos::mdspan + //printf("View %s: expected size = %zu, required allocation size = %zu\n", label.c_str(), expected_size, req_allocation_size); + if(expected_size == req_allocation_size) + { + launch_parallel_kernel(view); + } +} +template +auto make_mdrange_from_extents(const ExtentsType &extents,std::index_sequence<0>) +{ + return Kokkos::RangePolicy{0,static_cast(extents.extent(0))}; +} + +template +auto make_mdrange_from_extents(const ExtentsType &extents, std::index_sequence) +{ + using index_type = typename ExtentsType::index_type; + using rank_type = ExtentsType::rank_type; + rank_type rank = ExtentsType::rank(); + return Kokkos::MDRangePolicy,Kokkos::IndexType>{{static_cast(0*Indices)...}, {static_cast(extents.extent(Indices))...}}; +} +template +auto make_Bview_from_checked_accessor(const ExtentsType &extents){ + using extents_type = ExtentsType; + using layout_type = LayoutType; + using accessor_type = Kokkos::Impl::CheckedReferenceCountedAccessor; + using view_type = Kokkos::Impl::BV::BasicView; + view_type view("Test View", extents); + for (int r = 0; r(view_type::rank()); r++){ + expected_size *= extents.extent(r); + } + auto size = view.size(); + return std::pair(view, size, expected_size); +} +template +auto make_BView_from_mapping_and_padding(const ExtentsType &extents, std::size_t padding){ + using extents_type = ExtentsType; + using layout_type = LayoutType; + mapping_type = typename layout_type::template mapping_type; + using accessor_type = Kokkos::Impl::CheckedReferenceCountedAccessor; + using view_type = Kokkos::Impl::BV::BasicView; + auto mapping = mappint_type(extents, padding); + view_type("Bview", mapping); + for (int r =0; r(view_type::rank()); r++){ + expected_size *= view.extent(r); + } + std::size_t size = view.size(); + return std::pair(view, size, expected_size); +} +template +auto make_parallel_run_from_extents(const ExtentsType &extents){ + using extents_type = ExtentsType; + using layout_type = LayoutType; + //using mapping_type = typename layout_type::mapping_type; + using accessor_type = Kokkos::Impl::CheckedReferenceCountedAccessor; + using view_type = Kokkos::Impl::BV::BasicView; + auto view = view_type("Bview", extents); + auto mdrange = make_mdrange_from_extents(extents,std::make_index_sequence{}); + Kokkos::parallel_for("Label", mdrange_policy, mdrange, FuntorType{}); + auto host_view = create_mirror_view(view); + Kokkos::deep_copy(host_view,view); + return host_view; +} + + diff --git a/src/vector/CMakeLists.txt b/src/vector/CMakeLists.txt new file mode 100644 index 0000000..3bc5427 --- /dev/null +++ b/src/vector/CMakeLists.txt @@ -0,0 +1,5 @@ + +add_executable(kokkos_num_exe2 vec_perp.cpp) +target_compile_features(kokkos_num_exe2 PUBLIC cxx_std_20) +#target_compile_definitions(kokkos_num_exe2 PUBLIC KOKKOS_NUMS_ENABLE_TESTS) +target_link_libraries(kokkos_num_exe2 PRIVATE kokkos_num) diff --git a/src/vector/vec_perp.cpp b/src/vector/vec_perp.cpp new file mode 100644 index 0000000..aaba770 --- /dev/null +++ b/src/vector/vec_perp.cpp @@ -0,0 +1,13 @@ +#include "vec_perp.hpp" +#include + +int main(int argc, char* argv[]){ + Kokkos::initialize(argc, argv); + std::vector A = {1.0,2.0}; + std::vector B = {3.0,4.0}; + auto result = vector_approach::dot_product(A, B); + std::cout << "Dot product: " << result << std::endl; + + Kokkos::finalize(); + return 0; +} \ No newline at end of file diff --git a/src/vector/vec_perp.hpp b/src/vector/vec_perp.hpp new file mode 100644 index 0000000..a5cebc9 --- /dev/null +++ b/src/vector/vec_perp.hpp @@ -0,0 +1,42 @@ +#include +#include +#include + + +namespace vector_prep{ + template + using KokkosView = Kokkos::View; + template + using Vector = std::vector; + +} + +namespace vector_approach{ + template + ValueType dot_product(vector_prep::Vector const& A, vector_prep::Vector const& B){ + if(A.size() != B.size()) + { + throw std::invalid_argument("Vectors must be of the same size for dot product."); + } + const size_t size = A.size(); + vector_prep::KokkosView A_("A", size); + vector_prep::KokkosView B_("B", size); + vector_prep::KokkosView C_("C", size); + auto host_A = Kokkos::create_mirror_view(A_); + auto host_B = Kokkos::create_mirror_view(B_); + for (size_t i = 0; i < size; ++i) { + host_A(i) = A[i]; + host_B(i) = B[i]; + } + Kokkos::deep_copy(A_,host_A); + Kokkos::deep_copy(B_, host_B); + double sum = 0; + Kokkos::parallel_reduce(Kokkos::RangePolicy<>(0, size), KOKKOS_LAMBDA(int i, double& lsum){ + lsum += A_(i) * B_(i); + }, sum); + Kokkos::fence(); + + return sum; +} +} + diff --git a/tpls/googletest b/tpls/googletest new file mode 160000 index 0000000..f8d7d77 --- /dev/null +++ b/tpls/googletest @@ -0,0 +1 @@ +Subproject commit f8d7d77c06936315286eb55f8de22cd23c188571 diff --git a/tpls/kokkos b/tpls/kokkos new file mode 160000 index 0000000..d9397f9 --- /dev/null +++ b/tpls/kokkos @@ -0,0 +1 @@ +Subproject commit d9397f95a04a0334d327647e64ab7f1882ca0664