Example #1
0
void MapEpetra::importFromHDF5 ( std::string const& fileName, std::string const& mapName )
{
    ASSERT (M_commPtr.get()!=0, "Error! The stored communicator pointer is not valid.\n");

    EpetraExt::HDF5 HDF5 ( *M_commPtr );

    // Open an existing file
    HDF5.Open ( ( fileName + ".h5" ).data() );

    // Check if the file is created
    if ( !HDF5.IsOpen () )
    {
        std::cerr << "Unable to open " + fileName + ".h5";
        abort();
    }

    // Read the unique map from the file
    Epetra_Map* importedMap ( 0 );
    HDF5.Read ( ( mapName + "Unique" ).c_str(), importedMap );

    // Copy the loaded map to the member object
    M_uniqueMapEpetra.reset ( new map_Type ( *importedMap ) );

    // Read the repeated map from the file
    HDF5.Read ( ( mapName + "Repeated" ).c_str(), importedMap );

    // Copy the loaded matrix to the member object
    M_repeatedMapEpetra.reset ( new map_Type ( *importedMap ) );

    // Close the file
    HDF5.Close();

} // importFromHDF5
Example #2
0
void MapEpetra::exportToHDF5 ( std::string const& fileName, std::string const& mapName, bool const truncate )
{
    ASSERT (M_commPtr.get()!=0, "Error! The stored communicator pointer is not valid.\n");
    ASSERT (M_uniqueMapEpetra.get()!=0 && M_repeatedMapEpetra.get()!=0, "Error! One (or both) the map pointers are not valid.\n");

    EpetraExt::HDF5 HDF5 ( *M_commPtr );

    if ( truncate )
    {
        // Create and open the file / Truncate and open the file
        HDF5.Create ( ( fileName + ".h5" ).data() );
    }
    else
    {
        // Open an existing file without truncating it
        HDF5.Open ( ( fileName + ".h5" ).data() );
    }

    // Check if the file is created
    if ( !HDF5.IsOpen () )
    {
        std::cerr << "Unable to create " + fileName + ".h5";
        abort();
    }

    // Save the maps into the file
    HDF5.Write ( ( mapName + "Unique" ).c_str(), *M_uniqueMapEpetra );
    HDF5.Write ( ( mapName + "Repeated" ).c_str(), *M_repeatedMapEpetra );

    // Close the file
    HDF5.Close();

} // exportToHDF5
Example #3
0
int main (int argc, char **argv)
{
#ifdef HAVE_MPI
  MPI_Init(&argc, &argv);
  Epetra_MpiComm Comm(MPI_COMM_WORLD);
#else
  Epetra_SerialComm Comm;
#endif

  try 
  {
    // This is the HDF5 file manager
    EpetraExt::HDF5 HDF5(Comm);

    // creates a new file. To open an existing file, use Open("myfile.h5")
    // This file contains:
    // - a sparse (diagonal) matrix, whose group name is "speye"
    // - a multivector, whose group name is "x"
    // - a map for 2-processor run, whose group name is "map-2"

    HDF5.Open("matlab.h5");

    if (Comm.MyPID() == 0)
      cout << endl;
      cout << "*) Reading Epetra_CrsMatrix from HDF5 file matlab.h5..." << endl;
      cout << endl;

    // first query for matrix properties:
    int NumGlobalRows, NumGlobalCols, NumGlobalNonzeros;
    int NumGlobalDiagonals, MaxNumEntries;
    double NormOne, NormInf;

    HDF5.ReadCrsMatrixProperties("speye", NumGlobalRows, NumGlobalCols,
                                 NumGlobalNonzeros, NumGlobalDiagonals, 
                                 MaxNumEntries, NormOne, NormInf);

    if (Comm.MyPID() == 0)
    {
      cout << "Matrix information as given by ReadCrsMatrixProperties()";
      cout << endl << endl;
      cout << "NumGlobalRows = " << NumGlobalRows << endl;
      cout << "NumGlobalCols = " << NumGlobalCols << endl;
      cout << "NumGlobalNonzeros = " << NumGlobalNonzeros << endl;
      cout << "NumGlobalDiagonals = " << NumGlobalDiagonals << endl;
      cout << "MaxNumEntries = " << MaxNumEntries << endl;
      cout << "NormOne = " << NormOne << endl;
      cout << "NormInf = " << NormInf << endl;
    }

    // the reading the actual matrix, with a linear map, since no map 
    // has been specified.
    Epetra_CrsMatrix* Matrix = 0;
    HDF5.Read("speye", Matrix);

    cout << *Matrix;

    if (Comm.MyPID() == 0)
    {
      cout << endl;
      cout << "*) Reading Epetra_MultiVector from HDF5 file matlab.h5..." << endl;
      cout << endl;
    }

    Epetra_MultiVector* x;
    HDF5.Read("x", x);
    cout << *x;

    if (Comm.NumProc() == 2)
    {
      if (Comm.MyPID() == 0)
      {
        cout << endl;
        cout << "*) Reading Epetra_Map from HDF5 file matlab.h5..." << endl;
        cout << endl;
      }

      Epetra_Map* Map;
      HDF5.Read("map-2", Map);
      cout << *Map;
    }

    // We finally close the file. Better to close it before calling
    // MPI_Finalize() to avoid MPI-related errors, since Close() might call MPI
    // functions.
    HDF5.Close();

    // delete memory
    if (Matrix) delete Matrix;
  }
  catch(EpetraExt::Exception& rhs) 
  {
    rhs.Print();
  }
  catch (...) 
  {
    cerr << "Caught generic exception" << endl;
  }

#ifdef HAVE_MPI
  MPI_Finalize();
#endif

  return(EXIT_SUCCESS);
}
Example #4
0
int main (int argc, char **argv)
{
#ifdef HAVE_MPI
  MPI_Init(&argc, &argv);
  Epetra_MpiComm Comm(MPI_COMM_WORLD);
#else
  Epetra_SerialComm Comm;
#endif

  if (Comm.MyPID() == 0)
  {
    cout << "Converter from MatrixMarket files to HDF5 files" << endl;
    cout << "For notes on the usage, execute" << endl;
    cout << "  ./HDF5Converter.exe --help" << endl;
    cout << endl;
  }

  // Creating an empty command line processor looks like:
  Teuchos::CommandLineProcessor CLP;

  string MapFileName    = "not-set";
  string XFileName      = "not-set";
  string BFileName      = "not-set";
  string MatrixFileName = "not-set";
  string HDF5FileName   = "myfile.f5";
  string MapHDF5Name    = "map";
  string XHDF5Name      = "X";
  string BHDF5Name      = "B";
  string MatrixHDF5Name = "matrix";

  CLP.setOption("in-map",    &MapFileName,    "map file name");
  CLP.setOption("in-matrix", &MatrixFileName, "matrix file name");
  CLP.setOption("in-x",      &XFileName,      "x vector file name");
  CLP.setOption("in-b",      &BFileName,      "b vector file name");
  CLP.setOption("output",    &HDF5FileName,   "name of HDF5 file");
  CLP.setOption("out-map",    &MapHDF5Name,    "map name in HDF5 file");
  CLP.setOption("out-matrix", &MatrixHDF5Name, "matrix name in HDF5 file");
  CLP.setOption("out-x",      &XHDF5Name,      "x vector name in HDF5 file");
  CLP.setOption("out-b",      &BHDF5Name,      "b vector name in HDF5 file");

  CLP.throwExceptions(false);
  CLP.parse(argc,argv);

  Epetra_Map* Map = 0;
  Epetra_CrsMatrix* Matrix = 0;
  Epetra_MultiVector* X = 0;
  Epetra_MultiVector* B = 0;

  if (MapFileName != "not-set")
  {
    if (Comm.MyPID() == 0)
      cout << "Reading map from " << MapFileName << endl;

    EpetraExt::MatrixMarketFileToMap(MapFileName.c_str(), Comm, Map);
  }
  else
  {
    cerr << "You need to specify a map, sorry" << endl;
#ifdef HAVE_MPI
    MPI_Finalize();
#endif
    exit(EXIT_SUCCESS);
  }

  if (XFileName != "not-set")
  {
    if (Comm.MyPID() == 0)
      cout << "Reading vector from " << XFileName << endl;

    EpetraExt::MatrixMarketFileToMultiVector(XFileName.c_str(), *Map, X);
  }

  if (BFileName != "not-set")
  {
    if (Comm.MyPID() == 0)
      cout << "Reading vector from " << BFileName << endl;

    EpetraExt::MatrixMarketFileToMultiVector(BFileName.c_str(), *Map, B);
  }

  if (MatrixFileName != "not-set")
  {
    if (Comm.MyPID() == 0)
      cout << "Reading matrix from " << MatrixFileName << endl;

    EpetraExt::MatrixMarketFileToCrsMatrix(MatrixFileName.c_str(), *Map, Matrix);
  }

  // ================================= //
  // Open HDF5 file and append data in //
  // ================================= //
  
  EpetraExt::HDF5 HDF5(Comm);

  HDF5.Create(HDF5FileName);

  if (Map)
    HDF5.Write(MapHDF5Name + EpetraExt::toString(Comm.NumProc()), *Map);
  if (Matrix)
    HDF5.Write(MatrixHDF5Name, *Matrix);
  if (X)
    HDF5.Write(XHDF5Name, *X);
  if (B)
    HDF5.Write(BHDF5Name, *B);
  HDF5.Close();

  if (Map) delete Map;
  if (Matrix) delete Matrix;
  if (X) delete X;
  if (B) delete B;

#ifdef HAVE_MPI
  MPI_Finalize();
#endif

  return(EXIT_SUCCESS);
}