XdmfMap.cpp 8.78 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include "XdmfAttribute.hpp"
#include "XdmfGridCollection.hpp"
#include "XdmfGridCollectionType.hpp"
#include "XdmfHDF5Controller.hpp"
#include "XdmfMap.hpp"

/**
 * PIMPL
 */
class XdmfMap::XdmfMapImpl {

public:

	XdmfMapImpl() :
		mLocalNodeIdsHDF5Controller(boost::shared_ptr<XdmfHDF5Controller>()),
		mRemoteLocalNodeIdsHDF5Controller(boost::shared_ptr<XdmfHDF5Controller>()),
		mRemoteTaskIdsHDF5Controller(boost::shared_ptr<XdmfHDF5Controller>())
	{
	};

	~XdmfMapImpl()
	{
	};

	boost::shared_ptr<XdmfHDF5Controller> mLocalNodeIdsHDF5Controller;
	std::map<unsigned int, std::map<unsigned int, unsigned int> > mMap;	// localNodeId | remoteTaskId | remoteLocalNodeId
	boost::shared_ptr<XdmfHDF5Controller> mRemoteLocalNodeIdsHDF5Controller;
	boost::shared_ptr<XdmfHDF5Controller> mRemoteTaskIdsHDF5Controller;

};

boost::shared_ptr<XdmfMap> XdmfMap::New()
{
	boost::shared_ptr<XdmfMap> p(new XdmfMap());
	return p;
}

std::vector<boost::shared_ptr<XdmfMap> > XdmfMap::New(const std::vector<boost::shared_ptr<XdmfAttribute> > & globalNodeIds)
{
	unsigned int maxGlobalNodeId = 0;
	for(unsigned int i=0; i<globalNodeIds.size(); ++i)
	{
		const boost::shared_ptr<XdmfAttribute> currGlobalNodeIds = globalNodeIds[i];
44
		for(unsigned int j=0; j<currGlobalNodeIds->getSize(); ++j)
45
		{
46
			unsigned int currGlobalNodeId = currGlobalNodeIds->getValue<unsigned int>(j);
47 48 49 50 51 52 53 54 55 56 57 58 59 60
			if(currGlobalNodeId > maxGlobalNodeId)
			{
				maxGlobalNodeId = currGlobalNodeId;
			}
		}
	}

	// globalNodeId | taskId | localNodeId at taskId
	std::vector<std::map<unsigned int, unsigned int> > globalNodeIdMap(maxGlobalNodeId + 1, std::map<unsigned int, unsigned int>());

	// Fill globalNodeIdMap
	for(unsigned int i=0; i<globalNodeIds.size(); ++i)
	{
		const boost::shared_ptr<XdmfAttribute> currGlobalNodeIds = globalNodeIds[i];
61
		for(unsigned int j=0; j<currGlobalNodeIds->getSize(); ++j)
62
		{
63
			unsigned int currGlobalNodeId = currGlobalNodeIds->getValue<unsigned int>(j);
64 65 66 67 68 69 70 71 72 73 74 75
			globalNodeIdMap[currGlobalNodeId][i] = j;
		}
	}

	std::vector<boost::shared_ptr<XdmfMap> > toReturn;
	toReturn.reserve(globalNodeIds.size());
	// Fill maps for each partition
	for(unsigned int i=0; i<globalNodeIds.size(); ++i)
	{
		boost::shared_ptr<XdmfMap> map = XdmfMap::New();
		toReturn.push_back(map);
		const boost::shared_ptr<XdmfAttribute> currGlobalNodeIds = globalNodeIds[i];
76
		for(unsigned int j=0; j<currGlobalNodeIds->getSize(); ++j)
77
		{
78
			unsigned int currGlobalNodeId = currGlobalNodeIds->getValue<unsigned int>(j);
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
			if(globalNodeIdMap[currGlobalNodeId].size() > 1)
			{
				for(std::map<unsigned int, unsigned int>::const_iterator iter = globalNodeIdMap[currGlobalNodeId].begin(); iter != globalNodeIdMap[currGlobalNodeId].end(); ++iter)
				{
					if(iter->first != i)
					{
						map->insert(j, iter->first, iter->second);
					}
				}
			}
		}
	}

	return toReturn;
}

XdmfMap::XdmfMap() :
	mImpl(new XdmfMapImpl())
{
}

