array.C 2.9 KB

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