VTK
/Users/kitware/Dashboards/MyTests/VTK_BLD_Release_docs/Utilities/Doxygen/dox/Accelerators/Piston/vtkPistonMinMax.h
Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    vtkPistonMapper.h
00005 
00006   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
00007   All rights reserved.
00008   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
00009 
00010      This software is distributed WITHOUT ANY WARRANTY; without even
00011      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00012      PURPOSE.  See the above copyright notice for more information.
00013 
00014 =========================================================================*/
00022 #ifndef vtkPistonMinMax_h
00023 #define vtkPistonMinMax_h
00024 
00025 #include <thrust/device_vector.h>
00026 #include <thrust/host_vector.h>
00027 #include <thrust/transform_reduce.h>
00028 #include <thrust/functional.h>
00029 #include <thrust/extrema.h>
00030 #include <thrust/random.h>
00031 
00032 namespace vtkPiston
00033 {
00034   // Compute minimum and maximum values in a single reduction
00035 
00036   // minmax_pair stores the minimum and maximum
00037   // values that have been encountered so far
00038   template <typename T>
00039   struct minmax_pair
00040   {
00041     T min_val;
00042     T max_val;
00043   };
00044 
00045   // minmax_unary_op is a functor that takes in a value x and
00046   // returns a minmax_pair whose minimum and maximum values
00047   // are initialized to x.
00048   template <typename T>
00049   struct minmax_unary_op
00050     : public thrust::unary_function< T, minmax_pair<T> >
00051   {
00052     __host__ __device__
00053     minmax_pair<T> operator()(const T& x) const
00054     {
00055       minmax_pair<T> result;
00056       result.min_val = x;
00057       result.max_val = x;
00058       return result;
00059     }
00060   };
00061 
00062   // minmax_binary_op is a functor that accepts two minmax_pair
00063   // structs and returns a new minmax_pair whose minimum and
00064   // maximum values are the min() and max() respectively of
00065   // the minimums and maximums of the input pairs
00066   template <typename T>
00067   struct minmax_binary_op
00068     : public thrust::binary_function< minmax_pair<T>, minmax_pair<T>, minmax_pair<T> >
00069   {
00070     __host__ __device__
00071     minmax_pair<T> operator()(const minmax_pair<T>& x, const minmax_pair<T>& y) const
00072     {
00073       minmax_pair<T> result;
00074       result.min_val = thrust::min(x.min_val, y.min_val);
00075       result.max_val = thrust::max(x.max_val, y.max_val);
00076       return result;
00077     }
00078   };
00079 
00080   template <typename T>
00081   minmax_pair<T> find_min_max(thrust::device_vector<T>* data)
00082   {
00083     // Setup arguments
00084     minmax_unary_op<T>  unary_op;
00085     minmax_binary_op<T> binary_op;
00086 
00087     // Initialize reduction with the first value
00088     minmax_pair<T> init = unary_op((*data)[0]);
00089 
00090     // Compute minimum and maximum values
00091     minmax_pair<T> result = thrust::transform_reduce(
00092       data->begin(), data->end(), unary_op, init, binary_op);
00093 
00094     return result;
00095   }
00096 }
00097 
00098 #endif // vtkPistonMinMax_h
00099 // VTK-HeaderTest-Exclude: vtkPistonMinMax.h