1 | #ifndef OPENFPM_FOR_EACH_HPP_INCLUDED |
2 | #define OPENFPM_FOR_EACH_HPP_INCLUDED |
3 | |
4 | |
5 | // Copyright Aleksey Gurtovoy 2000-2008 |
6 | // |
7 | // Distributed under the Boost Software License, Version 1.0. |
8 | // (See accompanying file LICENSE_1_0.txt or copy at |
9 | // http://www.boost.org/LICENSE_1_0.txt) |
10 | // |
11 | // See http://www.boost.org/libs/mpl for documentation. |
12 | |
13 | // $Id: for_each.hpp 55648 2009-08-18 05:16:53Z agurtovoy $ |
14 | // $Date: 2009-08-17 22:16:53 -0700 (Mon, 17 Aug 2009) $ |
15 | // $Revision: 55648 $ |
16 | // Changed from Pietro incardona to handle reference functor |
17 | |
18 | #include "util/cuda_util.hpp" |
19 | #include <boost/mpl/is_sequence.hpp> |
20 | #include <boost/mpl/begin_end.hpp> |
21 | #include <boost/mpl/apply.hpp> |
22 | #include <boost/mpl/bool.hpp> |
23 | #include <boost/mpl/next_prior.hpp> |
24 | #include <boost/mpl/deref.hpp> |
25 | #include <boost/mpl/identity.hpp> |
26 | #include <boost/mpl/assert.hpp> |
27 | #include <boost/mpl/aux_/unwrap.hpp> |
28 | |
29 | #include <boost/type_traits/is_same.hpp> |
30 | #include <boost/utility/value_init.hpp> |
31 | |
32 | #if defined(__NVCC__) && !defined(__INTEL_COMPILER) |
33 | #pragma hd_warning_disable |
34 | #endif |
35 | |
36 | namespace boost { namespace mpl { |
37 | |
38 | namespace aux { |
39 | |
40 | template< bool done = true > |
41 | struct for_each_ref_impl |
42 | { |
43 | template< |
44 | typename Iterator |
45 | , typename LastIterator |
46 | , typename TransformFunc |
47 | , typename F |
48 | > |
49 | BOOST_GPU_ENABLED static void execute( |
50 | Iterator* |
51 | , LastIterator* |
52 | , TransformFunc* |
53 | , F & |
54 | ) |
55 | { |
56 | } |
57 | }; |
58 | |
59 | template<> |
60 | struct for_each_ref_impl<false> |
61 | { |
62 | template< |
63 | typename Iterator |
64 | , typename LastIterator |
65 | , typename TransformFunc |
66 | , typename F |
67 | > |
68 | BOOST_GPU_ENABLED static void execute( |
69 | Iterator* |
70 | , LastIterator* |
71 | , TransformFunc* |
72 | , F & f |
73 | ) |
74 | { |
75 | typedef typename deref<Iterator>::type item; |
76 | typedef typename apply1<TransformFunc,item>::type arg; |
77 | |
78 | // dwa 2002/9/10 -- make sure not to invoke undefined behavior |
79 | // when we pass arg. |
80 | value_initialized<arg> x; |
81 | aux::unwrap(f, 0)(boost::get(x)); |
82 | |
83 | typedef typename mpl::next<Iterator>::type iter; |
84 | for_each_ref_impl<boost::is_same<iter,LastIterator>::value> |
85 | ::execute( static_cast<iter*>(0), static_cast<LastIterator*>(0), static_cast<TransformFunc*>(0), f); |
86 | } |
87 | }; |
88 | |
89 | template< bool done = true > |
90 | struct for_each_ref_cpu_impl |
91 | { |
92 | template< |
93 | typename Iterator |
94 | , typename LastIterator |
95 | , typename TransformFunc |
96 | , typename F |
97 | > |
98 | static void execute( |
99 | Iterator* |
100 | , LastIterator* |
101 | , TransformFunc* |
102 | , F & |
103 | ) |
104 | { |
105 | } |
106 | }; |
107 | |
108 | template<> |
109 | struct for_each_ref_cpu_impl<false> |
110 | { |
111 | template< |
112 | typename Iterator |
113 | , typename LastIterator |
114 | , typename TransformFunc |
115 | , typename F |
116 | > |
117 | static void execute( |
118 | Iterator* |
119 | , LastIterator* |
120 | , TransformFunc* |
121 | , F & f |
122 | ) |
123 | { |
124 | typedef typename deref<Iterator>::type item; |
125 | typedef typename apply1<TransformFunc,item>::type arg; |
126 | |
127 | // dwa 2002/9/10 -- make sure not to invoke undefined behavior |
128 | // when we pass arg. |
129 | value_initialized<arg> x; |
130 | aux::unwrap(f, 0)(boost::get(x)); |
131 | |
132 | typedef typename mpl::next<Iterator>::type iter; |
133 | for_each_ref_impl<boost::is_same<iter,LastIterator>::value> |
134 | ::execute( static_cast<iter*>(0), static_cast<LastIterator*>(0), static_cast<TransformFunc*>(0), f); |
135 | } |
136 | }; |
137 | |
138 | } // namespace aux |
139 | |
140 | // agurt, 17/mar/02: pointer default parameters are necessary to workaround |
141 | // MSVC 6.5 function template signature's mangling bug |
142 | template< |
143 | typename Sequence |
144 | , typename TransformOp |
145 | , typename F |
146 | > |
147 | BOOST_GPU_ENABLED inline void for_each_ref(F & f, Sequence* = 0, TransformOp* = 0) |
148 | { |
149 | BOOST_MPL_ASSERT(( is_sequence<Sequence> )); |
150 | |
151 | typedef typename begin<Sequence>::type first; |
152 | typedef typename end<Sequence>::type last; |
153 | |
154 | aux::for_each_ref_impl< boost::is_same<first,last>::value > |
155 | ::execute(static_cast<first*>(0), static_cast<last*>(0), static_cast<TransformOp*>(0), f); |
156 | } |
157 | |
158 | template< |
159 | typename Sequence |
160 | , typename F |
161 | > |
162 | BOOST_GPU_ENABLED inline |
163 | void for_each_ref(F & f, Sequence* = 0) |
164 | { |
165 | for_each_ref<Sequence, identity<> >(f); |
166 | } |
167 | |
168 | // agurt, 17/mar/02: pointer default parameters are necessary to workaround |
169 | // MSVC 6.5 function template signature's mangling bug |
170 | template< |
171 | typename Sequence |
172 | , typename TransformOp |
173 | , typename F |
174 | > |
175 | inline void for_each_cpu_ref(F & f, Sequence* = 0, TransformOp* = 0) |
176 | { |
177 | BOOST_MPL_ASSERT(( is_sequence<Sequence> )); |
178 | |
179 | typedef typename begin<Sequence>::type first; |
180 | typedef typename end<Sequence>::type last; |
181 | |
182 | aux::for_each_ref_impl< boost::is_same<first,last>::value > |
183 | ::execute(static_cast<first*>(0), static_cast<last*>(0), static_cast<TransformOp*>(0), f); |
184 | } |
185 | |
186 | template< |
187 | typename Sequence |
188 | , typename F |
189 | > |
190 | inline |
191 | void for_each_cpu_ref(F & f, Sequence* = 0) |
192 | { |
193 | for_each_ref<Sequence, identity<> >(f); |
194 | } |
195 | |
196 | }} |
197 | |
198 | #endif // OPENFPM_FOR_EACH_HPP_INCLUDED |
199 | |