From badf5bf2c962f18b3d9ee15b639842822f14195d Mon Sep 17 00:00:00 2001 From: Benjamin Brands Date: Thu, 11 Oct 2018 10:47:39 +0200 Subject: [PATCH] use std::string instead of char * --- include/deal.II/lac/scalapack.h | 27 +++++++++++++++--- source/lac/scalapack.cc | 46 +++++++++++++++++++++++-------- tests/scalapack/scalapack_10.cc | 4 +-- tests/scalapack/scalapack_10_a.cc | 8 +++--- tests/scalapack/scalapack_10_b.cc | 4 +-- tests/scalapack/scalapack_10_c.cc | 36 ++++++++++++------------ tests/scalapack/scalapack_10_d.cc | 2 +- 7 files changed, 84 insertions(+), 43 deletions(-) diff --git a/include/deal.II/lac/scalapack.h b/include/deal.II/lac/scalapack.h index 693f1b4217..f5d9d0a8f7 100644 --- a/include/deal.II/lac/scalapack.h +++ b/include/deal.II/lac/scalapack.h @@ -467,6 +467,17 @@ public: * and chunk_size.second the number of columns. */ void + save(const std::string & filename, + const std::pair &chunk_size = + std::make_pair(numbers::invalid_unsigned_int, + numbers::invalid_unsigned_int)) const; + + /** + * Same as above but takes a filename as a char pointer. + * + * @deprecated This function is deprecated. Use the one taking std::string instead. + */ + DEAL_II_DEPRECATED void save(const char * filename, const std::pair &chunk_size = std::make_pair(numbers::invalid_unsigned_int, @@ -484,6 +495,14 @@ public: * and distribute the content to the other processes subsequently. */ void + load(const std::string &filename); + + /** + * Same as above but takes a filename as a char pointer. + * + * @deprecated This function is deprecated. Use the one taking std::string instead. + */ + DEAL_II_DEPRECATED void load(const char *filename); /** @@ -867,7 +886,7 @@ private: * using serial routines */ void - save_serial(const char * filename, + save_serial(const std::string & filename, const std::pair &chunk_size) const; /* @@ -875,14 +894,14 @@ private: * using serial routines */ void - load_serial(const char *filename); + load_serial(const std::string &filename); /* * Stores the distributed matrix in @p filename * using parallel routines */ void - save_parallel(const char * filename, + save_parallel(const std::string & filename, const std::pair &chunk_size) const; /* @@ -890,7 +909,7 @@ private: * using parallel routines */ void - load_parallel(const char *filename); + load_parallel(const std::string &filename); /** * Since ScaLAPACK operations notoriously change the meaning of the matrix diff --git a/source/lac/scalapack.cc b/source/lac/scalapack.cc index 96275b4155..be71ea82a0 100644 --- a/source/lac/scalapack.cc +++ b/source/lac/scalapack.cc @@ -2610,7 +2610,7 @@ namespace internal template void ScaLAPACKMatrix::save( - const char * filename, + const std::string & filename, const std::pair &chunk_size) const { # ifndef DEAL_II_WITH_HDF5 @@ -2651,9 +2651,20 @@ ScaLAPACKMatrix::save( template void -ScaLAPACKMatrix::save_serial( +ScaLAPACKMatrix::save( const char * filename, const std::pair &chunk_size) const +{ + save(std::string(filename), chunk_size); +} + + + +template +void +ScaLAPACKMatrix::save_serial( + const std::string & filename, + const std::pair &chunk_size) const { # ifndef DEAL_II_WITH_HDF5 (void)filename; @@ -2687,7 +2698,7 @@ ScaLAPACKMatrix::save_serial( // create a new file using default properties hid_t file_id = - H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); + H5Fcreate(filename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); // modify dataset creation properties, i.e. enable chunking hsize_t chunk_dims[2]; @@ -2809,7 +2820,7 @@ ScaLAPACKMatrix::save_serial( template void ScaLAPACKMatrix::save_parallel( - const char * filename, + const std::string & filename, const std::pair &chunk_size) const { # ifndef DEAL_II_WITH_HDF5 @@ -2865,8 +2876,9 @@ ScaLAPACKMatrix::save_parallel( AssertThrow(status >= 0, ExcIO()); // create a new file collectively and release property list identifier - hid_t file_id = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, plist_id); - status = H5Pclose(plist_id); + hid_t file_id = + H5Fcreate(filename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, plist_id); + status = H5Pclose(plist_id); AssertThrow(status >= 0, ExcIO()); // As ScaLAPACK, and therefore the class ScaLAPACKMatrix, uses column-major @@ -2967,7 +2979,8 @@ ScaLAPACKMatrix::save_parallel( if (tmp.grid->this_mpi_process == 0) { // open file using default properties - hid_t file_id_reopen = H5Fopen(filename, H5F_ACC_RDWR, H5P_DEFAULT); + hid_t file_id_reopen = + H5Fopen(filename.c_str(), H5F_ACC_RDWR, H5P_DEFAULT); // create HDF5 enum type for LAPACKSupport::State and // LAPACKSupport::Property @@ -3041,7 +3054,7 @@ ScaLAPACKMatrix::save_parallel( template void -ScaLAPACKMatrix::load(const char *filename) +ScaLAPACKMatrix::load(const std::string &filename) { # ifndef DEAL_II_WITH_HDF5 (void)filename; @@ -3062,7 +3075,16 @@ ScaLAPACKMatrix::load(const char *filename) template void -ScaLAPACKMatrix::load_serial(const char *filename) +ScaLAPACKMatrix::load(const char *filename) +{ + load(std::string(filename)); +} + + + +template +void +ScaLAPACKMatrix::load_serial(const std::string &filename) { # ifndef DEAL_II_WITH_HDF5 (void)filename; @@ -3093,7 +3115,7 @@ ScaLAPACKMatrix::load_serial(const char *filename) herr_t status; // open the file in read-only mode - hid_t file_id = H5Fopen(filename, H5F_ACC_RDONLY, H5P_DEFAULT); + hid_t file_id = H5Fopen(filename.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT); // open the dataset in the file hid_t dataset_id = H5Dopen2(file_id, "/matrix", H5P_DEFAULT); @@ -3240,7 +3262,7 @@ ScaLAPACKMatrix::load_serial(const char *filename) template void -ScaLAPACKMatrix::load_parallel(const char *filename) +ScaLAPACKMatrix::load_parallel(const std::string &filename) { # ifndef DEAL_II_WITH_HDF5 (void)filename; @@ -3282,7 +3304,7 @@ ScaLAPACKMatrix::load_parallel(const char *filename) // open file collectively in read-only mode and release property list // identifier - hid_t file_id = H5Fopen(filename, H5F_ACC_RDONLY, plist_id); + hid_t file_id = H5Fopen(filename.c_str(), H5F_ACC_RDONLY, plist_id); status = H5Pclose(plist_id); AssertThrow(status >= 0, ExcIO()); diff --git a/tests/scalapack/scalapack_10.cc b/tests/scalapack/scalapack_10.cc index 869bb28cc2..b1c5b4ee1d 100644 --- a/tests/scalapack/scalapack_10.cc +++ b/tests/scalapack/scalapack_10.cc @@ -60,8 +60,8 @@ test(const unsigned int size, const unsigned int block_size) size, size, grid, block_size, block_size); scalapack_matrix = full; - scalapack_matrix.save(filename.c_str()); - scalapack_matrix_copy.load(filename.c_str()); + scalapack_matrix.save(filename); + scalapack_matrix_copy.load(filename); FullMatrix copy(size); scalapack_matrix_copy.copy_to(copy); diff --git a/tests/scalapack/scalapack_10_a.cc b/tests/scalapack/scalapack_10_a.cc index 4a63413178..03d7aa5101 100644 --- a/tests/scalapack/scalapack_10_a.cc +++ b/tests/scalapack/scalapack_10_a.cc @@ -72,11 +72,11 @@ test(const unsigned int size, const unsigned int block_size) matrix = full; matrix_one = full; - matrix.save(filename_parallel.c_str()); - matrix_one.save(filename_serial.c_str()); + matrix.save(filename_parallel); + matrix_one.save(filename_serial); - matrix.load(filename_serial.c_str()); - matrix_one.load(filename_parallel.c_str()); + matrix.load(filename_serial); + matrix_one.load(filename_parallel); std::remove(filename_serial.c_str()); std::remove(filename_parallel.c_str()); diff --git a/tests/scalapack/scalapack_10_b.cc b/tests/scalapack/scalapack_10_b.cc index 958e12f39e..48e064e6b0 100644 --- a/tests/scalapack/scalapack_10_b.cc +++ b/tests/scalapack/scalapack_10_b.cc @@ -59,8 +59,8 @@ test(const std::pair &size, size.first, size.second, grid, block_size, block_size); scalapack_matrix = full; - scalapack_matrix.save(filename.c_str(), chunk_size); - scalapack_matrix_copy.load(filename.c_str()); + scalapack_matrix.save(filename, chunk_size); + scalapack_matrix_copy.load(filename); FullMatrix copy(size.first, size.second); scalapack_matrix_copy.copy_to(copy); diff --git a/tests/scalapack/scalapack_10_c.cc b/tests/scalapack/scalapack_10_c.cc index 9ded2e2dae..a8060a7de9 100644 --- a/tests/scalapack/scalapack_10_c.cc +++ b/tests/scalapack/scalapack_10_c.cc @@ -63,64 +63,64 @@ test() size, size, grid, block_size, block_size); scalapack_matrix.set_property(LAPACKSupport::Property::diagonal); - scalapack_matrix.save(filename.c_str()); - scalapack_matrix_copy.load(filename.c_str()); + scalapack_matrix.save(filename); + scalapack_matrix_copy.load(filename); std::remove(filename.c_str()); AssertThrow(scalapack_matrix.get_property() == scalapack_matrix_copy.get_property(), ExcInternalError()); scalapack_matrix.set_property(LAPACKSupport::Property::general); - scalapack_matrix.save(filename.c_str()); - scalapack_matrix_copy.load(filename.c_str()); + scalapack_matrix.save(filename); + scalapack_matrix_copy.load(filename); std::remove(filename.c_str()); AssertThrow(scalapack_matrix.get_property() == scalapack_matrix_copy.get_property(), ExcInternalError()); scalapack_matrix.set_property(LAPACKSupport::Property::hessenberg); - scalapack_matrix.save(filename.c_str()); - scalapack_matrix_copy.load(filename.c_str()); + scalapack_matrix.save(filename); + scalapack_matrix_copy.load(filename); std::remove(filename.c_str()); AssertThrow(scalapack_matrix.get_property() == scalapack_matrix_copy.get_property(), ExcInternalError()); scalapack_matrix.set_property(LAPACKSupport::Property::lower_triangular); - scalapack_matrix.save(filename.c_str()); - scalapack_matrix_copy.load(filename.c_str()); + scalapack_matrix.save(filename); + scalapack_matrix_copy.load(filename); std::remove(filename.c_str()); AssertThrow(scalapack_matrix.get_property() == scalapack_matrix_copy.get_property(), ExcInternalError()); scalapack_matrix.set_property(LAPACKSupport::Property::symmetric); - scalapack_matrix.save(filename.c_str()); - scalapack_matrix_copy.load(filename.c_str()); + scalapack_matrix.save(filename); + scalapack_matrix_copy.load(filename); std::remove(filename.c_str()); AssertThrow(scalapack_matrix.get_property() == scalapack_matrix_copy.get_property(), ExcInternalError()); scalapack_matrix.set_property(LAPACKSupport::Property::upper_triangular); - scalapack_matrix.save(filename.c_str()); - scalapack_matrix_copy.load(filename.c_str()); + scalapack_matrix.save(filename); + scalapack_matrix_copy.load(filename); std::remove(filename.c_str()); AssertThrow(scalapack_matrix.get_property() == scalapack_matrix_copy.get_property(), ExcInternalError()); // after construction the matrix state is LAPACKSupport::State::unusable - scalapack_matrix.save(filename.c_str()); - scalapack_matrix_copy.load(filename.c_str()); + scalapack_matrix.save(filename); + scalapack_matrix_copy.load(filename); std::remove(filename.c_str()); AssertThrow(scalapack_matrix.get_state() == scalapack_matrix_copy.get_state(), ExcInternalError()); // the assignment operator changes the state to LAPACKSupport::State::matrix scalapack_matrix = full; - scalapack_matrix.save(filename.c_str()); - scalapack_matrix_copy.load(filename.c_str()); + scalapack_matrix.save(filename); + scalapack_matrix_copy.load(filename); std::remove(filename.c_str()); AssertThrow(scalapack_matrix.get_state() == scalapack_matrix_copy.get_state(), ExcInternalError()); @@ -128,8 +128,8 @@ test() // calling invert changes the state to LAPACKSupport::inverse_matrix scalapack_matrix.set_property(LAPACKSupport::Property::symmetric); scalapack_matrix.invert(); - scalapack_matrix.save(filename.c_str()); - scalapack_matrix_copy.load(filename.c_str()); + scalapack_matrix.save(filename); + scalapack_matrix_copy.load(filename); std::remove(filename.c_str()); AssertThrow(scalapack_matrix.get_state() == scalapack_matrix_copy.get_state(), ExcInternalError()); diff --git a/tests/scalapack/scalapack_10_d.cc b/tests/scalapack/scalapack_10_d.cc index 7d27c7efd3..7972225622 100644 --- a/tests/scalapack/scalapack_10_d.cc +++ b/tests/scalapack/scalapack_10_d.cc @@ -62,7 +62,7 @@ test(const unsigned int size, const unsigned int block_size) size, size, grid_1, block_size, block_size); scalapack_matrix_1 = full; - scalapack_matrix_1.save(filename.c_str()); + scalapack_matrix_1.save(filename); ScaLAPACKMatrix scalapack_matrix(filename, grid, -- 2.39.5