thrust
Modules | Functions
Transformations
Algorithms

Modules

 Filling
 Modifying
 Replacing

Functions

template<typename InputIterator , typename OutputIterator >
OutputIterator thrust::adjacent_difference (InputIterator first, InputIterator last, OutputIterator result)
template<typename InputIterator , typename OutputIterator , typename BinaryFunction >
OutputIterator thrust::adjacent_difference (InputIterator first, InputIterator last, OutputIterator result, BinaryFunction binary_op)
template<typename ForwardIterator , typename Generator >
void thrust::generate (ForwardIterator first, ForwardIterator last, Generator gen)
template<typename OutputIterator , typename Size , typename Generator >
OutputIterator thrust::generate_n (OutputIterator first, Size n, Generator gen)
template<typename ForwardIterator >
void thrust::sequence (ForwardIterator first, ForwardIterator last)
template<typename ForwardIterator , typename T >
void thrust::sequence (ForwardIterator first, ForwardIterator last, T init)
template<typename ForwardIterator , typename T >
void thrust::sequence (ForwardIterator first, ForwardIterator last, T init, T step)
template<typename InputIterator , typename OutputIterator , typename UnaryFunction >
OutputIterator thrust::transform (InputIterator first, InputIterator last, OutputIterator result, UnaryFunction op)
template<typename InputIterator1 , typename InputIterator2 , typename OutputIterator , typename BinaryFunction >
OutputIterator thrust::transform (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputIterator result, BinaryFunction op)
template<typename InputIterator , typename ForwardIterator , typename UnaryFunction , typename Predicate >
ForwardIterator thrust::transform_if (InputIterator first, InputIterator last, ForwardIterator result, UnaryFunction op, Predicate pred)
template<typename InputIterator1 , typename InputIterator2 , typename ForwardIterator , typename UnaryFunction , typename Predicate >
ForwardIterator thrust::transform_if (InputIterator1 first, InputIterator1 last, InputIterator2 stencil, ForwardIterator result, UnaryFunction op, Predicate pred)
template<typename InputIterator1 , typename InputIterator2 , typename InputIterator3 , typename ForwardIterator , typename BinaryFunction , typename Predicate >
ForwardIterator thrust::transform_if (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator3 stencil, ForwardIterator result, BinaryFunction binary_op, Predicate pred)

Function Documentation

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

Parameters:
firstThe beginning of the input range.
lastThe end of the input range.
resultThe beginning of the output range.
Returns:
The iterator result + (last - first)
Template Parameters:
InputIteratoris a model of Input Iterator, and x and y are objects of InputIterator's value_type, then x - is defined, and InputIterator's value_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.
OutputIteratoris a model of Output Iterator.

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]
See also:
http://www.sgi.com/tech/stl/adjacent_difference.html
inclusive_scan
Note:
Note that result is permitted to be the same iterator as first. This is useful for computing differences "in place".
template<typename InputIterator , typename OutputIterator , typename BinaryFunction >
OutputIterator thrust::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.

Parameters:
firstThe beginning of the input range.
lastThe end of the input range.
resultThe beginning of the output range.
binary_opThe binary function used to compute differences.
Returns:
The iterator result + (last - first)
Template Parameters:
InputIteratoris a model of Input Iterator, and InputIterator's value_type is convertible to BinaryFunction's first_argument_type and second_argument_type, and InputIterator's value_type is convertible to a type in OutputIterator's set of value_types.
OutputIteratoris a model of Output Iterator.
BinaryFunction'sresult_type is convertible to a type in OutputIterator's set of value_types.

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_data is now [1, 3, 3, 3, 3, 3, 3, 3]
See also:
http://www.sgi.com/tech/stl/adjacent_difference.html
inclusive_scan
Note:
Note that result is permitted to be the same iterator as first. This is useful for computing differences "in place".
template<typename ForwardIterator , typename Generator >
void thrust::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).

Parameters:
firstThe first element in the range of interest.
lastThe last element in the range of interest.
genA function argument, taking no parameters, used to generate values to assign to elements in the range [first,last).
Template Parameters:
ForwardIteratoris a model of Forward Iterator, and ForwardIterator is mutable.
Generatoris a model of Generator, and Generator's result_type is convertible to ForwardIterator's value_type.

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(v.begin(), v.end(), rand);

  // the elements of v are now pseudo-random numbers
See also:
generate_n
http://www.sgi.com/tech/stl/generate.html
template<typename OutputIterator , typename Size , typename Generator >
OutputIterator thrust::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.

Parameters:
firstThe first element in the range of interest.
nThe size of the range of interest.
genA function argument, taking no parameters, used to generate values to assign to elements in the range [first,first + n).
Template Parameters:
OutputIteratoris a model of Output Iterator.
Sizeis an integral type (either signed or unsigned).
Generatoris a model of Generator, and Generator's result_type is convertible to a type in OutputIterator's set of value_types.

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
See also:
generate
http://www.sgi.com/tech/stl/generate.html
template<typename ForwardIterator >
void thrust::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).

Parameters:
firstThe beginning of the sequence.
lastThe end of the sequence.
Template Parameters:
ForwardIteratoris a model of Forward Iterator, and ForwardIterator is mutable, and if x and y are objects of ForwardIterator's value_type, then x + y is defined, and if T is ForwardIterator's value_type, then T(0) is defined.

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.
See also:
http://www.sgi.com/tech/stl/iota.html
template<typename ForwardIterator , typename T >
void thrust::sequence ( ForwardIterator  first,
ForwardIterator  last,
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).

