roessler_simd.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Simulation of an ensemble of Roessler attractors using NT2 SIMD library
  3. * This requires the SIMD library headers.
  4. *
  5. * Copyright 2014 Mario Mulansky
  6. *
  7. * Distributed under the Boost Software License, Version 1.0.
  8. * (See accompanying file LICENSE_1_0.txt or
  9. * copy at http://www.boost.org/LICENSE_1_0.txt)
  10. *
  11. */
  12. #include <iostream>
  13. #include <vector>
  14. #include <random>
  15. #include <boost/timer.hpp>
  16. #include <boost/array.hpp>
  17. #include <boost/numeric/odeint.hpp>
  18. #include <boost/simd/sdk/simd/pack.hpp>
  19. #include <boost/simd/sdk/simd/io.hpp>
  20. #include <boost/simd/memory/allocator.hpp>
  21. #include <boost/simd/include/functions/splat.hpp>
  22. #include <boost/simd/include/functions/plus.hpp>
  23. #include <boost/simd/include/functions/multiplies.hpp>
  24. namespace odeint = boost::numeric::odeint;
  25. namespace simd = boost::simd;
  26. typedef boost::timer timer_type;
  27. static const size_t dim = 3; // roessler is 3D
  28. typedef double fp_type;
  29. //typedef float fp_type;
  30. typedef simd::pack<fp_type> simd_pack;
  31. typedef boost::array<simd_pack, dim> state_type;
  32. // use the simd allocator to get properly aligned memory
  33. typedef std::vector< state_type, simd::allocator< state_type > > state_vec;
  34. static const size_t pack_size = simd_pack::static_size;
  35. //---------------------------------------------------------------------------
  36. struct roessler_system {
  37. const fp_type m_a, m_b, m_c;
  38. roessler_system(const fp_type a, const fp_type b, const fp_type c)
  39. : m_a(a), m_b(b), m_c(c)
  40. {}
  41. void operator()(const state_type &x, state_type &dxdt, const fp_type t) const
  42. {
  43. dxdt[0] = -1.0*x[1] - x[2];
  44. dxdt[1] = x[0] + m_a * x[1];
  45. dxdt[2] = m_b + x[2] * (x[0] - m_c);
  46. }
  47. };
  48. //---------------------------------------------------------------------------
  49. int main(int argc, char *argv[]) {
  50. if(argc<3)
  51. {
  52. std::cerr << "Expected size and steps as parameter" << std::endl;
  53. exit(1);
  54. }
  55. const size_t n = atoi(argv[1]);
  56. const size_t steps = atoi(argv[2]);
  57. const fp_type dt = 0.01;
  58. const fp_type a = 0.2;
  59. const fp_type b = 1.0;
  60. const fp_type c = 9.0;
  61. // random initial conditions on the device
  62. std::vector<fp_type> x(n), y(n), z(n);
  63. std::default_random_engine generator;
  64. std::uniform_real_distribution<fp_type> distribution_xy(-8.0, 8.0);
  65. std::uniform_real_distribution<fp_type> distribution_z(0.0, 20.0);
  66. auto rand_xy = std::bind(distribution_xy, std::ref(generator));
  67. auto rand_z = std::bind(distribution_z, std::ref(generator));
  68. std::generate(x.begin(), x.end(), rand_xy);
  69. std::generate(y.begin(), y.end(), rand_xy);
  70. std::generate(z.begin(), z.end(), rand_z);
  71. state_vec state(n/pack_size);
  72. for(size_t i=0; i<n/pack_size; ++i)
  73. {
  74. for(size_t p=0; p<pack_size; ++p)
  75. {
  76. state[i][0][p] = x[i*pack_size+p];
  77. state[i][1][p] = y[i*pack_size+p];
  78. state[i][2][p] = z[i*pack_size+p];
  79. }
  80. }
  81. std::cout << "Systems: " << n << std::endl;
  82. std::cout << "Steps: " << steps << std::endl;
  83. std::cout << "SIMD pack size: " << pack_size << std::endl;
  84. std::cout << state[0][0] << std::endl;
  85. // Stepper type
  86. odeint::runge_kutta4_classic<state_type, fp_type, state_type, fp_type,
  87. odeint::array_algebra, odeint::default_operations,
  88. odeint::never_resizer> stepper;
  89. roessler_system sys(a, b, c);
  90. timer_type timer;
  91. fp_type t = 0.0;
  92. for(int step = 0; step < steps; step++)
  93. {
  94. for(size_t i = 0; i < n/pack_size; ++i)
  95. {
  96. stepper.do_step(sys, state[i], t, dt);
  97. }
  98. t += dt;
  99. }
  100. std::cout.precision(16);
  101. std::cout << "Integration finished, runtime for " << steps << " steps: ";
  102. std::cout << timer.elapsed() << " s" << std::endl;
  103. // compute some accumulation to make sure all results have been computed
  104. simd_pack s_pack = 0.0;
  105. for(size_t i = 0; i < n/pack_size; ++i)
  106. {
  107. s_pack += state[i][0];
  108. }
  109. fp_type s = 0.0;
  110. for(size_t p=0; p<pack_size; ++p)
  111. {
  112. s += s_pack[p];
  113. }
  114. std::cout << state[0][0] << std::endl;
  115. std::cout << s/n << std::endl;
  116. }