a9561e08c201816e1e73d51fe779ddb3c3078873
[alexxy/gromacs.git] / src / gromacs / commandline / cmdlineinit.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*! \internal \file
36  * \brief
37  * Implements functions from cmdlineinit.h.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_commandline
41  */
42 #include "gmxpre.h"
43
44 #include "cmdlineinit.h"
45
46 #include <cstring>
47
48 #include <memory>
49 #include <utility>
50
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"
63
64 namespace gmx
65 {
66
67 namespace
68 {
69
70 //! \addtogroup module_commandline
71 //! \{
72
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;
78
79 /*! \brief
80  * Broadcasts command-line arguments to all ranks.
81  *
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
84  * separately.
85  */
86 void broadcastArguments(int *argc, char ***argv)
87 {
88     if (gmx_node_num() <= 1)
89     {
90         return;
91     }
92     gmx_broadcast_world(sizeof(*argc), argc);
93
94     const bool isMaster = (gmx_node_rank() == 0);
95     if (!isMaster)
96     {
97         snew(*argv, *argc+1);
98     }
99     for (int i = 0; i < *argc; i++)
100     {
101         int len;
102         if (isMaster)
103         {
104             len = std::strlen((*argv)[i])+1;
105         }
106         gmx_broadcast_world(sizeof(len), &len);
107         if (!isMaster)
108         {
109             snew((*argv)[i], len);
110         }
111         gmx_broadcast_world(len, (*argv)[i]);
112     }
113 }
114
115 //! \}
116
117 }   // namespace
118
119 CommandLineProgramContext &initForCommandLine(int *argc, char ***argv)
120 {
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);
127     try
128     {
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());
134     }
135     catch (const std::exception &ex)
136     {
137         printFatalErrorMessage(stderr, ex);
138         std::exit(processExceptionAtExit(ex));
139     }
140     return *g_commandLineContext;
141 }
142
143 void finalizeForCommandLine()
144 {
145     gmx::finalize();
146     setLibraryFileFinder(nullptr);
147     g_libFileFinder.reset();
148     setProgramContext(nullptr);
149     g_commandLineContext.reset();
150 }
151
152 int processExceptionAtExitForCommandLine(const std::exception &ex)
153 {
154     int rc = processExceptionAtExit(ex); // Currently this aborts for real MPI
155     finalizeForCommandLine();            // thus this MPI_Finalize doesn't matter.
156     return rc;
157 }
158
159 int runCommandLineModule(int argc, char *argv[],
160                          ICommandLineModule *module)
161 {
162     return CommandLineModuleManager::runAsMainSingleModule(argc, argv, module);
163 }
164
165 int runCommandLineModule(
166         int argc, char *argv[], const char *name, const char *description,
167         std::function<std::unique_ptr<ICommandLineOptionsModule>()> factory)
168 {
169     return ICommandLineOptionsModule::runAsMain(
170             argc, argv, name, description, std::move(factory));
171 }
172
173 } // namespace gmx
174
175 int gmx_run_cmain(int argc, char *argv[], int (*mainFunction)(int, char *[]))
176 {
177     return gmx::CommandLineModuleManager::runAsMainCMain(argc, argv, mainFunction);
178 }