From 7afa87dc7592bbc6badb00f74808b4c47ab2230a Mon Sep 17 00:00:00 2001 From: Lynn Dang <52342620+dangglxxx@users.noreply.github.com> Date: Fri, 1 Mar 2024 22:10:08 +0800 Subject: [PATCH 1/3] Use smart pointers to manage memory and prevent memory leaks --- applications/solvers/dfLowMachFoam/createFields_GPU.H | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/applications/solvers/dfLowMachFoam/createFields_GPU.H b/applications/solvers/dfLowMachFoam/createFields_GPU.H index fc115f80..aa739631 100755 --- a/applications/solvers/dfLowMachFoam/createFields_GPU.H +++ b/applications/solvers/dfLowMachFoam/createFields_GPU.H @@ -44,11 +44,10 @@ 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]; +std::unique_ptr unique_T(new double[num_cells]); double* h_T = unique_T.get(); +std::unique_ptr unique_p(new double[num_cells]); double* h_p = unique_p.get(); +std::unique_ptr unique_y(new double[num_cells * num_species]); double* h_y = unique_y.get(); +std::unique_ptr unique_y_t(new double[num_cells * num_species]); double* h_y_t = unique_y_t.get(); memcpy(h_T, &T[0], num_cells * sizeof(double)); memcpy(h_p, &p[0], num_cells * sizeof(double)); @@ -58,8 +57,6 @@ forAll(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("")); From bec8fe44efcd1619cc3081cfa26ec0b4a1069375 Mon Sep 17 00:00:00 2001 From: Lynn Dang <52342620+dangglxxx@users.noreply.github.com> Date: Sat, 2 Mar 2024 10:14:10 +0800 Subject: [PATCH 2/3] Update createFields_GPU.H --- applications/solvers/dfLowMachFoam/createFields_GPU.H | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/applications/solvers/dfLowMachFoam/createFields_GPU.H b/applications/solvers/dfLowMachFoam/createFields_GPU.H index aa739631..78058253 100755 --- a/applications/solvers/dfLowMachFoam/createFields_GPU.H +++ b/applications/solvers/dfLowMachFoam/createFields_GPU.H @@ -44,14 +44,9 @@ forAll(RRGPU, fieldi) int num_cells = T.size(); int num_species = Y.size(); -std::unique_ptr unique_T(new double[num_cells]); double* h_T = unique_T.get(); -std::unique_ptr unique_p(new double[num_cells]); double* h_p = unique_p.get(); std::unique_ptr unique_y(new double[num_cells * num_species]); double* h_y = unique_y.get(); std::unique_ptr unique_y_t(new double[num_cells * num_species]); double* h_y_t = unique_y_t.get(); -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)); @@ -63,6 +58,6 @@ string mechanismFile = CanteraTorchProperties.lookupOrDefault("CanteraMechanismF 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]; From 16a222fcc4f4d8c489bd5bdeef6c1bfd562beb14 Mon Sep 17 00:00:00 2001 From: Lynn Dang <52342620+dangglxxx@users.noreply.github.com> Date: Sat, 2 Mar 2024 10:16:34 +0800 Subject: [PATCH 3/3] Update YEqn.H --- applications/solvers/dfLowMachFoam/YEqn.H | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/applications/solvers/dfLowMachFoam/YEqn.H b/applications/solvers/dfLowMachFoam/YEqn.H index 01dd085b..7e16f4ec 100644 --- a/applications/solvers/dfLowMachFoam/YEqn.H +++ b/applications/solvers/dfLowMachFoam/YEqn.H @@ -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)); @@ -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++) {