XdmfMap::~XdmfMap()
{
	delete mImpl;
}

const std::string XdmfMap::ItemTag = "Map";

std::map<std::string, std::string> XdmfMap::getItemProperties() const
{
	std::map<std::string, std::string> mapProperties;
	return mapProperties;
}

std::string XdmfMap::getItemTag() const
{
	return ItemTag;
}

std::map<unsigned int, unsigned int> XdmfMap::getRemoteNodeIds(const unsigned int localNodeId)
{
	std::map<unsigned int, std::map<unsigned int, unsigned int> >::const_iterator iter = mImpl->mMap.find(localNodeId);
	if(iter != mImpl->mMap.end())
	{
		return iter->second;
	}
	// No entry, return empty map.
	return std::map<unsigned int, unsigned int>();
}

void XdmfMap::insert(const unsigned int localNodeId, const unsigned int remoteTaskId, const unsigned int remoteLocalNodeId)
{
	std::map<unsigned int, std::map<unsigned int, unsigned int> >::iterator iter = mImpl->mMap.find(localNodeId);
	if(iter != mImpl->mMap.end())
	{
		iter->second[remoteTaskId] = remoteLocalNodeId;
	}
	else
	{
		std::map<unsigned int, unsigned int> newMap;
		newMap[remoteTaskId] = remoteLocalNodeId;
		mImpl->mMap[localNodeId] = newMap;
	}
}

144 145 146 147 148
bool XdmfMap::isInitialized() const
{
	return mImpl->mMap.size() > 0;
}

149
void XdmfMap::populateItem(const std::map<std::string, std::string> & itemProperties, std::vector<boost::shared_ptr<XdmfItem> > & childItems, const XdmfCoreReader * const reader)
150
{
151
	XdmfItem::populateItem(itemProperties, childItems, reader);
152 153 154 155 156 157 158 159 160 161
	std::vector<boost::shared_ptr<XdmfArray> > arrayVector;
	arrayVector.reserve(3);
	for(std::vector<boost::shared_ptr<XdmfItem> >::const_iterator iter = childItems.begin(); iter != childItems.end(); ++iter)
	{
		if(boost::shared_ptr<XdmfArray> array = boost::shared_dynamic_cast<XdmfArray>(*iter))
		{
			arrayVector.push_back(array);
		}
	}
	assert(arrayVector.size() == 3);
162
	assert(arrayVector[0]->getSize() == arrayVector[1]->getSize() &&  arrayVector[0]->getSize() == arrayVector[2]->getSize());
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
	bool needToRead = false;
	for(std::vector<boost::shared_ptr<XdmfArray> >::const_iterator iter = arrayVector.begin(); iter != arrayVector.end(); ++iter)
	{
		if((*iter)->isInitialized())
		{
			needToRead = true;
			break;
		}
	}
	if(needToRead)
	{
		for(std::vector<boost::shared_ptr<XdmfArray> >::const_iterator iter = arrayVector.begin(); iter != arrayVector.end(); ++iter)
		{
			(*iter)->read();
		}
178
		for(unsigned int i=0; i<arrayVector[0]->getSize(); ++i)
179
		{
180
			this->insert(arrayVector[0]->getValue<unsigned int>(i), arrayVector[1]->getValue<unsigned int>(i), arrayVector[2]->getValue<unsigned int>(i));
181 182 183 184 185 186 187 188 189 190 191 192
		}
	}
	else
	{
		mImpl->mLocalNodeIdsHDF5Controller = arrayVector[0]->getHDF5Controller();
		mImpl->mRemoteTaskIdsHDF5Controller = arrayVector[1]->getHDF5Controller();
		mImpl->mRemoteLocalNodeIdsHDF5Controller = arrayVector[2]->getHDF5Controller();
	}
}

