36 #ifndef MADNESS_WORLD_WORLDFUT_H__INCLUDED
37 #define MADNESS_WORLD_WORLDFUT_H__INCLUDED
111 template <
typename T>
class Future;
116 template <
typename T>
122 template <
typename T>
128 template <
typename T>
136 template <
typename T>
144 #define REMFUTURE(T) typename remove_future< T >::type
149 template <
typename T>
150 std::ostream& operator<<(std::ostream& out, const Future<T>&
f);
156 template <
typename T>
159 friend std::ostream& operator<< <T>(std::ostream& out,
const Future<T>&
f);
162 static const int MAXCALLBACKS = 4;
163 typedef std::stack<CallbackInterface*, std::vector<CallbackInterface*> > callbackT;
165 volatile callbackT callbacks;
166 volatile mutable assignmentT assignments;
167 volatile bool assigned;
172 static void set_handler(
const AmArg&
arg) {
181 if(pimpl->remote_ref) {
189 World& world = pimpl->remote_ref.get_world();
190 const ProcessID owner = pimpl->remote_ref.owner();
194 pimpl->set_assigned(value);
197 input_arch &
const_cast<T&
>(pimpl->t);
199 pimpl->set_assigned(const_cast<const T&>(pimpl->t));
206 inline void set_assigned(
const T& value) {
215 MADNESS_ASSERT(!assigned);
218 assignmentT& as =
const_cast<assignmentT&
>(assignments);
219 callbackT& cb =
const_cast<callbackT&
>(callbacks);
221 while (!as.empty()) {
222 MADNESS_ASSERT(as.front());
223 as.top()->set(value);
227 while (!cb.empty()) {
228 MADNESS_ASSERT(cb.top());
238 f->set(const_cast<T&>(t));
241 assignmentT* as =
const_cast<assignmentT*
>(&assignments);
264 , remote_ref(remote_ref)
270 inline bool probe()
const {
return assigned; }
280 if (assigned) callback->
notify();
281 else const_cast<callbackT&
>(callbacks).push(callback);
286 template <
typename U>
287 void set(
const U& value) {
298 set_assigned((const_cast<T&>(t) = value));
304 MADNESS_ASSERT(! remote_ref);
305 input_arch &
const_cast<T&
>(t);
306 set_assigned(const_cast<T&>(t));
311 MADNESS_ASSERT(! remote_ref);
313 return *
const_cast<T*
>(&t);
317 const T&
get()
const {
318 MADNESS_ASSERT(! remote_ref);
320 return *
const_cast<const T*
>(&t);
343 if (const_cast<callbackT&>(callbacks).size()) {
344 print(
"Future: uninvoked callbacks being destroyed?", assigned);
347 if (const_cast<assignmentT&>(assignments).size()) {
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);
387 char buffer[
sizeof(
T)];
391 explicit Future(
const dddd&) : f(), value(NULL) { }
403 f(), value(new(static_cast<void*>(buffer))
T(t))
407 explicit Future(
const remote_refT& remote_ref) :
409 remote_ref.get_shared() :
416 f(), value(new(static_cast<void*>(buffer))
T())
418 input_arch & (*value);
425 new(static_cast<void*>(buffer))
T(* other.value) :
428 if(other.is_default_initialized())
450 MADNESS_ASSERT(!
probe());
476 MADNESS_ASSERT(! f->probe());
491 of->add_to_assignments(ff);
498 inline void set(
const T& value) {
514 MADNESS_ASSERT(f || value);
515 return (f ? f->
get() : *value);
519 inline const T&
get()
const {
520 MADNESS_ASSERT(f || value);
521 return (f ? f->
get() : *value);
527 inline bool probe()
const {
return (f ? f->probe() : bool(value)); }
530 inline operator T&() {
return get(); }
533 inline operator const T&()
const {
return get(); }
553 MADNESS_ASSERT(!
probe());
555 return f->remote_ref;
561 inline bool is_local()
const {
return (f && f->is_local()) || value; }
576 f->register_callback(callback);
619 static bool probe() {
return true; }
650 static bool probe() {
return true; }
660 template <
typename T>
663 typedef typename std::vector< Future<T> > vectorT;
670 for (
int i=0; i<(int)v.size(); ++i) {
680 vectorT&
get() {
return v; }
681 const vectorT&
get()
const {
return v; }
682 operator vectorT& () {
return get(); }
683 operator const vectorT& ()
const {
return get(); }
686 for(
typename std::vector<
Future<T> >::const_iterator it = v.begin(); it != v.end(); ++it)
697 template <
typename T>
707 template <
class Archive,
typename T>
711 MADNESS_ASSERT(f.probe());
720 template <
class Archive,
typename T>
724 MADNESS_ASSERT(!f.probe());
735 template <
class Archive>
744 template <
class Archive>
752 template <
class Archive>
761 template <
class Archive>
767 template <
class Archive,
typename T>
772 for(
typename std::vector<
Future<T> >::const_iterator it = v.begin(); it != v.end(); ++it) {
773 MADNESS_ASSERT(it->probe());
783 template <
class Archive,
typename T>
793 for(
typename std::vector<
Future<T> >::iterator it = v.begin(); it < v.end(); ++it, --n) {
794 MADNESS_ASSERT(! it->probe());
806 template <
typename T>
807 std::ostream& operator<<(std::ostream& out, const Future<T>&
f) ;
810 std::ostream& operator<<(std::ostream& out, const Future<void>&
f) ;
813 std::ostream& operator<<(std::ostream& out, const Future<Void>&
f) ;
815 #ifdef WORLD_INSTANTIATE_STATIC_TEMPLATES
819 template <
typename T>
820 std::ostream& operator<<(std::ostream& out, const Future<T>&
f) {
821 if (
f.probe()) out <<
f.get();
822 else if (
f.is_remote()) out <<
f.f->remote_ref;
823 else if (
f.f) out <<
"<unassigned refcnt=" <<
f.f.use_count() <<
">";
824 else out <<
"<unassigned>";
833 #endif // MADNESS_WORLD_WORLDFUT_H__INCLUDED
Future< T > & operator=(const Future< T > &other)
Assignment future = future makes a shallow copy just like copy constructor.
Definition: worldfut.h:448
Mutex that is applied/released at start/end of a scope.
Definition: worldmutex.h:186
static void await(SafeMPI::Request &request, bool dowork=true)
Wait for MPI request to complete.
Definition: worldfwd.h:650
FutureImpl(const RemoteReference< FutureImpl< T > > &remote_ref)
Definition: worldfut.h:260
World & get_world() const
Owning world accessor.
Definition: worldref.h:586
Definition: shared_ptr_bits.h:38
Specialization of FutureImpl for internal convenience ... does nothing useful! ...
Definition: worldfut.h:593
World active message that extends an RMI message.
Definition: worldam.h:81
Boost-type-trait-like mapping of Future to T.
Definition: typestuff.h:73
double fred(const coordT &r)
Definition: tdse4.cc:369
T & get()
Definition: worldfut.h:310
static void load(const Archive &, Future< Void > &)
Definition: worldfut.h:763
static bool probe()
Definition: worldfut.h:619
void register_callback(CallbackInterface *callback)
Definition: worldfut.h:278
Future(const RemoteReference< FutureImpl< void > > &)
Definition: worldfut.h:608
static void store(const Archive &ar, const std::vector< Future< T > > &v)
Definition: worldfut.h:769
Future()
Definition: worldfut.h:606
Future(const archive::BufferInputArchive &input_arch)
Makes an assigned future from an input archive.
Definition: worldfut.h:415
~Future()
Definition: worldfut.h:432
T & unwrap_move(const detail::MoveWrapper< T > &t)
Remove move wrapper from a movable object.
Definition: move.h:119
Boost-type-trait-like testing of if a type is a future.
Definition: worldfut.h:117
static void load(const Archive &ar, Future< T > &f)
Definition: worldfut.h:722
bool replace_with(FutureImpl< T > *f)
Definition: worldfut.h:327
remote_refT remote_ref(World &) const
Definition: worldfut.h:634
AmArg * new_am_arg(const A &a, const B &b, const C &c, const D &d, const E &e, const F &f, const G &g, const H &h, const I &i, const J &j)
Convenience template for serializing arguments into a new AmArg.
Definition: worldam.h:185
bool probe() const
Query the whether this future has been assigned.
Definition: worldfut.h:527
detail::ReferenceWrapper< T > const ref(T &t)
Reference wrapper factory function.
Definition: ref.h:132
This class used for callbacks (e.g., for dependency tracking)
Definition: worlddep.h:51
Specialization of Future for internal convenience ... does nothing useful!
Definition: worldfut.h:598
Grossly simplified Boost-like type traits and templates.
Definition: mpreal.h:3066
Future(const archive::BufferInputArchive &input_arch)
Not implemented.
Definition: worldfut.h:676
static void set(const Future< void > &)
Definition: worldfut.h:616
static void store(const Archive &, const Future< Void > &)
Definition: worldfut.h:754
static void set()
Definition: worldfut.h:617
Specialization of Future for internal convenience ... does nothing useful!
Definition: worldfut.h:630
NDIM & f
Definition: mra.h:2179
bool is_remote() const
Definition: worldfut.h:563
bool_constant< true > true_type
Definition: gtest-port.h:1618
Default store of a thingy via serialize(ar,t)
Definition: archive.h:708
void register_callback(CallbackInterface *callback)
Registers an object to be called when future is assigned.
Definition: worldfut.h:571
void set(const Future< Void > &)
Definition: worldfut.h:642
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:2429
bool_constant< false > false_type
Definition: gtest-port.h:1617
Future(const vectorT &v)
Definition: worldfut.h:669
WorldAmInterface & am
AM interface.
Definition: worldfwd.h:460
BindNullaryMemFun< T, resultT > bind_nullary_mem_fun(T *t, resultT(T::*op)())
Factory function for BindNullaryMemFun.
Definition: typestuff.h:143
Simple structure used to manage references/pointers to remote instances.
Definition: worldref.h:59
T * get() const
Definition: shared_ptr_bits.h:485
A simple, fixed-size, stack.
Definition: array.h:499
static bool probe()
Definition: worldfut.h:650
Future< void > & operator=(const Future< void > &)
Definition: worldfut.h:614
static void store(const Archive &ar, const Future< T > &f)
Definition: worldfut.h:709
Future(const remote_refT &remote_ref)
Makes a future wrapping a remote reference.
Definition: worldfut.h:407
RemoteReference< FutureImpl< T > > remote_refT
Definition: worldfut.h:394
const T1 &f1 return GTEST_2_TUPLE_() T(f0, f1)
Provides interface for tracking dependencies.
Definition: worlddep.h:60
bool is_local() const
Definition: worldfut.h:323
std::vector< Future< T > > future_vector_factory(std::size_t n)
Factory for vectors of futures (see section Gotchas on the mainpage)
Definition: worldfut.h:698
Future()
Makes an unassigned future.
Definition: worldfut.h:397
Default load of a thingy via serialize(ar,t)
Definition: archive.h:718
static void load(const Archive &, const Future< void > &)
Definition: worldfut.h:746
virtual ~FutureImpl()
Definition: worldfut.h:342
T type
Definition: worldfut.h:138
void set(const Void &)
Definition: worldfut.h:648
FutureImpl()
Definition: worldfut.h:250
Implements the functionality of Futures.
Definition: worldfut.h:157
ProcessID owner() const
Reference owner accessor.
Definition: worldref.h:580
RMI::Request send(const ProcessID dest, am_handlerT op, const AmArg *arg, const int attr=RMI::ATTR_ORDERED, const bool managed=true)
Sends an unmanaged non-blocking active message.
Definition: worldam.h:386
bool is_local() const
Definition: worldfut.h:561
A parallel world with full functionality wrapping an MPI communicator.
Definition: worldfwd.h:416
RemoteReference< FutureImpl< Void > > remote_refT
Definition: worldfut.h:632
bool probe() const
Definition: worldfut.h:270
Future()
Definition: worldfut.h:667
#define MAD_ARCHIVE_DEBUG(s)
Definition: archive.h:438
bool is_default_initialized() const
Default initialized query.
Definition: worldfut.h:445
Specialization of FutureImpl for internal convenience ... does nothing useful! ...
Definition: worldfut.h:625
static const Future< T > default_initializer()
See Gotchas on the documentation mainpage about why this exists and how to use it.
Definition: worldfut.h:439
bool probe() const
Definition: worldfut.h:685
int ProcessID
Used to clearly identify process number/rank.
Definition: worldtypes.h:37
Future(const RemoteReference< FutureImpl< Void > > &)
Definition: worldfut.h:640
void reset()
Definition: shared_ptr_bits.h:459
Future(const Future< T > &other)
Copy constructor is shallow.
Definition: worldfut.h:422
void set(const archive::BufferInputArchive &input_arch)
Definition: worldfut.h:302
remote_refT remote_ref(World &world) const
Returns a structure used to pass references to another process.
Definition: worldfut.h:552
static const Future< void > value
Definition: worldfut.h:602
static void load(const Archive &ar, std::vector< Future< T > > &v)
Definition: worldfut.h:785
Implements RemoteReference which is for internal use.
void set(const T &value)
Assigns the value ... it can only be set ONCE.
Definition: worldfut.h:498
A future is a possibly yet unevaluated value.
Definition: ref.h:210
A type you can return when you want to return void ... use "return None".
Definition: typestuff.h:154
void print(const A &a)
Print a single item to std::cout terminating with new line.
Definition: print.h:122
static remote_refT remote_ref(World &)
Definition: worldfut.h:604
Future(const archive::BufferInputArchive &input_arch)
Definition: worldfut.h:610
#define MADNESS_EXCEPTION(msg, value)
Definition: worldexc.h:88
RemoteReference< FutureImpl< void > > remote_refT
Definition: worldfut.h:600
Future< Void > & operator=(const Future< Void > &)
Definition: worldfut.h:644
Spinlock using pthread spinlock operations.
Definition: worldmutex.h:200
void set(const Future< T > &other)
A.set(B) where A & B are futures ensures A has/will have the same value as B.
Definition: worldfut.h:473
Holds machinery to set up Functions/FuncImpls using various Factories and Interfaces.
Definition: chem/atomutil.cc:45
Future()
Definition: worldfut.h:638
T type
Definition: worldfut.h:130
static void store(const Archive &, const Future< void > &)
Definition: worldfut.h:737
void register_callback(CallbackInterface *callback)
Registers a callback for when ndepend==0 , immediately invoked if ndepend==0.
Definition: worlddep.h:94
Defines DependencyInterface and CallbackInterface.
void set(const U &value)
Definition: worldfut.h:287
Disables default copy constructor and assignment operators.
Definition: nodefaults.h:49
Future(const T &t)
Makes an assigned future.
Definition: worldfut.h:402
void set(const archive::BufferInputArchive &input_arch)
Assigns the value ... it can only be set ONCE.
Definition: worldfut.h:505