Transformations

Groups

template <typename DerivedPolicy,   typename InputIterator,   typename OutputIterator> _CCCL_HOST_DEVICE OutputIterator adjacent_difference(const thrust::detail::execution_policy_base< DerivedPolicy > & exec,   InputIterator first,   InputIterator last,   OutputIterator result);
template <typename DerivedPolicy,   typename InputIterator,   typename OutputIterator,   typename BinaryFunction> _CCCL_HOST_DEVICE OutputIterator adjacent_difference(const thrust::detail::execution_policy_base< DerivedPolicy > & exec,   InputIterator first,   InputIterator last,   OutputIterator result,   BinaryFunction binary_op);
template <typename InputIterator,   typename OutputIterator> OutputIterator adjacent_difference(InputIterator first,   InputIterator last,   OutputIterator result);
template <typename InputIterator,   typename OutputIterator,   typename BinaryFunction> OutputIterator adjacent_difference(InputIterator first,   InputIterator last,   OutputIterator result,   BinaryFunction binary_op);
template <typename DerivedPolicy,   typename ForwardIterator,   typename Generator> _CCCL_HOST_DEVICE void generate(const thrust::detail::execution_policy_base< DerivedPolicy > & exec,   ForwardIterator first,   ForwardIterator last,   Generator gen);
template <typename ForwardIterator,   typename Generator> void generate(ForwardIterator first,   ForwardIterator last,   Generator gen);
template <typename DerivedPolicy,   typename OutputIterator,   typename Size,   typename Generator> _CCCL_HOST_DEVICE OutputIterator generate_n(const thrust::detail::execution_policy_base< DerivedPolicy > & exec,   OutputIterator first,   Size n,   Generator gen);
template <typename OutputIterator,   typename Size,   typename Generator> OutputIterator generate_n(OutputIterator first,   Size n,   Generator gen);
template <typename DerivedPolicy,   typename ForwardIterator> _CCCL_HOST_DEVICE void sequence(const thrust::detail::execution_policy_base< DerivedPolicy > & exec,   ForwardIterator first,   ForwardIterator last);
template <typename ForwardIterator> void sequence(ForwardIterator first,   ForwardIterator last);
template <typename DerivedPolicy,   typename ForwardIterator,   typename T> _CCCL_HOST_DEVICE void sequence(const thrust::detail::execution_policy_base< DerivedPolicy > & exec,   ForwardIterator first,   ForwardIterator last,   T init);
template <typename ForwardIterator,   typename T> void sequence(ForwardIterator first,   ForwardIterator last,   T init);
template <typename DerivedPolicy,   typename ForwardIterator,   typename T> _CCCL_HOST_DEVICE void sequence(const thrust::detail::execution_policy_base< DerivedPolicy > & exec,   ForwardIterator first,   ForwardIterator last,   T init,   T step);
template <typename ForwardIterator,   typename T> void sequence(ForwardIterator first,   ForwardIterator last,   T init,   T step);
template <typename DerivedPolicy,   typename ForwardIterator,   typename UnaryOperation> _CCCL_HOST_DEVICE void tabulate(const thrust::detail::execution_policy_base< DerivedPolicy > & exec,   ForwardIterator first,   ForwardIterator last,   UnaryOperation unary_op);
template <typename ForwardIterator,   typename UnaryOperation> void tabulate(ForwardIterator first,   ForwardIterator last,   UnaryOperation unary_op);
template <typename DerivedPolicy,   typename InputIterator,   typename OutputIterator,   typename UnaryFunction> _CCCL_HOST_DEVICE OutputIterator transform(const thrust::detail::execution_policy_base< DerivedPolicy > & exec,   InputIterator first,   InputIterator last,   OutputIterator result,   UnaryFunction op);
template <typename InputIterator,   typename OutputIterator,   typename UnaryFunction> OutputIterator transform(InputIterator first,   InputIterator last,   OutputIterator result,   UnaryFunction op);
template <typename DerivedPolicy,   typename InputIterator1,   typename InputIterator2,   typename OutputIterator,   typename BinaryFunction> _CCCL_HOST_DEVICE OutputIterator transform(const thrust::detail::execution_policy_base< DerivedPolicy > & exec,   InputIterator1 first1,   InputIterator1 last1,   InputIterator2 first2,   OutputIterator result,   BinaryFunction op);
template <typename InputIterator1,   typename InputIterator2,   typename OutputIterator,   typename BinaryFunction> OutputIterator transform(InputIterator1 first1,   InputIterator1 last1,   InputIterator2 first2,   OutputIterator result,   BinaryFunction op);
template <typename DerivedPolicy,   typename InputIterator,   typename ForwardIterator,   typename UnaryFunction,   typename Predicate> _CCCL_HOST_DEVICE ForwardIterator transform_if(const thrust::detail::execution_policy_base< DerivedPolicy > & exec,   InputIterator first,   InputIterator last,   ForwardIterator result,   UnaryFunction op,   Predicate pred);
template <typename InputIterator,   typename ForwardIterator,   typename UnaryFunction,   typename Predicate> ForwardIterator transform_if(InputIterator first,   InputIterator last,   ForwardIterator result,   UnaryFunction op,   Predicate pred);
template <typename DerivedPolicy,   typename InputIterator1,   typename InputIterator2,   typename ForwardIterator,   typename UnaryFunction,   typename Predicate> _CCCL_HOST_DEVICE ForwardIterator transform_if(const thrust::detail::execution_policy_base< DerivedPolicy > & exec,   InputIterator1 first,   InputIterator1 last,   InputIterator2 stencil,   ForwardIterator result,   UnaryFunction op,   Predicate pred);
template <typename InputIterator1,   typename InputIterator2,   typename ForwardIterator,   typename UnaryFunction,   typename Predicate> ForwardIterator transform_if(InputIterator1 first,   InputIterator1 last,   InputIterator2 stencil,   ForwardIterator result,   UnaryFunction op,   Predicate pred);
template <typename DerivedPolicy,   typename InputIterator1,   typename InputIterator2,   typename InputIterator3,   typename ForwardIterator,   typename BinaryFunction,   typename Predicate> _CCCL_HOST_DEVICE ForwardIterator transform_if(const thrust::detail::execution_policy_base< DerivedPolicy > & exec,   InputIterator1 first1,   InputIterator1 last1,   InputIterator2 first2,   InputIterator3 stencil,   ForwardIterator result,   BinaryFunction binary_op,   Predicate pred);
template <typename InputIterator1,   typename InputIterator2,   typename InputIterator3,   typename ForwardIterator,   typename BinaryFunction,   typename Predicate> ForwardIterator transform_if(InputIterator1 first1,   InputIterator1 last1,   InputIterator2 first2,   InputIterator3 stencil,   ForwardIterator result,   BinaryFunction binary_op,   Predicate pred);

