Function minimization and multi-threading

Dear ROOT users and developers,
I am facing the following problem. I have a multi-threaded application for data analysis, and in each processing thread I want to solve a function minimization problem. The function depends on 4 parameters.

Currently, I developed my code as follows:

  1. Define a class (TMyFunction) deriving from ROOT::Math::IBaseFunctionMultiDim, that implements the
virtual double DoEval(const double* X) const

method

  1. In each processing thread, create a TMyFunction and a TMinuitMinimizer objects, and use the method SetFunction of the TMinuitMinimizer:
TMyFunction       *m_function;
TMinuitMinimizer *m_minimizer;
m_minimizer->SetFunction(m_function);
  1. Do the minimization
m_minimizer->Minimize();

However, I do not think this approach is thread-safe: looking in the TMinuitMinimizer documentation for the SetFunction method: https://root.cern.ch/root/html534/src/TMinuitMinimizer.cxx.html#j93FoB
I clearly see that this is not thread safe (NO THREAD SAFETY HERE).

In fact, when I run my code with 1 thread only, everything is ok. Otherwise, the code crashes.

I also tried to add in the code:

TMinuitMinimizer::UseStaticMinuit(kFALSE);

but this did not help

Is there a multi-thread safe way to perform, in ROOT, a function minimization?
Thanks!

Andrea

Hi Andrea,

[quote]In fact, when I run my code with 1 thread only, everything is ok. Otherwise, the code crashes.[/quote]Did you turn on ROOT thread support (TThread::Initialize()) and did you create one TThread object per thread?

Cheers,
Philippe.

Perhaps thread [url=https://root-forum.cern.ch/t/multi-threaded-fitting/16313/1 fitting[/url] has something useful.