1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
|
/***********************************************/
/**
* @file parallel.h
*
* @brief Wrapper for Message Passing Interface (MPI).
* All functions are empty statements in case
* of the single processor version (parallelSingle.cpp).
*
* @author Torsten Mayer-Guerr
* @date 2004-11-13
*
*/
/***********************************************/
#ifndef __GROOPS_PARALLEL__
#define __GROOPS_PARALLEL__
#include "base/import.h"
#include "inputOutput/archiveBinary.h"
#include "inputOutput/logging.h"
/***********************************************/
class GnssType;
/***********************************************/
/** @brief Wrapper for Message Passing Interface (MPI).
* All functions are empty statements in case
* of the single processor version (parallelSingle.cpp).
* @ingroup parallelGroup */
namespace Parallel
{
class Communicator;
typedef std::shared_ptr<Communicator> CommunicatorPtr;
/** @brief Must be called firstly in main.
* @return global communicator. */
CommunicatorPtr init(int argc, char *argv[]);
/** @brief Add an extra communcation channel to @p comm.
* @p receive is called on main process, if the returned send function is called by an arbitrary process.
* This function is used for the log.
* Must be called by every process in @a comm. */
std::function<void(UInt type, const std::string &str)> addChannel(const std::function<void(UInt rank, UInt type, const std::string &str)> &receive, CommunicatorPtr comm);
// =========================================================
/** @brief Creates new communicators.
* a new group is created for each different @a color.
* the ranks in the groups are sorted by the @a key. */
CommunicatorPtr splitCommunicator(UInt color, UInt key, CommunicatorPtr comm);
/** @brief Creates new communicators.
* Must be called by every process in @a comm. */
CommunicatorPtr createCommunicator(std::vector<UInt> ranks, CommunicatorPtr comm);
/** @brief The communicator that refers to the own process only. */
CommunicatorPtr selfCommunicator();
// =========================================================
/** @brief Number of processes. */
UInt size(CommunicatorPtr comm);
/** @brief Process index. */
UInt myRank(CommunicatorPtr comm);
/** @brief Is ths the master process (rank==0)? */
inline Bool isMaster(CommunicatorPtr comm) {return (myRank(comm) == 0);}
/** @brief Blocks until all process have reached this routine. */
void barrier(CommunicatorPtr comm);
/** @brief Non blocking check of extra channels. */
void peek(CommunicatorPtr comm);
/** @brief Distribute exceptions thrown in @p func by a single node to all nodes.
* Must be called by every process in @a comm.
* Exceptions causes memory leaks due to unfinished communications.
* Based on the idea: https://arxiv.org/abs/1804.04481 */
void broadCastExceptions(CommunicatorPtr comm, std::function<void(CommunicatorPtr)> func);
/** @brief Is @a broadCastExceptions interrupted by an external process? */
Bool isExternal(std::exception &e);
// =========================================================
/** @brief Send raw data @a x to process with rank @a process. */
void send(const Byte *x, UInt size, UInt process, CommunicatorPtr comm);
/** @brief receive raw data @a x from prozess with rank @a process.
* If @a process = NULLINDEX then receive from an arbitrary process. */
void receive(Byte *x, UInt size, UInt process, CommunicatorPtr comm);
/** @brief Distribute raw data @a x at @a process to all other processes. */
void broadCast(Byte *x, UInt size, UInt process, CommunicatorPtr comm);
// =========================================================
/** @brief Send @a x to process with rank @a process. */
///@{
template<typename T> void send(const T &x, UInt process, CommunicatorPtr comm);
template<> void send(const UInt &x, UInt process, CommunicatorPtr comm);
template<> void send(const Double &x, UInt process, CommunicatorPtr comm);
template<> void send(const Bool &x, UInt process, CommunicatorPtr comm);
template<> void send(const Angle &x, UInt process, CommunicatorPtr comm);
template<> void send(const Time &x, UInt process, CommunicatorPtr comm);
template<> void send(const GnssType &x, UInt process, CommunicatorPtr comm);
template<> void send(const Vector3d &x, UInt process, CommunicatorPtr comm);
template<> void send(const Vector &x, UInt process, CommunicatorPtr comm);
template<> void send(const Matrix &x, UInt process, CommunicatorPtr comm);
///@}
/** @brief receive @a x from prozess with rank @a process.
* If @a process = NULLINDEX then receive from an arbitrary process. */
///@{
template<typename T> void receive(T &x, UInt process, CommunicatorPtr comm);
template<> void receive(UInt &x, UInt process, CommunicatorPtr comm);
template<> void receive(Double &x, UInt process, CommunicatorPtr comm);
template<> void receive(Bool &x, UInt process, CommunicatorPtr comm);
template<> void receive(Angle &x, UInt process, CommunicatorPtr comm);
template<> void receive(Time &x, UInt process, CommunicatorPtr comm);
template<> void receive(GnssType &x, UInt process, CommunicatorPtr comm);
template<> void receive(Vector3d &x, UInt process, CommunicatorPtr comm);
template<> void receive(Vector &x, UInt process, CommunicatorPtr comm);
template<> void receive(Matrix &x, UInt process, CommunicatorPtr comm);
///@}
/** @brief Distribute @a x at @a process to all other processes. */
///@{
template<typename T> void broadCast(T &x, UInt process, CommunicatorPtr comm);
template<> void broadCast(UInt &x, UInt process, CommunicatorPtr comm);
template<> void broadCast(Double &x, UInt process, CommunicatorPtr comm);
template<> void broadCast(Bool &x, UInt process, CommunicatorPtr comm);
template<> void broadCast(Angle &x, UInt process, CommunicatorPtr comm);
template<> void broadCast(Time &x, UInt process, CommunicatorPtr comm);
template<> void broadCast(GnssType &x, UInt process, CommunicatorPtr comm);
template<> void broadCast(Vector3d &x, UInt process, CommunicatorPtr comm);
template<> void broadCast(Vector &x, UInt process, CommunicatorPtr comm);
template<> void broadCast(Matrix &x, UInt process, CommunicatorPtr comm);
///@}
/** @brief Sum up @a x at all processes (also rank 0) and send the result to @a process. */
///@{
void reduceSum(UInt &x, UInt process, CommunicatorPtr comm);
void reduceSum(Double &x, UInt process, CommunicatorPtr comm);
void reduceSum(Bool &x, UInt process, CommunicatorPtr comm);
void reduceSum(Matrix &x, UInt process, CommunicatorPtr comm);
void reduceSum(std::vector<Double> &x, UInt process, CommunicatorPtr comm);
///@}
/** @brief Find min/max of @a x at all processes (also rank 0) and send the result to @a process. */
///@{
void reduceMin(UInt &x, UInt process, CommunicatorPtr comm);
void reduceMin(Double &x, UInt process, CommunicatorPtr comm);
void reduceMax(UInt &x, UInt process, CommunicatorPtr comm);
void reduceMax(Double &x, UInt process, CommunicatorPtr comm);
///@}
// =========================================================
/** @brief Parallelized loop.
* Calls @a func(i) for every @a i in [0,count).
* The different calls are distributed other the processes (without master).
* @return The process number for @a i is returned (valid at master). */
template<typename T> std::vector<UInt> forEach(UInt count, T func, CommunicatorPtr comm, Bool timing=TRUE);
/** @brief Parallelized loop.
* Calls @a vec[i]=func(i) for every @a i in [0,vec.size()).
* The different calls are distributed other the processes (without master).
* The result in @a vec is only valid at master.
* @return The process number for @a i is returned (valid at master). */
template<typename A, typename T> std::vector<UInt> forEach(std::vector<A> &vec, T func, CommunicatorPtr comm, Bool timing=TRUE);
/** @brief Parallelized loop.
* Calls @a func(i) for every @a i in [0,count).
* The different calls are distributed other the processes (without master).
* @return The process number for @a i is returned (valid at master). */
template<typename T> std::vector<UInt> forEachInterval(UInt count, const std::vector<UInt> &interval, T func, CommunicatorPtr comm, Bool timing=TRUE);
/** @brief Parallelized loop.
* Calls @a vec[i]=func(i) for every @a i in [0,vec.size()).
* The different calls are distributed other the processes (without master).
* The result in @a vec is only valid at master.
* @return The process number for @a i is returned (valid at master). */
template<typename A, typename T> std::vector<UInt> forEachInterval(std::vector<A> &vec, const std::vector<UInt> &interval, T func, CommunicatorPtr comm, Bool timing=TRUE);
/** @brief Parallelized loop.
* Calls @a func(i) for every @a i in [0,count).
* The different calls are distributed using @a processNo (without master).
* The result in @a vec is only valid at master. */
template<typename T> void forEachProcess(UInt count, T func, const std::vector<UInt> &processNo, CommunicatorPtr comm, Bool timing=TRUE);
/** @brief Parallelized loop.
* Calls @a vec[i]=func(i) for every @a i in [0,vec.size()).
* The different calls are distributed using @a processNo (without master).
* The result in @a vec is only valid at master. */
template<typename A, typename T> void forEachProcess(std::vector<A> &vec, T func, const std::vector<UInt> &processNo, CommunicatorPtr comm, Bool timing=TRUE);
} // end namespace Parallel
/***********************************************/
/** @brief Loop with timing.
* @ingroup parallelGroup */
namespace Single
{
/** @brief loop with timing.
* Calls @a func(i) for every @a i in [0,count). */
template<typename T> void forEach(UInt count, T func, Bool timing=TRUE);
} // end namespace Single
/***********************************************/
/***** INLINES ***********************************/
/***********************************************/
template<typename T>
inline void Parallel::send(const T &x, UInt process, CommunicatorPtr comm)
{
if(size(comm)<=1)
return;
std::stringstream stream; //(std::ios::binary);
OutArchiveBinary oa(stream, "", MAX_UINT);
oa<<nameValue("xxx", x);
std::string str = stream.str();
UInt size = str.size();
send(size, process, comm);
send(str.data(), size, process, comm);
}
/***********************************************/
template<typename T>
inline void Parallel::receive(T &x, UInt process, CommunicatorPtr comm)
{
if(size(comm)<=1)
return;
UInt size;
receive(size, process, comm);
Byte *str = new Byte[size+1];
receive(str, size, process, comm);
std::stringstream stream(std::string(str, size)); //, std::ios::binary);
InArchiveBinary ia(stream);
ia>>nameValue("xxx", x);
delete[] str;
}
/***********************************************/
template<typename T>
inline void Parallel::broadCast(T &x, UInt process, CommunicatorPtr comm)
{
if(size(comm)<=1)
return;
if(Parallel::myRank(comm) == process)
{
std::stringstream stream(std::ios_base::out | std::ios::binary);
OutArchiveBinary oa(stream, "", MAX_UINT);
oa<<nameValue("xxx", x);
std::string str = stream.str();
UInt size = str.size();
broadCast(size, process, comm);
broadCast(const_cast<Byte*>(str.data()), size, process, comm);
}
else
{
UInt size;
broadCast(size, process, comm);
Byte *str = new Byte[size+1];
broadCast(str, size, process, comm);
std::stringstream stream(std::string(str, size), std::ios_base::in | std::ios::binary);
InArchiveBinary ia(stream);
ia>>nameValue("xxx", x);
delete[] str;
}
}
/***********************************************/
/***********************************************/
template<typename T>
inline std::vector<UInt> Parallel::forEach(UInt count, T func, CommunicatorPtr comm, Bool timing)
{
return forEachInterval(count, {0, count}, func, comm, timing);
}
/***********************************************/
template<typename A, typename T>
inline std::vector<UInt> Parallel::forEach(std::vector<A> &vec, T func, CommunicatorPtr comm, Bool timing)
{
return forEachInterval(vec, {0, vec.size()}, func, comm, timing);
}
/***********************************************/
template<typename T>
inline std::vector<UInt> Parallel::forEachInterval(UInt count, const std::vector<UInt> &interval, T func, CommunicatorPtr comm, Bool timing)
{
try
{
std::vector<UInt> processNo(count, 0);
// single process version
// ----------------------
if(size(comm) < 3)
{
// single process version
if(isMaster(comm))
{
Log::Timer timer(count, 1, timing);
for(UInt i=0; i<count; i++)
{
timer.loopStep(i);
func(i);
}
timer.loopEnd();
}
return processNo;
}
if(count!=interval.back())
throw(Exception("interval size and count differ"));
// parallel version
// ----------------
if(isMaster(comm))
{
std::vector<UInt> countInInterval(interval.size()-1, 0);
std::vector<UInt> processedInterval(size(comm), NULLINDEX);
// master distributes the loop numbers
UInt process, index;
Log::Timer timer(count, size(comm)-1, timing);
for(UInt i=0; i<count; i++)
{
receive(process, NULLINDEX, comm); // which process needs work?
receive(index, process, comm); // loop numer be computed at process
// can we compute func in the same interval?
UInt idInterval = processedInterval.at(process);
if((idInterval==NULLINDEX) || (countInInterval.at(idInterval) >= interval.at(idInterval+1)-interval.at(idInterval)))
{
// search new interval to compute
UInt maxLeft = 0;
for(UInt k=0; k<countInInterval.size(); k++)
{
UInt left = interval.at(k+1)-interval.at(k)-countInInterval.at(k);
// interval not used?
if((countInInterval.at(k) == 0) && (left>0))
{
idInterval = k;
break;
}
if(left>maxLeft)
{
maxLeft = left;
idInterval = k;
}
}
processedInterval.at(process) = idInterval;
}
UInt id = interval.at(idInterval) + countInInterval.at(idInterval);
countInInterval.at(idInterval)++;
send(id, process, comm); // send new loop number to be computed at process
processNo.at(id) = process;
timer.loopStep(i);
}
// send to all processes the end signal (NULLINDEX)
for(UInt i=1; i<size(comm); i++)
{
receive(process, NULLINDEX, comm); // which process needs work?
receive(index, process, comm); // loop numer be computed at process
send(NULLINDEX, process, comm); // end signal
}
timer.loopEnd();
}
else // clients
{
send(myRank(comm), 0, comm);
send(NULLINDEX, 0, comm); // no results computed yet
for(;;)
{
UInt i;
receive(i,0, comm);
if(i==NULLINDEX)
break;
func(i);
send(myRank(comm), 0, comm);
send(i, 0, comm);
}
}
broadCast(processNo, 0, comm);
return processNo;
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
template<typename A, typename T>
inline std::vector<UInt> Parallel::forEachInterval(std::vector<A> &vec, const std::vector<UInt> &interval, T func, CommunicatorPtr comm, Bool timing)
{
try
{
std::vector<UInt> processNo(vec.size(), 0);
// single process version
// ----------------------
if(size(comm) < 3)
{
// single process version
if(isMaster(comm))
{
Log::Timer timer(vec.size(), 1, timing);
for(UInt i=0; i<vec.size(); i++)
{
timer.loopStep(i);
vec[i] = func(i);
}
timer.loopEnd();
}
return processNo;
}
if(vec.size()!=interval.back())
throw(Exception("interval size and vec.size() differ"));
// parallel version
// ----------------
if(isMaster(comm))
{
std::vector<UInt> countInInterval(interval.size()-1, 0);
std::vector<UInt> processedInterval(size(comm), NULLINDEX);
// master distributes the loop numbers
UInt process, index;
Log::Timer timer(vec.size(), size(comm)-1, timing);
for(UInt i=0; i<vec.size(); i++)
{
receive(process, NULLINDEX, comm); // which process needs work?
receive(index, process, comm); // loop numer be computed at process
if(index!=NULLINDEX)
{
receive(vec[index], process, comm); // receive result
}
// can we compute func in the same interval?
UInt idInterval = processedInterval.at(process);
if((idInterval==NULLINDEX) || (countInInterval.at(idInterval) >= interval.at(idInterval+1)-interval.at(idInterval)))
{
// search new interval to compute
UInt maxLeft = 0;
for(UInt k=0; k<countInInterval.size(); k++)
{
UInt left = interval.at(k+1)-interval.at(k)-countInInterval.at(k);
// interval not used?
if((countInInterval.at(k) == 0) && (left>0))
{
idInterval = k;
break;
}
if(left>maxLeft)
{
maxLeft = left;
idInterval = k;
}
}
processedInterval.at(process) = idInterval;
}
UInt id = interval.at(idInterval) + countInInterval.at(idInterval);
countInInterval.at(idInterval)++;
send(id, process, comm); // send new loop number to be computed at process
processNo.at(id) = process;
timer.loopStep(i);
}
// send to all processes the end signal (NULLINDEX)
for(UInt i=1; i<size(comm); i++)
{
receive(process, NULLINDEX, comm); // which process needs work?
receive(index, process, comm); // loop numer be computed at process
if(index!=NULLINDEX)
receive(vec[index], process, comm); // receive result
send(NULLINDEX, process, comm); // end signal
}
timer.loopEnd();
}
else // clients
{
send(myRank(comm), 0, comm);
send(NULLINDEX, 0, comm); // no results computed yet
for(;;)
{
UInt i;
receive(i,0, comm);
if(i==NULLINDEX)
break;
vec[i] = func(i);
send(myRank(comm), 0, comm);
send(i, 0, comm);
send(vec[i], 0, comm);
}
}
broadCast(processNo, 0, comm);
return processNo;
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
template<typename T>
inline void Parallel::forEachProcess(UInt count, T func, const std::vector<UInt> &processNo, CommunicatorPtr comm, Bool timing)
{
try
{
std::set<UInt> procs;
for(UInt p : processNo)
procs.insert(p);
UInt idx;
Log::Timer timer(count, procs.size(), timing);
for(UInt i=0; i<count; i++)
{
timer.loopStep(i);
if(myRank(comm) == processNo.at(i))
{
func(i);
if(!isMaster(comm)) send(i, 0, comm);
} // if(arcs.at(i))
else if(isMaster(comm))
{
receive(idx, NULLINDEX, comm);
}
} // for(i)
barrier(comm);
timer.loopEnd();
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
template<typename A, typename T>
inline void Parallel::forEachProcess(std::vector<A> &vec, T func, const std::vector<UInt> &processNo, CommunicatorPtr comm, Bool timing)
{
try
{
std::set<UInt> procs;
for(UInt p : processNo)
procs.insert(p);
UInt idx = 0;
Log::Timer timer(vec.size(), procs.size(), timing);
for(UInt i=0; i<vec.size(); i++)
{
timer.loopStep(i);
if(myRank(comm) == processNo.at(i))
{
vec[i] = func(i);
if(!isMaster(comm)) send(i, 0, comm);
if(!isMaster(comm)) send(vec[i], 0, comm);
} // if(arcs.at(i))
else if(isMaster(comm))
{
receive(idx, NULLINDEX, comm);
receive(vec[idx], processNo.at(idx), comm);
}
} // for(i)
barrier(comm);
timer.loopEnd();
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
/***********************************************/
template<typename T>
inline void Single::forEach(UInt count, T func, Bool timing)
{
{
try
{
Log::Timer timer(count, 1, timing);
for(UInt i=0; i<count; i++)
{
timer.loopStep(i);
func(i);
} // for(i)
timer.loopEnd();
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
}
/***********************************************/
#endif /* __GROOPS__ */
|