longest_vector.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. // See http://boostorg.github.com/compute for more information.
  9. //---------------------------------------------------------------------------//
  10. #include <iostream>
  11. #include <iterator>
  12. #include <boost/compute/algorithm/max_element.hpp>
  13. #include <boost/compute/container/vector.hpp>
  14. #include <boost/compute/functional/geometry.hpp>
  15. #include <boost/compute/iterator/transform_iterator.hpp>
  16. #include <boost/compute/types/fundamental.hpp>
  17. namespace compute = boost::compute;
  18. // this example shows how to use the max_element() algorithm along with
  19. // a transform_iterator and the length() function to find the longest
  20. // 4-component vector in an array of vectors
  21. int main()
  22. {
  23. using compute::float4_;
  24. // vectors data
  25. float data[] = { 1.0f, 2.0f, 3.0f, 0.0f,
  26. 4.0f, 5.0f, 6.0f, 0.0f,
  27. 7.0f, 8.0f, 9.0f, 0.0f,
  28. 0.0f, 0.0f, 0.0f, 0.0f };
  29. // create device vector with the vector data
  30. compute::vector<float4_> vector(
  31. reinterpret_cast<float4_ *>(data),
  32. reinterpret_cast<float4_ *>(data) + 4
  33. );
  34. // find the longest vector
  35. compute::vector<float4_>::const_iterator iter =
  36. compute::max_element(
  37. compute::make_transform_iterator(
  38. vector.begin(), compute::length<float4_>()
  39. ),
  40. compute::make_transform_iterator(
  41. vector.end(), compute::length<float4_>()
  42. )
  43. ).base();
  44. // print the index of the longest vector
  45. std::cout << "longest vector index: "
  46. << std::distance(vector.begin(), iter)
  47. << std::endl;
  48. return 0;
  49. }