Copying

Groups

template <typename DerivedPolicy,   typename InputIterator,   typename OutputIterator> _CCCL_HOST_DEVICE OutputIterator copy(const thrust::detail::execution_policy_base< DerivedPolicy > & exec,   InputIterator first,   InputIterator last,   OutputIterator result);
template <typename DerivedPolicy,   typename InputIterator,   typename Size,   typename OutputIterator> _CCCL_HOST_DEVICE OutputIterator copy_n(const thrust::detail::execution_policy_base< DerivedPolicy > & exec,   InputIterator first,   Size n,   OutputIterator result);
template <typename InputIterator,   typename OutputIterator> OutputIterator copy(InputIterator first,   InputIterator last,   OutputIterator result);
template <typename InputIterator,   typename Size,   typename OutputIterator> OutputIterator copy_n(InputIterator first,   Size n,   OutputIterator result);
template <typename DerivedPolicy,   typename ForwardIterator1,   typename ForwardIterator2> _CCCL_HOST_DEVICE ForwardIterator2 swap_ranges(const thrust::detail::execution_policy_base< DerivedPolicy > & exec,   ForwardIterator1 first1,   ForwardIterator1 last1,   ForwardIterator2 first2);
template <typename ForwardIterator1,   typename ForwardIterator2> ForwardIterator2 swap_ranges(ForwardIterator1 first1,   ForwardIterator1 last1,   ForwardIterator2 first2);
template <typename DerivedPolicy,   typename InputIterator,   typename ForwardIterator> _CCCL_HOST_DEVICE ForwardIterator uninitialized_copy(const thrust::detail::execution_policy_base< DerivedPolicy > & exec,   InputIterator first,   InputIterator last,   ForwardIterator result);
template <typename InputIterator,   typename ForwardIterator> ForwardIterator uninitialized_copy(InputIterator first,   InputIterator last,   ForwardIterator result);
template <typename DerivedPolicy,   typename InputIterator,   typename Size,   typename ForwardIterator> _CCCL_HOST_DEVICE ForwardIterator uninitialized_copy_n(const thrust::detail::execution_policy_base< DerivedPolicy > & exec,   InputIterator first,   Size n,   ForwardIterator result);
template <typename InputIterator,   typename Size,   typename ForwardIterator> ForwardIterator uninitialized_copy_n(InputIterator first,   Size n,   ForwardIterator result);

Functions

Function copy

template <typename DerivedPolicy,   typename InputIterator,   typename OutputIterator> _CCCL_HOST_DEVICE OutputIterator copy(const thrust::detail::execution_policy_base< DerivedPolicy > & exec,   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).

The algorithm’s execution is parallelized as determined by exec.

The following code snippet demonstrates how to use copy to copy from one range to another using the thrust::device parallelization policy:

#include <thrust/copy.h>
#include <thrust/device_vector.h>
#include <thrust/execution_policy.h>
...

thrust::device_vector<int> vec0(100);
thrust::device_vector<int> vec1(100);
...

thrust::copy(thrust::device, vec0.begin(), vec0.end(), vec1.begin());

// vec1 is now a copy of vec0

Template Parameters:

  • DerivedPolicy The name of the derived execution policy.
  • InputIterator must be a model of Input Iterator and InputIterator'svalue_type must be convertible to OutputIterator'svalue_type.
  • OutputIterator must be a model of Output Iterator.

Function Parameters:

  • exec The execution policy to use for parallelization.
  • first The beginning of the sequence to copy.
  • last The end of the sequence to copy.
  • result The destination sequence.

Preconditions: result may be equal to first, but result shall not be in the range [first, last) otherwise.

Returns: The end of the destination sequence.

See: https://en.cppreference.com/w/cpp/algorithm/copy

Function copy_n

template <typename DerivedPolicy,   typename InputIterator,   typename Size,   typename OutputIterator> _CCCL_HOST_DEVICE OutputIterator copy_n(const thrust::detail::execution_policy_base< DerivedPolicy > & exec,   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. The algorithm’s execution is parallelized as determined by exec.

The following code snippet demonstrates how to use copy to copy from one range to another using the thrust::device parallelization policy:

#include <thrust/copy.h>
#include <thrust/device_vector.h>
#include <thrust/execution_policy.h>
...
size_t n = 100;
thrust::device_vector<int> vec0(n);
thrust::device_vector<int> vec1(n);
...
thrust::copy_n(thrust::device, vec0.begin(), n, vec1.begin());

// vec1 is now a copy of vec0

Template Parameters:

  • DerivedPolicy The name of the derived execution policy.
  • InputIterator must be a model of Input Iterator and InputIterator'svalue_type must be convertible to OutputIterator'svalue_type.
  • Size is an integral type.
  • OutputIterator must be a model of Output Iterator.

Function Parameters:

  • exec The execution policy to use for parallelization.
  • first The beginning of the range to copy.
  • n The number of elements to copy.
  • result The beginning destination range.

Preconditions: result may be equal to first, but result shall not be in the range [first, first + n) otherwise.

Returns: The end of the destination range.

See:

Function copy

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

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 Parameters:

  • InputIterator must be a model of Input Iterator and InputIterator'svalue_type must be convertible to OutputIterator'svalue_type.
  • OutputIterator must be a model of Output Iterator.

Function Parameters:

  • first The beginning of the sequence to copy.
  • last The end of the sequence to copy.
  • result The destination sequence.

Preconditions: result may be equal to first, but result shall not be in the range [first, last) otherwise.

Returns: The end of the destination sequence.

See: https://en.cppreference.com/w/cpp/algorithm/copy

Function copy_n

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

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

Template Parameters:

  • InputIterator must be a model of Input Iterator and InputIterator'svalue_type must be convertible to OutputIterator'svalue_type.
  • Size is an integral type.
  • OutputIterator must be a model of Output Iterator.

Function Parameters:

  • first The beginning of the range to copy.
  • n The number of elements to copy.
  • result The beginning destination range.

Preconditions: result may be equal to first, but result shall not be in the range [first, first + n) otherwise.

Returns: The end of the destination range.

See:

Function swap_ranges

template <typename DerivedPolicy,   typename ForwardIterator1,   typename ForwardIterator2> _CCCL_HOST_DEVICE ForwardIterator2 swap_ranges(const thrust::detail::execution_policy_base< DerivedPolicy > & exec,   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).

The algorithm’s execution is parallelized as determined by exec.

The following code snippet demonstrates how to use swap_ranges to swap the contents of two thrust::device_vectors using the thrust::device execution policy for parallelization:

#include <thrust/swap.h>
#include <thrust/device_vector.h>
#include <thrust/execution_policy.h>
...
thrust::device_vector<int> v1(2), v2(2);
v1[0] = 1;
v1[1] = 2;
v2[0] = 3;
v2[1] = 4;

thrust::swap_ranges(thrust::device, v1.begin(), v1.end(), v2.begin());

// v1[0] == 3, v1[1] == 4, v2[0] == 1, v2[1] == 2

Template Parameters:

  • DerivedPolicy The name of the derived execution policy.
  • ForwardIterator1 is a model of Forward Iterator, and ForwardIterator1'svalue_type must be convertible to ForwardIterator2'svalue_type.
  • ForwardIterator2 is a model of Forward Iterator, and ForwardIterator2'svalue_type must be convertible to ForwardIterator1'svalue_type.

Function Parameters:

  • exec The execution policy to use for parallelization.
  • 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.

Preconditions: first1 may equal first2, but the range [first1, last1) shall not overlap the range [first2, first2 + (last1 - first1)) otherwise.

Returns: An iterator pointing to one position past the last element of the second sequence to swap.

See:

Function swap_ranges

template <typename ForwardIterator1,   typename ForwardIterator2> ForwardIterator2 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).

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

Template Parameters:

  • ForwardIterator1 is a model of Forward Iterator, and ForwardIterator1'svalue_type must be convertible to ForwardIterator2'svalue_type.
  • ForwardIterator2 is a model of Forward Iterator, and ForwardIterator2'svalue_type must be convertible to ForwardIterator1'svalue_type.

Function Parameters:

  • 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.

Preconditions: first1 may equal first2, but the range [first1, last1) shall not overlap the range [first2, first2 + (last1 - first1)) otherwise.

