Advertisement
mohomran

Array Benchmark

Jun 7th, 2013
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.68 KB | None | 0 0
  1. /*TO COMPILE: g++ -I /usr/include/eigen3 test.cpp -Wall -O3 -DNDEBUG -DBOOST_DISABLE_ASSERTS -fopenmp -ffast-math -funroll-loops -march=native -mtune=native -o array_test */
  2.  
  3. // Please note that Eigen version 3 is required
  4.  
  5.  
  6. #include <Eigen/Dense>
  7. #include <boost/multi_array.hpp>
  8.  
  9. #include <vector>
  10. #include <iostream>
  11.  
  12. #include <omp.h>
  13.  
  14. using namespace std;
  15.  
  16. const size_t
  17.   rows = 25000,
  18.   cols = 25000,
  19.   cols_subset = 10000,
  20.   iterations = 1e1;
  21.  
  22. /*** SEQUENTIAL ACCESS ***/
  23.  
  24. template<typename MatrixType>
  25. void sequential_access_test_eigen(const MatrixType &eigen_matrix)
  26. {
  27.     double
  28.             tmp_sum = 0,
  29.             tmp_time = omp_get_wtime();
  30.  
  31.     for(size_t i=0; i<iterations; i++)
  32.     {
  33.         for(size_t j=0; j<rows; j++)
  34.         {
  35.             for(size_t k=0; k<cols_subset; k++)
  36.             {
  37.                 tmp_sum += eigen_matrix(j, k);
  38.             }
  39.         }
  40.     }
  41.     double sequential_access_time = omp_get_wtime() - tmp_time;
  42.  
  43.     std::cout << "SEQUENTIAL ACCESS (EIGEN)          :" << sequential_access_time << "s" << std::endl;
  44.     std::cout << "tmp_sum == " << tmp_sum << std::endl;
  45. }
  46.  
  47. template<typename ArrayType>
  48. void sequential_access_test_boost(ArrayType &boost_matrix)
  49. {
  50.     double
  51.             tmp_sum = 0,
  52.             tmp_time = omp_get_wtime();
  53.  
  54.     for(size_t i=0; i<iterations; i++)
  55.     {
  56.         for(size_t j=0; j<rows; j++)
  57.         {
  58.             for(size_t k=0; k<cols_subset; k++)
  59.             {
  60.                 tmp_sum += boost_matrix[j][k];
  61.             }
  62.         }
  63.     }
  64.  
  65.     double sequential_access_time = omp_get_wtime() - tmp_time;
  66.     std::cout << "SEQUENTIAL ACCESS (BOOST)          :" << sequential_access_time << "s" << std::endl;
  67.     std::cout << "tmp_sum == " << tmp_sum << std::endl;
  68. }
  69.  
  70. template<typename ArrayType>
  71. void sequential_access_test_boost_mapped(ArrayType &boost_matrix)
  72. {
  73.     double
  74.             tmp_sum = 0,
  75.             tmp_time = omp_get_wtime();
  76.  
  77.     Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> > boost_matrix_mapped(boost_matrix.data(), rows, cols);
  78.  
  79.     for(size_t i=0; i<iterations; i++)
  80.     {
  81.         for(size_t j=0; j<rows; j++)
  82.         {
  83.             for(size_t k=0; k<cols_subset; k++)
  84.             {
  85.                 tmp_sum += boost_matrix_mapped(j,k);
  86.             }
  87.         }
  88.     }
  89.  
  90.     double sequential_access_time = omp_get_wtime() - tmp_time;
  91.     std::cout << "SEQUENTIAL ACCESS (BOOST_MAPPED)   :" << sequential_access_time << "s" << std::endl;
  92.     std::cout << "tmp_sum == " << tmp_sum << std::endl;
  93. }
  94.  
  95. /*** CONDITIONAL ACCESS ***/
  96.  
  97. template<typename MatrixType>
  98. void conditional_access_test_eigen(const MatrixType &eigen_matrix)
  99. {
  100.     double
  101.             tmp_sum = 0,
  102.             tmp_time = omp_get_wtime();
  103.  
  104.     for(size_t i=0; i<iterations; i++)
  105.     {
  106.         for(size_t j=0; j<rows; j++)
  107.         {
  108.             for(size_t k=0; k<cols_subset; k++)
  109.             {
  110.                 if(k%2==0)
  111.                 {
  112.                     tmp_sum += eigen_matrix(j, k);
  113.                 }
  114.             }
  115.         }
  116.     }
  117.     double sequential_access_time = omp_get_wtime() - tmp_time;
  118.  
  119.     std::cout << "CONDITIONAL ACCESS (EIGEN)         :" << sequential_access_time << "s" << std::endl;
  120.     std::cout << "tmp_sum == " << tmp_sum << std::endl;
  121. }
  122.  
  123. template<typename ArrayType>
  124. void conditional_access_test_boost(ArrayType &boost_matrix)
  125. {
  126.     double
  127.             tmp_sum = 0,
  128.             tmp_time = omp_get_wtime();
  129.  
  130.     for(size_t i=0; i<iterations; i++)
  131.     {
  132.         for(size_t j=0; j<rows; j++)
  133.         {
  134.             for(size_t k=0; k<cols_subset; k++)
  135.             {
  136.                 if(k%2==0)
  137.                 {
  138.                     tmp_sum += boost_matrix[j][k];
  139.                 }
  140.             }
  141.         }
  142.     }
  143.  
  144.     double sequential_access_time = omp_get_wtime() - tmp_time;
  145.     std::cout << "CONDITIONAL ACCESS (BOOST)         :" << sequential_access_time << "s" << std::endl;
  146.     std::cout << "tmp_sum == " << tmp_sum << std::endl;
  147. }
  148.  
  149. template<typename ArrayType>
  150. void conditional_access_test_boost_mapped(ArrayType &boost_matrix)
  151. {
  152.     double
  153.             tmp_sum = 0,
  154.             tmp_time = omp_get_wtime();
  155.  
  156.     Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> > boost_matrix_mapped(boost_matrix.data(), rows, cols);
  157.  
  158.     for(size_t i=0; i<iterations; i++)
  159.     {
  160.         for(size_t j=0; j<rows; j++)
  161.         {
  162.             for(size_t k=0; k<cols_subset; k++)
  163.             {
  164.                 if(k%2==0)
  165.                 {
  166.                     tmp_sum += boost_matrix_mapped(j,k);
  167.                 }
  168.             }
  169.         }
  170.     }
  171.  
  172.     double sequential_access_time = omp_get_wtime() - tmp_time;
  173.     std::cout << "CONDITIONAL ACCESS (BOOST_MAPPED)  :" << sequential_access_time << "s" << std::endl;
  174.     std::cout << "tmp_sum == " << tmp_sum << std::endl;
  175. }
  176.  
  177.  
  178.  
  179. int main()
  180. {
  181.     //// INITIALISE RANDOM TEST DATA ////
  182.  
  183.     Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> eigen_matrix;
  184.     eigen_matrix = Eigen::MatrixXd::Random(rows, cols);
  185.  
  186.     boost::multi_array<double, 2, Eigen::aligned_allocator<double> > boost_matrix(boost::extents[rows][cols]);
  187.  
  188.     for(size_t j=0; j<rows; j++)
  189.     {
  190.         for(size_t k=0; k<cols; k++)
  191.         {
  192.             boost_matrix[j][k] = eigen_matrix(j,k);
  193.         }
  194.     }
  195.  
  196.     //// RUN TESTS ////
  197.  
  198.     sequential_access_test_boost_mapped(boost_matrix);
  199.     sequential_access_test_eigen(eigen_matrix);
  200.     sequential_access_test_boost(boost_matrix);
  201.  
  202.     conditional_access_test_boost_mapped(boost_matrix);
  203.     conditional_access_test_eigen(eigen_matrix);
  204.     conditional_access_test_boost(boost_matrix);
  205.  
  206.     return 0;
  207.  
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement