42#ifndef MADNESS_WORLD_WORLD_H__INCLUDED
43#define MADNESS_WORLD_WORLD_H__INCLUDED
87 class WorldAmInterface;
88 class WorldGopInterface;
108 void error(
const char *msg);
117 template <
typename T>
118 static void error(
const char *msg,
const T&
data);
166 return std::size_t(
p);
198 std::shared_ptr<std::atomic<bool>>
alive;
245 typedef std::list<World *>::const_iterator
citer;
247 if ((*it)->mpi.comm() == comm)
270#ifdef MADNESS_DISABLE_WORLD_GET_DEFAULT
319 void args(
int argc,
char**argv);
324 unsigned long id()
const {
return _id; }
399 template <
typename T>
419 void* ptr =
static_cast<void*
>(
acc->second);
426 acc->second =
nullptr;
439 template <
typename T>
446 return {
static_cast<T *
>(
acc->second)};
454 template <
typename T>
461 return {
acc->second};
464#ifndef MADNESS_DISABLE_SHARED_FROM_THIS
472 template <
typename T>
476 return (
ptr_opt ? *
ptr_opt->shared_from_this() : std::shared_ptr<T>());
484 template <
typename T>
493 std::vector<uniqueidT>
ids;
495 ids.push_back(it->first);
501 std::vector<unsigned long>
ids;
502 for (
auto it =
worlds.begin(); it !=
worlds.end(); ++it)
503 ids.push_back((*it)->id());
518 for (std::list<World *>::iterator it =
worlds.begin();
519 it !=
worlds.end(); ++it) {
520 if ((*it) && (*it)->_id ==
id)
570 template <
typename Probe>
585 for (
int i=0; i<1000; ++i)
rand();
623 if (
size() == 1)
return -1;
627 }
while (
p ==
rank());
640 template <
class Archive>
648 unsigned long id = 0
ul;
659 template <
class Archive>
674 template <
typename T>
676 std::cerr <<
"MADNESS: fatal error: " << msg <<
" " <<
data << std::endl;
Disables default copy constructor and assignment operators.
Definition nodefaults.h:49
Wrapper around MPI_Comm. Has a shallow copy constructor; use Create(Get_group()) for deep copy.
Definition safempi.h:497
void Abort(int code=1) const
Definition safempi.h:807
bool Test(MPI_Status &status)
Definition safempi.h:416
Definition worldhashmap.h:396
iterator begin()
Definition worldhashmap.h:571
void erase(const iterator &it)
Definition worldhashmap.h:507
std::pair< iterator, bool > insert(const datumT &datum)
Definition worldhashmap.h:468
bool try_erase(const keyT &key)
Definition worldhashmap.h:502
iterator end()
Definition worldhashmap.h:583
iterator find(const keyT &key)
Definition worldhashmap.h:524
Definition worldhashmap.h:330
static void await(const Probe &probe, bool dowork=true, bool sleep=false)
Gracefully wait for a condition to become true, executing any tasks in the queue.
Definition thread.h:1450
Implements AM interface.
Definition worldam.h:207
Provides collectives that interoperate with the AM and task interfaces.
Definition worldgop.h:147
This class wraps/extends the MPI interface for World.
Definition worldmpi.h:266
int nproc() const
Access the total number of processes.
Definition worldmpi.h:444
SafeMPI::Intracomm & comm()
Returns the associated SafeMPI communicator.
Definition worldmpi.h:286
int size() const
Access the total number of processes.
Definition worldmpi.h:449
int rank() const
Access the rank of this process.
Definition worldmpi.h:439
Multi-threaded queue to manage and run tasks.
Definition world_task_queue.h:319
A parallel world class.
Definition world.h:134
std::shared_ptr< T > shared_ptr_from_id(uniqueidT id) const
Look up a local pointer from a world-wide unique ID.
Definition world.h:473
void * get_user_state()
Returns a pointer to the user-managed local state set by set_user_state().
Definition world.h:306
map_ptr_to_idT map_ptr_to_id
inverse of map_id_to_ptr
Definition world.h:186
static World & get_default()
Default World object accessor.
Definition world.h:269
static World * default_world
Default world.
Definition world.h:143
static World * world_from_id(std::uint64_t id)
Convert a World ID to a World pointer.
Definition world.h:516
WorldTaskQueue & taskq
Task queue.
Definition world.h:215
static bool is_default(const SafeMPI::Intracomm &comm)
Checks if the default World object corresponds to the given Intracomm.
Definition world.h:282
friend void finalize()
Call this once at the very end of your main program instead of MPI_Finalize().
Definition world.cc:246
liveness_handleT liveness() const
Returns a handle reporting whether this World is still alive.
Definition world.h:339
void set_user_state(void *state)
Sets the user-managed local state.
Definition world.h:300
void clear_user_state()
Clears the user-defined state.
Definition world.h:311
std::vector< uniqueidT > get_object_ids() const
Returns a vector of all unique IDs in this World.
Definition world.h:492
uniqueidT make_unique_obj_id()
Creates a new universe-wide unique ID for objects created in this World. No comms.
Definition world.h:369
static std::pair< std::uint64_t, std::uint64_t > world_id__next_last
Unique {next, last} world IDs to be used by this rank.
Definition world.h:142
void srand(unsigned long seed=0ul)
Crude seed function for random number generation.
Definition world.h:579
unsigned int myrand_next
State of crude internal random number generator.
Definition world.h:219
ProcessID rank() const
Returns the process rank in this World (same as MPI_Comm_rank()).
Definition world.h:344
std::shared_ptr< const std::atomic< bool > > liveness_handleT
The type of the handle returned by liveness().
Definition world.h:327
friend World & initialize(int &, char **&, const SafeMPI::Intracomm &, int, bool)
Definition world.cc:169
~World()
Definition world.cc:119
static void await(SafeMPI::Request &request, bool dowork=true)
Wait for a MPI request to complete.
Definition world.h:558
static void await(const Probe &probe, bool dowork=true, bool sleep=false)
Gracefully wait for a condition to become true.
Definition world.h:571
map_id_to_ptrT map_id_to_ptr
Definition world.h:184
static std::vector< unsigned long > get_world_ids()
return a vector containing all world ids
Definition world.h:500
WorldMpiInterface & mpi
MPI interface.
Definition world.h:213
std::optional< uniqueidT > id_from_ptr(std::shared_ptr< T > &ptr) const
Look up a unique ID from a local pointer.
Definition world.h:485
std::optional< uniqueidT > id_from_ptr(T *ptr) const
Look up an ID from a local pointer.
Definition world.h:455
std::uint64_t _id
Universe wide unique ID of this world.
Definition world.h:189
madness::ConcurrentHashMap< uniqueidT, void *, hashuniqueT > map_id_to_ptrT
Definition world.h:172
void args(int argc, char **argv)
Processes command line arguments.
Definition world.cc:110
void unregister_ptr(const uniqueidT id)
Unregister a global object via its the unique ID.
Definition world.h:413
ProcessID random_proc_not_me()
Returns a random process number [0,world.size()) that is not the calling process.
Definition world.h:622
static World * find_instance(const SafeMPI::Intracomm &comm)
Find the World (if it exists) corresponding to the given communicator.
Definition world.h:241
uniqueidT register_ptr(T *ptr)
Associate a local pointer with a universe-wide unique ID.
Definition world.h:400
static std::list< World * > worlds
Maintains list of active worlds, EXCLUDES default_world.
Definition world.h:144
ProcessID size() const
Returns the number of processes in this World (same as MPI_Comm_size()).
Definition world.h:354
unsigned long id() const
Definition world.h:324
madness::ConcurrentHashMap< void *, uniqueidT, hashvoidp > map_ptr_to_idT
Definition world.h:174
void * user_state
Holds a user-defined and managed local state.
Definition world.h:191
WorldGopInterface & gop
Global operations.
Definition world.h:216
double drand()
Returns a CRUDE, LOW-QUALITY, random number (real) uniformly distributed in [0,1).
Definition world.h:610
static bool exists(World *world)
Check if the World exists in the registry.
Definition world.h:258
std::optional< T * > ptr_from_id(uniqueidT id) const
Look up a local pointer from a world-wide unique ID.
Definition world.h:440
ProcessID nproc() const
Returns the number of processes in this World (same as MPI_Comm_size()).
Definition world.h:349
ProcessID random_proc()
Returns a random process number; that is, an integer in [0,world.size()).
Definition world.h:615
std::shared_ptr< std::atomic< bool > > alive
Tracks the liveness of this World; cleared at the end of ~World.
Definition world.h:198
uniqueidT next_unique_obj_id() const
Reports the next universe-wide unique ID generated by make_unique_obj_id() objects created in this Wo...
Definition world.h:377
unsigned long obj_id
Counter for generating unique IDs within this world.
Definition world.h:190
static std::uint64_t next_world_id()
Definition world.cc:142
static void initialize_world_id_range(int global_rank)
Definition world.cc:136
int rand()
Returns a CRUDE, LOW-QUALITY, random number (integer) uniformly distributed in [0,...
Definition world.h:595
WorldAmInterface & am
AM interface.
Definition world.h:214
Class for unique global IDs.
Definition uniqueid.h:53
char * p(char *buf, const char *name, int k, int initial_level, double thresh, int order)
Definition derivatives.cc:72
Macros and tools pertaining to the configuration of MADNESS.
#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
Intracomm COMM_WORLD
Definition safempi.cc:67
Namespace for all elements and tools of MADNESS.
Definition DFParameters.h:10
void print_stats(World &world)
Print miscellaneous stats on a World.
Definition world.cc:274
void error(const char *msg)
Definition world.cc:147
void xterm_debug(const char *path, const char *display)
Definition debug.cc:118
static XNonlinearSolver< std::vector< Function< T, NDIM > >, T, vector_function_allocator< T, NDIM > > nonlinear_vector_solver(World &world, const long nvec)
Definition nonlinsol.h:371
Definition test_ccpairfunction.cc:22
bool operator()() const
Definition world.h:546
MpiRequestTester(SafeMPI::Request &r)
Definition world.h:540
SafeMPI::Request * r
Definition world.h:534
std::size_t operator()(const uniqueidT &id) const
Definition world.h:153
std::size_t operator()(const void *p) const
Definition world.h:165
static void load(const Archive &ar, World *&wptr)
Loads a World from the specified archive.
Definition world.h:647
Default load of an object via serialize(ar, t).
Definition archive.h:667
static void store(const Archive &ar, World *const &wptr)
Writes a World to the specified archive.
Definition world.h:665
Default store of an object via serialize(ar, t).
Definition archive.h:612
Implements Dqueue, Thread, ThreadBase and ThreadPool.
Defines and implements a concurrent hashmap.
Declares the functions that initialize the parallel runtime.
Implements WorldMpiInterface.
int ProcessID
Used to clearly identify process number/rank.
Definition worldtypes.h:43