Use proper doxygen tags in modular simulator
[gromacs.git] / src / gromacs / utility / basenetwork.cpp
blob6fade03e577a04d6be1bea33987e1ed80e3b2847
1 /*
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) 2013,2014,2015,2016,2017 by the GROMACS development team.
7 * Copyright (c) 2018,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.
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 #include "gmxpre.h"
40 #include "basenetwork.h"
42 #include "config.h"
44 #include <climits>
45 #include <cstdio>
46 #include <cstdlib>
47 #include <cstring>
49 #include "gromacs/utility/cstringutil.h"
50 #include "gromacs/utility/fatalerror.h"
51 #include "gromacs/utility/gmxmpi.h"
53 bool gmx_mpi_initialized()
55 #if !GMX_MPI
56 return false;
57 #else
58 int n;
59 MPI_Initialized(&n);
61 return n != 0;
62 #endif
65 int gmx_node_num()
67 #if !GMX_MPI
68 return 1;
69 #else
70 # if GMX_THREAD_MPI
71 if (!gmx_mpi_initialized())
73 return 1;
75 # endif
76 int i;
77 (void)MPI_Comm_size(MPI_COMM_WORLD, &i);
78 return i;
79 #endif
82 int gmx_node_rank()
84 #if !GMX_MPI
85 return 0;
86 #else
87 # if GMX_THREAD_MPI
88 if (!gmx_mpi_initialized())
90 return 0;
92 # endif
93 int i;
94 (void)MPI_Comm_rank(MPI_COMM_WORLD, &i);
95 return i;
96 #endif
99 static int mpi_hostname_hash()
101 int hash_int;
103 #if GMX_LIB_MPI
104 int resultlen;
105 char mpi_hostname[MPI_MAX_PROCESSOR_NAME];
107 /* This procedure can only differentiate nodes with different names.
108 * Architectures where different physical nodes have identical names,
109 * such as IBM Blue Gene, should use an architecture specific solution.
111 MPI_Get_processor_name(mpi_hostname, &resultlen);
113 /* The string hash function returns an unsigned int. We cast to an int.
114 * Negative numbers are converted to positive by setting the sign bit to 0.
115 * This makes the hash one bit smaller.
116 * A 63-bit hash (with 64-bit int) should be enough for unique node hashes,
117 * even on a million node machine. 31 bits might not be enough though!
119 hash_int = static_cast<int>(gmx_string_fullhash_func(mpi_hostname, gmx_string_hash_init));
120 if (hash_int < 0)
122 hash_int -= INT_MIN;
124 #else
126 /* thread-MPI currently puts the thread number in the process name,
127 * we might want to change this, as this is inconsistent with what
128 * most MPI implementations would do when running on a single node.
130 hash_int = 0;
131 #endif
133 return hash_int;
136 int gmx_physicalnode_id_hash()
138 int hash = 0;
140 if (GMX_MPI)
142 hash = mpi_hostname_hash();
145 if (debug)
147 fprintf(debug, "In gmx_physicalnode_id_hash: hash %d\n", hash);
150 return hash;
153 void gmx_broadcast_world(int size, void* buffer)
155 #if GMX_MPI
156 MPI_Bcast(buffer, size, MPI_BYTE, 0, MPI_COMM_WORLD);
157 #else
158 GMX_UNUSED_VALUE(size);
159 GMX_UNUSED_VALUE(buffer);
160 #endif
163 #if GMX_LIB_MPI
164 void gmx_abort(int errorno)
166 MPI_Abort(MPI_COMM_WORLD, errorno);
167 std::abort();
169 #endif