thrust
Modules | Functions
Copying
Algorithms

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)

Detailed Description

// utility


Function Documentation

template<typename InputIterator , typename OutputIterator >
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).

Parameters:
firstThe beginning of the sequence to copy.
lastThe end of the sequence to copy.
resultThe destination sequence.
Returns:
The end of the destination sequence.
See also:
http://www.sgi.com/tech/stl/copy.html
Template Parameters:
InputIteratormust be a model of Input Iterator and InputIterator's value_type must be convertible to OutputIterator's value_type.
OutputIteratormust 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
template<typename InputIterator , typename Size , typename OutputIterator >
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.

Parameters:
firstThe beginning of the range to copy.
nThe number of elements to copy.
resultThe beginning destination range.
Returns:
The end of the destination range.
Template Parameters:
InputIteratormust be a model of Input Iterator and InputIterator's value_type must be convertible to OutputIterator's value_type.
Sizeis an integral type.
OutputIteratormust 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
See also:
http://www.sgi.com/tech/stl/copy_n.html
thrust::copy
template<typename ForwardIterator1 , typename ForwardIterator2 >
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).

Parameters:
first1The beginning of the first sequence to swap.
last1One position past the last element of the first sequence to swap.
first2The beginning of the second sequence to swap.
Returns:
An iterator pointing to one position past the last element of the second sequence to swap.
Template Parameters:
ForwardIterator1is a model of Forward Iterator, and ForwardIterator1's value_type must be convertible to ForwardIterator2's value_type.
ForwardIterator2is 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
See also:
http://www.sgi.com/tech/stl/swap_ranges.html
swap
template<typename InputIterator , typename ForwardIterator >
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.

Parameters:
firstThe first element of the input range to copy from.
lastThe last element of the input range to copy from.
resultThe first element of the output range to copy to.
Returns:
An iterator pointing to the last element of the output range.
Template Parameters:
InputIteratoris a model of Input Iterator.
ForwardIteratoris 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
See also:
http://www.sgi.com/tech/stl/uninitialized_copy.html
copy
uninitialized_fill
device_new
device_malloc
template<typename InputIterator , typename Size , typename ForwardIterator >
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.

Parameters:
firstThe first element of the input range to copy from.
nThe number of elements to copy.
resultThe first element of the output range to copy to.
Returns:
An iterator pointing to the last element of the output range.
Template Parameters:
InputIteratoris a model of Input Iterator.
Sizeis an integral type.
ForwardIteratoris 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
See also:
http://www.sgi.com/tech/stl/uninitialized_copy.html
uninitialized_copy
copy
uninitialized_fill
device_new
device_malloc