File: loopPrograms.cpp

package info (click to toggle)
groops 0%2Bgit20250907%2Bds-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 11,140 kB
  • sloc: cpp: 135,607; fortran: 1,603; makefile: 20
file content (180 lines) | stat: -rw-r--r-- 6,920 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/***********************************************/
/**
* @file loopPrograms.cpp
*
* @brief Runs programs multiple times.
*
* @author Sebastian Strasser
* @author Torsten Mayer-Guerr
* @date 2017-02-05
*/
/***********************************************/

// Latex documentation
#define DOCSTRING docstring
static const char *docstring = R"(
This program runs a list of programs in a \configClass{loop}{loopType}.

If \config{continueAfterError}=\verb|yes| and an error occurs, the remaining programs in the current iteration
are skipped and the loop continues with the next iteration. Otherwise an exception is thrown.

If this program is executed on multiple processing nodes, the iterations can be computed in parallel,
see \reference{parallelization}{general.parallelization}. The first process serves as load balancer
and the other processes are assigned to iterations according to \config{processCountPerIteration}.
For example, running a loop containing three iterations on 13 processes with \config{processCountPerIteration}=\verb|4|,
runs the three iterations in parallel, with each iteration being assigned four processes.
With \config{parallelLog}=\verb|yes| all processes write output to screen and the log file.
As the output can be quite confusing in this case, running \program{GroupPrograms} with an extra \config{outputfileLog}
for each iteration (use the loop variables for the name of the log files) might be helpful.
)";

/***********************************************/

#include "programs/program.h"
#include "classes/loop/loop.h"

/***** CLASS ***********************************/

/** @brief  Runs programs multiple times.
* @ingroup programsGroup */
class LoopPrograms
{
public:
  void run(Config &config, Parallel::CommunicatorPtr comm);
};

GROOPS_REGISTER_PROGRAM(LoopPrograms, PARALLEL, "Runs programs multiple times.", System)
GROOPS_RENAMED_PROGRAM(LoopProgramme, LoopPrograms, date2time(2020, 6, 3))

/***********************************************/

void LoopPrograms::run(Config &config, Parallel::CommunicatorPtr comm)
{
  try
  {
    LoopPtr       loopPtr;
    Bool          continueAfterError;
    UInt          processCount;
    Bool          parallelLog;
    ProgramConfig programs;

    renameDeprecatedConfig(config, "programme",     "program", date2time(2020, 6, 3));
    renameDeprecatedConfig(config, "parallelLoops", "processCountPerLoopStep", date2time(2020, 12, 28));
    renameDeprecatedConfig(config, "processCountPerLoopStep", "processCountPerIteration", date2time(2020, 1, 14));

    readConfig(config, "loop",                     loopPtr,            Config::MUSTSET,  "",  "subprograms are called for every iteration");
    readConfig(config, "continueAfterError",       continueAfterError, Config::DEFAULT,  "0", "continue with next iteration after error, otherwise throw exception");
    readConfig(config, "processCountPerIteration", processCount,       Config::DEFAULT,  "0", "0: use all processes for each iteration");
    readConfig(config, "parallelLog",              parallelLog,        Config::DEFAULT,  "1", "write to screen/log file from all processing nodes in parallelized loops");
    readConfig(config, "program",                  programs,           Config::OPTIONAL, "",  "");
    if(isCreateSchema(config)) return;

    VariableList varList;
    if(!loopPtr->iteration(varList))
      return;

    // Every process executes every iteration
    // --------------------------------------
    if((processCount == 0) || (processCount+1 >= Parallel::size(comm)) || (Parallel::size(comm) < 3))
    {
      Log::Timer timer(loopPtr->count());
      do
      {
        logStatus<<"=== "<<loopPtr->index()<<". loop ==="<<Log::endl;
        timer.loopStep(loopPtr->index()-1);
        try
        {
          Parallel::broadCastExceptions(comm, [&](Parallel::CommunicatorPtr comm)
          {
            auto varListTmp = varList;
            programs.run(varListTmp, comm);
          });
        }
        catch(std::exception &e)
        {
          if(!continueAfterError || Parallel::isExternal(e))
            throw;
          if(Parallel::isMaster(comm))
            logError<<e.what()<<"  continue..."<<Log::endl;
        }
      }
      while(loopPtr->iteration(varList));
      timer.loopEnd();
      return;
    }

    // Iterations in parallel
    // ----------------------
    processCount = std::min(processCount, Parallel::size(comm)-1);
    UInt rank = Parallel::myRank(comm);
    auto commLocal = Parallel::splitCommunicator((rank > 0) ? (rank-1)/processCount : NULLINDEX, rank, comm); // processes of an iteration
    auto commLoop  = Parallel::splitCommunicator(((rank == 0) || ((rank-1)%processCount == 0)) ?  0 : NULLINDEX, rank, comm); // 'main' processes of all iterations
    Log::GroupPtr groupPtr = Log::group(((rank == 0) || ((rank-1)%processCount == 0)), (rank>0) && !parallelLog); // group is freed in the destructor

    if(commLoop && Parallel::isMaster(commLoop))
    {
      // parallel version: main node
      // ---------------------------
      Log::Timer timer(loopPtr->count(), Parallel::size(commLoop)-1, TRUE);
      do
      {
        timer.loopStep(loopPtr->index()-1);
        UInt process;
        Parallel::receive(process, NULLINDEX, commLoop);     // which process needs work?
        Parallel::send(loopPtr->index(), process, commLoop); // send new loop number to be computed at process
      }
      while(loopPtr->iteration(varList));
      // send to all processes the end signal (NULLINDEX)
      for(UInt i=1; i<Parallel::size(commLoop); i++)
      {
        UInt process;
        Parallel::receive(process, NULLINDEX, commLoop); // which process needs work?
        Parallel::send(NULLINDEX, process, commLoop);    // end signal
      }
      Parallel::barrier(comm);
      timer.loopEnd();
    }
    else
    {
      // clients
      // -------
      for(;;)
      {
        UInt index;
        if(Parallel::isMaster(commLocal))
        {
          Parallel::send(Parallel::myRank(commLoop), 0, commLoop);
          Parallel::receive(index, 0, commLoop);
        }
        Parallel::broadCast(index, 0, commLocal);
        if(index == NULLINDEX) // end signal?
          break;
        while(loopPtr->index() != index)
          loopPtr->iteration(varList);

        try
        {
          Parallel::broadCastExceptions(commLocal, [&](Parallel::CommunicatorPtr commLocal)
          {
            auto varListTmp = varList;
            programs.run(varListTmp, commLocal);
          });
        }
        catch(std::exception &e)
        {
          if(!continueAfterError || Parallel::isExternal(e))
            throw;
          if(Parallel::isMaster(commLocal))
            logError<<e.what()<<"  continue..."<<Log::endl;
        }
      }
      Parallel::barrier(comm);
    } // clients
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/