|
thrust
|
Modules | |
| Gathering | |
| Scattering | |
Functions | |
| template<typename InputIterator , typename OutputIterator > | |
| OutputIterator | thrust::copy (InputIterator first, InputIterator last, OutputIterator result) |
| template<typename InputIterator , typename Size , typename OutputIterator > | |
| OutputIterator | thrust::copy_n (InputIterator first, Size n, OutputIterator result) |
| template<typename ForwardIterator1 , typename ForwardIterator2 > | |
| ForwardIterator2 | thrust::swap_ranges (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2) |
| template<typename InputIterator , typename ForwardIterator > | |
| ForwardIterator | thrust::uninitialized_copy (InputIterator first, InputIterator last, ForwardIterator result) |
| template<typename InputIterator , typename Size , typename ForwardIterator > | |
| ForwardIterator | thrust::uninitialized_copy_n (InputIterator first, Size n, ForwardIterator result) |
// utility
| OutputIterator thrust::copy | ( | InputIterator | first, |
| InputIterator | last, | ||
| OutputIterator | result | ||
| ) |
copy copies elements from the range [first, last) to the range [result, result + (last - first)). That is, it performs the assignments *result = *first, *(result + 1) = *(first + 1), and so on. Generally, for every integer n from 0 to last - first, copy performs the assignment *(result + n) = *(first + n). Unlike std::copy, copy offers no guarantee on order of operation. As a result, calling copy with overlapping source and destination ranges has undefined behavior.
The return value is result + (last - first).
| first | The beginning of the sequence to copy. |
| last | The end of the sequence to copy. |
| result | The destination sequence. |
| InputIterator | must be a model of Input Iterator and InputIterator's value_type must be convertible to OutputIterator's value_type. |
| OutputIterator | must be a model of Output Iterator. |
The following code snippet demonstrates how to use copy to copy from one range to another.
#include <thrust/copy.h> #include <thrust/device_vector.h> ... thrust::device_vector<int> vec0(100); thrust::device_vector<int> vec1(100); ... thrust::copy(vec0.begin(), vec0.end(), vec1.begin()); // vec1 is now a copy of vec0
| OutputIterator thrust::copy_n | ( | InputIterator | first, |
| Size | n, | ||
| OutputIterator | result | ||
| ) |
copy_n copies elements from the range [first, first + n) to the range [result, result + n). That is, it performs the assignments *result = *first, *(result + 1) = *(first + 1), and so on. Generally, for every integer i from 0 to n, copy performs the assignment *(result + i) = *(first + i). Unlike std::copy_n, copy_n offers no guarantee on order of operation. As a result, calling copy_n with overlapping source and destination ranges has undefined behavior.
The return value is result + n.
| first | The beginning of the range to copy. |
| n | The number of elements to copy. |
| result | The beginning destination range. |
| InputIterator | must be a model of Input Iterator and InputIterator's value_type must be convertible to OutputIterator's value_type. |
| Size | is an integral type. |
| OutputIterator | must be a model of Output Iterator. |
The following code snippet demonstrates how to use copy to copy from one range to another.
#include <thrust/copy.h> #include <thrust/device_vector.h> ... size_t n = 100; thrust::device_vector<int> vec0(n); thrust::device_vector<int> vec1(n); ... thrust::copy_n(vec0.begin(), n, vec1.begin()); // vec1 is now a copy of vec0
| ForwardIterator2 thrust::swap_ranges | ( | ForwardIterator1 | first1, |
| ForwardIterator1 | last1, | ||
| ForwardIterator2 | first2 | ||
| ) |
swap_ranges swaps each of the elements in the range [first1, last1) with the corresponding element in the range [first2, first2 + (last1 - first1)). That is, for each integer n such that 0 <= n < (last1 - first1), it swaps *(first1 + n) and *(first2 + n). The return value is first2 + (last1 - first1).
| first1 | The beginning of the first sequence to swap. |
| last1 | One position past the last element of the first sequence to swap. |
| first2 | The beginning of the second sequence to swap. |
| ForwardIterator1 | is a model of Forward Iterator, and ForwardIterator1's value_type must be convertible to ForwardIterator2's value_type. |
| ForwardIterator2 | is a model of Forward Iterator, and ForwardIterator2's value_type must be convertible to ForwardIterator1's value_type. |
The following code snippet demonstrates how to use swap_ranges to swap the contents of two thrust::device_vectors.
#include <thrust/swap.h> #include <thrust/device_vector.h> ... thrust::device_vector<int> v1(2), v2(2); v1[0] = 1; v1[1] = 2; v2[0] = 3; v2[1] = 4; thrust::swap_ranges(v1.begin(), v1.end(), v2.begin()); // v1[0] == 3, v1[1] == 4, v2[0] == 1, v2[1] == 2
| ForwardIterator thrust::uninitialized_copy | ( | InputIterator | first, |
| InputIterator | last, | ||
| ForwardIterator | result | ||
| ) |
In thrust, the function thrust::device_new allocates memory for an object and then creates an object at that location by calling a constructor. Occasionally, however, it is useful to separate those two operations. If each iterator in the range [result, result + (last - first)) points to uninitialized memory, then uninitialized_copy creates a copy of [first, last) in that range. That is, for each iterator i in the input, uninitialized_copy creates a copy of *i in the location pointed to by the corresponding iterator in the output range by ForwardIterator's value_type's copy constructor with *i as its argument.
| first | The first element of the input range to copy from. |
| last | The last element of the input range to copy from. |
| result | The first element of the output range to copy to. |
| InputIterator | is a model of Input Iterator. |
| ForwardIterator | is a model of Forward Iterator, ForwardIterator is mutable, and ForwardIterator's value_type has a constructor that takes a single argument whose type is InputIterator's value_type. |
The following code snippet demonstrates how to use uninitialized_copy to initialize a range of uninitialized memory.
#include <thrust/uninitialized_copy.h> #include <thrust/device_malloc.h> #include <thrust/device_vector.h> ... struct Int { __host__ __device__ Int(int x) : val(x) {} int val; }; ... const int N = 137; Int val(46); thrust::device_vector<Int> input(N, val); thrust::device_ptr<Int> array = thrust::device_malloc<Int>(N); thrust::uninitialized_copy(input.begin(), input.end(), array); // Int x = array[i]; // x.val == 46 for all 0 <= i < N
| ForwardIterator thrust::uninitialized_copy_n | ( | InputIterator | first, |
| Size | n, | ||
| ForwardIterator | result | ||
| ) |
In thrust, the function thrust::device_new allocates memory for an object and then creates an object at that location by calling a constructor. Occasionally, however, it is useful to separate those two operations. If each iterator in the range [result, result + n) points to uninitialized memory, then uninitialized_copy_n creates a copy of [first, first + n) in that range. That is, for each iterator i in the input, uninitialized_copy_n creates a copy of *i in the location pointed to by the corresponding iterator in the output range by InputIterator's value_type's copy constructor with *i as its argument.
| first | The first element of the input range to copy from. |
| n | The number of elements to copy. |
| result | The first element of the output range to copy to. |
| InputIterator | is a model of Input Iterator. |
| Size | is an integral type. |
| ForwardIterator | is a model of Forward Iterator, ForwardIterator is mutable, and ForwardIterator's value_type has a constructor that takes a single argument whose type is InputIterator's value_type. |
The following code snippet demonstrates how to use uninitialized_copy to initialize a range of uninitialized memory.
#include <thrust/uninitialized_copy.h> #include <thrust/device_malloc.h> #include <thrust/device_vector.h> ... struct Int { __host__ __device__ Int(int x) : val(x) {} int val; }; ... const int N = 137; Int val(46); thrust::device_vector<Int> input(N, val); thrust::device_ptr<Int> array = thrust::device_malloc<Int>(N); thrust::uninitialized_copy_n(input.begin(), N, array); // Int x = array[i]; // x.val == 46 for all 0 <= i < N
1.7.4