PhoenixThread  1.0.0
Tools to ease parallel programming in C++
Loading...
Searching...
No Matches
phoenix_program_call.cpp
Go to the documentation of this file.
1/***************************************
2 Auteur : Pierre Aubert
3 Mail : pierre.aubert@lapp.in2p3.fr
4 Licence : CeCILL-C
5****************************************/
6
7#include <iostream>
8
9#include "phoenix_color.h"
10#include "phoenix_system.h"
11#include "convertToString.h"
12#include "phoenix_vector_split.h"
13
14#include "lauchParallelThread.h"
16
18
19
21
24 PString command(call.getCommand());
25 if(call.getWorkingDirectory() != ""){
26 command = "cd " + PString(call.getWorkingDirectory()) + ";" + command;
27 }
28 if(call.getLogFile() != ""){
29 PString logContent("");
30 call.setExitStatus(phoenix_popen(logContent, command));
31 call.getLogFile().saveFileContent(logContent);
32 }else{
33 std::cout << termCyan() << "Call command '"<<command<<"'" << termDefault() << std::endl;
34 call.setExitStatus(system(command.c_str()));
35 }
36}
37
39
42 for(VecProgramCall::iterator it(vecCall.begin()); it != vecCall.end(); ++it){
44 }
45}
46
48
51void phoenix_program_callParallel(VecProgramCall & vecCall, size_t nbThread){
52 if(vecCall.size() == 0lu){return;}
54}
55
57
61void phoenix_program_callVecProgress(PMultiThreadProgress & progress, VecProgramCall & vecCall, size_t threadIndex){
62 size_t indexProgressBar = progress.addProgressBar("Progress of thread " + valueToString(threadIndex), vecCall.size());
63 for(VecProgramCall::iterator it(vecCall.begin()); it != vecCall.end(); ++it){
65 progress.incrementProgress(indexProgressBar); //Update the progress bar
66 }
67}
68
70
73 size_t nbThread = vecVecProgramCall.size(); //In case we need less thread than required
74 PMultiThreadProgress progress(nbThread);
75 std::vector<std::thread> vecThread;
76 vecThread.resize(nbThread);
77 std::thread threadProgress(phoenix_print_parallel_progress, std::ref(progress), 1);
78 for(size_t i(0lu); i < nbThread; ++i){
79 vecThread[i] = std::thread(phoenix_program_callVecProgress, std::ref(progress), std::ref(vecVecProgramCall[i]), i);
80 }
81 threadProgress.join();
82 for(size_t i(0lu); i < nbThread; ++i){
83 vecThread[i].join();
84 }
85 size_t nbSuccess(phoenix_program_getNbSuccessCall(vecVecProgramCall));
86 size_t nbFail(phoenix_program_getNbCall(vecVecProgramCall) - nbSuccess);
87 std::cout << "VecProgramCall : summary : " << nbSuccess << " success, " << nbFail << " fail" << std::endl;
88}
89
91
95 if(vecCall.size() == 0lu){return;}
96 VecVecProgramCall vecVecProgramCall;
97 phoenix_vector_split(vecVecProgramCall, vecCall, nbThread);
99}
100
102
108void phoenix_program_addcall(VecProgramCall & vecCall, const PString & name, const PString & command,
109 const PPath & workingDirectory, const PPath & logFile)
110{
111 ProgramCall call;
112 call.setName(name);
113 call.setCommand(command);
114 call.setWorkingDirectory(workingDirectory);
115 call.setLogFile(logFile);
116 call.setExitStatus(0);
117 vecCall.push_back(call);
118}
119
121
125size_t phoenix_program_getNbCallExitStatusType(const VecProgramCall & vecCall, int result){
126 size_t nbSuccessCall(0lu);
127 for(VecProgramCall::const_iterator it(vecCall.begin()); it != vecCall.end(); ++it){
128 nbSuccessCall += it->getExitStatus() == result;
129 }
130 return nbSuccessCall;
131}
132
134
139 size_t nbSuccessCall(0lu);
140 for(VecVecProgramCall::const_iterator it(vecCall.begin()); it != vecCall.end(); ++it){
141 nbSuccessCall += phoenix_program_getNbCallExitStatusType(*it, result);
142 }
143 return nbSuccessCall;
144}
145
147
153
155
161
163
167 size_t nbCall(0lu);
168 for(VecVecProgramCall::const_iterator it(vecCall.begin()); it != vecCall.end(); ++it){
169 nbCall += it->size();
170 }
171 return nbCall;
172}
173
void phoenix_print_parallel_progress(PMultiThreadProgress &progress, int refreshSecond)
Print the parallel progression of the computing.
Deal with Progress bar in mutlithread mode.
size_t addProgressBar(const PString &name, int progressMax=1)
Add a progress line.
void incrementProgress(size_t index)
Increment the progress of the progress bar at index.
Call a program.
Definition ProgramCall.h:14
void setLogFile(const PPath &logFile)
Sets the logFile of the ProgramCall.
const PPath & getLogFile() const
Gets the logFile of the ProgramCall.
void setName(const PString &name)
Sets the name of the ProgramCall.
const PPath & getWorkingDirectory() const
Gets the workingDirectory of the ProgramCall.
void setExitStatus(int exitStatus)
Sets the exitStatus of the ProgramCall.
const PString & getCommand() const
Gets the command of the ProgramCall.
void setCommand(const PString &command)
Sets the command of the ProgramCall.
void setWorkingDirectory(const PPath &workingDirectory)
Sets the workingDirectory of the ProgramCall.
void lauchParallelThread(const std::vector< T > &listInputPTabModelFile, long unsigned int nbThread, _Callable &&__f, _Args &&... __args)
Lauch the callable function __f on nbThread threads with __args to be passed to each thread.
void phoenix_program_callParallelProgressBar(VecVecProgramCall &vecVecProgramCall)
Exectute programs on several threads (with a multithread progress bar)
size_t phoenix_program_getNbCallExitStatusType(const VecProgramCall &vecCall, int result)
Get number of call result of a given type.
size_t phoenix_program_getNbSuccessCall(const VecProgramCall &vecCall)
Get the number of success call.
void phoenix_program_callParallel(VecProgramCall &vecCall, size_t nbThread)
Exectute programs on several threads.
void phoenix_program_callVecProgress(PMultiThreadProgress &progress, VecProgramCall &vecCall, size_t threadIndex)
Call a vector of program.
void phoenix_program_callVec(VecProgramCall &vecCall)
Call a vector of program.
void phoenix_program_addcall(VecProgramCall &vecCall, const PString &name, const PString &command, const PPath &workingDirectory, const PPath &logFile)
Add a program call.
void phoenix_program_call(ProgramCall &call)
Call a program.
size_t phoenix_program_getNbCall(const VecVecProgramCall &vecCall)
Get the number of call.
std::vector< VecProgramCall > VecVecProgramCall
Vector of vector of program call.
std::vector< ProgramCall > VecProgramCall
Vector of program call.