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