1 /*
2 //@HEADER
3 // ************************************************************************
4 //
5 //                        Kokkos v. 3.0
6 //       Copyright (2020) National Technology & Engineering
7 //               Solutions of Sandia, LLC (NTESS).
8 //
9 // Under the terms of Contract DE-NA0003525 with NTESS,
10 // the U.S. Government retains certain rights in this software.
11 //
12 // Redistribution and use in source and binary forms, with or without
13 // modification, are permitted provided that the following conditions are
14 // met:
15 //
16 // 1. Redistributions of source code must retain the above copyright
17 // notice, this list of conditions and the following disclaimer.
18 //
19 // 2. Redistributions in binary form must reproduce the above copyright
20 // notice, this list of conditions and the following disclaimer in the
21 // documentation and/or other materials provided with the distribution.
22 //
23 // 3. Neither the name of the Corporation nor the names of the
24 // contributors may be used to endorse or promote products derived from
25 // this software without specific prior written permission.
26 //
27 // THIS SOFTWARE IS PROVIDED BY NTESS "AS IS" AND ANY
28 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NTESS OR THE
31 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 //
39 // Questions? Contact Christian R. Trott (crtrott@sandia.gov)
40 //
41 // ************************************************************************
42 //@HEADER
43 */
44 
45 /// @Kokkos_Feature_Level_Required:14
46 // Incremental test for MDRange reduction .
47 // Reduction is tested with scalar, view and a customized reduction.
48 
49 #include <Kokkos_Core.hpp>
50 #include <gtest/gtest.h>
51 
52 namespace Test {
53 using value_type = double;
54 const int N      = 10;
55 const int M      = 10;
56 
57 // A structure for complex number.
58 struct MyComplex {
59   value_type _re, _im;
60 
61   MyComplex() = default;
62 
63   KOKKOS_INLINE_FUNCTION
MyComplexTest::MyComplex64   MyComplex(value_type re, value_type im) : _re(re), _im(im) {}
65 
66   KOKKOS_INLINE_FUNCTION
MyComplexTest::MyComplex67   MyComplex(const MyComplex& src) : _re(src._re), _im(src._im) {}
68 
69   KOKKOS_INLINE_FUNCTION
operator +=Test::MyComplex70   void operator+=(const MyComplex& src) {
71     _re += src._re;
72     _im += src._im;
73   }
74 
75   KOKKOS_INLINE_FUNCTION
operator +=Test::MyComplex76   void operator+=(const volatile MyComplex& src) volatile {
77     _re += src._re;
78     _im += src._im;
79   }
80 };
81 
82 template <class ExecSpace>
83 struct TestMDRangeReduce {
84   // 1D  View of double
85   using View_1D = typename Kokkos::View<value_type*, ExecSpace>;
86 
87   // 2D  View of double
88   using View_2D = typename Kokkos::View<value_type**, ExecSpace>;
89 
90   // Index Type for the iterator
91   using int_index = Kokkos::IndexType<int>;
92 
93   // An MDRangePolicy for 2 nested loops
94   using MDPolicyType_2D = typename Kokkos::Experimental::MDRangePolicy<
95       ExecSpace, Kokkos::Experimental::Rank<2>, int_index>;
96 
97   //  1D - complex View
98   using Complex_View_1D = typename Kokkos::View<MyComplex*, ExecSpace>;
99 
100   // Reduction when ExecPolicy = MDRangePolicy and ReducerArgument =
101   // scalar/1-element view
reduce_MDRangeTest::TestMDRangeReduce102   void reduce_MDRange() {
103     View_2D d_data("d_data", N, M);
104 
105     MDPolicyType_2D mdPolicy_2D({0, 0}, {N, M});
106 
107     // Store the reduced value.
108     value_type d_result = 0.0, h_result = 0.0;
109     Kokkos::View<value_type, ExecSpace> d_resultView("result View");
110 
111     // Compute reference solution on the host.
112     for (int i = 0; i < N; ++i)
113       for (int j = 0; j < M; ++j) h_result += i * j;
114     h_result *= 0.5;
115 
116     // Fill data.
117     Kokkos::parallel_for(
118         mdPolicy_2D, KOKKOS_LAMBDA(const int i, const int j) {
119           d_data(i, j) = i * j * 0.5;
120         });
121 
122     // Parallel reduce on a scalar.
123     Kokkos::parallel_reduce(
124         mdPolicy_2D,
125         KOKKOS_LAMBDA(const int i, const int j, value_type& update_value) {
126           update_value += d_data(i, j);
127         },
128         d_result);
129 
130     // Parallel reduce on a view.
131     Kokkos::parallel_reduce(
132         mdPolicy_2D,
133         KOKKOS_LAMBDA(const int i, const int j, value_type& update_value) {
134           update_value += d_data(i, j);
135         },
136         d_resultView);
137 
138     // Check correctness.
139     ASSERT_EQ(h_result, d_result);
140 
141     // Copy view back to host.
142     value_type view_result = 0.0;
143     Kokkos::deep_copy(view_result, d_resultView);
144     ASSERT_EQ(h_result, view_result);
145   }
146 
147   // Custom Reduction
reduce_customTest::TestMDRangeReduce148   void reduce_custom() {
149     Complex_View_1D d_data("complex array", N);
150     MyComplex result(0.0, 0.0);
151     int sum = 0;
152 
153     // Fill data
154     Kokkos::parallel_for(
155         Kokkos::RangePolicy<ExecSpace>(0, N), KOKKOS_LAMBDA(const int i) {
156           d_data(i) = MyComplex(i * 0.5, -i * 0.5);
157         });
158 
159     // Reduction for complex number.
160     Kokkos::parallel_reduce(
161         Kokkos::RangePolicy<ExecSpace>(0, N),
162         KOKKOS_LAMBDA(const int i, MyComplex& update_value) {
163           update_value += d_data(i);
164         },
165         result);
166 
167     // Correctness Check
168     for (int i = 0; i < N; ++i) sum += i;
169 
170     ASSERT_EQ(result._re, sum * 0.5);
171     ASSERT_EQ(result._im, -sum * 0.5);
172   }
173 };
174 
175 // Reductions tests for MDRange policy and customized reduction.
TEST(TEST_CATEGORY,incr_14_MDrangeReduce)176 TEST(TEST_CATEGORY, incr_14_MDrangeReduce) {
177   TestMDRangeReduce<TEST_EXECSPACE> test;
178   test.reduce_MDRange();
179   test.reduce_custom();
180 }
181 
182 }  // namespace Test
183