NAMD
BroadcastObject.h
Go to the documentation of this file.
1 
7 /*
8  Coordinates broadcast of a data type from a Controller/Seq
9  to all other Controller/Sequencer type objects (they must
10  run in a thread!)
11 */
12 
13 #ifndef _BCASTOBJ_H
14 #define _BCASTOBJ_H
15 
16 #include "BroadcastMgr.h"
17 #include "BroadcastClient.h"
18 #include "LdbCoordinator.h"
19 #include "Node.h"
20 #include "SimParameters.h"
21 #include "common.h"
22 
23 #include <map>
24 #include <any>
25 
26 
43 public:
44  CmiNodeLock lock;
45  // Map from [id][tag] -> data
46  //
47  // std::any will be cast to type std::map<int, T> where T is the data type being broadcast.
48  std::map<int, std::any> data;
49  // Map from [id][tag] -> retrieval count
50  std::map<int, std::map<int, int>> current;
51 };
52 
53 template <class T> class SimpleBroadcastObject : public BroadcastClient {
54 
55  public:
56 
57  const LDObjHandle *ldObjPtr;
58 
59  bool useShared = false;
60  int id = -1;
62 
74  SimpleBroadcastObject(int id_in, const LDObjHandle *h = 0,
75  NodeBroadcast* nodeBroadcastIn = 0, const bool useSharedIfPossible = false)
76  : BroadcastClient(id_in), ldObjPtr(h), nodeBroadcast(nodeBroadcastIn), id(id_in) {
77  if ( sizeof(T) > BCASTMSGSIZE ) {
78  NAMD_bug("SimpleBroadcastObject instantiated on class larger than BCASTMSGSIZE");
79  }
80 
81  if (nodeBroadcast != 0) {
83 
84  if (useShared) {
85  // Add a map for this broadcast object if needed
86  CmiLock(nodeBroadcast->lock);
87  if (nodeBroadcast->data.find(id) == nodeBroadcast->data.end()) {
88  nodeBroadcast->data[id] = new std::map<int, T>();
89  }
90  CmiUnlock(nodeBroadcast->lock);
91  }
92  }
93  }
95 
105  T get(int tag, const int expected = -1) {
106  T tmp;
107  if (useShared) {
108  CmiLock(nodeBroadcast->lock);
109  // Attempt to retrieve data from map
110  if (nodeBroadcast->data.find(id) == nodeBroadcast->data.end()) {
111  NAMD_bug("SimpleBroadcastObject: Did not find id");
112  }
113  std::map<int, T>* data = std::any_cast<std::map<int, T>*>(nodeBroadcast->data[id]);
114  if (data->find(tag) == data->end()) {
115  NAMD_bug("SimpleBroadcastObject: Did not find tag");
116  }
117  tmp = (*data)[tag];
118  // Check if the data has been retrieved the expected number of times, if so, delete it.
119  nodeBroadcast->current[id][tag]++;
120  if (nodeBroadcast->current[id][tag] == expected) {
121  data->erase(tag);
122  nodeBroadcast->current[id].erase(tag);
123  }
124  CmiUnlock(nodeBroadcast->lock);
125  } else {
127  while ( BroadcastMgr::Object()->getbuf(*this, tag, (void*)(&tmp)) < 0 ) {
128  suspendFor(tag);
129  }
131  }
132  return tmp;
133  }
134  int getSize() {
135  return BroadcastMgr::Object()->boidSize();
136  }
137  int getBcastSize() {
139  }
142  }
143  void publish(int tag,const T &t ) {
144  if (useShared) {
145  CmiLock(nodeBroadcast->lock);
146  // Insert new data into map.
147  std::map<int, T>* data = std::any_cast<std::map<int, T>*>(nodeBroadcast->data[id]);
148  data->insert({tag, t});
149  // Reset retrieval counter to 0.
150  nodeBroadcast->current[id][tag] = 0;
151  CmiUnlock(nodeBroadcast->lock);
152  } else {
153  BroadcastMgr::Object()->send(*this, tag, (void*)(&t), sizeof(T));
154  }
155  }
156 };
157 
158 #endif
static Node * Object()
Definition: Node.h:86
Broadcast object for intra-node GPU-resident broadcasts.
SimParameters * simParameters
Definition: Node.h:181
void startWork(const LDObjHandle &handle)
void pauseWork(const LDObjHandle &handle)
int boidBroadcastSize()
Definition: BroadcastMgr.C:28
const LDObjHandle * ldObjPtr
void send(BroadcastClient &b, int tag, void *buf, size_t)
Definition: BroadcastMgr.C:69
void NAMD_bug(const char *err_msg)
Definition: common.C:196
std::map< int, std::any > data
NodeBroadcast * nodeBroadcast
int boidTaggedMsgSize()
Definition: BroadcastMgr.C:38
static LdbCoordinator * Object()
SimpleBroadcastObject(int id_in, const LDObjHandle *h=0, NodeBroadcast *nodeBroadcastIn=0, const bool useSharedIfPossible=false)
Creates a new SimpleBroadcastObject with given id.
void publish(int tag, const T &t)
Bool GPUresidentSingleProcessMode
void suspendFor(int tag)
static BroadcastMgr * Object()
Definition: BroadcastMgr.h:95
#define BCASTMSGSIZE
Definition: BroadcastMgr.h:22
CmiNodeLock lock
std::map< int, std::map< int, int > > current