view.cc 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // Adapted from blitz++/examples/array.cpp
  3. // Daniel Llorens - 2015
  4. // TODO Better traversal...
  5. #include "ra/ra.hh"
  6. #include "ra/test.hh"
  7. #include "ra/format.hh"
  8. #include <iomanip>
  9. #include <chrono>
  10. using std::cout, std::endl, std::flush, ra::TestRecorder;
  11. auto now() { return std::chrono::high_resolution_clock::now(); }
  12. template <class DT> auto ms(DT && dt) { return std::chrono::duration_cast<std::chrono::milliseconds>(dt).count(); }
  13. int main()
  14. {
  15. int numIters = 301;
  16. int N = 64;
  17. ra::Big<float, 3> A({N, N, N}, ra::none);
  18. ra::Big<float, 3> B({N, N, N}, ra::none);
  19. ra::Iota<int> interior(N/2, N/4);
  20. // Set up initial conditions: +30 C over an interior block, and +22 C elsewhere
  21. A = 22.;
  22. A(interior, interior, interior) = 30.;
  23. // Note that you don't really need separate I, J, K. You could just use I for every subscript.
  24. ra::Iota<int> I(N-2, 1), J(N-2, 1), K(N-2, 1);
  25. // The views A(...) can be precomputed, but that's only useful if the subscripts are beatable.
  26. {
  27. std::chrono::duration<float> dt(0);
  28. double c = 1/6.5;
  29. for (int i=0; i<numIters; ++i) {
  30. auto t0 = now();
  31. B(I, J, K) = c * (.5 * A(I, J, K) + A(I+1, J, K) + A(I-1, J, K)
  32. + A(I, J+1, K) + A(I, J-1, K) + A(I, J, K+1) + A(I, J, K-1));
  33. A(I, J, K) = c * (.5 * B(I, J, K) + B(I+1, J, K) + B(I-1, J, K)
  34. + B(I, J+1, K) + B(I, J-1, K) + B(I, J, K+1) + B(I, J, K-1));
  35. dt += (now()-t0);
  36. // Output the result along a line through the centre
  37. cout << std::setprecision(4) << std::fixed << ra::noshape << A(N/2, N/2, ra::iota(8, 0, N/8)) << endl;
  38. }
  39. cout << std::setw(10) << std::fixed << (ms(dt)/double(numIters)) << " ms / iter " << endl;
  40. }
  41. ra::Big<float, 3> first_A = A;
  42. A = 22.;
  43. A(interior, interior, interior) = 30.;
  44. // These are always beatable. I+1 and I-1 are also beatable if RA_DO_OPT is #defined to 1, which is the default.
  45. ra::Iota<int> Im(N-2, 0), Ip(N-2, 2);
  46. {
  47. std::chrono::duration<float> dt(0);
  48. double c = 1/6.5;
  49. for (int i=0; i<numIters; ++i) {
  50. auto t0 = now();
  51. B(I, I, I) = c * (.5 * A(I, I, I) + A(Ip, I, I) + A(Im, I, I)
  52. + A(I, Ip, I) + A(I, Im, I) + A(I, I, Ip) + A(I, I, Im));
  53. A(I, I, I) = c * (.5 * B(I, I, I) + B(Ip, I, I) + B(Im, I, I)
  54. + B(I, Ip, I) + B(I, Im, I) + B(I, I, Ip) + B(I, I, Im));
  55. dt += (now()-t0);
  56. // Output the result along a line through the centre
  57. cout << std::setprecision(4) << std::fixed << ra::noshape << A(N/2, N/2, ra::iota(8, 0, N/8)) << endl;
  58. }
  59. cout << std::setw(10) << std::fixed << (ms(dt)/double(numIters)) << " ms / iter " << endl;
  60. }
  61. TestRecorder tr(std::cout);
  62. tr.quiet().test_rel_error(first_A, A, 0.);
  63. return tr.summary();
  64. }