Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. #include <range/v3/view.hpp>
  2. #include <range/v3/numeric/inner_product.hpp>
  3. #include <chrono>
  4.  
  5. using namespace ranges::view;
  6.  
  7. int main(int argc, char * argv[]) {
  8. size_t N = 3000;
  9. if(argc > 1) N = std::atol(argv[1]);
  10.  
  11. auto matrix = [](size_t n) {
  12. return iota(0ul, n * n) | transform([n, tmp = 1. / n / n](auto x) {
  13. ssize_t i = x / n, j = x % n;
  14. return tmp * (i - j) * (i + j);
  15. });
  16. };
  17.  
  18. auto a = matrix(N), b = matrix(N);
  19.  
  20. auto column = [N](const auto & m, size_t n) {
  21. return slice(m, n, m.size()) | stride(N);
  22. };
  23.  
  24. auto row = [N](const auto & m, size_t n) {
  25. return slice(m, n * N, n * N + N);
  26. };
  27.  
  28. auto mul = [=](const auto & a, const auto & b) {
  29. return iota(0ul, N) | transform([=](auto i) {
  30. return iota(0ul, N) | transform([=](auto j) {
  31. return ranges::inner_product(row(a, i), column(b, j), 0.);
  32. });
  33. });
  34. };
  35.  
  36.  
  37. auto start = std::chrono::high_resolution_clock::now();
  38. auto c = mul(a, b);
  39.  
  40. auto sm=0.0;
  41. for (int i=0; i<N; ++i)
  42. for (int j=0; j<N; ++j)
  43. sm=+c[i][j];
  44.  
  45. auto time = std::chrono::high_resolution_clock::now() - start;
  46. fprintf(stderr, "%luns\n", size_t(std::chrono::duration_cast<std::chrono::nanoseconds>(time).count()));
  47. if (sm != 0)
  48. fprintf(stderr, "%f\n", c[N/2][N/2]);
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement