Split lines with many copyright years
[alexxy/gromacs.git] / src / gromacs / utility / basenetwork.cpp
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) 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.
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 #include "gmxpre.h"
39
40 #include "basenetwork.h"
41
42 #include "config.h"
43
44 #include <climits>
45 #include <cstdio>
46 #include <cstdlib>
47 #include <cstring>
48
49 #include "gromacs/utility/cstringutil.h"
50 #include "gromacs/utility/fatalerror.h"
51 #include "gromacs/utility/gmxmpi.h"
52
53 bool gmx_mpi_initialized()
54 {
55 #if !GMX_MPI
56     return false;
57 #else
58     int n;
59     MPI_Initialized(&n);
60
61     return n != 0;
62 #endif
63 }
64
65 int gmx_node_num()
66 {
67 #if !GMX_MPI
68     return 1;
69 #else
70 #    if GMX_THREAD_MPI
71     if (!gmx_mpi_initialized())
72     {
73         return 1;
74     }
75 #    endif
76     int i;
77     (void)MPI_Comm_size(MPI_COMM_WORLD, &i);
78     return i;
79 #endif
80 }
81
82 int gmx_node_rank()
83 {
84 #if !GMX_MPI
85     return 0;
86 #else
87 #    if GMX_THREAD_MPI
88     if (!gmx_mpi_initialized())
89     {
90         return 0;
91     }
92 #    endif
93     int i;
94     (void)MPI_Comm_rank(MPI_COMM_WORLD, &i);
95     return i;
96 #endif
97 }
98
99 static int mpi_hostname_hash()
100 {
101     int hash_int;
102
103 #if GMX_LIB_MPI
104     int  resultlen;
105     char mpi_hostname[MPI_MAX_PROCESSOR_NAME];
106
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.
110      */
111     MPI_Get_processor_name(mpi_hostname, &resultlen);
112
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!
118      */
119     hash_int = static_cast<int>(gmx_string_fullhash_func(mpi_hostname, gmx_string_hash_init));
120     if (hash_int < 0)
121     {
122         hash_int -= INT_MIN;
123     }
124 #else
125
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.
129      */
130     hash_int = 0;
131 #endif
132
133     return hash_int;
134 }
135
136 int gmx_physicalnode_id_hash()
137 {
138     int hash = 0;
139
140     if (GMX_MPI)
141     {
142         hash = mpi_hostname_hash();
143     }
144
145     if (debug)
146     {
147         fprintf(debug, "In gmx_physicalnode_id_hash: hash %d\n", hash);
148     }
149
150     return hash;
151 }
152
153 void gmx_broadcast_world(int size, void* buffer)
154 {
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
161 }
162
163 #if GMX_LIB_MPI
164 void gmx_abort(int errorno)
165 {
166     MPI_Abort(MPI_COMM_WORLD, errorno);
167     std::abort();
168 }
169 #endif