File: XdmfDSMCommMPI.cpp

package info (click to toggle)
xdmf 3.0%2Bgit20160803-3
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 35,388 kB
  • ctags: 36,627
  • sloc: ansic: 265,382; cpp: 162,889; python: 10,976; f90: 1,378; yacc: 687; fortran: 464; xml: 200; java: 187; lex: 125; makefile: 82; sh: 28
file content (1196 lines) | stat: -rw-r--r-- 31,933 bytes parent folder | download | duplicates (7)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
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
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
/*****************************************************************************/
/*                                    XDMF                                   */
/*                       eXtensible Data Model and Format                    */
/*                                                                           */
/*  Id : XdmfDSMCommMPI.cpp                                                  */
/*                                                                           */
/*  Author:                                                                  */
/*     Andrew Burns                                                          */
/*     andrew.j.burns2@us.army.mil                                           */
/*     US Army Research Laboratory                                           */
/*     Aberdeen Proving Ground, MD                                           */
/*                                                                           */
/*     Copyright @ 2013 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.                                                 */
/*                                                                           */
/*****************************************************************************/

/*=========================================================================
  This code is derived from an earlier work and is distributed
  with permission from, and thanks to ...
=========================================================================*/

/*============================================================================

  Project                 : H5FDdsm
  Module                  : H5FDdsmCommMpi.cxx

  Authors:
     John Biddiscombe     Jerome Soumagne
     biddisco@cscs.ch     soumagne@cscs.ch

  Copyright (C) CSCS - Swiss National Supercomputing Centre.
  You may use modify and and distribute this code freely providing
  1) This copyright notice appears on all copies of source code
  2) An acknowledgment appears with any substantial usage of the code
  3) If this code is contributed to any other open source project, it
  must not be reformatted such that the indentation, bracketing or
  overall style is modified significantly.

  This software is distributed WITHOUT ANY WARRANTY; without even the
  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  This work has received funding from the European Community's Seventh
  Framework Programme (FP7/2007-2013) under grant agreement 225967 âxtMuSEâOC

============================================================================*/

#include <XdmfDSMCommMPI.hpp>
#include <XdmfError.hpp>
#include <mpi.h>
#include <string.h>
#include <cstdlib>
#include <fstream>
#include <iostream>

bool XdmfDSMCommMPI::UseEnvFileName = false;

XdmfDSMCommMPI::XdmfDSMCommMPI()
{
  IntraComm = MPI_COMM_NULL;
  Id = -1;
  IntraSize = -1;
  InterComm = MPI_COMM_NULL;
  InterId = -1;
  InterSize = -1;
  SetDsmPortName("");
  // This is the default file name for the config file.
  DsmFileName = "dsmconnect.cfg";
  if (XdmfDSMCommMPI::UseEnvFileName)
  {
    // Grab from ENV
    if (std::getenv("XDMFDSM_CONFIG_FILE") != NULL)
    {
      DsmFileName = std::getenv("XDMFDSM_CONFIG_FILE");
    }
  }
  InterCommType = XDMF_DSM_COMM_MPI;
  HasOpenedPort = false;
  ApplicationName = "Application";
}

XdmfDSMCommMPI::~XdmfDSMCommMPI()
{
#ifndef OPEN_MPI
  if (InterComm != MPI_COMM_NULL) {
    int status = MPI_Comm_free(&InterComm);
    if (status != MPI_SUCCESS) {
      try {
        XdmfError::message(XdmfError::FATAL, "Failed to free intercomm Comm");
      }
      catch (XdmfError & e) {
        throw e;
      }
    }
  }
  if (IntraComm != MPI_COMM_NULL) {
    int status = MPI_Comm_free(&IntraComm);
    if (status != MPI_SUCCESS) {
      try {
        XdmfError::message(XdmfError::FATAL, "Failed to free intercomm Comm");
      }
      catch (XdmfError & e) {
        throw e;
      }
    }
  }
#endif
}

