I have Cython code that I'm trying to compile as a DLL so that I can call it from other languages. The strange thing is that using STL vectors instead of NumPy memoryviews I get 12x lower performance. Trying to use OpenMP with Cython's prange
also doesn't seem to work (I get 100% utilization on all 4 threads w/ memoryviews and maybe 50% max w/ STL vectors). Anyone have any thoughts on how to revamp the STL version to be comparable? The Cython profiler only shows the cimport cython
and cpdef
statements to interact with Python... could it be that when called from C++ just renaming them as cdef
would improve things? Or do I have to use Intel MKL vectors as in the examples here https://software.intel.com/en-us/node/531898 which with simple option formulas these are really not a big deal to rewrite...? I'm really so inexperienced in C++ I would have to research on the internet to just create a C++ only test script... Code segment below:
cimport cython
from libcpp.vector cimport vector
cdef extern from "math.h":
double exp(double)
double sqrt(double)
double log(double)
double erf(double)
cdef inline double std_norm_cdf(double x):
return 0.5*(1+erf(x/sqrt(2.0)))
cpdef CyBlackP(vector[double] Black_PnL, vector[double] Black_S, vector[double] Black_Texpiry, vector[double] Black_strike, vector[double] Black_volatility, vector[double] Black_IR, vector[int] Black_callput):
cdef int i, N
N = Black_PnL.size()
cdef double d1, d2
for i in range(N):
d1 = (log(Black_S[i] / Black_strike[i]) + Black_Texpiry[i] * (Black_volatility[i] *Black_volatility[i]) / 2) / (Black_volatility[i] * sqrt(Black_Texpiry[i]))
d2 = d1 - Black_volatility[i] * sqrt(Black_Texpiry[i])
Black_PnL[i] = exp(-Black_IR[i] * Black_Texpiry[i]) * (Black_callput[i] * Black_S[i] * std_norm_cdf(Black_callput[i] * d1) - Black_callput[i] * Black_strike[i] * std_norm_cdf(Black_callput[i] * d2))
return Black_PnL
FYI for others out there this code above is tied out completely to be accurate w/ the Black model if you want to use it :)
Aucun commentaire:
Enregistrer un commentaire