Advertisement
Guest User

Untitled

a guest
Jan 19th, 2016
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. #include <math.h>
  2. #include <cblas.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5.  
  6. typedef float value_type;
  7. typedef unsigned long index_type;
  8.  
  9. static value_type F(value_type v) {
  10. return 1.0/(1.0 + exp(-v));
  11. }
  12.  
  13. static value_type DF(value_type v) {
  14. const value_type Ev = exp(-v);
  15. return Ev/((1.0 + Ev)*(1.0 + Ev));
  16. }
  17.  
  18. #ifndef WITH_BLAS
  19.  
  20. static void get_Yp(const value_type * __restrict__ Ap, const value_type * __restrict__ W,
  21. value_type * __restrict__ Yp, const value_type * __restrict__ Dp,
  22. const index_type height, const index_type width) {
  23. for (index_type i=0;i<width;i++) {
  24. Yp[height*width + i] = 2*DF(Ap[height*width + i])*(Dp[i] - F(Ap[height*width + i]));
  25. }
  26.  
  27. for (index_type k=height-1;k+1>0;k--) {
  28. for (index_type i=0;i<width;i++) {
  29. Yp[k*width + i] = 0.0;
  30. for (index_type j=0;j<width;j++) {
  31. Yp[k*width + i] += W[k*width*width + j*width + i]*Yp[(k+1)*width + j];
  32. }
  33. Yp[k*width + i] *= DF(Ap[k*width + i]);
  34. }
  35. }
  36. }
  37.  
  38. #else
  39.  
  40. static void get_Yp(const value_type * __restrict__ Ap, const value_type * __restrict__ W,
  41. value_type * __restrict__ Yp, const value_type * __restrict__ Dp,
  42. const index_type height, const index_type width) {
  43. for (index_type i=0;i<width;i++) {
  44. Yp[height*width + i] = 2*DF(Ap[height*width + i])*(Dp[i] - F(Ap[height*width + i]));
  45. }
  46.  
  47. for (index_type k=height-1;k+1>0;k--) {
  48. cblas_sgemv(CblasRowMajor, CblasTrans, width, width, 1,
  49. W+k*width*width, width, Yp+(k+1)*width, 1, 0, Yp+k*width, 1);
  50. for (index_type i=0;i<width;i++)
  51. Yp[k*width + i] *= DF(Ap[k*width + i]);
  52. }
  53. }
  54.  
  55. #endif
  56.  
  57. int main() {
  58. const index_type height=10, width=10000;
  59.  
  60. value_type *Ap=malloc((height+1)*width*sizeof(value_type)),
  61. *W=malloc(height*width*width*sizeof(value_type)),
  62. *Yp=malloc((height+1)*width*sizeof(value_type)),
  63. *Dp=malloc(width*sizeof(value_type));
  64.  
  65. get_Yp(Ap, W, Yp, Dp, height, width);
  66. printf("Done %f\n", Yp[3]);
  67.  
  68. return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement