SST  11.0.0
StructuralSimulationToolkit
sharedRegion.h
1 // Copyright 2009-2021 NTESS. Under the terms
2 // of Contract DE-NA0003525 with NTESS, the U.S.
3 // Government retains certain rights in this software.
4 //
5 // Copyright (c) 2009-2021, NTESS
6 // All rights reserved.
7 //
8 // This file is part of the SST software package. For license
9 // information, see the LICENSE file in the top level directory of the
10 // distribution.
11 
12 #ifndef SST_CORE_CORE_SHAREDREGION_H
13 #define SST_CORE_CORE_SHAREDREGION_H
14 
15 #include "sst/core/warnmacros.h"
16 #include "sst/core/sst_types.h"
17 
18 #include <string>
19 #include <vector>
20 
21 #if !SST_BUILDING_CORE
22 #warning "SharedRegion and its accompanying classes have been deprecated and will be removed in SST 12. Please use the new SharedObject classes found in sst/core/shared."
23 #endif
24 
25 namespace SST {
26 
27 class SharedRegion;
28 class ChangeSet;
29 
30 /**
31  * Utility class to define how to merge multiple pieces of shared memory regions
32  * Useful in the multi-MPI-rank, "Global Shared" model
33  */
35 public:
36 
37  virtual ~SharedRegionMerger() { }
38 
39  /**
40  * Merge the data from 'newData' into 'target'
41  * @return True on success, False on failure
42  */
43  virtual bool merge(uint8_t *target, const uint8_t *newData, size_t size);
44  virtual bool merge(uint8_t *target, size_t size, const std::vector<ChangeSet> &changeSets);
45 };
46 
47 
49  uint8_t defVal;
50 public:
51  SharedRegionInitializedMerger(uint8_t defaultValue) {
52  defVal = defaultValue;
53  }
54 
55  bool merge(uint8_t *target, const uint8_t *newData, size_t size) override;
56 
57  bool merge(uint8_t *target, size_t size, const std::vector<ChangeSet> &changeSets) override {
58  return SharedRegionMerger::merge(target,size,changeSets);
59  }
60 
61 };
62 
63 
65 protected:
66  friend class SharedRegion;
67 
68  virtual void modifyRegion(SharedRegion *sr, size_t offset, size_t length, const void *data) = 0;
69  virtual void* getMemory(SharedRegion* sr) = 0;
70  virtual const void* getConstPtr(const SharedRegion* sr) const = 0;
71  virtual size_t getSize(const SharedRegion* sr) const = 0;
72 
73 public:
74 
75  /**
76  Create a SharedRegion that will only be shared with elements on
77  the current rank. This type of SharedRegion is intended to
78  have at least one element on each rank initialize the region.
79 
80  @param key Name of the SharedRegion. All elements om the rank
81  using this name will get the same underlying data.
82 
83  @param size Size of the SharedRegion. Elements can specify 0
84  size if they don't know the size of the region. At least one
85  element will need to specify the size and all elements that
86  specify a size must pass in the same size. There are many
87  calls in SharedRegion that can't be called until the size is
88  known.
89 
90  @param initByte Value to initialize all bytes in the region to
91 
92  @return SharedRegion object to be used by the calling element
93  to access the shared region.
94  */
95  virtual SharedRegion* getLocalSharedRegion(const std::string& key, size_t size, uint8_t initByte = 0) = 0;
96 
97  /**
98  Create a SharedRegion that will be shared with elements on the
99  all ranks. The SharedRegion data will be merged across ranks
100  before each round of calls to init().
101 
102  @param key Name of the SharedRegion. All elements using this
103  name will get the same underlying data.
104 
105  @param size Size of the SharedRegion. Elements can specify 0
106  size if they don't know the size of the region. At least one
107  element will need to specify the size and all elements that
108  specify a size must pass in the same size. There are many
109  calls in SharedRegion that can't be called until the size is
110  known.
111 
112  @param merger Merger to use when combining data across ranks
113 
114  @param initByte Value to initialize all bytes in the region to
115 
116  @return SharedRegion object to be used by the calling element
117  to access the shared region.
118  */
119  virtual SharedRegion* getGlobalSharedRegion(const std::string& key, size_t size, SharedRegionMerger *merger = nullptr, uint8_t initByte = 0) = 0;
120 
121 
122  // The following functions will become protected in SST 12. Need
123  // to look for instances of these used in the core, they will be
124  // bracketed with DISABLE_WARN_DEPRECATED_DECLARATION.
125  virtual void publishRegion(SharedRegion*) = 0;
126 
127  virtual bool isRegionReady(const SharedRegion*) = 0;
128 
129  virtual void shutdownSharedRegion(SharedRegion*) = 0;
130 
131  virtual void updateState(bool finalize) = 0;
132 
133 };
134 
136 private:
137  SharedRegionManager *manager;
138  size_t id;
139 
140 
141 
142 protected:
143 
145  public:
146  virtual ~DeferredPointerBase() {}
147  virtual void setPointer(const void *p) = 0;
148  };
149 
150  template <typename T>
152  T& ptr;
153 
154  public:
155  DeferredPointer(T& ptr) : DeferredPointerBase(), ptr(ptr) {}
156 
157  void setPointer(const void *p) {
158  ptr = static_cast<const T>(p);
159  }
160  };
161 
162  DeferredPointerBase* deferred_pointer;
163 
164  SharedRegion(SharedRegionManager *manager, size_t id) :
165  manager(manager),
166  id(id),
167  deferred_pointer(nullptr)
168  { }
169 
170 public:
171 
172  virtual ~SharedRegion()
173  { }
174 
175  void shutdown() {
176  DISABLE_WARN_DEPRECATED_DECLARATION ;
177  manager->shutdownSharedRegion(this);
178  REENABLE_WARNING;
179  }
180 
181  /**
182  * @return The ID of this instance. (Number in range 0->N)
183  */
184  size_t getLocalShareID() const { return id; }
185 
186  /**
187  * @return The size of the shared memory region, may return 0 if
188  * this copy of the region doesn't know the final size yet.
189  */
190  size_t getSize() const { return manager->getSize(this); }
191 
192  /**
193  * Call to denote that you are done making any changes to this region
194  */
195  void publish() {
196  DISABLE_WARN_DEPRECATED_DECLARATION;
197  manager->publishRegion(this);
198  REENABLE_WARNING;
199  }
200 
201 
202  /**
203  * Check to see if the region is ready (all elements requesting
204  * the region have called publish()).
205  *
206  * @return True if the region is ready to use (all sharers have
207  * called publish()).
208  */
209  bool isReady() const {
210  DISABLE_WARN_DEPRECATED_DECLARATION;
211  return manager->isRegionReady(this);
212  REENABLE_WARNING;
213  }
214 
215 
216  /**
217  * Before the region has published, apply a modification. (Copy
218  * this data in). This function cannot be called if the size is
219  * still set to 0.
220  */
221  void modifyRegion(size_t offset, size_t length, const void *data)
222  { manager->modifyRegion(this, offset, length, data); }
223 
224  /**
225  * Before the region has published, apply a modification. (Copy
226  * this data in). This function cannot be called if the size is
227  * still set to 0.
228  */
229  template<typename T>
230  void modifyArray(size_t offset, const T &data)
231  { manager->modifyRegion(this, offset*sizeof(T), sizeof(T), &data); }
232 
233  /**
234  * @return a void* pointer to the shared memory region This
235  * pointer is only valid to write to before a call to publish().
236  * This function cannot be called if the size is still set to
237  * 0, as there is no backing memory yet.
238  */
239  void* getRawPtr() { return manager->getMemory(this); }
240 
241  /**
242  * Get a pointer to the const shared data
243  *
244  * @return a const pointer to the shared memory region. This
245  * function cannot be called if the size is still set to
246  * 0, as there is no backing memory yet.
247  */
248  template<typename T>
249  T getPtr() const {
250  return static_cast<T>(manager->getConstPtr(this));
251  }
252 
253 
254  /**
255  * Used to get a pointer to the const shared data when the element
256  * doesn't yet know the size. This will defer setting the pointer
257  * until the size of the region is known (and therefor the memory
258  * has been allocated). This allows an element to initialize the
259  * region and get the pointer without having to make the call to
260  * getPtr() later. This is particularly helpful if the
261  * SharedRegion is created in an object that doesn't get called
262  * during init() or setup().
263  *
264  * @ptr Pointer that should be set once the size is known. User
265  * can tell pointer is valid when getSize() returns a value != 0.
266  */
267  template<typename T>
268  void getPtrDeferred(T& ptr) {
269  if ( getSize() != 0 ) {
270  // Size is already set, just set the pointer
271  ptr = getPtr<T>();
272  return;
273  }
274  if ( deferred_pointer != nullptr ) return;
275  deferred_pointer = new DeferredPointer<T>(ptr);
276  }
277 
278 
279 };
280 
281 
282 
283 }
284 
285 
286 #endif
void modifyRegion(size_t offset, size_t length, const void *data)
Before the region has published, apply a modification.
Definition: sharedRegion.h:221
Definition: sharedRegion.h:135
bool merge(uint8_t *target, const uint8_t *newData, size_t size) override
Merge the data from &#39;newData&#39; into &#39;target&#39;.
Definition: sharedRegion.cc:78
bool isReady() const
Check to see if the region is ready (all elements requesting the region have called publish())...
Definition: sharedRegion.h:209
Definition: sharedRegion.h:144
void getPtrDeferred(T &ptr)
Used to get a pointer to the const shared data when the element doesn&#39;t yet know the size...
Definition: sharedRegion.h:268
void * getRawPtr()
Definition: sharedRegion.h:239
Utility class to define how to merge multiple pieces of shared memory regions Useful in the multi-MPI...
Definition: sharedRegion.h:34
void publish()
Call to denote that you are done making any changes to this region.
Definition: sharedRegion.h:195
virtual SharedRegion * getLocalSharedRegion(const std::string &key, size_t size, uint8_t initByte=0)=0
Create a SharedRegion that will only be shared with elements on the current rank. ...
size_t getLocalShareID() const
Definition: sharedRegion.h:184
void modifyArray(size_t offset, const T &data)
Before the region has published, apply a modification.
Definition: sharedRegion.h:230
T getPtr() const
Get a pointer to the const shared data.
Definition: sharedRegion.h:249
virtual bool merge(uint8_t *target, const uint8_t *newData, size_t size)
Merge the data from &#39;newData&#39; into &#39;target&#39;.
virtual SharedRegion * getGlobalSharedRegion(const std::string &key, size_t size, SharedRegionMerger *merger=nullptr, uint8_t initByte=0)=0
Create a SharedRegion that will be shared with elements on the all ranks.
Definition: sharedRegion.h:48
size_t getSize() const
Definition: sharedRegion.h:190
Definition: sharedRegion.h:151
Definition: sharedRegion.h:64