2

I am fairly new to QuantLib and don't yet know all the ins and outs of the source but I was trying to test out a simple multi threaded calculation of several option's NPVs and am getting runtime errors. Here's my test code which is just expanded from the EquityOpiton example included with QL.

// options
VanillaOption europeanOption(payoff, europeanExercise);
VanillaOption bermudanOption(payoff, bermudanExercise);
VanillaOption americanOption(payoff, americanExercise);

boost::thread_group worker_threads;
for( int x = 0; x < 3; ++x )
{
     switch (x)
     {
     case 0:
        europeanOption.setPricingEngine(boost::shared_ptr(
           new FDEuropeanEngine(bsmProcess,
                                                100,99)));
        worker_threads.create_thread( boost::bind( &VanillaOption::NPV, &europeanOption ) );
     case 1:
        bermudanOption.setPricingEngine(boost::shared_ptr(
          new FDBermudanEngine(bsmProcess,
                                                100,99)));
        worker_threads.create_thread( boost::bind( &VanillaOption::NPV, &bermudanOption ) );
     case 2:
       americanOption.setPricingEngine(boost::shared_ptr(
         new FDAmericanEngine(bsmProcess,
                                                100,99)));
        worker_threads.create_thread( boost::bind( &VanillaOption::NPV, &americanOption ) );
      default:
        break;
  }
}


worker_threads.join_all();

What exactly is causing these runtime errors and how can I fix it? I'm guessing it has something to do with the shared pointers but they are used pretty heavily throughout QL I've noticed and I'm not sure which one(s) are causing the issue.

user2183336
  • 706
  • 8
  • 19

1 Answers1

2

It seems like you forget to add break after each case. Try following

 switch (x)
 {
 case 0:
 {
    europeanOption.setPricingEngine(boost::shared_ptr(
       new FDEuropeanEngine(bsmProcess, 100,99)));
    worker_threads.create_thread( boost::bind( &VanillaOption::NPV, &europeanOption )  );
    break;
 }
 case 1:
 {
    opt = &bermudanOption;
    bermudanOption.setPricingEngine(boost::shared_ptr(
      new FDBermudanEngine(bsmProcess,
                                            100,99)));
    worker_threads.create_thread( boost::bind( &VanillaOption::NPV, &bermudanOption ) );
    break;
 }
 case 2:
 {
   americanOption.setPricingEngine(boost::shared_ptr(
     new FDAmericanEngine(bsmProcess,
                                            100,99)));
    worker_threads.create_thread( boost::bind( &VanillaOption::NPV, &americanOption ) );
    break;
  }
  default:
    break;
 }
Nikolay Viskov
  • 1,016
  • 6
  • 9
  • derp. You got it, that did the trick... thanks a lot for the read and answer. – user2183336 May 18 '13 at 17:40
  • @user218336 You're welcome. You can upvote answers you find useful by clicking the up-triangle to their left. (This is available in addition to check-marking the answer most useful to you.) – Nikolay Viskov May 19 '13 at 00:51