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 <cstdio>
46 
47 #include <gtest/gtest.h>
48 
49 #include <Kokkos_Core.hpp>
50 #include <Kokkos_DynRankView.hpp>
51 
52 #include <type_traits>
53 #include <typeinfo>
54 
55 namespace Test {
56 
57 namespace {
58 
59 template <typename ExecSpace>
60 struct TestViewCtorProp_EmbeddedDim {
61   using ViewIntType    = typename Kokkos::View<int**, ExecSpace>;
62   using ViewDoubleType = typename Kokkos::View<double*, ExecSpace>;
63 
64   using DynRankViewIntType    = typename Kokkos::DynRankView<int, ExecSpace>;
65   using DynRankViewDoubleType = typename Kokkos::DynRankView<double, ExecSpace>;
66 
67   // Cuda 7.0 has issues with using a lambda in parallel_for to initialize the
68   // view - replace with this functor
69   template <class ViewType>
70   struct Functor {
71     ViewType v;
72 
FunctorTest::__anonfe501a9c0111::TestViewCtorProp_EmbeddedDim::Functor73     Functor(const ViewType& v_) : v(v_) {}
74 
75     KOKKOS_INLINE_FUNCTION
operator ()Test::__anonfe501a9c0111::TestViewCtorProp_EmbeddedDim::Functor76     void operator()(const int i) const { v(i) = i; }
77   };
78 
test_vcptTest::__anonfe501a9c0111::TestViewCtorProp_EmbeddedDim79   static void test_vcpt(const int N0, const int N1) {
80     // Create two views to test
81     {
82       using VIT = typename TestViewCtorProp_EmbeddedDim::ViewIntType;
83       using VDT = typename TestViewCtorProp_EmbeddedDim::ViewDoubleType;
84 
85       VIT vi1("vi1", N0, N1);
86       VDT vd1("vd1", N0);
87 
88       // TEST: Test for common type between two views, one with type double,
89       // other with type int Deduce common value_type and construct a view with
90       // that type
91       {
92         // Two views
93         auto view_alloc_arg = Kokkos::common_view_alloc_prop(vi1, vd1);
94         using CommonViewValueType =
95             typename decltype(view_alloc_arg)::value_type;
96         using CVT     = typename Kokkos::View<CommonViewValueType*, ExecSpace>;
97         using HostCVT = typename CVT::HostMirror;
98 
99         // Construct View using the common type; for case of specialization, an
100         // 'embedded_dim' would be stored by view_alloc_arg
101         CVT cv1(Kokkos::view_alloc("cv1", view_alloc_arg), N0 * N1);
102 
103         Kokkos::parallel_for(Kokkos::RangePolicy<ExecSpace>(0, N0 * N1),
104                              Functor<CVT>(cv1));
105 
106         HostCVT hcv1 = Kokkos::create_mirror_view(cv1);
107         Kokkos::deep_copy(hcv1, cv1);
108 
109         ASSERT_EQ((std::is_same<CommonViewValueType, double>::value), true);
110 #if 0
111       // debug output
112       for ( int i = 0; i < N0*N1; ++i ) {
113         printf(" Output check: hcv1(%d) = %lf\n ", i, hcv1(i) );
114       }
115 
116       printf( " Common value type view: %s \n", typeid( CVT() ).name() );
117       printf( " Common value type: %s \n", typeid( CommonViewValueType() ).name() );
118       if ( std::is_same< CommonViewValueType, double >::value == true ) {
119         printf("Proper common value_type\n");
120       }
121       else {
122         printf("WRONG common value_type\n");
123       }
124       // end debug output
125 #endif
126       }
127 
128       {
129         // Single view
130         auto view_alloc_arg = Kokkos::common_view_alloc_prop(vi1);
131         using CommonViewValueType =
132             typename decltype(view_alloc_arg)::value_type;
133         using CVT     = typename Kokkos::View<CommonViewValueType*, ExecSpace>;
134         using HostCVT = typename CVT::HostMirror;
135 
136         // Construct View using the common type; for case of specialization, an
137         // 'embedded_dim' would be stored by view_alloc_arg
138         CVT cv1(Kokkos::view_alloc("cv1", view_alloc_arg), N0 * N1);
139 
140         Kokkos::parallel_for(Kokkos::RangePolicy<ExecSpace>(0, N0 * N1),
141                              Functor<CVT>(cv1));
142 
143         HostCVT hcv1 = Kokkos::create_mirror_view(cv1);
144         Kokkos::deep_copy(hcv1, cv1);
145 
146         ASSERT_EQ((std::is_same<CommonViewValueType, int>::value), true);
147       }
148     }
149 
150     // Create two dynamic rank views to test
151     {
152       using VIT = typename TestViewCtorProp_EmbeddedDim::DynRankViewIntType;
153       using VDT = typename TestViewCtorProp_EmbeddedDim::DynRankViewDoubleType;
154 
155       VIT vi1("vi1", N0, N1);
156       VDT vd1("vd1", N0);
157 
158       // TEST: Test for common type between two views, one with type double,
159       // other with type int Deduce common value_type and construct a view with
160       // that type
161       {
162         // Two views
163         auto view_alloc_arg = Kokkos::common_view_alloc_prop(vi1, vd1);
164         using CommonViewValueType =
165             typename decltype(view_alloc_arg)::value_type;
166         using CVT     = typename Kokkos::View<CommonViewValueType*, ExecSpace>;
167         using HostCVT = typename CVT::HostMirror;
168 
169         // Construct View using the common type; for case of specialization, an
170         // 'embedded_dim' would be stored by view_alloc_arg
171         CVT cv1(Kokkos::view_alloc("cv1", view_alloc_arg), N0 * N1);
172 
173         Kokkos::parallel_for(Kokkos::RangePolicy<ExecSpace>(0, N0 * N1),
174                              Functor<CVT>(cv1));
175 
176         HostCVT hcv1 = Kokkos::create_mirror_view(cv1);
177         Kokkos::deep_copy(hcv1, cv1);
178 
179         ASSERT_EQ((std::is_same<CommonViewValueType, double>::value), true);
180       }
181 
182       {
183         // Single views
184         auto view_alloc_arg = Kokkos::common_view_alloc_prop(vi1);
185         using CommonViewValueType =
186             typename decltype(view_alloc_arg)::value_type;
187         using CVT     = typename Kokkos::View<CommonViewValueType*, ExecSpace>;
188         using HostCVT = typename CVT::HostMirror;
189 
190         // Construct View using the common type; for case of specialization, an
191         // 'embedded_dim' would be stored by view_alloc_arg
192         CVT cv1(Kokkos::view_alloc("cv1", view_alloc_arg), N0 * N1);
193 
194         Kokkos::parallel_for(Kokkos::RangePolicy<ExecSpace>(0, N0 * N1),
195                              Functor<CVT>(cv1));
196 
197         HostCVT hcv1 = Kokkos::create_mirror_view(cv1);
198         Kokkos::deep_copy(hcv1, cv1);
199 
200         ASSERT_EQ((std::is_same<CommonViewValueType, int>::value), true);
201       }
202     }
203 
204   }  // end test_vcpt
205 
206 };  // end struct
207 
208 }  // namespace
209 
TEST(TEST_CATEGORY,viewctorprop_embedded_dim)210 TEST(TEST_CATEGORY, viewctorprop_embedded_dim) {
211   TestViewCtorProp_EmbeddedDim<TEST_EXECSPACE>::test_vcpt(2, 3);
212 }
213 }  // namespace Test
214