Remove gmx custom fixed int (e.g. gmx_int64_t) types
[gromacs.git] / src / gromacs / mdlib / main.cpp
blob92f24fc71ca5fb54d532fb7ffe0575a6d7e28372
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,2018, 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.
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.
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.
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.
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.
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.
37 #include "gmxpre.h"
39 #include "main.h"
41 #include "config.h"
43 #include <cstdio>
44 #include <cstdlib>
45 #include <cstring>
47 #include <string>
49 #include "gromacs/commandline/filenm.h"
50 #include "gromacs/fileio/gmxfio.h"
51 #include "gromacs/gmxlib/network.h"
52 #include "gromacs/mdtypes/commrec.h"
53 #include "gromacs/utility/binaryinformation.h"
54 #include "gromacs/utility/cstringutil.h"
55 #include "gromacs/utility/exceptions.h"
56 #include "gromacs/utility/fatalerror.h"
57 #include "gromacs/utility/futil.h"
58 #include "gromacs/utility/gmxassert.h"
59 #include "gromacs/utility/gmxmpi.h"
60 #include "gromacs/utility/path.h"
61 #include "gromacs/utility/programcontext.h"
62 #include "gromacs/utility/smalloc.h"
63 #include "gromacs/utility/snprintf.h"
64 #include "gromacs/utility/stringutil.h"
65 #include "gromacs/utility/sysinfo.h"
67 /* The source code in this file should be thread-safe.
68 Please keep it that way. */
70 // TODO move this to multi-sim module
71 void check_multi_int(FILE *log, const gmx_multisim_t *ms, int val,
72 const char *name,
73 gmx_bool bQuiet)
75 int *ibuf, p;
76 gmx_bool bCompatible;
78 if (nullptr != log && !bQuiet)
80 fprintf(log, "Multi-checking %s ... ", name);
83 if (ms == nullptr)
85 gmx_fatal(FARGS,
86 "check_multi_int called with a NULL communication pointer");
89 snew(ibuf, ms->nsim);
90 ibuf[ms->sim] = val;
91 gmx_sumi_sim(ms->nsim, ibuf, ms);
93 bCompatible = TRUE;
94 for (p = 1; p < ms->nsim; p++)
96 bCompatible = bCompatible && (ibuf[p-1] == ibuf[p]);
99 if (bCompatible)
101 if (nullptr != log && !bQuiet)
103 fprintf(log, "OK\n");
106 else
108 if (nullptr != log)
110 fprintf(log, "\n%s is not equal for all subsystems\n", name);
111 for (p = 0; p < ms->nsim; p++)
113 fprintf(log, " subsystem %d: %d\n", p, ibuf[p]);
116 gmx_fatal(FARGS, "The %d subsystems are not compatible\n", ms->nsim);
119 sfree(ibuf);
122 // TODO move this to multi-sim module
123 void check_multi_int64(FILE *log, const gmx_multisim_t *ms,
124 int64_t val, const char *name,
125 gmx_bool bQuiet)
127 int64_t *ibuf;
128 int p;
129 gmx_bool bCompatible;
131 if (nullptr != log && !bQuiet)
133 fprintf(log, "Multi-checking %s ... ", name);
136 if (ms == nullptr)
138 gmx_fatal(FARGS,
139 "check_multi_int called with a NULL communication pointer");
142 snew(ibuf, ms->nsim);
143 ibuf[ms->sim] = val;
144 gmx_sumli_sim(ms->nsim, ibuf, ms);
146 bCompatible = TRUE;
147 for (p = 1; p < ms->nsim; p++)
149 bCompatible = bCompatible && (ibuf[p-1] == ibuf[p]);
152 if (bCompatible)
154 if (nullptr != log && !bQuiet)
156 fprintf(log, "OK\n");
159 else
161 // TODO Part of this error message would also be good to go to
162 // stderr (from one rank of one sim only)
163 if (nullptr != log)
165 fprintf(log, "\n%s is not equal for all subsystems\n", name);
166 for (p = 0; p < ms->nsim; p++)
168 char strbuf[255];
169 /* first make the format string */
170 snprintf(strbuf, 255, " subsystem %%d: %s\n",
171 "%" PRId64);
172 fprintf(log, strbuf, p, ibuf[p]);
175 gmx_fatal(FARGS, "The %d subsystems are not compatible\n", ms->nsim);
178 sfree(ibuf);
182 void gmx_log_open(const char *lognm, const t_commrec *cr,
183 gmx_bool bAppendFiles, FILE** fplog)
185 int pid;
186 char host[256];
187 char timebuf[STRLEN];
188 FILE *fp = *fplog;
190 if (!bAppendFiles)
192 fp = gmx_fio_fopen(lognm, bAppendFiles ? "a+" : "w+" );
195 gmx_fatal_set_log_file(fp);
197 /* Get some machine parameters */
198 gmx_gethostname(host, 256);
199 pid = gmx_getpid();
200 gmx_format_current_time(timebuf, STRLEN);
202 if (bAppendFiles)
204 fprintf(fp,
205 "\n"
206 "\n"
207 "-----------------------------------------------------------\n"
208 "Restarting from checkpoint, appending to previous log file.\n"
209 "\n"
213 fprintf(fp,
214 "Log file opened on %s"
215 "Host: %s pid: %d rank ID: %d number of ranks: %d\n",
216 timebuf, host, pid, cr->nodeid, cr->nnodes);
219 gmx::BinaryInformationSettings settings;
220 settings.extendedInfo(true);
221 settings.copyright(!bAppendFiles);
222 gmx::printBinaryInformation(fp, gmx::getProgramContext(), settings);
224 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
225 fprintf(fp, "\n");
227 fflush(fp);
229 *fplog = fp;
232 void gmx_log_close(FILE *fp)
234 if (fp)
236 gmx_fatal_set_log_file(nullptr);
237 gmx_fio_fclose(fp);
241 // TODO move this to multi-sim module
242 gmx_multisim_t *init_multisystem(MPI_Comm comm,
243 gmx::ArrayRef<const std::string> multidirs)
245 gmx_multisim_t *ms;
246 #if GMX_MPI
247 MPI_Group mpi_group_world;
248 int *rank;
249 #endif
251 if (multidirs.empty())
253 return nullptr;
256 if (!GMX_LIB_MPI && !multidirs.empty())
258 gmx_fatal(FARGS, "mdrun -multidir is only supported when GROMACS has been "
259 "configured with a proper external MPI library.");
262 if (multidirs.size() == 1)
264 /* NOTE: It would be nice if this special case worked, but this requires checks/tests. */
265 gmx_fatal(FARGS, "To run mdrun in multiple simulation mode, more then one "
266 "actual simulation is required. The single simulation case is not supported.");
269 #if GMX_MPI
270 int numRanks;
271 MPI_Comm_size(comm, &numRanks);
272 if (numRanks % multidirs.size() != 0)
274 gmx_fatal(FARGS, "The number of ranks (%d) is not a multiple of the number of simulations (%td)", numRanks, multidirs.size());
277 int numRanksPerSim = numRanks/multidirs.size();
278 int rankWithinComm;
279 MPI_Comm_rank(comm, &rankWithinComm);
281 if (debug)
283 fprintf(debug, "We have %td simulations, %d ranks per simulation, local simulation is %d\n", multidirs.size(), numRanksPerSim, rankWithinComm/numRanksPerSim);
286 ms = new gmx_multisim_t;
287 ms->nsim = multidirs.size();
288 ms->sim = rankWithinComm/numRanksPerSim;
289 /* Create a communicator for the master nodes */
290 snew(rank, ms->nsim);
291 for (int i = 0; i < ms->nsim; i++)
293 rank[i] = i*numRanksPerSim;
295 MPI_Comm_group(comm, &mpi_group_world);
296 MPI_Group_incl(mpi_group_world, ms->nsim, rank, &ms->mpi_group_masters);
297 sfree(rank);
298 MPI_Comm_create(MPI_COMM_WORLD, ms->mpi_group_masters,
299 &ms->mpi_comm_masters);
301 #if !MPI_IN_PLACE_EXISTS
302 /* initialize the MPI_IN_PLACE replacement buffers */
303 snew(ms->mpb, 1);
304 ms->mpb->ibuf = NULL;
305 ms->mpb->libuf = NULL;
306 ms->mpb->fbuf = NULL;
307 ms->mpb->dbuf = NULL;
308 ms->mpb->ibuf_alloc = 0;
309 ms->mpb->libuf_alloc = 0;
310 ms->mpb->fbuf_alloc = 0;
311 ms->mpb->dbuf_alloc = 0;
312 #endif
314 // TODO This should throw upon error
315 gmx_chdir(multidirs[ms->sim].c_str());
316 #else
317 GMX_UNUSED_VALUE(comm);
318 ms = nullptr;
319 #endif
321 return ms;
324 void done_multisim(gmx_multisim_t *ms)
326 if (nullptr != ms)
328 done_mpi_in_place_buf(ms->mpb);
329 delete ms;