OpenFPM  5.2.0
Project that contain the implementation of distributed structures
rk4-odeint-cuda.cu
1 #include "config.h"
2 #include <type_traits>
3 #include <cstring>
4 #include "util/common.hpp"
5 #include <iostream>
6 #include "Operators/Vector/vector_dist_operators.hpp"
7 #include "OdeIntegrators/OdeIntegrators.hpp"
8 
9 //#include "DCPSE/DCPSE_op/DCPSE_op.hpp"
10 
11 typedef state_type_1d_ofp_gpu state_type;
12 
13 
14 void ExponentialGPU( const state_type &x , state_type &dxdt , const double t )
15 {
16  std::cout << "Work " << t << std::endl;
17  dxdt.data.get<0>() = x.data.get<0>();
18 }
19 
20 
21 int main(int argc, char* argv[])
22  {
23  openfpm_init(&argc,&argv);
24  size_t edgeSemiSize = int(std::atof(argv[1]));
25  double tf = std::atof(argv[2]);
26  const size_t sz[2] = {edgeSemiSize,edgeSemiSize };
27  Box<2, double> box({ 0, 0 }, { 1.0, 1.0 });
28  size_t bc[2] = { NON_PERIODIC, NON_PERIODIC };
29  double spacing[2];
30  spacing[0] = 1.0 / (sz[0] - 1);
31  spacing[1] = 1.0 / (sz[1] - 1);
32  double rCut = 3.9 * spacing[0];
33  Ghost<2, double> ghost(rCut);
34 
36 
37  auto it = Particles.getGridIterator(sz);
38  while (it.isNext())
39  {
40  Particles.add();
41  auto key = it.get();
42  mem_id k0 = key.get(0);
43  double xp0 = k0 * spacing[0];
44  Particles.getLastPos()[0] = xp0;
45  mem_id k1 = key.get(1);
46  double yp0 = k1 * spacing[1];
47  Particles.getLastPos()[1] = yp0;
48  Particles.getLastProp<0>() = xp0*yp0*exp(-5);
49  Particles.getLastProp<1>() = xp0*yp0*exp(5);
50  ++it;
51  }
52 
53  Particles.map();
54  Particles.ghost_get<0>();
55  Particles.hostToDeviceProp<0,1,2>();
56  auto Init = getV<0,comp_dev>(Particles);
57  auto Sol = getV<1,comp_dev>(Particles);
58  auto OdeSol = getV<2,comp_dev>(Particles);
59 
60  state_type x0;
61  x0.data.get<0>()=Init;
62  x0.data.get<0>().getVector().deviceToHost<0>();
63  // The rhs of x' = f(x)
64 
65  double t0=-5;
66  const double dt=0.01;
67 
68  timer tt;
69  tt.start();
70  size_t steps=boost::numeric::odeint::integrate_const( boost::numeric::odeint::runge_kutta4< state_type, double, state_type, double, boost::numeric::odeint::vector_space_algebra_ofp_gpu,boost::numeric::odeint::ofp_operations>(),ExponentialGPU,x0,t0,tf,dt);
71  tt.stop();
72  OdeSol=x0.data.get<0>();
73  Particles.deviceToHostProp<0,1,2>();
74  auto it2 = Particles.getDomainIterator();
75  double worst = 0.0;
76  while (it2.isNext()) {
77  auto p = it2.get();
78  if (fabs(Particles.getProp<1>(p) - Particles.getProp<2>(p)) > worst) {
79  worst = fabs(Particles.getProp<1>(p) - Particles.getProp<2>(p));
80  }
81  ++it2;
82  }
83  std::cout<<"WCT:"<<tt.getwct()<<std::endl;
84  std::cout<<"CPU:"<<tt.getcputime()<<std::endl;
85  std::cout<<worst<<std::endl;
86  openfpm_finalize();
87  return 0;
88 
89 }
This class represent an N-dimensional box.
Definition: Box.hpp:60
Definition: Ghost.hpp:40
Class for cpu time benchmarking.
Definition: timer.hpp:28
void stop()
Stop the timer.
Definition: timer.hpp:119
double getcputime()
Return the cpu time.
Definition: timer.hpp:142
void start()
Start the timer.
Definition: timer.hpp:90
double getwct()
Return the elapsed real time.
Definition: timer.hpp:130
Main class that encapsulate a vector properties operand to be used for expressions construction.
Distributed vector.
KeyT const ValueT ValueT OffsetIteratorT OffsetIteratorT int
[in] The number of segments that comprise the sorting data