From 54b4ce54a83cb145af8a4f22c0e9073c57cf7791 Mon Sep 17 00:00:00 2001 From: Guillaume Horel Date: Tue, 28 Aug 2018 16:00:22 -0400 Subject: fix openmp code --- pyisda/credit_index.pyx | 93 ++++++++++++++++++++++++++----------------------- 1 file changed, 50 insertions(+), 43 deletions(-) diff --git a/pyisda/credit_index.pyx b/pyisda/credit_index.pyx index ef04fec..1d7ae43 100644 --- a/pyisda/credit_index.pyx +++ b/pyisda/credit_index.pyx @@ -6,7 +6,7 @@ from libcpp.pair cimport pair from libcpp.memory cimport unique_ptr from cython.operator cimport dereference as deref from cpython cimport PyObject, Py_INCREF -from cython.parallel cimport prange +from cython.parallel cimport prange, parallel cimport cython from legs cimport (JpmcdsCdsContingentLegMake, JpmcdsCdsFeeLegMake, @@ -620,47 +620,54 @@ cdef double pv(const vector[shared_ptr[TCurve]]& curves, double recovery_rate if epsilon == 0.: - for i in prange(curves.size()): - tweaked_curve = curves[i].get() - recovery_rate = recovery_rates[i].get()[0] - JpmcdsContingentLegPV(legs.first, - base_date, - cash_settle_date, - step_in_date, - yc, - tweaked_curve, - recovery_rate, - &cl_pv) - JpmcdsFeeLegPV(legs.second, - base_date, - step_in_date, - cash_settle_date, - yc, - tweaked_curve, - True, - &fl_pv) - r += weights[i] * (cl_pv - fl_pv * fixed_rate) + with parallel(): + fl_pv = 0. + cl_pv = 0. + + for i in prange(curves.size()): + tweaked_curve = curves[i].get() + recovery_rate = recovery_rates[i].get()[0] + JpmcdsContingentLegPV(legs.first, + base_date, + cash_settle_date, + step_in_date, + yc, + tweaked_curve, + recovery_rate, + &cl_pv) + JpmcdsFeeLegPV(legs.second, + base_date, + step_in_date, + cash_settle_date, + yc, + tweaked_curve, + True, + &fl_pv) + r += weights[i] * (cl_pv - fl_pv * fixed_rate) else: - tweaked_curve = JpmcdsCopyCurve(curves[0].get()) - for i in prange(curves.size()): - orig_curve = curves[i].get() - tweak_curve(orig_curve, tweaked_curve, epsilon, mask) - recovery_rate = recovery_rates[i].get()[0] - JpmcdsContingentLegPV(legs.first, - base_date, - cash_settle_date, - step_in_date, - yc, - tweaked_curve, - recovery_rate, - &cl_pv) - JpmcdsFeeLegPV(legs.second, - base_date, - step_in_date, - cash_settle_date, - yc, - tweaked_curve, - True, - &fl_pv) - r += weights[i] * (cl_pv - fl_pv * fixed_rate) + with parallel(): + tweaked_curve = JpmcdsCopyCurve(curves[0].get()) + cl_pv = 0. + fl_pv = 0. + for i in prange(curves.size()): + orig_curve = curves[i].get() + tweak_curve(orig_curve, tweaked_curve, epsilon, mask) + recovery_rate = recovery_rates[i].get()[0] + JpmcdsContingentLegPV(legs.first, + base_date, + cash_settle_date, + step_in_date, + yc, + tweaked_curve, + recovery_rate, + &cl_pv) + JpmcdsFeeLegPV(legs.second, + base_date, + step_in_date, + cash_settle_date, + yc, + tweaked_curve, + True, + &fl_pv) + r += weights[i] * (cl_pv - fl_pv * fixed_rate) return r -- cgit v1.2.3-70-g09d2