2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2013,2014,2015,2017,2018 by the GROMACS development team.
5 * Copyright (c) 2019,2020,2021, by the GROMACS development team, led by
6 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 * and including many others, as listed in the AUTHORS file in the
8 * top-level source directory and at http://www.gromacs.org.
10 * GROMACS is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
15 * GROMACS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GROMACS; if not, see
22 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * If you want to redistribute modifications to GROMACS, please
26 * consider that scientific software is very special. Version
27 * control is crucial - bugs must be traceable. We will be happy to
28 * consider code for inclusion in the official distribution, but
29 * derived work must not be called official GROMACS. Details are found
30 * in the README & COPYING files - if they are missing, get the
31 * official version at http://www.gromacs.org.
33 * To help us fund GROMACS development, we humbly ask that you cite
34 * the research papers on the package. Check out http://www.gromacs.org.
38 * Implements functions from cmdlineinit.h.
40 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_commandline
45 #include "cmdlineinit.h"
52 #include "gromacs/commandline/cmdlinemodulemanager.h"
53 #include "gromacs/commandline/cmdlineoptionsmodule.h"
54 #include "gromacs/commandline/cmdlineprogramcontext.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 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
76 std::unique_ptr<CommandLineProgramContext> g_commandLineContext;
77 //! Global library data file finder that respects GMXLIB.
78 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
79 std::unique_ptr<DataFileFinder> g_libFileFinder;
82 * Broadcasts command-line arguments to all ranks.
84 * MPI does not ensure that command-line arguments would be passed on any
85 * other rank than zero, but our code wants to parse them on each rank
88 void broadcastArguments(int* argc, char*** argv)
90 if (gmx_node_num() <= 1)
94 gmx_broadcast_world(sizeof(*argc), argc);
96 const bool isMaster = (gmx_node_rank() == 0);
99 snew(*argv, *argc + 1);
101 for (int i = 0; i < *argc; i++)
106 len = std::strlen((*argv)[i]) + 1;
108 gmx_broadcast_world(sizeof(len), &len);
111 snew((*argv)[i], len);
113 gmx_broadcast_world(len, (*argv)[i]);
121 CommandLineProgramContext& initForCommandLine(int* argc, char*** argv)
123 gmx::init(argc, argv);
124 GMX_RELEASE_ASSERT(!g_commandLineContext, "initForCommandLine() calls cannot be nested");
125 // TODO: Consider whether the argument broadcast would better be done
126 // in CommandLineModuleManager.
127 broadcastArguments(argc, argv);
130 g_commandLineContext = std::make_unique<CommandLineProgramContext>(*argc, *argv);
131 setProgramContext(g_commandLineContext.get());
132 g_libFileFinder = std::make_unique<DataFileFinder>();
133 g_libFileFinder->setSearchPathFromEnv("GMXLIB");
134 setLibraryFileFinder(g_libFileFinder.get());
136 catch (const std::exception& ex)
138 printFatalErrorMessage(stderr, ex);
139 std::exit(processExceptionAtExit(ex));
141 return *g_commandLineContext;
144 void finalizeForCommandLine()
147 setLibraryFileFinder(nullptr);
148 g_libFileFinder.reset();
149 setProgramContext(nullptr);
150 g_commandLineContext.reset();
153 int processExceptionAtExitForCommandLine(const std::exception& ex)
155 int rc = processExceptionAtExit(ex); // Currently this aborts for real MPI
156 finalizeForCommandLine(); // thus this MPI_Finalize doesn't matter.
160 int runCommandLineModule(int argc, char* argv[], ICommandLineModule* module)
162 return CommandLineModuleManager::runAsMainSingleModule(argc, argv, module);
165 int runCommandLineModule(int argc,
168 const char* description,
169 std::function<std::unique_ptr<ICommandLineOptionsModule>()> factory)
171 return ICommandLineOptionsModule::runAsMain(argc, argv, name, description, std::move(factory));
176 int gmx_run_cmain(int argc, char* argv[], int (*mainFunction)(int, char*[]))
178 return gmx::CommandLineModuleManager::runAsMainCMain(argc, argv, mainFunction);