Is there a way to make TH2::Fit thread safe?

Hi, I have a function with fits a TH2 histogram with a TF2 object. This function is called from several threads simultaneously. The program crashes always at the line h2->Fit(f2,"QWWR0");

My workaround right now is to have a QMutex. This way, the program does not crash, but it is very very slow, as I am fitting 2800 2D histograms.

rootMutex.lock();
h2->Fit(f2,"QWWR0");
rootMutex.unlock();

Do you have an idea how to make my own custom Fit function with h2 and f2 so that it is thread safe and I dont need a Mutex? Thanks.

Hi,

It should be thread safe, but you need to activate the internal locks in ROOT by calling

ROOT::EnableThreadSafety()

Otherwise in the master revision each single fit can be computed in multi-threads by calling
ROOT::EnableImplicitMT() or using the fit option "MULTITHREAD"

Lorenzo

Thanks for the fast reply. I had already enabled ROOT::EnableThreadSafety(), but it still crashes. If I use the additional QMutex, everything works fine (and slow). If I dont use the QMutex, I get this error:

THE COMMAND:    MIGRAD IS UNKNOWN.
 THE FORMAT OF THE  SET COMMAND IS:
 SET xxx    [numerical arguments if any]
 WHERE xxx MAY BE ONE OF THE FOLLOWING:
 FCN value 
 PARameters
 LIMits    
 COVariance
 CORrelatio
 PRInt levl
 NOGradient
 GRAdient  
 ERRor def 
 INPut file
 WIDth page
 LINes page
 NOWarnings
 WARnings  
 RANdom gen
 TITle     
 STRategy  
 EIGenvalue
 PAGe throw
 MINos errs
 EPSmachine
 OUTputfile
 BATch     
 INTeractiv
 VERsion   
*** Error in `/home/ferhue/Programs/build-pga-Desktop-Release/pga': corrupted size vs. prev_size: 0x00007f8260051480 ***
*** Error in `/home/ferhue/Programs/build-pga-Desktop-Release/pga': double free or corruption (fasttop): 0x00007f8238003450 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f847aaec7e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x8037a)[0x7f847aaf537a]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7f847aaf953c]
/opt/root/lib/libCore.so(_ZN7TStringaSERKS_+0x46)[0x7f847e649166]
/opt/root/lib/libMinuit.so(_ZN7TMinuit6mnwarnEPKcS1_S1_+0x151)[0x7f847d035d91]
/opt/root/lib/libMinuit.so(_ZN7TMinuit6mnlineEPddS0_dd+0xd07)[0x7f847d02bfc7]
/opt/root/lib/libMinuit.so(_ZN7TMinuit6mnmigrEv+0x4be)[0x7f847d02923e]
/opt/root/lib/libMinuit.so(_ZN7TMinuit6mnexcmEPKcPdiRi+0x8c1)[0x7f847d03b481]
/opt/root/lib/libMinuit.so(_ZN16TMinuitMinimizer8MinimizeEv+0x272)[0x7f847d0424d2]
/opt/root/lib/libMathCore.so(_ZN4ROOT3Fit6Fitter14DoMinimizationEPKNS_4Math26IBaseFunctionMultiDimTemplIdEE+0x1e)[0x7f8478c07ffe]
/opt/root/lib/libMathCore.so(_ZN4ROOT3Fit6Fitter16DoLeastSquareFitEv+0x4ed)[0x7f8478c08c3d]
/opt/root/lib/libHist.so(_ZN4HFit3FitI3TH1EE13TFitResultPtrPT_P3TF1R9Foption_tRKN4ROOT4Math16MinimizerOptionsEPKcRNS9_3Fit9DataRangeE+0xe43)[0x7f847e027c03]
/opt/root/lib/libHist.so(_ZN4ROOT3Fit9FitObjectEP3TH1P3TF1R9Foption_tRKNS_4Math16MinimizerOptionsEPKcRNS0_9DataRangeE+0x3a)[0x7f847e01d53a]
/opt/root/lib/libHist.so(_ZN3TH13FitEP3TF1PKcS3_dd+0x1b3)[0x7f847e0ccc43]
/home/ferhue/Programs/build-pga-Desktop-Release/pga(_ZN9Histogram21backgroundSubtractionEv+0x543)[0x594273]
/home/ferhue/Programs/build-pga-Desktop-Release/pga(_Z25histogramPhaseShiftToPeakRP9Histogram+0x14)[0x4bee74]
/home/ferhue/Programs/build-pga-Desktop-Release/pga(_ZN12QtConcurrent9MapKernelIPP9HistogramNS_16FunctionWrapper1IvRS2_EEE13runIterationsES3_iiPv+0x37)[0x4ca047]
/home/ferhue/Programs/build-pga-Desktop-Release/pga(_ZN12QtConcurrent13IterateKernelIPP9HistogramvE14threadFunctionEv+0x211)[0x4cd541]
/usr/lib/x86_64-linux-gnu/libQt5Concurrent.so.5(_ZN12QtConcurrent16ThreadEngineBase3runEv+0x6d)[0x7f848037f6bd]
/usr/lib/x86_64-linux-gnu/libQt5Core.so.5(+0xa22d3)[0x7f847b99f2d3]
/usr/lib/x86_64-linux-gnu/libQt5Core.so.5(+0xa57be)[0x7f847b9a27be]
/lib/x86_64-linux-gnu/libpthread.so.0(+0x76ba)[0x7f847b6e76ba]
/lib/x86_64-linux-gnu/libc.so.6(clone+0x6d)[0x7f847ab7c3dd]

I always get a crash, but the error messages are each time slightly different. Another one is:

*** Error in `/home/ferhue/Programs/build-pga-Desktop-Release/pga': corrupted double-linked list: 0x00007f635c012ed0 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f65916217e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x80baf)[0x7f659162abaf]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7f659162e53c]
/opt/root/lib/libMinuit.so(_ZN7TMinuit12DeleteArraysEv+0x259)[0x7f6593b6eb59]
/opt/root/lib/libMinuit.so(_ZN7TMinuitD1Ev+0x20)[0x7f6593b72a40]
/opt/root/lib/libMinuit.so(_ZN7TMinuitD0Ev+0x9)[0x7f6593b72c49]
/opt/root/lib/libMinuit.so(_ZN16TMinuitMinimizer11InitTMinuitEi+0x1ab)[0x7f6593b768fb]
/opt/root/lib/libMinuit.so(_ZN16TMinuitMinimizer11SetFunctionERKN4ROOT4Math26IBaseFunctionMultiDimTemplIdEE+0x35)[0x7f6593b769f5]
/opt/root/lib/libMathCore.so(_ZN4ROOT3Fit6Fitter15DoInitMinimizerEv+0x2d8)[0x7f658f73b398]
/opt/root/lib/libMathCore.so(_ZN4ROOT3Fit6Fitter14DoMinimizationERKNS_4Math26IBaseFunctionMultiDimTemplIdEEPS5_+0x96)[0x7f658f73d636]
/opt/root/lib/libMathCore.so(_ZN4ROOT3Fit6Fitter16DoLeastSquareFitEv+0x4ed)[0x7f658f73dc3d]
/opt/root/lib/libHist.so(_ZN4HFit3FitI3TH1EE13TFitResultPtrPT_P3TF1R9Foption_tRKN4ROOT4Math16MinimizerOptionsEPKcRNS9_3Fit9DataRangeE+0xe43)[0x7f6594b5cc03]
/opt/root/lib/libHist.so(_ZN4ROOT3Fit9FitObjectEP3TH1P3TF1R9Foption_tRKNS_4Math16MinimizerOptionsEPKcRNS0_9DataRangeE+0x3a)[0x7f6594b5253a]
/opt/root/lib/libHist.so(_ZN3TH13FitEP3TF1PKcS3_dd+0x1b3)[0x7f6594c01c43]
/home/ferhue/Programs/build-pga-Desktop-Release/pga(_ZN9Histogram21backgroundSubtractionEv+0x738)[0x594448]
/home/ferhue/Programs/build-pga-Desktop-Release/pga(_Z25histogramPhaseShiftToPeakRP9Histogram+0x14)[0x4bee24]
/home/ferhue/Programs/build-pga-Desktop-Release/pga(_ZN12QtConcurrent9MapKernelIPP9HistogramNS_16FunctionWrapper1IvRS2_EEE13runIterationsES3_iiPv+0x37)[0x4c9ff7]
/home/ferhue/Programs/build-pga-Desktop-Release/pga(_ZN12QtConcurrent13IterateKernelIPP9HistogramvE14threadFunctionEv+0x211)[0x4cd4f1]
/usr/lib/x86_64-linux-gnu/libQt5Concurrent.so.5(_ZN12QtConcurrent16ThreadEngineBase3runEv+0x6d)[0x7f6596eb46bd]
/usr/lib/x86_64-linux-gnu/libQt5Core.so.5(+0xa22d3)[0x7f65924d42d3]

 *** Break *** segmentation violation
/usr/lib/x86_64-linux-gnu/libQt5Core.so.5(+0xa57be)[0x7f65924d77be]
/lib/x86_64-linux-gnu/libpthread.so.0(+0x76ba)[0x7f659221c6ba]
/lib/x86_64-linux-gnu/libc.so.6(clone+0x6d)[0x7f65916b13dd]

Im using ROOT 6.10/02

Hi,

The problem is in TMinuit, and yes it is not thread-safe. You should use Minuit2, so just do before fitting and spwaning the threads

ROOT::Math::MinimizerOptions::SetDefaultMinimizer(“Minuit2”);

In case you don’t have Minuit2, you need to build enabling that option
cmake . -Dminuit2=On

Lorenzo

Thanks so much, it works perfect now.

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