2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2013,2014,2015,2017,2018, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 * Implements functions from cmdlineinit.h.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_commandline
44 #include "cmdlineinit.h"
51 #include "gromacs/commandline/cmdlinemodulemanager.h"
52 #include "gromacs/commandline/cmdlineoptionsmodule.h"
53 #include "gromacs/commandline/cmdlineprogramcontext.h"
54 #include "gromacs/compat/make_unique.h"
55 #include "gromacs/utility/basenetwork.h"
56 #include "gromacs/utility/datafilefinder.h"
57 #include "gromacs/utility/exceptions.h"
58 #include "gromacs/utility/futil.h"
59 #include "gromacs/utility/gmxassert.h"
60 #include "gromacs/utility/init.h"
61 #include "gromacs/utility/programcontext.h"
62 #include "gromacs/utility/smalloc.h"
70 //! \addtogroup module_commandline
73 // These never release ownership.
74 //! Global context instance initialized in initForCommandLine().
75 std::unique_ptr<CommandLineProgramContext> g_commandLineContext;
76 //! Global library data file finder that respects GMXLIB.
77 std::unique_ptr<DataFileFinder> g_libFileFinder;
80 * Broadcasts command-line arguments to all ranks.
82 * MPI does not ensure that command-line arguments would be passed on any
83 * other rank than zero, but our code wants to parse them on each rank
86 void broadcastArguments(int *argc, char ***argv)
88 if (gmx_node_num() <= 1)
92 gmx_broadcast_world(sizeof(*argc), argc);
94 const bool isMaster = (gmx_node_rank() == 0);
99 for (int i = 0; i < *argc; i++)
104 len = std::strlen((*argv)[i])+1;
106 gmx_broadcast_world(sizeof(len), &len);
109 snew((*argv)[i], len);
111 gmx_broadcast_world(len, (*argv)[i]);
119 CommandLineProgramContext &initForCommandLine(int *argc, char ***argv)
121 gmx::init(argc, argv);
122 GMX_RELEASE_ASSERT(!g_commandLineContext,
123 "initForCommandLine() calls cannot be nested");
124 // TODO: Consider whether the argument broadcast would better be done
125 // in CommandLineModuleManager.
126 broadcastArguments(argc, argv);
129 g_commandLineContext = compat::make_unique<CommandLineProgramContext>(*argc, *argv);
130 setProgramContext(g_commandLineContext.get());
131 g_libFileFinder = compat::make_unique<DataFileFinder>();
132 g_libFileFinder->setSearchPathFromEnv("GMXLIB");
133 setLibraryFileFinder(g_libFileFinder.get());
135 catch (const std::exception &ex)
137 printFatalErrorMessage(stderr, ex);
138 std::exit(processExceptionAtExit(ex));
140 return *g_commandLineContext;
143 void finalizeForCommandLine()
146 setLibraryFileFinder(nullptr);
147 g_libFileFinder.reset();
148 setProgramContext(nullptr);
149 g_commandLineContext.reset();
152 int processExceptionAtExitForCommandLine(const std::exception &ex)
154 int rc = processExceptionAtExit(ex); // Currently this aborts for real MPI
155 finalizeForCommandLine(); // thus this MPI_Finalize doesn't matter.
159 int runCommandLineModule(int argc, char *argv[],
160 ICommandLineModule *module)
162 return CommandLineModuleManager::runAsMainSingleModule(argc, argv, module);
165 int runCommandLineModule(
166 int argc, char *argv[], const char *name, const char *description,
167 std::function<std::unique_ptr<ICommandLineOptionsModule>()> factory)
169 return ICommandLineOptionsModule::runAsMain(
170 argc, argv, name, description, std::move(factory));
175 int gmx_run_cmain(int argc, char *argv[], int (*mainFunction)(int, char *[]))
177 return gmx::CommandLineModuleManager::runAsMainCMain(argc, argv, mainFunction);