File: groupPrograms.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 (119 lines) | stat: -rw-r--r-- 3,945 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
/***********************************************/
/**
* @file groupPrograms.cpp
*
* @brief Runs programs in group and can catch errors.
*
* @author Sebastian Strasser
* @author Torsten Mayer-Guerr
* @date 2017-09-01
*/
/***********************************************/

// Latex documentation
#define DOCSTRING docstring
static const char *docstring = R"(
Runs \config{program}s in a group, which can be used to structure a config file.
If \config{catchErrors} is enabled and an error occurs, the remaining \config{program}s
are skipped and execution continues with \config{errorProgram}s, in case any are defined.
Otherwise an exception is thrown.

The \config{silently} option disables the screen ouput of the \config{program}s.
With \config{outputfileLog} a log file is written for this group additional to a global log file.
This might be helpful within \program{LoopPrograms} with parallel iterations.
)";

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

#include "programs/program.h"

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

/** @brief  Runs programs in group and can catch errors.
* @ingroup programsGroup */
class GroupPrograms
{
public:
  void run(Config &config, Parallel::CommunicatorPtr comm);
};

GROOPS_REGISTER_PROGRAM(GroupPrograms, PARALLEL, "Runs programs in group and can catch errors.", System)
GROOPS_RENAMED_PROGRAM(GroupProgramme, GroupPrograms, date2time(2020, 6, 3))

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

void GroupPrograms::run(Config &config, Parallel::CommunicatorPtr comm)
{
  Log::GroupPtr groupPtr;
  try {Parallel::broadCastExceptions(comm, [&](Parallel::CommunicatorPtr comm)
  {
    Bool          silently;
    ProgramConfig programs, errorPrograms;
    Bool          continueAfterError = FALSE;
    FileName      fileNameLog;

    renameDeprecatedConfig(config, "programme", "program", date2time(2020, 6, 3));

    readConfig(config, "outputfileLog", fileNameLog, Config::OPTIONAL, "",  "additional log file");
    readConfig(config, "silently",      silently,    Config::DEFAULT,  "0", "without showing the output.");
    readConfig(config, "program",       programs,    Config::OPTIONAL, "",  "");
    if(readConfigSequence(config, "catchErrors", Config::OPTIONAL, "", ""))
    {
      continueAfterError = TRUE;
      readConfig(config, "errorProgram", errorPrograms, Config::OPTIONAL, "", "executed if an error occured");
      endSequence(config);
    }
    if(isCreateSchema(config)) return;

    // -------------------------------

    Parallel::barrier(comm);
    groupPtr = Log::group(Parallel::isMaster(comm), silently);
    Log::setLogFile(fileNameLog);
    Log::currentLogFileOnly(TRUE);
    if(Parallel::size(comm) > 1)
      logStatus<<"=== Starting GROOPS subgroup with "<<Parallel::size(comm)<<" processes ==="<<Log::endl;
    else
      logStatus<<"=== Starting GROOPS subgroup ==="<<Log::endl;
    Log::currentLogFileOnly(FALSE);
    Parallel::barrier(comm);

    try
    {
      Parallel::broadCastExceptions(comm, [&](Parallel::CommunicatorPtr comm)
      {
        VariableList varList;
        programs.run(varList, comm);
      });
    }
    catch(std::exception &e)
    {
      if(!continueAfterError || Parallel::isExternal(e))
        throw;
      if(Parallel::isMaster(comm))
        logError<<e.what()<<"  continue with error programs..."<<Log::endl;
      VariableList varList;
      errorPrograms.run(varList, comm);
    }

    Parallel::barrier(comm);
    Log::currentLogFileOnly(TRUE);
    logStatus<<"=== Finished GROOPS subgroup ==="<<Log::endl;
  });}
  catch(std::exception &e)
  {
    if(groupPtr)
    {
      Log::currentLogFileOnly(TRUE);
      if(Parallel::isMaster(comm))
      {
        logError<<"****** Error ******"<<Log::endl;
        logError<<e.what()<<Log::endl;
        logStatus<<"=== Finished GROOPS subgroup with error ==="<<Log::endl;
      }
    }
    GROOPS_RETHROW(e)
  }
}

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