Functions

Function adjacent_difference

template <typename DerivedPolicy,   typename InputIterator,   typename OutputIterator> _CCCL_HOST_DEVICE OutputIterator adjacent_difference(const thrust::detail::execution_policy_base< DerivedPolicy > & exec,   InputIterator first,   InputIterator last,   OutputIterator result); adjacent_difference calculates the differences of adjacent elements in the range [first, last). That is, *first is assigned to *result, and, for each iterator i in the range [first + 1, last), the difference of *i and *(i - 1) is assigned to *(result + (i - first)).

This version of adjacent_difference uses operator- to calculate differences.

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

The following code snippet demonstrates how to use adjacent_difference to compute the difference between adjacent elements of a range using the thrust::device execution policy:

#include <thrust/adjacent_difference.h>
#include <thrust/device_vector.h>
#include <thrust/execution_policy.h>
...
int h_data[8] = {1, 2, 1, 2, 1, 2, 1, 2};
thrust::device_vector<int> d_data(h_data, h_data + 8);
thrust::device_vector<int> d_result(8);

thrust::adjacent_difference(thrust::device, d_data.begin(), d_data.end(), d_result.begin());

// d_result is now [1, 1, -1, 1, -1, 1, -1, 1]

Remark: Note that result is permitted to be the same iterator as first. This is useful for computing differences “in place”.

Template Parameters:

  • DerivedPolicy The name of the derived execution policy.
  • InputIterator is a model of Input Iterator, and x and y are objects of InputIterator'svalue_type, then x - is defined, and InputIterator'svalue_type is convertible to a type in OutputIterator's set of value_types, and the return type of x - y is convertible to a type in OutputIterator's set of value_types.
  • OutputIterator is a model of Output Iterator.

Function Parameters:

  • exec The execution policy to use for parallelization.
  • first The beginning of the input range.
  • last The end of the input range.
  • result The beginning of the output range.

Returns: The iterator result + (last - first)

See:

Function adjacent_difference

template <typename DerivedPolicy,   typename InputIterator,   typename OutputIterator,   typename BinaryFunction> _CCCL_HOST_DEVICE OutputIterator adjacent_difference(const thrust::detail::execution_policy_base< DerivedPolicy > & exec,   InputIterator first,   InputIterator last,   OutputIterator result,   BinaryFunction binary_op); adjacent_difference calculates the differences of adjacent elements in the range [first, last). That is, *first is assigned to *result, and, for each iterator i in the range [first + 1, last), binary_op(*i, *(i - 1)) is assigned to *(result + (i - first)).

This version of adjacent_difference uses the binary function binary_op to calculate differences.

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

The following code snippet demonstrates how to use adjacent_difference to compute the sum between adjacent elements of a range using the thrust::device execution policy:

#include <thrust/adjacent_difference.h>
#include <thrust/functional.h>
#include <thrust/device_vector.h>
#include <thrust/execution_policy.h>
...
int h_data[8] = {1, 2, 1, 2, 1, 2, 1, 2};
thrust::device_vector<int> d_data(h_data, h_data + 8);
thrust::device_vector<int> d_result(8);

thrust::adjacent_difference(thrust::device, d_data.begin(), d_data.end(), d_result.begin(), thrust::plus<int>());

// d_result is now [1, 3, 3, 3, 3, 3, 3, 3]

Remark: Note that result is permitted to be the same iterator as first. This is useful for computing differences “in place”.

Template Parameters:

  • DerivedPolicy The name of the derived execution policy.
  • InputIterator is a model of Input Iterator, and InputIterator'svalue_type is convertible to BinaryFunction'sfirst_argument_type and second_argument_type, and InputIterator'svalue_type is convertible to a type in OutputIterator's set of value_types.
  • OutputIterator is a model of Output Iterator.
  • BinaryFunction's result_type is convertible to a type in OutputIterator's set of value_types.

Function Parameters:

  • exec The execution policy to use for parallelization.
  • first The beginning of the input range.
  • last The end of the input range.
  • result The beginning of the output range.
  • binary_op The binary function used to compute differences.

Returns: The iterator result + (last - first)

See:

Function adjacent_difference

template <typename InputIterator,   typename OutputIterator> OutputIterator adjacent_difference(InputIterator first,   InputIterator last,   OutputIterator result); adjacent_difference calculates the differences of adjacent elements in the range [first, last). That is, *first is assigned to *result, and, for each iterator i in the range [first + 1, last), the difference of *i and *(i - 1) is assigned to *(result + (i - first)).

This version of adjacent_difference uses operator- to calculate differences.

The following code snippet demonstrates how to use adjacent_difference to compute the difference between adjacent elements of a range.

#include <thrust/adjacent_difference.h>
#include <thrust/device_vector.h>
...
int h_data[8] = {1, 2, 1, 2, 1, 2, 1, 2};
thrust::device_vector<int> d_data(h_data, h_data + 8);
thrust::device_vector<int> d_result(8);

thrust::adjacent_difference(d_data.begin(), d_data.end(), d_result.begin());

// d_result is now [1, 1, -1, 1, -1, 1, -1, 1]

Remark: Note that result is permitted to be the same iterator as first. This is useful for computing differences “in place”.

Template Parameters:

  • InputIterator is a model of Input Iterator, and x and y are objects of InputIterator'svalue_type, then x - is defined, and InputIterator'svalue_type is convertible to a type in OutputIterator's set of value_types, and the return type of x - y is convertible to a type in OutputIterator's set of value_types.
  • OutputIterator is a model of Output Iterator.

Function Parameters:

  • first The beginning of the input range.
  • last The end of the input range.
  • result The beginning of the output range.

Returns: The iterator result + (last - first)

See:

Function adjacent_difference

template <typename InputIterator,   typename OutputIterator,   typename BinaryFunction> OutputIterator adjacent_difference(InputIterator first,   InputIterator last,   OutputIterator result,   BinaryFunction binary_op); adjacent_difference calculates the differences of adjacent elements in the range [first, last). That is, *first is assigned to *result, and, for each iterator i in the range [first + 1, last), binary_op(*i, *(i - 1)) is assigned to *(result + (i - first)).

This version of adjacent_difference uses the binary function binary_op to calculate differences.

The following code snippet demonstrates how to use adjacent_difference to compute the sum between adjacent elements of a range.

#include <thrust/adjacent_difference.h>
#include <thrust/functional.h>
#include <thrust/device_vector.h>
...
int h_data[8] = {1, 2, 1, 2, 1, 2, 1, 2};
thrust::device_vector<int> d_data(h_data, h_data + 8);
thrust::device_vector<int> d_result(8);

thrust::adjacent_difference(d_data.begin(), d_data.end(), d_result.begin(), thrust::plus<int>());

// d_result is now [1, 3, 3, 3, 3, 3, 3, 3]

Remark: Note that result is permitted to be the same iterator as first. This is useful for computing differences “in place”.

Template Parameters:

  • InputIterator is a model of Input Iterator, and InputIterator'svalue_type is convertible to BinaryFunction'sfirst_argument_type and second_argument_type, and InputIterator'svalue_type is convertible to a type in OutputIterator's set of value_types.
  • OutputIterator is a model of Output Iterator.
  • BinaryFunction's result_type is convertible to a type in OutputIterator's set of value_types.

Function Parameters:

  • first The beginning of the input range.
  • last The end of the input range.
  • result The beginning of the output range.
  • binary_op The binary function used to compute differences.

Returns: The iterator result + (last - first)

See:

Function generate

template <typename DerivedPolicy,   typename ForwardIterator,   typename Generator> _CCCL_HOST_DEVICE void generate(const thrust::detail::execution_policy_base< DerivedPolicy > & exec,   ForwardIterator first,   ForwardIterator last,   Generator gen); generate assigns the result of invoking gen, a function object that takes no arguments, to each element in the range [first,last).

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

The following code snippet demonstrates how to fill a host_vector with random numbers, using the standard C library function rand using the thrust::host execution policy for parallelization:

#include <thrust/generate.h>
#include <thrust/host_vector.h>
#include <thrust/execution_policy.h>
#include <cstdlib>
...
thrust::host_vector<int> v(10);
srand(13);
thrust::generate(thrust::host, v.begin(), v.end(), rand);

// the elements of v are now pseudo-random numbers

Template Parameters:

  • DerivedPolicy The name of the derived execution policy.
  • ForwardIterator is a model of Forward Iterator, and ForwardIterator is mutable.
  • Generator is a model of Generator, and Generator'sresult_type is convertible to ForwardIterator'svalue_type.

Function Parameters:

  • exec The execution policy to use for parallelization.
  • first The first element in the range of interest.
  • last The last element in the range of interest.
  • gen A function argument, taking no parameters, used to generate values to assign to elements in the range [first,last).

See:

Function generate

template <typename ForwardIterator,   typename Generator> void generate(ForwardIterator first,   ForwardIterator last,   Generator gen); generate assigns the result of invoking gen, a function object that takes no arguments, to each element in the range [first,last).

The following code snippet demonstrates how to fill a host_vector with random numbers, using the standard C library function rand.

#include <thrust/generate.h>
#include <thrust/host_vector.h>
#include <thrust/execution_policy.h>
#include <cstdlib>
...
thrust::host_vector<int> v(10);
srand(13);
thrust::generate(v.begin(), v.end(), rand);

// the elements of v are now pseudo-random numbers

Template Parameters:

  • ForwardIterator is a model of Forward Iterator, and ForwardIterator is mutable.
  • Generator is a model of Generator, and Generator'sresult_type is convertible to ForwardIterator'svalue_type.

Function Parameters:

  • first The first element in the range of interest.
  • last The last element in the range of interest.
  • gen A function argument, taking no parameters, used to generate values to assign to elements in the range [first,last).

See:

Function generate_n

template <typename DerivedPolicy,   typename OutputIterator,   typename Size,   typename Generator> _CCCL_HOST_DEVICE OutputIterator generate_n(const thrust::detail::execution_policy_base< DerivedPolicy > & exec,   OutputIterator first,   Size n,   Generator gen); generate_n assigns the result of invoking gen, a function object that takes no arguments, to each element in the range [first,first + n). The return value is first + n.

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

The following code snippet demonstrates how to fill a host_vector with random numbers, using the standard C library function rand using the thrust::host execution policy for parallelization:

#include <thrust/generate.h>
#include <thrust/host_vector.h>
#include <thrust/execution_policy.h>
#include <cstdlib>
...
thrust::host_vector<int> v(10);
srand(13);
thrust::generate_n(thrust::host, v.begin(), 10, rand);

// the elements of v are now pseudo-random numbers

Template Parameters:

  • DerivedPolicy The name of the derived execution policy.
  • OutputIterator is a model of Output Iterator.
  • Size is an integral type (either signed or unsigned).
  • Generator is a model of Generator, and Generator'sresult_type is convertible to a type in OutputIterator's set of value_types.

Function Parameters:

  • exec The execution policy to use for parallelization.
  • first The first element in the range of interest.
  • n The size of the range of interest.
  • gen A function argument, taking no parameters, used to generate values to assign to elements in the range [first,first + n).

See:

Function generate_n

template <typename OutputIterator,   typename Size,   typename Generator> OutputIterator generate_n(OutputIterator first,   Size n,   Generator gen); generate_n assigns the result of invoking gen, a function object that takes no arguments, to each element in the range [first,first + n). The return value is first + n.

The following code snippet demonstrates how to fill a host_vector with random numbers, using the standard C library function rand.

#include <thrust/generate.h>
#include <thrust/host_vector.h>
#include <stdlib.h>
...
thrust::host_vector<int> v(10);
srand(13);
thrust::generate_n(v.begin(), 10, rand);

// the elements of v are now pseudo-random numbers

Template Parameters:

  • OutputIterator is a model of Output Iterator.
  • Size is an integral type (either signed or unsigned).
  • Generator is a model of Generator, and Generator'sresult_type is convertible to a type in OutputIterator's set of value_types.

Function Parameters:

  • first The first element in the range of interest.
  • n The size of the range of interest.
  • gen A function argument, taking no parameters, used to generate values to assign to elements in the range [first,first + n).

See:

Function sequence

template <typename DerivedPolicy,   typename ForwardIterator> _CCCL_HOST_DEVICE void sequence(const thrust::detail::execution_policy_base< DerivedPolicy > & exec,   ForwardIterator first,   ForwardIterator last); sequence fills the range [first, last) with a sequence of numbers.

For each iterator i in the range [first, last), this version of sequence performs the assignment *i = (i - first).

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

The following code snippet demonstrates how to use sequence to fill a range with a sequence of numbers using the thrust::host execution policy for parallelization:

#include <thrust/sequence.h>
#include <thrust/execution_policy.h>
...
const int N = 10;
int A[N];
thrust::sequence(thrust::host, A, A + 10);
// A is now {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

Note: Unlike the similar C++ STL function std::iota, sequence offers no guarantee on order of execution.

Template Parameters:

  • DerivedPolicy The name of the derived execution policy.
  • ForwardIterator is a model of Forward Iterator, and ForwardIterator is mutable, and if x and y are objects of ForwardIterator'svalue_type, then x + y is defined, and if T is ForwardIterator'svalue_type, then T(0) is defined.

Function Parameters:

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

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

Function sequence

template <typename ForwardIterator> void sequence(ForwardIterator first,   ForwardIterator last); sequence fills the range [first, last) with a sequence of numbers.

For each iterator i in the range [first, last), this version of sequence performs the assignment *i = (i - first).

The following code snippet demonstrates how to use sequence to fill a range with a sequence of numbers.

#include <thrust/sequence.h>
...
const int N = 10;
int A[N];
thrust::sequence(A, A + 10);
// A is now {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

Note: Unlike the similar C++ STL function std::iota, sequence offers no guarantee on order of execution.

Template Parameters: ForwardIterator: is a model of Forward Iterator, and ForwardIterator is mutable, and if x and y are objects of ForwardIterator'svalue_type, then x + y is defined, and if T is ForwardIterator'svalue_type, then T(0) is defined.

Function Parameters:

  • first The beginning of the sequence.
  • last The end of the sequence.

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

Function sequence

template <typename DerivedPolicy,   typename ForwardIterator,   typename T> _CCCL_HOST_DEVICE void sequence(const thrust::detail::execution_policy_base< DerivedPolicy > & exec,   ForwardIterator first,   ForwardIterator last,   T init); sequence fills the range [first, last) with a sequence of numbers.

For each iterator i in the range [first, last), this version of sequence performs the assignment *i = init + (i - first).

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

The following code snippet demonstrates how to use sequence to fill a range with a sequence of numbers starting from the value 1 using the thrust::host execution policy for parallelization:

#include <thrust/sequence.h>
#include <thrust/execution_policy.h>
...
const int N = 10;
int A[N];
thrust::sequence(thrust::host, A, A + 10, 1);
// A is now {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

Note: Unlike the similar C++ STL function std::iota, sequence offers no guarantee on order of execution.

Template Parameters:

  • DerivedPolicy The name of the derived execution policy.
  • ForwardIterator is a model of Forward Iterator, and ForwardIterator is mutable, and if x and y are objects of ForwardIterator'svalue_type, then x + y is defined, and if T is ForwardIterator'svalue_type, then T(0) is defined.
  • T is a model of Assignable, and T is convertible to ForwardIterator'svalue_type.

Function Parameters:

  • exec The execution policy to use for parallelization.
  • first The beginning of the sequence.
  • last The end of the sequence.
  • init The first value of the sequence of numbers.

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

Function sequence

template <typename ForwardIterator,   typename T> void sequence(ForwardIterator first,   ForwardIterator last,   T init); sequence fills the range [first, last) with a sequence of numbers.

For each iterator i in the range [first, last), this version of sequence performs the assignment *i = init + (i - first).

The following code snippet demonstrates how to use sequence to fill a range with a sequence of numbers starting from the value 1.

#include <thrust/sequence.h>
...
const int N = 10;
int A[N];
thrust::sequence(A, A + 10, 1);
// A is now {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

Note: Unlike the similar C++ STL function std::iota, sequence offers no guarantee on order of execution.

Template Parameters:

  • ForwardIterator is a model of Forward Iterator, and ForwardIterator is mutable, and if x and y are objects of ForwardIterator'svalue_type, then x + y is defined, and if T is ForwardIterator'svalue_type, then T(0) is defined.
  • T is a model of Assignable, and T is convertible to ForwardIterator'svalue_type.

Function Parameters:

  • first The beginning of the sequence.
  • last The end of the sequence.
  • init The first value of the sequence of numbers.

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

Function sequence

template <typename DerivedPolicy,   typename ForwardIterator,   typename T> _CCCL_HOST_DEVICE void sequence(const thrust::detail::execution_policy_base< DerivedPolicy > & exec,   ForwardIterator first,   ForwardIterator last,   T init,   T step); sequence fills the range [first, last) with a sequence of numbers.

For each iterator i in the range [first, last), this version of sequence performs the assignment *i = init + step * (i - first).

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

The following code snippet demonstrates how to use sequence to fill a range with a sequence of numbers starting from the value 1 with a step size of 3 using the thrust::host execution policy for parallelization:

#include <thrust/sequence.h>
#include <thrust/execution_policy.h>
...
const int N = 10;
int A[N];
thrust::sequence(thrust::host, A, A + 10, 1, 3);
// A is now {1, 4, 7, 10, 13, 16, 19, 22, 25, 28}

Note: Unlike the similar C++ STL function std::iota, sequence offers no guarantee on order of execution.

Template Parameters:

  • DerivedPolicy The name of the derived execution policy.
  • ForwardIterator is a model of Forward Iterator, and ForwardIterator is mutable, and if x and y are objects of ForwardIterator'svalue_type, then x + y is defined, and if T is ForwardIterator'svalue_type, then T(0) is defined.
  • T is a model of Assignable, and T is convertible to ForwardIterator'svalue_type.

Function Parameters:

  • exec The execution policy to use for parallelization.
  • first The beginning of the sequence.
  • last The end of the sequence.
  • init The first value of the sequence of numbers
  • step The difference between consecutive elements.

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

Function sequence

template <typename ForwardIterator,   typename T> void sequence(ForwardIterator first,   ForwardIterator last,   T init,   T step); sequence fills the range [first, last) with a sequence of numbers.

For each iterator i in the range [first, last), this version of sequence performs the assignment *i = init + step * (i - first).

The following code snippet demonstrates how to use sequence to fill a range with a sequence of numbers starting from the value 1 with a step size of 3.

#include <thrust/sequence.h>
...
const int N = 10;
int A[N];
thrust::sequence(A, A + 10, 1, 3);
// A is now {1, 4, 7, 10, 13, 16, 19, 22, 25, 28}

Note: Unlike the similar C++ STL function std::iota, sequence offers no guarantee on order of execution.

Template Parameters:

  • ForwardIterator is a model of Forward Iterator, and ForwardIterator is mutable, and if x and y are objects of ForwardIterator'svalue_type, then x + y is defined, and if T is ForwardIterator'svalue_type, then T(0) is defined.
  • T is a model of Assignable, and T is convertible to ForwardIterator'svalue_type.

Function Parameters:

  • first The beginning of the sequence.
  • last The end of the sequence.
  • init The first value of the sequence of numbers
  • step The difference between consecutive elements.

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

Function tabulate

