# Problems about multithreading techniques using OpenMP and ComponentComsol

**URL:** <https://root-forum.cern.ch/t/problems-about-multithreading-techniques-using-openmp-and-componentcomsol/54777>\
**Category:** Garfield++\
**Created:** [May 8, 2023, 2:52pm UTC](https://root-forum.cern.ch/t/problems-about-multithreading-techniques-using-openmp-and-componentcomsol/54777 "2023-05-08T14:52:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lan-sx](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/lan-sx/32/30631_2.png) [@Lan-sx](https://root-forum.cern.ch/u/Lan-sx)\
**Post date:** [May 8, 2023, 2:52pm UTC](https://root-forum.cern.ch/t/problems-about-multithreading-techniques-using-openmp-and-componentcomsol/54777/1 "2023-05-08T14:52:44Z")

</div>

Dear Garfield++ experts:  
  I am trying to speed up my simulation by using the **OpenMP**. In the code below, I built my detector model in **Comsol** software, then imported the field maps to Garfiled++ and simulated the electron drift using the class `AvalancheMicroscopic`.  
  Some Garfield++ objects were defined as arrays of pointers for each thread. However, The results of multi-threading and single-threading simulations were consistent, but the former was much slower. Race conditions seemed happened between different threads.  
  Is there an issue with either my code or the `ComponentComsol` class in Garfield++? And how can I solve the issue?

  Here is part of my code:

```plaintext
    // Initial ComponentComsol Obj
    ComponentComsol* fm = new ComponentComsol();
    fm->Initialise(fileMesh.c_str(), fileDielectrics.c_str(), fileField.c_str(), "mm");
    fm->SetMagneticField(0.,0.,0.);
    if( Debug ) fm->PrintRange();
    const unsigned int nMaterials = fm->GetNumberOfMaterials();
    for (unsigned int i = 0; i < nMaterials; ++i)
    {
        const double eps = fm->GetPermittivity(i);
        if (eps == 1.)
          fm->SetMedium(i, &gas); // for Gas Medium ,eps=1. copper set to 1e10
    }
    
   //Set up avalanche and sensor for each thread:
    Sensor **sensor=new Sensor*[nThreads];
    AvalancheMicroscopic **driftelectron=new AvalancheMicroscopic*[nThreads];
    std::vector<TRandom3*> vRnd;
    vRnd.resize(nThreads);
    for(int iThread=0;iThread<nThreads;++iThread)
    {
         //Random Engine
        vRnd[iThread] = new TRandom3(0);
         //Sensor
        sensor[iThread]=new Sensor();
        sensor[iThread]->AddComponent(fm);
        sensor[iThread]->SetArea(-Radius-4, -Radius-4,0,Radius+4,Radius+4,Zmax);
         //aval
        driftelectron[iThread]=new AvalancheMicroscopic();
        driftelectron[iThread]->SetSensor(sensor[iThread]);
        driftelectron[iThread]->EnableMagneticField();
        driftelectron[iThread]->SetCollisionSteps(100);    
    }
    // Create ROOT file
    TFile *file = new TFile("DriftLine_e_MP.root", "RECREATE");
    // Creat TTree
    TTree *tte = new TTree("tte", "DriftVelocity");
    tte->Branch("velocity", &velocity, "velocity/D");
    tte->Branch("x0", &x0, "x0/D");
    tte->Branch("y0", &y0, "y0/D");
    tte->Branch("z0", &z0, "z0/D");
    tte->Branch("x1", &x1, "x1/D");
    tte->Branch("y1", &y1, "y1/D");
    tte->Branch("z1", &z1, "z1/D");
    tte->Branch("status", &status, "status/I");
    tte->Branch("threadid",&threadid,"threadid/I");
    tte->Branch("t1", &t1, "t1/D");

    omp_set_num_threads(nThreads);
#pragma omp parallel for
    for (int i = 0; i < nEvents; ++i)
    {
        omp_set_lock(&writelock);
        int thread_num = omp_get_thread_num();
        if (i % 200 == 0)
          std::cout << "----WT" << thread_num << ">: " << i << "/" << nEvents << " || " << std::endl;
        omp_unset_lock(&writelock);
        int statusmt(0);
        double xx0(0.), yy0(0.), zz0(0.), tt0(0.), ee0(0.), velocitymt(0.);
        double xx1(0.), yy1(0.), zz1(0.), tt1(0.), ee1(0.);

        xx0 = vRnd[thread_num]->Uniform(-15, 15);
        // xx0 = rnd.Uniform(-15, 15);
        yy0 = 0., zz0 = posZ, tt0 = 0;

        driftelectron[thread_num]->DriftElectron(xx0, yy0, zz0, tt0, ee0);
        driftelectron[thread_num]->GetElectronEndpoint(0, xx0, yy0, zz0, tt0, ee0, xx1, yy1, zz1, tt1, ee1, statusmt);

        if ((tt1 - tt0) != 0)
          velocitymt = 1e9 * TMath::Abs(zz0 - zz1) / (tt1 - tt0); // Unit [cm/s]
        else
          velocitymt = 0;
        
#pragma omp critical
        {
          x0 = xx0;
          y0 = yy0;
          z0 = zz0;
          x1 = xx1;
          y1 = yy1;
          z1 = zz1;
          velocity = velocitymt;
          status = statusmt;
          threadid = thread_num;
          t1 = tt1;

          tte->Fill();
        }
        omp_destroy_lock(&writelock);
    }
    tte->Write();
    file->Close();

```

  Lansx.

---

<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:** [May 9, 2023, 5:47am UTC](https://root-forum.cern.ch/t/problems-about-multithreading-techniques-using-openmp-and-componentcomsol/54777/2 "2023-05-09T05:47:17Z")

</div>

Good question… I wouldn’t expect `ComponentComsol` to be the issue. One potential issue is that all instances of `AvalancheMicroscopic` share the same random number generator.

---

<div class="post-metadata">

**Author:** ![Lan-sx](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/lan-sx/32/30631_2.png) [@Lan-sx](https://root-forum.cern.ch/u/Lan-sx)\
**Post date:** [May 9, 2023, 4:12pm UTC](https://root-forum.cern.ch/t/problems-about-multithreading-techniques-using-openmp-and-componentcomsol/54777/3 "2023-05-09T16:12:46Z")

</div>

Thanks for your reply!

- Can it be solved? I couldn’t find any method in the `AvalancheMicroscopic` that can change the random number generator.😔
- This may also be related to the platform ( **WSL2\_Ubuntu22.04** ) or machine I used. I ran the same code on the Compute Cluster ( **Centos7.9** ), and it did improve the calculation speed to a certain extent by using **OpenMP** , as shown in the figure below. 👿  
 ![TwoPlatforms](https://root-forum.cern.ch/uploads/default/original/3X/f/b/fba475314cd9fe519cdc902e04eaef26d0ced723.png)

---

<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:** [May 10, 2023, 8:06am UTC](https://root-forum.cern.ch/t/problems-about-multithreading-techniques-using-openmp-and-componentcomsol/54777/4 "2023-05-10T08:06:35Z")

</div>

> [@Lan-sx](#):
>
> Can it be solved? I couldn’t find any method in the `AvalancheMicroscopic` that can change the random number generator.

It’s something on the to-do list but I’m not sure if there’s a quick solution…

---

<div class="post-metadata">

**Author:** ![Lan-sx](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/lan-sx/32/30631_2.png) [@Lan-sx](https://root-forum.cern.ch/u/Lan-sx)\
**Post date:** [May 10, 2023, 10:21am UTC](https://root-forum.cern.ch/t/problems-about-multithreading-techniques-using-openmp-and-componentcomsol/54777/5 "2023-05-10T10:21:53Z")

</div>

Thanks so much for your reply. I will continue to look for other solutions. 😃

---

<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:** [May 24, 2023, 10:22am UTC](https://root-forum.cern.ch/t/problems-about-multithreading-techniques-using-openmp-and-componentcomsol/54777/6 "2023-05-24T10:22:28Z")

</div>

Closed on May 24, 2023, 10:22am UTC

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