# Garfield gas generator using multiple threads

**URL:** <https://root-forum.cern.ch/t/garfield-gas-generator-using-multiple-threads/52514>\
**Category:** Garfield++\
**Created:** [November 25, 2022, 3:23pm UTC](https://root-forum.cern.ch/t/garfield-gas-generator-using-multiple-threads/52514 "2022-11-25T15:23:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lobis](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/lobis/32/25455_2.png) [@lobis](https://root-forum.cern.ch/u/lobis)\
**Post date:** [November 25, 2022, 3:23pm UTC](https://root-forum.cern.ch/t/garfield-gas-generator-using-multiple-threads/52514/1 "2022-11-25T15:23:59Z")

</div>

Hello everyone,

I am using Garfield++ to compute gas properties using [this example](https://gitlab.cern.ch/garfield/garfieldpp/-/blob/master/Examples/GasFile/generate.C) 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](https://root-forum.cern.ch/t/is-it-possible-to-parallelize-garfield/41641/10).

This is the code I am using:

```auto
// 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:

```auto
// 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

---

<div class="post-metadata">

**Author:** ![jalopezg](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jalopezg/32/25844_2.png) [@jalopezg](https://root-forum.cern.ch/u/jalopezg)\
**Post date:** [November 25, 2022, 9:33pm UTC](https://root-forum.cern.ch/t/garfield-gas-generator-using-multiple-threads/52514/2 "2022-11-25T21:33:54Z")

</div>

Hi @lobis; I am sure @hschindl can help you with that.

Cheers,  
J.

---

<div class="post-metadata">

**Author:** ![hschindl](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/977dab/32.png) [@hschindl](https://root-forum.cern.ch/u/hschindl)\
**Post date:** [November 26, 2022, 10:23am UTC](https://root-forum.cern.ch/t/garfield-gas-generator-using-multiple-threads/52514/4 "2022-11-26T10:23:35Z")

</div>

Hi,  
the function `GenerateGasTable` (which is essentially an interface to calling the Fortran Magboltz program) is indeed not thread-safe.

Running multiple single-threaded jobs should work though. Maybe check if the amount of memory you request in the job submission file is sufficient.

---

<div class="post-metadata">

**Author:** ![lobis](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/lobis/32/25455_2.png) [@lobis](https://root-forum.cern.ch/u/lobis)\
**Post date:** [December 2, 2022, 5:26pm UTC](https://root-forum.cern.ch/t/garfield-gas-generator-using-multiple-threads/52514/5 "2022-12-02T17:26:08Z")

</div>

Thanks! I reviewed my code / job submissions and found the problem to be a low amount of jobs failing randomly, some of them due to [this issue](https://root-forum.cern.ch/t/warning-energy-out-of-range-increase-electron-energy-integration-range/42823/2). Perhaps this is fixed already in the latest version. I can get around this problem so its not blocking but if I can find a consistent way to reproduce the problem I will share it. Thanks again!

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [December 16, 2022, 5:26pm UTC](https://root-forum.cern.ch/t/garfield-gas-generator-using-multiple-threads/52514/6 "2022-12-16T17:26:14Z")

</div>

Closed on December 16, 2022, 5:26pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
