SST  6.0.0
StructuralSimulationToolkit
sharedRegionImpl.h
1 // Copyright 2009-2016 Sandia Corporation. Under the terms
2 // of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S.
3 // Government retains certain rights in this software.
4 //
5 // Copyright (c) 2009-2016, Sandia Corporation
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_SHAREDREGIONIMPL_H
13 #define SST_CORE_CORE_SHAREDREGIONIMPL_H
14 
15 #include <sst/core/sst_types.h>
16 
17 #include <string>
18 #include <vector>
19 #include <set>
20 #include <map>
21 #include <mutex>
22 
23 #include <sst/core/sharedRegion.h>
24 #include <sst/core/serialization/serializable.h>
25 //#include <sst/core/changeSet.h>
26 
27 namespace SST {
28 
29 class SharedRegionImpl;
30 
32 public:
33  ChangeSet() { }
34  size_t offset;
35  size_t length;
36  /*const*/ uint8_t *data;
37 
38  ChangeSet(size_t offset, size_t length, /*const*/ uint8_t *data = NULL) : offset(offset), length(length), data(data) { }
39 
40  void serialize_order(SST::Core::Serialization::serializer &ser) {
41  ser & offset;
42  // ser & length;
43  // if ( ser.mode() == SST::Core::Serialization::serializer::UNPACK ) {
44  // data = new uint8_t[length];
45  // }
46  // for ( int i = 0; i < length; i++ ) {
47  // ser & data[i];
48  // }
49  ser & SST::Core::Serialization::array(data,length);
50  // ser.binary(data,length);
51 
52  }
53 
54  ImplementSerializable(SST::ChangeSet)
55 
56 
57 };
58 
59 class RegionInfo {
60 public:
62 
63  protected:
64  int rank;
65  std::string key;
66 
67  public:
68  RegionMergeInfo() {}
69  RegionMergeInfo(int rank, const std::string &key) : rank(rank), key(key) { }
70  virtual ~RegionMergeInfo() { }
71 
72  virtual bool merge(RegionInfo *ri) { return true; }
73  const std::string& getKey() const { return key; }
74 
75  void serialize_order(SST::Core::Serialization::serializer &ser) {
76  ser & rank;
77  ser & key;
78  }
79 
80  ImplementSerializable(SST::RegionInfo::RegionMergeInfo)
81  };
82 
83 
84  class BulkMergeInfo : public RegionMergeInfo {
85  protected:
86  size_t length;
87  void *data;
88 
89  public:
91  BulkMergeInfo(int rank, const std::string &key, void *data, size_t length) : RegionMergeInfo(rank, key),
92  length(length), data(data)
93  { }
94 
95  bool merge(RegionInfo *ri) {
96  bool ret = ri->getMerger()->merge((uint8_t*)ri->getMemory(), (const uint8_t*)data, length);
97  free(data);
98  return ret;
99  }
100 
101  void serialize_order(SST::Core::Serialization::serializer &ser) {
102  RegionInfo::RegionMergeInfo::serialize_order(ser);
103 
104  ser & length;
105  // if ( ser.mode() == SST::Core::Serialization::serializer::UNPACK ) {
106  // data = malloc(length);
107  // }
108  // for ( int i = 0; i < length; i++ ) {
109  // ser & ((uint8_t*)data)[i];
110  // }
111  ser & SST::Core::Serialization::array(data,length);
112  // ser.binary(data,length);
113  }
114 
115  ImplementSerializable(SST::RegionInfo::BulkMergeInfo)
116  };
117 
119  protected:
120  // std::vector<SharedRegionMerger::ChangeSet> changeSets;
121  std::vector<ChangeSet> changeSets;
122  public:
124  ChangeSetMergeInfo(int rank, const std::string & key,
125  // std::vector<SharedRegionMerger::ChangeSet> & changeSets) : RegionMergeInfo(rank, key),
126  std::vector<ChangeSet> & changeSets) : RegionMergeInfo(rank, key),
127  changeSets(changeSets)
128  { }
129  bool merge(RegionInfo *ri) {
130  return ri->getMerger()->merge((uint8_t*)ri->getMemory(), ri->getSize(), changeSets);
131  }
132 
133  void serialize_order(SST::Core::Serialization::serializer &ser) {
134  RegionInfo::RegionMergeInfo::serialize_order(ser);
135  ser & changeSets;
136  }
137 
138  ImplementSerializable(SST::RegionInfo::ChangeSetMergeInfo)
139  };
140 
141 
142 private:
143  std::string myKey;
144  size_t realSize;
145  size_t apparentSize;
146  void *memory;
147 
148  size_t shareCount;
149  size_t publishCount;
150 
151  std::vector<SharedRegionImpl*> sharers;
152 
153  SharedRegionMerger *merger; // If null, no multi-rank merging
154  // std::vector<SharedRegionMerger::ChangeSet> changesets;
155  std::vector<ChangeSet> changesets;
156 
157  bool didBulk;
158  bool initialized;
159  bool ready;
160 
161 
162 public:
163  RegionInfo() : realSize(0), apparentSize(0), memory(NULL),
164  shareCount(0), publishCount(0), merger(NULL),
165  didBulk(false), initialized(false), ready(false)
166  { }
167  ~RegionInfo();
168  bool initialize(const std::string &key, size_t size, uint8_t initByte, SharedRegionMerger *mergeObj);
169  bool isInitialized() const { return initialized; }
170  bool isReady() const { return ready; }
171 
172  SharedRegionImpl* addSharer(SharedRegionManager *manager);
173  void removeSharer(SharedRegionImpl *sri);
174 
175 
176  void modifyRegion(size_t offset, size_t length, const void *data);
177  void publish();
178 
179  void updateState(bool finalize);
180 
181  const std::string& getKey() const { return myKey; }
182  void* getMemory() { didBulk = true; return memory; }
183  const void* getConstPtr() const { return memory; }
184  size_t getSize() const { return apparentSize; }
185  size_t getNumSharers() const { return shareCount; }
186 
187  bool shouldMerge() const { return (NULL != merger); }
188  SharedRegionMerger* getMerger() { return merger; }
189  /** Returns the size of the data to be transferred */
190  RegionMergeInfo* getMergeInfo();
191 
192  void setProtected(bool readOnly);
193 };
194 
195 
197  bool published;
198  RegionInfo *region;
199 public:
200  SharedRegionImpl(SharedRegionManager *manager, size_t id,
201  size_t size, RegionInfo *region) : SharedRegion(manager, id, size),
202  published(false), region(region)
203  { }
204 
205  bool isPublished() const { return published; }
206  void setPublished() { published = true; }
207  RegionInfo* getRegion() const { return region; }
208 };
209 
210 
211 
213 
214  struct CommInfo_t {
215  RegionInfo *region;
216  std::vector<int> tgtRanks;
217  std::vector<char> sendBuffer;
218  };
219 
220  std::map<std::string, RegionInfo> regions;
221  std::mutex mtx;
222 
223 protected:
224  void modifyRegion(SharedRegion *sr, size_t offset, size_t length, const void *data);
225  void* getMemory(SharedRegion *sr);
226  const void* getConstPtr(const SharedRegion *sr) const;
227 
228 public:
231 
232  virtual SharedRegion* getLocalSharedRegion(const std::string &key, size_t size, uint8_t initByte = 0);
233  virtual SharedRegion* getGlobalSharedRegion(const std::string &key, size_t size, SharedRegionMerger *merger = NULL, uint8_t initByte = 0);
234 
235  virtual void publishRegion(SharedRegion*);
236  virtual bool isRegionReady(const SharedRegion*);
237  virtual void shutdownSharedRegion(SharedRegion*);
238 
239  void updateState(bool finalize);
240 };
241 
242 
243 }
244 #endif
Definition: sharedRegion.h:77
Definition: sharedRegionImpl.h:212
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:35
Definition: sharedRegionImpl.h:31
Definition: sharedRegionImpl.h:196
Utility class to define how to merge multiple pieces of shared memory regions Useful in the multi-MPI...
Definition: sharedRegion.h:30
RegionMergeInfo * getMergeInfo()
Returns the size of the data to be transferred.
Definition: sharedRegion.cc:187
Definition: action.cc:17
Definition: serializable.h:108
Definition: sharedRegionImpl.h:59
virtual bool merge(uint8_t *target, const uint8_t *newData, size_t size)
Merge the data from 'newData' into 'target'.
Definition: sharedRegion.cc:31
Definition: sharedRegionImpl.h:118
Definition: sharedRegionImpl.h:61
Definition: sharedRegion.h:56
Definition: sharedRegionImpl.h:84