QuantLib has great facilities for Monte-Carlo pricing engines, classes McSimulation and MonteCarloModel do a lot of work. But they do it in a single thread. What is best way to introduce parallel run in my custom engine (inherited from McSimulation class, as it is done for some instruments in QuantLib)? Now I see 2 options

  1. Use QuantLib routines as they are present, and handle multiple deals in parallel.
  2. Customize MonteCarloModel

    2.1 Make a subclass of MonteCarloModel and overwrite addSamples: insert there OpenMP code

    2.2 My engine is already a subclass of McSimulation (I overwrite timeGrid, pathGenerator, pathPricer), I will also overwrite "calculate" - I will copy its current code, but replace instantiation of MonteCarloModel with my implementation.

I don't like neither first, nor second options. The first doesn't allow me to price a single deal quickly, the second makes me in trouble in case of amendments in classes MonteCarloModel and McSimulation in QuantLib distributive.


1 Answer 1


Sigh. I'm not sure that there's a best way to do multi-threaded MC in QuantLib.

I'm afraid that you're underestimating the amount of development you'd need for option 2. You're not going to get away with some OpenMP code as you suggest, because calculations on different paths are not trivially parallel:

  • the RNGs we have are not parallel, and even if you use a different instance for each thread, you still risk superposition in the set of random numbers they'll draw;
  • the path generator keeps state, so you can't use the same one in different threads;
  • the statistics class keeps state, so you have to either use a different instance per thread and aggregate the results afterwards, or add a lock to the existing one to serialize access.

And these are just the problems that just come to mind in the first two minutes without looking too hard. All in all, I'd go for option 1 if you don't want to invest a lot of development time.

  • $\begingroup$ Is there any plan on ever covering multi-threaded officially? $\endgroup$
    – SmallChess
    Commented Jun 1, 2015 at 12:24
  • $\begingroup$ So far, thinking about multi-threading in MC has taken a back-seat with respect to the problem of multi-threading in general (for which there's the intent, but not a clear idea about the plan; see implementingquantlib.com/2015/02/… ). So no, not that I know of. $\endgroup$ Commented Jun 1, 2015 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.