XdmfMap.cpp 23.3 KB
Newer Older
Kenneth Leiter's avatar
Kenneth Leiter committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*****************************************************************************/
/*                                    XDMF                                   */
/*                       eXtensible Data Model and Format                    */
/*                                                                           */
/*  Id : XdmfMap.cpp                                                         */
/*                                                                           */
/*  Author:                                                                  */
/*     Kenneth Leiter                                                        */
/*     kenneth.leiter@arl.army.mil                                           */
/*     US Army Research Laboratory                                           */
/*     Aberdeen Proving Ground, MD                                           */
/*                                                                           */
/*     Copyright @ 2011 US Army Research Laboratory                          */
/*     All Rights Reserved                                                   */
/*     See Copyright.txt for details                                         */
/*                                                                           */
/*     This software is distributed WITHOUT ANY WARRANTY; without            */
/*     even the implied warranty of MERCHANTABILITY or FITNESS               */
/*     FOR A PARTICULAR PURPOSE.  See the above copyright notice             */
/*     for more information.                                                 */
/*                                                                           */
/*****************************************************************************/

24
#include <utility>
25
#include <string.h>
26
#include "XdmfAttribute.hpp"
27
#include "XdmfError.hpp"
28 29
#include "XdmfGridCollection.hpp"
#include "XdmfGridCollectionType.hpp"
30
#include "XdmfHeavyDataController.hpp"
31
#include "XdmfMap.hpp"
32
#include "XdmfWriter.hpp"
33

34
shared_ptr<XdmfMap>
35
XdmfMap::New()
36
{
37
  shared_ptr<XdmfMap> p(new XdmfMap());
38
  return p;
39 40
}

41 42
std::vector<shared_ptr<XdmfMap> >
XdmfMap::New(const std::vector<shared_ptr<XdmfAttribute> > & globalNodeIds)
43
{
44
  // globalNodeId | taskId | localNodeId at taskId
45
  std::map<node_id, std::map<task_id, node_id> > globalNodeIdMap;
46

47
  // fill globalNodeIdMap using globalNodeIds
48
  std::vector<bool> releaseGlobalNodeIds(globalNodeIds.size(), false);
49
  for(unsigned int i=0; i<globalNodeIds.size(); ++i) {
50
    const shared_ptr<XdmfAttribute> currGlobalNodeIds = globalNodeIds[i];
51 52 53 54
    if(!currGlobalNodeIds->isInitialized()) {
      currGlobalNodeIds->read();
      releaseGlobalNodeIds[i] = true;
    }
55
    for(unsigned int j=0; j<currGlobalNodeIds->getSize(); ++j) {
56
      const node_id currGlobalNodeId = currGlobalNodeIds->getValue<node_id>(j);
57 58 59
      globalNodeIdMap[currGlobalNodeId][i] = j;
    }
  }
60

61
  std::vector<shared_ptr<XdmfMap> > returnValue;
62 63 64
  returnValue.resize(globalNodeIds.size());

  // fill maps for each partition
65
  for(unsigned int i=0; i<globalNodeIds.size(); ++i)  {
66
    shared_ptr<XdmfMap> map = XdmfMap::New();
67
    returnValue[i] = map;
68 69
    const shared_ptr<XdmfAttribute> currGlobalNodeIds = globalNodeIds[i];

70
    for(unsigned int j=0; j<currGlobalNodeIds->getSize(); ++j) {
71 72 73 74
      const node_id currGlobalNodeId = currGlobalNodeIds->getValue<node_id>(j);
      const std::map<task_id, node_id> & currMap = 
        globalNodeIdMap[currGlobalNodeId];
      if(currMap.size() > 1) {
75
        for(std::map<task_id, node_id>::const_iterator iter = currMap.begin();
76
            iter != currMap.end();
77
            ++iter) {
78
          if(iter->first != (int)i) {
79
            map->insert(iter->first, j, iter->second);
80 81 82 83
          }
        }
      }
    }
84 85 86
    if(releaseGlobalNodeIds[i]) {
      currGlobalNodeIds->release();
    }
87
  }
88

89
  return returnValue;
90 91
}

92 93
XdmfMap::XdmfMap() :
  mName("")
94 95 96
{
}

97 98 99 100 101 102 103 104 105
XdmfMap::XdmfMap(XdmfMap & refMap):
  mLocalNodeIdsControllers(refMap.mLocalNodeIdsControllers),
  mMap(refMap.mMap),
  mName(refMap.mName),
  mRemoteLocalNodeIdsControllers(refMap.mRemoteLocalNodeIdsControllers),
  mRemoteTaskIdsControllers(refMap.mRemoteTaskIdsControllers)
{
}

106 107 108 109 110 111
XdmfMap::~XdmfMap()
{
}

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

112 113
std::map<std::string, std::string>
XdmfMap::getItemProperties() const
114
{
115
  std::map<std::string, std::string> mapProperties;
116
  mapProperties.insert(std::make_pair("Name", mName));
117
  return mapProperties;
118 119
}

120 121
std::string
XdmfMap::getItemTag() const
122
{
123
  return ItemTag;
124 125
}

126
std::map<XdmfMap::task_id, XdmfMap::node_id_map>
127
XdmfMap::getMap() const
128
{
129 130 131
  return mMap;
}

132 133 134 135 136 137
std::string
XdmfMap::getName() const
{
  return mName;
}

138 139 140
XdmfMap::node_id_map
XdmfMap::getRemoteNodeIds(const task_id remoteTaskId)
{
141
  std::map<task_id, node_id_map>::const_iterator iter =
142
    mMap.find(remoteTaskId);
143 144 145 146
  if(iter != mMap.end()) {
    return iter->second;
  }
  // No entry, return empty map.
147
  return node_id_map();
148 149
}

150
void
151 152 153
XdmfMap::insert(const task_id remoteTaskId,
                const node_id localNodeId,
                const node_id remoteLocalNodeId)
154
{
155
  mMap[remoteTaskId][localNodeId].insert(remoteLocalNodeId);
156
  this->setIsChanged(true);
157 158
}

159 160
bool XdmfMap::isInitialized() const
{
161
  return mMap.size() > 0;
162 163
}

164 165
void
XdmfMap::populateItem(const std::map<std::string, std::string> & itemProperties,
166
                      const std::vector<shared_ptr<XdmfItem> > & childItems,
167
                      const XdmfCoreReader * const reader)
168
{
169
  XdmfItem::populateItem(itemProperties, childItems, reader);
170 171 172 173 174 175 176 177
  std::map<std::string, std::string>::const_iterator name =
    itemProperties.find("Name");
  if(name != itemProperties.end()) {
    mName = name->second;
  }
  else {
    mName = "";
  }
178
  std::vector<shared_ptr<XdmfArray> > arrayVector;
179
  arrayVector.reserve(3);
180
  for(std::vector<shared_ptr<XdmfItem> >::const_iterator iter =
181 182 183
        childItems.begin();
      iter != childItems.end();
      ++iter) {
184 185 186
    if(shared_ptr<XdmfArray> array = shared_dynamic_cast<XdmfArray>(*iter)) {
      arrayVector.push_back(array);
    }
187
  }
188

189 190
  if(arrayVector.size() != 0) {
    if(arrayVector.size() != 3) {
191 192 193
      XdmfError::message(XdmfError::FATAL,
                         "Expected 3 arrays attached to "
                         "XdmfMap::populateItem");
194 195 196
    }
    if(!(arrayVector[0]->getSize() == arrayVector[1]->getSize() &&
         arrayVector[0]->getSize() == arrayVector[2]->getSize())) {
197 198 199
      XdmfError::message(XdmfError::FATAL,
                         "Arrays must be of equal size in "
                         "XdmfMap:: populateItem");
200
    }
201

202 203 204
    // check if any arrays have values in memory - if so, they need to be
    // read into map
    bool needToRead = false;
205
    for(std::vector<shared_ptr<XdmfArray> >::const_iterator iter =
206 207 208
          arrayVector.begin();
        iter != arrayVector.end();
        ++iter) {
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
      if((*iter)->isInitialized()) {
        needToRead = true;
        break;
      }
    }

    if(needToRead) {
      for(std::vector<shared_ptr<XdmfArray> >::const_iterator iter =
            arrayVector.begin();
          iter != arrayVector.end();
          ++iter) {
        if(!(*iter)->isInitialized()) {
          (*iter)->read();
        }
      }
      for(unsigned int i=0; i<arrayVector[0]->getSize(); ++i) {
        this->insert(arrayVector[0]->getValue<task_id>(i),
                     arrayVector[1]->getValue<node_id>(i),
                     arrayVector[2]->getValue<node_id>(i));
228
      }
229
    }
230
    else {
231 232

      mRemoteTaskIdsControllers.clear();
233
      for (unsigned int i = 0; i <  arrayVector[0]->getNumberHeavyDataControllers(); ++i)
234 235 236 237
      {
        mRemoteTaskIdsControllers.push_back(arrayVector[0]->getHeavyDataController(i));
      }
      mLocalNodeIdsControllers.clear();
238
      for (unsigned int i = 0; i <  arrayVector[1]->getNumberHeavyDataControllers(); ++i)
239 240 241 242
      {
        mLocalNodeIdsControllers.push_back(arrayVector[1]->getHeavyDataController(i));
      }
      mRemoteLocalNodeIdsControllers.clear();
243
      for (unsigned int i = 0; i <  arrayVector[2]->getNumberHeavyDataControllers(); ++i)
244 245 246
      {
        mRemoteLocalNodeIdsControllers.push_back(arrayVector[2]->getHeavyDataController(i));
      }
247 248
    }
  }
249 250
}

251 252
void
XdmfMap::read()
253
{
254 255 256 257
  if(mLocalNodeIdsControllers.size() > 0 &&
     mRemoteTaskIdsControllers.size() > 0 &&
     mRemoteLocalNodeIdsControllers.size() > 0) {

258
    unsigned int localNodeCount = 0;
259
    for (unsigned int i = 0; i< mLocalNodeIdsControllers.size(); ++i)
260 261 262
    {
      localNodeCount += mLocalNodeIdsControllers[i]->getSize();
    }
263
    unsigned int remoteTaskCount = 0;
264
    for (unsigned int i = 0; i< mRemoteTaskIdsControllers.size(); ++i)
265 266 267
    {
      remoteTaskCount += mRemoteTaskIdsControllers[i]->getSize();
    }
268
    unsigned int remoteNodeCount = 0;
269
    for (unsigned int i = 0; i< mRemoteLocalNodeIdsControllers.size(); ++i)
270 271 272 273 274 275 276 277
    {
      remoteNodeCount += mRemoteLocalNodeIdsControllers[i]->getSize();
    }

    if(!(localNodeCount ==
         remoteTaskCount &&
         localNodeCount ==
         remoteNodeCount)){
278 279
      XdmfError::message(XdmfError::FATAL,
                         "Arrays must be of equal size in XdmfMap::read");
280 281
    }

282 283 284
    shared_ptr<XdmfArray> remoteTaskIds = XdmfArray::New();
    shared_ptr<XdmfArray> localNodeIds = XdmfArray::New();
    shared_ptr<XdmfArray> remoteLocalNodeIds = XdmfArray::New();
285

286
    mRemoteTaskIdsControllers[0]->read(remoteTaskIds.get());
287
    for (unsigned int i = 1; i < mRemoteTaskIdsControllers.size(); ++i)
288 289 290 291 292 293
    {
      shared_ptr<XdmfArray> tempArray = XdmfArray::New();
      mRemoteTaskIdsControllers[i]->read(tempArray.get());
      remoteTaskIds->insert(remoteTaskIds->getSize(), tempArray, 0, tempArray->getSize());
    }
    mLocalNodeIdsControllers[0]->read(localNodeIds.get());
294
    for (unsigned int i = 1; i < mLocalNodeIdsControllers.size(); ++i)
295 296 297 298 299 300
    {
      shared_ptr<XdmfArray> tempArray = XdmfArray::New();
      mLocalNodeIdsControllers[i]->read(tempArray.get());
      localNodeIds->insert(localNodeIds->getSize(), tempArray, 0, tempArray->getSize());
    }
    mRemoteLocalNodeIdsControllers[0]->read(remoteLocalNodeIds.get());
301
    for (unsigned int i = 1; i < mRemoteLocalNodeIdsControllers.size(); ++i)
302 303 304 305 306
    {
      shared_ptr<XdmfArray> tempArray = XdmfArray::New();
      mRemoteLocalNodeIdsControllers[i]->read(tempArray.get());
      remoteLocalNodeIds->insert(remoteLocalNodeIds->getSize(), tempArray, 0, tempArray->getSize());
    }
307 308 309 310

    for(unsigned int i=0; i<remoteTaskIds->getSize(); ++i) {
      const unsigned int remoteTaskId = remoteTaskIds->getValue<task_id>(i);
      const unsigned int localNodeId = localNodeIds->getValue<node_id>(i);
311
      const unsigned int remoteLocalNodeId =
312 313
        remoteLocalNodeIds->getValue<node_id>(i);
      mMap[remoteTaskId][localNodeId].insert(remoteLocalNodeId);
314 315
    }
  }
316 317
}

