File: TestHPX_IndependentInstances.cpp

package info (click to toggle)
kokkos 5.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,148 kB
  • sloc: cpp: 225,388; sh: 1,250; python: 78; makefile: 16; fortran: 4; ansic: 2
file content (133 lines) | stat: -rw-r--r-- 4,539 bytes parent folder | download
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
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright Contributors to the Kokkos project

#include <Kokkos_Macros.hpp>
#ifdef KOKKOS_ENABLE_EXPERIMENTAL_CXX20_MODULES
import kokkos.core;
#else
#include <Kokkos_Core.hpp>
#endif
#include <TestHPX_Category.hpp>

#include <hpx/config.hpp>
#include <hpx/future.hpp>

#ifndef HPX_COMPUTE_DEVICE_CODE

namespace {

struct FunctorInitConstant {
  Kokkos::View<int *, Kokkos::Experimental::HPX> a;
  int c;
  FunctorInitConstant(Kokkos::View<int *, Kokkos::Experimental::HPX> a_, int c_)
      : a(a_), c(c_) {}

  KOKKOS_INLINE_FUNCTION
  void operator()(const int i) const { a(i) = c; }
};

struct FunctorAdd {
  Kokkos::View<int *, Kokkos::Experimental::HPX> a;
  Kokkos::View<int *, Kokkos::Experimental::HPX> b;
  int c;
  FunctorAdd(Kokkos::View<int *, Kokkos::Experimental::HPX> a_,
             Kokkos::View<int *, Kokkos::Experimental::HPX> b_, int c_)
      : a(a_), b(b_), c(c_) {}

  KOKKOS_INLINE_FUNCTION
  void operator()(const int i) const { b(i) += a(i) + c; }
};

struct FunctorAddIndex {
  Kokkos::View<int *, Kokkos::Experimental::HPX> a;
  Kokkos::View<int *, Kokkos::Experimental::HPX> b;
  FunctorAddIndex(Kokkos::View<int *, Kokkos::Experimental::HPX> a_,
                  Kokkos::View<int *, Kokkos::Experimental::HPX> b_)
      : a(a_), b(b_) {}

  KOKKOS_INLINE_FUNCTION
  void operator()(const int i) const { b(i) += a(i) + i; }
};

struct FunctorPointwiseSum {
  Kokkos::View<int *, Kokkos::Experimental::HPX> a;
  Kokkos::View<int *, Kokkos::Experimental::HPX> b;
  Kokkos::View<int *, Kokkos::Experimental::HPX> c;
  FunctorPointwiseSum(Kokkos::View<int *, Kokkos::Experimental::HPX> a_,
                      Kokkos::View<int *, Kokkos::Experimental::HPX> b_,
                      Kokkos::View<int *, Kokkos::Experimental::HPX> c_)
      : a(a_), b(b_), c(c_) {}

  KOKKOS_INLINE_FUNCTION
  void operator()(const int i) const { c(i) = a(i) + b(i); }
};

struct FunctorReduce {
  Kokkos::View<int *, Kokkos::Experimental::HPX> a;
  FunctorReduce(Kokkos::View<int *, Kokkos::Experimental::HPX> a_) : a(a_) {}

  KOKKOS_INLINE_FUNCTION
  void operator()(const int i, int &lsum) const { lsum += a(i); }
};

TEST(hpx, independent_instances) {
  const int n = 100;
  const int c = 1;
  const int d = 3;

  Kokkos::View<int *, Kokkos::Experimental::HPX> v1("v1", n);
  Kokkos::View<int *, Kokkos::Experimental::HPX> v2("v2", n);
  Kokkos::View<int *, Kokkos::Experimental::HPX> v3("v3", n);
  Kokkos::View<int *, Kokkos::Experimental::HPX> v4("v4", n);
  Kokkos::View<int, Kokkos::Experimental::HPX> sum_v("sum_v");

  Kokkos::Experimental::HPX hpx1(
      Kokkos::Experimental::HPX::instance_mode::independent);
  Kokkos::parallel_for(
      "Test::hpx::independent_instances::init",
      Kokkos::Experimental::require(
          Kokkos::RangePolicy<Kokkos::Experimental::HPX>(hpx1, 0, n),
          Kokkos::Experimental::WorkItemProperty::HintLightWeight),
      FunctorInitConstant(v1, c));

  Kokkos::Experimental::HPX hpx2(hpx1.get_sender());
  Kokkos::parallel_for(
      "Test::hpx::independent_instances::add",
      Kokkos::Experimental::require(
          Kokkos::RangePolicy<Kokkos::Experimental::HPX>(hpx2, 0, n),
          Kokkos::Experimental::WorkItemProperty::HintLightWeight),
      FunctorAdd(v1, v2, d));

  Kokkos::Experimental::HPX hpx3(hpx1.get_sender());
  Kokkos::parallel_for(
      "Test::hpx::independent_instances::add_index",
      Kokkos::Experimental::require(
          Kokkos::RangePolicy<Kokkos::Experimental::HPX>(hpx3, 0, n),
          Kokkos::Experimental::WorkItemProperty::HintLightWeight),
      FunctorAddIndex(v1, v3));

  Kokkos::Experimental::HPX hpx4(hpx::execution::experimental::when_all(
      hpx2.get_sender(), hpx3.get_sender()));
  Kokkos::parallel_for(
      "Test::hpx::independent_instances::pointwise_sum",
      Kokkos::Experimental::require(
          Kokkos::RangePolicy<Kokkos::Experimental::HPX>(hpx4, 0, n),
          Kokkos::Experimental::WorkItemProperty::HintLightWeight),
      FunctorPointwiseSum(v2, v3, v4));

  Kokkos::parallel_reduce(
      "Test::hpx::independent_instances::reduce",
      Kokkos::Experimental::require(
          Kokkos::RangePolicy<Kokkos::Experimental::HPX>(hpx4, 0, n),
          Kokkos::Experimental::WorkItemProperty::HintLightWeight),
      FunctorReduce(v4), Kokkos::Sum<int>(sum_v));

  hpx4.fence();

  const int expected_sum = n * (2 * c + d) + (n * (n - 1) / 2);
  ASSERT_EQ(expected_sum, sum_v());
}

}  // namespace

#endif