SHARE
TWEET

Untitled

a guest Mar 18th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. auto to_dense_column_major_naive(std::vector<std::vector<double>> const & vec)
  2.     -> std::vector<double>
  3. {
  4.     auto n_col = vec.size();
  5.     auto n_row = vec[0].size();
  6.     std::vector<double> out_vec(n_col * n_row);
  7.     for (size_t i = 0; i < n_col; ++i)
  8.         for (size_t j = 0; j < n_row; ++j)
  9.             out_vec[i * n_row + j] = vec[i][j];
  10.     return out_vec;
  11. }
  12.      
  13. auto to_dense_row_major_blocking(std::vector<std::vector<double>> const & vec)
  14.     -> std::vector<double>
  15. {
  16.     auto n_col = vec.size();
  17.     auto n_row = vec[0].size();
  18.     std::vector<double> out_vec(n_col * n_row);
  19.     size_t block_side = 8;
  20.  
  21.     for (size_t l = 0; l < n_col; l += block_side) {
  22.         for (size_t k = 0; k < n_row; k += block_side) {
  23.             for (size_t j = l; j < l + block_side && j < n_col; ++j) {
  24.                 auto const &column = vec[j];
  25.                 for (size_t i = k; i < k + block_side && i < n_row; ++i)
  26.                     out_vec[i * n_col + j] = column[i];
  27.             }
  28.         }
  29.     }
  30.     return out_vec;
  31. }
  32.      
  33. auto to_dense_column_major_naive(std::vector<std::vector<double>> const & vec)
  34.     -> std::vector<double>
  35. {
  36.     auto n_col = vec.size();
  37.     auto n_row = vec[0].size();
  38.     std::vector<double> out_vec(n_col * n_row);
  39.     for (size_t i = 0; i < n_col; ++i)
  40.         for (size_t j = 0; j < n_row; ++j)
  41.             out_vec[i * n_row + j] = vec[i][j];
  42.     return out_vec;
  43. }
  44.  
  45. auto to_dense_row_major_naive(std::vector<std::vector<double>> const & vec)
  46.     -> std::vector<double>
  47. {
  48.     auto n_col = vec.size();
  49.     auto n_row = vec[0].size();
  50.     std::vector<double> out_vec(n_col * n_row);
  51.     for (size_t i = 0; i < n_col; ++i)
  52.         for (size_t j = 0; j < n_row; ++j)
  53.             out_vec[j * n_col + i] = vec[i][j];
  54.     return out_vec;
  55. }
  56.  
  57. auto to_dense_row_major_blocking(std::vector<std::vector<double>> const & vec)
  58.     -> std::vector<double>
  59. {
  60.     auto n_col = vec.size();
  61.     auto n_row = vec[0].size();
  62.     std::vector<double> out_vec(n_col * n_row);
  63.     size_t block_side = 8;
  64.  
  65.     for (size_t l = 0; l < n_col; l += block_side) {
  66.         for (size_t k = 0; k < n_row; k += block_side) {
  67.             for (size_t j = l; j < l + block_side && j < n_col; ++j) {
  68.                 auto const &column = vec[j];
  69.                 for (size_t i = k; i < k + block_side && i < n_row; ++i)
  70.                     out_vec[i * n_col + j] = column[i];
  71.             }
  72.         }
  73.     }
  74.     return out_vec;
  75. }
  76.  
  77. auto to_dense_column_major_blocking(std::vector<std::vector<double>> const & vec)
  78.     -> std::vector<double>
  79. {
  80.     auto n_col = vec.size();
  81.     auto n_row = vec[0].size();
  82.     std::vector<double> out_vec(n_col * n_row);
  83.     size_t block_side = 8;
  84.  
  85.     for (size_t l = 0; l < n_col; l += block_side) {
  86.         for (size_t k = 0; k < n_row; k += block_side) {
  87.             for (size_t j = l; j < l + block_side && j < n_col; ++j) {
  88.                 auto const &column = vec[j];
  89.                 for (size_t i = k; i < k + block_side && i < n_row; ++i)
  90.                     out_vec[j * n_row + i] = column[i];
  91.             }
  92.         }
  93.     }
  94.     return out_vec;
  95. }
  96.  
  97. auto make_vecvec() -> std::vector<std::vector<double>>
  98. {
  99.   std::vector<std::vector<double>> vecvec(50, std::vector<double>(4000));
  100.   std::mt19937 mersenne {2019};
  101.   std::uniform_real_distribution<double> dist(-1000, 1000);
  102.   for (auto &vec: vecvec)
  103.    for (auto &val: vec)
  104.        val = dist(mersenne);
  105.   return vecvec;
  106. }
  107.  
  108. static void NaiveColumnMajor(benchmark::State& state) {
  109.   // Code before the loop is not measured
  110.  
  111.   auto vecvec = make_vecvec();
  112.   for (auto _ : state) {
  113.     benchmark::DoNotOptimize(to_dense_column_major_naive(vecvec));
  114.   }
  115. }
  116. BENCHMARK(NaiveColumnMajor);
  117.  
  118. static void NaiveRowMajor(benchmark::State& state) {
  119.   // Code before the loop is not measured
  120.  
  121.   auto vecvec = make_vecvec();
  122.   for (auto _ : state) {
  123.     benchmark::DoNotOptimize(to_dense_row_major_naive(vecvec));
  124.   }
  125. }
  126. BENCHMARK(NaiveRowMajor);
  127.  
  128. static void BlockingRowMajor(benchmark::State& state) {
  129.   // Code before the loop is not measured
  130.  
  131.   auto vecvec = make_vecvec();
  132.   for (auto _ : state) {
  133.     benchmark::DoNotOptimize(to_dense_row_major_blocking(vecvec));
  134.   }
  135. }
  136. BENCHMARK(BlockingRowMajor);
  137.  
  138. static void BlockingColumnMajor(benchmark::State& state) {
  139.   // Code before the loop is not measured
  140.  
  141.   auto vecvec = make_vecvec();
  142.   for (auto _ : state) {
  143.     benchmark::DoNotOptimize(to_dense_column_major_blocking(vecvec));
  144.   }
  145. }
  146. BENCHMARK(BlockingColumnMajor);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top