318

319 320
void
XdmfMap::release()
321
{
322
  mMap.clear();
323 324
}

325
void
326 327 328
XdmfMap::setHeavyDataControllers(std::vector<shared_ptr<XdmfHeavyDataController> > remoteTaskIdsControllers,
                                 std::vector<shared_ptr<XdmfHeavyDataController> > localNodeIdsControllers,
                                 std::vector<shared_ptr<XdmfHeavyDataController> > remoteLocalNodeIdsControllers)
329
{
330
  unsigned int localNodeCount = 0;
331
  for (unsigned int i = 0; i< localNodeIdsControllers.size(); ++i)
332 333 334
  {
    localNodeCount += localNodeIdsControllers[i]->getSize();
  }
335
  unsigned int remoteTaskCount = 0;
336
  for (unsigned int i = 0; i< remoteTaskIdsControllers.size(); ++i)
337 338 339
  {
    remoteTaskCount += remoteTaskIdsControllers[i]->getSize();
  }
340
  unsigned int remoteNodeCount = 0;
341
  for (unsigned int i = 0; i< remoteLocalNodeIdsControllers.size(); ++i)
342 343 344 345 346 347
  {
    remoteNodeCount += remoteLocalNodeIdsControllers[i]->getSize();
  }
  if(!(localNodeCount ==
       remoteTaskCount &&
       localNodeCount ==
348
       remoteNodeCount)) {
349 350 351
    XdmfError::message(XdmfError::FATAL,
                       "Arrays must be of equal size in "
                       "XdmfMap::setHeavyDataControllers");
352
  }
353 354 355
  mRemoteTaskIdsControllers = remoteTaskIdsControllers;
  mLocalNodeIdsControllers = localNodeIdsControllers;
  mRemoteLocalNodeIdsControllers = remoteLocalNodeIdsControllers;
356
  this->setIsChanged(true);
357 358
}

359 360 361 362
void 
XdmfMap::setMap(std::map<task_id, node_id_map> map)
{
  mMap = map;
363
  this->setIsChanged(true);
364
}
365

366 367 368 369
void
XdmfMap::setName(const std::string & name)
{
  mName = name;
370
  this->setIsChanged(true);
371 372
}