void
XdmfDSMCommMPI::Accept(unsigned int numConnections)
{
#ifndef XDMF_DSM_IS_CRAY
  int status;
  int acceptingLeadId;
  while (numConnections > 0) {
    if (InterComm == MPI_COMM_NULL) {
      acceptingLeadId = this->IntraSize;
      // If there is no InterComm, then accept from IntraComm and merge into InterComm
      MPI_Comm tempComm;
      int * portCheck = new int[GetInterSize()]();
      int portStatus;
      portStatus = 0;
      if (HasOpenedPort) {
        portStatus = 1;
      }
      MPI_Allgather(&portStatus, 1, MPI_INT, &(portCheck[0]), 1, MPI_INT, InterComm);
      int index = 0;
      for (index = 0; index < GetInterSize(); ++index) {
        if (portCheck[index] == 1) {
          break;
        }
      }
      int status = MPI_Comm_accept(DsmPortName, MPI_INFO_NULL, index, IntraComm, &tempComm);
      if (status != MPI_SUCCESS) {
        try {
          std::string message = "Failed to accept port ";
          message = message + DsmPortName;
          XdmfError::message(XdmfError::FATAL, message);
        }
        catch (XdmfError & e) {
          throw e;
        }
      }
      // False is specified for high so that the index of the cores doesn't change
      MPI_Comm mergedComm;
      status = MPI_Intercomm_merge(tempComm, false, &mergedComm);
      this->DupInterComm(mergedComm);
      if (status != MPI_SUCCESS) {
        try {
          XdmfError::message(XdmfError::FATAL, "Failed to merge intercomm");
        }
        catch (XdmfError & e) {
          throw e;
        }
      }
      else {
        MPI_Comm_rank(InterComm, &InterId);
        MPI_Comm_size(InterComm, &InterSize);
      }
    }
    else {
      acceptingLeadId = this->InterSize;
      // If there is an InterComm, accept into the InterComm and merge
      MPI_Comm tempComm;
      int * portCheck = new int[GetInterSize()]();
      int portStatus;
      portStatus = 0;
      if (HasOpenedPort) {
        portStatus = 1;
      }
      MPI_Allgather(&portStatus, 1, MPI_INT, &(portCheck[0]), 1, MPI_INT, InterComm);
      int index = 0;
      for (index = 0; index < GetInterSize(); ++index) {
        if (portCheck[index] == 1) {
          break;
        }
      }
      int status = MPI_Comm_accept(DsmPortName, MPI_INFO_NULL, index, InterComm, &tempComm);
      if (status != MPI_SUCCESS) {
        try {
          std::string message = "Failed to accept port ";
          message = message + DsmPortName;
          XdmfError::message(XdmfError::FATAL, message);
        }
        catch (XdmfError & e) {
          throw e;
        }
      }
      // False is specified for high so that the index of the cores doesn't change
      MPI_Comm mergedComm;
      status = MPI_Intercomm_merge(tempComm, false, &mergedComm);
      this->DupInterComm(mergedComm);
      if (status != MPI_SUCCESS) {
        try {
          XdmfError::message(XdmfError::FATAL, "Failed to merge InterComm");
        }
        catch (XdmfError & e) {
          throw e;
        }
      }
      else {
        MPI_Comm_rank(InterComm, &InterId);
        MPI_Comm_size(InterComm, &InterSize);
      }
    }

    //regen Intra comm from Inter Comm
    MPI_Group IntraGroup, InterGroup;
    MPI_Comm_group(InterComm, &InterGroup);
    int * ServerIds = (int *)calloc(this->IntraSize, sizeof(int));
    unsigned int index = 0;
    for(int i=this->InterId - this->Id; i < this->InterId - this->Id + this->IntraSize; ++i)
    {
      ServerIds[index++] = i;
    }
    MPI_Group_incl(InterGroup, this->IntraSize, ServerIds, &IntraGroup);
    MPI_Comm_create(InterComm, IntraGroup, &IntraComm);
    cfree(ServerIds);

    // Since this is accept, we will be recieving the local data from the new core and
    // sending the overarching data to the connecting cores
    unsigned int length;
    char * appname;
    unsigned int appsize;
    if (DsmProcessStructure.size() == 0)
    {
      DsmProcessStructure.push_back(std::pair<std::string, unsigned int>(this->ApplicationName, this->IntraSize));
    }
    int numSections = DsmProcessStructure.size();
    if (InterId == 0)
    {
      // Loop in the applicaiton names of the already existing sections of the DSM
      // Get the number of application sections
      MPI_Send(&numSections, 1, MPI_INT, acceptingLeadId, XDMF_DSM_EXCHANGE_TAG, this->InterComm);
      for (unsigned int i = 0; i < numSections; ++i)
      {
        length = DsmProcessStructure[i].first.size();
        // Get the length of the name
        MPI_Send(&length, 1, MPI_UNSIGNED, acceptingLeadId, XDMF_DSM_EXCHANGE_TAG, InterComm);
        // Get the string of characters
        appname = new char[length]();
        strcpy(appname, DsmProcessStructure[i].first.c_str());
        MPI_Send(appname, length, MPI_CHAR, acceptingLeadId, XDMF_DSM_EXCHANGE_TAG, InterComm);
        delete appname;
        // Get associated numprocs
        appsize = DsmProcessStructure[i].second;
        MPI_Send(&appsize, 1, MPI_UNSIGNED, acceptingLeadId, XDMF_DSM_EXCHANGE_TAG, InterComm);
      }
    }
    // Add the information for the newly added application
    // For each application in the connecting set.
    MPI_Bcast(&numSections, 1, MPI_UNSIGNED, acceptingLeadId, InterComm);
    for (unsigned int i = 0; i < numSections; ++i)
    {
      MPI_Bcast(&length, 1, MPI_UNSIGNED, acceptingLeadId, InterComm);
      appname = new char[length+1]();
      MPI_Bcast(appname, length, MPI_CHAR, acceptingLeadId, InterComm);
      appname[length] = 0;
      MPI_Bcast(&appsize, 1, MPI_UNSIGNED, acceptingLeadId, InterComm);
      DsmProcessStructure.push_back(std::pair<std::string, unsigned int>(std::string(appname), appsize));
    }
    --numConnections;
    MPI_Bcast(&numConnections, 1, MPI_INT, 0, InterComm);
  }
#endif
}

void
XdmfDSMCommMPI::AllGather(void *sendbuf,
                          int sendbytes,
                          void *recvbuf,
                          int recvbytes,
                          int comm)
{
  if (comm == XDMF_DSM_INTRA_COMM)
  {
    MPI_Allgather(sendbuf,
                  sendbytes,
                  MPI_UNSIGNED_CHAR,
                  recvbuf,
                  recvbytes,
                  MPI_UNSIGNED_CHAR,
                  IntraComm);
  }
  else if (comm == XDMF_DSM_INTER_COMM)
  {
    MPI_Allgather(sendbuf,
                  sendbytes,
                  MPI_UNSIGNED_CHAR,
                  recvbuf,
                  recvbytes,
                  MPI_UNSIGNED_CHAR,
                  InterComm);
  }
}

void
XdmfDSMCommMPI::Barrier(int comm)
{
  int status;

  if (comm == XDMF_DSM_INTRA_COMM)
  {
    status = MPI_Barrier(IntraComm);
  }
  else if (comm == XDMF_DSM_INTER_COMM)
  {
    status = MPI_Barrier(InterComm);
  }
}

void
XdmfDSMCommMPI::Broadcast(void * pointer,
                          int sizebytes,
                          int root,
                          int comm)
{
  int status;

  if (comm == XDMF_DSM_INTRA_COMM)
  {
    status = MPI_Bcast(pointer, sizebytes, MPI_UNSIGNED_CHAR, root, IntraComm);
  }
  else if (comm == XDMF_DSM_INTER_COMM)
  {
    status = MPI_Bcast(pointer, sizebytes, MPI_UNSIGNED_CHAR, root, InterComm);
  }
}

void
XdmfDSMCommMPI::ClosePort()
{
#ifndef XDMF_DSM_IS_CRAY
  if (Id == 0) {
    int status;
    for (unsigned int i = 0; i < PreviousDsmPortNames.size(); ++i)
    {
      status = MPI_Close_port(PreviousDsmPortNames[i]);
      if (status != MPI_SUCCESS) {
        try {// OpenMPI iterate through open ports in order to close the multiple needed
          std::string message = "Failed to close port ";
          message = message + PreviousDsmPortNames[i];
          XdmfError::message(XdmfError::FATAL, message);
        }
        catch (XdmfError & e) {
          throw e;
        }
      }
    }
  }
#endif
  HasOpenedPort = false;
}

int
XdmfDSMCommMPI::Connect()
{
#ifndef XDMF_DSM_IS_CRAY
  int status;
  MPI_Status mpistatus;
  if (InterComm == MPI_COMM_NULL) {
    this->DupInterComm(IntraComm);
  }
  MPI_Comm tempComm;
  MPI_Comm tempConnectComm;
  MPI_Comm_dup(InterComm, &tempConnectComm);
  MPI_Errhandler_set(InterComm, MPI_ERRORS_RETURN);
  status = MPI_Comm_connect(DsmPortName, MPI_INFO_NULL, 0, tempConnectComm, &tempComm);
  MPI_Errhandler_set(InterComm, MPI_ERRORS_ARE_FATAL);
  if (status != MPI_SUCCESS) {
    try {
      std::string message = "Failed to connect to port ";
      message = message + DsmPortName;
      XdmfError::message(XdmfError::FATAL, message);
    }
    catch (XdmfError & e) {
      throw e;
    }
  }
  MPI_Comm mergedComm;
  status = MPI_Intercomm_merge(tempComm, true, &mergedComm);
  this->DupInterComm(mergedComm);
  if (status != MPI_SUCCESS) {
    try {
      XdmfError::message(XdmfError::FATAL, "Failed to merge InterComm");
    }
    catch (XdmfError & e) {
      throw e;
    }
  }
  else {
    status = MPI_Comm_rank(InterComm, &InterId);
    status = MPI_Comm_size(InterComm, &InterSize);
  }

  //regen Intra comm from Inter Comm
  MPI_Group IntraGroup, InterGroup;
  MPI_Comm_group(InterComm, &InterGroup);
  int * ServerIds = (int *)calloc(this->IntraSize, sizeof(int));
  unsigned int index = 0;
  for(int i=this->InterId - this->Id; i < this->InterId - this->Id + this->IntraSize; ++i)
  {
    ServerIds[index++] = i;
  }
  MPI_Group_incl(InterGroup, this->IntraSize, ServerIds, &IntraGroup);
  MPI_Comm_create(InterComm, IntraGroup, &IntraComm);
  cfree(ServerIds);

  // Here the process will send information about itself to the server and
  // the server will diseminate that info across all the connected processes
  std::vector<std::pair<std::string, unsigned int> > structureArchive;
  // Archive old structure if it exists
  if (DsmProcessStructure.size() > 0)
  {
    for (unsigned int i = 0; i < DsmProcessStructure.size(); ++i)
    {
      structureArchive.push_back(DsmProcessStructure[i]);
    }
    DsmProcessStructure.clear();
  }
  // Loop in the applicaiton names of the already existing sections of the DSM
  // Get the number of application sections
  int numSections;
  if (this->Id == 0)
  {
    MPI_Recv(&numSections, 1, MPI_INT, 0, XDMF_DSM_EXCHANGE_TAG, this->InterComm, &mpistatus);
  }
  MPI_Bcast(&numSections, 1, MPI_INT, 0, IntraComm);
  unsigned int length;
  char * appname;
  unsigned int appsize;
  for (unsigned int i = 0; i < numSections; ++i)
  {
    if (this->Id == 0)
    {
      // Get the length of the name
      MPI_Recv(&length, 1, MPI_UNSIGNED, 0, XDMF_DSM_EXCHANGE_TAG, InterComm, &mpistatus);
      // Get the string of characters
      appname = new char[length+1]();
      MPI_Recv(appname, length, MPI_CHAR, 0, XDMF_DSM_EXCHANGE_TAG, InterComm, &mpistatus);
      appname[length] = 0;
      // Get associated numprocs
      MPI_Recv(&appsize, 1, MPI_UNSIGNED, 0, XDMF_DSM_EXCHANGE_TAG, InterComm, &mpistatus);
    }
    // Broadcast to local comm
    MPI_Bcast(&length, 1, MPI_UNSIGNED, 0, IntraComm);
    if (this->Id != 0)
    {
      appname = new char[length+1]();
    }
    MPI_Bcast(appname, length+1, MPI_CHAR, 0, IntraComm);
    MPI_Bcast(&appsize, 1, MPI_UNSIGNED, 0, IntraComm);

    DsmProcessStructure.push_back(std::pair<std::string, unsigned int>(std::string(appname), appsize));
  }
  if (structureArchive.size() == 0)
  {
    numSections = 1;
    MPI_Bcast(&numSections, 1, MPI_INT, InterId-Id, InterComm);
    length = ApplicationName.size();
    appsize = this->IntraSize;
    MPI_Bcast(&length, 1, MPI_UNSIGNED, InterId-Id, InterComm);
    appname = new char[length]();
    strcpy(appname, ApplicationName.c_str());
    MPI_Bcast(appname, length, MPI_CHAR, InterId-Id, InterComm);
    delete appname;
    MPI_Bcast(&appsize, 1, MPI_UNSIGNED, InterId-Id, InterComm);
    DsmProcessStructure.push_back(std::pair<std::string, unsigned int>(ApplicationName, appsize));
  }
  else
  {
    numSections = structureArchive.size();
    MPI_Bcast(&numSections, 1, MPI_UNSIGNED, InterId-Id, InterComm);
    for (unsigned int i = 0; i < numSections; ++i)
    {
      length = structureArchive[i].first.size();
      appsize = structureArchive[i].second;
      MPI_Bcast(&length, 1, MPI_UNSIGNED, InterId-Id, InterComm);
      appname = new char[length]();
      strcpy(appname, structureArchive[i].first.c_str());
      MPI_Bcast(appname, length, MPI_CHAR, InterId-Id, InterComm);
      delete appname;
      MPI_Bcast(&appsize, 1, MPI_UNSIGNED, InterId-Id, InterComm);
      DsmProcessStructure.push_back(std::pair<std::string, unsigned int>(structureArchive[i].first, appsize));
    }
  }
  int numAccepts = 0;
  MPI_Bcast(&numAccepts, 1, MPI_INT, 0, InterComm);
#ifdef OPEN_MPI
    if (numAccepts > 0) {
      MPI_Bcast(DsmPortName, MPI_MAX_PORT_NAME, MPI_CHAR, 0, InterComm);
    }
#endif
  Accept(numAccepts);
  return MPI_SUCCESS;
#endif
  return MPI_SUCCESS;
}

void
XdmfDSMCommMPI::Disconnect()
{
#ifndef XDMF_DSM_IS_CRAY
#ifndef OPEN_MPI
  if (InterComm != MPI_COMM_NULL) {
    int status = MPI_Comm_free(&InterComm);
    if (status != MPI_SUCCESS) {
      try {
        XdmfError::message(XdmfError::FATAL, "Failed to disconnect Comm");
      }
      catch (XdmfError & e) {
        throw e;
      }
    }
  }
#endif
#endif
  InterComm = MPI_COMM_NULL;
}

void
XdmfDSMCommMPI::DupComm(MPI_Comm comm)
{
  if (IntraComm != comm) {
    int status;
#ifndef OPEN_MPI
    if (IntraComm != MPI_COMM_NULL) {
      status = MPI_Comm_free(&IntraComm);
      if (status != MPI_SUCCESS) {
        try {
          XdmfError::message(XdmfError::FATAL, "Failed to disconnect Comm");
        }
        catch (XdmfError & e) {
          throw e;
        }
      }
    }
#endif
    if (comm != MPI_COMM_NULL) {
      status = MPI_Comm_dup(comm, &IntraComm);
      if (status != MPI_SUCCESS) {
        try {
          XdmfError::message(XdmfError::FATAL, "Failed to duplicate Comm");
        }
        catch (XdmfError & e) {
          throw e;
        }
      }
      else {
        status = MPI_Comm_size(IntraComm, &IntraSize);
        status = MPI_Comm_rank(IntraComm, &Id);
      }
    }
  }
}

void
XdmfDSMCommMPI::DupInterComm(MPI_Comm comm)
{
  if (InterComm != comm) {
    int status;
#ifndef OPEN_MPI
    if (InterComm != MPI_COMM_NULL) {
      status = MPI_Comm_free(&InterComm);
      if (status != MPI_SUCCESS) {
        try {
          XdmfError::message(XdmfError::FATAL, "Failed to disconnect Comm");
        }
        catch (XdmfError & e) {
          throw e;
        }
      }
    }
#endif
    if (comm != MPI_COMM_NULL) {
      status = MPI_Comm_dup(comm, &InterComm);
      if (status != MPI_SUCCESS) {
        try {
          XdmfError::message(XdmfError::FATAL, "Failed to duplicate Comm");
        }
        catch (XdmfError & e) {
          throw e;
        }
      }
      else {
        status = MPI_Comm_rank(InterComm, &InterId);
        status = MPI_Comm_size(InterComm, &InterSize);
      }
    }
    else {
      InterId = -1;
      InterSize = -1;
    }
  }
}

std::string
XdmfDSMCommMPI::GetApplicationName()
{
  return ApplicationName;
}

std::string
XdmfDSMCommMPI::GetDsmFileName()
{
  return DsmFileName;
}

char *
XdmfDSMCommMPI::GetDsmPortName()
{
  return DsmPortName;
}

std::vector<std::pair<std::string, unsigned int> >
XdmfDSMCommMPI::GetDsmProcessStructure()
{
  return DsmProcessStructure;
}

int
XdmfDSMCommMPI::GetId()
{
  return this->Id;
}

MPI_Comm
XdmfDSMCommMPI::GetInterComm()
{
  return InterComm;
}

int
XdmfDSMCommMPI::GetInterCommType()
{
  return this->InterCommType;
}

int
XdmfDSMCommMPI::GetInterId()
{
  return this->InterId;
}

int
XdmfDSMCommMPI::GetInterSize()
{
  return this->InterSize;
}

MPI_Comm
XdmfDSMCommMPI::GetIntraComm()
{
  return IntraComm;
}

int
XdmfDSMCommMPI::GetIntraSize()
{
  return this->IntraSize;
}

bool
XdmfDSMCommMPI::GetUseEnvFileName()
{
  return XdmfDSMCommMPI::UseEnvFileName;
}

void
XdmfDSMCommMPI::Init()
{
  int size, rank;
  if (MPI_Comm_size(this->IntraComm, &size) != MPI_SUCCESS) {
    try {
      XdmfError::message(XdmfError::FATAL, "Failed to initialize size");
    }
    catch (XdmfError & e) {
      throw e;
    }
  }
  if (MPI_Comm_rank(this->IntraComm, &rank) != MPI_SUCCESS) {
    try {
      XdmfError::message(XdmfError::FATAL, "Failed to initialize rank");
    }
    catch (XdmfError & e) {
      throw e;
    }
  }

  this->Id = rank;
  this->IntraSize = size;
}

void
XdmfDSMCommMPI::OpenPort()
{
  if (Id == 0) {
#ifndef XDMF_DSM_IS_CRAY
    int status = MPI_Open_port(MPI_INFO_NULL, DsmPortName);
    if (status != MPI_SUCCESS) {
      try {
        std::string message = "Failed to open port ";
        message = message + DsmPortName;
        XdmfError::message(XdmfError::FATAL, message);
      }
      catch (XdmfError & e) {
        throw e;
      }
    }
    PreviousDsmPortNames.push_back(DsmPortName);
#endif
    std::ofstream connectFile (DsmFileName.c_str());
    if (connectFile.is_open()) {
      connectFile << DsmPortName;
      connectFile.close();
    }
    else {
      try {
        XdmfError::message(XdmfError::FATAL, "Failed to write port to file");
      }
      catch (XdmfError & e) {
        throw e;
      }
    }
    HasOpenedPort = true;
  }
#ifndef XDMF_DSM_IS_CRAY
  MPI_Bcast(DsmPortName, MPI_MAX_PORT_NAME, MPI_CHAR, 0, IntraComm);
#endif
}

void
XdmfDSMCommMPI::Probe(int *comm)
{
  // Used for finding a comm that has a waiting command, then sets the comm
  int status = XDMF_DSM_FAIL;
  MPI_Status signalStatus;

  int flag;
  MPI_Comm probeComm =
    this->GetIntraComm();

  // Spin until a message is found on one of the communicators
  while (status != XDMF_DSM_SUCCESS) {
    status = MPI_Iprobe(XDMF_DSM_ANY_SOURCE,
                        XDMF_DSM_ANY_TAG,
                        probeComm,
                        &flag,
                        &signalStatus);
    if (status != MPI_SUCCESS)
    {
       try {
         XdmfError::message(XdmfError::FATAL,
                            "Error: Failed to probe for command header");
       }
       catch (XdmfError & e) {
         throw e;
       }
    }
    if (flag) {
      status = XDMF_DSM_SUCCESS;
    }
    else {
      if (this->GetInterComm() != MPI_COMM_NULL) {
        if (probeComm == this->GetIntraComm()) {
          probeComm = this->GetInterComm();
        }
        else {
          probeComm = this->GetIntraComm();
        }
      }
    }
  }
  if (probeComm == this->GetInterComm()) {
    *comm = XDMF_DSM_INTER_COMM;
  }
  else
  {
    *comm = XDMF_DSM_INTRA_COMM;
  }

  probeComm = MPI_COMM_NULL;
}

void
XdmfDSMCommMPI::ReadDsmPortName()
{
#ifndef XDMF_DSM_IS_CRAY
  std::ifstream connectFile(DsmFileName.c_str());
  std::string connectLine;
  if (connectFile.is_open()) {
    getline(connectFile, connectLine);
  }
  strcpy(DsmPortName, connectLine.c_str());
#endif
}

void
XdmfDSMCommMPI::Send(void * pointer,
                     int sizebytes,
                     int coreTo,
                     int comm,
                     int tag)
{
  int status;
  if (comm == XDMF_DSM_INTRA_COMM) {
    status = MPI_Send(pointer,
                      sizebytes,
                      MPI_UNSIGNED_CHAR,
                      coreTo,
                      tag,
                      IntraComm);
  }
  else if (comm == XDMF_DSM_INTER_COMM) {
    status = MPI_Send(pointer,
                      sizebytes,
                      MPI_UNSIGNED_CHAR,
                      coreTo,
                      tag,
                      InterComm);
  }
}

void
XdmfDSMCommMPI::Receive(void * pointer,
                        int sizebytes,
                        int coreFrom,
                        int comm,
                        int tag)
{
  int status;
  MPI_Status signalStatus;
  if (comm == XDMF_DSM_INTRA_COMM) {
    status = MPI_Recv(pointer,
                      sizebytes,
                      MPI_UNSIGNED_CHAR,
                      coreFrom,
                      tag,
                      IntraComm,
                      &signalStatus);
  }
  else if (comm == XDMF_DSM_INTER_COMM) {
    status = MPI_Recv(pointer,
                      sizebytes,
                      MPI_UNSIGNED_CHAR,
                      coreFrom,
                      tag,
                      InterComm,
                      &signalStatus);
  }
}

void
XdmfDSMCommMPI::SetApplicationName(std::string newName)
{
  ApplicationName = newName;
}

void
XdmfDSMCommMPI::SetDsmFileName(std::string filename)
{
  DsmFileName = filename;
}

void
XdmfDSMCommMPI::SetDsmPortName(const char *hostName)
{
  strcpy(DsmPortName, hostName);
}

void
XdmfDSMCommMPI::SetDsmProcessStructure(std::vector<std::pair<std::string, unsigned int> > & newStructure)
{
  DsmProcessStructure = newStructure;
}

void
XdmfDSMCommMPI::SetUseEnvFileName(bool status)
{
  XdmfDSMCommMPI::UseEnvFileName = status;
}

// C Wrappers

XDMFDSMCOMMMPI * XdmfDSMCommMPINew()
{
  try
  {
    return (XDMFDSMCOMMMPI *)((void *)(new XdmfDSMCommMPI()));
  }
  catch (...)
  {
    return (XDMFDSMCOMMMPI *)((void *)(new XdmfDSMCommMPI()));
  }
}

void XdmfDSMCommMPIFree(XDMFDSMCOMMMPI * item)
{
  if (item != NULL) {
    delete ((XdmfDSMCommMPI *)item);
  }
  item = NULL;
}

