EpetraExt Development
EpetraExt_MultiVectorIn.cpp
Go to the documentation of this file.
00001 //@HEADER
00002 // ***********************************************************************
00003 //
00004 //     EpetraExt: Epetra Extended - Linear Algebra Services Package
00005 //                 Copyright (2011) Sandia Corporation
00006 //
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ***********************************************************************
00040 //@HEADER
00041 #include "EpetraExt_MultiVectorIn.h"
00042 #include "Epetra_Comm.h"
00043 #include "Epetra_MultiVector.h"
00044 #include "Epetra_Vector.h"
00045 #include "Epetra_BlockMap.h"
00046 
00047 using namespace EpetraExt;
00048 namespace EpetraExt {
00049 
00050 int MatrixMarketFileToMultiVector( const char *filename, const Epetra_BlockMap & map, Epetra_MultiVector * & A) {
00051 
00052   const int lineLength = 1025;
00053   const int tokenLength = 35;
00054   char line[lineLength];
00055   char token1[tokenLength];
00056   char token2[tokenLength];
00057   char token3[tokenLength];
00058   char token4[tokenLength];
00059   char token5[tokenLength];
00060   int M, N;
00061 
00062   FILE * handle = 0;
00063 
00064   handle = fopen(filename,"r");  // Open file
00065   if (handle == 0)
00066     EPETRA_CHK_ERR(-1); // file not found
00067 
00068   // Check first line, which should be "%%MatrixMarket matrix coordinate real general" (without quotes)
00069   if(fgets(line, lineLength, handle)==0) return(-1);
00070   if(sscanf(line, "%s %s %s %s %s", token1, token2, token3, token4, token5 )==0) return(-1);
00071   if (strcmp(token1, "%%MatrixMarket") ||
00072       strcmp(token2, "matrix") ||
00073       strcmp(token3, "array") ||
00074       strcmp(token4, "real") ||
00075       strcmp(token5, "general")) return(-1);
00076 
00077   // Next, strip off header lines (which start with "%")
00078   do {
00079     if(fgets(line, lineLength, handle)==0) return(-1);
00080   } while (line[0] == '%');
00081 
00082   // Next get problem dimensions: M, N
00083   if(sscanf(line, "%d %d", &M, &N)==0) return(-1);
00084 
00085   // Compute the offset for each processor for when it should start storing values
00086   int numMyPoints = map.NumMyPoints();
00087   int offset;
00088   map.Comm().ScanSum(&numMyPoints, &offset, 1); // ScanSum will compute offsets for us
00089   offset -= numMyPoints; // readjust for my PE
00090 
00091   // Now construct vector/multivector
00092   if (N==1)
00093     A = new Epetra_Vector(map);
00094   else
00095     A = new Epetra_MultiVector(map, N);
00096 
00097   double ** Ap = A->Pointers();
00098 
00099   for (int j=0; j<N; j++) {
00100     double * v = Ap[j];
00101 
00102     // Now read in lines that we will discard
00103     for (int i=0; i<offset; i++)
00104       if(fgets(line, lineLength, handle)==0) return(-1);
00105     
00106     // Now read in each value and store to the local portion of the the  if the row is owned.
00107     double V;
00108     for (int i=0; i<numMyPoints; i++) {
00109       if(fgets(line, lineLength, handle)==0) return(-1);
00110       if(sscanf(line, "%lg\n", &V)==0) return(-1);
00111       v[i] = V;
00112     }
00113     // Now read in the rest of the lines to discard
00114     for (int i=0; i < M-numMyPoints-offset; i++) {
00115       if(fgets(line, lineLength, handle)==0) return(-1);
00116     }
00117   }
00118 
00119   if (fclose(handle)) return(-1);
00120   
00121   return(0);
00122 }
00123 
00124 } // namespace EpetraExt
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines