SST  9.0.0
StructuralSimulationToolkit
sharedRegion.h
1 // Copyright 2009-2019 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-2019, 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 = NULL, 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,
89  size_t size) : manager(manager), id(id),
90  size(size)
91  { }
92 
93 public:
94 
95  virtual ~SharedRegion()
96  { }
97 
98  void shutdown() { manager->shutdownSharedRegion(this); }
99 
100  /**
101  * @return The ID of this instance. (Number in range 0->N)
102  */
103  size_t getLocalShareID() const { return id; }
104 
105  /**
106  * @return The size of the shared memory region
107  */
108  size_t getSize() const { return size; }
109 
110  /**
111  * Call to denote that you are done making any changes to this region
112  */
113  void publish() { manager->publishRegion(this); }
114 
115  /**
116  * @return True if the region is ready to use (all sharers have called publish()).
117  */
118  bool isReady() const { return manager->isRegionReady(this); }
119 
120 
121  /**
122  * Before the region has published, apply a modification. (Copy this data in)
123  */
124  void modifyRegion(size_t offset, size_t length, const void *data)
125  { manager->modifyRegion(this, offset, length, data); }
126 
127  template<typename T>
128  void modifyArray(size_t offset, const T &data)
129  { manager->modifyRegion(this, offset*sizeof(T), sizeof(T), &data); }
130 
131  /**
132  * @return a void* pointer to the shared memory region
133  * This pointer is only valid to write to before a call to publish()
134  */
135  void* getRawPtr() { return manager->getMemory(this); }
136 
137  /**
138  * @return a const pointer to the shared memory region
139  */
140  template<typename T>
141  T getPtr() const {
142  return static_cast<T>(manager->getConstPtr(this));
143  }
144 };
145 
146 
147 
148 }
149 
150 
151 #endif
152 
void modifyRegion(size_t offset, size_t length, const void *data)
Before the region has published, apply a modification.
Definition: sharedRegion.h:124
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:78
bool isReady() const
Definition: sharedRegion.h:118
void * getRawPtr()
Definition: sharedRegion.h:135
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:113
size_t getLocalShareID() const
Definition: sharedRegion.h:103
T getPtr() const
Definition: sharedRegion.h:141
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:108
Definition: sharedRegion.h:60