void XdmfDSMCommMPIAccept(XDMFDSMCOMMMPI * dsmComm, unsigned int numConnections, int * status)
{
  XDMF_ERROR_WRAP_START(status)
  ((XdmfDSMCommMPI *)dsmComm)->Accept(numConnections);
  XDMF_ERROR_WRAP_END(status)
}

void XdmfDSMCommMPIClosePort(XDMFDSMCOMMMPI * dsmComm, int * status)
{
  XDMF_ERROR_WRAP_START(status)
  ((XdmfDSMCommMPI *)dsmComm)->ClosePort();
  XDMF_ERROR_WRAP_END(status)
}

int XdmfDSMCommMPIConnect(XDMFDSMCOMMMPI * dsmComm, int * status)
{
  XDMF_ERROR_WRAP_START(status)
  return ((XdmfDSMCommMPI *)dsmComm)->Connect();
  XDMF_ERROR_WRAP_END(status)
  return -1;
}

void XdmfDSMCommMPIDisconnect(XDMFDSMCOMMMPI * dsmComm, int * status)
{
  XDMF_ERROR_WRAP_START(status)
  ((XdmfDSMCommMPI *)dsmComm)->Disconnect();
  XDMF_ERROR_WRAP_END(status)
}

void XdmfDSMCommMPIDupComm(XDMFDSMCOMMMPI * dsmComm, MPI_Comm comm, int * status)
{
  XDMF_ERROR_WRAP_START(status)
  ((XdmfDSMCommMPI *)dsmComm)->DupComm(comm);
  XDMF_ERROR_WRAP_END(status)
}

void XdmfDSMCommMPIDupInterComm(XDMFDSMCOMMMPI * dsmComm, MPI_Comm comm, int * status)
{
  XDMF_ERROR_WRAP_START(status)
  ((XdmfDSMCommMPI *)dsmComm)->DupInterComm(comm);
  XDMF_ERROR_WRAP_END(status)
}

char * XdmfDSMCommMPIGetApplicationName(XDMFDSMCOMMMPI * dsmComm)
{
  try
  {
    char * returnPointer = strdup(((XdmfDSMCommMPI *)dsmComm)->GetApplicationName().c_str());
    return returnPointer;
  }
  catch (...)
  {
    char * returnPointer = strdup(((XdmfDSMCommMPI *)dsmComm)->GetApplicationName().c_str());
    return returnPointer;
  }
}

char * XdmfDSMCommMPIGetDsmFileName(XDMFDSMCOMMMPI * dsmComm)
{
  try
  {
    char * returnPointer = strdup(((XdmfDSMCommMPI *)dsmComm)->GetDsmFileName().c_str());
    return returnPointer;
  }
  catch (...)
  {
    char * returnPointer = strdup(((XdmfDSMCommMPI *)dsmComm)->GetDsmFileName().c_str());
    return returnPointer;
  }
}

char * XdmfDSMCommMPIGetDsmPortName(XDMFDSMCOMMMPI * dsmComm)
{
  try
  {
    return ((XdmfDSMCommMPI *)dsmComm)->GetDsmPortName();
  }
  catch (...)
  {
    return ((XdmfDSMCommMPI *)dsmComm)->GetDsmPortName();
  }
}

void XdmfDSMCommMPIGetDsmProcessStructure(XDMFDSMCOMMMPI * dsmComm,
                                          char ** names,
                                          unsigned int * coreCount,
                                          int * numApplications)
{
  try
  {
    std::vector<std::pair<std::string, unsigned int> > structure =
      ((XdmfDSMCommMPI *)dsmComm)->GetDsmProcessStructure();
    *numApplications = structure.size();
    coreCount = new unsigned int[*numApplications]();
    names = new char *[*numApplications]();
    for (unsigned int i = 0; i < *numApplications; ++i)
    {
      coreCount[i] = structure[i].second;
      names[i] = strdup(structure[i].first.c_str());
    }
  }
  catch (...)
  {
    std::vector<std::pair<std::string, unsigned int> > structure =
      ((XdmfDSMCommMPI *)dsmComm)->GetDsmProcessStructure();
    *numApplications = structure.size();
    coreCount = new unsigned int[*numApplications]();
    names = new char *[*numApplications]();
    for (unsigned int i = 0; i < *numApplications; ++i)
    {
      coreCount[i] = structure[i].second;
      names[i] = strdup(structure[i].first.c_str());
    }
  }
}

int XdmfDSMCommMPIGetId(XDMFDSMCOMMMPI * dsmComm)
{
  try
  {
    return ((XdmfDSMCommMPI *)dsmComm)->GetId();
  }
  catch (...)
  {
    return ((XdmfDSMCommMPI *)dsmComm)->GetId();
  }
}