Returns: An iterator pointing to one position past the last element of the second sequence to swap.

See:

Function uninitialized_copy

template <typename DerivedPolicy,   typename InputIterator,   typename ForwardIterator> _CCCL_HOST_DEVICE ForwardIterator uninitialized_copy(const thrust::detail::execution_policy_base< DerivedPolicy > & exec,   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'svalue_type's copy constructor with *i as its argument.

The algorithm’s execution is parallelized as determined by exec.

The following code snippet demonstrates how to use uninitialized_copy to initialize a range of uninitialized memory using the thrust::device execution policy for parallelization:

#include <thrust/uninitialized_copy.h>
#include <thrust/device_malloc.h>
#include <thrust/device_vector.h>
#include <thrust/execution_policy.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(thrust::device, input.begin(), input.end(), array);

// Int x = array[i];
// x.val == 46 for all 0 <= i < N

Template Parameters:

  • DerivedPolicy The name of the derived execution policy.
  • InputIterator is a model of Input Iterator.
  • ForwardIterator is a model of Forward Iterator, ForwardIterator is mutable, and ForwardIterator'svalue_type has a constructor that takes a single argument whose type is InputIterator'svalue_type.

Function Parameters:

  • exec The execution policy to use for parallelization.
  • 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.

Preconditions: first may equal result, but the range [first, last) and the range [result, result + (last - first)) shall not overlap otherwise.

Returns: An iterator pointing to the last element of the output range.

See:

Function uninitialized_copy

template <typename InputIterator,   typename ForwardIterator> ForwardIterator 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'svalue_type's copy constructor with *i as its argument.

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

Template Parameters:

  • InputIterator is a model of Input Iterator.
  • ForwardIterator is a model of Forward Iterator, ForwardIterator is mutable, and ForwardIterator'svalue_type has a constructor that takes a single argument whose type is InputIterator'svalue_type.

Function Parameters:

  • 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.

Preconditions: first may equal result, but the range [first, last) and the range [result, result + (last - first)) shall not overlap otherwise.

Returns: An iterator pointing to the last element of the output range.

See:

Function uninitialized_copy_n

template <typename DerivedPolicy,   typename InputIterator,   typename Size,   typename ForwardIterator> _CCCL_HOST_DEVICE ForwardIterator uninitialized_copy_n(const thrust::detail::execution_policy_base< DerivedPolicy > & exec,   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'svalue_type's copy constructor with *i as its argument.

The algorithm’s execution is parallelized as determined by exec.

The following code snippet demonstrates how to use uninitialized_copy to initialize a range of uninitialized memory using the thrust::device execution policy for parallelization:

#include <thrust/uninitialized_copy.h>
#include <thrust/device_malloc.h>
#include <thrust/device_vector.h>
#include <thrust/execution_policy.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(thrust::device, input.begin(), N, array);

// Int x = array[i];
// x.val == 46 for all 0 <= i < N

Template Parameters:

  • DerivedPolicy The name of the derived execution policy.
  • InputIterator is a model of Input Iterator.
  • Size is an integral type.
  • ForwardIterator is a model of Forward Iterator, ForwardIterator is mutable, and ForwardIterator'svalue_type has a constructor that takes a single argument whose type is InputIterator'svalue_type.

Function Parameters:

  • exec The execution policy to use for parallelization.
  • 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.

Preconditions: first may equal result, but the range [first, first + n) and the range [result, result + n) shall not overlap otherwise.

Returns: An iterator pointing to the last element of the output range.

See:

Function uninitialized_copy_n

template <typename InputIterator,   typename Size,   typename ForwardIterator> ForwardIterator 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'svalue_type's copy constructor with *i as its argument.

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

Template Parameters:

  • InputIterator is a model of Input Iterator.
  • Size is an integral type.
  • ForwardIterator is a model of Forward Iterator, ForwardIterator is mutable, and ForwardIterator'svalue_type has a constructor that takes a single argument whose type is InputIterator'svalue_type.

Function Parameters:

  • 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.

Preconditions: first may equal result, but the range [first, first + n) and the range [result, result + n) shall not overlap otherwise.

Returns: An iterator pointing to the last element of the output range.

See: