blob: d79af4d8bf5b5481066f6c1657acb85099cbe0cc [file] [log] [blame]
Brian Silverman7c33ab22018-08-04 17:14:51 -07001/*
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
13
14#include <iostream>
15#include <vector>
16#include <random>
17
18#include <boost/timer.hpp>
19#include <boost/array.hpp>
20
21#include <boost/numeric/odeint.hpp>
22#include <boost/simd/sdk/simd/pack.hpp>
23#include <boost/simd/sdk/simd/io.hpp>
24#include <boost/simd/memory/allocator.hpp>
25#include <boost/simd/include/functions/splat.hpp>
26#include <boost/simd/include/functions/plus.hpp>
27#include <boost/simd/include/functions/multiplies.hpp>
28
29
30namespace odeint = boost::numeric::odeint;
31namespace simd = boost::simd;
32
33typedef boost::timer timer_type;
34
35static const size_t dim = 3; // roessler is 3D
36
37typedef double fp_type;
38//typedef float fp_type;
39
40typedef simd::pack<fp_type> simd_pack;
41typedef boost::array<simd_pack, dim> state_type;
42// use the simd allocator to get properly aligned memory
43typedef std::vector< state_type, simd::allocator< state_type > > state_vec;
44
45static const size_t pack_size = simd_pack::static_size;
46
47//---------------------------------------------------------------------------
48struct roessler_system {
49 const fp_type m_a, m_b, m_c;
50
51 roessler_system(const fp_type a, const fp_type b, const fp_type c)
52 : m_a(a), m_b(b), m_c(c)
53 {}
54
55 void operator()(const state_type &x, state_type &dxdt, const fp_type t) const
56 {
57 dxdt[0] = -1.0*x[1] - x[2];
58 dxdt[1] = x[0] + m_a * x[1];
59 dxdt[2] = m_b + x[2] * (x[0] - m_c);
60 }
61};
62
63//---------------------------------------------------------------------------
64int main(int argc, char *argv[]) {
65if(argc<3)
66{
67 std::cerr << "Expected size and steps as parameter" << std::endl;
68 exit(1);
69}
70const size_t n = atoi(argv[1]);
71const size_t steps = atoi(argv[2]);
72
73const fp_type dt = 0.01;
74
75const fp_type a = 0.2;
76const fp_type b = 1.0;
77const fp_type c = 9.0;
78
79// random initial conditions on the device
80std::vector<fp_type> x(n), y(n), z(n);
81std::default_random_engine generator;
82std::uniform_real_distribution<fp_type> distribution_xy(-8.0, 8.0);
83std::uniform_real_distribution<fp_type> distribution_z(0.0, 20.0);
84auto rand_xy = std::bind(distribution_xy, std::ref(generator));
85auto rand_z = std::bind(distribution_z, std::ref(generator));
86std::generate(x.begin(), x.end(), rand_xy);
87std::generate(y.begin(), y.end(), rand_xy);
88std::generate(z.begin(), z.end(), rand_z);
89
90state_vec state(n/pack_size);
91for(size_t i=0; i<n/pack_size; ++i)
92{
93 for(size_t p=0; p<pack_size; ++p)
94 {
95 state[i][0][p] = x[i*pack_size+p];
96 state[i][1][p] = y[i*pack_size+p];
97 state[i][2][p] = z[i*pack_size+p];
98 }
99}
100
101std::cout << "Systems: " << n << std::endl;
102std::cout << "Steps: " << steps << std::endl;
103std::cout << "SIMD pack size: " << pack_size << std::endl;
104
105std::cout << state[0][0] << std::endl;
106
107// Stepper type
108odeint::runge_kutta4_classic<state_type, fp_type, state_type, fp_type,
109 odeint::array_algebra, odeint::default_operations,
110 odeint::never_resizer> stepper;
111
112roessler_system sys(a, b, c);
113
114timer_type timer;
115
116fp_type t = 0.0;
117
118for(int step = 0; step < steps; step++)
119{
120 for(size_t i = 0; i < n/pack_size; ++i)
121 {
122 stepper.do_step(sys, state[i], t, dt);
123 }
124 t += dt;
125}
126
127std::cout.precision(16);
128
129std::cout << "Integration finished, runtime for " << steps << " steps: ";
130std::cout << timer.elapsed() << " s" << std::endl;
131
132// compute some accumulation to make sure all results have been computed
133simd_pack s_pack = 0.0;
134for(size_t i = 0; i < n/pack_size; ++i)
135{
136 s_pack += state[i][0];
137}
138
139fp_type s = 0.0;
140for(size_t p=0; p<pack_size; ++p)
141{
142 s += s_pack[p];
143}
144
145
146std::cout << state[0][0] << std::endl;
147std::cout << s/n << std::endl;
148
149}