MPI_Comm XdmfDSMCommMPIGetInterComm(XDMFDSMCOMMMPI * dsmComm)
{
  try
  {
    return ((XdmfDSMCommMPI *)dsmComm)->GetInterComm();
  }
  catch (...)
  {
    return ((XdmfDSMCommMPI *)dsmComm)->GetInterComm();
  }
}

int XdmfDSMCommMPIGetInterCommType(XDMFDSMCOMMMPI * dsmComm)
{
  try
  {
    return ((XdmfDSMCommMPI *)dsmComm)->GetInterCommType();
  }
  catch (...)
  {
    return ((XdmfDSMCommMPI *)dsmComm)->GetInterCommType();
  }
}

int XdmfDSMCommMPIGetInterId(XDMFDSMCOMMMPI * dsmComm)
{
  try
  {
    return ((XdmfDSMCommMPI *)dsmComm)->GetInterId();
  }
  catch (...)
  {
    return ((XdmfDSMCommMPI *)dsmComm)->GetInterId();
  }
}

int XdmfDSMCommMPIGetInterSize(XDMFDSMCOMMMPI * dsmComm)
{
  try
  {
    return ((XdmfDSMCommMPI *)dsmComm)->GetInterSize();
  }
  catch (...)
  {
    return ((XdmfDSMCommMPI *)dsmComm)->GetInterSize();
  }
}

MPI_Comm XdmfDSMCommMPIGetIntraComm(XDMFDSMCOMMMPI * dsmComm)
{
  try
  {
    return ((XdmfDSMCommMPI *)dsmComm)->GetIntraComm();
  }
  catch (...)
  {
    return ((XdmfDSMCommMPI *)dsmComm)->GetIntraComm();
  }
}

int XdmfDSMCommMPIGetIntraSize(XDMFDSMCOMMMPI * dsmComm)
{
  try
  {
    return ((XdmfDSMCommMPI *)dsmComm)->GetIntraSize();
  }
  catch (...)
  {
    return ((XdmfDSMCommMPI *)dsmComm)->GetIntraSize();
  }
}

int XdmfDSMCommMPIGetUseEnvFileName(XDMFDSMCOMMMPI * dsmComm)
{
  try
  {
    return ((XdmfDSMCommMPI *)dsmComm)->GetUseEnvFileName();
  }
  catch (...)
  {
    return ((XdmfDSMCommMPI *)dsmComm)->GetUseEnvFileName();
  }
}

void XdmfDSMCommMPIInit(XDMFDSMCOMMMPI * dsmComm, int * status)
{
  XDMF_ERROR_WRAP_START(status)
  ((XdmfDSMCommMPI *)dsmComm)->Init();
  XDMF_ERROR_WRAP_END(status)
}

void XdmfDSMCommMPIOpenPort(XDMFDSMCOMMMPI * dsmComm, int * status)
{
  XDMF_ERROR_WRAP_START(status)
  ((XdmfDSMCommMPI *)dsmComm)->OpenPort();
  XDMF_ERROR_WRAP_END(status)
}

void XdmfDSMCommMPIReadDsmPortName(XDMFDSMCOMMMPI * dsmComm)
{
  try
  {
    ((XdmfDSMCommMPI *)dsmComm)->ReadDsmPortName();
  }
  catch (...)
  {
    ((XdmfDSMCommMPI *)dsmComm)->ReadDsmPortName();
  }
}

void XdmfDSMCommMPISetApplicationName(XDMFDSMCOMMMPI * dsmComm, char * newName)
{
  try
  {
    ((XdmfDSMCommMPI *)dsmComm)->SetApplicationName(std::string(newName));
  }
  catch (...)
  {
    ((XdmfDSMCommMPI *)dsmComm)->SetApplicationName(std::string(newName));
  }
}

void XdmfDSMCommMPISetDsmFileName(XDMFDSMCOMMMPI * dsmComm, char * filename)
{
  try
  {
    ((XdmfDSMCommMPI *)dsmComm)->SetDsmFileName(std::string(filename));
  }
  catch (...)
  {
    ((XdmfDSMCommMPI *)dsmComm)->SetDsmFileName(std::string(filename));
  }
}

void XdmfDSMCommMPISetDsmPortName(XDMFDSMCOMMMPI * dsmComm, char * hostName)
{
  try
  {
    ((XdmfDSMCommMPI *)dsmComm)->SetDsmPortName(hostName);
  }
  catch (...)
  {
    ((XdmfDSMCommMPI *)dsmComm)->SetDsmPortName(hostName);
  }
}

void XdmfDSMCommMPISetUseEnvFileName(XDMFDSMCOMMMPI * dsmComm, int status)
{
  try
  {
    ((XdmfDSMCommMPI *)dsmComm)->SetUseEnvFileName(status);
  }
  catch (...)
  {
    ((XdmfDSMCommMPI *)dsmComm)->SetUseEnvFileName(status);
  }
}