# The wrong result when calculating using multiple threads

**URL:** <https://root-forum.cern.ch/t/the-wrong-result-when-calculating-using-multiple-threads/59159>\
**Category:** Garfield++\
**Created:** [April 28, 2024, 3:50am UTC](https://root-forum.cern.ch/t/the-wrong-result-when-calculating-using-multiple-threads/59159 "2024-04-28T03:50:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![newconcept1979](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/newconcept1979/32/26349_2.png) [@newconcept1979](https://root-forum.cern.ch/u/newconcept1979)\
**Post date:** [April 28, 2024, 3:50am UTC](https://root-forum.cern.ch/t/the-wrong-result-when-calculating-using-multiple-threads/59159/1 "2024-04-28T03:50:43Z")

</div>

The gas gain was calculated by single thread and multiple threads.  
The mean gain by 1000 events from single thread is 315,  
but, the mean gain is 23 from multiple threads.

the key codes are:

```auto
 #pragma omp parallel for
for(int event=0;event<EventNumber;event++)
{
  AvalancheMicroscopic aval(&sensor);
  
  aval.AvalancheElectron(x0, y0, z0, t0, e0);
 
  ElectronNumber[event]=aval.GetNumberOfElectronEndpoints();
}
for(int i=0;i<EventNumber;i++)
{
  TotalElectronNumber+=ElectronNumber[i];
}

```

what is the wrong thing with the codes?

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [April 28, 2024, 4:33am UTC](https://root-forum.cern.ch/t/the-wrong-result-when-calculating-using-multiple-threads/59159/2 "2024-04-28T04:33:19Z")

</div>

Hi,

Have you checked for the absence of data races, for example with helgrind?

Best,  
D

---

<div class="post-metadata">

**Author:** ![newconcept1979](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/newconcept1979/32/26349_2.png) [@newconcept1979](https://root-forum.cern.ch/u/newconcept1979)\
**Post date:** [April 28, 2024, 5:31am UTC](https://root-forum.cern.ch/t/the-wrong-result-when-calculating-using-multiple-threads/59159/3 "2024-04-28T05:31:41Z")

</div>

@Danilo , no, i will try!

---

<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 12, 2024, 5:31am UTC](https://root-forum.cern.ch/t/the-wrong-result-when-calculating-using-multiple-threads/59159/4 "2024-05-12T05:31:54Z")

</div>

Closed on May 12, 2024, 5:31am UTC

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