SYCL: Avoid using no_init read accessor in rocFFT
[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.
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.
9  *
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.
14  *
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.
19  *
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.
24  *
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.
32  *
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.
35  */
36 /*! \internal \file
37  * \brief
38  * Implements functions from cmdlineinit.h.
39  *
40  * \author Teemu Murtola <teemu.murtola@gmail.com>
41  * \ingroup module_commandline
42  */
43 #include "gmxpre.h"
44
45 #include "cmdlineinit.h"
46
47 #include <cstring>
48
49 #include <memory>
50 #include <utility>
51
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"
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 // 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;
80
81 /*! \brief
82  * Broadcasts command-line arguments to all ranks.
83  *
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
86  * separately.
87  */
88 void broadcastArguments(int* argc, char*** argv)
89 {
90     if (gmx_node_num() <= 1)
91     {
92         return;
93     }
94     gmx_broadcast_world(sizeof(*argc), argc);
95
96     const bool isMaster = (gmx_node_rank() == 0);
97     if (!isMaster)
98     {
99         snew(*argv, *argc + 1);
100     }
101     for (int i = 0; i < *argc; i++)
102     {
103         int len;
104         if (isMaster)
105         {
106             len = std::strlen((*argv)[i]) + 1;
107         }
108         gmx_broadcast_world(sizeof(len), &len);
109         if (!isMaster)
110         {
111             snew((*argv)[i], len);
112         }
113         gmx_broadcast_world(len, (*argv)[i]);
114     }
115 }
116
117 //! \}
118
119 } // namespace
120
121 CommandLineProgramContext& initForCommandLine(int* argc, char*** argv)
122 {
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);
128     try
129     {
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());
135     }
136     catch (const std::exception& ex)
137     {
138         printFatalErrorMessage(stderr, ex);
139         std::exit(processExceptionAtExit(ex));
140     }
141     return *g_commandLineContext;
142 }
143
144 void finalizeForCommandLine()
145 {
146     gmx::finalize();
147     setLibraryFileFinder(nullptr);
148     g_libFileFinder.reset();
149     setProgramContext(nullptr);
150     g_commandLineContext.reset();
151 }
152
153 int processExceptionAtExitForCommandLine(const std::exception& ex)
154 {
155     int rc = processExceptionAtExit(ex); // Currently this aborts for real MPI
156     finalizeForCommandLine();            // thus this MPI_Finalize doesn't matter.
157     return rc;
158 }
159
160 int runCommandLineModule(int argc, char* argv[], ICommandLineModule* module)
161 {
162     return CommandLineModuleManager::runAsMainSingleModule(argc, argv, module);
163 }
164
165 int runCommandLineModule(int                                                         argc,
166                          char*                                                       argv[],
167                          const char*                                                 name,
168                          const char*                                                 description,
169                          std::function<std::unique_ptr<ICommandLineOptionsModule>()> factory)
170 {
171     return ICommandLineOptionsModule::runAsMain(argc, argv, name, description, std::move(factory));
172 }
173
174 } // namespace gmx
175
176 int gmx_run_cmain(int argc, char* argv[], int (*mainFunction)(int, char*[]))
177 {
178     return gmx::CommandLineModuleManager::runAsMainCMain(argc, argv, mainFunction);
179 }