template <typename DerivedPolicy,   typename ForwardIterator,   typename UnaryOperation> _CCCL_HOST_DEVICE void tabulate(const thrust::detail::execution_policy_base< DerivedPolicy > & exec,   ForwardIterator first,   ForwardIterator last,   UnaryOperation unary_op); tabulate fills the range [first, last) with the value of a function applied to each element’s index.

For each iterator i in the range [first, last), tabulate performs the assignment *i = unary_op(i - first).

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

The following code snippet demonstrates how to use tabulate to generate the first n non-positive integers using the thrust::host execution policy for parallelization:

#include <thrust/tabulate.h>
#include <thrust/functional.h>
#include <thrust/execution_policy.h>
...
const int N = 10;
int A[N];
thrust::tabulate(thrust::host, A, A + 10, thrust::negate<int>());
// A is now {0, -1, -2, -3, -4, -5, -6, -7, -8, -9}

Template Parameters:

  • DerivedPolicy The name of the derived execution policy.
  • ForwardIterator is a model of Forward Iterator, and ForwardIterator is mutable, and if x and y are objects of ForwardIterator'svalue_type, then x + y is defined, and if T is ForwardIterator'svalue_type, then T(0) is defined.
  • UnaryOperation is a model of Unary Function and UnaryFunction'sresult_type is convertible to OutputIterator'svalue_type.

Function Parameters:

  • exec The execution policy to use for parallelization.
  • first The beginning of the range.
  • last The end of the range.
  • unary_op The unary operation to apply.

See:

  • thrust::fill
  • thrust::generate
  • thrust::sequence

Function tabulate

template <typename ForwardIterator,   typename UnaryOperation> void tabulate(ForwardIterator first,   ForwardIterator last,   UnaryOperation unary_op); tabulate fills the range [first, last) with the value of a function applied to each element’s index.

For each iterator i in the range [first, last), tabulate performs the assignment *i = unary_op(i - first).

The following code snippet demonstrates how to use tabulate to generate the first n non-positive integers:

#include <thrust/tabulate.h>
#include <thrust/functional.h>
...
const int N = 10;
int A[N];
thrust::tabulate(A, A + 10, thrust::negate<int>());
// A is now {0, -1, -2, -3, -4, -5, -6, -7, -8, -9}

Template Parameters:

  • ForwardIterator is a model of Forward Iterator, and ForwardIterator is mutable, and if x and y are objects of ForwardIterator'svalue_type, then x + y is defined, and if T is ForwardIterator'svalue_type, then T(0) is defined.
  • UnaryOperation is a model of Unary Function and UnaryFunction'sresult_type is convertible to OutputIterator'svalue_type.

Function Parameters:

  • first The beginning of the range.
  • last The end of the range.
  • unary_op The unary operation to apply.

See:

  • thrust::fill
  • thrust::generate
  • thrust::sequence

Function transform

template <typename DerivedPolicy,   typename InputIterator,   typename OutputIterator,   typename UnaryFunction> _CCCL_HOST_DEVICE OutputIterator transform(const thrust::detail::execution_policy_base< DerivedPolicy > & exec,   InputIterator first,   InputIterator last,   OutputIterator result,   UnaryFunction op); This version of transform applies a unary function to each element of an input sequence and stores the result in the corresponding position in an output sequence. Specifically, for each iterator i in the range [first, last) the operation op(*i) is performed and the result is assigned to *o, where o is the corresponding output iterator in the range [result, result + (last - first) ). The input and output sequences may coincide, resulting in an in-place transformation.

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

The following code snippet demonstrates how to use transform to negate a range in-place using the thrust::host execution policy for parallelization:

#include <thrust/transform.h>
#include <thrust/functional.h>
#include <thrust/execution_policy.h>
...

int data[10] = {-5, 0, 2, -3, 2, 4, 0, -1, 2, 8};

thrust::negate<int> op;

thrust::transform(thrust::host, data, data + 10, data, op); // in-place transformation

// data is now {5, 0, -2, 3, -2, -4, 0, 1, -2, -8};

Template Parameters:

  • DerivedPolicy The name of the derived execution policy.
  • InputIterator is a model of Input Iterator and InputIterator'svalue_type is convertible to UnaryFunction'sargument_type.
  • OutputIterator is a model of Output Iterator.
  • UnaryFunction is a model of Unary Function and UnaryFunction'sresult_type is convertible to OutputIterator'svalue_type.

Function Parameters:

  • exec The execution policy to use for parallelization.
  • first The beginning of the input sequence.
  • last The end of the input sequence.
  • result The beginning of the output sequence.
  • op The transformation operation.

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

Returns: The end of the output sequence.

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

Function transform

template <typename InputIterator,   typename OutputIterator,   typename UnaryFunction> OutputIterator transform(InputIterator first,   InputIterator last,   OutputIterator result,   UnaryFunction op); This version of transform applies a unary function to each element of an input sequence and stores the result in the corresponding position in an output sequence. Specifically, for each iterator i in the range [first, last) the operation op(*i) is performed and the result is assigned to *o, where o is the corresponding output iterator in the range [result, result + (last - first) ). The input and output sequences may coincide, resulting in an in-place transformation.

The following code snippet demonstrates how to use transform

#include <thrust/transform.h>
#include <thrust/functional.h>

int data[10] = {-5, 0, 2, -3, 2, 4, 0, -1, 2, 8};

thrust::negate<int> op;

thrust::transform(data, data + 10, data, op); // in-place transformation

// data is now {5, 0, -2, 3, -2, -4, 0, 1, -2, -8};

Template Parameters:

  • InputIterator is a model of Input Iterator and InputIterator'svalue_type is convertible to UnaryFunction'sargument_type.
  • OutputIterator is a model of Output Iterator.
  • UnaryFunction is a model of Unary Function and UnaryFunction'sresult_type is convertible to OutputIterator'svalue_type.

Function Parameters:

  • first The beginning of the input sequence.
  • last The end of the input sequence.
  • result The beginning of the output sequence.
  • op The tranformation operation.

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

Returns: The end of the output sequence.

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

Function transform

template <typename DerivedPolicy,   typename InputIterator1,   typename InputIterator2,   typename OutputIterator,   typename BinaryFunction> _CCCL_HOST_DEVICE OutputIterator transform(const thrust::detail::execution_policy_base< DerivedPolicy > & exec,   InputIterator1 first1,   InputIterator1 last1,   InputIterator2 first2,   OutputIterator result,   BinaryFunction op); This version of transform applies a binary function to each pair of elements from two input sequences and stores the result in the corresponding position in an output sequence. Specifically, for each iterator i in the range [first1, last1) and j = first + (i - first1) in the range [first2, last2) the operation op(*i,*j) is performed and the result is assigned to *o, where o is the corresponding output iterator in the range [result, result + (last - first) ). The input and output sequences may coincide, resulting in an in-place transformation.

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

