Add replacements for pbc enumerations
[gromacs.git] / src / gromacs / mdspan / tests / mdspan.cpp
blobbb98daa7dc35326900ea85ebddcc44b01fd6350d
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2018, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
36 * This file is a modified version of original work of Sandia Corporation.
37 * In the spirit of the original code, this particular file can be distributed
38 * on the terms of Sandia Corporation.
41 * Kokkos v. 2.0
42 * Copyright (2014) Sandia Corporation
44 * Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
45 * the U.S. Government retains certain rights in this software.
47 * Kokkos is licensed under 3-clause BSD terms of use:
49 * Redistribution and use in source and binary forms, with or without
50 * modification, are permitted provided that the following conditions are
51 * met:
53 * 1. Redistributions of source code must retain the above copyright
54 * notice, this list of conditions and the following disclaimer.
56 * 2. Redistributions in binary form must reproduce the above copyright
57 * notice, this list of conditions and the following disclaimer in the
58 * documentation and/or other materials provided with the distribution.
60 * 3. Neither the name of the Corporation nor the names of the
61 * contributors may be used to endorse or promote products derived from
62 * this software without specific prior written permission.
64 * THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
65 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
66 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
67 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
68 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
69 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
70 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
71 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
72 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
73 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
74 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
76 * Questions? Contact Christian R. Trott (crtrott@sandia.gov)
78 /*! \internal \file
79 * \brief Testing gmx::basic_mdspan.
81 * \author Christian Trott <crtrott@sandia.gov>
82 * \author Carter Edwards <hedwards@nvidia.com>
83 * \author David Hollman <dshollm@sandia.gov>
84 * \author Christian Blau <cblau@gwdg.de>
86 #include "gmxpre.h"
88 #include "gromacs/mdspan/mdspan.h"
90 #include <cstdio>
92 #include <gtest/gtest.h>
94 namespace gmx
97 namespace
101 // Test basic_mdspan with a mixture of dynamic and static extents, as well as extent of one.
102 // The dynamic extents will be set to 4 and 2 repsectively so that we'll tests
103 // a multidimensional array of extent 5 x 4 x 3 x 2 x 1, i.e. 120 elements
105 //! View on int data with mixed static and dynamic extents
106 using mdspan_int = basic_mdspan < int, extents<5, dynamic_extent, 3, dynamic_extent, 1>, layout_right, accessor_basic < int>>;
108 TEST(MdSpanTest, MdSpanWrapsBasicMdSpanCorrectly)
110 // Check that mdspan wraps basic_mdspan as expected
111 ::testing::StaticAssertTypeEq < mdspan_int, mdspan < int, 5, dynamic_extent, 3, dynamic_extent, 1>>();
114 //! View on float data with mixed static and dynamic extents
115 using mdspan_float = basic_mdspan < float, extents<5, dynamic_extent, 3, dynamic_extent, 1>, layout_right, accessor_basic < float>>;
116 //! Types to be tested
117 using MdSpanTypes = ::testing::Types < mdspan_int, mdspan_float>;
119 template<class ElementType, class Mapping>
120 struct fill_raw_data {
121 static void fill(ElementType* p, Mapping m)
123 typename Mapping::extents_type e = m.extents();
124 for (ptrdiff_t i0 = 0; i0 < e.extent(0); i0++)
126 for (ptrdiff_t i1 = 0; i1 < e.extent(1); i1++)
128 for (ptrdiff_t i2 = 0; i2 < e.extent(2); i2++)
130 for (ptrdiff_t i3 = 0; i3 < e.extent(3); i3++)
132 for (ptrdiff_t i4 = 0; i4 < e.extent(4); i4++)
134 p[i0*m.stride(0)+i1*m.stride(1)+i2*m.stride(2)+i3*m.stride(3)+i4*m.stride(4)] =
135 ElementType(i0*10000+i1*1000+i2*100+i3*10+i4);
143 template<class MDSPAN>
144 struct MdSpanTest : public ::testing::Test
146 using mdspan_type = MDSPAN;
147 using element_type = typename mdspan_type::element_type;
148 using extents_type = typename mdspan_type::extents_type;
149 using mapping_type = typename mdspan_type::mapping_type;
150 using accessor_type = typename mdspan_type::accessor_type;
151 using pointer_type = typename mdspan_type::pointer;
153 mdspan_type my_mdspan_extents;
154 mdspan_type my_mdspan_array;
155 mdspan_type my_mdspan_mapping;
156 mdspan_type my_mdspan_map_acc;
157 mdspan_type my_mdspan_copy;
159 std::vector<element_type> rawData;
161 template<class ... ED>
162 void SetUp(ED ... e)
164 mapping_type map {extents_type(e ...)};
165 accessor_type acc;
166 rawData.resize(map.required_span_size());
167 fill_raw_data<element_type, mapping_type>::fill(rawData.data(), map);
168 pointer_type p(rawData.data());
170 my_mdspan_array = mdspan_type(p, std::array<ptrdiff_t, sizeof ... (ED)>({{e ...}}));
171 my_mdspan_mapping = mdspan_type(p, map);
172 my_mdspan_map_acc = mdspan_type(p, map, acc);
173 my_mdspan_extents = mdspan_type(p, e ...);
174 my_mdspan_copy = my_mdspan_mapping;
177 void check_rank(ptrdiff_t r)
179 EXPECT_EQ(my_mdspan_mapping.rank(), r);
180 EXPECT_EQ(my_mdspan_map_acc.rank(), r);
181 EXPECT_EQ(my_mdspan_extents.rank(), r);
182 EXPECT_EQ(my_mdspan_copy.rank(), r);
184 void check_rank_dynamic(ptrdiff_t r)
186 EXPECT_EQ(my_mdspan_mapping.rank_dynamic(), r);
187 EXPECT_EQ(my_mdspan_map_acc.rank_dynamic(), r);
188 EXPECT_EQ(my_mdspan_extents.rank_dynamic(), r);
189 EXPECT_EQ(my_mdspan_copy.rank_dynamic(), r);
191 template<class ... E>
192 void check_extents(E ... e)
194 std::array<ptrdiff_t, extents_type::rank()> a {{e ...}};
195 for (size_t r = 0; r < extents_type::rank(); r++)
197 EXPECT_EQ(my_mdspan_mapping.extent(r), a[r]);
198 EXPECT_EQ(my_mdspan_map_acc.extent(r), a[r]);
199 EXPECT_EQ(my_mdspan_extents.extent(r), a[r]);
200 EXPECT_EQ(my_mdspan_copy.extent(r), a[r]);
203 template<class ... E>
204 void check_strides(E ... e)
206 std::array<ptrdiff_t, extents_type::rank()> a {{e ...}};
207 for (size_t r = 0; r < extents_type::rank(); r++)
209 EXPECT_EQ(my_mdspan_mapping.stride(r), a[r]);
210 EXPECT_EQ(my_mdspan_map_acc.stride(r), a[r]);
211 EXPECT_EQ(my_mdspan_extents.stride(r), a[r]);
212 EXPECT_EQ(my_mdspan_copy.stride(r), a[r]);
216 void check_properties_internal(mdspan_type my_mdspan, bool always_unique, bool always_contiguous, bool always_strided,
217 bool unique, bool contiguous, bool strided)
219 EXPECT_EQ(my_mdspan.is_always_unique(), always_unique);
220 EXPECT_EQ(my_mdspan.is_always_contiguous(), always_contiguous);
221 EXPECT_EQ(my_mdspan.is_always_strided(), always_strided);
222 EXPECT_EQ(my_mdspan.is_unique(), unique);
223 EXPECT_EQ(my_mdspan.is_contiguous(), contiguous);
224 EXPECT_EQ(my_mdspan.is_strided(), strided);
227 void check_properties(bool always_unique, bool always_contiguous, bool always_strided,
228 bool unique, bool contiguous, bool strided)
230 check_properties_internal(my_mdspan_mapping, always_unique, always_contiguous, always_strided, unique, contiguous, strided);
231 check_properties_internal(my_mdspan_map_acc, always_unique, always_contiguous, always_strided, unique, contiguous, strided);
232 check_properties_internal(my_mdspan_extents, always_unique, always_contiguous, always_strided, unique, contiguous, strided);
233 check_properties_internal(my_mdspan_copy, always_unique, always_contiguous, always_strided, unique, contiguous, strided);
236 void check_operator()
238 extents_type e = my_mdspan_mapping.extents();
239 for (ptrdiff_t i0 = 0; i0 < e.extent(0); i0++)
241 for (ptrdiff_t i1 = 0; i1 < e.extent(1); i1++)
243 for (ptrdiff_t i2 = 0; i2 < e.extent(2); i2++)
245 for (ptrdiff_t i3 = 0; i3 < e.extent(3); i3++)
247 for (ptrdiff_t i4 = 0; i4 < e.extent(4); i4++)
249 element_type value = i0*10000+i1*1000+i2*100+i3*10+i4;
250 EXPECT_EQ(my_mdspan_mapping(i0, i1, i2, i3, i4), value);
251 EXPECT_EQ(my_mdspan_map_acc(i0, i1, i2, i3, i4), value);
252 EXPECT_EQ(my_mdspan_extents(i0, i1, i2, i3, i4), value);
253 EXPECT_EQ(my_mdspan_copy (i0, i1, i2, i3, i4), value);
262 TYPED_TEST_CASE(MdSpanTest, MdSpanTypes);
264 TYPED_TEST(MdSpanTest, Rank) {
265 this->SetUp(4, 2);
266 this->check_rank(5);
269 TYPED_TEST(MdSpanTest, DynamicRank) {
270 this->SetUp(4, 2);
271 this->check_rank_dynamic(2);
274 TYPED_TEST(MdSpanTest, Extents) {
275 this->SetUp(4, 2);
276 this->check_extents(5, 4, 3, 2, 1);
279 TYPED_TEST(MdSpanTest, Strides) {
280 this->SetUp(4, 2);
281 this->check_strides(24, 6, 2, 1, 1);
284 TYPED_TEST(MdSpanTest, Properties) {
285 this->SetUp(4, 2);
286 const bool always_unique = true;
287 const bool always_contiguous = true;
288 const bool always_strided = true;
289 const bool unique = true;
290 const bool contiguous = true;
291 const bool strided = true;
292 this->check_properties(always_unique, always_contiguous, always_strided, unique, contiguous, strided);
295 TYPED_TEST(MdSpanTest, Operator) {
296 this->SetUp(4, 2);
297 this->check_operator();
300 } // namespace
302 } // namespace gmx