373
void
374
XdmfMap::traverse(const shared_ptr<XdmfBaseVisitor> visitor)
375
{
376
  XdmfItem::traverse(visitor);
377

378 379 380
  shared_ptr<XdmfArray> remoteTaskIds = XdmfArray::New();
  shared_ptr<XdmfArray> localNodeIds = XdmfArray::New();
  shared_ptr<XdmfArray> remoteLocalNodeIds = XdmfArray::New();
381 382

  for(std::map<task_id, node_id_map>::const_iterator
383 384 385
        iter = mMap.begin();
      iter != mMap.end();
      ++iter) {
386
    for(node_id_map::const_iterator
387
          iter2 = iter->second.begin();
388 389
        iter2 != iter->second.end();
        ++iter2) {
390
      for(node_id_map::mapped_type::const_iterator iter3 =
391 392 393 394 395 396 397
            iter2->second.begin();
          iter3 != iter2->second.end();
          ++iter3) {
        remoteTaskIds->pushBack(iter->first);
        localNodeIds->pushBack(iter2->first);
        remoteLocalNodeIds->pushBack(*iter3);
      }
398 399
    }
  }
400

401
  for (unsigned int i = 0; i < mRemoteTaskIdsControllers.size(); ++i)
402 403 404
  {
    remoteTaskIds->insert(mRemoteTaskIdsControllers[i]);
  }
405
  for (unsigned int i = 0; i < mLocalNodeIdsControllers.size(); ++i)
406 407 408
  {
    localNodeIds->insert(mLocalNodeIdsControllers[i]);
  }
409
  for (unsigned int i = 0; i < mRemoteLocalNodeIdsControllers.size(); ++i)
410 411 412
  {
    remoteLocalNodeIds->insert(mRemoteLocalNodeIdsControllers[i]);
  }
413

414 415 416 417 418 419 420 421
  bool originalXPath;

  if (shared_ptr<XdmfWriter> writer =
        shared_dynamic_cast<XdmfWriter>(visitor)) {
    originalXPath = writer->getWriteXPaths();
    writer->setWriteXPaths(false);
  }

422
  remoteTaskIds->accept(visitor);
423 424
  localNodeIds->accept(visitor);
  remoteLocalNodeIds->accept(visitor);
425

426 427 428 429 430
  if (shared_ptr<XdmfWriter> writer =
        shared_dynamic_cast<XdmfWriter>(visitor)) {
    writer->setWriteXPaths(originalXPath);
  }

431 432 433 434
  mLocalNodeIdsControllers.clear();
  mRemoteTaskIdsControllers.clear();
  mRemoteLocalNodeIdsControllers.clear();

435
  for (unsigned int i = 0; i < remoteTaskIds->getNumberHeavyDataControllers(); ++i)
436 437 438
  {
    mRemoteTaskIdsControllers.push_back(remoteTaskIds->getHeavyDataController(i));
  }
439
  for (unsigned int i = 0; i < localNodeIds->getNumberHeavyDataControllers(); ++i)
440 441 442
  {
    mLocalNodeIdsControllers.push_back(localNodeIds->getHeavyDataController(i));
  }
443
  for (unsigned int i = 0; i < remoteLocalNodeIds->getNumberHeavyDataControllers(); ++i)
444 445 446
  {
    mRemoteLocalNodeIdsControllers.push_back(remoteLocalNodeIds->getHeavyDataController(i));
  }
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699

  remoteTaskIds.reset();
  localNodeIds.reset();
  remoteLocalNodeIds.reset();
}

// C Wrappers

XDMFMAP * XdmfMapNew()
{
  try
  {
    shared_ptr<XdmfMap> generatedMap = XdmfMap::New();
    return (XDMFMAP *)((void *)(new XdmfMap(*generatedMap.get())));
  }
  catch (...)
  {
    shared_ptr<XdmfMap> generatedMap = XdmfMap::New();
    return (XDMFMAP *)((void *)(new XdmfMap(*generatedMap.get())));
  }
}

XDMFMAP ** XdmfMapNewFromIdVector(int ** globalNodeIds, int * numIdsOnNode, int numIds)
{
  try
  {
    std::vector<shared_ptr<XdmfAttribute> > insertedAttributeVector;
    for (int i = 0; i < numIds; ++i) {
      shared_ptr<XdmfAttribute> insertedAttribute = XdmfAttribute::New();
      insertedAttribute->insert(0, globalNodeIds[i], numIdsOnNode[i], 1, 1);
      insertedAttributeVector.push_back(insertedAttribute);
    }
    std::vector<shared_ptr<XdmfMap> > generatedMaps = XdmfMap::New(insertedAttributeVector);
    unsigned int returnSize = generatedMaps.size();
    XDMFMAP ** returnArray = new XDMFMAP *[returnSize]();
    for (unsigned int i = 0; i < returnSize; ++i) {
      returnArray[i] = (XDMFMAP *)((void *)(new XdmfMap(*generatedMaps[i].get())));
    }
    return returnArray;
  }
  catch (...)
  {
    std::vector<shared_ptr<XdmfAttribute> > insertedAttributeVector;
    for (int i = 0; i < numIds; ++i) {
      shared_ptr<XdmfAttribute> insertedAttribute = XdmfAttribute::New();
      insertedAttribute->insert(0, globalNodeIds[i], numIdsOnNode[i], 1, 1);
      insertedAttributeVector.push_back(insertedAttribute);
    }
    std::vector<shared_ptr<XdmfMap> > generatedMaps = XdmfMap::New(insertedAttributeVector);
    unsigned int returnSize = generatedMaps.size();
    XDMFMAP ** returnArray = new XDMFMAP *[returnSize]();
    for (unsigned int i = 0; i < returnSize; ++i) {
      returnArray[i] = (XDMFMAP *)((void *)(new XdmfMap(*generatedMaps[i].get())));
    }
    return returnArray;
  }
}