The following code snippet demonstrates how to use transform to compute the sum of two ranges using the thrust::host execution policy for parallelization:

#include <thrust/transform.h>
#include <thrust/functional.h>
#include <thrust/execution_policy.h>
...

int input1[6] = {-5,  0,  2,  3,  2,  4};
int input2[6] = { 3,  6, -2,  1,  2,  3};
int output[6];

thrust::plus<int> op;

thrust::transform(thrust::host, input1, input1 + 6, input2, output, op);

// output is now {-2,  6,  0,  4,  4,  7};

Template Parameters:

  • DerivedPolicy The name of the derived execution policy.
  • InputIterator1 is a model of Input Iterator and InputIterator1'svalue_type is convertible to BinaryFunction'sfirst_argument_type.
  • InputIterator2 is a model of Input Iterator and InputIterator2'svalue_type is convertible to BinaryFunction'ssecond_argument_type.
  • OutputIterator is a model of Output Iterator.
  • BinaryFunction is a model of Binary Function and BinaryFunction'sresult_type is convertible to OutputIterator'svalue_type.

Function Parameters:

  • exec The execution policy to use for parallelization.
  • first1 The beginning of the first input sequence.
  • last1 The end of the first input sequence.
  • first2 The beginning of the second input sequence.
  • result The beginning of the output sequence.
  • op The tranformation operation.

Preconditions:

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

Returns: The end of the output sequence.

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

Function transform

template <typename InputIterator1,   typename InputIterator2,   typename OutputIterator,   typename BinaryFunction> OutputIterator transform(InputIterator1 first1,   InputIterator1 last1,   InputIterator2 first2,   OutputIterator result,   BinaryFunction op); This version of transform applies a binary function to each pair of elements from two input sequences and stores the result in the corresponding position in an output sequence. Specifically, for each iterator i in the range [first1, last1) and j = first + (i - first1) in the range [first2, last2) the operation op(*i,*j) is performed and the result is assigned to *o, where o is the corresponding output iterator in the range [result, result + (last - first) ). The input and output sequences may coincide, resulting in an in-place transformation.

The following code snippet demonstrates how to use transform

#include <thrust/transform.h>
#include <thrust/functional.h>

int input1[6] = {-5,  0,  2,  3,  2,  4};
int input2[6] = { 3,  6, -2,  1,  2,  3};
int output[6];

thrust::plus<int> op;

thrust::transform(input1, input1 + 6, input2, output, op);

// output is now {-2,  6,  0,  4,  4,  7};

Template Parameters:

  • InputIterator1 is a model of Input Iterator and InputIterator1'svalue_type is convertible to BinaryFunction'sfirst_argument_type.
  • InputIterator2 is a model of Input Iterator and InputIterator2'svalue_type is convertible to BinaryFunction'ssecond_argument_type.
  • OutputIterator is a model of Output Iterator.
  • BinaryFunction is a model of Binary Function and BinaryFunction'sresult_type is convertible to OutputIterator'svalue_type.

Function Parameters:

  • first1 The beginning of the first input sequence.
  • last1 The end of the first input sequence.
  • first2 The beginning of the second input sequence.
  • result The beginning of the output sequence.
  • op The tranformation operation.

Preconditions:

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

Returns: The end of the output sequence.

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

Function transform_if

template <typename DerivedPolicy,   typename InputIterator,   typename ForwardIterator,   typename UnaryFunction,   typename Predicate> _CCCL_HOST_DEVICE ForwardIterator transform_if(const thrust::detail::execution_policy_base< DerivedPolicy > & exec,   InputIterator first,   InputIterator last,   ForwardIterator result,   UnaryFunction op,   Predicate pred); This version of transform_if conditionally applies a unary function to each element of an input sequence and stores the result in the corresponding position in an output sequence if the corresponding position in the input sequence satifies a predicate. Otherwise, the corresponding position in the output sequence is not modified.

Specifically, for each iterator i in the range [first, last) the predicate pred(*i) is evaluated. If this predicate evaluates to true, the result of op(*i) is assigned to *o, where o is the corresponding output iterator in the range [result, result + (last - first) ). Otherwise, op(*i) is not evaluated and no assignment occurs. The input and output sequences may coincide, resulting in an in-place transformation.

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

The following code snippet demonstrates how to use transform_if to negate the odd-valued elements of a range using the thrust::host execution policy for parallelization:

#include <thrust/transform.h>
#include <thrust/functional.h>
#include <thrust/execution_policy.h>
...

int data[10]    = {-5, 0, 2, -3, 2, 4, 0, -1, 2, 8};

struct is_odd
{
  __host__ __device__
  bool operator()(int x)
  {
    return x % 2;
  }
};

thrust::negate<int> op;
thrust::identity<int> identity;

// negate odd elements
thrust::transform_if(thrust::host, data, data + 10, data, op, is_odd()); // in-place transformation

// data is now {5, 0, 2, 3, 2, 4, 0, 1, 2, 8};

Template Parameters:

  • DerivedPolicy The name of the derived execution policy.
  • InputIterator is a model of Input Iterator, and InputIterator'svalue_type is convertible to Predicate'sargument_type, and InputIterator'svalue_type is convertible to UnaryFunction'sargument_type.
  • ForwardIterator is a model of Forward Iterator.
  • UnaryFunction is a model of Unary Function and UnaryFunction'sresult_type is convertible to OutputIterator'svalue_type.
  • Predicate is a model of Predicate.

Function Parameters:

  • exec The execution policy to use for parallelization.
  • first The beginning of the input sequence.
  • last The end of the input sequence.
  • result The beginning of the output sequence.
  • op The tranformation operation.
  • pred The predicate operation.

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

Returns: The end of the output sequence.

See: thrust::transform

Function transform_if

template <typename InputIterator,   typename ForwardIterator,   typename UnaryFunction,   typename Predicate> ForwardIterator transform_if(InputIterator first,   InputIterator last,   ForwardIterator result,   UnaryFunction op,   Predicate pred); This version of transform_if conditionally applies a unary function to each element of an input sequence and stores the result in the corresponding position in an output sequence if the corresponding position in the input sequence satifies a predicate. Otherwise, the corresponding position in the output sequence is not modified.

Specifically, for each iterator i in the range [first, last) the predicate pred(*i) is evaluated. If this predicate evaluates to true, the result of op(*i) is assigned to *o, where o is the corresponding output iterator in the range [result, result + (last - first) ). Otherwise, op(*i) is not evaluated and no assignment occurs. The input and output sequences may coincide, resulting in an in-place transformation.

