Convert gmx_residuetype_t to a non-pointer
[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, 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 #include "basenetwork.h"
38
39 #include "config.h"
40
41 #include <cctype>
42 #include <cstdio>
43 #include <cstdlib>
44 #include <cstring>
45
46 #include <algorithm>
47 #include <exception>
48
49 #ifdef HAVE_UNISTD_H
50 #include <unistd.h>
51 #endif
52
53 #include "gromacs/utility/cstringutil.h"
54 #include "gromacs/utility/fatalerror.h"
55 #include "gromacs/utility/gmxmpi.h"
56 #include "gromacs/utility/programcontext.h"
57
58 int gmx_gethostname(char *name, size_t len)
59 {
60     if (len < 8)
61     {
62         gmx_incons("gmx_gethostname called with len<8");
63     }
64 #if defined(HAVE_UNISTD_H) && !defined(__native_client__)
65     if (gethostname(name, len-1) != 0)
66     {
67         std::strncpy(name, "unknown", 8);
68         return -1;
69     }
70     return 0;
71 #else
72     std::strncpy(name, "unknown", 8);
73     return -1;
74 #endif
75 }
76
77 gmx_bool gmx_mpi_initialized(void)
78 {
79 #ifndef GMX_MPI
80     return 0;
81 #else
82     int n;
83     MPI_Initialized(&n);
84
85     return n;
86 #endif
87 }
88
89 int gmx_node_num(void)
90 {
91 #ifndef GMX_MPI
92     return 1;
93 #else
94     int i;
95     (void) MPI_Comm_size(MPI_COMM_WORLD, &i);
96     return i;
97 #endif
98 }
99
100 int gmx_node_rank(void)
101 {
102 #ifndef GMX_MPI
103     return 0;
104 #else
105     int i;
106     (void) MPI_Comm_rank(MPI_COMM_WORLD, &i);
107     return i;
108 #endif
109 }
110
111 #if defined GMX_LIB_MPI && defined GMX_TARGET_BGQ
112 #include <spi/include/kernel/location.h>
113 #endif
114
115 int gmx_physicalnode_id_hash(void)
116 {
117     int hash_int;
118
119 #ifndef GMX_LIB_MPI
120     /* We have a single physical node */
121     hash_int = 0;
122 #else
123     int  resultlen;
124     char mpi_hostname[MPI_MAX_PROCESSOR_NAME];
125
126     /* This procedure can only differentiate nodes with different names.
127      * Architectures where different physical nodes have identical names,
128      * such as IBM Blue Gene, should use an architecture specific solution.
129      */
130     MPI_Get_processor_name(mpi_hostname, &resultlen);
131
132     /* The string hash function returns an unsigned int. We cast to an int.
133      * Negative numbers are converted to positive by setting the sign bit to 0.
134      * This makes the hash one bit smaller.
135      * A 63-bit hash (with 64-bit int) should be enough for unique node hashes,
136      * even on a million node machine. 31 bits might not be enough though!
137      */
138     hash_int =
139         (int)gmx_string_fullhash_func(mpi_hostname, gmx_string_hash_init);
140     if (hash_int < 0)
141     {
142         hash_int -= INT_MIN;
143     }
144 #endif
145
146     return hash_int;
147 }
148
149 int gmx_hostname_num()
150 {
151 #ifndef GMX_MPI
152     return 0;
153 #else
154 #ifdef GMX_THREAD_MPI
155     /* thread-MPI currently puts the thread number in the process name,
156      * we might want to change this, as this is inconsistent with what
157      * most MPI implementations would do when running on a single node.
158      */
159     return 0;
160 #else
161     int  resultlen, hostnum, i, j;
162     char mpi_hostname[MPI_MAX_PROCESSOR_NAME], hostnum_str[MPI_MAX_PROCESSOR_NAME];
163
164     MPI_Get_processor_name(mpi_hostname, &resultlen);
165 #ifdef GMX_TARGET_BGQ
166     Personality_t personality;
167     Kernel_GetPersonality(&personality, sizeof(personality));
168     /* Each MPI rank has a unique coordinate in a 6-dimensional space
169        (A,B,C,D,E,T), with dimensions A-E corresponding to different
170        physical nodes, and T within each node. Each node has sixteen
171        physical cores, each of which can have up to four hardware
172        threads, so 0 <= T <= 63 (but the maximum value of T depends on
173        the confituration of ranks and OpenMP threads per
174        node). However, T is irrelevant for computing a suitable return
175        value for gmx_hostname_num().
176      */
177     hostnum  = personality.Network_Config.Acoord;
178     hostnum *= personality.Network_Config.Bnodes;
179     hostnum += personality.Network_Config.Bcoord;
180     hostnum *= personality.Network_Config.Cnodes;
181     hostnum += personality.Network_Config.Ccoord;
182     hostnum *= personality.Network_Config.Dnodes;
183     hostnum += personality.Network_Config.Dcoord;
184     hostnum *= personality.Network_Config.Enodes;
185     hostnum += personality.Network_Config.Ecoord;
186 #else
187     /* This procedure can only differentiate nodes with host names
188      * that end on unique numbers.
189      */
190     i = 0;
191     j = 0;
192     /* Only parse the host name up to the first dot */
193     while (i < resultlen && mpi_hostname[i] != '.')
194     {
195         if (std::isdigit(mpi_hostname[i]))
196         {
197             hostnum_str[j++] = mpi_hostname[i];
198         }
199         i++;
200     }
201     hostnum_str[j] = '\0';
202     if (j == 0)
203     {
204         hostnum = 0;
205     }
206     else
207     {
208         /* Use only the last 9 decimals, so we don't overflow an int */
209         hostnum = std::strtol(hostnum_str + std::max(0, j-9), NULL, 10);
210     }
211 #endif
212
213     if (debug)
214     {
215         std::fprintf(debug, "In gmx_hostname_num: hostname '%s', hostnum %d\n",
216                      mpi_hostname, hostnum);
217 #ifdef GMX_TARGET_BGQ
218         std::fprintf(debug,
219                      "Torus ID A: %d / %d B: %d / %d C: %d / %d D: %d / %d E: %d / %d\n"
220                      "Node ID T: %d / %d core: %d / %d hardware thread: %d / %d\n",
221                      personality.Network_Config.Acoord,
222                      personality.Network_Config.Anodes,
223                      personality.Network_Config.Bcoord,
224                      personality.Network_Config.Bnodes,
225                      personality.Network_Config.Ccoord,
226                      personality.Network_Config.Cnodes,
227                      personality.Network_Config.Dcoord,
228                      personality.Network_Config.Dnodes,
229                      personality.Network_Config.Ecoord,
230                      personality.Network_Config.Enodes,
231                      Kernel_ProcessorCoreID(),
232                      16,
233                      Kernel_ProcessorID(),
234                      64,
235                      Kernel_ProcessorThreadID(),
236                      4);
237 #endif
238     }
239     return hostnum;
240 #endif
241 #endif
242 }
243
244 #ifdef GMX_LIB_MPI
245 void gmx_abort(int errorno)
246 {
247     const char *programName = "GROMACS";
248     try
249     {
250         programName = gmx::getProgramContext().displayName();
251     }
252     catch (const std::exception &)
253     {
254     }
255     const int nnodes   = gmx_node_num();
256     const int noderank = gmx_node_rank();
257     if (nnodes > 1)
258     {
259         std::fprintf(stderr, "Halting parallel program %s on node %d out of %d\n",
260                      programName, noderank, nnodes);
261     }
262     else
263     {
264         std::fprintf(stderr, "Halting program %s\n", programName);
265     }
266
267     MPI_Abort(MPI_COMM_WORLD, errorno);
268     std::exit(errorno);
269 }
270 #endif