PhoenixThread  1.0.0
Tools to ease parallel programming in C++
Loading...
Searching...
No Matches
lauchParallelThread_impl.h File Reference
#include <iostream>
#include <fstream>
#include <sstream>
#include "phoenix_vector_split.h"
#include "lauchParallelThread.h"
+ Include dependency graph for lauchParallelThread_impl.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

template<typename T, typename _Callable, typename... _Args>
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.
 
template<typename T, typename _Callable, typename... _Args>
void lauchParallelThread (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.
 
template<typename _Callable, typename... _Args>
bool lauchParallelThreadLog (const PPath &baseOutputName, const PPath &fileSuffix, long unsigned int nbThread, _Callable &&__f, _Args &&... __args)
 Launch the callable function __f on nbThread threads with __args to be passed to each thread.
 
template<typename _Callable, typename... _Args>
bool lauchParallelThreadLog (const PPath &logFile, long unsigned int nbThread, _Callable &&__f, _Args &&... __args)
 Lauch the callable function __f on nbThread threads with __args to be passed to each thread.
 
template<typename _Callable, typename... _Args>
bool lauchParallelThreadLog (const std::vector< PPath > &listInputPTabModelFile, const PPath &baseOutputName, const PPath &fileSuffix, long unsigned int nbThread, _Callable &&__f, _Args &&... __args)
 Lauch the callable function __f on nbThread threads with __args to be passed to each thread.
 
template<typename _Callable, typename... _Args>
bool lauchParallelThreadLog (const std::vector< PPath > &listInputPTabModelFile, const PPath &logFile, long unsigned int nbThread, _Callable &&__f, _Args &&... __args)
 Lauch the callable function __f on nbThread threads with __args to be passed to each thread.
 

Function Documentation

◆ lauchParallelThread() [1/2]

template<typename T, typename _Callable, typename... _Args>
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.

Parameters
listInputPTabModelFile: list of all files to be processed by the threads, this list will be split between each thread
nbThread: number of thread to lauch, if 0 : use every able cores (never upper than PLIB_SYSTEM_NB_CORE, number of thread on the current system)
__f: function to be lauched on each thread (function with prototype void f(const std::vector<PPath>& listFile, __args); )
__args: extra arguments to be passed to the function __f (optional arguments) This function needs to be used with –std=c++11 compilation option Note : nbThread == 0 avoid the execution of the function __f

Definition at line 27 of file lauchParallelThread_impl.h.

27 {
28 if(nbThread == 0lu){return;}
29 if(nbThread == 1lu){
30 __f(listInputPTabModelFile, __args...);
31 return;
32 }
33 std::vector<std::vector<T> > listInputFilePerThread;
34 phoenix_vector_split(listInputFilePerThread, listInputPTabModelFile, nbThread);
35 nbThread = listInputFilePerThread.size(); //In case we need less thread than required
36 std::vector<std::thread> tabThread;
37 tabThread.resize(nbThread);
38 for(long unsigned int i(0lu); i < nbThread; ++i){
39 tabThread[i] = std::thread(__f, listInputFilePerThread[i], __args...);
40 }
41 for(long unsigned int i(0lu); i < nbThread; ++i){
42 tabThread[i].join();
43 }
44}

Referenced by phoenix_program_callParallel().

+ Here is the caller graph for this function:

◆ lauchParallelThread() [2/2]

template<typename T, typename _Callable, typename... _Args>
void lauchParallelThread ( 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.

Parameters
[out]listInputPTabModelFile: list of all files to be processed by the threads, this list will be split between each thread
nbThread: number of thread to lauch, if 0 : use every able cores (never upper than PLIB_SYSTEM_NB_CORE, number of thread on the current system)
__f: function to be lauched on each thread (function with prototype void f(const std::vector<PPath>& listFile, __args); )
__args: extra arguments to be passed to the function __f (optional arguments) This function needs to be used with –std=c++11 compilation option Note : nbThread == 0 avoid the execution of the function __f

Definition at line 55 of file lauchParallelThread_impl.h.

55 {
56 if(nbThread == 0lu){return;}
57 if(nbThread == 1lu){
58 __f(listInputPTabModelFile, __args...);
59 return;
60 }
61 std::vector<std::vector<T> > listInputFilePerThread;
62 phoenix_vector_split(listInputFilePerThread, listInputPTabModelFile, nbThread);
63 nbThread = listInputFilePerThread.size(); //In case we need less thread than required
64 std::vector<std::thread> tabThread;
65 tabThread.resize(nbThread);
66 for(long unsigned int i(0lu); i < nbThread; ++i){
67 tabThread[i] = std::thread(__f, std::ref(listInputFilePerThread[i]), __args...);
68 }
69 for(long unsigned int i(0lu); i < nbThread; ++i){
70 tabThread[i].join();
71 }
72}

◆ lauchParallelThreadLog() [1/4]

template<typename _Callable, typename... _Args>
bool lauchParallelThreadLog ( const PPath & baseOutputName,
const PPath & fileSuffix,
long unsigned int nbThread,
_Callable && __f,
_Args &&... __args )

Launch the callable function __f on nbThread threads with __args to be passed to each thread.

Parameters
baseOutputName: base of the log file output name
fileSuffix: suffix of the log file output name (can be anything such as file extention)
nbThread: number of thread to lauch, if 0 : use every able cores (never upper than PLIB_SYSTEM_NB_CORE, number of thread on the current system)
__f: function to be lauched on each thread (function with prototype void f(std::ofstream & fs, __args); )
__args: extra arguments to be passed to the function __f (optional arguments)
Returns
true if all output file where opened with success, false otherwise This function needs to be used with –std=c++11 compilation option Note : nbThread == 0 avoid the execution of the function __f Each thread will have a std::ofstream to write a separate file

Definition at line 167 of file lauchParallelThread_impl.h.

169{
170 if(nbThread == 0lu){return true;}
171 if(nbThread == 1lu){
172 std::ofstream fs;
173 fs.open(baseOutputName + fileSuffix);
174 if(fs.is_open()){
175 __f(fs, __args...);
176 fs.close();
177 return true;
178 }else{
179 std::cerr << "lauchParallelThreadLog : cannot open file '"<<baseOutputName << fileSuffix<<"'" << std::endl;
180 return false;
181 }
182 }
183 bool b(true);
184 std::vector<std::ofstream> vecFs;
185 vecFs.resize(nbThread);
186 std::vector<std::thread> tabThread;
187 tabThread.resize(nbThread);
188 for(long unsigned int i(0lu); i < nbThread; ++i){
189 std::stringstream fileName;
190 fileName << baseOutputName << "_"<< i << fileSuffix;
191 std::ofstream & fs = vecFs[i];
192 fs.open(fileName.str());
193 if(fs.is_open()){
194 tabThread[i] = std::thread(__f, std::ref(fs), __args...);
195 }else{
196 std::cerr << "lauchParallelThreadLog : thread("<<i<<") cannot open file '"<<fileName.str()<<"'" << std::endl;
197 b &= false;
198 }
199 }
200 for(long unsigned int i(0lu); i < nbThread; ++i){
201 if(vecFs[i].is_open()){
202 tabThread[i].join();
203 vecFs[i].close();
204 }
205 }
206 return b;
207}

◆ lauchParallelThreadLog() [2/4]

template<typename _Callable, typename... _Args>
bool lauchParallelThreadLog ( const PPath & logFile,
long unsigned int nbThread,
_Callable && __f,
_Args &&... __args )

Lauch the callable function __f on nbThread threads with __args to be passed to each thread.

Parameters
logFile: log file output name
nbThread: number of thread to lauch, if 0 : use every able cores (never upper than PLIB_SYSTEM_NB_CORE, number of thread on the current system)
__f: function to be lauched on each thread (function with prototype void f(std::ofstream & fs, __args); )
__args: extra arguments to be passed to the function __f (optional arguments)
Returns
true if all output file where opened with success, false otherwise This function needs to be used with –std=c++11 compilation option Note : nbThread == 0 avoid the execution of the function __f Each thread will have a std::ofstream to write a separate file

Definition at line 220 of file lauchParallelThread_impl.h.

222{
223 PString extention(logFile.getExtension()), baseLogFileName(logFile.eraseExtension());
224 if(extention != ""){extention = "." + extention;}
225 return lauchParallelThreadLog(baseLogFileName, extention, nbThread, __f, __args...);
226}
bool lauchParallelThreadLog(const std::vector< PPath > &listInputPTabModelFile, const PPath &baseOutputName, const PPath &fileSuffix, long unsigned int nbThread, _Callable &&__f, _Args &&... __args)
Lauch the callable function __f on nbThread threads with __args to be passed to each thread.

References lauchParallelThreadLog().

+ Here is the call graph for this function:

◆ lauchParallelThreadLog() [3/4]

template<typename _Callable, typename... _Args>
bool lauchParallelThreadLog ( const std::vector< PPath > & listInputPTabModelFile,
const PPath & baseOutputName,
const PPath & fileSuffix,
long unsigned int nbThread,
_Callable && __f,
_Args &&... __args )

Lauch the callable function __f on nbThread threads with __args to be passed to each thread.

Parameters
listInputPTabModelFile: list of all files to be processed by the threads, this list will be split between each thread
baseOutputName: base of the log file output name
fileSuffix: suffix of the log file output name (can be anything such as file extention)
nbThread: number of thread to lauch, if 0 : use every able cores (never upper than PLIB_SYSTEM_NB_CORE, number of thread on the current system)
__f: function to be lauched on each thread (function with prototype void f(std::ofstream & fs, const std::vector<PPath>& listFile, __args); )
__args: extra arguments to be passed to the function __f (optional arguments)
Returns
true if all output file where opened with success, false otherwise This function needs to be used with –std=c++11 compilation option Note : nbThread == 0 avoid the execution of the function __f Each thread will have a std::ofstream to write a separate file

Definition at line 87 of file lauchParallelThread_impl.h.

90{
91 if(nbThread == 0lu){return true;}
92 if(nbThread == 1lu){
93 std::ofstream fs;
94 fs.open(baseOutputName + fileSuffix);
95 if(fs.is_open()){
96 __f(fs, listInputPTabModelFile, __args...);
97 fs.close();
98 return true;
99 }else{
100 std::cerr << "lauchParallelThreadLog : cannot open file '"<<baseOutputName << fileSuffix<<"'" << std::endl;
101 return false;
102 }
103 }
104 std::vector<std::vector<PPath> > listInputFilePerThread;
105 phoenix_vector_split(listInputFilePerThread, listInputPTabModelFile, nbThread);
106 nbThread = listInputFilePerThread.size(); //In case we need less thread than required
107 bool b(true);
108 std::vector<std::ofstream> vecFs;
109 vecFs.resize(nbThread);
110 std::vector<std::thread> tabThread;
111 tabThread.resize(nbThread);
112 for(long unsigned int i(0lu); i < nbThread; ++i){
113 std::stringstream fileName;
114 fileName << baseOutputName << "_"<< i << fileSuffix;
115 std::ofstream & fs = vecFs[i];
116 fs.open(fileName.str());
117 if(fs.is_open()){
118 tabThread[i] = std::thread(__f, std::ref(fs), listInputFilePerThread[i], __args...);
119 }else{
120 std::cerr << "lauchParallelThreadLog : thread("<<i<<") cannot open file '"<<fileName.str()<<"'" << std::endl;
121 b &= false;
122 }
123 }
124 for(long unsigned int i(0lu); i < nbThread; ++i){
125 if(vecFs[i].is_open()){
126 tabThread[i].join();
127 vecFs[i].close();
128 }
129 }
130 return b;
131}

Referenced by lauchParallelThreadLog(), and lauchParallelThreadLog().

+ Here is the caller graph for this function:

◆ lauchParallelThreadLog() [4/4]

template<typename _Callable, typename... _Args>
bool lauchParallelThreadLog ( const std::vector< PPath > & listInputPTabModelFile,
const PPath & logFile,
long unsigned int nbThread,
_Callable && __f,
_Args &&... __args )

Lauch the callable function __f on nbThread threads with __args to be passed to each thread.

Parameters
listInputPTabModelFile: list of all files to be processed by the threads, this list will be split between each thread
logFile: log file output name
nbThread: number of thread to lauch, if 0 : use every able cores (never upper than PLIB_SYSTEM_NB_CORE, number of thread on the current system)
__f: function to be lauched on each thread (function with prototype void f(std::ofstream & fs, const std::vector<PPath>& listFile, __args); )
__args: extra arguments to be passed to the function __f (optional arguments)
Returns
true if all output file where opened with success, false otherwise This function needs to be used with –std=c++11 compilation option Note : nbThread == 0 avoid the execution of the function __f Each thread will have a std::ofstream to write a separate file

Definition at line 145 of file lauchParallelThread_impl.h.

148{
149 PString extention(logFile.getExtension()), baseLogFileName(logFile.eraseExtension());
150 if(extention != ""){extention = "." + extention;}
151 return lauchParallelThreadLog(listInputPTabModelFile, baseLogFileName, extention, nbThread, __f, __args...);
152}

References lauchParallelThreadLog().

+ Here is the call graph for this function: