benchmark_multiphase.cpp 15.6 KB
Newer Older
Markus Holzer's avatar
Markus Holzer committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//======================================================================================================================
//
//  This file is part of waLBerla. waLBerla is free software: you can
//  redistribute it and/or modify it under the terms of the GNU General Public
//  License as published by the Free Software Foundation, either version 3 of
//  the License, or (at your option) any later version.
//
//  waLBerla is distributed in the hope that it will be useful, but WITHOUT
//  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
//  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
//  for more details.
//
//  You should have received a copy of the GNU General Public License along
//  with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>.
//
//! \file benchmark_multiphase.cpp
//! \author Markus Holzer <markus.holzer@fau.de>
//
//======================================================================================================================
#include "blockforest/Initialization.h"
#include "blockforest/communication/UniformDirectScheme.h"

#include "core/Environment.h"
#include "core/logging/Initialization.h"
#include "core/math/Constants.h"
#include "core/timing/RemainingTimeLogger.h"

#include "field/AddToStorage.h"
#include "field/FlagField.h"
#include "field/vtk/VTKWriter.h"

#include "geometry/InitBoundaryHandling.h"

#include "python_coupling/CreateConfig.h"
#include "python_coupling/DictWrapper.h"
#include "python_coupling/PythonCallback.h"

#include "timeloop/SweepTimeloop.h"

#include "InitializerFunctions.h"

//////////////////////////////
// INCLUDE GENERATED FILES //
////////////////////////////

#if defined(WALBERLA_BUILD_WITH_CUDA)
#   include "cuda/AddGPUFieldToStorage.h"
#   include "cuda/DeviceSelectMPI.h"
#   include "cuda/ParallelStreams.h"
#   include "cuda/communication/MemcpyPackInfo.h"
#   include "cuda/communication/UniformGPUScheme.h"
#else
Markus Holzer's avatar
Markus Holzer committed
53
#   include <blockforest/communication/UniformBufferedScheme.h>
Markus Holzer's avatar
Markus Holzer committed
54
55
#endif

Markus Holzer's avatar
Markus Holzer committed
56
57
#include "GenDefines.h"

Markus Holzer's avatar
Markus Holzer committed
58
59
using namespace walberla;

Markus Holzer's avatar
Markus Holzer committed
60
61
using flag_t      = walberla::uint8_t;
using FlagField_T = FlagField< flag_t >;
Markus Holzer's avatar
Markus Holzer committed
62
63

#if defined(WALBERLA_BUILD_WITH_CUDA)
Markus Holzer's avatar
Markus Holzer committed
64
typedef cuda::GPUField< real_t > GPUField;
Markus Holzer's avatar
Markus Holzer committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#endif

int main(int argc, char** argv)
{
   mpi::Environment env(argc, argv);
#if defined(WALBERLA_BUILD_WITH_CUDA)
   cuda::selectDeviceBasedOnMpiRank();
#endif

   for (auto cfg = python_coupling::configBegin(argc, argv); cfg != python_coupling::configEnd(); ++cfg)
   {
      WALBERLA_MPI_WORLD_BARRIER()

      auto config = *cfg;
      logging::configureLogging(config);
      shared_ptr< StructuredBlockForest > blocks = blockforest::createUniformBlockGridFromConfig(config);

      Vector3< uint_t > cellsPerBlock =
         config->getBlock("DomainSetup").getParameter< Vector3< uint_t > >("cellsPerBlock");
      // Reading parameters
      auto parameters                    = config->getOneBlock("Parameters");
      const std::string timeStepStrategy = parameters.getParameter< std::string >("timeStepStrategy", "normal");
      const uint_t timesteps             = parameters.getParameter< uint_t >("timesteps", uint_c(50));
      const real_t remainingTimeLoggerFrequency =
         parameters.getParameter< real_t >("remainingTimeLoggerFrequency", 3.0);
      const uint_t scenario = parameters.getParameter< uint_t >("scenario", uint_c(1));
Markus Holzer's avatar
Markus Holzer committed
91
      const uint_t warmupSteps  = parameters.getParameter< uint_t >("warmupSteps", uint_t(2));
Markus Holzer's avatar
Markus Holzer committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142

#if defined(WALBERLA_BUILD_WITH_CUDA)
      // CPU fields
      BlockDataID vel_field   = field::addToStorage< VelocityField_T >(blocks, "vel", real_t(0), field::fzyx);
      BlockDataID phase_field = field::addToStorage< PhaseField_T >(blocks, "phase", real_t(0), field::fzyx);
      // GPU fields
      BlockDataID lb_phase_field_gpu = cuda::addGPUFieldToStorage< cuda::GPUField< real_t > >(
         blocks, "lb phase field on GPU", Stencil_phase_T::Size, field::fzyx, 1);
      BlockDataID lb_velocity_field_gpu = cuda::addGPUFieldToStorage< cuda::GPUField< real_t > >(
         blocks, "lb velocity field on GPU", Stencil_hydro_T::Size, field::fzyx, 1);
      BlockDataID vel_field_gpu =
         cuda::addGPUFieldToStorage< VelocityField_T >(blocks, vel_field, "velocity field on GPU", true);
      BlockDataID phase_field_gpu =
         cuda::addGPUFieldToStorage< PhaseField_T >(blocks, phase_field, "phase field on GPU", true);
#else
      BlockDataID lb_phase_field =
         field::addToStorage< PdfField_phase_T >(blocks, "lb phase field", real_t(0), field::fzyx);
      BlockDataID lb_velocity_field =
         field::addToStorage< PdfField_hydro_T >(blocks, "lb velocity field", real_t(0), field::fzyx);
      BlockDataID vel_field   = field::addToStorage< VelocityField_T >(blocks, "vel", real_t(0), field::fzyx);
      BlockDataID phase_field = field::addToStorage< PhaseField_T >(blocks, "phase", real_t(0), field::fzyx);
#endif

      if (timeStepStrategy != "phase_only" && timeStepStrategy != "hydro_only" && timeStepStrategy != "kernel_only")
      {
         WALBERLA_LOG_INFO_ON_ROOT("initialization of the phase field")
         if (scenario == 1)
         {
            auto bubbleParameters = config->getOneBlock("Bubble");
            const Vector3< real_t > bubbleMidPoint =
               bubbleParameters.getParameter< Vector3< real_t > >("bubbleMidPoint");
            const real_t bubbleRadius = bubbleParameters.getParameter< real_t >("bubbleRadius", 20.0);
            initPhaseField_bubble(blocks, phase_field, bubbleRadius, bubbleMidPoint);
         }
         else if (scenario == 2)
         {
            initPhaseField_RTI(blocks, phase_field);
         }
#if defined(WALBERLA_BUILD_WITH_CUDA)
         cuda::fieldCpy< GPUField, PhaseField_T >(blocks, phase_field_gpu, phase_field);
#endif
         WALBERLA_LOG_INFO_ON_ROOT("initialization of the phase field done")
      }

#if defined(WALBERLA_BUILD_WITH_CUDA)
      Vector3< int32_t > gpuBlockSize =
         parameters.getParameter< Vector3< int32_t > >("gpuBlockSize", Vector3< int32_t >(256, 1, 1));
      pystencils::initialize_phase_field_distributions init_h(lb_phase_field_gpu, phase_field_gpu, vel_field_gpu);
      pystencils::initialize_velocity_based_distributions init_g(lb_velocity_field_gpu, vel_field_gpu);

      pystencils::phase_field_LB_step phase_field_LB_step(
Markus Holzer's avatar
Markus Holzer committed
143
         lb_phase_field_gpu, phase_field_gpu, vel_field_gpu, gpuBlockSize[0], gpuBlockSize[1], gpuBlockSize[2]);
Markus Holzer's avatar
Markus Holzer committed
144
      pystencils::hydro_LB_step hydro_LB_step(lb_velocity_field_gpu, phase_field_gpu, vel_field_gpu, gpuBlockSize[0],
Markus Holzer's avatar
Markus Holzer committed
145
                                              gpuBlockSize[1], gpuBlockSize[2]);
Markus Holzer's avatar
Markus Holzer committed
146
147
148
#else
      pystencils::initialize_phase_field_distributions init_h(lb_phase_field, phase_field, vel_field);
      pystencils::initialize_velocity_based_distributions init_g(lb_velocity_field, vel_field);
Markus Holzer's avatar
Markus Holzer committed
149
150
      pystencils::phase_field_LB_step phase_field_LB_step(lb_phase_field, phase_field, vel_field);
      pystencils::hydro_LB_step hydro_LB_step(lb_velocity_field, phase_field, vel_field);
Markus Holzer's avatar
Markus Holzer committed
151
152
153
154
#endif

// add communication
#if defined(WALBERLA_BUILD_WITH_CUDA)
Markus Holzer's avatar
Markus Holzer committed
155
      const bool cudaEnabledMpi = parameters.getParameter< bool >("cudaEnabledMpi", false);
Markus Holzer's avatar
Markus Holzer committed
156
      auto Comm_velocity_based_distributions =
Markus Holzer's avatar
Markus Holzer committed
157
         make_shared< cuda::communication::UniformGPUScheme< Stencil_hydro_T > >(blocks, cudaEnabledMpi);
Markus Holzer's avatar
Markus Holzer committed
158
      auto generatedPackInfo_velocity_based_distributions =
Markus Holzer's avatar
Markus Holzer committed
159
         make_shared< lbm::PackInfo_velocity_based_distributions >(lb_velocity_field_gpu);
Markus Holzer's avatar
Markus Holzer committed
160
161
162
163
164
      Comm_velocity_based_distributions->addPackInfo(generatedPackInfo_velocity_based_distributions);
      auto generatedPackInfo_phase_field = make_shared< pystencils::PackInfo_phase_field >(phase_field_gpu);
      Comm_velocity_based_distributions->addPackInfo(generatedPackInfo_phase_field);

      auto Comm_phase_field_distributions =
Markus Holzer's avatar
Markus Holzer committed
165
         make_shared< cuda::communication::UniformGPUScheme< Stencil_hydro_T > >(blocks, cudaEnabledMpi);
Markus Holzer's avatar
Markus Holzer committed
166
      auto generatedPackInfo_phase_field_distributions =
Markus Holzer's avatar
Markus Holzer committed
167
         make_shared< lbm::PackInfo_phase_field_distributions >(lb_phase_field_gpu);
Markus Holzer's avatar
Markus Holzer committed
168
169
170
171
172
173
174
      Comm_phase_field_distributions->addPackInfo(generatedPackInfo_phase_field_distributions);
#else

      blockforest::communication::UniformBufferedScheme< Stencil_hydro_T > Comm_velocity_based_distributions(blocks);

      auto generatedPackInfo_phase_field = make_shared< pystencils::PackInfo_phase_field >(phase_field);
      auto generatedPackInfo_velocity_based_distributions =
Markus Holzer's avatar
Markus Holzer committed
175
         make_shared< lbm::PackInfo_velocity_based_distributions >(lb_velocity_field);
Markus Holzer's avatar
Markus Holzer committed
176
177
178
179
180
181

      Comm_velocity_based_distributions.addPackInfo(generatedPackInfo_phase_field);
      Comm_velocity_based_distributions.addPackInfo(generatedPackInfo_velocity_based_distributions);

      blockforest::communication::UniformBufferedScheme< Stencil_hydro_T > Comm_phase_field_distributions(blocks);
      auto generatedPackInfo_phase_field_distributions =
Markus Holzer's avatar
Markus Holzer committed
182
         make_shared< lbm::PackInfo_phase_field_distributions >(lb_phase_field);
Markus Holzer's avatar
Markus Holzer committed
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
      Comm_phase_field_distributions.addPackInfo(generatedPackInfo_phase_field_distributions);
#endif

      BlockDataID flagFieldID = field::addFlagFieldToStorage< FlagField_T >(blocks, "flag field");
      // Boundaries
      const FlagUID fluidFlagUID("Fluid");
      auto boundariesConfig = config->getBlock("Boundaries_GPU");
      if (boundariesConfig)
      {
         geometry::initBoundaryHandling< FlagField_T >(*blocks, flagFieldID, boundariesConfig);
         geometry::setNonBoundaryCellsToDomain< FlagField_T >(*blocks, flagFieldID, fluidFlagUID);
      }

      // initialize the two lattice Boltzmann fields
      if (timeStepStrategy != "phase_only" && timeStepStrategy != "hydro_only" && timeStepStrategy != "kernel_only")
      {
         WALBERLA_LOG_INFO_ON_ROOT("initialization of the distributions")
         for (auto& block : *blocks)
         {
            init_h(&block);
            init_g(&block);
         }
         WALBERLA_LOG_INFO_ON_ROOT("initialization of the distributions done")
      }

#if defined(WALBERLA_BUILD_WITH_CUDA)
      int streamLowPriority  = 0;
      int streamHighPriority = 0;
      auto defaultStream     = cuda::StreamRAII::newPriorityStream(streamLowPriority);
      auto innerOuterStreams = cuda::ParallelStreams(streamHighPriority);
#endif

      auto timeLoop = make_shared< SweepTimeloop >(blocks->getBlockStorage(), timesteps);
#if defined(WALBERLA_BUILD_WITH_CUDA)
      auto normalTimeStep = [&]() {
Markus Holzer's avatar
Markus Holzer committed
218
         Comm_velocity_based_distributions->startCommunication(defaultStream);
Markus Holzer's avatar
Markus Holzer committed
219
         for (auto& block : *blocks)
Markus Holzer's avatar
Markus Holzer committed
220
221
            phase_field_LB_step(&block, defaultStream);
         Comm_velocity_based_distributions->wait(defaultStream);
Markus Holzer's avatar
Markus Holzer committed
222
223
224

         Comm_phase_field_distributions->startCommunication(defaultStream);
         for (auto& block : *blocks)
Markus Holzer's avatar
Markus Holzer committed
225
            hydro_LB_step(&block, defaultStream);
Markus Holzer's avatar
Markus Holzer committed
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
         Comm_phase_field_distributions->wait(defaultStream);
      };
      auto phase_only = [&]() {
         for (auto& block : *blocks)
            phase_field_LB_step(&block);
      };
      auto hydro_only = [&]() {
         for (auto& block : *blocks)
            hydro_LB_step(&block);
      };
      auto without_comm = [&]() {
         for (auto& block : *blocks)
            phase_field_LB_step(&block);
         for (auto& block : *blocks)
            hydro_LB_step(&block);
      };
#else
      auto normalTimeStep = [&]() {
Markus Holzer's avatar
Markus Holzer committed
244
245
246
247
248
249
250
251
252
            Comm_velocity_based_distributions.startCommunication();
            for (auto& block : *blocks)
               phase_field_LB_step(&block);
            Comm_velocity_based_distributions.wait();

            Comm_phase_field_distributions.startCommunication();
            for (auto& block : *blocks)
               hydro_LB_step(&block);
            Comm_phase_field_distributions.wait();
Markus Holzer's avatar
Markus Holzer committed
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
      };
      auto phase_only = [&]() {
         for (auto& block : *blocks)
            phase_field_LB_step(&block);
      };
      auto hydro_only = [&]() {
         for (auto& block : *blocks)
            hydro_LB_step(&block);
      };
      auto without_comm = [&]() {
         for (auto& block : *blocks)
            phase_field_LB_step(&block);
         for (auto& block : *blocks)
            hydro_LB_step(&block);
      };
#endif
      std::function< void() > timeStep;
Markus Holzer's avatar
Markus Holzer committed
270
      if (timeStepStrategy == "phase_only")
Markus Holzer's avatar
Markus Holzer committed
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
      {
         timeStep = std::function< void() >(phase_only);
         WALBERLA_LOG_INFO_ON_ROOT("started only phasefield step without communication for benchmarking")
      }
      else if (timeStepStrategy == "hydro_only")
      {
         timeStep = std::function< void() >(hydro_only);
         WALBERLA_LOG_INFO_ON_ROOT("started only hydro step without communication for benchmarking")
      }
      else if (timeStepStrategy == "kernel_only")
      {
         timeStep = std::function< void() >(without_comm);
         WALBERLA_LOG_INFO_ON_ROOT("started complete phasefield model without communication for benchmarking")
      }
      else
      {
         timeStep = std::function< void() >(normalTimeStep);
Markus Holzer's avatar
Markus Holzer committed
288
         WALBERLA_LOG_INFO_ON_ROOT("normal timestep with overlapping")
Markus Holzer's avatar
Markus Holzer committed
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
      }

      timeLoop->add() << BeforeFunction(timeStep) << Sweep([](IBlock*) {}, "time step");

      // remaining time logger
      if (remainingTimeLoggerFrequency > 0)
         timeLoop->addFuncAfterTimeStep(
            timing::RemainingTimeLogger(timeLoop->getNrOfTimeSteps(), remainingTimeLoggerFrequency),
            "remaining time logger");

      uint_t vtkWriteFrequency = parameters.getParameter< uint_t >("vtkWriteFrequency", 0);
      if (vtkWriteFrequency > 1)
      {
         auto vtkOutput = vtk::createVTKOutput_BlockData(*blocks, "vtk", vtkWriteFrequency, 0, false, "vtk_out",
                                                         "simulation_step", false, true, true, false, 0);
#if defined(WALBERLA_BUILD_WITH_CUDA)
Markus Holzer's avatar
Markus Holzer committed
305
306
         vtkOutput->addBeforeFunction(
            [&]() { cuda::fieldCpy< PhaseField_T, GPUField >(blocks, phase_field, phase_field_gpu); });
Markus Holzer's avatar
Markus Holzer committed
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#endif
         auto phaseWriter = make_shared< field::VTKWriter< PhaseField_T > >(phase_field, "phase");
         vtkOutput->addCellDataWriter(phaseWriter);

         timeLoop->addFuncBeforeTimeStep(vtk::writeFiles(vtkOutput), "VTK Output");
      }

      for (uint_t i = 0; i < warmupSteps; ++i)
         timeLoop->singleStep();

      timeLoop->setCurrentTimeStepToZero();
      WALBERLA_LOG_INFO_ON_ROOT("Starting simulation with " << timesteps << " time steps")
      WcTimer simTimer;
#if defined(WALBERLA_BUILD_WITH_CUDA)
      cudaDeviceSynchronize();
#endif
      simTimer.start();
      timeLoop->run();
#if defined(WALBERLA_BUILD_WITH_CUDA)
      cudaDeviceSynchronize();
#endif
      simTimer.end();
      WALBERLA_LOG_INFO_ON_ROOT("Simulation finished")
      auto time            = simTimer.last();
      auto nrOfCells       = real_c(cellsPerBlock[0] * cellsPerBlock[1] * cellsPerBlock[2]);
      auto mlupsPerProcess = nrOfCells * real_c(timesteps) / time * 1e-6;
      WALBERLA_LOG_RESULT_ON_ROOT("MLUPS per process: " << mlupsPerProcess)
      WALBERLA_LOG_RESULT_ON_ROOT("Time per time step: " << time / real_c(timesteps) << " s")
      WALBERLA_ROOT_SECTION()
      {
         python_coupling::PythonCallback pythonCallbackResults("results_callback");
         if (pythonCallbackResults.isCallable())
         {
            pythonCallbackResults.data().exposeValue("mlupsPerProcess", mlupsPerProcess);
Markus Holzer's avatar
Markus Holzer committed
341
342
343
344
345
            pythonCallbackResults.data().exposeValue("stencil_phase", StencilNamePhase);
            pythonCallbackResults.data().exposeValue("stencil_hydro", StencilNameHydro);
            #if defined(WALBERLA_BUILD_WITH_CUDA)
               pythonCallbackResults.data().exposeValue("cuda_enabled_mpi", cudaEnabledMpi);
            #endif
Markus Holzer's avatar
Markus Holzer committed
346
347
348
349
350
351
352
353
            // Call Python function to report results
            pythonCallbackResults();
         }
      }
   }

   return EXIT_SUCCESS;
}