Extended CUDA Library (ecuda)  2.0
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros
numeric.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 // numeric.hpp
32 //
33 // CUDA implementations from STL header <numeric>.
34 //
35 // Author: Scott D. Zuyderduyn, Ph.D. (scott.zuyderduyn@utoronto.ca)
36 //----------------------------------------------------------------------------
37 
38 #pragma once
39 #ifndef ECUDA_NUMERIC_HPP
40 #define ECUDA_NUMERIC_HPP
41 
42 #include <numeric>
43 
44 #include "global.hpp"
45 #include "iterator.hpp" // for ecuda::iterator_traits
46 #include "type_traits.hpp" // for ecuda::is_same
47 
48 namespace ecuda {
49 
51 namespace impl {
52 
53 template<class InputIterator,typename T,class BinaryOperation>
54 __HOST__ __DEVICE__ inline T accumulate( InputIterator first, InputIterator last, T init, BinaryOperation op, ecuda::true_type )
55 {
56  // is an iterator to device memory
57  #ifdef __CUDA_ARCH__
58  while( first != last ) { init = op(init,*first); ++first; }
59  return init;
60  #else
61  const bool isIteratorContiguous = ecuda::is_same<typename ecuda::iterator_traits<InputIterator>::is_contiguous,ecuda::true_type>::value;
62  ECUDA_STATIC_ASSERT(isIteratorContiguous,CANNOT_ACCUMULATE_RANGE_REPRESENTED_BY_NONCONTIGUOUS_DEVICE_MEMORY);
64  std::vector<typename ecuda::iterator_traits<InputIterator>::value_type> hostVector( n );
65  ecuda::copy( first, last, hostVector.begin() );
66  return std::accumulate( hostVector.begin(), hostVector.end(), init, op );
67  #endif
68 }
69 
70 template<class InputIterator,typename T>
71 __HOST__ __DEVICE__ inline T accumulate( InputIterator first, InputIterator last, T init, ecuda::true_type )
72 {
73  // is an iterator to device memory
74  #ifdef __CUDA_ARCH__
75  while( first != last ) { init += *first; ++first; }
76  return init;
77  #else
78  const bool isIteratorContiguous = ecuda::is_same<typename ecuda::iterator_traits<InputIterator>::is_contiguous,ecuda::true_type>::value;
79  ECUDA_STATIC_ASSERT(isIteratorContiguous,CANNOT_ACCUMULATE_RANGE_REPRESENTED_BY_NONCONTIGUOUS_DEVICE_MEMORY);
81  std::vector<typename ecuda::iterator_traits<InputIterator>::value_type> hostVector( n );
82  ecuda::copy( first, last, hostVector.begin() );
83  return std::accumulate( hostVector.begin(), hostVector.end(), init );
84  #endif
85 }
86 
87 template<class InputIterator,typename T,class BinaryOperation>
88 __HOST__ __DEVICE__ inline T accumulate( InputIterator first, InputIterator last, T init, BinaryOperation op, ecuda::false_type )
89 {
90  // not an iterator to device memory, delegate to STL
91  #ifdef __CUDA_ARCH__
92  return last; // can never be called from device code, dummy return to satisfy nvcc
93  #else
94  return std::accumulate( first, last, init, op );
95  #endif
96 }
97 
98 template<class InputIterator,typename T>
99 __HOST__ __DEVICE__ inline T accumulate( InputIterator first, InputIterator last, T init, ecuda::false_type )
100 {
101  // not an iterator to device memory, delegate to STL
102  #ifdef __CUDA_ARCH__
103  return last; // can never be called from device code, dummy return to satisfy nvcc
104  #else
105  return std::accumulate( first, last, init );
106  #endif
107 }
108 
109 } // namespace impl
111 
121 template<class InputIterator,typename T>
122 __HOST__ __DEVICE__ inline T accumulate( InputIterator first, InputIterator last, T init )
123 {
125 }
126 
142 template<class InputIterator,typename T,class BinaryOperation>
143 __HOST__ __DEVICE__ inline T accumulate( InputIterator first, InputIterator last, T init, BinaryOperation op )
144 {
146 }
147 
148 } // namespace ecuda
149 
150 #endif
__HOST__ __DEVICE__ OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result)
Replacement for std::copy.
Definition: copy.hpp:801
__HOST__ __DEVICE__ T accumulate(InputIterator first, InputIterator last, T init, BinaryOperation op)
Computes the sum of a sequence of elements.
Definition: numeric.hpp:143
#define __HOST__
Definition: global.hpp:150
__HOST__ __DEVICE__ T accumulate(InputIterator first, InputIterator last, T init)
Computes the sum of a sequence of elements.
Definition: numeric.hpp:122
ecuda::false_type is_device_iterator
Definition: iterator.hpp:441
#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