Parameters:
firstThe beginning of the sequence.
lastThe end of the sequence.
initThe first value of the sequence of numbers.
Template Parameters:
ForwardIteratoris a model of Forward Iterator, and ForwardIterator is mutable, and if x and y are objects of ForwardIterator's value_type, then x + y is defined, and if T is ForwardIterator's value_type, then T(0) is defined.
Tis a model of Assignable, and T is convertible to ForwardIterator's value_type.

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.
See also:
http://www.sgi.com/tech/stl/iota.html
template<typename ForwardIterator , typename T >
void thrust::sequence ( ForwardIterator  first,
ForwardIterator  last,
init,
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).

Parameters:
firstThe beginning of the sequence.
lastThe end of the sequence.
initThe first value of the sequence of numbers
stepThe difference between consecutive elements.
Template Parameters:
ForwardIteratoris a model of Forward Iterator, and ForwardIterator is mutable, and if x and y are objects of ForwardIterator's value_type, then x + y is defined, and if T is ForwardIterator's value_type, then T(0) is defined.
Tis a model of Assignable, and T is convertible to ForwardIterator's value_type.

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.
See also:
http://www.sgi.com/tech/stl/iota.html
template<typename InputIterator1 , typename InputIterator2 , typename OutputIterator , typename BinaryFunction >
OutputIterator thrust::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.

Parameters:
first1The beginning of the first input sequence.
last1The end of the first input sequence.
first2The beginning of the second input sequence.
resultThe beginning of the output sequence.
opThe tranformation operation.
Returns:
The end of the output sequence.
Template Parameters:
InputIterator1is a model of Input Iterator and InputIterator1's value_type is convertible to BinaryFunction's first_argument_type.
InputIterator2is a model of Input Iterator and InputIterator2's value_type is convertible to BinaryFunction's second_argument_type.
OutputIteratoris a model of Output Iterator.
BinaryFunctionis a model of Binary Function and BinaryFunction's result_type is convertible to OutputIterator's value_type.

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};
See also:
http://www.sgi.com/tech/stl/transform.html
template<typename InputIterator , typename OutputIterator , typename UnaryFunction >
OutputIterator thrust::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.

Parameters:
firstThe beginning of the input sequence.
lastThe end of the input sequence.
resultThe beginning of the output sequence.
opThe tranformation operation.
Returns:
The end of the output sequence.
Template Parameters:
InputIteratoris a model of Input Iterator and InputIterator's value_type is convertible to UnaryFunction's argument_type.
OutputIteratoris a model of Output Iterator.
UnaryFunctionis a model of Unary Function and UnaryFunction's result_type is convertible to OutputIterator's value_type.

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};
See also:
http://www.sgi.com/tech/stl/transform.html
template<typename InputIterator1 , typename InputIterator2 , typename ForwardIterator , typename UnaryFunction , typename Predicate >
ForwardIterator thrust::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.

Parameters:
firstThe beginning of the input sequence.
lastThe end of the input sequence.
stencilThe beginning of the stencil sequence.
resultThe beginning of the output sequence.
opThe tranformation operation.
predThe predicate operation.
Returns:
The end of the output sequence.
Template Parameters:
InputIterator1is a model of Input Iterator and InputIterator1's value_type is convertible to UnaryFunction's argument_type.
InputIterator2is a model of Input Iterator and InputIterator2's value_type is convertible to Predicate's argument_type.
ForwardIteratoris a model of Forward Iterator.
UnaryFunctionis a model of Unary Function and UnaryFunction's result_type is convertible to OutputIterator's value_type.
Predicateis a model of Predicate.

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};
See also:
thrust::transform
template<typename InputIterator1 , typename InputIterator2 , typename InputIterator3 , typename ForwardIterator , typename BinaryFunction , typename Predicate >
ForwardIterator thrust::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.

Parameters:
first1The beginning of the first input sequence.
last1The end of the first input sequence.
first2The beginning of the second input sequence.
stencilThe beginning of the stencil sequence.
resultThe beginning of the output sequence.
binary_opThe transformation operation.
predThe predicate operation.
Returns:
The end of the output sequence.
Template Parameters:
InputIterator1is a model of Input Iterator and InputIterator1's value_type is convertible to BinaryFunction's first_argument_type.
InputIterator2is a model of Input Iterator and InputIterator2's value_type is convertible to BinaryFunction's second_argument_type.
ForwardIteratoris a model of Forward Iterator.
BinaryFunctionis a model of Binary Function and BinaryFunction's result_type is convertible to OutputIterator's value_type.
Predicateis a model of Predicate.

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};
See also:
thrust::transform
template<typename InputIterator , typename ForwardIterator , typename UnaryFunction , typename Predicate >
ForwardIterator thrust::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.

Parameters:
firstThe beginning of the input sequence.
lastThe end of the input sequence.
resultThe beginning of the output sequence.
opThe tranformation operation.
predThe predicate operation.
Returns:
The end of the output sequence.
Template Parameters:
InputIteratoris a model of Input Iterator, and InputIterator's value_type is convertible to Predicate's argument_type, and InputIterator's value_type is convertible to UnaryFunction's argument_type.
ForwardIteratoris a model of Forward Iterator.
UnaryFunctionis a model of Unary Function and UnaryFunction's result_type is convertible to OutputIterator's value_type.
Predicateis a model of Predicate.

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};
See also:
thrust::transform