|
| 1 | +// async_gpu_launch.cpp — Async CPU+GPU optical photon simulation example |
| 2 | +// |
| 3 | +// Demonstrates double-buffered async GPU processing where the CPU event |
| 4 | +// loop continues while the GPU processes accumulated gensteps in batches. |
| 5 | +// |
| 6 | +// Usage: |
| 7 | +// async_gpu_launch -g apex.gdml -m run.mac [--async] [--sync] |
| 8 | +// |
| 9 | +// Default is --async. Use --sync for the original end-of-run GPU mode. |
| 10 | + |
| 11 | +#include <string> |
| 12 | + |
| 13 | +#include <argparse/argparse.hpp> |
| 14 | + |
| 15 | +#include "FTFP_BERT.hh" |
| 16 | +#include "G4OpticalPhysics.hh" |
| 17 | +#include "G4VModularPhysicsList.hh" |
| 18 | + |
| 19 | +#include "G4UIExecutive.hh" |
| 20 | +#include "G4UImanager.hh" |
| 21 | +#include "G4VisExecutive.hh" |
| 22 | + |
| 23 | +#include "async_gpu_launch.h" |
| 24 | +#include "config.h" |
| 25 | +#include "sysrap/OPTICKS_LOG.hh" |
| 26 | + |
| 27 | +#include "G4RunManager.hh" |
| 28 | +#include "G4RunManagerFactory.hh" |
| 29 | +#include "G4VUserActionInitialization.hh" |
| 30 | + |
| 31 | +using namespace std; |
| 32 | + |
| 33 | +struct ActionInitialization : public G4VUserActionInitialization |
| 34 | +{ |
| 35 | + G4App* fG4App; |
| 36 | + |
| 37 | + ActionInitialization(G4App* app) : |
| 38 | + G4VUserActionInitialization(), |
| 39 | + fG4App(app) |
| 40 | + { |
| 41 | + } |
| 42 | + |
| 43 | + void BuildForMaster() const override |
| 44 | + { |
| 45 | + SetUserAction(fG4App->run_act_); |
| 46 | + } |
| 47 | + |
| 48 | + void Build() const override |
| 49 | + { |
| 50 | + SetUserAction(fG4App->prim_gen_); |
| 51 | + SetUserAction(fG4App->run_act_); |
| 52 | + SetUserAction(fG4App->event_act_); |
| 53 | + SetUserAction(fG4App->tracking_); |
| 54 | + SetUserAction(fG4App->stepping_); |
| 55 | + } |
| 56 | +}; |
| 57 | + |
| 58 | +int main(int argc, char** argv) |
| 59 | +{ |
| 60 | + OPTICKS_LOG(argc, argv); |
| 61 | + |
| 62 | + argparse::ArgumentParser program("async_gpu_launch", "0.0.0"); |
| 63 | + |
| 64 | + string gdml_file, macro_name; |
| 65 | + bool interactive; |
| 66 | + |
| 67 | + program.add_argument("-g", "--gdml") |
| 68 | + .help("path to GDML file") |
| 69 | + .default_value(string("apex.gdml")) |
| 70 | + .nargs(1) |
| 71 | + .store_into(gdml_file); |
| 72 | + |
| 73 | + program.add_argument("-m", "--macro") |
| 74 | + .help("path to G4 macro") |
| 75 | + .default_value(string("run.mac")) |
| 76 | + .nargs(1) |
| 77 | + .store_into(macro_name); |
| 78 | + |
| 79 | + program.add_argument("-c", "--config") |
| 80 | + .help("config file name (without .json extension)") |
| 81 | + .default_value(string("")) |
| 82 | + .nargs(1); |
| 83 | + |
| 84 | + program.add_argument("-i", "--interactive").help("open interactive viewer").flag().store_into(interactive); |
| 85 | + |
| 86 | + program.add_argument("-s", "--seed").help("fixed random seed").scan<'i', long>(); |
| 87 | + |
| 88 | + program.add_argument("--async").help("use async double-buffered GPU processing (default)").flag(); |
| 89 | + |
| 90 | + program.add_argument("--sync").help("use synchronous end-of-run GPU processing").flag(); |
| 91 | + |
| 92 | + try |
| 93 | + { |
| 94 | + program.parse_args(argc, argv); |
| 95 | + } |
| 96 | + catch (const exception& err) |
| 97 | + { |
| 98 | + cerr << err.what() << endl; |
| 99 | + cerr << program; |
| 100 | + return EXIT_FAILURE; |
| 101 | + } |
| 102 | + |
| 103 | + // Seed |
| 104 | + long seed; |
| 105 | + if (program.is_used("--seed")) |
| 106 | + seed = program.get<long>("--seed"); |
| 107 | + else |
| 108 | + seed = static_cast<long>(time(nullptr)); |
| 109 | + CLHEP::HepRandom::setTheSeed(seed); |
| 110 | + G4cout << "Random seed: " << seed << G4endl; |
| 111 | + |
| 112 | + // Mode: async by default, sync if --sync is given |
| 113 | + bool enable_async = !program.get<bool>("--sync"); |
| 114 | + G4cout << "Mode: " << (enable_async ? "ASYNC" : "SYNC") << G4endl; |
| 115 | + |
| 116 | + // Physics |
| 117 | + G4VModularPhysicsList* physics = new FTFP_BERT; |
| 118 | + physics->RegisterPhysics(new G4OpticalPhysics); |
| 119 | + |
| 120 | + auto* run_mgr = G4RunManagerFactory::CreateRunManager(); |
| 121 | + run_mgr->SetUserInitialization(physics); |
| 122 | + |
| 123 | + // Application |
| 124 | + G4App* g4app = new G4App(gdml_file, enable_async); |
| 125 | + |
| 126 | + ActionInitialization* actionInit = new ActionInitialization(g4app); |
| 127 | + run_mgr->SetUserInitialization(actionInit); |
| 128 | + run_mgr->SetUserInitialization(g4app->det_cons_); |
| 129 | + |
| 130 | + // UI |
| 131 | + G4UIExecutive* uix = nullptr; |
| 132 | + G4VisManager* vis = nullptr; |
| 133 | + |
| 134 | + if (interactive) |
| 135 | + { |
| 136 | + uix = new G4UIExecutive(argc, argv); |
| 137 | + vis = new G4VisExecutive; |
| 138 | + vis->Initialize(); |
| 139 | + } |
| 140 | + |
| 141 | + G4UImanager* ui = G4UImanager::GetUIpointer(); |
| 142 | + ui->ApplyCommand("/control/execute " + macro_name); |
| 143 | + |
| 144 | + if (interactive) |
| 145 | + uix->SessionStart(); |
| 146 | + |
| 147 | + delete uix; |
| 148 | + return EXIT_SUCCESS; |
| 149 | +} |
0 commit comments