GCC Code Coverage Report


Directory: ./
File: src/lauchParallelThread_impl.h
Date: 2025-05-16 18:34:22
Exec Total Coverage
Lines: 92 92 100.0%
Branches: 128 129 99.2%

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 taken 1 times.
✓ Branch 1 taken 5 times.
7 if(nbThread == 0lu){return;}
57
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4 times.
5 if(nbThread == 1lu){
58
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 __f(listInputPTabModelFile, __args...);
59 1 return;
60 }
61 4 std::vector<std::vector<T> > listInputFilePerThread;
62
1/1
✓ Branch 1 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 1 taken 4 times.
4 tabThread.resize(nbThread);
66
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
16 for(long unsigned int i(0lu); i < nbThread; ++i){
67
1/1
✓ Branch 3 taken 12 times.
12 tabThread[i] = std::thread(__f, std::ref(listInputFilePerThread[i]), __args...);
68 }
69
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
16 for(long unsigned int i(0lu); i < nbThread; ++i){
70
1/1
✓ Branch 2 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 taken 3 times.
✓ Branch 1 taken 12 times.
15 if(nbThread == 0lu){return true;}
92
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 9 times.
12 if(nbThread == 1lu){
93
1/1
✓ Branch 1 taken 3 times.
3 std::ofstream fs;
94
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
3 fs.open(baseOutputName + fileSuffix);
95
3/3
✓ Branch 1 taken 3 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 1 times.
3 if(fs.is_open()){
96
1/1
✓ Branch 1 taken 2 times.
2 __f(fs, listInputPTabModelFile, __args...);
97
1/1
✓ Branch 1 taken 2 times.
2 fs.close();
98 2 return true;
99 }else{
100
5/5
✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 7 taken 1 times.
✓ Branch 10 taken 1 times.
✓ Branch 13 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 1 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 1 taken 9 times.
9 vecFs.resize(nbThread);
110 9 std::vector<std::thread> tabThread;
111
1/1
✓ Branch 1 taken 9 times.
9 tabThread.resize(nbThread);
112
2/2
✓ Branch 1 taken 27 times.
✓ Branch 2 taken 9 times.
36 for(long unsigned int i(0lu); i < nbThread; ++i){
113
1/1
✓ Branch 1 taken 27 times.
27 std::stringstream fileName;
114
4/4
✓ Branch 1 taken 27 times.
✓ Branch 4 taken 27 times.
✓ Branch 7 taken 27 times.
✓ Branch 10 taken 27 times.
27 fileName << baseOutputName << "_"<< i << fileSuffix;
115 27 std::ofstream & fs = vecFs[i];
116
2/2
✓ Branch 1 taken 27 times.
✓ Branch 4 taken 27 times.
27 fs.open(fileName.str());
117
3/3
✓ Branch 1 taken 27 times.
✓ Branch 3 taken 18 times.
✓ Branch 4 taken 9 times.
27 if(fs.is_open()){
118
1/1
✓ Branch 3 taken 18 times.
18 tabThread[i] = std::thread(__f, std::ref(fs), listInputFilePerThread[i], __args...);
119 }else{
120
7/7
✓ Branch 1 taken 9 times.
✓ Branch 4 taken 9 times.
✓ Branch 7 taken 9 times.
✓ Branch 10 taken 9 times.
✓ Branch 13 taken 9 times.
✓ Branch 16 taken 9 times.
✓ Branch 19 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 taken 27 times.
✓ Branch 1 taken 9 times.
36 for(long unsigned int i(0lu); i < nbThread; ++i){
125
3/3
✓ Branch 2 taken 27 times.
✓ Branch 4 taken 18 times.
✓ Branch 5 taken 9 times.
27 if(vecFs[i].is_open()){
126
1/1
✓ Branch 2 taken 18 times.
18 tabThread[i].join();
127
1/1
✓ Branch 2 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 1 taken 15 times.
✓ Branch 4 taken 15 times.
✓ Branch 7 taken 15 times.
15 PString extention(logFile.getExtension()), baseLogFileName(logFile.eraseExtension());
150
5/5
✓ Branch 1 taken 15 times.
✓ Branch 3 taken 10 times.
✓ Branch 4 taken 5 times.
✓ Branch 6 taken 10 times.
✓ Branch 9 taken 10 times.
15 if(extention != ""){extention = "." + extention;}
151
3/3
✓ Branch 1 taken 15 times.
✓ Branch 4 taken 15 times.
✓ Branch 7 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 taken 3 times.
✓ Branch 1 taken 12 times.
15 if(nbThread == 0lu){return true;}
171
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 9 times.
12 if(nbThread == 1lu){
172
1/1
✓ Branch 1 taken 3 times.
3 std::ofstream fs;
173
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
3 fs.open(baseOutputName + fileSuffix);
174
3/3
✓ Branch 1 taken 3 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 1 times.
3 if(fs.is_open()){
175
1/1
✓ Branch 1 taken 2 times.
2 __f(fs, __args...);
176
1/1
✓ Branch 1 taken 2 times.
2 fs.close();
177 2 return true;
178 }else{
179
5/5
✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 7 taken 1 times.
✓ Branch 10 taken 1 times.
✓ Branch 13 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 1 taken 9 times.
9 vecFs.resize(nbThread);
186 9 std::vector<std::thread> tabThread;
187
1/1
✓ Branch 1 taken 9 times.
9 tabThread.resize(nbThread);
188
2/2
✓ Branch 1 taken 27 times.
✓ Branch 2 taken 9 times.
36 for(long unsigned int i(0lu); i < nbThread; ++i){
189
1/1
✓ Branch 1 taken 27 times.
27 std::stringstream fileName;
190
4/4
✓ Branch 1 taken 27 times.
✓ Branch 4 taken 27 times.
✓ Branch 7 taken 27 times.
✓ Branch 10 taken 27 times.
27 fileName << baseOutputName << "_"<< i << fileSuffix;
191 27 std::ofstream & fs = vecFs[i];
192
2/2
✓ Branch 1 taken 27 times.
✓ Branch 4 taken 27 times.
27 fs.open(fileName.str());
193
3/3
✓ Branch 1 taken 27 times.
✓ Branch 3 taken 18 times.
✓ Branch 4 taken 9 times.
27 if(fs.is_open()){
194
1/1
✓ Branch 2 taken 18 times.
18 tabThread[i] = std::thread(__f, std::ref(fs), __args...);
195 }else{
196
7/7
✓ Branch 1 taken 9 times.
✓ Branch 4 taken 9 times.
✓ Branch 7 taken 9 times.
✓ Branch 10 taken 9 times.
✓ Branch 13 taken 9 times.
✓ Branch 16 taken 9 times.
✓ Branch 19 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 taken 27 times.
✓ Branch 1 taken 9 times.
36 for(long unsigned int i(0lu); i < nbThread; ++i){
201
3/3
✓ Branch 2 taken 27 times.
✓ Branch 4 taken 18 times.
✓ Branch 5 taken 9 times.
27 if(vecFs[i].is_open()){
202
1/1
✓ Branch 2 taken 18 times.
18 tabThread[i].join();
203
1/1
✓ Branch 2 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 1 taken 15 times.
✓ Branch 4 taken 15 times.
✓ Branch 7 taken 15 times.
15 PString extention(logFile.getExtension()), baseLogFileName(logFile.eraseExtension());
224
5/5
✓ Branch 1 taken 15 times.
✓ Branch 3 taken 10 times.
✓ Branch 4 taken 5 times.
✓ Branch 6 taken 10 times.
✓ Branch 9 taken 10 times.
15 if(extention != ""){extention = "." + extention;}
225
3/3
✓ Branch 1 taken 15 times.
✓ Branch 4 taken 15 times.
✓ Branch 7 taken 15 times.
30 return lauchParallelThreadLog(baseLogFileName, extention, nbThread, __f, __args...);
226 15 }
227
228
229 #endif
230
231