Skip to content

Commit

Permalink
temporary fix #73 by copying to bu
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas-Ulrich committed Jul 17, 2024
1 parent a612ab7 commit 9875761
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
48 changes: 48 additions & 0 deletions app/form/SeasQDDiscreteGreenOperator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,52 @@ PetscInt SeasQDDiscreteGreenOperator::create_discrete_greens_function() {
return n_gf;
}



void SeasQDDiscreteGreenOperator::back_up_file(std::string file_to_backup) {
std::string backup_filename = file_to_backup + ".bak";
int rank;
int commsize;
MPI_Comm_rank(base::comm(), &rank);
MPI_Comm_size(base::comm(), &commsize);

// Open the source file
MPI_File src_file;
if (MPI_File_open(base::comm(), file_to_backup.c_str(), MPI_MODE_RDONLY, MPI_INFO_NULL, &src_file) != MPI_SUCCESS) {
if (rank == 0) PetscPrintf(base::comm(), "No existing file to backup or unable to open source file.\n");
} else {
// Open the destination file
MPI_File dst_file;
if (MPI_File_open(base::comm(), backup_filename.c_str(), MPI_MODE_CREATE | MPI_MODE_WRONLY, MPI_INFO_NULL, &dst_file) != MPI_SUCCESS) {
if (rank == 0) PetscPrintf(base::comm(), "Failed to create backup file.\n");
MPI_File_close(&src_file);
} else {
// Get the file size
MPI_Offset filesize;
MPI_File_get_size(src_file, &filesize);

// Calculate chunk size for each process
MPI_Offset chunk_size = (filesize + commsize - 1) / commsize;
MPI_Offset start = rank * chunk_size;
MPI_Offset end = (rank == commsize - 1) ? filesize : (start + chunk_size);
MPI_Offset my_chunk_size = end - start;

// Allocate buffer and read data
char* buffer = new char[my_chunk_size];
MPI_File_read_at(src_file, start, buffer, my_chunk_size, MPI_BYTE, MPI_STATUS_IGNORE);

// Write data to backup file
MPI_File_write_at(dst_file, start, buffer, my_chunk_size, MPI_BYTE, MPI_STATUS_IGNORE);

delete[] buffer;

MPI_File_close(&dst_file);
MPI_File_close(&src_file);
}
}
}


void SeasQDDiscreteGreenOperator::write_discrete_greens_operator(
LocalSimplexMesh<DomainDimension> const& mesh, PetscInt current_gf, PetscInt n_gf) {
PetscViewer v;
Expand All @@ -263,6 +309,7 @@ void SeasQDDiscreteGreenOperator::write_discrete_greens_operator(
MPI_Comm_size(base::comm(), &commsize);

CHKERRTHROW(PetscTime(&t0));
back_up_file(gf_operator_filename_);
CHKERRTHROW(PetscViewerBinaryOpen(PetscObjectComm((PetscObject)G_),
gf_operator_filename_.c_str(), FILE_MODE_WRITE, &v));
CHKERRTHROW(PetscViewerBinarySetUseMPIIO(v, PETSC_TRUE));
Expand Down Expand Up @@ -313,6 +360,7 @@ void SeasQDDiscreteGreenOperator ::write_facet_labels_IS(
(const PetscInt*)idx_, PETSC_USE_POINTER, &is));

CHKERRTHROW(PetscTime(&t0));
back_up_file(gf_facet_filename_);
CHKERRTHROW(PetscViewerBinaryOpen(PetscObjectComm((PetscObject)G_), gf_facet_filename_.c_str(),
FILE_MODE_WRITE, &v));
CHKERRTHROW(PetscViewerBinarySetUseMPIIO(v, PETSC_TRUE));
Expand Down
1 change: 1 addition & 0 deletions app/form/SeasQDDiscreteGreenOperator.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class SeasQDDiscreteGreenOperator : public SeasQDOperator {
PetscInt n_gf_);
// all logic associated with matix craetion, loading / partial assembly is done here
void get_discrete_greens_function(LocalSimplexMesh<DomainDimension> const& mesh);
void back_up_file(std::string file_to_backup);
void write_discrete_greens_traction();
void load_discrete_greens_traction();
void get_boundary_traction();
Expand Down

0 comments on commit 9875761

Please sign in to comment.