# Multithreading on Minuit

**URL:** <https://root-forum.cern.ch/t/multithreading-on-minuit/46225>\
**Category:** Newbie\
**Created:** [August 7, 2021, 10:21am UTC](https://root-forum.cern.ch/t/multithreading-on-minuit/46225 "2021-08-07T10:21:42Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mattia\_Lopresti](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mattia_lopresti/32/27026_2.png) [@Mattia\_Lopresti](https://root-forum.cern.ch/u/Mattia_Lopresti)\
**Post date:** [August 7, 2021, 10:21am UTC](https://root-forum.cern.ch/t/multithreading-on-minuit/46225/1 "2021-08-07T10:21:42Z")

</div>

Dear all,  
currently, I am working on a macro in which Minuit/Migrad is the default minimization engine for fitting and with big data matrices it is really slow. On both this forum, on many .ppt files across the internet and other documentation files I read that Minuit2 supports parallelization. Therefore, to speed up the fitting process, I tried this:

```auto
//--introduces implicit multithreading
   ROOT::EnableImplicitMT();
//--set fitter to Minuit2 for multithreading
   ROOT::Math::MinimizerOptions::SetDefaultMinimizer("Minuit2");

```

This piece of code speeded up all the other parts of the code, such as big for loop iterations, but not the fitting procedure. I checked if Minuit2 was correctly enabled and it is. Am I missing something?  
Thank you in advance for the help. I leave here also the function and the fitting lines of code as maybe it can help.

```auto

      fqua = new TF1("fqua",fquantExafsVar,fitxminActual,fitxmaxActual,npar);
      grQua->Fit("fqua","menq","",fitxminActual,fitxmaxActual); 

```

\_ROOT Version: \_6.24.00  
_Platform:_ Ubuntu 20.04  
_Compiler:_ gcc 9.3.0

* * *

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [August 10, 2021, 3:44pm UTC](https://root-forum.cern.ch/t/multithreading-on-minuit/46225/2 "2021-08-10T15:44:24Z")

</div>

Hi,  
By doing this the fitting (the evaluation of the chi2 function) should be happening in multithreads.  
You can try to use the fit option `MULTITHREAD` in `TH1::Fit` to force this.  
If it is not happening , it could be that your ROOT build does not support multithread

Lorenzo

---

<div class="post-metadata">

**Author:** ![Mattia\_Lopresti](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mattia_lopresti/32/27026_2.png) [@Mattia\_Lopresti](https://root-forum.cern.ch/u/Mattia_Lopresti)\
**Post date:** [August 14, 2021, 12:43pm UTC](https://root-forum.cern.ch/t/multithreading-on-minuit/46225/3 "2021-08-14T12:43:06Z")

</div>

Dear Lorenzo,  
I’m struggling to find documentation about this. Where can I specify the option `MULTITHREAD`? On the page belonging to class TH1 I can’t find the field in the Fit function where it can be specified.  
Thank you again,

Mattia

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [August 16, 2021, 9:57am UTC](https://root-forum.cern.ch/t/multithreading-on-minuit/46225/4 "2021-08-16T09:57:53Z")

</div>

Hi,

This option should be passed as a fit option in `TH1::Fit`. It is true it is not documented, but in principle it should not be needed. `MULTITHREAD` should be enabled automatically when you do:  
` ROOT::EnableImplicitMT();`  
Can you tell me what is the output if you type `root-config --has-imt` ?

Lorenzo

---

<div class="post-metadata">

**Author:** ![Mattia\_Lopresti](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mattia_lopresti/32/27026_2.png) [@Mattia\_Lopresti](https://root-forum.cern.ch/u/Mattia_Lopresti)\
**Post date:** [August 16, 2021, 10:19am UTC](https://root-forum.cern.ch/t/multithreading-on-minuit/46225/5 "2021-08-16T10:19:30Z")

</div>

Hello,  
therefore have I to specify:

```auto
TH1::Fit::MULTITHREAD;

```

Sort of?  
By typing that command i receive response “yes”.  
Best regards,

Mattia

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [August 16, 2021, 10:40am UTC](https://root-forum.cern.ch/t/multithreading-on-minuit/46225/6 "2021-08-16T10:40:08Z")

</div>

> [@Mattia\_Lopresti](#):
>
> Hello,  
> therefore have I to specify:
> 
> ```auto
> TH1::Fit::MULTITHREAD;
> 
> ```
> 
> Sort of?

No, you should do:

`h1->Fit(myFunction,"MULTITHREAD")`

but as I said it should be the default. How are you sure that the fit is not run in multithread ?

Lorenzo

---

<div class="post-metadata">

**Author:** ![Mattia\_Lopresti](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mattia_lopresti/32/27026_2.png) [@Mattia\_Lopresti](https://root-forum.cern.ch/u/Mattia_Lopresti)\
**Post date:** [August 16, 2021, 11:24am UTC](https://root-forum.cern.ch/t/multithreading-on-minuit/46225/7 "2021-08-16T11:24:22Z")

</div>

I use htop to check if my macro is running in multithread or not:

In this first screenshot, i am running my app with the fitting function:

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/e/c/ec7e358174e341985a0365bf8126f19ad6312a2d.png)

In this second one, I am running my app without fitting:

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/3/8/3857c5d7fed1983756ea1377e563039e0c3cf43a.png)

As you can see, the process is shared among the threads of my laptop. From my original post I changed the fitting procedure as suggested, but nothing changed.

```auto
fqua = new TF1("fqua",fquantExafsVar,fitxminActual,fitxmaxActual,npar);
rQua->Fit("fqua","menq","MULTITHREAD",fitxminActual,fitxmaxActual); 

```

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [August 16, 2021, 12:12pm UTC](https://root-forum.cern.ch/t/multithreading-on-minuit/46225/8 "2021-08-16T12:12:43Z")

</div>

`rQua->Fit("fqua", "menq MULTITHREAD", "", fitxminActual, fitxmaxActual);`

---

<div class="post-metadata">

**Author:** ![Mattia\_Lopresti](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mattia_lopresti/32/27026_2.png) [@Mattia\_Lopresti](https://root-forum.cern.ch/u/Mattia_Lopresti)\
**Post date:** [August 16, 2021, 3:41pm UTC](https://root-forum.cern.ch/t/multithreading-on-minuit/46225/9 "2021-08-16T15:41:31Z")

</div>

Dear Wile\_E\_Coyote, thanks for your help, but modifying the line as you wrote does not enable the multithreading of the fitting function.

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/f/8/f84a5ac9c672db335a067c5bba7bcaace152b32f.png)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [August 16, 2021, 5:18pm UTC](https://root-forum.cern.ch/t/multithreading-on-minuit/46225/10 "2021-08-16T17:18:35Z")

</div>

I guess you need to post a small “standalone reproducer”, which one can run (and which demonstrates your problem).

---

<div class="post-metadata">

**Author:** ![Mattia\_Lopresti](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mattia_lopresti/32/27026_2.png) [@Mattia\_Lopresti](https://root-forum.cern.ch/u/Mattia_Lopresti)\
**Post date:** [August 17, 2021, 8:55am UTC](https://root-forum.cern.ch/t/multithreading-on-minuit/46225/11 "2021-08-17T08:55:22Z")

</div>

Dear Wile\_E\_Coyote,  
I made this small macro that has the same problem. To reproduce the error you can just edit the following line:  
`FF->Fit("f","MULTITHREAD");`  
If you specify just `MULTITHREAD`, it correctly works. If there is a space, like in `MENQ MULTITHREAD`, the fit is not performed and an error is printed out [L (Log Likelihood fit) is an invalid option when fitting a graph. It is ignored]. If you use just `MENQ`, the fit is performed, but not in multithread mode.

Mattia

[macro.C](https://root-forum.cern.ch/uploads/short-url/nao5ijwwgQeMrPnBicMmpt8js0I.C) (2.0 KB)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [August 17, 2021, 9:31am UTC](https://root-forum.cern.ch/t/multithreading-on-minuit/46225/12 "2021-08-17T09:31:11Z")

</div>

[The “`MULTITHREAD`” option works only for histograms.](https://github.com/root-project/root/blob/master/hist/hist/src/HFitImpl.cxx)

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [August 17, 2021, 9:50am UTC](https://root-forum.cern.ch/t/multithreading-on-minuit/46225/13 "2021-08-17T09:50:18Z")

</div>

Hi,  
This is correct the Multithread option is not implemented when fitting TGraphs. I will probably add an error message when the MULTITHREAD option is requested.  
How many graph points are you fitting ? Are you fitting a Graph with errors in both x and y coordinates ?  
If it is something needed, we could implement for next ROOT version

Best regards

Lorenzo

---

<div class="post-metadata">

**Author:** ![Mattia\_Lopresti](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mattia_lopresti/32/27026_2.png) [@Mattia\_Lopresti](https://root-forum.cern.ch/u/Mattia_Lopresti)\
**Post date:** [January 24, 2022, 3:41pm UTC](https://root-forum.cern.ch/t/multithreading-on-minuit/46225/15 "2022-01-24T15:41:50Z")

</div>

Dear Dr. Moneta,  
I work on a ROOT-based data processing software which is used in crystallography for X-ray diffraction data. We only plot errors on the y coordinate. Usually data are 200-10’000 x,y pairs for multiple data files organized in data matrices. We tried to edit the software [we are working on](https://users.ba.cnr.it/ic/crisrc25/RootProf/RootProf_help.html) using the TH1 histograms instead of TGraph. However, we noticed that fit results change between TGraph and TH1 and, mainly for this reason, we would like to retain the use of TGraph for consistency for our users. Can MULTITHREAD option be implemented in TGraph as you suggested?  
Best regards

Mattia

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [January 24, 2022, 5:12pm UTC](https://root-forum.cern.ch/t/multithreading-on-minuit/46225/16 "2022-01-24T17:12:58Z")

</div>

Hi,

Yes we can implement this option for fitting TGraphErrors. It can be added in the next release. I will create a GitHub issue to not forgetting this

Cheers

Lorenzo

---

<div class="post-metadata">

**Author:** ![smgrig](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/smgrig/32/19464_2.png) [@smgrig](https://root-forum.cern.ch/u/smgrig)\
**Post date:** [March 2, 2022, 9:02am UTC](https://root-forum.cern.ch/t/multithreading-on-minuit/46225/17 "2022-03-02T09:02:21Z")

</div>

Dear Lorenzo,

is there a news on multithreading for TGraphErrors fitting?

Thanks,  
Smbat

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [March 2, 2022, 2:06pm UTC](https://root-forum.cern.ch/t/multithreading-on-minuit/46225/18 "2022-03-02T14:06:25Z")

</div>

It is not yet implemented. A new issue, [https://github.com/root-project/root/issues/10021](https://github.com/root-project/root/issues/10021) i open to track this.
