Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $ cat t1.cpp
- #include <iostream>
- #include <stdio.h>
- #include <time.h>
- #include <cblas.h>
- //#include <cublas_v2.h>
- using namespace std;
- int main()
- {
- const int m = 5000;
- timespec blas_start, blas_end;
- long totalnsec; //total nano sec
- double totalsec, totaltime;
- int i, j;
- float *A = new float[m]; // 1 x m
- float *B = new float[m*m]; // m x m
- float *C = new float[m]; // 1 x m
- float input;
- cout << "Enter a value to populate the vector (0 for sparse) ";
- cin >> input; // enter 0 for sparse
- // input martix A: every 32nd element is non-zero, rest of the values = input
- for(i = 0; i < m; i++)
- {
- A[i] = input;
- if( i % 32 == 0) //adjust for sparsity
- A[i] = i;
- }
- // input matrix B: identity matrix
- for(i = 0; i < m; i++)
- for(j = 0; j < m; j++)
- B[i*m + j] = (i==j);
- clock_gettime(CLOCK_REALTIME, &blas_start);
- cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, 1, m, m, 1.0f, A, m, B, m, 0.0f, C, m);
- clock_gettime(CLOCK_REALTIME, &blas_end);
- /* for(i = 0; i < m; i++)
- printf("%f ", C[i]);
- printf("\n\n"); */
- // Print time
- totalsec = (double)blas_end.tv_sec - (double)blas_start.tv_sec;
- totalnsec = blas_end.tv_nsec - blas_start.tv_nsec;
- if(totalnsec < 0)
- {
- totalnsec += 1e9;
- totalsec -= 1;
- }
- totaltime = totalsec + (double)totalnsec*1e-9;
- cout<<"Duration = "<< totaltime << "\n";
- return 0;
- }
- $ g++ -I/usr/include/openblas t1.cpp -lopenblas -o t1
- $ ./t1
- Enter a value to populate the vector (0 for sparse) 5
- Duration = 0.026064
- $ ./t1
- Enter a value to populate the vector (0 for sparse) 0
- Duration = 0.0260185
- $
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement