| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*************************************** | ||
| 2 | Auteur : Pierre Aubert | ||
| 3 | Mail : pierre.aubert@lapp.in2p3.fr | ||
| 4 | Licence : CeCILL-C | ||
| 5 | ****************************************/ | ||
| 6 | |||
| 7 | #ifndef __LAUCHPARALLELTHREAD_IMPL_H__ | ||
| 8 | #define __LAUCHPARALLELTHREAD_IMPL_H__ | ||
| 9 | |||
| 10 | #include <iostream> | ||
| 11 | #include <fstream> | ||
| 12 | #include <sstream> | ||
| 13 | |||
| 14 | #include "phoenix_vector_split.h" | ||
| 15 | |||
| 16 | #include "lauchParallelThread.h" | ||
| 17 | |||
| 18 | ///Lauch the callable function __f on nbThread threads with __args to be passed to each thread | ||
| 19 | /** @param listInputPTabModelFile : list of all files to be processed by the threads, this list will be split between each thread | ||
| 20 | * @param 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) | ||
| 21 | * @param __f : function to be lauched on each thread (function with prototype void f(const std::vector<PPath>& listFile, __args); ) | ||
| 22 | * @param __args : extra arguments to be passed to the function __f (optional arguments) | ||
| 23 | * This function needs to be used with --std=c++11 compilation option | ||
| 24 | * Note : nbThread == 0 avoid the execution of the function __f | ||
| 25 | */ | ||
| 26 | template<typename T, typename _Callable, typename... _Args> | ||
| 27 | void lauchParallelThread(const std::vector<T> & listInputPTabModelFile, long unsigned int nbThread, _Callable&& __f, _Args&&... __args){ | ||
| 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 | } | ||
| 45 | |||
| 46 | ///Lauch the callable function __f on nbThread threads with __args to be passed to each thread | ||
| 47 | /** @param[out] listInputPTabModelFile : list of all files to be processed by the threads, this list will be split between each thread | ||
| 48 | * @param 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) | ||
| 49 | * @param __f : function to be lauched on each thread (function with prototype void f(const std::vector<PPath>& listFile, __args); ) | ||
| 50 | * @param __args : extra arguments to be passed to the function __f (optional arguments) | ||
| 51 | * This function needs to be used with --std=c++11 compilation option | ||
| 52 | * Note : nbThread == 0 avoid the execution of the function __f | ||
| 53 | */ | ||
| 54 | template<typename T, typename _Callable, typename... _Args> | ||
| 55 | 6 | void lauchParallelThread(std::vector<T> & listInputPTabModelFile, long unsigned int nbThread, _Callable&& __f, _Args&&... __args){ | |
| 56 |
2/2✓ Branch 0 (2→3) taken 1 times.
✓ Branch 1 (2→4) taken 5 times.
|
7 | if(nbThread == 0lu){return;} |
| 57 |
2/2✓ Branch 0 (4→5) taken 1 times.
✓ Branch 1 (4→7) taken 4 times.
|
5 | if(nbThread == 1lu){ |
| 58 |
1/1✓ Branch 0 (5→6) taken 1 times.
|
1 | __f(listInputPTabModelFile, __args...); |
| 59 | 1 | return; | |
| 60 | } | ||
| 61 | 4 | std::vector<std::vector<T> > listInputFilePerThread; | |
| 62 |
1/1✓ Branch 0 (8→9) taken 4 times.
|
4 | phoenix_vector_split(listInputFilePerThread, listInputPTabModelFile, nbThread); |
| 63 | 4 | nbThread = listInputFilePerThread.size(); //In case we need less thread than required | |
| 64 | 4 | std::vector<std::thread> tabThread; | |
| 65 |
1/1✓ Branch 0 (11→12) taken 4 times.
|
4 | tabThread.resize(nbThread); |
| 66 |
2/2✓ Branch 0 (20→13) taken 12 times.
✓ Branch 1 (20→21) taken 4 times.
|
16 | for(long unsigned int i(0lu); i < nbThread; ++i){ |
| 67 |
1/1✓ Branch 0 (15→16) taken 12 times.
|
12 | tabThread[i] = std::thread(__f, std::ref(listInputFilePerThread[i]), __args...); |
| 68 | } | ||
| 69 |
2/2✓ Branch 0 (25→22) taken 12 times.
✓ Branch 1 (25→26) taken 4 times.
|
16 | for(long unsigned int i(0lu); i < nbThread; ++i){ |
| 70 |
1/1✓ Branch 0 (23→24) taken 12 times.
|
12 | tabThread[i].join(); |
| 71 | } | ||
| 72 | 4 | } | |
| 73 | |||
| 74 | ///Lauch the callable function __f on nbThread threads with __args to be passed to each thread | ||
| 75 | /** @param listInputPTabModelFile : list of all files to be processed by the threads, this list will be split between each thread | ||
| 76 | * @param baseOutputName : base of the log file output name | ||
| 77 | * @param fileSuffix : suffix of the log file output name (can be anything such as file extention) | ||
| 78 | * @param 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) | ||
| 79 | * @param __f : function to be lauched on each thread (function with prototype void f(std::ofstream & fs, const std::vector<PPath>& listFile, __args); ) | ||
| 80 | * @param __args : extra arguments to be passed to the function __f (optional arguments) | ||
| 81 | * @return true if all output file where opened with success, false otherwise | ||
| 82 | * This function needs to be used with --std=c++11 compilation option | ||
| 83 | * Note : nbThread == 0 avoid the execution of the function __f | ||
| 84 | * Each thread will have a std::ofstream to write a separate file | ||
| 85 | */ | ||
| 86 | template<typename _Callable, typename... _Args> | ||
| 87 | 15 | bool lauchParallelThreadLog(const std::vector<PPath> & listInputPTabModelFile, | |
| 88 | const PPath & baseOutputName, const PPath & fileSuffix, | ||
| 89 | long unsigned int nbThread, _Callable&& __f, _Args&&... __args) | ||
| 90 | { | ||
| 91 |
2/2✓ Branch 0 (2→3) taken 3 times.
✓ Branch 1 (2→4) taken 12 times.
|
15 | if(nbThread == 0lu){return true;} |
| 92 |
2/2✓ Branch 0 (4→5) taken 3 times.
✓ Branch 1 (4→22) taken 9 times.
|
12 | if(nbThread == 1lu){ |
| 93 |
1/1✓ Branch 0 (5→6) taken 3 times.
|
3 | std::ofstream fs; |
| 94 |
2/2✓ Branch 0 (6→7) taken 3 times.
✓ Branch 2 (7→8) taken 3 times.
|
3 | fs.open(baseOutputName + fileSuffix); |
| 95 |
2/2✓ Branch 0 (10→11) taken 2 times.
✓ Branch 1 (10→14) taken 1 times.
|
3 | if(fs.is_open()){ |
| 96 |
1/1✓ Branch 0 (11→12) taken 2 times.
|
2 | __f(fs, listInputPTabModelFile, __args...); |
| 97 |
1/1✓ Branch 0 (12→13) taken 2 times.
|
2 | fs.close(); |
| 98 | 2 | return true; | |
| 99 | }else{ | ||
| 100 |
5/5✓ Branch 0 (14→15) taken 1 times.
✓ Branch 2 (15→16) taken 1 times.
✓ Branch 4 (16→17) taken 1 times.
✓ Branch 6 (17→18) taken 1 times.
✓ Branch 8 (18→19) taken 1 times.
|
1 | std::cerr << "lauchParallelThreadLog : cannot open file '"<<baseOutputName << fileSuffix<<"'" << std::endl; |
| 101 | 1 | return false; | |
| 102 | } | ||
| 103 | 3 | } | |
| 104 | 9 | std::vector<std::vector<PPath> > listInputFilePerThread; | |
| 105 |
1/1✓ Branch 0 (23→24) taken 9 times.
|
9 | phoenix_vector_split(listInputFilePerThread, listInputPTabModelFile, nbThread); |
| 106 | 9 | nbThread = listInputFilePerThread.size(); //In case we need less thread than required | |
| 107 | 9 | bool b(true); | |
| 108 | 9 | std::vector<std::ofstream> vecFs; | |
| 109 |
1/1✓ Branch 0 (26→27) taken 9 times.
|
9 | vecFs.resize(nbThread); |
| 110 | 9 | std::vector<std::thread> tabThread; | |
| 111 |
1/1✓ Branch 0 (28→29) taken 9 times.
|
9 | tabThread.resize(nbThread); |
| 112 |
2/2✓ Branch 0 (59→30) taken 27 times.
✓ Branch 1 (59→60) taken 9 times.
|
36 | for(long unsigned int i(0lu); i < nbThread; ++i){ |
| 113 |
1/1✓ Branch 0 (30→31) taken 27 times.
|
27 | std::stringstream fileName; |
| 114 |
4/4✓ Branch 0 (31→32) taken 27 times.
✓ Branch 2 (32→33) taken 27 times.
✓ Branch 4 (33→34) taken 27 times.
✓ Branch 6 (34→35) taken 27 times.
|
27 | fileName << baseOutputName << "_"<< i << fileSuffix; |
| 115 | 27 | std::ofstream & fs = vecFs[i]; | |
| 116 |
2/2✓ Branch 0 (36→37) taken 27 times.
✓ Branch 2 (37→38) taken 27 times.
|
27 | fs.open(fileName.str()); |
| 117 |
2/2✓ Branch 0 (40→41) taken 18 times.
✓ Branch 1 (40→48) taken 9 times.
|
27 | if(fs.is_open()){ |
| 118 |
1/1✓ Branch 0 (43→44) taken 18 times.
|
18 | tabThread[i] = std::thread(__f, std::ref(fs), listInputFilePerThread[i], __args...); |
| 119 | }else{ | ||
| 120 |
7/7✓ Branch 0 (48→49) taken 9 times.
✓ Branch 2 (49→50) taken 9 times.
✓ Branch 4 (50→51) taken 9 times.
✓ Branch 6 (51→52) taken 9 times.
✓ Branch 8 (52→53) taken 9 times.
✓ Branch 10 (53→54) taken 9 times.
✓ Branch 12 (54→55) taken 9 times.
|
9 | std::cerr << "lauchParallelThreadLog : thread("<<i<<") cannot open file '"<<fileName.str()<<"'" << std::endl; |
| 121 | 9 | b &= false; | |
| 122 | } | ||
| 123 | } | ||
| 124 |
2/2✓ Branch 0 (69→61) taken 27 times.
✓ Branch 1 (69→70) taken 9 times.
|
36 | for(long unsigned int i(0lu); i < nbThread; ++i){ |
| 125 |
2/2✓ Branch 0 (63→64) taken 18 times.
✓ Branch 1 (63→68) taken 9 times.
|
27 | if(vecFs[i].is_open()){ |
| 126 |
1/1✓ Branch 0 (65→66) taken 18 times.
|
18 | tabThread[i].join(); |
| 127 |
1/1✓ Branch 0 (67→68) taken 18 times.
|
18 | vecFs[i].close(); |
| 128 | } | ||
| 129 | } | ||
| 130 | 9 | return b; | |
| 131 | 9 | } | |
| 132 | |||
| 133 | ///Lauch the callable function __f on nbThread threads with __args to be passed to each thread | ||
| 134 | /** @param listInputPTabModelFile : list of all files to be processed by the threads, this list will be split between each thread | ||
| 135 | * @param logFile : log file output name | ||
| 136 | * @param 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) | ||
| 137 | * @param __f : function to be lauched on each thread (function with prototype void f(std::ofstream & fs, const std::vector<PPath>& listFile, __args); ) | ||
| 138 | * @param __args : extra arguments to be passed to the function __f (optional arguments) | ||
| 139 | * @return true if all output file where opened with success, false otherwise | ||
| 140 | * This function needs to be used with --std=c++11 compilation option | ||
| 141 | * Note : nbThread == 0 avoid the execution of the function __f | ||
| 142 | * Each thread will have a std::ofstream to write a separate file | ||
| 143 | */ | ||
| 144 | template<typename _Callable, typename... _Args> | ||
| 145 | 15 | bool lauchParallelThreadLog(const std::vector<PPath> & listInputPTabModelFile, | |
| 146 | const PPath & logFile, | ||
| 147 | long unsigned int nbThread, _Callable&& __f, _Args&&... __args) | ||
| 148 | { | ||
| 149 |
3/3✓ Branch 0 (2→3) taken 15 times.
✓ Branch 2 (3→4) taken 15 times.
✓ Branch 4 (4→5) taken 15 times.
|
15 | PString extention(logFile.getExtension()), baseLogFileName(logFile.eraseExtension()); |
| 150 |
5/5✓ Branch 0 (6→7) taken 15 times.
✓ Branch 2 (7→8) taken 10 times.
✓ Branch 3 (7→12) taken 5 times.
✓ Branch 4 (8→9) taken 10 times.
✓ Branch 6 (9→10) taken 10 times.
|
15 | if(extention != ""){extention = "." + extention;} |
| 151 |
3/3✓ Branch 0 (12→13) taken 15 times.
✓ Branch 2 (13→14) taken 15 times.
✓ Branch 4 (14→15) taken 15 times.
|
30 | return lauchParallelThreadLog(listInputPTabModelFile, baseLogFileName, extention, nbThread, __f, __args...); |
| 152 | 15 | } | |
| 153 | |||
| 154 | |||
| 155 | ///Launch the callable function __f on nbThread threads with __args to be passed to each thread | ||
| 156 | /** @param baseOutputName : base of the log file output name | ||
| 157 | * @param fileSuffix : suffix of the log file output name (can be anything such as file extention) | ||
| 158 | * @param 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) | ||
| 159 | * @param __f : function to be lauched on each thread (function with prototype void f(std::ofstream & fs, __args); ) | ||
| 160 | * @param __args : extra arguments to be passed to the function __f (optional arguments) | ||
| 161 | * @return true if all output file where opened with success, false otherwise | ||
| 162 | * This function needs to be used with --std=c++11 compilation option | ||
| 163 | * Note : nbThread == 0 avoid the execution of the function __f | ||
| 164 | * Each thread will have a std::ofstream to write a separate file | ||
| 165 | */ | ||
| 166 | template<typename _Callable, typename... _Args> | ||
| 167 | 15 | bool lauchParallelThreadLog(const PPath & baseOutputName, const PPath & fileSuffix, | |
| 168 | long unsigned int nbThread, _Callable&& __f, _Args&&... __args) | ||
| 169 | { | ||
| 170 |
2/2✓ Branch 0 (2→3) taken 3 times.
✓ Branch 1 (2→4) taken 12 times.
|
15 | if(nbThread == 0lu){return true;} |
| 171 |
2/2✓ Branch 0 (4→5) taken 3 times.
✓ Branch 1 (4→22) taken 9 times.
|
12 | if(nbThread == 1lu){ |
| 172 |
1/1✓ Branch 0 (5→6) taken 3 times.
|
3 | std::ofstream fs; |
| 173 |
2/2✓ Branch 0 (6→7) taken 3 times.
✓ Branch 2 (7→8) taken 3 times.
|
3 | fs.open(baseOutputName + fileSuffix); |
| 174 |
2/2✓ Branch 0 (10→11) taken 2 times.
✓ Branch 1 (10→14) taken 1 times.
|
3 | if(fs.is_open()){ |
| 175 |
1/1✓ Branch 0 (11→12) taken 2 times.
|
2 | __f(fs, __args...); |
| 176 |
1/1✓ Branch 0 (12→13) taken 2 times.
|
2 | fs.close(); |
| 177 | 2 | return true; | |
| 178 | }else{ | ||
| 179 |
5/5✓ Branch 0 (14→15) taken 1 times.
✓ Branch 2 (15→16) taken 1 times.
✓ Branch 4 (16→17) taken 1 times.
✓ Branch 6 (17→18) taken 1 times.
✓ Branch 8 (18→19) taken 1 times.
|
1 | std::cerr << "lauchParallelThreadLog : cannot open file '"<<baseOutputName << fileSuffix<<"'" << std::endl; |
| 180 | 1 | return false; | |
| 181 | } | ||
| 182 | 3 | } | |
| 183 | 9 | bool b(true); | |
| 184 | 9 | std::vector<std::ofstream> vecFs; | |
| 185 |
1/1✓ Branch 0 (23→24) taken 9 times.
|
9 | vecFs.resize(nbThread); |
| 186 | 9 | std::vector<std::thread> tabThread; | |
| 187 |
1/1✓ Branch 0 (25→26) taken 9 times.
|
9 | tabThread.resize(nbThread); |
| 188 |
2/2✓ Branch 0 (55→27) taken 27 times.
✓ Branch 1 (55→56) taken 9 times.
|
36 | for(long unsigned int i(0lu); i < nbThread; ++i){ |
| 189 |
1/1✓ Branch 0 (27→28) taken 27 times.
|
27 | std::stringstream fileName; |
| 190 |
4/4✓ Branch 0 (28→29) taken 27 times.
✓ Branch 2 (29→30) taken 27 times.
✓ Branch 4 (30→31) taken 27 times.
✓ Branch 6 (31→32) taken 27 times.
|
27 | fileName << baseOutputName << "_"<< i << fileSuffix; |
| 191 | 27 | std::ofstream & fs = vecFs[i]; | |
| 192 |
2/2✓ Branch 0 (33→34) taken 27 times.
✓ Branch 2 (34→35) taken 27 times.
|
27 | fs.open(fileName.str()); |
| 193 |
2/2✓ Branch 0 (37→38) taken 18 times.
✓ Branch 1 (37→44) taken 9 times.
|
27 | if(fs.is_open()){ |
| 194 |
1/1✓ Branch 0 (39→40) taken 18 times.
|
18 | tabThread[i] = std::thread(__f, std::ref(fs), __args...); |
| 195 | }else{ | ||
| 196 |
7/7✓ Branch 0 (44→45) taken 9 times.
✓ Branch 2 (45→46) taken 9 times.
✓ Branch 4 (46→47) taken 9 times.
✓ Branch 6 (47→48) taken 9 times.
✓ Branch 8 (48→49) taken 9 times.
✓ Branch 10 (49→50) taken 9 times.
✓ Branch 12 (50→51) taken 9 times.
|
9 | std::cerr << "lauchParallelThreadLog : thread("<<i<<") cannot open file '"<<fileName.str()<<"'" << std::endl; |
| 197 | 9 | b &= false; | |
| 198 | } | ||
| 199 | } | ||
| 200 |
2/2✓ Branch 0 (65→57) taken 27 times.
✓ Branch 1 (65→66) taken 9 times.
|
36 | for(long unsigned int i(0lu); i < nbThread; ++i){ |
| 201 |
2/2✓ Branch 0 (59→60) taken 18 times.
✓ Branch 1 (59→64) taken 9 times.
|
27 | if(vecFs[i].is_open()){ |
| 202 |
1/1✓ Branch 0 (61→62) taken 18 times.
|
18 | tabThread[i].join(); |
| 203 |
1/1✓ Branch 0 (63→64) taken 18 times.
|
18 | vecFs[i].close(); |
| 204 | } | ||
| 205 | } | ||
| 206 | 9 | return b; | |
| 207 | 9 | } | |
| 208 | |||
| 209 | ///Lauch the callable function __f on nbThread threads with __args to be passed to each thread | ||
| 210 | /** @param logFile : log file output name | ||
| 211 | * @param 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) | ||
| 212 | * @param __f : function to be lauched on each thread (function with prototype void f(std::ofstream & fs, __args); ) | ||
| 213 | * @param __args : extra arguments to be passed to the function __f (optional arguments) | ||
| 214 | * @return true if all output file where opened with success, false otherwise | ||
| 215 | * This function needs to be used with --std=c++11 compilation option | ||
| 216 | * Note : nbThread == 0 avoid the execution of the function __f | ||
| 217 | * Each thread will have a std::ofstream to write a separate file | ||
| 218 | */ | ||
| 219 | template<typename _Callable, typename... _Args> | ||
| 220 | 15 | bool lauchParallelThreadLog(const PPath & logFile, | |
| 221 | long unsigned int nbThread, _Callable&& __f, _Args&&... __args) | ||
| 222 | { | ||
| 223 |
3/3✓ Branch 0 (2→3) taken 15 times.
✓ Branch 2 (3→4) taken 15 times.
✓ Branch 4 (4→5) taken 15 times.
|
15 | PString extention(logFile.getExtension()), baseLogFileName(logFile.eraseExtension()); |
| 224 |
5/5✓ Branch 0 (6→7) taken 15 times.
✓ Branch 2 (7→8) taken 10 times.
✓ Branch 3 (7→12) taken 5 times.
✓ Branch 4 (8→9) taken 10 times.
✓ Branch 6 (9→10) taken 10 times.
|
15 | if(extention != ""){extention = "." + extention;} |
| 225 |
3/3✓ Branch 0 (12→13) taken 15 times.
✓ Branch 2 (13→14) taken 15 times.
✓ Branch 4 (14→15) taken 15 times.
|
30 | return lauchParallelThreadLog(baseLogFileName, extention, nbThread, __f, __args...); |
| 226 | 15 | } | |
| 227 | |||
| 228 | |||
| 229 | #endif | ||
| 230 | |||
| 231 |