Update instructions in containers.rst
[gromacs.git] / src / gromacs / mdspan / extensions.h
blob7a62dd61c8725f99dc9568a09061ae74bc66d514
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2019,2020, 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.
35 /*! \libinternal
36 * \file
37 * \brief GROMACS extensions to mdspan.
39 * \author Christian Blau <cblau@gwdg.de>
40 * \inlibraryapi
41 * \ingroup module_mdspan
44 #ifndef GMX_MDSPAN_EXTENSIONS_H_
45 #define GMX_MDSPAN_EXTENSIONS_H_
47 #include <algorithm>
48 #include <functional>
50 #include "gromacs/mdspan/mdspan.h"
52 namespace gmx
55 /*! \brief
56 * Free begin function addressing memory of a contiguously laid out basic_mdspan.
58 * \note Changing the elements that basic_mdspan views does not change
59 * the view itself, so a single begin that takes a const view suffices.
61 template<class BasicMdspan>
62 constexpr std::enable_if_t<BasicMdspan::is_always_contiguous(), typename BasicMdspan::pointer>
63 begin(const BasicMdspan& basicMdspan)
65 return basicMdspan.data();
68 /*! \brief
69 * Free end function addressing memory of a contiguously laid out basic_mdspan.
71 * \note Changing the elements that basic_mdspan views does not change
72 * the view itself, so a single end that takes a const view suffices.
74 template<class BasicMdspan>
75 constexpr std::enable_if_t<BasicMdspan::is_always_contiguous(), typename BasicMdspan::pointer>
76 end(const BasicMdspan& basicMdspan)
78 return basicMdspan.data() + basicMdspan.mapping().required_span_size();
81 //! Convenience type for often-used two dimensional extents
82 using dynamicExtents2D = extents<dynamic_extent, dynamic_extent>;
84 //! Convenience type for often-used three dimensional extents
85 using dynamicExtents3D = extents<dynamic_extent, dynamic_extent, dynamic_extent>;
87 //! Elementwise addition
88 template<class BasicMdspan>
89 constexpr BasicMdspan addElementwise(const BasicMdspan& span1, const BasicMdspan& span2)
91 BasicMdspan result(span1);
92 std::transform(begin(span1), end(span1), begin(span2), begin(result),
93 std::plus<typename BasicMdspan::element_type>());
94 return result;
97 //! Elementwise subtraction - left minus right
98 template<class BasicMdspan>
99 constexpr BasicMdspan subtractElementwise(const BasicMdspan& span1, const BasicMdspan& span2)
101 BasicMdspan result(span1);
102 std::transform(begin(span1), end(span1), begin(span2), begin(result),
103 std::minus<typename BasicMdspan::element_type>());
104 return result;
107 //! Elementwise multiplication
108 template<class BasicMdspan>
109 constexpr BasicMdspan multiplyElementwise(const BasicMdspan& span1, const BasicMdspan& span2)
111 BasicMdspan result(span1);
112 std::transform(begin(span1), end(span1), begin(span2), begin(result),
113 std::multiplies<typename BasicMdspan::element_type>());
114 return result;
117 //! Elementwise division - left / right
118 template<class BasicMdspan>
119 constexpr BasicMdspan divideElementwise(const BasicMdspan& span1, const BasicMdspan& span2)
121 BasicMdspan result(span1);
122 std::transform(begin(span1), end(span1), begin(span2), begin(result),
123 std::divides<typename BasicMdspan::element_type>());
124 return result;
127 } // namespace gmx
129 #endif // GMX_MDSPAN_EXTENSIONS_H_