2 * Copyright 2008-2013 NVIDIA Corporation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 /*! \file uninitialized_copy.h
19 * \brief Copy construction into a range of uninitialized elements from a source range
24 #include <thrust/detail/config.h>
25 #include <thrust/detail/execution_policy.h>
31 /*! \addtogroup copying
36 /*! In \c thrust, the function \c thrust::device_new allocates memory for
37 * an object and then creates an object at that location by calling a constructor.
38 * Occasionally, however, it is useful to separate those two operations.
39 * If each iterator in the range <tt>[result, result + (last - first))</tt> points
40 * to uninitialized memory, then \p uninitialized_copy creates a copy of
41 * <tt>[first, last)</tt> in that range. That is, for each iterator \c i in
42 * the input, \p uninitialized_copy creates a copy of \c *i in the location pointed
43 * to by the corresponding iterator in the output range by \p ForwardIterator's
44 * \c value_type's copy constructor with *i as its argument.
46 * The algorithm's execution is parallelized as determined by \p exec.
48 * \param exec The execution policy to use for parallelization.
49 * \param first The first element of the input range to copy from.
50 * \param last The last element of the input range to copy from.
51 * \param result The first element of the output range to copy to.
52 * \return An iterator pointing to the last element of the output range.
54 * \tparam DerivedPolicy The name of the derived execution policy.
55 * \tparam InputIterator is a model of <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a>.
56 * \tparam ForwardIterator is a model of <a href="http://www.sgi.com/tech/stl/ForwardIterator.html">Forward Iterator</a>,
57 * \p ForwardIterator is mutable, and \p ForwardIterator's \c value_type has a constructor that takes
58 * a single argument whose type is \p InputIterator's \c value_type.
60 * \pre \p first may equal \p result, but the range <tt>[first, last)</tt> and the range <tt>[result, result + (last - first))</tt> shall not overlap otherwise.
62 * The following code snippet demonstrates how to use \p uninitialized_copy to initialize
63 * a range of uninitialized memory using the \p thrust::device execution policy for
67 * #include <thrust/uninitialized_copy.h>
68 * #include <thrust/device_malloc.h>
69 * #include <thrust/device_vector.h>
70 * #include <thrust/execution_policy.h>
75 * Int(int x) : val(x) {}
82 * thrust::device_vector<Int> input(N, val);
83 * thrust::device_ptr<Int> array = thrust::device_malloc<Int>(N);
84 * thrust::uninitialized_copy(thrust::device, input.begin(), input.end(), array);
86 * // Int x = array[i];
87 * // x.val == 46 for all 0 <= i < N
90 * \see http://www.sgi.com/tech/stl/uninitialized_copy.html
92 * \see \c uninitialized_fill
94 * \see \c device_malloc
96 template<typename DerivedPolicy, typename InputIterator, typename ForwardIterator>
97 ForwardIterator uninitialized_copy(const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
100 ForwardIterator result);
103 /*! In \c thrust, the function \c thrust::device_new allocates memory for
104 * an object and then creates an object at that location by calling a constructor.
105 * Occasionally, however, it is useful to separate those two operations.
106 * If each iterator in the range <tt>[result, result + (last - first))</tt> points
107 * to uninitialized memory, then \p uninitialized_copy creates a copy of
108 * <tt>[first, last)</tt> in that range. That is, for each iterator \c i in
109 * the input, \p uninitialized_copy creates a copy of \c *i in the location pointed
110 * to by the corresponding iterator in the output range by \p ForwardIterator's
111 * \c value_type's copy constructor with *i as its argument.
113 * \param first The first element of the input range to copy from.
114 * \param last The last element of the input range to copy from.
115 * \param result The first element of the output range to copy to.
116 * \return An iterator pointing to the last element of the output range.
118 * \tparam InputIterator is a model of <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a>.
119 * \tparam ForwardIterator is a model of <a href="http://www.sgi.com/tech/stl/ForwardIterator.html">Forward Iterator</a>,
120 * \p ForwardIterator is mutable, and \p ForwardIterator's \c value_type has a constructor that takes
121 * a single argument whose type is \p InputIterator's \c value_type.
123 * \pre \p first may equal \p result, but the range <tt>[first, last)</tt> and the range <tt>[result, result + (last - first))</tt> shall not overlap otherwise.
125 * The following code snippet demonstrates how to use \p uninitialized_copy to initialize
126 * a range of uninitialized memory.
129 * #include <thrust/uninitialized_copy.h>
130 * #include <thrust/device_malloc.h>
131 * #include <thrust/device_vector.h>
135 * __host__ __device__
136 * Int(int x) : val(x) {}
143 * thrust::device_vector<Int> input(N, val);
144 * thrust::device_ptr<Int> array = thrust::device_malloc<Int>(N);
145 * thrust::uninitialized_copy(input.begin(), input.end(), array);
147 * // Int x = array[i];
148 * // x.val == 46 for all 0 <= i < N
151 * \see http://www.sgi.com/tech/stl/uninitialized_copy.html
153 * \see \c uninitialized_fill
155 * \see \c device_malloc
157 template<typename InputIterator, typename ForwardIterator>
158 ForwardIterator uninitialized_copy(InputIterator first,
160 ForwardIterator result);
163 /*! In \c thrust, the function \c thrust::device_new allocates memory for
164 * an object and then creates an object at that location by calling a constructor.
165 * Occasionally, however, it is useful to separate those two operations.
166 * If each iterator in the range <tt>[result, result + n)</tt> points
167 * to uninitialized memory, then \p uninitialized_copy_n creates a copy of
168 * <tt>[first, first + n)</tt> in that range. That is, for each iterator \c i in
169 * the input, \p uninitialized_copy_n creates a copy of \c *i in the location pointed
170 * to by the corresponding iterator in the output range by \p InputIterator's
171 * \c value_type's copy constructor with *i as its argument.
173 * The algorithm's execution is parallelized as determined by \p exec.
175 * \param exec The execution policy to use for parallelization.
176 * \param first The first element of the input range to copy from.
177 * \param n The number of elements to copy.
178 * \param result The first element of the output range to copy to.
179 * \return An iterator pointing to the last element of the output range.
181 * \tparam DerivedPolicy The name of the derived execution policy.
182 * \tparam InputIterator is a model of <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a>.
183 * \tparam Size is an integral type.
184 * \tparam ForwardIterator is a model of <a href="http://www.sgi.com/tech/stl/ForwardIterator.html">Forward Iterator</a>,
185 * \p ForwardIterator is mutable, and \p ForwardIterator's \c value_type has a constructor that takes
186 * a single argument whose type is \p InputIterator's \c value_type.
188 * \pre \p first may equal \p result, but the range <tt>[first, first + n)</tt> and the range <tt>[result, result + n)</tt> shall not overlap otherwise.
190 * The following code snippet demonstrates how to use \p uninitialized_copy to initialize
191 * a range of uninitialized memory using the \p thrust::device execution policy for
195 * #include <thrust/uninitialized_copy.h>
196 * #include <thrust/device_malloc.h>
197 * #include <thrust/device_vector.h>
198 * #include <thrust/execution_policy.h>
202 * __host__ __device__
203 * Int(int x) : val(x) {}
210 * thrust::device_vector<Int> input(N, val);
211 * thrust::device_ptr<Int> array = thrust::device_malloc<Int>(N);
212 * thrust::uninitialized_copy_n(thrust::device, input.begin(), N, array);
214 * // Int x = array[i];
215 * // x.val == 46 for all 0 <= i < N
218 * \see http://www.sgi.com/tech/stl/uninitialized_copy.html
219 * \see \c uninitialized_copy
221 * \see \c uninitialized_fill
223 * \see \c device_malloc
225 template<typename DerivedPolicy, typename InputIterator, typename Size, typename ForwardIterator>
226 ForwardIterator uninitialized_copy_n(const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
229 ForwardIterator result);
232 /*! In \c thrust, the function \c thrust::device_new allocates memory for
233 * an object and then creates an object at that location by calling a constructor.
234 * Occasionally, however, it is useful to separate those two operations.
235 * If each iterator in the range <tt>[result, result + n)</tt> points
236 * to uninitialized memory, then \p uninitialized_copy_n creates a copy of
237 * <tt>[first, first + n)</tt> in that range. That is, for each iterator \c i in
238 * the input, \p uninitialized_copy_n creates a copy of \c *i in the location pointed
239 * to by the corresponding iterator in the output range by \p InputIterator's
240 * \c value_type's copy constructor with *i as its argument.
242 * \param first The first element of the input range to copy from.
243 * \param n The number of elements to copy.
244 * \param result The first element of the output range to copy to.
245 * \return An iterator pointing to the last element of the output range.
247 * \tparam InputIterator is a model of <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a>.
248 * \tparam Size is an integral type.
249 * \tparam ForwardIterator is a model of <a href="http://www.sgi.com/tech/stl/ForwardIterator.html">Forward Iterator</a>,
250 * \p ForwardIterator is mutable, and \p ForwardIterator's \c value_type has a constructor that takes
251 * a single argument whose type is \p InputIterator's \c value_type.
253 * \pre \p first may equal \p result, but the range <tt>[first, first + n)</tt> and the range <tt>[result, result + n)</tt> shall not overlap otherwise.
255 * The following code snippet demonstrates how to use \p uninitialized_copy to initialize
256 * a range of uninitialized memory.
259 * #include <thrust/uninitialized_copy.h>
260 * #include <thrust/device_malloc.h>
261 * #include <thrust/device_vector.h>
265 * __host__ __device__
266 * Int(int x) : val(x) {}
273 * thrust::device_vector<Int> input(N, val);
274 * thrust::device_ptr<Int> array = thrust::device_malloc<Int>(N);
275 * thrust::uninitialized_copy_n(input.begin(), N, array);
277 * // Int x = array[i];
278 * // x.val == 46 for all 0 <= i < N
281 * \see http://www.sgi.com/tech/stl/uninitialized_copy.html
282 * \see \c uninitialized_copy
284 * \see \c uninitialized_fill
286 * \see \c device_malloc
288 template<typename InputIterator, typename Size, typename ForwardIterator>
289 ForwardIterator uninitialized_copy_n(InputIterator first,
291 ForwardIterator result);
300 #include <thrust/detail/uninitialized_copy.inl>