The following code snippet demonstrates how to use transform_if:

#include <thrust/transform.h>
#include <thrust/functional.h>

int data[10]    = {-5, 0, 2, -3, 2, 4, 0, -1, 2, 8};

struct is_odd
{
  __host__ __device__
  bool operator()(int x)
  {
    return x % 2;
  }
};

thrust::negate<int> op;
thrust::identity<int> identity;

// negate odd elements
thrust::transform_if(data, data + 10, data, op, is_odd()); // in-place transformation

// data is now {5, 0, 2, 3, 2, 4, 0, 1, 2, 8};

Template Parameters:

  • InputIterator is a model of Input Iterator, and InputIterator'svalue_type is convertible to Predicate'sargument_type, and InputIterator'svalue_type is convertible to UnaryFunction'sargument_type.
  • ForwardIterator is a model of Forward Iterator.
  • UnaryFunction is a model of Unary Function and UnaryFunction'sresult_type is convertible to OutputIterator'svalue_type.
  • Predicate is a model of Predicate.

Function Parameters:

  • first The beginning of the input sequence.
  • last The end of the input sequence.
  • result The beginning of the output sequence.
  • op The tranformation operation.
  • pred The predicate operation.

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

Returns: The end of the output sequence.

See: thrust::transform

Function transform_if

template <typename DerivedPolicy,   typename InputIterator1,   typename InputIterator2,   typename ForwardIterator,   typename UnaryFunction,   typename Predicate> _CCCL_HOST_DEVICE ForwardIterator transform_if(const thrust::detail::execution_policy_base< DerivedPolicy > & exec,   InputIterator1 first,   InputIterator1 last,   InputIterator2 stencil,   ForwardIterator result,   UnaryFunction op,   Predicate pred); This version of transform_if conditionally applies a unary function to each element of an input sequence and stores the result in the corresponding position in an output sequence if the corresponding position in a stencil sequence satisfies a predicate. Otherwise, the corresponding position in the output sequence is not modified.

Specifically, for each iterator i in the range [first, last) the predicate pred(*s) is evaluated, where s is the corresponding input iterator in the range [stencil, stencil + (last - first) ). If this predicate evaluates to true, the result of op(*i) is assigned to *o, where o is the corresponding output iterator in the range [result, result + (last - first) ). Otherwise, op(*i) is not evaluated and no assignment occurs. The input and output sequences may coincide, resulting in an in-place transformation.

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

The following code snippet demonstrates how to use transform_if using the thrust::host execution policy for parallelization:

#include <thrust/transform.h>
#include <thrust/functional.h>
#include <thrust/execution_policy.h>
...

int data[10]    = {-5, 0, 2, -3, 2, 4, 0, -1, 2, 8};
int stencil[10] = { 1, 0, 1,  0, 1, 0, 1,  0, 1, 0};

thrust::negate<int> op;
thrust::identity<int> identity;

thrust::transform_if(thrust::host, data, data + 10, stencil, data, op, identity); // in-place transformation

// data is now {5, 0, -2, -3, -2,  4, 0, -1, -2,  8};

Template Parameters:

  • DerivedPolicy The name of the derived execution policy.
  • InputIterator1 is a model of Input Iterator and InputIterator1'svalue_type is convertible to UnaryFunction'sargument_type.
  • InputIterator2 is a model of Input Iterator and InputIterator2'svalue_type is convertible to Predicate'sargument_type.
  • ForwardIterator is a model of Forward Iterator.
  • UnaryFunction is a model of Unary Function and UnaryFunction'sresult_type is convertible to OutputIterator'svalue_type.
  • Predicate is a model of Predicate.

Function Parameters:

  • exec The execution policy to use for parallelization.
  • first The beginning of the input sequence.
  • last The end of the input sequence.
  • stencil The beginning of the stencil sequence.
  • result The beginning of the output sequence.
  • op The tranformation operation.
  • pred The predicate operation.

Preconditions:

  • first may equal result, but the range [first, last) shall not overlap the range [result, result + (last - first)) otherwise.
  • stencil may equal result, but the range [stencil, stencil + (last - first)) shall not overlap the range [result, result + (last - first)) otherwise.

Returns: The end of the output sequence.

See: thrust::transform

Function transform_if

template <typename InputIterator1,   typename InputIterator2,   typename ForwardIterator,   typename UnaryFunction,   typename Predicate> ForwardIterator transform_if(InputIterator1 first,   InputIterator1 last,   InputIterator2 stencil,   ForwardIterator result,   UnaryFunction op,   Predicate pred); This version of transform_if conditionally applies a unary function to each element of an input sequence and stores the result in the corresponding position in an output sequence if the corresponding position in a stencil sequence satisfies a predicate. Otherwise, the corresponding position in the output sequence is not modified.

Specifically, for each iterator i in the range [first, last) the predicate pred(*s) is evaluated, where s is the corresponding input iterator in the range [stencil, stencil + (last - first) ). If this predicate evaluates to true, the result of op(*i) is assigned to *o, where o is the corresponding output iterator in the range [result, result + (last - first) ). Otherwise, op(*i) is not evaluated and no assignment occurs. The input and output sequences may coincide, resulting in an in-place transformation.

The following code snippet demonstrates how to use transform_if:

#include <thrust/transform.h>
#include <thrust/functional.h>

int data[10]    = {-5, 0, 2, -3, 2, 4, 0, -1, 2, 8};
int stencil[10] = { 1, 0, 1,  0, 1, 0, 1,  0, 1, 0};

thrust::negate<int> op;
thrust::identity<int> identity;

thrust::transform_if(data, data + 10, stencil, data, op, identity); // in-place transformation

// data is now {5, 0, -2, -3, -2,  4, 0, -1, -2,  8};

Template Parameters:

  • InputIterator1 is a model of Input Iterator and InputIterator1'svalue_type is convertible to UnaryFunction'sargument_type.
  • InputIterator2 is a model of Input Iterator and InputIterator2'svalue_type is convertible to Predicate'sargument_type.
  • ForwardIterator is a model of Forward Iterator.
  • UnaryFunction is a model of Unary Function and UnaryFunction'sresult_type is convertible to OutputIterator'svalue_type.
  • Predicate is a model of Predicate.

Function Parameters:

  • first The beginning of the input sequence.
  • last The end of the input sequence.
  • stencil The beginning of the stencil sequence.
  • result The beginning of the output sequence.
  • op The tranformation operation.
  • pred The predicate operation.

