PhoenixThread
1.2.3
Tools to ease parallel programming in C++
Toggle main menu visibility
Loading...
Searching...
No Matches
lauchParallelThread_impl.h
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
#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
19
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
47
54
template
<
typename
T,
typename
_Callable,
typename
... _Args>
55
void
lauchParallelThread
(std::vector<T> & listInputPTabModelFile,
long
unsigned
int
nbThread, _Callable&& __f, _Args&&... __args){
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
}
73
75
86
template
<
typename
_Callable,
typename
... _Args>
87
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
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
}
132
134
144
template
<
typename
_Callable,
typename
... _Args>
145
bool
lauchParallelThreadLog
(
const
std::vector<PPath> & listInputPTabModelFile,
146
const
PPath & logFile,
147
long
unsigned
int
nbThread, _Callable&& __f, _Args&&... __args)
148
{
149
PString extention(logFile.getExtension()), baseLogFileName(logFile.eraseExtension());
150
if
(extention !=
""
){extention =
"."
+ extention;}
151
return
lauchParallelThreadLog
(listInputPTabModelFile, baseLogFileName, extention, nbThread, __f, __args...);
152
}
153
154
156
166
template
<
typename
_Callable,
typename
... _Args>
167
bool
lauchParallelThreadLog
(
const
PPath & baseOutputName,
const
PPath & fileSuffix,
168
long
unsigned
int
nbThread, _Callable&& __f, _Args&&... __args)
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
}
208
210
219
template
<
typename
_Callable,
typename
... _Args>
220
bool
lauchParallelThreadLog
(
const
PPath & logFile,
221
long
unsigned
int
nbThread, _Callable&& __f, _Args&&... __args)
222
{
223
PString extention(logFile.getExtension()), baseLogFileName(logFile.eraseExtension());
224
if
(extention !=
""
){extention =
"."
+ extention;}
225
return
lauchParallelThreadLog
(baseLogFileName, extention, nbThread, __f, __args...);
226
}
227
228
229
#endif
230
lauchParallelThread.h
lauchParallelThread
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.
Definition
lauchParallelThread_impl.h:27
lauchParallelThreadLog
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.
Definition
lauchParallelThread_impl.h:87
src
lauchParallelThread_impl.h
Generated by
1.17.0