Split lines with many copyright years
[alexxy/gromacs.git] / src / gromacs / domdec / ga2la.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2010,2014,2015,2017,2018 by the GROMACS development team.
7  * Copyright (c) 2019,2020, by the GROMACS development team, led by
8  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9  * and including many others, as listed in the AUTHORS file in the
10  * top-level source directory and at http://www.gromacs.org.
11  *
12  * GROMACS is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation; either version 2.1
15  * of the License, or (at your option) any later version.
16  *
17  * GROMACS is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with GROMACS; if not, see
24  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
26  *
27  * If you want to redistribute modifications to GROMACS, please
28  * consider that scientific software is very special. Version
29  * control is crucial - bugs must be traceable. We will be happy to
30  * consider code for inclusion in the official distribution, but
31  * derived work must not be called official GROMACS. Details are found
32  * in the README & COPYING files - if they are missing, get the
33  * official version at http://www.gromacs.org.
34  *
35  * To help us fund GROMACS development, we humbly ask that you cite
36  * the research papers on the package. Check out http://www.gromacs.org.
37  */
38 /*! \libinternal \file
39  * \brief
40  * Defines structures and functions for mapping from global to local atom
41  * indices. The functions are performance critical and should be inlined.
42  *
43  * \inlibraryapi
44  * \ingroup module_domdec
45  *
46  * \author Berk Hess <hess@kth.se>
47  *
48  */
49 #ifndef GMX_DOMDEC_GA2LA_H
50 #define GMX_DOMDEC_GA2LA_H
51
52 #include <vector>
53
54 #include "gromacs/domdec/hashedmap.h"
55 #include "gromacs/utility/gmxassert.h"
56
57 /*! \libinternal \brief Global to local atom mapping
58  *
59  * Used for efficient mapping from global atom indices to local atom indices
60  * in the domain decomposition.
61  */
62 class gmx_ga2la_t
63 {
64 public:
65     /*! \libinternal \brief Structure for the local atom info */
66     struct Entry
67     {
68         int la;   /**< The local atom index */
69         int cell; /**< The DD zone index for neighboring domains, zone+zone otherwise */
70     };
71
72     /*! \brief Constructor
73      *
74      * \param[in] numAtomsTotal  The total number of atoms in the system
75      * \param[in] numAtomsLocal  An estimate of the number of home+communicated atoms
76      */
77     gmx_ga2la_t(int numAtomsTotal, int numAtomsLocal);
78     ~gmx_ga2la_t() { usingDirect_ ? data_.direct.~vector() : data_.hashed.~HashedMap(); }
79
80     /*! \brief Inserts an entry, there should not already be an entry for \p a_gl
81      *
82      * \param[in]  a_gl   The global atom index
83      * \param[in]  value  The value to set for this index
84      */
85     void insert(int a_gl, const Entry& value)
86     {
87         GMX_ASSERT(a_gl >= 0, "Only global atom indices >= 0 are supported");
88         if (usingDirect_)
89         {
90             GMX_ASSERT(data_.direct[a_gl].cell == -1,
91                        "The key to be inserted should not be present");
92             data_.direct[a_gl] = value;
93         }
94         else
95         {
96             data_.hashed.insert(a_gl, value);
97         }
98     }
99
100     //! Delete the entry for global atom a_gl
101     void erase(int a_gl)
102     {
103         if (usingDirect_)
104         {
105             data_.direct[a_gl].cell = -1;
106         }
107         else
108         {
109             data_.hashed.erase(a_gl);
110         }
111     }
112
113     //! Returns a pointer to the entry when present, nullptr otherwise
114     const Entry* find(int a_gl) const
115     {
116         if (usingDirect_)
117         {
118             return (data_.direct[a_gl].cell == -1) ? nullptr : &(data_.direct[a_gl]);
119         }
120         else
121         {
122             return (data_.hashed.find(a_gl));
123         }
124     }
125
126     //! Returns the local atom index if it is a home atom, nullptr otherwise
127     const int* findHome(int a_gl) const
128     {
129         const Entry* const e = find(a_gl);
130         return (e && e->cell == 0) ? &(e->la) : nullptr;
131     }
132
133     /*! \brief Returns a reference to the entry for a_gl
134      *
135      * A non-release assert checks that a_gl is present.
136      */
137     Entry& at(int a_gl)
138     {
139         if (usingDirect_)
140         {
141             GMX_ASSERT(data_.direct[a_gl].cell >= 0, "a_gl should be present");
142             return data_.direct[a_gl];
143         }
144         else
145         {
146             Entry* search = data_.hashed.find(a_gl);
147             GMX_ASSERT(search, "a_gl should be present");
148             return *search;
149         }
150     }
151
152     //! Clear all the entries in the list.
153     void clear()
154     {
155         if (usingDirect_)
156         {
157             for (Entry& entry : data_.direct)
158             {
159                 entry.cell = -1;
160             }
161         }
162         else
163         {
164             data_.hashed.clear();
165         }
166     }
167
168 private:
169     union Data {
170         std::vector<Entry>    direct;
171         gmx::HashedMap<Entry> hashed;
172         // constructor and destructor function in parent class
173         Data() {}
174         ~Data() {}
175     } data_;
176     const bool usingDirect_;
177 };
178
179 #endif