Advertisement
SepandMeenu

Comparison of Eigen with raw implementation

Jun 18th, 2024
471
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <chrono>
  3.  
  4. #define EIGEN_NO_DEBUG
  5. #include <Eigen/Dense>
  6.  
  7. void meanStd(const double data[],
  8.              const std::size_t dataSize, const std::size_t dataElementSize,
  9.              double mean[])
  10. {
  11.     for (std::size_t idxElement = 0; idxElement < dataElementSize; ++idxElement)
  12.         mean[idxElement] = 0;
  13.  
  14.     for (std::size_t idxData = 0, offset = 0; idxData < dataSize;
  15.          ++idxData, offset += dataElementSize) {
  16.         const double* const dataElm = data + offset;
  17.         for (std::size_t idxElement = 0; idxElement < dataElementSize; ++idxElement)
  18.             mean[idxElement] += dataElm[idxElement];
  19.     }
  20.  
  21.     for (std::size_t idxElement = 0; idxElement < dataElementSize; ++idxElement)
  22.         mean[idxElement] /= dataSize;
  23. }
  24.  
  25.  
  26. void meanStdEigen(const double data[], const std::size_t dataSize,
  27.                   const std::size_t dataElementSize,
  28.                   double mean[/* dataElementSize */])
  29. {
  30.     Eigen::Map<const Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>>
  31.         dataMat(data, dataElementSize, dataSize);
  32.  
  33.     Eigen::Map<Eigen::Vector<double, Eigen::Dynamic>>
  34.     meanMat(mean, dataElementSize);
  35.  
  36.     meanMat = dataMat.rowwise().mean();
  37. }
  38.  
  39.  
  40. int main()
  41. {
  42.     const std::size_t dataElementSize = 1000,
  43.         dataSize = 30000,
  44.         a_size = dataSize * dataElementSize,
  45.         N_TIMES = 100;
  46.  
  47.     // Generate random data
  48.     double* data = new double[a_size];
  49.     for (std::size_t i = 0; i < a_size; ++i)
  50.         data[i] = i + 1;
  51.  
  52.     double mean[dataElementSize];
  53.  
  54.     // measure execution time of the raw implementation
  55.     auto startOriginal = std::chrono::high_resolution_clock::now();
  56.     for (std::size_t i = 0; i < N_TIMES; ++i)
  57.         meanStd(data, dataSize, dataElementSize, mean);
  58.     auto endOriginal = std::chrono::high_resolution_clock::now();
  59.     std::chrono::duration<double> elapsedOriginal = endOriginal - startOriginal;
  60.     float elapsed = elapsedOriginal.count() / N_TIMES;
  61.  
  62.     printf("Low-level) elapsed = %0.3e s\n", elapsed);
  63.  
  64.     // measure execution time of the Eigen-based implementation
  65.     startOriginal = std::chrono::high_resolution_clock::now();
  66.     for (std::size_t i = 0; i < N_TIMES; ++i)
  67.         meanStdEigen(data, dataSize, dataElementSize, mean);
  68.     endOriginal = std::chrono::high_resolution_clock::now();
  69.     std::chrono::duration<double> elapsedOriginal_Eigen = endOriginal - startOriginal;
  70.     float elapsed_Eigen = elapsedOriginal_Eigen.count() / N_TIMES;
  71.     printf("Eigen) elapsed = %0.3e s\n", elapsed_Eigen);
  72.  
  73.     printf("> comparison of elapsed times: t_Eigen/t_raw = %0.1f s\n", elapsed_Eigen / elapsed);
  74.  
  75.     delete[] data;
  76.     return 0;
  77. }
  78.  
  79.  
  80. // g++ -Wall -Wpedantic -O3 -DNDEBUG -std=c++17 -I"/usr/include/eigen3" test_meanstd.cpp -o test_meanstd.out
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement