EpetraExt  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EpetraExt_BlockMapIn.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_BlockMapIn.h"
42 #include "Epetra_Comm.h"
43 #include "Epetra_Util.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 #include "EpetraExt_mmio.h"
50 
51 using namespace EpetraExt;
52 namespace EpetraExt {
53 
54 int MatrixMarketFileToMap( const char *filename, const Epetra_Comm & comm, Epetra_Map * & map) {
55 
56  Epetra_BlockMap * bmap;
57  if (MatrixMarketFileToBlockMap(filename, comm, bmap)) return(-1);
58  map = dynamic_cast<Epetra_Map *>(bmap);
59  return(0);
60 }
61 
62 int MatrixMarketFileToBlockMap( const char *filename, const Epetra_Comm & comm, Epetra_BlockMap * & map) {
63 
64  const int lineLength = 1025;
65  char line[lineLength];
66  char token[lineLength];
67  int M, N, numProc, MaxElementSize, MinElementSize, NumMyElements, IndexBase, NumGlobalElements, firstGid;
68 
69  FILE * handle = 0;
70 
71  bool inHeader = true;
72 
73  handle = fopen(filename,"r");
74  if (handle == 0)
75  EPETRA_CHK_ERR(-1); // file not found
76 
77  while (inHeader) {
78  if(fgets(line, lineLength, handle)==0) return(-1);
79  if(sscanf(line, "%s", token)==0) return(-1);
80  if (!strcmp(token, "%NumProc:")) inHeader = false;
81  }
82 
83  if(fgets(line, lineLength, handle)==0) return(-1); // numProc value
84  if(sscanf(line, "%s %d", token, &numProc)==0) return(-1);
85 
86  if(fgets(line, lineLength, handle)==0) return(-1); // MaxElementSize header line
87  if(fgets(line, lineLength, handle)==0) return(-1); // MaxElementSize value
88  if(sscanf(line, "%s %d", token, &MaxElementSize)==0) return(-1);
89 
90  if(fgets(line, lineLength, handle)==0) return(-1); // MinElementSize header line
91  if(fgets(line, lineLength, handle)==0) return(-1); // MinElementSize value
92  if(sscanf(line, "%s %d", token, &MinElementSize)==0) return(-1);
93 
94  if(fgets(line, lineLength, handle)==0) return(-1); // IndexBase header line
95  if(fgets(line, lineLength, handle)==0) return(-1); // IndexBase value
96  if(sscanf(line, "%s %d", token, &IndexBase)==0) return(-1);
97 
98  if(fgets(line, lineLength, handle)==0) return(-1); // NumGlobalElements header line
99  if(fgets(line, lineLength, handle)==0) return(-1); // NumGlobalElements value
100  if(sscanf(line, "%s %d", token, &NumGlobalElements)==0) return(-1);
101 
102  int ierr = 0;
103  (void) ierr; // mfh 13 Jan 2013: Forestall compiler warning for unused var.
104  if (comm.NumProc()==numProc) {
105  if(fgets(line, lineLength, handle)==0) return(-1); // NumMyElements header line
106  firstGid = 0;
107  for (int i=0; i<comm.MyPID(); i++) {
108  if(fgets(line, lineLength, handle)==0) return(-1); // ith NumMyElements value
109  if(sscanf(line, "%s %d", token, &NumMyElements)==0) return(-1);
110  firstGid += NumMyElements;
111  }
112 
113  if(fgets(line, lineLength, handle)==0) return(-1); // This PE's NumMyElements value
114  if(sscanf(line, "%s %d", token, &NumMyElements)==0) return(-1);
115 
116  for (int i=comm.MyPID()+1; i<numProc; i++) {
117  if(fgets(line, lineLength, handle)==0) return(-1); // ith NumMyElements value (dump these)
118  }
119  }
120  else {
121  ierr = 1; // Warning error, different number of processors.
122 
123  if(fgets(line, lineLength, handle)==0) return(-1); // NumMyElements header line
124  for (int i=0; i<numProc; i++) {
125  if(fgets(line, lineLength, handle)==0) return(-1); // ith NumMyElements value (dump these)
126  }
127 
128  NumMyElements = NumGlobalElements/comm.NumProc();
129  firstGid = comm.MyPID()*NumMyElements;
130  int remainder = NumGlobalElements%comm.NumProc();
131  if (comm.MyPID()<remainder) NumMyElements++;
132  int extra = remainder;
133  if (comm.MyPID()<remainder) extra = comm.MyPID();
134  firstGid += extra;
135  }
136  if(fgets(line, lineLength, handle)==0) return(-1); // Number of rows, columns
137  if(sscanf(line, "%d %d", &M, &N)==0) return(-1);
138 
139  bool doSizes = (N>1);
140  Epetra_IntSerialDenseVector v1(NumMyElements);
141  Epetra_IntSerialDenseVector v2(NumMyElements);
142  for (int i=0; i<firstGid; i++) {
143  if(fgets(line, lineLength, handle)==0) return(-1); // dump these
144  }
145 
146  if (doSizes) {
147  for (int i=0; i<NumMyElements; i++) {
148  if(fgets(line, lineLength, handle)==0) return(-1);
149  if(sscanf(line, "%d %d", &v1[i], &v2[i])==0) return(-1); // load v1, v2
150  }
151  }
152  else {
153  for (int i=0; i<NumMyElements; i++) {
154  if(fgets(line, lineLength, handle)==0) return(-1);
155  if(sscanf(line, "%d", &v1[i])==0) return(-1); // load v1
156  v2[i] = MinElementSize; // Fill with constant size
157  }
158  }
159  if (fclose(handle)) return(-1);
160 
161  comm.Barrier();
162 
163  if (MinElementSize==1 && MaxElementSize==1)
164  map = new Epetra_Map(-1, NumMyElements, v1.Values(), IndexBase, comm);
165  else
166  map = new Epetra_BlockMap(-1, NumMyElements, v1.Values(), v2.Values(), IndexBase, comm);
167  return(0);
168 }
169 
170 int MatrixMarketFileToRowMap(const char* filename,
171  const Epetra_Comm& comm,
172  Epetra_BlockMap*& rowmap)
173 {
174  FILE* infile = fopen(filename, "r");
175  MM_typecode matcode;
176 
177  int err = mm_read_banner(infile, &matcode);
178  if (err != 0) return(err);
179 
180  if (!mm_is_matrix(matcode) || !mm_is_coordinate(matcode) ||
181  !mm_is_real(matcode) || !mm_is_general(matcode)) {
182  return(-1);
183  }
184 
185  int numrows, numcols;
186  err = mm_read_mtx_array_size(infile, &numrows, &numcols);
187  if (err != 0) return(err);
188 
189  fclose(infile);
190 
191  rowmap = new Epetra_BlockMap(numrows, 1, 0, comm);
192  return(0);
193 }
194 
195 int MatrixMarketFileToBlockMaps(const char* filename,
196  const Epetra_Comm& comm,
197  Epetra_BlockMap*& rowmap,
198  Epetra_BlockMap*& colmap,
199  Epetra_BlockMap*& rangemap,
200  Epetra_BlockMap*& domainmap)
201 {
202  FILE* infile = fopen(filename, "r");
203  if (infile == NULL) {
204  return(-1);
205  }
206 
207  MM_typecode matcode;
208 
209  int err = mm_read_banner(infile, &matcode);
210  if (err != 0) return(err);
211 
212  if (!mm_is_matrix(matcode) || !mm_is_coordinate(matcode) ||
213  !mm_is_real(matcode) || !mm_is_general(matcode)) {
214  return(-1);
215  }
216 
217  int numrows, numcols, nnz;
218  err = mm_read_mtx_crd_size(infile, &numrows, &numcols, &nnz);
219  if (err != 0) return(err);
220 
221  //for this case, we'll assume that the row-map is the same as
222  //the range-map.
223  //create row-map and range-map with linear distributions.
224 
225  rowmap = new Epetra_BlockMap(numrows, 1, 0, comm);
226  rangemap = new Epetra_BlockMap(numrows, 1, 0, comm);
227 
228  int I, J;
229  double val, imag;
230 
231  int num_map_cols = 0, insertPoint, foundOffset;
232  int allocLen = numcols;
233  int* map_cols = new int[allocLen];
234 
235  //read through all matrix data and construct a list of the column-
236  //indices that occur in rows that are local to this processor.
237 
238  for(int i=0; i<nnz; ++i) {
239  err = mm_read_mtx_crd_entry(infile, &I, &J, &val,
240  &imag, matcode);
241 
242  if (err == 0) {
243  --I;
244  --J;
245  if (rowmap->MyGID(I)) {
246  foundOffset = Epetra_Util_binary_search(J, map_cols, num_map_cols,
247  insertPoint);
248  if (foundOffset < 0) {
249  Epetra_Util_insert(J, insertPoint, map_cols,
250  num_map_cols, allocLen);
251  }
252  }
253  }
254  }
255 
256  //create colmap with the list of columns associated with rows that are
257  //local to this processor.
258  colmap = new Epetra_Map(-1, num_map_cols, map_cols, 0, comm);
259 
260  //create domainmap which has a linear distribution
261  domainmap = new Epetra_BlockMap(numcols, 1, 0, comm);
262 
263  delete [] map_cols;
264 
265  return(0);
266 }
267 
268 } // namespace EpetraExt
269 
int mm_read_mtx_crd_size(FILE *f, int *M, int *N, int *nz)
int mm_read_mtx_array_size(FILE *f, int *M, int *N)
int mm_read_banner(FILE *f, MM_typecode *matcode)
#define mm_is_coordinate(typecode)
int MatrixMarketFileToMap(const char *filename, const Epetra_Comm &comm, Epetra_Map *&map)
Constructs an Epetra_BlockMap object from a Matrix Market format file.
int MatrixMarketFileToRowMap(const char *filename, const Epetra_Comm &comm, Epetra_BlockMap *&rowmap)
char MM_typecode[4]
#define mm_is_real(typecode)
int mm_read_mtx_crd_entry(FILE *f, int *I, int *J, double *real, double *imag, MM_typecode matcode)
#define mm_is_matrix(typecode)
#define mm_is_general(typecode)
int MatrixMarketFileToBlockMap(const char *filename, const Epetra_Comm &comm, Epetra_BlockMap *&map)
Constructs an Epetra_BlockMap object from a Matrix Market format file.
int MatrixMarketFileToBlockMaps(const char *filename, const Epetra_Comm &comm, Epetra_BlockMap *&rowmap, Epetra_BlockMap *&colmap, Epetra_BlockMap *&rangemap, Epetra_BlockMap *&domainmap)
Constructs row,col,range and domain maps from a matrix-market matrix file.