c78f448792c1d8c54a66069444947cce51035f7f
[alexxy/gromacs.git] / src / gromacs / mdrun / legacymdrunoptions.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2011-2019,2020,2021, 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.
10  *
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.
15  *
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.
20  *
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.
25  *
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.
33  *
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.
36  */
37 /*! \internal \file
38  *
39  * \brief This file declares helper functionality for legacy option handling for mdrun
40  *
41  * \author Berk Hess <hess@kth.se>
42  * \author David van der Spoel <david.vanderspoel@icm.uu.se>
43  * \author Erik Lindahl <erik@kth.se>
44  * \author Mark Abraham <mark.j.abraham@gmail.com>
45  *
46  * \ingroup module_mdrun
47  */
48 #include "gmxpre.h"
49
50 #include "legacymdrunoptions.h"
51
52 #include <cstring>
53
54 #include "gromacs/math/functions.h"
55 #include "gromacs/utility/arrayref.h"
56 #include "gromacs/utility/arraysize.h"
57 #include "gromacs/utility/fatalerror.h"
58
59 namespace gmx
60 {
61
62 /*! \brief Return whether the command-line parameter that
63  *  will trigger a multi-simulation is set */
64 static bool is_multisim_option_set(int argc, const char* const argv[])
65 {
66     for (int i = 0; i < argc; ++i)
67     {
68         if (strcmp(argv[i], "-multidir") == 0)
69         {
70             return true;
71         }
72     }
73     return false;
74 }
75
76 int LegacyMdrunOptions::updateFromCommandLine(int argc, char** argv, ArrayRef<const char*> desc)
77 {
78     unsigned long PCA_Flags = PCA_CAN_SET_DEFFNM;
79     // With -multidir, the working directory still needs to be
80     // changed, so we can't check for the existence of files during
81     // parsing.  It isn't useful to do any completion based on file
82     // system contents, either.
83     if (is_multisim_option_set(argc, argv))
84     {
85         PCA_Flags |= PCA_DISABLE_INPUT_FILE_CHECKING;
86     }
87
88     if (!parse_common_args(&argc,
89                            argv,
90                            PCA_Flags,
91                            ssize(filenames),
92                            filenames.data(),
93                            asize(pa),
94                            pa,
95                            ssize(desc),
96                            desc.data(),
97                            0,
98                            nullptr,
99                            &oenv))
100     {
101         return 0;
102     }
103
104     // Handle the options that permits the user to either declare
105     // which compatible GPUs are availble for use, or to select a GPU
106     // task assignment. Either could be in an environment variable (so
107     // that there is a way to customize it, when using MPI in
108     // heterogeneous contexts).
109     {
110         // TODO Argument parsing can't handle std::string. We should
111         // fix that by changing the parsing, once more of the roles of
112         // handling, validating and implementing defaults for user
113         // command-line options have been seperated.
114         hw_opt.devicesSelectedByUser = devicesSelectedByUser;
115         hw_opt.userGpuTaskAssignment = userGpuTaskAssignment;
116
117         const char* env = getenv("GMX_GPU_ID");
118         if (env != nullptr)
119         {
120             if (!hw_opt.devicesSelectedByUser.empty())
121             {
122                 gmx_fatal(FARGS, "GMX_GPU_ID and -gpu_id can not be used at the same time");
123             }
124             hw_opt.devicesSelectedByUser = env;
125         }
126
127         env = getenv("GMX_GPUTASKS");
128         if (env != nullptr)
129         {
130             if (!hw_opt.userGpuTaskAssignment.empty())
131             {
132                 gmx_fatal(FARGS, "GMX_GPUTASKS and -gputasks can not be used at the same time");
133             }
134             hw_opt.userGpuTaskAssignment = env;
135         }
136
137         if (!hw_opt.devicesSelectedByUser.empty() && !hw_opt.userGpuTaskAssignment.empty())
138         {
139             gmx_fatal(FARGS, "-gpu_id and -gputasks cannot be used at the same time");
140         }
141     }
142
143     hw_opt.threadAffinity = static_cast<ThreadAffinity>(nenum(thread_aff_opt_choices));
144
145     if (!opt2parg_bSet("-append", asize(pa), pa))
146     {
147         mdrunOptions.appendingBehavior = AppendingBehavior::Auto;
148     }
149     else
150     {
151         if (opt2parg_bool("-append", asize(pa), pa))
152         {
153             mdrunOptions.appendingBehavior = AppendingBehavior::Appending;
154         }
155         else
156         {
157             mdrunOptions.appendingBehavior = AppendingBehavior::NoAppending;
158         }
159     }
160
161     mdrunOptions.rerun            = opt2bSet("-rerun", ssize(filenames), filenames.data());
162     mdrunOptions.ntompOptionIsSet = opt2parg_bSet("-ntomp", asize(pa), pa);
163
164     domdecOptions.rankOrder    = static_cast<DdRankOrder>(nenum(ddrank_opt_choices));
165     domdecOptions.dlbOption    = static_cast<DlbOption>(nenum(dddlb_opt_choices));
166     domdecOptions.numCells[XX] = roundToInt(realddxyz[XX]);
167     domdecOptions.numCells[YY] = roundToInt(realddxyz[YY]);
168     domdecOptions.numCells[ZZ] = roundToInt(realddxyz[ZZ]);
169
170     return 1;
171 }
172
173 LegacyMdrunOptions::~LegacyMdrunOptions()
174 {
175     output_env_done(oenv);
176 }
177
178 } // namespace gmx