Advertisement
Guest User

Untitled

a guest
Jun 27th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. $ cat t1.cpp
  2. #include <iostream>
  3. #include <stdio.h>
  4. #include <time.h>
  5. #include <cblas.h>
  6. //#include <cublas_v2.h>
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11. const int m = 5000;
  12.  
  13. timespec blas_start, blas_end;
  14. long totalnsec; //total nano sec
  15. double totalsec, totaltime;
  16. int i, j;
  17. float *A = new float[m]; // 1 x m
  18. float *B = new float[m*m]; // m x m
  19. float *C = new float[m]; // 1 x m
  20.  
  21. float input;
  22. cout << "Enter a value to populate the vector (0 for sparse) ";
  23. cin >> input; // enter 0 for sparse
  24.  
  25. // input martix A: every 32nd element is non-zero, rest of the values = input
  26. for(i = 0; i < m; i++)
  27. {
  28. A[i] = input;
  29. if( i % 32 == 0) //adjust for sparsity
  30. A[i] = i;
  31. }
  32.  
  33. // input matrix B: identity matrix
  34. for(i = 0; i < m; i++)
  35. for(j = 0; j < m; j++)
  36. B[i*m + j] = (i==j);
  37.  
  38. clock_gettime(CLOCK_REALTIME, &blas_start);
  39. cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, 1, m, m, 1.0f, A, m, B, m, 0.0f, C, m);
  40. clock_gettime(CLOCK_REALTIME, &blas_end);
  41.  
  42. /* for(i = 0; i < m; i++)
  43. printf("%f ", C[i]);
  44. printf("\n\n"); */
  45.  
  46. // Print time
  47. totalsec = (double)blas_end.tv_sec - (double)blas_start.tv_sec;
  48. totalnsec = blas_end.tv_nsec - blas_start.tv_nsec;
  49. if(totalnsec < 0)
  50. {
  51. totalnsec += 1e9;
  52. totalsec -= 1;
  53. }
  54. totaltime = totalsec + (double)totalnsec*1e-9;
  55. cout<<"Duration = "<< totaltime << "\n";
  56.  
  57. return 0;
  58. }
  59. $ g++ -I/usr/include/openblas t1.cpp -lopenblas -o t1
  60. $ ./t1
  61. Enter a value to populate the vector (0 for sparse) 5
  62. Duration = 0.026064
  63. $ ./t1
  64. Enter a value to populate the vector (0 for sparse) 0
  65. Duration = 0.0260185
  66. $
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement