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);
119#pragma clang diagnostic push
120#pragma clang diagnostic ignored "-Wuninitialized-const-reference"
125#pragma clang diagnostic pop
139 input_arch &
const_cast<T&
>(pimpl->
t);
168 while (!as.empty()) {
170 as.top()->set(value);
174 while (!cb.empty()) {
191 f->set(
const_cast<T&
>(
t));
254 template <
typename U>
278 input_arch &
const_cast<T&
>(
t);
295 return *
const_cast<T*
>(&
t);
308 const T&
get(
bool dowork =
true)
const {
311 return *
const_cast<const T*
>(&
t);
348 print(
"Future: uninvoked callbacks being destroyed?",
assigned);
352 print(
"Future: uninvoked assignment being destroyed?",
assigned);
372 template <
typename T>
375 friend std::ostream& operator<< <T>(std::ostream& out,
const Future<T>&
f);
380 std::shared_ptr< FutureImpl<T> >
f;
435 input_arch & (*value);
521 std::shared_ptr< FutureImpl<T> > ff =
f;
522 std::shared_ptr< FutureImpl<T> > of = other.
f;
526 of->add_to_assignments(ff);
539 std::shared_ptr< FutureImpl<T> > ff =
f;
549 std::shared_ptr< FutureImpl<T> > ff =
f;
550 ff->set(std::move(
value));
560 std::shared_ptr< FutureImpl<T> > ff =
f;
574 inline T&
get(
bool dowork =
true) & {
576 return (f ?
f->get(dowork) : *
value);
588 inline const T&
get(
bool dowork =
true) const & {
590 return (f ?
f->get(dowork) : *
value);
602 inline T get(
bool dowork =
true) && {
614 auto &value_ref =
f->get(dowork);
618 auto fcopy = std::atomic_exchange(&
f, {});
620 if (fcopy.use_count() == 1)
621 return std::move(value_ref);
625 return std::move(*
value);
632 return (
f ?
f->probe() : bool(
value));
638 inline operator T&() & {
639 return static_cast<Future&
>(*this).
get();
645 inline operator const T&()
const& {
646 return static_cast<const Future&
>(*this).
get();
652 inline operator T() && {
653 return static_cast<Future&&
>(*this).
get();
678 return f->remote_ref;
689 return (
f &&
f->is_local()) ||
value;
713 f->register_callback(callback);
722 template <
typename T>
810 template <
typename T>
814 typedef typename std::vector< Future<T> >
vectorT;
827 for (
int i=0; i<(int)
v.size(); ++i) {
891 template <
typename T>
903 template <
class Archive,
typename T>
910 template <
typename U = T,
typename A = Archive>
912 std::enable_if_t<is_serializable_v<A, U>,
void>
925 template <
class Archive,
typename T>
932 template <
typename U = T,
typename A = Archive>
934 std::enable_if_t<is_serializable_v<A, U>,
void>
load(
const Archive& ar,
Future<T>&
f) {
947 template <
class Archive>
962 template <
class Archive>
977 template <
class Archive,
typename T>
987 for(
typename std::vector<
Future<T> >::const_iterator it =
v.begin(); it !=
v.end(); ++it) {
999 template <
class Archive,
typename T>
1014 for(
typename std::vector<
Future<T> >::iterator it =
v.begin(); it <
v.end(); ++it, --n) {
1033 template <
typename T>
1034 std::ostream&
operator<<(std::ostream& out,
const Future<T>&
f);
1043 std::ostream&
operator<<(std::ostream& out,
const Future<void>&
f);
1053 template <
typename T>
1056 if constexpr (is_ostreammable_v<T>) {
1060 else if (
f.is_remote()) out <<
f.f->remote_ref;
1061 else if (
f.f) out <<
"<unassigned refcnt=" <<
f.f.use_count() <<
">";
1062 else out <<
"<unassigned>";
#define MAD_ARCHIVE_DEBUG(s)
Macro for helping debug archive tools.
Definition archive.h:76
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:203
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:308
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:217
void set_assigned(const T &value)
Definition future.h:153
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:188
void register_callback(CallbackInterface *callback)
Registers a function to be invoked when future is assigned.
Definition future.h:242
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:255
void set(const archive::BufferInputArchive &input_arch)
Definition future.h:275
virtual ~FutureImpl()
Destructor.
Definition future.h:346
bool replace_with(FutureImpl< T > *f)
Definition future.h:328
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:230
T & get(bool dowork=true)
Gets/forces the value, waiting if necessary.
Definition future.h:292
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:318
vectorT v
The vector of futures.
Definition future.h:817
bool probe() const
Check if all of the futures in the vector have been assigned.
Definition future.h:878
const vectorT & get() const
Access the const vector of futures.
Definition future.h:854
Future()
Definition future.h:820
Future(const vectorT &v)
Definition future.h:826
vectorT & get()
Access the vector of futures.
Definition future.h:846
Future(const archive::BufferInputArchive &input_arch)
Definition future.h:838
std::vector< Future< T > > vectorT
Alias for a vector of futures.
Definition future.h:814
static const Future< void > value
Definition future.h:742
static void set(const Future< void > &f)
Set the future from another void future.
Definition future.h:784
static void set()
Set the future.
Definition future.h:790
Future()
Definition future.h:754
static remote_refT remote_ref(World &world)
Definition future.h:750
Future(const archive::BufferInputArchive &input_arch)
Construct from an input archive.
Definition future.h:767
Future(const RemoteReference< FutureImpl< void > > &remote_ref)
Definition future.h:761
RemoteReference< FutureImpl< void > > remote_refT
Definition future.h:739
static bool probe()
Check if this future has been assigned.
Definition future.h:796
Future< void > & operator=(const Future< void > &other)
Assignment operator.
Definition future.h:776
A future is a possibly yet unevaluated value.
Definition future.h:373
T & get(bool dowork=true) &
Gets the value, waiting if necessary.
Definition future.h:574
T get(bool dowork=true) &&
Gets the value, waiting if necessary.
Definition future.h:602
Future(const T &t)
Makes an assigned future.
Definition future.h:410
T *const value
Pointer to buffer when it holds a T object.
Definition future.h:382
remote_refT remote_ref(World &world) const
Returns a structure used to pass references to another process.
Definition future.h:675
Future()
Makes an unassigned future.
Definition future.h:401
const T & get(bool dowork=true) const &
Gets the value, waiting if necessary.
Definition future.h:588
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:508
void set(const T &value)
Assigns the value.
Definition future.h:537
void set(const archive::BufferInputArchive &input_arch)
Assigns the value.
Definition future.h:558
Future(const archive::BufferInputArchive &input_arch)
Makes an assigned future from an input archive.
Definition future.h:432
~Future()
Destructor.
Definition future.h:453
static const Future< T > default_initializer()
See "Gotchas" on Futures about why this exists and how to use it.
Definition future.h:462
Future(const dddd &blah)
Definition future.h:394
char buffer[sizeof(T)]
Buffer to hold a single T object.
Definition future.h:381
RemoteReference< FutureImpl< T > > remote_refT
Definition future.h:398
bool is_local() const
Checks the future remoteness trait.
Definition future.h:688
Future(const remote_refT &remote_ref)
Makes a future wrapping a remote reference.
Definition future.h:419
bool is_remote() const
Checks the future remoteness trait.
Definition future.h:697
void register_callback(CallbackInterface *callback)
Registers an object to be called when future is assigned.
Definition future.h:708
Future(const Future< T > &other)
Shallow copy constructor.
Definition future.h:442
std::shared_ptr< FutureImpl< T > > f
Pointer to the implementation object.
Definition future.h:380
void set(T &&value)
Assigns the value.
Definition future.h:547
Future< T > & operator=(const Future< T > &other)
Shallow assignment operator.
Definition future.h:479
bool is_default_initialized() const
Check if the future is default initialized.
Definition future.h:470
bool probe() const
Check whether this future has been assigned.
Definition future.h:631
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:516
WorldAmInterface & am
AM interface.
Definition world.h:203
Defines DependencyInterface and CallbackInterface.
auto T(World &world, response_space &f) -> response_space
Definition global_functions.cc:34
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:2502
static const double v
Definition hatom_sf_dirac.cc:20
#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:397
AmArg * new_am_arg(const argT &... args)
Convenience template for serializing arguments into a new AmArg.
Definition worldam.h:194
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:225
NDIM & f
Definition mra.h:2416
std::vector< Future< T > > future_vector_factory(std::size_t n)
Factory for a vectors of futures.
Definition future.h:892
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:934
static void load(const Archive &ar, const Future< void > &f)
Read into an unassigned void future.
Definition future.h:969
static void load(const Archive &ar, std::vector< Future< T > > &v)
Read into a vector of unassigned futures.
Definition future.h:1006
Default load of an object via serialize(ar, t).
Definition archive.h:666
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:913
static void store(const Archive &ar, const Future< void > &f)
Store the assigned void future in the archive (do nothing).
Definition future.h:954
static void store(const Archive &ar, const std::vector< Future< T > > &v)
Store the vector of assigned futures in the archive.
Definition future.h:984
Default store of an object via serialize(ar, t).
Definition archive.h:611
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