Hello everyone,
I am using Garfield++ to compute gas properties using this example as baseline.
I have tried to paralelize it using threads but this does not seem to work, possibly because of some global variables as stated in this thread.
This is the code I am using:
// main.cpp
#include <iostream>
#include <thread>
#include "Garfield/FundamentalConstants.hh"
#include "Garfield/MediumMagboltz.hh"
using namespace std;
using namespace Garfield;
int main(int argc, char** argv) {
const int nThreads = 2;
std::vector<thread> threads;
for (int i = 0; i < nThreads; i++) {
threads.emplace_back([](unsigned int threadId) {
cout << "Thread " << threadId << endl;
MediumMagboltz gas("Ar", 93., "CO2", 7.);
const double pressure = 3 * AtmosphericPressure;
const double temperature = 293.15;
gas.SetTemperature(temperature);
gas.SetPressure(pressure);
// one would use different e-field values for each thread and then merge the results
gas.SetFieldGrid({1000.}, {0.0}, {HalfPi});
gas.GenerateGasTable(1);
gas.WriteGasFile("demo" + to_string(threadId) + ".gas");
},
i);
}
for (auto& t: threads) {
t.join();
}
return 0;
}
Which can be compiled with the following cmake:
// CMakeLists.txt
find_package(Garfield REQUIRED)
add_executable(demo main.cpp)
target_link_libraries(demo PUBLIC Garfield::Garfield)
My question is: can this paralelization be achieved some other way? I have also had problems when running multiple single threaded jobs on a cluster to achieve the same effect, some jobs were getting stuck, as if there was some global state accross the different processes… this seemed very wierd to me so perhaps I was doing something wrong.
Anyway, I could only get the paralelization to work running the jobs as individual docker containers. I would appreciate any advice on this.
Thanks!
Luis