42#ifndef MADNESS_WORLD_WORLD_H__INCLUDED
43#define MADNESS_WORLD_WORLD_H__INCLUDED
85 class WorldAmInterface;
86 class WorldGopInterface;
106 void error(
const char *msg);
115 template <
typename T>
116 static void error(
const char *msg,
const T&
data);
164 return std::size_t(
p);
234 typedef std::list<World *>::const_iterator citer;
235 for (citer it =
worlds.begin(); it !=
worlds.end(); ++it) {
236 if ((*it)->mpi.comm() == comm)
259#ifdef MADNESS_DISABLE_WORLD_GET_DEFAULT
308 void args(
int argc,
char**argv);
313 unsigned long id()
const {
return _id; }
373 template <
typename T>
377 [[maybe_unused]]
auto&& [it1, inserted1] =
map_id_to_ptr.
insert(std::pair<uniqueidT,void*>(
id,
static_cast<void*
>(ptr)));
379 [[maybe_unused]]
auto&& [it2, inserted2] =
map_ptr_to_id.
insert(std::pair<void*,uniqueidT>(
static_cast<void*
>(ptr),
id));
393 void* ptr =
static_cast<void*
>(acc->second);
400 acc->second =
nullptr;
413 template <
typename T>
420 return {
static_cast<T *
>(acc->second)};
428 template <
typename T>
435 return {acc->second};
438#ifndef MADNESS_DISABLE_SHARED_FROM_THIS
446 template <
typename T>
448 std::optional<T*> ptr_opt = ptr_from_id<T>(
id);
450 return (ptr_opt ? *ptr_opt->shared_from_this() : std::shared_ptr<T>());
458 template <
typename T>
459 [[nodiscard]] std::optional<uniqueidT>
id_from_ptr(std::shared_ptr<T>& ptr)
const {
476 for (std::list<World *>::iterator it =
worlds.begin();
477 it !=
worlds.end(); ++it) {
478 if ((*it) && (*it)->_id ==
id)
528 template <
typename Probe>
529 static void inline await(
const Probe& probe,
bool dowork =
true,
bool sleep=
false) {
537 void srand(
unsigned long seed = 0ul) {
538 if (seed == 0) seed =
rank();
543 for (
int i=0; i<1000; ++i)
rand();
555 return int(random() & 0xfffffful);
581 if (
size() == 1)
return -1;
585 }
while (
p ==
rank());
598 template <
class Archive>
605 static inline void load(
const Archive& ar,
World*& wptr) {
606 unsigned long id = 0ul;
617 template <
class Archive>
623 static inline void store(
const Archive& ar,
World*
const & wptr) {
632 template <
typename T>
634 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:490
void Abort(int code=1) const
Definition safempi.h:800
bool Test(MPI_Status &status)
Definition safempi.h:409
Definition worldhashmap.h:396
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 find(const keyT &key)
Definition worldhashmap.h:524
Definition worldhashmap.h:330
void release()
Definition worldhashmap.h:380
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:1444
Implements AM interface.
Definition worldam.h:207
Provides collectives that interoperate with the AM and task interfaces.
Definition worldgop.h:145
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:132
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:447
void * get_user_state()
Returns a pointer to the user-managed local state set by set_user_state().
Definition world.h:295
map_ptr_to_idT map_ptr_to_id
inverse of map_id_to_ptr
Definition world.h:184
static World & get_default()
Default World object accessor.
Definition world.h:258
static World * default_world
Default world.
Definition world.h:141
static World * world_from_id(std::uint64_t id)
Convert a World ID to a World pointer.
Definition world.h:474
WorldTaskQueue & taskq
Task queue.
Definition world.h:204
static bool is_default(const SafeMPI::Intracomm &comm)
Checks if the default World object corresponds to the given Intracomm.
Definition world.h:271
friend void finalize()
Call this once at the very end of your main program instead of MPI_Finalize().
Definition world.cc:232
void set_user_state(void *state)
Sets the user-managed local state.
Definition world.h:289
void clear_user_state()
Clears the user-defined state.
Definition world.h:300
uniqueidT make_unique_obj_id()
Creates a new universe-wide unique ID for objects created in this World. No comms.
Definition world.h:343
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:140
void srand(unsigned long seed=0ul)
Crude seed function for random number generation.
Definition world.h:537
unsigned int myrand_next
State of crude internal random number generator.
Definition world.h:208
ProcessID rank() const
Returns the process rank in this World (same as MPI_Comm_rank()).
Definition world.h:318
friend World & initialize(int &, char **&, const SafeMPI::Intracomm &, int, bool)
Definition world.cc:161
~World()
Definition world.cc:117
static void await(SafeMPI::Request &request, bool dowork=true)
Wait for a MPI request to complete.
Definition world.h:516
static void await(const Probe &probe, bool dowork=true, bool sleep=false)
Gracefully wait for a condition to become true.
Definition world.h:529
map_id_to_ptrT map_id_to_ptr
Definition world.h:182
WorldMpiInterface & mpi
MPI interface.
Definition world.h:202
std::optional< uniqueidT > id_from_ptr(std::shared_ptr< T > &ptr) const
Look up a unique ID from a local pointer.
Definition world.h:459
std::optional< uniqueidT > id_from_ptr(T *ptr) const
Look up an ID from a local pointer.
Definition world.h:429
std::uint64_t _id
Universe wide unique ID of this world.
Definition world.h:187
madness::ConcurrentHashMap< uniqueidT, void *, hashuniqueT > map_id_to_ptrT
Definition world.h:170
void args(int argc, char **argv)
Processes command line arguments.
Definition world.cc:108
void unregister_ptr(const uniqueidT id)
Unregister a global object via its the unique ID.
Definition world.h:387
ProcessID random_proc_not_me()
Returns a random process number [0,world.size()) that is not the calling process.
Definition world.h:580
static World * find_instance(const SafeMPI::Intracomm &comm)
Find the World (if it exists) corresponding to the given communicator.
Definition world.h:230
uniqueidT register_ptr(T *ptr)
Associate a local pointer with a universe-wide unique ID.
Definition world.h:374
static std::list< World * > worlds
Maintains list of active worlds, EXCLUDES default_world.
Definition world.h:142
ProcessID size() const
Returns the number of processes in this World (same as MPI_Comm_size()).
Definition world.h:328
unsigned long id() const
Definition world.h:313
madness::ConcurrentHashMap< void *, uniqueidT, hashvoidp > map_ptr_to_idT
Definition world.h:172
void * user_state
Holds a user-defined and managed local state.
Definition world.h:189
WorldGopInterface & gop
Global operations.
Definition world.h:205
double drand()
Returns a CRUDE, LOW-QUALITY, random number (real) uniformly distributed in [0,1).
Definition world.h:568
static bool exists(World *world)
Check if the World exists in the registry.
Definition world.h:247
std::optional< T * > ptr_from_id(uniqueidT id) const
Look up a local pointer from a world-wide unique ID.
Definition world.h:414
ProcessID nproc() const
Returns the number of processes in this World (same as MPI_Comm_size()).
Definition world.h:323
ProcessID random_proc()
Returns a random process number; that is, an integer in [0,world.size()).
Definition world.h:573
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:351
unsigned long obj_id
Counter for generating unique IDs within this world.
Definition world.h:188
static std::uint64_t next_world_id()
Definition world.cc:134
static void initialize_world_id_range(int global_rank)
Definition world.cc:128
int rand()
Returns a CRUDE, LOW-QUALITY, random number (integer) uniformly distributed in [0,...
Definition world.h:553
WorldAmInterface & am
AM interface.
Definition world.h:203
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
std::filesystem::path path
Definition excited_state_calc.cpp:25
auto T(World &world, response_space &f) -> response_space
Definition global_functions.cc:34
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:260
void error(const char *msg)
Definition world.cc:139
void xterm_debug(const char *path, const char *display)
Definition debug.cc:116
Definition test_ccpairfunction.cc:22
bool operator()() const
Definition world.h:504
MpiRequestTester(SafeMPI::Request &r)
Definition world.h:498
SafeMPI::Request * r
Definition world.h:492
std::size_t operator()(const uniqueidT &id) const
Definition world.h:151
std::size_t operator()(const void *p) const
Definition world.h:163
static void load(const Archive &ar, World *&wptr)
Loads a World from the specified archive.
Definition world.h:605
Default load of an object via serialize(ar, t).
Definition archive.h:666
static void store(const Archive &ar, World *const &wptr)
Writes a World to the specified archive.
Definition world.h:623
Default store of an object via serialize(ar, t).
Definition archive.h:611
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