Extended CUDA Library (ecuda)  2.0
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros
max_element.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2015, Scott Zuyderduyn
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 
8 1. Redistributions of source code must retain the above copyright notice, this
9  list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation
12  and/or other materials provided with the distribution.
13 
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 
25 The views and conclusions contained in the software and documentation are those
26 of the authors and should not be interpreted as representing official policies,
27 either expressed or implied, of the FreeBSD Project.
28 */
29 
30 //----------------------------------------------------------------------------
31 // algo/max_element.hpp
32 //
33 // Extension of std::max_element that recognizes device memory and can be
34 // called from host or device code.
35 //
36 // Author: Scott D. Zuyderduyn, Ph.D. (scott.zuyderduyn@utoronto.ca)
37 //----------------------------------------------------------------------------
38 #pragma once
39 #ifndef ECUDA_ALGO_MAX_ELEMENT_HPP
40 #define ECUDA_ALGO_MAX_ELEMENT_HPP
41 
42 #include <algorithm>
43 #include <vector>
44 
45 #include "../global.hpp"
46 #include "../iterator.hpp"
47 
48 namespace ecuda {
49 
50 // forward declaration
51 template<class ForwardIterator> __HOST__ __DEVICE__ inline ForwardIterator max_element( ForwardIterator first, ForwardIterator last );
52 
54 namespace impl {
55 
56 template<class ForwardIterator>
57 __HOST__ __DEVICE__ inline ForwardIterator max_element( ForwardIterator first, ForwardIterator last, ecuda::true_type ) {
58  // is an iterator to device memory
59  #ifdef __CUDA_ARCH__
60  if( first == last ) return last;
61  ForwardIterator largest = first;
62  ++first;
63  for( ; first != last; ++first ) {
64  if( *largest < *first ) largest = first;
65  }
66  return largest;
67  #else
68  const bool isIteratorContiguous = ecuda::is_same<typename ecuda::iterator_traits<ForwardIterator>::is_contiguous,ecuda::true_type>::value;
69  ECUDA_STATIC_ASSERT(isIteratorContiguous,CANNOT_FIND_MAX_ELEMENT_IN_RANGE_REPRESENTED_BY_NONCONTIGUOUS_DEVICE_MEMORY);
71  std::vector<typename ecuda::iterator_traits<ForwardIterator>::value_type> hostVector( n );
72  ecuda::copy( first, last, hostVector.begin() );
73  const std::size_t index = std::max_element( hostVector.begin(), hostVector.end() ) - hostVector.begin();
74  first += index;
75  return first;
76  #endif
77 }
78 
79 
80 template<class ForwardIterator>
81 __HOST__ __DEVICE__ inline ForwardIterator max_element( ForwardIterator first, ForwardIterator last, ecuda::false_type ) {
82  // not an iterator to device memory, delegate to STL
83  #ifdef __CUDA_ARCH__
84  return last; // can never be called from device code, dummy return to satisfy nvcc
85  #else
86  return std::max_element( first, last );
87  #endif
88 }
89 
90 } // namespace impl
92 
93 template<class ForwardIterator>
94 __HOST__ __DEVICE__ inline ForwardIterator max_element( ForwardIterator first, ForwardIterator last ) {
96  first, last,
98  );
99 }
100 
101 } // namespace ecuda
102 
103 #endif
__HOST__ __DEVICE__ OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result)
Replacement for std::copy.
Definition: copy.hpp:801
#define __HOST__
Definition: global.hpp:150
ecuda::false_type is_device_iterator
Definition: iterator.hpp:441
__HOST__ __DEVICE__ ForwardIterator max_element(ForwardIterator first, ForwardIterator last)
Definition: max_element.hpp:94
#define ECUDA_STATIC_ASSERT(x, msg)
Definition: global.hpp:191
base_type::difference_type difference_type
Definition: iterator.hpp:436
#define __DEVICE__
Definition: global.hpp:151
ECUDA_SUPPRESS_HD_WARNINGS __HOST__ __DEVICE__ std::iterator_traits< Iterator >::difference_type distance(const Iterator &first, const Iterator &last)
Definition: iterator.hpp:627