2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2018,2019, 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.
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
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 /*! \libinternal \file
79 * \brief Declares gmx::extents for mdspan.
81 * \author Christian Trott <crtrott@sandia.gov>
82 * \author Ronan Keryell <ronan.keryell@xilinx.com>
83 * \author Carter Edwards <hedwards@nvidia.com>
84 * \author David Hollman <dshollm@sandia.gov>
85 * \author Christian Blau <cblau@gwdg.de>
88 #ifndef MDSPAN_EXTENTS_H
89 #define MDSPAN_EXTENTS_H
98 /*! \brief Define constant that signals dynamic extent.
100 enum : std::ptrdiff_t
105 template<std::ptrdiff_t... StaticExtents>
108 template<std::ptrdiff_t... LHS, std::ptrdiff_t... RHS>
109 constexpr bool operator==(const extents<LHS...>& lhs, const extents<RHS...>& rhs) noexcept;
111 template<std::ptrdiff_t... LHS, std::ptrdiff_t... RHS>
112 constexpr bool operator!=(const extents<LHS...>& lhs, const extents<RHS...>& rhs) noexcept;
117 template<int R, std::ptrdiff_t... StaticExtents>
118 struct extents_analyse;
120 /*! \libinternal \brief Enable querying extent of specific rank by splitting
121 * a static extents off the variadic template arguments.
124 template<int R, std::ptrdiff_t E0, std::ptrdiff_t... StaticExtents>
125 struct extents_analyse<R, E0, StaticExtents...>
128 //! The extent analysis of the next lower rank.
129 using next_extents_analyse = extents_analyse<R - 1, StaticExtents...>;
131 /*! \brief Accumulate the total rank from all extents.
132 * \returns incremented rank of the next extent
134 static constexpr std::size_t rank() noexcept { return next_extents_analyse::rank() + 1; }
135 /*! \brief Accumulate the dynamic rank from all extents.
136 * This extent is static, so hand down query to the next extent analysis.
137 * \returns the dynamic rank of the next extent analysis.
139 static constexpr std::size_t rank_dynamic() noexcept
141 return next_extents_analyse::rank_dynamic();
144 //! Store analysis of the next extent of next lower rank.
145 next_extents_analyse next;
147 //! Trivial constructor.
148 constexpr extents_analyse() : next() {}
150 /*! \brief Construction from dynamic extents hands the extents down
151 * to the next extents analysis of lower rank.
152 * \param[in] de dynamic extents
154 template<class... DynamicExtents>
155 constexpr extents_analyse(DynamicExtents... de) : next(de...)
159 /*! \brief Construct from an array of dynamic extentes and rank.
160 * Hand down the dynamic rank parameters to the next extents analysis rank
161 * \param[in] de dynamic extents
162 * \param[in] r rank to read from the dynamic extent
164 template<std::size_t Rank>
165 constexpr extents_analyse(const std::array<std::ptrdiff_t, Rank>& de, const std::size_t r) :
170 //! Copy constructor.
171 template<std::ptrdiff_t... OtherStaticExtents>
172 extents_analyse(extents_analyse<R, OtherStaticExtents...> rhs) : next(rhs.next)
176 //! Assignment operator.
177 template<std::ptrdiff_t... OtherStaticExtents>
178 extents_analyse& operator=(extents_analyse<R, OtherStaticExtents...> rhs)
184 /*! \brief Report extent of dimension r.
185 * \param[in] r the dimension to query
186 * \returns the extent in dimension r.
188 constexpr std::ptrdiff_t extent(const std::size_t r) const noexcept
190 return (r == R) ? E0 : next.extent(r);
192 /*! \brief Report the static extent of dimension r.
193 * \param[in] r the dimension to query
194 * \returns the static extent in dimension r.
196 static constexpr std::ptrdiff_t static_extent(const std::size_t r) noexcept
198 return (r == R) ? E0 : next_extents_analyse::static_extent(r);
201 //! Returns the extent with the first dimension sliced off
202 constexpr auto sliced_extents() const noexcept { return next; }
205 /*! \libinternal \brief Enable querying extent of specific rank by splitting
206 * a dynamic extent off the variadic template arguments.
208 template<int R, std::ptrdiff_t... StaticExtents>
209 struct extents_analyse<R, dynamic_extent, StaticExtents...>
211 //! The extent analysis of the next lower rank.
212 using next_extents_analyse = extents_analyse<R - 1, StaticExtents...>;
213 /*! \brief Accumulate the total rank from all extents.
214 * \returns incremented rank of the next extent
216 static constexpr std::size_t rank() noexcept { return next_extents_analyse::rank() + 1; }
217 /*! \brief Accumulate the dynamic rank from all extents.
218 * \returns the dynamic rank of the next extent analysis.
220 static constexpr std::size_t rank_dynamic() noexcept
222 return next_extents_analyse::rank_dynamic() + 1;
225 //! Store analysis of the next extent of next lower rank.
226 next_extents_analyse next;
227 //! The dynamic extent of this rank
228 std::ptrdiff_t this_extent;
230 //! Trivial constructor.
231 extents_analyse() : next(), this_extent(0) {}
233 /*! \brief Construction from dynamic extents hands the extents down
234 * to the next extents analysis of lower rank.
235 * \param[in] E the dynamic extent of this rank.
236 * \param[in] de dynamic extents
238 template<class... DynamicExtents>
239 extents_analyse(std::ptrdiff_t E, DynamicExtents... de) : next(de...), this_extent(E)
243 /*! \brief Construct from an array of dynamic extentes and rank.
244 * Hand down the dynamic rank parameters to the next extents analysis rank
245 * \param[in] de dynamic extents
246 * \param[in] r rank to read from the dynamic extent
248 template<std::size_t Rank>
249 extents_analyse(const std::array<std::ptrdiff_t, Rank>& de, const std::size_t r) :
255 //! Copy constructor.
256 template<std::ptrdiff_t... OtherStaticExtents>
257 extents_analyse(extents_analyse<R, OtherStaticExtents...> rhs) :
259 this_extent(rhs.extent(R))
263 //! Assignment operator.
264 template<std::ptrdiff_t... OtherStaticExtents>
265 extents_analyse& operator=(extents_analyse<R, OtherStaticExtents...> rhs)
268 this_extent = rhs.extent(R);
272 /*! \brief Report extent of dimension r.
273 * \param[in] r the dimension to query
274 * \returns the extent in dimension r.
276 constexpr std::ptrdiff_t extent(const std::size_t r) const noexcept
278 return (r == R) ? this_extent : next.extent(r);
280 /*! \brief Report the static extent of dimension r.
281 * \param[in] r the dimension to query
282 * \returns the static extent in dimension r.
284 static constexpr std::ptrdiff_t static_extent(const std::size_t r) noexcept
286 return (r == R) ? dynamic_extent : next_extents_analyse::static_extent(r);
289 //! Returns the extent with the first dimension sliced off
290 constexpr auto sliced_extents() const noexcept { return next; }
293 /*! \libinternal \brief Specialisation for rank 0 extents analysis.
294 * Ends recursive rank analysis.
297 struct extents_analyse<0>
299 /*! \brief Rank of extent of rank 0.
302 static constexpr std::size_t rank() noexcept { return 0; }
303 /*! \brief Dynamic rank of extent of rank 0.
306 static constexpr std::size_t rank_dynamic() noexcept { return 0; }
308 //! Trivial constructor.
309 constexpr extents_analyse() {}
311 //! Construct from array and rank, doing nothing.
312 template<std::size_t Rank>
313 extents_analyse(const std::array<std::ptrdiff_t, Rank>& /*de*/, const std::size_t /*r*/)
317 // extents_analyse & operator=(extents_analyse) = default;
319 /*! \brief Extent of rank 0 is 1, ensuring that product of extents yields required size and not zero.
320 * NOTE changed from ORNL reference implementation in making this static constexpr instead of constexpr .. const
322 static constexpr std::ptrdiff_t extent(const std::size_t /*r*/) noexcept { return 1; }
324 //! Static extent of rank 0 is 1, ensuring that product of extents yields required size and not zero.
325 static constexpr std::ptrdiff_t static_extent(const std::size_t /*r*/) noexcept { return 1; }
328 template<std::ptrdiff_t E0, std::ptrdiff_t... StaticExtents>
329 struct sliced_extents
331 using type = extents<StaticExtents...>;
333 } // namespace detail
335 /*! \libinternal \brief Multidimensional extents with static and dynamic dimensions.
337 * Describes a multidimensional index space of rank R.
338 * This is equivalent to the Cartesian product space of integer intervals
339 * [0, N_0) x [0, N_1) x ... x [0,N_{R-1} )
341 * Confer to P0009r8 of the Library Evolution Working Group and mdspan.extents
343 * \tparam StaticExtents rank number of extents, where the dynamic_extent
344 * constant for static extent is used to signal a dynamic extent.
346 template<std::ptrdiff_t... StaticExtents>
350 using extents_analyse_t = detail::extents_analyse<sizeof...(StaticExtents), StaticExtents...>;
353 //! Type used to index elements.
354 using index_type = std::ptrdiff_t;
355 //! Trivial constructor
356 constexpr extents() noexcept {}
358 constexpr extents(extents&&) noexcept = default;
359 //! Copy constructor.
360 constexpr extents(const extents&) noexcept = default;
361 /*! \brief Construct with dynamic extents.
363 * Allows for extents(u,v,w..) syntax when setting dynamic extents
365 * \tparam IndexType type of index
366 * \param[in] dn first dynamic index
367 * \param[in] DynamicExtents parameter pack
369 template<class... IndexType>
370 constexpr extents(std::ptrdiff_t dn, IndexType... DynamicExtents) noexcept :
371 impl(dn, DynamicExtents...)
373 static_assert(1 + sizeof...(DynamicExtents) == rank_dynamic(), "");
376 /*! \brief Construct from array of dynamic extents.
378 * Allows for extents({{u,v,w..}}) syntax when setting dynamic extents
380 * \param[in] dynamic_extents array of dynamic rank size containing extents
382 constexpr extents(const std::array<std::ptrdiff_t, extents_analyse_t::rank_dynamic()> dynamic_extents) noexcept :
383 impl(dynamic_extents, 0)
388 template<std::ptrdiff_t... OtherStaticExtents>
389 extents(const extents<OtherStaticExtents...>& other) : impl(other.impl)
393 //! Default move assignment
394 extents& operator=(extents&&) noexcept = default;
395 //! Default copy assignment
396 extents& operator=(const extents&) noexcept = default;
398 template<std::ptrdiff_t... OtherStaticExtents>
399 extents& operator=(const extents<OtherStaticExtents...>& other)
404 //! Default destructor
405 ~extents() = default;
407 // [mdspan.extents.obs]
408 /*! \brief The rank of the extent.
409 * \returns the rank all extents together
411 static constexpr std::size_t rank() noexcept { return sizeof...(StaticExtents); }
412 /*! \brief The rank of the dynamic extents.
413 * \returns Only the dynamic extents.
415 static constexpr std::size_t rank_dynamic() noexcept
417 return extents_analyse_t::rank_dynamic();
419 /*! \brief The rank of the static extents.
420 * \returns Only the static extents.
422 static constexpr index_type static_extent(std::size_t k) noexcept
424 return extents_analyse_t::static_extent(rank() - k);
426 /*! \brief The extent along a specific dimension.
427 * \param[in] k the dimension
428 * \returns the extent along that dimension
430 constexpr index_type extent(std::size_t k) const noexcept { return impl.extent(rank() - k); }
431 //! Returns the extent with the first dimension sliced off
432 constexpr auto sliced_extents() const noexcept
434 return typename detail::sliced_extents<StaticExtents...>::type(impl.sliced_extents());
438 extents(extents_analyse_t o) : impl(o) {}
439 //! For copy assignment, extents are friends of extents.
440 template<std::ptrdiff_t...>
441 friend class extents;
442 //! The implementation class.
443 extents_analyse_t impl;
447 /*! \brief Comparison operator.
448 * \returns true if extents are equal
450 template<std::ptrdiff_t... LHS, std::ptrdiff_t... RHS>
451 constexpr bool operator==(const extents<LHS...>& lhs, const extents<RHS...>& rhs) noexcept
453 bool equal = lhs.rank() == rhs.rank();
454 for (std::size_t r = 0; r < lhs.rank(); r++)
456 equal = equal && (lhs.extent(r) == rhs.extent(r));
461 /*! \brief Check for non-equality.
462 * \returns true if extents are unequal
464 template<std::ptrdiff_t... LHS, std::ptrdiff_t... RHS>
465 constexpr bool operator!=(const extents<LHS...>& lhs, const extents<RHS...>& rhs) noexcept
467 return !(lhs == rhs);
471 #endif /* end of include guard: MDSPAN_EXTENTS_H */