char * XdmfMapGetName(XDMFMAP * map)
{
  try
  {
    char * returnPointer = strdup(((XdmfMap *)(map))->getName().c_str());
    return returnPointer;
  }
  catch (...)
  {
    char * returnPointer = strdup(((XdmfMap *)(map))->getName().c_str());
    return returnPointer;
  }
}

void XdmfMapInsert(XDMFMAP * map, int remoteTaskId, int localNodeId, int remoteLocalNodeId)
{
  ((XdmfMap *)(map))->insert(remoteTaskId, localNodeId, remoteLocalNodeId);
}

int XdmfMapIsInitialized(XDMFMAP * map)
{
  return ((XdmfMap *)(map))->isInitialized();
}

void XdmfMapRead(XDMFMAP * map, int * status)
{
  XDMF_ERROR_WRAP_START(status)
  ((XdmfMap *)(map))->read();
  XDMF_ERROR_WRAP_END(status)
}

void XdmfMapRelease(XDMFMAP * map)
{
  ((XdmfMap *)(map))->release();
}

int * XdmfMapRetrieveLocalNodeIds(XDMFMAP * map, int remoteTaskId)
{
  try
  {
    int * returnPointer = new int[XdmfMapRetrieveNumberLocalNodeIds(map, remoteTaskId)]();
    std::map<int, std::map<int, std::set<int> > > testMap = ((XdmfMap *)(map))->getMap();
    std::map<int, std::map<int, std::set<int> > >::const_iterator iter = testMap.find(remoteTaskId);
    unsigned int i = 0;
    for(std::map<int, std::set<int> >::const_iterator
          iter2 = iter->second.begin();
        iter2 != iter->second.end();
        ++iter2) {
      returnPointer[i] = iter2->first;
      ++i;
    }
    return returnPointer;
  }
  catch (...)
  {
    int * returnPointer = new int[XdmfMapRetrieveNumberLocalNodeIds(map, remoteTaskId)]();
    std::map<int, std::map<int, std::set<int> > > testMap = ((XdmfMap *)(map))->getMap();
    std::map<int, std::map<int, std::set<int> > >::const_iterator iter = testMap.find(remoteTaskId);
    unsigned int i = 0;
    for(std::map<int, std::set<int> >::const_iterator
          iter2 = iter->second.begin();
        iter2 != iter->second.end();
        ++iter2) {
      returnPointer[i] = iter2->first;
      ++i;
    }
    return returnPointer;
  }
}

int XdmfMapRetrieveNumberLocalNodeIds(XDMFMAP * map, int remoteTaskId)
{
  return ((XdmfMap *)(map))->getMap()[remoteTaskId].size();
}

int XdmfMapRetrieveNumberRemoteTaskIds(XDMFMAP * map)
{
  return ((XdmfMap *)(map))->getMap().size();
}

int XdmfMapRetrieveNumberRemoteNodeIds(XDMFMAP * map, int remoteTaskId, int localNodeId)
{
  return ((XdmfMap *)(map))->getMap()[remoteTaskId][localNodeId].size();
}

int * XdmfMapRetrieveRemoteTaskIds(XDMFMAP * map)
{
  try
  {
    int * returnPointer = new int[((XdmfMap *)(map))->getMap().size()]();
    std::map<int, std::map<int, std::set<int> > > testMap = ((XdmfMap *)(map))->getMap();
    unsigned int i = 0;
    for(std::map<int, std::map<int, std::set<int> > >::const_iterator
          iter = testMap.begin();
        iter != testMap.end();
        ++iter) {
      returnPointer[i] = iter->first;
      ++i;
    }
    return returnPointer;
  }
  catch (...)
  {
    int * returnPointer = new int[((XdmfMap *)(map))->getMap().size()]();
    std::map<int, std::map<int, std::set<int> > > testMap = ((XdmfMap *)(map))->getMap();
    unsigned int i = 0;
    for(std::map<int, std::map<int, std::set<int> > >::const_iterator
          iter = testMap.begin();
        iter != testMap.end();
        ++iter) {
      returnPointer[i] = iter->first;
      ++i;
    }
    return returnPointer;
  }
}

