Skip to content

Commit

Permalink
Merge pull request #456 from deepmodeling/dangglxxx-patch-1
Browse files Browse the repository at this point in the history
Use smart pointers to manage memory and prevent memory leaks
  • Loading branch information
dangglxxx authored Mar 13, 2024
2 parents 89979ee + 16a222f commit 4810206
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 15 deletions.
5 changes: 1 addition & 4 deletions applications/solvers/dfLowMachFoam/YEqn.H
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,6 @@ if(flag_mpi_init) MPI_Barrier(PstreamGlobals::MPI_COMM_FOAM);
#ifdef ODE_GPU_SOLVER
scalar dt = runTime.deltaTValue();

memcpy(h_T, &T[0], num_cells * sizeof(double));
memcpy(h_p, &p[0], num_cells * sizeof(double));

forAll(Y, speciesI) {
volScalarField& Yi = Y[speciesI];
memcpy(h_y + speciesI * num_cells, &Yi[0], num_cells * sizeof(double));
Expand All @@ -301,7 +298,7 @@ if(flag_mpi_init) MPI_Barrier(PstreamGlobals::MPI_COMM_FOAM);
}
}

opencc_ode_all(h_T, h_p, h_y_t, 1e-10, dt, CPU);
opencc_ode_all(&T[0], &p[0], h_y_t, 1e-10, dt, CPU);

for (int i = 0; i < num_cells; i++) {
for (int j = 0; j < sp_num; j++) {
Expand Down
14 changes: 3 additions & 11 deletions applications/solvers/dfLowMachFoam/createFields_GPU.H
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,20 @@ forAll(RRGPU, fieldi)
int num_cells = T.size();
int num_species = Y.size();

double* h_T = new double[num_cells];
double* h_p = new double[num_cells];
double* h_y = new double[num_cells * num_species];
double* h_y_t = new double[num_cells * num_species];
int* h_size = new int[1];

memcpy(h_T, &T[0], num_cells * sizeof(double));
memcpy(h_p, &p[0], num_cells * sizeof(double));
std::unique_ptr<double[]> unique_y(new double[num_cells * num_species]); double* h_y = unique_y.get();
std::unique_ptr<double[]> unique_y_t(new double[num_cells * num_species]); double* h_y_t = unique_y_t.get();

forAll(Y, speciesI) {
volScalarField& Yi = Y[speciesI];
memcpy(h_y + speciesI * num_cells, &Yi[0], num_cells * sizeof(double));
}

h_size[0] = num_cells;

int sp_num = num_species;

string mechanismFile = CanteraTorchProperties.lookupOrDefault("CanteraMechanismFile", string(""));
char target_mechanismFile[100];
std::strcpy(target_mechanismFile, mechanismFile.c_str());

opencc_ode_init(target_mechanismFile, num_cells, h_T, h_p, h_y);
opencc_ode_init(target_mechanismFile, num_cells, &T[0], &p[0], h_y);

double* Ynew = new double[num_cells * num_species];

0 comments on commit 4810206

Please sign in to comment.