API Reference Guide

Contents

API Reference Guide#

This chapter describes the rocThrust C++ API.

Memory Management#

group memory_management

All Thrust functionalities related to memory allocation and deallocation.

Functions

template<typename T>
inline void device_delete(thrust::device_ptr<T> ptr, const size_t n = 1)#

device_delete deletes a device_ptr allocated with device_new.

See also

device_ptr

See also

device_new

Parameters:
  • ptr – The device_ptr to delete, assumed to have been allocated with device_new.

  • n – The number of objects to destroy at ptr. Defaults to 1 similar to device_new.

inline void device_free(thrust::device_ptr<void> ptr)#

device_free deallocates memory allocated by the function device_malloc.

The following code snippet demonstrates how to use device_free to deallocate memory allocated by device_malloc.

#include <thrust/device_malloc.h>
#include <thrust/device_free.h>
...
// allocate some integers with device_malloc
const int N = 100;
thrust::device_ptr<int> int_array = thrust::device_malloc<int>(N);

// manipulate integers
...

// deallocate with device_free
thrust::device_free(int_array);

See also

device_ptr

See also

device_malloc

Parameters:

ptr – A device_ptr pointing to memory to be deallocated.

template<typename T>
inline thrust::device_ptr<T> device_malloc(const std::size_t n)#

This version of device_malloc allocates sequential device storage for new objects of the given type.

The following code snippet demonstrates how to use device_malloc to allocate a range of device memory.

#include <thrust/device_malloc.h>
#include <thrust/device_free.h>
...
// allocate some integers with device_malloc
const int N = 100;
thrust::device_ptr<int> int_array = thrust::device_malloc<int>(N);

// manipulate integers
...

// deallocate with device_free
thrust::device_free(int_array);

See also

device_ptr

See also

device_free

Parameters:

n – The number of objects of type T to allocate sequentially in device memory.

Returns:

A device_ptr to the newly allocated memory.

inline thrust::device_ptr<void> device_malloc(const std::size_t n)#

This version of device_malloc allocates untyped sequential device storage.

The following code snippet demonstrates how to use device_malloc to allocate a range of device memory.

#include <thrust/device_malloc.h>
#include <thrust/device_free.h>
...
// allocate some memory with device_malloc
const int N = 100;
thrust::device_ptr<void> void_ptr = thrust::device_malloc(N);

// manipulate memory
...

// deallocate with device_free
thrust::device_free(void_ptr);

See also

device_ptr

See also

device_free

Parameters:

n – The number of bytes to allocate sequentially in device memory.

Returns:

A device_ptr to the newly allocated memory.

template<typename T>
device_ptr<T> device_new(device_ptr<void> p, const size_t n = 1)#

device_new implements the placement new operator for types resident in device memory. device_new calls T’s null constructor on a array of objects in device memory. No memory is allocated by this function.

See also

device_ptr

Parameters:
  • p – A device_ptr to a region of device memory into which to construct one or many Ts.

  • n – The number of objects to construct at p.

Returns:

p, casted to T’s type.

template<typename T>
device_ptr<T> device_new(device_ptr<void> p, const T &exemplar, const size_t n = 1)#

device_new implements the placement new operator for types resident in device memory. device_new calls T’s copy constructor on a array of objects in device memory. No memory is allocated by this function.

See also

device_ptr

See also

fill

Parameters:
  • p – A device_ptr to a region of device memory into which to construct one or many Ts.

  • exemplar – The value from which to copy.

  • n – The number of objects to construct at p.

Returns:

p, casted to T’s type.

template<typename T>
device_ptr<T> device_new(const size_t n = 1)#

device_new implements the new operator for types resident in device memory. It allocates device memory large enough to hold n new objects of type T.

Parameters:

n – The number of objects to allocate. Defaults to 1.

Returns:

A device_ptr to the newly allocated region of device memory.

template<typename T> __host__ __device__ device_ptr< T > device_pointer_cast (T *ptr)

Create a device_ptr from a raw pointer.

Template Parameters:

T – Any type.

Parameters:

ptr – A raw pointer to a T in device memory.

Pre:

ptr points to a location in device memory.

Returns:

A device_ptr<T> pointing to ptr.

template<typename T> __host__ __device__ device_ptr< T > device_pointer_cast (device_ptr< T > const &dptr)

Create a device_ptr from another device_ptr.

Template Parameters:

T – Any type.

Parameters:

dptr – A device_ptr to a T.

template<typename T> __host__ __device__ void swap (device_reference< T > &x, device_reference< T > &y)

swaps the value of one device_reference with another. x The first device_reference of interest. y The second device_reference of interest.

template<typename DerivedPolicy> __host__ __device__ pointer< void, DerivedPolicy > malloc (const thrust::detail::execution_policy_base< DerivedPolicy > &system, std::size_t n)

This version of malloc allocates untyped uninitialized storage associated with a given system.

The following code snippet demonstrates how to use malloc to allocate a range of memory associated with Thrust’s device system.

#include <thrust/memory.h>
...
// allocate some memory with thrust::malloc
const int N = 100;
thrust::device_system_tag device_sys;
thrust::pointer<void,thrust::device_space_tag> void_ptr = thrust::malloc(device_sys, N);

// manipulate memory
...

// deallocate void_ptr with thrust::free
thrust::free(device_sys, void_ptr);

See also

free

See also

device_malloc

Parameters:
  • system – The Thrust system with which to associate the storage.

  • n – The number of bytes of storage to allocate.

Template Parameters:

DerivedPolicy – The name of the derived execution policy.

Returns:

If allocation succeeds, a pointer to the allocated storage; a null pointer otherwise. The pointer must be deallocated with thrust::free.

Pre:

DerivedPolicy must be publically derived from thrust::execution_policy<DerivedPolicy>.

template<typename T, typename DerivedPolicy> __host__ __device__ pointer< T, DerivedPolicy > malloc (const thrust::detail::execution_policy_base< DerivedPolicy > &system, std::size_t n)

This version of malloc allocates typed uninitialized storage associated with a given system.

The following code snippet demonstrates how to use malloc to allocate a range of memory to accomodate integers associated with Thrust’s device system.

#include <thrust/memory.h>
...
// allocate storage for 100 ints with thrust::malloc
const int N = 100;
thrust::device_system_tag device_sys;
thrust::pointer<int,thrust::device_system_tag> ptr = thrust::malloc<int>(device_sys, N);

// manipulate memory
...

// deallocate ptr with thrust::free
thrust::free(device_sys, ptr);

See also

free

See also

device_malloc

Parameters:
  • system – The Thrust system with which to associate the storage.

  • n – The number of elements of type T which the storage should accomodate.

Template Parameters:

DerivedPolicy – The name of the derived execution policy.

Returns:

If allocation succeeds, a pointer to an allocation large enough to accomodate n elements of type T; a null pointer otherwise. The pointer must be deallocated with thrust::free.

Pre:

DerivedPolicy must be publically derived from thrust::execution_policy<DerivedPolicy>.

template<typename T, typename DerivedPolicy> __host__ __device__ thrust::pair< thrust::pointer< T, DerivedPolicy >, typename thrust::pointer< T, DerivedPolicy >::difference_type > get_temporary_buffer (const thrust::detail::execution_policy_base< DerivedPolicy > &system, typename thrust::pointer< T, DerivedPolicy >::difference_type n)

get_temporary_buffer returns a pointer to storage associated with a given Thrust system sufficient to store up to n objects of type T. If not enough storage is available to accomodate n objects, an implementation may return a smaller buffer. The number of objects the returned buffer can accomodate is also returned.

Thrust uses get_temporary_buffer internally when allocating temporary storage required by algorithm implementations.

The storage allocated with get_temporary_buffer must be returned to the system with return_temporary_buffer.

The following code snippet demonstrates how to use get_temporary_buffer to allocate a range of memory to accomodate integers associated with Thrust’s device system.

#include <thrust/memory.h>
...
// allocate storage for 100 ints with thrust::get_temporary_buffer
const int N = 100;

typedef thrust::pair<
  thrust::pointer<int,thrust::device_system_tag>,
  std::ptrdiff_t
> ptr_and_size_t;

thrust::device_system_tag device_sys;
ptr_and_size_t ptr_and_size = thrust::get_temporary_buffer<int>(device_sys, N);

// manipulate up to 100 ints
for(int i = 0; i < ptr_and_size.second; ++i)
{
  *ptr_and_size.first = i;
}

// deallocate storage with thrust::return_temporary_buffer
thrust::return_temporary_buffer(device_sys, ptr_and_size.first);

See also

malloc

Parameters:
  • system – The Thrust system with which to associate the storage.

  • n – The requested number of objects of type T the storage should accomodate.

Template Parameters:

DerivedPolicy – The name of the derived execution policy.

Returns:

A pair p such that p.first is a pointer to the allocated storage and p.second is the number of contiguous objects of type T that the storage can accomodate. If no storage can be allocated, p.first if no storage can be obtained. The storage must be returned to the system using return_temporary_buffer.

Pre:

DerivedPolicy must be publically derived from thrust::execution_policy<DerivedPolicy>.

template<typename DerivedPolicy, typename Pointer> __host__ __device__ void free (const thrust::detail::execution_policy_base< DerivedPolicy > &system, Pointer ptr)

free deallocates the storage previously allocated by thrust::malloc.

The following code snippet demonstrates how to use free to deallocate a range of memory previously allocated with thrust::malloc.

#include <thrust/memory.h>
...
// allocate storage for 100 ints with thrust::malloc
const int N = 100;
thrust::device_system_tag device_sys;
thrust::pointer<int,thrust::device_system_tag> ptr = thrust::malloc<int>(device_sys, N);

// mainpulate memory
...

// deallocate ptr with thrust::free
thrust::free(device_sys, ptr);
Parameters:
  • system – The Thrust system with which the storage is associated.

  • ptr – A pointer previously returned by thrust::malloc. If ptr is null, free does nothing.

Template Parameters:

DerivedPolicy – The name of the derived execution policy.

Pre:

ptr shall have been returned by a previous call to thrust::malloc(system, n) or thrust::malloc<T>(system, n) for some type T.

template<typename DerivedPolicy, typename Pointer> __host__ __device__ void return_temporary_buffer (const thrust::detail::execution_policy_base< DerivedPolicy > &system, Pointer p, std::ptrdiff_t n)

return_temporary_buffer deallocates storage associated with a given Thrust system previously allocated by get_temporary_buffer.

Thrust uses return_temporary_buffer internally when deallocating temporary storage required by algorithm implementations.

The following code snippet demonstrates how to use return_temporary_buffer to deallocate a range of memory previously allocated by get_temporary_buffer.

#include <thrust/memory.h>
...
// allocate storage for 100 ints with thrust::get_temporary_buffer
const int N = 100;

typedef thrust::pair<
  thrust::pointer<int,thrust::device_system_tag>,
  std::ptrdiff_t
> ptr_and_size_t;

thrust::device_system_tag device_sys;
ptr_and_size_t ptr_and_size = thrust::get_temporary_buffer<int>(device_sys, N);

// manipulate up to 100 ints
for(int i = 0; i < ptr_and_size.second; ++i)
{
  *ptr_and_size.first = i;
}

// deallocate storage with thrust::return_temporary_buffer
thrust::return_temporary_buffer(device_sys, ptr_and_size.first);

See also

free

Parameters:
  • system – The Thrust system with which the storage is associated.

  • p – A pointer previously returned by thrust::get_temporary_buffer. If ptr is null, return_temporary_buffer does nothing.

  • n

Template Parameters:

DerivedPolicy – The name of the derived execution policy.

Pre:

p shall have been previously allocated by thrust::get_temporary_buffer.

template<typename Pointer> __host__ __device__ thrust::detail::pointer_traits< Pointer >::raw_pointer raw_pointer_cast (Pointer ptr)

raw_pointer_cast creates a “raw” pointer from a pointer-like type, simply returning the wrapped pointer, should it exist.

Parameters:

ptr – The pointer of interest.

Returns:

ptr.get(), if the expression is well formed; ptr, otherwise.

template<typename T> __host__ __device__ detail::raw_reference< T >::type raw_reference_cast (T &ref)

raw_reference_cast creates a “raw” reference from a wrapped reference type, simply returning the underlying reference, should it exist.

If the argument is not a reference wrapper, the result is a reference to the argument.

See also

raw_pointer_cast

Note

There are two versions of raw_reference_cast. One for const references, and one for non-const.

Parameters:

ref – The reference of interest.

Returns:

*raw_pointer_cast(&ref).

template<typename T> __host__ __device__ detail::raw_reference< constT >::type raw_reference_cast (const T &ref)

raw_reference_cast creates a “raw” reference from a wrapped reference type, simply returning the underlying reference, should it exist.

If the argument is not a reference wrapper, the result is a reference to the argument.

See also

raw_pointer_cast

Note

There are two versions of raw_reference_cast. One for const references, and one for non-const.

Parameters:

ref – The reference of interest.

Returns:

*raw_reference_cast(&ref).

template<typename T>
class device_reference : public thrust::reference<T, thrust::device_ptr<T>, thrust::device_reference<T>>#
#include <device_reference.h>

device_reference acts as a reference-like object to an object stored in device memory. device_reference is not intended to be used directly; rather, this type is the result of deferencing a device_ptr. Similarly, taking the address of a device_reference yields a device_ptr.

device_reference may often be used from host code in place of operations defined on its associated value_type. For example, when device_reference refers to an arithmetic type, arithmetic operations on it are legal:

#include <thrust/device_vector.h>

int main(void)
{
  thrust::device_vector<int> vec(1, 13);

  thrust::device_reference<int> ref_to_thirteen = vec[0];

  int x = ref_to_thirteen + 1;

  // x is 14

  return 0;
}

Similarly, we can print the value of ref_to_thirteen in the above code by using an iostream:

#include <thrust/device_vector.h>
#include <iostream>

int main(void)
{
  thrust::device_vector<int> vec(1, 13);

  thrust::device_reference<int> ref_to_thirteen = vec[0];

  std::cout << ref_to_thirteen << std::endl;

  // 13 is printed

  return 0;
}

Of course, we needn’t explicitly create a device_reference in the previous example, because one is returned by device_vector's bracket operator. A more natural way to print the value of a device_vector element might be:

#include <thrust/device_vector.h>
#include <iostream>

int main(void)
{
  thrust::device_vector<int> vec(1, 13);

  std::cout << vec[0] << std::endl;

  // 13 is printed

  return 0;
}

These kinds of operations should be used sparingly in performance-critical code, because they imply a potentially expensive copy between host and device space.

Some operations which are possible with regular objects are impossible with their corresponding device_reference objects due to the requirements of the C++ language. For example, because the member access operator cannot be overloaded, member variables and functions of a referent object cannot be directly accessed through its device_reference.

The following code, which generates a compiler error, illustrates:

#include <thrust/device_vector.h>

struct foo
{
  int x;
};

int main(void)
{
  thrust::device_vector<foo> foo_vec(1);

  thrust::device_reference<foo> foo_ref = foo_vec[0];

  foo_ref.x = 13; // ERROR: x cannot be accessed through foo_ref

  return 0;
}

Instead, a host space copy must be created to access foo's x member:

#include <thrust/device_vector.h>

struct foo
{
  int x;
};

int main(void)
{
  thrust::device_vector<foo> foo_vec(1);

  // create a local host-side foo object
  foo host_foo;
  host_foo.x = 13;

  thrust::device_reference<foo> foo_ref = foo_vec[0];

  foo_ref = host_foo;

  // foo_ref's x member is 13

  return 0;
}

Another common case where a device_reference cannot directly be used in place of its referent object occurs when passing them as parameters to functions like printf which have varargs parameters. Because varargs parameters must be Plain Old Data, a device_reference to a POD type requires a cast when passed to printf:

#include <stdio.h>
#include <thrust/device_vector.h>

int main(void)
{
  thrust::device_vector<int> vec(1,13);

  // vec[0] must be cast to int when passing to printf
  printf("%d\n", (int) vec[0]);

  return 0;
}

See also

device_ptr

See also

device_vector

template<typename T>
class device_ptr : public thrust::pointer<T, thrust::device_system_tag, thrust::device_reference<T>, thrust::device_ptr<T>>#
#include <device_ptr.h>

forward declaration to WAR circular #includes

device_ptr is a pointer-like object which points to an object that resides in memory associated with the device system.

device_ptr has pointer semantics: it may be dereferenced safely from anywhere, including the host, and may be manipulated with pointer arithmetic.

device_ptr can be created with device_new, device_malloc, device_malloc_allocator, device_allocator, or device_pointer_cast, or by explicitly calling its constructor with a raw pointer.

The raw pointer contained in a device_ptr may be obtained via get member function or the raw_pointer_cast free function.

Algorithms operating on device_ptr types will automatically be dispatched to the device system.

See also

device_new

See also

device_malloc

See also

device_allocator

See also

raw_pointer_cast

Note

device_ptr is not a smart pointer; it is the programmer’s responsibility to deallocate memory pointed to by device_ptr.

Functions

template<typename T, typename MR> __host__ __device__ bool operator== (const allocator< T, MR > &lhs, const allocator< T, MR > &rhs) noexcept

Compares the allocators for equality by comparing the underlying memory resources.

template<typename T, typename MR> __host__ __device__ bool operator!= (const allocator< T, MR > &lhs, const allocator< T, MR > &rhs) noexcept

Compares the allocators for inequality by comparing the underlying memory resources.

template<typename Upstream>
class device_ptr_memory_resource : public thrust::mr::memory_resource<device_ptr<void>>#
#include <device_allocator.h>

Memory resource adaptor that turns any memory resource that returns a fancy with the same tag as device_ptr, and adapts it to a resource that returns a device_ptr.

template<typename T>
class device_allocator : public thrust::mr::stateless_resource_allocator<T, device_ptr_memory_resource<device_memory_resource>>#
#include <device_allocator.h>

An allocator which creates new elements in memory accessible by devices.

template<typename T>
class device_malloc_allocator#
#include <device_malloc_allocator.h>

device_malloc_allocator is a device memory allocator that employs the device_malloc function for allocation.

device_malloc_allocator is deprecated in favor of thrust::mr memory resource-based allocators.

See also

device_malloc

See also

device_ptr

See also

device_allocator

template<typename T>
class device_new_allocator#
#include <device_new_allocator.h>

device_new_allocator is a device memory allocator that employs the device_new function for allocation.

See also

device_new

See also

device_ptr

template<typename T, class MR>
class allocator : private mr::validator<MR>#
#include <allocator.h>

An mr::allocator is a template that fulfills the C++ requirements for Allocators, allowing to use the NPA-based memory resources where an Allocator is required. Unlike memory resources, but like other allocators, mr::allocator is typed and bound to allocate object of a specific type, however it can be freely rebound to other types.

Template Parameters:
  • T – the type that will be allocated by this allocator.

  • MR – the upstream memory resource to use for memory allocation. Must derive from thrust::mr::memory_resource and must be final (in C++11 and beyond).

template<typename T, typename Pointer>
class polymorphic_allocator : public mr::allocator<T, polymorphic_adaptor_resource<Pointer>>#
#include <allocator.h>

An allocator whose behaviour depends on a memory resource that we can dynamically configure at runtime.

Template Parameters:
  • T – - the type that will be allocated by this allocator

  • Pointer – - the pointer type that will be used to create the memory resource

template<typename T, typename Upstream>
class stateless_resource_allocator : public thrust::mr::allocator<T, Upstream>#
#include <allocator.h>

A helper allocator class that uses global instances of a given upstream memory resource. Requires the memory resource to be default constructible.

Template Parameters:
  • T – the type that will be allocated by this allocator.

  • Upstream – the upstream memory resource to use for memory allocation. Must derive from thrust::mr::memory_resource and must be final (in C++11 and beyond).

Typedefs

template<typename T>
using universal_ptr = thrust::system::__THRUST_DEVICE_SYSTEM_NAMESPACE::universal_pointer<T>#

universal_ptr stores a pointer to an object allocated in memory accessible to both hosts and devices.

Algorithms dispatched with this type of pointer will be dispatched to either host or device, depending on which backend you are using. Explicit policies (thrust::device, etc) can be used to specify where an algorithm should be run.

universal_ptr has pointer semantics: it may be dereferenced safely from both hosts and devices and may be manipulated with pointer arithmetic.

universal_ptr can be created with universal_allocator or by explicitly calling its constructor with a raw pointer.

The raw pointer encapsulated by a universal_ptr may be obtained by either its get method or the raw_pointer_cast free function.

See also

host_ptr For the documentation of the complete interface which is shared by universal_ptr.

See also

raw_pointer_cast

Note

universal_ptr is not a smart pointer; it is the programmer’s responsibility to deallocate memory pointed to by universal_ptr.

Functions

template<typename Pointer> __host__ __device__ bool operator== (const memory_resource< Pointer > &lhs, const memory_resource< Pointer > &rhs) noexcept

Compares the memory resources for equality, first by identity, then by is_equal.

template<typename Pointer> __host__ __device__ bool operator!= (const memory_resource< Pointer > &lhs, const memory_resource< Pointer > &rhs) noexcept

Compares the memory resources for inequality, first by identity, then by is_equal.

template<typename MR> __host__ MR * get_global_resource ()

Returns a global instance of MR, created as a function local static variable.

Template Parameters:

MR – type of a memory resource to get an instance from. Must be DefaultConstructible.

Returns:

a pointer to a global instance of MR.

template<typename Upstream, typename Bookkeeper>
class disjoint_unsynchronized_pool_resource : public mr::memory_resource<Upstream::pointer>, private mr::validator2<Upstream, Bookkeeper>#
#include <disjoint_pool.h>

A memory resource adaptor allowing for pooling and caching allocations from Upstream, using Bookkeeper for management of that cached and pooled memory, allowing to cache portions of memory inaccessible from the host.

On a typical memory resource, calls to allocate and deallocate actually allocate and deallocate memory. Pooling memory resources only allocate and deallocate memory from an external resource (the upstream memory resource) when there’s no suitable memory currently cached; otherwise, they use memory they have acquired beforehand, to make memory allocation faster and more efficient.

The disjoint version of the pool resources uses a separate upstream memory resource, Bookkeeper, to allocate memory necessary to manage the cached memory. There may be many reasons to do that; the canonical one is that Upstream allocates memory that is inaccessible to the code of the pool resource, which means that it cannot embed the necessary information in memory obtained from Upstream; for instance, Upstream can be a CUDA non-managed memory resource, or a CUDA managed memory resource whose memory we would prefer to not migrate back and forth between host and device when executing bookkeeping code.

This is not the only case where it makes sense to use a disjoint pool resource, though. In a multi-core environment it may be beneficial to avoid stealing cache lines from other cores by writing over bookkeeping information embedded in an allocated block of memory. In such a case, one can imagine wanting to use a disjoint pool where both the upstream and the bookkeeper are of the same type, to allocate memory consistently, but separately for those two purposes.

Template Parameters:
  • Upstream – the type of memory resources that will be used for allocating memory blocks to be handed off to the user

  • Bookkeeper – the type of memory resources that will be used for allocating bookkeeping memory

template<typename Pointer = void*>
class memory_resource#
#include <memory_resource.h>

memory_resource is the base class for all other memory resources.

Template Parameters:

Pointer – the pointer type that is allocated and deallocated by the memory resource derived from this base class. If this is void *, this class derives from std::pmr::memory_resource.

Subclassed by mr::new_delete_resource, mr::polymorphic_adaptor_resource< Pointer >

class new_delete_resource : public mr::memory_resource<>#
#include <new.h>

A memory resource that uses global operators new and delete to allocate and deallocate memory. Uses alignment-enabled overloads when available, otherwise uses regular overloads and implements alignment requirements by itself.

template<typename Upstream>
class unsynchronized_pool_resource : public mr::memory_resource<Upstream::pointer>, private mr::validator<Upstream>#
#include <pool.h>

A memory resource adaptor allowing for pooling and caching allocations from Upstream, using memory allocated from it for both blocks then allocated to the user and for internal bookkeeping of the cached memory.

On a typical memory resource, calls to allocate and deallocate actually allocate and deallocate memory. Pooling memory resources only allocate and deallocate memory from an external resource (the upstream memory resource) when there’s no suitable memory currently cached; otherwise, they use memory they have acquired beforehand, to make memory allocation faster and more efficient.

The non-disjoint version of the pool resource uses a single upstream memory resource. Every allocation is larger than strictly necessary to fulfill the end-user’s request, because it needs to account for the memory overhead of tracking the memory blocks and chunks inside those same memory regions. Nevertheless, this version should be more memory-efficient than the disjoint_unsynchronized_pool_resource, because it doesn’t need to allocate additional blocks of memory from a separate resource, which in turn would necessitate the bookkeeping overhead in the upstream resource.

This version requires that memory allocated from Upstream is accessible from device. It supports smart references, meaning that the non-managed CUDA resource, returning a device-tagged pointer, will work, but will be much less efficient than the disjoint version, which wouldn’t need to touch device memory at all, and therefore wouldn’t need to transfer it back and forth between the host and the device whenever an allocation or a deallocation happens.

Template Parameters:

Upstream – the type of memory resources that will be used for allocating memory blocks

struct pool_options#
#include <pool_options.h>

A type used for configuring pooling resource adaptors, to fine-tune their behavior and parameters.

Iterators#

group iterators

Functions

template<typename InputIterator, typename Distance> __host__ __device__ void advance (InputIterator &i, Distance n)

advance(i, n) increments the iterator i by the distance n. If n > 0 it is equivalent to executing ++i n times, and if n < 0 it is equivalent to executing i n times. If n == 0, the call has no effect.

The following code snippet demonstrates how to use advance to increment an iterator a given number of times.

#include <thrust/advance.h>
#include <thrust/device_vector.h>
...
thrust::device_vector<int> vec(13);
thrust::device_vector<int>::iterator iter = vec.begin();

thrust::advance(iter, 7);

// iter - vec.begin() == 7

Parameters:
  • i – The iterator to be advanced.

  • n – The distance by which to advance the iterator.

Template Parameters:
  • InputIterator – is a model of Input Iterator.

  • Distance – is an integral type that is convertible to InputIterator's distance type.

Pre:

n shall be negative only for bidirectional and random access iterators.

template<typename InputIterator> inline __host__ __device__ thrust::iterator_traits< InputIterator >::difference_type distance (InputIterator first, InputIterator last)

distance finds the distance between first and last, i.e. the number of times that first must be incremented until it is equal to last.

The following code snippet demonstrates how to use distance to compute the distance to one iterator from another.

#include <thrust/distance.h>
#include <thrust/device_vector.h>
...
thrust::device_vector<int> vec(13);
thrust::device_vector<int>::iterator iter1 = vec.begin();
thrust::device_vector<int>::iterator iter2 = iter1 + 7;

int d = thrust::distance(iter1, iter2);

// d is 7

Parameters:
  • first – The beginning of an input range of interest.

  • last – The end of an input range of interest.

Template Parameters:

InputIterator – is a model of Input Iterator.

Returns:

The distance between the beginning and end of the input range.

Pre:

If InputIterator meets the requirements of random access iterator, last shall be reachable from first or first shall be reachable from last; otherwise, last shall be reachable from first.

Functions

template<typename ValueT, typename IndexT> inline __host__ __device__ constant_iterator< ValueT, IndexT > make_constant_iterator (ValueT x, IndexT i=int())

This version of make_constant_iterator creates a constant_iterator from values given for both value and index. The type of constant_iterator may be inferred by the compiler from the types of its parameters.

Parameters:
  • x – The value of the returned constant_iterator's constant value.

  • i – The index of the returned constant_iterator within a sequence. The type of this parameter defaults to int. In the default case, the value of this parameter is 0.

Returns:

A new constant_iterator with constant value & index as given by x & i.

template<typename V> inline __host__ __device__ constant_iterator< V > make_constant_iterator (V x)

This version of make_constant_iterator creates a constant_iterator using only a parameter for the desired constant value. The value of the returned constant_iterator's index is set to 0.

Parameters:

x – The value of the returned constant_iterator's constant value.

Returns:

A new constant_iterator with constant value equal to x and index equal to 0.

template<typename Incrementable> inline __host__ __device__ counting_iterator< Incrementable > make_counting_iterator (Incrementable x)

make_counting_iterator creates a counting_iterator using an initial value for its Incrementable counter.

Parameters:

x – The initial value of the new counting_iterator's counter.

Returns:

A new counting_iterator whose counter has been initialized to x.

inline __host__ __device__ discard_iterator make_discard_iterator (discard_iterator<>::difference_type i=discard_iterator<>::difference_type(0))

make_discard_iterator creates a discard_iterator from an optional index parameter.

Parameters:

i – The index of the returned discard_iterator within a range. In the default case, the value of this parameter is 0.

Returns:

A new discard_iterator with index as given by i.

template<typename ElementIterator, typename IndexIterator> __host__ __device__ permutation_iterator< ElementIterator, IndexIterator > make_permutation_iterator (ElementIterator e, IndexIterator i)

make_permutation_iterator creates a permutation_iterator from an ElementIterator pointing to a range of elements to “permute” and an IndexIterator pointing to a range of indices defining an indexing scheme on the values.

Parameters:
  • e – An ElementIterator pointing to a range of values.

  • i – An IndexIterator pointing to an indexing scheme to use on e.

Returns:

A new permutation_iterator which permutes the range e by i.

template<typename BidirectionalIterator> __host__ __device__ reverse_iterator< BidirectionalIterator > make_reverse_iterator (BidirectionalIterator x)

make_reverse_iterator creates a reverse_iterator from a BidirectionalIterator pointing to a range of elements to reverse.

Parameters:

x – A BidirectionalIterator pointing to a range to reverse.

Returns:

A new reverse_iterator which reverses the range x.

template<typename InputFunction, typename OutputFunction, typename Iterator> transform_input_output_iterator< InputFunction, OutputFunction, Iterator > __host__ __device__ make_transform_input_output_iterator (Iterator io, InputFunction input_function, OutputFunction output_function)

make_transform_input_output_iterator creates a transform_input_output_iterator from an Iterator a InputFunction and a OutputFunction

Parameters:
  • io – An Iterator pointing to where the input to InputFunction will be read from and the result of OutputFunction will be written to

  • input_function – An InputFunction to be executed on values read from the iterator

  • output_function – An OutputFunction to be executed on values written to the iterator

template<class AdaptableUnaryFunction, class Iterator> inline __host__ __device__ transform_iterator< AdaptableUnaryFunction, Iterator > make_transform_iterator (Iterator it, AdaptableUnaryFunction fun)

make_transform_iterator creates a transform_iterator from an Iterator and AdaptableUnaryFunction.

Parameters:
  • it – The Iterator pointing to the input range of the newly created transform_iterator.

  • fun – The AdaptableUnaryFunction used to transform the range pointed to by it in the newly created transform_iterator.

Returns:

A new transform_iterator which transforms the range at it by fun.

template<typename UnaryFunction, typename OutputIterator> transform_output_iterator< UnaryFunction, OutputIterator > __host__ __device__ make_transform_output_iterator (OutputIterator out, UnaryFunction fun)

make_transform_output_iterator creates a transform_output_iterator from an OutputIterator and UnaryFunction.

Parameters:
template<typename... Iterators> inline __host__ __device__ zip_iterator< thrust::tuple< Iterators... > > make_zip_iterator (thrust::tuple< Iterators... > t)

make_zip_iterator creates a zip_iterator from a tuple of iterators.

See also

zip_iterator

Parameters:

t – The tuple of iterators to copy.

Returns:

A newly created zip_iterator which zips the iterators encapsulated in t.

template<typename... Iterators> inline __host__ __device__ zip_iterator< thrust::tuple< Iterators... > > make_zip_iterator (Iterators... its)

make_zip_iterator creates a zip_iterator from iterators.

See also

zip_iterator

Parameters:

its – The iterators to copy.

Returns:

A newly created zip_iterator which zips the iterators.

template<typename Value, typename Incrementable = use_default, typename System = use_default>
class constant_iterator : public detail::constant_iterator_base::type#
#include <constant_iterator.h>

constant_iterator is an iterator which represents a pointer into a range of constant values. This iterator is useful for creating a range filled with the same value without explicitly storing it in memory. Using constant_iterator saves both memory capacity and bandwidth.

The following code snippet demonstrates how to create a constant_iterator whose value_type is int and whose value is 10.

#include <thrust/iterator/constant_iterator.h>

thrust::constant_iterator<int> iter(10);

*iter;    // returns 10
iter[0];  // returns 10
iter[1];  // returns 10
iter[13]; // returns 10

// and so on...

This next example demonstrates how to use a constant_iterator with the thrust::transform function to increment all elements of a sequence by the same value. We will create a temporary constant_iterator with the function make_constant_iterator function in order to avoid explicitly specifying its type:

#include <thrust/iterator/constant_iterator.h>
#include <thrust/transform.h>
#include <thrust/functional.h>
#include <thrust/device_vector.h>

int main()
{
  thrust::device_vector<int> data(4);
  data[0] = 3;
  data[1] = 7;
  data[2] = 2;
  data[3] = 5;

  // add 10 to all values in data
  thrust::transform(data.begin(), data.end(),
                    thrust::make_constant_iterator(10),
                    data.begin(),
                    thrust::plus<int>());

  // data is now [13, 17, 12, 15]

  return 0;
}

template<typename Incrementable, typename System = use_default, typename Traversal = use_default, typename Difference = use_default>
class counting_iterator : public detail::counting_iterator_base::type#
#include <counting_iterator.h>

counting_iterator is an iterator which represents a pointer into a range of sequentially changing values. This iterator is useful for creating a range filled with a sequence without explicitly storing it in memory. Using counting_iterator saves memory capacity and bandwidth.

The following code snippet demonstrates how to create a counting_iterator whose value_type is int and which sequentially increments by 1.

#include <thrust/iterator/counting_iterator.h>
...
// create iterators
thrust::counting_iterator<int> first(10);
thrust::counting_iterator<int> last = first + 3;

first[0]   // returns 10
first[1]   // returns 11
first[100] // returns 110

// sum of [first, last)
thrust::reduce(first, last);   // returns 33 (i.e. 10 + 11 + 12)

// initialize vector to [0,1,2,..]
thrust::counting_iterator<int> iter(0);
thrust::device_vector<int> vec(500);
thrust::copy(iter, iter + vec.size(), vec.begin());

This next example demonstrates how to use a counting_iterator with the thrust::copy_if function to compute the indices of the non-zero elements of a device_vector. In this example, we use the make_counting_iterator function to avoid specifying the type of the counting_iterator.

#include <thrust/iterator/counting_iterator.h>
#include <thrust/copy.h>
#include <thrust/functional.h>
#include <thrust/device_vector.h>

int main()
{
 // this example computes indices for all the nonzero values in a sequence

 // sequence of zero and nonzero values
 thrust::device_vector<int> stencil(8);
 stencil[0] = 0;
 stencil[1] = 1;
 stencil[2] = 1;
 stencil[3] = 0;
 stencil[4] = 0;
 stencil[5] = 1;
 stencil[6] = 0;
 stencil[7] = 1;

 // storage for the nonzero indices
 thrust::device_vector<int> indices(8);

 // compute indices of nonzero elements
 typedef thrust::device_vector<int>::iterator IndexIterator;

 // use make_counting_iterator to define the sequence [0, 8)
 IndexIterator indices_end = thrust::copy_if(thrust::make_counting_iterator(0),
                                             thrust::make_counting_iterator(8),
                                             stencil.begin(),
                                             indices.begin(),
                                             thrust::identity<int>());
 // indices now contains [1,2,5,7]

 return 0;
}

template<typename System = use_default>
class discard_iterator : public detail::discard_iterator_base::type#
#include <discard_iterator.h>

discard_iterator is an iterator which represents a special kind of pointer that ignores values written to it upon dereference. This iterator is useful for ignoring the output of certain algorithms without wasting memory capacity or bandwidth. discard_iterator may also be used to count the size of an algorithm’s output which may not be known a priori.

The following code snippet demonstrates how to use discard_iterator to ignore ignore one of the output ranges of reduce_by_key

#include <thrust/iterator/discard_iterator.h>
#include <thrust/reduce.h>
#include <thrust/device_vector.h>

int main()
{
  thrust::device_vector<int> keys(7), values(7);

  keys[0] = 1;
  keys[1] = 3;
  keys[2] = 3;
  keys[3] = 3;
  keys[4] = 2;
  keys[5] = 2;
  keys[6] = 1;

  values[0] = 9;
  values[1] = 8;
  values[2] = 7;
  values[3] = 6;
  values[4] = 5;
  values[5] = 4;
  values[6] = 3;

  thrust::device_vector<int> result(4);

  // we are only interested in the reduced values
  // use discard_iterator to ignore the output keys
  thrust::reduce_by_key(keys.begin(), keys.end(),
                        values.begin(),
                        thrust::make_discard_iterator(),
                        result.begin());

  // result is now [9, 21, 9, 3]

  return 0;
}

template<typename Derived, typename Base, typename Value = use_default, typename System = use_default, typename Traversal = use_default, typename Reference = use_default, typename Difference = use_default>
class iterator_adaptor : public detail::iterator_adaptor_base::type#
#include <iterator_adaptor.h>

iterator_adaptor is an iterator which adapts an existing type of iterator to create a new type of iterator. Most of Thrust’s fancy iterators are defined via inheritance from iterator_adaptor. While composition of these existing Thrust iterators is often sufficient for expressing the desired functionality, it is occasionally more straightforward to derive from iterator_adaptor directly.

To see how to use iterator_adaptor to create a novel iterator type, let’s examine how to use it to define repeat_iterator, a fancy iterator which repeats elements from another range a given number of time:

#include <thrust/iterator/iterator_adaptor.h>

// derive repeat_iterator from iterator_adaptor
template<typename Iterator>
  class repeat_iterator
    : public thrust::iterator_adaptor<
        repeat_iterator<Iterator>, // the first template parameter is the name of the iterator we're creating
        Iterator                   // the second template parameter is the name of the iterator we're adapting
                                   // we can use the default for the additional template parameters
      >
{
  public:
    // shorthand for the name of the iterator_adaptor we're deriving from
    typedef thrust::iterator_adaptor<
      repeat_iterator<Iterator>,
      Iterator
    > super_t;

    __host__ __device__
    repeat_iterator(const Iterator &x, int n) : super_t(x), begin(x), n(n) {}

    // befriend thrust::iterator_core_access to allow it access to the private interface below
    friend class thrust::iterator_core_access;

  private:
    // repeat each element of the adapted range n times
    unsigned int n;

    // used to keep track of where we began
    const Iterator begin;

    // it is private because only thrust::iterator_core_access needs access to it
    __host__ __device__
    typename super_t::reference dereference() const
    {
      return *(begin + (this->base() - begin) / n);
    }
};

Except for the first two, iterator_adaptor's template parameters are optional. When omitted, or when the user specifies thrust::use_default in its place, iterator_adaptor will use a default type inferred from Base.

iterator_adaptor's functionality is derived from and generally equivalent to boost::iterator_adaptor. The exception is Thrust’s addition of the template parameter System, which is necessary to allow Thrust to dispatch an algorithm to one of several parallel backend systems.

iterator_adaptor is a powerful tool for creating custom iterators directly. However, the large set of iterator semantics which must be satisfied for algorithm compatibility can make iterator_adaptor difficult to use correctly. Unless you require the full expressivity of iterator_adaptor, consider building a custom iterator through composition of existing higher-level fancy iterators instead.

Interested users may refer to boost::iterator_adaptor’s documentation for further usage examples.

template<typename Derived, typename Value, typename System, typename Traversal, typename Reference, typename Difference = std::ptrdiff_t>
class iterator_facade#
#include <iterator_facade.h>

iterator_facade is a template which allows the programmer to define a novel iterator with a standards-conforming interface which Thrust can use to reason about algorithm acceleration opportunities.

Because most of a standard iterator’s interface is defined in terms of a small set of core primitives, iterator_facade defines the non-primitive portion mechanically. In principle a novel iterator could explicitly provide the entire interface in an ad hoc fashion but doing so might be tedious and prone to subtle errors.

Often iterator_facade is too primitive a tool to use for defining novel iterators. In these cases, iterator_adaptor or a specific fancy iterator should be used instead.

iterator_facade's functionality is derived from and generally equivalent to boost::iterator_facade. The exception is Thrust’s addition of the template parameter System, which is necessary to allow Thrust to dispatch an algorithm to one of several parallel backend systems. An additional exception is Thrust’s omission of the operator-> member function.

Interested users may refer to boost::iterator_facade’s documentation for usage examples.

Note

iterator_facade's arithmetic operator free functions exist with the usual meanings but are omitted here for brevity.

class iterator_core_access#
#include <iterator_facade.h>

iterator_core_access is the class which user iterator types derived from thrust::iterator_adaptor or thrust::iterator_facade must befriend to allow it to access their private interface.

template<typename ElementIterator, typename IndexIterator>
class permutation_iterator : public thrust::detail::permutation_iterator_base::type#
#include <permutation_iterator.h>

permutation_iterator is an iterator which represents a pointer into a reordered view of a given range. permutation_iterator is an imprecise name; the reordered view need not be a strict permutation. This iterator is useful for fusing a scatter or gather operation with other algorithms.

This iterator takes two arguments:

  • an iterator to the range V on which the “permutation” will be applied

  • the reindexing scheme that defines how the elements of V will be permuted.

Note that permutation_iterator is not limited to strict permutations of the given range V. The distance between begin and end of the reindexing iterators is allowed to be smaller compared to the size of the range V, in which case the permutation_iterator only provides a “permutation” of a subrange of V. The indices neither need to be unique. In this same context, it must be noted that the past-the-end permutation_iterator is completely defined by means of the past-the-end iterator to the indices.

The following code snippet demonstrates how to create a permutation_iterator which represents a reordering of the contents of a device_vector.

#include <thrust/iterator/permutation_iterator.h>
#include <thrust/device_vector.h>
...
thrust::device_vector<float> values(8);
values[0] = 10.0f;
values[1] = 20.0f;
values[2] = 30.0f;
values[3] = 40.0f;
values[4] = 50.0f;
values[5] = 60.0f;
values[6] = 70.0f;
values[7] = 80.0f;

thrust::device_vector<int> indices(4);
indices[0] = 2;
indices[1] = 6;
indices[2] = 1;
indices[3] = 3;

typedef thrust::device_vector<float>::iterator ElementIterator;
typedef thrust::device_vector<int>::iterator   IndexIterator;

thrust::permutation_iterator<ElementIterator,IndexIterator> iter(values.begin(), indices.begin());

*iter;   // returns 30.0f;
iter[0]; // returns 30.0f;
iter[1]; // returns 70.0f;
iter[2]; // returns 20.0f;
iter[3]; // returns 40.0f;

// iter[4] is an out-of-bounds error

*iter   = -1.0f; // sets values[2] to -1.0f;
iter[0] = -1.0f; // sets values[2] to -1.0f;
iter[1] = -1.0f; // sets values[6] to -1.0f;
iter[2] = -1.0f; // sets values[1] to -1.0f;
iter[3] = -1.0f; // sets values[3] to -1.0f;

// values is now {10, -1, -1, -1, 50, 60, -1, 80}

template<typename BidirectionalIterator>
class reverse_iterator : public detail::reverse_iterator_base::type#
#include <reverse_iterator.h>

reverse_iterator is an iterator which represents a pointer into a reversed view of a given range. In this way, reverse_iterator allows backwards iteration through a bidirectional input range.

It is important to note that although reverse_iterator is constructed from a given iterator, it points to the element preceding it. In this way, the past-the-end reverse_iterator of a given range points to the element preceding the first element of the input range. By the same token, the first reverse_iterator of a given range is constructed from a past-the-end iterator of the original range yet points to the last element of the input.

The following code snippet demonstrates how to create a reverse_iterator which represents a reversed view of the contents of a device_vector.

#include <thrust/iterator/reverse_iterator.h>
#include <thrust/device_vector.h>
...
thrust::device_vector<float> v(4);
v[0] = 0.0f;
v[1] = 1.0f;
v[2] = 2.0f;
v[3] = 3.0f;

typedef thrust::device_vector<float>::iterator Iterator;

// note that we point the iterator to the *end* of the device_vector
thrust::reverse_iterator<Iterator> iter(values.end());

*iter;   // returns 3.0f;
iter[0]; // returns 3.0f;
iter[1]; // returns 2.0f;
iter[2]; // returns 1.0f;
iter[3]; // returns 0.0f;

// iter[4] is an out-of-bounds error

Since reversing a range is a common operation, containers like device_vector have nested typedefs for declaration shorthand and methods for constructing reverse_iterators. The following code snippet is equivalent to the previous:

#include <thrust/device_vector.h>
...
thrust::device_vector<float> v(4);
v[0] = 0.0f;
v[1] = 1.0f;
v[2] = 2.0f;
v[3] = 3.0f;

// we use the nested type reverse_iterator to refer to a reversed view of
// a device_vector and the method rbegin() to create a reverse_iterator pointing
// to the beginning of the reversed device_vector
thrust::device_iterator<float>::reverse_iterator iter = values.rbegin();

*iter;   // returns 3.0f;
iter[0]; // returns 3.0f;
iter[1]; // returns 2.0f;
iter[2]; // returns 1.0f;
iter[3]; // returns 0.0f;

// iter[4] is an out-of-bounds error

// similarly, rend() points to the end of the reversed sequence:
assert(values.rend() == (iter + 4));

Finally, the following code snippet demonstrates how to use reverse_iterator to perform a reversed prefix sum operation on the contents of a device_vector:

#include <thrust/device_vector.h>
#include <thrust/scan.h>
...
thrust::device_vector<int> v(5);
v[0] = 0;
v[1] = 1;
v[2] = 2;
v[3] = 3;
v[4] = 4;

thrust::device_vector<int> result(5);

// exclusive scan v into result in reverse
thrust::exclusive_scan(v.rbegin(), v.rend(), result.begin());

// result is now {0, 4, 7, 9, 10}

template<typename InputFunction, typename OutputFunction, typename Iterator>
class transform_input_output_iterator : public detail::transform_input_output_iterator_base::type#
#include <transform_input_output_iterator.h>

transform_input_output_iterator is a special kind of iterator which applies transform functions when reading from or writing to dereferenced values. This iterator is useful for algorithms that operate on a type that needs to be serialized/deserialized from values in another iterator, avoiding the need to materialize intermediate results in memory. This also enables the transform functions to be fused with the operations that read and write to the transform_input_output_iterator.

The following code snippet demonstrates how to create a transform_input_output_iterator which performs different transformations when reading from and writing to the iterator.

#include <thrust/iterator/transform_input_output_iterator.h>
#include <thrust/device_vector.h>

 int main()
 {
   const size_t size = 4;
   thrust::device_vector<float> v(size);

   // Write 1.0f, 2.0f, 3.0f, 4.0f to vector
   thrust::sequence(v.begin(), v.end(), 1);

   // Iterator that returns negated values and writes squared values
   auto iter = thrust::make_transform_input_output_iterator(v.begin(),
       thrust::negate<float>{}, thrust::square<float>{});

   // Iterator negates values when reading
   std::cout << iter[0] << " ";  // -1.0f;
   std::cout << iter[1] << " ";  // -2.0f;
   std::cout << iter[2] << " ";  // -3.0f;
   std::cout << iter[3] << "\n"; // -4.0f;

   // Write 1.0f, 2.0f, 3.0f, 4.0f to iterator
   thrust::sequence(iter, iter + size, 1);

   // Values were squared before writing to vector
   std::cout << v[0] << " ";  // 1.0f;
   std::cout << v[1] << " ";  // 4.0f;
   std::cout << v[2] << " ";  // 9.0f;
   std::cout << v[3] << "\n"; // 16.0f;

 }

template<class AdaptableUnaryFunction, class Iterator, class Reference = use_default, class Value = use_default>
class transform_iterator : public detail::transform_iterator_base::type#
#include <transform_iterator.h>

transform_iterator is an iterator which represents a pointer into a range of values after transformation by a function. This iterator is useful for creating a range filled with the result of applying an operation to another range without either explicitly storing it in memory, or explicitly executing the transformation. Using transform_iterator facilitates kernel fusion by deferring the execution of a transformation until the value is needed while saving both memory capacity and bandwidth.

The following code snippet demonstrates how to create a transform_iterator which represents the result of sqrtf applied to the contents of a device_vector.

#include <thrust/iterator/transform_iterator.h>
#include <thrust/device_vector.h>

// note: functor inherits from unary_function
struct square_root : public thrust::unary_function<float,float>
{
  __host__ __device__
  float operator()(float x) const
  {
    return sqrtf(x);
  }
};

int main()
{
  thrust::device_vector<float> v(4);
  v[0] = 1.0f;
  v[1] = 4.0f;
  v[2] = 9.0f;
  v[3] = 16.0f;

  typedef thrust::device_vector<float>::iterator FloatIterator;

  thrust::transform_iterator<square_root, FloatIterator> iter(v.begin(), square_root());

  *iter;   // returns 1.0f
  iter[0]; // returns 1.0f;
  iter[1]; // returns 2.0f;
  iter[2]; // returns 3.0f;
  iter[3]; // returns 4.0f;

  // iter[4] is an out-of-bounds error
}

This next example demonstrates how to use a transform_iterator with the thrust::reduce function to compute the sum of squares of a sequence. We will create temporary transform_iterators with the make_transform_iterator function in order to avoid explicitly specifying their type:

#include <thrust/iterator/transform_iterator.h>
#include <thrust/device_vector.h>
#include <thrust/reduce.h>
#include <iostream>

// note: functor inherits from unary_function
struct square : public thrust::unary_function<float,float>
{
  __host__ __device__
  float operator()(float x) const
  {
    return x * x;
  }
};

int main()
{
  // initialize a device array
  thrust::device_vector<float> v(4);
  v[0] = 1.0f;
  v[1] = 2.0f;
  v[2] = 3.0f;
  v[3] = 4.0f;

  float sum_of_squares =
   thrust::reduce(thrust::make_transform_iterator(v.begin(), square()),
                  thrust::make_transform_iterator(v.end(),   square()));

  std::cout << "sum of squares: " << sum_of_squares << std::endl;
  return 0;
}

Note that in the previous two examples the transform functor (namely square_root and square) inherits from thrust::unary_function. Inheriting from thrust::unary_function ensures that a functor is a valid AdaptableUnaryFunction and provides all the necessary typedef declarations. The transform_iterator can also be applied to a UnaryFunction that does not inherit from thrust::unary_function using an optional template argument. The following example illustrates how to use the third template argument to specify the result_type of the function.

#include <thrust/iterator/transform_iterator.h>
#include <thrust/device_vector.h>

// note: functor *does not* inherit from unary_function
struct square_root
{
  __host__ __device__
  float operator()(float x) const
  {
    return sqrtf(x);
  }
};

int main()
{
  thrust::device_vector<float> v(4);
  v[0] = 1.0f;
  v[1] = 4.0f;
  v[2] = 9.0f;
  v[3] = 16.0f;

  typedef thrust::device_vector<float>::iterator FloatIterator;

  // note: float result_type is specified explicitly
  thrust::transform_iterator<square_root, FloatIterator, float> iter(v.begin(), square_root());

  *iter;   // returns 1.0f
  iter[0]; // returns 1.0f;
  iter[1]; // returns 2.0f;
  iter[2]; // returns 3.0f;
  iter[3]; // returns 4.0f;

  // iter[4] is an out-of-bounds error
}

template<typename UnaryFunction, typename OutputIterator>
class transform_output_iterator : public detail::transform_output_iterator_base::type#
#include <transform_output_iterator.h>

transform_output_iterator is a special kind of output iterator which transforms a value written upon dereference. This iterator is useful for transforming an output from algorithms without explicitly storing the intermediate result in the memory and applying subsequent transformation, thereby avoiding wasting memory capacity and bandwidth. Using transform_iterator facilitates kernel fusion by deferring execution of transformation until the value is written while saving both memory capacity and bandwidth.

The following code snippet demonstrated how to create a transform_output_iterator which applies sqrtf to the assigning value.

#include <thrust/iterator/transform_output_iterator.h>
#include <thrust/device_vector.h>

// note: functor inherits form unary function
 // note: functor inherits from unary_function
 struct square_root : public thrust::unary_function<float,float>
 {
   __host__ __device__
   float operator()(float x) const
   {
     return sqrtf(x);
   }
 };

 int main()
 {
   thrust::device_vector<float> v(4);

   typedef thrust::device_vector<float>::iterator FloatIterator;
   thrust::transform_output_iterator<square_root, FloatIterator> iter(v.begin(), square_root());

   iter[0] =  1.0f;    // stores sqrtf( 1.0f)
   iter[1] =  4.0f;    // stores sqrtf( 4.0f)
   iter[2] =  9.0f;    // stores sqrtf( 9.0f)
   iter[3] = 16.0f;    // stores sqrtf(16.0f)
   // iter[4] is an out-of-bounds error

   v[0]; // returns 1.0f;
   v[1]; // returns 2.0f;
   v[2]; // returns 3.0f;
   v[3]; // returns 4.0f;

 }

template<typename IteratorTuple>
class zip_iterator : public detail::zip_iterator_base::type#
#include <zip_iterator.h>

zip_iterator is an iterator which represents a pointer into a range of tuples whose elements are themselves taken from a tuple of input iterators. This iterator is useful for creating a virtual array of structures while achieving the same performance and bandwidth as the structure of arrays idiom. zip_iterator also facilitates kernel fusion by providing a convenient means of amortizing the execution of the same operation over multiple ranges.

The following code snippet demonstrates how to create a zip_iterator which represents the result of “zipping” multiple ranges together.

#include <thrust/iterator/zip_iterator.h>
#include <thrust/tuple.h>
#include <thrust/device_vector.h>
...
thrust::device_vector<int> int_v(3);
int_v[0] = 0; int_v[1] = 1; int_v[2] = 2;

thrust::device_vector<float> float_v(3);
float_v[0] = 0.0f; float_v[1] = 1.0f; float_v[2] = 2.0f;

thrust::device_vector<char> char_v(3);
char_v[0] = 'a'; char_v[1] = 'b'; char_v[2] = 'c';

// typedef these iterators for shorthand
typedef thrust::device_vector<int>::iterator   IntIterator;
typedef thrust::device_vector<float>::iterator FloatIterator;
typedef thrust::device_vector<char>::iterator  CharIterator;

// typedef a tuple of these iterators
typedef thrust::tuple<IntIterator, FloatIterator, CharIterator> IteratorTuple;

// typedef the zip_iterator of this tuple
typedef thrust::zip_iterator<IteratorTuple> ZipIterator;

// finally, create the zip_iterator
ZipIterator iter(thrust::make_tuple(int_v.begin(), float_v.begin(), char_v.begin()));

*iter;   // returns (0, 0.0f, 'a')
iter[0]; // returns (0, 0.0f, 'a')
iter[1]; // returns (1, 1.0f, 'b')
iter[2]; // returns (2, 2.0f, 'c')

thrust::get<0>(iter[2]); // returns 2
thrust::get<1>(iter[0]); // returns 0.0f
thrust::get<2>(iter[1]); // returns 'b'

// iter[3] is an out-of-bounds error

Defining the type of a zip_iterator can be complex. The next code example demonstrates how to use the make_zip_iterator function with the make_tuple function to avoid explicitly specifying the type of the zip_iterator. This example shows how to use zip_iterator to copy multiple ranges with a single call to thrust::copy.

#include <thrust/zip_iterator.h>
#include <thrust/tuple.h>
#include <thrust/device_vector.h>

int main()
{
  thrust::device_vector<int> int_in(3), int_out(3);
  int_in[0] = 0;
  int_in[1] = 1;
  int_in[2] = 2;

  thrust::device_vector<float> float_in(3), float_out(3);
  float_in[0] =  0.0f;
  float_in[1] = 10.0f;
  float_in[2] = 20.0f;

  thrust::copy(thrust::make_zip_iterator(thrust::make_tuple(int_in.begin(), float_in.begin())),
               thrust::make_zip_iterator(thrust::make_tuple(int_in.end(),   float_in.end())),
               thrust::make_zip_iterator(thrust::make_tuple(int_out.begin(),float_out.begin())));

  // int_out is now [0, 1, 2]
  // float_out is now [0.0f, 10.0f, 20.0f]

  return 0;
}

See also

make_tuple

See also

tuple

See also

get

Typedefs

typedef std::input_iterator_tag input_host_iterator_tag#

input_host_iterator_tag is an empty class: it has no member functions, member variables, or nested types. It is used solely as a “tag”: a representation of the Input Host Iterator concept within the C++ type system.

typedef std::output_iterator_tag output_host_iterator_tag#

output_host_iterator_tag is an empty class: it has no member functions, member variables, or nested types. It is used solely as a “tag”: a representation of the Output Host Iterator concept within the C++ type system.

typedef std::forward_iterator_tag forward_host_iterator_tag#

forward_host_iterator_tag is an empty class: it has no member functions, member variables, or nested types. It is used solely as a “tag”: a representation of the Forward Host Iterator concept within the C++ type system.

typedef std::bidirectional_iterator_tag bidirectional_host_iterator_tag#

bidirectional_host_iterator_tag is an empty class: it has no member functions, member variables, or nested types. It is used solely as a “tag”: a representation of the Forward Host Iterator concept within the C++ type system.

typedef std::random_access_iterator_tag random_access_host_iterator_tag#

random_access_host_iterator_tag is an empty class: it has no member functions, member variables, or nested types. It is used solely as a “tag”: a representation of the Forward Host Iterator concept within the C++ type system.

struct input_device_iterator_tag : public thrust::detail::iterator_category_with_system_and_traversal<std::input_iterator_tag, thrust::device_system_tag, thrust::single_pass_traversal_tag>#
#include <iterator_categories.h>

input_device_iterator_tag is an empty class: it has no member functions, member variables, or nested types. It is used solely as a “tag”: a representation of the Input Device Iterator concept within the C++ type system.

struct output_device_iterator_tag : public thrust::detail::iterator_category_with_system_and_traversal<std::output_iterator_tag, thrust::device_system_tag, thrust::single_pass_traversal_tag>#
#include <iterator_categories.h>

output_device_iterator_tag is an empty class: it has no member functions, member variables, or nested types. It is used solely as a “tag”: a representation of the Output Device Iterator concept within the C++ type system.

struct forward_device_iterator_tag : public thrust::detail::iterator_category_with_system_and_traversal<std::forward_iterator_tag, thrust::device_system_tag, thrust::forward_traversal_tag>#
#include <iterator_categories.h>

forward_device_iterator_tag is an empty class: it has no member functions, member variables, or nested types. It is used solely as a “tag”: a representation of the Forward Device Iterator concept within the C++ type system.

struct bidirectional_device_iterator_tag : public thrust::detail::iterator_category_with_system_and_traversal<std::bidirectional_iterator_tag, thrust::device_system_tag, thrust::bidirectional_traversal_tag>#
#include <iterator_categories.h>

bidirectional_device_iterator_tag is an empty class: it has no member functions, member variables, or nested types. It is used solely as a “tag”: a representation of the Bidirectional Device Iterator concept within the C++ type system.

struct random_access_device_iterator_tag : public thrust::detail::iterator_category_with_system_and_traversal<std::random_access_iterator_tag, thrust::device_system_tag, thrust::random_access_traversal_tag>#
#include <iterator_categories.h>

random_access_device_iterator_tag is an empty class: it has no member functions, member variables, or nested types. It is used solely as a “tag”: a representation of the Random Access Device Iterator concept within the C++ type system.

Algorithms#

group algorithms

Functions

template<typename DerivedPolicy, typename InputIterator, typename T> __host__ __device__ InputIterator find (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator first, InputIterator last, const T &value)

find returns the first iterator i in the range [first, last) such that *i == value or last if no such iterator exists.

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

#include <thrust/find.h>
#include <thrust/device_vector.h>
#include <thrust/execution_policy.h>
...
thrust::device_vector<int> input(4);

input[0] = 0;
input[1] = 5;
input[2] = 3;
input[3] = 7;

thrust::device_vector<int>::iterator iter;

iter = thrust::find(thrust::device, input.begin(), input.end(), 3); // returns input.first() + 2
iter = thrust::find(thrust::device, input.begin(), input.end(), 5); // returns input.first() + 1
iter = thrust::find(thrust::device, input.begin(), input.end(), 9); // returns input.end()

See also

find_if

See also

mismatch

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – Beginning of the sequence to search.

  • last – End of the sequence to search.

  • value – The value to find.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator – is a model of Input Iterator and InputIterator's value_type is equality comparable to type T.

  • T – is a model of EqualityComparable.

Returns:

The first iterator i such that *i == value or last.

template<typename InputIterator, typename T>
InputIterator find(InputIterator first, InputIterator last, const T &value)#

find returns the first iterator i in the range [first, last) such that *i == value or last if no such iterator exists.

#include <thrust/find.h>
#include <thrust/device_vector.h>
...
thrust::device_vector<int> input(4);

input[0] = 0;
input[1] = 5;
input[2] = 3;
input[3] = 7;

thrust::device_vector<int>::iterator iter;

iter = thrust::find(input.begin(), input.end(), 3); // returns input.first() + 2
iter = thrust::find(input.begin(), input.end(), 5); // returns input.first() + 1
iter = thrust::find(input.begin(), input.end(), 9); // returns input.end()

See also

find_if

See also

mismatch

Parameters:
  • first – Beginning of the sequence to search.

  • last – End of the sequence to search.

  • value – The value to find.

Template Parameters:
Returns:

The first iterator i such that *i == value or last.

template<typename DerivedPolicy, typename InputIterator, typename Predicate> __host__ __device__ InputIterator find_if (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator first, InputIterator last, Predicate pred)

find_if returns the first iterator i in the range [first, last) such that pred(*i) is true or last if no such iterator exists.

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

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

struct greater_than_four
{
  __host__ __device__
  bool operator()(int x)
  {
    return x > 4;
  }
};

struct greater_than_ten
{
  __host__ __device__
  bool operator()(int x)
  {
    return x > 10;
  }
};

...
thrust::device_vector<int> input(4);

input[0] = 0;
input[1] = 5;
input[2] = 3;
input[3] = 7;

thrust::device_vector<int>::iterator iter;

iter = thrust::find_if(thrust::device, input.begin(), input.end(), greater_than_four()); // returns input.first() + 1

iter = thrust::find_if(thrust::device, input.begin(), input.end(), greater_than_ten());  // returns input.end()

See also

find

See also

find_if_not

See also

mismatch

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – Beginning of the sequence to search.

  • last – End of the sequence to search.

  • pred – A predicate used to test range elements.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator – is a model of Input Iterator.

  • Predicate – is a model of Predicate.

Returns:

The first iterator i such that pred(*i) is true, or last.

template<typename InputIterator, typename Predicate>
InputIterator find_if(InputIterator first, InputIterator last, Predicate pred)#

find_if returns the first iterator i in the range [first, last) such that pred(*i) is true or last if no such iterator exists.

#include <thrust/find.h>
#include <thrust/device_vector.h>

struct greater_than_four
{
  __host__ __device__
  bool operator()(int x)
  {
    return x > 4;
  }
};

struct greater_than_ten
{
  __host__ __device__
  bool operator()(int x)
  {
    return x > 10;
  }
};

...
thrust::device_vector<int> input(4);

input[0] = 0;
input[1] = 5;
input[2] = 3;
input[3] = 7;

thrust::device_vector<int>::iterator iter;

iter = thrust::find_if(input.begin(), input.end(), greater_than_four()); // returns input.first() + 1

iter = thrust::find_if(input.begin(), input.end(), greater_than_ten());  // returns input.end()

See also

find

See also

find_if_not

See also

mismatch

Parameters:
  • first – Beginning of the sequence to search.

  • last – End of the sequence to search.

  • pred – A predicate used to test range elements.

Template Parameters:
Returns:

The first iterator i such that pred(*i) is true, or last.

template<typename DerivedPolicy, typename InputIterator, typename Predicate> __host__ __device__ InputIterator find_if_not (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator first, InputIterator last, Predicate pred)

find_if_not returns the first iterator i in the range [first, last) such that pred(*i) is false or last if no such iterator exists.

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

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

struct greater_than_four
{
  __host__ __device__
  bool operator()(int x)
  {
    return x > 4;
  }
};

struct greater_than_ten
{
  __host__ __device__
  bool operator()(int x)
  {
    return x > 10;
  }
};

...
thrust::device_vector<int> input(4);

input[0] = 0;
input[1] = 5;
input[2] = 3;
input[3] = 7;

thrust::device_vector<int>::iterator iter;

iter = thrust::find_if_not(thrust::device, input.begin(), input.end(), greater_than_four()); // returns input.first()

iter = thrust::find_if_not(thrust::device, input.begin(), input.end(), greater_than_ten());  // returns input.first()

See also

find

See also

find_if

See also

mismatch

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – Beginning of the sequence to search.

  • last – End of the sequence to search.

  • pred – A predicate used to test range elements.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator – is a model of Input Iterator.

  • Predicate – is a model of Predicate.

Returns:

The first iterator i such that pred(*i) is false, or last.

template<typename InputIterator, typename Predicate>
InputIterator find_if_not(InputIterator first, InputIterator last, Predicate pred)#

find_if_not returns the first iterator i in the range [first, last) such that pred(*i) is false or last if no such iterator exists.

#include <thrust/find.h>
#include <thrust/device_vector.h>

struct greater_than_four
{
  __host__ __device__
  bool operator()(int x)
  {
    return x > 4;
  }
};

struct greater_than_ten
{
  __host__ __device__
  bool operator()(int x)
  {
    return x > 10;
  }
};

...
thrust::device_vector<int> input(4);

input[0] = 0;
input[1] = 5;
input[2] = 3;
input[3] = 7;

thrust::device_vector<int>::iterator iter;

iter = thrust::find_if_not(input.begin(), input.end(), greater_than_four()); // returns input.first()

iter = thrust::find_if_not(input.begin(), input.end(), greater_than_ten());  // returns input.first()

See also

find

See also

find_if

See also

mismatch

Parameters:
  • first – Beginning of the sequence to search.

  • last – End of the sequence to search.

  • pred – A predicate used to test range elements.

Template Parameters:
Returns:

The first iterator i such that pred(*i) is false, or last.

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2> __host__ __device__ thrust::pair< InputIterator1, InputIterator2 > mismatch (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2)

mismatch finds the first position where the two ranges [first1, last1) and [first2, first2 + (last1 - first1)) differ. The two versions of mismatch use different tests for whether elements differ.

This version of mismatch finds the first iterator i in [first1, last1) such that *i == *(first2 + (i - first1)) is false. The return value is a pair whose first element is i and whose second element is *(first2 + (i - first1)). If no such iterator i exists, the return value is a pair whose first element is last1 and whose second element is *(first2 + (last1 - first1)).

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

#include <thrust/mismatch.h>
#include <thrust/device_vector.h>
#include <thrust/execution_policy.h>
...
thrust::device_vector<int> vec1(4);
thrust::device_vector<int> vec2(4);

vec1[0] = 0;  vec2[0] = 0; 
vec1[1] = 5;  vec2[1] = 5;
vec1[2] = 3;  vec2[2] = 8;
vec1[3] = 7;  vec2[3] = 7;

typedef thrust::device_vector<int>::iterator Iterator;
thrust::pair<Iterator,Iterator> result;

result = thrust::mismatch(thrust::device, vec1.begin(), vec1.end(), vec2.begin());

// result.first  is vec1.begin() + 2
// result.second is vec2.begin() + 2

See also

find

See also

find_if

Parameters:
  • exec – The execution policy to use for parallelization.

  • first1 – The beginning of the first sequence.

  • last1 – The end of the first sequence.

  • first2 – The beginning of the second sequence.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator and InputIterator1's value_type is equality comparable to InputIterator2's value_type.

  • InputIterator2 – is a model of Input Iterator.

Returns:

The first position where the sequences differ.

template<typename InputIterator1, typename InputIterator2>
thrust::pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2)#

mismatch finds the first position where the two ranges [first1, last1) and [first2, first2 + (last1 - first1)) differ. The two versions of mismatch use different tests for whether elements differ.

This version of mismatch finds the first iterator i in [first1, last1) such that *i == *(first2 + (i - first1)) is false. The return value is a pair whose first element is i and whose second element is *(first2 + (i - first1)). If no such iterator i exists, the return value is a pair whose first element is last1 and whose second element is *(first2 + (last1 - first1)).

#include <thrust/mismatch.h>
#include <thrust/device_vector.h>
...
thrust::device_vector<int> vec1(4);
thrust::device_vector<int> vec2(4);

vec1[0] = 0;  vec2[0] = 0; 
vec1[1] = 5;  vec2[1] = 5;
vec1[2] = 3;  vec2[2] = 8;
vec1[3] = 7;  vec2[3] = 7;

typedef thrust::device_vector<int>::iterator Iterator;
thrust::pair<Iterator,Iterator> result;

result = thrust::mismatch(vec1.begin(), vec1.end(), vec2.begin());

// result.first  is vec1.begin() + 2
// result.second is vec2.begin() + 2

See also

find

See also

find_if

Parameters:
  • first1 – The beginning of the first sequence.

  • last1 – The end of the first sequence.

  • first2 – The beginning of the second sequence.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator and InputIterator1's value_type is equality comparable to InputIterator2's value_type.

  • InputIterator2 – is a model of Input Iterator.

Returns:

The first position where the sequences differ.

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename BinaryPredicate> __host__ __device__ thrust::pair< InputIterator1, InputIterator2 > mismatch (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate pred)

mismatch finds the first position where the two ranges [first1, last1) and [first2, first2 + (last1 - first1)) differ. The two versions of mismatch use different tests for whether elements differ.

This version of mismatch finds the first iterator i in [first1, last1) such that pred(*i, *(first2 + (i - first1)) is false. The return value is a pair whose first element is i and whose second element is *(first2 + (i - first1)). If no such iterator i exists, the return value is a pair whose first element is last1 and whose second element is *(first2 + (last1 - first1)).

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

#include <thrust/mismatch.h>
#include <thrust/device_vector.h>
#include <thrust/execution_policy.h>
...
thrust::device_vector<int> vec1(4);
thrust::device_vector<int> vec2(4);

vec1[0] = 0;  vec2[0] = 0; 
vec1[1] = 5;  vec2[1] = 5;
vec1[2] = 3;  vec2[2] = 8;
vec1[3] = 7;  vec2[3] = 7;

typedef thrust::device_vector<int>::iterator Iterator;
thrust::pair<Iterator,Iterator> result;

result = thrust::mismatch(thrust::device, vec1.begin(), vec1.end(), vec2.begin(), thrust::equal_to<int>());

// result.first  is vec1.begin() + 2
// result.second is vec2.begin() + 2

See also

find

See also

find_if

Parameters:
  • exec – The execution policy to use for parallelization.

  • first1 – The beginning of the first sequence.

  • last1 – The end of the first sequence.

  • first2 – The beginning of the second sequence.

  • pred – The binary predicate to compare elements.

Template Parameters:
Returns:

The first position where the sequences differ.

template<typename InputIterator1, typename InputIterator2, typename BinaryPredicate>
thrust::pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate pred)#

mismatch finds the first position where the two ranges [first1, last1) and [first2, first2 + (last1 - first1)) differ. The two versions of mismatch use different tests for whether elements differ.

This version of mismatch finds the first iterator i in [first1, last1) such that pred(*i, *(first2 + (i - first1)) is false. The return value is a pair whose first element is i and whose second element is *(first2 + (i - first1)). If no such iterator i exists, the return value is a pair whose first element is last1 and whose second element is *(first2 + (last1 - first1)).

#include <thrust/mismatch.h>
#include <thrust/device_vector.h>
...
thrust::device_vector<int> vec1(4);
thrust::device_vector<int> vec2(4);

vec1[0] = 0;  vec2[0] = 0; 
vec1[1] = 5;  vec2[1] = 5;
vec1[2] = 3;  vec2[2] = 8;
vec1[3] = 7;  vec2[3] = 7;

typedef thrust::device_vector<int>::iterator Iterator;
thrust::pair<Iterator,Iterator> result;

result = thrust::mismatch(vec1.begin(), vec1.end(), vec2.begin(), thrust::equal_to<int>());

// result.first  is vec1.begin() + 2
// result.second is vec2.begin() + 2

See also

find

See also

find_if

Parameters:
  • first1 – The beginning of the first sequence.

  • last1 – The end of the first sequence.

  • first2 – The beginning of the second sequence.

  • pred – The binary predicate to compare elements.

Template Parameters:
Returns:

The first position where the sequences differ.

template<typename DerivedPolicy, typename ForwardIterator, typename Predicate> __host__ __device__ ForwardIterator partition_point (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last, Predicate pred)

partition_point returns an iterator pointing to the end of the true partition of a partitioned range. partition_point requires the input range [first,last) to be a partition; that is, all elements which satisfy pred shall appear before those that do not.

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

#include <thrust/partition.h>
#include <thrust/execution_policy.h>

struct is_even
{
  __host__ __device__
  bool operator()(const int &x)
  {
    return (x % 2) == 0;
  }
};

...

int A[] = {2, 4, 6, 8, 10, 1, 3, 5, 7, 9};
int * B = thrust::partition_point(thrust::host, A, A + 10, is_even());
// B - A is 5
// [A, B) contains only even values

See also

partition

See also

find_if_not

Note

Though similar, partition_point is not redundant with find_if_not. partition_point's precondition provides an opportunity for a faster implemention.

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the range to consider.

  • last – The end of the range to consider.

  • pred – A function object which decides to which partition each element of the range [first, last) belongs.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator, and ForwardIterator's value_type is convertible to Predicate's argument_type.

  • Predicate – is a model of Predicate.

Returns:

An iterator mid such that all_of(first, mid, pred) and none_of(mid, last, pred) are both true.

Pre:

The range [first, last) shall be partitioned by pred.

template<typename ForwardIterator, typename Predicate>
ForwardIterator partition_point(ForwardIterator first, ForwardIterator last, Predicate pred)#

partition_point returns an iterator pointing to the end of the true partition of a partitioned range. partition_point requires the input range [first,last) to be a partition; that is, all elements which satisfy pred shall appear before those that do not.

#include <thrust/partition.h>

struct is_even
{
  __host__ __device__
  bool operator()(const int &x)
  {
    return (x % 2) == 0;
  }
};

...

int A[] = {2, 4, 6, 8, 10, 1, 3, 5, 7, 9};
int * B = thrust::partition_point(A, A + 10, is_even());
// B - A is 5
// [A, B) contains only even values

See also

partition

See also

find_if_not

Note

Though similar, partition_point is not redundant with find_if_not. partition_point's precondition provides an opportunity for a faster implemention.

Parameters:
  • first – The beginning of the range to consider.

  • last – The end of the range to consider.

  • pred – A function object which decides to which partition each element of the range [first, last) belongs.

Template Parameters:
  • ForwardIterator – is a model of Forward Iterator, and ForwardIterator's value_type is convertible to Predicate's argument_type.

  • Predicate – is a model of Predicate.

Returns:

An iterator mid such that all_of(first, mid, pred) and none_of(mid, last, pred) are both true.

Pre:

The range [first, last) shall be partitioned by pred.

Functions

template<typename DerivedPolicy, typename ForwardIterator, typename LessThanComparable> __host__ __device__ ForwardIterator lower_bound (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last, const LessThanComparable &value)

lower_bound is a version of binary search: it attempts to find the element value in an ordered range [first, last). Specifically, it returns the first position where value could be inserted without violating the ordering. This version of lower_bound uses operator< for comparison and returns the furthermost iterator i in [first, last) such that, for every iterator j in [first, i), *j < value.

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

The following code snippet demonstrates how to use lower_bound to search for values in a ordered range using the thrust::device execution policy for parallelization:

#include <thrust/binary_search.h>
#include <thrust/device_vector.h>
#include <thrust/execution_policy.h>
...
thrust::device_vector<int> input(5);

input[0] = 0;
input[1] = 2;
input[2] = 5;
input[3] = 7;
input[4] = 8;

thrust::lower_bound(thrust::device, input.begin(), input.end(), 0); // returns input.begin()
thrust::lower_bound(thrust::device, input.begin(), input.end(), 1); // returns input.begin() + 1
thrust::lower_bound(thrust::device, input.begin(), input.end(), 2); // returns input.begin() + 1
thrust::lower_bound(thrust::device, input.begin(), input.end(), 3); // returns input.begin() + 2
thrust::lower_bound(thrust::device, input.begin(), input.end(), 8); // returns input.begin() + 4
thrust::lower_bound(thrust::device, input.begin(), input.end(), 9); // returns input.end()

See also

upper_bound

See also

equal_range

See also

binary_search

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the ordered sequence.

  • last – The end of the ordered sequence.

  • value – The value to be searched.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator.

  • LessThanComparable – is a model of LessThanComparable.

Returns:

The furthermost iterator i, such that *i < value.

template<class ForwardIterator, class LessThanComparable>
ForwardIterator lower_bound(ForwardIterator first, ForwardIterator last, const LessThanComparable &value)#

lower_bound is a version of binary search: it attempts to find the element value in an ordered range [first, last). Specifically, it returns the first position where value could be inserted without violating the ordering. This version of lower_bound uses operator< for comparison and returns the furthermost iterator i in [first, last) such that, for every iterator j in [first, i), *j < value.

The following code snippet demonstrates how to use lower_bound to search for values in a ordered range.

#include <thrust/binary_search.h>
#include <thrust/device_vector.h>
...
thrust::device_vector<int> input(5);

input[0] = 0;
input[1] = 2;
input[2] = 5;
input[3] = 7;
input[4] = 8;

thrust::lower_bound(input.begin(), input.end(), 0); // returns input.begin()
thrust::lower_bound(input.begin(), input.end(), 1); // returns input.begin() + 1
thrust::lower_bound(input.begin(), input.end(), 2); // returns input.begin() + 1
thrust::lower_bound(input.begin(), input.end(), 3); // returns input.begin() + 2
thrust::lower_bound(input.begin(), input.end(), 8); // returns input.begin() + 4
thrust::lower_bound(input.begin(), input.end(), 9); // returns input.end()

See also

upper_bound

See also

equal_range

See also

binary_search

Parameters:
  • first – The beginning of the ordered sequence.

  • last – The end of the ordered sequence.

  • value – The value to be searched.

Template Parameters:
Returns:

The furthermost iterator i, such that *i < value.

template<typename DerivedPolicy, typename ForwardIterator, typename T, typename StrictWeakOrdering> __host__ __device__ ForwardIterator lower_bound (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last, const T &value, StrictWeakOrdering comp)

lower_bound is a version of binary search: it attempts to find the element value in an ordered range [first, last). Specifically, it returns the first position where value could be inserted without violating the ordering. This version of lower_bound uses function object comp for comparison and returns the furthermost iterator i in [first, last) such that, for every iterator j in [first, i), comp(*j, value) is true.

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

The following code snippet demonstrates how to use lower_bound to search for values in a ordered range using the thrust::device execution policy for parallelization:

#include <thrust/binary_search.h>
#include <thrust/device_vector.h>
#include <thrust/functional.h>
#include <thrust/execution_policy.h>
...
thrust::device_vector<int> input(5);

input[0] = 0;
input[1] = 2;
input[2] = 5;
input[3] = 7;
input[4] = 8;

thrust::lower_bound(input.begin(), input.end(), 0, thrust::less<int>()); // returns input.begin()
thrust::lower_bound(input.begin(), input.end(), 1, thrust::less<int>()); // returns input.begin() + 1
thrust::lower_bound(input.begin(), input.end(), 2, thrust::less<int>()); // returns input.begin() + 1
thrust::lower_bound(input.begin(), input.end(), 3, thrust::less<int>()); // returns input.begin() + 2
thrust::lower_bound(input.begin(), input.end(), 8, thrust::less<int>()); // returns input.begin() + 4
thrust::lower_bound(input.begin(), input.end(), 9, thrust::less<int>()); // returns input.end()

See also

upper_bound

See also

equal_range

See also

binary_search

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the ordered sequence.

  • last – The end of the ordered sequence.

  • value – The value to be searched.

  • comp – The comparison operator.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator.

  • T – is comparable to ForwardIterator's value_type.

  • StrictWeakOrdering – is a model of Strict Weak Ordering.

Returns:

The furthermost iterator i, such that comp(*i, value) is true.

template<class ForwardIterator, class T, class StrictWeakOrdering>
ForwardIterator lower_bound(ForwardIterator first, ForwardIterator last, const T &value, StrictWeakOrdering comp)#

lower_bound is a version of binary search: it attempts to find the element value in an ordered range [first, last). Specifically, it returns the first position where value could be inserted without violating the ordering. This version of lower_bound uses function object comp for comparison and returns the furthermost iterator i in [first, last) such that, for every iterator j in [first, i), comp(*j, value) is true.

The following code snippet demonstrates how to use lower_bound to search for values in a ordered range.

#include <thrust/binary_search.h>
#include <thrust/device_vector.h>
#include <thrust/functional.h>
...
thrust::device_vector<int> input(5);

input[0] = 0;
input[1] = 2;
input[2] = 5;
input[3] = 7;
input[4] = 8;

thrust::lower_bound(input.begin(), input.end(), 0, thrust::less<int>()); // returns input.begin()
thrust::lower_bound(input.begin(), input.end(), 1, thrust::less<int>()); // returns input.begin() + 1
thrust::lower_bound(input.begin(), input.end(), 2, thrust::less<int>()); // returns input.begin() + 1
thrust::lower_bound(input.begin(), input.end(), 3, thrust::less<int>()); // returns input.begin() + 2
thrust::lower_bound(input.begin(), input.end(), 8, thrust::less<int>()); // returns input.begin() + 4
thrust::lower_bound(input.begin(), input.end(), 9, thrust::less<int>()); // returns input.end()

See also

upper_bound

See also

equal_range

See also

binary_search

Parameters:
  • first – The beginning of the ordered sequence.

  • last – The end of the ordered sequence.

  • value – The value to be searched.

  • comp – The comparison operator.

Template Parameters:
Returns:

The furthermost iterator i, such that comp(*i, value) is true.

template<typename DerivedPolicy, typename ForwardIterator, typename LessThanComparable> __host__ __device__ ForwardIterator upper_bound (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last, const LessThanComparable &value)

upper_bound is a version of binary search: it attempts to find the element value in an ordered range [first, last). Specifically, it returns the last position where value could be inserted without violating the ordering. This version of upper_bound uses operator< for comparison and returns the furthermost iterator i in [first, last) such that, for every iterator j in [first, i), value < *j is false.

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

The following code snippet demonstrates how to use upper_bound to search for values in a ordered range using the thrust::device execution policy for parallelism:

#include <thrust/binary_search.h>
#include <thrust/device_vector.h>
#include <thrust/execution_policy.h>
...
thrust::device_vector<int> input(5);

input[0] = 0;
input[1] = 2;
input[2] = 5;
input[3] = 7;
input[4] = 8;

thrust::upper_bound(thrust::device, input.begin(), input.end(), 0); // returns input.begin() + 1
thrust::upper_bound(thrust::device, input.begin(), input.end(), 1); // returns input.begin() + 1
thrust::upper_bound(thrust::device, input.begin(), input.end(), 2); // returns input.begin() + 2
thrust::upper_bound(thrust::device, input.begin(), input.end(), 3); // returns input.begin() + 2
thrust::upper_bound(thrust::device, input.begin(), input.end(), 8); // returns input.end()
thrust::upper_bound(thrust::device, input.begin(), input.end(), 9); // returns input.end()

See also

lower_bound

See also

equal_range

See also

binary_search

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the ordered sequence.

  • last – The end of the ordered sequence.

  • value – The value to be searched.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator.

  • LessThanComparable – is a model of LessThanComparable.

Returns:

The furthermost iterator i, such that value < *i is false.

template<class ForwardIterator, class LessThanComparable>
ForwardIterator upper_bound(ForwardIterator first, ForwardIterator last, const LessThanComparable &value)#

upper_bound is a version of binary search: it attempts to find the element value in an ordered range [first, last). Specifically, it returns the last position where value could be inserted without violating the ordering. This version of upper_bound uses operator< for comparison and returns the furthermost iterator i in [first, last) such that, for every iterator j in [first, i), value < *j is false.

The following code snippet demonstrates how to use upper_bound to search for values in a ordered range.

#include <thrust/binary_search.h>
#include <thrust/device_vector.h>
...
thrust::device_vector<int> input(5);

input[0] = 0;
input[1] = 2;
input[2] = 5;
input[3] = 7;
input[4] = 8;

thrust::upper_bound(input.begin(), input.end(), 0); // returns input.begin() + 1
thrust::upper_bound(input.begin(), input.end(), 1); // returns input.begin() + 1
thrust::upper_bound(input.begin(), input.end(), 2); // returns input.begin() + 2
thrust::upper_bound(input.begin(), input.end(), 3); // returns input.begin() + 2
thrust::upper_bound(input.begin(), input.end(), 8); // returns input.end()
thrust::upper_bound(input.begin(), input.end(), 9); // returns input.end()

See also

lower_bound

See also

equal_range

See also

binary_search

Parameters:
  • first – The beginning of the ordered sequence.

  • last – The end of the ordered sequence.

  • value – The value to be searched.

Template Parameters:
Returns:

The furthermost iterator i, such that value < *i is false.

template<typename DerivedPolicy, typename ForwardIterator, typename T, typename StrictWeakOrdering> __host__ __device__ ForwardIterator upper_bound (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last, const T &value, StrictWeakOrdering comp)

upper_bound is a version of binary search: it attempts to find the element value in an ordered range [first, last). Specifically, it returns the last position where value could be inserted without violating the ordering. This version of upper_bound uses function object comp for comparison and returns the furthermost iterator i in [first, last) such that, for every iterator j in [first, i), comp(value, *j) is false.

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

The following code snippet demonstrates how to use upper_bound to search for values in a ordered range using the thrust::device execution policy for parallelization:

#include <thrust/binary_search.h>
#include <thrust/device_vector.h>
#include <thrust/functional.h>
#include <thrust/execution_policy.h>
...
thrust::device_vector<int> input(5);

input[0] = 0;
input[1] = 2;
input[2] = 5;
input[3] = 7;
input[4] = 8;

thrust::upper_bound(thrust::device, input.begin(), input.end(), 0, thrust::less<int>()); // returns input.begin() + 1
thrust::upper_bound(thrust::device, input.begin(), input.end(), 1, thrust::less<int>()); // returns input.begin() + 1
thrust::upper_bound(thrust::device, input.begin(), input.end(), 2, thrust::less<int>()); // returns input.begin() + 2
thrust::upper_bound(thrust::device, input.begin(), input.end(), 3, thrust::less<int>()); // returns input.begin() + 2
thrust::upper_bound(thrust::device, input.begin(), input.end(), 8, thrust::less<int>()); // returns input.end()
thrust::upper_bound(thrust::device, input.begin(), input.end(), 9, thrust::less<int>()); // returns input.end()

See also

lower_bound

See also

equal_range

See also

binary_search

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the ordered sequence.

  • last – The end of the ordered sequence.

  • value – The value to be searched.

  • comp – The comparison operator.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator.

  • T – is comparable to ForwardIterator's value_type.

  • StrictWeakOrdering – is a model of Strict Weak Ordering.

Returns:

The furthermost iterator i, such that comp(value, *i) is false.

template<class ForwardIterator, class T, class StrictWeakOrdering>
ForwardIterator upper_bound(ForwardIterator first, ForwardIterator last, const T &value, StrictWeakOrdering comp)#

upper_bound is a version of binary search: it attempts to find the element value in an ordered range [first, last). Specifically, it returns the last position where value could be inserted without violating the ordering. This version of upper_bound uses function object comp for comparison and returns the furthermost iterator i in [first, last) such that, for every iterator j in [first, i), comp(value, *j) is false.

The following code snippet demonstrates how to use upper_bound to search for values in a ordered range.

#include <thrust/binary_search.h>
#include <thrust/device_vector.h>
#include <thrust/functional.h>
...
thrust::device_vector<int> input(5);

input[0] = 0;
input[1] = 2;
input[2] = 5;
input[3] = 7;
input[4] = 8;

thrust::upper_bound(input.begin(), input.end(), 0, thrust::less<int>()); // returns input.begin() + 1
thrust::upper_bound(input.begin(), input.end(), 1, thrust::less<int>()); // returns input.begin() + 1
thrust::upper_bound(input.begin(), input.end(), 2, thrust::less<int>()); // returns input.begin() + 2
thrust::upper_bound(input.begin(), input.end(), 3, thrust::less<int>()); // returns input.begin() + 2
thrust::upper_bound(input.begin(), input.end(), 8, thrust::less<int>()); // returns input.end()
thrust::upper_bound(input.begin(), input.end(), 9, thrust::less<int>()); // returns input.end()

See also

lower_bound

See also

equal_range

See also

binary_search

Parameters:
  • first – The beginning of the ordered sequence.

  • last – The end of the ordered sequence.

  • value – The value to be searched.

  • comp – The comparison operator.

Template Parameters:
Returns:

The furthermost iterator i, such that comp(value, *i) is false.

template<typename DerivedPolicy, typename ForwardIterator, typename LessThanComparable> __host__ __device__ bool binary_search (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last, const LessThanComparable &value)

binary_search is a version of binary search: it attempts to find the element value in an ordered range [first, last). It returns true if an element that is equivalent to value is present in [first, last) and false if no such element exists. Specifically, this version returns true if and only if there exists an iterator i in [first, last) such that *i < value and value < *i are both false.

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

The following code snippet demonstrates how to use binary_search to search for values in a ordered range using the thrust::device execution policy for parallelization:

#include <thrust/binary_search.h>
#include <thrust/device_vector.h>
#include <thrust/execution_policy.h>
...
thrust::device_vector<int> input(5);

input[0] = 0;
input[1] = 2;
input[2] = 5;
input[3] = 7;
input[4] = 8;

thrust::binary_search(thrust::device, input.begin(), input.end(), 0); // returns true
thrust::binary_search(thrust::device, input.begin(), input.end(), 1); // returns false
thrust::binary_search(thrust::device, input.begin(), input.end(), 2); // returns true
thrust::binary_search(thrust::device, input.begin(), input.end(), 3); // returns false
thrust::binary_search(thrust::device, input.begin(), input.end(), 8); // returns true
thrust::binary_search(thrust::device, input.begin(), input.end(), 9); // returns false

See also

lower_bound

See also

upper_bound

See also

equal_range

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the ordered sequence.

  • last – The end of the ordered sequence.

  • value – The value to be searched.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator.

  • LessThanComparable – is a model of LessThanComparable.

Returns:

true if an equivalent element exists in [first, last), otherwise false.

template<class ForwardIterator, class LessThanComparable>
bool binary_search(ForwardIterator first, ForwardIterator last, const LessThanComparable &value)#

binary_search is a version of binary search: it attempts to find the element value in an ordered range [first, last). It returns true if an element that is equivalent to value is present in [first, last) and false if no such element exists. Specifically, this version returns true if and only if there exists an iterator i in [first, last) such that *i < value and value < *i are both false.

The following code snippet demonstrates how to use binary_search to search for values in a ordered range.

#include <thrust/binary_search.h>
#include <thrust/device_vector.h>
...
thrust::device_vector<int> input(5);

input[0] = 0;
input[1] = 2;
input[2] = 5;
input[3] = 7;
input[4] = 8;

thrust::binary_search(input.begin(), input.end(), 0); // returns true
thrust::binary_search(input.begin(), input.end(), 1); // returns false
thrust::binary_search(input.begin(), input.end(), 2); // returns true
thrust::binary_search(input.begin(), input.end(), 3); // returns false
thrust::binary_search(input.begin(), input.end(), 8); // returns true
thrust::binary_search(input.begin(), input.end(), 9); // returns false

See also

lower_bound

See also

upper_bound

See also

equal_range

Parameters:
  • first – The beginning of the ordered sequence.

  • last – The end of the ordered sequence.

  • value – The value to be searched.

Template Parameters:
Returns:

true if an equivalent element exists in [first, last), otherwise false.

template<typename DerivedPolicy, typename ForwardIterator, typename T, typename StrictWeakOrdering> __host__ __device__ bool binary_search (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last, const T &value, StrictWeakOrdering comp)

binary_search is a version of binary search: it attempts to find the element value in an ordered range [first, last). It returns true if an element that is equivalent to value is present in [first, last) and false if no such element exists. Specifically, this version returns true if and only if there exists an iterator i in [first, last) such that comp(*i, value) and comp(value, *i) are both false.

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

The following code snippet demonstrates how to use binary_search to search for values in a ordered range using the thrust::device execution policy for parallelization:

#include <thrust/binary_search.h>
#include <thrust/device_vector.h>
#include <thrust/functional.h>
#include <thrust/execution_policy.h>
...
thrust::device_vector<int> input(5);

input[0] = 0;
input[1] = 2;
input[2] = 5;
input[3] = 7;
input[4] = 8;

thrust::binary_search(thrust::device, input.begin(), input.end(), 0, thrust::less<int>()); // returns true
thrust::binary_search(thrust::device, input.begin(), input.end(), 1, thrust::less<int>()); // returns false
thrust::binary_search(thrust::device, input.begin(), input.end(), 2, thrust::less<int>()); // returns true
thrust::binary_search(thrust::device, input.begin(), input.end(), 3, thrust::less<int>()); // returns false
thrust::binary_search(thrust::device, input.begin(), input.end(), 8, thrust::less<int>()); // returns true
thrust::binary_search(thrust::device, input.begin(), input.end(), 9, thrust::less<int>()); // returns false

See also

lower_bound

See also

upper_bound

See also

equal_range

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the ordered sequence.

  • last – The end of the ordered sequence.

  • value – The value to be searched.

  • comp – The comparison operator.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator.

  • T – is comparable to ForwardIterator's value_type.

  • StrictWeakOrdering – is a model of Strict Weak Ordering.

Returns:

true if an equivalent element exists in [first, last), otherwise false.

template<class ForwardIterator, class T, class StrictWeakOrdering>
bool binary_search(ForwardIterator first, ForwardIterator last, const T &value, StrictWeakOrdering comp)#

binary_search is a version of binary search: it attempts to find the element value in an ordered range [first, last). It returns true if an element that is equivalent to value is present in [first, last) and false if no such element exists. Specifically, this version returns true if and only if there exists an iterator i in [first, last) such that comp(*i, value) and comp(value, *i) are both false.

The following code snippet demonstrates how to use binary_search to search for values in a ordered range.

#include <thrust/binary_search.h>
#include <thrust/device_vector.h>
#include <thrust/functional.h>
...
thrust::device_vector<int> input(5);

input[0] = 0;
input[1] = 2;
input[2] = 5;
input[3] = 7;
input[4] = 8;

thrust::binary_search(input.begin(), input.end(), 0, thrust::less<int>()); // returns true
thrust::binary_search(input.begin(), input.end(), 1, thrust::less<int>()); // returns false
thrust::binary_search(input.begin(), input.end(), 2, thrust::less<int>()); // returns true
thrust::binary_search(input.begin(), input.end(), 3, thrust::less<int>()); // returns false
thrust::binary_search(input.begin(), input.end(), 8, thrust::less<int>()); // returns true
thrust::binary_search(input.begin(), input.end(), 9, thrust::less<int>()); // returns false

See also

lower_bound

See also

upper_bound

See also

equal_range

Parameters:
  • first – The beginning of the ordered sequence.

  • last – The end of the ordered sequence.

  • value – The value to be searched.

  • comp – The comparison operator.

Template Parameters:
Returns:

true if an equivalent element exists in [first, last), otherwise false.

template<typename DerivedPolicy, typename ForwardIterator, typename LessThanComparable> __host__ __device__ thrust::pair< ForwardIterator, ForwardIterator > equal_range (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last, const LessThanComparable &value)

equal_range is a version of binary search: it attempts to find the element value in an ordered range [first, last). The value returned by equal_range is essentially a combination of the values returned by lower_bound and upper_bound: it returns a pair of iterators i and j such that i is the first position where value could be inserted without violating the ordering and j is the last position where value could be inserted without violating the ordering. It follows that every element in the range [i, j) is equivalent to value, and that [i, j) is the largest subrange of [first, last) that has this property.

This version of equal_range returns a pair of iterators [i, j), where i is the furthermost iterator in [first, last) such that, for every iterator k in [first, i), *k < value. j is the furthermost iterator in [first, last) such that, for every iterator k in [first, j), value < *k is false. For every iterator k in [i, j), neither value < *k nor *k < value is true.

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

The following code snippet demonstrates how to use equal_range to search for values in a ordered range using the thrust::device execution policy for parallelization:

#include <thrust/binary_search.h>
#include <thrust/device_vector.h>
#include <thrust/execution_policy.h>
...
thrust::device_vector<int> input(5);

input[0] = 0;
input[1] = 2;
input[2] = 5;
input[3] = 7;
input[4] = 8;

thrust::equal_range(thrust::device, input.begin(), input.end(), 0); // returns [input.begin(), input.begin() + 1)
thrust::equal_range(thrust::device, input.begin(), input.end(), 1); // returns [input.begin() + 1, input.begin() + 1)
thrust::equal_range(thrust::device, input.begin(), input.end(), 2); // returns [input.begin() + 1, input.begin() + 2)
thrust::equal_range(thrust::device, input.begin(), input.end(), 3); // returns [input.begin() + 2, input.begin() + 2)
thrust::equal_range(thrust::device, input.begin(), input.end(), 8); // returns [input.begin() + 4, input.end)
thrust::equal_range(thrust::device, input.begin(), input.end(), 9); // returns [input.end(), input.end)

See also

lower_bound

See also

upper_bound

See also

binary_search

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the ordered sequence.

  • last – The end of the ordered sequence.

  • value – The value to be searched.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator.

  • LessThanComparable – is a model of LessThanComparable.

Returns:

A pair of iterators [i, j) that define the range of equivalent elements.

template<class ForwardIterator, class LessThanComparable>
thrust::pair<ForwardIterator, ForwardIterator> equal_range(ForwardIterator first, ForwardIterator last, const LessThanComparable &value)#

equal_range is a version of binary search: it attempts to find the element value in an ordered range [first, last). The value returned by equal_range is essentially a combination of the values returned by lower_bound and upper_bound: it returns a pair of iterators i and j such that i is the first position where value could be inserted without violating the ordering and j is the last position where value could be inserted without violating the ordering. It follows that every element in the range [i, j) is equivalent to value, and that [i, j) is the largest subrange of [first, last) that has this property.

This version of equal_range returns a pair of iterators [i, j), where i is the furthermost iterator in [first, last) such that, for every iterator k in [first, i), *k < value. j is the furthermost iterator in [first, last) such that, for every iterator k in [first, j), value < *k is false. For every iterator k in [i, j), neither value < *k nor *k < value is true.

The following code snippet demonstrates how to use equal_range to search for values in a ordered range.

#include <thrust/binary_search.h>
#include <thrust/device_vector.h>
...
thrust::device_vector<int> input(5);

input[0] = 0;
input[1] = 2;
input[2] = 5;
input[3] = 7;
input[4] = 8;

thrust::equal_range(input.begin(), input.end(), 0); // returns [input.begin(), input.begin() + 1)
thrust::equal_range(input.begin(), input.end(), 1); // returns [input.begin() + 1, input.begin() + 1)
thrust::equal_range(input.begin(), input.end(), 2); // returns [input.begin() + 1, input.begin() + 2)
thrust::equal_range(input.begin(), input.end(), 3); // returns [input.begin() + 2, input.begin() + 2)
thrust::equal_range(input.begin(), input.end(), 8); // returns [input.begin() + 4, input.end)
thrust::equal_range(input.begin(), input.end(), 9); // returns [input.end(), input.end)

See also

lower_bound

See also

upper_bound

See also

binary_search

Parameters:
  • first – The beginning of the ordered sequence.

  • last – The end of the ordered sequence.

  • value – The value to be searched.

Template Parameters:
Returns:

A pair of iterators [i, j) that define the range of equivalent elements.

template<typename DerivedPolicy, typename ForwardIterator, typename T, typename StrictWeakOrdering> __host__ __device__ thrust::pair< ForwardIterator, ForwardIterator > equal_range (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last, const T &value, StrictWeakOrdering comp)

equal_range is a version of binary search: it attempts to find the element value in an ordered range [first, last). The value returned by equal_range is essentially a combination of the values returned by lower_bound and upper_bound: it returns a pair of iterators i and j such that i is the first position where value could be inserted without violating the ordering and j is the last position where value could be inserted without violating the ordering. It follows that every element in the range [i, j) is equivalent to value, and that [i, j) is the largest subrange of [first, last) that has this property.

This version of equal_range returns a pair of iterators [i, j). i is the furthermost iterator in [first, last) such that, for every iterator k in [first, i), comp(*k, value) is true. j is the furthermost iterator in [first, last) such that, for every iterator k in [first, last), comp(value, *k) is false. For every iterator k in [i, j), neither comp(value, *k) nor comp(*k, value) is true.

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

The following code snippet demonstrates how to use equal_range to search for values in a ordered range using the thrust::device execution policy for parallelization:

#include <thrust/binary_search.h>
#include <thrust/device_vector.h>
#include <thrust/functional.h>
#include <thrust/execution_policy.h>
...
thrust::device_vector<int> input(5);

input[0] = 0;
input[1] = 2;
input[2] = 5;
input[3] = 7;
input[4] = 8;

thrust::equal_range(thrust::device, input.begin(), input.end(), 0, thrust::less<int>()); // returns [input.begin(), input.begin() + 1)
thrust::equal_range(thrust::device, input.begin(), input.end(), 1, thrust::less<int>()); // returns [input.begin() + 1, input.begin() + 1)
thrust::equal_range(thrust::device, input.begin(), input.end(), 2, thrust::less<int>()); // returns [input.begin() + 1, input.begin() + 2)
thrust::equal_range(thrust::device, input.begin(), input.end(), 3, thrust::less<int>()); // returns [input.begin() + 2, input.begin() + 2)
thrust::equal_range(thrust::device, input.begin(), input.end(), 8, thrust::less<int>()); // returns [input.begin() + 4, input.end)
thrust::equal_range(thrust::device, input.begin(), input.end(), 9, thrust::less<int>()); // returns [input.end(), input.end)

See also

lower_bound

See also

upper_bound

See also

binary_search

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the ordered sequence.

  • last – The end of the ordered sequence.

  • value – The value to be searched.

  • comp – The comparison operator.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator.

  • T – is comparable to ForwardIterator's value_type.

  • StrictWeakOrdering – is a model of Strict Weak Ordering.

Returns:

A pair of iterators [i, j) that define the range of equivalent elements.

template<class ForwardIterator, class T, class StrictWeakOrdering>
thrust::pair<ForwardIterator, ForwardIterator> equal_range(ForwardIterator first, ForwardIterator last, const T &value, StrictWeakOrdering comp)#

equal_range is a version of binary search: it attempts to find the element value in an ordered range [first, last). The value returned by equal_range is essentially a combination of the values returned by lower_bound and upper_bound: it returns a pair of iterators i and j such that i is the first position where value could be inserted without violating the ordering and j is the last position where value could be inserted without violating the ordering. It follows that every element in the range [i, j) is equivalent to value, and that [i, j) is the largest subrange of [first, last) that has this property.

This version of equal_range returns a pair of iterators [i, j). i is the furthermost iterator in [first, last) such that, for every iterator k in [first, i), comp(*k, value) is true. j is the furthermost iterator in [first, last) such that, for every iterator k in [first, last), comp(value, *k) is false. For every iterator k in [i, j), neither comp(value, *k) nor comp(*k, value) is true.

The following code snippet demonstrates how to use equal_range to search for values in a ordered range.

#include <thrust/binary_search.h>
#include <thrust/device_vector.h>
#include <thrust/functional.h>
...
thrust::device_vector<int> input(5);

input[0] = 0;
input[1] = 2;
input[2] = 5;
input[3] = 7;
input[4] = 8;

thrust::equal_range(input.begin(), input.end(), 0, thrust::less<int>()); // returns [input.begin(), input.begin() + 1)
thrust::equal_range(input.begin(), input.end(), 1, thrust::less<int>()); // returns [input.begin() + 1, input.begin() + 1)
thrust::equal_range(input.begin(), input.end(), 2, thrust::less<int>()); // returns [input.begin() + 1, input.begin() + 2)
thrust::equal_range(input.begin(), input.end(), 3, thrust::less<int>()); // returns [input.begin() + 2, input.begin() + 2)
thrust::equal_range(input.begin(), input.end(), 8, thrust::less<int>()); // returns [input.begin() + 4, input.end)
thrust::equal_range(input.begin(), input.end(), 9, thrust::less<int>()); // returns [input.end(), input.end)

See also

lower_bound

See also

upper_bound

See also

binary_search

Parameters:
  • first – The beginning of the ordered sequence.

  • last – The end of the ordered sequence.

  • value – The value to be searched.

  • comp – The comparison operator.

Template Parameters:
Returns:

A pair of iterators [i, j) that define the range of equivalent elements.

Functions

template<typename DerivedPolicy, typename ForwardIterator, typename InputIterator, typename OutputIterator> __host__ __device__ OutputIterator lower_bound (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last, InputIterator values_first, InputIterator values_last, OutputIterator result)

lower_bound is a vectorized version of binary search: for each iterator v in [values_first, values_last) it attempts to find the value *v in an ordered range [first, last). Specifically, it returns the index of first position where value could be inserted without violating the ordering.

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

The following code snippet demonstrates how to use lower_bound to search for multiple values in a ordered range using the thrust::device execution policy for parallelization:

#include <thrust/binary_search.h>
#include <thrust/device_vector.h>
#include <thrust/execution_policy.h>
...
thrust::device_vector<int> input(5);

input[0] = 0;
input[1] = 2;
input[2] = 5;
input[3] = 7;
input[4] = 8;

thrust::device_vector<int> values(6);
values[0] = 0; 
values[1] = 1;
values[2] = 2;
values[3] = 3;
values[4] = 8;
values[5] = 9;

thrust::device_vector<unsigned int> output(6);

thrust::lower_bound(thrust::device,
                    input.begin(), input.end(),
                    values.begin(), values.end(),
                    output.begin());

// output is now [0, 1, 1, 2, 4, 5]

See also

upper_bound

See also

equal_range

See also

binary_search

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the ordered sequence.

  • last – The end of the ordered sequence.

  • values_first – The beginning of the search values sequence.

  • values_last – The end of the search values sequence.

  • result – The beginning of the output sequence.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator.

  • InputIterator – is a model of Input Iterator. and InputIterator's value_type is LessThanComparable.

  • OutputIterator – is a model of Output Iterator. and ForwardIterator's difference_type is convertible to OutputIterator's value_type.

Pre:

The ranges [first,last) and [result, result + (last - first)) shall not overlap.

template<class ForwardIterator, class InputIterator, class OutputIterator>
OutputIterator lower_bound(ForwardIterator first, ForwardIterator last, InputIterator values_first, InputIterator values_last, OutputIterator result)#

lower_bound is a vectorized version of binary search: for each iterator v in [values_first, values_last) it attempts to find the value *v in an ordered range [first, last). Specifically, it returns the index of first position where value could be inserted without violating the ordering.

The following code snippet demonstrates how to use lower_bound to search for multiple values in a ordered range.

#include <thrust/binary_search.h>
#include <thrust/device_vector.h>
...
thrust::device_vector<int> input(5);

input[0] = 0;
input[1] = 2;
input[2] = 5;
input[3] = 7;
input[4] = 8;

thrust::device_vector<int> values(6);
values[0] = 0; 
values[1] = 1;
values[2] = 2;
values[3] = 3;
values[4] = 8;
values[5] = 9;

thrust::device_vector<unsigned int> output(6);

thrust::lower_bound(input.begin(), input.end(),
                    values.begin(), values.end(),
                    output.begin());

// output is now [0, 1, 1, 2, 4, 5]

See also

upper_bound

See also

equal_range

See also

binary_search

Parameters:
  • first – The beginning of the ordered sequence.

  • last – The end of the ordered sequence.

  • values_first – The beginning of the search values sequence.

  • values_last – The end of the search values sequence.

  • result – The beginning of the output sequence.

Template Parameters:
Pre:

The ranges [first,last) and [result, result + (last - first)) shall not overlap.

template<typename DerivedPolicy, typename ForwardIterator, typename InputIterator, typename OutputIterator, typename StrictWeakOrdering> __host__ __device__ OutputIterator lower_bound (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last, InputIterator values_first, InputIterator values_last, OutputIterator result, StrictWeakOrdering comp)

lower_bound is a vectorized version of binary search: for each iterator v in [values_first, values_last) it attempts to find the value *v in an ordered range [first, last). Specifically, it returns the index of first position where value could be inserted without violating the ordering. This version of lower_bound uses function object comp for comparison.

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

The following code snippet demonstrates how to use lower_bound to search for multiple values in a ordered range.

#include <thrust/binary_search.h>
#include <thrust/device_vector.h>
#include <thrust/functional.h>
#include <thrust/execution_policy.h>
...
thrust::device_vector<int> input(5);

input[0] = 0;
input[1] = 2;
input[2] = 5;
input[3] = 7;
input[4] = 8;

thrust::device_vector<int> values(6);
values[0] = 0; 
values[1] = 1;
values[2] = 2;
values[3] = 3;
values[4] = 8;
values[5] = 9;

thrust::device_vector<unsigned int> output(6);

thrust::lower_bound(input.begin(), input.end(),
                    values.begin(), values.end(), 
                    output.begin(),
                    thrust::less<int>());

// output is now [0, 1, 1, 2, 4, 5]

See also

upper_bound

See also

equal_range

See also

binary_search

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the ordered sequence.

  • last – The end of the ordered sequence.

  • values_first – The beginning of the search values sequence.

  • values_last – The end of the search values sequence.

  • result – The beginning of the output sequence.

  • comp – The comparison operator.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator.

  • InputIterator – is a model of Input Iterator. and InputIterator's value_type is comparable to ForwardIterator's value_type.

  • OutputIterator – is a model of Output Iterator. and ForwardIterator's difference_type is convertible to OutputIterator's value_type.

  • StrictWeakOrdering – is a model of Strict Weak Ordering.

Pre:

The ranges [first,last) and [result, result + (last - first)) shall not overlap.

template<class ForwardIterator, class InputIterator, class OutputIterator, class StrictWeakOrdering>
OutputIterator lower_bound(ForwardIterator first, ForwardIterator last, InputIterator values_first, InputIterator values_last, OutputIterator result, StrictWeakOrdering comp)#

lower_bound is a vectorized version of binary search: for each iterator v in [values_first, values_last) it attempts to find the value *v in an ordered range [first, last). Specifically, it returns the index of first position where value could be inserted without violating the ordering. This version of lower_bound uses function object comp for comparison.

The following code snippet demonstrates how to use lower_bound to search for multiple values in a ordered range.

#include <thrust/binary_search.h>
#include <thrust/device_vector.h>
#include <thrust/functional.h>
...
thrust::device_vector<int> input(5);

input[0] = 0;
input[1] = 2;
input[2] = 5;
input[3] = 7;
input[4] = 8;

thrust::device_vector<int> values(6);
values[0] = 0; 
values[1] = 1;
values[2] = 2;
values[3] = 3;
values[4] = 8;
values[5] = 9;

thrust::device_vector<unsigned int> output(6);

thrust::lower_bound(input.begin(), input.end(),
                    values.begin(), values.end(), 
                    output.begin(),
                    thrust::less<int>());

// output is now [0, 1, 1, 2, 4, 5]

See also

upper_bound

See also

equal_range

See also

binary_search

Parameters:
  • first – The beginning of the ordered sequence.

  • last – The end of the ordered sequence.

  • values_first – The beginning of the search values sequence.

  • values_last – The end of the search values sequence.

  • result – The beginning of the output sequence.

  • comp – The comparison operator.

Template Parameters:
  • ForwardIterator – is a model of Forward Iterator.

  • InputIterator – is a model of Input Iterator. and InputIterator's value_type is comparable to ForwardIterator's value_type.

  • OutputIterator – is a model of Output Iterator. and ForwardIterator's difference_type is convertible to OutputIterator's value_type.

  • StrictWeakOrdering – is a model of Strict Weak Ordering.

Pre:

The ranges [first,last) and [result, result + (last - first)) shall not overlap.

template<typename DerivedPolicy, typename ForwardIterator, typename InputIterator, typename OutputIterator> __host__ __device__ OutputIterator upper_bound (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last, InputIterator values_first, InputIterator values_last, OutputIterator result)

upper_bound is a vectorized version of binary search: for each iterator v in [values_first, values_last) it attempts to find the value *v in an ordered range [first, last). Specifically, it returns the index of last position where value could be inserted without violating the ordering.

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

The following code snippet demonstrates how to use upper_bound to search for multiple values in a ordered range using the thrust::device execution policy for parallelization:

#include <thrust/binary_search.h>
#include <thrust/device_vector.h>
#include <thrust/execution_policy.h>
...
thrust::device_vector<int> input(5);

input[0] = 0;
input[1] = 2;
input[2] = 5;
input[3] = 7;
input[4] = 8;

thrust::device_vector<int> values(6);
values[0] = 0; 
values[1] = 1;
values[2] = 2;
values[3] = 3;
values[4] = 8;
values[5] = 9;

thrust::device_vector<unsigned int> output(6);

thrust::upper_bound(thrust::device,
                    input.begin(), input.end(),
                    values.begin(), values.end(),
                    output.begin());

// output is now [1, 1, 2, 2, 5, 5]

See also

upper_bound

See also

equal_range

See also

binary_search

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the ordered sequence.

  • last – The end of the ordered sequence.

  • values_first – The beginning of the search values sequence.

  • values_last – The end of the search values sequence.

  • result – The beginning of the output sequence.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator.

  • InputIterator – is a model of Input Iterator. and InputIterator's value_type is LessThanComparable.

  • OutputIterator – is a model of Output Iterator. and ForwardIterator's difference_type is convertible to OutputIterator's value_type.

Pre:

The ranges [first,last) and [result, result + (last - first)) shall not overlap.

template<class ForwardIterator, class InputIterator, class OutputIterator>
OutputIterator upper_bound(ForwardIterator first, ForwardIterator last, InputIterator values_first, InputIterator values_last, OutputIterator result)#

upper_bound is a vectorized version of binary search: for each iterator v in [values_first, values_last) it attempts to find the value *v in an ordered range [first, last). Specifically, it returns the index of last position where value could be inserted without violating the ordering.

The following code snippet demonstrates how to use upper_bound to search for multiple values in a ordered range.

#include <thrust/binary_search.h>
#include <thrust/device_vector.h>
...
thrust::device_vector<int> input(5);

input[0] = 0;
input[1] = 2;
input[2] = 5;
input[3] = 7;
input[4] = 8;

thrust::device_vector<int> values(6);
values[0] = 0; 
values[1] = 1;
values[2] = 2;
values[3] = 3;
values[4] = 8;
values[5] = 9;

thrust::device_vector<unsigned int> output(6);

thrust::upper_bound(input.begin(), input.end(),
                    values.begin(), values.end(),
                    output.begin());

// output is now [1, 1, 2, 2, 5, 5]

See also

upper_bound

See also

equal_range

See also

binary_search

Parameters:
  • first – The beginning of the ordered sequence.

  • last – The end of the ordered sequence.

  • values_first – The beginning of the search values sequence.

  • values_last – The end of the search values sequence.

  • result – The beginning of the output sequence.

Template Parameters:
Pre:

The ranges [first,last) and [result, result + (last - first)) shall not overlap.

template<typename DerivedPolicy, typename ForwardIterator, typename InputIterator, typename OutputIterator, typename StrictWeakOrdering> __host__ __device__ OutputIterator upper_bound (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last, InputIterator values_first, InputIterator values_last, OutputIterator result, StrictWeakOrdering comp)

upper_bound is a vectorized version of binary search: for each iterator v in [values_first, values_last) it attempts to find the value *v in an ordered range [first, last). Specifically, it returns the index of first position where value could be inserted without violating the ordering. This version of upper_bound uses function object comp for comparison.

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

The following code snippet demonstrates how to use upper_bound to search for multiple values in a ordered range using the thrust::device execution policy for parallelization:

#include <thrust/binary_search.h>
#include <thrust/device_vector.h>
#include <thrust/functional.h>
#include <thrust/execution_policy.h>
...
thrust::device_vector<int> input(5);

input[0] = 0;
input[1] = 2;
input[2] = 5;
input[3] = 7;
input[4] = 8;

thrust::device_vector<int> values(6);
values[0] = 0; 
values[1] = 1;
values[2] = 2;
values[3] = 3;
values[4] = 8;
values[5] = 9;

thrust::device_vector<unsigned int> output(6);

thrust::upper_bound(thrust::device,
                    input.begin(), input.end(),
                    values.begin(), values.end(), 
                    output.begin(),
                    thrust::less<int>());

// output is now [1, 1, 2, 2, 5, 5]

See also

lower_bound

See also

equal_range

See also

binary_search

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the ordered sequence.

  • last – The end of the ordered sequence.

  • values_first – The beginning of the search values sequence.

  • values_last – The end of the search values sequence.

  • result – The beginning of the output sequence.

  • comp – The comparison operator.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator.

  • InputIterator – is a model of Input Iterator. and InputIterator's value_type is comparable to ForwardIterator's value_type.

  • OutputIterator – is a model of Output Iterator. and ForwardIterator's difference_type is convertible to OutputIterator's value_type.

  • StrictWeakOrdering – is a model of Strict Weak Ordering.

Pre:

The ranges [first,last) and [result, result + (last - first)) shall not overlap.

template<class ForwardIterator, class InputIterator, class OutputIterator, class StrictWeakOrdering>
OutputIterator upper_bound(ForwardIterator first, ForwardIterator last, InputIterator values_first, InputIterator values_last, OutputIterator result, StrictWeakOrdering comp)#

upper_bound is a vectorized version of binary search: for each iterator v in [values_first, values_last) it attempts to find the value *v in an ordered range [first, last). Specifically, it returns the index of first position where value could be inserted without violating the ordering. This version of upper_bound uses function object comp for comparison.

The following code snippet demonstrates how to use upper_bound to search for multiple values in a ordered range.

#include <thrust/binary_search.h>
#include <thrust/device_vector.h>
#include <thrust/functional.h>
...
thrust::device_vector<int> input(5);

input[0] = 0;
input[1] = 2;
input[2] = 5;
input[3] = 7;
input[4] = 8;

thrust::device_vector<int> values(6);
values[0] = 0; 
values[1] = 1;
values[2] = 2;
values[3] = 3;
values[4] = 8;
values[5] = 9;

thrust::device_vector<unsigned int> output(6);

thrust::upper_bound(input.begin(), input.end(),
                    values.begin(), values.end(), 
                    output.begin(),
                    thrust::less<int>());

// output is now [1, 1, 2, 2, 5, 5]

See also

lower_bound

See also

equal_range

See also

binary_search

Parameters:
  • first – The beginning of the ordered sequence.

  • last – The end of the ordered sequence.

  • values_first – The beginning of the search values sequence.

  • values_last – The end of the search values sequence.

  • result – The beginning of the output sequence.

  • comp – The comparison operator.

Template Parameters:
  • ForwardIterator – is a model of Forward Iterator.

  • InputIterator – is a model of Input Iterator. and InputIterator's value_type is comparable to ForwardIterator's value_type.

  • OutputIterator – is a model of Output Iterator. and ForwardIterator's difference_type is convertible to OutputIterator's value_type.

  • StrictWeakOrdering – is a model of Strict Weak Ordering.

Pre:

The ranges [first,last) and [result, result + (last - first)) shall not overlap.

template<typename DerivedPolicy, typename ForwardIterator, typename InputIterator, typename OutputIterator> __host__ __device__ OutputIterator binary_search (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last, InputIterator values_first, InputIterator values_last, OutputIterator result)

binary_search is a vectorized version of binary search: for each iterator v in [values_first, values_last) it attempts to find the value *v in an ordered range [first, last). It returns true if an element that is equivalent to value is present in [first, last) and false if no such element exists.

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

The following code snippet demonstrates how to use binary_search to search for multiple values in a ordered range using the thrust::device execution policy for parallelization:

#include <thrust/binary_search.h>
#include <thrust/device_vector.h>
#include <thrust/execution_policy.h>
...
thrust::device_vector<int> input(5);

input[0] = 0;
input[1] = 2;
input[2] = 5;
input[3] = 7;
input[4] = 8;

thrust::device_vector<int> values(6);
values[0] = 0; 
values[1] = 1;
values[2] = 2;
values[3] = 3;
values[4] = 8;
values[5] = 9;

thrust::device_vector<bool> output(6);

thrust::binary_search(thrust::device,
                      input.begin(), input.end(),
                      values.begin(), values.end(),
                      output.begin());

// output is now [true, false, true, false, true, false]

See also

lower_bound

See also

upper_bound

See also

equal_range

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the ordered sequence.

  • last – The end of the ordered sequence.

  • values_first – The beginning of the search values sequence.

  • values_last – The end of the search values sequence.

  • result – The beginning of the output sequence.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator.

  • InputIterator – is a model of Input Iterator. and InputIterator's value_type is LessThanComparable.

  • OutputIterator – is a model of Output Iterator. and bool is convertible to OutputIterator's value_type.

Pre:

The ranges [first,last) and [result, result + (last - first)) shall not overlap.

template<class ForwardIterator, class InputIterator, class OutputIterator>
OutputIterator binary_search(ForwardIterator first, ForwardIterator last, InputIterator values_first, InputIterator values_last, OutputIterator result)#

binary_search is a vectorized version of binary search: for each iterator v in [values_first, values_last) it attempts to find the value *v in an ordered range [first, last). It returns true if an element that is equivalent to value is present in [first, last) and false if no such element exists.

The following code snippet demonstrates how to use binary_search to search for multiple values in a ordered range.

#include <thrust/binary_search.h>
#include <thrust/device_vector.h>
...
thrust::device_vector<int> input(5);

input[0] = 0;
input[1] = 2;
input[2] = 5;
input[3] = 7;
input[4] = 8;

thrust::device_vector<int> values(6);
values[0] = 0; 
values[1] = 1;
values[2] = 2;
values[3] = 3;
values[4] = 8;
values[5] = 9;

thrust::device_vector<bool> output(6);

thrust::binary_search(input.begin(), input.end(),
                      values.begin(), values.end(),
                      output.begin());

// output is now [true, false, true, false, true, false]

See also

lower_bound

See also

upper_bound

See also

equal_range

Parameters:
  • first – The beginning of the ordered sequence.

  • last – The end of the ordered sequence.

  • values_first – The beginning of the search values sequence.

  • values_last – The end of the search values sequence.

  • result – The beginning of the output sequence.

Template Parameters:
Pre:

The ranges [first,last) and [result, result + (last - first)) shall not overlap.

template<typename DerivedPolicy, typename ForwardIterator, typename InputIterator, typename OutputIterator, typename StrictWeakOrdering> __host__ __device__ OutputIterator binary_search (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last, InputIterator values_first, InputIterator values_last, OutputIterator result, StrictWeakOrdering comp)

binary_search is a vectorized version of binary search: for each iterator v in [values_first, values_last) it attempts to find the value *v in an ordered range [first, last). It returns true if an element that is equivalent to value is present in [first, last) and false if no such element exists. This version of binary_search uses function object comp for comparison.

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

The following code snippet demonstrates how to use binary_search to search for multiple values in a ordered range using the thrust::device execution policy for parallelization:

#include <thrust/binary_search.h>
#include <thrust/device_vector.h>
#include <thrust/functional.h>
#include <thrust/execution_policy.h>
...
thrust::device_vector<int> input(5);

input[0] = 0;
input[1] = 2;
input[2] = 5;
input[3] = 7;
input[4] = 8;

thrust::device_vector<int> values(6);
values[0] = 0; 
values[1] = 1;
values[2] = 2;
values[3] = 3;
values[4] = 8;
values[5] = 9;

thrust::device_vector<bool> output(6);

thrust::binary_search(thrust::device,
                      input.begin(), input.end(),
                      values.begin(), values.end(),
                      output.begin(),
                      thrust::less<T>());

// output is now [true, false, true, false, true, false]

See also

lower_bound

See also

upper_bound

See also

equal_range

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the ordered sequence.

  • last – The end of the ordered sequence.

  • values_first – The beginning of the search values sequence.

  • values_last – The end of the search values sequence.

  • result – The beginning of the output sequence.

  • comp – The comparison operator.

Template Parameters:
Pre:

The ranges [first,last) and [result, result + (last - first)) shall not overlap.

template<class ForwardIterator, class InputIterator, class OutputIterator, class StrictWeakOrdering>
OutputIterator binary_search(ForwardIterator first, ForwardIterator last, InputIterator values_first, InputIterator values_last, OutputIterator result, StrictWeakOrdering comp)#

binary_search is a vectorized version of binary search: for each iterator v in [values_first, values_last) it attempts to find the value *v in an ordered range [first, last). It returns true if an element that is equivalent to value is present in [first, last) and false if no such element exists. This version of binary_search uses function object comp for comparison.

The following code snippet demonstrates how to use binary_search to search for multiple values in a ordered range.

#include <thrust/binary_search.h>
#include <thrust/device_vector.h>
#include <thrust/functional.h>
...
thrust::device_vector<int> input(5);

input[0] = 0;
input[1] = 2;
input[2] = 5;
input[3] = 7;
input[4] = 8;

thrust::device_vector<int> values(6);
values[0] = 0; 
values[1] = 1;
values[2] = 2;
values[3] = 3;
values[4] = 8;
values[5] = 9;

thrust::device_vector<bool> output(6);

thrust::binary_search(input.begin(), input.end(),
                      values.begin(), values.end(),
                      output.begin(),
                      thrust::less<T>());

// output is now [true, false, true, false, true, false]

See also

lower_bound

See also

upper_bound

See also

equal_range

Parameters:
  • first – The beginning of the ordered sequence.

  • last – The end of the ordered sequence.

  • values_first – The beginning of the search values sequence.

  • values_last – The end of the search values sequence.

  • result – The beginning of the output sequence.

  • comp – The comparison operator.

Template Parameters:
Pre:

The ranges [first,last) and [result, result + (last - first)) shall not overlap.

Functions

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

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator – must be a model of Input Iterator and InputIterator's value_type must be convertible to OutputIterator's value_type.

  • OutputIterator – must be a model of Output Iterator.

Returns:

The end of the destination sequence.

Pre:

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

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

See also

thrust::copy

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.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator – must be a model of Input Iterator and InputIterator's value_type must be convertible to OutputIterator's value_type.

  • Size – is an integral type.

  • OutputIterator – must be a model of Output Iterator.

Returns:

The end of the destination range.

Pre:

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

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
Parameters:
  • first – The beginning of the sequence to copy.

  • last – The end of the sequence to copy.

  • result – The destination sequence.

Template Parameters:
  • InputIterator – must be a model of Input Iterator and InputIterator's value_type must be convertible to OutputIterator's value_type.

  • OutputIterator – must be a model of Output Iterator.

Returns:

The end of the destination sequence.

Pre:

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

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

See also

thrust::copy

Parameters:
  • first – The beginning of the range to copy.

  • n – The number of elements to copy.

  • result – The beginning destination range.

Template Parameters:
  • InputIterator – must be a model of Input Iterator and InputIterator's value_type must be convertible to OutputIterator's value_type.

  • Size – is an integral type.

  • OutputIterator – must be a model of Output Iterator.

Returns:

The end of the destination range.

Pre:

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

template<typename DerivedPolicy, typename ForwardIterator1, typename ForwardIterator2> __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

See also

swap

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.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

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

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

Returns:

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

Pre:

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

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

See also

swap

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.

Template Parameters:
  • ForwardIterator1 – is a model of Forward Iterator, and ForwardIterator1's value_type must be convertible to ForwardIterator2's value_type.

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

Returns:

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

Pre:

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

template<typename DerivedPolicy, typename InputIterator, typename ForwardIterator> __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's value_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

See also

copy

See also

device_new

See also

device_malloc

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.

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's value_type has a constructor that takes a single argument whose type is InputIterator's value_type.

Returns:

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

Pre:

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

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's value_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

See also

copy

See also

device_new

See also

device_malloc

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.

Template Parameters:
  • InputIterator – is a model of Input Iterator.

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

Returns:

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

Pre:

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

template<typename DerivedPolicy, typename InputIterator, typename Size, typename ForwardIterator> __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's value_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

See also

copy

See also

device_new

See also

device_malloc

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.

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's value_type has a constructor that takes a single argument whose type is InputIterator's value_type.

Returns:

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

Pre:

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

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's value_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

See also

copy

See also

device_new

See also

device_malloc

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.

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's value_type has a constructor that takes a single argument whose type is InputIterator's value_type.

Returns:

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

Pre:

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

Functions

template<typename DerivedPolicy, typename InputIterator, typename RandomAccessIterator, typename OutputIterator> __host__ __device__ OutputIterator gather (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator map_first, InputIterator map_last, RandomAccessIterator input_first, OutputIterator result)

gather copies elements from a source array into a destination range according to a map. For each input iterator i in the range [map_first, map_last), the value input_first[*i] is assigned to *(result + (i - map_first)). RandomAccessIterator must permit random access.

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

The following code snippet demonstrates how to use gather to reorder a range using the thrust::device execution policy for parallelization:

Remark

gather is the inverse of thrust::scatter.

#include <thrust/gather.h>
#include <thrust/device_vector.h>
#include <thrust/execution_policy.h>
...
// mark even indices with a 1; odd indices with a 0
int values[10] = {1, 0, 1, 0, 1, 0, 1, 0, 1, 0};
thrust::device_vector<int> d_values(values, values + 10);

// gather all even indices into the first half of the range
// and odd indices to the last half of the range
int map[10]   = {0, 2, 4, 6, 8, 1, 3, 5, 7, 9};
thrust::device_vector<int> d_map(map, map + 10);

thrust::device_vector<int> d_output(10);
thrust::gather(thrust::device,
               d_map.begin(), d_map.end(),
               d_values.begin(),
               d_output.begin());
// d_output is now {1, 1, 1, 1, 1, 0, 0, 0, 0, 0}
Parameters:
  • exec – The execution policy to use for parallelization.

  • map_first – Beginning of the range of gather locations.

  • map_last – End of the range of gather locations.

  • input_first – Beginning of the source range.

  • result – Beginning of the destination range.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator – must be a model of Input Iterator and InputIterator's value_type must be convertible to RandomAccessIterator's difference_type.

  • RandomAccessIterator – must be a model of Random Access Iterator and RandomAccessIterator's value_type must be convertible to OutputIterator's value_type.

  • OutputIterator – must be a model of Output Iterator.

Pre:

The range [map_first, map_last) shall not overlap the range [result, result + (map_last - map_first)).

Pre:

The input data shall not overlap the range [result, result + (map_last - map_first)).

template<typename InputIterator, typename RandomAccessIterator, typename OutputIterator>
OutputIterator gather(InputIterator map_first, InputIterator map_last, RandomAccessIterator input_first, OutputIterator result)#

gather copies elements from a source array into a destination range according to a map. For each input iterator i in the range [map_first, map_last), the value input_first[*i] is assigned to *(result + (i - map_first)). RandomAccessIterator must permit random access.

The following code snippet demonstrates how to use gather to reorder a range.

Remark

gather is the inverse of thrust::scatter.

#include <thrust/gather.h>
#include <thrust/device_vector.h>
...
// mark even indices with a 1; odd indices with a 0
int values[10] = {1, 0, 1, 0, 1, 0, 1, 0, 1, 0};
thrust::device_vector<int> d_values(values, values + 10);

// gather all even indices into the first half of the range
// and odd indices to the last half of the range
int map[10]   = {0, 2, 4, 6, 8, 1, 3, 5, 7, 9};
thrust::device_vector<int> d_map(map, map + 10);

thrust::device_vector<int> d_output(10);
thrust::gather(d_map.begin(), d_map.end(),
               d_values.begin(),
               d_output.begin());
// d_output is now {1, 1, 1, 1, 1, 0, 0, 0, 0, 0}
Parameters:
  • map_first – Beginning of the range of gather locations.

  • map_last – End of the range of gather locations.

  • input_first – Beginning of the source range.

  • result – Beginning of the destination range.

Template Parameters:
  • InputIterator – must be a model of Input Iterator and InputIterator's value_type must be convertible to RandomAccessIterator's difference_type.

  • RandomAccessIterator – must be a model of Random Access Iterator and RandomAccessIterator's value_type must be convertible to OutputIterator's value_type.

  • OutputIterator – must be a model of Output Iterator.

Pre:

The range [map_first, map_last) shall not overlap the range [result, result + (map_last - map_first)).

Pre:

The input data shall not overlap the range [result, result + (map_last - map_first)).

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename RandomAccessIterator, typename OutputIterator> __host__ __device__ OutputIterator gather_if (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 map_first, InputIterator1 map_last, InputIterator2 stencil, RandomAccessIterator input_first, OutputIterator result)

gather_if conditionally copies elements from a source array into a destination range according to a map. For each input iterator i in the range [map_first, map_last), such that the value of *(stencil + (i - map_first)) is true, the value input_first[*i] is assigned to *(result + (i - map_first)). RandomAccessIterator must permit random access.

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

The following code snippet demonstrates how to use gather_if to gather selected values from an input range using the thrust::device execution policy:

Remark

gather_if is the inverse of scatter_if.

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

int values[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
thrust::device_vector<int> d_values(values, values + 10);

// select elements at even-indexed locations
int stencil[10] = {1, 0, 1, 0, 1, 0, 1, 0, 1, 0};
thrust::device_vector<int> d_stencil(stencil, stencil + 10);

// map all even indices into the first half of the range
// and odd indices to the last half of the range
int map[10]   = {0, 2, 4, 6, 8, 1, 3, 5, 7, 9};
thrust::device_vector<int> d_map(map, map + 10);

thrust::device_vector<int> d_output(10, 7);
thrust::gather_if(thrust::device,
                  d_map.begin(), d_map.end(),
                  d_stencil.begin(),
                  d_values.begin(),
                  d_output.begin());
// d_output is now {0, 7, 4, 7, 8, 7, 3, 7, 7, 7}
Parameters:
  • exec – The execution policy to use for parallelization.

  • map_first – Beginning of the range of gather locations.

  • map_last – End of the range of gather locations.

  • stencil – Beginning of the range of predicate values.

  • input_first – Beginning of the source range.

  • result – Beginning of the destination range.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – must be a model of Input Iterator and InputIterator1's value_type must be convertible to RandomAccessIterator's difference_type.

  • InputIterator2 – must be a model of Input Iterator and InputIterator2's value_type must be convertible to bool.

  • RandomAccessIterator – must be a model of Random Access iterator and RandomAccessIterator's value_type must be convertible to OutputIterator's value_type.

  • OutputIterator – must be a model of Output Iterator.

Pre:

The range [map_first, map_last) shall not overlap the range [result, result + (map_last - map_first)).

Pre:

The range [stencil, stencil + (map_last - map_first)) shall not overlap the range [result, result + (map_last - map_first)).

Pre:

The input data shall not overlap the range [result, result + (map_last - map_first)).

template<typename InputIterator1, typename InputIterator2, typename RandomAccessIterator, typename OutputIterator>
OutputIterator gather_if(InputIterator1 map_first, InputIterator1 map_last, InputIterator2 stencil, RandomAccessIterator input_first, OutputIterator result)#

gather_if conditionally copies elements from a source array into a destination range according to a map. For each input iterator i in the range [map_first, map_last), such that the value of *(stencil + (i - map_first)) is true, the value input_first[*i] is assigned to *(result + (i - map_first)). RandomAccessIterator must permit random access.

The following code snippet demonstrates how to use gather_if to gather selected values from an input range.

Remark

gather_if is the inverse of scatter_if.

#include <thrust/gather.h>
#include <thrust/device_vector.h>
...

int values[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
thrust::device_vector<int> d_values(values, values + 10);

// select elements at even-indexed locations
int stencil[10] = {1, 0, 1, 0, 1, 0, 1, 0, 1, 0};
thrust::device_vector<int> d_stencil(stencil, stencil + 10);

// map all even indices into the first half of the range
// and odd indices to the last half of the range
int map[10]   = {0, 2, 4, 6, 8, 1, 3, 5, 7, 9};
thrust::device_vector<int> d_map(map, map + 10);

thrust::device_vector<int> d_output(10, 7);
thrust::gather_if(d_map.begin(), d_map.end(),
                  d_stencil.begin(),
                  d_values.begin(),
                  d_output.begin());
// d_output is now {0, 7, 4, 7, 8, 7, 3, 7, 7, 7}
Parameters:
  • map_first – Beginning of the range of gather locations.

  • map_last – End of the range of gather locations.

  • stencil – Beginning of the range of predicate values.

  • input_first – Beginning of the source range.

  • result – Beginning of the destination range.

Template Parameters:
  • InputIterator1 – must be a model of Input Iterator and InputIterator1's value_type must be convertible to RandomAccessIterator's difference_type.

  • InputIterator2 – must be a model of Input Iterator and InputIterator2's value_type must be convertible to bool.

  • RandomAccessIterator – must be a model of Random Access iterator and RandomAccessIterator's value_type must be convertible to OutputIterator's value_type.

  • OutputIterator – must be a model of Output Iterator.

Pre:

The range [map_first, map_last) shall not overlap the range [result, result + (map_last - map_first)).

Pre:

The range [stencil, stencil + (map_last - map_first)) shall not overlap the range [result, result + (map_last - map_first)).

Pre:

The input data shall not overlap the range [result, result + (map_last - map_first)).

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename RandomAccessIterator, typename OutputIterator, typename Predicate> __host__ __device__ OutputIterator gather_if (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 map_first, InputIterator1 map_last, InputIterator2 stencil, RandomAccessIterator input_first, OutputIterator result, Predicate pred)

gather_if conditionally copies elements from a source array into a destination range according to a map. For each input iterator i in the range [map_first, map_last) such that the value of pred(*(stencil + (i - map_first))) is true, the value input_first[*i] is assigned to *(result + (i - map_first)). RandomAccessIterator must permit random access.

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

The following code snippet demonstrates how to use gather_if to gather selected values from an input range based on an arbitrary selection function using the thrust::device execution policy for parallelization:

Remark

gather_if is the inverse of scatter_if.

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

struct is_even
{
  __host__ __device__
  bool operator()(const int x)
  {
    return (x % 2) == 0;
  }
};
...

int values[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
thrust::device_vector<int> d_values(values, values + 10);

// we will select an element when our stencil is even
int stencil[10] = {0, 3, 4, 1, 4, 1, 2, 7, 8, 9};
thrust::device_vector<int> d_stencil(stencil, stencil + 10);

// map all even indices into the first half of the range
// and odd indices to the last half of the range
int map[10]   = {0, 2, 4, 6, 8, 1, 3, 5, 7, 9};
thrust::device_vector<int> d_map(map, map + 10);

thrust::device_vector<int> d_output(10, 7);
thrust::gather_if(thrust::device,
                  d_map.begin(), d_map.end(),
                  d_stencil.begin(),
                  d_values.begin(),
                  d_output.begin(),
                  is_even());
// d_output is now {0, 7, 4, 7, 8, 7, 3, 7, 7, 7}
Parameters:
  • exec – The execution policy to use for parallelization.

  • map_first – Beginning of the range of gather locations.

  • map_last – End of the range of gather locations.

  • stencil – Beginning of the range of predicate values.

  • input_first – Beginning of the source range.

  • result – Beginning of the destination range.

  • pred – Predicate to apply to the stencil values.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – must be a model of Input Iterator and InputIterator1's value_type must be convertible to RandomAccessIterator's difference_type.

  • InputIterator2 – must be a model of Input Iterator and InputIterator2's value_type must be convertible to Predicate's argument_type.

  • RandomAccessIterator – must be a model of Random Access iterator and RandomAccessIterator's value_type must be convertible to OutputIterator's value_type.

  • OutputIterator – must be a model of Output Iterator.

  • Predicate – must be a model of Predicate.

Pre:

The range [map_first, map_last) shall not overlap the range [result, result + (map_last - map_first)).

Pre:

The range [stencil, stencil + (map_last - map_first)) shall not overlap the range [result, result + (map_last - map_first)).

Pre:

The input data shall not overlap the range [result, result + (map_last - map_first)).

template<typename InputIterator1, typename InputIterator2, typename RandomAccessIterator, typename OutputIterator, typename Predicate>
OutputIterator gather_if(InputIterator1 map_first, InputIterator1 map_last, InputIterator2 stencil, RandomAccessIterator input_first, OutputIterator result, Predicate pred)#

gather_if conditionally copies elements from a source array into a destination range according to a map. For each input iterator i in the range [map_first, map_last) such that the value of pred(*(stencil + (i - map_first))) is true, the value input_first[*i] is assigned to *(result + (i - map_first)). RandomAccessIterator must permit random access.

The following code snippet demonstrates how to use gather_if to gather selected values from an input range based on an arbitrary selection function.

Remark

gather_if is the inverse of scatter_if.

#include <thrust/gather.h>
#include <thrust/device_vector.h>

struct is_even
{
  __host__ __device__
  bool operator()(const int x)
  {
    return (x % 2) == 0;
  }
};
...

int values[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
thrust::device_vector<int> d_values(values, values + 10);

// we will select an element when our stencil is even
int stencil[10] = {0, 3, 4, 1, 4, 1, 2, 7, 8, 9};
thrust::device_vector<int> d_stencil(stencil, stencil + 10);

// map all even indices into the first half of the range
// and odd indices to the last half of the range
int map[10]   = {0, 2, 4, 6, 8, 1, 3, 5, 7, 9};
thrust::device_vector<int> d_map(map, map + 10);

thrust::device_vector<int> d_output(10, 7);
thrust::gather_if(d_map.begin(), d_map.end(),
                  d_stencil.begin(),
                  d_values.begin(),
                  d_output.begin(),
                  is_even());
// d_output is now {0, 7, 4, 7, 8, 7, 3, 7, 7, 7}
Parameters:
  • map_first – Beginning of the range of gather locations.

  • map_last – End of the range of gather locations.

  • stencil – Beginning of the range of predicate values.

  • input_first – Beginning of the source range.

  • result – Beginning of the destination range.

  • pred – Predicate to apply to the stencil values.

Template Parameters:
  • InputIterator1 – must be a model of Input Iterator and InputIterator1's value_type must be convertible to RandomAccessIterator's difference_type.

  • InputIterator2 – must be a model of Input Iterator and InputIterator2's value_type must be convertible to Predicate's argument_type.

  • RandomAccessIterator – must be a model of Random Access iterator and RandomAccessIterator's value_type must be convertible to OutputIterator's value_type.

  • OutputIterator – must be a model of Output Iterator.

  • Predicate – must be a model of Predicate.

Pre:

The range [map_first, map_last) shall not overlap the range [result, result + (map_last - map_first)).

Pre:

The range [stencil, stencil + (map_last - map_first)) shall not overlap the range [result, result + (map_last - map_first)).

Pre:

The input data shall not overlap the range [result, result + (map_last - map_first)).

Functions

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename RandomAccessIterator> __host__ __device__ void scatter (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 first, InputIterator1 last, InputIterator2 map, RandomAccessIterator result)

scatter copies elements from a source range into an output array according to a map. For each iterator i in the range [first, last), the value *i is assigned to output[*(map + (i - first))]. The output iterator must permit random access. If the same index appears more than once in the range [map, map + (last - first)), the result is undefined.

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

The following code snippet demonstrates how to use scatter to reorder a range using the thrust::device execution policy for parallelization:

#include <thrust/scatter.h>
#include <thrust/device_vector.h>
#include <thrust/execution_policy.h>
...
// mark even indices with a 1; odd indices with a 0
int values[10] = {1, 0, 1, 0, 1, 0, 1, 0, 1, 0};
thrust::device_vector<int> d_values(values, values + 10);

// scatter all even indices into the first half of the
// range, and odd indices vice versa
int map[10]   = {0, 5, 1, 6, 2, 7, 3, 8, 4, 9};
thrust::device_vector<int> d_map(map, map + 10);

thrust::device_vector<int> d_output(10);
thrust::scatter(thrust::device,
                d_values.begin(), d_values.end(),
                d_map.begin(), d_output.begin());
// d_output is now {1, 1, 1, 1, 1, 0, 0, 0, 0, 0}

Note

scatter is the inverse of thrust::gather.

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – Beginning of the sequence of values to scatter.

  • last – End of the sequence of values to scatter.

  • map – Beginning of the sequence of output indices.

  • result – Destination of the source elements.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – must be a model of Input Iterator and InputIterator1's value_type must be convertible to RandomAccessIterator's value_type.

  • InputIterator2 – must be a model of Input Iterator and InputIterator2's value_type must be convertible to RandomAccessIterator's difference_type.

  • RandomAccessIterator – must be a model of Random Access iterator.

Pre:

The iterator result + i shall not refer to any element referenced by any iterator j in the range [first,last) for all iterators i in the range [map,map + (last - first)).

Pre:

The iterator result + i shall not refer to any element referenced by any iterator j in the range [map,map + (last - first)) for all iterators i in the range [map,map + (last - first)).

Pre:

The expression result[*i] shall be valid for all iterators in the range [map,map + (last - first)).

template<typename InputIterator1, typename InputIterator2, typename RandomAccessIterator>
void scatter(InputIterator1 first, InputIterator1 last, InputIterator2 map, RandomAccessIterator result)#

scatter copies elements from a source range into an output array according to a map. For each iterator i in the range [first, last), the value *i is assigned to output[*(map + (i - first))]. The output iterator must permit random access. If the same index appears more than once in the range [map, map + (last - first)), the result is undefined.

The following code snippet demonstrates how to use scatter to reorder a range.

#include <thrust/scatter.h>
#include <thrust/device_vector.h>
...
// mark even indices with a 1; odd indices with a 0
int values[10] = {1, 0, 1, 0, 1, 0, 1, 0, 1, 0};
thrust::device_vector<int> d_values(values, values + 10);

// scatter all even indices into the first half of the
// range, and odd indices vice versa
int map[10]   = {0, 5, 1, 6, 2, 7, 3, 8, 4, 9};
thrust::device_vector<int> d_map(map, map + 10);

thrust::device_vector<int> d_output(10);
thrust::scatter(d_values.begin(), d_values.end(),
                d_map.begin(), d_output.begin());
// d_output is now {1, 1, 1, 1, 1, 0, 0, 0, 0, 0}

Note

scatter is the inverse of thrust::gather.

Parameters:
  • first – Beginning of the sequence of values to scatter.

  • last – End of the sequence of values to scatter.

  • map – Beginning of the sequence of output indices.

  • result – Destination of the source elements.

Template Parameters:
  • InputIterator1 – must be a model of Input Iterator and InputIterator1's value_type must be convertible to RandomAccessIterator's value_type.

  • InputIterator2 – must be a model of Input Iterator and InputIterator2's value_type must be convertible to RandomAccessIterator's difference_type.

  • RandomAccessIterator – must be a model of Random Access iterator.

Pre:

The iterator result + i shall not refer to any element referenced by any iterator j in the range [first,last) for all iterators i in the range [map,map + (last - first)).

Pre:

The iterator result + i shall not refer to any element referenced by any iterator j in the range [map,map + (last - first)) for all iterators i in the range [map,map + (last - first)).

Pre:

The expression result[*i] shall be valid for all iterators in the range [map,map + (last - first)).

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename InputIterator3, typename RandomAccessIterator> __host__ __device__ void scatter_if (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 first, InputIterator1 last, InputIterator2 map, InputIterator3 stencil, RandomAccessIterator output)

scatter_if conditionally copies elements from a source range into an output array according to a map. For each iterator i in the range [first, last) such that *(stencil + (i - first)) is true, the value *i is assigned to output[*(map + (i - first))]. The output iterator must permit random access. If the same index appears more than once in the range [map, map + (last - first)) the result is undefined.

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

#include <thrust/scatter.h>
#include <thrust/execution_policy.h>
...
int V[8] = {10, 20, 30, 40, 50, 60, 70, 80};
int M[8] = {0, 5, 1, 6, 2, 7, 3, 4};
int S[8] = {1, 0, 1, 0, 1, 0, 1, 0};
int D[8] = {0, 0, 0, 0, 0, 0, 0, 0};

thrust::scatter_if(thrust::host, V, V + 8, M, S, D);

// D contains [10, 30, 50, 70, 0, 0, 0, 0];

Note

scatter_if is the inverse of thrust::gather_if.

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – Beginning of the sequence of values to scatter.

  • last – End of the sequence of values to scatter.

  • map – Beginning of the sequence of output indices.

  • stencil – Beginning of the sequence of predicate values.

  • output – Beginning of the destination range.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – must be a model of Input Iterator and InputIterator1's value_type must be convertible to RandomAccessIterator's value_type.

  • InputIterator2 – must be a model of Input Iterator and InputIterator2's value_type must be convertible to RandomAccessIterator's difference_type.

  • InputIterator3 – must be a model of Input Iterator and InputIterator3's value_type must be convertible to bool.

  • RandomAccessIterator – must be a model of Random Access iterator.

Pre:

The iterator result + i shall not refer to any element referenced by any iterator j in the range [first,last) for all iterators i in the range [map,map + (last - first)).

Pre:

The iterator result + i shall not refer to any element referenced by any iterator j in the range [map,map + (last - first)) for all iterators i in the range [map,map + (last - first)).

Pre:

The iterator result + i shall not refer to any element referenced by any iterator j in the range [stencil,stencil + (last - first)) for all iterators i in the range [map,map + (last - first)).

Pre:

The expression result[*i] shall be valid for all iterators i in the range [map,map + (last - first)) for which the following condition holds: *(stencil + i) != false.

template<typename InputIterator1, typename InputIterator2, typename InputIterator3, typename RandomAccessIterator>
void scatter_if(InputIterator1 first, InputIterator1 last, InputIterator2 map, InputIterator3 stencil, RandomAccessIterator output)#

scatter_if conditionally copies elements from a source range into an output array according to a map. For each iterator i in the range [first, last) such that *(stencil + (i - first)) is true, the value *i is assigned to output[*(map + (i - first))]. The output iterator must permit random access. If the same index appears more than once in the range [map, map + (last - first)) the result is undefined.

#include <thrust/scatter.h>
...
int V[8] = {10, 20, 30, 40, 50, 60, 70, 80};
int M[8] = {0, 5, 1, 6, 2, 7, 3, 4};
int S[8] = {1, 0, 1, 0, 1, 0, 1, 0};
int D[8] = {0, 0, 0, 0, 0, 0, 0, 0};

thrust::scatter_if(V, V + 8, M, S, D);

// D contains [10, 30, 50, 70, 0, 0, 0, 0];

Note

scatter_if is the inverse of thrust::gather_if.

Parameters:
  • first – Beginning of the sequence of values to scatter.

  • last – End of the sequence of values to scatter.

  • map – Beginning of the sequence of output indices.

  • stencil – Beginning of the sequence of predicate values.

  • output – Beginning of the destination range.

Template Parameters:
  • InputIterator1 – must be a model of Input Iterator and InputIterator1's value_type must be convertible to RandomAccessIterator's value_type.

  • InputIterator2 – must be a model of Input Iterator and InputIterator2's value_type must be convertible to RandomAccessIterator's difference_type.

  • InputIterator3 – must be a model of Input Iterator and InputIterator3's value_type must be convertible to bool.

  • RandomAccessIterator – must be a model of Random Access iterator.

Pre:

The iterator result + i shall not refer to any element referenced by any iterator j in the range [first,last) for all iterators i in the range [map,map + (last - first)).

Pre:

The iterator result + i shall not refer to any element referenced by any iterator j in the range [map,map + (last - first)) for all iterators i in the range [map,map + (last - first)).

Pre:

The iterator result + i shall not refer to any element referenced by any iterator j in the range [stencil,stencil + (last - first)) for all iterators i in the range [map,map + (last - first)).

Pre:

The expression result[*i] shall be valid for all iterators i in the range [map,map + (last - first)) for which the following condition holds: *(stencil + i) != false.

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename InputIterator3, typename RandomAccessIterator, typename Predicate> __host__ __device__ void scatter_if (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 first, InputIterator1 last, InputIterator2 map, InputIterator3 stencil, RandomAccessIterator output, Predicate pred)

scatter_if conditionally copies elements from a source range into an output array according to a map. For each iterator i in the range [first, last) such that pred(*(stencil + (i - first))) is true, the value *i is assigned to output[*(map + (i - first))]. The output iterator must permit random access. If the same index appears more than once in the range [map, map + (last - first)) the result is undefined.

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

#include <thrust/scatter.h>
#include <thrust/execution_policy.h>

struct is_even
{
  __host__ __device__
  bool operator()(int x)
  {
    return (x % 2) == 0;
  }
};

...

int V[8] = {10, 20, 30, 40, 50, 60, 70, 80};
int M[8] = {0, 5, 1, 6, 2, 7, 3, 4};
int S[8] = {2, 1, 2, 1, 2, 1, 2, 1};
int D[8] = {0, 0, 0, 0, 0, 0, 0, 0};

is_even pred;
thrust::scatter_if(thrust::host, V, V + 8, M, S, D, pred);

// D contains [10, 30, 50, 70, 0, 0, 0, 0];

Note

scatter_if is the inverse of thrust::gather_if.

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – Beginning of the sequence of values to scatter.

  • last – End of the sequence of values to scatter.

  • map – Beginning of the sequence of output indices.

  • stencil – Beginning of the sequence of predicate values.

  • output – Beginning of the destination range.

  • pred – Predicate to apply to the stencil values.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – must be a model of Input Iterator and InputIterator1's value_type must be convertible to RandomAccessIterator's value_type.

  • InputIterator2 – must be a model of Input Iterator and InputIterator2's value_type must be convertible to RandomAccessIterator's difference_type.

  • InputIterator3 – must be a model of Input Iterator and InputIterator3's value_type must be convertible to Predicate's argument_type.

  • RandomAccessIterator – must be a model of Random Access iterator.

  • Predicate – must be a model of Predicate.

Pre:

The iterator result + i shall not refer to any element referenced by any iterator j in the range [first,last) for all iterators i in the range [map,map + (last - first)).

Pre:

The iterator result + i shall not refer to any element referenced by any iterator j in the range [map,map + (last - first)) for all iterators i in the range [map,map + (last - first)).

Pre:

The iterator result + i shall not refer to any element referenced by any iterator j in the range [stencil,stencil + (last - first)) for all iterators i in the range [map,map + (last - first)).

Pre:

The expression result[*i] shall be valid for all iterators i in the range [map,map + (last - first)) for which the following condition holds: pred(*(stencil + i)) != false.

template<typename InputIterator1, typename InputIterator2, typename InputIterator3, typename RandomAccessIterator, typename Predicate>
void scatter_if(InputIterator1 first, InputIterator1 last, InputIterator2 map, InputIterator3 stencil, RandomAccessIterator output, Predicate pred)#

scatter_if conditionally copies elements from a source range into an output array according to a map. For each iterator i in the range [first, last) such that pred(*(stencil + (i - first))) is true, the value *i is assigned to output[*(map + (i - first))]. The output iterator must permit random access. If the same index appears more than once in the range [map, map + (last - first)) the result is undefined.

#include <thrust/scatter.h>

struct is_even
{
  __host__ __device__
  bool operator()(int x)
  {
    return (x % 2) == 0;
  }
};

...

int V[8] = {10, 20, 30, 40, 50, 60, 70, 80};
int M[8] = {0, 5, 1, 6, 2, 7, 3, 4};
int S[8] = {2, 1, 2, 1, 2, 1, 2, 1};
int D[8] = {0, 0, 0, 0, 0, 0, 0, 0};

is_even pred;
thrust::scatter_if(V, V + 8, M, S, D, pred);

// D contains [10, 30, 50, 70, 0, 0, 0, 0];

Note

scatter_if is the inverse of thrust::gather_if.

Parameters:
  • first – Beginning of the sequence of values to scatter.

  • last – End of the sequence of values to scatter.

  • map – Beginning of the sequence of output indices.

  • stencil – Beginning of the sequence of predicate values.

  • output – Beginning of the destination range.

  • pred – Predicate to apply to the stencil values.

Template Parameters:
  • InputIterator1 – must be a model of Input Iterator and InputIterator1's value_type must be convertible to RandomAccessIterator's value_type.

  • InputIterator2 – must be a model of Input Iterator and InputIterator2's value_type must be convertible to RandomAccessIterator's difference_type.

  • InputIterator3 – must be a model of Input Iterator and InputIterator3's value_type must be convertible to Predicate's argument_type.

  • RandomAccessIterator – must be a model of Random Access iterator.

  • Predicate – must be a model of Predicate.

Pre:

The iterator result + i shall not refer to any element referenced by any iterator j in the range [first,last) for all iterators i in the range [map,map + (last - first)).

Pre:

The iterator result + i shall not refer to any element referenced by any iterator j in the range [map,map + (last - first)) for all iterators i in the range [map,map + (last - first)).

Pre:

The iterator result + i shall not refer to any element referenced by any iterator j in the range [stencil,stencil + (last - first)) for all iterators i in the range [map,map + (last - first)).

Pre:

The expression result[*i] shall be valid for all iterators i in the range [map,map + (last - first)) for which the following condition holds: pred(*(stencil + i)) != false.

Functions

template<typename DerivedPolicy, typename InputIterator> __host__ __device__ thrust::iterator_traits< InputIterator >::value_type reduce (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator first, InputIterator last)

reduce is a generalization of summation: it computes the sum (or some other binary operation) of all the elements in the range [first, last). This version of reduce uses 0 as the initial value of the reduction. reduce is similar to the C++ Standard Template Library’s std::accumulate. The primary difference between the two functions is that std::accumulate guarantees the order of summation, while reduce requires associativity of the binary operation to parallelize the reduction.

Note that reduce also assumes that the binary reduction operator (in this case operator+) is commutative. If the reduction operator is not commutative then thrust::reduce should not be used. Instead, one could use inclusive_scan (which does not require commutativity) and select the last element of the output array.

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

The following code snippet demonstrates how to use reduce to compute the sum of a sequence of integers using the thrust::host execution policy for parallelization:

#include <thrust/reduce.h>
#include <thrust/execution_policy.h>
...
int data[6] = {1, 0, 2, 2, 1, 3};
int result = thrust::reduce(thrust::host, data, data + 6);

// result == 9

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the sequence.

  • last – The end of the sequence.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator – is a model of Input Iterator and if x and y are objects of InputIterator's value_type, then x + y is defined and is convertible to InputIterator's value_type. If T is InputIterator's value_type, then T(0) is defined.

Returns:

The result of the reduction.

template<typename InputIterator>
thrust::iterator_traits<InputIterator>::value_type reduce(InputIterator first, InputIterator last)#

reduce is a generalization of summation: it computes the sum (or some other binary operation) of all the elements in the range [first, last). This version of reduce uses 0 as the initial value of the reduction. reduce is similar to the C++ Standard Template Library’s std::accumulate. The primary difference between the two functions is that std::accumulate guarantees the order of summation, while reduce requires associativity of the binary operation to parallelize the reduction.

Note that reduce also assumes that the binary reduction operator (in this case operator+) is commutative. If the reduction operator is not commutative then thrust::reduce should not be used. Instead, one could use inclusive_scan (which does not require commutativity) and select the last element of the output array.

The following code snippet demonstrates how to use reduce to compute the sum of a sequence of integers.

#include <thrust/reduce.h>
...
int data[6] = {1, 0, 2, 2, 1, 3};
int result = thrust::reduce(data, data + 6);

// result == 9

Parameters:
  • first – The beginning of the sequence.

  • last – The end of the sequence.

Template Parameters:

InputIterator – is a model of Input Iterator and if x and y are objects of InputIterator's value_type, then x + y is defined and is convertible to InputIterator's value_type. If T is InputIterator's value_type, then T(0) is defined.

Returns:

The result of the reduction.

template<typename DerivedPolicy, typename InputIterator, typename T> __host__ __device__ T reduce (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator first, InputIterator last, T init)

reduce is a generalization of summation: it computes the sum (or some other binary operation) of all the elements in the range [first, last). This version of reduce uses init as the initial value of the reduction. reduce is similar to the C++ Standard Template Library’s std::accumulate. The primary difference between the two functions is that std::accumulate guarantees the order of summation, while reduce requires associativity of the binary operation to parallelize the reduction.

Note that reduce also assumes that the binary reduction operator (in this case operator+) is commutative. If the reduction operator is not commutative then thrust::reduce should not be used. Instead, one could use inclusive_scan (which does not require commutativity) and select the last element of the output array.

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

The following code snippet demonstrates how to use reduce to compute the sum of a sequence of integers including an intialization value using the thrust::host execution policy for parallelization:

#include <thrust/reduce.h>
#include <thrust/execution_policy.h>
...
int data[6] = {1, 0, 2, 2, 1, 3};
int result = thrust::reduce(thrust::host, data, data + 6, 1);

// result == 10

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the input sequence.

  • last – The end of the input sequence.

  • init – The initial value.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator – is a model of Input Iterator and if x and y are objects of InputIterator's value_type, then x + y is defined and is convertible to T.

  • T – is convertible to InputIterator's value_type.

Returns:

The result of the reduction.

template<typename InputIterator, typename T>
T reduce(InputIterator first, InputIterator last, T init)#

reduce is a generalization of summation: it computes the sum (or some other binary operation) of all the elements in the range [first, last). This version of reduce uses init as the initial value of the reduction. reduce is similar to the C++ Standard Template Library’s std::accumulate. The primary difference between the two functions is that std::accumulate guarantees the order of summation, while reduce requires associativity of the binary operation to parallelize the reduction.

Note that reduce also assumes that the binary reduction operator (in this case operator+) is commutative. If the reduction operator is not commutative then thrust::reduce should not be used. Instead, one could use inclusive_scan (which does not require commutativity) and select the last element of the output array.

The following code snippet demonstrates how to use reduce to compute the sum of a sequence of integers including an intialization value.

#include <thrust/reduce.h>
...
int data[6] = {1, 0, 2, 2, 1, 3};
int result = thrust::reduce(data, data + 6, 1);

// result == 10

Parameters:
  • first – The beginning of the input sequence.

  • last – The end of the input sequence.

  • init – The initial value.

Template Parameters:
  • InputIterator – is a model of Input Iterator and if x and y are objects of InputIterator's value_type, then x + y is defined and is convertible to T.

  • T – is convertible to InputIterator's value_type.

Returns:

The result of the reduction.

template<typename DerivedPolicy, typename InputIterator, typename T, typename BinaryFunction> __host__ __device__ T reduce (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator first, InputIterator last, T init, BinaryFunction binary_op)

reduce is a generalization of summation: it computes the sum (or some other binary operation) of all the elements in the range [first, last). This version of reduce uses init as the initial value of the reduction and binary_op as the binary function used for summation. reduce is similar to the C++ Standard Template Library’s std::accumulate. The primary difference between the two functions is that std::accumulate guarantees the order of summation, while reduce requires associativity of binary_op to parallelize the reduction.

Note that reduce also assumes that the binary reduction operator (in this case binary_op) is commutative. If the reduction operator is not commutative then thrust::reduce should not be used. Instead, one could use inclusive_scan (which does not require commutativity) and select the last element of the output array.

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

The following code snippet demonstrates how to use reduce to compute the maximum value of a sequence of integers using the thrust::host execution policy for parallelization:

#include <thrust/reduce.h>
#include <thrust/functional.h>
#include <thrust/execution_policy.h>
...
int data[6] = {1, 0, 2, 2, 1, 3};
int result = thrust::reduce(thrust::host,
                            data, data + 6,
                            -1,
                            thrust::maximum<int>());
// result == 3

See also

transform_reduce

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the input sequence.

  • last – The end of the input sequence.

  • init – The initial value.

  • binary_op – The binary function used to ‘sum’ values.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator – is a model of Input Iterator and InputIterator's value_type is convertible to T.

  • T – is a model of Assignable, and is convertible to BinaryFunction's first_argument_type and second_argument_type.

  • BinaryFunction – is a model of Binary Function, and BinaryFunction's result_type is convertible to OutputType.

Returns:

The result of the reduction.

template<typename InputIterator, typename T, typename BinaryFunction>
T reduce(InputIterator first, InputIterator last, T init, BinaryFunction binary_op)#

reduce is a generalization of summation: it computes the sum (or some other binary operation) of all the elements in the range [first, last). This version of reduce uses init as the initial value of the reduction and binary_op as the binary function used for summation. reduce is similar to the C++ Standard Template Library’s std::accumulate. The primary difference between the two functions is that std::accumulate guarantees the order of summation, while reduce requires associativity of binary_op to parallelize the reduction.

Note that reduce also assumes that the binary reduction operator (in this case binary_op) is commutative. If the reduction operator is not commutative then thrust::reduce should not be used. Instead, one could use inclusive_scan (which does not require commutativity) and select the last element of the output array.

The following code snippet demonstrates how to use reduce to compute the maximum value of a sequence of integers.

#include <thrust/reduce.h>
#include <thrust/functional.h>
...
int data[6] = {1, 0, 2, 2, 1, 3};
int result = thrust::reduce(data, data + 6,
                            -1,
                            thrust::maximum<int>());
// result == 3

See also

transform_reduce

Parameters:
  • first – The beginning of the input sequence.

  • last – The end of the input sequence.

  • init – The initial value.

  • binary_op – The binary function used to ‘sum’ values.

Template Parameters:
  • InputIterator – is a model of Input Iterator and InputIterator's value_type is convertible to T.

  • T – is a model of Assignable, and is convertible to BinaryFunction's first_argument_type and second_argument_type.

  • BinaryFunction – is a model of Binary Function, and BinaryFunction's result_type is convertible to OutputType.

Returns:

The result of the reduction.

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename OutputIterator1, typename OutputIterator2> __host__ __device__ thrust::pair< OutputIterator1, OutputIterator2 > reduce_by_key (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 keys_first, InputIterator1 keys_last, InputIterator2 values_first, OutputIterator1 keys_output, OutputIterator2 values_output)

reduce_by_key is a generalization of reduce to key-value pairs. For each group of consecutive keys in the range [keys_first, keys_last) that are equal, reduce_by_key copies the first element of the group to the keys_output. The corresponding values in the range are reduced using the plus and the result copied to values_output.

This version of reduce_by_key uses the function object equal_to to test for equality and plus to reduce values with equal keys.

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

The following code snippet demonstrates how to use reduce_by_key to compact a sequence of key/value pairs and sum values with equal keys using the thrust::host execution policy for parallelization:

#include <thrust/reduce.h>
#include <thrust/execution_policy.h>
...
const int N = 7;
int A[N] = {1, 3, 3, 3, 2, 2, 1}; // input keys
int B[N] = {9, 8, 7, 6, 5, 4, 3}; // input values
int C[N];                         // output keys
int D[N];                         // output values

thrust::pair<int*,int*> new_end;
new_end = thrust::reduce_by_key(thrust::host, A, A + N, B, C, D);

// The first four keys in C are now {1, 3, 2, 1} and new_end.first - C is 4.
// The first four values in D are now {9, 21, 9, 3} and new_end.second - D is 4.

See also

reduce

See also

unique_copy

See also

unique_by_key

Parameters:
  • exec – The execution policy to use for parallelization.

  • keys_first – The beginning of the input key range.

  • keys_last – The end of the input key range.

  • values_first – The beginning of the input value range.

  • keys_output – The beginning of the output key range.

  • values_output – The beginning of the output value range.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator,

  • InputIterator2 – is a model of Input Iterator,

  • OutputIterator1 – is a model of Output Iterator and and InputIterator1's value_type is convertible to OutputIterator1's value_type.

  • OutputIterator2 – is a model of Output Iterator and and InputIterator2's value_type is convertible to OutputIterator2's value_type.

Returns:

A pair of iterators at end of the ranges [keys_output, keys_output_last) and [values_output, values_output_last).

Pre:

The input ranges shall not overlap either output range.

template<typename InputIterator1, typename InputIterator2, typename OutputIterator1, typename OutputIterator2>
thrust::pair<OutputIterator1, OutputIterator2> reduce_by_key(InputIterator1 keys_first, InputIterator1 keys_last, InputIterator2 values_first, OutputIterator1 keys_output, OutputIterator2 values_output)#

reduce_by_key is a generalization of reduce to key-value pairs. For each group of consecutive keys in the range [keys_first, keys_last) that are equal, reduce_by_key copies the first element of the group to the keys_output. The corresponding values in the range are reduced using the plus and the result copied to values_output.

This version of reduce_by_key uses the function object equal_to to test for equality and plus to reduce values with equal keys.

The following code snippet demonstrates how to use reduce_by_key to compact a sequence of key/value pairs and sum values with equal keys.

#include <thrust/reduce.h>
...
const int N = 7;
int A[N] = {1, 3, 3, 3, 2, 2, 1}; // input keys
int B[N] = {9, 8, 7, 6, 5, 4, 3}; // input values
int C[N];                         // output keys
int D[N];                         // output values

thrust::pair<int*,int*> new_end;
new_end = thrust::reduce_by_key(A, A + N, B, C, D);

// The first four keys in C are now {1, 3, 2, 1} and new_end.first - C is 4.
// The first four values in D are now {9, 21, 9, 3} and new_end.second - D is 4.

See also

reduce

See also

unique_copy

See also

unique_by_key

Parameters:
  • keys_first – The beginning of the input key range.

  • keys_last – The end of the input key range.

  • values_first – The beginning of the input value range.

  • keys_output – The beginning of the output key range.

  • values_output – The beginning of the output value range.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator,

  • InputIterator2 – is a model of Input Iterator,

  • OutputIterator1 – is a model of Output Iterator and and InputIterator1's value_type is convertible to OutputIterator1's value_type.

  • OutputIterator2 – is a model of Output Iterator and and InputIterator2's value_type is convertible to OutputIterator2's value_type.

Returns:

A pair of iterators at end of the ranges [keys_output, keys_output_last) and [values_output, values_output_last).

Pre:

The input ranges shall not overlap either output range.

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename OutputIterator1, typename OutputIterator2, typename BinaryPredicate> __host__ __device__ thrust::pair< OutputIterator1, OutputIterator2 > reduce_by_key (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 keys_first, InputIterator1 keys_last, InputIterator2 values_first, OutputIterator1 keys_output, OutputIterator2 values_output, BinaryPredicate binary_pred)

reduce_by_key is a generalization of reduce to key-value pairs. For each group of consecutive keys in the range [keys_first, keys_last) that are equal, reduce_by_key copies the first element of the group to the keys_output. The corresponding values in the range are reduced using the plus and the result copied to values_output.

This version of reduce_by_key uses the function object binary_pred to test for equality and plus to reduce values with equal keys.

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

The following code snippet demonstrates how to use reduce_by_key to compact a sequence of key/value pairs and sum values with equal keys using the thrust::host execution policy for parallelization:

#include <thrust/reduce.h>
#include <thrust/execution_policy.h>
...
const int N = 7;
int A[N] = {1, 3, 3, 3, 2, 2, 1}; // input keys
int B[N] = {9, 8, 7, 6, 5, 4, 3}; // input values
int C[N];                         // output keys
int D[N];                         // output values

thrust::pair<int*,int*> new_end;
thrust::equal_to<int> binary_pred;
new_end = thrust::reduce_by_key(thrust::host, A, A + N, B, C, D, binary_pred);

// The first four keys in C are now {1, 3, 2, 1} and new_end.first - C is 4.
// The first four values in D are now {9, 21, 9, 3} and new_end.second - D is 4.

See also

reduce

See also

unique_copy

See also

unique_by_key

Parameters:
  • exec – The execution policy to use for parallelization.

  • keys_first – The beginning of the input key range.

  • keys_last – The end of the input key range.

  • values_first – The beginning of the input value range.

  • keys_output – The beginning of the output key range.

  • values_output – The beginning of the output value range.

  • binary_pred – The binary predicate used to determine equality.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator,

  • InputIterator2 – is a model of Input Iterator,

  • OutputIterator1 – is a model of Output Iterator and and InputIterator1's value_type is convertible to OutputIterator1's value_type.

  • OutputIterator2 – is a model of Output Iterator and and InputIterator2's value_type is convertible to OutputIterator2's value_type.

  • BinaryPredicate – is a model of Binary Predicate.

Returns:

A pair of iterators at end of the ranges [keys_output, keys_output_last) and [values_output, values_output_last).

Pre:

The input ranges shall not overlap either output range.

template<typename InputIterator1, typename InputIterator2, typename OutputIterator1, typename OutputIterator2, typename BinaryPredicate>
thrust::pair<OutputIterator1, OutputIterator2> reduce_by_key(InputIterator1 keys_first, InputIterator1 keys_last, InputIterator2 values_first, OutputIterator1 keys_output, OutputIterator2 values_output, BinaryPredicate binary_pred)#

reduce_by_key is a generalization of reduce to key-value pairs. For each group of consecutive keys in the range [keys_first, keys_last) that are equal, reduce_by_key copies the first element of the group to the keys_output. The corresponding values in the range are reduced using the plus and the result copied to values_output.

This version of reduce_by_key uses the function object binary_pred to test for equality and plus to reduce values with equal keys.

The following code snippet demonstrates how to use reduce_by_key to compact a sequence of key/value pairs and sum values with equal keys.

#include <thrust/reduce.h>
...
const int N = 7;
int A[N] = {1, 3, 3, 3, 2, 2, 1}; // input keys
int B[N] = {9, 8, 7, 6, 5, 4, 3}; // input values
int C[N];                         // output keys
int D[N];                         // output values

thrust::pair<int*,int*> new_end;
thrust::equal_to<int> binary_pred;
new_end = thrust::reduce_by_key(A, A + N, B, C, D, binary_pred);

// The first four keys in C are now {1, 3, 2, 1} and new_end.first - C is 4.
// The first four values in D are now {9, 21, 9, 3} and new_end.second - D is 4.

See also

reduce

See also

unique_copy

See also

unique_by_key

Parameters:
  • keys_first – The beginning of the input key range.

  • keys_last – The end of the input key range.

  • values_first – The beginning of the input value range.

  • keys_output – The beginning of the output key range.

  • values_output – The beginning of the output value range.

  • binary_pred – The binary predicate used to determine equality.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator,

  • InputIterator2 – is a model of Input Iterator,

  • OutputIterator1 – is a model of Output Iterator and and InputIterator1's value_type is convertible to OutputIterator1's value_type.

  • OutputIterator2 – is a model of Output Iterator and and InputIterator2's value_type is convertible to OutputIterator2's value_type.

  • BinaryPredicate – is a model of Binary Predicate.

Returns:

A pair of iterators at end of the ranges [keys_output, keys_output_last) and [values_output, values_output_last).

Pre:

The input ranges shall not overlap either output range.

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename OutputIterator1, typename OutputIterator2, typename BinaryPredicate, typename BinaryFunction> __host__ __device__ thrust::pair< OutputIterator1, OutputIterator2 > reduce_by_key (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 keys_first, InputIterator1 keys_last, InputIterator2 values_first, OutputIterator1 keys_output, OutputIterator2 values_output, BinaryPredicate binary_pred, BinaryFunction binary_op)

reduce_by_key is a generalization of reduce to key-value pairs. For each group of consecutive keys in the range [keys_first, keys_last) that are equal, reduce_by_key copies the first element of the group to the keys_output. The corresponding values in the range are reduced using the BinaryFunction binary_op and the result copied to values_output. Specifically, if consecutive key iterators i and (i + 1) are such that binary_pred(*i, *(i+1)) is true, then the corresponding values are reduced to a single value with binary_op.

This version of reduce_by_key uses the function object binary_pred to test for equality and binary_op to reduce values with equal keys.

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

The following code snippet demonstrates how to use reduce_by_key to compact a sequence of key/value pairs and sum values with equal keys using the thrust::host execution policy for parallelization:

#include <thrust/reduce.h>
#include <thrust/execution_policy.h>
...
const int N = 7;
int A[N] = {1, 3, 3, 3, 2, 2, 1}; // input keys
int B[N] = {9, 8, 7, 6, 5, 4, 3}; // input values
int C[N];                         // output keys
int D[N];                         // output values

thrust::pair<int*,int*> new_end;
thrust::equal_to<int> binary_pred;
thrust::plus<int> binary_op;
new_end = thrust::reduce_by_key(thrust::host, A, A + N, B, C, D, binary_pred, binary_op);

// The first four keys in C are now {1, 3, 2, 1} and new_end.first - C is 4.
// The first four values in D are now {9, 21, 9, 3} and new_end.second - D is 4.

See also

reduce

See also

unique_copy

See also

unique_by_key

Parameters:
  • exec – The execution policy to use for parallelization.

  • keys_first – The beginning of the input key range.

  • keys_last – The end of the input key range.

  • values_first – The beginning of the input value range.

  • keys_output – The beginning of the output key range.

  • values_output – The beginning of the output value range.

  • binary_pred – The binary predicate used to determine equality.

  • binary_op – The binary function used to accumulate values.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator,

  • InputIterator2 – is a model of Input Iterator,

  • OutputIterator1 – is a model of Output Iterator and and InputIterator1's value_type is convertible to OutputIterator1's value_type.

  • OutputIterator2 – is a model of Output Iterator and and InputIterator2's value_type is convertible to OutputIterator2's value_type.

  • BinaryPredicate – is a model of Binary Predicate.

  • BinaryFunction – is a model of Binary Function and BinaryFunction's result_type is convertible to OutputIterator2's value_type.

Returns:

A pair of iterators at end of the ranges [keys_output, keys_output_last) and [values_output, values_output_last).

Pre:

The input ranges shall not overlap either output range.

template<typename InputIterator1, typename InputIterator2, typename OutputIterator1, typename OutputIterator2, typename BinaryPredicate, typename BinaryFunction>
thrust::pair<OutputIterator1, OutputIterator2> reduce_by_key(InputIterator1 keys_first, InputIterator1 keys_last, InputIterator2 values_first, OutputIterator1 keys_output, OutputIterator2 values_output, BinaryPredicate binary_pred, BinaryFunction binary_op)#

reduce_by_key is a generalization of reduce to key-value pairs. For each group of consecutive keys in the range [keys_first, keys_last) that are equal, reduce_by_key copies the first element of the group to the keys_output. The corresponding values in the range are reduced using the BinaryFunction binary_op and the result copied to values_output. Specifically, if consecutive key iterators i and (i + 1) are such that binary_pred(*i, *(i+1)) is true, then the corresponding values are reduced to a single value with binary_op.

This version of reduce_by_key uses the function object binary_pred to test for equality and binary_op to reduce values with equal keys.

The following code snippet demonstrates how to use reduce_by_key to compact a sequence of key/value pairs and sum values with equal keys.

#include <thrust/reduce.h>
...
const int N = 7;
int A[N] = {1, 3, 3, 3, 2, 2, 1}; // input keys
int B[N] = {9, 8, 7, 6, 5, 4, 3}; // input values
int C[N];                         // output keys
int D[N];                         // output values

thrust::pair<int*,int*> new_end;
thrust::equal_to<int> binary_pred;
thrust::plus<int> binary_op;
new_end = thrust::reduce_by_key(A, A + N, B, C, D, binary_pred, binary_op);

// The first four keys in C are now {1, 3, 2, 1} and new_end.first - C is 4.
// The first four values in D are now {9, 21, 9, 3} and new_end.second - D is 4.

See also

reduce

See also

unique_copy

See also

unique_by_key

Parameters:
  • keys_first – The beginning of the input key range.

  • keys_last – The end of the input key range.

  • values_first – The beginning of the input value range.

  • keys_output – The beginning of the output key range.

  • values_output – The beginning of the output value range.

  • binary_pred – The binary predicate used to determine equality.

  • binary_op – The binary function used to accumulate values.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator,

  • InputIterator2 – is a model of Input Iterator,

  • OutputIterator1 – is a model of Output Iterator and and InputIterator1's value_type is convertible to OutputIterator1's value_type.

  • OutputIterator2 – is a model of Output Iterator and and InputIterator2's value_type is convertible to OutputIterator2's value_type.

  • BinaryPredicate – is a model of Binary Predicate.

  • BinaryFunction – is a model of Binary Function and BinaryFunction's result_type is convertible to OutputIterator2's value_type.

Returns:

A pair of iterators at end of the ranges [keys_output, keys_output_last) and [values_output, values_output_last).

Pre:

The input ranges shall not overlap either output range.

Functions

template<typename DerivedPolicy, typename InputIterator, typename EqualityComparable> __host__ __device__ thrust::iterator_traits< InputIterator >::difference_type count (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator first, InputIterator last, const EqualityComparable &value)

count finds the number of elements in [first,last) that are equal to value. More precisely, count returns the number of iterators i in [first, last) such that *i == value.

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

The following code snippet demonstrates how to use count to count the number of instances in a range of a value of interest using the thrust::device execution policy:

#include <thrust/count.h>
#include <thrust/device_vector.h>
#include <thrust/execution_policy.h>
...
// put 3 1s in a device_vector
thrust::device_vector<int> vec(5,0);
vec[1] = 1;
vec[3] = 1;
vec[4] = 1;

// count the 1s
int result = thrust::count(thrust::device, vec.begin(), vec.end(), 1);
// result == 3

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the sequence.

  • last – The end of the sequence.

  • value – The value to be counted.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator – must be a model of Input Iterator and InputIterator's value_type must be a model of must be a model of Equality Comparable.

  • EqualityComparable – must be a model of Equality Comparable and can be compared for equality with InputIterator's value_type

Returns:

The number of elements equal to value.

template<typename InputIterator, typename EqualityComparable>
thrust::iterator_traits<InputIterator>::difference_type count(InputIterator first, InputIterator last, const EqualityComparable &value)#

count finds the number of elements in [first,last) that are equal to value. More precisely, count returns the number of iterators i in [first, last) such that *i == value.

The following code snippet demonstrates how to use count to count the number of instances in a range of a value of interest.

#include <thrust/count.h>
#include <thrust/device_vector.h>
...
// put 3 1s in a device_vector
thrust::device_vector<int> vec(5,0);
vec[1] = 1;
vec[3] = 1;
vec[4] = 1;

// count the 1s
int result = thrust::count(vec.begin(), vec.end(), 1);
// result == 3

Parameters:
  • first – The beginning of the sequence.

  • last – The end of the sequence.

  • value – The value to be counted.

Template Parameters:
  • InputIterator – must be a model of Input Iterator and InputIterator's value_type must be a model of must be a model of Equality Comparable.

  • EqualityComparable – must be a model of Equality Comparable and can be compared for equality with InputIterator's value_type

Returns:

The number of elements equal to value.

template<typename DerivedPolicy, typename InputIterator, typename Predicate> __host__ __device__ thrust::iterator_traits< InputIterator >::difference_type count_if (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator first, InputIterator last, Predicate pred)

count_if finds the number of elements in [first,last) for which a predicate is true. More precisely, count_if returns the number of iterators i in [first, last) such that pred(*i) == true.

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

The following code snippet demonstrates how to use count to count the number of odd numbers in a range using the thrust::device execution policy:

#include <thrust/count.h>
#include <thrust/device_vector.h>
#include <thrust/execution_policy.h>
...
struct is_odd
{
  __host__ __device__
  bool operator()(int &x)
  {
    return x & 1;
  }
};
...
// fill a device_vector with even & odd numbers
thrust::device_vector<int> vec(5);
vec[0] = 0;
vec[1] = 1;
vec[2] = 2;
vec[3] = 3;
vec[4] = 4;

// count the odd elements in vec
int result = thrust::count_if(thrust::device, vec.begin(), vec.end(), is_odd());
// result == 2

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the sequence.

  • last – The end of the sequence.

  • pred – The predicate.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator – must be a model of Input Iterator and InputIterator's value_type must be convertible to Predicate's argument_type.

  • Predicate – must be a model of Predicate.

Returns:

The number of elements where pred is true.

template<typename InputIterator, typename Predicate>
thrust::iterator_traits<InputIterator>::difference_type count_if(InputIterator first, InputIterator last, Predicate pred)#

count_if finds the number of elements in [first,last) for which a predicate is true. More precisely, count_if returns the number of iterators i in [first, last) such that pred(*i) == true.

The following code snippet demonstrates how to use count to count the number of odd numbers in a range.

#include <thrust/count.h>
#include <thrust/device_vector.h>
...
struct is_odd
{
  __host__ __device__
  bool operator()(int &x)
  {
    return x & 1;
  }
};
...
// fill a device_vector with even & odd numbers
thrust::device_vector<int> vec(5);
vec[0] = 0;
vec[1] = 1;
vec[2] = 2;
vec[3] = 3;
vec[4] = 4;

// count the odd elements in vec
int result = thrust::count_if(vec.begin(), vec.end(), is_odd());
// result == 2

Parameters:
  • first – The beginning of the sequence.

  • last – The end of the sequence.

  • pred – The predicate.

Template Parameters:
  • InputIterator – must be a model of Input Iterator and InputIterator's value_type must be convertible to Predicate's argument_type.

  • Predicate – must be a model of Predicate.

Returns:

The number of elements where pred is true.

Functions

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2> __host__ __device__ bool equal (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2)

equal returns true if the two ranges [first1, last1) and [first2, first2 + (last1 - first1)) are identical when compared element-by-element, and otherwise returns false.

This version of equal returns true if and only if for every iterator i in [first1, last1), *i == *(first2 + (i - first1)).

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

The following code snippet demonstrates how to use equal to test two ranges for equality using the thrust::host execution policy:

#include <thrust/equal.h>
#include <thrust/execution_policy.h>
...
int A1[7] = {3, 1, 4, 1, 5, 9, 3};
int A2[7] = {3, 1, 4, 2, 8, 5, 7};
...
bool result = thrust::equal(thrust::host, A1, A1 + 7, A2);

// result == false

Parameters:
  • exec – The execution policy to use for parallelization.

  • first1 – The beginning of the first sequence.

  • last1 – The end of the first sequence.

  • first2 – The beginning of the second sequence.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator, and InputIterator1's value_type is a model of Equality Comparable, and InputIterator1's value_type can be compared for equality with InputIterator2's value_type.

  • InputIterator2 – is a model of Input Iterator, and InputIterator2's value_type is a model of Equality Comparable, and InputIterator2's value_type can be compared for equality with InputIterator1's value_type.

Returns:

true, if the sequences are equal; false, otherwise.

template<typename InputIterator1, typename InputIterator2>
bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2)#

equal returns true if the two ranges [first1, last1) and [first2, first2 + (last1 - first1)) are identical when compared element-by-element, and otherwise returns false.

This version of equal returns true if and only if for every iterator i in [first1, last1), *i == *(first2 + (i - first1)).

The following code snippet demonstrates how to use equal to test two ranges for equality.

#include <thrust/equal.h>
...
int A1[7] = {3, 1, 4, 1, 5, 9, 3};
int A2[7] = {3, 1, 4, 2, 8, 5, 7};
...
bool result = thrust::equal(A1, A1 + 7, A2);

// result == false

Parameters:
  • first1 – The beginning of the first sequence.

  • last1 – The end of the first sequence.

  • first2 – The beginning of the second sequence.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator, and InputIterator1's value_type is a model of Equality Comparable, and InputIterator1's value_type can be compared for equality with InputIterator2's value_type.

  • InputIterator2 – is a model of Input Iterator, and InputIterator2's value_type is a model of Equality Comparable, and InputIterator2's value_type can be compared for equality with InputIterator1's value_type.

Returns:

true, if the sequences are equal; false, otherwise.

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename BinaryPredicate> __host__ __device__ bool equal (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate binary_pred)

equal returns true if the two ranges [first1, last1) and [first2, first2 + (last1 - first1)) are identical when compared element-by-element, and otherwise returns false.

This version of equal returns true if and only if for every iterator i in [first1, last1), binary_pred(*i, *(first2 + (i - first1))) is true.

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

The following code snippet demonstrates how to use equal to compare the elements in two ranges modulo 2 using the thrust::host execution policy.

#include <thrust/equal.h>
#include <thrust/execution_policy.h>
...

struct compare_modulo_two
{
  __host__ __device__
  bool operator()(int x, int y) const
  {
    return (x % 2) == (y % 2);
  }
};
...
int x[6] = {0, 2, 4, 6, 8, 10};
int y[6] = {1, 3, 5, 7, 9, 11};

bool result = thrust::equal(x, x + 6, y, compare_modulo_two());

// result is false

Parameters:
  • exec – The execution policy to use for parallelization.

  • first1 – The beginning of the first sequence.

  • last1 – The end of the first sequence.

  • first2 – The beginning of the second sequence.

  • binary_pred – Binary predicate used to test element equality.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator, and InputIterator1's value_type is convertible to BinaryPredicate's first_argument_type.

  • InputIterator2 – is a model of Input Iterator, and InputIterator2's value_type is convertible to BinaryPredicate's second_argument_type.

  • BinaryPredicate – is a model of Binary Predicate.

Returns:

true, if the sequences are equal; false, otherwise.

template<typename InputIterator1, typename InputIterator2, typename BinaryPredicate>
bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate binary_pred)#

equal returns true if the two ranges [first1, last1) and [first2, first2 + (last1 - first1)) are identical when compared element-by-element, and otherwise returns false.

This version of equal returns true if and only if for every iterator i in [first1, last1), binary_pred(*i, *(first2 + (i - first1))) is true.

The following code snippet demonstrates how to use equal to compare the elements in two ranges modulo 2.

#include <thrust/equal.h>

struct compare_modulo_two
{
  __host__ __device__
  bool operator()(int x, int y) const
  {
    return (x % 2) == (y % 2);
  }
};
...
int x[6] = {0, 2, 4, 6, 8, 10};
int y[6] = {1, 3, 5, 7, 9, 11};

bool result = thrust::equal(x, x + 5, y, compare_modulo_two());

// result is true

Parameters:
  • first1 – The beginning of the first sequence.

  • last1 – The end of the first sequence.

  • first2 – The beginning of the second sequence.

  • binary_pred – Binary predicate used to test element equality.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator, and InputIterator1's value_type is convertible to BinaryPredicate's first_argument_type.

  • InputIterator2 – is a model of Input Iterator, and InputIterator2's value_type is convertible to BinaryPredicate's second_argument_type.

  • BinaryPredicate – is a model of Binary Predicate.

Returns:

true, if the sequences are equal; false, otherwise.

Functions

template<typename DerivedPolicy, typename ForwardIterator> __host__ __device__ ForwardIterator min_element (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last)

min_element finds the smallest element in the range [first, last). It returns the first iterator i in [first, last) such that no other iterator in [first, last) points to a value smaller than *i. The return value is last if and only if [first, last) is an empty range.

The two versions of min_element differ in how they define whether one element is less than another. This version compares objects using operator<. Specifically, this version of min_element returns the first iterator i in [first, last) such that, for every iterator j in [first, last), *j < *i is false.

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

#include <thrust/extrema.h>
#include <thrust/execution_policy.h>
...
int data[6] = {1, 0, 2, 2, 1, 3};
int *result = thrust::min_element(thrust::host, data, data + 6);

// result is data + 1
// *result is 0

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the sequence.

  • last – The end of the sequence.

Template Parameters:

ForwardIterator – is a model of Forward Iterator, and ForwardIterator's value_type is a model of LessThan Comparable.

Returns:

An iterator pointing to the smallest element of the range [first, last), if it is not an empty range; last, otherwise.

template<typename ForwardIterator>
ForwardIterator min_element(ForwardIterator first, ForwardIterator last)#

min_element finds the smallest element in the range [first, last). It returns the first iterator i in [first, last) such that no other iterator in [first, last) points to a value smaller than *i. The return value is last if and only if [first, last) is an empty range.

The two versions of min_element differ in how they define whether one element is less than another. This version compares objects using operator<. Specifically, this version of min_element returns the first iterator i in [first, last) such that, for every iterator j in [first, last), *j < *i is false.

#include <thrust/extrema.h>
...
int data[6] = {1, 0, 2, 2, 1, 3};
int *result = thrust::min_element(data, data + 6);

// result is data + 1
// *result is 0

Parameters:
  • first – The beginning of the sequence.

  • last – The end of the sequence.

Template Parameters:

ForwardIterator – is a model of Forward Iterator, and ForwardIterator's value_type is a model of LessThan Comparable.

Returns:

An iterator pointing to the smallest element of the range [first, last), if it is not an empty range; last, otherwise.

template<typename DerivedPolicy, typename ForwardIterator, typename BinaryPredicate> __host__ __device__ ForwardIterator min_element (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last, BinaryPredicate comp)

min_element finds the smallest element in the range [first, last). It returns the first iterator i in [first, last) such that no other iterator in [first, last) points to a value smaller than *i. The return value is last if and only if [first, last) is an empty range.

The two versions of min_element differ in how they define whether one element is less than another. This version compares objects using a function object comp. Specifically, this version of min_element returns the first iterator i in [first, last) such that, for every iterator j in [first, last), comp(*j, *i) is false.

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

The following code snippet demonstrates how to use min_element to find the smallest element of a collection of key-value pairs using the thrust::host execution policy for parallelization:

#include <thrust/extrema.h>
#include <thrust/execution_policy.h>
...

struct key_value
{
  int key;
  int value;
};

struct compare_key_value
{
  __host__ __device__
  bool operator()(key_value lhs, key_value rhs)
  {
    return lhs.key < rhs.key;
  }
};

...
key_value data[4] = { {4,5}, {0,7}, {2,3}, {6,1} };

key_value *smallest = thrust::min_element(thrust::host, data, data + 4, compare_key_value());

// smallest == data + 1
// *smallest == {0,7}

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the sequence.

  • last – The end of the sequence.

  • comp – A binary predicate used for comparison.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator, and ForwardIterator's value_type is convertible to both comp's first_argument_type and second_argument_type.

  • BinaryPredicate – is a model of Binary Predicate.

Returns:

An iterator pointing to the smallest element of the range [first, last), if it is not an empty range; last, otherwise.

template<typename ForwardIterator, typename BinaryPredicate>
ForwardIterator min_element(ForwardIterator first, ForwardIterator last, BinaryPredicate comp)#

min_element finds the smallest element in the range [first, last). It returns the first iterator i in [first, last) such that no other iterator in [first, last) points to a value smaller than *i. The return value is last if and only if [first, last) is an empty range.

The two versions of min_element differ in how they define whether one element is less than another. This version compares objects using a function object comp. Specifically, this version of min_element returns the first iterator i in [first, last) such that, for every iterator j in [first, last), comp(*j, *i) is false.

The following code snippet demonstrates how to use min_element to find the smallest element of a collection of key-value pairs.

#include <thrust/extrema.h>

struct key_value
{
  int key;
  int value;
};

struct compare_key_value
{
  __host__ __device__
  bool operator()(key_value lhs, key_value rhs)
  {
    return lhs.key < rhs.key;
  }
};

...
key_value data[4] = { {4,5}, {0,7}, {2,3}, {6,1} };

key_value *smallest = thrust::min_element(data, data + 4, compare_key_value());

// smallest == data + 1
// *smallest == {0,7}

Parameters:
  • first – The beginning of the sequence.

  • last – The end of the sequence.

  • comp – A binary predicate used for comparison.

Template Parameters:
  • ForwardIterator – is a model of Forward Iterator, and ForwardIterator's value_type is convertible to both comp's first_argument_type and second_argument_type.

  • BinaryPredicate – is a model of Binary Predicate.

Returns:

An iterator pointing to the smallest element of the range [first, last), if it is not an empty range; last, otherwise.

template<typename DerivedPolicy, typename ForwardIterator> __host__ __device__ ForwardIterator max_element (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last)

max_element finds the largest element in the range [first, last). It returns the first iterator i in [first, last) such that no other iterator in [first, last) points to a value larger than *i. The return value is last if and only if [first, last) is an empty range.

The two versions of max_element differ in how they define whether one element is greater than another. This version compares objects using operator<. Specifically, this version of max_element returns the first iterator i in [first, last) such that, for every iterator j in [first, last), *i < *j is false.

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

#include <thrust/extrema.h>
#include <thrust/execution_policy.h>
...
int data[6] = {1, 0, 2, 2, 1, 3};
int *result = thrust::max_element(thrust::host, data, data + 6);

// *result == 3

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the sequence.

  • last – The end of the sequence.

Template Parameters:
Returns:

An iterator pointing to the largest element of the range [first, last), if it is not an empty range; last, otherwise.

template<typename ForwardIterator>
ForwardIterator max_element(ForwardIterator first, ForwardIterator last)#

max_element finds the largest element in the range [first, last). It returns the first iterator i in [first, last) such that no other iterator in [first, last) points to a value larger than *i. The return value is last if and only if [first, last) is an empty range.

The two versions of max_element differ in how they define whether one element is greater than another. This version compares objects using operator<. Specifically, this version of max_element returns the first iterator i in [first, last) such that, for every iterator j in [first, last), *i < *j is false.

#include <thrust/extrema.h>
...
int data[6] = {1, 0, 2, 2, 1, 3};
int *result = thrust::max_element(data, data + 6);

// *result == 3

Parameters:
  • first – The beginning of the sequence.

  • last – The end of the sequence.

Template Parameters:

ForwardIterator – is a model of Forward Iterator, and ForwardIterator's value_type is a model of LessThan Comparable.

Returns:

An iterator pointing to the largest element of the range [first, last), if it is not an empty range; last, otherwise.

template<typename DerivedPolicy, typename ForwardIterator, typename BinaryPredicate> __host__ __device__ ForwardIterator max_element (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last, BinaryPredicate comp)

max_element finds the largest element in the range [first, last). It returns the first iterator i in [first, last) such that no other iterator in [first, last) points to a value larger than *i. The return value is last if and only if [first, last) is an empty range.

The two versions of max_element differ in how they define whether one element is less than another. This version compares objects using a function object comp. Specifically, this version of max_element returns the first iterator i in [first, last) such that, for every iterator j in [first, last), comp(*i, *j) is false.

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

The following code snippet demonstrates how to use max_element to find the largest element of a collection of key-value pairs using the thrust::host execution policy for parallelization.

#include <thrust/extrema.h>
#include <thrust/execution_policy.h>
...

struct key_value
{
  int key;
  int value;
};

struct compare_key_value
{
  __host__ __device__
  bool operator()(key_value lhs, key_value rhs)
  {
    return lhs.key < rhs.key;
  }
};

...
key_value data[4] = { {4,5}, {0,7}, {2,3}, {6,1} };

key_value *largest = thrust::max_element(thrust::host, data, data + 4, compare_key_value());

// largest == data + 3
// *largest == {6,1}

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the sequence.

  • last – The end of the sequence.

  • comp – A binary predicate used for comparison.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator, and ForwardIterator's value_type is convertible to both comp's first_argument_type and second_argument_type.

  • BinaryPredicate – is a model of Binary Predicate.

Returns:

An iterator pointing to the largest element of the range [first, last), if it is not an empty range; last, otherwise.

template<typename ForwardIterator, typename BinaryPredicate>
ForwardIterator max_element(ForwardIterator first, ForwardIterator last, BinaryPredicate comp)#

max_element finds the largest element in the range [first, last). It returns the first iterator i in [first, last) such that no other iterator in [first, last) points to a value larger than *i. The return value is last if and only if [first, last) is an empty range.

The two versions of max_element differ in how they define whether one element is less than another. This version compares objects using a function object comp. Specifically, this version of max_element returns the first iterator i in [first, last) such that, for every iterator j in [first, last), comp(*i, *j) is false.

The following code snippet demonstrates how to use max_element to find the largest element of a collection of key-value pairs.

#include <thrust/extrema.h>

struct key_value
{
  int key;
  int value;
};

struct compare_key_value
{
  __host__ __device__
  bool operator()(key_value lhs, key_value rhs)
  {
    return lhs.key < rhs.key;
  }
};

...
key_value data[4] = { {4,5}, {0,7}, {2,3}, {6,1} };

key_value *largest = thrust::max_element(data, data + 4, compare_key_value());

// largest == data + 3
// *largest == {6,1}

Parameters:
  • first – The beginning of the sequence.

  • last – The end of the sequence.

  • comp – A binary predicate used for comparison.

Template Parameters:
  • ForwardIterator – is a model of Forward Iterator, and ForwardIterator's value_type is convertible to both comp's first_argument_type and second_argument_type.

  • BinaryPredicate – is a model of Binary Predicate.

Returns:

An iterator pointing to the largest element of the range [first, last), if it is not an empty range; last, otherwise.

template<typename DerivedPolicy, typename ForwardIterator> __host__ __device__ thrust::pair< ForwardIterator, ForwardIterator > minmax_element (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last)

minmax_element finds the smallest and largest elements in the range [first, last). It returns a pair of iterators (imin, imax) where imin is the same iterator returned by min_element and imax is the same iterator returned by max_element. This function is potentially more efficient than separate calls to min_element and max_element.

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

#include <thrust/extrema.h>
#include <thrust/execution_policy.h>
...
int data[6] = {1, 0, 2, 2, 1, 3};
thrust::pair<int *, int *> result = thrust::minmax_element(thrust::host, data, data + 6);

// result.first is data + 1
// result.second is data + 5
// *result.first is 0
// *result.second is 3

See also

min_element

See also

max_element

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the sequence.

  • last – The end of the sequence.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator, and ForwardIterator's value_type is a model of LessThan Comparable.

Returns:

A pair of iterator pointing to the smallest and largest elements of the range [first, last), if it is not an empty range; last, otherwise.

template<typename ForwardIterator>
thrust::pair<ForwardIterator, ForwardIterator> minmax_element(ForwardIterator first, ForwardIterator last)#

minmax_element finds the smallest and largest elements in the range [first, last). It returns a pair of iterators (imin, imax) where imin is the same iterator returned by min_element and imax is the same iterator returned by max_element. This function is potentially more efficient than separate calls to min_element and max_element.

#include <thrust/extrema.h>
...
int data[6] = {1, 0, 2, 2, 1, 3};
thrust::pair<int *, int *> result = thrust::minmax_element(data, data + 6);

// result.first is data + 1
// result.second is data + 5
// *result.first is 0
// *result.second is 3

See also

min_element

See also

max_element

Parameters:
  • first – The beginning of the sequence.

  • last – The end of the sequence.

Template Parameters:

ForwardIterator – is a model of Forward Iterator, and ForwardIterator's value_type is a model of LessThan Comparable.

Returns:

A pair of iterator pointing to the smallest and largest elements of the range [first, last), if it is not an empty range; last, otherwise.

template<typename DerivedPolicy, typename ForwardIterator, typename BinaryPredicate> __host__ __device__ thrust::pair< ForwardIterator, ForwardIterator > minmax_element (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last, BinaryPredicate comp)

minmax_element finds the smallest and largest elements in the range [first, last). It returns a pair of iterators (imin, imax) where imin is the same iterator returned by min_element and imax is the same iterator returned by max_element. This function is potentially more efficient than separate calls to min_element and max_element.

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

The following code snippet demonstrates how to use minmax_element to find the smallest and largest elements of a collection of key-value pairs using the thrust::host execution policy for parallelization:

#include <thrust/extrema.h>
#include <thrust/pair.h>
#include <thrust/execution_policy.h>
...

struct key_value
{
  int key;
  int value;
};

struct compare_key_value
{
  __host__ __device__
  bool operator()(key_value lhs, key_value rhs)
  {
    return lhs.key < rhs.key;
  }
};

...
key_value data[4] = { {4,5}, {0,7}, {2,3}, {6,1} };

thrust::pair<key_value*,key_value*> extrema = thrust::minmax_element(thrust::host, data, data + 4, compare_key_value());

// extrema.first   == data + 1
// *extrema.first  == {0,7}
// extrema.second  == data + 3
// *extrema.second == {6,1}

See also

min_element

See also

max_element

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the sequence.

  • last – The end of the sequence.

  • comp – A binary predicate used for comparison.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator, and ForwardIterator's value_type is convertible to both comp's first_argument_type and second_argument_type.

  • BinaryPredicate – is a model of Binary Predicate.

Returns:

A pair of iterator pointing to the smallest and largest elements of the range [first, last), if it is not an empty range; last, otherwise.

template<typename ForwardIterator, typename BinaryPredicate>
thrust::pair<ForwardIterator, ForwardIterator> minmax_element(ForwardIterator first, ForwardIterator last, BinaryPredicate comp)#

minmax_element finds the smallest and largest elements in the range [first, last). It returns a pair of iterators (imin, imax) where imin is the same iterator returned by min_element and imax is the same iterator returned by max_element. This function is potentially more efficient than separate calls to min_element and max_element.

The following code snippet demonstrates how to use minmax_element to find the smallest and largest elements of a collection of key-value pairs.

#include <thrust/extrema.h>
#include <thrust/pair.h>

struct key_value
{
  int key;
  int value;
};

struct compare_key_value
{
  __host__ __device__
  bool operator()(key_value lhs, key_value rhs)
  {
    return lhs.key < rhs.key;
  }
};

...
key_value data[4] = { {4,5}, {0,7}, {2,3}, {6,1} };

thrust::pair<key_value*,key_value*> extrema = thrust::minmax_element(data, data + 4, compare_key_value());

// extrema.first   == data + 1
// *extrema.first  == {0,7}
// extrema.second  == data + 3
// *extrema.second == {6,1}

See also

min_element

See also

max_element

Parameters:
  • first – The beginning of the sequence.

  • last – The end of the sequence.

  • comp – A binary predicate used for comparison.

Template Parameters:
  • ForwardIterator – is a model of Forward Iterator, and ForwardIterator's value_type is convertible to both comp's first_argument_type and second_argument_type.

  • BinaryPredicate – is a model of Binary Predicate.

Returns:

A pair of iterator pointing to the smallest and largest elements of the range [first, last), if it is not an empty range; last, otherwise.

Functions

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename OutputType> __host__ __device__ OutputType inner_product (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputType init)

inner_product calculates an inner product of the ranges [first1, last1) and [first2, first2 + (last1 - first1)).

Specifically, this version of inner_product computes the sum init + (*first1 * *first2) + (*(first1+1) * *(first2+1)) + ...

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

The following code demonstrates how to use inner_product to compute the dot product of two vectors using the thrust::host execution policy for parallelization.

#include <thrust/inner_product.h>
#include <thrust/execution_policy.h>
...
float vec1[3] = {1.0f, 2.0f, 5.0f};
float vec2[3] = {4.0f, 1.0f, 5.0f};

float result = thrust::inner_product(thrust::host, vec1, vec1 + 3, vec2, 0.0f);

// result == 31.0f

Parameters:
  • exec – The execution policy to use for parallelization.

  • first1 – The beginning of the first sequence.

  • last1 – The end of the first sequence.

  • first2 – The beginning of the second sequence.

  • init – Initial value of the result.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator,

  • InputIterator2 – is a model of Input Iterator,

  • OutputType – is a model of Assignable, and if x is an object of type OutputType, and y is an object of InputIterator1's value_type, and z is an object of InputIterator2's value_type, then x + y * z is defined and is convertible to OutputType.

Returns:

The inner product of sequences [first1, last1) and [first2, last2) plus init.

template<typename InputIterator1, typename InputIterator2, typename OutputType>
OutputType inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputType init)#

inner_product calculates an inner product of the ranges [first1, last1) and [first2, first2 + (last1 - first1)).

Specifically, this version of inner_product computes the sum init + (*first1 * *first2) + (*(first1+1) * *(first2+1)) + ...

Unlike the C++ Standard Template Library function std::inner_product, this version offers no guarantee on order of execution.

The following code demonstrates how to use inner_product to compute the dot product of two vectors.

#include <thrust/inner_product.h>
...
float vec1[3] = {1.0f, 2.0f, 5.0f};
float vec2[3] = {4.0f, 1.0f, 5.0f};

float result = thrust::inner_product(vec1, vec1 + 3, vec2, 0.0f);

// result == 31.0f

Parameters:
  • first1 – The beginning of the first sequence.

  • last1 – The end of the first sequence.

  • first2 – The beginning of the second sequence.

  • init – Initial value of the result.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator,

  • InputIterator2 – is a model of Input Iterator,

  • OutputType – is a model of Assignable, and if x is an object of type OutputType, and y is an object of InputIterator1's value_type, and z is an object of InputIterator2's value_type, then x + y * z is defined and is convertible to OutputType.

Returns:

The inner product of sequences [first1, last1) and [first2, last2) plus init.

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename OutputType, typename BinaryFunction1, typename BinaryFunction2> __host__ __device__ OutputType inner_product (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputType init, BinaryFunction1 binary_op1, BinaryFunction2 binary_op2)

inner_product calculates an inner product of the ranges [first1, last1) and [first2, first2 + (last1 - first1)).

This version of inner_product is identical to the first, except that is uses two user-supplied function objects instead of operator+ and operator*.

Specifically, this version of inner_product computes the sum binary_op1( init, binary_op2(*first1, *first2) ), ...

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

#include <thrust/inner_product.h>
#include <thrust/execution_policy.h>
...
float vec1[3] = {1.0f, 2.0f, 5.0f};
float vec2[3] = {4.0f, 1.0f, 5.0f};

float init = 0.0f;
thrust::plus<float>       binary_op1;
thrust::multiplies<float> binary_op2;

float result = thrust::inner_product(thrust::host, vec1, vec1 + 3, vec2, init, binary_op1, binary_op2);

// result == 31.0f

Parameters:
  • exec – The execution policy to use for parallelization.

  • first1 – The beginning of the first sequence.

  • last1 – The end of the first sequence.

  • first2 – The beginning of the second sequence.

  • init – Initial value of the result.

  • binary_op1 – Generalized addition operation.

  • binary_op2 – Generalized multiplication operation.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator, and InputIterator1's value_type is convertible to BinaryFunction2's first_argument_type.

  • InputIterator2 – is a model of Input Iterator. and InputIterator2's value_type is convertible to BinaryFunction2's second_argument_type.

  • OutputType – is a model of Assignable, and OutputType is convertible to BinaryFunction1's first_argument_type.

  • BinaryFunction1 – is a model of Binary Function, and BinaryFunction1's return_type is convertible to OutputType.

  • BinaryFunction2 – is a model of Binary Function, and BinaryFunction2's return_type is convertible to BinaryFunction1's second_argument_type.

Returns:

The inner product of sequences [first1, last1) and [first2, last2).

template<typename InputIterator1, typename InputIterator2, typename OutputType, typename BinaryFunction1, typename BinaryFunction2>
OutputType inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputType init, BinaryFunction1 binary_op1, BinaryFunction2 binary_op2)#

inner_product calculates an inner product of the ranges [first1, last1) and [first2, first2 + (last1 - first1)).

This version of inner_product is identical to the first, except that is uses two user-supplied function objects instead of operator+ and operator*.

Specifically, this version of inner_product computes the sum binary_op1( init, binary_op2(*first1, *first2) ), ...

Unlike the C++ Standard Template Library function std::inner_product, this version offers no guarantee on order of execution.

#include <thrust/inner_product.h>
...
float vec1[3] = {1.0f, 2.0f, 5.0f};
float vec2[3] = {4.0f, 1.0f, 5.0f};

float init = 0.0f;
thrust::plus<float>       binary_op1;
thrust::multiplies<float> binary_op2;

float result = thrust::inner_product(vec1, vec1 + 3, vec2, init, binary_op1, binary_op2);

// result == 31.0f

Parameters:
  • first1 – The beginning of the first sequence.

  • last1 – The end of the first sequence.

  • first2 – The beginning of the second sequence.

  • init – Initial value of the result.

  • binary_op1 – Generalized addition operation.

  • binary_op2 – Generalized multiplication operation.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator, and InputIterator1's value_type is convertible to BinaryFunction2's first_argument_type.

  • InputIterator2 – is a model of Input Iterator. and InputIterator2's value_type is convertible to BinaryFunction2's second_argument_type.

  • OutputType – is a model of Assignable, and OutputType is convertible to BinaryFunction1's first_argument_type.

  • BinaryFunction1 – is a model of Binary Function, and BinaryFunction1's return_type is convertible to OutputType.

  • BinaryFunction2 – is a model of Binary Function, and BinaryFunction2's return_type is convertible to BinaryFunction1's second_argument_type.

Returns:

The inner product of sequences [first1, last1) and [first2, last2).

template<typename DerivedPolicy, typename InputIterator, typename UnaryFunction, typename OutputType, typename BinaryFunction> __host__ __device__ OutputType transform_reduce (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator first, InputIterator last, UnaryFunction unary_op, OutputType init, BinaryFunction binary_op)

transform_reduce fuses the transform and reduce operations. transform_reduce is equivalent to performing a transformation defined by unary_op into a temporary sequence and then performing reduce on the transformed sequence. In most cases, fusing these two operations together is more efficient, since fewer memory reads and writes are required.

transform_reduce performs a reduction on the transformation of the sequence [first, last) according to unary_op. Specifically, unary_op is applied to each element of the sequence and then the result is reduced to a single value with binary_op using the initial value init. Note that the transformation unary_op is not applied to the initial value init. The order of reduction is not specified, so binary_op must be both commutative and associative.

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

The following code snippet demonstrates how to use transform_reduce to compute the maximum value of the absolute value of the elements of a range using the thrust::host execution policy for parallelization:

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

template<typename T>
struct absolute_value : public unary_function<T,T>
{
  __host__ __device__ T operator()(const T &x) const
  {
    return x < T(0) ? -x : x;
  }
};

...

int data[6] = {-1, 0, -2, -2, 1, -3};
int result = thrust::transform_reduce(thrust::host,
                                      data, data + 6,
                                      absolute_value<int>(),
                                      0,
                                      thrust::maximum<int>());
// result == 3

See also

transform

See also

reduce

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the sequence.

  • last – The end of the sequence.

  • unary_op – The function to apply to each element of the input sequence.

  • init – The result is initialized to this value.

  • binary_op – The reduction operation.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator – is a model of Input Iterator, and InputIterator's value_type is convertible to UnaryFunction's argument_type.

  • UnaryFunction – is a model of Unary Function, and UnaryFunction's result_type is convertible to OutputType.

  • OutputType – is a model of Assignable, and is convertible to BinaryFunction's first_argument_type and second_argument_type.

  • BinaryFunction – is a model of Binary Function, and BinaryFunction's result_type is convertible to OutputType.

Returns:

The result of the transformed reduction.

template<typename InputIterator, typename UnaryFunction, typename OutputType, typename BinaryFunction>
OutputType transform_reduce(InputIterator first, InputIterator last, UnaryFunction unary_op, OutputType init, BinaryFunction binary_op)#

transform_reduce fuses the transform and reduce operations. transform_reduce is equivalent to performing a transformation defined by unary_op into a temporary sequence and then performing reduce on the transformed sequence. In most cases, fusing these two operations together is more efficient, since fewer memory reads and writes are required.

transform_reduce performs a reduction on the transformation of the sequence [first, last) according to unary_op. Specifically, unary_op is applied to each element of the sequence and then the result is reduced to a single value with binary_op using the initial value init. Note that the transformation unary_op is not applied to the initial value init. The order of reduction is not specified, so binary_op must be both commutative and associative.

The following code snippet demonstrates how to use transform_reduce to compute the maximum value of the absolute value of the elements of a range.

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

template<typename T>
struct absolute_value : public unary_function<T,T>
{
  __host__ __device__ T operator()(const T &x) const
  {
    return x < T(0) ? -x : x;
  }
};

...

int data[6] = {-1, 0, -2, -2, 1, -3};
int result = thrust::transform_reduce(data, data + 6,
                                      absolute_value<int>(),
                                      0,
                                      thrust::maximum<int>());
// result == 3

See also

transform

See also

reduce

Parameters:
  • first – The beginning of the sequence.

  • last – The end of the sequence.

  • unary_op – The function to apply to each element of the input sequence.

  • init – The result is initialized to this value.

  • binary_op – The reduction operation.

Template Parameters:
  • InputIterator – is a model of Input Iterator, and InputIterator's value_type is convertible to UnaryFunction's argument_type.

  • UnaryFunction – is a model of Unary Function, and UnaryFunction's result_type is convertible to OutputType.

  • OutputType – is a model of Assignable, and is convertible to BinaryFunction's first_argument_type and second_argument_type.

  • BinaryFunction – is a model of Binary Function, and BinaryFunction's result_type is convertible to OutputType.

Returns:

The result of the transformed reduction.

Functions

template<typename DerivedPolicy, typename InputIterator, typename Predicate> __host__ __device__ bool all_of (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator first, InputIterator last, Predicate pred)

all_of determines whether all elements in a range satify a predicate. Specifically, all_of returns true if pred(*i) is true for every iterator i in the range [first, last) and false otherwise.

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

#include <thrust/logical.h>
#include <thrust/functional.h>
#include <thrust/execution_policy.h>
...
bool A[3] = {true, true, false};

thrust::all_of(thrust::host, A, A + 2, thrust::identity<bool>()); // returns true
thrust::all_of(thrust::host, A, A + 3, thrust::identity<bool>()); // returns false

// empty range
thrust::all_of(thrust::host, A, A, thrust::identity<bool>()); // returns false

See also

any_of

See also

none_of

See also

transform_reduce

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the sequence.

  • last – The end of the sequence.

  • pred – A predicate used to test range elements.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator – is a model of Input Iterator,

  • Predicate – must be a model of Predicate.

Returns:

true, if all elements satisfy the predicate; false, otherwise.

template<typename InputIterator, typename Predicate>
bool all_of(InputIterator first, InputIterator last, Predicate pred)#

all_of determines whether all elements in a range satify a predicate. Specifically, all_of returns true if pred(*i) is true for every iterator i in the range [first, last) and false otherwise.

#include <thrust/logical.h>
#include <thrust/functional.h>
...
bool A[3] = {true, true, false};

thrust::all_of(A, A + 2, thrust::identity<bool>()); // returns true
thrust::all_of(A, A + 3, thrust::identity<bool>()); // returns false

// empty range
thrust::all_of(A, A, thrust::identity<bool>()); // returns false

See also

any_of

See also

none_of

See also

transform_reduce

Parameters:
  • first – The beginning of the sequence.

  • last – The end of the sequence.

  • pred – A predicate used to test range elements.

Template Parameters:
Returns:

true, if all elements satisfy the predicate; false, otherwise.

template<typename DerivedPolicy, typename InputIterator, typename Predicate> __host__ __device__ bool any_of (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator first, InputIterator last, Predicate pred)

any_of determines whether any element in a range satifies a predicate. Specifically, any_of returns true if pred(*i) is true for any iterator i in the range [first, last) and false otherwise.

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

#include <thrust/logical.h>
#include <thrust/functional.h>
#include <thrust/execution_policy.h>
...
bool A[3] = {true, true, false};

thrust::any_of(thrust::host, A, A + 2, thrust::identity<bool>()); // returns true
thrust::any_of(thrust::host, A, A + 3, thrust::identity<bool>()); // returns true

thrust::any_of(thrust::host, A + 2, A + 3, thrust::identity<bool>()); // returns false

// empty range
thrust::any_of(thrust::host, A, A, thrust::identity<bool>()); // returns false

See also

all_of

See also

none_of

See also

transform_reduce

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the sequence.

  • last – The end of the sequence.

  • pred – A predicate used to test range elements.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator – is a model of Input Iterator,

  • Predicate – must be a model of Predicate.

Returns:

true, if any element satisfies the predicate; false, otherwise.

template<typename InputIterator, typename Predicate>
bool any_of(InputIterator first, InputIterator last, Predicate pred)#

any_of determines whether any element in a range satifies a predicate. Specifically, any_of returns true if pred(*i) is true for any iterator i in the range [first, last) and false otherwise.

#include <thrust/logical.h>
#include <thrust/functional.h>
...
bool A[3] = {true, true, false};

thrust::any_of(A, A + 2, thrust::identity<bool>()); // returns true
thrust::any_of(A, A + 3, thrust::identity<bool>()); // returns true

thrust::any_of(A + 2, A + 3, thrust::identity<bool>()); // returns false

// empty range
thrust::any_of(A, A, thrust::identity<bool>()); // returns false

See also

all_of

See also

none_of

See also

transform_reduce

Parameters:
  • first – The beginning of the sequence.

  • last – The end of the sequence.

  • pred – A predicate used to test range elements.

Template Parameters:
Returns:

true, if any element satisfies the predicate; false, otherwise.

template<typename DerivedPolicy, typename InputIterator, typename Predicate> __host__ __device__ bool none_of (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator first, InputIterator last, Predicate pred)

none_of determines whether no element in a range satifies a predicate. Specifically, none_of returns true if there is no iterator i in the range [first, last) such that pred(*i) is true, and false otherwise.

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

#include <thrust/logical.h>
#include <thrust/functional.h>
#include <thrust/execution_policy.h>
...
bool A[3] = {true, true, false};

thrust::none_of(thrust::host, A, A + 2, thrust::identity<bool>()); // returns false
thrust::none_of(thrust::host, A, A + 3, thrust::identity<bool>()); // returns false

thrust::none_of(thrust::host, A + 2, A + 3, thrust::identity<bool>()); // returns true

// empty range
thrust::none_of(thrust::host, A, A, thrust::identity<bool>()); // returns true

See also

all_of

See also

any_of

See also

transform_reduce

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the sequence.

  • last – The end of the sequence.

  • pred – A predicate used to test range elements.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator – is a model of Input Iterator,

  • Predicate – must be a model of Predicate.

Returns:

true, if no element satisfies the predicate; false, otherwise.

template<typename InputIterator, typename Predicate>
bool none_of(InputIterator first, InputIterator last, Predicate pred)#

none_of determines whether no element in a range satifies a predicate. Specifically, none_of returns true if there is no iterator i in the range [first, last) such that pred(*i) is true, and false otherwise.

#include <thrust/logical.h>
#include <thrust/functional.h>
...
bool A[3] = {true, true, false};

thrust::none_of(A, A + 2, thrust::identity<bool>()); // returns false
thrust::none_of(A, A + 3, thrust::identity<bool>()); // returns false

thrust::none_of(A + 2, A + 3, thrust::identity<bool>()); // returns true

// empty range
thrust::none_of(A, A, thrust::identity<bool>()); // returns true

See also

all_of

See also

any_of

See also

transform_reduce

Parameters:
  • first – The beginning of the sequence.

  • last – The end of the sequence.

  • pred – A predicate used to test range elements.

Template Parameters:
Returns:

true, if no element satisfies the predicate; false, otherwise.

Functions

template<typename DerivedPolicy, typename InputIterator, typename Predicate> __host__ __device__ bool is_partitioned (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator first, InputIterator last, Predicate pred)

is_partitioned returns true if the given range is partitioned with respect to a predicate, and false otherwise.

Specifically, is_partitioned returns true if [first, last) is empty of if [first, last) is partitioned by pred, i.e. if all elements that satisfy pred appear before those that do not.

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

#include <thrust/partition.h>
#include <thrust/execution_policy.h>

struct is_even
{
  __host__ __device__
  bool operator()(const int &x)
  {
    return (x % 2) == 0;
  }
};

...

int A[] = {2, 4, 6, 8, 10, 1, 3, 5, 7, 9};
int B[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

thrust::is_partitioned(thrust::host, A, A + 10, is_even()); // returns true
thrust::is_partitioned(thrust::host, B, B + 10, is_even()); // returns false

See also

partition

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the range to consider.

  • last – The end of the range to consider.

  • pred – A function object which decides to which partition each element of the range [first, last) belongs.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator – is a model of Input Iterator, and InputIterator's value_type is convertible to Predicate's argument_type.

  • Predicate – is a model of Predicate.

Returns:

true if the range [first, last) is partitioned with respect to pred, or if [first, last) is empty. false, otherwise.

template<typename InputIterator, typename Predicate>
bool is_partitioned(InputIterator first, InputIterator last, Predicate pred)#

is_partitioned returns true if the given range is partitioned with respect to a predicate, and false otherwise.

Specifically, is_partitioned returns true if [first, last) is empty of if [first, last) is partitioned by pred, i.e. if all elements that satisfy pred appear before those that do not.

#include <thrust/partition.h>

struct is_even
{
  __host__ __device__
  bool operator()(const int &x)
  {
    return (x % 2) == 0;
  }
};

...

int A[] = {2, 4, 6, 8, 10, 1, 3, 5, 7, 9};
int B[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

thrust::is_partitioned(A, A + 10, is_even()); // returns true
thrust::is_partitioned(B, B + 10, is_even()); // returns false

See also

partition

Parameters:
  • first – The beginning of the range to consider.

  • last – The end of the range to consider.

  • pred – A function object which decides to which partition each element of the range [first, last) belongs.

Template Parameters:
  • InputIterator – is a model of Input Iterator, and InputIterator's value_type is convertible to Predicate's argument_type.

  • Predicate – is a model of Predicate.

Returns:

true if the range [first, last) is partitioned with respect to pred, or if [first, last) is empty. false, otherwise.

template<typename DerivedPolicy, typename ForwardIterator> __host__ __device__ bool is_sorted (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last)

is_sorted returns true if the range [first, last) is sorted in ascending order, and false otherwise.

Specifically, this version of is_sorted returns false if for some iterator i in the range [first, last - 1) the expression *(i + 1) < *i is true.

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

The following code demonstrates how to use is_sorted to test whether the contents of a device_vector are stored in ascending order using the thrust::device execution policy for parallelization:

#include <thrust/sort.h>
#include <thrust/device_vector.h>
#include <thrust/sort.h>
#include <thrust/execution_policy.h>
...
thrust::device_vector<int> v(6);
v[0] = 1;
v[1] = 4;
v[2] = 2;
v[3] = 8;
v[4] = 5;
v[5] = 7;

bool result = thrust::is_sorted(thrust::device, v.begin(), v.end());

// result == false

thrust::sort(v.begin(), v.end());
result = thrust::is_sorted(thrust::device, v.begin(), v.end());

// result == true

See also

is_sorted_until

See also

sort

See also

stable_sort

See also

less<T>

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the sequence.

  • last – The end of the sequence.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator, ForwardIterator's value_type is a model of LessThan Comparable, and the ordering on objects of ForwardIterator's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements.

Returns:

true, if the sequence is sorted; false, otherwise.

template<typename ForwardIterator>
bool is_sorted(ForwardIterator first, ForwardIterator last)#

is_sorted returns true if the range [first, last) is sorted in ascending order, and false otherwise.

Specifically, this version of is_sorted returns false if for some iterator i in the range [first, last - 1) the expression *(i + 1) < *i is true.

The following code demonstrates how to use is_sorted to test whether the contents of a device_vector are stored in ascending order.

#include <thrust/sort.h>
#include <thrust/device_vector.h>
#include <thrust/sort.h>
...
thrust::device_vector<int> v(6);
v[0] = 1;
v[1] = 4;
v[2] = 2;
v[3] = 8;
v[4] = 5;
v[5] = 7;

bool result = thrust::is_sorted(v.begin(), v.end());

// result == false

thrust::sort(v.begin(), v.end());
result = thrust::is_sorted(v.begin(), v.end());

// result == true

See also

is_sorted_until

See also

sort

See also

stable_sort

See also

less<T>

Parameters:
  • first – The beginning of the sequence.

  • last – The end of the sequence.

Template Parameters:

ForwardIterator – is a model of Forward Iterator, ForwardIterator's value_type is a model of LessThan Comparable, and the ordering on objects of ForwardIterator's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements.

Returns:

true, if the sequence is sorted; false, otherwise.

template<typename DerivedPolicy, typename ForwardIterator, typename Compare> __host__ __device__ bool is_sorted (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last, Compare comp)

is_sorted returns true if the range [first, last) is sorted in ascending order accoring to a user-defined comparison operation, and false otherwise.

Specifically, this version of is_sorted returns false if for some iterator i in the range [first, last - 1) the expression comp(*(i + 1), *i) is true.

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

The following code snippet demonstrates how to use is_sorted to test whether the contents of a device_vector are stored in descending order using the thrust::device execution policy for parallelization:

#include <thrust/sort.h>
#include <thrust/functional.h>
#include <thrust/device_vector.h>
#include <thrust/execution_policy.h>
...
thrust::device_vector<int> v(6);
v[0] = 1;
v[1] = 4;
v[2] = 2;
v[3] = 8;
v[4] = 5;
v[5] = 7;

thrust::greater<int> comp;
bool result = thrust::is_sorted(thrust::device, v.begin(), v.end(), comp);

// result == false

thrust::sort(v.begin(), v.end(), comp);
result = thrust::is_sorted(thrust::device, v.begin(), v.end(), comp);

// result == true

See also

sort

See also

stable_sort

See also

less<T>

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the sequence.

  • last – The end of the sequence.

  • comp – Comparison operator.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator, and ForwardIterator's value_type is convertible to both StrictWeakOrdering's first_argument_type and second_argument_type.

  • Compare – is a model of Strict Weak Ordering.

Returns:

true, if the sequence is sorted according to comp; false, otherwise.

template<typename ForwardIterator, typename Compare>
bool is_sorted(ForwardIterator first, ForwardIterator last, Compare comp)#

is_sorted returns true if the range [first, last) is sorted in ascending order accoring to a user-defined comparison operation, and false otherwise.

Specifically, this version of is_sorted returns false if for some iterator i in the range [first, last - 1) the expression comp(*(i + 1), *i) is true.

The following code snippet demonstrates how to use is_sorted to test whether the contents of a device_vector are stored in descending order.

#include <thrust/sort.h>
#include <thrust/functional.h>
#include <thrust/device_vector.h>
...
thrust::device_vector<int> v(6);
v[0] = 1;
v[1] = 4;
v[2] = 2;
v[3] = 8;
v[4] = 5;
v[5] = 7;

thrust::greater<int> comp;
bool result = thrust::is_sorted(v.begin(), v.end(), comp);

// result == false

thrust::sort(v.begin(), v.end(), comp);
result = thrust::is_sorted(v.begin(), v.end(), comp);

// result == true

See also

sort

See also

stable_sort

See also

less<T>

Parameters:
  • first – The beginning of the sequence.

  • last – The end of the sequence.

  • comp – Comparison operator.

Template Parameters:
  • ForwardIterator – is a model of Forward Iterator, and ForwardIterator's value_type is convertible to both StrictWeakOrdering's first_argument_type and second_argument_type.

  • Compare – is a model of Strict Weak Ordering.

Returns:

true, if the sequence is sorted according to comp; false, otherwise.

template<typename DerivedPolicy, typename ForwardIterator> __host__ __device__ ForwardIterator is_sorted_until (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last)

This version of is_sorted_until returns the last iterator i in [first,last] for which the range [first,last) is sorted using operator<. If distance(first,last) < 2, is_sorted_until simply returns last.

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

The following code snippet demonstrates how to use is_sorted_until to find the first position in an array where the data becomes unsorted using the thrust::host execution policy for parallelization:

#include <thrust/sort.h>
#include <thrust/execution_policy.h>

...
 
int A[8] = {0, 1, 2, 3, 0, 1, 2, 3};

int * B = thrust::is_sorted_until(thrust::host, A, A + 8);

// B - A is 4
// [A, B) is sorted

See also

is_sorted

See also

sort

See also

sort_by_key

See also

stable_sort

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the range of interest.

  • last – The end of the range of interest.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator and ForwardIterator's value_type is a model of LessThan Comparable.

Returns:

The last iterator in the input range for which it is sorted.

template<typename ForwardIterator>
ForwardIterator is_sorted_until(ForwardIterator first, ForwardIterator last)#

This version of is_sorted_until returns the last iterator i in [first,last] for which the range [first,last) is sorted using operator<. If distance(first,last) < 2, is_sorted_until simply returns last.

The following code snippet demonstrates how to use is_sorted_until to find the first position in an array where the data becomes unsorted:

#include <thrust/sort.h>

...
 
int A[8] = {0, 1, 2, 3, 0, 1, 2, 3};

int * B = thrust::is_sorted_until(A, A + 8);

// B - A is 4
// [A, B) is sorted

See also

is_sorted

See also

sort

See also

sort_by_key

See also

stable_sort

Parameters:
  • first – The beginning of the range of interest.

  • last – The end of the range of interest.

Template Parameters:

ForwardIterator – is a model of Forward Iterator and ForwardIterator's value_type is a model of LessThan Comparable.

Returns:

The last iterator in the input range for which it is sorted.

template<typename DerivedPolicy, typename ForwardIterator, typename Compare> __host__ __device__ ForwardIterator is_sorted_until (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last, Compare comp)

This version of is_sorted_until returns the last iterator i in [first,last] for which the range [first,last) is sorted using the function object comp. If distance(first,last) < 2, is_sorted_until simply returns last.

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

The following code snippet demonstrates how to use is_sorted_until to find the first position in an array where the data becomes unsorted in descending order using the thrust::host execution policy for parallelization:

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

...
 
int A[8] = {3, 2, 1, 0, 3, 2, 1, 0};

thrust::greater<int> comp;
int * B = thrust::is_sorted_until(thrust::host, A, A + 8, comp);

// B - A is 4
// [A, B) is sorted in descending order

See also

is_sorted

See also

sort

See also

sort_by_key

See also

stable_sort

Parameters:
  • exec – The execution policy to use for parallelization:

  • first – The beginning of the range of interest.

  • last – The end of the range of interest.

  • comp – The function object to use for comparison.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator and ForwardIterator's value_type is convertible to Compare's argument_type.

  • Compare – is a model of Strict Weak Ordering.

Returns:

The last iterator in the input range for which it is sorted.

template<typename ForwardIterator, typename Compare>
ForwardIterator is_sorted_until(ForwardIterator first, ForwardIterator last, Compare comp)#

This version of is_sorted_until returns the last iterator i in [first,last] for which the range [first,last) is sorted using the function object comp. If distance(first,last) < 2, is_sorted_until simply returns last.

The following code snippet demonstrates how to use is_sorted_until to find the first position in an array where the data becomes unsorted in descending order:

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

...
 
int A[8] = {3, 2, 1, 0, 3, 2, 1, 0};

thrust::greater<int> comp;
int * B = thrust::is_sorted_until(A, A + 8, comp);

// B - A is 4
// [A, B) is sorted in descending order

See also

is_sorted

See also

sort

See also

sort_by_key

See also

stable_sort

Parameters:
  • first – The beginning of the range of interest.

  • last – The end of the range of interest.

  • comp – The function object to use for comparison.

Template Parameters:
Returns:

The last iterator in the input range for which it is sorted.

Functions

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename OutputIterator> __host__ __device__ OutputIterator merge (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result)

merge combines two sorted ranges [first1, last1) and [first2, last2) into a single sorted range. That is, it copies from [first1, last1) and [first2, last2) into [result, result + (last1 - first1) + (last2 - first2)) such that the resulting range is in ascending order. merge is stable, meaning both that the relative order of elements within each input range is preserved, and that for equivalent elements in both input ranges the element from the first range precedes the element from the second. The return value is result + (last1 - first1) + (last2 - first2).

This version of merge compares elements using operator<.

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

The following code snippet demonstrates how to use merge to compute the merger of two sorted sets of integers using the thrust::host execution policy for parallelization:

#include <thrust/merge.h>
#include <thrust/execution_policy.h>
...
int A1[6] = {1, 3, 5, 7, 9, 11};
int A2[7] = {1, 1, 2, 3, 5,  8, 13};

int result[13];

int *result_end =
  thrust::merge(thrust::host,
                A1, A1 + 6,
                A2, A2 + 7,
                result);
// result = {1, 1, 1, 2, 3, 3, 5, 5, 7, 8, 9, 11, 13}

See also

set_union

See also

sort

See also

is_sorted

Parameters:
  • exec – The execution policy to use for parallelization.

  • first1 – The beginning of the first input range.

  • last1 – The end of the first input range.

  • first2 – The beginning of the second input range.

  • last2 – The end of the second input range.

  • result – The beginning of the merged output.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator, InputIterator1 and InputIterator2 have the same value_type, InputIterator1's value_type is a model of LessThan Comparable, the ordering on InputIterator1's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator1's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2 and InputIterator1 have the same value_type, InputIterator2's value_type is a model of LessThan Comparable, the ordering on InputIterator2's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator2's value_type is convertable to a type in OutputIterator's set of value_types.

  • OutputIterator – is a model of Output Iterator.

Returns:

The end of the output range.

Pre:

The ranges [first1, last1) and [first2, last2) shall be sorted with respect to operator<.

Pre:

The resulting range shall not overlap with either input range.

template<typename InputIterator1, typename InputIterator2, typename OutputIterator>
OutputIterator merge(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result)#

merge combines two sorted ranges [first1, last1) and [first2, last2) into a single sorted range. That is, it copies from [first1, last1) and [first2, last2) into [result, result + (last1 - first1) + (last2 - first2)) such that the resulting range is in ascending order. merge is stable, meaning both that the relative order of elements within each input range is preserved, and that for equivalent elements in both input ranges the element from the first range precedes the element from the second. The return value is result + (last1 - first1) + (last2 - first2).

This version of merge compares elements using operator<.

The following code snippet demonstrates how to use merge to compute the merger of two sorted sets of integers.

#include <thrust/merge.h>
...
int A1[6] = {1, 3, 5, 7, 9, 11};
int A2[7] = {1, 1, 2, 3, 5,  8, 13};

int result[13];

int *result_end = thrust::merge(A1, A1 + 6, A2, A2 + 7, result);
// result = {1, 1, 1, 2, 3, 3, 5, 5, 7, 8, 9, 11, 13}

See also

set_union

See also

sort

See also

is_sorted

Parameters:
  • first1 – The beginning of the first input range.

  • last1 – The end of the first input range.

  • first2 – The beginning of the second input range.

  • last2 – The end of the second input range.

  • result – The beginning of the merged output.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator, InputIterator1 and InputIterator2 have the same value_type, InputIterator1's value_type is a model of LessThan Comparable, the ordering on InputIterator1's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator1's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2 and InputIterator1 have the same value_type, InputIterator2's value_type is a model of LessThan Comparable, the ordering on InputIterator2's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator2's value_type is convertable to a type in OutputIterator's set of value_types.

  • OutputIterator – is a model of Output Iterator.

Returns:

The end of the output range.

Pre:

The ranges [first1, last1) and [first2, last2) shall be sorted with respect to operator<.

Pre:

The resulting range shall not overlap with either input range.

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename OutputIterator, typename StrictWeakCompare> __host__ __device__ OutputIterator merge (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, StrictWeakCompare comp)

merge combines two sorted ranges [first1, last1) and [first2, last2) into a single sorted range. That is, it copies from [first1, last1) and [first2, last2) into [result, result + (last1 - first1) + (last2 - first2)) such that the resulting range is in ascending order. merge is stable, meaning both that the relative order of elements within each input range is preserved, and that for equivalent elements in both input ranges the element from the first range precedes the element from the second. The return value is result + (last1 - first1) + (last2 - first2).

This version of merge compares elements using a function object comp.

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

The following code snippet demonstrates how to use merge to compute the merger of two sets of integers sorted in descending order using the thrust::host execution policy for parallelization:

#include <thrust/merge.h>
#include <thrust/functional.h>
#include <thrust/execution_policy.h>
...
int A1[6] = {11, 9, 7, 5, 3, 1};
int A2[7] = {13, 8, 5, 3, 2, 1, 1};

int result[13];

int *result_end = thrust::merge(thrust::host,
                                A1, A1 + 6,
                                A2, A2 + 7,
                                result,
                                thrust::greater<int>());
// result = {13, 11, 9, 8, 7, 5, 5, 3, 3, 2, 1, 1, 1}

See also

sort

See also

is_sorted

Parameters:
  • exec – The execution policy to use for parallelization.

  • first1 – The beginning of the first input range.

  • last1 – The end of the first input range.

  • first2 – The beginning of the second input range.

  • last2 – The end of the second input range.

  • result – The beginning of the merged output.

  • comp – Comparison operator.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator, InputIterator1's value_type is convertable to StrictWeakCompare's first_argument_type. and InputIterator1's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2's value_type is convertable to StrictWeakCompare's second_argument_type. and InputIterator2's value_type is convertable to a type in OutputIterator's set of value_types.

  • OutputIterator – is a model of Output Iterator.

  • StrictWeakCompare – is a model of Strict Weak Ordering.

Returns:

The end of the output range.

Pre:

The ranges [first1, last1) and [first2, last2) shall be sorted with respect to comp.

Pre:

The resulting range shall not overlap with either input range.

template<typename InputIterator1, typename InputIterator2, typename OutputIterator, typename StrictWeakCompare>
OutputIterator merge(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, StrictWeakCompare comp)#

merge combines two sorted ranges [first1, last1) and [first2, last2) into a single sorted range. That is, it copies from [first1, last1) and [first2, last2) into [result, result + (last1 - first1) + (last2 - first2)) such that the resulting range is in ascending order. merge is stable, meaning both that the relative order of elements within each input range is preserved, and that for equivalent elements in both input ranges the element from the first range precedes the element from the second. The return value is result + (last1 - first1) + (last2 - first2).

This version of merge compares elements using a function object comp.

The following code snippet demonstrates how to use merge to compute the merger of two sets of integers sorted in descending order.

#include <thrust/merge.h>
#include <thrust/functional.h>
...
int A1[6] = {11, 9, 7, 5, 3, 1};
int A2[7] = {13, 8, 5, 3, 2, 1, 1};

int result[13];

int *result_end = thrust::merge(A1, A1 + 6, A2, A2 + 7, result, thrust::greater<int>());
// result = {13, 11, 9, 8, 7, 5, 5, 3, 3, 2, 1, 1, 1}

See also

sort

See also

is_sorted

Parameters:
  • first1 – The beginning of the first input range.

  • last1 – The end of the first input range.

  • first2 – The beginning of the second input range.

  • last2 – The end of the second input range.

  • result – The beginning of the merged output.

  • comp – Comparison operator.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator, InputIterator1's value_type is convertable to StrictWeakCompare's first_argument_type. and InputIterator1's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2's value_type is convertable to StrictWeakCompare's second_argument_type. and InputIterator2's value_type is convertable to a type in OutputIterator's set of value_types.

  • OutputIterator – is a model of Output Iterator.

  • StrictWeakCompare – is a model of Strict Weak Ordering.

Returns:

The end of the output range.

Pre:

The ranges [first1, last1) and [first2, last2) shall be sorted with respect to comp.

Pre:

The resulting range shall not overlap with either input range.

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename InputIterator3, typename InputIterator4, typename OutputIterator1, typename OutputIterator2> __host__ __device__ thrust::pair< OutputIterator1, OutputIterator2 > merge_by_key (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 keys_first1, InputIterator1 keys_last1, InputIterator2 keys_first2, InputIterator2 keys_last2, InputIterator3 values_first1, InputIterator4 values_first2, OutputIterator1 keys_result, OutputIterator2 values_result)

merge_by_key performs a key-value merge. That is, merge_by_key copies elements from [keys_first1, keys_last1) and [keys_first2, keys_last2) into a single range, [keys_result, keys_result + (keys_last1 - keys_first1) + (keys_last2 - keys_first2)) such that the resulting range is in ascending key order.

At the same time, merge_by_key copies elements from the two associated ranges [values_first1 + (keys_last1 - keys_first1)) and [values_first2 + (keys_last2 - keys_first2)) into a single range, [values_result, values_result + (keys_last1 - keys_first1) + (keys_last2 - keys_first2)) such that the resulting range is in ascending order implied by each input element’s associated key.

merge_by_key is stable, meaning both that the relative order of elements within each input range is preserved, and that for equivalent elements in all input key ranges the element from the first range precedes the element from the second.

The return value is is (keys_result + (keys_last1 - keys_first1) + (keys_last2 - keys_first2)) and (values_result + (keys_last1 - keys_first1) + (keys_last2 - keys_first2)).

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

The following code snippet demonstrates how to use merge_by_key to compute the merger of two sets of integers sorted in ascending order using the thrust::host execution policy for parallelization:

#include <thrust/merge.h>
#include <thrust/functional.h>
#include <thrust/execution_policy.h>
...
int A_keys[6] = {1, 3, 5, 7, 9, 11};
int A_vals[6] = {0, 0, 0, 0, 0, 0};

int B_keys[7] = {1, 1, 2, 3, 5, 8, 13};
int B_vals[7] = {1, 1, 1, 1, 1, 1, 1};

int keys_result[13];
int vals_result[13];

thrust::pair<int*,int*> end =
  thrust::merge_by_key(thrust::host,
                       A_keys, A_keys + 6,
                       B_keys, B_keys + 7,
                       A_vals, B_vals,
                       keys_result, vals_result);

// keys_result = {1, 1, 1, 2, 3, 3, 5, 5, 7, 8, 9, 11, 13}
// vals_result = {0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0,  0,  1}

See also

merge

See also

sort_by_key

See also

is_sorted

Parameters:
  • exec – The execution policy to use for parallelization.

  • keys_first1 – The beginning of the first input range of keys.

  • keys_last1 – The end of the first input range of keys.

  • keys_first2 – The beginning of the second input range of keys.

  • keys_last2 – The end of the second input range of keys.

  • values_first1 – The beginning of the first input range of values.

  • values_first2 – The beginning of the first input range of values.

  • keys_result – The beginning of the merged output range of keys.

  • values_result – The beginning of the merged output range of values.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator, InputIterator1 and InputIterator2 have the same value_type, InputIterator1's value_type is a model of LessThan Comparable, the ordering on InputIterator1's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator1's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2 and InputIterator1 have the same value_type, InputIterator2's value_type is a model of LessThan Comparable, the ordering on InputIterator2's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator2's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator3 – is a model of Input Iterator, and InputIterator3's value_type is convertible to a type in OutputIterator2's set of value_types.

  • InputIterator4 – is a model of Input Iterator, and InputIterator4's value_type is convertible to a type in OutputIterator2's set of value_types.

  • OutputIterator1 – is a model of Output Iterator.

  • OutputIterator2 – is a model of Output Iterator.

Returns:

A pair p such that p.first is the end of the output range of keys, and such that p.second is the end of the output range of values.

Pre:

The ranges [keys_first1, keys_last1) and [keys_first2, keys_last2) shall be sorted with respect to operator<.

Pre:

The resulting ranges shall not overlap with any input range.

template<typename InputIterator1, typename InputIterator2, typename InputIterator3, typename InputIterator4, typename OutputIterator1, typename OutputIterator2>
thrust::pair<OutputIterator1, OutputIterator2> merge_by_key(InputIterator1 keys_first1, InputIterator1 keys_last1, InputIterator2 keys_first2, InputIterator2 keys_last2, InputIterator3 values_first1, InputIterator4 values_first2, OutputIterator1 keys_result, OutputIterator2 values_result)#

merge_by_key performs a key-value merge. That is, merge_by_key copies elements from [keys_first1, keys_last1) and [keys_first2, keys_last2) into a single range, [keys_result, keys_result + (keys_last1 - keys_first1) + (keys_last2 - keys_first2)) such that the resulting range is in ascending key order.

At the same time, merge_by_key copies elements from the two associated ranges [values_first1 + (keys_last1 - keys_first1)) and [values_first2 + (keys_last2 - keys_first2)) into a single range, [values_result, values_result + (keys_last1 - keys_first1) + (keys_last2 - keys_first2)) such that the resulting range is in ascending order implied by each input element’s associated key.

merge_by_key is stable, meaning both that the relative order of elements within each input range is preserved, and that for equivalent elements in all input key ranges the element from the first range precedes the element from the second.

The return value is is (keys_result + (keys_last1 - keys_first1) + (keys_last2 - keys_first2)) and (values_result + (keys_last1 - keys_first1) + (keys_last2 - keys_first2)).

The following code snippet demonstrates how to use merge_by_key to compute the merger of two sets of integers sorted in ascending order.

#include <thrust/merge.h>
#include <thrust/functional.h>
...
int A_keys[6] = {1, 3, 5, 7, 9, 11};
int A_vals[6] = {0, 0, 0, 0, 0, 0};

int B_keys[7] = {1, 1, 2, 3, 5, 8, 13};
int B_vals[7] = {1, 1, 1, 1, 1, 1, 1};

int keys_result[13];
int vals_result[13];

thrust::pair<int*,int*> end = thrust::merge_by_key(A_keys, A_keys + 6, B_keys, B_keys + 7, A_vals, B_vals, keys_result, vals_result);

// keys_result = {1, 1, 1, 2, 3, 3, 5, 5, 7, 8, 9, 11, 13}
// vals_result = {0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0,  0,  1}

See also

merge

See also

sort_by_key

See also

is_sorted

Parameters:
  • keys_first1 – The beginning of the first input range of keys.

  • keys_last1 – The end of the first input range of keys.

  • keys_first2 – The beginning of the second input range of keys.

  • keys_last2 – The end of the second input range of keys.

  • values_first1 – The beginning of the first input range of values.

  • values_first2 – The beginning of the first input range of values.

  • keys_result – The beginning of the merged output range of keys.

  • values_result – The beginning of the merged output range of values.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator, InputIterator1 and InputIterator2 have the same value_type, InputIterator1's value_type is a model of LessThan Comparable, the ordering on InputIterator1's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator1's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2 and InputIterator1 have the same value_type, InputIterator2's value_type is a model of LessThan Comparable, the ordering on InputIterator2's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator2's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator3 – is a model of Input Iterator, and InputIterator3's value_type is convertible to a type in OutputIterator2's set of value_types.

  • InputIterator4 – is a model of Input Iterator, and InputIterator4's value_type is convertible to a type in OutputIterator2's set of value_types.

  • OutputIterator1 – is a model of Output Iterator.

  • OutputIterator2 – is a model of Output Iterator.

Returns:

A pair p such that p.first is the end of the output range of keys, and such that p.second is the end of the output range of values.

Pre:

The ranges [keys_first1, keys_last1) and [keys_first2, keys_last2) shall be sorted with respect to operator<.

Pre:

The resulting ranges shall not overlap with any input range.

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename InputIterator3, typename InputIterator4, typename OutputIterator1, typename OutputIterator2, typename Compare> __host__ __device__ thrust::pair< OutputIterator1, OutputIterator2 > merge_by_key (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 keys_first1, InputIterator1 keys_last1, InputIterator2 keys_first2, InputIterator2 keys_last2, InputIterator3 values_first1, InputIterator4 values_first2, OutputIterator1 keys_result, OutputIterator2 values_result, Compare comp)

merge_by_key performs a key-value merge. That is, merge_by_key copies elements from [keys_first1, keys_last1) and [keys_first2, keys_last2) into a single range, [keys_result, keys_result + (keys_last1 - keys_first1) + (keys_last2 - keys_first2)) such that the resulting range is in ascending key order.

At the same time, merge_by_key copies elements from the two associated ranges [values_first1 + (keys_last1 - keys_first1)) and [values_first2 + (keys_last2 - keys_first2)) into a single range, [values_result, values_result + (keys_last1 - keys_first1) + (keys_last2 - keys_first2)) such that the resulting range is in ascending order implied by each input element’s associated key.

merge_by_key is stable, meaning both that the relative order of elements within each input range is preserved, and that for equivalent elements in all input key ranges the element from the first range precedes the element from the second.

The return value is is (keys_result + (keys_last1 - keys_first1) + (keys_last2 - keys_first2)) and (values_result + (keys_last1 - keys_first1) + (keys_last2 - keys_first2)).

This version of merge_by_key compares key elements using a function object comp.

The algorithm’s execution is parallelized using exec.

The following code snippet demonstrates how to use merge_by_key to compute the merger of two sets of integers sorted in descending order using the thrust::host execution policy for parallelization:

#include <thrust/merge.h>
#include <thrust/functional.h>
#include <thrust/execution_policy.h>
...
int A_keys[6] = {11, 9, 7, 5, 3, 1};
int A_vals[6] = { 0, 0, 0, 0, 0, 0};

int B_keys[7] = {13, 8, 5, 3, 2, 1, 1};
int B_vals[7] = { 1, 1, 1, 1, 1, 1, 1};

int keys_result[13];
int vals_result[13];

thrust::pair<int*,int*> end =
  thrust::merge_by_key(thrust::host,
                       A_keys, A_keys + 6,
                       B_keys, B_keys + 7,
                       A_vals, B_vals,
                       keys_result, vals_result,
                       thrust::greater<int>());

// keys_result = {13, 11, 9, 8, 7, 5, 5, 3, 3, 2, 1, 1, 1}
// vals_result = { 1,  0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1}

See also

merge

See also

sort_by_key

See also

is_sorted

Parameters:
  • exec – The execution policy to use for parallelization.

  • keys_first1 – The beginning of the first input range of keys.

  • keys_last1 – The end of the first input range of keys.

  • keys_first2 – The beginning of the second input range of keys.

  • keys_last2 – The end of the second input range of keys.

  • values_first1 – The beginning of the first input range of values.

  • values_first2 – The beginning of the first input range of values.

  • keys_result – The beginning of the merged output range of keys.

  • values_result – The beginning of the merged output range of values.

  • comp – Comparison operator.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator, InputIterator1's value_type is convertable to StrictWeakCompare's first_argument_type. and InputIterator1's value_type is convertable to a type in OutputIterator1's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2's value_type is convertable to StrictWeakCompare's second_argument_type. and InputIterator2's value_type is convertable to a type in OutputIterator1's set of value_types.

  • InputIterator3 – is a model of Input Iterator, and InputIterator3's value_type is convertible to a type in OutputIterator2's set of value_types.

  • InputIterator4 – is a model of Input Iterator, and InputIterator4's value_type is convertible to a type in OutputIterator2's set of value_types.

  • OutputIterator1 – is a model of Output Iterator.

  • OutputIterator2 – is a model of Output Iterator.

  • StrictWeakCompare – is a model of Strict Weak Ordering.

Returns:

A pair p such that p.first is the end of the output range of keys, and such that p.second is the end of the output range of values.

Pre:

The ranges [keys_first1, keys_last1) and [keys_first2, keys_last2) shall be sorted with respect to comp.

Pre:

The resulting ranges shall not overlap with any input range.

template<typename InputIterator1, typename InputIterator2, typename InputIterator3, typename InputIterator4, typename OutputIterator1, typename OutputIterator2, typename StrictWeakCompare>
thrust::pair<OutputIterator1, OutputIterator2> merge_by_key(InputIterator1 keys_first1, InputIterator1 keys_last1, InputIterator2 keys_first2, InputIterator2 keys_last2, InputIterator3 values_first1, InputIterator4 values_first2, OutputIterator1 keys_result, OutputIterator2 values_result, StrictWeakCompare comp)#

merge_by_key performs a key-value merge. That is, merge_by_key copies elements from [keys_first1, keys_last1) and [keys_first2, keys_last2) into a single range, [keys_result, keys_result + (keys_last1 - keys_first1) + (keys_last2 - keys_first2)) such that the resulting range is in ascending key order.

At the same time, merge_by_key copies elements from the two associated ranges [values_first1 + (keys_last1 - keys_first1)) and [values_first2 + (keys_last2 - keys_first2)) into a single range, [values_result, values_result + (keys_last1 - keys_first1) + (keys_last2 - keys_first2)) such that the resulting range is in ascending order implied by each input element’s associated key.

merge_by_key is stable, meaning both that the relative order of elements within each input range is preserved, and that for equivalent elements in all input key ranges the element from the first range precedes the element from the second.

The return value is is (keys_result + (keys_last1 - keys_first1) + (keys_last2 - keys_first2)) and (values_result + (keys_last1 - keys_first1) + (keys_last2 - keys_first2)).

This version of merge_by_key compares key elements using a function object comp.

The following code snippet demonstrates how to use merge_by_key to compute the merger of two sets of integers sorted in descending order.

#include <thrust/merge.h>
#include <thrust/functional.h>
...
int A_keys[6] = {11, 9, 7, 5, 3, 1};
int A_vals[6] = { 0, 0, 0, 0, 0, 0};

int B_keys[7] = {13, 8, 5, 3, 2, 1, 1};
int B_vals[7] = { 1, 1, 1, 1, 1, 1, 1};

int keys_result[13];
int vals_result[13];

thrust::pair<int*,int*> end = thrust::merge_by_key(A_keys, A_keys + 6, B_keys, B_keys + 7, A_vals, B_vals, keys_result, vals_result, thrust::greater<int>());

// keys_result = {13, 11, 9, 8, 7, 5, 5, 3, 3, 2, 1, 1, 1}
// vals_result = { 1,  0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1}

See also

merge

See also

sort_by_key

See also

is_sorted

Parameters:
  • keys_first1 – The beginning of the first input range of keys.

  • keys_last1 – The end of the first input range of keys.

  • keys_first2 – The beginning of the second input range of keys.

  • keys_last2 – The end of the second input range of keys.

  • values_first1 – The beginning of the first input range of values.

  • values_first2 – The beginning of the first input range of values.

  • keys_result – The beginning of the merged output range of keys.

  • values_result – The beginning of the merged output range of values.

  • comp – Comparison operator.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator, InputIterator1's value_type is convertable to StrictWeakCompare's first_argument_type. and InputIterator1's value_type is convertable to a type in OutputIterator1's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2's value_type is convertable to StrictWeakCompare's second_argument_type. and InputIterator2's value_type is convertable to a type in OutputIterator1's set of value_types.

  • InputIterator3 – is a model of Input Iterator, and InputIterator3's value_type is convertible to a type in OutputIterator2's set of value_types.

  • InputIterator4 – is a model of Input Iterator, and InputIterator4's value_type is convertible to a type in OutputIterator2's set of value_types.

  • OutputIterator1 – is a model of Output Iterator.

  • OutputIterator2 – is a model of Output Iterator.

  • StrictWeakCompare – is a model of Strict Weak Ordering.

Returns:

A pair p such that p.first is the end of the output range of keys, and such that p.second is the end of the output range of values.

Pre:

The ranges [keys_first1, keys_last1) and [keys_first2, keys_last2) shall be sorted with respect to comp.

Pre:

The resulting ranges shall not overlap with any input range.

Functions

template<typename DerivedPolicy, typename BidirectionalIterator> __host__ __device__ void reverse (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, BidirectionalIterator first, BidirectionalIterator last)

reverse reverses a range. That is: for every i such that 0 <= i <= (last - first) / 2, it exchanges *(first + i) and *(last - (i + 1)).

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

The following code snippet demonstrates how to use reverse to reverse a device_vector of integers using the thrust::device execution policy for parallelization:

#include <thrust/reverse.h>
#include <thrust/execution_policy.h>
...
const int N = 6;
int data[N] = {0, 1, 2, 3, 4, 5};
thrust::device_vector<int> v(data, data + N);
thrust::reverse(thrust::device, v.begin(), v.end());
// v is now {5, 4, 3, 2, 1, 0}

See also

reverse_copy

See also

reverse_iterator

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the range to reverse.

  • last – The end of the range to reverse.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • BidirectionalIterator – is a model of Bidirectional Iterator and BidirectionalIterator is mutable.

template<typename BidirectionalIterator>
void reverse(BidirectionalIterator first, BidirectionalIterator last)#

reverse reverses a range. That is: for every i such that 0 <= i <= (last - first) / 2, it exchanges *(first + i) and *(last - (i + 1)).

The following code snippet demonstrates how to use reverse to reverse a device_vector of integers.

#include <thrust/reverse.h>
...
const int N = 6;
int data[N] = {0, 1, 2, 3, 4, 5};
thrust::device_vector<int> v(data, data + N);
thrust::reverse(v.begin(), v.end());
// v is now {5, 4, 3, 2, 1, 0}

See also

reverse_copy

See also

reverse_iterator

Parameters:
  • first – The beginning of the range to reverse.

  • last – The end of the range to reverse.

Template Parameters:

BidirectionalIterator – is a model of Bidirectional Iterator and BidirectionalIterator is mutable.

template<typename DerivedPolicy, typename BidirectionalIterator, typename OutputIterator> __host__ __device__ OutputIterator reverse_copy (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, BidirectionalIterator first, BidirectionalIterator last, OutputIterator result)

reverse_copy differs from reverse only in that the reversed range is written to a different output range, rather than inplace.

reverse_copy copies elements from the range [first, last) to the range [result, result + (last - first)) such that the copy is a reverse of the original range. Specifically: for every i such that 0 <= i < (last - first), reverse_copy performs the assignment *(result + (last - first) - i) = *(first + i).

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 reverse_copy to reverse an input device_vector of integers to an output device_vector using the thrust::device execution policy for parallelization:

#include <thrust/reverse.h>
#include <thrust/execution_policy.h>
...
const int N = 6;
int data[N] = {0, 1, 2, 3, 4, 5};
thrust::device_vector<int> input(data, data + N);
thrust::device_vector<int> output(N);
thrust::reverse_copy(thrust::device, v.begin(), v.end(), output.begin());
// input is still {0, 1, 2, 3, 4, 5}
// output is now  {5, 4, 3, 2, 1, 0}

See also

reverse

See also

reverse_iterator

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the range to reverse.

  • last – The end of the range to reverse.

  • result – The beginning of the output range.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • BidirectionalIterator – is a model of Bidirectional Iterator, and BidirectionalIterator's value_type is convertible to OutputIterator's value_type.

  • OutputIterator – is a model of Output Iterator.

Pre:

The range [first, last) and the range [result, result + (last - first)) shall not overlap.

template<typename BidirectionalIterator, typename OutputIterator>
OutputIterator reverse_copy(BidirectionalIterator first, BidirectionalIterator last, OutputIterator result)#

reverse_copy differs from reverse only in that the reversed range is written to a different output range, rather than inplace.

reverse_copy copies elements from the range [first, last) to the range [result, result + (last - first)) such that the copy is a reverse of the original range. Specifically: for every i such that 0 <= i < (last - first), reverse_copy performs the assignment *(result + (last - first) - i) = *(first + i).

The return value is result + (last - first)).

The following code snippet demonstrates how to use reverse_copy to reverse an input device_vector of integers to an output device_vector.

#include <thrust/reverse.h>
...
const int N = 6;
int data[N] = {0, 1, 2, 3, 4, 5};
thrust::device_vector<int> input(data, data + N);
thrust::device_vector<int> output(N);
thrust::reverse_copy(v.begin(), v.end(), output.begin());
// input is still {0, 1, 2, 3, 4, 5}
// output is now  {5, 4, 3, 2, 1, 0}

See also

reverse

See also

reverse_iterator

Parameters:
  • first – The beginning of the range to reverse.

  • last – The end of the range to reverse.

  • result – The beginning of the output range.

Template Parameters:
  • BidirectionalIterator – is a model of Bidirectional Iterator, and BidirectionalIterator's value_type is convertible to OutputIterator's value_type.

  • OutputIterator – is a model of Output Iterator.

Pre:

The range [first, last) and the range [result, result + (last - first)) shall not overlap.

Functions

template<typename DerivedPolicy, typename ForwardIterator, typename Predicate> __host__ __device__ ForwardIterator partition (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last, Predicate pred)

partition reorders the elements [first, last) based on the function object pred, such that all of the elements that satisfy pred precede the elements that fail to satisfy it. The postcondition is that, for some iterator middle in the range [first, last), pred(*i) is true for every iterator i in the range [first,middle) and false for every iterator i in the range [middle, last). The return value of partition is middle.

Note that the relative order of elements in the two reordered sequences is not necessarily the same as it was in the original sequence. A different algorithm, stable_partition, does guarantee to preserve the relative order.

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

The following code snippet demonstrates how to use partition to reorder a sequence so that even numbers precede odd numbers using the thrust::host execution policy for parallelization:

#include <thrust/partition.h>
#include <thrust/execution_policy.h>
...
struct is_even
{
  __host__ __device__
  bool operator()(const int &x)
  {
    return (x % 2) == 0;
  }
};
...
int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
const int N = sizeof(A)/sizeof(int);
thrust::partition(thrust::host,
                  A, A + N,
                  is_even());
// A is now {2, 4, 6, 8, 10, 1, 3, 5, 7, 9}

See also

stable_partition

See also

partition_copy

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the sequence to reorder.

  • last – The end of the sequence to reorder.

  • pred – A function object which decides to which partition each element of the sequence [first, last) belongs.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator, and ForwardIterator's value_type is convertible to Predicate's argument_type, and ForwardIterator is mutable.

  • Predicate – is a model of Predicate.

Returns:

An iterator referring to the first element of the second partition, that is, the sequence of the elements which do not satisfy pred.

template<typename ForwardIterator, typename Predicate>
ForwardIterator partition(ForwardIterator first, ForwardIterator last, Predicate pred)#

partition reorders the elements [first, last) based on the function object pred, such that all of the elements that satisfy pred precede the elements that fail to satisfy it. The postcondition is that, for some iterator middle in the range [first, last), pred(*i) is true for every iterator i in the range [first,middle) and false for every iterator i in the range [middle, last). The return value of partition is middle.

Note that the relative order of elements in the two reordered sequences is not necessarily the same as it was in the original sequence. A different algorithm, stable_partition, does guarantee to preserve the relative order.

The following code snippet demonstrates how to use partition to reorder a sequence so that even numbers precede odd numbers.

#include <thrust/partition.h>
...
struct is_even
{
  __host__ __device__
  bool operator()(const int &x)
  {
    return (x % 2) == 0;
  }
};
...
int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
const int N = sizeof(A)/sizeof(int);
thrust::partition(A, A + N,
                   is_even());
// A is now {2, 4, 6, 8, 10, 1, 3, 5, 7, 9}

See also

stable_partition

See also

partition_copy

Parameters:
  • first – The beginning of the sequence to reorder.

  • last – The end of the sequence to reorder.

  • pred – A function object which decides to which partition each element of the sequence [first, last) belongs.

Template Parameters:
  • ForwardIterator – is a model of Forward Iterator, and ForwardIterator's value_type is convertible to Predicate's argument_type, and ForwardIterator is mutable.

  • Predicate – is a model of Predicate.

Returns:

An iterator referring to the first element of the second partition, that is, the sequence of the elements which do not satisfy pred.

template<typename DerivedPolicy, typename ForwardIterator, typename InputIterator, typename Predicate> __host__ __device__ ForwardIterator partition (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last, InputIterator stencil, Predicate pred)

partition reorders the elements [first, last) based on the function object pred applied to a stencil range [stencil, stencil + (last - first)), such that all of the elements whose corresponding stencil element satisfies pred precede all of the elements whose corresponding stencil element fails to satisfy it. The postcondition is that, for some iterator middle in the range [first, last), pred(*stencil_i) is true for every iterator stencil_i in the range [stencil,stencil + (middle - first)) and false for every iterator stencil_i in the range [stencil + (middle - first), stencil + (last - first)). The return value of stable_partition is middle.

Note that the relative order of elements in the two reordered sequences is not necessarily the same as it was in the original sequence. A different algorithm, stable_partition, does guarantee to preserve the relative order.

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

The following code snippet demonstrates how to use partition to reorder a sequence so that even numbers precede odd numbers using the thrust::host execution policy for parallelization:

#include <thrust/partition.h>
#include <thrust/execution_policy.h>
...
struct is_even
{
  __host__ __device__
  bool operator()(const int &x)
  {
    return (x % 2) == 0;
  }
};
...
int A[] = {0, 1, 0, 1, 0, 1, 0, 1, 0,  1};
int S[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
const int N = sizeof(A)/sizeof(int);
thrust::partition(thrust::host, A, A + N, S, is_even());
// A is now {1, 1, 1, 1, 1, 0, 0, 0, 0, 0}
// S is unmodified

See also

stable_partition

See also

partition_copy

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the sequence to reorder.

  • last – The end of the sequence to reorder.

  • stencil – The beginning of the stencil sequence.

  • pred – A function object which decides to which partition each element of the sequence [first, last) belongs.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator, and ForwardIterator is mutable.

  • InputIterator – is a model of Input Iterator, and InputIterator's value_type is convertible to Predicate's argument_type.

  • Predicate – is a model of Predicate.

Returns:

An iterator referring to the first element of the second partition, that is, the sequence of the elements whose stencil elements do not satisfy pred.

Pre:

The ranges [first,last) and [stencil, stencil + (last - first)) shall not overlap.

template<typename ForwardIterator, typename InputIterator, typename Predicate>
ForwardIterator partition(ForwardIterator first, ForwardIterator last, InputIterator stencil, Predicate pred)#

partition reorders the elements [first, last) based on the function object pred applied to a stencil range [stencil, stencil + (last - first)), such that all of the elements whose corresponding stencil element satisfies pred precede all of the elements whose corresponding stencil element fails to satisfy it. The postcondition is that, for some iterator middle in the range [first, last), pred(*stencil_i) is true for every iterator stencil_i in the range [stencil,stencil + (middle - first)) and false for every iterator stencil_i in the range [stencil + (middle - first), stencil + (last - first)). The return value of stable_partition is middle.

Note that the relative order of elements in the two reordered sequences is not necessarily the same as it was in the original sequence. A different algorithm, stable_partition, does guarantee to preserve the relative order.

The following code snippet demonstrates how to use partition to reorder a sequence so that even numbers precede odd numbers.

#include <thrust/partition.h>
...
struct is_even
{
  __host__ __device__
  bool operator()(const int &x)
  {
    return (x % 2) == 0;
  }
};
...
int A[] = {0, 1, 0, 1, 0, 1, 0, 1, 0,  1};
int S[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
const int N = sizeof(A)/sizeof(int);
thrust::partition(A, A + N, S, is_even());
// A is now {1, 1, 1, 1, 1, 0, 0, 0, 0, 0}
// S is unmodified

See also

stable_partition

See also

partition_copy

Parameters:
  • first – The beginning of the sequence to reorder.

  • last – The end of the sequence to reorder.

  • stencil – The beginning of the stencil sequence.

  • pred – A function object which decides to which partition each element of the sequence [first, last) belongs.

Template Parameters:
  • ForwardIterator – is a model of Forward Iterator, and ForwardIterator is mutable.

  • InputIterator – is a model of Input Iterator, and InputIterator's value_type is convertible to Predicate's argument_type.

  • Predicate – is a model of Predicate.

Returns:

An iterator referring to the first element of the second partition, that is, the sequence of the elements whose stencil elements do not satisfy pred.

Pre:

The ranges [first,last) and [stencil, stencil + (last - first)) shall not overlap.

template<typename DerivedPolicy, typename InputIterator, typename OutputIterator1, typename OutputIterator2, typename Predicate> __host__ __device__ thrust::pair< OutputIterator1, OutputIterator2 > partition_copy (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator first, InputIterator last, OutputIterator1 out_true, OutputIterator2 out_false, Predicate pred)

partition_copy differs from partition only in that the reordered sequence is written to difference output sequences, rather than in place.

partition_copy copies the elements [first, last) based on the function object pred. All of the elements that satisfy pred are copied to the range beginning at out_true and all the elements that fail to satisfy it are copied to the range beginning at out_false.

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

The following code snippet demonstrates how to use partition_copy to separate a sequence into two output sequences of even and odd numbers using the thrust::host execution policy for parallelization:

#include <thrust/partition.h>
#include <thrust/execution_policy.h>
...
struct is_even
{
  __host__ __device__
  bool operator()(const int &x)
  {
    return (x % 2) == 0;
  }
};
...
int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int result[10];
const int N = sizeof(A)/sizeof(int);
int *evens = result;
int *odds  = result + 5;
thrust::partition_copy(thrust::host, A, A + N, evens, odds, is_even());
// A remains {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
// result is now {2, 4, 6, 8, 10, 1, 3, 5, 7, 9}
// evens points to {2, 4, 6, 8, 10}
// odds points to {1, 3, 5, 7, 9}

See also

partition

Note

The relative order of elements in the two reordered sequences is not necessarily the same as it was in the original sequence. A different algorithm, stable_partition_copy, does guarantee to preserve the relative order.

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the sequence to reorder.

  • last – The end of the sequence to reorder.

  • out_true – The destination of the resulting sequence of elements which satisfy pred.

  • out_false – The destination of the resulting sequence of elements which fail to satisfy pred.

  • pred – A function object which decides to which partition each element of the sequence [first, last) belongs.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator – is 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 OutputIterator1 and OutputIterator2's value_types.

  • OutputIterator1 – is a model of Output Iterator.

  • OutputIterator2 – is a model of Output Iterator.

  • Predicate – is a model of Predicate.

Returns:

A pair p such that p.first is the end of the output range beginning at out_true and p.second is the end of the output range beginning at out_false.

Pre:

The input range shall not overlap with either output range.

template<typename InputIterator, typename OutputIterator1, typename OutputIterator2, typename Predicate>
thrust::pair<OutputIterator1, OutputIterator2> partition_copy(InputIterator first, InputIterator last, OutputIterator1 out_true, OutputIterator2 out_false, Predicate pred)#

partition_copy differs from partition only in that the reordered sequence is written to difference output sequences, rather than in place.

partition_copy copies the elements [first, last) based on the function object pred. All of the elements that satisfy pred are copied to the range beginning at out_true and all the elements that fail to satisfy it are copied to the range beginning at out_false.

The following code snippet demonstrates how to use partition_copy to separate a sequence into two output sequences of even and odd numbers.

#include <thrust/partition.h>
...
struct is_even
{
  __host__ __device__
  bool operator()(const int &x)
  {
    return (x % 2) == 0;
  }
};
...
int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int result[10];
const int N = sizeof(A)/sizeof(int);
int *evens = result;
int *odds  = result + 5;
thrust::partition_copy(A, A + N, evens, odds, is_even());
// A remains {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
// result is now {2, 4, 6, 8, 10, 1, 3, 5, 7, 9}
// evens points to {2, 4, 6, 8, 10}
// odds points to {1, 3, 5, 7, 9}

See also

partition

Note

The relative order of elements in the two reordered sequences is not necessarily the same as it was in the original sequence. A different algorithm, stable_partition_copy, does guarantee to preserve the relative order.

Parameters:
  • first – The beginning of the sequence to reorder.

  • last – The end of the sequence to reorder.

  • out_true – The destination of the resulting sequence of elements which satisfy pred.

  • out_false – The destination of the resulting sequence of elements which fail to satisfy pred.

  • pred – A function object which decides to which partition each element of the sequence [first, last) belongs.

Template Parameters:
  • InputIterator – is 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 OutputIterator1 and OutputIterator2's value_types.

  • OutputIterator1 – is a model of Output Iterator.

  • OutputIterator2 – is a model of Output Iterator.

  • Predicate – is a model of Predicate.

Returns:

A pair p such that p.first is the end of the output range beginning at out_true and p.second is the end of the output range beginning at out_false.

Pre:

The input range shall not overlap with either output range.

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename OutputIterator1, typename OutputIterator2, typename Predicate> __host__ __device__ thrust::pair< OutputIterator1, OutputIterator2 > partition_copy (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 first, InputIterator1 last, InputIterator2 stencil, OutputIterator1 out_true, OutputIterator2 out_false, Predicate pred)

partition_copy differs from partition only in that the reordered sequence is written to difference output sequences, rather than in place.

partition_copy copies the elements [first, last) based on the function object pred which is applied to a range of stencil elements. All of the elements whose corresponding stencil element satisfies pred are copied to the range beginning at out_true and all the elements whose stencil element fails to satisfy it are copied to the range beginning at out_false.

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

The following code snippet demonstrates how to use partition_copy to separate a sequence into two output sequences of even and odd numbers using the thrust::host execution policy for parallelization.

#include <thrust/partition.h>
#include <thrust/functional.h>
#include <thrust/execution_policy.h>
...
int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int S[] = {0, 1, 0, 1, 0, 1, 0, 1, 0,  1};
int result[10];
const int N = sizeof(A)/sizeof(int);
int *evens = result;
int *odds  = result + 5;
thrust::stable_partition_copy(thrust::host, A, A + N, S, evens, odds, thrust::identity<int>());
// A remains {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
// S remains {0, 1, 0, 1, 0, 1, 0, 1, 0,  1}
// result is now {2, 4, 6, 8, 10, 1, 3, 5, 7, 9}
// evens points to {2, 4, 6, 8, 10}
// odds points to {1, 3, 5, 7, 9}

See also

partition

Note

The relative order of elements in the two reordered sequences is not necessarily the same as it was in the original sequence. A different algorithm, stable_partition_copy, does guarantee to preserve the relative order.

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the sequence to reorder.

  • last – The end of the sequence to reorder.

  • stencil – The beginning of the stencil sequence.

  • out_true – The destination of the resulting sequence of elements which satisfy pred.

  • out_false – The destination of the resulting sequence of elements which fail to satisfy pred.

  • pred – A function object which decides to which partition each element of the sequence [first, last) belongs.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator, and InputIterator's value_type is convertible to OutputIterator1 and OutputIterator2's value_types.

  • InputIterator2 – is a model of Input Iterator, and InputIterator2's value_type is convertible to Predicate's argument_type.

  • OutputIterator1 – is a model of Output Iterator.

  • OutputIterator2 – is a model of Output Iterator.

  • Predicate – is a model of Predicate.

Returns:

A pair p such that p.first is the end of the output range beginning at out_true and p.second is the end of the output range beginning at out_false.

Pre:

The input ranges shall not overlap with either output range.

template<typename InputIterator1, typename InputIterator2, typename OutputIterator1, typename OutputIterator2, typename Predicate>
thrust::pair<OutputIterator1, OutputIterator2> partition_copy(InputIterator1 first, InputIterator1 last, InputIterator2 stencil, OutputIterator1 out_true, OutputIterator2 out_false, Predicate pred)#

partition_copy differs from partition only in that the reordered sequence is written to difference output sequences, rather than in place.

partition_copy copies the elements [first, last) based on the function object pred which is applied to a range of stencil elements. All of the elements whose corresponding stencil element satisfies pred are copied to the range beginning at out_true and all the elements whose stencil element fails to satisfy it are copied to the range beginning at out_false.

The following code snippet demonstrates how to use partition_copy to separate a sequence into two output sequences of even and odd numbers.

#include <thrust/partition.h>
#include <thrust/functional.h>
...
int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int S[] = {0, 1, 0, 1, 0, 1, 0, 1, 0,  1};
int result[10];
const int N = sizeof(A)/sizeof(int);
int *evens = result;
int *odds  = result + 5;
thrust::stable_partition_copy(A, A + N, S, evens, odds, thrust::identity<int>());
// A remains {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
// S remains {0, 1, 0, 1, 0, 1, 0, 1, 0,  1}
// result is now {2, 4, 6, 8, 10, 1, 3, 5, 7, 9}
// evens points to {2, 4, 6, 8, 10}
// odds points to {1, 3, 5, 7, 9}

See also

partition

Note

The relative order of elements in the two reordered sequences is not necessarily the same as it was in the original sequence. A different algorithm, stable_partition_copy, does guarantee to preserve the relative order.

Parameters:
  • first – The beginning of the sequence to reorder.

  • last – The end of the sequence to reorder.

  • stencil – The beginning of the stencil sequence.

  • out_true – The destination of the resulting sequence of elements which satisfy pred.

  • out_false – The destination of the resulting sequence of elements which fail to satisfy pred.

  • pred – A function object which decides to which partition each element of the sequence [first, last) belongs.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator, and InputIterator's value_type is convertible to OutputIterator1 and OutputIterator2's value_types.

  • InputIterator2 – is a model of Input Iterator, and InputIterator2's value_type is convertible to Predicate's argument_type.

  • OutputIterator1 – is a model of Output Iterator.

  • OutputIterator2 – is a model of Output Iterator.

  • Predicate – is a model of Predicate.

Returns:

A pair p such that p.first is the end of the output range beginning at out_true and p.second is the end of the output range beginning at out_false.

Pre:

The input ranges shall not overlap with either output range.

template<typename DerivedPolicy, typename ForwardIterator, typename Predicate> __host__ __device__ ForwardIterator stable_partition (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last, Predicate pred)

stable_partition is much like partition : it reorders the elements in the range [first, last) based on the function object pred, such that all of the elements that satisfy pred precede all of the elements that fail to satisfy it. The postcondition is that, for some iterator middle in the range [first, last), pred(*i) is true for every iterator i in the range [first,middle) and false for every iterator i in the range [middle, last). The return value of stable_partition is middle.

stable_partition differs from partition in that stable_partition is guaranteed to preserve relative order. That is, if x and y are elements in [first, last), and stencil_x and stencil_y are the stencil elements in corresponding positions within [stencil, stencil + (last - first)), and pred(stencil_x) == pred(stencil_y), and if x precedes y, then it will still be true after stable_partition that x precedes y.

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

The following code snippet demonstrates how to use stable_partition to reorder a sequence so that even numbers precede odd numbers using the thrust::host execution policy for parallelization:

#include <thrust/partition.h>
#include <thrust/execution_policy.h>
...
struct is_even
{
  __host__ __device__
  bool operator()(const int &x)
  {
    return (x % 2) == 0;
  }
};
...
int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
const int N = sizeof(A)/sizeof(int);
thrust::stable_partition(thrust::host,
                         A, A + N,
                         is_even());
// A is now {2, 4, 6, 8, 10, 1, 3, 5, 7, 9}

See also

partition

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The first element of the sequence to reorder.

  • last – One position past the last element of the sequence to reorder.

  • pred – A function object which decides to which partition each element of the sequence [first, last) belongs.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator, and ForwardIterator's value_type is convertible to Predicate's argument_type, and ForwardIterator is mutable.

  • Predicate – is a model of Predicate.

Returns:

An iterator referring to the first element of the second partition, that is, the sequence of the elements which do not satisfy pred.

template<typename ForwardIterator, typename Predicate>
ForwardIterator stable_partition(ForwardIterator first, ForwardIterator last, Predicate pred)#

stable_partition is much like partition : it reorders the elements in the range [first, last) based on the function object pred, such that all of the elements that satisfy pred precede all of the elements that fail to satisfy it. The postcondition is that, for some iterator middle in the range [first, last), pred(*i) is true for every iterator i in the range [first,middle) and false for every iterator i in the range [middle, last). The return value of stable_partition is middle.

stable_partition differs from partition in that stable_partition is guaranteed to preserve relative order. That is, if x and y are elements in [first, last), and stencil_x and stencil_y are the stencil elements in corresponding positions within [stencil, stencil + (last - first)), and pred(stencil_x) == pred(stencil_y), and if x precedes y, then it will still be true after stable_partition that x precedes y.

The following code snippet demonstrates how to use stable_partition to reorder a sequence so that even numbers precede odd numbers.

#include <thrust/partition.h>
...
struct is_even
{
  __host__ __device__
  bool operator()(const int &x)
  {
    return (x % 2) == 0;
  }
};
...
int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
const int N = sizeof(A)/sizeof(int);
thrust::stable_partition(A, A + N,
                          is_even());
// A is now {2, 4, 6, 8, 10, 1, 3, 5, 7, 9}

See also

partition

Parameters:
  • first – The first element of the sequence to reorder.

  • last – One position past the last element of the sequence to reorder.

  • pred – A function object which decides to which partition each element of the sequence [first, last) belongs.

Template Parameters:
  • ForwardIterator – is a model of Forward Iterator, and ForwardIterator's value_type is convertible to Predicate's argument_type, and ForwardIterator is mutable.

  • Predicate – is a model of Predicate.

Returns:

An iterator referring to the first element of the second partition, that is, the sequence of the elements which do not satisfy pred.

template<typename DerivedPolicy, typename ForwardIterator, typename InputIterator, typename Predicate> __host__ __device__ ForwardIterator stable_partition (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last, InputIterator stencil, Predicate pred)

stable_partition is much like partition: it reorders the elements in the range [first, last) based on the function object pred applied to a stencil range [stencil, stencil + (last - first)), such that all of the elements whose corresponding stencil element satisfies pred precede all of the elements whose corresponding stencil element fails to satisfy it. The postcondition is that, for some iterator middle in the range [first, last), pred(*stencil_i) is true for every iterator stencil_i in the range [stencil,stencil + (middle - first)) and false for every iterator stencil_i in the range [stencil + (middle - first), stencil + (last - first)). The return value of stable_partition is middle.

stable_partition differs from partition in that stable_partition is guaranteed to preserve relative order. That is, if x and y are elements in [first, last), such that pred(x) == pred(y), and if x precedes y, then it will still be true after stable_partition that x precedes y.

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

The following code snippet demonstrates how to use stable_partition to reorder a sequence so that even numbers precede odd numbers using the thrust::host execution policy for parallelization:

#include <thrust/partition.h>
#include <thrust/execution_policy.h>
...
struct is_even
{
  __host__ __device__
  bool operator()(const int &x)
  {
    return (x % 2) == 0;
  }
};
...
int A[] = {0, 1, 0, 1, 0, 1, 0, 1, 0,  1};
int S[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
const int N = sizeof(A)/sizeof(int);
thrust::stable_partition(thrust::host, A, A + N, S, is_even());
// A is now {1, 1, 1, 1, 1, 0, 0, 0, 0, 0}
// S is unmodified

See also

partition

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The first element of the sequence to reorder.

  • last – One position past the last element of the sequence to reorder.

  • stencil – The beginning of the stencil sequence.

  • pred – A function object which decides to which partition each element of the sequence [first, last) belongs.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator, and ForwardIterator is mutable.

  • InputIterator – is a model of Input Iterator, and InputIterator's value_type is convertible to Predicate's argument_type.

  • Predicate – is a model of Predicate.

Returns:

An iterator referring to the first element of the second partition, that is, the sequence of the elements whose stencil elements do not satisfy pred.

Pre:

The range [first, last) shall not overlap with the range [stencil, stencil + (last - first)).

template<typename ForwardIterator, typename InputIterator, typename Predicate>
ForwardIterator stable_partition(ForwardIterator first, ForwardIterator last, InputIterator stencil, Predicate pred)#

stable_partition is much like partition: it reorders the elements in the range [first, last) based on the function object pred applied to a stencil range [stencil, stencil + (last - first)), such that all of the elements whose corresponding stencil element satisfies pred precede all of the elements whose corresponding stencil element fails to satisfy it. The postcondition is that, for some iterator middle in the range [first, last), pred(*stencil_i) is true for every iterator stencil_i in the range [stencil,stencil + (middle - first)) and false for every iterator stencil_i in the range [stencil + (middle - first), stencil + (last - first)). The return value of stable_partition is middle.

stable_partition differs from partition in that stable_partition is guaranteed to preserve relative order. That is, if x and y are elements in [first, last), such that pred(x) == pred(y), and if x precedes y, then it will still be true after stable_partition that x precedes y.

The following code snippet demonstrates how to use stable_partition to reorder a sequence so that even numbers precede odd numbers.

#include <thrust/partition.h>
...
struct is_even
{
  __host__ __device__
  bool operator()(const int &x)
  {
    return (x % 2) == 0;
  }
};
...
int A[] = {0, 1, 0, 1, 0, 1, 0, 1, 0,  1};
int S[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
const int N = sizeof(A)/sizeof(int);
thrust::stable_partition(A, A + N, S, is_even());
// A is now {1, 1, 1, 1, 1, 0, 0, 0, 0, 0}
// S is unmodified

See also

partition

Parameters:
  • first – The first element of the sequence to reorder.

  • last – One position past the last element of the sequence to reorder.

  • stencil – The beginning of the stencil sequence.

  • pred – A function object which decides to which partition each element of the sequence [first, last) belongs.

Template Parameters:
  • ForwardIterator – is a model of Forward Iterator, and ForwardIterator is mutable.

  • InputIterator – is a model of Input Iterator, and InputIterator's value_type is convertible to Predicate's argument_type.

  • Predicate – is a model of Predicate.

Returns:

An iterator referring to the first element of the second partition, that is, the sequence of the elements whose stencil elements do not satisfy pred.

Pre:

The range [first, last) shall not overlap with the range [stencil, stencil + (last - first)).

template<typename DerivedPolicy, typename InputIterator, typename OutputIterator1, typename OutputIterator2, typename Predicate> __host__ __device__ thrust::pair< OutputIterator1, OutputIterator2 > stable_partition_copy (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator first, InputIterator last, OutputIterator1 out_true, OutputIterator2 out_false, Predicate pred)

stable_partition_copy differs from stable_partition only in that the reordered sequence is written to different output sequences, rather than in place.

stable_partition_copy copies the elements [first, last) based on the function object pred. All of the elements that satisfy pred are copied to the range beginning at out_true and all the elements that fail to satisfy it are copied to the range beginning at out_false.

stable_partition_copy differs from partition_copy in that stable_partition_copy is guaranteed to preserve relative order. That is, if x and y are elements in [first, last), such that pred(x) == pred(y), and if x precedes y, then it will still be true after stable_partition_copy that x precedes y in the output.

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

The following code snippet demonstrates how to use stable_partition_copy to reorder a sequence so that even numbers precede odd numbers using the thrust::host execution policy for parallelization:

#include <thrust/partition.h>
#include <thrust/execution_policy.h>
...
struct is_even
{
  __host__ __device__
  bool operator()(const int &x)
  {
    return (x % 2) == 0;
  }
};
...
int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int result[10];
const int N = sizeof(A)/sizeof(int);
int *evens = result;
int *odds  = result + 5;
thrust::stable_partition_copy(thrust::host, A, A + N, evens, odds, is_even());
// A remains {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
// result is now {2, 4, 6, 8, 10, 1, 3, 5, 7, 9}
// evens points to {2, 4, 6, 8, 10}
// odds points to {1, 3, 5, 7, 9}

See also

partition_copy

See also

stable_partition

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The first element of the sequence to reorder.

  • last – One position past the last element of the sequence to reorder.

  • out_true – The destination of the resulting sequence of elements which satisfy pred.

  • out_false – The destination of the resulting sequence of elements which fail to satisfy pred.

  • pred – A function object which decides to which partition each element of the sequence [first, last) belongs.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator – is 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 OutputIterator1 and OutputIterator2's value_types.

  • OutputIterator1 – is a model of Output Iterator.

  • OutputIterator2 – is a model of Output Iterator.

  • Predicate – is a model of Predicate.

Returns:

A pair p such that p.first is the end of the output range beginning at out_true and p.second is the end of the output range beginning at out_false.

Pre:

The input ranges shall not overlap with either output range.

template<typename InputIterator, typename OutputIterator1, typename OutputIterator2, typename Predicate>
thrust::pair<OutputIterator1, OutputIterator2> stable_partition_copy(InputIterator first, InputIterator last, OutputIterator1 out_true, OutputIterator2 out_false, Predicate pred)#

stable_partition_copy differs from stable_partition only in that the reordered sequence is written to different output sequences, rather than in place.

stable_partition_copy copies the elements [first, last) based on the function object pred. All of the elements that satisfy pred are copied to the range beginning at out_true and all the elements that fail to satisfy it are copied to the range beginning at out_false.

stable_partition_copy differs from partition_copy in that stable_partition_copy is guaranteed to preserve relative order. That is, if x and y are elements in [first, last), such that pred(x) == pred(y), and if x precedes y, then it will still be true after stable_partition_copy that x precedes y in the output.

The following code snippet demonstrates how to use stable_partition_copy to reorder a sequence so that even numbers precede odd numbers.

#include <thrust/partition.h>
...
struct is_even
{
  __host__ __device__
  bool operator()(const int &x)
  {
    return (x % 2) == 0;
  }
};
...
int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int result[10];
const int N = sizeof(A)/sizeof(int);
int *evens = result;
int *odds  = result + 5;
thrust::stable_partition_copy(A, A + N, evens, odds, is_even());
// A remains {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
// result is now {2, 4, 6, 8, 10, 1, 3, 5, 7, 9}
// evens points to {2, 4, 6, 8, 10}
// odds points to {1, 3, 5, 7, 9}

See also

partition_copy

See also

stable_partition

Parameters:
  • first – The first element of the sequence to reorder.

  • last – One position past the last element of the sequence to reorder.

  • out_true – The destination of the resulting sequence of elements which satisfy pred.

  • out_false – The destination of the resulting sequence of elements which fail to satisfy pred.

  • pred – A function object which decides to which partition each element of the sequence [first, last) belongs.

Template Parameters:
  • InputIterator – is 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 OutputIterator1 and OutputIterator2's value_types.

  • OutputIterator1 – is a model of Output Iterator.

  • OutputIterator2 – is a model of Output Iterator.

  • Predicate – is a model of Predicate.

Returns:

A pair p such that p.first is the end of the output range beginning at out_true and p.second is the end of the output range beginning at out_false.

Pre:

The input ranges shall not overlap with either output range.

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename OutputIterator1, typename OutputIterator2, typename Predicate> __host__ __device__ thrust::pair< OutputIterator1, OutputIterator2 > stable_partition_copy (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 first, InputIterator1 last, InputIterator2 stencil, OutputIterator1 out_true, OutputIterator2 out_false, Predicate pred)

stable_partition_copy differs from stable_partition only in that the reordered sequence is written to different output sequences, rather than in place.

stable_partition_copy copies the elements [first, last) based on the function object pred which is applied to a range of stencil elements. All of the elements whose corresponding stencil element satisfies pred are copied to the range beginning at out_true and all the elements whose stencil element fails to satisfy it are copied to the range beginning at out_false.

stable_partition_copy differs from partition_copy in that stable_partition_copy is guaranteed to preserve relative order. That is, if x and y are elements in [first, last), such that pred(x) == pred(y), and if x precedes y, then it will still be true after stable_partition_copy that x precedes y in the output.

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

The following code snippet demonstrates how to use stable_partition_copy to reorder a sequence so that even numbers precede odd numbers using the thrust::host execution policy for parallelization:

#include <thrust/partition.h>
#include <thrust/functional.h>
#include <thrust/execution_policy.h>
...
int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int S[] = {0, 1, 0, 1, 0, 1, 0, 1, 0,  1};
int result[10];
const int N = sizeof(A)/sizeof(int);
int *evens = result;
int *odds  = result + 5;
thrust::stable_partition_copy(thrust::host, A, A + N, S, evens, odds, thrust::identity<int>());
// A remains {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
// S remains {0, 1, 0, 1, 0, 1, 0, 1, 0,  1}
// result is now {2, 4, 6, 8, 10, 1, 3, 5, 7, 9}
// evens points to {2, 4, 6, 8, 10}
// odds points to {1, 3, 5, 7, 9}

See also

partition_copy

See also

stable_partition

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The first element of the sequence to reorder.

  • last – One position past the last element of the sequence to reorder.

  • stencil – The beginning of the stencil sequence.

  • out_true – The destination of the resulting sequence of elements which satisfy pred.

  • out_false – The destination of the resulting sequence of elements which fail to satisfy pred.

  • pred – A function object which decides to which partition each element of the sequence [first, last) belongs.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator, and InputIterator's value_type is convertible to OutputIterator1 and OutputIterator2's value_types.

  • InputIterator2 – is a model of Input Iterator, and InputIterator2's value_type is convertible to Predicate's argument_type.

  • OutputIterator1 – is a model of Output Iterator.

  • OutputIterator2 – is a model of Output Iterator.

  • Predicate – is a model of Predicate.

Returns:

A pair p such that p.first is the end of the output range beginning at out_true and p.second is the end of the output range beginning at out_false.

Pre:

The input ranges shall not overlap with either output range.

template<typename InputIterator1, typename InputIterator2, typename OutputIterator1, typename OutputIterator2, typename Predicate>
thrust::pair<OutputIterator1, OutputIterator2> stable_partition_copy(InputIterator1 first, InputIterator1 last, InputIterator2 stencil, OutputIterator1 out_true, OutputIterator2 out_false, Predicate pred)#

stable_partition_copy differs from stable_partition only in that the reordered sequence is written to different output sequences, rather than in place.

stable_partition_copy copies the elements [first, last) based on the function object pred which is applied to a range of stencil elements. All of the elements whose corresponding stencil element satisfies pred are copied to the range beginning at out_true and all the elements whose stencil element fails to satisfy it are copied to the range beginning at out_false.

stable_partition_copy differs from partition_copy in that stable_partition_copy is guaranteed to preserve relative order. That is, if x and y are elements in [first, last), such that pred(x) == pred(y), and if x precedes y, then it will still be true after stable_partition_copy that x precedes y in the output.

The following code snippet demonstrates how to use stable_partition_copy to reorder a sequence so that even numbers precede odd numbers.

#include <thrust/partition.h>
#include <thrust/functional.h>
...
int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int S[] = {0, 1, 0, 1, 0, 1, 0, 1, 0,  1};
int result[10];
const int N = sizeof(A)/sizeof(int);
int *evens = result;
int *odds  = result + 5;
thrust::stable_partition_copy(A, A + N, S, evens, odds, thrust::identity<int>());
// A remains {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
// S remains {0, 1, 0, 1, 0, 1, 0, 1, 0,  1}
// result is now {2, 4, 6, 8, 10, 1, 3, 5, 7, 9}
// evens points to {2, 4, 6, 8, 10}
// odds points to {1, 3, 5, 7, 9}

See also

partition_copy

See also

stable_partition

Parameters:
  • first – The first element of the sequence to reorder.

  • last – One position past the last element of the sequence to reorder.

  • stencil – The beginning of the stencil sequence.

  • out_true – The destination of the resulting sequence of elements which satisfy pred.

  • out_false – The destination of the resulting sequence of elements which fail to satisfy pred.

  • pred – A function object which decides to which partition each element of the sequence [first, last) belongs.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator, and InputIterator's value_type is convertible to OutputIterator1 and OutputIterator2's value_types.

  • InputIterator2 – is a model of Input Iterator, and InputIterator2's value_type is convertible to Predicate's argument_type.

  • OutputIterator1 – is a model of Output Iterator.

  • OutputIterator2 – is a model of Output Iterator.

  • Predicate – is a model of Predicate.

Returns:

A pair p such that p.first is the end of the output range beginning at out_true and p.second is the end of the output range beginning at out_false.

Pre:

The input ranges shall not overlap with either output range.

Functions

template<typename DerivedPolicy, typename InputIterator, typename OutputIterator, typename Predicate> __host__ __device__ OutputIterator copy_if (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator first, InputIterator last, OutputIterator result, Predicate pred)

This version of copy_if copies elements from the range [first,last) to a range beginning at result, except that any element which causes pred to be false is not copied. copy_if is stable, meaning that the relative order of elements that are copied is unchanged.

More precisely, for every integer n such that 0 <= n < last-first, copy_if performs the assignment *result = *(first+n) and result is advanced one position if pred(*(first+n)). Otherwise, no assignment occurs and result is not advanced.

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

The following code snippet demonstrates how to use copy_if to perform stream compaction to copy even numbers to an output range using the thrust::host parallelization policy:

#include <thrust/copy.h>
#include <thrust/execution_policy.h>
...
struct is_even
{
  __host__ __device__
  bool operator()(const int x)
  {
    return (x % 2) == 0;
  }
};
...
const int N = 6;
int V[N] = {-2, 0, -1, 0, 1, 2};
int result[4];

thrust::copy_if(thrust::host, V, V + N, result, is_even());

// V remains {-2, 0, -1, 0, 1, 2}
// result is now {-2, 0, 0, 2}

See also

remove_copy_if

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the sequence from which to copy.

  • last – The end of the sequence from which to copy.

  • result – The beginning of the sequence into which to copy.

  • pred – The predicate to test on every value of the range [first, last).

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator – is a model of Input Iterator, and InputIterator's value_type is convertible to Predicate's argument_type.

  • OutputIterator – is a model of Output Iterator.

  • Predicate – is a model of Predicate.

Returns:

result + n, where n is equal to the number of times pred evaluated to true in the range [first, last).

Pre:

The ranges [first, last) and [result, result + (last - first)) shall not overlap.

template<typename InputIterator, typename OutputIterator, typename Predicate>
OutputIterator copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred)#

This version of copy_if copies elements from the range [first,last) to a range beginning at result, except that any element which causes pred to false is not copied. copy_if is stable, meaning that the relative order of elements that are copied is unchanged.

More precisely, for every integer n such that 0 <= n < last-first, copy_if performs the assignment *result = *(first+n) and result is advanced one position if pred(*(first+n)). Otherwise, no assignment occurs and result is not advanced.

The following code snippet demonstrates how to use copy_if to perform stream compaction to copy even numbers to an output range.

#include <thrust/copy.h>
...
struct is_even
{
  __host__ __device__
  bool operator()(const int x)
  {
    return (x % 2) == 0;
  }
};
...
const int N = 6;
int V[N] = {-2, 0, -1, 0, 1, 2};
int result[4];

thrust::copy_if(V, V + N, result, is_even());

// V remains {-2, 0, -1, 0, 1, 2}
// result is now {-2, 0, 0, 2}

See also

remove_copy_if

Parameters:
  • first – The beginning of the sequence from which to copy.

  • last – The end of the sequence from which to copy.

  • result – The beginning of the sequence into which to copy.

  • pred – The predicate to test on every value of the range [first, last).

Template Parameters:
  • InputIterator – is a model of Input Iterator, and InputIterator's value_type is convertible to Predicate's argument_type.

  • OutputIterator – is a model of Output Iterator.

  • Predicate – is a model of Predicate.

Returns:

result + n, where n is equal to the number of times pred evaluated to true in the range [first, last).

Pre:

The ranges [first, last) and [result, result + (last - first)) shall not overlap.

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename OutputIterator, typename Predicate> __host__ __device__ OutputIterator copy_if (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 first, InputIterator1 last, InputIterator2 stencil, OutputIterator result, Predicate pred)

This version of copy_if copies elements from the range [first,last) to a range beginning at result, except that any element whose corresponding stencil element causes pred to be false is not copied. copy_if is stable, meaning that the relative order of elements that are copied is unchanged.

More precisely, for every integer n such that 0 <= n < last-first, copy_if performs the assignment *result = *(first+n) and result is advanced one position if pred(*(stencil+n)). Otherwise, no assignment occurs and result is not advanced.

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

The following code snippet demonstrates how to use copy_if to perform stream compaction to copy numbers to an output range when corresponding stencil elements are even using the thrust::host execution policy:

#include <thrust/copy.h>
#include <thrust/execution_policy.h>
...
struct is_even
{
  __host__ __device__
  bool operator()(const int x)
  {
    return (x % 2) == 0;
  }
};
...
int N = 6;
int data[N]    = { 0, 1,  2, 3, 4, 5};
int stencil[N] = {-2, 0, -1, 0, 1, 2};
int result[4];

thrust::copy_if(thrust::host, data, data + N, stencil, result, is_even());

// data remains    = { 0, 1,  2, 3, 4, 5};
// stencil remains = {-2, 0, -1, 0, 1, 2};
// result is now     { 0, 1,  3, 5}

See also

remove_copy_if

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the sequence from which to copy.

  • last – The end of the sequence from which to copy.

  • stencil – The beginning of the stencil sequence.

  • result – The beginning of the sequence into which to copy.

  • pred – The predicate to test on every value of the range [stencil, stencil + (last-first)).

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator.

  • InputIterator2 – is a model of Input Iterator, and InputIterator2's value_type is convertible to Predicate's argument_type.

  • OutputIterator – is a model of Output Iterator.

  • Predicate – is a model of Predicate.

Returns:

result + n, where n is equal to the number of times pred evaluated to true in the range [stencil, stencil + (last-first)).

Pre:

The ranges [first, last) and [result, result + (last - first)) shall not overlap.

Pre:

The ranges [stencil, stencil + (last - first)) and [result, result + (last - first)) shall not overlap.

template<typename InputIterator1, typename InputIterator2, typename OutputIterator, typename Predicate>
OutputIterator copy_if(InputIterator1 first, InputIterator1 last, InputIterator2 stencil, OutputIterator result, Predicate pred)#

This version of copy_if copies elements from the range [first,last) to a range beginning at result, except that any element whose corresponding stencil element causes pred to be false is not copied. copy_if is stable, meaning that the relative order of elements that are copied is unchanged.

More precisely, for every integer n such that 0 <= n < last-first, copy_if performs the assignment *result = *(first+n) and result is advanced one position if pred(*(stencil+n)). Otherwise, no assignment occurs and result is not advanced.

The following code snippet demonstrates how to use copy_if to perform stream compaction to copy numbers to an output range when corresponding stencil elements are even:

#include <thrust/copy.h>
...
struct is_even
{
  __host__ __device__
  bool operator()(const int x)
  {
    return (x % 2) == 0;
  }
};
...
int N = 6;
int data[N]    = { 0, 1,  2, 3, 4, 5};
int stencil[N] = {-2, 0, -1, 0, 1, 2};
int result[4];

thrust::copy_if(data, data + N, stencil, result, is_even());

// data remains    = { 0, 1,  2, 3, 4, 5};
// stencil remains = {-2, 0, -1, 0, 1, 2};
// result is now     { 0, 1,  3, 5}

See also

remove_copy_if

Parameters:
  • first – The beginning of the sequence from which to copy.

  • last – The end of the sequence from which to copy.

  • stencil – The beginning of the stencil sequence.

  • result – The beginning of the sequence into which to copy.

  • pred – The predicate to test on every value of the range [stencil, stencil + (last-first)).

Template Parameters:
  • InputIterator1 – is a model of Input Iterator.

  • InputIterator2 – is a model of Input Iterator, and InputIterator2's value_type is convertible to Predicate's argument_type.

  • OutputIterator – is a model of Output Iterator.

  • Predicate – is a model of Predicate.

Returns:

result + n, where n is equal to the number of times pred evaluated to true in the range [stencil, stencil + (last-first)).

Pre:

The ranges [first, last) and [result, result + (last - first)) shall not overlap.

Pre:

The ranges [stencil, stencil + (last - first)) and [result, result + (last - first)) shall not overlap.

template<typename DerivedPolicy, typename ForwardIterator, typename T> __host__ __device__ ForwardIterator remove (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last, const T &value)

remove removes from the range [first, last) all elements that are equal to value. That is, remove returns an iterator new_last such that the range [first, new_last) contains no elements equal to value. The iterators in the range [new_first,last) are all still dereferenceable, but the elements that they point to are unspecified. remove is stable, meaning that the relative order of elements that are not equal to value is unchanged.

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

The following code snippet demonstrates how to use remove to remove a number of interest from a range using the thrust::host execution policy for parallelization:

#include <thrust/remove.h>
#include <thrust/execution_policy.h>
...
const int N = 6;
int A[N] = {3, 1, 4, 1, 5, 9};
int *new_end = thrust::remove(A, A + N, 1);
// The first four values of A are now {3, 4, 5, 9}
// Values beyond new_end are unspecified

See also

remove_if

See also

remove_copy

See also

remove_copy_if

Note

The meaning of “removal” is somewhat subtle. remove does not destroy any iterators, and does not change the distance between first and last. (There’s no way that it could do anything of the sort.) So, for example, if V is a device_vector, remove(V.begin(), V.end(), 0) does not change V.size(): V will contain just as many elements as it did before. remove returns an iterator that points to the end of the resulting range after elements have been removed from it; it follows that the elements after that iterator are of no interest, and may be discarded. If you are removing elements from a Sequence, you may simply erase them. That is, a reasonable way of removing elements from a Sequence is S.erase(remove(S.begin(), S.end(), x), S.end()).

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the range of interest.

  • last – The end of the range of interest.

  • value – The value to remove from the range [first, last). Elements which are equal to value are removed from the sequence.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator, and ForwardIterator is mutable.

  • T – is a model of Equality Comparable, and objects of type T can be compared for equality with objects of ForwardIterator's value_type.

Returns:

A ForwardIterator pointing to the end of the resulting range of elements which are not equal to value.

template<typename ForwardIterator, typename T>
ForwardIterator remove(ForwardIterator first, ForwardIterator last, const T &value)#

remove removes from the range [first, last) all elements that are equal to value. That is, remove returns an iterator new_last such that the range [first, new_last) contains no elements equal to value. The iterators in the range [new_first,last) are all still dereferenceable, but the elements that they point to are unspecified. remove is stable, meaning that the relative order of elements that are not equal to value is unchanged.

The following code snippet demonstrates how to use remove to remove a number of interest from a range.

#include <thrust/remove.h>
...
const int N = 6;
int A[N] = {3, 1, 4, 1, 5, 9};
int *new_end = thrust::remove(A, A + N, 1);
// The first four values of A are now {3, 4, 5, 9}
// Values beyond new_end are unspecified

See also

remove_if

See also

remove_copy

See also

remove_copy_if

Note

The meaning of “removal” is somewhat subtle. remove does not destroy any iterators, and does not change the distance between first and last. (There’s no way that it could do anything of the sort.) So, for example, if V is a device_vector, remove(V.begin(), V.end(), 0) does not change V.size(): V will contain just as many elements as it did before. remove returns an iterator that points to the end of the resulting range after elements have been removed from it; it follows that the elements after that iterator are of no interest, and may be discarded. If you are removing elements from a Sequence, you may simply erase them. That is, a reasonable way of removing elements from a Sequence is S.erase(remove(S.begin(), S.end(), x), S.end()).

Parameters:
  • first – The beginning of the range of interest.

  • last – The end of the range of interest.

  • value – The value to remove from the range [first, last). Elements which are equal to value are removed from the sequence.

Template Parameters:
  • ForwardIterator – is a model of Forward Iterator, and ForwardIterator is mutable.

  • T – is a model of Equality Comparable, and objects of type T can be compared for equality with objects of ForwardIterator's value_type.

Returns:

A ForwardIterator pointing to the end of the resulting range of elements which are not equal to value.

template<typename DerivedPolicy, typename InputIterator, typename OutputIterator, typename T> __host__ __device__ OutputIterator remove_copy (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator first, InputIterator last, OutputIterator result, const T &value)

remove_copy copies elements that are not equal to value from the range [first, last) to a range beginning at result. The return value is the end of the resulting range. This operation is stable, meaning that the relative order of the elements that are copied is the same as in the range [first, last).

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

The following code snippet demonstrates how to use remove_copy to copy a sequence of numbers to an output range while omitting a value of interest using the thrust::host execution policy for parallelization:

#include <thrust/remove.h>
#include <thrust/execution_policy.h>
...
const int N = 6;
int V[N] = {-2, 0, -1, 0, 1, 2};
int result[N-2];
thrust::remove_copy(thrust::host, V, V + N, result, 0);
// V remains {-2, 0, -1, 0, 1, 2}
// result is now {-2, -1, 1, 2}

See also

remove

See also

remove_if

See also

remove_copy_if

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the range of interest.

  • last – The end of the range of interest.

  • result – The resulting range is copied to the sequence beginning at this location.

  • value – The value to omit from the copied range.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator – is a model of Input Iterator, and InputIterator's value_type is convertible to a type in OutputIterator's set of value_types.

  • OutputIterator – is a model of Output Iterator.

  • T – is a model of Equality Comparable, and objects of type T can be compared for equality with objects of InputIterator's value_type.

Returns:

An OutputIterator pointing to the end of the resulting range of elements which are not equal to value.

Pre:

The range [first, last) shall not overlap the range [result, result + (last - first)).

template<typename InputIterator, typename OutputIterator, typename T>
OutputIterator remove_copy(InputIterator first, InputIterator last, OutputIterator result, const T &value)#

remove_copy copies elements that are not equal to value from the range [first, last) to a range beginning at result. The return value is the end of the resulting range. This operation is stable, meaning that the relative order of the elements that are copied is the same as in the range [first, last).

The following code snippet demonstrates how to use remove_copy to copy a sequence of numbers to an output range while omitting a value of interest.

#include <thrust/remove.h>
...
const int N = 6;
int V[N] = {-2, 0, -1, 0, 1, 2};
int result[N-2];
thrust::remove_copy(V, V + N, result, 0);
// V remains {-2, 0, -1, 0, 1, 2}
// result is now {-2, -1, 1, 2}

See also

remove

See also

remove_if

See also

remove_copy_if

Parameters:
  • first – The beginning of the range of interest.

  • last – The end of the range of interest.

  • result – The resulting range is copied to the sequence beginning at this location.

  • value – The value to omit from the copied range.

Template Parameters:
  • InputIterator – is a model of Input Iterator, and InputIterator's value_type is convertible to a type in OutputIterator's set of value_types.

  • OutputIterator – is a model of Output Iterator.

  • T – is a model of Equality Comparable, and objects of type T can be compared for equality with objects of InputIterator's value_type.

Returns:

An OutputIterator pointing to the end of the resulting range of elements which are not equal to value.

Pre:

The range [first, last) shall not overlap the range [result, result + (last - first)).

template<typename DerivedPolicy, typename ForwardIterator, typename Predicate> __host__ __device__ ForwardIterator remove_if (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last, Predicate pred)

remove_if removes from the range [first, last) every element x such that pred(x) is true. That is, remove_if returns an iterator new_last such that the range [first,new_last) contains no elements for which pred is true. The iterators in the range [new_last,last) are all still dereferenceable, but the elements that they point to are unspecified. remove_if is stable, meaning that the relative order of elements that are not removed is unchanged.

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

The following code snippet demonstrates how to use remove_if to remove all even numbers from an array of integers using the thrust::host execution policy for parallelization:

#include <thrust/remove.h>
#include <thrust/execution_policy.h>
...
struct is_even
{
  __host__ __device__
  bool operator()(const int x)
  {
    return (x % 2) == 0;
  }
};
...
const int N = 6;
int A[N] = {1, 4, 2, 8, 5, 7};
int *new_end = thrust::remove_if(thrust::host, A, A + N, is_even());
// The first three values of A are now {1, 5, 7}
// Values beyond new_end are unspecified

See also

remove

See also

remove_copy

See also

remove_copy_if

Note

The meaning of “removal” is somewhat subtle. remove_if does not destroy any iterators, and does not change the distance between first and last. (There’s no way that it could do anything of the sort.) So, for example, if V is a device_vector, remove_if(V.begin(), V.end(), pred) does not change V.size(): V will contain just as many elements as it did before. remove_if returns an iterator that points to the end of the resulting range after elements have been removed from it; it follows that the elements after that iterator are of no interest, and may be discarded. If you are removing elements from a Sequence, you may simply erase them. That is, a reasonable way of removing elements from a Sequence is S.erase(remove_if(S.begin(), S.end(), pred), S.end()).

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the range of interest.

  • last – The end of the range of interest.

  • pred – A predicate to evaluate for each element of the range [first,last). Elements for which pred evaluates to true are removed from the sequence.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator, ForwardIterator is mutable, and ForwardIterator's value_type is convertible to Predicate's argument_type.

  • Predicate – is a model of Predicate.

Returns:

A ForwardIterator pointing to the end of the resulting range of elements for which pred evaluated to true.

template<typename ForwardIterator, typename Predicate>
ForwardIterator remove_if(ForwardIterator first, ForwardIterator last, Predicate pred)#

remove_if removes from the range [first, last) every element x such that pred(x) is true. That is, remove_if returns an iterator new_last such that the range [first,new_last) contains no elements for which pred is true. The iterators in the range [new_last,last) are all still dereferenceable, but the elements that they point to are unspecified. remove_if is stable, meaning that the relative order of elements that are not removed is unchanged.

The following code snippet demonstrates how to use remove_if to remove all even numbers from an array of integers.

#include <thrust/remove.h>
...
struct is_even
{
  __host__ __device__
  bool operator()(const int x)
  {
    return (x % 2) == 0;
  }
};
...
const int N = 6;
int A[N] = {1, 4, 2, 8, 5, 7};
int *new_end = thrust::remove_if(A, A + N, is_even());
// The first three values of A are now {1, 5, 7}
// Values beyond new_end are unspecified

See also

remove

See also

remove_copy

See also

remove_copy_if

Note

The meaning of “removal” is somewhat subtle. remove_if does not destroy any iterators, and does not change the distance between first and last. (There’s no way that it could do anything of the sort.) So, for example, if V is a device_vector, remove_if(V.begin(), V.end(), pred) does not change V.size(): V will contain just as many elements as it did before. remove_if returns an iterator that points to the end of the resulting range after elements have been removed from it; it follows that the elements after that iterator are of no interest, and may be discarded. If you are removing elements from a Sequence, you may simply erase them. That is, a reasonable way of removing elements from a Sequence is S.erase(remove_if(S.begin(), S.end(), pred), S.end()).

Parameters:
  • first – The beginning of the range of interest.

  • last – The end of the range of interest.

  • pred – A predicate to evaluate for each element of the range [first,last). Elements for which pred evaluates to true are removed from the sequence.

Template Parameters:
  • ForwardIterator – is a model of Forward Iterator, ForwardIterator is mutable, and ForwardIterator's value_type is convertible to Predicate's argument_type.

  • Predicate – is a model of Predicate.

Returns:

A ForwardIterator pointing to the end of the resulting range of elements for which pred evaluated to true.

template<typename DerivedPolicy, typename InputIterator, typename OutputIterator, typename Predicate> __host__ __device__ OutputIterator remove_copy_if (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator first, InputIterator last, OutputIterator result, Predicate pred)

remove_copy_if copies elements from the range [first,last) to a range beginning at result, except that elements for which pred is true are not copied. The return value is the end of the resulting range. This operation is stable, meaning that the relative order of the elements that are copied is the same as the range [first,last).

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

The following code snippet demonstrates how to use remove_copy_if to copy a sequence of numbers to an output range while omitting even numbers using the thrust::host execution policy for parallelization:

#include <thrust/remove.h>
#include <thrust/execution_policy.h>
...
struct is_even
{
  __host__ __device__
  bool operator()(const int x)
  {
    return (x % 2) == 0;
  }
};
...
const int N = 6;
int V[N] = {-2, 0, -1, 0, 1, 2};
int result[2];
thrust::remove_copy_if(thrust::host, V, V + N, result, is_even());
// V remains {-2, 0, -1, 0, 1, 2}
// result is now {-1, 1}

See also

remove

See also

remove_copy

See also

remove_if

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the range of interest.

  • last – The end of the range of interest.

  • result – The resulting range is copied to the sequence beginning at this location.

  • pred – A predicate to evaluate for each element of the range [first,last). Elements for which pred evaluates to false are not copied to the resulting sequence.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator – is a model of Input Iterator, InputIterator's value_type is convertible to a type in OutputIterator's set of value_types, and InputIterator's value_type is convertible to Predicate's argument_type.

  • OutputIterator – is a model of Output Iterator.

  • Predicate – is a model of Predicate.

Returns:

An OutputIterator pointing to the end of the resulting range.

Pre:

The range [first, last) shall not overlap the range [result, result + (last - first)).

template<typename InputIterator, typename OutputIterator, typename Predicate>
OutputIterator remove_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred)#

remove_copy_if copies elements from the range [first,last) to a range beginning at result, except that elements for which pred is true are not copied. The return value is the end of the resulting range. This operation is stable, meaning that the relative order of the elements that are copied is the same as the range [first,last).

The following code snippet demonstrates how to use remove_copy_if to copy a sequence of numbers to an output range while omitting even numbers.

#include <thrust/remove.h>
...
struct is_even
{
  __host__ __device__
  bool operator()(const int x)
  {
    return (x % 2) == 0;
  }
};
...
const int N = 6;
int V[N] = {-2, 0, -1, 0, 1, 2};
int result[2];
thrust::remove_copy_if(V, V + N, result, is_even());
// V remains {-2, 0, -1, 0, 1, 2}
// result is now {-1, 1}

See also

remove

See also

remove_copy

See also

remove_if

Parameters:
  • first – The beginning of the range of interest.

  • last – The end of the range of interest.

  • result – The resulting range is copied to the sequence beginning at this location.

  • pred – A predicate to evaluate for each element of the range [first,last). Elements for which pred evaluates to false are not copied to the resulting sequence.

Template Parameters:
  • InputIterator – is a model of Input Iterator, InputIterator's value_type is convertible to a type in OutputIterator's set of value_types, and InputIterator's value_type is convertible to Predicate's argument_type.

  • OutputIterator – is a model of Output Iterator.

  • Predicate – is a model of Predicate.

Returns:

An OutputIterator pointing to the end of the resulting range.

Pre:

The range [first, last) shall not overlap the range [result, result + (last - first)).

template<typename DerivedPolicy, typename ForwardIterator, typename InputIterator, typename Predicate> __host__ __device__ ForwardIterator remove_if (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last, InputIterator stencil, Predicate pred)

remove_if removes from the range [first, last) every element x such that pred(x) is true. That is, remove_if returns an iterator new_last such that the range [first, new_last) contains no elements for which pred of the corresponding stencil value is true. The iterators in the range [new_last,last) are all still dereferenceable, but the elements that they point to are unspecified. remove_if is stable, meaning that the relative order of elements that are not removed is unchanged.

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

The following code snippet demonstrates how to use remove_if to remove specific elements from an array of integers using the thrust::host execution policy for parallelization:

#include <thrust/remove.h>
#include <thrust/execution_policy.h>
...
const int N = 6;
int A[N] = {1, 4, 2, 8, 5, 7};
int S[N] = {0, 1, 1, 1, 0, 0};

int *new_end = thrust::remove_if(thrust::host, A, A + N, S, thrust::identity<int>());
// The first three values of A are now {1, 5, 7}
// Values beyond new_end are unspecified

See also

remove

See also

remove_copy

See also

remove_copy_if

Note

The range [first, last) is not permitted to overlap with the range [stencil, stencil + (last - first)).

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the range of interest.

  • last – The end of the range of interest.

  • stencil – The beginning of the stencil sequence.

  • pred – A predicate to evaluate for each element of the range [stencil, stencil + (last - first)). Elements for which pred evaluates to true are removed from the sequence [first, last)

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator and ForwardIterator is mutable.

  • InputIterator – is a model of Input Iterator, and InputIterator's value_type is convertible to Predicate's argument_type.

  • Predicate – is a model of Predicate.

Returns:

A ForwardIterator pointing to the end of the resulting range of elements for which pred evaluated to true.

Pre:

The range [first, last) shall not overlap the range [result, result + (last - first)).

Pre:

The range [stencil, stencil + (last - first)) shall not overlap the range [result, result + (last - first)).

template<typename ForwardIterator, typename InputIterator, typename Predicate>
ForwardIterator remove_if(ForwardIterator first, ForwardIterator last, InputIterator stencil, Predicate pred)#

remove_if removes from the range [first, last) every element x such that pred(x) is true. That is, remove_if returns an iterator new_last such that the range [first, new_last) contains no elements for which pred of the corresponding stencil value is true. The iterators in the range [new_last,last) are all still dereferenceable, but the elements that they point to are unspecified. remove_if is stable, meaning that the relative order of elements that are not removed is unchanged.

The following code snippet demonstrates how to use remove_if to remove specific elements from an array of integers.

#include <thrust/remove.h>
...
const int N = 6;
int A[N] = {1, 4, 2, 8, 5, 7};
int S[N] = {0, 1, 1, 1, 0, 0};

int *new_end = thrust::remove_if(A, A + N, S, thrust::identity<int>());
// The first three values of A are now {1, 5, 7}
// Values beyond new_end are unspecified

See also

remove

See also

remove_copy

See also

remove_copy_if

Note

The range [first, last) is not permitted to overlap with the range [stencil, stencil + (last - first)).

Parameters:
  • first – The beginning of the range of interest.

  • last – The end of the range of interest.

  • stencil – The beginning of the stencil sequence.

  • pred – A predicate to evaluate for each element of the range [stencil, stencil + (last - first)). Elements for which pred evaluates to true are removed from the sequence [first, last)

Template Parameters:
  • ForwardIterator – is a model of Forward Iterator and ForwardIterator is mutable.

  • InputIterator – is a model of Input Iterator, and InputIterator's value_type is convertible to Predicate's argument_type.

  • Predicate – is a model of Predicate.

Returns:

A ForwardIterator pointing to the end of the resulting range of elements for which pred evaluated to true.

Pre:

The range [first, last) shall not overlap the range [result, result + (last - first)).

Pre:

The range [stencil, stencil + (last - first)) shall not overlap the range [result, result + (last - first)).

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename OutputIterator, typename Predicate> __host__ __device__ OutputIterator remove_copy_if (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 first, InputIterator1 last, InputIterator2 stencil, OutputIterator result, Predicate pred)

remove_copy_if copies elements from the range [first,last) to a range beginning at result, except that elements for which pred of the corresponding stencil value is true are not copied. The return value is the end of the resulting range. This operation is stable, meaning that the relative order of the elements that are copied is the same as the range [first,last).

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

The following code snippet demonstrates how to use remove_copy_if to copy a sequence of numbers to an output range while omitting specific elements using the thrust::host execution policy for parallelization.

#include <thrust/remove.h>
#include <thrust/execution_policy.h>
...
const int N = 6;
int V[N] = {-2, 0, -1, 0, 1, 2};
int S[N] = { 1, 1,  0, 1, 0, 1};
int result[2];
thrust::remove_copy_if(thrust::host, V, V + N, S, result, thrust::identity<int>());
// V remains {-2, 0, -1, 0, 1, 2}
// result is now {-1, 1}

See also

remove

See also

remove_copy

See also

remove_if

See also

copy_if

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the range of interest.

  • last – The end of the range of interest.

  • stencil – The beginning of the stencil sequence.

  • result – The resulting range is copied to the sequence beginning at this location.

  • pred – A predicate to evaluate for each element of the range [first,last). Elements for which pred evaluates to false are not copied to the resulting sequence.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator, InputIterator1's value_type is convertible to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, and InputIterator2's value_type is convertible to Predicate's argument_type.

  • OutputIterator – is a model of Output Iterator.

  • Predicate – is a model of Predicate.

Returns:

An OutputIterator pointing to the end of the resulting range.

Pre:

The range [stencil, stencil + (last - first)) shall not overlap the range [result, result + (last - first)).

template<typename InputIterator1, typename InputIterator2, typename OutputIterator, typename Predicate>
OutputIterator remove_copy_if(InputIterator1 first, InputIterator1 last, InputIterator2 stencil, OutputIterator result, Predicate pred)#

remove_copy_if copies elements from the range [first,last) to a range beginning at result, except that elements for which pred of the corresponding stencil value is true are not copied. The return value is the end of the resulting range. This operation is stable, meaning that the relative order of the elements that are copied is the same as the range [first,last).

The following code snippet demonstrates how to use remove_copy_if to copy a sequence of numbers to an output range while omitting specific elements.

#include <thrust/remove.h>
...
const int N = 6;
int V[N] = {-2, 0, -1, 0, 1, 2};
int S[N] = { 1, 1,  0, 1, 0, 1};
int result[2];
thrust::remove_copy_if(V, V + N, S, result, thrust::identity<int>());
// V remains {-2, 0, -1, 0, 1, 2}
// result is now {-1, 1}

See also

remove

See also

remove_copy

See also

remove_if

See also

copy_if

Parameters:
  • first – The beginning of the range of interest.

  • last – The end of the range of interest.

  • stencil – The beginning of the stencil sequence.

  • result – The resulting range is copied to the sequence beginning at this location.

  • pred – A predicate to evaluate for each element of the range [first,last). Elements for which pred evaluates to false are not copied to the resulting sequence.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator, InputIterator1's value_type is convertible to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, and InputIterator2's value_type is convertible to Predicate's argument_type.

  • OutputIterator – is a model of Output Iterator.

  • Predicate – is a model of Predicate.

Returns:

An OutputIterator pointing to the end of the resulting range.

Pre:

The range [stencil, stencil + (last - first)) shall not overlap the range [result, result + (last - first)).

template<typename DerivedPolicy, typename ForwardIterator> __host__ __device__ ForwardIterator unique (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last)

For each group of consecutive elements in the range [first, last) with the same value, unique removes all but the first element of the group. The return value is an iterator new_last such that no two consecutive elements in the range [first, new_last) are equal. The iterators in the range [new_last, last) are all still dereferenceable, but the elements that they point to are unspecified. unique is stable, meaning that the relative order of elements that are not removed is unchanged.

This version of unique uses operator== to test for equality.

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

The following code snippet demonstrates how to use unique to compact a sequence of numbers to remove consecutive duplicates using the thrust::host execution policy for parallelization:

#include <thrust/unique.h>
#include <thrust/execution_policy.h>
...
const int N = 7;
int A[N] = {1, 3, 3, 3, 2, 2, 1};
int *new_end = thrust::unique(thrust::host, A, A + N);
// The first four values of A are now {1, 3, 2, 1}
// Values beyond new_end are unspecified.

See also

unique_copy

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the input range.

  • last – The end of the input range.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator, and ForwardIterator is mutable, and ForwardIterator's value_type is a model of Equality Comparable.

Returns:

The end of the unique range [first, new_last).

template<typename ForwardIterator>
ForwardIterator unique(ForwardIterator first, ForwardIterator last)#

For each group of consecutive elements in the range [first, last) with the same value, unique removes all but the first element of the group. The return value is an iterator new_last such that no two consecutive elements in the range [first, new_last) are equal. The iterators in the range [new_last, last) are all still dereferenceable, but the elements that they point to are unspecified. unique is stable, meaning that the relative order of elements that are not removed is unchanged.

This version of unique uses operator== to test for equality.

The following code snippet demonstrates how to use unique to compact a sequence of numbers to remove consecutive duplicates.

#include <thrust/unique.h>
...
const int N = 7;
int A[N] = {1, 3, 3, 3, 2, 2, 1};
int *new_end = thrust::unique(A, A + N);
// The first four values of A are now {1, 3, 2, 1}
// Values beyond new_end are unspecified.

See also

unique_copy

Parameters:
  • first – The beginning of the input range.

  • last – The end of the input range.

Template Parameters:

ForwardIterator – is a model of Forward Iterator, and ForwardIterator is mutable, and ForwardIterator's value_type is a model of Equality Comparable.

Returns:

The end of the unique range [first, new_last).

template<typename DerivedPolicy, typename ForwardIterator, typename BinaryPredicate> __host__ __device__ ForwardIterator unique (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last, BinaryPredicate binary_pred)

For each group of consecutive elements in the range [first, last) with the same value, unique removes all but the first element of the group. The return value is an iterator new_last such that no two consecutive elements in the range [first, new_last) are equal. The iterators in the range [new_last, last) are all still dereferenceable, but the elements that they point to are unspecified. unique is stable, meaning that the relative order of elements that are not removed is unchanged.

This version of unique uses the function object binary_pred to test for equality.

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

The following code snippet demonstrates how to use unique to compact a sequence of numbers to remove consecutive duplicates using the thrust::host execution policy for parallelization:

#include <thrust/unique.h>
#include <thrust/execution_policy.h>
...
const int N = 7;
int A[N] = {1, 3, 3, 3, 2, 2, 1};
int *new_end = thrust::unique(thrust::host, A, A + N, thrust::equal_to<int>());
// The first four values of A are now {1, 3, 2, 1}
// Values beyond new_end are unspecified.

See also

unique_copy

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the input range.

  • last – The end of the input range.

  • binary_pred – The binary predicate used to determine equality.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator, and ForwardIterator is mutable, and ForwardIterator's value_type is convertible to BinaryPredicate's first_argument_type and to BinaryPredicate's second_argument_type.

  • BinaryPredicate – is a model of Binary Predicate.

Returns:

The end of the unique range [first, new_last)

template<typename ForwardIterator, typename BinaryPredicate>
ForwardIterator unique(ForwardIterator first, ForwardIterator last, BinaryPredicate binary_pred)#

For each group of consecutive elements in the range [first, last) with the same value, unique removes all but the first element of the group. The return value is an iterator new_last such that no two consecutive elements in the range [first, new_last) are equal. The iterators in the range [new_last, last) are all still dereferenceable, but the elements that they point to are unspecified. unique is stable, meaning that the relative order of elements that are not removed is unchanged.

This version of unique uses the function object binary_pred to test for equality.

The following code snippet demonstrates how to use unique to compact a sequence of numbers to remove consecutive duplicates.

#include <thrust/unique.h>
...
const int N = 7;
int A[N] = {1, 3, 3, 3, 2, 2, 1};
int *new_end = thrust::unique(A, A + N, thrust::equal_to<int>());
// The first four values of A are now {1, 3, 2, 1}
// Values beyond new_end are unspecified.

See also

unique_copy

Parameters:
  • first – The beginning of the input range.

  • last – The end of the input range.

  • binary_pred – The binary predicate used to determine equality.

Template Parameters:
  • ForwardIterator – is a model of Forward Iterator, and ForwardIterator is mutable, and ForwardIterator's value_type is convertible to BinaryPredicate's first_argument_type and to BinaryPredicate's second_argument_type.

  • BinaryPredicate – is a model of Binary Predicate.

Returns:

The end of the unique range [first, new_last)

template<typename DerivedPolicy, typename InputIterator, typename OutputIterator> __host__ __device__ OutputIterator unique_copy (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator first, InputIterator last, OutputIterator result)

unique_copy copies elements from the range [first, last) to a range beginning with result, except that in a consecutive group of duplicate elements only the first one is copied. The return value is the end of the range to which the elements are copied.

The reason there are two different versions of unique_copy is that there are two different definitions of what it means for a consecutive group of elements to be duplicates. In the first version, the test is simple equality: the elements in a range [f, l) are duplicates if, for every iterator i in the range, either i == f or else *i == *(i-1). In the second, the test is an arbitrary BinaryPredicate binary_pred: the elements in [f, l) are duplicates if, for every iterator i in the range, either i == f or else binary_pred(*i, *(i-1)) is true.

This version of unique_copy uses operator== to test for equality.

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

The following code snippet demonstrates how to use unique_copy to compact a sequence of numbers to remove consecutive duplicates using the thrust::host execution policy for parallelization:

#include <thrust/unique.h>
#include <thrust/execution_policy.h>
...
const int N = 7;
int A[N] = {1, 3, 3, 3, 2, 2, 1};
int B[N];
int *result_end = thrust::unique_copy(thrust::host, A, A + N, B);
// The first four values of B are now {1, 3, 2, 1} and (result_end - B) is 4
// Values beyond result_end are unspecified

See also

unique

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.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator – is a model of Input Iterator, and InputIterator's value_type is a model of Equality Comparable.

  • OutputIterator – is a model of Output Iterator and and InputIterator's value_type is convertible to OutputIterator's value_type.

Returns:

The end of the unique range [result, result_end).

Pre:

The range [first,last) and the range [result, result + (last - first)) shall not overlap.

template<typename InputIterator, typename OutputIterator>
OutputIterator unique_copy(InputIterator first, InputIterator last, OutputIterator result)#

unique_copy copies elements from the range [first, last) to a range beginning with result, except that in a consecutive group of duplicate elements only the first one is copied. The return value is the end of the range to which the elements are copied.

The reason there are two different versions of unique_copy is that there are two different definitions of what it means for a consecutive group of elements to be duplicates. In the first version, the test is simple equality: the elements in a range [f, l) are duplicates if, for every iterator i in the range, either i == f or else *i == *(i-1). In the second, the test is an arbitrary BinaryPredicate binary_pred: the elements in [f, l) are duplicates if, for every iterator i in the range, either i == f or else binary_pred(*i, *(i-1)) is true.

This version of unique_copy uses operator== to test for equality.

The following code snippet demonstrates how to use unique_copy to compact a sequence of numbers to remove consecutive duplicates.

#include <thrust/unique.h>
...
const int N = 7;
int A[N] = {1, 3, 3, 3, 2, 2, 1};
int B[N];
int *result_end = thrust::unique_copy(A, A + N, B);
// The first four values of B are now {1, 3, 2, 1} and (result_end - B) is 4
// Values beyond result_end are unspecified

See also

unique

Parameters:
  • first – The beginning of the input range.

  • last – The end of the input range.

  • result – The beginning of the output range.

Template Parameters:
  • InputIterator – is a model of Input Iterator, and InputIterator's value_type is a model of Equality Comparable.

  • OutputIterator – is a model of Output Iterator and and InputIterator's value_type is convertible to OutputIterator's value_type.

Returns:

The end of the unique range [result, result_end).

Pre:

The range [first,last) and the range [result, result + (last - first)) shall not overlap.

template<typename DerivedPolicy, typename InputIterator, typename OutputIterator, typename BinaryPredicate> __host__ __device__ OutputIterator unique_copy (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate binary_pred)

unique_copy copies elements from the range [first, last) to a range beginning with result, except that in a consecutive group of duplicate elements only the first one is copied. The return value is the end of the range to which the elements are copied.

This version of unique_copy uses the function object binary_pred to test for equality.

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

The following code snippet demonstrates how to use unique_copy to compact a sequence of numbers to remove consecutive duplicates using the thrust::host execution policy for parallelization:

#include <thrust/unique.h>
#include <thrust/execution_policy.h>
...
const int N = 7;
int A[N] = {1, 3, 3, 3, 2, 2, 1};
int B[N];
int *result_end = thrust::unique_copy(thrust::host, A, A + N, B, thrust::equal_to<int>());
// The first four values of B are now {1, 3, 2, 1} and (result_end - B) is 4
// Values beyond result_end are unspecified.

See also

unique

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_pred – The binary predicate used to determine equality.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator – is a model of Input Iterator, and InputIterator's value_type is a model of Equality Comparable.

  • OutputIterator – is a model of Output Iterator and and InputIterator's value_type is convertible to OutputIterator's value_type.

  • BinaryPredicate – is a model of Binary Predicate.

Returns:

The end of the unique range [result, result_end).

Pre:

The range [first,last) and the range [result, result + (last - first)) shall not overlap.

template<typename InputIterator, typename OutputIterator, typename BinaryPredicate>
OutputIterator unique_copy(InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate binary_pred)#

unique_copy copies elements from the range [first, last) to a range beginning with result, except that in a consecutive group of duplicate elements only the first one is copied. The return value is the end of the range to which the elements are copied.

This version of unique_copy uses the function object binary_pred to test for equality.

The following code snippet demonstrates how to use unique_copy to compact a sequence of numbers to remove consecutive duplicates.

#include <thrust/unique.h>
...
const int N = 7;
int A[N] = {1, 3, 3, 3, 2, 2, 1};
int B[N];
int *result_end = thrust::unique_copy(A, A + N, B, thrust::equal_to<int>());
// The first four values of B are now {1, 3, 2, 1} and (result_end - B) is 4
// Values beyond result_end are unspecified.

See also

unique

Parameters:
  • first – The beginning of the input range.

  • last – The end of the input range.

  • result – The beginning of the output range.

  • binary_pred – The binary predicate used to determine equality.

Template Parameters:
Returns:

The end of the unique range [result, result_end).

Pre:

The range [first,last) and the range [result, result + (last - first)) shall not overlap.

template<typename DerivedPolicy, typename ForwardIterator1, typename ForwardIterator2> __host__ __device__ thrust::pair< ForwardIterator1, ForwardIterator2 > unique_by_key (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator1 keys_first, ForwardIterator1 keys_last, ForwardIterator2 values_first)

unique_by_key is a generalization of unique to key-value pairs. For each group of consecutive keys in the range [keys_first, keys_last) that are equal, unique_by_key removes all but the first element of the group. Similarly, the corresponding values in the range [values_first, values_first + (keys_last - keys_first)) are also removed.

The return value is a pair of iterators (new_keys_last,new_values_last) such that no two consecutive elements in the range [keys_first, new_keys_last) are equal.

This version of unique_by_key uses operator== to test for equality and project1st to reduce values with equal keys.

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

The following code snippet demonstrates how to use unique_by_key to compact a sequence of key/value pairs to remove consecutive duplicates using the thrust::host execution policy for parallelization:

#include <thrust/unique.h>
#include <thrust/execution_policy.h>
...
const int N = 7;
int A[N] = {1, 3, 3, 3, 2, 2, 1}; // keys
int B[N] = {9, 8, 7, 6, 5, 4, 3}; // values

thrust::pair<int*,int*> new_end;
new_end = thrust::unique_by_key(thrust::host, A, A + N, B);

// The first four keys in A are now {1, 3, 2, 1} and new_end.first - A is 4.
// The first four values in B are now {9, 8, 5, 3} and new_end.second - B is 4.

See also

unique

See also

reduce_by_key

Parameters:
  • exec – The execution policy to use for parallelization.

  • keys_first – The beginning of the key range.

  • keys_last – The end of the key range.

  • values_first – The beginning of the value range.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator1 – is a model of Forward Iterator, and ForwardIterator1 is mutable, and ForwardIterator's value_type is a model of Equality Comparable.

  • ForwardIterator2 – is a model of Forward Iterator, and ForwardIterator2 is mutable.

Returns:

A pair of iterators at end of the ranges [key_first, keys_new_last) and [values_first, values_new_last).

Pre:

The range [keys_first, keys_last) and the range [values_first, values_first + (keys_last - keys_first)) shall not overlap.

template<typename ForwardIterator1, typename ForwardIterator2>
thrust::pair<ForwardIterator1, ForwardIterator2> unique_by_key(ForwardIterator1 keys_first, ForwardIterator1 keys_last, ForwardIterator2 values_first)#

unique_by_key is a generalization of unique to key-value pairs. For each group of consecutive keys in the range [keys_first, keys_last) that are equal, unique_by_key removes all but the first element of the group. Similarly, the corresponding values in the range [values_first, values_first + (keys_last - keys_first)) are also removed.

The return value is a pair of iterators (new_keys_last,new_values_last) such that no two consecutive elements in the range [keys_first, new_keys_last) are equal.

This version of unique_by_key uses operator== to test for equality and project1st to reduce values with equal keys.

The following code snippet demonstrates how to use unique_by_key to compact a sequence of key/value pairs to remove consecutive duplicates.

#include <thrust/unique.h>
...
const int N = 7;
int A[N] = {1, 3, 3, 3, 2, 2, 1}; // keys
int B[N] = {9, 8, 7, 6, 5, 4, 3}; // values

thrust::pair<int*,int*> new_end;
new_end = thrust::unique_by_key(A, A + N, B);

// The first four keys in A are now {1, 3, 2, 1} and new_end.first - A is 4.
// The first four values in B are now {9, 8, 5, 3} and new_end.second - B is 4.

See also

unique

See also

reduce_by_key

Parameters:
  • keys_first – The beginning of the key range.

  • keys_last – The end of the key range.

  • values_first – The beginning of the value range.

Template Parameters:
Returns:

A pair of iterators at end of the ranges [key_first, keys_new_last) and [values_first, values_new_last).

Pre:

The range [keys_first, keys_last) and the range [values_first, values_first + (keys_last - keys_first)) shall not overlap.

template<typename DerivedPolicy, typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate> __host__ __device__ thrust::pair< ForwardIterator1, ForwardIterator2 > unique_by_key (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator1 keys_first, ForwardIterator1 keys_last, ForwardIterator2 values_first, BinaryPredicate binary_pred)

unique_by_key is a generalization of unique to key-value pairs. For each group of consecutive keys in the range [keys_first, keys_last) that are equal, unique_by_key removes all but the first element of the group. Similarly, the corresponding values in the range [values_first, values_first + (keys_last - keys_first)) are also removed.

This version of unique_by_key uses the function object binary_pred to test for equality and project1st to reduce values with equal keys.

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

The following code snippet demonstrates how to use unique_by_key to compact a sequence of key/value pairs to remove consecutive duplicates using the thrust::host execution policy for parallelization:

#include <thrust/unique.h>
#include <thrust/execution_policy.h>
...
const int N = 7;
int A[N] = {1, 3, 3, 3, 2, 2, 1}; // keys
int B[N] = {9, 8, 7, 6, 5, 4, 3}; // values

thrust::pair<int*,int*> new_end;
thrust::equal_to<int> binary_pred;
new_end = thrust::unique_by_key(thrust::host, keys, keys + N, values, binary_pred);

// The first four keys in A are now {1, 3, 2, 1} and new_end.first - A is 4.
// The first four values in B are now {9, 8, 5, 3} and new_end.second - B is 4.

See also

unique

See also

reduce_by_key

Parameters:
  • exec – The execution policy to use for parallelization.

  • keys_first – The beginning of the key range.

  • keys_last – The end of the key range.

  • values_first – The beginning of the value range.

  • binary_pred – The binary predicate used to determine equality.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator1 – is a model of Forward Iterator, and ForwardIterator1 is mutable, and ForwardIterator's value_type is a model of Equality Comparable.

  • ForwardIterator2 – is a model of Forward Iterator, and ForwardIterator2 is mutable.

  • BinaryPredicate – is a model of Binary Predicate.

Returns:

The end of the unique range [first, new_last).

Pre:

The range [keys_first, keys_last) and the range [values_first, values_first + (keys_last - keys_first)) shall not overlap.

template<typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>
thrust::pair<ForwardIterator1, ForwardIterator2> unique_by_key(ForwardIterator1 keys_first, ForwardIterator1 keys_last, ForwardIterator2 values_first, BinaryPredicate binary_pred)#

unique_by_key is a generalization of unique to key-value pairs. For each group of consecutive keys in the range [keys_first, keys_last) that are equal, unique_by_key removes all but the first element of the group. Similarly, the corresponding values in the range [values_first, values_first + (keys_last - keys_first)) are also removed.

This version of unique_by_key uses the function object binary_pred to test for equality and project1st to reduce values with equal keys.

The following code snippet demonstrates how to use unique_by_key to compact a sequence of key/value pairs to remove consecutive duplicates.

#include <thrust/unique.h>
...
const int N = 7;
int A[N] = {1, 3, 3, 3, 2, 2, 1}; // keys
int B[N] = {9, 8, 7, 6, 5, 4, 3}; // values

thrust::pair<int*,int*> new_end;
thrust::equal_to<int> binary_pred;
new_end = thrust::unique_by_key(keys, keys + N, values, binary_pred);

// The first four keys in A are now {1, 3, 2, 1} and new_end.first - A is 4.
// The first four values in B are now {9, 8, 5, 3} and new_end.second - B is 4.

See also

unique

See also

reduce_by_key

Parameters:
  • keys_first – The beginning of the key range.

  • keys_last – The end of the key range.

  • values_first – The beginning of the value range.

  • binary_pred – The binary predicate used to determine equality.

Template Parameters:
Returns:

The end of the unique range [first, new_last).

Pre:

The range [keys_first, keys_last) and the range [values_first, values_first + (keys_last - keys_first)) shall not overlap.

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename OutputIterator1, typename OutputIterator2> __host__ __device__ thrust::pair< OutputIterator1, OutputIterator2 > unique_by_key_copy (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 keys_first, InputIterator1 keys_last, InputIterator2 values_first, OutputIterator1 keys_result, OutputIterator2 values_result)

unique_by_key_copy is a generalization of unique_copy to key-value pairs. For each group of consecutive keys in the range [keys_first, keys_last) that are equal, unique_by_key_copy copies the first element of the group to a range beginning with keys_result and the corresponding values from the range [values_first, values_first + (keys_last - keys_first)) are copied to a range beginning with values_result.

This version of unique_by_key_copy uses operator== to test for equality and project1st to reduce values with equal keys.

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

The following code snippet demonstrates how to use unique_by_key_copy to compact a sequence of key/value pairs and with equal keys using the thrust::host execution policy for parallelization:

#include <thrust/unique.h>
#include <thrust/execution_policy.h>
...
const int N = 7;
int A[N] = {1, 3, 3, 3, 2, 2, 1}; // input keys
int B[N] = {9, 8, 7, 6, 5, 4, 3}; // input values
int C[N];                         // output keys
int D[N];                         // output values

thrust::pair<int*,int*> new_end;
new_end = thrust::unique_by_key_copy(thrust::host, A, A + N, B, C, D);

// The first four keys in C are now {1, 3, 2, 1} and new_end.first - C is 4.
// The first four values in D are now {9, 8, 5, 3} and new_end.second - D is 4.

See also

unique_copy

See also

unique_by_key

See also

reduce_by_key

Parameters:
  • exec – The execution policy to use for parallelization.

  • keys_first – The beginning of the input key range.

  • keys_last – The end of the input key range.

  • values_first – The beginning of the input value range.

  • keys_result – The beginning of the output key range.

  • values_result – The beginning of the output value range.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator,

  • InputIterator2 – is a model of Input Iterator,

  • OutputIterator1 – is a model of Output Iterator and and InputIterator1's value_type is convertible to OutputIterator1's value_type.

  • OutputIterator2 – is a model of Output Iterator and and InputIterator2's value_type is convertible to OutputIterator2's value_type.

Returns:

A pair of iterators at end of the ranges [keys_result, keys_result_last) and [values_result, values_result_last).

Pre:

The input ranges shall not overlap either output range.

template<typename InputIterator1, typename InputIterator2, typename OutputIterator1, typename OutputIterator2>
thrust::pair<OutputIterator1, OutputIterator2> unique_by_key_copy(InputIterator1 keys_first, InputIterator1 keys_last, InputIterator2 values_first, OutputIterator1 keys_result, OutputIterator2 values_result)#

unique_by_key_copy is a generalization of unique_copy to key-value pairs. For each group of consecutive keys in the range [keys_first, keys_last) that are equal, unique_by_key_copy copies the first element of the group to a range beginning with keys_result and the corresponding values from the range [values_first, values_first + (keys_last - keys_first)) are copied to a range beginning with values_result.

This version of unique_by_key_copy uses operator== to test for equality and project1st to reduce values with equal keys.

The following code snippet demonstrates how to use unique_by_key_copy to compact a sequence of key/value pairs and with equal keys.

#include <thrust/unique.h>
...
const int N = 7;
int A[N] = {1, 3, 3, 3, 2, 2, 1}; // input keys
int B[N] = {9, 8, 7, 6, 5, 4, 3}; // input values
int C[N];                         // output keys
int D[N];                         // output values

thrust::pair<int*,int*> new_end;
new_end = thrust::unique_by_key_copy(A, A + N, B, C, D);

// The first four keys in C are now {1, 3, 2, 1} and new_end.first - C is 4.
// The first four values in D are now {9, 8, 5, 3} and new_end.second - D is 4.

See also

unique_copy

See also

unique_by_key

See also

reduce_by_key

Parameters:
  • keys_first – The beginning of the input key range.

  • keys_last – The end of the input key range.

  • values_first – The beginning of the input value range.

  • keys_result – The beginning of the output key range.

  • values_result – The beginning of the output value range.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator,

  • InputIterator2 – is a model of Input Iterator,

  • OutputIterator1 – is a model of Output Iterator and and InputIterator1's value_type is convertible to OutputIterator1's value_type.

  • OutputIterator2 – is a model of Output Iterator and and InputIterator2's value_type is convertible to OutputIterator2's value_type.

Returns:

A pair of iterators at end of the ranges [keys_result, keys_result_last) and [values_result, values_result_last).

Pre:

The input ranges shall not overlap either output range.

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename OutputIterator1, typename OutputIterator2, typename BinaryPredicate> __host__ __device__ thrust::pair< OutputIterator1, OutputIterator2 > unique_by_key_copy (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 keys_first, InputIterator1 keys_last, InputIterator2 values_first, OutputIterator1 keys_result, OutputIterator2 values_result, BinaryPredicate binary_pred)

unique_by_key_copy is a generalization of unique_copy to key-value pairs. For each group of consecutive keys in the range [keys_first, keys_last) that are equal, unique_by_key_copy copies the first element of the group to a range beginning with keys_result and the corresponding values from the range [values_first, values_first + (keys_last - keys_first)) are copied to a range beginning with values_result.

This version of unique_by_key_copy uses the function object binary_pred to test for equality and project1st to reduce values with equal keys.

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

The following code snippet demonstrates how to use unique_by_key_copy to compact a sequence of key/value pairs and with equal keys using the thrust::host execution policy for parallelization:

#include <thrust/unique.h>
#include <thrust/execution_policy.h>
...
const int N = 7;
int A[N] = {1, 3, 3, 3, 2, 2, 1}; // input keys
int B[N] = {9, 8, 7, 6, 5, 4, 3}; // input values
int C[N];                         // output keys
int D[N];                         // output values

thrust::pair<int*,int*> new_end;
thrust::equal_to<int> binary_pred;
new_end = thrust::unique_by_key_copy(thrust::host, A, A + N, B, C, D, binary_pred);

// The first four keys in C are now {1, 3, 2, 1} and new_end.first - C is 4.
// The first four values in D are now {9, 8, 5, 3} and new_end.second - D is 4.

See also

unique_copy

See also

unique_by_key

See also

reduce_by_key

Parameters:
  • exec – The execution policy to use for parallelization.

  • keys_first – The beginning of the input key range.

  • keys_last – The end of the input key range.

  • values_first – The beginning of the input value range.

  • keys_result – The beginning of the output key range.

  • values_result – The beginning of the output value range.

  • binary_pred – The binary predicate used to determine equality.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator,

  • InputIterator2 – is a model of Input Iterator,

  • OutputIterator1 – is a model of Output Iterator and and InputIterator1's value_type is convertible to OutputIterator1's value_type.

  • OutputIterator2 – is a model of Output Iterator and and InputIterator2's value_type is convertible to OutputIterator2's value_type.

  • BinaryPredicate – is a model of Binary Predicate.

Returns:

A pair of iterators at end of the ranges [keys_result, keys_result_last) and [values_result, values_result_last).

Pre:

The input ranges shall not overlap either output range.

template<typename InputIterator1, typename InputIterator2, typename OutputIterator1, typename OutputIterator2, typename BinaryPredicate>
thrust::pair<OutputIterator1, OutputIterator2> unique_by_key_copy(InputIterator1 keys_first, InputIterator1 keys_last, InputIterator2 values_first, OutputIterator1 keys_result, OutputIterator2 values_result, BinaryPredicate binary_pred)#

unique_by_key_copy is a generalization of unique_copy to key-value pairs. For each group of consecutive keys in the range [keys_first, keys_last) that are equal, unique_by_key_copy copies the first element of the group to a range beginning with keys_result and the corresponding values from the range [values_first, values_first + (keys_last - keys_first)) are copied to a range beginning with values_result.

This version of unique_by_key_copy uses the function object binary_pred to test for equality and project1st to reduce values with equal keys.

The following code snippet demonstrates how to use unique_by_key_copy to compact a sequence of key/value pairs and with equal keys.

#include <thrust/unique.h>
...
const int N = 7;
int A[N] = {1, 3, 3, 3, 2, 2, 1}; // input keys
int B[N] = {9, 8, 7, 6, 5, 4, 3}; // input values
int C[N];                         // output keys
int D[N];                         // output values

thrust::pair<int*,int*> new_end;
thrust::equal_to<int> binary_pred;
new_end = thrust::unique_by_key_copy(A, A + N, B, C, D, binary_pred);

// The first four keys in C are now {1, 3, 2, 1} and new_end.first - C is 4.
// The first four values in D are now {9, 8, 5, 3} and new_end.second - D is 4.

See also

unique_copy

See also

unique_by_key

See also

reduce_by_key

Parameters:
  • keys_first – The beginning of the input key range.

  • keys_last – The end of the input key range.

  • values_first – The beginning of the input value range.

  • keys_result – The beginning of the output key range.

  • values_result – The beginning of the output value range.

  • binary_pred – The binary predicate used to determine equality.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator,

  • InputIterator2 – is a model of Input Iterator,

  • OutputIterator1 – is a model of Output Iterator and and InputIterator1's value_type is convertible to OutputIterator1's value_type.

  • OutputIterator2 – is a model of Output Iterator and and InputIterator2's value_type is convertible to OutputIterator2's value_type.

  • BinaryPredicate – is a model of Binary Predicate.

Returns:

A pair of iterators at end of the ranges [keys_result, keys_result_last) and [values_result, values_result_last).

Pre:

The input ranges shall not overlap either output range.

template<typename DerivedPolicy, typename ForwardIterator, typename BinaryPredicate> __host__ __device__ thrust::iterator_traits< ForwardIterator >::difference_type unique_count (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last, BinaryPredicate binary_pred)

unique_count counts runs of equal elements in the range [first, last) with the same value,

This version of unique_count uses the function object binary_pred to test for equality.

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

The following code snippet demonstrates how to use unique_count to determine a number of runs of equal elements using the thrust::host execution policy for parallelization:

#include <thrust/unique.h>
#include <thrust/execution_policy.h>
...
const int N = 7;
int A[N] = {1, 3, 3, 3, 2, 2, 1};
int count = thrust::unique_count(thrust::host, A, A + N, thrust::equal_to<int>());
// count is now 4

See also

unique_copy

See also

reduce_by_key_copy

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the input range.

  • last – The end of the input range.

  • binary_pred – The binary predicate used to determine equality.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator, and ForwardIterator's value_type is convertible to BinaryPredicate's first_argument_type and to BinaryPredicate's second_argument_type.

  • BinaryPredicate – is a model of Binary Predicate.

Returns:

The number of runs of equal elements in [first, new_last)

template<typename DerivedPolicy, typename ForwardIterator> __host__ __device__ thrust::iterator_traits< ForwardIterator >::difference_type unique_count (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last)

unique_count counts runs of equal elements in the range [first, last) with the same value,

This version of unique_count uses operator== to test for equality.

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

The following code snippet demonstrates how to use unique_count to determine the number of runs of equal elements using the thrust::host execution policy for parallelization:

#include <thrust/unique.h>
#include <thrust/execution_policy.h>
...
const int N = 7;
int A[N] = {1, 3, 3, 3, 2, 2, 1};
int count = thrust::unique_count(thrust::host, A, A + N);
// count is now 4

See also

unique_copy

See also

reduce_by_key_copy

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the input range.

  • last – The end of the input range.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator, and ForwardIterator's value_type is convertible to BinaryPredicate's first_argument_type and to BinaryPredicate's second_argument_type.

  • BinaryPredicate – is a model of Binary Predicate.

Returns:

The number of runs of equal elements in [first, new_last)

template<typename ForwardIterator, typename BinaryPredicate> __host__ __device__ thrust::iterator_traits< ForwardIterator >::difference_type unique_count (ForwardIterator first, ForwardIterator last, BinaryPredicate binary_pred)

unique_count counts runs of equal elements in the range [first, last) with the same value,

This version of unique_count uses the function object binary_pred to test for equality.

The following code snippet demonstrates how to use unique_count to determine the number of runs of equal elements:

#include <thrust/unique.h>
#include <thrust/execution_policy.h>
...
const int N = 7;
int A[N] = {1, 3, 3, 3, 2, 2, 1};
int count = thrust::unique_count(A, A + N, thrust::equal_to<int>());
// count is now 4

See also

unique_copy

See also

reduce_by_key_copy

Parameters:
  • first – The beginning of the input range.

  • last – The end of the input range.

  • binary_pred – The binary predicate used to determine equality.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator, and ForwardIterator's value_type is convertible to BinaryPredicate's first_argument_type and to BinaryPredicate's second_argument_type.

  • BinaryPredicate – is a model of Binary Predicate.

Returns:

The number of runs of equal elements in [first, new_last)

template<typename ForwardIterator> __host__ __device__ thrust::iterator_traits< ForwardIterator >::difference_type unique_count (ForwardIterator first, ForwardIterator last)

unique_count counts runs of equal elements in the range [first, last) with the same value,

This version of unique_count uses operator== to test for equality.

The following code snippet demonstrates how to use unique_count to determine the number of runs of equal elements:

#include <thrust/unique.h>
#include <thrust/execution_policy.h>
...
const int N = 7;
int A[N] = {1, 3, 3, 3, 2, 2, 1};
int count = thrust::unique_count(thrust::host, A, A + N);
// count is now 4

See also

unique_copy

See also

reduce_by_key_copy

Parameters:
  • first – The beginning of the input range.

  • last – The end of the input range.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator, and ForwardIterator's value_type is convertible to BinaryPredicate's first_argument_type and to BinaryPredicate's second_argument_type.

  • BinaryPredicate – is a model of Binary Predicate.

Returns:

The number of runs of equal elements in [first, new_last)

Functions

template<typename DerivedPolicy, typename InputIterator, typename OutputIterator> __host__ __device__ OutputIterator inclusive_scan (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator first, InputIterator last, OutputIterator result)

inclusive_scan computes an inclusive prefix sum operation. The term ‘inclusive’ means that each result includes the corresponding input operand in the partial sum. More precisely, *first is assigned to *result and the sum of *first and *(first + 1) is assigned to *(result + 1), and so on. This version of inclusive_scan assumes plus as the associative operator. When the input and output sequences are the same, the scan is performed in-place.

inclusive_scan is similar to std::partial_sum in the STL. The primary difference between the two functions is that std::partial_sum guarantees a serial summation order, while inclusive_scan requires associativity of the binary operation to parallelize the prefix sum.

Results are not deterministic for pseudo-associative operators (e.g., addition of floating-point types). Results for pseudo-associative operators may vary from run to run.

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

The following code snippet demonstrates how to use inclusive_scan to compute an in-place prefix sum using the thrust::host execution policy for parallelization:

#include <thrust/scan.h>
#include <thrust/execution_policy.h>
...

int data[6] = {1, 0, 2, 2, 1, 3};

thrust::inclusive_scan(thrust::host, data, data + 6, data); // in-place scan

// data is now {1, 1, 3, 5, 6, 9}

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.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator – is a model of Input Iterator and InputIterator's value_type is convertible to OutputIterator's value_type.

  • OutputIterator – is a model of Output Iterator, and if x and y are objects of OutputIterator's value_type, then x + y is defined. If T is OutputIterator's value_type, then T(0) is defined.

Returns:

The end of the output sequence.

Pre:

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

template<typename InputIterator, typename OutputIterator>
OutputIterator inclusive_scan(InputIterator first, InputIterator last, OutputIterator result)#

inclusive_scan computes an inclusive prefix sum operation. The term ‘inclusive’ means that each result includes the corresponding input operand in the partial sum. More precisely, *first is assigned to *result and the sum of *first and *(first + 1) is assigned to *(result + 1), and so on. This version of inclusive_scan assumes plus as the associative operator. When the input and output sequences are the same, the scan is performed in-place.

inclusive_scan is similar to std::partial_sum in the STL. The primary difference between the two functions is that std::partial_sum guarantees a serial summation order, while inclusive_scan requires associativity of the binary operation to parallelize the prefix sum.

Results are not deterministic for pseudo-associative operators (e.g., addition of floating-point types). Results for pseudo-associative operators may vary from run to run.

The following code snippet demonstrates how to use inclusive_scan

#include <thrust/scan.h>

int data[6] = {1, 0, 2, 2, 1, 3};

thrust::inclusive_scan(data, data + 6, data); // in-place scan

// data is now {1, 1, 3, 5, 6, 9}

Parameters:
  • first – The beginning of the input sequence.

  • last – The end of the input sequence.

  • result – The beginning of the output sequence.

Template Parameters:
  • InputIterator – is a model of Input Iterator and InputIterator's value_type is convertible to OutputIterator's value_type.

  • OutputIterator – is a model of Output Iterator, and if x and y are objects of OutputIterator's value_type, then x + y is defined. If T is OutputIterator's value_type, then T(0) is defined.

Returns:

The end of the output sequence.

Pre:

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

template<typename DerivedPolicy, typename InputIterator, typename OutputIterator, typename AssociativeOperator> __host__ __device__ OutputIterator inclusive_scan (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator first, InputIterator last, OutputIterator result, AssociativeOperator binary_op)

inclusive_scan computes an inclusive prefix sum operation. The term ‘inclusive’ means that each result includes the corresponding input operand in the partial sum. When the input and output sequences are the same, the scan is performed in-place.

inclusive_scan is similar to std::partial_sum in the STL. The primary difference between the two functions is that std::partial_sum guarantees a serial summation order, while inclusive_scan requires associativity of the binary operation to parallelize the prefix sum.

Results are not deterministic for pseudo-associative operators (e.g., addition of floating-point types). Results for pseudo-associative operators may vary from run to run.

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

The following code snippet demonstrates how to use inclusive_scan to compute an in-place prefix sum using the thrust::host execution policy for parallelization:

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

thrust::maximum<int> binary_op;

thrust::inclusive_scan(thrust::host, data, data + 10, data, binary_op); // in-place scan

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

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.

  • binary_op – The associatve operator used to ‘sum’ values.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator – is a model of Input Iterator and InputIterator's value_type is convertible to OutputIterator's value_type.

  • OutputIterator – is a model of Output Iterator and OutputIterator's value_type is convertible to both AssociativeOperator's first_argument_type and second_argument_type.

  • AssociativeOperator – is a model of Binary Function and AssociativeOperator's result_type is convertible to OutputIterator's value_type.

Returns:

The end of the output sequence.

Pre:

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

template<typename InputIterator, typename OutputIterator, typename AssociativeOperator>
OutputIterator inclusive_scan(InputIterator first, InputIterator last, OutputIterator result, AssociativeOperator binary_op)#

inclusive_scan computes an inclusive prefix sum operation. The term ‘inclusive’ means that each result includes the corresponding input operand in the partial sum. When the input and output sequences are the same, the scan is performed in-place.

inclusive_scan is similar to std::partial_sum in the STL. The primary difference between the two functions is that std::partial_sum guarantees a serial summation order, while inclusive_scan requires associativity of the binary operation to parallelize the prefix sum.

Results are not deterministic for pseudo-associative operators (e.g., addition of floating-point types). Results for pseudo-associative operators may vary from run to run.

The following code snippet demonstrates how to use inclusive_scan

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

thrust::maximum<int> binary_op;

thrust::inclusive_scan(data, data + 10, data, binary_op); // in-place scan

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

Parameters:
  • first – The beginning of the input sequence.

  • last – The end of the input sequence.

  • result – The beginning of the output sequence.

  • binary_op – The associatve operator used to ‘sum’ values.

Template Parameters:
  • InputIterator – is a model of Input Iterator and InputIterator's value_type is convertible to OutputIterator's value_type.

  • OutputIterator – is a model of Output Iterator and OutputIterator's value_type is convertible to both AssociativeOperator's first_argument_type and second_argument_type.

  • AssociativeOperator – is a model of Binary Function and AssociativeOperator's result_type is convertible to OutputIterator's value_type.

Returns:

The end of the output sequence.

Pre:

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

template<typename DerivedPolicy, typename InputIterator, typename OutputIterator> __host__ __device__ OutputIterator exclusive_scan (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator first, InputIterator last, OutputIterator result)

exclusive_scan computes an exclusive prefix sum operation. The term ‘exclusive’ means that each result does not include the corresponding input operand in the partial sum. More precisely, 0 is assigned to *result and the sum of 0 and *first is assigned to *(result + 1), and so on. This version of exclusive_scan assumes plus as the associative operator and 0 as the initial value. When the input and output sequences are the same, the scan is performed in-place.

Results are not deterministic for pseudo-associative operators (e.g., addition of floating-point types). Results for pseudo-associative operators may vary from run to run.

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

The following code snippet demonstrates how to use exclusive_scan to compute an in-place prefix sum using the thrust::host execution policy for parallelization:

#include <thrust/scan.h>
#include <thrust/execution_policy.h>
...

int data[6] = {1, 0, 2, 2, 1, 3};

thrust::exclusive_scan(thrust::host, data, data + 6, data); // in-place scan

// data is now {0, 1, 1, 3, 5, 6}

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.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator – is a model of Input Iterator and InputIterator's value_type is convertible to OutputIterator's value_type.

  • OutputIterator – is a model of Output Iterator, and if x and y are objects of OutputIterator's value_type, then x + y is defined. If T is OutputIterator's value_type, then T(0) is defined.

Returns:

The end of the output sequence.

Pre:

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

template<typename InputIterator, typename OutputIterator>
OutputIterator exclusive_scan(InputIterator first, InputIterator last, OutputIterator result)#

exclusive_scan computes an exclusive prefix sum operation. The term ‘exclusive’ means that each result does not include the corresponding input operand in the partial sum. More precisely, 0 is assigned to *result and the sum of 0 and *first is assigned to *(result + 1), and so on. This version of exclusive_scan assumes plus as the associative operator and 0 as the initial value. When the input and output sequences are the same, the scan is performed in-place.

Results are not deterministic for pseudo-associative operators (e.g., addition of floating-point types). Results for pseudo-associative operators may vary from run to run.

The following code snippet demonstrates how to use exclusive_scan

#include <thrust/scan.h>

int data[6] = {1, 0, 2, 2, 1, 3};

thrust::exclusive_scan(data, data + 6, data); // in-place scan

// data is now {0, 1, 1, 3, 5, 6}

Parameters:
  • first – The beginning of the input sequence.

  • last – The end of the input sequence.

  • result – The beginning of the output sequence.

Template Parameters:
  • InputIterator – is a model of Input Iterator and InputIterator's value_type is convertible to OutputIterator's value_type.

  • OutputIterator – is a model of Output Iterator, and if x and y are objects of OutputIterator's value_type, then x + y is defined. If T is OutputIterator's value_type, then T(0) is defined.

Returns:

The end of the output sequence.

Pre:

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

template<typename DerivedPolicy, typename InputIterator, typename OutputIterator, typename T> __host__ __device__ OutputIterator exclusive_scan (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator first, InputIterator last, OutputIterator result, T init)

exclusive_scan computes an exclusive prefix sum operation. The term ‘exclusive’ means that each result does not include the corresponding input operand in the partial sum. More precisely, init is assigned to *result and the sum of init and *first is assigned to *(result + 1), and so on. This version of exclusive_scan assumes plus as the associative operator but requires an initial value init. When the input and output sequences are the same, the scan is performed in-place.

Results are not deterministic for pseudo-associative operators (e.g., addition of floating-point types). Results for pseudo-associative operators may vary from run to run.

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

The following code snippet demonstrates how to use exclusive_scan to compute an in-place prefix sum using the thrust::host execution policy for parallelization:

#include <thrust/scan.h>
#include <thrust/execution_policy.h>

int data[6] = {1, 0, 2, 2, 1, 3};

thrust::exclusive_scan(thrust::host, data, data + 6, data, 4); // in-place scan

// data is now {4, 5, 5, 7, 9, 10}

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.

  • init – The initial value.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator – is a model of Input Iterator and InputIterator's value_type is convertible to OutputIterator's value_type.

  • OutputIterator – is a model of Output Iterator, and if x and y are objects of OutputIterator's value_type, then x + y is defined.

  • T – is convertible to OutputIterator's value_type.

Returns:

The end of the output sequence.

Pre:

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

template<typename InputIterator, typename OutputIterator, typename T>
OutputIterator exclusive_scan(InputIterator first, InputIterator last, OutputIterator result, T init)#

exclusive_scan computes an exclusive prefix sum operation. The term ‘exclusive’ means that each result does not include the corresponding input operand in the partial sum. More precisely, init is assigned to *result and the sum of init and *first is assigned to *(result + 1), and so on. This version of exclusive_scan assumes plus as the associative operator but requires an initial value init. When the input and output sequences are the same, the scan is performed in-place.

Results are not deterministic for pseudo-associative operators (e.g., addition of floating-point types). Results for pseudo-associative operators may vary from run to run.

The following code snippet demonstrates how to use exclusive_scan

#include <thrust/scan.h>

int data[6] = {1, 0, 2, 2, 1, 3};

thrust::exclusive_scan(data, data + 6, data, 4); // in-place scan

// data is now {4, 5, 5, 7, 9, 10}

Parameters:
  • first – The beginning of the input sequence.

  • last – The end of the input sequence.

  • result – The beginning of the output sequence.

  • init – The initial value.

Template Parameters:
  • InputIterator – is a model of Input Iterator and InputIterator's value_type is convertible to OutputIterator's value_type.

  • OutputIterator – is a model of Output Iterator, and if x and y are objects of OutputIterator's value_type, then x + y is defined.

  • T – is convertible to OutputIterator's value_type.

Returns:

The end of the output sequence.

Pre:

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

template<typename DerivedPolicy, typename InputIterator, typename OutputIterator, typename T, typename AssociativeOperator> __host__ __device__ OutputIterator exclusive_scan (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator first, InputIterator last, OutputIterator result, T init, AssociativeOperator binary_op)

exclusive_scan computes an exclusive prefix sum operation. The term ‘exclusive’ means that each result does not include the corresponding input operand in the partial sum. More precisely, init is assigned to *result and the value binary_op(init, *first) is assigned to *(result + 1), and so on. This version of the function requires both an associative operator and an initial value init. When the input and output sequences are the same, the scan is performed in-place.

Results are not deterministic for pseudo-associative operators (e.g., addition of floating-point types). Results for pseudo-associative operators may vary from run to run.

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

The following code snippet demonstrates how to use exclusive_scan to compute an in-place prefix sum using the thrust::host execution policy for parallelization:

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

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

thrust::maximum<int> binary_op;

thrust::exclusive_scan(thrust::host, data, data + 10, data, 1, binary_op); // in-place scan

// data is now {1, 1, 1, 2, 2, 2, 4, 4, 4, 4 }

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.

  • init – The initial value.

  • binary_op – The associatve operator used to ‘sum’ values.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator – is a model of Input Iterator and InputIterator's value_type is convertible to OutputIterator's value_type.

  • OutputIterator – is a model of Output Iterator and OutputIterator's value_type is convertible to both AssociativeOperator's first_argument_type and second_argument_type.

  • T – is convertible to OutputIterator's value_type.

  • AssociativeOperator – is a model of Binary Function and AssociativeOperator's result_type is convertible to OutputIterator's value_type.

Returns:

The end of the output sequence.

Pre:

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

template<typename InputIterator, typename OutputIterator, typename T, typename AssociativeOperator>
OutputIterator exclusive_scan(InputIterator first, InputIterator last, OutputIterator result, T init, AssociativeOperator binary_op)#

exclusive_scan computes an exclusive prefix sum operation. The term ‘exclusive’ means that each result does not include the corresponding input operand in the partial sum. More precisely, init is assigned to *result and the value binary_op(init, *first) is assigned to *(result + 1), and so on. This version of the function requires both an associative operator and an initial value init. When the input and output sequences are the same, the scan is performed in-place.

Results are not deterministic for pseudo-associative operators (e.g., addition of floating-point types). Results for pseudo-associative operators may vary from run to run.

The following code snippet demonstrates how to use exclusive_scan

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

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

thrust::maximum<int> binary_op;

thrust::exclusive_scan(data, data + 10, data, 1, binary_op); // in-place scan

// data is now {1, 1, 1, 2, 2, 2, 4, 4, 4, 4 }

Parameters:
  • first – The beginning of the input sequence.

  • last – The end of the input sequence.

  • result – The beginning of the output sequence.

  • init – The initial value.

  • binary_op – The associatve operator used to ‘sum’ values.

Template Parameters:
  • InputIterator – is a model of Input Iterator and InputIterator's value_type is convertible to OutputIterator's value_type.

  • OutputIterator – is a model of Output Iterator and OutputIterator's value_type is convertible to both AssociativeOperator's first_argument_type and second_argument_type.

  • T – is convertible to OutputIterator's value_type.

  • AssociativeOperator – is a model of Binary Function and AssociativeOperator's result_type is convertible to OutputIterator's value_type.

Returns:

The end of the output sequence.

Pre:

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

Functions

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename OutputIterator> __host__ __device__ OutputIterator inclusive_scan_by_key (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputIterator result)

inclusive_scan_by_key computes an inclusive key-value or ‘segmented’ prefix sum operation. The term ‘inclusive’ means that each result includes the corresponding input operand in the partial sum. The term ‘segmented’ means that the partial sums are broken into distinct segments. In other words, within each segment a separate inclusive scan operation is computed. Refer to the code sample below for example usage.

This version of inclusive_scan_by_key assumes equal_to as the binary predicate used to compare adjacent keys. Specifically, consecutive iterators i and i+1 in the range [first1, last1) belong to the same segment if *i == *(i+1), and belong to different segments otherwise.

This version of inclusive_scan_by_key assumes plus as the associative operator used to perform the prefix sum. When the input and output sequences are the same, the scan is performed in-place.

Results are not deterministic for pseudo-associative operators (e.g., addition of floating-point types). Results for pseudo-associative operators may vary from run to run.

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

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

#include <thrust/scan.h>
#include <thrust/execution_policy.h>
...

int data[10] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
int keys[10] = {0, 0, 0, 1, 1, 2, 3, 3, 3, 3};

thrust::inclusive_scan_by_key(thrust::host, keys, keys + 10, data, data); // in-place scan

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

See also

inclusive_scan

Parameters:
  • exec – The execution policy to use for parallelization.

  • first1 – The beginning of the key sequence.

  • last1 – The end of the key sequence.

  • first2 – The beginning of the input value sequence.

  • result – The beginning of the output value sequence.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator

  • InputIterator2 – is a model of Input Iterator and InputIterator2's value_type is convertible to OutputIterator's value_type.

  • OutputIterator – is a model of Output Iterator, and if x and y are objects of OutputIterator's value_type, then binary_op(x,y) is defined.

Returns:

The end of the output sequence.

Pre:

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

Pre:

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

template<typename InputIterator1, typename InputIterator2, typename OutputIterator>
OutputIterator inclusive_scan_by_key(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputIterator result)#

inclusive_scan_by_key computes an inclusive key-value or ‘segmented’ prefix sum operation. The term ‘inclusive’ means that each result includes the corresponding input operand in the partial sum. The term ‘segmented’ means that the partial sums are broken into distinct segments. In other words, within each segment a separate inclusive scan operation is computed. Refer to the code sample below for example usage.

This version of inclusive_scan_by_key assumes equal_to as the binary predicate used to compare adjacent keys. Specifically, consecutive iterators i and i+1 in the range [first1, last1) belong to the same segment if *i == *(i+1), and belong to different segments otherwise.

This version of inclusive_scan_by_key assumes plus as the associative operator used to perform the prefix sum. When the input and output sequences are the same, the scan is performed in-place.

Results are not deterministic for pseudo-associative operators (e.g., addition of floating-point types). Results for pseudo-associative operators may vary from run to run.

The following code snippet demonstrates how to use inclusive_scan_by_key

#include <thrust/scan.h>

int data[10] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
int keys[10] = {0, 0, 0, 1, 1, 2, 3, 3, 3, 3};

thrust::inclusive_scan_by_key(keys, keys + 10, data, data); // in-place scan

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

See also

inclusive_scan

Parameters:
  • first1 – The beginning of the key sequence.

  • last1 – The end of the key sequence.

  • first2 – The beginning of the input value sequence.

  • result – The beginning of the output value sequence.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator

  • InputIterator2 – is a model of Input Iterator and InputIterator2's value_type is convertible to OutputIterator's value_type.

  • OutputIterator – is a model of Output Iterator, and if x and y are objects of OutputIterator's value_type, then binary_op(x,y) is defined.

Returns:

The end of the output sequence.

Pre:

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

Pre:

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

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename OutputIterator, typename BinaryPredicate> __host__ __device__ OutputIterator inclusive_scan_by_key (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputIterator result, BinaryPredicate binary_pred)

inclusive_scan_by_key computes an inclusive key-value or ‘segmented’ prefix sum operation. The term ‘inclusive’ means that each result includes the corresponding input operand in the partial sum. The term ‘segmented’ means that the partial sums are broken into distinct segments. In other words, within each segment a separate inclusive scan operation is computed. Refer to the code sample below for example usage.

This version of inclusive_scan_by_key uses the binary predicate pred to compare adjacent keys. Specifically, consecutive iterators i and i+1 in the range [first1, last1) belong to the same segment if binary_pred(*i, *(i+1)) is true, and belong to different segments otherwise.

This version of inclusive_scan_by_key assumes plus as the associative operator used to perform the prefix sum. When the input and output sequences are the same, the scan is performed in-place.

Results are not deterministic for pseudo-associative operators (e.g., addition of floating-point types). Results for pseudo-associative operators may vary from run to run.

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

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

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

int data[10] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
int keys[10] = {0, 0, 0, 1, 1, 2, 3, 3, 3, 3};

thrust::equal_to<int> binary_pred;

thrust::inclusive_scan_by_key(thrust::host, keys, keys + 10, data, data, binary_pred); // in-place scan

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

See also

inclusive_scan

Parameters:
  • exec – The execution policy to use for parallelization.

  • first1 – The beginning of the key sequence.

  • last1 – The end of the key sequence.

  • first2 – The beginning of the input value sequence.

  • result – The beginning of the output value sequence.

  • binary_pred – The binary predicate used to determine equality of keys.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator

  • InputIterator2 – is a model of Input Iterator and InputIterator2's value_type is convertible to OutputIterator's value_type.

  • OutputIterator – is a model of Output Iterator, and if x and y are objects of OutputIterator's value_type, then binary_op(x,y) is defined.

  • BinaryPredicate – is a model of Binary Predicate.

Returns:

The end of the output sequence.

Pre:

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

Pre:

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

template<typename InputIterator1, typename InputIterator2, typename OutputIterator, typename BinaryPredicate>
OutputIterator inclusive_scan_by_key(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputIterator result, BinaryPredicate binary_pred)#

inclusive_scan_by_key computes an inclusive key-value or ‘segmented’ prefix sum operation. The term ‘inclusive’ means that each result includes the corresponding input operand in the partial sum. The term ‘segmented’ means that the partial sums are broken into distinct segments. In other words, within each segment a separate inclusive scan operation is computed. Refer to the code sample below for example usage.

This version of inclusive_scan_by_key uses the binary predicate pred to compare adjacent keys. Specifically, consecutive iterators i and i+1 in the range [first1, last1) belong to the same segment if binary_pred(*i, *(i+1)) is true, and belong to different segments otherwise.

This version of inclusive_scan_by_key assumes plus as the associative operator used to perform the prefix sum. When the input and output sequences are the same, the scan is performed in-place.

Results are not deterministic for pseudo-associative operators (e.g., addition of floating-point types). Results for pseudo-associative operators may vary from run to run.

The following code snippet demonstrates how to use inclusive_scan_by_key

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

int data[10] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
int keys[10] = {0, 0, 0, 1, 1, 2, 3, 3, 3, 3};

thrust::equal_to<int> binary_pred;

thrust::inclusive_scan_by_key(keys, keys + 10, data, data, binary_pred); // in-place scan

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

See also

inclusive_scan

Parameters:
  • first1 – The beginning of the key sequence.

  • last1 – The end of the key sequence.

  • first2 – The beginning of the input value sequence.

  • result – The beginning of the output value sequence.

  • binary_pred – The binary predicate used to determine equality of keys.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator

  • InputIterator2 – is a model of Input Iterator and InputIterator2's value_type is convertible to OutputIterator's value_type.

  • OutputIterator – is a model of Output Iterator, and if x and y are objects of OutputIterator's value_type, then binary_op(x,y) is defined.

  • BinaryPredicate – is a model of Binary Predicate.

Returns:

The end of the output sequence.

Pre:

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

Pre:

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

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename OutputIterator, typename BinaryPredicate, typename AssociativeOperator> __host__ __device__ OutputIterator inclusive_scan_by_key (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputIterator result, BinaryPredicate binary_pred, AssociativeOperator binary_op)

inclusive_scan_by_key computes an inclusive key-value or ‘segmented’ prefix sum operation. The term ‘inclusive’ means that each result includes the corresponding input operand in the partial sum. The term ‘segmented’ means that the partial sums are broken into distinct segments. In other words, within each segment a separate inclusive scan operation is computed. Refer to the code sample below for example usage.

This version of inclusive_scan_by_key uses the binary predicate pred to compare adjacent keys. Specifically, consecutive iterators i and i+1 in the range [first1, last1) belong to the same segment if binary_pred(*i, *(i+1)) is true, and belong to different segments otherwise.

This version of inclusive_scan_by_key uses the associative operator binary_op to perform the prefix sum. When the input and output sequences are the same, the scan is performed in-place.

Results are not deterministic for pseudo-associative operators (e.g., addition of floating-point types). Results for pseudo-associative operators may vary from run to run.

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

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

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

int data[10] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
int keys[10] = {0, 0, 0, 1, 1, 2, 3, 3, 3, 3};

thrust::equal_to<int> binary_pred;
thrust::plus<int>     binary_op;

thrust::inclusive_scan_by_key(thrust::host, keys, keys + 10, data, data, binary_pred, binary_op); // in-place scan

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

See also

inclusive_scan

Parameters:
  • exec – The execution policy to use for parallelization.

  • first1 – The beginning of the key sequence.

  • last1 – The end of the key sequence.

  • first2 – The beginning of the input value sequence.

  • result – The beginning of the output value sequence.

  • binary_pred – The binary predicate used to determine equality of keys.

  • binary_op – The associatve operator used to ‘sum’ values.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator

  • InputIterator2 – is a model of Input Iterator and InputIterator2's value_type is convertible to OutputIterator's value_type.

  • OutputIterator – is a model of Output Iterator, and if x and y are objects of OutputIterator's value_type, then binary_op(x,y) is defined.

  • BinaryPredicate – is a model of Binary Predicate.

  • AssociativeOperator – is a model of Binary Function and AssociativeOperator's result_type is convertible to OutputIterator's value_type.

Returns:

The end of the output sequence.

Pre:

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

Pre:

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

template<typename InputIterator1, typename InputIterator2, typename OutputIterator, typename BinaryPredicate, typename AssociativeOperator>
OutputIterator inclusive_scan_by_key(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputIterator result, BinaryPredicate binary_pred, AssociativeOperator binary_op)#

inclusive_scan_by_key computes an inclusive key-value or ‘segmented’ prefix sum operation. The term ‘inclusive’ means that each result includes the corresponding input operand in the partial sum. The term ‘segmented’ means that the partial sums are broken into distinct segments. In other words, within each segment a separate inclusive scan operation is computed. Refer to the code sample below for example usage.

This version of inclusive_scan_by_key uses the binary predicate pred to compare adjacent keys. Specifically, consecutive iterators i and i+1 in the range [first1, last1) belong to the same segment if binary_pred(*i, *(i+1)) is true, and belong to different segments otherwise.

Results are not deterministic for pseudo-associative operators (e.g., addition of floating-point types). Results for pseudo-associative operators may vary from run to run.

This version of inclusive_scan_by_key uses the associative operator binary_op to perform the prefix sum. When the input and output sequences are the same, the scan is performed in-place.

The following code snippet demonstrates how to use inclusive_scan_by_key

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

int data[10] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
int keys[10] = {0, 0, 0, 1, 1, 2, 3, 3, 3, 3};

thrust::equal_to<int> binary_pred;
thrust::plus<int>     binary_op;

thrust::inclusive_scan_by_key(keys, keys + 10, data, data, binary_pred, binary_op); // in-place scan

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

See also

inclusive_scan

Parameters:
  • first1 – The beginning of the key sequence.

  • last1 – The end of the key sequence.

  • first2 – The beginning of the input value sequence.

  • result – The beginning of the output value sequence.

  • binary_pred – The binary predicate used to determine equality of keys.

  • binary_op – The associatve operator used to ‘sum’ values.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator

  • InputIterator2 – is a model of Input Iterator and InputIterator2's value_type is convertible to OutputIterator's value_type.

  • OutputIterator – is a model of Output Iterator, and if x and y are objects of OutputIterator's value_type, then binary_op(x,y) is defined.

  • BinaryPredicate – is a model of Binary Predicate.

  • AssociativeOperator – is a model of Binary Function and AssociativeOperator's result_type is convertible to OutputIterator's value_type.

Returns:

The end of the output sequence.

Pre:

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

Pre:

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

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename OutputIterator> __host__ __device__ OutputIterator exclusive_scan_by_key (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputIterator result)

exclusive_scan_by_key computes an exclusive segmented prefix

This version of exclusive_scan_by_key uses the value 0 to initialize the exclusive scan operation.

This version of exclusive_scan_by_key assumes plus as the associative operator used to perform the prefix sum. When the input and output sequences are the same, the scan is performed in-place.

This version of exclusive_scan_by_key assumes equal_to as the binary predicate used to compare adjacent keys. Specifically, consecutive iterators i and i+1 in the range [first1, last1 belong to the same segment if *i == *(i+1), and belong to different segments otherwise.

Results are not deterministic for pseudo-associative operators (e.g., addition of floating-point types). Results for pseudo-associative operators may vary from run to run.

Refer to the most general form of exclusive_scan_by_key for additional details.

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

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

#include <thrust/scan.h>
#include <thrust/execution_policy.h>
...

int keys[10] = {0, 0, 0, 1, 1, 2, 3, 3, 3, 3};
int vals[10] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};

thrust::exclusive_scan_by_key(thrust::host, key, key + 10, vals, vals); // in-place scan

// vals is now {0, 1, 2, 0, 1, 0, 0, 1, 2, 3};

See also

exclusive_scan

Parameters:
  • exec – The execution policy to use for parallelization.

  • first1 – The beginning of the key sequence.

  • last1 – The end of the key sequence.

  • first2 – The beginning of the input value sequence.

  • result – The beginning of the output value sequence.

Pre:

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

Pre:

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

template<typename InputIterator1, typename InputIterator2, typename OutputIterator>
OutputIterator exclusive_scan_by_key(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputIterator result)#

exclusive_scan_by_key computes an exclusive segmented prefix

This version of exclusive_scan_by_key uses the value 0 to initialize the exclusive scan operation.

This version of exclusive_scan_by_key assumes plus as the associative operator used to perform the prefix sum. When the input and output sequences are the same, the scan is performed in-place.

This version of exclusive_scan_by_key assumes equal_to as the binary predicate used to compare adjacent keys. Specifically, consecutive iterators i and i+1 in the range [first1, last1 belong to the same segment if *i == *(i+1), and belong to different segments otherwise.

Results are not deterministic for pseudo-associative operators (e.g., addition of floating-point types). Results for pseudo-associative operators may vary from run to run.

Refer to the most general form of exclusive_scan_by_key for additional details.

The following code snippet demonstrates how to use exclusive_scan_by_key.

#include <thrust/scan.h>

int keys[10] = {0, 0, 0, 1, 1, 2, 3, 3, 3, 3};
int vals[10] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};

thrust::exclusive_scan_by_key(key, key + 10, vals, vals); // in-place scan

// vals is now {0, 1, 2, 0, 1, 0, 0, 1, 2, 3};

See also

exclusive_scan

Parameters:
  • first1 – The beginning of the key sequence.

  • last1 – The end of the key sequence.

  • first2 – The beginning of the input value sequence.

  • result – The beginning of the output value sequence.

Pre:

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

Pre:

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

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename OutputIterator, typename T> __host__ __device__ OutputIterator exclusive_scan_by_key (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputIterator result, T init)

exclusive_scan_by_key computes an exclusive key-value or ‘segmented’ prefix sum operation. The term ‘exclusive’ means that each result does not include the corresponding input operand in the partial sum. The term ‘segmented’ means that the partial sums are broken into distinct segments. In other words, within each segment a separate exclusive scan operation is computed. Refer to the code sample below for example usage.

This version of exclusive_scan_by_key uses the value init to initialize the exclusive scan operation.

Results are not deterministic for pseudo-associative operators (e.g., addition of floating-point types). Results for pseudo-associative operators may vary from run to run.

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

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

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

int keys[10] = {0, 0, 0, 1, 1, 2, 3, 3, 3, 3};
int vals[10] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};

int init = 5;

thrust::exclusive_scan_by_key(thrust::host, key, key + 10, vals, vals, init); // in-place scan

// vals is now {5, 6, 7, 5, 6, 5, 5, 6, 7, 8};

See also

exclusive_scan

Parameters:
  • exec – The execution policy to use for parallelization.

  • first1 – The beginning of the key sequence.

  • last1 – The end of the key sequence.

  • first2 – The beginning of the input value sequence.

  • result – The beginning of the output value sequence.

  • init – The initial of the exclusive sum value.

Returns:

The end of the output sequence.

Pre:

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

Pre:

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

template<typename InputIterator1, typename InputIterator2, typename OutputIterator, typename T>
OutputIterator exclusive_scan_by_key(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputIterator result, T init)#

exclusive_scan_by_key computes an exclusive key-value or ‘segmented’ prefix sum operation. The term ‘exclusive’ means that each result does not include the corresponding input operand in the partial sum. The term ‘segmented’ means that the partial sums are broken into distinct segments. In other words, within each segment a separate exclusive scan operation is computed. Refer to the code sample below for example usage.

This version of exclusive_scan_by_key uses the value init to initialize the exclusive scan operation.

Results are not deterministic for pseudo-associative operators (e.g., addition of floating-point types). Results for pseudo-associative operators may vary from run to run.

The following code snippet demonstrates how to use exclusive_scan_by_key

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

int keys[10] = {0, 0, 0, 1, 1, 2, 3, 3, 3, 3};
int vals[10] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};

int init = 5;

thrust::exclusive_scan_by_key(key, key + 10, vals, vals, init); // in-place scan

// vals is now {5, 6, 7, 5, 6, 5, 5, 6, 7, 8};

See also

exclusive_scan

Parameters:
  • first1 – The beginning of the key sequence.

  • last1 – The end of the key sequence.

  • first2 – The beginning of the input value sequence.

  • result – The beginning of the output value sequence.

  • init – The initial of the exclusive sum value.

Returns:

The end of the output sequence.

Pre:

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

Pre:

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

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename OutputIterator, typename T, typename BinaryPredicate> __host__ __device__ OutputIterator exclusive_scan_by_key (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputIterator result, T init, BinaryPredicate binary_pred)

exclusive_scan_by_key computes an exclusive key-value or ‘segmented’ prefix sum operation. The term ‘exclusive’ means that each result does not include the corresponding input operand in the partial sum. The term ‘segmented’ means that the partial sums are broken into distinct segments. In other words, within each segment a separate exclusive scan operation is computed. Refer to the code sample below for example usage.

This version of exclusive_scan_by_key uses the value init to initialize the exclusive scan operation.

This version of exclusive_scan_by_key uses the binary predicate binary_pred to compare adjacent keys. Specifically, consecutive iterators i and i+1 in the range [first1, last1) belong to the same segment if binary_pred(*i, *(i+1)) is true, and belong to different segments otherwise.

Results are not deterministic for pseudo-associative operators (e.g., addition of floating-point types). Results for pseudo-associative operators may vary from run to run.

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

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

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

int keys[10] = {0, 0, 0, 1, 1, 2, 3, 3, 3, 3};
int vals[10] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};

int init = 5;

thrust::equal_to<int> binary_pred;

thrust::exclusive_scan_by_key(thrust::host, key, key + 10, vals, vals, init, binary_pred); // in-place scan

// vals is now {5, 6, 7, 5, 6, 5, 5, 6, 7, 8};

See also

exclusive_scan

Parameters:
  • exec – The execution policy to use for parallelization.

  • first1 – The beginning of the key sequence.

  • last1 – The end of the key sequence.

  • first2 – The beginning of the input value sequence.

  • result – The beginning of the output value sequence.

  • init – The initial of the exclusive sum value.

  • binary_pred – The binary predicate used to determine equality of keys.

Returns:

The end of the output sequence.

Pre:

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

Pre:

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

template<typename InputIterator1, typename InputIterator2, typename OutputIterator, typename T, typename BinaryPredicate>
OutputIterator exclusive_scan_by_key(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputIterator result, T init, BinaryPredicate binary_pred)#

exclusive_scan_by_key computes an exclusive key-value or ‘segmented’ prefix sum operation. The term ‘exclusive’ means that each result does not include the corresponding input operand in the partial sum. The term ‘segmented’ means that the partial sums are broken into distinct segments. In other words, within each segment a separate exclusive scan operation is computed. Refer to the code sample below for example usage.

This version of exclusive_scan_by_key uses the value init to initialize the exclusive scan operation.

This version of exclusive_scan_by_key uses the binary predicate binary_pred to compare adjacent keys. Specifically, consecutive iterators i and i+1 in the range [first1, last1) belong to the same segment if binary_pred(*i, *(i+1)) is true, and belong to different segments otherwise.

Results are not deterministic for pseudo-associative operators (e.g., addition of floating-point types). Results for pseudo-associative operators may vary from run to run.

The following code snippet demonstrates how to use exclusive_scan_by_key

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

int keys[10] = {0, 0, 0, 1, 1, 2, 3, 3, 3, 3};
int vals[10] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};

int init = 5;

thrust::equal_to<int> binary_pred;

thrust::exclusive_scan_by_key(key, key + 10, vals, vals, init, binary_pred); // in-place scan

// vals is now {5, 6, 7, 5, 6, 5, 5, 6, 7, 8};

See also

exclusive_scan

Parameters:
  • first1 – The beginning of the key sequence.

  • last1 – The end of the key sequence.

  • first2 – The beginning of the input value sequence.

  • result – The beginning of the output value sequence.

  • init – The initial of the exclusive sum value.

  • binary_pred – The binary predicate used to determine equality of keys.

Returns:

The end of the output sequence.

Pre:

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

Pre:

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

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename OutputIterator, typename T, typename BinaryPredicate, typename AssociativeOperator> __host__ __device__ OutputIterator exclusive_scan_by_key (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputIterator result, T init, BinaryPredicate binary_pred, AssociativeOperator binary_op)

exclusive_scan_by_key computes an exclusive key-value or ‘segmented’ prefix sum operation. The term ‘exclusive’ means that each result does not include the corresponding input operand in the partial sum. The term ‘segmented’ means that the partial sums are broken into distinct segments. In other words, within each segment a separate exclusive scan operation is computed. Refer to the code sample below for example usage.

This version of exclusive_scan_by_key uses the value init to initialize the exclusive scan operation.

This version of exclusive_scan_by_key uses the binary predicate binary_pred to compare adjacent keys. Specifically, consecutive iterators i and i+1 in the range [first1, last1) belong to the same segment if binary_pred(*i, *(i+1)) is true, and belong to different segments otherwise.

This version of exclusive_scan_by_key uses the associative operator binary_op to perform the prefix sum. When the input and output sequences are the same, the scan is performed in-place.

Results are not deterministic for pseudo-associative operators (e.g., addition of floating-point types). Results for pseudo-associative operators may vary from run to run.

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

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

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

int keys[10] = {0, 0, 0, 1, 1, 2, 3, 3, 3, 3};
int vals[10] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};

int init = 5;

thrust::equal_to<int> binary_pred;
thrust::plus<int>     binary_op;

thrust::exclusive_scan_by_key(thrust::host, key, key + 10, vals, vals, init, binary_pred, binary_op); // in-place scan

// vals is now {5, 6, 7, 5, 6, 5, 5, 6, 7, 8};

See also

exclusive_scan

Parameters:
  • exec – The execution policy to use for parallelization.

  • first1 – The beginning of the key sequence.

  • last1 – The end of the key sequence.

  • first2 – The beginning of the input value sequence.

  • result – The beginning of the output value sequence.

  • init – The initial of the exclusive sum value.

  • binary_pred – The binary predicate used to determine equality of keys.

  • binary_op – The associatve operator used to ‘sum’ values.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator

  • InputIterator2 – is a model of Input Iterator and InputIterator2's value_type is convertible to OutputIterator's value_type.

  • OutputIterator – is a model of Output Iterator, and if x and y are objects of OutputIterator's value_type, then binary_op(x,y) is defined.

  • T – is convertible to OutputIterator's value_type.

  • BinaryPredicate – is a model of Binary Predicate.

  • AssociativeOperator – is a model of Binary Function and AssociativeOperator's result_type is convertible to OutputIterator's value_type.

Returns:

The end of the output sequence.

Pre:

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

Pre:

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

template<typename InputIterator1, typename InputIterator2, typename OutputIterator, typename T, typename BinaryPredicate, typename AssociativeOperator>
OutputIterator exclusive_scan_by_key(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputIterator result, T init, BinaryPredicate binary_pred, AssociativeOperator binary_op)#

exclusive_scan_by_key computes an exclusive key-value or ‘segmented’ prefix sum operation. The term ‘exclusive’ means that each result does not include the corresponding input operand in the partial sum. The term ‘segmented’ means that the partial sums are broken into distinct segments. In other words, within each segment a separate exclusive scan operation is computed. Refer to the code sample below for example usage.

This version of exclusive_scan_by_key uses the value init to initialize the exclusive scan operation.

This version of exclusive_scan_by_key uses the binary predicate binary_pred to compare adjacent keys. Specifically, consecutive iterators i and i+1 in the range [first1, last1) belong to the same segment if binary_pred(*i, *(i+1)) is true, and belong to different segments otherwise.

This version of exclusive_scan_by_key uses the associative operator binary_op to perform the prefix sum. When the input and output sequences are the same, the scan is performed in-place.

Results are not deterministic for pseudo-associative operators (e.g., addition of floating-point types). Results for pseudo-associative operators may vary from run to run.

The following code snippet demonstrates how to use exclusive_scan_by_key

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

int keys[10] = {0, 0, 0, 1, 1, 2, 3, 3, 3, 3};
int vals[10] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};

int init = 5;

thrust::equal_to<int> binary_pred;
thrust::plus<int>     binary_op;

thrust::exclusive_scan_by_key(key, key + 10, vals, vals, init, binary_pred, binary_op); // in-place scan

// vals is now {5, 6, 7, 5, 6, 5, 5, 6, 7, 8};

See also

exclusive_scan

Parameters:
  • first1 – The beginning of the key sequence.

  • last1 – The end of the key sequence.

  • first2 – The beginning of the input value sequence.

  • result – The beginning of the output value sequence.

  • init – The initial of the exclusive sum value.

  • binary_pred – The binary predicate used to determine equality of keys.

  • binary_op – The associatve operator used to ‘sum’ values.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator

  • InputIterator2 – is a model of Input Iterator and InputIterator2's value_type is convertible to OutputIterator's value_type.

  • OutputIterator – is a model of Output Iterator, and if x and y are objects of OutputIterator's value_type, then binary_op(x,y) is defined.

  • T – is convertible to OutputIterator's value_type.

  • BinaryPredicate – is a model of Binary Predicate.

  • AssociativeOperator – is a model of Binary Function and AssociativeOperator's result_type is convertible to OutputIterator's value_type.

Returns:

The end of the output sequence.

Pre:

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

Pre:

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

Functions

template<typename DerivedPolicy, typename InputIterator, typename OutputIterator, typename UnaryFunction, typename AssociativeOperator> __host__ __device__ OutputIterator transform_inclusive_scan (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator first, InputIterator last, OutputIterator result, UnaryFunction unary_op, AssociativeOperator binary_op)

transform_inclusive_scan fuses the transform and inclusive_scan operations. transform_inclusive_scan is equivalent to performing a tranformation defined by unary_op into a temporary sequence and then performing an inclusive_scan on the tranformed sequence. In most cases, fusing these two operations together is more efficient, since fewer memory reads and writes are required. In transform_inclusive_scan, unary_op(*first) is assigned to *result and the result of binary_op(unary_op(*first), unary_op(*(first + 1))) is assigned to *(result + 1), and so on. The transform scan operation is permitted to be in-place.

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

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

#include <thrust/transform_scan.h>
#include <thrust/execution_policy.h>
...

int data[6] = {1, 0, 2, 2, 1, 3};

thrust::negate<int> unary_op;
thrust::plus<int> binary_op;

thrust::transform_inclusive_scan(thrust::host, data, data + 6, data, unary_op, binary_op); // in-place scan

// data is now {-1, -1, -3, -5, -6, -9}

See also

transform

See also

inclusive_scan

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.

  • unary_op – The function used to tranform the input sequence.

  • binary_op – The associatve operator used to ‘sum’ transformed values.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator – is a model of Input Iterator and InputIterator's value_type is convertible to unary_op's input type.

  • OutputIterator – is a model of Output Iterator.

  • UnaryFunction – is a model of Unary Function and accepts inputs of InputIterator's value_type. UnaryFunction's result_type is convertable to OutputIterator's value_type.

  • AssociativeOperator – is a model of Binary Function and AssociativeOperator's result_type is convertible to OutputIterator's value_type.

Returns:

The end of the output sequence.

Pre:

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

template<typename InputIterator, typename OutputIterator, typename UnaryFunction, typename AssociativeOperator>
OutputIterator transform_inclusive_scan(InputIterator first, InputIterator last, OutputIterator result, UnaryFunction unary_op, AssociativeOperator binary_op)#

transform_inclusive_scan fuses the transform and inclusive_scan operations. transform_inclusive_scan is equivalent to performing a tranformation defined by unary_op into a temporary sequence and then performing an inclusive_scan on the tranformed sequence. In most cases, fusing these two operations together is more efficient, since fewer memory reads and writes are required. In transform_inclusive_scan, unary_op(*first) is assigned to *result and the result of binary_op(unary_op(*first), unary_op(*(first + 1))) is assigned to *(result + 1), and so on. The transform scan operation is permitted to be in-place.

The following code snippet demonstrates how to use transform_inclusive_scan

#include <thrust/transform_scan.h>

int data[6] = {1, 0, 2, 2, 1, 3};

thrust::negate<int> unary_op;
thrust::plus<int> binary_op;

thrust::transform_inclusive_scan(data, data + 6, data, unary_op, binary_op); // in-place scan

// data is now {-1, -1, -3, -5, -6, -9}

See also

transform

See also

inclusive_scan

Parameters:
  • first – The beginning of the input sequence.

  • last – The end of the input sequence.

  • result – The beginning of the output sequence.

  • unary_op – The function used to tranform the input sequence.

  • binary_op – The associatve operator used to ‘sum’ transformed values.

Template Parameters:
  • InputIterator – is a model of Input Iterator and InputIterator's value_type is convertible to unary_op's input type.

  • OutputIterator – is a model of Output Iterator.

  • UnaryFunction – is a model of Unary Function and accepts inputs of InputIterator's value_type. UnaryFunction's result_type is convertable to OutputIterator's value_type.

  • AssociativeOperator – is a model of Binary Function and AssociativeOperator's result_type is convertible to OutputIterator's value_type.

Returns:

The end of the output sequence.

Pre:

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

template<typename DerivedPolicy, typename InputIterator, typename OutputIterator, typename UnaryFunction, typename T, typename AssociativeOperator> __host__ __device__ OutputIterator transform_exclusive_scan (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator first, InputIterator last, OutputIterator result, UnaryFunction unary_op, T init, AssociativeOperator binary_op)

transform_exclusive_scan fuses the transform and exclusive_scan operations. transform_exclusive_scan is equivalent to performing a tranformation defined by unary_op into a temporary sequence and then performing an exclusive_scan on the tranformed sequence. In most cases, fusing these two operations together is more efficient, since fewer memory reads and writes are required. In transform_exclusive_scan, init is assigned to *result and the result of binary_op(init, unary_op(*first)) is assigned to *(result + 1), and so on. The transform scan operation is permitted to be in-place.

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

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

#include <thrust/transform_scan.h>
#include <thrust/execution_policy.h>
...

int data[6] = {1, 0, 2, 2, 1, 3};

thrust::negate<int> unary_op;
thrust::plus<int> binary_op;

thrust::transform_exclusive_scan(thrust::host, data, data + 6, data, unary_op, 4, binary_op); // in-place scan

// data is now {4, 3, 3, 1, -1, -2}

See also

transform

See also

exclusive_scan

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.

  • unary_op – The function used to tranform the input sequence.

  • init – The initial value of the exclusive_scan

  • binary_op – The associatve operator used to ‘sum’ transformed values.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator – is a model of Input Iterator and InputIterator's value_type is convertible to unary_op's input type.

  • OutputIterator – is a model of Output Iterator.

  • UnaryFunction – is a model of Unary Function and accepts inputs of InputIterator's value_type. UnaryFunction's result_type is convertable to OutputIterator's value_type.

  • T – is convertible to OutputIterator's value_type.

  • AssociativeOperator – is a model of Binary Function and AssociativeOperator's result_type is convertible to OutputIterator's value_type.

Returns:

The end of the output sequence.

Pre:

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

template<typename InputIterator, typename OutputIterator, typename UnaryFunction, typename T, typename AssociativeOperator>
OutputIterator transform_exclusive_scan(InputIterator first, InputIterator last, OutputIterator result, UnaryFunction unary_op, T init, AssociativeOperator binary_op)#

transform_exclusive_scan fuses the transform and exclusive_scan operations. transform_exclusive_scan is equivalent to performing a tranformation defined by unary_op into a temporary sequence and then performing an exclusive_scan on the tranformed sequence. In most cases, fusing these two operations together is more efficient, since fewer memory reads and writes are required. In transform_exclusive_scan, init is assigned to *result and the result of binary_op(init, unary_op(*first)) is assigned to *(result + 1), and so on. The transform scan operation is permitted to be in-place.

The following code snippet demonstrates how to use transform_exclusive_scan

#include <thrust/transform_scan.h>

int data[6] = {1, 0, 2, 2, 1, 3};

thrust::negate<int> unary_op;
thrust::plus<int> binary_op;

thrust::transform_exclusive_scan(data, data + 6, data, unary_op, 4, binary_op); // in-place scan

// data is now {4, 3, 3, 1, -1, -2}

See also

transform

See also

exclusive_scan

Parameters:
  • first – The beginning of the input sequence.

  • last – The end of the input sequence.

  • result – The beginning of the output sequence.

  • unary_op – The function used to tranform the input sequence.

  • init – The initial value of the exclusive_scan

  • binary_op – The associatve operator used to ‘sum’ transformed values.

Template Parameters:
  • InputIterator – is a model of Input Iterator and InputIterator's value_type is convertible to unary_op's input type.

  • OutputIterator – is a model of Output Iterator.

  • UnaryFunction – is a model of Unary Function and accepts inputs of InputIterator's value_type. UnaryFunction's result_type is convertable to OutputIterator's value_type.

  • T – is convertible to OutputIterator's value_type.

  • AssociativeOperator – is a model of Binary Function and AssociativeOperator's result_type is convertible to OutputIterator's value_type.

Returns:

The end of the output sequence.

Pre:

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

Functions

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename OutputIterator> __host__ __device__ OutputIterator set_difference (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result)

set_difference constructs a sorted range that is the set difference of the sorted ranges [first1, last1) and [first2, last2). The return value is the end of the output range.

In the simplest case, set_difference performs the “difference” operation from set theory: the output range contains a copy of every element that is contained in [first1, last1) and not contained in [first2, last1). The general case is more complicated, because the input ranges may contain duplicate elements. The generalization is that if [first1, last1) contains m elements that are equivalent to each other and if [first2, last2) contains n elements that are equivalent to them, the last max(m-n,0) elements from [first1, last1) range shall be copied to the output range.

This version of set_difference compares elements using operator<.

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

The following code snippet demonstrates how to use set_difference to compute the set difference of two sets of integers sorted in ascending order using the thrust::host execution policy for parallelization:

#include <thrust/set_operations.h>
#include <thrust/execution_policy.h>
...
int A1[7] = {0, 1, 3, 4, 5, 6, 9};
int A2[5] = {1, 3, 5, 7, 9};

int result[3];

int *result_end = thrust::set_difference(thrust::host, A1, A1 + 7, A2, A2 + 5, result);
// result is now {0, 4, 6}

See also

includes

See also

set_union

See also

set_intersection

See also

sort

See also

is_sorted

Parameters:
  • exec – The execution policy to use for parallelization.

  • first1 – The beginning of the first input range.

  • last1 – The end of the first input range.

  • first2 – The beginning of the second input range.

  • last2 – The end of the second input range.

  • result – The beginning of the output range.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator, InputIterator1 and InputIterator2 have the same value_type, InputIterator1's value_type is a model of LessThan Comparable, the ordering on InputIterator1's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator1's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2 and InputIterator1 have the same value_type, InputIterator2's value_type is a model of LessThan Comparable, the ordering on InputIterator2's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator2's value_type is convertable to a type in OutputIterator's set of value_types.

  • OutputIterator – is a model of Output Iterator.

Returns:

The end of the output range.

Pre:

The ranges [first1, last1) and [first2, last2) shall be sorted with respect to operator<.

Pre:

The resulting range shall not overlap with either input range.

template<typename InputIterator1, typename InputIterator2, typename OutputIterator>
OutputIterator set_difference(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result)#

set_difference constructs a sorted range that is the set difference of the sorted ranges [first1, last1) and [first2, last2). The return value is the end of the output range.

In the simplest case, set_difference performs the “difference” operation from set theory: the output range contains a copy of every element that is contained in [first1, last1) and not contained in [first2, last1). The general case is more complicated, because the input ranges may contain duplicate elements. The generalization is that if [first1, last1) contains m elements that are equivalent to each other and if [first2, last2) contains n elements that are equivalent to them, the last max(m-n,0) elements from [first1, last1) range shall be copied to the output range.

This version of set_difference compares elements using operator<.

The following code snippet demonstrates how to use set_difference to compute the set difference of two sets of integers sorted in ascending order.

#include <thrust/set_operations.h>
...
int A1[7] = {0, 1, 3, 4, 5, 6, 9};
int A2[5] = {1, 3, 5, 7, 9};

int result[3];

int *result_end = thrust::set_difference(A1, A1 + 7, A2, A2 + 5, result);
// result is now {0, 4, 6}

See also

includes

See also

set_union

See also

set_intersection

See also

sort

See also

is_sorted

Parameters:
  • first1 – The beginning of the first input range.

  • last1 – The end of the first input range.

  • first2 – The beginning of the second input range.

  • last2 – The end of the second input range.

  • result – The beginning of the output range.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator, InputIterator1 and InputIterator2 have the same value_type, InputIterator1's value_type is a model of LessThan Comparable, the ordering on InputIterator1's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator1's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2 and InputIterator1 have the same value_type, InputIterator2's value_type is a model of LessThan Comparable, the ordering on InputIterator2's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator2's value_type is convertable to a type in OutputIterator's set of value_types.

  • OutputIterator – is a model of Output Iterator.

Returns:

The end of the output range.

Pre:

The ranges [first1, last1) and [first2, last2) shall be sorted with respect to operator<.

Pre:

The resulting range shall not overlap with either input range.

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename OutputIterator, typename StrictWeakCompare> __host__ __device__ OutputIterator set_difference (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, StrictWeakCompare comp)

set_difference constructs a sorted range that is the set difference of the sorted ranges [first1, last1) and [first2, last2). The return value is the end of the output range.

In the simplest case, set_difference performs the “difference” operation from set theory: the output range contains a copy of every element that is contained in [first1, last1) and not contained in [first2, last1). The general case is more complicated, because the input ranges may contain duplicate elements. The generalization is that if [first1, last1) contains m elements that are equivalent to each other and if [first2, last2) contains n elements that are equivalent to them, the last max(m-n,0) elements from [first1, last1) range shall be copied to the output range.

This version of set_difference compares elements using a function object comp.

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

The following code snippet demonstrates how to use set_difference to compute the set difference of two sets of integers sorted in descending order using the thrust::host execution policy for parallelization:

#include <thrust/set_operations.h>
#include <thrust/functional.h>
#include <thrust/execution_policy.h>
...
int A1[7] = {9, 6, 5, 4, 3, 1, 0};
int A2[5] = {9, 7, 5, 3, 1};

int result[3];

int *result_end = thrust::set_difference(thrust::host, A1, A1 + 7, A2, A2 + 5, result, thrust::greater<int>());
// result is now {6, 4, 0}

See also

includes

See also

set_union

See also

set_intersection

See also

sort

See also

is_sorted

Parameters:
  • exec – The execution policy to use for parallelization.

  • first1 – The beginning of the first input range.

  • last1 – The end of the first input range.

  • first2 – The beginning of the second input range.

  • last2 – The end of the second input range.

  • result – The beginning of the output range.

  • comp – Comparison operator.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator, InputIterator1's value_type is convertable to StrictWeakCompare's first_argument_type. and InputIterator1's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2's value_type is convertable to StrictWeakCompare's second_argument_type. and InputIterator2's value_type is convertable to a type in OutputIterator's set of value_types.

  • OutputIterator – is a model of Output Iterator.

  • StrictWeakCompare – is a model of Strict Weak Ordering.

Returns:

The end of the output range.

Pre:

The ranges [first1, last1) and [first2, last2) shall be sorted with respect to comp.

Pre:

The resulting range shall not overlap with either input range.

template<typename InputIterator1, typename InputIterator2, typename OutputIterator, typename StrictWeakCompare>
OutputIterator set_difference(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, StrictWeakCompare comp)#

set_difference constructs a sorted range that is the set difference of the sorted ranges [first1, last1) and [first2, last2). The return value is the end of the output range.

In the simplest case, set_difference performs the “difference” operation from set theory: the output range contains a copy of every element that is contained in [first1, last1) and not contained in [first2, last1). The general case is more complicated, because the input ranges may contain duplicate elements. The generalization is that if [first1, last1) contains m elements that are equivalent to each other and if [first2, last2) contains n elements that are equivalent to them, the last max(m-n,0) elements from [first1, last1) range shall be copied to the output range.

This version of set_difference compares elements using a function object comp.

The following code snippet demonstrates how to use set_difference to compute the set difference of two sets of integers sorted in descending order.

#include <thrust/set_operations.h>
#include <thrust/functional.h>
...
int A1[7] = {9, 6, 5, 4, 3, 1, 0};
int A2[5] = {9, 7, 5, 3, 1};

int result[3];

int *result_end = thrust::set_difference(A1, A1 + 7, A2, A2 + 5, result, thrust::greater<int>());
// result is now {6, 4, 0}

See also

includes

See also

set_union

See also

set_intersection

See also

sort

See also

is_sorted

Parameters:
  • first1 – The beginning of the first input range.

  • last1 – The end of the first input range.

  • first2 – The beginning of the second input range.

  • last2 – The end of the second input range.

  • result – The beginning of the output range.

  • comp – Comparison operator.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator, InputIterator1's value_type is convertable to StrictWeakCompare's first_argument_type. and InputIterator1's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2's value_type is convertable to StrictWeakCompare's second_argument_type. and InputIterator2's value_type is convertable to a type in OutputIterator's set of value_types.

  • OutputIterator – is a model of Output Iterator.

  • StrictWeakCompare – is a model of Strict Weak Ordering.

Returns:

The end of the output range.

Pre:

The ranges [first1, last1) and [first2, last2) shall be sorted with respect to comp.

Pre:

The resulting range shall not overlap with either input range.

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename OutputIterator> __host__ __device__ OutputIterator set_intersection (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result)

set_intersection constructs a sorted range that is the intersection of sorted ranges [first1, last1) and [first2, last2). The return value is the end of the output range.

In the simplest case, set_intersection performs the “intersection” operation from set theory: the output range contains a copy of every element that is contained in both [first1, last1) and [first2, last2). The general case is more complicated, because the input ranges may contain duplicate elements. The generalization is that if a value appears m times in [first1, last1) and n times in [first2, last2) (where m may be zero), then it appears min(m,n) times in the output range. set_intersection is stable, meaning that both elements are copied from the first range rather than the second, and that the relative order of elements in the output range is the same as in the first input range.

This version of set_intersection compares objects using operator<.

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

The following code snippet demonstrates how to use set_intersection to compute the set intersection of two sets of integers sorted in ascending order using the thrust::host execution policy for parallelization:

#include <thrust/set_operations.h>
#include <thrust/execution_policy.h>
...
int A1[6] = {1, 3, 5, 7, 9, 11};
int A2[7] = {1, 1, 2, 3, 5,  8, 13};

int result[7];

int *result_end = thrust::set_intersection(thrust::host, A1, A1 + 6, A2, A2 + 7, result);
// result is now {1, 3, 5}

See also

includes

See also

set_union

See also

set_intersection

See also

sort

See also

is_sorted

Parameters:
  • exec – The execution policy to use for parallelization.

  • first1 – The beginning of the first input range.

  • last1 – The end of the first input range.

  • first2 – The beginning of the second input range.

  • last2 – The end of the second input range.

  • result – The beginning of the output range.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator, InputIterator1 and InputIterator2 have the same value_type, InputIterator1's value_type is a model of LessThan Comparable, the ordering on InputIterator1's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator1's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2 and InputIterator1 have the same value_type, InputIterator2's value_type is a model of LessThan Comparable, the ordering on InputIterator2's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator2's value_type is convertable to a type in OutputIterator's set of value_types.

  • OutputIterator – is a model of Output Iterator.

Returns:

The end of the output range.

Pre:

The ranges [first1, last1) and [first2, last2) shall be sorted with respect to operator<.

Pre:

The resulting range shall not overlap with either input range.

template<typename InputIterator1, typename InputIterator2, typename OutputIterator>
OutputIterator set_intersection(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result)#

set_intersection constructs a sorted range that is the intersection of sorted ranges [first1, last1) and [first2, last2). The return value is the end of the output range.

In the simplest case, set_intersection performs the “intersection” operation from set theory: the output range contains a copy of every element that is contained in both [first1, last1) and [first2, last2). The general case is more complicated, because the input ranges may contain duplicate elements. The generalization is that if a value appears m times in [first1, last1) and n times in [first2, last2) (where m may be zero), then it appears min(m,n) times in the output range. set_intersection is stable, meaning that both elements are copied from the first range rather than the second, and that the relative order of elements in the output range is the same as in the first input range.

This version of set_intersection compares objects using operator<.

The following code snippet demonstrates how to use set_intersection to compute the set intersection of two sets of integers sorted in ascending order.

#include <thrust/set_operations.h>
...
int A1[6] = {1, 3, 5, 7, 9, 11};
int A2[7] = {1, 1, 2, 3, 5,  8, 13};

int result[7];

int *result_end = thrust::set_intersection(A1, A1 + 6, A2, A2 + 7, result);
// result is now {1, 3, 5}

See also

includes

See also

set_union

See also

set_intersection

See also

sort

See also

is_sorted

Parameters:
  • first1 – The beginning of the first input range.

  • last1 – The end of the first input range.

  • first2 – The beginning of the second input range.

  • last2 – The end of the second input range.

  • result – The beginning of the output range.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator, InputIterator1 and InputIterator2 have the same value_type, InputIterator1's value_type is a model of LessThan Comparable, the ordering on InputIterator1's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator1's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2 and InputIterator1 have the same value_type, InputIterator2's value_type is a model of LessThan Comparable, the ordering on InputIterator2's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator2's value_type is convertable to a type in OutputIterator's set of value_types.

  • OutputIterator – is a model of Output Iterator.

Returns:

The end of the output range.

Pre:

The ranges [first1, last1) and [first2, last2) shall be sorted with respect to operator<.

Pre:

The resulting range shall not overlap with either input range.

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename OutputIterator, typename StrictWeakCompare> __host__ __device__ OutputIterator set_intersection (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, StrictWeakCompare comp)

set_intersection constructs a sorted range that is the intersection of sorted ranges [first1, last1) and [first2, last2). The return value is the end of the output range.

In the simplest case, set_intersection performs the “intersection” operation from set theory: the output range contains a copy of every element that is contained in both [first1, last1) and [first2, last2). The general case is more complicated, because the input ranges may contain duplicate elements. The generalization is that if a value appears m times in [first1, last1) and n times in [first2, last2) (where m may be zero), then it appears min(m,n) times in the output range. set_intersection is stable, meaning that both elements are copied from the first range rather than the second, and that the relative order of elements in the output range is the same as in the first input range.

This version of set_intersection compares elements using a function object comp.

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

The following code snippet demonstrates how to use set_intersection to compute the set intersection of sets of integers sorted in descending order using the thrust::host execution policy for parallelization:

#include <thrust/set_operations.h>
#include <thrust/execution_policy.h>
...
int A1[6] = {11, 9, 7, 5, 3, 1};
int A2[7] = {13, 8, 5, 3, 2,  1, 1};

int result[3];

int *result_end = thrust::set_intersection(thrust::host, A1, A1 + 6, A2, A2 + 7, result, thrust::greater<int>());
// result is now {5, 3, 1}

See also

includes

See also

set_union

See also

set_intersection

See also

sort

See also

is_sorted

Parameters:
  • exec – The execution policy to use for parallelization.

  • first1 – The beginning of the first input range.

  • last1 – The end of the first input range.

  • first2 – The beginning of the second input range.

  • last2 – The end of the second input range.

  • result – The beginning of the output range.

  • comp – Comparison operator.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator, InputIterator1 and InputIterator2 have the same value_type, InputIterator1's value_type is a model of LessThan Comparable, the ordering on InputIterator1's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator1's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2 and InputIterator1 have the same value_type, InputIterator2's value_type is a model of LessThan Comparable, the ordering on InputIterator2's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator2's value_type is convertable to a type in OutputIterator's set of value_types.

  • OutputIterator – is a model of Output Iterator.

Returns:

The end of the output range.

Pre:

The ranges [first1, last1) and [first2, last2) shall be sorted with respect to comp.

Pre:

The resulting range shall not overlap with either input range.

template<typename InputIterator1, typename InputIterator2, typename OutputIterator, typename StrictWeakCompare>
OutputIterator set_intersection(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, StrictWeakCompare comp)#

set_intersection constructs a sorted range that is the intersection of sorted ranges [first1, last1) and [first2, last2). The return value is the end of the output range.

In the simplest case, set_intersection performs the “intersection” operation from set theory: the output range contains a copy of every element that is contained in both [first1, last1) and [first2, last2). The general case is more complicated, because the input ranges may contain duplicate elements. The generalization is that if a value appears m times in [first1, last1) and n times in [first2, last2) (where m may be zero), then it appears min(m,n) times in the output range. set_intersection is stable, meaning that both elements are copied from the first range rather than the second, and that the relative order of elements in the output range is the same as in the first input range.

This version of set_intersection compares elements using a function object comp.

The following code snippet demonstrates how to use set_intersection to compute the set intersection of sets of integers sorted in descending order.

#include <thrust/set_operations.h>
...
int A1[6] = {11, 9, 7, 5, 3, 1};
int A2[7] = {13, 8, 5, 3, 2,  1, 1};

int result[3];

int *result_end = thrust::set_intersection(A1, A1 + 6, A2, A2 + 7, result, thrust::greater<int>());
// result is now {5, 3, 1}

See also

includes

See also

set_union

See also

set_intersection

See also

sort

See also

is_sorted

Parameters:
  • first1 – The beginning of the first input range.

  • last1 – The end of the first input range.

  • first2 – The beginning of the second input range.

  • last2 – The end of the second input range.

  • result – The beginning of the output range.

  • comp – Comparison operator.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator, InputIterator1 and InputIterator2 have the same value_type, InputIterator1's value_type is a model of LessThan Comparable, the ordering on InputIterator1's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator1's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2 and InputIterator1 have the same value_type, InputIterator2's value_type is a model of LessThan Comparable, the ordering on InputIterator2's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator2's value_type is convertable to a type in OutputIterator's set of value_types.

  • OutputIterator – is a model of Output Iterator.

Returns:

The end of the output range.

Pre:

The ranges [first1, last1) and [first2, last2) shall be sorted with respect to comp.

Pre:

The resulting range shall not overlap with either input range.

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename OutputIterator> __host__ __device__ OutputIterator set_symmetric_difference (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result)

set_symmetric_difference constructs a sorted range that is the set symmetric difference of the sorted ranges [first1, last1) and [first2, last2). The return value is the end of the output range.

In the simplest case, set_symmetric_difference performs a set theoretic calculation: it constructs the union of the two sets A - B and B - A, where A and B are the two input ranges. That is, the output range contains a copy of every element that is contained in [first1, last1) but not [first2, last1), and a copy of every element that is contained in [first2, last2) but not [first1, last1). The general case is more complicated, because the input ranges may contain duplicate elements. The generalization is that if [first1, last1) contains m elements that are equivalent to each other and [first2, last1) contains n elements that are equivalent to them, then |m - n| of those elements shall be copied to the output range: the last m - n elements from [first1, last1) if m > n, and the last n - m of these elements from [first2, last2) if m < n.

This version of set_union compares elements using operator<.

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

The following code snippet demonstrates how to use set_symmetric_difference to compute the symmetric difference of two sets of integers sorted in ascending order using the thrust::host execution policy for parallelization:

#include <thrust/set_operations.h>
#include <thrust/execution_policy.h>
...
int A1[7] = {0, 1, 2, 2, 4, 6, 7};
int A2[5] = {1, 1, 2, 5, 8};

int result[6];

int *result_end = thrust::set_symmetric_difference(thrust::host, A1, A1 + 7, A2, A2 + 5, result);
// result = {0, 4, 5, 6, 7, 8}

See also

merge

See also

includes

See also

set_difference

See also

set_union

See also

set_intersection

See also

sort

See also

is_sorted

Parameters:
  • exec – The execution policy to use for parallelization.

  • first1 – The beginning of the first input range.

  • last1 – The end of the first input range.

  • first2 – The beginning of the second input range.

  • last2 – The end of the second input range.

  • result – The beginning of the output range.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator, InputIterator1 and InputIterator2 have the same value_type, InputIterator1's value_type is a model of LessThan Comparable, the ordering on InputIterator1's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator1's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2 and InputIterator1 have the same value_type, InputIterator2's value_type is a model of LessThan Comparable, the ordering on InputIterator2's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator2's value_type is convertable to a type in OutputIterator's set of value_types.

  • OutputIterator – is a model of Output Iterator.

Returns:

The end of the output range.

Pre:

The ranges [first1, last1) and [first2, last2) shall be sorted with respect to operator<.

Pre:

The resulting range shall not overlap with either input range.

template<typename InputIterator1, typename InputIterator2, typename OutputIterator>
OutputIterator set_symmetric_difference(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result)#

set_symmetric_difference constructs a sorted range that is the set symmetric difference of the sorted ranges [first1, last1) and [first2, last2). The return value is the end of the output range.

In the simplest case, set_symmetric_difference performs a set theoretic calculation: it constructs the union of the two sets A - B and B - A, where A and B are the two input ranges. That is, the output range contains a copy of every element that is contained in [first1, last1) but not [first2, last1), and a copy of every element that is contained in [first2, last2) but not [first1, last1). The general case is more complicated, because the input ranges may contain duplicate elements. The generalization is that if [first1, last1) contains m elements that are equivalent to each other and [first2, last1) contains n elements that are equivalent to them, then |m - n| of those elements shall be copied to the output range: the last m - n elements from [first1, last1) if m > n, and the last n - m of these elements from [first2, last2) if m < n.

This version of set_union compares elements using operator<.

The following code snippet demonstrates how to use set_symmetric_difference to compute the symmetric difference of two sets of integers sorted in ascending order.

#include <thrust/set_operations.h>
...
int A1[7] = {0, 1, 2, 2, 4, 6, 7};
int A2[5] = {1, 1, 2, 5, 8};

int result[6];

int *result_end = thrust::set_symmetric_difference(A1, A1 + 7, A2, A2 + 5, result);
// result = {0, 4, 5, 6, 7, 8}

See also

merge

See also

includes

See also

set_difference

See also

set_union

See also

set_intersection

See also

sort

See also

is_sorted

Parameters:
  • first1 – The beginning of the first input range.

  • last1 – The end of the first input range.

  • first2 – The beginning of the second input range.

  • last2 – The end of the second input range.

  • result – The beginning of the output range.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator, InputIterator1 and InputIterator2 have the same value_type, InputIterator1's value_type is a model of LessThan Comparable, the ordering on InputIterator1's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator1's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2 and InputIterator1 have the same value_type, InputIterator2's value_type is a model of LessThan Comparable, the ordering on InputIterator2's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator2's value_type is convertable to a type in OutputIterator's set of value_types.

  • OutputIterator – is a model of Output Iterator.

Returns:

The end of the output range.

Pre:

The ranges [first1, last1) and [first2, last2) shall be sorted with respect to operator<.

Pre:

The resulting range shall not overlap with either input range.

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename OutputIterator, typename StrictWeakCompare> __host__ __device__ OutputIterator set_symmetric_difference (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, StrictWeakCompare comp)

set_symmetric_difference constructs a sorted range that is the set symmetric difference of the sorted ranges [first1, last1) and [first2, last2). The return value is the end of the output range.

In the simplest case, set_symmetric_difference performs a set theoretic calculation: it constructs the union of the two sets A - B and B - A, where A and B are the two input ranges. That is, the output range contains a copy of every element that is contained in [first1, last1) but not [first2, last1), and a copy of every element that is contained in [first2, last2) but not [first1, last1). The general case is more complicated, because the input ranges may contain duplicate elements. The generalization is that if [first1, last1) contains m elements that are equivalent to each other and [first2, last1) contains n elements that are equivalent to them, then |m - n| of those elements shall be copied to the output range: the last m - n elements from [first1, last1) if m > n, and the last n - m of these elements from [first2, last2) if m < n.

This version of set_union compares elements using a function object comp.

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

The following code snippet demonstrates how to use set_symmetric_difference to compute the symmetric difference of two sets of integers sorted in descending order using the thrust::host execution policy for parallelization:

#include <thrust/set_operations.h>
#include <thrust/execution_policy.h>
...
int A1[7] = {7, 6, 4, 2, 2, 1, 0};
int A2[5] = {8, 5, 2, 1, 1};

int result[6];

int *result_end = thrust::set_symmetric_difference(thrust::host, A1, A1 + 7, A2, A2 + 5, result);
// result = {8, 7, 6, 5, 4, 0}

See also

merge

See also

includes

See also

set_difference

See also

set_union

See also

set_intersection

See also

sort

See also

is_sorted

Parameters:
  • exec – The execution policy to use for parallelization.

  • first1 – The beginning of the first input range.

  • last1 – The end of the first input range.

  • first2 – The beginning of the second input range.

  • last2 – The end of the second input range.

  • result – The beginning of the output range.

  • comp – Comparison operator.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator, InputIterator1 and InputIterator2 have the same value_type, InputIterator1's value_type is a model of LessThan Comparable, the ordering on InputIterator1's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator1's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2 and InputIterator1 have the same value_type, InputIterator2's value_type is a model of LessThan Comparable, the ordering on InputIterator2's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator2's value_type is convertable to a type in OutputIterator's set of value_types.

  • OutputIterator – is a model of Output Iterator.

Returns:

The end of the output range.

Pre:

The ranges [first1, last1) and [first2, last2) shall be sorted with respect to comp.

Pre:

The resulting range shall not overlap with either input range.

template<typename InputIterator1, typename InputIterator2, typename OutputIterator, typename StrictWeakCompare>
OutputIterator set_symmetric_difference(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, StrictWeakCompare comp)#

set_symmetric_difference constructs a sorted range that is the set symmetric difference of the sorted ranges [first1, last1) and [first2, last2). The return value is the end of the output range.

In the simplest case, set_symmetric_difference performs a set theoretic calculation: it constructs the union of the two sets A - B and B - A, where A and B are the two input ranges. That is, the output range contains a copy of every element that is contained in [first1, last1) but not [first2, last1), and a copy of every element that is contained in [first2, last2) but not [first1, last1). The general case is more complicated, because the input ranges may contain duplicate elements. The generalization is that if [first1, last1) contains m elements that are equivalent to each other and [first2, last1) contains n elements that are equivalent to them, then |m - n| of those elements shall be copied to the output range: the last m - n elements from [first1, last1) if m > n, and the last n - m of these elements from [first2, last2) if m < n.

This version of set_union compares elements using a function object comp.

The following code snippet demonstrates how to use set_symmetric_difference to compute the symmetric difference of two sets of integers sorted in descending order.

#include <thrust/set_operations.h>
...
int A1[7] = {7, 6, 4, 2, 2, 1, 0};
int A2[5] = {8, 5, 2, 1, 1};

int result[6];

int *result_end = thrust::set_symmetric_difference(A1, A1 + 7, A2, A2 + 5, result);
// result = {8, 7, 6, 5, 4, 0}

See also

merge

See also

includes

See also

set_difference

See also

set_union

See also

set_intersection

See also

sort

See also

is_sorted

Parameters:
  • first1 – The beginning of the first input range.

  • last1 – The end of the first input range.

  • first2 – The beginning of the second input range.

  • last2 – The end of the second input range.

  • result – The beginning of the output range.

  • comp – Comparison operator.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator, InputIterator1 and InputIterator2 have the same value_type, InputIterator1's value_type is a model of LessThan Comparable, the ordering on InputIterator1's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator1's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2 and InputIterator1 have the same value_type, InputIterator2's value_type is a model of LessThan Comparable, the ordering on InputIterator2's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator2's value_type is convertable to a type in OutputIterator's set of value_types.

  • OutputIterator – is a model of Output Iterator.

Returns:

The end of the output range.

Pre:

The ranges [first1, last1) and [first2, last2) shall be sorted with respect to comp.

Pre:

The resulting range shall not overlap with either input range.

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename OutputIterator> __host__ __device__ OutputIterator set_union (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result)

set_union constructs a sorted range that is the union of the sorted ranges [first1, last1) and [first2, last2). The return value is the end of the output range.

In the simplest case, set_union performs the “union” operation from set theory: the output range contains a copy of every element that is contained in [first1, last1), [first2, last1), or both. The general case is more complicated, because the input ranges may contain duplicate elements. The generalization is that if [first1, last1) contains m elements that are equivalent to each other and if [first2, last2) contains n elements that are equivalent to them, then all m elements from the first range shall be copied to the output range, in order, and then max(n - m, 0) elements from the second range shall be copied to the output, in order.

This version of set_union compares elements using operator<.

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

The following code snippet demonstrates how to use set_union to compute the union of two sets of integers sorted in ascending order using the thrust::host execution policy for parallelization:

#include <thrust/set_operations.h>
#include <thrust/execution_policy.h>
...
int A1[7] = {0, 2, 4, 6, 8, 10, 12};
int A2[5] = {1, 3, 5, 7, 9};

int result[11];

int *result_end = thrust::set_union(thrust::host, A1, A1 + 7, A2, A2 + 5, result);
// result = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12}

See also

merge

See also

includes

See also

set_union

See also

set_intersection

See also

sort

See also

is_sorted

Parameters:
  • exec – The execution policy to use for parallelization.

  • first1 – The beginning of the first input range.

  • last1 – The end of the first input range.

  • first2 – The beginning of the second input range.

  • last2 – The end of the second input range.

  • result – The beginning of the output range.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator, InputIterator1 and InputIterator2 have the same value_type, InputIterator1's value_type is a model of LessThan Comparable, the ordering on InputIterator1's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator1's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2 and InputIterator1 have the same value_type, InputIterator2's value_type is a model of LessThan Comparable, the ordering on InputIterator2's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator2's value_type is convertable to a type in OutputIterator's set of value_types.

  • OutputIterator – is a model of Output Iterator.

Returns:

The end of the output range.

Pre:

The ranges [first1, last1) and [first2, last2) shall be sorted with respect to operator<.

Pre:

The resulting range shall not overlap with either input range.

template<typename InputIterator1, typename InputIterator2, typename OutputIterator>
OutputIterator set_union(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result)#

set_union constructs a sorted range that is the union of the sorted ranges [first1, last1) and [first2, last2). The return value is the end of the output range.

In the simplest case, set_union performs the “union” operation from set theory: the output range contains a copy of every element that is contained in [first1, last1), [first2, last1), or both. The general case is more complicated, because the input ranges may contain duplicate elements. The generalization is that if [first1, last1) contains m elements that are equivalent to each other and if [first2, last2) contains n elements that are equivalent to them, then all m elements from the first range shall be copied to the output range, in order, and then max(n - m, 0) elements from the second range shall be copied to the output, in order.

This version of set_union compares elements using operator<.

The following code snippet demonstrates how to use set_union to compute the union of two sets of integers sorted in ascending order.

#include <thrust/set_operations.h>
...
int A1[7] = {0, 2, 4, 6, 8, 10, 12};
int A2[5] = {1, 3, 5, 7, 9};

int result[11];

int *result_end = thrust::set_union(A1, A1 + 7, A2, A2 + 5, result);
// result = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12}

See also

merge

See also

includes

See also

set_union

See also

set_intersection

See also

sort

See also

is_sorted

Parameters:
  • first1 – The beginning of the first input range.

  • last1 – The end of the first input range.

  • first2 – The beginning of the second input range.

  • last2 – The end of the second input range.

  • result – The beginning of the output range.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator, InputIterator1 and InputIterator2 have the same value_type, InputIterator1's value_type is a model of LessThan Comparable, the ordering on InputIterator1's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator1's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2 and InputIterator1 have the same value_type, InputIterator2's value_type is a model of LessThan Comparable, the ordering on InputIterator2's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator2's value_type is convertable to a type in OutputIterator's set of value_types.

  • OutputIterator – is a model of Output Iterator.

Returns:

The end of the output range.

Pre:

The ranges [first1, last1) and [first2, last2) shall be sorted with respect to operator<.

Pre:

The resulting range shall not overlap with either input range.

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename OutputIterator, typename StrictWeakCompare> __host__ __device__ OutputIterator set_union (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, StrictWeakCompare comp)

set_union constructs a sorted range that is the union of the sorted ranges [first1, last1) and [first2, last2). The return value is the end of the output range.

In the simplest case, set_union performs the “union” operation from set theory: the output range contains a copy of every element that is contained in [first1, last1), [first2, last1), or both. The general case is more complicated, because the input ranges may contain duplicate elements. The generalization is that if [first1, last1) contains m elements that are equivalent to each other and if [first2, last2) contains n elements that are equivalent to them, then all m elements from the first range shall be copied to the output range, in order, and then max(n - m, 0) elements from the second range shall be copied to the output, in order.

This version of set_union compares elements using a function object comp.

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

The following code snippet demonstrates how to use set_union to compute the union of two sets of integers sorted in ascending order using the thrust::host execution policy for parallelization:

#include <thrust/set_operations.h>
#include <thrust/functional.h>
#include <thrust/execution_policy.h>
...
int A1[7] = {12, 10, 8, 6, 4, 2, 0};
int A2[5] = {9, 7, 5, 3, 1};

int result[11];

int *result_end = thrust::set_union(thrust::host, A1, A1 + 7, A2, A2 + 5, result, thrust::greater<int>());
// result = {12, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}

See also

merge

See also

includes

See also

set_union

See also

set_intersection

See also

sort

See also

is_sorted

Parameters:
  • exec – The execution policy to use for parallelization.

  • first1 – The beginning of the first input range.

  • last1 – The end of the first input range.

  • first2 – The beginning of the second input range.

  • last2 – The end of the second input range.

  • result – The beginning of the output range.

  • comp – Comparison operator.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator, InputIterator1's value_type is convertable to StrictWeakCompare's first_argument_type. and InputIterator1's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2's value_type is convertable to StrictWeakCompare's second_argument_type. and InputIterator2's value_type is convertable to a type in OutputIterator's set of value_types.

  • OutputIterator – is a model of Output Iterator.

  • StrictWeakCompare – is a model of Strict Weak Ordering.

Returns:

The end of the output range.

Pre:

The ranges [first1, last1) and [first2, last2) shall be sorted with respect to comp.

Pre:

The resulting range shall not overlap with either input range.

template<typename InputIterator1, typename InputIterator2, typename OutputIterator, typename StrictWeakCompare>
OutputIterator set_union(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, StrictWeakCompare comp)#

set_union constructs a sorted range that is the union of the sorted ranges [first1, last1) and [first2, last2). The return value is the end of the output range.

In the simplest case, set_union performs the “union” operation from set theory: the output range contains a copy of every element that is contained in [first1, last1), [first2, last1), or both. The general case is more complicated, because the input ranges may contain duplicate elements. The generalization is that if [first1, last1) contains m elements that are equivalent to each other and if [first2, last2) contains n elements that are equivalent to them, then all m elements from the first range shall be copied to the output range, in order, and then max(n - m, 0) elements from the second range shall be copied to the output, in order.

This version of set_union compares elements using a function object comp.

The following code snippet demonstrates how to use set_union to compute the union of two sets of integers sorted in ascending order.

#include <thrust/set_operations.h>
#include <thrust/functional.h>
...
int A1[7] = {12, 10, 8, 6, 4, 2, 0};
int A2[5] = {9, 7, 5, 3, 1};

int result[11];

int *result_end = thrust::set_union(A1, A1 + 7, A2, A2 + 5, result, thrust::greater<int>());
// result = {12, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}

See also

merge

See also

includes

See also

set_union

See also

set_intersection

See also

sort

See also

is_sorted

Parameters:
  • first1 – The beginning of the first input range.

  • last1 – The end of the first input range.

  • first2 – The beginning of the second input range.

  • last2 – The end of the second input range.

  • result – The beginning of the output range.

  • comp – Comparison operator.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator, InputIterator1's value_type is convertable to StrictWeakCompare's first_argument_type. and InputIterator1's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2's value_type is convertable to StrictWeakCompare's second_argument_type. and InputIterator2's value_type is convertable to a type in OutputIterator's set of value_types.

  • OutputIterator – is a model of Output Iterator.

  • StrictWeakCompare – is a model of Strict Weak Ordering.

Returns:

The end of the output range.

Pre:

The ranges [first1, last1) and [first2, last2) shall be sorted with respect to comp.

Pre:

The resulting range shall not overlap with either input range.

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename InputIterator3, typename InputIterator4, typename OutputIterator1, typename OutputIterator2> __host__ __device__ thrust::pair< OutputIterator1, OutputIterator2 > set_difference_by_key (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 keys_first1, InputIterator1 keys_last1, InputIterator2 keys_first2, InputIterator2 keys_last2, InputIterator3 values_first1, InputIterator4 values_first2, OutputIterator1 keys_result, OutputIterator2 values_result)

set_difference_by_key performs a key-value difference operation from set theory. set_difference_by_key constructs a sorted range that is the difference of the sorted ranges [keys_first1, keys_last1) and [keys_first2, keys_last2). Associated with each element from the input and output key ranges is a value element. The associated input value ranges need not be sorted.

In the simplest case, set_difference_by_key performs the “difference” operation from set theory: the keys output range contains a copy of every element that is contained in [keys_first1, keys_last1) and not contained in [keys_first2, keys_last2). The general case is more complicated, because the input ranges may contain duplicate elements. The generalization is that if [keys_first1, keys_last1) contains m elements that are equivalent to each other and if [keys_first2, keys_last2) contains n elements that are equivalent to them, the last max(m-n,0) elements from [keys_first1, keys_last1) range shall be copied to the output range.

Each time a key element is copied from [keys_first1, keys_last1) or [keys_first2, keys_last2) is copied to the keys output range, the corresponding value element is copied from the corresponding values input range (beginning at values_first1 or values_first2) to the values output range.

This version of set_difference_by_key compares key elements using operator<.

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

The following code snippet demonstrates how to use set_difference_by_key to compute the set difference of two sets of integers sorted in ascending order with their values using the thrust::host execution policy for parallelization:

#include <thrust/set_operations.h>
#include <thrust/execution_policy.h>
...
int A_keys[6] = {0, 1, 3, 4, 5, 6, 9};
int A_vals[6] = {0, 0, 0, 0, 0, 0, 0};

int B_keys[5] = {1, 3, 5, 7, 9};
int B_vals[5] = {1, 1, 1, 1, 1};

int keys_result[3];
int vals_result[3];

thrust::pair<int*,int*> end = thrust::set_difference_by_key(thrust::host, A_keys, A_keys + 6, B_keys, B_keys + 5, A_vals, B_vals, keys_result, vals_result);
// keys_result is now {0, 4, 6}
// vals_result is now {0, 0, 0}

See also

set_union_by_key

See also

sort_by_key

See also

is_sorted

Parameters:
  • exec – The execution policy to use for parallelization.

  • keys_first1 – The beginning of the first input range of keys.

  • keys_last1 – The end of the first input range of keys.

  • keys_first2 – The beginning of the second input range of keys.

  • keys_last2 – The end of the second input range of keys.

  • values_first1 – The beginning of the first input range of values.

  • values_first2 – The beginning of the first input range of values.

  • keys_result – The beginning of the output range of keys.

  • values_result – The beginning of the output range of values.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator, InputIterator1 and InputIterator2 have the same value_type, InputIterator1's value_type is a model of LessThan Comparable, the ordering on InputIterator1's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator1's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2 and InputIterator1 have the same value_type, InputIterator2's value_type is a model of LessThan Comparable, the ordering on InputIterator2's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator2's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator3 – is a model of Input Iterator, and InputIterator3's value_type is convertible to a type in OutputIterator2's set of value_types.

  • InputIterator4 – is a model of Input Iterator, and InputIterator4's value_type is convertible to a type in OutputIterator2's set of value_types.

  • OutputIterator1 – is a model of Output Iterator.

  • OutputIterator2 – is a model of Output Iterator.

Returns:

A pair p such that p.first is the end of the output range of keys, and such that p.second is the end of the output range of values.

Pre:

The ranges [keys_first1, keys_last1) and [keys_first2, keys_last2) shall be sorted with respect to operator<.

Pre:

The resulting ranges shall not overlap with any input range.

template<typename InputIterator1, typename InputIterator2, typename InputIterator3, typename InputIterator4, typename OutputIterator1, typename OutputIterator2>
thrust::pair<OutputIterator1, OutputIterator2> set_difference_by_key(InputIterator1 keys_first1, InputIterator1 keys_last1, InputIterator2 keys_first2, InputIterator2 keys_last2, InputIterator3 values_first1, InputIterator4 values_first2, OutputIterator1 keys_result, OutputIterator2 values_result)#

set_difference_by_key performs a key-value difference operation from set theory. set_difference_by_key constructs a sorted range that is the difference of the sorted ranges [keys_first1, keys_last1) and [keys_first2, keys_last2). Associated with each element from the input and output key ranges is a value element. The associated input value ranges need not be sorted.

In the simplest case, set_difference_by_key performs the “difference” operation from set theory: the keys output range contains a copy of every element that is contained in [keys_first1, keys_last1) and not contained in [keys_first2, keys_last2). The general case is more complicated, because the input ranges may contain duplicate elements. The generalization is that if [keys_first1, keys_last1) contains m elements that are equivalent to each other and if [keys_first2, keys_last2) contains n elements that are equivalent to them, the last max(m-n,0) elements from [keys_first1, keys_last1) range shall be copied to the output range.

Each time a key element is copied from [keys_first1, keys_last1) or [keys_first2, keys_last2) is copied to the keys output range, the corresponding value element is copied from the corresponding values input range (beginning at values_first1 or values_first2) to the values output range.

This version of set_difference_by_key compares key elements using operator<.

The following code snippet demonstrates how to use set_difference_by_key to compute the set difference of two sets of integers sorted in ascending order with their values.

#include <thrust/set_operations.h>
...
int A_keys[6] = {0, 1, 3, 4, 5, 6, 9};
int A_vals[6] = {0, 0, 0, 0, 0, 0, 0};

int B_keys[5] = {1, 3, 5, 7, 9};
int B_vals[5] = {1, 1, 1, 1, 1};

int keys_result[3];
int vals_result[3];

thrust::pair<int*,int*> end = thrust::set_difference_by_key(A_keys, A_keys + 6, B_keys, B_keys + 5, A_vals, B_vals, keys_result, vals_result);
// keys_result is now {0, 4, 6}
// vals_result is now {0, 0, 0}

See also

set_union_by_key

See also

sort_by_key

See also

is_sorted

Parameters:
  • keys_first1 – The beginning of the first input range of keys.

  • keys_last1 – The end of the first input range of keys.

  • keys_first2 – The beginning of the second input range of keys.

  • keys_last2 – The end of the second input range of keys.

  • values_first1 – The beginning of the first input range of values.

  • values_first2 – The beginning of the first input range of values.

  • keys_result – The beginning of the output range of keys.

  • values_result – The beginning of the output range of values.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator, InputIterator1 and InputIterator2 have the same value_type, InputIterator1's value_type is a model of LessThan Comparable, the ordering on InputIterator1's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator1's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2 and InputIterator1 have the same value_type, InputIterator2's value_type is a model of LessThan Comparable, the ordering on InputIterator2's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator2's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator3 – is a model of Input Iterator, and InputIterator3's value_type is convertible to a type in OutputIterator2's set of value_types.

  • InputIterator4 – is a model of Input Iterator, and InputIterator4's value_type is convertible to a type in OutputIterator2's set of value_types.

  • OutputIterator1 – is a model of Output Iterator.

  • OutputIterator2 – is a model of Output Iterator.

Returns:

A pair p such that p.first is the end of the output range of keys, and such that p.second is the end of the output range of values.

Pre:

The ranges [keys_first1, keys_last1) and [keys_first2, keys_last2) shall be sorted with respect to operator<.

Pre:

The resulting ranges shall not overlap with any input range.

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename InputIterator3, typename InputIterator4, typename OutputIterator1, typename OutputIterator2, typename StrictWeakCompare> __host__ __device__ thrust::pair< OutputIterator1, OutputIterator2 > set_difference_by_key (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 keys_first1, InputIterator1 keys_last1, InputIterator2 keys_first2, InputIterator2 keys_last2, InputIterator3 values_first1, InputIterator4 values_first2, OutputIterator1 keys_result, OutputIterator2 values_result, StrictWeakCompare comp)

set_difference_by_key performs a key-value difference operation from set theory. set_difference_by_key constructs a sorted range that is the difference of the sorted ranges [keys_first1, keys_last1) and [keys_first2, keys_last2). Associated with each element from the input and output key ranges is a value element. The associated input value ranges need not be sorted.

In the simplest case, set_difference_by_key performs the “difference” operation from set theory: the keys output range contains a copy of every element that is contained in [keys_first1, keys_last1) and not contained in [keys_first2, keys_last2). The general case is more complicated, because the input ranges may contain duplicate elements. The generalization is that if [keys_first1, keys_last1) contains m elements that are equivalent to each other and if [keys_first2, keys_last2) contains n elements that are equivalent to them, the last max(m-n,0) elements from [keys_first1, keys_last1) range shall be copied to the output range.

Each time a key element is copied from [keys_first1, keys_last1) or [keys_first2, keys_last2) is copied to the keys output range, the corresponding value element is copied from the corresponding values input range (beginning at values_first1 or values_first2) to the values output range.

This version of set_difference_by_key compares key elements using a function object comp.

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

The following code snippet demonstrates how to use set_difference_by_key to compute the set difference of two sets of integers sorted in descending order with their values using the thrust::host execution policy for parallelization:

#include <thrust/set_operations.h>
#include <thrust/functional.h>
#include <thrust/execution_policy.h>
...
int A_keys[6] = {9, 6, 5, 4, 3, 1, 0};
int A_vals[6] = {0, 0, 0, 0, 0, 0, 0};

int B_keys[5] = {9, 7, 5, 3, 1};
int B_vals[5] = {1, 1, 1, 1, 1};

int keys_result[3];
int vals_result[3];

thrust::pair<int*,int*> end = thrust::set_difference_by_key(thrust::host, A_keys, A_keys + 6, B_keys, B_keys + 5, A_vals, B_vals, keys_result, vals_result, thrust::greater<int>());
// keys_result is now {0, 4, 6}
// vals_result is now {0, 0, 0}

See also

set_union_by_key

See also

sort_by_key

See also

is_sorted

Parameters:
  • exec – The execution policy to use for parallelization.

  • keys_first1 – The beginning of the first input range of keys.

  • keys_last1 – The end of the first input range of keys.

  • keys_first2 – The beginning of the second input range of keys.

  • keys_last2 – The end of the second input range of keys.

  • values_first1 – The beginning of the first input range of values.

  • values_first2 – The beginning of the first input range of values.

  • keys_result – The beginning of the output range of keys.

  • values_result – The beginning of the output range of values.

  • comp – Comparison operator.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator, InputIterator1 and InputIterator2 have the same value_type, InputIterator1's value_type is a model of LessThan Comparable, the ordering on InputIterator1's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator1's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2 and InputIterator1 have the same value_type, InputIterator2's value_type is a model of LessThan Comparable, the ordering on InputIterator2's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator2's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator3 – is a model of Input Iterator, and InputIterator3's value_type is convertible to a type in OutputIterator2's set of value_types.

  • InputIterator4 – is a model of Input Iterator, and InputIterator4's value_type is convertible to a type in OutputIterator2's set of value_types.

  • OutputIterator1 – is a model of Output Iterator.

  • OutputIterator2 – is a model of Output Iterator.

  • StrictWeakCompare – is a model of Strict Weak Ordering.

Returns:

A pair p such that p.first is the end of the output range of keys, and such that p.second is the end of the output range of values.

Pre:

The ranges [keys_first1, keys_last1) and [keys_first2, keys_last2) shall be sorted with respect to comp.

Pre:

The resulting ranges shall not overlap with any input range.

template<typename InputIterator1, typename InputIterator2, typename InputIterator3, typename InputIterator4, typename OutputIterator1, typename OutputIterator2, typename StrictWeakCompare>
thrust::pair<OutputIterator1, OutputIterator2> set_difference_by_key(InputIterator1 keys_first1, InputIterator1 keys_last1, InputIterator2 keys_first2, InputIterator2 keys_last2, InputIterator3 values_first1, InputIterator4 values_first2, OutputIterator1 keys_result, OutputIterator2 values_result, StrictWeakCompare comp)#

set_difference_by_key performs a key-value difference operation from set theory. set_difference_by_key constructs a sorted range that is the difference of the sorted ranges [keys_first1, keys_last1) and [keys_first2, keys_last2). Associated with each element from the input and output key ranges is a value element. The associated input value ranges need not be sorted.

In the simplest case, set_difference_by_key performs the “difference” operation from set theory: the keys output range contains a copy of every element that is contained in [keys_first1, keys_last1) and not contained in [keys_first2, keys_last2). The general case is more complicated, because the input ranges may contain duplicate elements. The generalization is that if [keys_first1, keys_last1) contains m elements that are equivalent to each other and if [keys_first2, keys_last2) contains n elements that are equivalent to them, the last max(m-n,0) elements from [keys_first1, keys_last1) range shall be copied to the output range.

Each time a key element is copied from [keys_first1, keys_last1) or [keys_first2, keys_last2) is copied to the keys output range, the corresponding value element is copied from the corresponding values input range (beginning at values_first1 or values_first2) to the values output range.

This version of set_difference_by_key compares key elements using a function object comp.

The following code snippet demonstrates how to use set_difference_by_key to compute the set difference of two sets of integers sorted in descending order with their values.

#include <thrust/set_operations.h>
#include <thrust/functional.h>
...
int A_keys[6] = {9, 6, 5, 4, 3, 1, 0};
int A_vals[6] = {0, 0, 0, 0, 0, 0, 0};

int B_keys[5] = {9, 7, 5, 3, 1};
int B_vals[5] = {1, 1, 1, 1, 1};

int keys_result[3];
int vals_result[3];

thrust::pair<int*,int*> end = thrust::set_difference_by_key(A_keys, A_keys + 6, B_keys, B_keys + 5, A_vals, B_vals, keys_result, vals_result, thrust::greater<int>());
// keys_result is now {0, 4, 6}
// vals_result is now {0, 0, 0}

See also

set_union_by_key

See also

sort_by_key

See also

is_sorted

Parameters:
  • keys_first1 – The beginning of the first input range of keys.

  • keys_last1 – The end of the first input range of keys.

  • keys_first2 – The beginning of the second input range of keys.

  • keys_last2 – The end of the second input range of keys.

  • values_first1 – The beginning of the first input range of values.

  • values_first2 – The beginning of the first input range of values.

  • keys_result – The beginning of the output range of keys.

  • values_result – The beginning of the output range of values.

  • comp – Comparison operator.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator, InputIterator1 and InputIterator2 have the same value_type, InputIterator1's value_type is a model of LessThan Comparable, the ordering on InputIterator1's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator1's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2 and InputIterator1 have the same value_type, InputIterator2's value_type is a model of LessThan Comparable, the ordering on InputIterator2's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator2's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator3 – is a model of Input Iterator, and InputIterator3's value_type is convertible to a type in OutputIterator2's set of value_types.

  • InputIterator4 – is a model of Input Iterator, and InputIterator4's value_type is convertible to a type in OutputIterator2's set of value_types.

  • OutputIterator1 – is a model of Output Iterator.

  • OutputIterator2 – is a model of Output Iterator.

  • StrictWeakCompare – is a model of Strict Weak Ordering.

Returns:

A pair p such that p.first is the end of the output range of keys, and such that p.second is the end of the output range of values.

Pre:

The ranges [keys_first1, keys_last1) and [keys_first2, keys_last2) shall be sorted with respect to comp.

Pre:

The resulting ranges shall not overlap with any input range.

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename InputIterator3, typename OutputIterator1, typename OutputIterator2> __host__ __device__ thrust::pair< OutputIterator1, OutputIterator2 > set_intersection_by_key (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 keys_first1, InputIterator1 keys_last1, InputIterator2 keys_first2, InputIterator2 keys_last2, InputIterator3 values_first1, OutputIterator1 keys_result, OutputIterator2 values_result)

set_intersection_by_key performs a key-value intersection operation from set theory. set_intersection_by_key constructs a sorted range that is the intersection of the sorted ranges [keys_first1, keys_last1) and [keys_first2, keys_last2). Associated with each element from the input and output key ranges is a value element. The associated input value ranges need not be sorted.

In the simplest case, set_intersection_by_key performs the “intersection” operation from set theory: the keys output range contains a copy of every element that is contained in both [keys_first1, keys_last1) [keys_first2, keys_last2). The general case is more complicated, because the input ranges may contain duplicate elements. The generalization is that if an element appears m times in [keys_first1, keys_last1) and n times in [keys_first2, keys_last2) (where m may be zero), then it appears min(m,n) times in the keys output range. set_intersection_by_key is stable, meaning both that elements are copied from the first input range rather than the second, and that the relative order of elements in the output range is the same as the first input range.

Each time a key element is copied from [keys_first1, keys_last1) to the keys output range, the corresponding value element is copied from [values_first1, values_last1) to the values output range.

This version of set_intersection_by_key compares objects using operator<.

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

The following code snippet demonstrates how to use set_intersection_by_key to compute the set intersection of two sets of integers sorted in ascending order with their values using the thrust::host execution policy for parallelization:

#include <thrust/set_operations.h>
#include <thrust/execution_policy.h>
...
int A_keys[6] = {1, 3, 5, 7, 9, 11};
int A_vals[6] = {0, 0, 0, 0, 0,  0};

int B_keys[7] = {1, 1, 2, 3, 5,  8, 13};

int keys_result[7];
int vals_result[7];

thrust::pair<int*,int*> end = thrust::set_intersection_by_key(thrust::host, A_keys, A_keys + 6, B_keys, B_keys + 7, A_vals, keys_result, vals_result);

// keys_result is now {1, 3, 5}
// vals_result is now {0, 0, 0}

See also

set_union_by_key

See also

sort_by_key

See also

is_sorted

Note

Unlike the other key-value set operations, set_intersection_by_key is unique in that it has no values_first2 parameter because elements from the second input range are never copied to the output range.

Parameters:
  • exec – The execution policy to use for parallelization.

  • keys_first1 – The beginning of the first input range of keys.

  • keys_last1 – The end of the first input range of keys.

  • keys_first2 – The beginning of the second input range of keys.

  • keys_last2 – The end of the second input range of keys.

  • values_first1 – The beginning of the first input range of values.

  • keys_result – The beginning of the output range of keys.

  • values_result – The beginning of the output range of values.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator, InputIterator1 and InputIterator2 have the same value_type, InputIterator1's value_type is a model of LessThan Comparable, the ordering on InputIterator1's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator1's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2 and InputIterator1 have the same value_type, InputIterator2's value_type is a model of LessThan Comparable, the ordering on InputIterator2's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator2's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator3 – is a model of Input Iterator, and InputIterator3's value_type is convertible to a type in OutputIterator2's set of value_types.

  • OutputIterator1 – is a model of Output Iterator.

  • OutputIterator2 – is a model of Output Iterator.

Returns:

A pair p such that p.first is the end of the output range of keys, and such that p.second is the end of the output range of values.

Pre:

The ranges [keys_first1, keys_last1) and [keys_first2, keys_last2) shall be sorted with respect to operator<.

Pre:

The resulting ranges shall not overlap with any input range.

template<typename InputIterator1, typename InputIterator2, typename InputIterator3, typename OutputIterator1, typename OutputIterator2>
thrust::pair<OutputIterator1, OutputIterator2> set_intersection_by_key(InputIterator1 keys_first1, InputIterator1 keys_last1, InputIterator2 keys_first2, InputIterator2 keys_last2, InputIterator3 values_first1, OutputIterator1 keys_result, OutputIterator2 values_result)#

set_intersection_by_key performs a key-value intersection operation from set theory. set_intersection_by_key constructs a sorted range that is the intersection of the sorted ranges [keys_first1, keys_last1) and [keys_first2, keys_last2). Associated with each element from the input and output key ranges is a value element. The associated input value ranges need not be sorted.

In the simplest case, set_intersection_by_key performs the “intersection” operation from set theory: the keys output range contains a copy of every element that is contained in both [keys_first1, keys_last1) [keys_first2, keys_last2). The general case is more complicated, because the input ranges may contain duplicate elements. The generalization is that if an element appears m times in [keys_first1, keys_last1) and n times in [keys_first2, keys_last2) (where m may be zero), then it appears min(m,n) times in the keys output range. set_intersection_by_key is stable, meaning both that elements are copied from the first input range rather than the second, and that the relative order of elements in the output range is the same as the first input range.

Each time a key element is copied from [keys_first1, keys_last1) to the keys output range, the corresponding value element is copied from [values_first1, values_last1) to the values output range.

This version of set_intersection_by_key compares objects using operator<.

The following code snippet demonstrates how to use set_intersection_by_key to compute the set intersection of two sets of integers sorted in ascending order with their values.

#include <thrust/set_operations.h>
...
int A_keys[6] = {1, 3, 5, 7, 9, 11};
int A_vals[6] = {0, 0, 0, 0, 0,  0};

int B_keys[7] = {1, 1, 2, 3, 5,  8, 13};

int keys_result[7];
int vals_result[7];

thrust::pair<int*,int*> end = thrust::set_intersection_by_key(A_keys, A_keys + 6, B_keys, B_keys + 7, A_vals, keys_result, vals_result);

// keys_result is now {1, 3, 5}
// vals_result is now {0, 0, 0}

See also

set_union_by_key

See also

sort_by_key

See also

is_sorted

Note

Unlike the other key-value set operations, set_intersection_by_key is unique in that it has no values_first2 parameter because elements from the second input range are never copied to the output range.

Parameters:
  • keys_first1 – The beginning of the first input range of keys.

  • keys_last1 – The end of the first input range of keys.

  • keys_first2 – The beginning of the second input range of keys.

  • keys_last2 – The end of the second input range of keys.

  • values_first1 – The beginning of the first input range of values.

  • keys_result – The beginning of the output range of keys.

  • values_result – The beginning of the output range of values.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator, InputIterator1 and InputIterator2 have the same value_type, InputIterator1's value_type is a model of LessThan Comparable, the ordering on InputIterator1's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator1's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2 and InputIterator1 have the same value_type, InputIterator2's value_type is a model of LessThan Comparable, the ordering on InputIterator2's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator2's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator3 – is a model of Input Iterator, and InputIterator3's value_type is convertible to a type in OutputIterator2's set of value_types.

  • OutputIterator1 – is a model of Output Iterator.

  • OutputIterator2 – is a model of Output Iterator.

Returns:

A pair p such that p.first is the end of the output range of keys, and such that p.second is the end of the output range of values.

Pre:

The ranges [keys_first1, keys_last1) and [keys_first2, keys_last2) shall be sorted with respect to operator<.

Pre:

The resulting ranges shall not overlap with any input range.

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename InputIterator3, typename OutputIterator1, typename OutputIterator2, typename StrictWeakCompare> __host__ __device__ thrust::pair< OutputIterator1, OutputIterator2 > set_intersection_by_key (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 keys_first1, InputIterator1 keys_last1, InputIterator2 keys_first2, InputIterator2 keys_last2, InputIterator3 values_first1, OutputIterator1 keys_result, OutputIterator2 values_result, StrictWeakCompare comp)

set_intersection_by_key performs a key-value intersection operation from set theory. set_intersection_by_key constructs a sorted range that is the intersection of the sorted ranges [keys_first1, keys_last1) and [keys_first2, keys_last2). Associated with each element from the input and output key ranges is a value element. The associated input value ranges need not be sorted.

In the simplest case, set_intersection_by_key performs the “intersection” operation from set theory: the keys output range contains a copy of every element that is contained in both [keys_first1, keys_last1) [keys_first2, keys_last2). The general case is more complicated, because the input ranges may contain duplicate elements. The generalization is that if an element appears m times in [keys_first1, keys_last1) and n times in [keys_first2, keys_last2) (where m may be zero), then it appears min(m,n) times in the keys output range. set_intersection_by_key is stable, meaning both that elements are copied from the first input range rather than the second, and that the relative order of elements in the output range is the same as the first input range.

Each time a key element is copied from [keys_first1, keys_last1) to the keys output range, the corresponding value element is copied from [values_first1, values_last1) to the values output range.

This version of set_intersection_by_key compares objects using a function object comp.

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

The following code snippet demonstrates how to use set_intersection_by_key to compute the set intersection of two sets of integers sorted in descending order with their values using the thrust::host execution policy for parallelization:

#include <thrust/set_operations.h>
#include <thrust/functional.h>
#include <thrust/execution_policy.h>
...
int A_keys[6] = {11, 9, 7, 5, 3, 1};
int A_vals[6] = { 0, 0, 0, 0, 0, 0};

int B_keys[7] = {13, 8, 5, 3, 2, 1, 1};

int keys_result[7];
int vals_result[7];

thrust::pair<int*,int*> end = thrust::set_intersection_by_key(thrust::host, A_keys, A_keys + 6, B_keys, B_keys + 7, A_vals, keys_result, vals_result, thrust::greater<int>());

// keys_result is now {5, 3, 1}
// vals_result is now {0, 0, 0}

See also

set_union_by_key

See also

sort_by_key

See also

is_sorted

Note

Unlike the other key-value set operations, set_intersection_by_key is unique in that it has no values_first2 parameter because elements from the second input range are never copied to the output range.

Parameters:
  • exec – The execution policy to use for parallelization.

  • keys_first1 – The beginning of the first input range of keys.

  • keys_last1 – The end of the first input range of keys.

  • keys_first2 – The beginning of the second input range of keys.

  • keys_last2 – The end of the second input range of keys.

  • values_first1 – The beginning of the first input range of values.

  • keys_result – The beginning of the output range of keys.

  • values_result – The beginning of the output range of values.

  • comp – Comparison operator.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator, InputIterator1 and InputIterator2 have the same value_type, InputIterator1's value_type is a model of LessThan Comparable, the ordering on InputIterator1's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator1's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2 and InputIterator1 have the same value_type, InputIterator2's value_type is a model of LessThan Comparable, the ordering on InputIterator2's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator2's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator3 – is a model of Input Iterator, and InputIterator3's value_type is convertible to a type in OutputIterator2's set of value_types.

  • OutputIterator1 – is a model of Output Iterator.

  • OutputIterator2 – is a model of Output Iterator.

  • StrictWeakCompare – is a model of Strict Weak Ordering.

Returns:

A pair p such that p.first is the end of the output range of keys, and such that p.second is the end of the output range of values.

Pre:

The ranges [keys_first1, keys_last1) and [keys_first2, keys_last2) shall be sorted with respect to comp.

Pre:

The resulting ranges shall not overlap with any input range.

template<typename InputIterator1, typename InputIterator2, typename InputIterator3, typename OutputIterator1, typename OutputIterator2, typename StrictWeakCompare>
thrust::pair<OutputIterator1, OutputIterator2> set_intersection_by_key(InputIterator1 keys_first1, InputIterator1 keys_last1, InputIterator2 keys_first2, InputIterator2 keys_last2, InputIterator3 values_first1, OutputIterator1 keys_result, OutputIterator2 values_result, StrictWeakCompare comp)#

set_intersection_by_key performs a key-value intersection operation from set theory. set_intersection_by_key constructs a sorted range that is the intersection of the sorted ranges [keys_first1, keys_last1) and [keys_first2, keys_last2). Associated with each element from the input and output key ranges is a value element. The associated input value ranges need not be sorted.

In the simplest case, set_intersection_by_key performs the “intersection” operation from set theory: the keys output range contains a copy of every element that is contained in both [keys_first1, keys_last1) [keys_first2, keys_last2). The general case is more complicated, because the input ranges may contain duplicate elements. The generalization is that if an element appears m times in [keys_first1, keys_last1) and n times in [keys_first2, keys_last2) (where m may be zero), then it appears min(m,n) times in the keys output range. set_intersection_by_key is stable, meaning both that elements are copied from the first input range rather than the second, and that the relative order of elements in the output range is the same as the first input range.

Each time a key element is copied from [keys_first1, keys_last1) to the keys output range, the corresponding value element is copied from [values_first1, values_last1) to the values output range.

This version of set_intersection_by_key compares objects using a function object comp.

The following code snippet demonstrates how to use set_intersection_by_key to compute the set intersection of two sets of integers sorted in descending order with their values.

#include <thrust/set_operations.h>
#include <thrust/functional.h>
...
int A_keys[6] = {11, 9, 7, 5, 3, 1};
int A_vals[6] = { 0, 0, 0, 0, 0, 0};

int B_keys[7] = {13, 8, 5, 3, 2, 1, 1};

int keys_result[7];
int vals_result[7];

thrust::pair<int*,int*> end = thrust::set_intersection_by_key(A_keys, A_keys + 6, B_keys, B_keys + 7, A_vals, keys_result, vals_result, thrust::greater<int>());

// keys_result is now {5, 3, 1}
// vals_result is now {0, 0, 0}

See also

set_union_by_key

See also

sort_by_key

See also

is_sorted

Note

Unlike the other key-value set operations, set_intersection_by_key is unique in that it has no values_first2 parameter because elements from the second input range are never copied to the output range.

Parameters:
  • keys_first1 – The beginning of the first input range of keys.

  • keys_last1 – The end of the first input range of keys.

  • keys_first2 – The beginning of the second input range of keys.

  • keys_last2 – The end of the second input range of keys.

  • values_first1 – The beginning of the first input range of values.

  • keys_result – The beginning of the output range of keys.

  • values_result – The beginning of the output range of values.

  • comp – Comparison operator.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator, InputIterator1 and InputIterator2 have the same value_type, InputIterator1's value_type is a model of LessThan Comparable, the ordering on InputIterator1's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator1's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2 and InputIterator1 have the same value_type, InputIterator2's value_type is a model of LessThan Comparable, the ordering on InputIterator2's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator2's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator3 – is a model of Input Iterator, and InputIterator3's value_type is convertible to a type in OutputIterator2's set of value_types.

  • OutputIterator1 – is a model of Output Iterator.

  • OutputIterator2 – is a model of Output Iterator.

  • StrictWeakCompare – is a model of Strict Weak Ordering.

Returns:

A pair p such that p.first is the end of the output range of keys, and such that p.second is the end of the output range of values.

Pre:

The ranges [keys_first1, keys_last1) and [keys_first2, keys_last2) shall be sorted with respect to comp.

Pre:

The resulting ranges shall not overlap with any input range.

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename InputIterator3, typename InputIterator4, typename OutputIterator1, typename OutputIterator2> __host__ __device__ thrust::pair< OutputIterator1, OutputIterator2 > set_symmetric_difference_by_key (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 keys_first1, InputIterator1 keys_last1, InputIterator2 keys_first2, InputIterator2 keys_last2, InputIterator3 values_first1, InputIterator4 values_first2, OutputIterator1 keys_result, OutputIterator2 values_result)

set_symmetric_difference_by_key performs a key-value symmetric difference operation from set theory. set_difference_by_key constructs a sorted range that is the symmetric difference of the sorted ranges [keys_first1, keys_last1) and [keys_first2, keys_last2). Associated with each element from the input and output key ranges is a value element. The associated input value ranges need not be sorted.

In the simplest case, set_symmetric_difference_by_key performs a set theoretic calculation: it constructs the union of the two sets A - B and B - A, where A and B are the two input ranges. That is, the output range contains a copy of every element that is contained in [keys_first1, keys_last1) but not [keys_first2, keys_last1), and a copy of every element that is contained in [keys_first2, keys_last2) but not [keys_first1, keys_last1). The general case is more complicated, because the input ranges may contain duplicate elements. The generalization is that if [keys_first1, keys_last1) contains m elements that are equivalent to each other and [keys_first2, keys_last1) contains n elements that are equivalent to them, then |m - n| of those elements shall be copied to the output range: the last m - n elements from [keys_first1, keys_last1) if m > n, and the last n - m of these elements from [keys_first2, keys_last2) if m < n.

Each time a key element is copied from [keys_first1, keys_last1) or [keys_first2, keys_last2) is copied to the keys output range, the corresponding value element is copied from the corresponding values input range (beginning at values_first1 or values_first2) to the values output range.

This version of set_symmetric_difference_by_key compares key elements using operator<.

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

The following code snippet demonstrates how to use set_symmetric_difference_by_key to compute the symmetric difference of two sets of integers sorted in ascending order with their values using the thrust::host execution policy for parallelization:

#include <thrust/set_operations.h>
#include <thrust/execution_policy.h>
...
int A_keys[6] = {0, 1, 2, 2, 4, 6, 7};
int A_vals[6] = {0, 0, 0, 0, 0, 0, 0};

int B_keys[5] = {1, 1, 2, 5, 8};
int B_vals[5] = {1, 1, 1, 1, 1};

int keys_result[6];
int vals_result[6];

thrust::pair<int*,int*> end = thrust::set_symmetric_difference_by_key(thrust::host, A_keys, A_keys + 6, B_keys, B_keys + 5, A_vals, B_vals, keys_result, vals_result);
// keys_result is now {0, 4, 5, 6, 7, 8}
// vals_result is now {0, 0, 1, 0, 0, 1}

See also

set_union_by_key

See also

sort_by_key

See also

is_sorted

Parameters:
  • exec – The execution policy to use for parallelization.

  • keys_first1 – The beginning of the first input range of keys.

  • keys_last1 – The end of the first input range of keys.

  • keys_first2 – The beginning of the second input range of keys.

  • keys_last2 – The end of the second input range of keys.

  • values_first1 – The beginning of the first input range of values.

  • values_first2 – The beginning of the first input range of values.

  • keys_result – The beginning of the output range of keys.

  • values_result – The beginning of the output range of values.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator, InputIterator1 and InputIterator2 have the same value_type, InputIterator1's value_type is a model of LessThan Comparable, the ordering on InputIterator1's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator1's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2 and InputIterator1 have the same value_type, InputIterator2's value_type is a model of LessThan Comparable, the ordering on InputIterator2's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator2's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator3 – is a model of Input Iterator, and InputIterator3's value_type is convertible to a type in OutputIterator2's set of value_types.

  • InputIterator4 – is a model of Input Iterator, and InputIterator4's value_type is convertible to a type in OutputIterator2's set of value_types.

  • OutputIterator1 – is a model of Output Iterator.

  • OutputIterator2 – is a model of Output Iterator.

Returns:

A pair p such that p.first is the end of the output range of keys, and such that p.second is the end of the output range of values.

Pre:

The ranges [keys_first1, keys_last1) and [keys_first2, keys_last2) shall be sorted with respect to operator<.

Pre:

The resulting ranges shall not overlap with any input range.

template<typename InputIterator1, typename InputIterator2, typename InputIterator3, typename InputIterator4, typename OutputIterator1, typename OutputIterator2>
thrust::pair<OutputIterator1, OutputIterator2> set_symmetric_difference_by_key(InputIterator1 keys_first1, InputIterator1 keys_last1, InputIterator2 keys_first2, InputIterator2 keys_last2, InputIterator3 values_first1, InputIterator4 values_first2, OutputIterator1 keys_result, OutputIterator2 values_result)#

set_symmetric_difference_by_key performs a key-value symmetric difference operation from set theory. set_difference_by_key constructs a sorted range that is the symmetric difference of the sorted ranges [keys_first1, keys_last1) and [keys_first2, keys_last2). Associated with each element from the input and output key ranges is a value element. The associated input value ranges need not be sorted.

In the simplest case, set_symmetric_difference_by_key performs a set theoretic calculation: it constructs the union of the two sets A - B and B - A, where A and B are the two input ranges. That is, the output range contains a copy of every element that is contained in [keys_first1, keys_last1) but not [keys_first2, keys_last1), and a copy of every element that is contained in [keys_first2, keys_last2) but not [keys_first1, keys_last1). The general case is more complicated, because the input ranges may contain duplicate elements. The generalization is that if [keys_first1, keys_last1) contains m elements that are equivalent to each other and [keys_first2, keys_last1) contains n elements that are equivalent to them, then |m - n| of those elements shall be copied to the output range: the last m - n elements from [keys_first1, keys_last1) if m > n, and the last n - m of these elements from [keys_first2, keys_last2) if m < n.

Each time a key element is copied from [keys_first1, keys_last1) or [keys_first2, keys_last2) is copied to the keys output range, the corresponding value element is copied from the corresponding values input range (beginning at values_first1 or values_first2) to the values output range.

This version of set_symmetric_difference_by_key compares key elements using operator<.

The following code snippet demonstrates how to use set_symmetric_difference_by_key to compute the symmetric difference of two sets of integers sorted in ascending order with their values.

#include <thrust/set_operations.h>
...
int A_keys[6] = {0, 1, 2, 2, 4, 6, 7};
int A_vals[6] = {0, 0, 0, 0, 0, 0, 0};

int B_keys[5] = {1, 1, 2, 5, 8};
int B_vals[5] = {1, 1, 1, 1, 1};

int keys_result[6];
int vals_result[6];

thrust::pair<int*,int*> end = thrust::set_symmetric_difference_by_key(A_keys, A_keys + 6, B_keys, B_keys + 5, A_vals, B_vals, keys_result, vals_result);
// keys_result is now {0, 4, 5, 6, 7, 8}
// vals_result is now {0, 0, 1, 0, 0, 1}

See also

set_union_by_key

See also

sort_by_key

See also

is_sorted

Parameters:
  • keys_first1 – The beginning of the first input range of keys.

  • keys_last1 – The end of the first input range of keys.

  • keys_first2 – The beginning of the second input range of keys.

  • keys_last2 – The end of the second input range of keys.

  • values_first1 – The beginning of the first input range of values.

  • values_first2 – The beginning of the first input range of values.

  • keys_result – The beginning of the output range of keys.

  • values_result – The beginning of the output range of values.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator, InputIterator1 and InputIterator2 have the same value_type, InputIterator1's value_type is a model of LessThan Comparable, the ordering on InputIterator1's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator1's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2 and InputIterator1 have the same value_type, InputIterator2's value_type is a model of LessThan Comparable, the ordering on InputIterator2's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator2's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator3 – is a model of Input Iterator, and InputIterator3's value_type is convertible to a type in OutputIterator2's set of value_types.

  • InputIterator4 – is a model of Input Iterator, and InputIterator4's value_type is convertible to a type in OutputIterator2's set of value_types.

  • OutputIterator1 – is a model of Output Iterator.

  • OutputIterator2 – is a model of Output Iterator.

Returns:

A pair p such that p.first is the end of the output range of keys, and such that p.second is the end of the output range of values.

Pre:

The ranges [keys_first1, keys_last1) and [keys_first2, keys_last2) shall be sorted with respect to operator<.

Pre:

The resulting ranges shall not overlap with any input range.

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename InputIterator3, typename InputIterator4, typename OutputIterator1, typename OutputIterator2, typename StrictWeakCompare> __host__ __device__ thrust::pair< OutputIterator1, OutputIterator2 > set_symmetric_difference_by_key (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 keys_first1, InputIterator1 keys_last1, InputIterator2 keys_first2, InputIterator2 keys_last2, InputIterator3 values_first1, InputIterator4 values_first2, OutputIterator1 keys_result, OutputIterator2 values_result, StrictWeakCompare comp)

set_symmetric_difference_by_key performs a key-value symmetric difference operation from set theory. set_difference_by_key constructs a sorted range that is the symmetric difference of the sorted ranges [keys_first1, keys_last1) and [keys_first2, keys_last2). Associated with each element from the input and output key ranges is a value element. The associated input value ranges need not be sorted.

In the simplest case, set_symmetric_difference_by_key performs a set theoretic calculation: it constructs the union of the two sets A - B and B - A, where A and B are the two input ranges. That is, the output range contains a copy of every element that is contained in [keys_first1, keys_last1) but not [keys_first2, keys_last1), and a copy of every element that is contained in [keys_first2, keys_last2) but not [keys_first1, keys_last1). The general case is more complicated, because the input ranges may contain duplicate elements. The generalization is that if [keys_first1, keys_last1) contains m elements that are equivalent to each other and [keys_first2, keys_last1) contains n elements that are equivalent to them, then |m - n| of those elements shall be copied to the output range: the last m - n elements from [keys_first1, keys_last1) if m > n, and the last n - m of these elements from [keys_first2, keys_last2) if m < n.

Each time a key element is copied from [keys_first1, keys_last1) or [keys_first2, keys_last2) is copied to the keys output range, the corresponding value element is copied from the corresponding values input range (beginning at values_first1 or values_first2) to the values output range.

This version of set_symmetric_difference_by_key compares key elements using a function object comp.

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

The following code snippet demonstrates how to use set_symmetric_difference_by_key to compute the symmetric difference of two sets of integers sorted in descending order with their values using the thrust::host execution policy for parallelization:

#include <thrust/set_operations.h>
#include <thrust/functional.h>
#include <thrust/execution_policy.h>
...
int A_keys[6] = {7, 6, 4, 2, 2, 1, 0};
int A_vals[6] = {0, 0, 0, 0, 0, 0, 0};

int B_keys[5] = {8, 5, 2, 1, 1};
int B_vals[5] = {1, 1, 1, 1, 1};

int keys_result[6];
int vals_result[6];

thrust::pair<int*,int*> end = thrust::set_symmetric_difference_by_key(thrust::host, A_keys, A_keys + 6, B_keys, B_keys + 5, A_vals, B_vals, keys_result, vals_result);
// keys_result is now {8, 7, 6, 5, 4, 0}
// vals_result is now {1, 0, 0, 1, 0, 0}

See also

set_union_by_key

See also

sort_by_key

See also

is_sorted

Parameters:
  • exec – The execution policy to use for parallelization.

  • keys_first1 – The beginning of the first input range of keys.

  • keys_last1 – The end of the first input range of keys.

  • keys_first2 – The beginning of the second input range of keys.

  • keys_last2 – The end of the second input range of keys.

  • values_first1 – The beginning of the first input range of values.

  • values_first2 – The beginning of the first input range of values.

  • keys_result – The beginning of the output range of keys.

  • values_result – The beginning of the output range of values.

  • comp – Comparison operator.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator, InputIterator1 and InputIterator2 have the same value_type, InputIterator1's value_type is a model of LessThan Comparable, the ordering on InputIterator1's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator1's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2 and InputIterator1 have the same value_type, InputIterator2's value_type is a model of LessThan Comparable, the ordering on InputIterator2's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator2's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator3 – is a model of Input Iterator, and InputIterator3's value_type is convertible to a type in OutputIterator2's set of value_types.

  • InputIterator4 – is a model of Input Iterator, and InputIterator4's value_type is convertible to a type in OutputIterator2's set of value_types.

  • OutputIterator1 – is a model of Output Iterator.

  • OutputIterator2 – is a model of Output Iterator.

  • StrictWeakCompare – is a model of Strict Weak Ordering.

Returns:

A pair p such that p.first is the end of the output range of keys, and such that p.second is the end of the output range of values.

Pre:

The ranges [keys_first1, keys_last1) and [keys_first2, keys_last2) shall be sorted with respect to comp.

Pre:

The resulting ranges shall not overlap with any input range.

template<typename InputIterator1, typename InputIterator2, typename InputIterator3, typename InputIterator4, typename OutputIterator1, typename OutputIterator2, typename StrictWeakCompare>
thrust::pair<OutputIterator1, OutputIterator2> set_symmetric_difference_by_key(InputIterator1 keys_first1, InputIterator1 keys_last1, InputIterator2 keys_first2, InputIterator2 keys_last2, InputIterator3 values_first1, InputIterator4 values_first2, OutputIterator1 keys_result, OutputIterator2 values_result, StrictWeakCompare comp)#

set_symmetric_difference_by_key performs a key-value symmetric difference operation from set theory. set_difference_by_key constructs a sorted range that is the symmetric difference of the sorted ranges [keys_first1, keys_last1) and [keys_first2, keys_last2). Associated with each element from the input and output key ranges is a value element. The associated input value ranges need not be sorted.

In the simplest case, set_symmetric_difference_by_key performs a set theoretic calculation: it constructs the union of the two sets A - B and B - A, where A and B are the two input ranges. That is, the output range contains a copy of every element that is contained in [keys_first1, keys_last1) but not [keys_first2, keys_last1), and a copy of every element that is contained in [keys_first2, keys_last2) but not [keys_first1, keys_last1). The general case is more complicated, because the input ranges may contain duplicate elements. The generalization is that if [keys_first1, keys_last1) contains m elements that are equivalent to each other and [keys_first2, keys_last1) contains n elements that are equivalent to them, then |m - n| of those elements shall be copied to the output range: the last m - n elements from [keys_first1, keys_last1) if m > n, and the last n - m of these elements from [keys_first2, keys_last2) if m < n.

Each time a key element is copied from [keys_first1, keys_last1) or [keys_first2, keys_last2) is copied to the keys output range, the corresponding value element is copied from the corresponding values input range (beginning at values_first1 or values_first2) to the values output range.

This version of set_symmetric_difference_by_key compares key elements using a function object comp.

The following code snippet demonstrates how to use set_symmetric_difference_by_key to compute the symmetric difference of two sets of integers sorted in descending order with their values.

#include <thrust/set_operations.h>
#include <thrust/functional.h>
...
int A_keys[6] = {7, 6, 4, 2, 2, 1, 0};
int A_vals[6] = {0, 0, 0, 0, 0, 0, 0};

int B_keys[5] = {8, 5, 2, 1, 1};
int B_vals[5] = {1, 1, 1, 1, 1};

int keys_result[6];
int vals_result[6];

thrust::pair<int*,int*> end = thrust::set_symmetric_difference_by_key(A_keys, A_keys + 6, B_keys, B_keys + 5, A_vals, B_vals, keys_result, vals_result);
// keys_result is now {8, 7, 6, 5, 4, 0}
// vals_result is now {1, 0, 0, 1, 0, 0}

See also

set_union_by_key

See also

sort_by_key

See also

is_sorted

Parameters:
  • keys_first1 – The beginning of the first input range of keys.

  • keys_last1 – The end of the first input range of keys.

  • keys_first2 – The beginning of the second input range of keys.

  • keys_last2 – The end of the second input range of keys.

  • values_first1 – The beginning of the first input range of values.

  • values_first2 – The beginning of the first input range of values.

  • keys_result – The beginning of the output range of keys.

  • values_result – The beginning of the output range of values.

  • comp – Comparison operator.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator, InputIterator1 and InputIterator2 have the same value_type, InputIterator1's value_type is a model of LessThan Comparable, the ordering on InputIterator1's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator1's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2 and InputIterator1 have the same value_type, InputIterator2's value_type is a model of LessThan Comparable, the ordering on InputIterator2's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator2's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator3 – is a model of Input Iterator, and InputIterator3's value_type is convertible to a type in OutputIterator2's set of value_types.

  • InputIterator4 – is a model of Input Iterator, and InputIterator4's value_type is convertible to a type in OutputIterator2's set of value_types.

  • OutputIterator1 – is a model of Output Iterator.

  • OutputIterator2 – is a model of Output Iterator.

  • StrictWeakCompare – is a model of Strict Weak Ordering.

Returns:

A pair p such that p.first is the end of the output range of keys, and such that p.second is the end of the output range of values.

Pre:

The ranges [keys_first1, keys_last1) and [keys_first2, keys_last2) shall be sorted with respect to comp.

Pre:

The resulting ranges shall not overlap with any input range.

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename InputIterator3, typename InputIterator4, typename OutputIterator1, typename OutputIterator2> __host__ __device__ thrust::pair< OutputIterator1, OutputIterator2 > set_union_by_key (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 keys_first1, InputIterator1 keys_last1, InputIterator2 keys_first2, InputIterator2 keys_last2, InputIterator3 values_first1, InputIterator4 values_first2, OutputIterator1 keys_result, OutputIterator2 values_result)

set_union_by_key performs a key-value union operation from set theory. set_union_by_key constructs a sorted range that is the union of the sorted ranges [keys_first1, keys_last1) and [keys_first2, keys_last2). Associated with each element from the input and output key ranges is a value element. The associated input value ranges need not be sorted.

In the simplest case, set_union_by_key performs the “union” operation from set theory: the output range contains a copy of every element that is contained in [keys_first1, keys_last1), [keys_first2, keys_last1), or both. The general case is more complicated, because the input ranges may contain duplicate elements. The generalization is that if [keys_first1, keys_last1) contains m elements that are equivalent to each other and if [keys_first2, keys_last2) contains n elements that are equivalent to them, then all m elements from the first range shall be copied to the output range, in order, and then max(n - m, 0) elements from the second range shall be copied to the output, in order.

Each time a key element is copied from [keys_first1, keys_last1) or [keys_first2, keys_last2) is copied to the keys output range, the corresponding value element is copied from the corresponding values input range (beginning at values_first1 or values_first2) to the values output range.

This version of set_union_by_key compares key elements using operator<.

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

The following code snippet demonstrates how to use set_symmetric_difference_by_key to compute the symmetric difference of two sets of integers sorted in ascending order with their values using the thrust::host execution policy for parallelization:

#include <thrust/set_operations.h>
#include <thrust/execution_policy.h>
...
int A_keys[6] = {0, 2, 4, 6, 8, 10, 12};
int A_vals[6] = {0, 0, 0, 0, 0,  0,  0};

int B_keys[5] = {1, 3, 5, 7, 9};
int B_vals[5] = {1, 1, 1, 1, 1};

int keys_result[11];
int vals_result[11];

thrust::pair<int*,int*> end = thrust::set_symmetric_difference_by_key(thrust::host, A_keys, A_keys + 6, B_keys, B_keys + 5, A_vals, B_vals, keys_result, vals_result);
// keys_result is now {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12}
// vals_result is now {0, 1, 0, 1, 0, 1, 0, 1, 0, 1,  0,  0}

See also

sort_by_key

See also

is_sorted

Parameters:
  • exec – The execution policy to use for parallelization.

  • keys_first1 – The beginning of the first input range of keys.

  • keys_last1 – The end of the first input range of keys.

  • keys_first2 – The beginning of the second input range of keys.

  • keys_last2 – The end of the second input range of keys.

  • values_first1 – The beginning of the first input range of values.

  • values_first2 – The beginning of the first input range of values.

  • keys_result – The beginning of the output range of keys.

  • values_result – The beginning of the output range of values.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator, InputIterator1 and InputIterator2 have the same value_type, InputIterator1's value_type is a model of LessThan Comparable, the ordering on InputIterator1's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator1's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2 and InputIterator1 have the same value_type, InputIterator2's value_type is a model of LessThan Comparable, the ordering on InputIterator2's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator2's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator3 – is a model of Input Iterator, and InputIterator3's value_type is convertible to a type in OutputIterator2's set of value_types.

  • InputIterator4 – is a model of Input Iterator, and InputIterator4's value_type is convertible to a type in OutputIterator2's set of value_types.

  • OutputIterator1 – is a model of Output Iterator.

  • OutputIterator2 – is a model of Output Iterator.

Returns:

A pair p such that p.first is the end of the output range of keys, and such that p.second is the end of the output range of values.

Pre:

The ranges [keys_first1, keys_last1) and [keys_first2, keys_last2) shall be sorted with respect to operator<.

Pre:

The resulting ranges shall not overlap with any input range.

template<typename InputIterator1, typename InputIterator2, typename InputIterator3, typename InputIterator4, typename OutputIterator1, typename OutputIterator2>
thrust::pair<OutputIterator1, OutputIterator2> set_union_by_key(InputIterator1 keys_first1, InputIterator1 keys_last1, InputIterator2 keys_first2, InputIterator2 keys_last2, InputIterator3 values_first1, InputIterator4 values_first2, OutputIterator1 keys_result, OutputIterator2 values_result)#

set_union_by_key performs a key-value union operation from set theory. set_union_by_key constructs a sorted range that is the union of the sorted ranges [keys_first1, keys_last1) and [keys_first2, keys_last2). Associated with each element from the input and output key ranges is a value element. The associated input value ranges need not be sorted.

In the simplest case, set_union_by_key performs the “union” operation from set theory: the output range contains a copy of every element that is contained in [keys_first1, keys_last1), [keys_first2, keys_last1), or both. The general case is more complicated, because the input ranges may contain duplicate elements. The generalization is that if [keys_first1, keys_last1) contains m elements that are equivalent to each other and if [keys_first2, keys_last2) contains n elements that are equivalent to them, then all m elements from the first range shall be copied to the output range, in order, and then max(n - m, 0) elements from the second range shall be copied to the output, in order.

Each time a key element is copied from [keys_first1, keys_last1) or [keys_first2, keys_last2) is copied to the keys output range, the corresponding value element is copied from the corresponding values input range (beginning at values_first1 or values_first2) to the values output range.

This version of set_union_by_key compares key elements using operator<.

The following code snippet demonstrates how to use set_symmetric_difference_by_key to compute the symmetric difference of two sets of integers sorted in ascending order with their values.

#include <thrust/set_operations.h>
...
int A_keys[6] = {0, 2, 4, 6, 8, 10, 12};
int A_vals[6] = {0, 0, 0, 0, 0,  0,  0};

int B_keys[5] = {1, 3, 5, 7, 9};
int B_vals[5] = {1, 1, 1, 1, 1};

int keys_result[11];
int vals_result[11];

thrust::pair<int*,int*> end = thrust::set_symmetric_difference_by_key(A_keys, A_keys + 6, B_keys, B_keys + 5, A_vals, B_vals, keys_result, vals_result);
// keys_result is now {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12}
// vals_result is now {0, 1, 0, 1, 0, 1, 0, 1, 0, 1,  0,  0}

See also

sort_by_key

See also

is_sorted

Parameters:
  • keys_first1 – The beginning of the first input range of keys.

  • keys_last1 – The end of the first input range of keys.

  • keys_first2 – The beginning of the second input range of keys.

  • keys_last2 – The end of the second input range of keys.

  • values_first1 – The beginning of the first input range of values.

  • values_first2 – The beginning of the first input range of values.

  • keys_result – The beginning of the output range of keys.

  • values_result – The beginning of the output range of values.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator, InputIterator1 and InputIterator2 have the same value_type, InputIterator1's value_type is a model of LessThan Comparable, the ordering on InputIterator1's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator1's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2 and InputIterator1 have the same value_type, InputIterator2's value_type is a model of LessThan Comparable, the ordering on InputIterator2's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator2's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator3 – is a model of Input Iterator, and InputIterator3's value_type is convertible to a type in OutputIterator2's set of value_types.

  • InputIterator4 – is a model of Input Iterator, and InputIterator4's value_type is convertible to a type in OutputIterator2's set of value_types.

  • OutputIterator1 – is a model of Output Iterator.

  • OutputIterator2 – is a model of Output Iterator.

Returns:

A pair p such that p.first is the end of the output range of keys, and such that p.second is the end of the output range of values.

Pre:

The ranges [keys_first1, keys_last1) and [keys_first2, keys_last2) shall be sorted with respect to operator<.

Pre:

The resulting ranges shall not overlap with any input range.

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename InputIterator3, typename InputIterator4, typename OutputIterator1, typename OutputIterator2, typename StrictWeakCompare> __host__ __device__ thrust::pair< OutputIterator1, OutputIterator2 > set_union_by_key (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 keys_first1, InputIterator1 keys_last1, InputIterator2 keys_first2, InputIterator2 keys_last2, InputIterator3 values_first1, InputIterator4 values_first2, OutputIterator1 keys_result, OutputIterator2 values_result, StrictWeakCompare comp)

set_union_by_key performs a key-value union operation from set theory. set_union_by_key constructs a sorted range that is the union of the sorted ranges [keys_first1, keys_last1) and [keys_first2, keys_last2). Associated with each element from the input and output key ranges is a value element. The associated input value ranges need not be sorted.

In the simplest case, set_union_by_key performs the “union” operation from set theory: the output range contains a copy of every element that is contained in [keys_first1, keys_last1), [keys_first2, keys_last1), or both. The general case is more complicated, because the input ranges may contain duplicate elements. The generalization is that if [keys_first1, keys_last1) contains m elements that are equivalent to each other and if [keys_first2, keys_last2) contains n elements that are equivalent to them, then all m elements from the first range shall be copied to the output range, in order, and then max(n - m, 0) elements from the second range shall be copied to the output, in order.

Each time a key element is copied from [keys_first1, keys_last1) or [keys_first2, keys_last2) is copied to the keys output range, the corresponding value element is copied from the corresponding values input range (beginning at values_first1 or values_first2) to the values output range.

This version of set_union_by_key compares key elements using a function object comp.

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

The following code snippet demonstrates how to use set_symmetric_difference_by_key to compute the symmetric difference of two sets of integers sorted in descending order with their values using the thrust::host execution policy for parallelization:

#include <thrust/set_operations.h>
#include <thrust/functional.h>
#include <thrust/execution_policy.h>
...
int A_keys[6] = {12, 10, 8, 6, 4, 2, 0};
int A_vals[6] = { 0,  0, 0, 0, 0, 0, 0};

int B_keys[5] = {9, 7, 5, 3, 1};
int B_vals[5] = {1, 1, 1, 1, 1};

int keys_result[11];
int vals_result[11];

thrust::pair<int*,int*> end = thrust::set_symmetric_difference_by_key(thrust::host, A_keys, A_keys + 6, B_keys, B_keys + 5, A_vals, B_vals, keys_result, vals_result, thrust::greater<int>());
// keys_result is now {12, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
// vals_result is now { 0,  1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0}

See also

sort_by_key

See also

is_sorted

Parameters:
  • exec – The execution policy to use for parallelization.

  • keys_first1 – The beginning of the first input range of keys.

  • keys_last1 – The end of the first input range of keys.

  • keys_first2 – The beginning of the second input range of keys.

  • keys_last2 – The end of the second input range of keys.

  • values_first1 – The beginning of the first input range of values.

  • values_first2 – The beginning of the first input range of values.

  • keys_result – The beginning of the output range of keys.

  • values_result – The beginning of the output range of values.

  • comp – Comparison operator.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator, InputIterator1 and InputIterator2 have the same value_type, InputIterator1's value_type is a model of LessThan Comparable, the ordering on InputIterator1's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator1's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2 and InputIterator1 have the same value_type, InputIterator2's value_type is a model of LessThan Comparable, the ordering on InputIterator2's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator2's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator3 – is a model of Input Iterator, and InputIterator3's value_type is convertible to a type in OutputIterator2's set of value_types.

  • InputIterator4 – is a model of Input Iterator, and InputIterator4's value_type is convertible to a type in OutputIterator2's set of value_types.

  • OutputIterator1 – is a model of Output Iterator.

  • OutputIterator2 – is a model of Output Iterator.

  • StrictWeakCompare – is a model of Strict Weak Ordering.

Returns:

A pair p such that p.first is the end of the output range of keys, and such that p.second is the end of the output range of values.

Pre:

The ranges [keys_first1, keys_last1) and [keys_first2, keys_last2) shall be sorted with respect to comp.

Pre:

The resulting ranges shall not overlap with any input range.

template<typename InputIterator1, typename InputIterator2, typename InputIterator3, typename InputIterator4, typename OutputIterator1, typename OutputIterator2, typename StrictWeakCompare>
thrust::pair<OutputIterator1, OutputIterator2> set_union_by_key(InputIterator1 keys_first1, InputIterator1 keys_last1, InputIterator2 keys_first2, InputIterator2 keys_last2, InputIterator3 values_first1, InputIterator4 values_first2, OutputIterator1 keys_result, OutputIterator2 values_result, StrictWeakCompare comp)#

set_union_by_key performs a key-value union operation from set theory. set_union_by_key constructs a sorted range that is the union of the sorted ranges [keys_first1, keys_last1) and [keys_first2, keys_last2). Associated with each element from the input and output key ranges is a value element. The associated input value ranges need not be sorted.

In the simplest case, set_union_by_key performs the “union” operation from set theory: the output range contains a copy of every element that is contained in [keys_first1, keys_last1), [keys_first2, keys_last1), or both. The general case is more complicated, because the input ranges may contain duplicate elements. The generalization is that if [keys_first1, keys_last1) contains m elements that are equivalent to each other and if [keys_first2, keys_last2) contains n elements that are equivalent to them, then all m elements from the first range shall be copied to the output range, in order, and then max(n - m, 0) elements from the second range shall be copied to the output, in order.

Each time a key element is copied from [keys_first1, keys_last1) or [keys_first2, keys_last2) is copied to the keys output range, the corresponding value element is copied from the corresponding values input range (beginning at values_first1 or values_first2) to the values output range.

This version of set_union_by_key compares key elements using a function object comp.

The following code snippet demonstrates how to use set_symmetric_difference_by_key to compute the symmetric difference of two sets of integers sorted in descending order with their values.

#include <thrust/set_operations.h>
#include <thrust/functional.h>
...
int A_keys[6] = {12, 10, 8, 6, 4, 2, 0};
int A_vals[6] = { 0,  0, 0, 0, 0, 0, 0};

int B_keys[5] = {9, 7, 5, 3, 1};
int B_vals[5] = {1, 1, 1, 1, 1};

int keys_result[11];
int vals_result[11];

thrust::pair<int*,int*> end = thrust::set_symmetric_difference_by_key(A_keys, A_keys + 6, B_keys, B_keys + 5, A_vals, B_vals, keys_result, vals_result, thrust::greater<int>());
// keys_result is now {12, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
// vals_result is now { 0,  1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0}

See also

sort_by_key

See also

is_sorted

Parameters:
  • keys_first1 – The beginning of the first input range of keys.

  • keys_last1 – The end of the first input range of keys.

  • keys_first2 – The beginning of the second input range of keys.

  • keys_last2 – The end of the second input range of keys.

  • values_first1 – The beginning of the first input range of values.

  • values_first2 – The beginning of the first input range of values.

  • keys_result – The beginning of the output range of keys.

  • values_result – The beginning of the output range of values.

  • comp – Comparison operator.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator, InputIterator1 and InputIterator2 have the same value_type, InputIterator1's value_type is a model of LessThan Comparable, the ordering on InputIterator1's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator1's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2 and InputIterator1 have the same value_type, InputIterator2's value_type is a model of LessThan Comparable, the ordering on InputIterator2's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator2's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator3 – is a model of Input Iterator, and InputIterator3's value_type is convertible to a type in OutputIterator2's set of value_types.

  • InputIterator4 – is a model of Input Iterator, and InputIterator4's value_type is convertible to a type in OutputIterator2's set of value_types.

  • OutputIterator1 – is a model of Output Iterator.

  • OutputIterator2 – is a model of Output Iterator.

  • StrictWeakCompare – is a model of Strict Weak Ordering.

Returns:

A pair p such that p.first is the end of the output range of keys, and such that p.second is the end of the output range of values.

Pre:

The ranges [keys_first1, keys_last1) and [keys_first2, keys_last2) shall be sorted with respect to comp.

Pre:

The resulting ranges shall not overlap with any input range.

Functions

template<typename DerivedPolicy, typename RandomAccessIterator> __host__ __device__ void sort (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, RandomAccessIterator first, RandomAccessIterator last)

sort sorts the elements in [first, last) into ascending order, meaning that if i and j are any two valid iterators in [first, last) such that i precedes j, then *j is not less than *i. Note: sort is not guaranteed to be stable. That is, suppose that *i and *j are equivalent: neither one is less than the other. It is not guaranteed that the relative order of these two elements will be preserved by sort.

This version of sort compares objects using operator<.

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

The following code snippet demonstrates how to use sort to sort a sequence of integers using the thrust::host execution policy for parallelization:

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

See also

stable_sort

See also

sort_by_key

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the sequence.

  • last – The end of the sequence.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • RandomAccessIterator – is a model of Random Access Iterator, RandomAccessIterator is mutable, and RandomAccessIterator's value_type is a model of LessThan Comparable, and the ordering relation on RandomAccessIterator's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements.

template<typename RandomAccessIterator>
void sort(RandomAccessIterator first, RandomAccessIterator last)#

sort sorts the elements in [first, last) into ascending order, meaning that if i and j are any two valid iterators in [first, last) such that i precedes j, then *j is not less than *i. Note: sort is not guaranteed to be stable. That is, suppose that *i and *j are equivalent: neither one is less than the other. It is not guaranteed that the relative order of these two elements will be preserved by sort.

This version of sort compares objects using operator<.

The following code snippet demonstrates how to use sort to sort a sequence of integers.

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

See also

stable_sort

See also

sort_by_key

Parameters:
  • first – The beginning of the sequence.

  • last – The end of the sequence.

Template Parameters:

RandomAccessIterator – is a model of Random Access Iterator, RandomAccessIterator is mutable, and RandomAccessIterator's value_type is a model of LessThan Comparable, and the ordering relation on RandomAccessIterator's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements.

template<typename DerivedPolicy, typename RandomAccessIterator, typename StrictWeakOrdering> __host__ __device__ void sort (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, RandomAccessIterator first, RandomAccessIterator last, StrictWeakOrdering comp)

sort sorts the elements in [first, last) into ascending order, meaning that if i and j are any two valid iterators in [first, last) such that i precedes j, then *j is not less than *i. Note: sort is not guaranteed to be stable. That is, suppose that *i and *j are equivalent: neither one is less than the other. It is not guaranteed that the relative order of these two elements will be preserved by sort.

This version of sort compares objects using a function object comp.

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

The following code demonstrates how to sort integers in descending order using the greater<int> comparison operator using the thrust::host execution policy for parallelization:

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

See also

stable_sort

See also

sort_by_key

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the sequence.

  • last – The end of the sequence.

  • comp – Comparison operator.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • RandomAccessIterator – is a model of Random Access Iterator, RandomAccessIterator is mutable, and RandomAccessIterator's value_type is convertible to StrictWeakOrdering's first_argument_type and second_argument_type.

  • StrictWeakOrdering – is a model of Strict Weak Ordering.

template<typename RandomAccessIterator, typename StrictWeakOrdering> __host__ __device__ void sort (RandomAccessIterator first, RandomAccessIterator last, StrictWeakOrdering comp)

sort sorts the elements in [first, last) into ascending order, meaning that if i and j are any two valid iterators in [first, last) such that i precedes j, then *j is not less than *i. Note: sort is not guaranteed to be stable. That is, suppose that *i and *j are equivalent: neither one is less than the other. It is not guaranteed that the relative order of these two elements will be preserved by sort.

This version of sort compares objects using a function object comp.

The following code demonstrates how to sort integers in descending order using the greater<int> comparison operator.

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

See also

stable_sort

See also

sort_by_key

Parameters:
  • first – The beginning of the sequence.

  • last – The end of the sequence.

  • comp – Comparison operator.

Template Parameters:
  • RandomAccessIterator – is a model of Random Access Iterator, RandomAccessIterator is mutable, and RandomAccessIterator's value_type is convertible to StrictWeakOrdering's first_argument_type and second_argument_type.

  • StrictWeakOrdering – is a model of Strict Weak Ordering.

template<typename DerivedPolicy, typename RandomAccessIterator> __host__ __device__ void stable_sort (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, RandomAccessIterator first, RandomAccessIterator last)

stable_sort is much like sort: it sorts the elements in [first, last) into ascending order, meaning that if i and j are any two valid iterators in [first, last) such that i precedes j, then *j is not less than *i.

As the name suggests, stable_sort is stable: it preserves the relative ordering of equivalent elements. That is, if x and y are elements in [first, last) such that x precedes y, and if the two elements are equivalent (neither x < y nor y < x) then a postcondition of stable_sort is that x still precedes y.

This version of stable_sort compares objects using operator<.

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

The following code snippet demonstrates how to use sort to sort a sequence of integers using the thrust::host execution policy for parallelization:

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

See also

sort

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the sequence.

  • last – The end of the sequence.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • RandomAccessIterator – is a model of Random Access Iterator, RandomAccessIterator is mutable, and RandomAccessIterator's value_type is a model of LessThan Comparable, and the ordering relation on RandomAccessIterator's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements.

template<typename RandomAccessIterator>
void stable_sort(RandomAccessIterator first, RandomAccessIterator last)#

stable_sort is much like sort: it sorts the elements in [first, last) into ascending order, meaning that if i and j are any two valid iterators in [first, last) such that i precedes j, then *j is not less than *i.

As the name suggests, stable_sort is stable: it preserves the relative ordering of equivalent elements. That is, if x and y are elements in [first, last) such that x precedes y, and if the two elements are equivalent (neither x < y nor y < x) then a postcondition of stable_sort is that x still precedes y.

This version of stable_sort compares objects using operator<.

The following code snippet demonstrates how to use sort to sort a sequence of integers.

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

See also

sort

Parameters:
  • first – The beginning of the sequence.

  • last – The end of the sequence.

Template Parameters:

RandomAccessIterator – is a model of Random Access Iterator, RandomAccessIterator is mutable, and RandomAccessIterator's value_type is a model of LessThan Comparable, and the ordering relation on RandomAccessIterator's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements.

template<typename DerivedPolicy, typename RandomAccessIterator, typename StrictWeakOrdering> __host__ __device__ void stable_sort (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, RandomAccessIterator first, RandomAccessIterator last, StrictWeakOrdering comp)

stable_sort is much like sort: it sorts the elements in [first, last) into ascending order, meaning that if i and j are any two valid iterators in [first, last) such that i precedes j, then *j is not less than *i.

As the name suggests, stable_sort is stable: it preserves the relative ordering of equivalent elements. That is, if x and y are elements in [first, last) such that x precedes y, and if the two elements are equivalent (neither x < y nor y < x) then a postcondition of stable_sort is that x still precedes y.

This version of stable_sort compares objects using a function object comp.

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

The following code demonstrates how to sort integers in descending order using the greater<int> comparison operator using the thrust::host execution policy for parallelization:

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

See also

sort

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the sequence.

  • last – The end of the sequence.

  • comp – Comparison operator.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • RandomAccessIterator – is a model of Random Access Iterator, RandomAccessIterator is mutable, and RandomAccessIterator's value_type is convertible to StrictWeakOrdering's first_argument_type and second_argument_type.

  • StrictWeakOrdering – is a model of Strict Weak Ordering.

template<typename RandomAccessIterator, typename StrictWeakOrdering>
void stable_sort(RandomAccessIterator first, RandomAccessIterator last, StrictWeakOrdering comp)#

stable_sort is much like sort: it sorts the elements in [first, last) into ascending order, meaning that if i and j are any two valid iterators in [first, last) such that i precedes j, then *j is not less than *i.

As the name suggests, stable_sort is stable: it preserves the relative ordering of equivalent elements. That is, if x and y are elements in [first, last) such that x precedes y, and if the two elements are equivalent (neither x < y nor y < x) then a postcondition of stable_sort is that x still precedes y.

This version of stable_sort compares objects using a function object comp.

The following code demonstrates how to sort integers in descending order using the greater<int> comparison operator.

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

See also

sort

Parameters:
  • first – The beginning of the sequence.

  • last – The end of the sequence.

  • comp – Comparison operator.

Template Parameters:
  • RandomAccessIterator – is a model of Random Access Iterator, RandomAccessIterator is mutable, and RandomAccessIterator's value_type is convertible to StrictWeakOrdering's first_argument_type and second_argument_type.

  • StrictWeakOrdering – is a model of Strict Weak Ordering.

template<typename DerivedPolicy, typename RandomAccessIterator1, typename RandomAccessIterator2> __host__ __device__ void sort_by_key (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, RandomAccessIterator1 keys_first, RandomAccessIterator1 keys_last, RandomAccessIterator2 values_first)

sort_by_key performs a key-value sort. That is, sort_by_key sorts the elements in [keys_first, keys_last) and [values_first, values_first + (keys_last - keys_first)) into ascending key order, meaning that if i and j are any two valid iterators in [keys_first, keys_last) such that i precedes j, and p and q are iterators in [values_first, values_first + (keys_last - keys_first)) corresponding to i and j respectively, then *j is not less than *i.

Note: sort_by_key is not guaranteed to be stable. That is, suppose that *i and *j are equivalent: neither one is less than the other. It is not guaranteed that the relative order of these two keys or the relative order of their corresponding values will be preserved by sort_by_key.

This version of sort_by_key compares key objects using operator<.

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

The following code snippet demonstrates how to use sort_by_key to sort an array of character values using integers as sorting keys using the thrust::host execution policy for parallelization:

#include <thrust/sort.h>
#include <thrust/execution_policy.h>
...
const int N = 6;
int    keys[N] = {  1,   4,   2,   8,   5,   7};
char values[N] = {'a', 'b', 'c', 'd', 'e', 'f'};
thrust::sort_by_key(thrust::host, keys, keys + N, values);
// keys is now   {  1,   2,   4,   5,   7,   8}
// values is now {'a', 'c', 'b', 'e', 'f', 'd'}

See also

sort

Parameters:
  • exec – The execution policy to use for parallelization.

  • keys_first – The beginning of the key sequence.

  • keys_last – The end of the key sequence.

  • values_first – The beginning of the value sequence.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • RandomAccessIterator1 – is a model of Random Access Iterator, RandomAccessIterator1 is mutable, and RandomAccessIterator1's value_type is a model of LessThan Comparable, and the ordering relation on RandomAccessIterator1's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements.

  • RandomAccessIterator2 – is a model of Random Access Iterator, and RandomAccessIterator2 is mutable.

Pre:

The range [keys_first, keys_last)) shall not overlap the range [values_first, values_first + (keys_last - keys_first)).

template<typename RandomAccessIterator1, typename RandomAccessIterator2>
void sort_by_key(RandomAccessIterator1 keys_first, RandomAccessIterator1 keys_last, RandomAccessIterator2 values_first)#

sort_by_key performs a key-value sort. That is, sort_by_key sorts the elements in [keys_first, keys_last) and [values_first, values_first + (keys_last - keys_first)) into ascending key order, meaning that if i and j are any two valid iterators in [keys_first, keys_last) such that i precedes j, and p and q are iterators in [values_first, values_first + (keys_last - keys_first)) corresponding to i and j respectively, then *j is not less than *i.

Note: sort_by_key is not guaranteed to be stable. That is, suppose that *i and *j are equivalent: neither one is less than the other. It is not guaranteed that the relative order of these two keys or the relative order of their corresponding values will be preserved by sort_by_key.

This version of sort_by_key compares key objects using operator<.

The following code snippet demonstrates how to use sort_by_key to sort an array of character values using integers as sorting keys.

#include <thrust/sort.h>
...
const int N = 6;
int    keys[N] = {  1,   4,   2,   8,   5,   7};
char values[N] = {'a', 'b', 'c', 'd', 'e', 'f'};
thrust::sort_by_key(keys, keys + N, values);
// keys is now   {  1,   2,   4,   5,   7,   8}
// values is now {'a', 'c', 'b', 'e', 'f', 'd'}

See also

sort

Parameters:
  • keys_first – The beginning of the key sequence.

  • keys_last – The end of the key sequence.

  • values_first – The beginning of the value sequence.

Template Parameters:
  • RandomAccessIterator1 – is a model of Random Access Iterator, RandomAccessIterator1 is mutable, and RandomAccessIterator1's value_type is a model of LessThan Comparable, and the ordering relation on RandomAccessIterator1's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements.

  • RandomAccessIterator2 – is a model of Random Access Iterator, and RandomAccessIterator2 is mutable.

Pre:

The range [keys_first, keys_last)) shall not overlap the range [values_first, values_first + (keys_last - keys_first)).

template<typename DerivedPolicy, typename RandomAccessIterator1, typename RandomAccessIterator2, typename StrictWeakOrdering> __host__ __device__ void sort_by_key (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, RandomAccessIterator1 keys_first, RandomAccessIterator1 keys_last, RandomAccessIterator2 values_first, StrictWeakOrdering comp)

sort_by_key performs a key-value sort. That is, sort_by_key sorts the elements in [keys_first, keys_last) and [values_first, values_first + (keys_last - keys_first)) into ascending key order, meaning that if i and j are any two valid iterators in [keys_first, keys_last) such that i precedes j, and p and q are iterators in [values_first, values_first + (keys_last - keys_first)) corresponding to i and j respectively, then *j is not less than *i.

Note: sort_by_key is not guaranteed to be stable. That is, suppose that *i and *j are equivalent: neither one is less than the other. It is not guaranteed that the relative order of these two keys or the relative order of their corresponding values will be preserved by sort_by_key.

This version of sort_by_key compares key objects using a function object comp.

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

The following code snippet demonstrates how to use sort_by_key to sort an array of character values using integers as sorting keys using the thrust::host execution policy for parallelization.The keys are sorted in descending order using the greater<int> comparison operator.

#include <thrust/sort.h>
#include <thrust/execution_policy.h>
...
const int N = 6;
int    keys[N] = {  1,   4,   2,   8,   5,   7};
char values[N] = {'a', 'b', 'c', 'd', 'e', 'f'};
thrust::sort_by_key(thrust::host, keys, keys + N, values, thrust::greater<int>());
// keys is now   {  8,   7,   5,   4,   2,   1}
// values is now {'d', 'f', 'e', 'b', 'c', 'a'}

See also

sort

Parameters:
  • exec – The execution policy to use for parallelization.

  • keys_first – The beginning of the key sequence.

  • keys_last – The end of the key sequence.

  • values_first – The beginning of the value sequence.

  • comp – Comparison operator.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • RandomAccessIterator1 – is a model of Random Access Iterator, RandomAccessIterator1 is mutable, and RandomAccessIterator1's value_type is convertible to StrictWeakOrdering's first_argument_type and second_argument_type.

  • RandomAccessIterator2 – is a model of Random Access Iterator, and RandomAccessIterator2 is mutable.

  • StrictWeakOrdering – is a model of Strict Weak Ordering.

Pre:

The range [keys_first, keys_last)) shall not overlap the range [values_first, values_first + (keys_last - keys_first)).

template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename StrictWeakOrdering>
void sort_by_key(RandomAccessIterator1 keys_first, RandomAccessIterator1 keys_last, RandomAccessIterator2 values_first, StrictWeakOrdering comp)#

sort_by_key performs a key-value sort. That is, sort_by_key sorts the elements in [keys_first, keys_last) and [values_first, values_first + (keys_last - keys_first)) into ascending key order, meaning that if i and j are any two valid iterators in [keys_first, keys_last) such that i precedes j, and p and q are iterators in [values_first, values_first + (keys_last - keys_first)) corresponding to i and j respectively, then *j is not less than *i.

Note: sort_by_key is not guaranteed to be stable. That is, suppose that *i and *j are equivalent: neither one is less than the other. It is not guaranteed that the relative order of these two keys or the relative order of their corresponding values will be preserved by sort_by_key.

This version of sort_by_key compares key objects using a function object comp.

The following code snippet demonstrates how to use sort_by_key to sort an array of character values using integers as sorting keys. The keys are sorted in descending order using the greater<int> comparison operator.

#include <thrust/sort.h>
...
const int N = 6;
int    keys[N] = {  1,   4,   2,   8,   5,   7};
char values[N] = {'a', 'b', 'c', 'd', 'e', 'f'};
thrust::sort_by_key(keys, keys + N, values, thrust::greater<int>());
// keys is now   {  8,   7,   5,   4,   2,   1}
// values is now {'d', 'f', 'e', 'b', 'c', 'a'}

See also

sort

Parameters:
  • keys_first – The beginning of the key sequence.

  • keys_last – The end of the key sequence.

  • values_first – The beginning of the value sequence.

  • comp – Comparison operator.

Template Parameters:
  • RandomAccessIterator1 – is a model of Random Access Iterator, RandomAccessIterator1 is mutable, and RandomAccessIterator1's value_type is convertible to StrictWeakOrdering's first_argument_type and second_argument_type.

  • RandomAccessIterator2 – is a model of Random Access Iterator, and RandomAccessIterator2 is mutable.

  • StrictWeakOrdering – is a model of Strict Weak Ordering.

Pre:

The range [keys_first, keys_last)) shall not overlap the range [values_first, values_first + (keys_last - keys_first)).

template<typename DerivedPolicy, typename RandomAccessIterator1, typename RandomAccessIterator2> __host__ __device__ void stable_sort_by_key (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, RandomAccessIterator1 keys_first, RandomAccessIterator1 keys_last, RandomAccessIterator2 values_first)

stable_sort_by_key performs a key-value sort. That is, stable_sort_by_key sorts the elements in [keys_first, keys_last) and [values_first, values_first + (keys_last - keys_first)) into ascending key order, meaning that if i and j are any two valid iterators in [keys_first, keys_last) such that i precedes j, and p and q are iterators in [values_first, values_first + (keys_last - keys_first)) corresponding to i and j respectively, then *j is not less than *i.

As the name suggests, stable_sort_by_key is stable: it preserves the relative ordering of equivalent elements. That is, if x and y are elements in [keys_first, keys_last) such that x precedes y, and if the two elements are equivalent (neither x < y nor y < x) then a postcondition of stable_sort_by_key is that x still precedes y.

This version of stable_sort_by_key compares key objects using operator<.

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

The following code snippet demonstrates how to use stable_sort_by_key to sort an array of characters using integers as sorting keys using the thrust::host execution policy for parallelization:

#include <thrust/sort.h>
#include <thrust/execution_policy.h>
...
const int N = 6;
int    keys[N] = {  1,   4,   2,   8,   5,   7};
char values[N] = {'a', 'b', 'c', 'd', 'e', 'f'};
thrust::stable_sort_by_key(thrust::host, keys, keys + N, values);
// keys is now   {  1,   2,   4,   5,   7,   8}
// values is now {'a', 'c', 'b', 'e', 'f', 'd'}

See also

sort_by_key

See also

stable_sort

Parameters:
  • exec – The execution policy to use for parallelization.

  • keys_first – The beginning of the key sequence.

  • keys_last – The end of the key sequence.

  • values_first – The beginning of the value sequence.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • RandomAccessIterator1 – is a model of Random Access Iterator, RandomAccessIterator1 is mutable, and RandomAccessIterator1's value_type is a model of LessThan Comparable, and the ordering relation on RandomAccessIterator1's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements.

  • RandomAccessIterator2 – is a model of Random Access Iterator, and RandomAccessIterator2 is mutable.

Pre:

The range [keys_first, keys_last)) shall not overlap the range [values_first, values_first + (keys_last - keys_first)).

template<typename RandomAccessIterator1, typename RandomAccessIterator2>
void stable_sort_by_key(RandomAccessIterator1 keys_first, RandomAccessIterator1 keys_last, RandomAccessIterator2 values_first)#

stable_sort_by_key performs a key-value sort. That is, stable_sort_by_key sorts the elements in [keys_first, keys_last) and [values_first, values_first + (keys_last - keys_first)) into ascending key order, meaning that if i and j are any two valid iterators in [keys_first, keys_last) such that i precedes j, and p and q are iterators in [values_first, values_first + (keys_last - keys_first)) corresponding to i and j respectively, then *j is not less than *i.

As the name suggests, stable_sort_by_key is stable: it preserves the relative ordering of equivalent elements. That is, if x and y are elements in [keys_first, keys_last) such that x precedes y, and if the two elements are equivalent (neither x < y nor y < x) then a postcondition of stable_sort_by_key is that x still precedes y.

This version of stable_sort_by_key compares key objects using operator<.

The following code snippet demonstrates how to use stable_sort_by_key to sort an array of characters using integers as sorting keys.

#include <thrust/sort.h>
...
const int N = 6;
int    keys[N] = {  1,   4,   2,   8,   5,   7};
char values[N] = {'a', 'b', 'c', 'd', 'e', 'f'};
thrust::stable_sort_by_key(keys, keys + N, values);
// keys is now   {  1,   2,   4,   5,   7,   8}
// values is now {'a', 'c', 'b', 'e', 'f', 'd'}

See also

sort_by_key

See also

stable_sort

Parameters:
  • keys_first – The beginning of the key sequence.

  • keys_last – The end of the key sequence.

  • values_first – The beginning of the value sequence.

Template Parameters:
  • RandomAccessIterator1 – is a model of Random Access Iterator, RandomAccessIterator1 is mutable, and RandomAccessIterator1's value_type is a model of LessThan Comparable, and the ordering relation on RandomAccessIterator1's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements.

  • RandomAccessIterator2 – is a model of Random Access Iterator, and RandomAccessIterator2 is mutable.

Pre:

The range [keys_first, keys_last)) shall not overlap the range [values_first, values_first + (keys_last - keys_first)).

template<typename DerivedPolicy, typename RandomAccessIterator1, typename RandomAccessIterator2, typename StrictWeakOrdering> __host__ __device__ void stable_sort_by_key (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, RandomAccessIterator1 keys_first, RandomAccessIterator1 keys_last, RandomAccessIterator2 values_first, StrictWeakOrdering comp)

stable_sort_by_key performs a key-value sort. That is, stable_sort_by_key sorts the elements in [keys_first, keys_last) and [values_first, values_first + (keys_last - keys_first)) into ascending key order, meaning that if i and j are any two valid iterators in [keys_first, keys_last) such that i precedes j, and p and q are iterators in [values_first, values_first + (keys_last - keys_first)) corresponding to i and j respectively, then *j is not less than *i.

As the name suggests, stable_sort_by_key is stable: it preserves the relative ordering of equivalent elements. That is, if x and y are elements in [keys_first, keys_last) such that x precedes y, and if the two elements are equivalent (neither x < y nor y < x) then a postcondition of stable_sort_by_key is that x still precedes y.

This version of stable_sort_by_key compares key objects using the function object comp.

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

The following code snippet demonstrates how to use sort_by_key to sort an array of character values using integers as sorting keys using the thrust::host execution policy for parallelization. The keys are sorted in descending order using the greater<int> comparison operator.

#include <thrust/sort.h>
#include <thrust/execution_policy.h>
...
const int N = 6;
int    keys[N] = {  1,   4,   2,   8,   5,   7};
char values[N] = {'a', 'b', 'c', 'd', 'e', 'f'};
thrust::stable_sort_by_key(thrust::host, keys, keys + N, values, thrust::greater<int>());
// keys is now   {  8,   7,   5,   4,   2,   1}
// values is now {'d', 'f', 'e', 'b', 'c', 'a'}

See also

sort_by_key

See also

stable_sort

Parameters:
  • exec – The execution policy to use for parallelization.

  • keys_first – The beginning of the key sequence.

  • keys_last – The end of the key sequence.

  • values_first – The beginning of the value sequence.

  • comp – Comparison operator.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • RandomAccessIterator1 – is a model of Random Access Iterator, RandomAccessIterator1 is mutable, and RandomAccessIterator1's value_type is convertible to StrictWeakOrdering's first_argument_type and second_argument_type.

  • RandomAccessIterator2 – is a model of Random Access Iterator, and RandomAccessIterator2 is mutable.

  • StrictWeakOrdering – is a model of Strict Weak Ordering.

Pre:

The range [keys_first, keys_last)) shall not overlap the range [values_first, values_first + (keys_last - keys_first)).

template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename StrictWeakOrdering>
void stable_sort_by_key(RandomAccessIterator1 keys_first, RandomAccessIterator1 keys_last, RandomAccessIterator2 values_first, StrictWeakOrdering comp)#

stable_sort_by_key performs a key-value sort. That is, stable_sort_by_key sorts the elements in [keys_first, keys_last) and [values_first, values_first + (keys_last - keys_first)) into ascending key order, meaning that if i and j are any two valid iterators in [keys_first, keys_last) such that i precedes j, and p and q are iterators in [values_first, values_first + (keys_last - keys_first)) corresponding to i and j respectively, then *j is not less than *i.

As the name suggests, stable_sort_by_key is stable: it preserves the relative ordering of equivalent elements. That is, if x and y are elements in [keys_first, keys_last) such that x precedes y, and if the two elements are equivalent (neither x < y nor y < x) then a postcondition of stable_sort_by_key is that x still precedes y.

This version of stable_sort_by_key compares key objects using the function object comp.

The following code snippet demonstrates how to use sort_by_key to sort an array of character values using integers as sorting keys. The keys are sorted in descending order using the greater<int> comparison operator.

#include <thrust/sort.h>
...
const int N = 6;
int    keys[N] = {  1,   4,   2,   8,   5,   7};
char values[N] = {'a', 'b', 'c', 'd', 'e', 'f'};
thrust::stable_sort_by_key(keys, keys + N, values, thrust::greater<int>());
// keys is now   {  8,   7,   5,   4,   2,   1}
// values is now {'d', 'f', 'e', 'b', 'c', 'a'}

See also

sort_by_key

See also

stable_sort

Parameters:
  • keys_first – The beginning of the key sequence.

  • keys_last – The end of the key sequence.

  • values_first – The beginning of the value sequence.

  • comp – Comparison operator.

Template Parameters:
  • RandomAccessIterator1 – is a model of Random Access Iterator, RandomAccessIterator1 is mutable, and RandomAccessIterator1's value_type is convertible to StrictWeakOrdering's first_argument_type and second_argument_type.

  • RandomAccessIterator2 – is a model of Random Access Iterator, and RandomAccessIterator2 is mutable.

  • StrictWeakOrdering – is a model of Strict Weak Ordering.

Pre:

The range [keys_first, keys_last)) shall not overlap the range [values_first, values_first + (keys_last - keys_first)).

Functions

template<typename DerivedPolicy, typename InputIterator, typename OutputIterator> __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:

Remark

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

#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]

See also

inclusive_scan

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.

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

  • OutputIterator – is a model of Output Iterator.

Returns:

The iterator result + (last - first)

template<typename DerivedPolicy, typename InputIterator, typename OutputIterator, typename BinaryFunction> __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:

Remark

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

#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]

See also

inclusive_scan

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.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator – is 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.

  • OutputIterator – is a model of Output Iterator.

  • BinaryFunction'sresult_type is convertible to a type in OutputIterator's set of value_types.

Returns:

The iterator result + (last - first)

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.

Remark

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

#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

inclusive_scan

Parameters:
  • first – The beginning of the input range.

  • last – The end of the input range.

  • result – The beginning of the output range.

Template Parameters:
  • InputIterator – is 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.

  • OutputIterator – is a model of Output Iterator.

Returns:

The iterator result + (last - first)

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.

Remark

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

#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]

See also

inclusive_scan

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.

Template Parameters:
  • InputIterator – is 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.

  • OutputIterator – is a model of Output Iterator.

  • BinaryFunction'sresult_type is convertible to a type in OutputIterator's set of value_types.

Returns:

The iterator result + (last - first)

template<typename DerivedPolicy, typename ForwardIterator, typename Generator> __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

See also

generate_n

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

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's result_type is convertible to ForwardIterator's value_type.

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

See also

generate_n

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

Template Parameters:
  • ForwardIterator – is a model of Forward Iterator, and ForwardIterator is mutable.

  • Generator – is a model of Generator, and Generator's result_type is convertible to ForwardIterator's value_type.

template<typename DerivedPolicy, typename OutputIterator, typename Size, typename Generator> __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

See also

generate

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

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's result_type is convertible to a type in OutputIterator's set of value_types.

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

See also

generate

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

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's result_type is convertible to a type in OutputIterator's set of value_types.

template<typename DerivedPolicy, typename ForwardIterator> __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.

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the sequence.

  • last – The end of the sequence.

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's value_type, then x + y is defined, and if T is ForwardIterator's value_type, then T(0) is defined.

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.

Parameters:
  • first – The beginning of the sequence.

  • last – The end of the sequence.

Template Parameters:

ForwardIterator – is 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.

template<typename DerivedPolicy, typename ForwardIterator, typename T> __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.

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.

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's value_type, then x + y is defined, and if T is ForwardIterator's value_type, then T(0) is defined.

  • T – is a model of Assignable, and T is convertible to ForwardIterator's value_type.

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.

Parameters:
  • first – The beginning of the sequence.

  • last – The end of the sequence.

  • init – The first value of the sequence of numbers.

Template Parameters:
  • ForwardIterator – is 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.

  • T – is a model of Assignable, and T is convertible to ForwardIterator's value_type.

template<typename DerivedPolicy, typename ForwardIterator, typename T> __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.

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.

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's value_type, then x + y is defined, and if T is ForwardIterator's value_type, then T(0) is defined.

  • T – is a model of Assignable, and T is convertible to ForwardIterator's value_type.

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.

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.

Template Parameters:
  • ForwardIterator – is 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.

  • T – is a model of Assignable, and T is convertible to ForwardIterator's value_type.

template<typename DerivedPolicy, typename ForwardIterator, typename UnaryOperation> __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}

See also

thrust::fill

See also

thrust::generate

See also

thrust::sequence

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.

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's value_type, then x + y is defined, and if T is ForwardIterator's value_type, then T(0) is defined.

  • UnaryOperation – is a model of Unary Function and UnaryFunction's result_type is convertible to OutputIterator's value_type.

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}

See also

thrust::fill

See also

thrust::generate

See also

thrust::sequence

Parameters:
  • first – The beginning of the range.

  • last – The end of the range.

  • unary_op – The unary operation to apply.

Template Parameters:
  • ForwardIterator – is 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.

  • UnaryOperation – is a model of Unary Function and UnaryFunction's result_type is convertible to OutputIterator's value_type.

template<typename DerivedPolicy, typename InputIterator, typename OutputIterator, typename UnaryFunction> __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};

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.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator – is a model of Input Iterator and InputIterator's value_type is convertible to UnaryFunction's argument_type.

  • OutputIterator – is a model of Output Iterator.

  • UnaryFunction – is a model of Unary Function and UnaryFunction's result_type is convertible to OutputIterator's value_type.

Returns:

The end of the output sequence.

Pre:

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

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};

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.

Template Parameters:
  • InputIterator – is a model of Input Iterator and InputIterator's value_type is convertible to UnaryFunction's argument_type.

  • OutputIterator – is a model of Output Iterator.

  • UnaryFunction – is a model of Unary Function and UnaryFunction's result_type is convertible to OutputIterator's value_type.

Returns:

The end of the output sequence.

Pre:

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

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename OutputIterator, typename BinaryFunction> __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};

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.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator and InputIterator1's value_type is convertible to BinaryFunction's first_argument_type.

  • InputIterator2 – is a model of Input Iterator and InputIterator2's value_type is convertible to BinaryFunction's second_argument_type.

  • OutputIterator – is a model of Output Iterator.

  • BinaryFunction – is a model of Binary Function and BinaryFunction's result_type is convertible to OutputIterator's value_type.

Returns:

The end of the output sequence.

Pre:

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

Pre:

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

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};

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.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator and InputIterator1's value_type is convertible to BinaryFunction's first_argument_type.

  • InputIterator2 – is a model of Input Iterator and InputIterator2's value_type is convertible to BinaryFunction's second_argument_type.

  • OutputIterator – is a model of Output Iterator.

  • BinaryFunction – is a model of Binary Function and BinaryFunction's result_type is convertible to OutputIterator's value_type.

Returns:

The end of the output sequence.

Pre:

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

Pre:

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

template<typename DerivedPolicy, typename InputIterator, typename ForwardIterator, typename UnaryFunction, typename Predicate> __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};

See also

thrust::transform

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.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator – is 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.

  • ForwardIterator – is a model of Forward Iterator.

  • UnaryFunction – is a model of Unary Function and UnaryFunction's result_type is convertible to OutputIterator's value_type.

  • Predicate – is a model of Predicate.

Returns:

The end of the output sequence.

Pre:

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

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};

See also

thrust::transform

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.

Template Parameters:
  • InputIterator – is 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.

  • ForwardIterator – is a model of Forward Iterator.

  • UnaryFunction – is a model of Unary Function and UnaryFunction's result_type is convertible to OutputIterator's value_type.

  • Predicate – is a model of Predicate.

Returns:

The end of the output sequence.

Pre:

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

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename ForwardIterator, typename UnaryFunction, typename Predicate> __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};

See also

thrust::transform

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.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator and InputIterator1's value_type is convertible to UnaryFunction's argument_type.

  • InputIterator2 – is a model of Input Iterator and InputIterator2's value_type is convertible to Predicate's argument_type.

  • ForwardIterator – is a model of Forward Iterator.

  • UnaryFunction – is a model of Unary Function and UnaryFunction's result_type is convertible to OutputIterator's value_type.

  • Predicate – is a model of Predicate.

Returns:

The end of the output sequence.

Pre:

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

Pre:

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

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};

See also

thrust::transform

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.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator and InputIterator1's value_type is convertible to UnaryFunction's argument_type.

  • InputIterator2 – is a model of Input Iterator and InputIterator2's value_type is convertible to Predicate's argument_type.

  • ForwardIterator – is a model of Forward Iterator.

  • UnaryFunction – is a model of Unary Function and UnaryFunction's result_type is convertible to OutputIterator's value_type.

  • Predicate – is a model of Predicate.

Returns:

The end of the output sequence.

Pre:

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

Pre:

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

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename InputIterator3, typename ForwardIterator, typename BinaryFunction, typename Predicate> __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};

See also

thrust::transform

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.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator and InputIterator1's value_type is convertible to BinaryFunction's first_argument_type.

  • InputIterator2 – is a model of Input Iterator and InputIterator2's value_type is convertible to BinaryFunction's second_argument_type.

  • ForwardIterator – is a model of Forward Iterator.

  • BinaryFunction – is a model of Binary Function and BinaryFunction's result_type is convertible to OutputIterator's value_type.

  • Predicate – is a model of Predicate.

Returns:

The end of the output sequence.

Pre:

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

Pre:

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

Pre:

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

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};

See also

thrust::transform

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.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator and InputIterator1's value_type is convertible to BinaryFunction's first_argument_type.

  • InputIterator2 – is a model of Input Iterator and InputIterator2's value_type is convertible to BinaryFunction's second_argument_type.

  • ForwardIterator – is a model of Forward Iterator.

  • BinaryFunction – is a model of Binary Function and BinaryFunction's result_type is convertible to OutputIterator's value_type.

  • Predicate – is a model of Predicate.

Returns:

The end of the output sequence.

Pre:

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

Pre:

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

Pre:

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

Functions

template<typename DerivedPolicy, typename ForwardIterator, typename T> __host__ __device__ void fill (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last, const T &value)

fill assigns the value value to every element in the range [first, last). That is, for every iterator i in [first, last), it performs the assignment *i = value.

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

The following code snippet demonstrates how to use fill to set a thrust::device_vector’s elements to a given value using the thrust::device execution policy for parallelization:

#include <thrust/fill.h>
#include <thrust/device_vector.h>
#include <thrust/execution_policy.h>
...
thrust::device_vector<int> v(4);
thrust::fill(thrust::device, v.begin(), v.end(), 137);

// v[0] == 137, v[1] == 137, v[2] == 137, v[3] == 137

See also

fill_n

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the sequence.

  • last – The end of the sequence.

  • value – The value to be copied.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator, and ForwardIterator is mutable.

  • T – is a model of Assignable, and T's value_type is convertible to ForwardIterator's value_type.

template<typename ForwardIterator, typename T> __host__ __device__ void fill (ForwardIterator first, ForwardIterator last, const T &value)

fill assigns the value value to every element in the range [first, last). That is, for every iterator i in [first, last), it performs the assignment *i = value.

The following code snippet demonstrates how to use fill to set a thrust::device_vector’s elements to a given value.

#include <thrust/fill.h>
#include <thrust/device_vector.h>
...
thrust::device_vector<int> v(4);
thrust::fill(v.begin(), v.end(), 137);

// v[0] == 137, v[1] == 137, v[2] == 137, v[3] == 137

See also

fill_n

Parameters:
  • first – The beginning of the sequence.

  • last – The end of the sequence.

  • value – The value to be copied.

Template Parameters:
  • ForwardIterator – is a model of Forward Iterator, and ForwardIterator is mutable.

  • T – is a model of Assignable, and T's value_type is convertible to ForwardIterator's value_type.

template<typename DerivedPolicy, typename OutputIterator, typename Size, typename T> __host__ __device__ OutputIterator fill_n (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, OutputIterator first, Size n, const T &value)

fill_n assigns the value value to every element in the range [first, first+n). That is, for every iterator i in [first, first+n), it performs the assignment *i = value.

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

The following code snippet demonstrates how to use fill to set a thrust::device_vector’s elements to a given value using the thrust::device execution policy for parallelization:

#include <thrust/fill.h>
#include <thrust/device_vector.h>
#include <thrust/execution_policy.h>
...
thrust::device_vector<int> v(4);
thrust::fill_n(thrust::device, v.begin(), v.size(), 137);

// v[0] == 137, v[1] == 137, v[2] == 137, v[3] == 137

See also

fill

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the sequence.

  • n – The size of the sequence.

  • value – The value to be copied.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • OutputIterator – is a model of Output Iterator.

  • T – is a model of Assignable, and T's value_type is convertible to a type in OutputIterator's set of value_type.

Returns:

first + n

template<typename OutputIterator, typename Size, typename T> __host__ __device__ OutputIterator fill_n (OutputIterator first, Size n, const T &value)

fill_n assigns the value value to every element in the range [first, first+n). That is, for every iterator i in [first, first+n), it performs the assignment *i = value.

The following code snippet demonstrates how to use fill to set a thrust::device_vector’s elements to a given value.

#include <thrust/fill.h>
#include <thrust/device_vector.h>
...
thrust::device_vector<int> v(4);
thrust::fill_n(v.begin(), v.size(), 137);

// v[0] == 137, v[1] == 137, v[2] == 137, v[3] == 137

See also

fill

Parameters:
  • first – The beginning of the sequence.

  • n – The size of the sequence.

  • value – The value to be copied.

Template Parameters:
  • OutputIterator – is a model of Output Iterator.

  • T – is a model of Assignable, and T's value_type is convertible to a type in OutputIterator's set of value_type.

Returns:

first + n

template<typename DerivedPolicy, typename ForwardIterator, typename T> __host__ __device__ void uninitialized_fill (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last, const T &x)

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 [first, last) points to uninitialized memory, then uninitialized_fill creates copies of x in that range. That is, for each iterator i in the range [first, last), uninitialized_fill creates a copy of x in the location pointed to i by calling ForwardIterator's value_type's copy constructor.

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

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

#include <thrust/uninitialized_fill.h>
#include <thrust/device_malloc.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_ptr<Int> array = thrust::device_malloc<Int>(N);
thrust::uninitialized_fill(thrust::device, array, array + N, val);

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

See also

fill

See also

device_new

See also

device_malloc

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The first element of the range of interest.

  • last – The last element of the range of interest.

  • x – The value to use as the exemplar of the copy constructor.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator, ForwardIterator is mutable, and ForwardIterator's value_type has a constructor that takes a single argument of type T.

template<typename ForwardIterator, typename T>
void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T &x)#

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 [first, last) points to uninitialized memory, then uninitialized_fill creates copies of x in that range. That is, for each iterator i in the range [first, last), uninitialized_fill creates a copy of x in the location pointed to i by calling ForwardIterator's value_type's copy constructor.

The following code snippet demonstrates how to use uninitialized_fill to initialize a range of uninitialized memory.

#include <thrust/uninitialized_fill.h>
#include <thrust/device_malloc.h>

struct Int
{
  __host__ __device__
  Int(int x) : val(x) {}
  int val;
};  
...
const int N = 137;

Int val(46);
thrust::device_ptr<Int> array = thrust::device_malloc<Int>(N);
thrust::uninitialized_fill(array, array + N, val);

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

See also

fill

See also

device_new

See also

device_malloc

Parameters:
  • first – The first element of the range of interest.

  • last – The last element of the range of interest.

  • x – The value to use as the exemplar of the copy constructor.

Template Parameters:

ForwardIterator – is a model of Forward Iterator, ForwardIterator is mutable, and ForwardIterator's value_type has a constructor that takes a single argument of type T.

template<typename DerivedPolicy, typename ForwardIterator, typename Size, typename T> __host__ __device__ ForwardIterator uninitialized_fill_n (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, Size n, const T &x)

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 [first, first+n) points to uninitialized memory, then uninitialized_fill creates copies of x in that range. That is, for each iterator i in the range [first, first+n), uninitialized_fill creates a copy of x in the location pointed to i by calling ForwardIterator's value_type's copy constructor.

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

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

#include <thrust/uninitialized_fill.h>
#include <thrust/device_malloc.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_ptr<Int> array = thrust::device_malloc<Int>(N);
thrust::uninitialized_fill_n(thrust::device, array, N, val);

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

See also

fill

See also

device_new

See also

device_malloc

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The first element of the range of interest.

  • n – The size of the range of interest.

  • x – The value to use as the exemplar of the copy constructor.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator, ForwardIterator is mutable, and ForwardIterator's value_type has a constructor that takes a single argument of type T.

Returns:

first+n

template<typename ForwardIterator, typename Size, typename T>
ForwardIterator uninitialized_fill_n(ForwardIterator first, Size n, const T &x)#

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 [first, first+n) points to uninitialized memory, then uninitialized_fill creates copies of x in that range. That is, for each iterator i in the range [first, first+n), uninitialized_fill creates a copy of x in the location pointed to i by calling ForwardIterator's value_type's copy constructor.

The following code snippet demonstrates how to use uninitialized_fill to initialize a range of uninitialized memory.

#include <thrust/uninitialized_fill.h>
#include <thrust/device_malloc.h>

struct Int
{
  __host__ __device__
  Int(int x) : val(x) {}
  int val;
};  
...
const int N = 137;

Int val(46);
thrust::device_ptr<Int> array = thrust::device_malloc<Int>(N);
thrust::uninitialized_fill_n(array, N, val);

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

See also

fill

See also

device_new

See also

device_malloc

Parameters:
  • first – The first element of the range of interest.

  • n – The size of the range of interest.

  • x – The value to use as the exemplar of the copy constructor.

Template Parameters:

ForwardIterator – is a model of Forward Iterator, ForwardIterator is mutable, and ForwardIterator's value_type has a constructor that takes a single argument of type T.

Returns:

first+n

Functions

template<typename DerivedPolicy, typename InputIterator, typename UnaryFunction> __host__ __device__ InputIterator for_each (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator first, InputIterator last, UnaryFunction f)

for_each applies the function object f to each element in the range [first, last); f's return value, if any, is ignored. Unlike the C++ Standard Template Library function std::for_each, this version offers no guarantee on order of execution. For this reason, this version of for_each does not return a copy of the function object.

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

The following code snippet demonstrates how to use for_each to print the elements of a thrust::device_vector using the thrust::device parallelization policy:

#include <thrust/for_each.h>
#include <thrust/device_vector.h>
#include <thrust/execution_policy.h>
#include <cstdio>
...

struct printf_functor
{
  __host__ __device__
  void operator()(int x)
  {
    // note that using printf in a __device__ function requires
    // code compiled for a GPU with compute capability 2.0 or
    // higher (nvcc --arch=sm_20)
    printf("%d\n", x);
  }
};
...
thrust::device_vector<int> d_vec(3);
d_vec[0] = 0; d_vec[1] = 1; d_vec[2] = 2;

thrust::for_each(thrust::device, d_vec.begin(), d_vec.end(), printf_functor());

// 0 1 2 is printed to standard output in some unspecified order

See also

for_each_n

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the sequence.

  • last – The end of the sequence.

  • f – The function object to apply to the range [first, last).

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator – is a model of Input Iterator, and InputIterator's value_type is convertible to UnaryFunction's argument_type.

  • UnaryFunction – is a model of Unary Function, and UnaryFunction does not apply any non-constant operation through its argument.

Returns:

last

template<typename DerivedPolicy, typename InputIterator, typename Size, typename UnaryFunction> __host__ __device__ InputIterator for_each_n (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator first, Size n, UnaryFunction f)

for_each_n applies the function object f to each element in the range [first, first + n); f's return value, if any, is ignored. Unlike the C++ Standard Template Library function std::for_each, this version offers no guarantee on order of execution.

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

The following code snippet demonstrates how to use for_each_n to print the elements of a device_vector using the thrust::device parallelization policy.

#include <thrust/for_each.h>
#include <thrust/device_vector.h>
#include <thrust/execution_policy.h>
#include <cstdio>

struct printf_functor
{
  __host__ __device__
  void operator()(int x)
  {
    // note that using printf in a __device__ function requires
    // code compiled for a GPU with compute capability 2.0 or
    // higher (nvcc --arch=sm_20)
    printf("%d\n", x);
  }
};
...
thrust::device_vector<int> d_vec(3);
d_vec[0] = 0; d_vec[1] = 1; d_vec[2] = 2;

thrust::for_each_n(thrust::device, d_vec.begin(), d_vec.size(), printf_functor());

// 0 1 2 is printed to standard output in some unspecified order

See also

for_each

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the sequence.

  • n – The size of the input sequence.

  • f – The function object to apply to the range [first, first + n).

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator – is a model of Input Iterator, and InputIterator's value_type is convertible to UnaryFunction's argument_type.

  • Size – is an integral type.

  • UnaryFunction – is a model of Unary Function, and UnaryFunction does not apply any non-constant operation through its argument.

Returns:

first + n

template<typename InputIterator, typename UnaryFunction>
InputIterator for_each(InputIterator first, InputIterator last, UnaryFunction f)#

for_each applies the function object f to each element in the range [first, last); f's return value, if any, is ignored. Unlike the C++ Standard Template Library function std::for_each, this version offers no guarantee on order of execution. For this reason, this version of for_each does not return a copy of the function object.

The following code snippet demonstrates how to use for_each to print the elements of a device_vector.

#include <thrust/for_each.h>
#include <thrust/device_vector.h>
#include <stdio.h>

struct printf_functor
{
  __host__ __device__
  void operator()(int x)
  {
    // note that using printf in a __device__ function requires
    // code compiled for a GPU with compute capability 2.0 or
    // higher (nvcc --arch=sm_20)
    printf("%d\n", x);
  }
};
...
thrust::device_vector<int> d_vec(3);
d_vec[0] = 0; d_vec[1] = 1; d_vec[2] = 2;

thrust::for_each(d_vec.begin(), d_vec.end(), printf_functor());

// 0 1 2 is printed to standard output in some unspecified order

See also

for_each_n

Parameters:
  • first – The beginning of the sequence.

  • last – The end of the sequence.

  • f – The function object to apply to the range [first, last).

Template Parameters:
  • InputIterator – is a model of Input Iterator, and InputIterator's value_type is convertible to UnaryFunction's argument_type.

  • UnaryFunction – is a model of Unary Function, and UnaryFunction does not apply any non-constant operation through its argument.

Returns:

last

template<typename InputIterator, typename Size, typename UnaryFunction>
InputIterator for_each_n(InputIterator first, Size n, UnaryFunction f)#

for_each_n applies the function object f to each element in the range [first, first + n); f's return value, if any, is ignored. Unlike the C++ Standard Template Library function std::for_each, this version offers no guarantee on order of execution.

The following code snippet demonstrates how to use for_each_n to print the elements of a device_vector.

#include <thrust/for_each.h>
#include <thrust/device_vector.h>
#include <stdio.h>

struct printf_functor
{
  __host__ __device__
  void operator()(int x)
  {
    // note that using printf in a __device__ function requires
    // code compiled for a GPU with compute capability 2.0 or
    // higher (nvcc --arch=sm_20)
    printf("%d\n", x);
  }
};
...
thrust::device_vector<int> d_vec(3);
d_vec[0] = 0; d_vec[1] = 1; d_vec[2] = 2;

thrust::for_each_n(d_vec.begin(), d_vec.size(), printf_functor());

// 0 1 2 is printed to standard output in some unspecified order

See also

for_each

Parameters:
  • first – The beginning of the sequence.

  • n – The size of the input sequence.

  • f – The function object to apply to the range [first, first + n).

Template Parameters:
  • InputIterator – is a model of Input Iterator, and InputIterator's value_type is convertible to UnaryFunction's argument_type.

  • Size – is an integral type.

  • UnaryFunction – is a model of Unary Function, and UnaryFunction does not apply any non-constant operation through its argument.

Returns:

first + n

Functions

template<typename DerivedPolicy, typename ForwardIterator, typename T> __host__ __device__ void replace (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last, const T &old_value, const T &new_value)

replace replaces every element in the range [first, last) equal to old_value with new_value. That is: for every iterator i, if *i == old_value then it performs the assignment *i = new_value.

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

The following code snippet demonstrates how to use replace to replace a value of interest in a device_vector with another using the thrust::device execution policy for parallelization:

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

...

thrust::device_vector<int> A(4);
A[0] = 1;
A[1] = 2;
A[2] = 3;
A[3] = 1;

thrust::replace(thrust::device, A.begin(), A.end(), 1, 99);

// A contains [99, 2, 3, 99]

See also

replace_if

See also

replace_copy

See also

replace_copy_if

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the sequence of interest.

  • last – The end of the sequence of interest.

  • old_value – The value to replace.

  • new_value – The new value to replace old_value.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator, and ForwardIterator is mutable.

  • T – is a model of Assignable, T is a model of EqualityComparable, objects of T may be compared for equality with objects of ForwardIterator's value_type, and T is convertible to ForwardIterator's value_type.

template<typename ForwardIterator, typename T>
void replace(ForwardIterator first, ForwardIterator last, const T &old_value, const T &new_value)#

replace replaces every element in the range [first, last) equal to old_value with new_value. That is: for every iterator i, if *i == old_value then it performs the assignment *i = new_value.

The following code snippet demonstrates how to use replace to replace a value of interest in a device_vector with another.

#include <thrust/replace.h>
#include <thrust/device_vector.h>

...

thrust::device_vector<int> A(4);
A[0] = 1;
A[1] = 2;
A[2] = 3;
A[3] = 1;

thrust::replace(A.begin(), A.end(), 1, 99);

// A contains [99, 2, 3, 99]

See also

replace_if

See also

replace_copy

See also

replace_copy_if

Parameters:
  • first – The beginning of the sequence of interest.

  • last – The end of the sequence of interest.

  • old_value – The value to replace.

  • new_value – The new value to replace old_value.

Template Parameters:
  • ForwardIterator – is a model of Forward Iterator, and ForwardIterator is mutable.

  • T – is a model of Assignable, T is a model of EqualityComparable, objects of T may be compared for equality with objects of ForwardIterator's value_type, and T is convertible to ForwardIterator's value_type.

template<typename DerivedPolicy, typename ForwardIterator, typename Predicate, typename T> __host__ __device__ void replace_if (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last, Predicate pred, const T &new_value)

replace_if replaces every element in the range [first, last) for which pred returns true with new_value. That is: for every iterator i, if pred(*i) is true then it performs the assignment *i = new_value.

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

The following code snippet demonstrates how to use replace_if to replace a device_vector's negative elements with 0 using the thrust::device execution policy for parallelization:

#include <thrust/replace.h>
#include <thrust/device_vector.h>
#include <thrust/execution_policy.h>
...
struct is_less_than_zero
{
  __host__ __device__
  bool operator()(int x)
  {
    return x < 0;
  }
};

...

thrust::device_vector<int> A(4);
A[0] =  1;
A[1] = -3;
A[2] =  2;
A[3] = -1;

is_less_than_zero pred;

thrust::replace_if(thrust::device, A.begin(), A.end(), pred, 0);

// A contains [1, 0, 2, 0]

See also

replace

See also

replace_copy

See also

replace_copy_if

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the sequence of interest.

  • last – The end of the sequence of interest.

  • pred – The predicate to test on every value of the range [first,last).

  • new_value – The new value to replace elements which pred(*i) evaluates to true.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator, ForwardIterator is mutable, and ForwardIterator's value_type is convertible to Predicate's argument_type.

  • Predicate – is a model of Predicate.

  • T – is a model of Assignable, and T is convertible to ForwardIterator's value_type.

template<typename ForwardIterator, typename Predicate, typename T>
void replace_if(ForwardIterator first, ForwardIterator last, Predicate pred, const T &new_value)#

replace_if replaces every element in the range [first, last) for which pred returns true with new_value. That is: for every iterator i, if pred(*i) is true then it performs the assignment *i = new_value.

The following code snippet demonstrates how to use replace_if to replace a device_vector's negative elements with 0.

#include <thrust/replace.h>
#include <thrust/device_vector.h>
...
struct is_less_than_zero
{
  __host__ __device__
  bool operator()(int x)
  {
    return x < 0;
  }
};

...

thrust::device_vector<int> A(4);
A[0] =  1;
A[1] = -3;
A[2] =  2;
A[3] = -1;

is_less_than_zero pred;

thrust::replace_if(A.begin(), A.end(), pred, 0);

// A contains [1, 0, 2, 0]

See also

replace

See also

replace_copy

See also

replace_copy_if

Parameters:
  • first – The beginning of the sequence of interest.

  • last – The end of the sequence of interest.

  • pred – The predicate to test on every value of the range [first,last).

  • new_value – The new value to replace elements which pred(*i) evaluates to true.

Template Parameters:
  • ForwardIterator – is a model of Forward Iterator, ForwardIterator is mutable, and ForwardIterator's value_type is convertible to Predicate's argument_type.

  • Predicate – is a model of Predicate.

  • T – is a model of Assignable, and T is convertible to ForwardIterator's value_type.

template<typename DerivedPolicy, typename ForwardIterator, typename InputIterator, typename Predicate, typename T> __host__ __device__ void replace_if (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last, InputIterator stencil, Predicate pred, const T &new_value)

replace_if replaces every element in the range [first, last) for which pred(*s) returns true with new_value. That is: for every iterator i in the range [first, last), and s in the range [stencil, stencil + (last - first)), if pred(*s) is true then it performs the assignment *i = new_value.

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

The following code snippet demonstrates how to use replace_if to replace a device_vector's element with 0 when its corresponding stencil element is less than zero using the thrust::device execution policy for parallelization:

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

struct is_less_than_zero
{
  __host__ __device__
  bool operator()(int x)
  {
    return x < 0;
  }
};

...

thrust::device_vector<int> A(4);
A[0] =  10;
A[1] =  20;
A[2] =  30;
A[3] =  40;

thrust::device_vector<int> S(4);
S[0] = -1;
S[1] =  0;
S[2] = -1;
S[3] =  0;

is_less_than_zero pred;
thrust::replace_if(thrust::device, A.begin(), A.end(), S.begin(), pred, 0);

// A contains [0, 20, 0, 40]

See also

replace

See also

replace_copy

See also

replace_copy_if

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the sequence of interest.

  • last – The end of the sequence of interest.

  • stencil – The beginning of the stencil sequence.

  • pred – The predicate to test on every value of the range [first,last).

  • new_value – The new value to replace elements which pred(*i) evaluates to true.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator, and ForwardIterator is mutable.

  • InputIterator – is a model of Input Iterator, and InputIterator's value_type is convertible to Predicate's argument_type.

  • Predicate – is a model of Predicate.

  • T – is a model of Assignable, and T is convertible to ForwardIterator's value_type.

template<typename ForwardIterator, typename InputIterator, typename Predicate, typename T>
void replace_if(ForwardIterator first, ForwardIterator last, InputIterator stencil, Predicate pred, const T &new_value)#

replace_if replaces every element in the range [first, last) for which pred(*s) returns true with new_value. That is: for every iterator i in the range [first, last), and s in the range [stencil, stencil + (last - first)), if pred(*s) is true then it performs the assignment *i = new_value.

The following code snippet demonstrates how to use replace_if to replace a device_vector's element with 0 when its corresponding stencil element is less than zero.

#include <thrust/replace.h>
#include <thrust/device_vector.h>

struct is_less_than_zero
{
  __host__ __device__
  bool operator()(int x)
  {
    return x < 0;
  }
};

...

thrust::device_vector<int> A(4);
A[0] =  10;
A[1] =  20;
A[2] =  30;
A[3] =  40;

thrust::device_vector<int> S(4);
S[0] = -1;
S[1] =  0;
S[2] = -1;
S[3] =  0;

is_less_than_zero pred;
thrust::replace_if(A.begin(), A.end(), S.begin(), pred, 0);

// A contains [0, 20, 0, 40]

See also

replace

See also

replace_copy

See also

replace_copy_if

Parameters:
  • first – The beginning of the sequence of interest.

  • last – The end of the sequence of interest.

  • stencil – The beginning of the stencil sequence.

  • pred – The predicate to test on every value of the range [first,last).

  • new_value – The new value to replace elements which pred(*i) evaluates to true.

Template Parameters:
  • ForwardIterator – is a model of Forward Iterator, and ForwardIterator is mutable.

  • InputIterator – is a model of Input Iterator, and InputIterator's value_type is convertible to Predicate's argument_type.

  • Predicate – is a model of Predicate.

  • T – is a model of Assignable, and T is convertible to ForwardIterator's value_type.

template<typename DerivedPolicy, typename InputIterator, typename OutputIterator, typename T> __host__ __device__ OutputIterator replace_copy (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator first, InputIterator last, OutputIterator result, const T &old_value, const T &new_value)

replace_copy copies elements from the range [first, last) to the range [result, result + (last-first)), except that any element equal to old_value is not copied; new_value is copied instead.

More precisely, for every integer n such that 0 <= n < last-first, replace_copy performs the assignment *(result+n) = new_value if *(first+n) == old_value, and *(result+n) = *(first+n) otherwise.

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

#include <thrust/replace.h>
#include <thrust/device_vector.h>
#include <thrust/execution_policy.h>
...
thrust::device_vector<int> A(4);
A[0] = 1;
A[1] = 2;
A[2] = 3;
A[3] = 1;

thrust::device_vector<int> B(4);

thrust::replace_copy(thrust::device, A.begin(), A.end(), B.begin(), 1, 99);

// B contains [99, 2, 3, 99]

See also

copy

See also

replace

See also

replace_if

See also

replace_copy_if

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the sequence to copy from.

  • last – The end of the sequence to copy from.

  • result – The beginning of the sequence to copy to.

  • old_value – The value to replace.

  • new_value – The replacement value for which *i == old_value evaluates to true.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator – is a model of Input Iterator.

  • OutputIterator – is a model of Output Iterator.

  • T – is a model of Assignable, T is a model of Equality Comparable, T may be compared for equality with InputIterator's value_type, and T is convertible to OutputIterator's value_type.

Returns:

result + (last-first)

Pre:

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

template<typename InputIterator, typename OutputIterator, typename T>
OutputIterator replace_copy(InputIterator first, InputIterator last, OutputIterator result, const T &old_value, const T &new_value)#

replace_copy copies elements from the range [first, last) to the range [result, result + (last-first)), except that any element equal to old_value is not copied; new_value is copied instead.

More precisely, for every integer n such that 0 <= n < last-first, replace_copy performs the assignment *(result+n) = new_value if *(first+n) == old_value, and *(result+n) = *(first+n) otherwise.

#include <thrust/replace.h>
#include <thrust/device_vector.h>
...
thrust::device_vector<int> A(4);
A[0] = 1;
A[1] = 2;
A[2] = 3;
A[3] = 1;

thrust::device_vector<int> B(4);

thrust::replace_copy(A.begin(), A.end(), B.begin(), 1, 99);

// B contains [99, 2, 3, 99]

See also

copy

See also

replace

See also

replace_if

See also

replace_copy_if

Parameters:
  • first – The beginning of the sequence to copy from.

  • last – The end of the sequence to copy from.

  • result – The beginning of the sequence to copy to.

  • old_value – The value to replace.

  • new_value – The replacement value for which *i == old_value evaluates to true.

Template Parameters:
Returns:

result + (last-first)

Pre:

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

template<typename DerivedPolicy, typename InputIterator, typename OutputIterator, typename Predicate, typename T> __host__ __device__ OutputIterator replace_copy_if (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const T &new_value)

replace_copy_if copies elements from the range [first, last) to the range [result, result + (last-first)), except that any element for which pred is true is not copied; new_value is copied instead.

More precisely, for every integer n such that 0 <= n < last-first, replace_copy_if performs the assignment *(result+n) = new_value if pred(*(first+n)), and *(result+n) = *(first+n) otherwise.

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

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

struct is_less_than_zero
{
  __host__ __device__
  bool operator()(int x)
  {
    return x < 0;
  }
};

...

thrust::device_vector<int> A(4);
A[0] =  1;
A[1] = -3;
A[2] =  2;
A[3] = -1;

thrust::device_vector<int> B(4);
is_less_than_zero pred;

thrust::replace_copy_if(thrust::device, A.begin(), A.end(), B.begin(), pred, 0);

// B contains [1, 0, 2, 0]

See also

replace

See also

replace_if

See also

replace_copy

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the sequence to copy from.

  • last – The end of the sequence to copy from.

  • result – The beginning of the sequence to copy to.

  • pred – The predicate to test on every value of the range [first,last).

  • new_value – The replacement value to assign pred(*i) evaluates to true.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator – is a model of Input Iterator, and InputIterator's value_type is convertible to Predicate's argument_type.

  • OutputIterator – is a model of Output Iterator.

  • Predicate – is a model of Predicate.

  • T – is a model of Assignable, and T is convertible to OutputIterator's value_type.

Returns:

result + (last-first)

Pre:

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

template<typename InputIterator, typename OutputIterator, typename Predicate, typename T>
OutputIterator replace_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const T &new_value)#

replace_copy_if copies elements from the range [first, last) to the range [result, result + (last-first)), except that any element for which pred is true is not copied; new_value is copied instead.

More precisely, for every integer n such that 0 <= n < last-first, replace_copy_if performs the assignment *(result+n) = new_value if pred(*(first+n)), and *(result+n) = *(first+n) otherwise.

#include <thrust/replace.h>
#include <thrust/device_vector.h>

struct is_less_than_zero
{
  __host__ __device__
  bool operator()(int x)
  {
    return x < 0;
  }
};

...

thrust::device_vector<int> A(4);
A[0] =  1;
A[1] = -3;
A[2] =  2;
A[3] = -1;

thrust::device_vector<int> B(4);
is_less_than_zero pred;

thrust::replace_copy_if(A.begin(), A.end(), B.begin(), pred, 0);

// B contains [1, 0, 2, 0]

See also

replace

See also

replace_if

See also

replace_copy

Parameters:
  • first – The beginning of the sequence to copy from.

  • last – The end of the sequence to copy from.

  • result – The beginning of the sequence to copy to.

  • pred – The predicate to test on every value of the range [first,last).

  • new_value – The replacement value to assign pred(*i) evaluates to true.

Template Parameters:
  • InputIterator – is a model of Input Iterator, and InputIterator's value_type is convertible to Predicate's argument_type.

  • OutputIterator – is a model of Output Iterator.

  • Predicate – is a model of Predicate.

  • T – is a model of Assignable, and T is convertible to OutputIterator's value_type.

Returns:

result + (last-first)

Pre:

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

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename OutputIterator, typename Predicate, typename T> __host__ __device__ OutputIterator replace_copy_if (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator1 first, InputIterator1 last, InputIterator2 stencil, OutputIterator result, Predicate pred, const T &new_value)

This version of replace_copy_if copies elements from the range [first, last) to the range [result, result + (last-first)), except that any element whose corresponding stencil element causes pred to be true is not copied; new_value is copied instead.

More precisely, for every integer n such that 0 <= n < last-first, replace_copy_if performs the assignment *(result+n) = new_value if pred(*(stencil+n)), and *(result+n) = *(first+n) otherwise.

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

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

struct is_less_than_zero
{
  __host__ __device__
  bool operator()(int x)
  {
    return x < 0;
  }
};

...

thrust::device_vector<int> A(4);
A[0] =  10;
A[1] =  20;
A[2] =  30;
A[3] =  40;

thrust::device_vector<int> S(4);
S[0] = -1;
S[1] =  0;
S[2] = -1;
S[3] =  0;

thrust::device_vector<int> B(4);
is_less_than_zero pred;

thrust::replace_if(thrust::device, A.begin(), A.end(), S.begin(), B.begin(), pred, 0);

// B contains [0, 20, 0, 40]

See also

replace_copy

See also

replace_if

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the sequence to copy from.

  • last – The end of the sequence to copy from.

  • stencil – The beginning of the stencil sequence.

  • result – The beginning of the sequence to copy to.

  • pred – The predicate to test on every value of the range [stencil, stencil + (last - first)).

  • new_value – The replacement value to assign when pred(*s) evaluates to true.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator.

  • InputIterator2 – is a model of Input Iterator and InputIterator2's value_type is convertible to Predicate's argument_type.

  • OutputIterator – is a model of Output Iterator.

  • Predicate – is a model of Predicate.

  • T – is a model of Assignable, and T is convertible to OutputIterator's value_type.

Returns:

result + (last-first)

Pre:

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

Pre:

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

template<typename InputIterator1, typename InputIterator2, typename OutputIterator, typename Predicate, typename T>
OutputIterator replace_copy_if(InputIterator1 first, InputIterator1 last, InputIterator2 stencil, OutputIterator result, Predicate pred, const T &new_value)#

This version of replace_copy_if copies elements from the range [first, last) to the range [result, result + (last-first)), except that any element whose corresponding stencil element causes pred to be true is not copied; new_value is copied instead.

More precisely, for every integer n such that 0 <= n < last-first, replace_copy_if performs the assignment *(result+n) = new_value if pred(*(stencil+n)), and *(result+n) = *(first+n) otherwise.

#include <thrust/replace.h>
#include <thrust/device_vector.h>

struct is_less_than_zero
{
  __host__ __device__
  bool operator()(int x)
  {
    return x < 0;
  }
};

...

thrust::device_vector<int> A(4);
A[0] =  10;
A[1] =  20;
A[2] =  30;
A[3] =  40;

thrust::device_vector<int> S(4);
S[0] = -1;
S[1] =  0;
S[2] = -1;
S[3] =  0;

thrust::device_vector<int> B(4);
is_less_than_zero pred;

thrust::replace_if(A.begin(), A.end(), S.begin(), B.begin(), pred, 0);

// B contains [0, 20, 0, 40]

See also

replace_copy

See also

replace_if

Parameters:
  • first – The beginning of the sequence to copy from.

  • last – The end of the sequence to copy from.

  • stencil – The beginning of the stencil sequence.

  • result – The beginning of the sequence to copy to.

  • pred – The predicate to test on every value of the range [stencil, stencil + (last - first)).

  • new_value – The replacement value to assign when pred(*s) evaluates to true.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator.

  • InputIterator2 – is a model of Input Iterator and InputIterator2's value_type is convertible to Predicate's argument_type.

  • OutputIterator – is a model of Output Iterator.

  • Predicate – is a model of Predicate.

  • T – is a model of Assignable, and T is convertible to OutputIterator's value_type.

Returns:

result + (last-first)

Pre:

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

Pre:

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

Numerics#

group numerics

Functions

template<typename T> __host__ __device__ T abs (const complex< T > &z)

Returns the magnitude (also known as absolute value) of a complex.

Parameters:

z – The complex from which to calculate the absolute value.

template<typename T> __host__ __device__ T arg (const complex< T > &z)

Returns the phase angle (also known as argument) in radians of a complex.

Parameters:

z – The complex from which to calculate the phase angle.

template<typename T> __host__ __device__ T norm (const complex< T > &z)

Returns the square of the magnitude of a complex.

Parameters:

z – The complex from which to calculate the norm.

template<typename T> __host__ __device__ complex< T > conj (const complex< T > &z)

Returns the complex conjugate of a complex.

Parameters:

z – The complex from which to calculate the complex conjugate.

template<typename T0, typename T1> __host__ __device__ complex< typename detail::promoted_numerical_type< T0, T1 >::type > polar (const T0 &m, const T1 &theta=T1())

Returns a complex with the specified magnitude and phase.

Parameters:
  • m – The magnitude of the returned complex.

  • theta – The phase of the returned complex in radians.

template<typename T> __host__ __device__ complex< T > proj (const T &z)

Returns the projection of a complex on the Riemann sphere. For all finite complex it returns the argument. For complexs with a non finite part returns (INFINITY,+/-0) where the sign of the zero matches the sign of the imaginary part of the argument.

Parameters:

z – The complex argument.

template<typename T0, typename T1> __host__ __device__ complex< typename detail::promoted_numerical_type< T0, T1 >::type > operator+ (const complex< T0 > &x, const complex< T1 > &y)

Adds two complex numbers.

The value types of the two complex types should be compatible and the type of the returned complex is the promoted type of the two arguments.

Parameters:
  • x – The first complex.

  • y – The second complex.

template<typename T0, typename T1> __host__ __device__ complex< typename detail::promoted_numerical_type< T0, T1 >::type > operator+ (const complex< T0 > &x, const T1 &y)

Adds a scalar to a complex number.

The value type of the complex should be compatible with the scalar and the type of the returned complex is the promoted type of the two arguments.

Parameters:
  • x – The complex.

  • y – The scalar.

template<typename T0, typename T1> __host__ __device__ complex< typename detail::promoted_numerical_type< T0, T1 >::type > operator+ (const T0 &x, const complex< T1 > &y)

Adds a complex number to a scalar.

The value type of the complex should be compatible with the scalar and the type of the returned complex is the promoted type of the two arguments.

Parameters:
  • x – The scalar.

  • y – The complex.

template<typename T0, typename T1> __host__ __device__ complex< typename detail::promoted_numerical_type< T0, T1 >::type > operator- (const complex< T0 > &x, const complex< T1 > &y)

Subtracts two complex numbers.

The value types of the two complex types should be compatible and the type of the returned complex is the promoted type of the two arguments.

Parameters:
  • x – The first complex (minuend).

  • y – The second complex (subtrahend).

template<typename T0, typename T1> __host__ __device__ complex< typename detail::promoted_numerical_type< T0, T1 >::type > operator- (const complex< T0 > &x, const T1 &y)

Subtracts a scalar from a complex number.

The value type of the complex should be compatible with the scalar and the type of the returned complex is the promoted type of the two arguments.

Parameters:
  • x – The complex (minuend).

  • y – The scalar (subtrahend).

template<typename T0, typename T1> __host__ __device__ complex< typename detail::promoted_numerical_type< T0, T1 >::type > operator- (const T0 &x, const complex< T1 > &y)

Subtracts a complex number from a scalar.

The value type of the complex should be compatible with the scalar and the type of the returned complex is the promoted type of the two arguments.

Parameters:
  • x – The scalar (minuend).

  • y – The complex (subtrahend).

template<typename T0, typename T1> __host__ __device__ complex< typename detail::promoted_numerical_type< T0, T1 >::type > operator* (const complex< T0 > &x, const complex< T1 > &y)

Multiplies two complex numbers.

The value types of the two complex types should be compatible and the type of the returned complex is the promoted type of the two arguments.

Parameters:
  • x – The first complex.

  • y – The second complex.

template<typename T0, typename T1> __host__ __device__ complex< typename detail::promoted_numerical_type< T0, T1 >::type > operator* (const complex< T0 > &x, const T1 &y)

Multiplies a complex number by a scalar.

Parameters:
  • x – The complex.

  • y – The scalar.

template<typename T0, typename T1> __host__ __device__ complex< typename detail::promoted_numerical_type< T0, T1 >::type > operator* (const T0 &x, const complex< T1 > &y)

Multiplies a scalar by a complex number.

The value type of the complex should be compatible with the scalar and the type of the returned complex is the promoted type of the two arguments.

Parameters:
  • x – The scalar.

  • y – The complex.

template<typename T0, typename T1> __host__ __device__ complex< typename detail::promoted_numerical_type< T0, T1 >::type > operator/ (const complex< T0 > &x, const complex< T1 > &y)

Divides two complex numbers.

The value types of the two complex types should be compatible and the type of the returned complex is the promoted type of the two arguments.

Parameters:
  • x – The numerator (dividend).

  • y – The denomimator (divisor).

template<typename T0, typename T1> __host__ __device__ complex< typename detail::promoted_numerical_type< T0, T1 >::type > operator/ (const complex< T0 > &x, const T1 &y)

Divides a complex number by a scalar.

The value type of the complex should be compatible with the scalar and the type of the returned complex is the promoted type of the two arguments.

Parameters:
  • x – The complex numerator (dividend).

  • y – The scalar denomimator (divisor).

template<typename T0, typename T1> __host__ __device__ complex< typename detail::promoted_numerical_type< T0, T1 >::type > operator/ (const T0 &x, const complex< T1 > &y)

Divides a scalar by a complex number.

The value type of the complex should be compatible with the scalar and the type of the returned complex is the promoted type of the two arguments.

Parameters:
  • x – The scalar numerator (dividend).

  • y – The complex denomimator (divisor).

template<typename T> __host__ __device__ complex< T > operator+ (const complex< T > &y)

Unary plus, returns its complex argument.

Parameters:

y – The complex argument.

template<typename T> __host__ __device__ complex< T > operator- (const complex< T > &y)

Unary minus, returns the additive inverse (negation) of its complex argument.

Parameters:

y – The complex argument.

template<typename T> __host__ __device__ complex< T > exp (const complex< T > &z)

Returns the complex exponential of a complex number.

Parameters:

z – The complex argument.

template<typename T> __host__ __device__ complex< T > log (const complex< T > &z)

Returns the complex natural logarithm of a complex number.

Parameters:

z – The complex argument.

template<typename T> __host__ __device__ complex< T > log10 (const complex< T > &z)

Returns the complex base 10 logarithm of a complex number.

Parameters:

z – The complex argument.

template<typename T0, typename T1> __host__ __device__ complex< typename detail::promoted_numerical_type< T0, T1 >::type > pow (const complex< T0 > &x, const complex< T1 > &y)

Returns a complex number raised to another.

The value types of the two complex types should be compatible and the type of the returned complex is the promoted type of the two arguments.

Parameters:
  • x – The base.

  • y – The exponent.

template<typename T0, typename T1> __host__ __device__ complex< typename detail::promoted_numerical_type< T0, T1 >::type > pow (const complex< T0 > &x, const T1 &y)

Returns a complex number raised to a scalar.

The value type of the complex should be compatible with the scalar and the type of the returned complex is the promoted type of the two arguments.

Parameters:
  • x – The base.

  • y – The exponent.

template<typename T0, typename T1> __host__ __device__ complex< typename detail::promoted_numerical_type< T0, T1 >::type > pow (const T0 &x, const complex< T1 > &y)

Returns a scalar raised to a complex number.

The value type of the complex should be compatible with the scalar and the type of the returned complex is the promoted type of the two arguments.

Parameters:
  • x – The base.

  • y – The exponent.

template<typename T> __host__ __device__ complex< T > sqrt (const complex< T > &z)

Returns the complex square root of a complex number.

Parameters:

z – The complex argument.

template<typename T> __host__ __device__ complex< T > cos (const complex< T > &z)

Returns the complex cosine of a complex number.

Parameters:

z – The complex argument.

template<typename T> __host__ __device__ complex< T > sin (const complex< T > &z)

Returns the complex sine of a complex number.

Parameters:

z – The complex argument.

template<typename T> __host__ __device__ complex< T > tan (const complex< T > &z)

Returns the complex tangent of a complex number.

Parameters:

z – The complex argument.

template<typename T> __host__ __device__ complex< T > cosh (const complex< T > &z)

Returns the complex hyperbolic cosine of a complex number.

Parameters:

z – The complex argument.

template<typename T> __host__ __device__ complex< T > sinh (const complex< T > &z)

Returns the complex hyperbolic sine of a complex number.

Parameters:

z – The complex argument.

template<typename T> __host__ __device__ complex< T > tanh (const complex< T > &z)

Returns the complex hyperbolic tangent of a complex number.

Parameters:

z – The complex argument.

template<typename T> __host__ __device__ complex< T > acos (const complex< T > &z)

Returns the complex arc cosine of a complex number.

The range of the real part of the result is [0, Pi] and the range of the imaginary part is [-inf, +inf]

Parameters:

z – The complex argument.

template<typename T> __host__ __device__ complex< T > asin (const complex< T > &z)

Returns the complex arc sine of a complex number.

The range of the real part of the result is [-Pi/2, Pi/2] and the range of the imaginary part is [-inf, +inf]

Parameters:

z – The complex argument.

template<typename T> __host__ __device__ complex< T > atan (const complex< T > &z)

Returns the complex arc tangent of a complex number.

The range of the real part of the result is [-Pi/2, Pi/2] and the range of the imaginary part is [-inf, +inf]

Parameters:

z – The complex argument.

template<typename T> __host__ __device__ complex< T > acosh (const complex< T > &z)

Returns the complex inverse hyperbolic cosine of a complex number.

The range of the real part of the result is [0, +inf] and the range of the imaginary part is [-Pi, Pi]

Parameters:

z – The complex argument.

template<typename T> __host__ __device__ complex< T > asinh (const complex< T > &z)

Returns the complex inverse hyperbolic sine of a complex number.

The range of the real part of the result is [-inf, +inf] and the range of the imaginary part is [-Pi/2, Pi/2]

Parameters:

z – The complex argument.

template<typename T> __host__ __device__ complex< T > atanh (const complex< T > &z)

Returns the complex inverse hyperbolic tangent of a complex number.

The range of the real part of the result is [-inf, +inf] and the range of the imaginary part is [-Pi/2, Pi/2]

Parameters:

z – The complex argument.

template<typename T, typename CharT, typename Traits>
std::basic_ostream<CharT, Traits> &operator<<(std::basic_ostream<CharT, Traits> &os, const complex<T> &z)#

Writes to an output stream a complex number in the form (real, imaginary).

Parameters:
  • os – The output stream.

  • z – The complex number to output.

template<typename T, typename CharT, typename Traits> __host__ std::basic_istream< CharT, Traits > & operator>> (std::basic_istream< CharT, Traits > &is, complex< T > &z)

Reads a complex number from an input stream.

The recognized formats are:

  • real

  • (real)

  • (real, imaginary)

The values read must be convertible to the complex's value_type

Parameters:
  • is – The input stream.

  • z – The complex number to set.

template<typename T0, typename T1> __host__ __device__ bool operator== (const complex< T0 > &x, const complex< T1 > &y)

Returns true if two complex numbers are equal and false otherwise.

Parameters:
  • x – The first complex.

  • y – The second complex.

template<typename T0, typename T1> __host__ THRUST_STD_COMPLEX_DEVICE bool operator== (const complex< T0 > &x, const std::complex< T1 > &y)

Returns true if two complex numbers are equal and false otherwise.

Parameters:
  • x – The first complex.

  • y – The second complex.

template<typename T0, typename T1> __host__ THRUST_STD_COMPLEX_DEVICE bool operator== (const std::complex< T0 > &x, const complex< T1 > &y)

Returns true if two complex numbers are equal and false otherwise.

Parameters:
  • x – The first complex.

  • y – The second complex.

template<typename T0, typename T1> __host__ __device__ bool operator== (const T0 &x, const complex< T1 > &y)

Returns true if the imaginary part of the complex number is zero and the real part is equal to the scalar. Returns false otherwise.

Parameters:
  • x – The scalar.

  • y – The complex.

template<typename T0, typename T1> __host__ __device__ bool operator== (const complex< T0 > &x, const T1 &y)

Returns true if the imaginary part of the complex number is zero and the real part is equal to the scalar. Returns false otherwise.

Parameters:
  • x – The complex.

  • y – The scalar.

template<typename T0, typename T1> __host__ __device__ bool operator!= (const complex< T0 > &x, const complex< T1 > &y)

Returns true if two complex numbers are different and false otherwise.

Parameters:
  • x – The first complex.

  • y – The second complex.

template<typename T0, typename T1> __host__ THRUST_STD_COMPLEX_DEVICE bool operator!= (const complex< T0 > &x, const std::complex< T1 > &y)

Returns true if two complex numbers are different and false otherwise.

Parameters:
  • x – The first complex.

  • y – The second complex.

template<typename T0, typename T1> __host__ THRUST_STD_COMPLEX_DEVICE bool operator!= (const std::complex< T0 > &x, const complex< T1 > &y)

Returns true if two complex numbers are different and false otherwise.

Parameters:
  • x – The first complex.

  • y – The second complex.

template<typename T0, typename T1> __host__ __device__ bool operator!= (const T0 &x, const complex< T1 > &y)

Returns true if the imaginary part of the complex number is not zero or the real part is different from the scalar. Returns false otherwise.

Parameters:
  • x – The scalar.

  • y – The complex.

template<typename T0, typename T1> __host__ __device__ bool operator!= (const complex< T0 > &x, const T1 &y)

Returns true if the imaginary part of the complex number is not zero or the real part is different from the scalar. Returns false otherwise.

Parameters:
  • x – The complex.

  • y – The scalar.

template<typename T>
struct complex#
#include <complex.h>

complex is the Thrust equivalent to std::complex. It is functionally identical to it, but can also be used in device code which std::complex currently cannot.

Template Parameters:

T – The type used to hold the real and imaginary parts. Should be float or double. Others types are not supported.

Containers#

group containers

Functions

template<typename T, typename Alloc>
void swap(device_vector<T, Alloc> &a, device_vector<T, Alloc> &b)#

Exchanges the values of two vectors. x The first device_vector of interest. y The second device_vector of interest.

template<typename T, typename Alloc = thrust::device_allocator<T>>
class device_vector : public detail::vector_base<T, thrust::device_allocator<T>>#
#include <device_vector.h>

A device_vector is a container that supports random access to elements, constant time removal of elements at the end, and linear time insertion and removal of elements at the beginning or in the middle. The number of elements in a device_vector may vary dynamically; memory management is automatic. The memory associated with a device_vector resides in the memory accessible to devices.

See also

device_allocator

See also

host_vector

See also

universal_vector

Parallel Execution Policies#

group execution_policies

Variables

static const detail::host_t host#

thrust::host is the default parallel execution policy associated with Thrust’s host backend system configured by the THRUST_HOST_SYSTEM macro.

Instead of relying on implicit algorithm dispatch through iterator system tags, users may directly target algorithm dispatch at Thrust’s host system by providing thrust::host as an algorithm parameter.

Explicit dispatch can be useful in avoiding the introduction of data copies into containers such as thrust::host_vector.

Note that even though thrust::host targets the host CPU, it is a parallel execution policy. That is, the order that an algorithm invokes functors or dereferences iterators is not defined.

The type of thrust::host is implementation-defined.

The following code snippet demonstrates how to use thrust::host to explicitly dispatch an invocation of thrust::for_each to the host backend system:

#include <thrust/for_each.h>
#include <thrust/execution_policy.h>
#include <cstdio>

struct printf_functor
{
  __host__ __device__
  void operator()(int x)
  {
    printf("%d\n", x);
  }
};
...
int vec(3);
vec[0] = 0; vec[1] = 1; vec[2] = 2;

thrust::for_each(thrust::host, vec.begin(), vec.end(), printf_functor());

// 0 1 2 is printed to standard output in some unspecified order

See also

thrust::device

constexpr detail::device_t device#

thrust::device is the default parallel execution policy associated with Thrust’s device backend system configured by the THRUST_DEVICE_SYSTEM macro.

Instead of relying on implicit algorithm dispatch through iterator system tags, users may directly target algorithm dispatch at Thrust’s device system by providing thrust::device as an algorithm parameter.

Explicit dispatch can be useful in avoiding the introduction of data copies into containers such as thrust::device_vector or to avoid wrapping e.g. raw pointers allocated by the HIP API with types such as thrust::device_ptr.

The user must take care to guarantee that the iterators provided to an algorithm are compatible with the device backend system. For example, raw pointers allocated by std::malloc typically cannot be dereferenced by a GPU. For this reason, raw pointers allocated by host APIs should not be mixed with a thrust::device algorithm invocation when the device backend is HIP.

The type of thrust::device is implementation-defined.

The following code snippet demonstrates how to use thrust::device to explicitly dispatch an invocation of thrust::for_each to the device backend system:

#include <thrust/for_each.h>
#include <thrust/device_vector.h>
#include <thrust/execution_policy.h>
#include <cstdio>

struct printf_functor
{
  __host__ __device__
  void operator()(int x)
  {
    printf("%d\n", x);
  }
};
...
thrust::device_vector<int> vec(3);
vec[0] = 0; vec[1] = 1; vec[2] = 2;

thrust::for_each(thrust::device, vec.begin(), vec.end(), printf_functor());

// 0 1 2 is printed to standard output in some unspecified order

See also

thrust::device

template<typename DerivedPolicy>
struct host_execution_policy : public thrust::system::__THRUST_HOST_SYSTEM_NAMESPACE::execution_policy<DerivedPolicy>#
#include <execution_policy.h>

host_execution_policy is the base class for all Thrust parallel execution policies which are derived from Thrust’s default host backend system configured with the THRUST_HOST_SYSTEM macro.

Custom user-defined backends which wish to inherit the functionality of Thrust’s host backend system should derive a policy from this type in order to interoperate with Thrust algorithm dispatch.

The following code snippet demonstrates how to derive a standalone custom execution policy from thrust::host_execution_policy to implement a backend which specializes for_each while inheriting the behavior of every other algorithm from the host system:

#include <thrust/execution_policy.h>
#include <iostream>

// define a type derived from thrust::host_execution_policy to distinguish our custom execution policy:
struct my_policy : thrust::host_execution_policy<my_policy> {};

// overload for_each on my_policy
template<typename Iterator, typename Function>
Iterator for_each(my_policy, Iterator first, Iterator last, Function f)
{
  std::cout << "Hello, world from for_each(my_policy)!" << std::endl;

  for(; first < last; ++first)
  {
    f(*first);
  }

  return first;
}

struct ignore_argument
{
  void operator()(int) {}
};

int main()
{
  int data[4];

  // dispatch thrust::for_each using our custom policy:
  my_policy exec;
  thrust::for_each(exec, data, data + 4, ignore_argument());

  // dispatch thrust::transform whose behavior our policy inherits
  thrust::transform(exec, data, data, + 4, data, thrust::identity<int>());

  return 0;
}

See also

execution_policy

template<typename DerivedPolicy>
struct device_execution_policy : public thrust::system::__THRUST_DEVICE_SYSTEM_NAMESPACE::execution_policy<DerivedPolicy>#
#include <execution_policy.h>

device_execution_policy is the base class for all Thrust parallel execution policies which are derived from Thrust’s default device backend system configured with the THRUST_DEVICE_SYSTEM macro.

Custom user-defined backends which wish to inherit the functionality of Thrust’s device backend system should derive a policy from this type in order to interoperate with Thrust algorithm dispatch.

The following code snippet demonstrates how to derive a standalone custom execution policy from thrust::device_execution_policy to implement a backend which specializes for_each while inheriting the behavior of every other algorithm from the device system:

#include <thrust/execution_policy.h>
#include <iostream>

// define a type derived from thrust::device_execution_policy to distinguish our custom execution policy:
struct my_policy : thrust::device_execution_policy<my_policy> {};

// overload for_each on my_policy
template<typename Iterator, typename Function>
Iterator for_each(my_policy, Iterator first, Iterator last, Function f)
{
  std::cout << "Hello, world from for_each(my_policy)!" << std::endl;

  for(; first < last; ++first)
  {
    f(*first);
  }

  return first;
}

struct ignore_argument
{
  void operator()(int) {}
};

int main()
{
  int data[4];

  // dispatch thrust::for_each using our custom policy:
  my_policy exec;
  thrust::for_each(exec, data, data + 4, ignore_argument());

  // dispatch thrust::transform whose behavior our policy inherits
  thrust::transform(exec, data, data, + 4, data, thrust::identity<int>());

  return 0;
}

See also

execution_policy

Function Objects#

group function_objects

Functions

template<typename Predicate> __host__ __device__ unary_negate< Predicate > not1 (const Predicate &pred)

not1 is a helper function to simplify the creation of Adaptable Predicates: it takes an Adaptable Predicate pred as an argument and returns a new Adaptable Predicate that represents the negation of pred. That is: if pred is an object of a type which models Adaptable Predicate, then the the type of the result npred of not1(pred) is also a model of Adaptable Predicate and npred(x) always returns the same value as !pred(x).

See also

unary_negate

See also

not2

Parameters:

pred – The Adaptable Predicate to negate.

Template Parameters:

Predicate – is a model of Adaptable Predicate.

Returns:

A new object, npred such that npred(x) always returns the same value as !pred(x).

template<typename BinaryPredicate> __host__ __device__ binary_negate< BinaryPredicate > not2 (const BinaryPredicate &pred)

not2 is a helper function to simplify the creation of Adaptable Binary Predicates: it takes an Adaptable Binary Predicate pred as an argument and returns a new Adaptable Binary Predicate that represents the negation of pred. That is: if pred is an object of a type which models Adaptable Binary Predicate, then the the type of the result npred of not2(pred) is also a model of Adaptable Binary Predicate and npred(x,y) always returns the same value as !pred(x,y).

See also

binary_negate

See also

not1

Parameters:

pred – The Adaptable Binary Predicate to negate.

Template Parameters:

Binary – Predicate is a model of Adaptable Binary Predicate.

Returns:

A new object, npred such that npred(x,y) always returns the same value as !pred(x,y).

template<typename Argument, typename Result>
struct unary_function#
#include <functional.h>

unary_function is an empty base class: it contains no member functions or member variables, but only type information. The only reason it exists is to make it more convenient to define types that are models of the concept Adaptable Unary Function. Specifically, any model of Adaptable Unary Function must define nested typedefs. Those typedefs are provided by the base class unary_function.

The following code snippet demonstrates how to construct an Adaptable Unary Function using unary_function.

struct sine : public thrust::unary_function<float,float>
{
  __host__ __device__
  float operator()(float x) { return sinf(x); }
};

See also

binary_function

Note

Because C++11 language support makes the functionality of unary_function obsolete, its use is optional if C++11 language features are enabled.

template<typename Argument1, typename Argument2, typename Result>
struct binary_function#
#include <functional.h>

binary_function is an empty base class: it contains no member functions or member variables, but only type information. The only reason it exists is to make it more convenient to define types that are models of the concept Adaptable Binary Function. Specifically, any model of Adaptable Binary Function must define nested typedefs. Those typedefs are provided by the base class binary_function.

The following code snippet demonstrates how to construct an Adaptable Binary Function using binary_function.

struct exponentiate : public thrust::binary_function<float,float,float>
{
  __host__ __device__
  float operator()(float x, float y) { return powf(x,y); }
};

See also

unary_function

Note

Because C++11 language support makes the functionality of binary_function obsolete, its use is optional if C++11 language features are enabled.

template<typename Predicate>
struct unary_negate : public thrust::unary_function<Predicate::argument_type, bool>#
#include <functional.h>

unary_negate is a function object adaptor: it is an Adaptable Predicate that represents the logical negation of some other Adaptable Predicate. That is: if f is an object of class unary_negate<AdaptablePredicate>, then there exists an object pred of class AdaptablePredicate such that f(x) always returns the same value as !pred(x). There is rarely any reason to construct a unary_negate directly; it is almost always easier to use the helper function not1.

See also

not1

template<typename Predicate>
struct binary_negate : public thrust::binary_function<Predicate::first_argument_type, Predicate::second_argument_type, bool>#
#include <functional.h>

binary_negate is a function object adaptor: it is an Adaptable Binary Predicate that represents the logical negation of some other Adaptable Binary Predicate. That is: if f is an object of class binary_negate<AdaptablePredicate>, then there exists an object pred of class AdaptableBinaryPredicate such that f(x,y) always returns the same value as !pred(x,y). There is rarely any reason to construct a binary_negate directly; it is almost always easier to use the helper function not2.

Functions

THRUST_BINARY_FUNCTOR_VOID_SPECIALIZATION_OP (plus,+)

Specialization of plus for type void.

THRUST_BINARY_FUNCTOR_VOID_SPECIALIZATION_OP (minus, -)

Specialization of minus for type void.

THRUST_BINARY_FUNCTOR_VOID_SPECIALIZATION_OP (multiplies, *)

Specialization of multiplies for type void.

THRUST_BINARY_FUNCTOR_VOID_SPECIALIZATION_OP (divides,/)

Specialization of divides for type void.

THRUST_BINARY_FUNCTOR_VOID_SPECIALIZATION_OP (modulus, %)

Specialization of modulus for type void.

THRUST_UNARY_FUNCTOR_VOID_SPECIALIZATION (negate, -THRUST_FWD(x))

Specialization of negate for type void.

THRUST_UNARY_FUNCTOR_VOID_SPECIALIZATION(square, x *x)#

Specialization of square for type void.

template<typename T = void>
struct plus#
#include <functional.h>

plus is a function object. Specifically, it is an Adaptable Binary Function. If f is an object of class plus<T>, and x and y are objects of class T, then f(x,y) returns x+y.

The following code snippet demonstrates how to use plus to sum two device_vectors of floats.

#include <thrust/device_vector.h>
#include <thrust/functional.h>
#include <thrust/sequence.h>
#include <thrust/fill.h>
#include <thrust/transform.h>
...
const int N = 1000;
thrust::device_vector<float> V1(N);
thrust::device_vector<float> V2(N);
thrust::device_vector<float> V3(N);

thrust::sequence(V1.begin(), V1.end(), 1);
thrust::fill(V2.begin(), V2.end(), 75);

thrust::transform(V1.begin(), V1.end(), V2.begin(), V3.begin(),
                  thrust::plus<float>());
// V3 is now {76, 77, 78, ..., 1075}

See also

binary_function

Template Parameters:

T – is a model of Assignable, and if x and y are objects of type T, then x+y must be defined and must have a return type that is convertible to T.

template<typename T = void>
struct minus#
#include <functional.h>

minus is a function object. Specifically, it is an Adaptable Binary Function. If f is an object of class minus<T>, and x and y are objects of class T, then f(x,y) returns x-y.

The following code snippet demonstrates how to use minus to subtract a device_vector of floats from another.

#include <thrust/device_vector.h>
#include <thrust/functional.h>
#include <thrust/sequence.h>
#include <thrust/fill.h>
#include <thrust/transform.h>
...
const int N = 1000;
thrust::device_vector<float> V1(N);
thrust::device_vector<float> V2(N);
thrust::device_vector<float> V3(N);

thrust::sequence(V1.begin(), V1.end(), 1);
thrust::fill(V2.begin(), V2.end(), 75);

thrust::transform(V1.begin(), V1.end(), V2.begin(), V3.begin(),
                  thrust::minus<float>());
// V3 is now {-74, -73, -72, ..., 925}

See also

binary_function

Template Parameters:

T – is a model of Assignable, and if x and y are objects of type T, then x-y must be defined and must have a return type that is convertible to T.

template<typename T = void>
struct multiplies#
#include <functional.h>

multiplies is a function object. Specifically, it is an Adaptable Binary Function. If f is an object of class multiplies<T>, and x and y are objects of class T, then f(x,y) returns x*y.

The following code snippet demonstrates how to use multiplies to multiply two device_vectors of floats.

#include <thrust/device_vector.h>
#include <thrust/functional.h>
#include <thrust/sequence.h>
#include <thrust/fill.h>
#include <thrust/transform.h>
...
const int N = 1000;
thrust::device_vector<float> V1(N);
thrust::device_vector<float> V2(N);
thrust::device_vector<float> V3(N);

thrust::sequence(V1.begin(), V1.end(), 1);
thrust::fill(V2.begin(), V2.end(), 75);

thrust::transform(V1.begin(), V1.end(), V2.begin(), V3.begin(),
                  thrust::multiplies<float>());
// V3 is now {75, 150, 225, ..., 75000}

See also

binary_function

Template Parameters:

T – is a model of Assignable, and if x and y are objects of type T, then x*y must be defined and must have a return type that is convertible to T.

template<typename T = void>
struct divides#
#include <functional.h>

divides is a function object. Specifically, it is an Adaptable Binary Function. If f is an object of class divides<T>, and x and y are objects of class T, then f(x,y) returns x/y.

The following code snippet demonstrates how to use divides to divide one device_vectors of floats by another.

#include <thrust/device_vector.h>
#include <thrust/functional.h>
#include <thrust/sequence.h>
#include <thrust/fill.h>
#include <thrust/transform.h>
...
const int N = 1000;
thrust::device_vector<float> V1(N);
thrust::device_vector<float> V2(N);
thrust::device_vector<float> V3(N);

thrust::sequence(V1.begin(), V1.end(), 1);
thrust::fill(V2.begin(), V2.end(), 75);

thrust::transform(V1.begin(), V1.end(), V2.begin(), V3.begin(),
                  thrust::divides<float>());
// V3 is now {1/75, 2/75, 3/75, ..., 1000/75}

See also

binary_function

Template Parameters:

T – is a model of Assignable, and if x and y are objects of type T, then x/y must be defined and must have a return type that is convertible to T.

template<typename T = void>
struct modulus#
#include <functional.h>

modulus is a function object. Specifically, it is an Adaptable Binary Function. If f is an object of class modulus<T>, and x and y are objects of class T, then f(x,y) returns x % y.

The following code snippet demonstrates how to use modulus to take the modulus of one device_vectors of floats by another.

#include <thrust/device_vector.h>
#include <thrust/functional.h>
#include <thrust/sequence.h>
#include <thrust/fill.h>
#include <thrust/transform.h>
...
const int N = 1000;
thrust::device_vector<float> V1(N);
thrust::device_vector<float> V2(N);
thrust::device_vector<float> V3(N);

thrust::sequence(V1.begin(), V1.end(), 1);
thrust::fill(V2.begin(), V2.end(), 75);

thrust::transform(V1.begin(), V1.end(), V2.begin(), V3.begin(),
                  thrust::modulus<int>());
// V3 is now {1%75, 2%75, 3%75, ..., 1000%75}

See also

binary_function

Template Parameters:

T – is a model of Assignable, and if x and y are objects of type T, then x % y must be defined and must have a return type that is convertible to T.

template<typename T = void>
struct negate#
#include <functional.h>

negate is a function object. Specifically, it is an Adaptable Unary Function. If f is an object of class negate<T>, and x is an object of class T, then f(x) returns -x.

The following code snippet demonstrates how to use negate to negate the elements of a device_vector of floats.

#include <thrust/device_vector.h>
#include <thrust/functional.h>
#include <thrust/sequence.h>
#include <thrust/transform.h>
...
const int N = 1000;
thrust::device_vector<float> V1(N);
thrust::device_vector<float> V2(N);

thrust::sequence(V1.begin(), V1.end(), 1);

thrust::transform(V1.begin(), V1.end(), V2.begin(),
                  thrust::negate<float>());
// V2 is now {-1, -2, -3, ..., -1000}

See also

unary_function

Template Parameters:

T – is a model of Assignable, and if x is an object of type T, then -x must be defined and must have a return type that is convertible to T.

template<typename T = void>
struct square#
#include <functional.h>

square is a function object. Specifically, it is an Adaptable Unary Function. If f is an object of class square<T>, and x is an object of class T, then f(x) returns x*x.

The following code snippet demonstrates how to use square to square the elements of a device_vector of floats.

#include <thrust/device_vector.h>
#include <thrust/functional.h>
#include <thrust/sequence.h>
#include <thrust/transform.h>
...
const int N = 1000;
thrust::device_vector<float> V1(N);
thrust::device_vector<float> V2(N);

thrust::sequence(V1.begin(), V1.end(), 1);

thrust::transform(V1.begin(), V1.end(), V2.begin(),
                  thrust::square<float>());
// V2 is now {1, 4, 9, ..., 1000000}

See also

unary_function

Template Parameters:

T – is a model of Assignable, and if x is an object of type T, then x*x must be defined and must have a return type that is convertible to T.

Functions

THRUST_BINARY_FUNCTOR_VOID_SPECIALIZATION_OP (equal_to,==)

Specialization of equal_to for type void.

THRUST_BINARY_FUNCTOR_VOID_SPECIALIZATION_OP (not_equal_to, !=)

Specialization of not_equal_to for type void.

THRUST_BINARY_FUNCTOR_VOID_SPECIALIZATION_OP (greater, >)

Specialization of greater for type void.

THRUST_BINARY_FUNCTOR_VOID_SPECIALIZATION_OP (less,<)

Specialization of less for type void.

THRUST_BINARY_FUNCTOR_VOID_SPECIALIZATION_OP (greater_equal, >=)

Specialization of greater_equal for type void.

THRUST_BINARY_FUNCTOR_VOID_SPECIALIZATION_OP (less_equal,<=)

Specialization of less_equal for type void.

template<typename T = void>
struct equal_to#
#include <functional.h>

equal_to is a function object. Specifically, it is an Adaptable Binary Predicate, which means it is a function object that tests the truth or falsehood of some condition. If f is an object of class equal_to<T> and x and y are objects of class T, then f(x,y) returns true if x == y and false otherwise.

See also

binary_function

Template Parameters:

T – is a model of Equality Comparable.

template<typename T = void>
struct not_equal_to#
#include <functional.h>

not_equal_to is a function object. Specifically, it is an Adaptable Binary Predicate, which means it is a function object that tests the truth or falsehood of some condition. If f is an object of class not_equal_to<T> and x and y are objects of class T, then f(x,y) returns true if x != y and false otherwise.

See also

binary_function

Template Parameters:

T – is a model of Equality Comparable.

template<typename T = void>
struct greater#
#include <functional.h>

greater is a function object. Specifically, it is an Adaptable Binary Predicate, which means it is a function object that tests the truth or falsehood of some condition. If f is an object of class greater<T> and x and y are objects of class T, then f(x,y) returns true if x > y and false otherwise.

See also

binary_function

Template Parameters:

T – is a model of LessThan Comparable.

template<typename T = void>
struct less#
#include <functional.h>

less is a function object. Specifically, it is an Adaptable Binary Predicate, which means it is a function object that tests the truth or falsehood of some condition. If f is an object of class less<T> and x and y are objects of class T, then f(x,y) returns true if x < y and false otherwise.

See also

binary_function

Template Parameters:

T – is a model of LessThan Comparable.

template<typename T = void>
struct greater_equal#
#include <functional.h>

greater_equal is a function object. Specifically, it is an Adaptable Binary Predicate, which means it is a function object that tests the truth or falsehood of some condition. If f is an object of class greater_equal<T> and x and y are objects of class T, then f(x,y) returns true if x >= y and false otherwise.

See also

binary_function

Template Parameters:

T – is a model of LessThan Comparable.

template<typename T = void>
struct less_equal#
#include <functional.h>

less_equal is a function object. Specifically, it is an Adaptable Binary Predicate, which means it is a function object that tests the truth or falsehood of some condition. If f is an object of class less_equal<T> and x and y are objects of class T, then f(x,y) returns true if x <= y and false otherwise.

See also

binary_function

Template Parameters:

T – is a model of LessThan Comparable.

Functions

THRUST_BINARY_FUNCTOR_VOID_SPECIALIZATION_OP (logical_and, &&)

Specialization of logical_and for type void.

THRUST_BINARY_FUNCTOR_VOID_SPECIALIZATION_OP (logical_or,||)

Specialization of logical_or for type void.

THRUST_UNARY_FUNCTOR_VOID_SPECIALIZATION (logical_not, !THRUST_FWD(x))

Specialization of logical_not for type void.

template<typename T = void>
struct logical_and#
#include <functional.h>

logical_and is a function object. Specifically, it is an Adaptable Binary Predicate, which means it is a function object that tests the truth or falsehood of some condition. If f is an object of class logical_and<T> and x and y are objects of class T (where T is convertible to bool) then f(x,y) returns true if and only if both x and y are true.

See also

binary_function

Template Parameters:

T – must be convertible to bool.

template<typename T = void>
struct logical_or#
#include <functional.h>

logical_or is a function object. Specifically, it is an Adaptable Binary Predicate, which means it is a function object that tests the truth or falsehood of some condition. If f is an object of class logical_or<T> and x and y are objects of class T (where T is convertible to bool) then f(x,y) returns true if and only if either x or y are true.

See also

binary_function

Template Parameters:

T – must be convertible to bool.

template<typename T = void>
struct logical_not#
#include <functional.h>

logical_not is a function object. Specifically, it is an Adaptable Predicate, which means it is a function object that tests the truth or falsehood of some condition. If f is an object of class logical_not<T> and x is an object of class T (where T is convertible to bool) then f(x) returns true if and only if x is false.

The following code snippet demonstrates how to use logical_not to transform a device_vector of bools into its logical complement.

#include <thrust/device_vector.h>
#include <thrust/transform.h>
#include <thrust/functional.h>
...
thrust::device_vector<bool> V;
...
thrust::transform(V.begin(), V.end(), V.begin(), thrust::logical_not<bool>());
// The elements of V are now the logical complement of what they were prior

See also

unary_function

Template Parameters:

T – must be convertible to bool.

Functions

THRUST_BINARY_FUNCTOR_VOID_SPECIALIZATION_OP (bit_and, &)

Specialization of bit_and for type void.

THRUST_BINARY_FUNCTOR_VOID_SPECIALIZATION_OP (bit_or,|)

Specialization of bit_or for type void.

THRUST_BINARY_FUNCTOR_VOID_SPECIALIZATION_OP (bit_xor, ^)

Specialization of bit_xor for type void.

template<typename T = void>
struct bit_and#
#include <functional.h>

bit_and is a function object. Specifically, it is an Adaptable Binary Function. If f is an object of class bit_and<T>, and x and y are objects of class T, then f(x,y) returns x&y.

The following code snippet demonstrates how to use bit_and to take the bitwise AND of one device_vector of ints by another.

#include <thrust/device_vector.h>
#include <thrust/functional.h>
#include <thrust/sequence.h>
#include <thrust/fill.h>
#include <thrust/transform.h>
...
const int N = 1000;
thrust::device_vector<int> V1(N);
thrust::device_vector<int> V2(N);
thrust::device_vector<int> V3(N);

thrust::sequence(V1.begin(), V1.end(), 1);
thrust::fill(V2.begin(), V2.end(), 13);

thrust::transform(V1.begin(), V1.end(), V2.begin(), V3.begin(),
                  thrust::bit_and<int>());
// V3 is now {1&13, 2&13, 3&13, ..., 1000%13}

See also

binary_function

Template Parameters:

T – is a model of Assignable, and if x and y are objects of type T, then x&y must be defined and must have a return type that is convertible to T.

template<typename T = void>
struct bit_or#
#include <functional.h>

bit_or is a function object. Specifically, it is an Adaptable Binary Function. If f is an object of class bit_and<T>, and x and y are objects of class T, then f(x,y) returns x|y.

The following code snippet demonstrates how to use bit_or to take the bitwise OR of one device_vector of ints by another.

#include <thrust/device_vector.h>
#include <thrust/functional.h>
#include <thrust/sequence.h>
#include <thrust/fill.h>
#include <thrust/transform.h>
...
const int N = 1000;
thrust::device_vector<int> V1(N);
thrust::device_vector<int> V2(N);
thrust::device_vector<int> V3(N);

thrust::sequence(V1.begin(), V1.end(), 1);
thrust::fill(V2.begin(), V2.end(), 13);

thrust::transform(V1.begin(), V1.end(), V2.begin(), V3.begin(),
                  thrust::bit_or<int>());
// V3 is now {1|13, 2|13, 3|13, ..., 1000|13}

See also

binary_function

Template Parameters:

T – is a model of Assignable, and if x and y are objects of type T, then x|y must be defined and must have a return type that is convertible to T.

template<typename T = void>
struct bit_xor#
#include <functional.h>

bit_xor is a function object. Specifically, it is an Adaptable Binary Function. If f is an object of class bit_and<T>, and x and y are objects of class T, then f(x,y) returns x^y.

The following code snippet demonstrates how to use bit_xor to take the bitwise XOR of one device_vector of ints by another.

#include <thrust/device_vector.h>
#include <thrust/functional.h>
#include <thrust/sequence.h>
#include <thrust/fill.h>
#include <thrust/transform.h>
...
const int N = 1000;
thrust::device_vector<int> V1(N);
thrust::device_vector<int> V2(N);
thrust::device_vector<int> V3(N);

thrust::sequence(V1.begin(), V1.end(), 1);
thrust::fill(V2.begin(), V2.end(), 13);

thrust::transform(V1.begin(), V1.end(), V2.begin(), V3.begin(),
                  thrust::bit_xor<int>());
// V3 is now {1^13, 2^13, 3^13, ..., 1000^13}

See also

binary_function

Template Parameters:

T – is a model of Assignable, and if x and y are objects of type T, then x^y must be defined and must have a return type that is convertible to T.

Functions

THRUST_UNARY_FUNCTOR_VOID_SPECIALIZATION(identity, THRUST_FWD(x))#

Specialization of identity for type void.

THRUST_BINARY_FUNCTOR_VOID_SPECIALIZATION (maximum, t1< t2 ? THRUST_FWD(t2) :THRUST_FWD(t1))

Specialization of maximum for type void.

THRUST_BINARY_FUNCTOR_VOID_SPECIALIZATION (minimum, t1< t2 ? THRUST_FWD(t1) :THRUST_FWD(t2))

Specialization of minimum for type void.

template<typename T = void>
struct identity#
#include <functional.h>

identity is a Unary Function that represents the identity function: it takes a single argument x, and returns x.

The following code snippet demonstrates that identity returns its argument.

#include <thrust/functional.h>
#include <assert.h>
...
int x = 137;
thrust::identity<int> id;
assert(x == id(x));

See also

unary_function

Template Parameters:

T – No requirements on T.

template<typename T = void>
struct maximum#
#include <functional.h>

maximum is a function object that takes two arguments and returns the greater of the two. Specifically, it is an Adaptable Binary Function. If f is an object of class maximum<T> and x and y are objects of class T f(x,y) returns x if x > y and y, otherwise.

The following code snippet demonstrates that maximum returns its greater argument.

#include <thrust/functional.h>
#include <assert.h>
...
int x =  137;
int y = -137;
thrust::maximum<int> mx;
assert(x == mx(x,y));

See also

minimum

See also

min

See also

binary_function

Template Parameters:

T – is a model of LessThan Comparable.

template<typename T = void>
struct minimum#
#include <functional.h>

minimum is a function object that takes two arguments and returns the lesser of the two. Specifically, it is an Adaptable Binary Function. If f is an object of class minimum<T> and x and y are objects of class T f(x,y) returns x if x < y and y, otherwise.

The following code snippet demonstrates that minimum returns its lesser argument.

#include <thrust/functional.h>
#include <assert.h>
...
int x =  137;
int y = -137;
thrust::minimum<int> mn;
assert(y == mn(x,y));

See also

maximum

See also

max

See also

binary_function

Template Parameters:

T – is a model of LessThan Comparable.

template<typename T1 = void, typename T2 = void>
struct project1st#
#include <functional.h>

project1st is a function object that takes two arguments and returns its first argument; the second argument is unused. It is essentially a generalization of identity to the case of a Binary Function.

#include <thrust/functional.h>
#include <assert.h>
...
int x =  137;
int y = -137;
thrust::project1st<int> pj1;
assert(x == pj1(x,y));

See also

identity

See also

project2nd

See also

binary_function

template<>
struct project1st<void, void>#
#include <functional.h>

Specialization of project1st for two void arguments.

template<typename T1 = void, typename T2 = void>
struct project2nd#
#include <functional.h>

project2nd is a function object that takes two arguments and returns its second argument; the first argument is unused. It is essentially a generalization of identity to the case of a Binary Function.

#include <thrust/functional.h>
#include <assert.h>
...
int x =  137;
int y = -137;
thrust::project2nd<int> pj2;
assert(y == pj2(x,y));

See also

identity

See also

project1st

See also

binary_function

template<>
struct project2nd<void, void>#
#include <functional.h>

Specialization of project2nd for two void arguments.

namespace placeholders#

Facilities for constructing simple functions inline.

Objects in the thrust::placeholders namespace may be used to create simple arithmetic functions inline in an algorithm invocation. Combining placeholders such as _1 and _2 with arithmetic operations such as + creates an unnamed function object which applies the operation to their arguments.

The type of placeholder objects is implementation-defined.

The following code snippet demonstrates how to use the placeholders _1 and _2 with thrust::transform to implement the SAXPY computation:

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

int main()
{
  thrust::device_vector<float> x(4), y(4);
  x[0] = 1;
  x[1] = 2;
  x[2] = 3;
  x[3] = 4;

  y[0] = 1;
  y[1] = 1;
  y[2] = 1;
  y[3] = 1;

  float a = 2.0f;

  using namespace thrust::placeholders;

  thrust::transform(x.begin(), x.end(), y.begin(), y.begin(),
    a * _1 + _2
  );

  // y is now {3, 5, 7, 9}
}
namespace placeholders#

Variables

constexpr thrust::detail::functional::placeholder<0>::type _1#

thrust::placeholders::_1 is the placeholder for the first function parameter.

constexpr thrust::detail::functional::placeholder<1>::type _2#

thrust::placeholders::_2 is the placeholder for the second function parameter.

constexpr thrust::detail::functional::placeholder<2>::type _3#

thrust::placeholders::_3 is the placeholder for the third function parameter.

constexpr thrust::detail::functional::placeholder<3>::type _4#

thrust::placeholders::_4 is the placeholder for the fourth function parameter.

constexpr thrust::detail::functional::placeholder<4>::type _5#

thrust::placeholders::_5 is the placeholder for the fifth function parameter.

constexpr thrust::detail::functional::placeholder<5>::type _6#

thrust::placeholders::_6 is the placeholder for the sixth function parameter.

constexpr thrust::detail::functional::placeholder<6>::type _7#

thrust::placeholders::_7 is the placeholder for the seventh function parameter.

constexpr thrust::detail::functional::placeholder<7>::type _8#

thrust::placeholders::_8 is the placeholder for the eighth function parameter.

constexpr thrust::detail::functional::placeholder<8>::type _9#

thrust::placeholders::_9 is the placeholder for the ninth function parameter.

constexpr thrust::detail::functional::placeholder<9>::type _10#

thrust::placeholders::_10 is the placeholder for the tenth function parameter.

Container Classes#

group container_classes

Functions

template<typename T, typename Alloc>
void swap(host_vector<T, Alloc> &a, host_vector<T, Alloc> &b)#

Exchanges the values of two vectors. x The first host_vector of interest. y The second host_vector of interest.

template<typename T, typename Alloc = std::allocator<T>>
class host_vector : public detail::vector_base<T, std::allocator<T>>#
#include <host_vector.h>

A host_vector is a container that supports random access to elements, constant time removal of elements at the end, and linear time insertion and removal of elements at the beginning or in the middle. The number of elements in a host_vector may vary dynamically; memory management is automatic. The memory associated with a host_vector resides in memory accessible to hosts.

See also

device_vector

See also

universal_vector

Utility#

group utility

Functions

template<typename T1, typename T2> inline __host__ __device__ bool operator== (const pair< T1, T2 > &x, const pair< T1, T2 > &y)

This operator tests two pairs for equality.

Parameters:
  • x – The first pair to compare.

  • y – The second pair to compare.

Template Parameters:
Returns:

true if and only if x.first == y.first && x.second == y.second.

template<typename T1, typename T2> inline __host__ __device__ bool operator< (const pair< T1, T2 > &x, const pair< T1, T2 > &y)

This operator tests two pairs for ascending ordering.

Parameters:
  • x – The first pair to compare.

  • y – The second pair to compare.

Template Parameters:
Returns:

true if and only if x.first < y.first || (!(y.first < x.first) && x.second < y.second).

template<typename T1, typename T2> inline __host__ __device__ bool operator!= (const pair< T1, T2 > &x, const pair< T1, T2 > &y)

This operator tests two pairs for inequality.

Parameters:
  • x – The first pair to compare.

  • y – The second pair to compare.

Template Parameters:
Returns:

true if and only if !(x == y).

template<typename T1, typename T2> inline __host__ __device__ bool operator> (const pair< T1, T2 > &x, const pair< T1, T2 > &y)

This operator tests two pairs for descending ordering.

Parameters:
  • x – The first pair to compare.

  • y – The second pair to compare.

Template Parameters:
Returns:

true if and only if y < x.

template<typename T1, typename T2> inline __host__ __device__ bool operator<= (const pair< T1, T2 > &x, const pair< T1, T2 > &y)

This operator tests two pairs for ascending ordering or equivalence.

Parameters:
  • x – The first pair to compare.

  • y – The second pair to compare.

Template Parameters:
Returns:

true if and only if !(y < x).

template<typename T1, typename T2> inline __host__ __device__ bool operator>= (const pair< T1, T2 > &x, const pair< T1, T2 > &y)

This operator tests two pairs for descending ordering or equivalence.

Parameters:
  • x – The first pair to compare.

  • y – The second pair to compare.

Template Parameters:
Returns:

true if and only if !(x < y).

template<typename T1, typename T2> inline __host__ __device__ void swap (pair< T1, T2 > &x, pair< T1, T2 > &y)

swap swaps the contents of two pairs.

Parameters:
  • x – The first pair to swap.

  • y – The second pair to swap.

template<typename T1, typename T2> inline __host__ __device__ pair< T1, T2 > make_pair (T1 x, T2 y)

This convenience function creates a pair from two objects.

Parameters:
  • x – The first object to copy from.

  • y – The second object to copy from.

Template Parameters:
  • T1 – There are no requirements on the type of T1.

  • T2 – There are no requirements on the type of T2.

Returns:

A newly-constructed pair copied from a and b.

template<typename T1, typename T2>
struct pair#
#include <pair.h>

pair is a generic data structure encapsulating a heterogeneous pair of values.

Template Parameters:
  • T1 – The type of pair's first object type. There are no requirements on the type of T1. T1’s type is provided by pair::first_type.

  • T2 – The type of pair's second object type. There are no requirements on the type of T2. T2’s type is provided by pair::second_type.

template<size_t N, class T>
struct tuple_element#

This convenience metafunction is included for compatibility with tuple. It returns either the type of a pair's first_type or second_type in its nested type, type.

This metafunction returns the type of a tuple's Nth element.

See also

pair

See also

tuple

Template Parameters:
  • N – This parameter selects the member of interest.

  • T – A pair type of interest.

  • N – This parameter selects the element of interest.

  • T – A tuple type of interest.

template<typename Pair>
struct tuple_size#

This convenience metafunction is included for compatibility with tuple. It returns 2, the number of elements of a pair, in its nested data member, value.

This metafunction returns the number of elements of a tuple type of interest.

See also

pair

See also

tuple

Template Parameters:
  • Pair – A pair type of interest.

  • T – A tuple type of interest.

Functions

template<typename Assignable1, typename Assignable2> inline __host__ __device__ void swap (Assignable1 &a, Assignable2 &b)

swap assigns the contents of a to b and the contents of b to a. This is used as a primitive operation by many other algorithms.

The following code snippet demonstrates how to use swap to swap the contents of two variables.

#include <thrust/swap.h>
...
int x = 1;
int y = 2;
thrust::swap(x,h);

// x == 2, y == 1
Parameters:
  • a – The first value of interest. After completion, the value of b will be returned here.

  • b – The second value of interest. After completion, the value of a will be returned here.

Template Parameters:

Assignable – is a model of Assignable.

Functions

template<int N, class HT, class TT> inline __host__ __device__ access_traits< typenametuple_element< N, detail::cons< HT, TT > >::type >::non_const_type get (detail::cons< HT, TT > &t)

The get function returns a reference to a tuple element of interest.

The following code snippet demonstrates how to use get to print the value of a tuple element.

#include <thrust/tuple.h>
#include <iostream>
...
thrust::tuple<int, const char *> t(13, "thrust");

std::cout << "The 1st value of t is " << thrust::get<0>(t) << std::endl;

See also

pair

See also

tuple

Parameters:

t – A reference to a tuple of interest.

Template Parameters:

N – The index of the element of interest.

Returns:

A reference to t's Nth element.

template<int N, class HT, class TT> inline __host__ __device__ access_traits< typenametuple_element< N, detail::cons< HT, TT > >::type >::const_type get (const detail::cons< HT, TT > &t)

The get function returns a const reference to a tuple element of interest.

The following code snippet demonstrates how to use get to print the value of a tuple element.

#include <thrust/tuple.h>
#include <iostream>
...
thrust::tuple<int, const char *> t(13, "thrust");

std::cout << "The 1st value of t is " << thrust::get<0>(t) << std::endl;

See also

pair

See also

tuple

Parameters:

t – A reference to a tuple of interest.

Template Parameters:

N – The index of the element of interest.

Returns:

A const reference to t's Nth element.

template<class T0> inline __host__ __device__ detail::make_tuple_mapper< T0 >::type make_tuple (const T0 &t0)

This version of make_tuple creates a new tuple object from a single object.

Parameters:

t0 – The object to copy from.

Returns:

A tuple object with a single member which is a copy of t0.

template<class T0, class T1> inline __host__ __device__ detail::make_tuple_mapper< T0, T1 >::type make_tuple (const T0 &t0, const T1 &t1)

This version of make_tuple creates a new tuple object from two objects.

Note

make_tuple has ten variants, the rest of which are omitted here for brevity.

Parameters:
  • t0 – The first object to copy from.

  • t1 – The second object to copy from.

Returns:

A tuple object with two members which are copies of t0 and t1.

template<typename T0> inline __host__ __device__ tuple< T0 & > tie (T0 &t0)

This version of tie creates a new tuple whose single element is a reference which refers to this function’s argument.

Parameters:

t0 – The object to reference.

Returns:

A tuple object with one member which is a reference to t0.

template<typename T0, typename T1> inline __host__ __device__ tuple< T0 &, T1 & > tie (T0 &t0, T1 &t1)

This version of tie creates a new tuple of references object which refers to this function’s arguments.

Note

tie has ten variants, the rest of which are omitted here for brevity.

Parameters:
  • t0 – The first object to reference.

  • t1 – The second object to reference.

Returns:

A tuple object with two members which are references to t0 and t1.

template<typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9, typename U0, typename U1, typename U2, typename U3, typename U4, typename U5, typename U6, typename U7, typename U8, typename U9> inline __host__ __device__ void swap (tuple< T0, T1, T2, T3, T4, T5, T6, T7, T8, T9 > &x, tuple< U0, U1, U2, U3, U4, U5, U6, U7, U8, U9 > &y)

swap swaps the contents of two tuples.

Parameters:
  • x – The first tuple to swap.

  • y – The second tuple to swap.

template<size_t N, class T>
struct tuple_element

This convenience metafunction is included for compatibility with tuple. It returns either the type of a pair's first_type or second_type in its nested type, type.

This metafunction returns the type of a tuple's Nth element.

See also

pair

See also

tuple

Template Parameters:
  • N – This parameter selects the member of interest.

  • T – A pair type of interest.

  • N – This parameter selects the element of interest.

  • T – A tuple type of interest.

template<typename Pair>
struct tuple_size

This convenience metafunction is included for compatibility with tuple. It returns 2, the number of elements of a pair, in its nested data member, value.

This metafunction returns the number of elements of a tuple type of interest.

See also

pair

See also

tuple

Template Parameters:
  • Pair – A pair type of interest.

  • T – A tuple type of interest.

template<class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9>
class tuple#
#include <tuple.h>

tuple is a class template that can be instantiated with up to ten arguments. Each template argument specifies the type of element in the tuple. Consequently, tuples are heterogeneous, fixed-size collections of values. An instantiation of tuple with two arguments is similar to an instantiation of pair with the same two arguments. Individual elements of a tuple may be accessed with the get function.

The following code snippet demonstrates how to create a new tuple object and inspect and modify the value of its elements.

#include <thrust/tuple.h>
#include <iostream>

int main() {
  // Create a tuple containing an `int`, a `float`, and a string.
  thrust::tuple<int, float, const char*> t(13, 0.1f, "thrust");

  // Individual members are accessed with the free function `get`.
  std::cout << "The first element's value is " << thrust::get<0>(t) << std::endl;

  // ... or the member function `get`.
  std::cout << "The second element's value is " << t.get<1>() << std::endl;

  // We can also modify elements with the same function.
  thrust::get<0>(t) += 10;
}

See also

pair

See also

get

See also

make_tuple

See also

tuple_element

See also

tuple_size

See also

tie

Template Parameters:

TN – The type of the N tuple element. Thrust’s tuple type currently supports up to ten elements.

Random Number Generation#

group random
namespace random#

thrust::random is the namespace which contains random number engine class templates, random number engine adaptor class templates, engines with predefined parameters, and random number distribution class templates. They are provided in a separate namespace for import convenience but are also aliased in the top-level thrust namespace for easy access.

namespace random#
template<typename Engine, size_t p, size_t r>
class discard_block_engine#
#include <discard_block_engine.h>

A discard_block_engine adapts an existing base random number engine and produces random values by discarding some of the values returned by its base engine. Each cycle of the compound engine begins by returning r values successively produced by the base engine and ends by discarding p-r such values. The engine’s state is the state of its base engine followed by the number of calls to operator() that have occurred since the beginning of the current cycle.

The following code snippet shows an example of using a discard_block_engine instance:

#include <thrust/random/linear_congruential_engine.h>
#include <thrust/random/discard_block_engine.h>
#include <iostream>

int main(void)
{
  // create a discard_block_engine from minstd_rand, with a cycle length of 13
  // keep every first 10 values, and discard the next 3
  thrust::discard_block_engine<thrust::minstd_rand, 13, 10> rng;

  // print a random number to standard output
  std::cout << rng() << std::endl;

  return 0;
}
Template Parameters:
  • Engine – The type of the base random number engine to adapt.

  • p – The discard cycle length.

  • r – The number of values to return of the base engine. Because p-r will be discarded, r <= p.

template<typename UIntType, UIntType a, UIntType c, UIntType m>
class linear_congruential_engine#
#include <linear_congruential_engine.h>

A linear_congruential_engine random number engine produces unsigned integer random numbers using a linear congruential random number generation algorithm.

The generation algorithm has the form x_i = (a * x_{i-1} + c) mod m.

The following code snippet shows examples of use of a linear_congruential_engine instance:

#include <thrust/random/linear_congruential_engine.h>
#include <iostream>

int main(void)
{
  // create a minstd_rand object, which is an instance of linear_congruential_engine
  thrust::minstd_rand rng1;

  // output some random values to cout
  std::cout << rng1() << std::endl;

  // a random value is printed

  // create a new minstd_rand from a seed
  thrust::minstd_rand rng2(13);

  // discard some random values
  rng2.discard(13);

  // stream the object to an iostream
  std::cout << rng2 << std::endl;

  // rng2's current state is printed

  // print the minimum and maximum values that minstd_rand can produce
  std::cout << thrust::minstd_rand::min << std::endl;
  std::cout << thrust::minstd_rand::max << std::endl;

  // the range of minstd_rand is printed

  // save the state of rng2 to a different object
  thrust::minstd_rand rng3 = rng2;

  // compare rng2 and rng3
  std::cout << (rng2 == rng3) << std::endl;

  // 1 is printed

  // re-seed rng2 with a different seed
  rng2.seed(7);

  // compare rng2 and rng3
  std::cout << (rng2 == rng3) << std::endl;

  // 0 is printed

  return 0;
}

See also

thrust::random::minstd_rand

See also

thrust::random::minstd_rand0

Note

Inexperienced users should not use this class template directly. Instead, use minstd_rand or minstd_rand0.

Template Parameters:
  • UIntType – The type of unsigned integer to produce.

  • a – The multiplier used in the generation algorithm.

  • c – The increment used in the generation algorithm.

  • m – The modulus used in the generation algorithm.

template<typename UIntType, size_t w, size_t k, size_t q, size_t s>
class linear_feedback_shift_engine#
#include <linear_feedback_shift_engine.h>

A linear_feedback_shift_engine random number engine produces unsigned integer random values using a linear feedback shift random number generation algorithm.

Note

linear_feedback_shift_engine is based on the Boost Template Library’s linear_feedback_shift.

Template Parameters:
  • UIntType – The type of unsigned integer to produce.

  • w – The word size of the produced values (w <= sizeof(UIntType)).

  • k – The k parameter of Tausworthe’s 1965 algorithm.

  • q – The q exponent of Tausworthe’s 1965 algorithm.

  • s – The step size of Tausworthe’s 1965 algorithm.

template<typename RealType = double>
class normal_distribution : public detail::normal_distribution_base::type#
#include <normal_distribution.h>

A normal_distribution random number distribution produces floating point Normally distributed random numbers.

The following code snippet demonstrates examples of using a normal_distribution with a random number engine to produce random values drawn from the Normal distribution with a given mean and variance:

#include <thrust/random/linear_congruential_engine.h>
#include <thrust/random/normal_distribution.h>

int main(void)
{
  // create a minstd_rand object to act as our source of randomness
  thrust::minstd_rand rng;

  // create a normal_distribution to produce floats from the Normal distribution
  // with mean 2.0 and standard deviation 3.5
  thrust::random::normal_distribution<float> dist(2.0f, 3.5f);

  // write a random number to standard output
  std::cout << dist(rng) << std::endl;

  // write the mean of the distribution, just in case we forgot
  std::cout << dist.mean() << std::endl;

  // 2.0 is printed

  // and the standard deviation
  std::cout << dist.stddev() << std::endl;

  // 3.5 is printed

  return 0;
}
Template Parameters:

RealType – The type of floating point number to produce.

template<typename UIntType, size_t w, size_t s, size_t r>
class subtract_with_carry_engine#
#include <subtract_with_carry_engine.h>

A subtract_with_carry_engine random number engine produces unsigned integer random numbers using the subtract with carry algorithm of Marsaglia & Zaman.

The generation algorithm is performed as follows:

  1. Let Y = X_{i-s}- X_{i-r} - c.

  2. Set X_i to y = T mod m. Set c to 1 if Y < 0, otherwise set c to 0.

This algorithm corresponds to a modular linear function of the form

TA(x_i) = (a * x_i) mod b, where b is of the form m^r - m^s + 1 and a = b - (b-1)/m.

See also

thrust::random::ranlux24_base

See also

thrust::random::ranlux48_base

Note

Inexperienced users should not use this class template directly. Instead, use ranlux24_base or ranlux48_base, which are instances of subtract_with_carry_engine.

Template Parameters:
  • UIntType – The type of unsigned integer to produce.

  • w – The word size of the produced values ( w <= sizeof(UIntType)).

  • s – The short lag of the generation algorithm.

  • r – The long lag of the generation algorithm.

template<typename IntType = int>
class uniform_int_distribution#
#include <uniform_int_distribution.h>

A uniform_int_distribution random number distribution produces signed or unsigned integer uniform random numbers from a given range.

The following code snippet demonstrates examples of using a uniform_int_distribution with a random number engine to produce random integers drawn from a given range:

#include <thrust/random/linear_congruential_engine.h>
#include <thrust/random/uniform_int_distribution.h>

int main(void)
{
  // create a minstd_rand object to act as our source of randomness
  thrust::minstd_rand rng;

  // create a uniform_int_distribution to produce ints from [-7,13]
  thrust::uniform_int_distribution<int> dist(-7,13);

  // write a random number from the range [-7,13] to standard output
  std::cout << dist(rng) << std::endl;

  // write the range of the distribution, just in case we forgot
  std::cout << dist.min() << std::endl;

  // -7 is printed

  std::cout << dist.max() << std::endl;

  // 13 is printed

  // write the parameters of the distribution (which happen to be the bounds) to standard output
  std::cout << dist.a() << std::endl;

  // -7 is printed

  std::cout << dist.b() << std::endl;

  // 13 is printed

  return 0;
}
Template Parameters:

IntType – The type of integer to produce.

template<typename RealType = double>
class uniform_real_distribution#
#include <uniform_real_distribution.h>

A uniform_real_distribution random number distribution produces floating point uniform random numbers from a half-open interval.

The following code snippet demonstrates examples of using a uniform_real_distribution with a random number engine to produce random integers drawn from a given range:

#include <thrust/random/linear_congruential_engine.h>
#include <thrust/random/uniform_real_distribution.h>

int main(void)
{
  // create a minstd_rand object to act as our source of randomness
  thrust::minstd_rand rng;

  // create a uniform_real_distribution to produce floats from [-7,13)
  thrust::uniform_real_distribution<float> dist(-7,13);

  // write a random number from the range [-7,13) to standard output
  std::cout << dist(rng) << std::endl;

  // write the range of the distribution, just in case we forgot
  std::cout << dist.min() << std::endl;

  // -7.0 is printed

  std::cout << dist.max() << std::endl;

  // 13.0 is printed

  // write the parameters of the distribution (which happen to be the bounds) to standard output
  std::cout << dist.a() << std::endl;

  // -7.0 is printed

  std::cout << dist.b() << std::endl;

  // 13.0 is printed

  return 0;
}
Template Parameters:

RealType – The type of floating point number to produce.

template<typename Engine1, size_t s1, typename Engine2, size_t s2 = 0u>
class xor_combine_engine#
#include <xor_combine_engine.h>

An xor_combine_engine adapts two existing base random number engines and produces random values by combining the values produced by each.

The following code snippet shows an example of using an xor_combine_engine instance:

#include <thrust/random/linear_congruential_engine.h>
#include <thrust/random/xor_combine_engine.h>
#include <iostream>

int main(void)
{
  // create an xor_combine_engine from minstd_rand and minstd_rand0
  // use a shift of 0 for each
  thrust::xor_combine_engine<thrust::minstd_rand,0,thrust::minstd_rand0,0> rng;

  // print a random number to standard output
  std::cout << rng() << std::endl;

  return 0;
}
Template Parameters:
  • Engine1 – The type of the first base random number engine to adapt.

  • s1 – The size of the first shift to use in the generation algorithm.

  • Engine2 – The type of the second base random number engine to adapt.

  • s2 – The second of the second shift to use in the generation algorithm. Defaults to 0.

Typedefs

typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24#

A random number engine with predefined parameters which implements the RANLUX level-3 random number generation algorithm.

Note

The 10000th consecutive invocation of a default-constructed object of type ranlux24 shall produce the value 9901578 .

typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48#

A random number engine with predefined parameters which implements the RANLUX level-4 random number generation algorithm.

Note

The 10000th consecutive invocation of a default-constructed object of type ranlux48 shall produce the value 88229545517833 .

typedef xor_combine_engine<linear_feedback_shift_engine<thrust::detail::uint32_t, 32u, 31u, 13u, 12u>, 0, xor_combine_engine<linear_feedback_shift_engine<thrust::detail::uint32_t, 32u, 29u, 2u, 4u>, 0, linear_feedback_shift_engine<thrust::detail::uint32_t, 32u, 28u, 3u, 17u>, 0>, 0> taus88#

A random number engine with predefined parameters which implements L’Ecuyer’s 1996 three-component Tausworthe random number generator.

Note

The 10000th consecutive invocation of a default-constructed object of type taus88 shall produce the value 3535848941 .

typedef minstd_rand default_random_engine#

An implementation-defined “default” random number engine.

Note

default_random_engine is currently an alias for minstd_rand, and may change in a future version.

typedef linear_congruential_engine<thrust::detail::uint32_t, 16807, 0, 2147483647> minstd_rand0#

A random number engine with predefined parameters which implements a version of the Minimal Standard random number generation algorithm.

Note

The 10000th consecutive invocation of a default-constructed object of type minstd_rand0 shall produce the value 1043618065 .

typedef linear_congruential_engine<thrust::detail::uint32_t, 48271, 0, 2147483647> minstd_rand#

A random number engine with predefined parameters which implements a version of the Minimal Standard random number generation algorithm.

Note

The 10000th consecutive invocation of a default-constructed object of type minstd_rand shall produce the value 399268537 .

typedef subtract_with_carry_engine<thrust::detail::uint32_t, 24, 10, 24> ranlux24_base#

A random number engine with predefined parameters which implements the base engine of the ranlux24 random number engine.

Note

The 10000th consecutive invocation of a default-constructed object of type ranlux24_base shall produce the value 7937952 .

typedef subtract_with_carry_engine<thrust::detail::uint64_t, 48, 5, 12> ranlux48_base#

A random number engine with predefined parameters which implements the base engine of the ranlux48 random number engine.

Note

The 10000th consecutive invocation of a default-constructed object of type ranlux48_base shall produce the value 192113843633948 .

Functions

template<typename Engine, size_t p, size_t r> __host__ __device__ bool operator== (const discard_block_engine< Engine, p, r > &lhs, const discard_block_engine< Engine, p, r > &rhs)

This function checks two discard_block_engines for equality.

Parameters:
  • lhs – The first discard_block_engine to test.

  • rhs – The second discard_block_engine to test.

Returns:

true if lhs is equal to rhs; false, otherwise.

template<typename Engine, size_t p, size_t r> __host__ __device__ bool operator!= (const discard_block_engine< Engine, p, r > &lhs, const discard_block_engine< Engine, p, r > &rhs)

This function checks two discard_block_engines for inequality.

Parameters:
  • lhs – The first discard_block_engine to test.

  • rhs – The second discard_block_engine to test.

Returns:

true if lhs is not equal to rhs; false, otherwise.

template<typename Engine, size_t p, size_t r, typename CharT, typename Traits>
std::basic_ostream<CharT, Traits> &operator<<(std::basic_ostream<CharT, Traits> &os, const discard_block_engine<Engine, p, r> &e)#

This function streams a discard_block_engine to a std::basic_ostream.

Parameters:
  • os – The basic_ostream to stream out to.

  • e – The discard_block_engine to stream out.

Returns:

os

template<typename Engine, size_t p, size_t r, typename CharT, typename Traits>
std::basic_istream<CharT, Traits> &operator>>(std::basic_istream<CharT, Traits> &is, discard_block_engine<Engine, p, r> &e)#

This function streams a discard_block_engine in from a std::basic_istream.

Parameters:
  • is – The basic_istream to stream from.

  • e – The discard_block_engine to stream in.

Returns:

is

template<typename Engine1_, size_t s1_, typename Engine2_, size_t s2_> __host__ __device__ bool operator== (const xor_combine_engine< Engine1_, s1_, Engine2_, s2_ > &lhs, const xor_combine_engine< Engine1_, s1_, Engine2_, s2_ > &rhs)

This function checks two xor_combine_engines for equality.

Parameters:
  • lhs – The first xor_combine_engine to test.

  • rhs – The second xor_combine_engine to test.

Returns:

true if lhs is equal to rhs; false, otherwise.

template<typename Engine1_, size_t s1_, typename Engine2_, size_t s2_> __host__ __device__ bool operator!= (const xor_combine_engine< Engine1_, s1_, Engine2_, s2_ > &lhs, const xor_combine_engine< Engine1_, s1_, Engine2_, s2_ > &rhs)

This function checks two xor_combine_engines for inequality.

Parameters:
  • lhs – The first xor_combine_engine to test.

  • rhs – The second xor_combine_engine to test.

Returns:

true if lhs is not equal to rhs; false, otherwise.

template<typename Engine1_, size_t s1_, typename Engine2_, size_t s2_, typename CharT, typename Traits>
std::basic_ostream<CharT, Traits> &operator<<(std::basic_ostream<CharT, Traits> &os, const xor_combine_engine<Engine1_, s1_, Engine2_, s2_> &e)#

This function streams a xor_combine_engine to a std::basic_ostream.

Parameters:
  • os – The basic_ostream to stream out to.

  • e – The xor_combine_engine to stream out.

Returns:

os

template<typename Engine1_, size_t s1_, typename Engine2_, size_t s2_, typename CharT, typename Traits>
std::basic_istream<CharT, Traits> &operator>>(std::basic_istream<CharT, Traits> &is, xor_combine_engine<Engine1_, s1_, Engine2_, s2_> &e)#

This function streams a xor_combine_engine in from a std::basic_istream.

Parameters:
  • is – The basic_istream to stream from.

  • e – The xor_combine_engine to stream in.

Returns:

is

template<typename Engine, size_t p, size_t r>
class discard_block_engine
#include <discard_block_engine.h>

A discard_block_engine adapts an existing base random number engine and produces random values by discarding some of the values returned by its base engine. Each cycle of the compound engine begins by returning r values successively produced by the base engine and ends by discarding p-r such values. The engine’s state is the state of its base engine followed by the number of calls to operator() that have occurred since the beginning of the current cycle.

The following code snippet shows an example of using a discard_block_engine instance:

#include <thrust/random/linear_congruential_engine.h>
#include <thrust/random/discard_block_engine.h>
#include <iostream>

int main(void)
{
  // create a discard_block_engine from minstd_rand, with a cycle length of 13
  // keep every first 10 values, and discard the next 3
  thrust::discard_block_engine<thrust::minstd_rand, 13, 10> rng;

  // print a random number to standard output
  std::cout << rng() << std::endl;

  return 0;
}
Template Parameters:
  • Engine – The type of the base random number engine to adapt.

  • p – The discard cycle length.

  • r – The number of values to return of the base engine. Because p-r will be discarded, r <= p.

template<typename Engine1, size_t s1, typename Engine2, size_t s2 = 0u>
class xor_combine_engine
#include <xor_combine_engine.h>

An xor_combine_engine adapts two existing base random number engines and produces random values by combining the values produced by each.

The following code snippet shows an example of using an xor_combine_engine instance:

#include <thrust/random/linear_congruential_engine.h>
#include <thrust/random/xor_combine_engine.h>
#include <iostream>

int main(void)
{
  // create an xor_combine_engine from minstd_rand and minstd_rand0
  // use a shift of 0 for each
  thrust::xor_combine_engine<thrust::minstd_rand,0,thrust::minstd_rand0,0> rng;

  // print a random number to standard output
  std::cout << rng() << std::endl;

  return 0;
}
Template Parameters:
  • Engine1 – The type of the first base random number engine to adapt.

  • s1 – The size of the first shift to use in the generation algorithm.

  • Engine2 – The type of the second base random number engine to adapt.

  • s2 – The second of the second shift to use in the generation algorithm. Defaults to 0.

Functions

template<typename UIntType_, UIntType_ a_, UIntType_ c_, UIntType_ m_> __host__ __device__ bool operator== (const linear_congruential_engine< UIntType_, a_, c_, m_ > &lhs, const linear_congruential_engine< UIntType_, a_, c_, m_ > &rhs)

This function checks two linear_congruential_engines for equality.

Parameters:
  • lhs – The first linear_congruential_engine to test.

  • rhs – The second linear_congruential_engine to test.

Returns:

true if lhs is equal to rhs; false, otherwise.

template<typename UIntType_, UIntType_ a_, UIntType_ c_, UIntType_ m_> __host__ __device__ bool operator!= (const linear_congruential_engine< UIntType_, a_, c_, m_ > &lhs, const linear_congruential_engine< UIntType_, a_, c_, m_ > &rhs)

This function checks two linear_congruential_engines for inequality.

Parameters:
  • lhs – The first linear_congruential_engine to test.

  • rhs – The second linear_congruential_engine to test.

Returns:

true if lhs is not equal to rhs; false, otherwise.

template<typename UIntType_, UIntType_ a_, UIntType_ c_, UIntType_ m_, typename CharT, typename Traits>
std::basic_ostream<CharT, Traits> &operator<<(std::basic_ostream<CharT, Traits> &os, const linear_congruential_engine<UIntType_, a_, c_, m_> &e)#

This function streams a linear_congruential_engine to a std::basic_ostream.

Parameters:
  • os – The basic_ostream to stream out to.

  • e – The linear_congruential_engine to stream out.

Returns:

os

template<typename UIntType_, UIntType_ a_, UIntType_ c_, UIntType_ m_, typename CharT, typename Traits>
std::basic_istream<CharT, Traits> &operator>>(std::basic_istream<CharT, Traits> &is, linear_congruential_engine<UIntType_, a_, c_, m_> &e)#

This function streams a linear_congruential_engine in from a std::basic_istream.

Parameters:
  • is – The basic_istream to stream from.

  • e – The linear_congruential_engine to stream in.

Returns:

is

template<typename UIntType_, size_t w_, size_t k_, size_t q_, size_t s_> __host__ __device__ bool operator== (const linear_feedback_shift_engine< UIntType_, w_, k_, q_, s_ > &lhs, const linear_feedback_shift_engine< UIntType_, w_, k_, q_, s_ > &rhs)

This function checks two linear_feedback_shift_engines for equality.

Parameters:
  • lhs – The first linear_feedback_shift_engine to test.

  • rhs – The second linear_feedback_shift_engine to test.

Returns:

true if lhs is equal to rhs; false, otherwise.

template<typename UIntType_, size_t w_, size_t k_, size_t q_, size_t s_> __host__ __device__ bool operator!= (const linear_feedback_shift_engine< UIntType_, w_, k_, q_, s_ > &lhs, const linear_feedback_shift_engine< UIntType_, w_, k_, q_, s_ > &rhs)

This function checks two linear_feedback_shift_engines for inequality.

Parameters:
  • lhs – The first linear_feedback_shift_engine to test.

  • rhs – The second linear_feedback_shift_engine to test.

Returns:

true if lhs is not equal to rhs; false, otherwise.

template<typename UIntType_, size_t w_, size_t k_, size_t q_, size_t s_, typename CharT, typename Traits>
std::basic_ostream<CharT, Traits> &operator<<(std::basic_ostream<CharT, Traits> &os, const linear_feedback_shift_engine<UIntType_, w_, k_, q_, s_> &e)#

This function streams a linear_feedback_shift_engine to a std::basic_ostream.

Parameters:
  • os – The basic_ostream to stream out to.

  • e – The linear_feedback_shift_engine to stream out.

Returns:

os

template<typename UIntType_, size_t w_, size_t k_, size_t q_, size_t s_, typename CharT, typename Traits>
std::basic_istream<CharT, Traits> &operator>>(std::basic_istream<CharT, Traits> &is, linear_feedback_shift_engine<UIntType_, w_, k_, q_, s_> &e)#

This function streams a linear_feedback_shift_engine in from a std::basic_istream.

Parameters:
  • is – The basic_istream to stream from.

  • e – The linear_feedback_shift_engine to stream in.

Returns:

is

template<typename UIntType_, size_t w_, size_t s_, size_t r_> __host__ __device__ bool operator== (const subtract_with_carry_engine< UIntType_, w_, s_, r_ > &lhs, const subtract_with_carry_engine< UIntType_, w_, s_, r_ > &rhs)

This function checks two subtract_with_carry_engines for equality.

Parameters:
  • lhs – The first subtract_with_carry_engine to test.

  • rhs – The second subtract_with_carry_engine to test.

Returns:

true if lhs is equal to rhs; false, otherwise.

template<typename UIntType_, size_t w_, size_t s_, size_t r_> __host__ __device__ bool operator!= (const subtract_with_carry_engine< UIntType_, w_, s_, r_ > &lhs, const subtract_with_carry_engine< UIntType_, w_, s_, r_ > &rhs)

This function checks two subtract_with_carry_engines for inequality.

Parameters:
  • lhs – The first subtract_with_carry_engine to test.

  • rhs – The second subtract_with_carry_engine to test.

Returns:

true if lhs is not equal to rhs; false, otherwise.

template<typename UIntType_, size_t w_, size_t s_, size_t r_, typename CharT, typename Traits>
std::basic_ostream<CharT, Traits> &operator<<(std::basic_ostream<CharT, Traits> &os, const subtract_with_carry_engine<UIntType_, w_, s_, r_> &e)#

This function streams a subtract_with_carry_engine to a std::basic_ostream.

Parameters:
  • os – The basic_ostream to stream out to.

  • e – The subtract_with_carry_engine to stream out.

Returns:

os

template<typename UIntType_, size_t w_, size_t s_, size_t r_, typename CharT, typename Traits>
std::basic_istream<CharT, Traits> &operator>>(std::basic_istream<CharT, Traits> &is, subtract_with_carry_engine<UIntType_, w_, s_, r_> &e)#

This function streams a subtract_with_carry_engine in from a std::basic_istream.

Parameters:
  • is – The basic_istream to stream from.

  • e – The subtract_with_carry_engine to stream in.

Returns:

is

template<typename UIntType, UIntType a, UIntType c, UIntType m>
class linear_congruential_engine
#include <linear_congruential_engine.h>

A linear_congruential_engine random number engine produces unsigned integer random numbers using a linear congruential random number generation algorithm.

The generation algorithm has the form x_i = (a * x_{i-1} + c) mod m.

The following code snippet shows examples of use of a linear_congruential_engine instance:

#include <thrust/random/linear_congruential_engine.h>
#include <iostream>

int main(void)
{
  // create a minstd_rand object, which is an instance of linear_congruential_engine
  thrust::minstd_rand rng1;

  // output some random values to cout
  std::cout << rng1() << std::endl;

  // a random value is printed

  // create a new minstd_rand from a seed
  thrust::minstd_rand rng2(13);

  // discard some random values
  rng2.discard(13);

  // stream the object to an iostream
  std::cout << rng2 << std::endl;

  // rng2's current state is printed

  // print the minimum and maximum values that minstd_rand can produce
  std::cout << thrust::minstd_rand::min << std::endl;
  std::cout << thrust::minstd_rand::max << std::endl;

  // the range of minstd_rand is printed

  // save the state of rng2 to a different object
  thrust::minstd_rand rng3 = rng2;

  // compare rng2 and rng3
  std::cout << (rng2 == rng3) << std::endl;

  // 1 is printed

  // re-seed rng2 with a different seed
  rng2.seed(7);

  // compare rng2 and rng3
  std::cout << (rng2 == rng3) << std::endl;

  // 0 is printed

  return 0;
}

See also

thrust::random::minstd_rand

See also

thrust::random::minstd_rand0

Note

Inexperienced users should not use this class template directly. Instead, use minstd_rand or minstd_rand0.

Template Parameters:
  • UIntType – The type of unsigned integer to produce.

  • a – The multiplier used in the generation algorithm.

  • c – The increment used in the generation algorithm.

  • m – The modulus used in the generation algorithm.

template<typename UIntType, size_t w, size_t k, size_t q, size_t s>
class linear_feedback_shift_engine
#include <linear_feedback_shift_engine.h>

A linear_feedback_shift_engine random number engine produces unsigned integer random values using a linear feedback shift random number generation algorithm.

Note

linear_feedback_shift_engine is based on the Boost Template Library’s linear_feedback_shift.

Template Parameters:
  • UIntType – The type of unsigned integer to produce.

  • w – The word size of the produced values (w <= sizeof(UIntType)).

  • k – The k parameter of Tausworthe’s 1965 algorithm.

  • q – The q exponent of Tausworthe’s 1965 algorithm.

  • s – The step size of Tausworthe’s 1965 algorithm.

template<typename UIntType, size_t w, size_t s, size_t r>
class subtract_with_carry_engine
#include <subtract_with_carry_engine.h>

A subtract_with_carry_engine random number engine produces unsigned integer random numbers using the subtract with carry algorithm of Marsaglia & Zaman.

The generation algorithm is performed as follows:

  1. Let Y = X_{i-s}- X_{i-r} - c.

  2. Set X_i to y = T mod m. Set c to 1 if Y < 0, otherwise set c to 0.

This algorithm corresponds to a modular linear function of the form

TA(x_i) = (a * x_i) mod b, where b is of the form m^r - m^s + 1 and a = b - (b-1)/m.

See also

thrust::random::ranlux24_base

See also

thrust::random::ranlux48_base

Note

Inexperienced users should not use this class template directly. Instead, use ranlux24_base or ranlux48_base, which are instances of subtract_with_carry_engine.

Template Parameters:
  • UIntType – The type of unsigned integer to produce.

  • w – The word size of the produced values ( w <= sizeof(UIntType)).

  • s – The short lag of the generation algorithm.

  • r – The long lag of the generation algorithm.

Functions

template<typename RealType> __host__ __device__ bool operator== (const normal_distribution< RealType > &lhs, const normal_distribution< RealType > &rhs)

This function checks two normal_distributions for equality.

Parameters:
  • lhs – The first normal_distribution to test.

  • rhs – The second normal_distribution to test.

Returns:

true if lhs is equal to rhs; false, otherwise.

template<typename RealType> __host__ __device__ bool operator!= (const normal_distribution< RealType > &lhs, const normal_distribution< RealType > &rhs)

This function checks two normal_distributions for inequality.

Parameters:
  • lhs – The first normal_distribution to test.

  • rhs – The second normal_distribution to test.

Returns:

true if lhs is not equal to rhs; false, otherwise.

template<typename RealType, typename CharT, typename Traits>
std::basic_ostream<CharT, Traits> &operator<<(std::basic_ostream<CharT, Traits> &os, const normal_distribution<RealType> &d)#

This function streams a normal_distribution to a std::basic_ostream.

Parameters:
  • os – The basic_ostream to stream out to.

  • d – The normal_distribution to stream out.

Returns:

os

template<typename RealType, typename CharT, typename Traits>
std::basic_istream<CharT, Traits> &operator>>(std::basic_istream<CharT, Traits> &is, normal_distribution<RealType> &d)#

This function streams a normal_distribution in from a std::basic_istream.

Parameters:
  • is – The basic_istream to stream from.

  • d – The normal_distribution to stream in.

Returns:

is

template<typename IntType> __host__ __device__ bool operator== (const uniform_int_distribution< IntType > &lhs, const uniform_int_distribution< IntType > &rhs)

This function checks two uniform_int_distributions for equality.

Parameters:
  • lhs – The first uniform_int_distribution to test.

  • rhs – The second uniform_int_distribution to test.

Returns:

true if lhs is equal to rhs; false, otherwise.

template<typename IntType> __host__ __device__ bool operator!= (const uniform_int_distribution< IntType > &lhs, const uniform_int_distribution< IntType > &rhs)

This function checks two uniform_int_distributions for inequality.

Parameters:
  • lhs – The first uniform_int_distribution to test.

  • rhs – The second uniform_int_distribution to test.

Returns:

true if lhs is not equal to rhs; false, otherwise.

template<typename IntType, typename CharT, typename Traits>
std::basic_ostream<CharT, Traits> &operator<<(std::basic_ostream<CharT, Traits> &os, const uniform_int_distribution<IntType> &d)#

This function streams a uniform_int_distribution to a std::basic_ostream.

Parameters:
  • os – The basic_ostream to stream out to.

  • d – The uniform_int_distribution to stream out.

Returns:

os

template<typename IntType, typename CharT, typename Traits>
std::basic_istream<CharT, Traits> &operator>>(std::basic_istream<CharT, Traits> &is, uniform_int_distribution<IntType> &d)#

This function streams a uniform_int_distribution in from a std::basic_istream.

Parameters:
  • is – The basic_istream to stream from.

  • d – The uniform_int_distribution to stream in.

Returns:

is

template<typename RealType> __host__ __device__ bool operator== (const uniform_real_distribution< RealType > &lhs, const uniform_real_distribution< RealType > &rhs)

This function checks two uniform_real_distributions for equality.

Parameters:
  • lhs – The first uniform_real_distribution to test.

  • rhs – The second uniform_real_distribution to test.

Returns:

true if lhs is equal to rhs; false, otherwise.

template<typename RealType> __host__ __device__ bool operator!= (const uniform_real_distribution< RealType > &lhs, const uniform_real_distribution< RealType > &rhs)

This function checks two uniform_real_distributions for inequality.

Parameters:
  • lhs – The first uniform_real_distribution to test.

  • rhs – The second uniform_real_distribution to test.

Returns:

true if lhs is not equal to rhs; false, otherwise.

template<typename RealType, typename CharT, typename Traits>
std::basic_ostream<CharT, Traits> &operator<<(std::basic_ostream<CharT, Traits> &os, const uniform_real_distribution<RealType> &d)#

This function streams a uniform_real_distribution to a std::basic_ostream.

Parameters:
  • os – The basic_ostream to stream out to.

  • d – The uniform_real_distribution to stream out.

Returns:

os

template<typename RealType, typename CharT, typename Traits>
std::basic_istream<CharT, Traits> &operator>>(std::basic_istream<CharT, Traits> &is, uniform_real_distribution<RealType> &d)#

This function streams a uniform_real_distribution in from a std::basic_istream.

Parameters:
  • is – The basic_istream to stream from.

  • d – The uniform_real_distribution to stream in.

Returns:

is

template<typename RealType = double>
class normal_distribution : public detail::normal_distribution_base::type
#include <normal_distribution.h>

A normal_distribution random number distribution produces floating point Normally distributed random numbers.

The following code snippet demonstrates examples of using a normal_distribution with a random number engine to produce random values drawn from the Normal distribution with a given mean and variance:

#include <thrust/random/linear_congruential_engine.h>
#include <thrust/random/normal_distribution.h>

int main(void)
{
  // create a minstd_rand object to act as our source of randomness
  thrust::minstd_rand rng;

  // create a normal_distribution to produce floats from the Normal distribution
  // with mean 2.0 and standard deviation 3.5
  thrust::random::normal_distribution<float> dist(2.0f, 3.5f);

  // write a random number to standard output
  std::cout << dist(rng) << std::endl;

  // write the mean of the distribution, just in case we forgot
  std::cout << dist.mean() << std::endl;

  // 2.0 is printed

  // and the standard deviation
  std::cout << dist.stddev() << std::endl;

  // 3.5 is printed

  return 0;
}
Template Parameters:

RealType – The type of floating point number to produce.

template<typename IntType = int>
class uniform_int_distribution
#include <uniform_int_distribution.h>

A uniform_int_distribution random number distribution produces signed or unsigned integer uniform random numbers from a given range.

The following code snippet demonstrates examples of using a uniform_int_distribution with a random number engine to produce random integers drawn from a given range:

#include <thrust/random/linear_congruential_engine.h>
#include <thrust/random/uniform_int_distribution.h>

int main(void)
{
  // create a minstd_rand object to act as our source of randomness
  thrust::minstd_rand rng;

  // create a uniform_int_distribution to produce ints from [-7,13]
  thrust::uniform_int_distribution<int> dist(-7,13);

  // write a random number from the range [-7,13] to standard output
  std::cout << dist(rng) << std::endl;

  // write the range of the distribution, just in case we forgot
  std::cout << dist.min() << std::endl;

  // -7 is printed

  std::cout << dist.max() << std::endl;

  // 13 is printed

  // write the parameters of the distribution (which happen to be the bounds) to standard output
  std::cout << dist.a() << std::endl;

  // -7 is printed

  std::cout << dist.b() << std::endl;

  // 13 is printed

  return 0;
}
Template Parameters:

IntType – The type of integer to produce.

template<typename RealType = double>
class uniform_real_distribution
#include <uniform_real_distribution.h>

A uniform_real_distribution random number distribution produces floating point uniform random numbers from a half-open interval.

The following code snippet demonstrates examples of using a uniform_real_distribution with a random number engine to produce random integers drawn from a given range:

#include <thrust/random/linear_congruential_engine.h>
#include <thrust/random/uniform_real_distribution.h>

int main(void)
{
  // create a minstd_rand object to act as our source of randomness
  thrust::minstd_rand rng;

  // create a uniform_real_distribution to produce floats from [-7,13)
  thrust::uniform_real_distribution<float> dist(-7,13);

  // write a random number from the range [-7,13) to standard output
  std::cout << dist(rng) << std::endl;

  // write the range of the distribution, just in case we forgot
  std::cout << dist.min() << std::endl;

  // -7.0 is printed

  std::cout << dist.max() << std::endl;

  // 13.0 is printed

  // write the parameters of the distribution (which happen to be the bounds) to standard output
  std::cout << dist.a() << std::endl;

  // -7.0 is printed

  std::cout << dist.b() << std::endl;

  // 13.0 is printed

  return 0;
}
Template Parameters:

RealType – The type of floating point number to produce.

System#

group system
namespace system#

thrust::system is the namespace which contains specific Thrust backend systems. It also contains functionality for reporting error conditions originating from the operating system or other low-level application program interfaces such as the HIP runtime. They are provided in a separate namespace for import convenience but are also aliased in the top-level thrust namespace for easy access.

namespace __THRUST_DEVICE_SYSTEM_NAMESPACE#
namespace __THRUST_HOST_SYSTEM_NAMESPACE#
namespace system#
class error_category#
#include <error_code.h>

The class error_category serves as a base class for types used to identify the source and encoding of a particular category of error code. Classes may be derived from error_category to support categories of errors in addition to those defined in the C++ International Standard.

class error_code#
#include <error_code.h>

The class error_code describes an object used to hold error code values, such as those originating from the operating system or other low-level application program interfaces.

class error_condition#
#include <error_code.h>

The class error_condition describes an object used to hold values identifying error conditions.

Note

error_condition values are portable abstractions, while error_code values are implementation specific.

template<typename T>
struct is_error_code_enum : public thrust::detail::false_type#
#include <error_code.h>

A metafunction returning whether or not the parameter is an error_code enum.

template<typename T>
struct is_error_condition_enum : public thrust::detail::false_type#
#include <error_code.h>

A metafunction returning whether or not the parameter is an error_condition enum.

template<> errc_t > : public thrust::detail::true_type
#include <error_code.h>

Specialization of is_error_condition_enum for errc::errc_t

class system_error : public std::runtime_error#
#include <system_error.h>

The class system_error describes an exception object used to report error conditions that have an associated error_code. Such error conditions typically originate from the operating system or other low-level application program interfaces.

Thrust uses system_error to report the error codes returned from device backends such as the HIP runtime.

The following code listing demonstrates how to catch a system_error to recover from an error.

#include <thrust/device_vector.h>
#include <thrust/system.h>
#include <thrust/sort.h>

void terminate_gracefully(void)
{
  // application-specific termination code here
  ...
}

int main(void)
{
  try
  {
    thrust::device_vector<float> vec;
    thrust::sort(vec.begin(), vec.end());
  }
  catch(thrust::system_error e)
  {
    std::cerr << "Error inside sort: " << e.what() << std::endl;
    terminate_gracefully();
  }

  return 0;
}

Note

If an error represents an out-of-memory condition, implementations are encouraged to throw an exception object of type std::bad_alloc rather than system_error.

namespace errc#

Enums

enum errc_t#

An enum containing common error codes.

Values:

enumerator address_family_not_supported#
enumerator address_in_use#
enumerator address_not_available#
enumerator already_connected#
enumerator argument_list_too_long#
enumerator argument_out_of_domain#
enumerator bad_address#
enumerator bad_file_descriptor#
enumerator bad_message#
enumerator broken_pipe#
enumerator connection_aborted#
enumerator connection_already_in_progress#
enumerator connection_refused#
enumerator connection_reset#
enumerator cross_device_link#
enumerator destination_address_required#
enumerator device_or_resource_busy#
enumerator directory_not_empty#
enumerator executable_format_error#
enumerator file_exists#
enumerator file_too_large#
enumerator filename_too_long#
enumerator function_not_supported#
enumerator host_unreachable#
enumerator identifier_removed#
enumerator illegal_byte_sequence#
enumerator inappropriate_io_control_operation#
enumerator interrupted#
enumerator invalid_argument#
enumerator invalid_seek#
enumerator io_error#
enumerator is_a_directory#
enumerator message_size#
enumerator network_down#
enumerator network_reset#
enumerator network_unreachable#
enumerator no_buffer_space#
enumerator no_child_process#
enumerator no_link#
enumerator no_lock_available#
enumerator no_message_available#
enumerator no_message#
enumerator no_protocol_option#
enumerator no_space_on_device#
enumerator no_stream_resources#
enumerator no_such_device_or_address#
enumerator no_such_device#
enumerator no_such_file_or_directory#
enumerator no_such_process#
enumerator not_a_directory#
enumerator not_a_socket#
enumerator not_a_stream#
enumerator not_connected#
enumerator not_enough_memory#
enumerator not_supported#
enumerator operation_canceled#
enumerator operation_in_progress#
enumerator operation_not_permitted#
enumerator operation_not_supported#
enumerator operation_would_block#
enumerator owner_dead#
enumerator permission_denied#
enumerator protocol_error#
enumerator protocol_not_supported#
enumerator read_only_file_system#
enumerator resource_deadlock_would_occur#
enumerator resource_unavailable_try_again#
enumerator result_out_of_range#
enumerator state_not_recoverable#
enumerator stream_timeout#
enumerator text_file_busy#
enumerator timed_out#
enumerator too_many_files_open_in_system#
enumerator too_many_files_open#
enumerator too_many_links#
enumerator value_too_large#
enumerator wrong_protocol_type#

Functions

inline const error_category &generic_category(void)#

Note

The object’s default_error_condition and equivalent virtual functions shall behave as specified for the class error_category. The object’s name virtual function shall return a pointer to the string "generic".

Returns:

A reference to an object of a type derived from class error_category.

inline const error_category &system_category(void)#

If the argument ev corresponds to a POSIX errno value posv, the function shall return error_condition(ev,generic_category()). Otherwise, the function shall return error_condition(ev,system_category()). What constitutes correspondence for any given operating system is unspecified.

Note

The object’s equivalent virtual functions shall behave as specified for class error_category. The object’s name virtual function shall return a pointer to the string "system". The object’s default_error_condition virtual function shall behave as follows:

Returns:

A reference to an object of a type derived from class error_category.

inline error_code make_error_code(errc::errc_t e)#
Returns:

error_code(static_cast<int>(e), generic_category())

inline bool operator<(const error_code &lhs, const error_code &rhs)#
Returns:

lhs.category() < rhs.category() || lhs.category() == rhs.category() && lhs.value() < rhs.value().

template<typename charT, typename traits>
std::basic_ostream<charT, traits> &operator<<(std::basic_ostream<charT, traits> &os, const error_code &ec)#

Effects: os << ec.category().name() << ':' << ec.value().

inline error_condition make_error_condition(errc::errc_t e)#
Returns:

error_condition(static_cast<int>(e), generic_category()).

inline bool operator<(const error_condition &lhs, const error_condition &rhs)#
Returns:

lhs.category() < rhs.category() || lhs.category() == rhs.category() && lhs.value() < rhs.value().

inline bool operator==(const error_code &lhs, const error_code &rhs)#
Returns:

lhs.category() == rhs.category() && lhs.value() == rhs.value().

inline bool operator==(const error_code &lhs, const error_condition &rhs)#
Returns:

lhs.category().equivalent(lhs.value(), rhs) || rhs.category().equivalent(lhs,rhs.value()).

inline bool operator==(const error_condition &lhs, const error_code &rhs)#
Returns:

rhs.category().equivalent(lhs.value(), lhs) || lhs.category().equivalent(rhs, lhs.value()).

inline bool operator==(const error_condition &lhs, const error_condition &rhs)#
Returns:

lhs.category() == rhs.category() && lhs.value() == rhs.value()

inline bool operator!=(const error_code &lhs, const error_code &rhs)#
Returns:

!(lhs == rhs)

inline bool operator!=(const error_code &lhs, const error_condition &rhs)#
Returns:

!(lhs == rhs)

inline bool operator!=(const error_condition &lhs, const error_code &rhs)#
Returns:

!(lhs == rhs)

inline bool operator!=(const error_condition &lhs, const error_condition &rhs)#
Returns:

!(lhs == rhs)

template<typename T>
struct is_error_code_enum : public thrust::detail::false_type
#include <error_code.h>

A metafunction returning whether or not the parameter is an error_code enum.

template<typename T>
struct is_error_condition_enum : public thrust::detail::false_type
#include <error_code.h>

A metafunction returning whether or not the parameter is an error_condition enum.

template<>
struct is_error_condition_enum<errc::errc_t> : public thrust::detail::true_type#
#include <error_code.h>

Specialization of is_error_condition_enum for errc::errc_t

class error_category
#include <error_code.h>

The class error_category serves as a base class for types used to identify the source and encoding of a particular category of error code. Classes may be derived from error_category to support categories of errors in addition to those defined in the C++ International Standard.

class error_code
#include <error_code.h>

The class error_code describes an object used to hold error code values, such as those originating from the operating system or other low-level application program interfaces.

class error_condition
#include <error_code.h>

The class error_condition describes an object used to hold values identifying error conditions.

Note

error_condition values are portable abstractions, while error_code values are implementation specific.

class system_error : public std::runtime_error
#include <system_error.h>

The class system_error describes an exception object used to report error conditions that have an associated error_code. Such error conditions typically originate from the operating system or other low-level application program interfaces.

Thrust uses system_error to report the error codes returned from device backends such as the HIP runtime.

The following code listing demonstrates how to catch a system_error to recover from an error.

#include <thrust/device_vector.h>
#include <thrust/system.h>
#include <thrust/sort.h>

void terminate_gracefully(void)
{
  // application-specific termination code here
  ...
}

int main(void)
{
  try
  {
    thrust::device_vector<float> vec;
    thrust::sort(vec.begin(), vec.end());
  }
  catch(thrust::system_error e)
  {
    std::cerr << "Error inside sort: " << e.what() << std::endl;
    terminate_gracefully();
  }

  return 0;
}

Note

If an error represents an out-of-memory condition, implementations are encouraged to throw an exception object of type std::bad_alloc rather than system_error.

namespace errc

Enums

enum errc_t

An enum containing common error codes.

Values:

enumerator address_family_not_supported
enumerator address_in_use
enumerator address_not_available
enumerator already_connected
enumerator argument_list_too_long
enumerator argument_out_of_domain
enumerator bad_address
enumerator bad_file_descriptor
enumerator bad_message
enumerator broken_pipe
enumerator connection_aborted
enumerator connection_already_in_progress
enumerator connection_refused
enumerator connection_reset
enumerator cross_device_link
enumerator destination_address_required
enumerator device_or_resource_busy
enumerator directory_not_empty
enumerator executable_format_error
enumerator file_exists
enumerator file_too_large
enumerator filename_too_long
enumerator function_not_supported
enumerator host_unreachable
enumerator identifier_removed
enumerator illegal_byte_sequence
enumerator inappropriate_io_control_operation
enumerator interrupted
enumerator invalid_argument
enumerator invalid_seek
enumerator io_error
enumerator is_a_directory
enumerator message_size
enumerator network_down
enumerator network_reset
enumerator network_unreachable
enumerator no_buffer_space
enumerator no_child_process
enumerator no_link
enumerator no_lock_available
enumerator no_message_available
enumerator no_message
enumerator no_protocol_option
enumerator no_space_on_device
enumerator no_stream_resources
enumerator no_such_device_or_address
enumerator no_such_device
enumerator no_such_file_or_directory
enumerator no_such_process
enumerator not_a_directory
enumerator not_a_socket
enumerator not_a_stream
enumerator not_connected
enumerator not_enough_memory
enumerator not_supported
enumerator operation_canceled
enumerator operation_in_progress
enumerator operation_not_permitted
enumerator operation_not_supported
enumerator operation_would_block
enumerator owner_dead
enumerator permission_denied
enumerator protocol_error
enumerator protocol_not_supported
enumerator read_only_file_system
enumerator resource_deadlock_would_occur
enumerator resource_unavailable_try_again
enumerator result_out_of_range
enumerator state_not_recoverable
enumerator stream_timeout
enumerator text_file_busy
enumerator timed_out
enumerator too_many_files_open_in_system
enumerator too_many_files_open
enumerator too_many_links
enumerator too_many_symbolic_link_levels
enumerator value_too_large
enumerator wrong_protocol_type