int * XdmfMapRetrieveRemoteNodeIds(XDMFMAP * map, int remoteTaskId, int localNodeId)
{
  try
  {
    int * returnPointer = new int[XdmfMapRetrieveNumberRemoteNodeIds(map, remoteTaskId, localNodeId)]();
    std::map<int, std::map<int, std::set<int> > > testMap = ((XdmfMap *)(map))->getMap();
    std::map<int, std::map<int, std::set<int> > >::const_iterator iter = testMap.find(remoteTaskId);
    std::map<int, std::set<int> >::const_iterator iter2 = iter->second.find(localNodeId);
    unsigned int i = 0;
    for(std::map<int, std::set<int> >::mapped_type::const_iterator iter3 =
          iter2->second.begin();
        iter3 != iter2->second.end();
        ++iter3) {
      returnPointer[i] = *iter3;
      i++;
    }
    return returnPointer;
  }
  catch (...)
  {
    int * returnPointer = new int[XdmfMapRetrieveNumberRemoteNodeIds(map, remoteTaskId, localNodeId)]();
    std::map<int, std::map<int, std::set<int> > > testMap = ((XdmfMap *)(map))->getMap();
    std::map<int, std::map<int, std::set<int> > >::const_iterator iter = testMap.find(remoteTaskId);
    std::map<int, std::set<int> >::const_iterator iter2 = iter->second.find(localNodeId);
    unsigned int i = 0;
    for(std::map<int, std::set<int> >::mapped_type::const_iterator iter3 =
          iter2->second.begin();
        iter3 != iter2->second.end();
        ++iter3) {
      returnPointer[i] = *iter3;
      i++;
    }
    return returnPointer;
  }
}

void XdmfMapSetHeavyDataControllers(XDMFMAP * map,
                                    XDMFHEAVYDATACONTROLLER ** remoteTaskControllers,
                                    int numRemoteTaskControllers,
                                    XDMFHEAVYDATACONTROLLER ** localNodeControllers,
                                    int numLocalNodeControllers,
                                    XDMFHEAVYDATACONTROLLER ** remoteLocalNodeControllers,
                                    int numRemoteLocalNodeControllers,
                                    int passControl,
                                    int * status)
{
  XDMF_ERROR_WRAP_START(status)
  std::vector<shared_ptr<XdmfHeavyDataController> > insertRemoteTaskControllers;
  for (int i = 0; i < numRemoteTaskControllers; ++i) {
    if (passControl) {
      insertRemoteTaskControllers.push_back(shared_ptr<XdmfHeavyDataController>(((XdmfHeavyDataController *)remoteTaskControllers[i])));
    }
    else {
      insertRemoteTaskControllers.push_back(shared_ptr<XdmfHeavyDataController>(((XdmfHeavyDataController *)remoteTaskControllers[i]), XdmfNullDeleter()));
    }
  }

  std::vector<shared_ptr<XdmfHeavyDataController> > insertLocalNodeControllers;
  for (int i = 0; i < numLocalNodeControllers; ++i) {
    if (passControl) {
      insertLocalNodeControllers.push_back(shared_ptr<XdmfHeavyDataController>(((XdmfHeavyDataController *)localNodeControllers[i])));
    }
    else {
      insertLocalNodeControllers.push_back(shared_ptr<XdmfHeavyDataController>(((XdmfHeavyDataController *)localNodeControllers[i]), XdmfNullDeleter()));
    }
  }

  std::vector<shared_ptr<XdmfHeavyDataController> > insertRemoteLocalNodeControllers;
  for (int i = 0; i < numRemoteLocalNodeControllers; ++i) {
    if (passControl) {
      insertRemoteLocalNodeControllers.push_back(shared_ptr<XdmfHeavyDataController>(((XdmfHeavyDataController *)remoteLocalNodeControllers[i])));
    }
    else {
      insertRemoteLocalNodeControllers.push_back(shared_ptr<XdmfHeavyDataController>(((XdmfHeavyDataController *)remoteLocalNodeControllers[i]), XdmfNullDeleter()));
    }
  }
  ((XdmfMap *)(map))->setHeavyDataControllers(insertRemoteTaskControllers, insertLocalNodeControllers, insertRemoteLocalNodeControllers);
  XDMF_ERROR_WRAP_END(status)
700
}
701 702 703 704 705 706 707

void XdmfMapSetName(XDMFMAP * map, char * newName)
{
  ((XdmfMap *)(map))->setName(std::string(newName));
}

XDMF_ITEM_C_CHILD_WRAPPER(XdmfMap, XDMFMAP)