SST  6.1.0
StructuralSimulationToolkit
sharedRegion.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_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);
52 
53 };
54 
55 
57 protected:
58  friend class SharedRegion;
59 
60  virtual void modifyRegion(SharedRegion *sr, size_t offset, size_t length, const void *data) = 0;
61  virtual void* getMemory(SharedRegion* sr) = 0;
62  virtual const void* getConstPtr(const SharedRegion* sr) const = 0;
63 
64 public:
65  virtual SharedRegion* getLocalSharedRegion(const std::string &key, size_t size, uint8_t initByte = 0) = 0;
66  virtual SharedRegion* getGlobalSharedRegion(const std::string &key, size_t size, SharedRegionMerger *merger = NULL, uint8_t initByte = 0) = 0;
67 
68  virtual void publishRegion(SharedRegion*) = 0;
69  virtual bool isRegionReady(const SharedRegion*) = 0;
70  virtual void shutdownSharedRegion(SharedRegion*) = 0;
71 
72  virtual void updateState(bool finalize) = 0;
73 };
74 
75 
76 
77 class SharedRegion {
78 private:
79  SharedRegionManager *manager;
80  size_t id;
81  size_t size;
82 
83 protected:
84  SharedRegion(SharedRegionManager *manager, size_t id,
85  size_t size) : manager(manager), id(id),
86  size(size)
87  { }
88 
89 public:
90 
91  virtual ~SharedRegion()
92  { }
93 
94  void shutdown() { manager->shutdownSharedRegion(this); }
95 
96  /**
97  * @return The ID of this instance. (Number in range 0->N)
98  */
99  size_t getLocalShareID() const { return id; }
100 
101  /**
102  * @return The size of the shared memory region
103  */
104  size_t getSize() const { return size; }
105 
106  /**
107  * Call to denote that you are done making any changes to this region
108  */
109  void publish() { manager->publishRegion(this); }
110 
111  /**
112  * @return True if the region is ready to use (all sharers have called publish()).
113  */
114  bool isReady() const { return manager->isRegionReady(this); }
115 
116 
117  /**
118  * Before the region has published, apply a modification. (Copy this data in)
119  */
120  void modifyRegion(size_t offset, size_t length, const void *data)
121  { manager->modifyRegion(this, offset, length, data); }
122 
123  template<typename T>
124  void modifyArray(size_t offset, const T &data)
125  { manager->modifyRegion(this, offset*sizeof(T), sizeof(T), &data); }
126 
127  /**
128  * @return a void* pointer to the shared memory region
129  * This pointer is only valid to write to before a call to publish()
130  */
131  void* getRawPtr() { return manager->getMemory(this); }
132 
133  /**
134  * @return a const pointer to the shared memory region
135  */
136  template<typename T>
137  T getPtr() const {
138  return static_cast<T>(manager->getConstPtr(this));
139  }
140 };
141 
142 
143 
144 }
145 
146 
147 #endif
148 
void modifyRegion(size_t offset, size_t length, const void *data)
Before the region has published, apply a modification.
Definition: sharedRegion.h:120
Definition: sharedRegion.h:77
bool isReady() const
Definition: sharedRegion.h:114
bool merge(uint8_t *target, const uint8_t *newData, size_t size)
Merge the data from 'newData' into 'target'.
Definition: sharedRegion.cc:75
void * getRawPtr()
Definition: sharedRegion.h:131
Utility class to define how to merge multiple pieces of shared memory regions Useful in the multi-MPI...
Definition: sharedRegion.h:30
Definition: action.cc:17
void publish()
Call to denote that you are done making any changes to this region.
Definition: sharedRegion.h:109
size_t getLocalShareID() const
Definition: sharedRegion.h:99
T getPtr() const
Definition: sharedRegion.h:137
Definition: sharedRegion.h:44
virtual bool merge(uint8_t *target, const uint8_t *newData, size_t size)
Merge the data from 'newData' into 'target'.
Definition: sharedRegion.cc:31
size_t getSize() const
Definition: sharedRegion.h:104
Definition: sharedRegion.h:56