Preconditions:

  • first may equal result, but the range [first, last) shall not overlap the range [result, result + (last - first)) otherwise.
  • stencil may equal result, but the range [stencil, stencil + (last - first)) shall not overlap the range [result, result + (last - first)) otherwise.

Returns: The end of the output sequence.

See: thrust::transform

Function transform_if

template <typename DerivedPolicy,   typename InputIterator1,   typename InputIterator2,   typename InputIterator3,   typename ForwardIterator,   typename BinaryFunction,   typename Predicate> _CCCL_HOST_DEVICE ForwardIterator transform_if(const thrust::detail::execution_policy_base< DerivedPolicy > & exec,   InputIterator1 first1,   InputIterator1 last1,   InputIterator2 first2,   InputIterator3 stencil,   ForwardIterator result,   BinaryFunction binary_op,   Predicate pred); This version of transform_if conditionally applies a binary function to each pair of elements from two input sequences and stores the result in the corresponding position in an output sequence if the corresponding position in a stencil sequence satifies a predicate. Otherwise, the corresponding position in the output sequence is not modified.

Specifically, for each iterator i in the range [first1, last1) and j = first2 + (i - first1) in the range [first2, first2 + (last1 - first1) ), the predicate pred(*s) is evaluated, where s is the corresponding input iterator in the range [stencil, stencil + (last1 - first1) ). If this predicate evaluates to true, the result of binary_op(*i,*j) is assigned to *o, where o is the corresponding output iterator in the range [result, result + (last1 - first1) ). Otherwise, binary_op(*i,*j) is not evaluated and no assignment occurs. The input and output sequences may coincide, resulting in an in-place transformation.

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

The following code snippet demonstrates how to use transform_if using the thrust::host execution policy for parallelization:

#include <thrust/transform.h>
#include <thrust/functional.h>
#include <thrust/execution_policy.h>
...

int input1[6]  = {-5,  0,  2,  3,  2,  4};
int input2[6]  = { 3,  6, -2,  1,  2,  3};
int stencil[8] = { 1,  0,  1,  0,  1,  0};
int output[6];

thrust::plus<int> op;
thrust::identity<int> identity;

thrust::transform_if(thrust::host, input1, input1 + 6, input2, stencil, output, op, identity);

// output is now {-2,  0,  0,  3,  4,  4};

Template Parameters:

  • DerivedPolicy The name of the derived execution policy.
  • InputIterator1 is a model of Input Iterator and InputIterator1'svalue_type is convertible to BinaryFunction'sfirst_argument_type.
  • InputIterator2 is a model of Input Iterator and InputIterator2'svalue_type is convertible to BinaryFunction'ssecond_argument_type.
  • ForwardIterator is a model of Forward Iterator.
  • BinaryFunction is a model of Binary Function and BinaryFunction'sresult_type is convertible to OutputIterator'svalue_type.
  • Predicate is a model of Predicate.

Function Parameters:

  • exec The execution policy to use for parallelization.
  • first1 The beginning of the first input sequence.
  • last1 The end of the first input sequence.
  • first2 The beginning of the second input sequence.
  • stencil The beginning of the stencil sequence.
  • result The beginning of the output sequence.
  • binary_op The transformation operation.
  • pred The predicate operation.

Preconditions:

  • first1 may equal result, but the range [first1, last1) shall not overlap the range [result, result + (last1 - first1)) otherwise.
  • first2 may equal result, but the range [first2, first2 + (last1 - first1)) shall not overlap the range [result, result + (last1 - first1)) otherwise.
  • stencil may equal result, but the range [stencil, stencil + (last1 - first1)) shall not overlap the range [result, result + (last1 - first1)) otherwise.

Returns: The end of the output sequence.

See: thrust::transform

Function transform_if

template <typename InputIterator1,   typename InputIterator2,   typename InputIterator3,   typename ForwardIterator,   typename BinaryFunction,   typename Predicate> ForwardIterator transform_if(InputIterator1 first1,   InputIterator1 last1,   InputIterator2 first2,   InputIterator3 stencil,   ForwardIterator result,   BinaryFunction binary_op,   Predicate pred); This version of transform_if conditionally applies a binary function to each pair of elements from two input sequences and stores the result in the corresponding position in an output sequence if the corresponding position in a stencil sequence satifies a predicate. Otherwise, the corresponding position in the output sequence is not modified.

Specifically, for each iterator i in the range [first1, last1) and j = first2 + (i - first1) in the range [first2, first2 + (last1 - first1) ), the predicate pred(*s) is evaluated, where s is the corresponding input iterator in the range [stencil, stencil + (last1 - first1) ). If this predicate evaluates to true, the result of binary_op(*i,*j) is assigned to *o, where o is the corresponding output iterator in the range [result, result + (last1 - first1) ). Otherwise, binary_op(*i,*j) is not evaluated and no assignment occurs. The input and output sequences may coincide, resulting in an in-place transformation.

The following code snippet demonstrates how to use transform_if:

#include <thrust/transform.h>
#include <thrust/functional.h>

int input1[6]  = {-5,  0,  2,  3,  2,  4};
int input2[6]  = { 3,  6, -2,  1,  2,  3};
int stencil[8] = { 1,  0,  1,  0,  1,  0};
int output[6];

thrust::plus<int> op;
thrust::identity<int> identity;

thrust::transform_if(input1, input1 + 6, input2, stencil, output, op, identity);

// output is now {-2,  0,  0,  3,  4,  4};

Template Parameters:

  • InputIterator1 is a model of Input Iterator and InputIterator1'svalue_type is convertible to BinaryFunction'sfirst_argument_type.
  • InputIterator2 is a model of Input Iterator and InputIterator2'svalue_type is convertible to BinaryFunction'ssecond_argument_type.
  • ForwardIterator is a model of Forward Iterator.
  • BinaryFunction is a model of Binary Function and BinaryFunction'sresult_type is convertible to OutputIterator'svalue_type.
  • Predicate is a model of Predicate.

Function Parameters:

  • first1 The beginning of the first input sequence.
  • last1 The end of the first input sequence.
  • first2 The beginning of the second input sequence.
  • stencil The beginning of the stencil sequence.
  • result The beginning of the output sequence.
  • binary_op The transformation operation.
  • pred The predicate operation.

Preconditions:

  • first1 may equal result, but the range [first1, last1) shall not overlap the range [result, result + (last1 - first1)) otherwise.
  • first2 may equal result, but the range [first2, first2 + (last1 - first1)) shall not overlap the range [result, result + (last1 - first1)) otherwise.
  • stencil may equal result, but the range [stencil, stencil + (last1 - first1)) shall not overlap the range [result, result + (last1 - first1)) otherwise.

Returns: The end of the output sequence.

See: thrust::transform