| 1 |
/* |
| 2 |
* (C) Copyright 2013 ECMWF. |
| 3 |
* |
| 4 |
* This software is licensed under the terms of the Apache Licence Version 2.0 |
| 5 |
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
| 6 |
* In applying this licence, ECMWF does not waive the privileges and immunities |
| 7 |
* granted to it by virtue of its status as an intergovernmental organisation |
| 8 |
* nor does it submit to any jurisdiction. |
| 9 |
*/ |
| 10 |
|
| 11 |
#include "Barriers.h" |
| 12 |
|
| 13 |
#include <sstream> |
| 14 |
|
| 15 |
#include "atlas/library/Library.h" |
| 16 |
#include "atlas/parallel/mpi/mpi.h" |
| 17 |
#include "atlas/parallel/omp/omp.h" |
| 18 |
#include "atlas/runtime/trace/StopWatch.h" |
| 19 |
|
| 20 |
//----------------------------------------------------------------------------------------------------------- |
| 21 |
|
| 22 |
namespace atlas { |
| 23 |
namespace runtime { |
| 24 |
namespace trace { |
| 25 |
|
| 26 |
class BarriersState { |
| 27 |
private: |
| 28 |
BarriersState() { barriers_ = atlas::Library::instance().traceBarriers(); } |
| 29 |
bool barriers_; |
| 30 |
StopWatch stopwatch_; |
| 31 |
|
| 32 |
public: |
| 33 |
BarriersState(BarriersState const&) = delete; |
| 34 |
void operator=(BarriersState const&) = delete; |
| 35 |
static BarriersState& instance() { |
| 36 |
static BarriersState state; |
| 37 |
return state; |
| 38 |
} |
| 39 |
operator bool() const { return barriers_; } |
| 40 |
void set(bool state) { barriers_ = state; } |
| 41 |
StopWatch& stopwatch() { return stopwatch_; } |
| 42 |
|
| 43 |
std::string report() const { |
| 44 |
std::stringstream out; |
| 45 |
double time = stopwatch_.elapsed(); |
| 46 |
if (time) { |
| 47 |
out << "Total time spent in mpi barriers due to load imbalance : " << time << "s" << std::endl; |
| 48 |
} |
| 49 |
return out.str(); |
| 50 |
} |
| 51 |
}; |
| 52 |
|
| 53 |
Barriers::Barriers(bool state): previous_state_(BarriersState::instance()) { |
| 54 |
BarriersState::instance().set(state); |
| 55 |
} |
| 56 |
|
| 57 |
Barriers::~Barriers() { |
| 58 |
restore(); |
| 59 |
} |
| 60 |
|
| 61 |
void Barriers::restore() { |
| 62 |
BarriersState::instance().set(previous_state_); |
| 63 |
} |
| 64 |
|
| 65 |
bool Barriers::state() { |
| 66 |
return BarriersState::instance() && (atlas_omp_get_num_threads() <= 1); |
| 67 |
} |
| 68 |
|
| 69 |
void Barriers::execute() { |
| 70 |
if (state()) { |
| 71 |
BarriersState::instance().stopwatch().start(); |
| 72 |
mpi::comm().barrier(); |
| 73 |
BarriersState::instance().stopwatch().stop(); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
double Barriers::time() { |
| 78 |
return BarriersState::instance().stopwatch().elapsed(); |
| 79 |
} |
| 80 |
|
| 81 |
double NoBarriers::time() { |
| 82 |
return BarriersState::instance().stopwatch().elapsed(); |
| 83 |
} |
| 84 |
|
| 85 |
std::string Barriers::report() { |
| 86 |
return BarriersState::instance().report(); |
| 87 |
} |
| 88 |
|
| 89 |
std::string NoBarriers::report() { |
| 90 |
return BarriersState::instance().report(); |
| 91 |
} |
| 92 |
|
| 93 |
} // namespace trace |
| 94 |
} // namespace runtime |
| 95 |
} // namespace atlas |