00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef SST_CORE_CORE_SHAREDREGIONIMPL_H
00013 #define SST_CORE_CORE_SHAREDREGIONIMPL_H
00014
00015 #include <sst/core/sst_types.h>
00016
00017 #include <string>
00018 #include <vector>
00019 #include <set>
00020 #include <map>
00021
00022 #include <sst/core/sharedRegion.h>
00023
00024 namespace SST {
00025
00026 class SharedRegionImpl;
00027
00028
00029 class RegionInfo {
00030 public:
00031 class RegionMergeInfo {
00032
00033 protected:
00034 friend class boost::serialization::access;
00035 template<class Archive>
00036 void serialize(Archive & ar, const unsigned int version );
00037
00038 RegionMergeInfo() {}
00039 int rank;
00040 std::string key;
00041
00042 public:
00043 RegionMergeInfo(int rank, const std::string &key) : rank(rank), key(key) { }
00044 virtual ~RegionMergeInfo() { }
00045
00046 virtual bool merge(RegionInfo *ri) { return true; }
00047 const std::string& getKey() const { return key; }
00048 };
00049
00050
00051 class BulkMergeInfo : public RegionMergeInfo {
00052 protected:
00053 BulkMergeInfo() : RegionMergeInfo() {}
00054 friend class boost::serialization::access;
00055 template<class Archive>
00056 void serialize(Archive & ar, const unsigned int version );
00057
00058 size_t length;
00059 void *data;
00060
00061 public:
00062 BulkMergeInfo(int rank, const std::string &key, void *data, size_t length) : RegionMergeInfo(rank, key),
00063 length(length), data(data)
00064 { }
00065
00066 bool merge(RegionInfo *ri) {
00067 bool ret = ri->getMerger()->merge((uint8_t*)ri->getMemory(), (const uint8_t*)data, length);
00068 free(data);
00069 return ret;
00070 }
00071 };
00072
00073 class ChangeSetMergeInfo : public RegionMergeInfo {
00074 protected:
00075 friend class boost::serialization::access;
00076 template<class Archive>
00077 void serialize(Archive & ar, const unsigned int version );
00078
00079 ChangeSetMergeInfo() : RegionMergeInfo() {}
00080
00081 std::vector<SharedRegionMerger::ChangeSet> changeSets;
00082 public:
00083 ChangeSetMergeInfo(int rank, const std::string & key,
00084 std::vector<SharedRegionMerger::ChangeSet> & changeSets) : RegionMergeInfo(rank, key),
00085 changeSets(changeSets)
00086 { }
00087 bool merge(RegionInfo *ri) {
00088 return ri->getMerger()->merge((uint8_t*)ri->getMemory(), ri->getSize(), changeSets);
00089 }
00090 };
00091
00092
00093 private:
00094 std::string myKey;
00095 size_t realSize;
00096 size_t apparentSize;
00097 void *memory;
00098
00099 size_t shareCount;
00100 size_t publishCount;
00101
00102 std::vector<SharedRegionImpl*> sharers;
00103
00104 SharedRegionMerger *merger;
00105 std::vector<SharedRegionMerger::ChangeSet> changesets;
00106
00107 bool didBulk;
00108 bool initialized;
00109 bool ready;
00110
00111
00112 public:
00113 RegionInfo() : realSize(0), apparentSize(0), memory(NULL),
00114 shareCount(0), publishCount(0), merger(NULL),
00115 didBulk(false), initialized(false), ready(false)
00116 { }
00117 ~RegionInfo();
00118 bool initialize(const std::string &key, size_t size, uint8_t initByte, SharedRegionMerger *mergeObj);
00119 bool isInitialized() const { return initialized; }
00120 bool isReady() const { return ready; }
00121
00122 SharedRegionImpl* addSharer(SharedRegionManager *manager);
00123 void removeSharer(SharedRegionImpl *sri);
00124
00125
00126 void modifyRegion(size_t offset, size_t length, const void *data);
00127 void publish();
00128
00129 void updateState(bool finalize);
00130
00131 const std::string& getKey() const { return myKey; }
00132 void* getMemory() { didBulk = true; return memory; }
00133 const void* getConstPtr() const { return memory; }
00134 size_t getSize() const { return apparentSize; }
00135 size_t getNumSharers() const { return shareCount; }
00136
00137 bool shouldMerge() const { return (NULL != merger); }
00138 SharedRegionMerger* getMerger() { return merger; }
00139
00140 RegionMergeInfo* getMergeInfo();
00141
00142 void setProtected(bool readOnly);
00143 };
00144
00145
00146 class SharedRegionImpl : public SharedRegion {
00147 bool published;
00148 RegionInfo *region;
00149 public:
00150 SharedRegionImpl(SharedRegionManager *manager, size_t id,
00151 size_t size, RegionInfo *region) : SharedRegion(manager, id, size),
00152 published(false), region(region)
00153 { }
00154
00155 bool isPublished() const { return published; }
00156 void setPublished() { published = true; }
00157 RegionInfo* getRegion() const { return region; }
00158 };
00159
00160
00161
00162 class SharedRegionManagerImpl : public SharedRegionManager {
00163
00164 struct CommInfo_t {
00165 RegionInfo *region;
00166 std::vector<int> tgtRanks;
00167 std::vector<char> sendBuffer;
00168 };
00169
00170 std::map<std::string, RegionInfo> regions;
00171
00172 protected:
00173 void modifyRegion(SharedRegion *sr, size_t offset, size_t length, const void *data);
00174 void* getMemory(SharedRegion *sr);
00175 const void* getConstPtr(const SharedRegion *sr) const;
00176
00177 public:
00178 SharedRegionManagerImpl();
00179 ~SharedRegionManagerImpl();
00180
00181 virtual SharedRegion* getLocalSharedRegion(const std::string &key, size_t size, uint8_t initByte = 0);
00182 virtual SharedRegion* getGlobalSharedRegion(const std::string &key, size_t size, SharedRegionMerger *merger = NULL, uint8_t initByte = 0);
00183
00184 virtual void publishRegion(SharedRegion*);
00185 virtual bool isRegionReady(const SharedRegion*);
00186 virtual void shutdownSharedRegion(SharedRegion*);
00187
00188 void updateState(bool finalize);
00189 };
00190
00191
00192 }
00193 #endif