void XdmfMap::read()
{
193
	if(mImpl->mLocalNodeIdsHDF5Controller && mImpl->mRemoteTaskIdsHDF5Controller && mImpl->mRemoteLocalNodeIdsHDF5Controller)
194
	{
195
		assert(mImpl->mLocalNodeIdsHDF5Controller->getSize() == mImpl->mRemoteTaskIdsHDF5Controller->getSize() && mImpl->mLocalNodeIdsHDF5Controller->getSize() == mImpl->mRemoteLocalNodeIdsHDF5Controller->getSize());
196 197 198 199 200 201 202
		boost::shared_ptr<XdmfArray> globalNodeIds = XdmfArray::New();
		boost::shared_ptr<XdmfArray> taskIds = XdmfArray::New();
		boost::shared_ptr<XdmfArray> localNodeIds = XdmfArray::New();
		mImpl->mLocalNodeIdsHDF5Controller->read(globalNodeIds.get());
		mImpl->mRemoteTaskIdsHDF5Controller->read(taskIds.get());
		mImpl->mRemoteLocalNodeIdsHDF5Controller->read(localNodeIds.get());

203
		for(unsigned int i=0; i<globalNodeIds->getSize(); ++i)
204
		{
205
			std::map<unsigned int, std::map<unsigned int, unsigned int> >::iterator iter = mImpl->mMap.find(globalNodeIds->getValue<unsigned int>(i));
206 207
			if(iter != mImpl->mMap.end())
			{
208
				iter->second[taskIds->getValue<unsigned int>(i)] = localNodeIds->getValue<unsigned int>(i);
209 210 211 212
			}
			else
			{
				std::map<unsigned int, unsigned int> newMap;
213 214
				newMap[taskIds->getValue<unsigned int>(i)] = localNodeIds->getValue<unsigned int>(i);
				mImpl->mMap[globalNodeIds->getValue<unsigned int>(i)] = newMap;
215 216 217 218 219 220 221
			}
		}
	}
}

void XdmfMap::release()
{
222
	mImpl->mMap.clear();
223 224
}

225 226
void XdmfMap::setHDF5Controllers(boost::shared_ptr<XdmfHDF5Controller> localNodeIdsHDF5Controller, boost::shared_ptr<XdmfHDF5Controller> remoteTaskIdsHDF5Controller, boost::shared_ptr<XdmfHDF5Controller> remoteLocalNodeIdsHDF5Controller)
{
227
	assert(localNodeIdsHDF5Controller->getSize() == remoteTaskIdsHDF5Controller->getSize() && localNodeIdsHDF5Controller->getSize() == remoteLocalNodeIdsHDF5Controller->getSize());
228 229 230 231 232 233
	mImpl->mLocalNodeIdsHDF5Controller = localNodeIdsHDF5Controller;
	mImpl->mRemoteTaskIdsHDF5Controller = remoteTaskIdsHDF5Controller;
	mImpl->mRemoteLocalNodeIdsHDF5Controller = remoteLocalNodeIdsHDF5Controller;
}


234 235
void XdmfMap::traverse(const boost::shared_ptr<XdmfBaseVisitor> visitor)
{
236
	XdmfItem::traverse(visitor);
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
	boost::shared_ptr<XdmfArray> localNodeIds = XdmfArray::New();
	boost::shared_ptr<XdmfArray> remoteTaskIds = XdmfArray::New();
	boost::shared_ptr<XdmfArray> remoteLocalNodeIds = XdmfArray::New();
	for(std::map<unsigned int, std::map<unsigned int, unsigned int> >::const_iterator iter = mImpl->mMap.begin(); iter != mImpl->mMap.end(); ++iter)
	{
		for(std::map<unsigned int, unsigned int>::const_iterator iter2 = iter->second.begin(); iter2 != iter->second.end(); ++iter2)
		{
			localNodeIds->pushBack(iter->first);
			remoteTaskIds->pushBack(iter2->first);
			remoteLocalNodeIds->pushBack(iter2->second);
		}
	}
	localNodeIds->setHDF5Controller(mImpl->mLocalNodeIdsHDF5Controller);
	remoteTaskIds->setHDF5Controller(mImpl->mRemoteTaskIdsHDF5Controller);
	remoteLocalNodeIds->setHDF5Controller(mImpl->mRemoteLocalNodeIdsHDF5Controller);
	localNodeIds->accept(visitor);
	remoteTaskIds->accept(visitor);
	remoteLocalNodeIds->accept(visitor);
	mImpl->mLocalNodeIdsHDF5Controller = localNodeIds->getHDF5Controller();
	mImpl->mRemoteTaskIdsHDF5Controller = remoteTaskIds->getHDF5Controller();
	mImpl->mRemoteLocalNodeIdsHDF5Controller = remoteLocalNodeIds->getHDF5Controller();
}