Login  Register

Re: Quantlib - multithreading

Posted by Luigi Ballabio on Dec 21, 2016; 1:42pm
URL: http://quantlib.414.s1.nabble.com/Quantlib-multithreading-tp17934p17938.html

You don't have to call sessionId() from your code.  The idea is that, inside sessionId, you should somehow detect in what thread you are and return a corresponding id.  One way you could do that is to use some system function to return a unique id for the thread; searching for "thread id" should get you some directions.  But I haven't tried myself, so it would be better if someone else stepped in to share their experience.

Luigi


On Wed, Dec 21, 2016 at 3:07 PM Ghorpadkar, Suhas <[hidden email]> wrote:

Thank you. I am using the following setup but I am not sure how to return sessionId per each thread.

Any help much appreciated.

 

--------------------

 

//launch threads using async,  doWork() is called by multiple threads simultaneously

 

std::async(std::launch::async,doWork,Handle<YieldTermStructure>,arg2,arg3)

 

//how do I return sessionId from here ? I am not sure where exactly sessionId() method should go.

std::vector<Real> doWork(Handle<YieldTermStructure> & swapCurve, arg2, arg3) {

               

                //QuantLib::sessionId() = 10;  ?

               

                Real y_t = swapCurve->discount(t);

 

}

 

From: Luigi Ballabio [mailto:[hidden email]]
Sent: Wednesday, December 21, 2016 4:09 AM
To: Ghorpadkar, Suhas; [hidden email]
Subject: Re: [Quantlib-users] Quantlib - multithreading

 

Yes, you do need to return something other than 0.  It should be a unique number for each thread.

 

Luigi

 

 

On Wed, Dec 21, 2016 at 10:06 AM Ghorpadkar, Suhas <[hidden email]> wrote:

I am working on a custom MonteCarlo simulation and was trying to experiment with multiple threads.

I am aware that Quantlib as such is not thread safe. I have uncommented “define QL_ENABLE_SESSIONS” in userconfig.hpp

and have defined sessionId function at the beginning of my cpp file. I was trying to create QL objects such as YieldTermStructure handles and CalibratedModel inside the function that will called by each thread so that QL objects are not shared by multiple threads and each has a copy of its own. This worked for my Model object but not for YieldTermStructure objects. I had to lock the critical section using mutex as below even though swapCurve and dividendCurve objects are thread local.

I do not understand why. May be I need to return something other than zero in my sessionId() function ?

 

std::lock_guard<std::mutex> lg(mapMutex);

{

                Dr_t = swapCurve->discount(t);

                Dr_T = swapCurve->discount(t + dt);

 

                Dq_t = dividendCurve->discount(t);

                Dq_T = dividendCurve->discount(t + dt);

}

 

Thank you.

............................................................................

For further important information about AllianceBernstein please click here
http://www.abglobal.com/disclaimer/email/disclaimer.html

------------------------------------------------------------------------------
Developer Access Program for Intel Xeon Phi Processors
Access to Intel Xeon Phi processor-based developer platforms.
With one year of Intel Parallel Studio XE.
Training and support from Colfax.
Order your platform today.http://sdm.link/intel_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users


------------------------------------------------------------------------------
Developer Access Program for Intel Xeon Phi Processors
Access to Intel Xeon Phi processor-based developer platforms.
With one year of Intel Parallel Studio XE.
Training and support from Colfax.
Order your platform today.http://sdm.link/intel
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users