Planetary Orbital Evolution due to Tides
Orbital evolution of two objects experiencing tides
ThreadedInterpolation.cpp
Go to the documentation of this file.
1 
9 #define BUILDING_LIBRARY
10 #include "ThreadedInterpolation.h"
11 
12 namespace StellarEvolution {
13  void *do_interpolation (void *queue)
14  {
15  reinterpret_cast<InterpolationQueue*>(queue)->interpolate_thread();
16  return NULL;
17  }
18 
19  void InterpolationQueue::push_back(const double *x,
20  const double *y,
21  size_t npoints,
22  int nodes,
23  double smoothing,
24  int quantity_id,
25  int grid_index)
26  {
27  __x.push_back(x);
28  __y.push_back(y);
29  __npoints.push_back(npoints);
30  __nodes.push_back(nodes);
31  __smoothing.push_back(smoothing);
32  __quantity_id.push_back(quantity_id);
33  __grid_index.push_back(grid_index);
34  }
35 
37  {
38  while(true) {
39 #ifndef TOOLCHAIN_MSVC
40  pthread_mutex_lock(&__sync_mutex);
41 #endif
42 
43  if(__x.size() == 0) {
44  assert(__y.size() == 0);
45  assert(__npoints.size() == 0);
46  assert(__nodes.size() == 0);
47  assert(__smoothing.size() == 0);
48 #ifdef TOOLCHAIN_MSVC
49  return;
50 #else
51  pthread_mutex_unlock(&__sync_mutex);
52  pthread_exit(NULL);
53 #endif
54  assert(false);
55  }
56 
57  const double *x = __x.back(),
58  *y = __y.back();
59  size_t npoints = __npoints.back();
60  int nodes = __nodes.back();
61  double smoothing = __smoothing.back();
62 
63  __x.pop_back();
64  __y.pop_back();
65  __npoints.pop_back();
66  __nodes.pop_back();
67  __smoothing.pop_back();
68  int destination = __x.size();
69 
70 #ifndef NDEBUG
71  std::clog
72  << "Interpolating "
73  << QUANTITY_NAME[*__quantity_id_iter]
74  << " using " << nodes << " nodes and "
75  << smoothing << " smoothing."
76  << std::endl;
77  ++__quantity_id_iter;
78 #endif
79 
80 #ifndef TOOLCHAIN_MSVC
81  pthread_mutex_unlock(&__sync_mutex);
82 #endif
85  y,
86  npoints,
87  NULL,
88  smoothing,
89  nodes);
90 
91 #ifndef TOOLCHAIN_MSVC
92  pthread_mutex_lock(&__sync_mutex);
93 #endif
94 #ifndef NDEBUG
95  std::clog << "Created result @: " << quantity << std::endl;
96 #endif
97  __result[destination] = quantity;
98 #ifndef TOOLCHAIN_MSVC
99  pthread_mutex_unlock(&__sync_mutex);
100 #endif
101  }
102  }
103 
104  void InterpolationQueue::calculate(unsigned num_threads)
105  {
106 #ifdef TOOLCHAIN_MSVC
107  assert(num_threads == 1);
108 #else
109  pthread_attr_t thread_attributes;
110  pthread_attr_init(&thread_attributes);
111  pthread_attr_setdetachstate(&thread_attributes,
112  PTHREAD_CREATE_JOINABLE);
113  std::vector<pthread_t> threads(num_threads);
114 #endif
115  __result.resize(__x.size());
116 #ifndef NDEBUG
117  __quantity_id_iter = __quantity_id.begin();
118 #endif
119 #ifdef TOOLCHAIN_MSVC
120 #else
121  for(unsigned i = 0; i < num_threads; ++i)
122  pthread_create(&threads[i],
123  &thread_attributes,
124  do_interpolation,
125  reinterpret_cast<void*>(this));
126  for(unsigned i = 0; i < num_threads; ++i)
127  pthread_join(threads[i], NULL);
128 #endif
129  }
130 
132  {
133 #ifndef NDEBUG
134  assert(*this);
135  std::clog << "quantity ID size: " << __quantity_id.size() << std::endl;
136  std::clog << "grid index size: " << __grid_index.size() << std::endl;
137  std::clog << "result size: " << __result.size() << std::endl;
138  std::clog << "Popping result @: " << __result[__quantity_id.size()-1] << std::endl;
139 #endif
140  __quantity_id.pop_front();
141  __grid_index.pop_front();
142  }
143 }//End StellarEvolution namespace.
std::list< const double * > __y
The array of y values to use in the interpolation.
std::list< const double * > __x
The array of x values to use in the interpolation.
Declaration of a class that handles multithreaded stellar evolution interpolation.
std::list< int > __grid_index
The index of the grid point being interpolated.
void interpolate_thread()
Interpolate the first quantity and discard it from __x, __y, __npoints, __nodes and __smoothing...
std::list< int > __quantity_id
The quantity ID of the quantity being interpolated.
void push_back(const double *x, const double *y, size_t npoints, int nodes, double smoothing, int quantity_id, int grid_index)
Add an interpolation taks to the queue.
std::list< size_t > __npoints
The number of points to use in the interpolation.
std::list< int > __nodes
The number of nodes to use in the interpolation.
Function which interpolates, with possible smoothing, between points.
void pop_front()
Move to the next result in the queue (earlier results are no longer accessible.
std::list< double > __smoothing
The smoothing to use in the interpolation.
pthread_mutex_t __sync_mutex
A pthread mutex used to ensure that only one thread is extracting the next quantity for interpolation...
std::vector< Core::InterpolatingFunctionALGLIB * > __result
The interpolation results.