Skip to content

Commit

Permalink
create/open_parallel function.
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamer2368 committed Feb 17, 2024
1 parent e39aca7 commit 3a84a69
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 15 deletions.
54 changes: 41 additions & 13 deletions lib/utils/CSVDatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#define included_CSVDatabase_h

#include "Database.h"
#include "Utilities.h"
#include <string>
#include <fstream>
#include <complex>
Expand All @@ -39,25 +40,27 @@ class CSVDatabase : public Database

/**
* @brief Creates a new CSV database file with the supplied name.
* NOTE: CSVDatabase does not actually create a file with this.
* This function will only print out the file_name.
*
* @param[in] file_name Name of CSV database file to create.
*
* @return True if file create was successful.
*/
virtual
bool
create(
const std::string& file_name) override;

/**
* @brief Opens an existing CSV database file with the supplied name.
* NOTE: CSVDatabase does not actually open a file with this.
* This function will only print out the file_name.
*
* @param[in] file_name Name of existing CSV database file to open.
* @param[in] type Read/write type ("r"/"wr")
*
* @return True if file open was successful.
*/
virtual
bool
open(
const std::string& file_name,
Expand All @@ -68,7 +71,6 @@ class CSVDatabase : public Database
*
* @return True if the file close was successful.
*/
virtual
bool
close();

Expand All @@ -84,7 +86,6 @@ class CSVDatabase : public Database
* @param[in] data The array of integer values to be written.
* @param[in] nelements The number of integers in the array.
*/
virtual
void
putIntegerArray(
const std::string& file_name,
Expand All @@ -103,7 +104,6 @@ class CSVDatabase : public Database
* @param[in] data The array of double values to be written.
* @param[in] nelements The number of doubles in the array.
*/
virtual
void
putDoubleArray(
const std::string& file_name,
Expand All @@ -123,7 +123,6 @@ class CSVDatabase : public Database
* @param[in] data The vector of double values to be written.
* @param[in] nelements The number of doubles in the vector.
*/
virtual
void
putDoubleVector(
const std::string& file_name,
Expand All @@ -142,7 +141,6 @@ class CSVDatabase : public Database
* @param[in] data The vector of complex double values to be written.
* @param[in] nelements The number of complex doubles in the vector.
*/
virtual
void
putComplexVector(
const std::string& file_name,
Expand All @@ -161,7 +159,6 @@ class CSVDatabase : public Database
* @param[in] data The vector of strings to be written.
* @param[in] nelements The number of strings in the vector.
*/
virtual
void
putStringVector(
const std::string& file_name,
Expand All @@ -179,7 +176,6 @@ class CSVDatabase : public Database
* @param[out] data The allocated array of integer values to be read.
* @param[in] nelements The number of integers in the array.
*/
virtual
void
getIntegerArray(
const std::string& file_name,
Expand Down Expand Up @@ -211,7 +207,6 @@ class CSVDatabase : public Database
* @param[in] file_name The filename associated with the array of values to be
* read.
*/
virtual
int
getDoubleArraySize(const std::string& file_name)
{
Expand All @@ -231,7 +226,6 @@ class CSVDatabase : public Database
* @param[out] data The allocated array of double values to be read.
* @param[in] nelements The number of doubles in the array.
*/
virtual
void
getDoubleArray(
const std::string& file_name,
Expand All @@ -250,7 +244,6 @@ class CSVDatabase : public Database
* @param[in] nelements The number of doubles in the full array.
* @param[in] idx The set of indices in the sub-array.
*/
virtual
void
getDoubleArray(
const std::string& file_name,
Expand All @@ -272,7 +265,6 @@ class CSVDatabase : public Database
* @param[in] block_size The block size to read from the CSV dataset.
* @param[in] stride The stride to read from the CSV dataset.
*/
virtual
void
getDoubleArray(
const std::string& file_name,
Expand Down Expand Up @@ -326,6 +318,42 @@ class CSVDatabase : public Database
getLineCount(
const std::string& file_name);

/**
* @brief Unsupported parallel I/O function.
*
* @param[in] file_name Base Name of CSV database file to create.
* @param[in] comm MPI communicator to get the rank.
*
* @return True if file create was successful.
*/
bool
create_parallel(
const std::string& file_name,
const MPI_Comm comm)
{
CAROM_ERROR("CSVDatabase does not support parallel I/O!\n");
return false;
}

/**
* @brief Unsupported parallel I/O function.
*
* @param[in] file_name Name of existing CSV database file to open.
* @param[in] type Read/write type ("r"/"wr")
* @param[in] comm MPI communicator to get the rank.
*
* @return True if file open was successful.
*/
bool
open_parallel(
const std::string& file_name,
const std::string& type,
const MPI_Comm comm)
{
CAROM_ERROR("CSVDatabase does not support parallel I/O!\n");
return false;
}

private:
/**
* @brief Unimplemented copy constructor.
Expand Down
37 changes: 37 additions & 0 deletions lib/utils/Database.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include <string>
#include <vector>
#include "mpi.h"

namespace CAROM {

Expand Down Expand Up @@ -51,6 +52,23 @@ class Database
create(
const std::string& file_name);

/**
* @brief Creates a new database file with the supplied name, with parallel I/O support.
* Supported only for HDF5 format.
* For HDFDatabase, the function is equivalent to create,
* only extending the file name with 6 digits indicating processor rank.
* For HDFDatabaseMPIO, the file is created with MPI I/O file access property.
*
* @param[in] file_name Name of database file to create.
*
* @return True if file create was successful.
*/
virtual
bool
create_parallel(
const std::string& file_name,
const MPI_Comm comm) = 0;

/**
* @brief Opens an existing database file with the supplied name.
*
Expand All @@ -65,6 +83,25 @@ class Database
const std::string& file_name,
const std::string& type);

/**
* @brief Opens an existing database file with the supplied name, with parallel I/O support.
* Supported only for HDF5 format.
* For HDFDatabase, the function is equivalent to open,
* only extending the file name with 6 digits indicating processor rank.
* For HDFDatabaseMPIO, the file is opened with MPI I/O file access property.
*
* @param[in] file_name Name of existing database file to open.
* @param[in] type Read/write type ("r"/"wr")
*
* @return True if file open was successful.
*/
virtual
bool
open_parallel(
const std::string& file_name,
const std::string& type,
const MPI_Comm comm) = 0;

/**
* @brief Closes the currently open database file.
*
Expand Down
1 change: 0 additions & 1 deletion lib/utils/HDFDatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
// Description: The concrete database implementation using HDF5.

#include "HDFDatabase.h"
#include "Utilities.h"
#include <iostream>

namespace CAROM {
Expand Down
62 changes: 61 additions & 1 deletion lib/utils/HDFDatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#define included_HDFDatabase_h

#include "Database.h"
#include "Utilities.h"
#include "hdf5.h"
#include <string>

Expand Down Expand Up @@ -62,6 +63,59 @@ class HDFDatabase : public Database
const std::string& file_name,
const std::string& type) override;

/**
* @brief Creates a new HDF5 database file with the supplied name,
* extending it with 6 digits indicating the process rank.
*
* @param[in] file_name Name of HDF5 database file to create.
* @param[in] comm MPI communicator to obtain the process rank.
*
* @return True if file create was successful.
*/
virtual bool
create_parallel(
const std::string& file_name,
const MPI_Comm comm) override
{
CAROM_VERIFY(!file_name.empty());
MPI_Comm_rank(comm, &d_rank);

std::string ext_filename(file_name);
char tmp[10];
sprintf(tmp, ".%06d", d_rank);
ext_filename += tmp;

return create(ext_filename);
}

/**
* @brief Opens an existing HDF5 database file with the supplied name,
* extending it with 6 digits indicating the process rank.
*
* @param[in] file_name Name of existing HDF5 database file to open.
* @param[in] type Read/write type ("r"/"wr")
* @param[in] comm MPI communicator to obtain the process rank.
*
* @return True if file open was successful.
*/
virtual
bool
open_parallel(
const std::string& file_name,
const std::string& type,
const MPI_Comm comm) override
{
CAROM_VERIFY(!file_name.empty());
MPI_Comm_rank(comm, &d_rank);

std::string ext_filename(file_name);
char tmp[10];
sprintf(tmp, ".%06d", d_rank);
ext_filename += tmp;

return open(ext_filename, type);
}

/**
* @brief Closes the currently open HDF5 database file.
*
Expand Down Expand Up @@ -312,7 +366,13 @@ class HDFDatabase : public Database
/**
* @brief The key representing an integer array.
* */
static const int KEY_INT_ARRAY;
static const int KEY_INT_ARRAY;

/**
* @brief MPI process rank.
* Used only when d_parallel is true.
*/
int d_rank;
};

}
Expand Down

0 comments on commit 3a84a69

Please sign in to comment.