23 #include <thrust/device_vector.h> 24 #include <thrust/transform.h> 25 #include <thrust/sequence.h> 26 #include <thrust/copy.h> 27 #include <thrust/fill.h> 28 #include <thrust/replace.h> 29 #include <thrust/functional.h> 30 #include <thrust/iterator/zip_iterator.h> 31 #include <thrust/tuple.h> 32 #include <thrust/for_each.h> 33 #include <thrust/transform_reduce.h> 34 #include <thrust/extrema.h> 47 static const int SIGN_PLUS = 0;
48 static const int SIGN_MINUS = 1;
49 static const int SIGN_EQUALS = 2;
52 #define CONST_ARRAY_SIZE 512 53 #define BLOCK_SIZE (64) 78 dualBoxConstraintProx,
104 template <
typename T >
107 return x > 0 ? x : -x;
110 template <
typename T >
113 return a > b ? b : a;
116 template <
typename T >
119 return a < b ? b : a;
122 double pow2(
double x)
131 template <
typename T >
132 T vectorProduct(
const std::vector<T> &v)
134 return std::accumulate(v.begin(), v.end(), 1, std::multiplies<T>());
139 template <
typename T >
140 T vectorSum(
const std::vector<T> &v)
142 return std::accumulate(v.begin(), v.end(), (T)0);
145 template <
typename T >
146 float vectorMax(std::vector<T> &v)
148 return *std::max_element(v.begin(), v.end());
151 template <
typename T >
152 void vectorScalarProduct(std::vector<T> &v,T scalarValue)
154 std::transform(v.begin(), v.end(), v.begin(), [scalarValue](T x) {
return scalarValue*x;});
157 template <
typename T >
158 void vectorScalarSet(std::vector<T> &v,
const T scalarValue)
160 std::fill(v.begin(), v.end(), scalarValue);
163 template <
typename T >
164 void vectorPlus(std::vector<T> &v1, std::vector<T> &v2)
166 std::transform(v1.begin(), v1.end(), v2.begin(), v1.begin(), std::plus<T>());
169 template <
typename T >
170 void vectorMinus(std::vector<T> &v1, std::vector<T> &v2)
172 std::transform(v1.begin(), v1.end(), v2.begin(), v1.begin(), std::minus<T>());
175 template <
typename T >
176 void vectorAbs(std::vector<T> &v)
178 std::transform(v.begin(), v.end(), v.begin(), [](T x) {
return std::abs(x); });
182 template <
typename T >
183 void doOverrelaxation(std::vector<T> &x, std::vector<T> &xOld, std::vector<T> &xBar)
185 std::transform(x.begin(), x.end(), xOld.begin(), xBar.begin(), [](T x, T y) {
return x + x - y; });
188 template <
typename T >
189 void vectorPow2(std::vector<T> &v)
191 std::transform(v.begin(), v.end(), v.begin(), [](T x) {
return x *x ; });
194 template <
typename T >
195 void vectorAddVectorTimesVector(std::vector<T> &result,
const std::vector<T> &v1,
const std::vector<T> &v2,
const int signRule)
201 int numElements = (int)result.size();
202 for (
int i = 0; i < numElements; ++i)
204 result[i] += v1[i] * v2[i];
210 int numElements = (int)result.size();
211 for (
int i = 0; i < numElements; ++i)
213 result[i] -= v1[i] * v2[i];
219 int numElements = (int)result.size();
220 for (
int i = 0; i < numElements; ++i)
222 result[i] = v1[i] * v2[i];
238 Timer() : t_begin(std::chrono::system_clock::now()), isStopped(
false)
246 cudaDeviceSynchronize();
248 t_begin = std::chrono::system_clock::now();
256 cudaDeviceSynchronize();
258 t_end = std::chrono::system_clock::now();
271 std::chrono::duration<double> diff = t_end - t_begin;
280 std::chrono::system_clock::time_point t_begin;
281 std::chrono::system_clock::time_point t_end;
290 template <
typename T >
291 void calculateXYError(thrust::device_vector<T> &x, thrust::device_vector<T> &xOld, thrust::device_vector<T> &xError, T tau)
293 thrust::transform(x.begin(), x.end(), xOld.begin(), xError.begin(), (thrust::placeholders::_1 - thrust::placeholders::_2) / tau);
296 template <
typename T >
304 template <
typename T >
311 T operator()(T x)
const {
return abs(x); }
314 template <
typename T >
315 void vectorAbs(thrust::device_vector<T> &v)
317 thrust::transform(v.begin(), v.end(), v.begin(),
myAbsGPU<T>());
320 template <
typename T >
324 return a > b ? b : a;
327 __device__
float myMinGPUf(
float a,
float b)
329 return a > b ? b : a;
332 __device__
float myMaxGPUf(
float a,
float b)
334 return a > b ? a : b;
337 template <
typename T >
341 return a < b ? b : a;
344 template <
typename T >
345 T vectorSum(thrust::device_vector<T> &v)
347 return thrust::reduce(v.begin(), v.end(), (T)0, thrust::plus<T>());
351 template <
typename T >
352 void vectorScalarSet(thrust::device_vector<T> &v, T scalarValue)
354 thrust::fill(v.begin(), v.end(), scalarValue);
357 template <
typename T >
358 void vectorScalarProduct(thrust::device_vector<T> &v,
const T scalarValue)
360 thrust::transform(v.begin(), v.end(), v.begin(), scalarValue * thrust::placeholders::_1);
363 template <
typename T >
364 void vectorMinus(thrust::device_vector<T> &v1, thrust::device_vector<T> &v2)
366 thrust::transform(v1.begin(), v1.end(), v2.begin(), v1.begin(), thrust::minus<T>());
369 template <
typename T >
370 void vectorAddSquared(thrust::device_vector<T> &v1, thrust::device_vector<T> &v2)
372 thrust::transform(v1.begin(), v1.end(), v2.begin(), v1.begin(), thrust::placeholders::_1 + thrust::placeholders::_2*thrust::placeholders::_2);
381 template <
typename Tuple>
383 void operator()(Tuple t)
389 thrust::get<0>(t) += thrust::get<1>(t) * thrust::get<2>(t);
394 thrust::get<0>(t) -= thrust::get<1>(t) * thrust::get<2>(t);
399 thrust::get<0>(t) = thrust::get<1>(t) * thrust::get<2>(t);
408 template <
typename T >
409 void vectorAddVectorTimesVector(thrust::device_vector<T> &result,
const thrust::device_vector<T> &v1,
const thrust::device_vector<T> &v2,
const int signRule)
412 thrust::make_zip_iterator(thrust::make_tuple(result.begin(), v1.begin(), v2.begin())),
413 thrust::make_zip_iterator(thrust::make_tuple(result.end(), v1.end(), v2.end())),
418 template <
typename T >
425 template <
typename T >
426 void vectorSqrt(thrust::device_vector<T> &v1)
428 thrust::transform(v1.begin(), v1.end(), v1.begin(), sqrtGPU<T>());
431 template <
typename T >
432 float vectorMax(thrust::device_vector<T> &v)
434 return *thrust::max_element(v.begin(), v.end());
thrust functor for elemntwise multiplication of two vectors following a summation of the result on a ...
Definition: tools.h:376
class for timing execution times
Definition: tools.h:235
thrust functor for calculating the absolute value of vector
Definition: tools.h:305
void end()
ends the timer
Definition: tools.h:253
double elapsed() const
returns the duration
Definition: tools.h:267
void reset()
resets or starts the timer
Definition: tools.h:243