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
51 #include <type_traits>
52 #include <typeinfo>
53
54 namespace Test {
55
56 namespace {
57
58 template <typename ExecSpace>
59 struct TestViewCtorProp_EmbeddedDim {
60 using ViewIntType = typename Kokkos::View<int**, ExecSpace>;
61 using ViewDoubleType = typename Kokkos::View<double*, ExecSpace>;
62
63 // Cuda 7.0 has issues with using a lambda in parallel_for to initialize the
64 // view - replace with this functor
65 template <class ViewType>
66 struct Functor {
67 ViewType v;
68
FunctorTest::__anonb9ae7bb00111::TestViewCtorProp_EmbeddedDim::Functor69 Functor(const ViewType& v_) : v(v_) {}
70
71 KOKKOS_INLINE_FUNCTION
operator ()Test::__anonb9ae7bb00111::TestViewCtorProp_EmbeddedDim::Functor72 void operator()(const int i) const { v(i) = i; }
73 };
74
test_vcptTest::__anonb9ae7bb00111::TestViewCtorProp_EmbeddedDim75 static void test_vcpt(const int N0, const int N1) {
76 // Create views to test
77 {
78 using VIT = typename TestViewCtorProp_EmbeddedDim::ViewIntType;
79 using VDT = typename TestViewCtorProp_EmbeddedDim::ViewDoubleType;
80
81 VIT vi1("vi1", N0, N1);
82 VDT vd1("vd1", N0);
83
84 // TEST: Test for common type between two views, one with type double,
85 // other with type int Deduce common value_type and construct a view with
86 // that type
87 {
88 // Two views
89 auto view_alloc_arg = Kokkos::common_view_alloc_prop(vi1, vd1);
90 using CommonViewValueType =
91 typename decltype(view_alloc_arg)::value_type;
92 using CVT = typename Kokkos::View<CommonViewValueType*, ExecSpace>;
93 using HostCVT = typename CVT::HostMirror;
94
95 // Construct View using the common type; for case of specialization, an
96 // 'embedded_dim' would be stored by view_alloc_arg
97 CVT cv1(Kokkos::view_alloc("cv1", view_alloc_arg), N0 * N1);
98
99 Kokkos::parallel_for(Kokkos::RangePolicy<ExecSpace>(0, N0 * N1),
100 Functor<CVT>(cv1));
101
102 HostCVT hcv1 = Kokkos::create_mirror_view(cv1);
103 Kokkos::deep_copy(hcv1, cv1);
104
105 ASSERT_EQ((std::is_same<CommonViewValueType, double>::value), true);
106 ASSERT_EQ(
107 (std::is_same<typename decltype(view_alloc_arg)::scalar_array_type,
108 CommonViewValueType>::value),
109 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 } // end test_vcpt
151
152 }; // end struct
153
154 } // namespace
155
TEST(TEST_CATEGORY,viewctorprop_embedded_dim)156 TEST(TEST_CATEGORY, viewctorprop_embedded_dim) {
157 TestViewCtorProp_EmbeddedDim<TEST_EXECSPACE>::test_vcpt(2, 3);
158 }
159
TEST(TEST_CATEGORY,viewctorpop_view_allocate_without_initializing_backward_compatility)160 TEST(TEST_CATEGORY,
161 viewctorpop_view_allocate_without_initializing_backward_compatility) {
162 using deprecated_view_alloc = Kokkos::ViewAllocateWithoutInitializing;
163 Kokkos::View<int**, TEST_EXECSPACE> v(deprecated_view_alloc("v"), 5, 7);
164 }
165
166 } // namespace Test
167