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 #include <Kokkos_Core.hpp>
46 #include <gtest/gtest.h>
47 
48 /// @Kokkos_Feature_Level_Required:16
49 // Incremental test for parallel_scan.
50 // perform scan on a 1D view of double's and check for correctness.
51 
52 namespace Test {
53 
54 using value_type = double;
55 const int N      = 10;
56 
57 template <class ExecSpace>
58 struct TestScan {
59   // 1D  View of double
60   using View_1D = typename Kokkos::View<value_type *, ExecSpace>;
61 
parallel_scanTest::TestScan62   void parallel_scan() {
63     View_1D d_data("data", N);
64 
65     // Initialize data.
66     Kokkos::parallel_for(
67         Kokkos::RangePolicy<ExecSpace>(0, N),
68         KOKKOS_LAMBDA(const int i) { d_data(i) = i * 0.5; });
69 
70     // Exclusive parallel_scan call.
71     Kokkos::parallel_scan(
72         Kokkos::RangePolicy<ExecSpace>(0, N),
73         KOKKOS_LAMBDA(const int i, value_type &update_value, const bool final) {
74           const value_type val_i = d_data(i);
75           if (final) d_data(i) = update_value;
76 
77           update_value += val_i;
78         });
79 
80     // Copy back the data.
81     auto h_data =
82         Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), d_data);
83 
84     // Check Correctness
85     ASSERT_EQ(h_data(0), 0.0);
86     value_type upd = h_data(0);
87     for (int i = 1; i < N; ++i) {
88       upd += (i - 1) * 0.5;
89       ASSERT_EQ(h_data(i), upd);
90     }
91   }
92 };
93 
TEST(TEST_CATEGORY,IncrTest_16_parallelscan)94 TEST(TEST_CATEGORY, IncrTest_16_parallelscan) {
95   TestScan<TEST_EXECSPACE> test;
96   test.parallel_scan();
97 }
98 
99 }  // namespace Test
100