cpu_black_scholes.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2019 Mael Keryell
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <stdint.h>
  17. #include <starpu.h>
  18. #include <math.h>
  19. static inline double normcdf(double x)
  20. {
  21. return (1.0 + erf(x/sqrt(2.0)))/2.0;
  22. }
  23. void cpu_black_scholes(void *descr[], void *arg)
  24. {
  25. double *S, *K, *R, *T, *SIG, *CRES, *PRES;
  26. uint32_t nxS;
  27. S = (double *)STARPU_MATRIX_GET_PTR(descr[0]);
  28. K = (double *)STARPU_MATRIX_GET_PTR(descr[1]);
  29. R = (double *)STARPU_MATRIX_GET_PTR(descr[2]);
  30. T = (double *)STARPU_MATRIX_GET_PTR(descr[3]);
  31. SIG = (double *)STARPU_MATRIX_GET_PTR(descr[4]);
  32. CRES = (double *)STARPU_MATRIX_GET_PTR(descr[5]);
  33. PRES = (double *)STARPU_MATRIX_GET_PTR(descr[6]);
  34. nxS = STARPU_MATRIX_GET_NX(descr[0]);
  35. uint32_t i;
  36. for (i = 0; i < nxS; i++){
  37. double d1 = (log(S[i] / K[i]) + (R[i] + pow(SIG[i], 2.0) * 0.5) * T[i]) / (SIG[i] * sqrt(T[i]));
  38. double d2 = (log(S[i] / K[i]) + (R[i] - pow(SIG[i], 2.0) * 0.5) * T[i]) / (SIG[i] * sqrt(T[i]));
  39. CRES[i] = S[i] * normcdf(d1) - K[i] * exp(-R[i] * T[i]) * normcdf(d2);
  40. PRES[i] = -S[i] * normcdf(-d1) + K[i] * exp(-R[i] * T[i]) * normcdf(-d2);
  41. }
  42. }