EpetraExt Package Browser (Single Doxygen Collection)  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EpetraExt_MultiVectorIn.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
42 #include "Epetra_Comm.h"
43 #include "Epetra_MultiVector.h"
44 #include "Epetra_Vector.h"
45 #include "Epetra_BlockMap.h"
46 
47 using namespace EpetraExt;
48 namespace EpetraExt {
49 
50 int MatrixMarketFileToMultiVector( const char *filename, const Epetra_BlockMap & map, Epetra_MultiVector * & A) {
51 
52  const int lineLength = 1025;
53  const int tokenLength = 35;
54  char line[lineLength];
55  char token1[tokenLength];
56  char token2[tokenLength];
57  char token3[tokenLength];
58  char token4[tokenLength];
59  char token5[tokenLength];
60  int M, N;
61 
62  FILE * handle = 0;
63 
64  handle = fopen(filename,"r"); // Open file
65  if (handle == 0)
66  EPETRA_CHK_ERR(-1); // file not found
67 
68  // Check first line, which should be "%%MatrixMarket matrix coordinate real general" (without quotes)
69  if(fgets(line, lineLength, handle)==0) return(-1);
70  if(sscanf(line, "%s %s %s %s %s", token1, token2, token3, token4, token5 )==0) return(-1);
71  if (strcmp(token1, "%%MatrixMarket") ||
72  strcmp(token2, "matrix") ||
73  strcmp(token3, "array") ||
74  strcmp(token4, "real") ||
75  strcmp(token5, "general")) return(-1);
76 
77  // Next, strip off header lines (which start with "%")
78  do {
79  if(fgets(line, lineLength, handle)==0) return(-1);
80  } while (line[0] == '%');
81 
82  // Next get problem dimensions: M, N
83  if(sscanf(line, "%d %d", &M, &N)==0) return(-1);
84 
85  // Compute the offset for each processor for when it should start storing values
86  int numMyPoints = map.NumMyPoints();
87  int offset;
88  map.Comm().ScanSum(&numMyPoints, &offset, 1); // ScanSum will compute offsets for us
89  offset -= numMyPoints; // readjust for my PE
90 
91  // Now construct vector/multivector
92  if (N==1)
93  A = new Epetra_Vector(map);
94  else
95  A = new Epetra_MultiVector(map, N);
96 
97  double ** Ap = A->Pointers();
98 
99  for (int j=0; j<N; j++) {
100  double * v = Ap[j];
101 
102  // Now read in lines that we will discard
103  for (int i=0; i<offset; i++)
104  if(fgets(line, lineLength, handle)==0) return(-1);
105 
106  // Now read in each value and store to the local portion of the the if the row is owned.
107  double V;
108  for (int i=0; i<numMyPoints; i++) {
109  if(fgets(line, lineLength, handle)==0) return(-1);
110  if(sscanf(line, "%lg\n", &V)==0) return(-1);
111  v[i] = V;
112  }
113  // Now read in the rest of the lines to discard
114  for (int i=0; i < M-numMyPoints-offset; i++) {
115  if(fgets(line, lineLength, handle)==0) return(-1);
116  }
117  }
118 
119  if (fclose(handle)) return(-1);
120 
121  return(0);
122 }
123 
124 } // namespace EpetraExt
int MatrixMarketFileToMultiVector(const char *filename, const Epetra_BlockMap &map, Epetra_MultiVector *&A)
Constructs an Epetra_MultiVector object from a Matrix Market format file.
#define EPETRA_CHK_ERR(a)
const int N
const Epetra_Comm & Comm() const
int NumMyPoints() const
virtual int ScanSum(double *MyVals, double *ScanSums, int Count) const =0