2 * This file is part of the GROMACS molecular simulation package.
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,2021, 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.
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.
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.
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.
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.
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.
38 /*! \libinternal \file
40 * Defines structures and functions for mapping from global to local atom
41 * indices. The functions are performance critical and should be inlined.
44 * \ingroup module_domdec
46 * \author Berk Hess <hess@kth.se>
49 #ifndef GMX_DOMDEC_GA2LA_H
50 #define GMX_DOMDEC_GA2LA_H
54 #include "gromacs/domdec/hashedmap.h"
55 #include "gromacs/utility/gmxassert.h"
57 /*! \libinternal \brief Global to local atom mapping
59 * Used for efficient mapping from global atom indices to local atom indices
60 * in the domain decomposition.
65 /*! \libinternal \brief Structure for the local atom info */
68 int la; /**< The local atom index */
69 int cell; /**< The DD zone index for neighboring domains, zone+zone otherwise */
72 /*! \brief Constructor
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
77 gmx_ga2la_t(int numAtomsTotal, int numAtomsLocal);
78 ~gmx_ga2la_t() { usingDirect_ ? data_.direct.~vector() : data_.hashed.~HashedMap(); }
80 /*! \brief Inserts an entry, there should not already be an entry for \p a_gl
82 * \param[in] a_gl The global atom index
83 * \param[in] value The value to set for this index
85 void insert(int a_gl, const Entry& value)
87 GMX_ASSERT(a_gl >= 0, "Only global atom indices >= 0 are supported");
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;
96 data_.hashed.insert(a_gl, value);
100 //! Delete the entry for global atom a_gl
105 data_.direct[a_gl].cell = -1;
109 data_.hashed.erase(a_gl);
113 //! Returns a pointer to the entry when present, nullptr otherwise
114 const Entry* find(int a_gl) const
118 return (data_.direct[a_gl].cell == -1) ? nullptr : &(data_.direct[a_gl]);
122 return (data_.hashed.find(a_gl));
126 //! Returns the local atom index if it is a home atom, nullptr otherwise
127 const int* findHome(int a_gl) const
129 const Entry* const e = find(a_gl);
130 return (e && e->cell == 0) ? &(e->la) : nullptr;
133 /*! \brief Returns a reference to the entry for a_gl
135 * A non-release assert checks that a_gl is present.
141 GMX_ASSERT(data_.direct[a_gl].cell >= 0, "a_gl should be present");
142 return data_.direct[a_gl];
146 Entry* search = data_.hashed.find(a_gl);
147 GMX_ASSERT(search, "a_gl should be present");
152 /*! \brief Clear all the entries in the list.
154 * \param[in] resizeHashTable When true the hash table is optimized based on the current number of entries stored
156 void clear(const bool resizeHashTable)
160 for (Entry& entry : data_.direct)
165 else if (resizeHashTable)
167 data_.hashed.clearAndResizeHashTable();
171 data_.hashed.clear();
177 std::vector<Entry> direct;
178 gmx::HashedMap<Entry> hashed;
179 // constructor and destructor function in parent class
183 const bool usingDirect_;