EpetraExt  Development
EpetraExt_BlockMapOut.cpp
Go to the documentation of this file.
1 //@HEADER
2 // ***********************************************************************
3 //
4 // EpetraExt: Epetra Extended - Linear Algebra Services Package
5 // Copyright (2011) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 //@HEADER
41 #include "EpetraExt_BlockMapOut.h"
42 #include "EpetraExt_mmio.h"
43 #include "Epetra_Comm.h"
44 #include "Epetra_BlockMap.h"
45 #include "Epetra_Map.h"
46 #include "Epetra_IntVector.h"
47 #include "Epetra_IntSerialDenseVector.h"
48 #include "Epetra_Import.h"
49 
50 using namespace EpetraExt;
51 namespace EpetraExt {
52 
53 int BlockMapToMatrixMarketFile( const char *filename, const Epetra_BlockMap & map,
54  const char * mapName,
55  const char *mapDescription,
56  bool writeHeader) {
57  int M = map.NumGlobalElements();
58  int N = 1;
59  if (map.MaxElementSize()>1) N = 2; // Non-trivial block map, store element sizes in second column
60 
61  FILE * handle = 0;
62 
63  if (map.Comm().MyPID()==0) { // Only PE 0 does this section
64 
65  handle = fopen(filename,"w");
66  if (!handle) return(-1);
67  MM_typecode matcode;
68  mm_initialize_typecode(&matcode);
69  mm_set_matrix(&matcode);
70  mm_set_array(&matcode);
71  mm_set_integer(&matcode);
72 
73  if (writeHeader==true) { // Only write header if requested (true by default)
74 
75  if (mm_write_banner(handle, matcode)) return(-1);
76 
77  if (mapName!=0) fprintf(handle, "%% \n%% %s\n", mapName);
78  if (mapDescription!=0) fprintf(handle, "%% %s\n%% \n", mapDescription);
79 
80  }
81  }
82 
83  if (writeHeader==true) { // Only write header if requested (true by default)
84 
85  // Make an Epetra_IntVector of length numProc such that all elements are on PE 0 and
86  // the ith element is NumMyElements from the ith PE
87 
88  Epetra_Map map1(-1, 1, 0, map.Comm()); // map with one element on each processor
89  int length = 0;
90  if (map.Comm().MyPID()==0) length = map.Comm().NumProc();
91  Epetra_Map map2(-1, length, 0, map.Comm());
92  Epetra_Import lengthImporter(map2, map1);
93  Epetra_IntVector v1(map1);
94  Epetra_IntVector v2(map2);
95  v1[0] = map.NumMyElements();
96  if (v2.Import(v1, lengthImporter, Insert)) return(-1);
97  if (map.Comm().MyPID()==0) {
98  fprintf(handle, "%s", "%Format Version:\n");
99  //int version = 1; // We may change the format scheme at a later date.
100  fprintf(handle, "%% %d \n", map.Comm().NumProc());
101  fprintf(handle, "%s", "%NumProc: Number of processors:\n");
102  fprintf(handle, "%% %d \n", map.Comm().NumProc());
103  fprintf(handle, "%s", "%MaxElementSize: Maximum element size:\n");
104  fprintf(handle, "%% %d \n", map.MaxElementSize());
105  fprintf(handle, "%s", "%MinElementSize: Minimum element size:\n");
106  fprintf(handle, "%% %d \n", map.MinElementSize());
107  fprintf(handle, "%s", "%IndexBase: Index base of map:\n");
108  fprintf(handle, "%% %d \n", map.IndexBase());
109  fprintf(handle, "%s", "%NumGlobalElements: Total number of GIDs in map:\n");
110  fprintf(handle, "%% %d \n", map.NumGlobalElements());
111  fprintf(handle, "%s", "%NumMyElements: BlockMap lengths per processor:\n");
112  for ( int i=0; i< v2.MyLength(); i++) fprintf(handle, "%% %d\n", v2[i]);
113 
114  if (mm_write_mtx_array_size(handle, M, N)) return(-1);
115  }
116  }
117  if (BlockMapToHandle(handle, map)) return(-1); // Everybody calls this routine
118 
119  if (map.Comm().MyPID()==0) // Only PE 0 opened a file
120  if (fclose(handle)) return(-1);
121  return(0);
122 }
123 
124 int BlockMapToHandle(FILE * handle, const Epetra_BlockMap & map) {
125 
126  const Epetra_Comm & comm = map.Comm();
127  int numProc = comm.NumProc();
128  bool doSizes = !map.ConstantElementSize();
129 
130  if (numProc==1) {
131  int * myElements = map.MyGlobalElements();
132  int * elementSizeList = 0;
133  if (doSizes) elementSizeList = map.ElementSizeList();
134  return(writeBlockMap(handle, map.NumGlobalElements(), myElements, elementSizeList, doSizes));
135  }
136 
137  int numRows = map.NumMyElements();
138 
139  Epetra_Map allGidsMap(-1, numRows, 0,comm);
140 
141  Epetra_IntVector allGids(allGidsMap);
142  for (int i=0; i<numRows; i++) allGids[i] = map.GID(i);
143 
144  Epetra_IntVector allSizes(allGidsMap);
145  for (int i=0; i<numRows; i++) allSizes[i] = map.ElementSize(i);
146 
147  // Now construct a Map on PE 0 by strip-mining the rows of the input matrix map.
148  int numChunks = numProc;
149  int stripSize = allGids.GlobalLength()/numChunks;
150  int remainder = allGids.GlobalLength()%numChunks;
151  int curStart = 0;
152  int curStripSize = 0;
153  Epetra_IntSerialDenseVector importGidList;
154  Epetra_IntSerialDenseVector importSizeList;
155  if (comm.MyPID()==0) {
156  importGidList.Size(stripSize+1); // Set size of vector to max needed
157  if (doSizes) importSizeList.Size(stripSize+1); // Set size of vector to max needed
158  }
159  for (int i=0; i<numChunks; i++) {
160  if (comm.MyPID()==0) { // Only PE 0 does this part
161  curStripSize = stripSize;
162  if (i<remainder) curStripSize++; // handle leftovers
163  for (int j=0; j<curStripSize; j++) importGidList[j] = j + curStart;
164  curStart += curStripSize;
165  }
166  // The following import map will be non-trivial only on PE 0.
167  Epetra_Map importGidMap(-1, curStripSize, importGidList.Values(), 0, comm);
168  Epetra_Import gidImporter(importGidMap, allGidsMap);
169 
170  Epetra_IntVector importGids(importGidMap);
171  if (importGids.Import(allGids, gidImporter, Insert)) return(-1);
172  Epetra_IntVector importSizes(importGidMap);
173  if (doSizes) if (importSizes.Import(allSizes, gidImporter, Insert)) return(-1);
174 
175  // importGids (and importSizes, if non-trivial block map)
176  // now have a list of GIDs (and sizes, respectively) for the current strip of map.
177 
178  int * myElements = importGids.Values();
179  int * elementSizeList = 0;
180  if (doSizes) elementSizeList = importSizes.Values();
181  // Finally we are ready to write this strip of the map to file
182  writeBlockMap(handle, importGids.MyLength(), myElements, elementSizeList, doSizes);
183  }
184  return(0);
185 }
186 int writeBlockMap(FILE * handle, int length, const int * v1, const int * v2, bool doSizes) {
187 
188  for (int i=0; i<length; i++) {
189  fprintf(handle, "%d", v1[i]);
190  if (doSizes) fprintf(handle, " %d", v2[i]);
191  fprintf(handle, "%s", "\n");
192  }
193  return(0);
194 }
195 } // namespace EpetraExt
int BlockMapToMatrixMarketFile(const char *filename, const Epetra_BlockMap &map, const char *mapName, const char *mapDescription, bool writeHeader)
Writes an Epetra_BlockMap or Epetra_Map object to a Matrix Market format file.
#define mm_set_matrix(typecode)
int mm_write_banner(FILE *f, MM_typecode matcode)
#define mm_set_integer(typecode)
EpetraExt::BlockCrsMatrix: A class for constructing a distributed block matrix.
char MM_typecode[4]
int BlockMapToHandle(FILE *handle, const Epetra_BlockMap &map)
int mm_write_mtx_array_size(FILE *f, int M, int N)
int writeBlockMap(FILE *handle, int length, const int *v1, const int *v2, bool doSizes)
#define mm_set_array(typecode)
#define mm_initialize_typecode(typecode)