SST  10.1.0
StructuralSimulationToolkit
sharedRegion.h
1 // Copyright 2009-2020 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-2020, 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/sst_types.h"
16 
17 #include <string>
18 #include <vector>
19 
20 
21 namespace SST {
22 
23 class SharedRegion;
24 class ChangeSet;
25 
26 /**
27  * Utility class to define how to merge multiple pieces of shared memory regions
28  * Useful in the multi-MPI-rank, "Global Shared" model
29  */
31 public:
32 
33  virtual ~SharedRegionMerger() { }
34 
35  /**
36  * Merge the data from 'newData' into 'target'
37  * @return True on success, False on failure
38  */
39  virtual bool merge(uint8_t *target, const uint8_t *newData, size_t size);
40  virtual bool merge(uint8_t *target, size_t size, const std::vector<ChangeSet> &changeSets);
41 };
42 
43 
45  uint8_t defVal;
46 public:
47  SharedRegionInitializedMerger(uint8_t defaultValue) {
48  defVal = defaultValue;
49  }
50 
51  bool merge(uint8_t *target, const uint8_t *newData, size_t size) override;
52 
53  bool merge(uint8_t *target, size_t size, const std::vector<ChangeSet> &changeSets) override {
54  return SharedRegionMerger::merge(target,size,changeSets);
55  }
56 
57 };
58 
59 
61 protected:
62  friend class SharedRegion;
63 
64  virtual void modifyRegion(SharedRegion *sr, size_t offset, size_t length, const void *data) = 0;
65  virtual void* getMemory(SharedRegion* sr) = 0;
66  virtual const void* getConstPtr(const SharedRegion* sr) const = 0;
67 
68 public:
69  virtual SharedRegion* getLocalSharedRegion(const std::string& key, size_t size, uint8_t initByte = 0) = 0;
70  virtual SharedRegion* getGlobalSharedRegion(const std::string& key, size_t size, SharedRegionMerger *merger = nullptr, uint8_t initByte = 0) = 0;
71 
72  virtual void publishRegion(SharedRegion*) = 0;
73  virtual bool isRegionReady(const SharedRegion*) = 0;
74  virtual void shutdownSharedRegion(SharedRegion*) = 0;
75 
76  virtual void updateState(bool finalize) = 0;
77 };
78 
79 
80 
81 class SharedRegion {
82 private:
83  SharedRegionManager *manager;
84  size_t id;
85  size_t size;
86 
87 protected:
88  SharedRegion(SharedRegionManager *manager, size_t id, size_t size) :
89  manager(manager),
90  id(id),
91  size(size)
92  { }
93 
94 public:
95 
96  virtual ~SharedRegion()
97  { }
98 
99  void shutdown() { manager->shutdownSharedRegion(this); }
100 
101  /**
102  * @return The ID of this instance. (Number in range 0->N)
103  */
104  size_t getLocalShareID() const { return id; }
105 
106  /**
107  * @return The size of the shared memory region
108  */
109  size_t getSize() const { return size; }
110 
111  /**
112  * Call to denote that you are done making any changes to this region
113  */
114  void publish() { manager->publishRegion(this); }
115 
116  /**
117  * @return True if the region is ready to use (all sharers have called publish()).
118  */
119  bool isReady() const { return manager->isRegionReady(this); }
120 
121 
122  /**
123  * Before the region has published, apply a modification. (Copy this data in)
124  */
125  void modifyRegion(size_t offset, size_t length, const void *data)
126  { manager->modifyRegion(this, offset, length, data); }
127 
128  template<typename T>
129  void modifyArray(size_t offset, const T &data)
130  { manager->modifyRegion(this, offset*sizeof(T), sizeof(T), &data); }
131 
132  /**
133  * @return a void* pointer to the shared memory region
134  * This pointer is only valid to write to before a call to publish()
135  */
136  void* getRawPtr() { return manager->getMemory(this); }
137 
138  /**
139  * @return a const pointer to the shared memory region
140  */
141  template<typename T>
142  T getPtr() const {
143  return static_cast<T>(manager->getConstPtr(this));
144  }
145 };
146 
147 
148 
149 }
150 
151 
152 #endif
153 
void modifyRegion(size_t offset, size_t length, const void *data)
Before the region has published, apply a modification.
Definition: sharedRegion.h:125
Definition: sharedRegion.h:81
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:77
bool isReady() const
Definition: sharedRegion.h:119
void * getRawPtr()
Definition: sharedRegion.h:136
Utility class to define how to merge multiple pieces of shared memory regions Useful in the multi-MPI...
Definition: sharedRegion.h:30
void publish()
Call to denote that you are done making any changes to this region.
Definition: sharedRegion.h:114
size_t getLocalShareID() const
Definition: sharedRegion.h:104
T getPtr() const
Definition: sharedRegion.h:142
virtual bool merge(uint8_t *target, const uint8_t *newData, size_t size)
Merge the data from &#39;newData&#39; into &#39;target&#39;.
Definition: sharedRegion.h:44
size_t getSize() const
Definition: sharedRegion.h:109
Definition: sharedRegion.h:60