ALPSCore reference
stop_callback.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1998-2018 ALPS Collaboration. See COPYRIGHT.TXT
3  * All rights reserved. Use is subject to license terms. See LICENSE.TXT
4  * For use in publications, see ACKNOWLEDGE.TXT
5  */
6 
8 
11 
12 namespace alps {
13 
14  stop_callback::stop_callback(std::size_t timelimit)
15  : limit(timelimit)
16  , start(clock_type::now_time())
17  {}
18 
19 #ifdef ALPS_HAVE_MPI
20  stop_callback::stop_callback(alps::mpi::communicator const & cm, std::size_t timelimit)
21  : limit(timelimit), start(clock_type::now_time()), comm(cm)
22  {}
23 #endif
24 
26  time_point_type now(clock_type::now_time());
27 #ifdef ALPS_HAVE_MPI
28  if (comm) {
29  bool to_stop;
30  if (comm->rank() == 0) {
31  to_stop = !signals.empty() || (limit > 0 && clock_type::time_diff(now, start) >= limit);
32  }
33  broadcast(*comm, to_stop, 0);
34  return to_stop;
35  } else
36 #endif
37  return !signals.empty() || (limit > 0 && clock_type::time_diff(now, start) >= limit);
38  }
39 
40 
42  : limit(timelimit)
43  , start(clock_type::now_time())
44  {}
45 
47  time_point_type now(clock_type::now_time());
48  return (limit > 0 && clock_type::time_diff(now, start) >= limit);
49  }
50 }
bool operator()() const
Returns true if it's time to stop (time is up or signal is received)
void broadcast(C const &c, P &p, int r=0)
Definition: api.hpp:56
Encapsulation of an MPI communicator and some communicator-related operations.
Definition: mpi.hpp:111
bool operator()() const
Returns true if time is up.
simple_time_callback(std::size_t timelimit)
Initializes the functor with the desired time duration.
stop_callback(std::size_t timelimit)
Initializes the functor with the desired time duration.
bool empty() const
Definition: signal.cpp:42