Debian Codegraph @

packages/kokkos/core/unit_test/hpx/TestHPX_IndependentInstances.cpp

raw
1 //@HEADER
2 // ************************************************************************
3 //
4 // Kokkos v. 4.0
5 // Copyright (2022) National Technology & Engineering
6 // Solutions of Sandia, LLC (NTESS).
7 //
8 // Under the terms of Contract DE-NA0003525 with NTESS,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
12 // See https://kokkos.org/LICENSE for license information.
13 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
14 //
15 //@HEADER
16
17 #include <Kokkos_Core.hpp>
18 #include <TestHPX_Category.hpp>
19
20 #include <hpx/config.hpp>
21 #include <hpx/future.hpp>
22
23 #ifndef HPX_COMPUTE_DEVICE_CODE
24
25 namespace {
26
27 struct FunctorInitConstant {
28 Kokkos::View<int *, Kokkos::Experimental::HPX> a;
29 int c;
30 FunctorInitConstant(Kokkos::View<int *, Kokkos::Experimental::HPX> a_, int c_)
31 : a(a_), c(c_) {}
32
33 KOKKOS_INLINE_FUNCTION
34 void operator()(const int i) const { a(i) = c; }
35 };
36
37 struct FunctorAdd {
38 Kokkos::View<int *, Kokkos::Experimental::HPX> a;
39 Kokkos::View<int *, Kokkos::Experimental::HPX> b;
40 int c;
41 FunctorAdd(Kokkos::View<int *, Kokkos::Experimental::HPX> a_,
42 Kokkos::View<int *, Kokkos::Experimental::HPX> b_, int c_)
43 : a(a_), b(b_), c(c_) {}
44
45 KOKKOS_INLINE_FUNCTION
46 void operator()(const int i) const { b(i) += a(i) + c; }
47 };
48
49 struct FunctorAddIndex {
50 Kokkos::View<int *, Kokkos::Experimental::HPX> a;
51 Kokkos::View<int *, Kokkos::Experimental::HPX> b;
52 FunctorAddIndex(Kokkos::View<int *, Kokkos::Experimental::HPX> a_,
53 Kokkos::View<int *, Kokkos::Experimental::HPX> b_)
54 : a(a_), b(b_) {}
55
56 KOKKOS_INLINE_FUNCTION
57 void operator()(const int i) const { b(i) += a(i) + i; }
58 };
59
60 struct FunctorPointwiseSum {
61 Kokkos::View<int *, Kokkos::Experimental::HPX> a;
62 Kokkos::View<int *, Kokkos::Experimental::HPX> b;
63 Kokkos::View<int *, Kokkos::Experimental::HPX> c;
64 FunctorPointwiseSum(Kokkos::View<int *, Kokkos::Experimental::HPX> a_,
65 Kokkos::View<int *, Kokkos::Experimental::HPX> b_,
66 Kokkos::View<int *, Kokkos::Experimental::HPX> c_)
67 : a(a_), b(b_), c(c_) {}
68
69 KOKKOS_INLINE_FUNCTION
70 void operator()(const int i) const { c(i) = a(i) + b(i); }
71 };
72
73 struct FunctorReduce {
74 Kokkos::View<int *, Kokkos::Experimental::HPX> a;
75 FunctorReduce(Kokkos::View<int *, Kokkos::Experimental::HPX> a_) : a(a_) {}
76
77 KOKKOS_INLINE_FUNCTION
78 void operator()(const int i, int &lsum) const { lsum += a(i); }
79 };
80
81 TEST(hpx, independent_instances) {
82 const int n = 100;
83 const int c = 1;
84 const int d = 3;
85
86 Kokkos::View<int *, Kokkos::Experimental::HPX> v1("v1", n);
87 Kokkos::View<int *, Kokkos::Experimental::HPX> v2("v2", n);
88 Kokkos::View<int *, Kokkos::Experimental::HPX> v3("v3", n);
89 Kokkos::View<int *, Kokkos::Experimental::HPX> v4("v4", n);
90 Kokkos::View<int, Kokkos::Experimental::HPX> sum_v("sum_v");
91
92 Kokkos::Experimental::HPX hpx1(
93 Kokkos::Experimental::HPX::instance_mode::independent);
94 Kokkos::parallel_for(
95 "Test::hpx::independent_instances::init",
96 Kokkos::Experimental::require(
97 Kokkos::RangePolicy<Kokkos::Experimental::HPX>(hpx1, 0, n),
98 Kokkos::Experimental::WorkItemProperty::HintLightWeight),
99 FunctorInitConstant(v1, c));
100
101 Kokkos::Experimental::HPX hpx2(hpx1.get_sender());
102 Kokkos::parallel_for(
103 "Test::hpx::independent_instances::add",
104 Kokkos::Experimental::require(
105 Kokkos::RangePolicy<Kokkos::Experimental::HPX>(hpx2, 0, n),
106 Kokkos::Experimental::WorkItemProperty::HintLightWeight),
107 FunctorAdd(v1, v2, d));
108
109 Kokkos::Experimental::HPX hpx3(hpx1.get_sender());
110 Kokkos::parallel_for(
111 "Test::hpx::independent_instances::add_index",
112 Kokkos::Experimental::require(
113 Kokkos::RangePolicy<Kokkos::Experimental::HPX>(hpx3, 0, n),
114 Kokkos::Experimental::WorkItemProperty::HintLightWeight),
115 FunctorAddIndex(v1, v3));
116
117 Kokkos::Experimental::HPX hpx4(hpx::execution::experimental::when_all(
118 hpx2.get_sender(), hpx3.get_sender()));
119 Kokkos::parallel_for(
120 "Test::hpx::independent_instances::pointwise_sum",
121 Kokkos::Experimental::require(
122 Kokkos::RangePolicy<Kokkos::Experimental::HPX>(hpx4, 0, n),
123 Kokkos::Experimental::WorkItemProperty::HintLightWeight),
124 FunctorPointwiseSum(v2, v3, v4));
125
126 Kokkos::parallel_reduce(
127 "Test::hpx::independent_instances::reduce",
128 Kokkos::Experimental::require(
129 Kokkos::RangePolicy<Kokkos::Experimental::HPX>(hpx4, 0, n),
130 Kokkos::Experimental::WorkItemProperty::HintLightWeight),
131 FunctorReduce(v4), Kokkos::Sum<int>(sum_v));
132
133 hpx4.fence();
134
135 const int expected_sum = n * (2 * c + d) + (n * (n - 1) / 2);
136 ASSERT_EQ(expected_sum, sum_v());
137 }
138
139 } // namespace
140
141 #endif