38#ifndef MADNESS_WORLD_FUTURE_H__INCLUDED
39#define MADNESS_WORLD_FUTURE_H__INCLUDED
58 template <
typename T>
class Future;
67 std::ostream&
operator<<(std::ostream& out,
const Future<T>&
f);
76 friend std::ostream& operator<< <T>(std::ostream& out,
const Future<T>&
f);
135 input_arch &
const_cast<T&
>(pimpl->
t);
164 while (!as.empty()) {
166 as.top()->set(value);
170 while (!cb.empty()) {
187 f->set(
const_cast<T&
>(
t));
250 template <
typename U>
274 input_arch &
const_cast<T&
>(
t);
291 return *
const_cast<T*
>(&
t);
304 const T&
get(
bool dowork =
true)
const {
307 return *
const_cast<const T*
>(&
t);
344 print(
"Future: uninvoked callbacks being destroyed?",
assigned);
348 print(
"Future: uninvoked assignment being destroyed?",
assigned);
368 template <
typename T>
371 friend std::ostream& operator<< <T>(std::ostream& out,
const Future<T>&
f);
376 std::shared_ptr< FutureImpl<T> >
f;
431 input_arch & (*value);
517 std::shared_ptr< FutureImpl<T> > ff =
f;
518 std::shared_ptr< FutureImpl<T> > of = other.
f;
522 of->add_to_assignments(ff);
535 std::shared_ptr< FutureImpl<T> > ff =
f;
545 std::shared_ptr< FutureImpl<T> > ff =
f;
546 ff->set(std::move(
value));
556 std::shared_ptr< FutureImpl<T> > ff =
f;
570 inline T&
get(
bool dowork =
true) & {
572 return (f ?
f->get(dowork) : *
value);
584 inline const T&
get(
bool dowork =
true) const & {
586 return (f ?
f->get(dowork) : *
value);
598 inline T get(
bool dowork =
true) && {
610 auto &value_ref =
f->get(dowork);
614 auto fcopy = std::atomic_exchange(&
f, {});
616 if (fcopy.use_count() == 1)
617 return std::move(value_ref);
621 return std::move(*
value);
628 return (
f ?
f->probe() : bool(
value));
634 inline operator T&() & {
635 return static_cast<Future&
>(*this).
get();
641 inline operator const T&()
const& {
642 return static_cast<const Future&
>(*this).
get();
648 inline operator T() && {
649 return static_cast<Future&&
>(*this).
get();
674 return f->remote_ref;
685 return (
f &&
f->is_local()) ||
value;
709 f->register_callback(callback);
718 template <
typename T>
806 template <
typename T>
810 typedef typename std::vector< Future<T> >
vectorT;
823 for (
int i=0; i<(int)
v.size(); ++i) {
887 template <
typename T>
899 template <
class Archive,
typename T>
906 template <
typename U = T,
typename A = Archive>
908 std::enable_if_t<is_serializable_v<A, U>,
void>
921 template <
class Archive,
typename T>
928 template <
typename U = T,
typename A = Archive>
930 std::enable_if_t<is_serializable_v<A, U>,
void>
load(
const Archive& ar,
Future<T>&
f) {
943 template <
class Archive>
958 template <
class Archive>
973 template <
class Archive,
typename T>
983 for(
typename std::vector<
Future<T> >::const_iterator it =
v.begin(); it !=
v.end(); ++it) {
995 template <
class Archive,
typename T>
1010 for(
typename std::vector<
Future<T> >::iterator it =
v.begin(); it <
v.end(); ++it, --n) {
1029 template <
typename T>
1030 std::ostream&
operator<<(std::ostream& out,
const Future<T>&
f);
1039 std::ostream&
operator<<(std::ostream& out,
const Future<void>&
f);
1049 template <
typename T>
1052 if constexpr (is_ostreammable_v<T>) {
1056 else if (
f.is_remote()) out <<
f.f->remote_ref;
1057 else if (
f.f) out <<
"<unassigned refcnt=" <<
f.f.use_count() <<
">";
1058 else out <<
"<unassigned>";
#define MAD_ARCHIVE_DEBUG(s)
Macro for helping debug archive tools.
Definition archive.h:77
Disables default copy constructor and assignment operators.
Definition nodefaults.h:49
World active message that extends an RMI message.
Definition worldam.h:80
The class used for callbacks (e.g., dependency tracking).
Definition dependency_interface.h:61
virtual void notify()=0
Invoked by the callback to notify when a dependency is satisfied.
Provides an interface for tracking dependencies.
Definition dependency_interface.h:100
bool probe() const
Returns true if ndepend == 0 (no unsatisfied dependencies).
Definition dependency_interface.h:178
void register_callback(CallbackInterface *callback)
Registers a callback that will be executed when ndepend==0; immediately invoked if ndepend==0.
Definition dependency_interface.h:201
Implements the functionality of futures.
Definition future.h:74
FutureImpl()
Constructor that uses a local unassigned value.
Definition future.h:199
static void set_handler(const AmArg &arg)
AM handler for remote set operations.
Definition future.h:106
const T & get(bool dowork=true) const
Gets/forces the value, waiting if necessary.
Definition future.h:304
static const int MAXCALLBACKS
Definition future.h:81
volatile T t
The future data.
Definition future.h:100
FutureImpl(const RemoteReference< FutureImpl< T > > &remote_ref)
Constructor that uses a wrapper for a remote future.
Definition future.h:213
void set_assigned(const T &value)
Definition future.h:149
void add_to_assignments(const std::shared_ptr< FutureImpl< T > > f)
Pass by value with implied copy to manage lifetime of f.
Definition future.h:184
void register_callback(CallbackInterface *callback)
Registers a function to be invoked when future is assigned.
Definition future.h:238
Stack< std::shared_ptr< FutureImpl< T > >, MAXCALLBACKS > assignmentT
Assignment type.
Definition future.h:84
Stack< CallbackInterface *, MAXCALLBACKS > callbackT
Callback type.
Definition future.h:83
void set(U &&value)
Sets the value of the future (assignment).
Definition future.h:251
void set(const archive::BufferInputArchive &input_arch)
Definition future.h:271
virtual ~FutureImpl()
Destructor.
Definition future.h:342
bool replace_with(FutureImpl< T > *f)
Definition future.h:324
RemoteReference< FutureImpl< T > > remote_ref
Reference to a remote future pimpl.
Definition future.h:98
bool probe() const
Checks if the value has been assigned.
Definition future.h:226
T & get(bool dowork=true)
Gets/forces the value, waiting if necessary.
Definition future.h:288
volatile callbackT callbacks
Definition future.h:88
volatile assignmentT assignments
Definition future.h:92
std::atomic< bool > assigned
A flag indicating if the future has been set.
Definition future.h:95
bool is_local() const
Definition future.h:314
vectorT v
The vector of futures.
Definition future.h:813
bool probe() const
Check if all of the futures in the vector have been assigned.
Definition future.h:874
const vectorT & get() const
Access the const vector of futures.
Definition future.h:850
Future()
Definition future.h:816
Future(const vectorT &v)
Definition future.h:822
vectorT & get()
Access the vector of futures.
Definition future.h:842
Future(const archive::BufferInputArchive &input_arch)
Definition future.h:834
std::vector< Future< T > > vectorT
Alias for a vector of futures.
Definition future.h:810
static const Future< void > value
Definition future.h:738
static void set(const Future< void > &f)
Set the future from another void future.
Definition future.h:780
static void set()
Set the future.
Definition future.h:786
Future()
Definition future.h:750
static remote_refT remote_ref(World &world)
Definition future.h:746
Future(const archive::BufferInputArchive &input_arch)
Construct from an input archive.
Definition future.h:763
Future(const RemoteReference< FutureImpl< void > > &remote_ref)
Definition future.h:757
RemoteReference< FutureImpl< void > > remote_refT
Definition future.h:735
static bool probe()
Check if this future has been assigned.
Definition future.h:792
Future< void > & operator=(const Future< void > &other)
Assignment operator.
Definition future.h:772
A future is a possibly yet unevaluated value.
Definition future.h:369
T & get(bool dowork=true) &
Gets the value, waiting if necessary.
Definition future.h:570
T get(bool dowork=true) &&
Gets the value, waiting if necessary.
Definition future.h:598
Future(const T &t)
Makes an assigned future.
Definition future.h:406
T *const value
Pointer to buffer when it holds a T object.
Definition future.h:378
remote_refT remote_ref(World &world) const
Returns a structure used to pass references to another process.
Definition future.h:671
Future()
Makes an unassigned future.
Definition future.h:397
const T & get(bool dowork=true) const &
Gets the value, waiting if necessary.
Definition future.h:584
void set(const Future< T > &other)
A.set(B), where A and B are futures ensures A has/will have the same value as B.
Definition future.h:504
void set(const T &value)
Assigns the value.
Definition future.h:533
void set(const archive::BufferInputArchive &input_arch)
Assigns the value.
Definition future.h:554
Future(const archive::BufferInputArchive &input_arch)
Makes an assigned future from an input archive.
Definition future.h:428
~Future()
Destructor.
Definition future.h:449
static const Future< T > default_initializer()
See "Gotchas" on Futures about why this exists and how to use it.
Definition future.h:458
Future(const dddd &blah)
Definition future.h:390
char buffer[sizeof(T)]
Buffer to hold a single T object.
Definition future.h:377
RemoteReference< FutureImpl< T > > remote_refT
Definition future.h:394
bool is_local() const
Checks the future remoteness trait.
Definition future.h:684
Future(const remote_refT &remote_ref)
Makes a future wrapping a remote reference.
Definition future.h:415
bool is_remote() const
Checks the future remoteness trait.
Definition future.h:693
void register_callback(CallbackInterface *callback)
Registers an object to be called when future is assigned.
Definition future.h:704
Future(const Future< T > &other)
Shallow copy constructor.
Definition future.h:438
std::shared_ptr< FutureImpl< T > > f
Pointer to the implementation object.
Definition future.h:376
void set(T &&value)
Assigns the value.
Definition future.h:543
Future< T > & operator=(const Future< T > &other)
Shallow assignment operator.
Definition future.h:475
bool is_default_initialized() const
Check if the future is default initialized.
Definition future.h:466
bool probe() const
Check whether this future has been assigned.
Definition future.h:627
Simple structure used to manage references/pointers to remote instances.
Definition worldref.h:395
void reset()
Release this reference.
Definition worldref.h:478
pointerT get() const
Reference pointer accessor.
Definition worldref.h:497
Mutex that is applied/released at start/end of a scope.
Definition worldmutex.h:239
Spinlock using pthread spinlock operations.
Definition worldmutex.h:253
Dynamically sized Stack with small stack size optimization.
Definition stack.h:154
void send(ProcessID dest, am_handlerT op, const AmArg *arg, const int attr=RMI::ATTR_ORDERED)
Sends a managed non-blocking active message.
Definition worldam.h:278
A parallel world class.
Definition world.h:132
static void await(SafeMPI::Request &request, bool dowork=true)
Wait for a MPI request to complete.
Definition world.h:534
WorldAmInterface & am
AM interface.
Definition world.h:205
Defines DependencyInterface and CallbackInterface.
auto T(World &world, response_space &f) -> response_space
Definition global_functions.cc:28
Tensor< typename Tensor< T >::scalar_type > arg(const Tensor< T > &t)
Return a new tensor holding the argument of each element of t (complex types only)
Definition tensor.h:2503
static const double v
Definition hatom_sf_dirac.cc:20
#define MADNESS_PRAGMA_CLANG(x)
Definition madness_config.h:200
#define MADNESS_CHECK(condition)
Check a condition — even in a release build the condition is always evaluated so it can have side eff...
Definition madness_exception.h:182
#define MADNESS_EXCEPTION(msg, value)
Macro for throwing a MADNESS exception.
Definition madness_exception.h:119
#define MADNESS_ASSERT(condition)
Assert a condition that should be free of side-effects since in release builds this might be a no-op.
Definition madness_exception.h:134
Namespace for all elements and tools of MADNESS.
Definition DFParameters.h:10
std::ostream & operator<<(std::ostream &os, const particle< PDIM > &p)
Definition lowrankfunction.h:401
AmArg * new_am_arg(const argT &... args)
Convenience template for serializing arguments into a new AmArg.
Definition worldam.h:194
static double pop(std::vector< double > &v)
Definition SCF.cc:115
void print(const T &t, const Ts &... ts)
Print items to std::cout (items separated by spaces) and terminate with a new line.
Definition print.h:226
NDIM & f
Definition mra.h:2528
std::vector< Future< T > > future_vector_factory(std::size_t n)
Factory for a vectors of futures.
Definition future.h:888
Implement Stack for a fixed-size stack container.
static std::enable_if_t< is_serializable_v< A, U >, void > load(const Archive &ar, Future< T > &f)
Read into an unassigned future.
Definition future.h:930
static void load(const Archive &ar, const Future< void > &f)
Read into an unassigned void future.
Definition future.h:965
static void load(const Archive &ar, std::vector< Future< T > > &v)
Read into a vector of unassigned futures.
Definition future.h:1002
Default load of an object via serialize(ar, t).
Definition archive.h:667
static std::enable_if_t< is_serializable_v< A, U >, void > store(const Archive &ar, const Future< T > &f)
Store the assigned future in an archive.
Definition future.h:909
static void store(const Archive &ar, const Future< void > &f)
Store the assigned void future in the archive (do nothing).
Definition future.h:950
static void store(const Archive &ar, const std::vector< Future< T > > &v)
Store the vector of assigned futures in the archive.
Definition future.h:980
Default store of an object via serialize(ar, t).
Definition archive.h:612
double fred(const coordT &r)
Definition tdse4.cc:368
Declares the World class for the parallel runtime environment.
Implements RemoteReference which is for internal use.
int ProcessID
Used to clearly identify process number/rank.
Definition worldtypes.h:43