38#ifndef MADNESS_WORLD_FUTURE_H__INCLUDED
39#define MADNESS_WORLD_FUTURE_H__INCLUDED
59 template <
typename T>
class Future;
68 std::ostream&
operator<<(std::ostream& out,
const Future<T>&
f);
77 friend std::ostream& operator<< <T>(std::ostream& out,
const Future<T>&
f);
136 input_arch &
const_cast<T&
>(pimpl->
t);
165 while (!as.empty()) {
167 as.top()->set(value);
171 while (!cb.empty()) {
188 f->set(
const_cast<T&
>(
t));
251 template <
typename U>
275 input_arch &
const_cast<T&
>(
t);
292 return *
const_cast<T*
>(&
t);
305 const T&
get(
bool dowork =
true)
const {
308 return *
const_cast<const T*
>(&
t);
345 print(
"Future: uninvoked callbacks being destroyed?",
assigned);
349 print(
"Future: uninvoked assignment being destroyed?",
assigned);
369 template <
typename T>
372 friend std::ostream& operator<< <T>(std::ostream& out,
const Future<T>&
f);
432 input_arch & (*value);
518 std::shared_ptr< FutureImpl<T> > ff =
f;
519 std::shared_ptr< FutureImpl<T> > of = other.
f;
523 of->add_to_assignments(ff);
536 std::shared_ptr< FutureImpl<T> > ff =
f;
546 std::shared_ptr< FutureImpl<T> > ff =
f;
547 ff->set(std::move(
value));
557 std::shared_ptr< FutureImpl<T> > ff =
f;
571 inline T&
get(
bool dowork =
true) & {
573 return (f ?
f->get(dowork) : *
value);
585 inline const T&
get(
bool dowork =
true) const & {
587 return (f ?
f->get(dowork) : *
value);
599 inline T get(
bool dowork =
true) && {
611 auto &value_ref =
f->get(dowork);
615 auto fcopy =
f.exchange({});
617 if (fcopy.use_count() == 1)
618 return std::move(value_ref);
622 return std::move(*
value);
629 return (
f ?
f->probe() : bool(
value));
635 inline operator T&() & {
636 return static_cast<Future&
>(*this).
get();
642 inline operator const T&()
const& {
643 return static_cast<const Future&
>(*this).
get();
649 inline operator T() && {
650 return static_cast<Future&&
>(*this).
get();
675 return f->remote_ref;
686 return (
f &&
f->is_local()) ||
value;
710 f->register_callback(callback);
719 template <
typename T>
807 template <
typename T>
811 typedef typename std::vector< Future<T> >
vectorT;
824 for (
int i=0; i<(int)
v.size(); ++i) {
888 template <
typename T>
900 template <
class Archive,
typename T>
907 template <
typename U = T,
typename A = Archive>
909 std::enable_if_t<is_serializable_v<A, U>,
void>
922 template <
class Archive,
typename T>
929 template <
typename U = T,
typename A = Archive>
931 std::enable_if_t<is_serializable_v<A, U>,
void>
load(
const Archive& ar,
Future<T>&
f) {
944 template <
class Archive>
959 template <
class Archive>
974 template <
class Archive,
typename T>
984 for(
typename std::vector<
Future<T> >::const_iterator it =
v.begin(); it !=
v.end(); ++it) {
996 template <
class Archive,
typename T>
1011 for(
typename std::vector<
Future<T> >::iterator it =
v.begin(); it <
v.end(); ++it, --n) {
1030 template <
typename T>
1031 std::ostream&
operator<<(std::ostream& out,
const Future<T>&
f);
1040 std::ostream&
operator<<(std::ostream& out,
const Future<void>&
f);
1050 template <
typename T>
1053 if constexpr (is_ostreammable_v<T>) {
1057 else if (
f.is_remote()) out <<
f.f->remote_ref;
1058 else if (
f.f) out <<
"<unassigned refcnt=" <<
f.f.use_count() <<
">";
1059 else out <<
"<unassigned>";
#define MAD_ARCHIVE_DEBUG(s)
Macro for helping debug archive tools.
Definition archive.h:77
Defines atomic_shared_ptr — a std::shared_ptr-compatible wrapper backed by atomic storage.
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:75
FutureImpl()
Constructor that uses a local unassigned value.
Definition future.h:200
static void set_handler(const AmArg &arg)
AM handler for remote set operations.
Definition future.h:107
const T & get(bool dowork=true) const
Gets/forces the value, waiting if necessary.
Definition future.h:305
static const int MAXCALLBACKS
Definition future.h:82
volatile T t
The future data.
Definition future.h:101
FutureImpl(const RemoteReference< FutureImpl< T > > &remote_ref)
Constructor that uses a wrapper for a remote future.
Definition future.h:214
void set_assigned(const T &value)
Definition future.h:150
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:185
void register_callback(CallbackInterface *callback)
Registers a function to be invoked when future is assigned.
Definition future.h:239
Stack< std::shared_ptr< FutureImpl< T > >, MAXCALLBACKS > assignmentT
Assignment type.
Definition future.h:85
Stack< CallbackInterface *, MAXCALLBACKS > callbackT
Callback type.
Definition future.h:84
void set(U &&value)
Sets the value of the future (assignment).
Definition future.h:252
void set(const archive::BufferInputArchive &input_arch)
Definition future.h:272
virtual ~FutureImpl()
Destructor.
Definition future.h:343
bool replace_with(FutureImpl< T > *f)
Definition future.h:325
RemoteReference< FutureImpl< T > > remote_ref
Reference to a remote future pimpl.
Definition future.h:99
bool probe() const
Checks if the value has been assigned.
Definition future.h:227
T & get(bool dowork=true)
Gets/forces the value, waiting if necessary.
Definition future.h:289
volatile callbackT callbacks
Definition future.h:89
volatile assignmentT assignments
Definition future.h:93
std::atomic< bool > assigned
A flag indicating if the future has been set.
Definition future.h:96
bool is_local() const
Definition future.h:315
vectorT v
The vector of futures.
Definition future.h:814
bool probe() const
Check if all of the futures in the vector have been assigned.
Definition future.h:875
const vectorT & get() const
Access the const vector of futures.
Definition future.h:851
Future()
Definition future.h:817
Future(const vectorT &v)
Definition future.h:823
vectorT & get()
Access the vector of futures.
Definition future.h:843
Future(const archive::BufferInputArchive &input_arch)
Definition future.h:835
std::vector< Future< T > > vectorT
Alias for a vector of futures.
Definition future.h:811
static const Future< void > value
Definition future.h:739
static void set(const Future< void > &f)
Set the future from another void future.
Definition future.h:781
static void set()
Set the future.
Definition future.h:787
Future()
Definition future.h:751
static remote_refT remote_ref(World &world)
Definition future.h:747
Future(const archive::BufferInputArchive &input_arch)
Construct from an input archive.
Definition future.h:764
Future(const RemoteReference< FutureImpl< void > > &remote_ref)
Definition future.h:758
RemoteReference< FutureImpl< void > > remote_refT
Definition future.h:736
static bool probe()
Check if this future has been assigned.
Definition future.h:793
Future< void > & operator=(const Future< void > &other)
Assignment operator.
Definition future.h:773
A future is a possibly yet unevaluated value.
Definition future.h:370
T & get(bool dowork=true) &
Gets the value, waiting if necessary.
Definition future.h:571
T get(bool dowork=true) &&
Gets the value, waiting if necessary.
Definition future.h:599
Future(const T &t)
Makes an assigned future.
Definition future.h:407
T *const value
Pointer to buffer when it holds a T object.
Definition future.h:379
remote_refT remote_ref(World &world) const
Returns a structure used to pass references to another process.
Definition future.h:672
Future()
Makes an unassigned future.
Definition future.h:398
const T & get(bool dowork=true) const &
Gets the value, waiting if necessary.
Definition future.h:585
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:505
atomic_shared_ptr< FutureImpl< T > > f
Pointer to the implementation object.
Definition future.h:377
void set(const T &value)
Assigns the value.
Definition future.h:534
void set(const archive::BufferInputArchive &input_arch)
Assigns the value.
Definition future.h:555
Future(const archive::BufferInputArchive &input_arch)
Makes an assigned future from an input archive.
Definition future.h:429
~Future()
Destructor.
Definition future.h:450
static const Future< T > default_initializer()
See "Gotchas" on Futures about why this exists and how to use it.
Definition future.h:459
Future(const dddd &blah)
Definition future.h:391
char buffer[sizeof(T)]
Buffer to hold a single T object.
Definition future.h:378
RemoteReference< FutureImpl< T > > remote_refT
Definition future.h:395
bool is_local() const
Checks the future remoteness trait.
Definition future.h:685
Future(const remote_refT &remote_ref)
Makes a future wrapping a remote reference.
Definition future.h:416
bool is_remote() const
Checks the future remoteness trait.
Definition future.h:694
void register_callback(CallbackInterface *callback)
Registers an object to be called when future is assigned.
Definition future.h:705
Future(const Future< T > &other)
Shallow copy constructor.
Definition future.h:439
void set(T &&value)
Assigns the value.
Definition future.h:544
Future< T > & operator=(const Future< T > &other)
Shallow assignment operator.
Definition future.h:476
bool is_default_initialized() const
Check if the future is default initialized.
Definition future.h:467
bool probe() const
Check whether this future has been assigned.
Definition future.h:628
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
Definition atomic_shared_ptr.h:37
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:2518
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:2543
std::vector< Future< T > > future_vector_factory(std::size_t n)
Factory for a vectors of futures.
Definition future.h:889
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:931
static void load(const Archive &ar, const Future< void > &f)
Read into an unassigned void future.
Definition future.h:966
static void load(const Archive &ar, std::vector< Future< T > > &v)
Read into a vector of unassigned futures.
Definition future.h:1003
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:910
static void store(const Archive &ar, const Future< void > &f)
Store the assigned void future in the archive (do nothing).
Definition future.h:951
static void store(const Archive &ar, const std::vector< Future< T > > &v)
Store the vector of assigned futures in the archive.
Definition future.h:981
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