1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright Contributors to the Kokkos project
#include <cstdio>
#include <sstream>
#include <iostream>
#include <thread>
#include <Kokkos_Macros.hpp>
#ifdef KOKKOS_ENABLE_EXPERIMENTAL_CXX20_MODULES
import kokkos.core;
#else
#include <Kokkos_Core.hpp>
#endif
#ifdef KOKKOS_ENABLE_OPENMP
#include <omp.h>
#endif
namespace Test {
namespace {
struct SumFunctor {
KOKKOS_INLINE_FUNCTION
void operator()(int i, int& lsum) const { lsum += i; }
};
template <class ExecSpace>
void check_space_member_for_policies(const ExecSpace& exec) {
Kokkos::RangePolicy<ExecSpace> range_policy(exec, 0, 1);
ASSERT_EQ(range_policy.space(), exec);
Kokkos::MDRangePolicy<ExecSpace, Kokkos::Rank<2>> mdrange_policy(exec, {0, 0},
{1, 1});
ASSERT_EQ(mdrange_policy.space(), exec);
Kokkos::TeamPolicy<ExecSpace> team_policy(exec, 1, Kokkos::AUTO);
ASSERT_EQ(team_policy.space(), exec);
}
template <class ExecSpace>
void check_distinctive([[maybe_unused]] ExecSpace exec1,
[[maybe_unused]] ExecSpace exec2) {
#ifdef KOKKOS_ENABLE_SERIAL
if constexpr (std::is_same_v<ExecSpace, Kokkos::Serial>) {
ASSERT_NE(exec1, exec2);
}
#endif
#ifdef KOKKOS_ENABLE_OPENMP
if constexpr (std::is_same_v<ExecSpace, Kokkos::OpenMP>) {
#if (!defined(KOKKOS_COMPILER_GNU) || KOKKOS_COMPILER_GNU >= 1110) && \
_OPENMP >= 201511
bool has_nested = omp_get_max_active_levels() > 1;
#else
bool has_nested = static_cast<bool>(omp_get_nested());
#endif
if (has_nested) {
ASSERT_NE(exec1, exec2);
// FIXME_OPENMP exec.concurrency() does not return thread pool size
// outside of parallel regions
if (ExecSpace().concurrency() >= 2)
ASSERT_EQ(
ExecSpace().impl_internal_space_instance()->thread_pool_size(),
exec1.impl_internal_space_instance()->thread_pool_size() +
exec2.impl_internal_space_instance()->thread_pool_size());
else {
ASSERT_EQ(exec1.impl_internal_space_instance()->thread_pool_size(), 1);
ASSERT_EQ(exec2.impl_internal_space_instance()->thread_pool_size(), 1);
}
} else {
ASSERT_EQ(exec1, exec2);
}
}
#endif
#ifdef KOKKOS_ENABLE_CUDA
if constexpr (std::is_same_v<ExecSpace, Kokkos::Cuda>) {
ASSERT_NE(exec1.cuda_stream(), exec2.cuda_stream());
ASSERT_EQ(exec1.cuda_device(), ExecSpace().cuda_device());
ASSERT_EQ(exec2.cuda_device(), ExecSpace().cuda_device());
}
#endif
#ifdef KOKKOS_ENABLE_HIP
if constexpr (std::is_same_v<ExecSpace, Kokkos::HIP>) {
ASSERT_NE(exec1.hip_stream(), exec2.hip_stream());
ASSERT_EQ(exec1.hip_device(), ExecSpace().hip_device());
ASSERT_EQ(exec2.hip_device(), ExecSpace().hip_device());
}
#endif
#ifdef KOKKOS_ENABLE_SYCL
if constexpr (std::is_same_v<ExecSpace, Kokkos::SYCL>) {
ASSERT_NE(exec1.sycl_queue(), exec2.sycl_queue());
}
#endif
#ifdef KOKKOS_ENABLE_HPX
if constexpr (std::is_same_v<ExecSpace, Kokkos::Experimental::HPX>) {
ASSERT_NE(exec1.impl_instance_id(), exec2.impl_instance_id());
}
#endif
#ifdef KOKKOS_ENABLE_OPENACC
if constexpr (std::is_same_v<ExecSpace, Kokkos::Experimental::OpenACC>) {
ASSERT_NE(exec1.acc_async_queue(), exec2.acc_async_queue());
}
#endif
}
} // namespace
#ifdef KOKKOS_ENABLE_OPENMP
template <class Lambda1, class Lambda2>
void run_threaded_test(const Lambda1 l1, const Lambda2 l2) {
if (omp_get_max_threads() < 2)
GTEST_SKIP() << "insufficient number of supported concurrent threads";
#pragma omp parallel num_threads(2)
{
if (omp_get_thread_num() == 0) l1();
if (omp_get_thread_num() == 1) l2();
}
}
// We cannot run the multithreaded test when threads or HPX is enabled because
// we cannot launch a thread from inside another thread
#elif !defined(KOKKOS_ENABLE_THREADS) && !defined(KOKKOS_ENABLE_HPX)
template <class Lambda1, class Lambda2>
void run_threaded_test(const Lambda1 l1, const Lambda2 l2) {
std::thread t1(std::move(l1));
std::thread t2(std::move(l2));
t1.join();
t2.join();
}
#else
template <class Lambda1, class Lambda2>
void run_threaded_test(const Lambda1 l1, const Lambda2 l2) {
l1();
l2();
}
#endif
void test_partitioning(TEST_EXECSPACE& instance0, TEST_EXECSPACE& instance1) {
check_distinctive(instance0, instance1);
check_space_member_for_policies(instance0);
check_space_member_for_policies(instance1);
#ifdef KOKKOS_ENABLE_OPENMP
if constexpr (std::is_same_v<TEST_EXECSPACE, Kokkos::OpenMP>) {
#if (!defined(KOKKOS_COMPILER_GNU) || KOKKOS_COMPILER_GNU >= 1110) && \
_OPENMP >= 201511
bool supports_nested = omp_get_max_active_levels() > 1;
#else
bool supports_nested = static_cast<bool>(omp_get_nested());
#endif
if (!supports_nested)
GTEST_SKIP()
<< "The OpenMP configuration doesn't allow nested parallelism";
}
#endif
int sum1, sum2;
int N = 3910;
run_threaded_test(
[&]() {
Kokkos::parallel_reduce(
Kokkos::RangePolicy<TEST_EXECSPACE>(instance0, 0, N), SumFunctor(),
sum1);
},
[&]() {
Kokkos::parallel_reduce(
Kokkos::RangePolicy<TEST_EXECSPACE>(instance1, 0, N), SumFunctor(),
sum2);
});
ASSERT_EQ(sum1, sum2);
ASSERT_EQ(sum1, N * (N - 1) / 2);
}
TEST(TEST_CATEGORY, partitioning_by_args) {
auto instances =
Kokkos::Experimental::partition_space(TEST_EXECSPACE(), 1, 1);
ASSERT_EQ(int(instances.size()), 2);
static_assert(
std::is_same_v<decltype(instances), std::array<TEST_EXECSPACE, 2>>);
test_partitioning(instances[0], instances[1]);
}
TEST(TEST_CATEGORY, partitioning_by_args_with_structured_bindings) {
auto [instance0, instance1] =
Kokkos::Experimental::partition_space(TEST_EXECSPACE(), 1, 1);
test_partitioning(instance0, instance1);
}
TEST(TEST_CATEGORY, partitioning_by_vector) {
// Make sure we can use a temporary as argument for weights
auto instances = Kokkos::Experimental::partition_space(
TEST_EXECSPACE(), std::vector<int> /*weights*/ {1, 1});
static_assert(
std::is_same_v<decltype(instances), std::vector<TEST_EXECSPACE>>);
ASSERT_EQ(int(instances.size()), 2);
test_partitioning(instances[0], instances[1]);
}
} // namespace Test
|