From 8d9297ccb8557cff592560c7992193228cb200ea Mon Sep 17 00:00:00 2001 From: maorz1998 <515754068@qq.com> Date: Sun, 6 Nov 2022 16:07:48 +0800 Subject: [PATCH 1/2] use reference instead of copy to do data conversion in pybind11 --- applications/solvers/dfLowMachFoam/YEqn.H | 7 ++++--- .../solvers/dfLowMachFoam/dfLowMachFoam.C | 10 ++++++++++ src/dfChemistryModel/dfChemistryModel.C | 6 +++--- src/dfChemistryModel/dfChemistryModel.H | 18 +++++++++++++++++- 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/applications/solvers/dfLowMachFoam/YEqn.H b/applications/solvers/dfLowMachFoam/YEqn.H index 1f01cbde..95796a4e 100644 --- a/applications/solvers/dfLowMachFoam/YEqn.H +++ b/applications/solvers/dfLowMachFoam/YEqn.H @@ -22,13 +22,14 @@ const surfaceScalarField phiUc = linearInterpolate(sumYDiffError) & mesh.Sf(); { if (!splitting) { - start = std::clock(); + std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); combustion->correct(); label flag_mpi_init; MPI_Initialized(&flag_mpi_init); if(flag_mpi_init) MPI_Barrier(PstreamGlobals::MPI_COMM_FOAM); - end = std::clock(); - time_monitor_chem += double(end - start) / double(CLOCKS_PER_SEC); + std::chrono::steady_clock::time_point stop = std::chrono::steady_clock::now(); + std::chrono::duration processingTime = std::chrono::duration_cast>(stop - start); + time_monitor_chem += processingTime.count(); } volScalarField Yt(0.0*Y[0]); diff --git a/applications/solvers/dfLowMachFoam/dfLowMachFoam.C b/applications/solvers/dfLowMachFoam/dfLowMachFoam.C index 480ef4a9..c2622a9c 100644 --- a/applications/solvers/dfLowMachFoam/dfLowMachFoam.C +++ b/applications/solvers/dfLowMachFoam/dfLowMachFoam.C @@ -192,6 +192,16 @@ int main(int argc, char *argv[]) Info<< "ExecutionTime = " << runTime.elapsedCpuTime() << " s" << " ClockTime = " << runTime.elapsedClockTime() << " s" + << " allsolveTime = " << chemistry->time_allsolve() << " s" + << " submasterTime = " << chemistry->time_submaster() << " s" << nl + << " sendProblemTime = " << chemistry->time_sendProblem() << " s" + << " recvProblemTime = " << chemistry->time_RecvProblem() << " s" + << " sendRecvSolutionTime = " << chemistry->time_sendRecvSolution() << " s" << nl + << " getDNNinputsTime = " << chemistry->time_getDNNinputs() << " s" + << " DNNinferenceTime = " << chemistry->time_DNNinference() << " s" + << " updateSolutionBufferTime = " << chemistry->time_updateSolutionBuffer() << " s" << nl + << " vec2ndarrayTime = " << chemistry->time_vec2ndarray() << " s" + << " pythonTime = " << chemistry->time_python() << " s" << nl << endl; } diff --git a/src/dfChemistryModel/dfChemistryModel.C b/src/dfChemistryModel/dfChemistryModel.C index 332c06a3..ff527e3e 100644 --- a/src/dfChemistryModel/dfChemistryModel.C +++ b/src/dfChemistryModel/dfChemistryModel.C @@ -1382,9 +1382,9 @@ Foam::scalar Foam::dfChemistryModel::torchDCUSolve( std::chrono::steady_clock::time_point start7 = std::chrono::steady_clock::now(); std::chrono::steady_clock::time_point start8 = std::chrono::steady_clock::now(); - pybind11::array_t vec0 = pybind11::cast(DNNinputs[0]); // cast vector to np.array - pybind11::array_t vec1 = pybind11::cast(DNNinputs[1]); - pybind11::array_t vec2 = pybind11::cast(DNNinputs[2]); + pybind11::array_t vec0 = pybind11::array_t({DNNinputs[0].size()}, {8}, &DNNinputs[0][0]); // cast vector to np.array + pybind11::array_t vec1 = pybind11::array_t({DNNinputs[1].size()}, {8}, &DNNinputs[1][0]); + pybind11::array_t vec2 = pybind11::array_t({DNNinputs[2].size()}, {8}, &DNNinputs[2][0]); std::chrono::steady_clock::time_point stop8 = std::chrono::steady_clock::now(); std::chrono::duration processingTime8 = std::chrono::duration_cast>(stop8 - start8); diff --git a/src/dfChemistryModel/dfChemistryModel.H b/src/dfChemistryModel/dfChemistryModel.H index 03196073..c4401473 100644 --- a/src/dfChemistryModel/dfChemistryModel.H +++ b/src/dfChemistryModel/dfChemistryModel.H @@ -45,7 +45,8 @@ SourceFiles #ifdef USE_PYTORCH #include #include -#include //used to convert +#include +#include #include "GpuProblem.H" #include "GpuSolution.H" #include "DynamicBuffer.H" @@ -293,6 +294,21 @@ public: ThermoType& thermo() {return thermo_;} const CanteraMixture& mixture() {return mixture_;} + + // profiling +#ifdef USE_PYTORCH + double time_allsolve() {return time_allsolve_;} + double time_submaster() {return time_submaster_;} + double time_sendProblem() {return time_sendProblem_;} + double time_RecvProblem() {return time_RecvProblem_;} + double time_sendRecvSolution() {return time_sendRecvSolution_;} + double time_getDNNinputs() {return time_getDNNinputs_;} + double time_DNNinference() {return time_DNNinference_;} + double time_updateSolutionBuffer() {return time_updateSolutionBuffer_;} + double time_vec2ndarray() {return time_vec2ndarray_;} + double time_python() {return time_python_;} +#endif + }; From 8c9ccf0f770a8240b1bf2b8bba0a822e17c68e73 Mon Sep 17 00:00:00 2001 From: maorz1998 <515754068@qq.com> Date: Sun, 6 Nov 2022 16:37:40 +0800 Subject: [PATCH 2/2] use reference instead of copy to do data conversion in pybind11 --- applications/solvers/dfLowMachFoam/dfLowMachFoam.C | 2 ++ 1 file changed, 2 insertions(+) diff --git a/applications/solvers/dfLowMachFoam/dfLowMachFoam.C b/applications/solvers/dfLowMachFoam/dfLowMachFoam.C index c2622a9c..ae92adc1 100644 --- a/applications/solvers/dfLowMachFoam/dfLowMachFoam.C +++ b/applications/solvers/dfLowMachFoam/dfLowMachFoam.C @@ -192,6 +192,7 @@ int main(int argc, char *argv[]) Info<< "ExecutionTime = " << runTime.elapsedCpuTime() << " s" << " ClockTime = " << runTime.elapsedClockTime() << " s" + #ifdef USE_PYTORCH << " allsolveTime = " << chemistry->time_allsolve() << " s" << " submasterTime = " << chemistry->time_submaster() << " s" << nl << " sendProblemTime = " << chemistry->time_sendProblem() << " s" @@ -202,6 +203,7 @@ int main(int argc, char *argv[]) << " updateSolutionBufferTime = " << chemistry->time_updateSolutionBuffer() << " s" << nl << " vec2ndarrayTime = " << chemistry->time_vec2ndarray() << " s" << " pythonTime = " << chemistry->time_python() << " s" + #endif << nl << endl; }