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 adevice_ptr
allocated withdevice_new
.See also
See also
- Parameters:
ptr – The
device_ptr
to delete, assumed to have been allocated withdevice_new
.n – The number of objects to destroy at
ptr
. Defaults to1
similar todevice_new
.
-
inline void device_free(thrust::device_ptr<void> ptr)#
device_free
deallocates memory allocated by the functiondevice_malloc
.The following code snippet demonstrates how to use
device_free
to deallocate memory allocated bydevice_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
See also
- Parameters:
ptr – A
device_ptr
pointing to memory to be deallocated.
-
inline thrust::device_ptr<void> device_malloc(const std::size_t n)#
This version of
device_malloc
allocates sequential device storage for bytes.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<void> void_ptr = thrust::device_malloc(N); // manipulate integers ... // deallocate with device_free thrust::device_free(void_ptr);
See also
See also
- Parameters:
n – The number of bytes to allocate sequentially in device memory.
- Returns:
A
device_ptr
to the newly allocated memory.
-
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 memory with device_malloc const int N = 100; thrust::device_ptr<int> int_array = thrust::device_malloc<int>(N); // manipulate memory ... // deallocate with device_free thrust::device_free(int_array);
See also
See also
- Parameters:
n – The number of objects of type T 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 placementnew
operator for types resident in device memory.device_new
callsT
’s null constructor on a array of objects in device memory. No memory is allocated by this function.See also
- Parameters:
p – A
device_ptr
to a region of device memory into which to construct one or manyT
s.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
callsT
’s copy constructor on a array of objects in device memory. No memory is allocated by this function.See also
See also
- Parameters:
p – A
device_ptr
to a region of device memory into which to construct one or manyT
s.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 holdn
new objects of typeT
.- 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 toptr
.
- template<typename T> __host__ __device__ device_ptr< T > device_pointer_cast (device_ptr< T > const &dptr)
Create a
device_ptr
from anotherdevice_ptr
.- Template Parameters:
T – Any type.
- Parameters:
dptr – A
device_ptr
to aT
.
- 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 firstdevice_reference
of interest.y
The seconddevice_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
See also
- 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 fromthrust::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
See also
- 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 typeT
; a null pointer otherwise. The pointer must be deallocated withthrust::free
.- Pre:
DerivedPolicy
must be publically derived fromthrust::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 ton
objects of typeT
. If not enough storage is available to accomodaten
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 withreturn_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
See also
- 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 thatp.first
is a pointer to the allocated storage andp.second
is the number of contiguous objects of typeT
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 usingreturn_temporary_buffer
.- Pre:
DerivedPolicy
must be publically derived fromthrust::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 bythrust::malloc
.The following code snippet demonstrates how to use
free
to deallocate a range of memory previously allocated withthrust::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
. Ifptr
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 tothrust::malloc(system, n)
orthrust::malloc<T>(system, n)
for some typeT
.
- 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 byget_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 byget_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
See also
- Parameters:
system – The Thrust system with which the storage is associated.
p – A pointer previously returned by
thrust::get_temporary_buffer
. Ifptr
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 bythrust::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.See also
- 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
Note
There are two versions of
raw_reference_cast
. One forconst
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
Note
There are two versions of
raw_reference_cast
. One forconst
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 adevice_ptr
. Similarly, taking the address of adevice_reference
yields adevice_ptr
.device_reference
may often be used from host code in place of operations defined on its associatedvalue_type
. For example, whendevice_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 aniostream:
#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 bydevice_vector's
bracket operator. A more natural way to print the value of adevice_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 itsdevice_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 likeprintf
which have varargs parameters. Because varargs parameters must be Plain Old Data, adevice_reference
to a POD type requires a cast when passed toprintf:
#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
See also
-
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 viaget
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
See also
See also
See also
See also
See also
Note
device_ptr
is not a smart pointer; it is the programmer’s responsibility to deallocate memory pointed to bydevice_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 adevice_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 thedevice_malloc
function for allocation.device_malloc_allocator
is deprecated in favor ofthrust::mr
memory resource-based allocators.See also
See also
See also
-
template<typename T>
class device_new_allocator# - #include <device_new_allocator.h>
device_new_allocator
is a device memory allocator that employs thedevice_new
function for allocation.See also
See also
-
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 befinal
(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 befinal
(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 withuniversal_allocator
or by explicitly calling its constructor with a raw pointer.The raw pointer encapsulated by a
universal_ptr
may be obtained by either itsget
method or theraw_pointer_cast
free function.See also
host_ptr For the documentation of the complete interface which is shared by
universal_ptr
.See also
Note
universal_ptr
is not a smart pointer; it is the programmer’s responsibility to deallocate memory pointed to byuniversal_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
, usingBookkeeper
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
anddeallocate
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 thatUpstream
allocates memory that is inaccessible to the code of the pool resource, which means that it cannot embed the necessary information in memory obtained fromUpstream
; 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 fromstd::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
anddeallocate
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.
-
template<typename T>
Iterators#
- group iterators
Functions
- template<typename InputIterator, typename Distance> __host__ __device__ void advance (InputIterator &i, Distance n)
advance(i, n)
increments the iteratori
by the distancen
. Ifn > 0
it is equivalent to executing++i
n
times, and ifn < 0
it is equivalent to executingi
n
times. Ifn == 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 betweenfirst
andlast
, i.e. the number of times thatfirst
must be incremented until it is equal tolast
.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 fromfirst
orfirst
shall be reachable fromlast
; otherwise,last
shall be reachable fromfirst
.
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 aconstant_iterator
from values given for both value and index. The type ofconstant_iterator
may be inferred by the compiler from the types of its parameters.See also
- 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 toint
. In the default case, the value of this parameter is0
.
- Returns:
A new
constant_iterator
with constant value & index as given byx
&i
.
- template<typename V> inline __host__ __device__ constant_iterator< V > make_constant_iterator (V x)
This version of
make_constant_iterator
creates aconstant_iterator
using only a parameter for the desired constant value. The value of the returnedconstant_iterator's
index is set to0
.See also
- Parameters:
x – The value of the returned
constant_iterator's
constant value.- Returns:
A new
constant_iterator
with constant value equal tox
and index equal to0
.
- template<typename Incrementable> inline __host__ __device__ counting_iterator< Incrementable > make_counting_iterator (Incrementable x)
make_counting_iterator
creates acounting_iterator
using an initial value for itsIncrementable
counter.- Parameters:
x – The initial value of the new
counting_iterator's
counter.- Returns:
A new
counting_iterator
whose counter has been initialized tox
.
- inline __host__ __device__ discard_iterator make_discard_iterator (discard_iterator<>::difference_type i=discard_iterator<>::difference_type(0))
make_discard_iterator
creates adiscard_iterator
from an optional index parameter.See also
- Parameters:
i – The index of the returned
discard_iterator
within a range. In the default case, the value of this parameter is0
.- Returns:
A new
discard_iterator
with index as given byi
.
- template<typename ElementIterator, typename IndexIterator> __host__ __device__ permutation_iterator< ElementIterator, IndexIterator > make_permutation_iterator (ElementIterator e, IndexIterator i)
make_permutation_iterator
creates apermutation_iterator
from anElementIterator
pointing to a range of elements to “permute” and anIndexIterator
pointing to a range of indices defining an indexing scheme on the values.See also
- Parameters:
e – An
ElementIterator
pointing to a range of values.i – An
IndexIterator
pointing to an indexing scheme to use one
.
- Returns:
A new
permutation_iterator
which permutes the rangee
byi
.
- template<typename BidirectionalIterator> __host__ __device__ reverse_iterator< BidirectionalIterator > make_reverse_iterator (BidirectionalIterator x)
make_reverse_iterator
creates areverse_iterator
from aBidirectionalIterator
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 rangex
.
- 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 atransform_input_output_iterator
from anIterator
aInputFunction
and aOutputFunction
See also
- Parameters:
io – An
Iterator
pointing to where the input toInputFunction
will be read from and the result ofOutputFunction
will be written toinput_function – An
InputFunction
to be executed on values read from the iteratoroutput_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 atransform_iterator
from anIterator
andAdaptableUnaryFunction
.See also
- Parameters:
it – The
Iterator
pointing to the input range of the newly createdtransform_iterator
.fun – The
AdaptableUnaryFunction
used to transform the range pointed to byit
in the newly createdtransform_iterator
.
- Returns:
A new
transform_iterator
which transforms the range atit
byfun
.
- 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 atransform_output_iterator
from anOutputIterator
andUnaryFunction
.See also
- Parameters:
out – The
OutputIterator
pointing to the output range of the newly createdtransform_output_iterator
fun – The
UnaryFunction
transform the object before assigning it toout
by the newly createdtransform_output_iterator
- template<typename... Iterators> inline __host__ __device__ zip_iterator< thrust::tuple< Iterators... > > make_zip_iterator (thrust::tuple< Iterators... > t)
make_zip_iterator
creates azip_iterator
from atuple
of iterators.See also
- Parameters:
t – The
tuple
of iterators to copy.- Returns:
A newly created
zip_iterator
which zips the iterators encapsulated int
.
- template<typename... Iterators> inline __host__ __device__ zip_iterator< thrust::tuple< Iterators... > > make_zip_iterator (Iterators... its)
make_zip_iterator
creates azip_iterator
from iterators.See also
- 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. Usingconstant_iterator
saves both memory capacity and bandwidth.The following code snippet demonstrates how to create a
constant_iterator
whosevalue_type
isint
and whose value is10
.#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 thethrust::transform
function to increment all elements of a sequence by the same value. We will create a temporaryconstant_iterator
with the functionmake_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; }
See also
-
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. Usingcounting_iterator
saves memory capacity and bandwidth.The following code snippet demonstrates how to create a
counting_iterator
whosevalue_type
isint
and which sequentially increments by1
.#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 thethrust::copy_if
function to compute the indices of the non-zero elements of adevice_vector
. In this example, we use themake_counting_iterator
function to avoid specifying the type of thecounting_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; }
See also
-
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; }
See also
-
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 fromiterator_adaptor
. While composition of these existing Thrust iterators is often sufficient for expressing the desired functionality, it is occasionally more straightforward to derive fromiterator_adaptor
directly.To see how to use
iterator_adaptor
to create a novel iterator type, let’s examine how to use it to definerepeat_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 specifiesthrust::use_default
in its place,iterator_adaptor
will use a default type inferred fromBase
.iterator_adaptor's
functionality is derived from and generally equivalent toboost::iterator_adaptor
. The exception is Thrust’s addition of the template parameterSystem
, 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 makeiterator_adaptor
difficult to use correctly. Unless you require the full expressivity ofiterator_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 toboost::iterator_facade
. The exception is Thrust’s addition of the template parameterSystem
, 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 theoperator->
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 fromthrust::iterator_adaptor
orthrust::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 appliedthe 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 rangeV
. The distance between begin and end of the reindexing iterators is allowed to be smaller compared to the size of the rangeV
, in which case thepermutation_iterator
only provides a “permutation” of a subrange ofV
. The indices neither need to be unique. In this same context, it must be noted that the past-the-endpermutation_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 adevice_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}
See also
-
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-endreverse_iterator
of a given range points to the element preceding the first element of the input range. By the same token, the firstreverse_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 adevice_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}
See also
-
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 thetransform_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; }
See also
-
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. Usingtransform_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 ofsqrtf
applied to the contents of adevice_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 thethrust::reduce
function to compute the sum of squares of a sequence. We will create temporarytransform_iterators
with themake_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
andsquare
) inherits fromthrust::unary_function
. Inheriting fromthrust::unary_function
ensures that a functor is a validAdaptableUnaryFunction
and provides all the necessarytypedef
declarations. Thetransform_iterator
can also be applied to aUnaryFunction
that does not inherit fromthrust::unary_function
using an optional template argument. The following example illustrates how to use the third template argument to specify theresult_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 }
See also
-
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. Usingtransform_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 appliessqrtf
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; }
See also
-
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 oftuples
whose elements are themselves taken from atuple
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 themake_zip_iterator
function with themake_tuple
function to avoid explicitly specifying the type of thezip_iterator
. This example shows how to usezip_iterator
to copy multiple ranges with a single call tothrust::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
See also
See also
See also
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.See also
https://en.cppreference.com/w/cpp/iterator/iterator_tags iterator_traits, input_device_iterator_tag, output_device_iterator_tag, forward_device_iterator_tag, bidirectional_device_iterator_tag, random_access_device_iterator_tag, output_host_iterator_tag, forward_host_iterator_tag, bidirectional_host_iterator_tag, random_access_host_iterator_tag
-
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.See also
https://en.cppreference.com/w/cpp/iterator/iterator_tags iterator_traits, input_device_iterator_tag, output_device_iterator_tag, forward_device_iterator_tag, bidirectional_device_iterator_tag, random_access_device_iterator_tag, input_host_iterator_tag, forward_host_iterator_tag, bidirectional_host_iterator_tag, random_access_host_iterator_tag
-
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.See also
https://en.cppreference.com/w/cpp/iterator/iterator_tags iterator_traits, input_device_iterator_tag, output_device_iterator_tag, forward_device_iterator_tag, bidirectional_device_iterator_tag, random_access_device_iterator_tag, input_host_iterator_tag, output_host_iterator_tag, bidirectional_host_iterator_tag, random_access_host_iterator_tag
-
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.See also
https://en.cppreference.com/w/cpp/iterator/iterator_tags iterator_traits, input_device_iterator_tag, output_device_iterator_tag, forward_device_iterator_tag, bidirectional_device_iterator_tag, random_access_device_iterator_tag, input_host_iterator_tag, output_host_iterator_tag, forward_host_iterator_tag, random_access_host_iterator_tag
-
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.See also
https://en.cppreference.com/w/cpp/iterator/iterator_tags iterator_traits, input_device_iterator_tag, output_device_iterator_tag, forward_device_iterator_tag, bidirectional_device_iterator_tag, random_access_device_iterator_tag, input_host_iterator_tag, output_host_iterator_tag, forward_host_iterator_tag, bidirectional_host_iterator_tag
-
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.See also
https://en.cppreference.com/w/cpp/iterator/iterator_tags iterator_traits, output_device_iterator_tag, forward_device_iterator_tag, bidirectional_device_iterator_tag, random_access_device_iterator_tag, input_host_iterator_tag, output_host_iterator_tag, forward_host_iterator_tag, bidirectional_host_iterator_tag, random_access_host_iterator_tag
-
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.See also
https://en.cppreference.com/w/cpp/iterator/iterator_tags iterator_traits, input_device_iterator_tag, forward_device_iterator_tag, bidirectional_device_iterator_tag, random_access_device_iterator_tag, input_host_iterator_tag, output_host_iterator_tag, forward_host_iterator_tag, bidirectional_host_iterator_tag, random_access_host_iterator_tag
-
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.See also
https://en.cppreference.com/w/cpp/iterator/iterator_tags iterator_traits, input_device_iterator_tag, output_device_iterator_tag, bidirectional_device_iterator_tag, random_access_device_iterator_tag, input_host_iterator_tag, output_host_iterator_tag, forward_host_iterator_tag, bidirectional_host_iterator_tag, random_access_host_iterator_tag
-
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.See also
https://en.cppreference.com/w/cpp/iterator/iterator_tags iterator_traits, input_device_iterator_tag, output_device_iterator_tag, forward_device_iterator_tag, random_access_device_iterator_tag, input_host_iterator_tag, output_host_iterator_tag, forward_host_iterator_tag, bidirectional_host_iterator_tag, random_access_host_iterator_tag
-
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.See also
https://en.cppreference.com/w/cpp/iterator/iterator_tags iterator_traits, input_device_iterator_tag, output_device_iterator_tag, forward_device_iterator_tag, bidirectional_device_iterator_tag, input_host_iterator_tag, output_host_iterator_tag, forward_host_iterator_tag, bidirectional_host_iterator_tag, random_access_host_iterator_tag
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 iteratori
in the range[first, last)
such that*i == value
orlast
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
See also
- 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 typeT
.T – is a model of EqualityComparable.
- Returns:
The first iterator
i
such that*i == value
orlast
.
-
template<typename InputIterator, typename T>
InputIterator find(InputIterator first, InputIterator last, const T &value)# find
returns the first iteratori
in the range[first, last)
such that*i == value
orlast
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
See also
- Parameters:
first – Beginning of the sequence to search.
last – End of the sequence to search.
value – The value to find.
- Template Parameters:
InputIterator – is a model of Input Iterator and
InputIterator's
value_type
is equality comparable to typeT
.T – is a model of EqualityComparable.
- Returns:
The first iterator
i
such that*i == value
orlast
.
- 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 iteratori
in the range[first, last)
such thatpred(*i)
istrue
orlast
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
See also
See also
- 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 thatpred(*i)
istrue
, orlast
.
-
template<typename InputIterator, typename Predicate>
InputIterator find_if(InputIterator first, InputIterator last, Predicate pred)# find_if
returns the first iteratori
in the range[first, last)
such thatpred(*i)
istrue
orlast
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
See also
See also
- 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:
InputIterator – is a model of Input Iterator.
Predicate – is a model of Predicate.
- Returns:
The first iterator
i
such thatpred(*i)
istrue
, orlast
.
- 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 iteratori
in the range[first, last)
such thatpred(*i)
isfalse
orlast
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
See also
See also
- 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 thatpred(*i)
isfalse
, orlast
.
-
template<typename InputIterator, typename Predicate>
InputIterator find_if_not(InputIterator first, InputIterator last, Predicate pred)# find_if_not
returns the first iteratori
in the range[first, last)
such thatpred(*i)
isfalse
orlast
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
See also
See also
- 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:
InputIterator – is a model of Input Iterator.
Predicate – is a model of Predicate.
- Returns:
The first iterator
i
such thatpred(*i)
isfalse
, orlast
.
- 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 ofmismatch
use different tests for whether elements differ.This version of
mismatch
finds the first iteratori
in[first1, last1)
such that*i == *(first2 + (i - first1))
isfalse
. The return value is apair
whose first element isi
and whose second element is*(first2 + (i - first1))
. If no such iteratori
exists, the return value is apair
whose first element islast1
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
See also
- 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 toInputIterator2'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 ofmismatch
use different tests for whether elements differ.This version of
mismatch
finds the first iteratori
in[first1, last1)
such that*i == *(first2 + (i - first1))
isfalse
. The return value is apair
whose first element isi
and whose second element is*(first2 + (i - first1))
. If no such iteratori
exists, the return value is apair
whose first element islast1
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
See also
- 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 toInputIterator2'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 ofmismatch
use different tests for whether elements differ.This version of
mismatch
finds the first iteratori
in[first1, last1)
such thatpred(*i, *(first2 + (i - first1))
isfalse
. The return value is apair
whose first element isi
and whose second element is*(first2 + (i - first1))
. If no such iteratori
exists, the return value is apair
whose first element islast1
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
See also
- 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:
DerivedPolicy – The name of the derived execution policy.
InputIterator1 – is a model of Input Iterator.
InputIterator2 – is a model of Input Iterator.
Predicate – is a model of Input Iterator.
- 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 ofmismatch
use different tests for whether elements differ.This version of
mismatch
finds the first iteratori
in[first1, last1)
such thatpred(*i, *(first2 + (i - first1))
isfalse
. The return value is apair
whose first element isi
and whose second element is*(first2 + (i - first1))
. If no such iteratori
exists, the return value is apair
whose first element islast1
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
See also
- 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:
InputIterator1 – is a model of Input Iterator.
InputIterator2 – is a model of Input Iterator.
Predicate – is a model of Input Iterator.
- 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 satisfypred
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
See also
Note
Though similar,
partition_point
is not redundant withfind_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 toPredicate's
argument_type
.Predicate – is a model of Predicate.
- Returns:
An iterator
mid
such thatall_of(first, mid, pred)
andnone_of(mid, last, pred)
are both true.- Pre:
The range
[first, last)
shall be partitioned bypred
.
-
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 satisfypred
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
See also
Note
Though similar,
partition_point
is not redundant withfind_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 toPredicate's
argument_type
.Predicate – is a model of Predicate.
- Returns:
An iterator
mid
such thatall_of(first, mid, pred)
andnone_of(mid, last, pred)
are both true.- Pre:
The range
[first, last)
shall be partitioned bypred
.
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 oflower_bound
usesoperator<
for comparison and returns the furthermost iteratori
in[first, last)
such that, for every iteratorj
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 thethrust::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
See also
See also
- 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 oflower_bound
usesoperator<
for comparison and returns the furthermost iteratori
in[first, last)
such that, for every iteratorj
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
See also
See also
- Parameters:
first – The beginning of the ordered sequence.
last – The end of the ordered sequence.
value – The value to be searched.
- Template Parameters:
ForwardIterator – is a model of Forward Iterator.
LessThanComparable – is a model of LessThanComparable.
- 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 oflower_bound
uses function objectcomp
for comparison and returns the furthermost iteratori
in[first, last)
such that, for every iteratorj
in[first, i)
,comp(*j, value)
istrue
.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 thethrust::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
See also
See also
- 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 thatcomp(*i, value)
istrue
.
-
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 oflower_bound
uses function objectcomp
for comparison and returns the furthermost iteratori
in[first, last)
such that, for every iteratorj
in[first, i)
,comp(*j, value)
istrue
.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
See also
See also
- 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:
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 thatcomp(*i, value)
istrue
.
- 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 ofupper_bound
usesoperator<
for comparison and returns the furthermost iteratori
in[first, last)
such that, for every iteratorj
in[first, i)
,value < *j
isfalse
.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 thethrust::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
See also
See also
- 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 thatvalue < *i
isfalse
.
-
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 ofupper_bound
usesoperator<
for comparison and returns the furthermost iteratori
in[first, last)
such that, for every iteratorj
in[first, i)
,value < *j
isfalse
.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
See also
See also
- Parameters:
first – The beginning of the ordered sequence.
last – The end of the ordered sequence.
value – The value to be searched.
- Template Parameters:
ForwardIterator – is a model of Forward Iterator.
LessThanComparable – is a model of LessThanComparable.
- Returns:
The furthermost iterator
i
, such thatvalue < *i
isfalse
.
- 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 ofupper_bound
uses function objectcomp
for comparison and returns the furthermost iteratori
in[first, last)
such that, for every iteratorj
in[first, i)
,comp(value, *j)
isfalse
.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 thethrust::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
See also
See also
- 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 thatcomp(value, *i)
isfalse
.
-
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 ofupper_bound
uses function objectcomp
for comparison and returns the furthermost iteratori
in[first, last)
such that, for every iteratorj
in[first, i)
,comp(value, *j)
isfalse
.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
See also
See also
- 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:
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 thatcomp(value, *i)
isfalse
.
- 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 returnstrue
if an element that is equivalent tovalue
is present in[first, last)
andfalse
if no such element exists. Specifically, this version returnstrue
if and only if there exists an iteratori
in[first, last)
such that*i < value
andvalue < *i
are bothfalse
.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 thethrust::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
See also
See also
- 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)
, otherwisefalse
.
-
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 returnstrue
if an element that is equivalent tovalue
is present in[first, last)
andfalse
if no such element exists. Specifically, this version returnstrue
if and only if there exists an iteratori
in[first, last)
such that*i < value
andvalue < *i
are bothfalse
.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
See also
See also
- Parameters:
first – The beginning of the ordered sequence.
last – The end of the ordered sequence.
value – The value to be searched.
- Template Parameters:
ForwardIterator – is a model of Forward Iterator.
LessThanComparable – is a model of LessThanComparable.
- Returns:
true
if an equivalent element exists in[first, last)
, otherwisefalse
.
- 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 returnstrue
if an element that is equivalent tovalue
is present in[first, last)
andfalse
if no such element exists. Specifically, this version returnstrue
if and only if there exists an iteratori
in[first, last)
such thatcomp(*i, value)
andcomp(value, *i)
are bothfalse
.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 thethrust::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
See also
See also
- 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)
, otherwisefalse
.
-
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 returnstrue
if an element that is equivalent tovalue
is present in[first, last)
andfalse
if no such element exists. Specifically, this version returnstrue
if and only if there exists an iteratori
in[first, last)
such thatcomp(*i, value)
andcomp(value, *i)
are bothfalse
.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
See also
See also
- 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:
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)
, otherwisefalse
.
- 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 byequal_range
is essentially a combination of the values returned bylower_bound
andupper_bound:
it returns apair
of iteratorsi
andj
such thati
is the first position where value could be inserted without violating the ordering andj
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 apair
of iterators[i, j)
, wherei
is the furthermost iterator in[first, last)
such that, for every iteratork
in[first, i)
,*k < value
.j
is the furthermost iterator in[first, last)
such that, for every iteratork
in[first, j)
,value < *k
isfalse
. For every iteratork
in[i, j)
, neithervalue < *k
nor*k < value
istrue
.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 thethrust::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
See also
See also
- 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 byequal_range
is essentially a combination of the values returned bylower_bound
andupper_bound:
it returns apair
of iteratorsi
andj
such thati
is the first position where value could be inserted without violating the ordering andj
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 apair
of iterators[i, j)
, wherei
is the furthermost iterator in[first, last)
such that, for every iteratork
in[first, i)
,*k < value
.j
is the furthermost iterator in[first, last)
such that, for every iteratork
in[first, j)
,value < *k
isfalse
. For every iteratork
in[i, j)
, neithervalue < *k
nor*k < value
istrue
.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
See also
See also
- Parameters:
first – The beginning of the ordered sequence.
last – The end of the ordered sequence.
value – The value to be searched.
- Template Parameters:
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<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 byequal_range
is essentially a combination of the values returned bylower_bound
andupper_bound:
it returns apair
of iteratorsi
andj
such thati
is the first position where value could be inserted without violating the ordering andj
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 apair
of iterators[i, j)
.i
is the furthermost iterator in[first, last)
such that, for every iteratork
in[first, i)
,comp(*k, value)
istrue
.j
is the furthermost iterator in[first, last)
such that, for every iteratork
in[first, last)
,comp(value, *k)
isfalse
. For every iteratork
in[i, j)
, neithercomp(value, *k)
norcomp(*k, value)
istrue
.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 thethrust::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
See also
See also
- 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 byequal_range
is essentially a combination of the values returned bylower_bound
andupper_bound:
it returns apair
of iteratorsi
andj
such thati
is the first position where value could be inserted without violating the ordering andj
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 apair
of iterators[i, j)
.i
is the furthermost iterator in[first, last)
such that, for every iteratork
in[first, i)
,comp(*k, value)
istrue
.j
is the furthermost iterator in[first, last)
such that, for every iteratork
in[first, last)
,comp(value, *k)
isfalse
. For every iteratork
in[i, j)
, neithercomp(value, *k)
norcomp(*k, value)
istrue
.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
See also
See also
- 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:
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.
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 iteratorv
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 thethrust::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
See also
See also
- 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 toOutputIterator'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 iteratorv
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
See also
See also
- 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:
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 toOutputIterator's
value_type
.
- 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 iteratorv
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 oflower_bound
uses function objectcomp
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
See also
See also
- 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 toForwardIterator's
value_type
.OutputIterator – is a model of Output Iterator. and
ForwardIterator's
difference_type is convertible toOutputIterator'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 iteratorv
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 oflower_bound
uses function objectcomp
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
See also
See also
- 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 toForwardIterator's
value_type
.OutputIterator – is a model of Output Iterator. and
ForwardIterator's
difference_type is convertible toOutputIterator'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 iteratorv
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 thethrust::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
See also
See also
- 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 toOutputIterator'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 iteratorv
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
See also
See also
- 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:
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 toOutputIterator's
value_type
.
- 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 iteratorv
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 ofupper_bound
uses function objectcomp
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 thethrust::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
See also
See also
- 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 toForwardIterator's
value_type
.OutputIterator – is a model of Output Iterator. and
ForwardIterator's
difference_type is convertible toOutputIterator'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 iteratorv
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 ofupper_bound
uses function objectcomp
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
See also
See also
- 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 toForwardIterator's
value_type
.OutputIterator – is a model of Output Iterator. and
ForwardIterator's
difference_type is convertible toOutputIterator'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 iteratorv
in[values_first, values_last)
it attempts to find the value*v
in an ordered range[first, last)
. It returnstrue
if an element that is equivalent tovalue
is present in[first, last)
andfalse
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 thethrust::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
See also
See also
- 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 iteratorv
in[values_first, values_last)
it attempts to find the value*v
in an ordered range[first, last)
. It returnstrue
if an element that is equivalent tovalue
is present in[first, last)
andfalse
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
See also
See also
- 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:
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<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 iteratorv
in[values_first, values_last)
it attempts to find the value*v
in an ordered range[first, last)
. It returnstrue
if an element that is equivalent tovalue
is present in[first, last)
andfalse
if no such element exists. This version ofbinary_search
uses function objectcomp
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 thethrust::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
See also
See also
- 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 LessThanComparable.OutputIterator – is a model of Output Iterator. and bool 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 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 iteratorv
in[values_first, values_last)
it attempts to find the value*v
in an ordered range[first, last)
. It returnstrue
if an element that is equivalent tovalue
is present in[first, last)
andfalse
if no such element exists. This version ofbinary_search
uses function objectcomp
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
See also
See also
- 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 LessThanComparable.OutputIterator – is a model of Output Iterator. and bool 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.
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 integern
from0
tolast
-first
,copy
performs the assignment *(result
+n
) = *(first
+n
). Unlikestd::copy
,copy
offers no guarantee on order of operation. As a result, callingcopy
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 thethrust::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 toOutputIterator's
value_type
.OutputIterator – must be a model of Output Iterator.
- Returns:
The end of the destination sequence.
- Pre:
result
may be equal tofirst
, butresult
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 integeri
from0
ton
,copy
performs the assignment *(result
+i
) = *(first
+i
). Unlikestd::copy_n
,copy_n
offers no guarantee on order of operation. As a result, callingcopy_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 thethrust::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 toOutputIterator'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 tofirst
, butresult
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 integern
from0
tolast
-first
,copy
performs the assignment *(result
+n
) = *(first
+n
). Unlikestd::copy
,copy
offers no guarantee on order of operation. As a result, callingcopy
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 toOutputIterator's
value_type
.OutputIterator – must be a model of Output Iterator.
- Returns:
The end of the destination sequence.
- Pre:
result
may be equal tofirst
, butresult
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 integeri
from0
ton
,copy
performs the assignment *(result
+i
) = *(first
+i
). Unlikestd::copy_n
,copy_n
offers no guarantee on order of operation. As a result, callingcopy_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 toOutputIterator'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 tofirst
, butresult
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 integern
such that0 <= n < (last1 - first1)
, it swaps*(first1 + n)
and*(first2 + n)
. The return value isfirst2 + (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 twothrust::device_vectors
using thethrust::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
- 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 toForwardIterator2's
value_type
.ForwardIterator2 – is a model of Forward Iterator, and
ForwardIterator2's
value_type
must be convertible toForwardIterator1's
value_type
.
- Returns:
An iterator pointing to one position past the last element of the second sequence to swap.
- Pre:
first1
may equalfirst2
, 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 integern
such that0 <= n < (last1 - first1)
, it swaps*(first1 + n)
and*(first2 + n)
. The return value isfirst2 + (last1 - first1)
.The following code snippet demonstrates how to use
swap_ranges
to swap the contents of twothrust::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
- 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 toForwardIterator2's
value_type
.ForwardIterator2 – is a model of Forward Iterator, and
ForwardIterator2's
value_type
must be convertible toForwardIterator1's
value_type
.
- Returns:
An iterator pointing to one position past the last element of the second sequence to swap.
- Pre:
first1
may equalfirst2
, 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 functionthrust::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, thenuninitialized_copy
creates a copy of[first, last)
in that range. That is, for each iteratori
in the input,uninitialized_copy
creates a copy of*i
in the location pointed to by the corresponding iterator in the output range byForwardIterator'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 thethrust::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
See also
See also
See also
- 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, andForwardIterator's
value_type
has a constructor that takes a single argument whose type isInputIterator's
value_type
.
- Returns:
An iterator pointing to the last element of the output range.
- Pre:
first
may equalresult
, 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 functionthrust::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, thenuninitialized_copy
creates a copy of[first, last)
in that range. That is, for each iteratori
in the input,uninitialized_copy
creates a copy of*i
in the location pointed to by the corresponding iterator in the output range byForwardIterator'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
See also
See also
See also
- 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, andForwardIterator's
value_type
has a constructor that takes a single argument whose type isInputIterator's
value_type
.
- Returns:
An iterator pointing to the last element of the output range.
- Pre:
first
may equalresult
, 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 functionthrust::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, thenuninitialized_copy_n
creates a copy of[first, first + n)
in that range. That is, for each iteratori
in the input,uninitialized_copy_n
creates a copy of*i
in the location pointed to by the corresponding iterator in the output range byInputIterator'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 thethrust::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
See also
See also
See also
See also
- 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, andForwardIterator's
value_type
has a constructor that takes a single argument whose type isInputIterator's
value_type
.
- Returns:
An iterator pointing to the last element of the output range.
- Pre:
first
may equalresult
, 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 functionthrust::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, thenuninitialized_copy_n
creates a copy of[first, first + n)
in that range. That is, for each iteratori
in the input,uninitialized_copy_n
creates a copy of*i
in the location pointed to by the corresponding iterator in the output range byInputIterator'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
See also
See also
See also
See also
- 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, andForwardIterator's
value_type
has a constructor that takes a single argument whose type isInputIterator's
value_type
.
- Returns:
An iterator pointing to the last element of the output range.
- Pre:
first
may equalresult
, 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 iteratori
in the range[map_first, map_last)
, the valueinput_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 thethrust::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 toRandomAccessIterator's
difference_type
.RandomAccessIterator – must be a model of Random Access Iterator and
RandomAccessIterator's
value_type
must be convertible toOutputIterator'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 iteratori
in the range[map_first, map_last)
, the valueinput_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 toRandomAccessIterator's
difference_type
.RandomAccessIterator – must be a model of Random Access Iterator and
RandomAccessIterator's
value_type
must be convertible toOutputIterator'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 iteratori
in the range[map_first, map_last)
, such that the value of*(stencil + (i - map_first))
istrue
, the valueinput_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 thethrust::device
execution policy:Remark
gather_if
is the inverse ofscatter_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 toRandomAccessIterator's
difference_type
.InputIterator2 – must be a model of Input Iterator and
InputIterator2's
value_type
must be convertible tobool
.RandomAccessIterator – must be a model of Random Access iterator and
RandomAccessIterator's
value_type
must be convertible toOutputIterator'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 iteratori
in the range[map_first, map_last)
, such that the value of*(stencil + (i - map_first))
istrue
, the valueinput_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 ofscatter_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 toRandomAccessIterator's
difference_type
.InputIterator2 – must be a model of Input Iterator and
InputIterator2's
value_type
must be convertible tobool
.RandomAccessIterator – must be a model of Random Access iterator and
RandomAccessIterator's
value_type
must be convertible toOutputIterator'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 iteratori
in the range[map_first, map_last)
such that the value ofpred(*(stencil + (i - map_first)))
istrue
, the valueinput_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 thethrust::device
execution policy for parallelization:Remark
gather_if
is the inverse ofscatter_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 toRandomAccessIterator's
difference_type
.InputIterator2 – must be a model of Input Iterator and
InputIterator2's
value_type
must be convertible toPredicate's
argument_type
.RandomAccessIterator – must be a model of Random Access iterator and
RandomAccessIterator's
value_type
must be convertible toOutputIterator'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 iteratori
in the range[map_first, map_last)
such that the value ofpred(*(stencil + (i - map_first)))
istrue
, the valueinput_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 ofscatter_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 toRandomAccessIterator's
difference_type
.InputIterator2 – must be a model of Input Iterator and
InputIterator2's
value_type
must be convertible toPredicate's
argument_type
.RandomAccessIterator – must be a model of Random Access iterator and
RandomAccessIterator's
value_type
must be convertible toOutputIterator'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 iteratori
in the range [first
,last
), the value*i
is assigned tooutput[*(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 thethrust::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 toRandomAccessIterator's
value_type
.InputIterator2 – must be a model of Input Iterator and
InputIterator2's
value_type
must be convertible toRandomAccessIterator'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 iteratorj
in the range[first,last)
for all iteratorsi
in the range[map,map + (last - first))
.- Pre:
The iterator
result + i
shall not refer to any element referenced by any iteratorj
in the range[map,map + (last - first))
for all iteratorsi
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 iteratori
in the range [first
,last
), the value*i
is assigned tooutput[*(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 toRandomAccessIterator's
value_type
.InputIterator2 – must be a model of Input Iterator and
InputIterator2's
value_type
must be convertible toRandomAccessIterator'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 iteratorj
in the range[first,last)
for all iteratorsi
in the range[map,map + (last - first))
.- Pre:
The iterator
result + i
shall not refer to any element referenced by any iteratorj
in the range[map,map + (last - first))
for all iteratorsi
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 iteratori
in the range[first, last)
such that*(stencil + (i - first))
is true, the value*i
is assigned tooutput[*(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 toRandomAccessIterator's
value_type
.InputIterator2 – must be a model of Input Iterator and
InputIterator2's
value_type
must be convertible toRandomAccessIterator's
difference_type
.InputIterator3 – must be a model of Input Iterator and
InputIterator3's
value_type
must be convertible tobool
.RandomAccessIterator – must be a model of Random Access iterator.
- Pre:
The iterator
result + i
shall not refer to any element referenced by any iteratorj
in the range[first,last)
for all iteratorsi
in the range[map,map + (last - first))
.- Pre:
The iterator
result + i
shall not refer to any element referenced by any iteratorj
in the range[map,map + (last - first))
for all iteratorsi
in the range[map,map + (last - first))
.- Pre:
The iterator
result + i
shall not refer to any element referenced by any iteratorj
in the range[stencil,stencil + (last - first))
for all iteratorsi
in the range[map,map + (last - first))
.- Pre:
The expression
result[*i]
shall be valid for all iteratorsi
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 iteratori
in the range[first, last)
such that*(stencil + (i - first))
is true, the value*i
is assigned tooutput[*(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 toRandomAccessIterator's
value_type
.InputIterator2 – must be a model of Input Iterator and
InputIterator2's
value_type
must be convertible toRandomAccessIterator's
difference_type
.InputIterator3 – must be a model of Input Iterator and
InputIterator3's
value_type
must be convertible tobool
.RandomAccessIterator – must be a model of Random Access iterator.
- Pre:
The iterator
result + i
shall not refer to any element referenced by any iteratorj
in the range[first,last)
for all iteratorsi
in the range[map,map + (last - first))
.- Pre:
The iterator
result + i
shall not refer to any element referenced by any iteratorj
in the range[map,map + (last - first))
for all iteratorsi
in the range[map,map + (last - first))
.- Pre:
The iterator
result + i
shall not refer to any element referenced by any iteratorj
in the range[stencil,stencil + (last - first))
for all iteratorsi
in the range[map,map + (last - first))
.- Pre:
The expression
result[*i]
shall be valid for all iteratorsi
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 iteratori
in the range[first, last)
such thatpred(*(stencil + (i - first)))
istrue
, the value*i
is assigned tooutput[*(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 toRandomAccessIterator's
value_type
.InputIterator2 – must be a model of Input Iterator and
InputIterator2's
value_type
must be convertible toRandomAccessIterator's
difference_type
.InputIterator3 – must be a model of Input Iterator and
InputIterator3's
value_type
must be convertible toPredicate'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 iteratorj
in the range[first,last)
for all iteratorsi
in the range[map,map + (last - first))
.- Pre:
The iterator
result + i
shall not refer to any element referenced by any iteratorj
in the range[map,map + (last - first))
for all iteratorsi
in the range[map,map + (last - first))
.- Pre:
The iterator
result + i
shall not refer to any element referenced by any iteratorj
in the range[stencil,stencil + (last - first))
for all iteratorsi
in the range[map,map + (last - first))
.- Pre:
The expression
result[*i]
shall be valid for all iteratorsi
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 iteratori
in the range[first, last)
such thatpred(*(stencil + (i - first)))
istrue
, the value*i
is assigned tooutput[*(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 toRandomAccessIterator's
value_type
.InputIterator2 – must be a model of Input Iterator and
InputIterator2's
value_type
must be convertible toRandomAccessIterator's
difference_type
.InputIterator3 – must be a model of Input Iterator and
InputIterator3's
value_type
must be convertible toPredicate'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 iteratorj
in the range[first,last)
for all iteratorsi
in the range[map,map + (last - first))
.- Pre:
The iterator
result + i
shall not refer to any element referenced by any iteratorj
in the range[map,map + (last - first))
for all iteratorsi
in the range[map,map + (last - first))
.- Pre:
The iterator
result + i
shall not refer to any element referenced by any iteratorj
in the range[stencil,stencil + (last - first))
for all iteratorsi
in the range[map,map + (last - first))
.- Pre:
The expression
result[*i]
shall be valid for all iteratorsi
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 ofreduce
uses0
as the initial value of the reduction.reduce
is similar to the C++ Standard Template Library’sstd::accumulate
. The primary difference between the two functions is thatstd::accumulate
guarantees the order of summation, whilereduce
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 thenthrust::reduce
should not be used. Instead, one could useinclusive_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 thethrust::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
andy
are objects ofInputIterator's
value_type
, thenx + y
is defined and is convertible toInputIterator's
value_type
. IfT
isInputIterator's
value_type
, thenT(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 ofreduce
uses0
as the initial value of the reduction.reduce
is similar to the C++ Standard Template Library’sstd::accumulate
. The primary difference between the two functions is thatstd::accumulate
guarantees the order of summation, whilereduce
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 thenthrust::reduce
should not be used. Instead, one could useinclusive_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
andy
are objects ofInputIterator's
value_type
, thenx + y
is defined and is convertible toInputIterator's
value_type
. IfT
isInputIterator's
value_type
, thenT(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 ofreduce
usesinit
as the initial value of the reduction.reduce
is similar to the C++ Standard Template Library’sstd::accumulate
. The primary difference between the two functions is thatstd::accumulate
guarantees the order of summation, whilereduce
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 thenthrust::reduce
should not be used. Instead, one could useinclusive_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 thethrust::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
andy
are objects ofInputIterator's
value_type
, thenx + y
is defined and is convertible toT
.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 ofreduce
usesinit
as the initial value of the reduction.reduce
is similar to the C++ Standard Template Library’sstd::accumulate
. The primary difference between the two functions is thatstd::accumulate
guarantees the order of summation, whilereduce
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 thenthrust::reduce
should not be used. Instead, one could useinclusive_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
andy
are objects ofInputIterator's
value_type
, thenx + y
is defined and is convertible toT
.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 ofreduce
usesinit
as the initial value of the reduction andbinary_op
as the binary function used for summation.reduce
is similar to the C++ Standard Template Library’sstd::accumulate
. The primary difference between the two functions is thatstd::accumulate
guarantees the order of summation, whilereduce
requires associativity ofbinary_op
to parallelize the reduction.Note that
reduce
also assumes that the binary reduction operator (in this casebinary_op
) is commutative. If the reduction operator is not commutative thenthrust::reduce
should not be used. Instead, one could useinclusive_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 thethrust::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
- 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 toT
.T – is a model of Assignable, and is convertible to
BinaryFunction's
first_argument_type
andsecond_argument_type
.BinaryFunction – is a model of Binary Function, and
BinaryFunction's
result_type
is convertible toOutputType
.
- 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 ofreduce
usesinit
as the initial value of the reduction andbinary_op
as the binary function used for summation.reduce
is similar to the C++ Standard Template Library’sstd::accumulate
. The primary difference between the two functions is thatstd::accumulate
guarantees the order of summation, whilereduce
requires associativity ofbinary_op
to parallelize the reduction.Note that
reduce
also assumes that the binary reduction operator (in this casebinary_op
) is commutative. If the reduction operator is not commutative thenthrust::reduce
should not be used. Instead, one could useinclusive_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
- 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 toT
.T – is a model of Assignable, and is convertible to
BinaryFunction's
first_argument_type
andsecond_argument_type
.BinaryFunction – is a model of Binary Function, and
BinaryFunction's
result_type
is convertible toOutputType
.
- 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 ofreduce
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 thekeys_output
. The corresponding values in the range are reduced using theplus
and the result copied tovalues_output
.This version of
reduce_by_key
uses the function objectequal_to
to test for equality andplus
to reduce values with equal keys.Results from this function may vary from run to run depending on the inputs provided.
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 thethrust::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
See also
See also
See also
- 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 toOutputIterator1's
value_type
.OutputIterator2 – is a model of Output Iterator and and
InputIterator2's
value_type
is convertible toOutputIterator2'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 ofreduce
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 thekeys_output
. The corresponding values in the range are reduced using theplus
and the result copied tovalues_output
.This version of
reduce_by_key
uses the function objectequal_to
to test for equality andplus
to reduce values with equal keys.Results from this function may vary from run to run depending on the inputs provided.
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
See also
See also
See also
- 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 toOutputIterator1's
value_type
.OutputIterator2 – is a model of Output Iterator and and
InputIterator2's
value_type
is convertible toOutputIterator2'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 ofreduce
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 thekeys_output
. The corresponding values in the range are reduced using theplus
and the result copied tovalues_output
.This version of
reduce_by_key
uses the function objectbinary_pred
to test for equality andplus
to reduce values with equal keys.Results from this function may vary from run to run depending on the inputs provided.
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 thethrust::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
See also
See also
See also
- 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 toOutputIterator1's
value_type
.OutputIterator2 – is a model of Output Iterator and and
InputIterator2's
value_type
is convertible toOutputIterator2'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 ofreduce
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 thekeys_output
. The corresponding values in the range are reduced using theplus
and the result copied tovalues_output
.This version of
reduce_by_key
uses the function objectbinary_pred
to test for equality andplus
to reduce values with equal keys.Results from this function may vary from run to run depending on the inputs provided.
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
See also
See also
See also
- 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 toOutputIterator1's
value_type
.OutputIterator2 – is a model of Output Iterator and and
InputIterator2's
value_type
is convertible toOutputIterator2'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 ofreduce
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 thekeys_output
. The corresponding values in the range are reduced using theBinaryFunction
binary_op
and the result copied tovalues_output
. Specifically, if consecutive key iteratorsi
and(i + 1) are such that
binary_pred(*i, *(i+1))
istrue
, then the corresponding values are reduced to a single value withbinary_op
.This version of
reduce_by_key
uses the function objectbinary_pred
to test for equality andbinary_op
to reduce values with equal keys.Results from this function may vary from run to run depending on the inputs provided.
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 thethrust::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
See also
See also
See also
- 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 toOutputIterator1's
value_type
.OutputIterator2 – is a model of Output Iterator and and
InputIterator2's
value_type
is convertible toOutputIterator2's
value_type
.BinaryPredicate – is a model of Binary Predicate.
BinaryFunction – is a model of Binary Function and
BinaryFunction's
result_type
is convertible toOutputIterator2'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 ofreduce
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 thekeys_output
. The corresponding values in the range are reduced using theBinaryFunction
binary_op
and the result copied tovalues_output
. Specifically, if consecutive key iteratorsi
and(i + 1) are such that
binary_pred(*i, *(i+1))
istrue
, then the corresponding values are reduced to a single value withbinary_op
.This version of
reduce_by_key
uses the function objectbinary_pred
to test for equality andbinary_op
to reduce values with equal keys.Results from this function may vary from run to run depending on the inputs provided.
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
See also
See also
See also
- 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 toOutputIterator1's
value_type
.OutputIterator2 – is a model of Output Iterator and and
InputIterator2's
value_type
is convertible toOutputIterator2's
value_type
.BinaryPredicate – is a model of Binary Predicate.
BinaryFunction – is a model of Binary Function and
BinaryFunction's
result_type
is convertible toOutputIterator2'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 tovalue
. More precisely,count
returns the number of iteratorsi
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 thethrust::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 tovalue
. More precisely,count
returns the number of iteratorsi
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 istrue
. More precisely,count_if
returns the number of iteratorsi
in[first, last)
such thatpred(*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 thethrust::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 toPredicate's
argument_type
.Predicate – must be a model of Predicate.
- Returns:
The number of elements where
pred
istrue
.
-
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 istrue
. More precisely,count_if
returns the number of iteratorsi
in[first, last)
such thatpred(*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 toPredicate's
argument_type
.Predicate – must be a model of Predicate.
- Returns:
The number of elements where
pred
istrue
.
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
returnstrue
if the two ranges[first1, last1)
and[first2, first2 + (last1 - first1))
are identical when compared element-by-element, and otherwise returnsfalse
.This version of
equal
returnstrue
if and only if for every iteratori
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 thethrust::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, andInputIterator1's
value_type
can be compared for equality withInputIterator2's
value_type
.InputIterator2 – is a model of Input Iterator, and
InputIterator2's
value_type
is a model of Equality Comparable, andInputIterator2's
value_type
can be compared for equality withInputIterator1'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
returnstrue
if the two ranges[first1, last1)
and[first2, first2 + (last1 - first1))
are identical when compared element-by-element, and otherwise returnsfalse
.This version of
equal
returnstrue
if and only if for every iteratori
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, andInputIterator1's
value_type
can be compared for equality withInputIterator2's
value_type
.InputIterator2 – is a model of Input Iterator, and
InputIterator2's
value_type
is a model of Equality Comparable, andInputIterator2's
value_type
can be compared for equality withInputIterator1'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
returnstrue
if the two ranges[first1, last1)
and[first2, first2 + (last1 - first1))
are identical when compared element-by-element, and otherwise returnsfalse
.This version of
equal
returnstrue
if and only if for every iteratori
in[first1, last1)
,binary_pred(*i, *(first2 + (i - first1)))
istrue
.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 thethrust::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 toBinaryPredicate's
first_argument_type
.InputIterator2 – is a model of Input Iterator, and
InputIterator2's
value_type
is convertible toBinaryPredicate'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
returnstrue
if the two ranges[first1, last1)
and[first2, first2 + (last1 - first1))
are identical when compared element-by-element, and otherwise returnsfalse
.This version of
equal
returnstrue
if and only if for every iteratori
in[first1, last1)
,binary_pred(*i, *(first2 + (i - first1)))
istrue
.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 toBinaryPredicate's
first_argument_type
.InputIterator2 – is a model of Input Iterator, and
InputIterator2's
value_type
is convertible toBinaryPredicate'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 iteratori
in[first, last)
such that no other iterator in[first, last)
points to a value smaller than*i
. The return value islast
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 usingoperator<
. Specifically, this version ofmin_element
returns the first iteratori
in[first, last)
such that, for every iteratorj
in[first, last)
,*j < *i
isfalse
.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 iteratori
in[first, last)
such that no other iterator in[first, last)
points to a value smaller than*i
. The return value islast
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 usingoperator<
. Specifically, this version ofmin_element
returns the first iteratori
in[first, last)
such that, for every iteratorj
in[first, last)
,*j < *i
isfalse
.#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 iteratori
in[first, last)
such that no other iterator in[first, last)
points to a value smaller than*i
. The return value islast
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 objectcomp
. Specifically, this version ofmin_element
returns the first iteratori
in[first, last)
such that, for every iteratorj
in[first, last)
,comp(*j, *i)
isfalse
.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 thethrust::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 bothcomp's
first_argument_type
andsecond_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 iteratori
in[first, last)
such that no other iterator in[first, last)
points to a value smaller than*i
. The return value islast
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 objectcomp
. Specifically, this version ofmin_element
returns the first iteratori
in[first, last)
such that, for every iteratorj
in[first, last)
,comp(*j, *i)
isfalse
.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 bothcomp's
first_argument_type
andsecond_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 iteratori
in[first, last)
such that no other iterator in[first, last)
points to a value larger than*i
. The return value islast
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 usingoperator<
. Specifically, this version ofmax_element
returns the first iteratori
in[first, last)
such that, for every iteratorj
in[first, last)
,*i < *j
isfalse
.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:
A – Thrust backend system.
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 ForwardIterator>
ForwardIterator max_element(ForwardIterator first, ForwardIterator last)# max_element
finds the largest element in the range[first, last)
. It returns the first iteratori
in[first, last)
such that no other iterator in[first, last)
points to a value larger than*i
. The return value islast
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 usingoperator<
. Specifically, this version ofmax_element
returns the first iteratori
in[first, last)
such that, for every iteratorj
in[first, last)
,*i < *j
isfalse
.#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 iteratori
in[first, last)
such that no other iterator in[first, last)
points to a value larger than*i
. The return value islast
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 objectcomp
. Specifically, this version ofmax_element
returns the first iteratori
in[first, last)
such that, for every iteratorj
in[first, last)
,comp(*i, *j)
isfalse
.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 thethrust::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 bothcomp's
first_argument_type
andsecond_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 iteratori
in[first, last)
such that no other iterator in[first, last)
points to a value larger than*i
. The return value islast
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 objectcomp
. Specifically, this version ofmax_element
returns the first iteratori
in[first, last)
such that, for every iteratorj
in[first, last)
,comp(*i, *j)
isfalse
.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 bothcomp's
first_argument_type
andsecond_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)
whereimin
is the same iterator returned bymin_element
andimax
is the same iterator returned bymax_element
. This function is potentially more efficient than separate calls tomin_element
andmax_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
See also
- 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)
whereimin
is the same iterator returned bymin_element
andimax
is the same iterator returned bymax_element
. This function is potentially more efficient than separate calls tomin_element
andmax_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
See also
- 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)
whereimin
is the same iterator returned bymin_element
andimax
is the same iterator returned bymax_element
. This function is potentially more efficient than separate calls tomin_element
andmax_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 thethrust::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
See also
- 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 bothcomp's
first_argument_type
andsecond_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)
whereimin
is the same iterator returned bymin_element
andimax
is the same iterator returned bymax_element
. This function is potentially more efficient than separate calls tomin_element
andmax_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
See also
- 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 bothcomp's
first_argument_type
andsecond_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 suminit + (*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 thethrust::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 typeOutputType
, andy
is an object ofInputIterator1's
value_type
, andz
is an object ofInputIterator2's
value_type
, thenx + y * z
is defined and is convertible toOutputType
.
- Returns:
The inner product of sequences
[first1, last1)
and[first2, last2)
plusinit
.
-
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 suminit + (*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 typeOutputType
, andy
is an object ofInputIterator1's
value_type
, andz
is an object ofInputIterator2's
value_type
, thenx + y * z
is defined and is convertible toOutputType
.
- Returns:
The inner product of sequences
[first1, last1)
and[first2, last2)
plusinit
.
- 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 ofoperator+
andoperator*
.Specifically, this version of
inner_product
computes the sumbinary_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 toBinaryFunction2's
first_argument_type
.InputIterator2 – is a model of Input Iterator. and
InputIterator2's
value_type
is convertible toBinaryFunction2's
second_argument_type
.OutputType – is a model of Assignable, and
OutputType
is convertible toBinaryFunction1's
first_argument_type
.BinaryFunction1 – is a model of Binary Function, and
BinaryFunction1's
return_type
is convertible toOutputType
.BinaryFunction2 – is a model of Binary Function, and
BinaryFunction2's
return_type
is convertible toBinaryFunction1'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 ofoperator+
andoperator*
.Specifically, this version of
inner_product
computes the sumbinary_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 toBinaryFunction2's
first_argument_type
.InputIterator2 – is a model of Input Iterator. and
InputIterator2's
value_type
is convertible toBinaryFunction2's
second_argument_type
.OutputType – is a model of Assignable, and
OutputType
is convertible toBinaryFunction1's
first_argument_type
.BinaryFunction1 – is a model of Binary Function, and
BinaryFunction1's
return_type
is convertible toOutputType
.BinaryFunction2 – is a model of Binary Function, and
BinaryFunction2's
return_type
is convertible toBinaryFunction1'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 thetransform
andreduce
operations.transform_reduce
is equivalent to performing a transformation defined byunary_op
into a temporary sequence and then performingreduce
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 tounary_op
. Specifically,unary_op
is applied to each element of the sequence and then the result is reduced to a single value withbinary_op
using the initial valueinit
. Note that the transformationunary_op
is not applied to the initial valueinit
. The order of reduction is not specified, sobinary_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 thethrust::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
See also
- 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 toUnaryFunction's
argument_type
.UnaryFunction – is a model of Unary Function, and
UnaryFunction's
result_type
is convertible toOutputType
.OutputType – is a model of Assignable, and is convertible to
BinaryFunction's
first_argument_type
andsecond_argument_type
.BinaryFunction – is a model of Binary Function, and
BinaryFunction's
result_type
is convertible toOutputType
.
- 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 thetransform
andreduce
operations.transform_reduce
is equivalent to performing a transformation defined byunary_op
into a temporary sequence and then performingreduce
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 tounary_op
. Specifically,unary_op
is applied to each element of the sequence and then the result is reduced to a single value withbinary_op
using the initial valueinit
. Note that the transformationunary_op
is not applied to the initial valueinit
. The order of reduction is not specified, sobinary_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
See also
- 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 toUnaryFunction's
argument_type
.UnaryFunction – is a model of Unary Function, and
UnaryFunction's
result_type
is convertible toOutputType
.OutputType – is a model of Assignable, and is convertible to
BinaryFunction's
first_argument_type
andsecond_argument_type
.BinaryFunction – is a model of Binary Function, and
BinaryFunction's
result_type
is convertible toOutputType
.
- 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
returnstrue
ifpred(*i)
istrue
for every iteratori
in the range[first, last)
andfalse
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
See also
See also
- 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
returnstrue
ifpred(*i)
istrue
for every iteratori
in the range[first, last)
andfalse
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
See also
See also
- Parameters:
first – The beginning of the sequence.
last – The end of the sequence.
pred – A predicate used to test range elements.
- Template Parameters:
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 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
returnstrue
ifpred(*i)
istrue
for any iteratori
in the range[first, last)
andfalse
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
See also
See also
- 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
returnstrue
ifpred(*i)
istrue
for any iteratori
in the range[first, last)
andfalse
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
See also
See also
- Parameters:
first – The beginning of the sequence.
last – The end of the sequence.
pred – A predicate used to test range elements.
- Template Parameters:
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 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
returnstrue
if there is no iteratori
in the range[first, last)
such thatpred(*i)
istrue
, andfalse
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
See also
See also
- 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
returnstrue
if there is no iteratori
in the range[first, last)
such thatpred(*i)
istrue
, andfalse
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
See also
See also
- Parameters:
first – The beginning of the sequence.
last – The end of the sequence.
pred – A predicate used to test range elements.
- Template Parameters:
InputIterator – is a model of Input Iterator,
Predicate – must be a model of Predicate.
- 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
returnstrue
if the given range is partitioned with respect to a predicate, andfalse
otherwise.Specifically,
is_partitioned
returnstrue
if[first, last)
is empty of if[first, last)
is partitioned bypred
, i.e. if all elements that satisfypred
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
- 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 toPredicate's
argument_type
.Predicate – is a model of Predicate.
- Returns:
true
if the range[first, last)
is partitioned with respect topred
, or if[first, last)
is empty.false
, otherwise.
-
template<typename InputIterator, typename Predicate>
bool is_partitioned(InputIterator first, InputIterator last, Predicate pred)# is_partitioned
returnstrue
if the given range is partitioned with respect to a predicate, andfalse
otherwise.Specifically,
is_partitioned
returnstrue
if[first, last)
is empty of if[first, last)
is partitioned bypred
, i.e. if all elements that satisfypred
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
- 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 toPredicate's
argument_type
.Predicate – is a model of Predicate.
- Returns:
true
if the range[first, last)
is partitioned with respect topred
, 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
returnstrue
if the range[first, last)
is sorted in ascending order, andfalse
otherwise.Specifically, this version of
is_sorted
returnsfalse
if for some iteratori
in the range[first, last - 1)
the expression*(i + 1) < *i
istrue
.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 adevice_vector
are stored in ascending order using thethrust::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
See also
See also
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 ofForwardIterator'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
returnstrue
if the range[first, last)
is sorted in ascending order, andfalse
otherwise.Specifically, this version of
is_sorted
returnsfalse
if for some iteratori
in the range[first, last - 1)
the expression*(i + 1) < *i
istrue
.The following code demonstrates how to use
is_sorted
to test whether the contents of adevice_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
See also
See also
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 ofForwardIterator'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
returnstrue
if the range[first, last)
is sorted in ascending order accoring to a user-defined comparison operation, andfalse
otherwise.Specifically, this version of
is_sorted
returnsfalse
if for some iteratori
in the range[first, last - 1)
the expressioncomp(*(i + 1), *i)
istrue
.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 adevice_vector
are stored in descending order using thethrust::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
See also
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 bothStrictWeakOrdering's
first_argument_type
andsecond_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
returnstrue
if the range[first, last)
is sorted in ascending order accoring to a user-defined comparison operation, andfalse
otherwise.Specifically, this version of
is_sorted
returnsfalse
if for some iteratori
in the range[first, last - 1)
the expressioncomp(*(i + 1), *i)
istrue
.The following code snippet demonstrates how to use
is_sorted
to test whether the contents of adevice_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
See also
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 bothStrictWeakOrdering's
first_argument_type
andsecond_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 iteratori
in[first,last]
for which the range[first,last)
is sorted usingoperator<
. Ifdistance(first,last) < 2
,is_sorted_until
simply returnslast
.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 thethrust::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
See also
See also
See also
See also
- 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 iteratori
in[first,last]
for which the range[first,last)
is sorted usingoperator<
. Ifdistance(first,last) < 2
,is_sorted_until
simply returnslast
.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
See also
See also
See also
See also
- 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 iteratori
in[first,last]
for which the range[first,last)
is sorted using the function objectcomp
. Ifdistance(first,last) < 2
,is_sorted_until
simply returnslast
.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 thethrust::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
See also
See also
See also
See also
- 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 toCompare'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 iteratori
in[first,last]
for which the range[first,last)
is sorted using the function objectcomp
. Ifdistance(first,last) < 2
,is_sorted_until
simply returnslast
.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
See also
See also
See also
See also
- 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:
ForwardIterator – is a model of Forward Iterator and
ForwardIterator's
value_type
is convertible toCompare's
argument_type
.Compare – is a model of Strict Weak Ordering.
- 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 isresult + (last1 - first1) + (last2 - first2)
.This version of
merge
compares elements usingoperator<
.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 thethrust::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
See also
See also
- 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
andInputIterator2
have the samevalue_type
,InputIterator1's
value_type
is a model of LessThan Comparable, the ordering onInputIterator1's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator,
InputIterator2
andInputIterator1
have the samevalue_type
,InputIterator2's
value_type
is a model of LessThan Comparable, the ordering onInputIterator2's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2's
value_type
is convertable to a type inOutputIterator's
set ofvalue_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 tooperator<
.- 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 isresult + (last1 - first1) + (last2 - first2)
.This version of
merge
compares elements usingoperator<
.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
See also
See also
- 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
andInputIterator2
have the samevalue_type
,InputIterator1's
value_type
is a model of LessThan Comparable, the ordering onInputIterator1's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator,
InputIterator2
andInputIterator1
have the samevalue_type
,InputIterator2's
value_type
is a model of LessThan Comparable, the ordering onInputIterator2's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2's
value_type
is convertable to a type inOutputIterator's
set ofvalue_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 tooperator<
.- 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 isresult + (last1 - first1) + (last2 - first2)
.This version of
merge
compares elements using a function objectcomp
.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 thethrust::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
See also
- 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 toStrictWeakCompare's
first_argument_type
. andInputIterator1's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator,
InputIterator2's
value_type
is convertable toStrictWeakCompare's
second_argument_type
. andInputIterator2's
value_type
is convertable to a type inOutputIterator's
set ofvalue_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 tocomp
.- 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 isresult + (last1 - first1) + (last2 - first2)
.This version of
merge
compares elements using a function objectcomp
.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
See also
- 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 toStrictWeakCompare's
first_argument_type
. andInputIterator1's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator,
InputIterator2's
value_type
is convertable toStrictWeakCompare's
second_argument_type
. andInputIterator2's
value_type
is convertable to a type inOutputIterator's
set ofvalue_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 tocomp
.- 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 thethrust::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
See also
See also
- 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
andInputIterator2
have the samevalue_type
,InputIterator1's
value_type
is a model of LessThan Comparable, the ordering onInputIterator1's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator,
InputIterator2
andInputIterator1
have the samevalue_type
,InputIterator2's
value_type
is a model of LessThan Comparable, the ordering onInputIterator2's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator3 – is a model of Input Iterator, and
InputIterator3's
value_type
is convertible to a type inOutputIterator2's
set ofvalue_types
.InputIterator4 – is a model of Input Iterator, and
InputIterator4's
value_type
is convertible to a type inOutputIterator2's
set ofvalue_types
.OutputIterator1 – is a model of Output Iterator.
OutputIterator2 – is a model of Output Iterator.
- Returns:
A
pair
p
such thatp.first
is the end of the output range of keys, and such thatp.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 tooperator<
.- 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
See also
See also
- 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
andInputIterator2
have the samevalue_type
,InputIterator1's
value_type
is a model of LessThan Comparable, the ordering onInputIterator1's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator,
InputIterator2
andInputIterator1
have the samevalue_type
,InputIterator2's
value_type
is a model of LessThan Comparable, the ordering onInputIterator2's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator3 – is a model of Input Iterator, and
InputIterator3's
value_type
is convertible to a type inOutputIterator2's
set ofvalue_types
.InputIterator4 – is a model of Input Iterator, and
InputIterator4's
value_type
is convertible to a type inOutputIterator2's
set ofvalue_types
.OutputIterator1 – is a model of Output Iterator.
OutputIterator2 – is a model of Output Iterator.
- Returns:
A
pair
p
such thatp.first
is the end of the output range of keys, and such thatp.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 tooperator<
.- 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 objectcomp
.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 thethrust::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
See also
See also
- 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 toStrictWeakCompare's
first_argument_type
. andInputIterator1's
value_type
is convertable to a type inOutputIterator1's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator,
InputIterator2's
value_type
is convertable toStrictWeakCompare's
second_argument_type
. andInputIterator2's
value_type
is convertable to a type inOutputIterator1's
set ofvalue_types
.InputIterator3 – is a model of Input Iterator, and
InputIterator3's
value_type
is convertible to a type inOutputIterator2's
set ofvalue_types
.InputIterator4 – is a model of Input Iterator, and
InputIterator4's
value_type
is convertible to a type inOutputIterator2's
set ofvalue_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 thatp.first
is the end of the output range of keys, and such thatp.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 tocomp
.- 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 objectcomp
.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
See also
See also
- 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 toStrictWeakCompare's
first_argument_type
. andInputIterator1's
value_type
is convertable to a type inOutputIterator1's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator,
InputIterator2's
value_type
is convertable toStrictWeakCompare's
second_argument_type
. andInputIterator2's
value_type
is convertable to a type inOutputIterator1's
set ofvalue_types
.InputIterator3 – is a model of Input Iterator, and
InputIterator3's
value_type
is convertible to a type inOutputIterator2's
set ofvalue_types
.InputIterator4 – is a model of Input Iterator, and
InputIterator4's
value_type
is convertible to a type inOutputIterator2's
set ofvalue_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 thatp.first
is the end of the output range of keys, and such thatp.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 tocomp
.- 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 everyi
such that0 <= 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 adevice_vector
of integers using thethrust::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
See also
- 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 everyi
such that0 <= i <= (last - first) / 2
, it exchanges*(first + i)
and*(last - (i + 1))
.The following code snippet demonstrates how to use
reverse
to reverse adevice_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
See also
- 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 fromreverse
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 everyi
such that0 <= 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 inputdevice_vector
of integers to an outputdevice_vector
using thethrust::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
See also
- 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 toOutputIterator'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 fromreverse
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 everyi
such that0 <= 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 inputdevice_vector
of integers to an outputdevice_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
See also
- 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 toOutputIterator'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 objectpred
, such that all of the elements that satisfypred
precede the elements that fail to satisfy it. The postcondition is that, for some iteratormiddle
in the range[first, last)
,pred(*i)
istrue
for every iteratori
in the range[first,middle)
andfalse
for every iteratori
in the range[middle, last)
. The return value ofpartition
ismiddle
.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 thethrust::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
See also
- 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 toPredicate's
argument_type
, andForwardIterator
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 objectpred
, such that all of the elements that satisfypred
precede the elements that fail to satisfy it. The postcondition is that, for some iteratormiddle
in the range[first, last)
,pred(*i)
istrue
for every iteratori
in the range[first,middle)
andfalse
for every iteratori
in the range[middle, last)
. The return value ofpartition
ismiddle
.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
See also
- 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 toPredicate's
argument_type
, andForwardIterator
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 objectpred
applied to a stencil range[stencil, stencil + (last - first))
, such that all of the elements whose corresponding stencil element satisfiespred
precede all of the elements whose corresponding stencil element fails to satisfy it. The postcondition is that, for some iteratormiddle
in the range[first, last)
,pred(*stencil_i)
istrue
for every iteratorstencil_i
in the range[stencil,stencil + (middle - first))
andfalse
for every iteratorstencil_i
in the range[stencil + (middle - first), stencil + (last - first))
. The return value ofstable_partition
ismiddle
.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 thethrust::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
See also
- 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 toPredicate'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 objectpred
applied to a stencil range[stencil, stencil + (last - first))
, such that all of the elements whose corresponding stencil element satisfiespred
precede all of the elements whose corresponding stencil element fails to satisfy it. The postcondition is that, for some iteratormiddle
in the range[first, last)
,pred(*stencil_i)
istrue
for every iteratorstencil_i
in the range[stencil,stencil + (middle - first))
andfalse
for every iteratorstencil_i
in the range[stencil + (middle - first), stencil + (last - first))
. The return value ofstable_partition
ismiddle
.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
See also
- 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 toPredicate'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 frompartition
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 objectpred
. All of the elements that satisfypred
are copied to the range beginning atout_true
and all the elements that fail to satisfy it are copied to the range beginning atout_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 thethrust::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
See also
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 toPredicate's
argument_type
andInputIterator's
value_type
is convertible toOutputIterator1
andOutputIterator2'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 thatp.first
is the end of the output range beginning atout_true
andp.second
is the end of the output range beginning atout_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 frompartition
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 objectpred
. All of the elements that satisfypred
are copied to the range beginning atout_true
and all the elements that fail to satisfy it are copied to the range beginning atout_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
See also
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 toPredicate's
argument_type
andInputIterator's
value_type
is convertible toOutputIterator1
andOutputIterator2'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 thatp.first
is the end of the output range beginning atout_true
andp.second
is the end of the output range beginning atout_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 frompartition
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 objectpred
which is applied to a range of stencil elements. All of the elements whose corresponding stencil element satisfiespred
are copied to the range beginning atout_true
and all the elements whose stencil element fails to satisfy it are copied to the range beginning atout_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 thethrust::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
See also
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 toOutputIterator1
andOutputIterator2's
value_types
.InputIterator2 – is a model of Input Iterator, and
InputIterator2's
value_type
is convertible toPredicate'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 thatp.first
is the end of the output range beginning atout_true
andp.second
is the end of the output range beginning atout_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 frompartition
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 objectpred
which is applied to a range of stencil elements. All of the elements whose corresponding stencil element satisfiespred
are copied to the range beginning atout_true
and all the elements whose stencil element fails to satisfy it are copied to the range beginning atout_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
See also
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 toOutputIterator1
andOutputIterator2's
value_types
.InputIterator2 – is a model of Input Iterator, and
InputIterator2's
value_type
is convertible toPredicate'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 thatp.first
is the end of the output range beginning atout_true
andp.second
is the end of the output range beginning atout_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 likepartition
: it reorders the elements in the range[first, last)
based on the function objectpred
, such that all of the elements that satisfypred
precede all of the elements that fail to satisfy it. The postcondition is that, for some iteratormiddle
in the range[first, last)
,pred(*i)
istrue
for every iteratori
in the range[first,middle)
andfalse
for every iteratori
in the range[middle, last)
. The return value ofstable_partition
ismiddle
.stable_partition
differs frompartition
in thatstable_partition
is guaranteed to preserve relative order. That is, ifx
andy
are elements in[first, last)
, andstencil_x
andstencil_y
are the stencil elements in corresponding positions within[stencil, stencil + (last - first))
, andpred(stencil_x) == pred(stencil_y)
, and ifx
precedesy
, then it will still be true afterstable_partition
thatx
precedesy
.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 thethrust::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
See also
- 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 toPredicate's
argument_type
, andForwardIterator
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 likepartition
: it reorders the elements in the range[first, last)
based on the function objectpred
, such that all of the elements that satisfypred
precede all of the elements that fail to satisfy it. The postcondition is that, for some iteratormiddle
in the range[first, last)
,pred(*i)
istrue
for every iteratori
in the range[first,middle)
andfalse
for every iteratori
in the range[middle, last)
. The return value ofstable_partition
ismiddle
.stable_partition
differs frompartition
in thatstable_partition
is guaranteed to preserve relative order. That is, ifx
andy
are elements in[first, last)
, andstencil_x
andstencil_y
are the stencil elements in corresponding positions within[stencil, stencil + (last - first))
, andpred(stencil_x) == pred(stencil_y)
, and ifx
precedesy
, then it will still be true afterstable_partition
thatx
precedesy
.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
See also
- 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 toPredicate's
argument_type
, andForwardIterator
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 likepartition:
it reorders the elements in the range[first, last)
based on the function objectpred
applied to a stencil range[stencil, stencil + (last - first))
, such that all of the elements whose corresponding stencil element satisfiespred
precede all of the elements whose corresponding stencil element fails to satisfy it. The postcondition is that, for some iteratormiddle
in the range[first, last)
,pred(*stencil_i)
istrue
for every iteratorstencil_i
in the range[stencil,stencil + (middle - first))
andfalse
for every iteratorstencil_i
in the range[stencil + (middle - first), stencil + (last - first))
. The return value ofstable_partition
ismiddle
.stable_partition
differs frompartition
in thatstable_partition
is guaranteed to preserve relative order. That is, ifx
andy
are elements in[first, last)
, such thatpred(x) == pred(y)
, and ifx
precedesy
, then it will still be true afterstable_partition
thatx
precedesy
.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 thethrust::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
See also
- 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 toPredicate'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 likepartition:
it reorders the elements in the range[first, last)
based on the function objectpred
applied to a stencil range[stencil, stencil + (last - first))
, such that all of the elements whose corresponding stencil element satisfiespred
precede all of the elements whose corresponding stencil element fails to satisfy it. The postcondition is that, for some iteratormiddle
in the range[first, last)
,pred(*stencil_i)
istrue
for every iteratorstencil_i
in the range[stencil,stencil + (middle - first))
andfalse
for every iteratorstencil_i
in the range[stencil + (middle - first), stencil + (last - first))
. The return value ofstable_partition
ismiddle
.stable_partition
differs frompartition
in thatstable_partition
is guaranteed to preserve relative order. That is, ifx
andy
are elements in[first, last)
, such thatpred(x) == pred(y)
, and ifx
precedesy
, then it will still be true afterstable_partition
thatx
precedesy
.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
See also
- 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 toPredicate'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 fromstable_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 objectpred
. All of the elements that satisfypred
are copied to the range beginning atout_true
and all the elements that fail to satisfy it are copied to the range beginning atout_false
.stable_partition_copy
differs frompartition_copy
in thatstable_partition_copy
is guaranteed to preserve relative order. That is, ifx
andy
are elements in[first, last)
, such thatpred(x) == pred(y)
, and ifx
precedesy
, then it will still be true afterstable_partition_copy
thatx
precedesy
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 thethrust::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
See also
- 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 toPredicate's
argument_type
andInputIterator's
value_type
is convertible toOutputIterator1
andOutputIterator2'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 thatp.first
is the end of the output range beginning atout_true
andp.second
is the end of the output range beginning atout_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 fromstable_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 objectpred
. All of the elements that satisfypred
are copied to the range beginning atout_true
and all the elements that fail to satisfy it are copied to the range beginning atout_false
.stable_partition_copy
differs frompartition_copy
in thatstable_partition_copy
is guaranteed to preserve relative order. That is, ifx
andy
are elements in[first, last)
, such thatpred(x) == pred(y)
, and ifx
precedesy
, then it will still be true afterstable_partition_copy
thatx
precedesy
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
See also
- 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 toPredicate's
argument_type
andInputIterator's
value_type
is convertible toOutputIterator1
andOutputIterator2'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 thatp.first
is the end of the output range beginning atout_true
andp.second
is the end of the output range beginning atout_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 fromstable_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 objectpred
which is applied to a range of stencil elements. All of the elements whose corresponding stencil element satisfiespred
are copied to the range beginning atout_true
and all the elements whose stencil element fails to satisfy it are copied to the range beginning atout_false
.stable_partition_copy
differs frompartition_copy
in thatstable_partition_copy
is guaranteed to preserve relative order. That is, ifx
andy
are elements in[first, last)
, such thatpred(x) == pred(y)
, and ifx
precedesy
, then it will still be true afterstable_partition_copy
thatx
precedesy
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 thethrust::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
See also
- 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 toOutputIterator1
andOutputIterator2's
value_types
.InputIterator2 – is a model of Input Iterator, and
InputIterator2's
value_type
is convertible toPredicate'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 thatp.first
is the end of the output range beginning atout_true
andp.second
is the end of the output range beginning atout_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 fromstable_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 objectpred
which is applied to a range of stencil elements. All of the elements whose corresponding stencil element satisfiespred
are copied to the range beginning atout_true
and all the elements whose stencil element fails to satisfy it are copied to the range beginning atout_false
.stable_partition_copy
differs frompartition_copy
in thatstable_partition_copy
is guaranteed to preserve relative order. That is, ifx
andy
are elements in[first, last)
, such thatpred(x) == pred(y)
, and ifx
precedesy
, then it will still be true afterstable_partition_copy
thatx
precedesy
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
See also
- 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 toOutputIterator1
andOutputIterator2's
value_types
.InputIterator2 – is a model of Input Iterator, and
InputIterator2's
value_type
is convertible toPredicate'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 thatp.first
is the end of the output range beginning atout_true
andp.second
is the end of the output range beginning atout_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 atresult
, except that any element which causespred
to befalse
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 that0 <= n < last-first
,copy_if
performs the assignment*result = *(first+n)
andresult
is advanced one position ifpred(*(first+n))
. Otherwise, no assignment occurs andresult
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 thethrust::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
- 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 toPredicate's
argument_type
.OutputIterator – is a model of Output Iterator.
Predicate – is a model of Predicate.
- Returns:
result + n
, wheren
is equal to the number of timespred
evaluated totrue
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 atresult
, except that any element which causespred
tofalse
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 that0 <= n < last-first
,copy_if
performs the assignment*result = *(first+n)
andresult
is advanced one position ifpred(*(first+n))
. Otherwise, no assignment occurs andresult
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
- 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 toPredicate's
argument_type
.OutputIterator – is a model of Output Iterator.
Predicate – is a model of Predicate.
- Returns:
result + n
, wheren
is equal to the number of timespred
evaluated totrue
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 atresult
, except that any element whose corresponding stencil element causespred
to befalse
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 that0 <= n < last-first
,copy_if
performs the assignment*result = *(first+n)
andresult
is advanced one position ifpred(*(stencil+n))
. Otherwise, no assignment occurs andresult
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 thethrust::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
- 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 toPredicate's
argument_type
.OutputIterator – is a model of Output Iterator.
Predicate – is a model of Predicate.
- Returns:
result + n
, wheren
is equal to the number of timespred
evaluated totrue
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 atresult
, except that any element whose corresponding stencil element causespred
to befalse
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 that0 <= n < last-first
,copy_if
performs the assignment*result = *(first+n)
andresult
is advanced one position ifpred(*(stencil+n))
. Otherwise, no assignment occurs andresult
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
- 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 toPredicate's
argument_type
.OutputIterator – is a model of Output Iterator.
Predicate – is a model of Predicate.
- Returns:
result + n
, wheren
is equal to the number of timespred
evaluated totrue
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 tovalue
. That is,remove
returns an iteratornew_last
such that the range[first, new_last)
contains no elements equal tovalue
. 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 tovalue
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 thethrust::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
See also
See also
Note
The meaning of “removal” is somewhat subtle.
remove
does not destroy any iterators, and does not change the distance betweenfirst
andlast
. (There’s no way that it could do anything of the sort.) So, for example, ifV
is a device_vector,remove(V.begin(), V.end(), 0)
does not changeV.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 isS.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 ofForwardIterator's
value_type
.
- Returns:
A
ForwardIterator
pointing to the end of the resulting range of elements which are not equal tovalue
.
-
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 tovalue
. That is,remove
returns an iteratornew_last
such that the range[first, new_last)
contains no elements equal tovalue
. 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 tovalue
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
See also
See also
Note
The meaning of “removal” is somewhat subtle.
remove
does not destroy any iterators, and does not change the distance betweenfirst
andlast
. (There’s no way that it could do anything of the sort.) So, for example, ifV
is a device_vector,remove(V.begin(), V.end(), 0)
does not changeV.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 isS.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 ofForwardIterator's
value_type
.
- Returns:
A
ForwardIterator
pointing to the end of the resulting range of elements which are not equal tovalue
.
- 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 tovalue
from the range[first, last)
to a range beginning atresult
. 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 thethrust::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
See also
See also
- 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 inOutputIterator's
set ofvalue_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 ofInputIterator'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 tovalue
from the range[first, last)
to a range beginning atresult
. 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
See also
See also
- 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 inOutputIterator's
set ofvalue_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 ofInputIterator'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 elementx
such thatpred(x)
istrue
. That is,remove_if
returns an iteratornew_last
such that the range[first,new_last)
contains no elements for whichpred
istrue
. 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 thethrust::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
See also
See also
Note
The meaning of “removal” is somewhat subtle.
remove_if
does not destroy any iterators, and does not change the distance betweenfirst
andlast
. (There’s no way that it could do anything of the sort.) So, for example, ifV
is a device_vector,remove_if(V.begin(), V.end(), pred)
does not changeV.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 isS.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 whichpred
evaluates totrue
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, andForwardIterator's
value_type
is convertible toPredicate'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 totrue
.
-
template<typename ForwardIterator, typename Predicate>
ForwardIterator remove_if(ForwardIterator first, ForwardIterator last, Predicate pred)# remove_if
removes from the range[first, last)
every elementx
such thatpred(x)
istrue
. That is,remove_if
returns an iteratornew_last
such that the range[first,new_last)
contains no elements for whichpred
istrue
. 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
See also
See also
Note
The meaning of “removal” is somewhat subtle.
remove_if
does not destroy any iterators, and does not change the distance betweenfirst
andlast
. (There’s no way that it could do anything of the sort.) So, for example, ifV
is a device_vector,remove_if(V.begin(), V.end(), pred)
does not changeV.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 isS.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 whichpred
evaluates totrue
are removed from the sequence.
- Template Parameters:
ForwardIterator – is a model of Forward Iterator,
ForwardIterator
is mutable, andForwardIterator's
value_type
is convertible toPredicate'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 totrue
.
- 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 atresult
, except that elements for whichpred
istrue
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 thethrust::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
See also
See also
- 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 whichpred
evaluates tofalse
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 inOutputIterator's
set ofvalue_types
, andInputIterator's
value_type
is convertible toPredicate'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 atresult
, except that elements for whichpred
istrue
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
See also
See also
- 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 whichpred
evaluates tofalse
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 inOutputIterator's
set ofvalue_types
, andInputIterator's
value_type
is convertible toPredicate'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 elementx
such thatpred(x)
istrue
. That is,remove_if
returns an iteratornew_last
such that the range[first, new_last)
contains no elements for whichpred
of the corresponding stencil value istrue
. 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 thethrust::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
See also
See also
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 whichpred
evaluates totrue
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 toPredicate'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 totrue
.- 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 elementx
such thatpred(x)
istrue
. That is,remove_if
returns an iteratornew_last
such that the range[first, new_last)
contains no elements for whichpred
of the corresponding stencil value istrue
. 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
See also
See also
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 whichpred
evaluates totrue
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 toPredicate'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 totrue
.- 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 atresult
, except that elements for whichpred
of the corresponding stencil value istrue
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 thethrust::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
See also
See also
See also
- 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 whichpred
evaluates tofalse
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 inOutputIterator's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator, and
InputIterator2's
value_type
is convertible toPredicate'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 atresult
, except that elements for whichpred
of the corresponding stencil value istrue
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
See also
See also
See also
- 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 whichpred
evaluates tofalse
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 inOutputIterator's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator, and
InputIterator2's
value_type
is convertible toPredicate'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 iteratornew_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
usesoperator==
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 thethrust::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
- 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, andForwardIterator'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 iteratornew_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
usesoperator==
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
- 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, andForwardIterator'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 iteratornew_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 objectbinary_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 thethrust::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
- 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, andForwardIterator's
value_type
is convertible toBinaryPredicate's
first_argument_type
and toBinaryPredicate'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 iteratornew_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 objectbinary_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
- 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, andForwardIterator's
value_type
is convertible toBinaryPredicate's
first_argument_type
and toBinaryPredicate'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 withresult
, 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 iteratori
in the range, eitheri == f
or else*i == *(i-1)
. In the second, the test is an arbitraryBinaryPredicate
binary_pred:
the elements in[f, l)
are duplicates if, for every iteratori
in the range, eitheri == f
or elsebinary_pred(*i, *(i-1))
istrue
.This version of
unique_copy
usesoperator==
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 thethrust::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
- 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 toOutputIterator'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 withresult
, 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 iteratori
in the range, eitheri == f
or else*i == *(i-1)
. In the second, the test is an arbitraryBinaryPredicate
binary_pred:
the elements in[f, l)
are duplicates if, for every iteratori
in the range, eitheri == f
or elsebinary_pred(*i, *(i-1))
istrue
.This version of
unique_copy
usesoperator==
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
- 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 toOutputIterator'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 withresult
, 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 objectbinary_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 thethrust::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
- 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 toOutputIterator'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 withresult
, 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 objectbinary_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
- 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:
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 toOutputIterator'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 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 ofunique
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
usesoperator==
to test for equality andproject1st
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 thethrust::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
See also
See also
- 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, andForwardIterator'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 ofunique
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
usesoperator==
to test for equality andproject1st
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
See also
See also
- 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:
ForwardIterator1 – is a model of Forward Iterator, and
ForwardIterator1
is mutable, andForwardIterator'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 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 ofunique
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 objectbinary_pred
to test for equality andproject1st
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 thethrust::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
See also
See also
- 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, andForwardIterator'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 ofunique
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 objectbinary_pred
to test for equality andproject1st
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
See also
See also
- 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:
ForwardIterator1 – is a model of Forward Iterator, and
ForwardIterator1
is mutable, andForwardIterator'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 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 ofunique_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 withkeys_result
and the corresponding values from the range[values_first, values_first + (keys_last - keys_first))
are copied to a range beginning withvalues_result
.This version of
unique_by_key_copy
usesoperator==
to test for equality andproject1st
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 thethrust::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
See also
See also
- 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 toOutputIterator1's
value_type
.OutputIterator2 – is a model of Output Iterator and and
InputIterator2's
value_type
is convertible toOutputIterator2'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 ofunique_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 withkeys_result
and the corresponding values from the range[values_first, values_first + (keys_last - keys_first))
are copied to a range beginning withvalues_result
.This version of
unique_by_key_copy
usesoperator==
to test for equality andproject1st
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
See also
See also
- 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 toOutputIterator1's
value_type
.OutputIterator2 – is a model of Output Iterator and and
InputIterator2's
value_type
is convertible toOutputIterator2'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 ofunique_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 withkeys_result
and the corresponding values from the range[values_first, values_first + (keys_last - keys_first))
are copied to a range beginning withvalues_result
.This version of
unique_by_key_copy
uses the function objectbinary_pred
to test for equality andproject1st
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 thethrust::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
See also
See also
- 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 toOutputIterator1's
value_type
.OutputIterator2 – is a model of Output Iterator and and
InputIterator2's
value_type
is convertible toOutputIterator2'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 ofunique_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 withkeys_result
and the corresponding values from the range[values_first, values_first + (keys_last - keys_first))
are copied to a range beginning withvalues_result
.This version of
unique_by_key_copy
uses the function objectbinary_pred
to test for equality andproject1st
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
See also
See also
- 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 toOutputIterator1's
value_type
.OutputIterator2 – is a model of Output Iterator and and
InputIterator2's
value_type
is convertible toOutputIterator2'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 objectbinary_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 thethrust::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
See also
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 toBinaryPredicate's
first_argument_type
and toBinaryPredicate'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
usesoperator==
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 thethrust::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
See also
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 toBinaryPredicate's
first_argument_type
and toBinaryPredicate'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 objectbinary_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
See also
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 toBinaryPredicate's
first_argument_type
and toBinaryPredicate'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
usesoperator==
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
See also
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 toBinaryPredicate's
first_argument_type
and toBinaryPredicate'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 ofinclusive_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 tostd::partial_sum
in the STL. The primary difference between the two functions is thatstd::partial_sum
guarantees a serial summation order, whileinclusive_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 thethrust::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 toOutputIterator's
value_type
.OutputIterator – is a model of Output Iterator, and if
x
andy
are objects ofOutputIterator's
value_type
, thenx + y
is defined. IfT
isOutputIterator's
value_type
, thenT(0)
is defined.
- Returns:
The end of the output sequence.
- Pre:
first
may equalresult
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 ofinclusive_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 tostd::partial_sum
in the STL. The primary difference between the two functions is thatstd::partial_sum
guarantees a serial summation order, whileinclusive_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 toOutputIterator's
value_type
.OutputIterator – is a model of Output Iterator, and if
x
andy
are objects ofOutputIterator's
value_type
, thenx + y
is defined. IfT
isOutputIterator's
value_type
, thenT(0)
is defined.
- Returns:
The end of the output sequence.
- Pre:
first
may equalresult
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 tostd::partial_sum
in the STL. The primary difference between the two functions is thatstd::partial_sum
guarantees a serial summation order, whileinclusive_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 thethrust::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 toOutputIterator's
value_type
.OutputIterator – is a model of Output Iterator and
OutputIterator's
value_type
is convertible to bothAssociativeOperator's
first_argument_type
andsecond_argument_type
.AssociativeOperator – is a model of Binary Function and
AssociativeOperator's
result_type
is convertible toOutputIterator's
value_type
.
- Returns:
The end of the output sequence.
- Pre:
first
may equalresult
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 tostd::partial_sum
in the STL. The primary difference between the two functions is thatstd::partial_sum
guarantees a serial summation order, whileinclusive_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 toOutputIterator's
value_type
.OutputIterator – is a model of Output Iterator and
OutputIterator's
value_type
is convertible to bothAssociativeOperator's
first_argument_type
andsecond_argument_type
.AssociativeOperator – is a model of Binary Function and
AssociativeOperator's
result_type
is convertible toOutputIterator's
value_type
.
- Returns:
The end of the output sequence.
- Pre:
first
may equalresult
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 of0
and*first
is assigned to*(result + 1)
, and so on. This version ofexclusive_scan
assumes plus as the associative operator and0
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 thethrust::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 toOutputIterator's
value_type
.OutputIterator – is a model of Output Iterator, and if
x
andy
are objects ofOutputIterator's
value_type
, thenx + y
is defined. IfT
isOutputIterator's
value_type
, thenT(0)
is defined.
- Returns:
The end of the output sequence.
- Pre:
first
may equalresult
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 of0
and*first
is assigned to*(result + 1)
, and so on. This version ofexclusive_scan
assumes plus as the associative operator and0
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 toOutputIterator's
value_type
.OutputIterator – is a model of Output Iterator, and if
x
andy
are objects ofOutputIterator's
value_type
, thenx + y
is defined. IfT
isOutputIterator's
value_type
, thenT(0)
is defined.
- Returns:
The end of the output sequence.
- Pre:
first
may equalresult
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 ofinit
and*first
is assigned to*(result + 1)
, and so on. This version ofexclusive_scan
assumes plus as the associative operator but requires an initial valueinit
. 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 thethrust::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 toOutputIterator's
value_type
.OutputIterator – is a model of Output Iterator, and if
x
andy
are objects ofOutputIterator's
value_type
, thenx + y
is defined.T – is convertible to
OutputIterator's
value_type
.
- Returns:
The end of the output sequence.
- Pre:
first
may equalresult
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 ofinit
and*first
is assigned to*(result + 1)
, and so on. This version ofexclusive_scan
assumes plus as the associative operator but requires an initial valueinit
. 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 toOutputIterator's
value_type
.OutputIterator – is a model of Output Iterator, and if
x
andy
are objects ofOutputIterator's
value_type
, thenx + y
is defined.T – is convertible to
OutputIterator's
value_type
.
- Returns:
The end of the output sequence.
- Pre:
first
may equalresult
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 valuebinary_op(init, *first)
is assigned to*(result + 1)
, and so on. This version of the function requires both an associative operator and an initial valueinit
. 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 thethrust::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 toOutputIterator's
value_type
.OutputIterator – is a model of Output Iterator and
OutputIterator's
value_type
is convertible to bothAssociativeOperator's
first_argument_type
andsecond_argument_type
.T – is convertible to
OutputIterator's
value_type
.AssociativeOperator – is a model of Binary Function and
AssociativeOperator's
result_type
is convertible toOutputIterator's
value_type
.
- Returns:
The end of the output sequence.
- Pre:
first
may equalresult
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 valuebinary_op(init, *first)
is assigned to*(result + 1)
, and so on. This version of the function requires both an associative operator and an initial valueinit
. 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 toOutputIterator's
value_type
.OutputIterator – is a model of Output Iterator and
OutputIterator's
value_type
is convertible to bothAssociativeOperator's
first_argument_type
andsecond_argument_type
.T – is convertible to
OutputIterator's
value_type
.AssociativeOperator – is a model of Binary Function and
AssociativeOperator's
result_type
is convertible toOutputIterator's
value_type
.
- Returns:
The end of the output sequence.
- Pre:
first
may equalresult
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
assumesequal_to
as the binary predicate used to compare adjacent keys. Specifically, consecutive iteratorsi
andi+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
assumesplus
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 thethrust::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
See also
- 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 toOutputIterator's
value_type
.OutputIterator – is a model of Output Iterator, and if
x
andy
are objects ofOutputIterator's
value_type
, thenbinary_op(x,y)
is defined.
- Returns:
The end of the output sequence.
- Pre:
first1
may equalresult
but the range[first1, last1)
and the range[result, result + (last1 - first1))
shall not overlap otherwise.- Pre:
first2
may equalresult
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
assumesequal_to
as the binary predicate used to compare adjacent keys. Specifically, consecutive iteratorsi
andi+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
assumesplus
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
See also
- 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 toOutputIterator's
value_type
.OutputIterator – is a model of Output Iterator, and if
x
andy
are objects ofOutputIterator's
value_type
, thenbinary_op(x,y)
is defined.
- Returns:
The end of the output sequence.
- Pre:
first1
may equalresult
but the range[first1, last1)
and the range[result, result + (last1 - first1))
shall not overlap otherwise.- Pre:
first2
may equalresult
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 predicatepred
to compare adjacent keys. Specifically, consecutive iteratorsi
andi+1
in the range[first1, last1)
belong to the same segment ifbinary_pred(*i, *(i+1))
is true, and belong to different segments otherwise.This version of
inclusive_scan_by_key
assumesplus
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 thethrust::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
See also
- 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 toOutputIterator's
value_type
.OutputIterator – is a model of Output Iterator, and if
x
andy
are objects ofOutputIterator's
value_type
, thenbinary_op(x,y)
is defined.BinaryPredicate – is a model of Binary Predicate.
- Returns:
The end of the output sequence.
- Pre:
first1
may equalresult
but the range[first1, last1)
and the range[result, result + (last1 - first1))
shall not overlap otherwise.- Pre:
first2
may equalresult
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 predicatepred
to compare adjacent keys. Specifically, consecutive iteratorsi
andi+1
in the range[first1, last1)
belong to the same segment ifbinary_pred(*i, *(i+1))
is true, and belong to different segments otherwise.This version of
inclusive_scan_by_key
assumesplus
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
See also
- 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 toOutputIterator's
value_type
.OutputIterator – is a model of Output Iterator, and if
x
andy
are objects ofOutputIterator's
value_type
, thenbinary_op(x,y)
is defined.BinaryPredicate – is a model of Binary Predicate.
- Returns:
The end of the output sequence.
- Pre:
first1
may equalresult
but the range[first1, last1)
and the range[result, result + (last1 - first1))
shall not overlap otherwise.- Pre:
first2
may equalresult
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 predicatepred
to compare adjacent keys. Specifically, consecutive iteratorsi
andi+1
in the range[first1, last1)
belong to the same segment ifbinary_pred(*i, *(i+1))
is true, and belong to different segments otherwise.This version of
inclusive_scan_by_key
uses the associative operatorbinary_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 thethrust::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
See also
- 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 toOutputIterator's
value_type
.OutputIterator – is a model of Output Iterator, and if
x
andy
are objects ofOutputIterator's
value_type
, thenbinary_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 toOutputIterator's
value_type
.
- Returns:
The end of the output sequence.
- Pre:
first1
may equalresult
but the range[first1, last1)
and the range[result, result + (last1 - first1))
shall not overlap otherwise.- Pre:
first2
may equalresult
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 predicatepred
to compare adjacent keys. Specifically, consecutive iteratorsi
andi+1
in the range[first1, last1)
belong to the same segment ifbinary_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 operatorbinary_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
See also
- 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 toOutputIterator's
value_type
.OutputIterator – is a model of Output Iterator, and if
x
andy
are objects ofOutputIterator's
value_type
, thenbinary_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 toOutputIterator's
value_type
.
- Returns:
The end of the output sequence.
- Pre:
first1
may equalresult
but the range[first1, last1)
and the range[result, result + (last1 - first1))
shall not overlap otherwise.- Pre:
first2
may equalresult
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 prefixThis version of
exclusive_scan_by_key
uses the value0
to initialize the exclusive scan operation.This version of
exclusive_scan_by_key
assumesplus
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
assumesequal_to
as the binary predicate used to compare adjacent keys. Specifically, consecutive iteratorsi
andi+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 thethrust::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
- 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 equalresult
but the range[first1, last1)
and the range[result, result + (last1 - first1))
shall not overlap otherwise.- Pre:
first2
may equalresult
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 prefixThis version of
exclusive_scan_by_key
uses the value0
to initialize the exclusive scan operation.This version of
exclusive_scan_by_key
assumesplus
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
assumesequal_to
as the binary predicate used to compare adjacent keys. Specifically, consecutive iteratorsi
andi+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
- 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 equalresult
but the range[first1, last1)
and the range[result, result + (last1 - first1))
shall not overlap otherwise.- Pre:
first2
may equalresult
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 valueinit
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 thethrust::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
See also
- 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 equalresult
but the range[first1, last1)
and the range[result, result + (last1 - first1))
shall not overlap otherwise.- Pre:
first2
may equalresult
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 valueinit
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
See also
- 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 equalresult
but the range[first1, last1)
and the range[result, result + (last1 - first1))
shall not overlap otherwise.- Pre:
first2
may equalresult
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 valueinit
to initialize the exclusive scan operation.This version of
exclusive_scan_by_key
uses the binary predicatebinary_pred
to compare adjacent keys. Specifically, consecutive iteratorsi
andi+1
in the range[first1, last1)
belong to the same segment ifbinary_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 thethrust::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
See also
- 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 equalresult
but the range[first1, last1)
and the range[result, result + (last1 - first1))
shall not overlap otherwise.- Pre:
first2
may equalresult
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 valueinit
to initialize the exclusive scan operation.This version of
exclusive_scan_by_key
uses the binary predicatebinary_pred
to compare adjacent keys. Specifically, consecutive iteratorsi
andi+1
in the range[first1, last1)
belong to the same segment ifbinary_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
See also
- 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 equalresult
but the range[first1, last1)
and the range[result, result + (last1 - first1))
shall not overlap otherwise.- Pre:
first2
may equalresult
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 valueinit
to initialize the exclusive scan operation.This version of
exclusive_scan_by_key
uses the binary predicatebinary_pred
to compare adjacent keys. Specifically, consecutive iteratorsi
andi+1
in the range[first1, last1)
belong to the same segment ifbinary_pred(*i, *(i+1))
is true, and belong to different segments otherwise.This version of
exclusive_scan_by_key
uses the associative operatorbinary_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 thethrust::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
See also
- 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 toOutputIterator's
value_type
.OutputIterator – is a model of Output Iterator, and if
x
andy
are objects ofOutputIterator's
value_type
, thenbinary_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 toOutputIterator's
value_type
.
- Returns:
The end of the output sequence.
- Pre:
first1
may equalresult
but the range[first1, last1)
and the range[result, result + (last1 - first1))
shall not overlap otherwise.- Pre:
first2
may equalresult
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 valueinit
to initialize the exclusive scan operation.This version of
exclusive_scan_by_key
uses the binary predicatebinary_pred
to compare adjacent keys. Specifically, consecutive iteratorsi
andi+1
in the range[first1, last1)
belong to the same segment ifbinary_pred(*i, *(i+1))
is true, and belong to different segments otherwise.This version of
exclusive_scan_by_key
uses the associative operatorbinary_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
See also
- 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 toOutputIterator's
value_type
.OutputIterator – is a model of Output Iterator, and if
x
andy
are objects ofOutputIterator's
value_type
, thenbinary_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 toOutputIterator's
value_type
.
- Returns:
The end of the output sequence.
- Pre:
first1
may equalresult
but the range[first1, last1)
and the range[result, result + (last1 - first1))
shall not overlap otherwise.- Pre:
first2
may equalresult
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 thetransform
andinclusive_scan
operations.transform_inclusive_scan
is equivalent to performing a tranformation defined byunary_op
into a temporary sequence and then performing aninclusive_scan
on the tranformed sequence. In most cases, fusing these two operations together is more efficient, since fewer memory reads and writes are required. Intransform_inclusive_scan
,unary_op(*first)
is assigned to*result
and the result ofbinary_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.Results from this function may vary from run to run depending on the inputs provided.
The algorithm’s execution is parallelized as determined by
exec
.The following code snippet demonstrates how to use
transform_inclusive_scan
using thethrust::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
See also
- 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 tounary_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 toOutputIterator's
value_type
.AssociativeOperator – is a model of Binary Function and
AssociativeOperator's
result_type
is convertible toOutputIterator's
value_type
.
- Returns:
The end of the output sequence.
- Pre:
first
may equalresult
, 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 thetransform
andinclusive_scan
operations.transform_inclusive_scan
is equivalent to performing a tranformation defined byunary_op
into a temporary sequence and then performing aninclusive_scan
on the tranformed sequence. In most cases, fusing these two operations together is more efficient, since fewer memory reads and writes are required. Intransform_inclusive_scan
,unary_op(*first)
is assigned to*result
and the result ofbinary_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.Results from this function may vary from run to run depending on the inputs provided.
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
See also
- 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 tounary_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 toOutputIterator's
value_type
.AssociativeOperator – is a model of Binary Function and
AssociativeOperator's
result_type
is convertible toOutputIterator's
value_type
.
- Returns:
The end of the output sequence.
- Pre:
first
may equalresult
, 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 thetransform
andexclusive_scan
operations.transform_exclusive_scan
is equivalent to performing a tranformation defined byunary_op
into a temporary sequence and then performing anexclusive_scan
on the tranformed sequence. In most cases, fusing these two operations together is more efficient, since fewer memory reads and writes are required. Intransform_exclusive_scan
,init
is assigned to*result
and the result ofbinary_op(init, unary_op(*first))
is assigned to*(result + 1)
, and so on. The transform scan operation is permitted to be in-place.Results from this function may vary from run to run depending on the inputs provided.
The algorithm’s execution is parallelized as determined by
exec
.The following code snippet demonstrates how to use
transform_exclusive_scan
using thethrust::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
See also
- 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 tounary_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 toOutputIterator'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 toOutputIterator's
value_type
.
- Returns:
The end of the output sequence.
- Pre:
first
may equalresult
, 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 thetransform
andexclusive_scan
operations.transform_exclusive_scan
is equivalent to performing a tranformation defined byunary_op
into a temporary sequence and then performing anexclusive_scan
on the tranformed sequence. In most cases, fusing these two operations together is more efficient, since fewer memory reads and writes are required. Intransform_exclusive_scan
,init
is assigned to*result
and the result ofbinary_op(init, unary_op(*first))
is assigned to*(result + 1)
, and so on. The transform scan operation is permitted to be in-place.Results from this function may vary from run to run depending on the inputs provided.
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
See also
- 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 tounary_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 toOutputIterator'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 toOutputIterator's
value_type
.
- Returns:
The end of the output sequence.
- Pre:
first
may equalresult
, 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)
containsm
elements that are equivalent to each other and if[first2, last2)
containsn
elements that are equivalent to them, the lastmax(m-n,0)
elements from[first1, last1)
range shall be copied to the output range.This version of
set_difference
compares elements usingoperator<
.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 thethrust::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
See also
See also
See also
See also
- 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
andInputIterator2
have the samevalue_type
,InputIterator1's
value_type
is a model of LessThan Comparable, the ordering onInputIterator1's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator,
InputIterator2
andInputIterator1
have the samevalue_type
,InputIterator2's
value_type
is a model of LessThan Comparable, the ordering onInputIterator2's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2's
value_type
is convertable to a type inOutputIterator's
set ofvalue_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 tooperator<
.- 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)
containsm
elements that are equivalent to each other and if[first2, last2)
containsn
elements that are equivalent to them, the lastmax(m-n,0)
elements from[first1, last1)
range shall be copied to the output range.This version of
set_difference
compares elements usingoperator<
.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
See also
See also
See also
See also
- 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
andInputIterator2
have the samevalue_type
,InputIterator1's
value_type
is a model of LessThan Comparable, the ordering onInputIterator1's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator,
InputIterator2
andInputIterator1
have the samevalue_type
,InputIterator2's
value_type
is a model of LessThan Comparable, the ordering onInputIterator2's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2's
value_type
is convertable to a type inOutputIterator's
set ofvalue_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 tooperator<
.- 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)
containsm
elements that are equivalent to each other and if[first2, last2)
containsn
elements that are equivalent to them, the lastmax(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 objectcomp
.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 thethrust::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
See also
See also
See also
See also
- 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 toStrictWeakCompare's
first_argument_type
. andInputIterator1's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator,
InputIterator2's
value_type
is convertable toStrictWeakCompare's
second_argument_type
. andInputIterator2's
value_type
is convertable to a type inOutputIterator's
set ofvalue_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 tocomp
.- 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)
containsm
elements that are equivalent to each other and if[first2, last2)
containsn
elements that are equivalent to them, the lastmax(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 objectcomp
.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
See also
See also
See also
See also
- 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 toStrictWeakCompare's
first_argument_type
. andInputIterator1's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator,
InputIterator2's
value_type
is convertable toStrictWeakCompare's
second_argument_type
. andInputIterator2's
value_type
is convertable to a type inOutputIterator's
set ofvalue_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 tocomp
.- 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 appearsm
times in[first1, last1)
andn
times in[first2, last2)
(wherem
may be zero), then it appearsmin(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 usingoperator<
.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 thethrust::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
See also
See also
See also
See also
- 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
andInputIterator2
have the samevalue_type
,InputIterator1's
value_type
is a model of LessThan Comparable, the ordering onInputIterator1's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator,
InputIterator2
andInputIterator1
have the samevalue_type
,InputIterator2's
value_type
is a model of LessThan Comparable, the ordering onInputIterator2's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2's
value_type
is convertable to a type inOutputIterator's
set ofvalue_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 tooperator<
.- 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 appearsm
times in[first1, last1)
andn
times in[first2, last2)
(wherem
may be zero), then it appearsmin(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 usingoperator<
.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
See also
See also
See also
See also
- 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
andInputIterator2
have the samevalue_type
,InputIterator1's
value_type
is a model of LessThan Comparable, the ordering onInputIterator1's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator,
InputIterator2
andInputIterator1
have the samevalue_type
,InputIterator2's
value_type
is a model of LessThan Comparable, the ordering onInputIterator2's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2's
value_type
is convertable to a type inOutputIterator's
set ofvalue_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 tooperator<
.- 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 appearsm
times in[first1, last1)
andn
times in[first2, last2)
(wherem
may be zero), then it appearsmin(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 objectcomp
.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 thethrust::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
See also
See also
See also
See also
- 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
andInputIterator2
have the samevalue_type
,InputIterator1's
value_type
is a model of LessThan Comparable, the ordering onInputIterator1's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator,
InputIterator2
andInputIterator1
have the samevalue_type
,InputIterator2's
value_type
is a model of LessThan Comparable, the ordering onInputIterator2's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2's
value_type
is convertable to a type inOutputIterator's
set ofvalue_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 tocomp
.- 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 appearsm
times in[first1, last1)
andn
times in[first2, last2)
(wherem
may be zero), then it appearsmin(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 objectcomp
.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
See also
See also
See also
See also
- 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
andInputIterator2
have the samevalue_type
,InputIterator1's
value_type
is a model of LessThan Comparable, the ordering onInputIterator1's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator,
InputIterator2
andInputIterator1
have the samevalue_type
,InputIterator2's
value_type
is a model of LessThan Comparable, the ordering onInputIterator2's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2's
value_type
is convertable to a type inOutputIterator's
set ofvalue_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 tocomp
.- 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)
containsm
elements that are equivalent to each other and[first2, last1)
containsn
elements that are equivalent to them, then|m - n|
of those elements shall be copied to the output range: the lastm - n
elements from[first1, last1)
ifm > n
, and the lastn - m
of these elements from[first2, last2)
ifm < n
.This version of
set_union
compares elements usingoperator<
.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 thethrust::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
See also
includes
See also
See also
See also
See also
See also
- 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
andInputIterator2
have the samevalue_type
,InputIterator1's
value_type
is a model of LessThan Comparable, the ordering onInputIterator1's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator,
InputIterator2
andInputIterator1
have the samevalue_type
,InputIterator2's
value_type
is a model of LessThan Comparable, the ordering onInputIterator2's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2's
value_type
is convertable to a type inOutputIterator's
set ofvalue_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 tooperator<
.- 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)
containsm
elements that are equivalent to each other and[first2, last1)
containsn
elements that are equivalent to them, then|m - n|
of those elements shall be copied to the output range: the lastm - n
elements from[first1, last1)
ifm > n
, and the lastn - m
of these elements from[first2, last2)
ifm < n
.This version of
set_union
compares elements usingoperator<
.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
See also
includes
See also
See also
See also
See also
See also
- 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
andInputIterator2
have the samevalue_type
,InputIterator1's
value_type
is a model of LessThan Comparable, the ordering onInputIterator1's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator,
InputIterator2
andInputIterator1
have the samevalue_type
,InputIterator2's
value_type
is a model of LessThan Comparable, the ordering onInputIterator2's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2's
value_type
is convertable to a type inOutputIterator's
set ofvalue_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 tooperator<
.- 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)
containsm
elements that are equivalent to each other and[first2, last1)
containsn
elements that are equivalent to them, then|m - n|
of those elements shall be copied to the output range: the lastm - n
elements from[first1, last1)
ifm > n
, and the lastn - m
of these elements from[first2, last2)
ifm < n
.This version of
set_union
compares elements using a function objectcomp
.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 thethrust::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
See also
includes
See also
See also
See also
See also
See also
- 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
andInputIterator2
have the samevalue_type
,InputIterator1's
value_type
is a model of LessThan Comparable, the ordering onInputIterator1's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator,
InputIterator2
andInputIterator1
have the samevalue_type
,InputIterator2's
value_type
is a model of LessThan Comparable, the ordering onInputIterator2's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2's
value_type
is convertable to a type inOutputIterator's
set ofvalue_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 tocomp
.- 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)
containsm
elements that are equivalent to each other and[first2, last1)
containsn
elements that are equivalent to them, then|m - n|
of those elements shall be copied to the output range: the lastm - n
elements from[first1, last1)
ifm > n
, and the lastn - m
of these elements from[first2, last2)
ifm < n
.This version of
set_union
compares elements using a function objectcomp
.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
See also
includes
See also
See also
See also
See also
See also
- 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
andInputIterator2
have the samevalue_type
,InputIterator1's
value_type
is a model of LessThan Comparable, the ordering onInputIterator1's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator,
InputIterator2
andInputIterator1
have the samevalue_type
,InputIterator2's
value_type
is a model of LessThan Comparable, the ordering onInputIterator2's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2's
value_type
is convertable to a type inOutputIterator's
set ofvalue_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 tocomp
.- 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)
containsm
elements that are equivalent to each other and if[first2, last2)
containsn
elements that are equivalent to them, then allm
elements from the first range shall be copied to the output range, in order, and thenmax(n - m, 0)
elements from the second range shall be copied to the output, in order.This version of
set_union
compares elements usingoperator<
.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 thethrust::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
See also
includes
See also
See also
See also
See also
See also
- 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
andInputIterator2
have the samevalue_type
,InputIterator1's
value_type
is a model of LessThan Comparable, the ordering onInputIterator1's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator,
InputIterator2
andInputIterator1
have the samevalue_type
,InputIterator2's
value_type
is a model of LessThan Comparable, the ordering onInputIterator2's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2's
value_type
is convertable to a type inOutputIterator's
set ofvalue_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 tooperator<
.- 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)
containsm
elements that are equivalent to each other and if[first2, last2)
containsn
elements that are equivalent to them, then allm
elements from the first range shall be copied to the output range, in order, and thenmax(n - m, 0)
elements from the second range shall be copied to the output, in order.This version of
set_union
compares elements usingoperator<
.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
See also
includes
See also
See also
See also
See also
See also
- 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
andInputIterator2
have the samevalue_type
,InputIterator1's
value_type
is a model of LessThan Comparable, the ordering onInputIterator1's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator,
InputIterator2
andInputIterator1
have the samevalue_type
,InputIterator2's
value_type
is a model of LessThan Comparable, the ordering onInputIterator2's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2's
value_type
is convertable to a type inOutputIterator's
set ofvalue_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 tooperator<
.- 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)
containsm
elements that are equivalent to each other and if[first2, last2)
containsn
elements that are equivalent to them, then allm
elements from the first range shall be copied to the output range, in order, and thenmax(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 objectcomp
.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 thethrust::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
See also
includes
See also
See also
See also
See also
See also
- 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 toStrictWeakCompare's
first_argument_type
. andInputIterator1's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator,
InputIterator2's
value_type
is convertable toStrictWeakCompare's
second_argument_type
. andInputIterator2's
value_type
is convertable to a type inOutputIterator's
set ofvalue_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 tocomp
.- 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)
containsm
elements that are equivalent to each other and if[first2, last2)
containsn
elements that are equivalent to them, then allm
elements from the first range shall be copied to the output range, in order, and thenmax(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 objectcomp
.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
See also
includes
See also
See also
See also
See also
See also
- 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 toStrictWeakCompare's
first_argument_type
. andInputIterator1's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator,
InputIterator2's
value_type
is convertable toStrictWeakCompare's
second_argument_type
. andInputIterator2's
value_type
is convertable to a type inOutputIterator's
set ofvalue_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 tocomp
.- 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)
containsm
elements that are equivalent to each other and if[keys_first2, keys_last2)
containsn
elements that are equivalent to them, the lastmax(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 atvalues_first1
orvalues_first2
) to the values output range.This version of
set_difference_by_key
compares key elements usingoperator<
.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 thethrust::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
See also
See also
See also
See also
- 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
andInputIterator2
have the samevalue_type
,InputIterator1's
value_type
is a model of LessThan Comparable, the ordering onInputIterator1's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator,
InputIterator2
andInputIterator1
have the samevalue_type
,InputIterator2's
value_type
is a model of LessThan Comparable, the ordering onInputIterator2's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator3 – is a model of Input Iterator, and
InputIterator3's
value_type
is convertible to a type inOutputIterator2's
set ofvalue_types
.InputIterator4 – is a model of Input Iterator, and
InputIterator4's
value_type
is convertible to a type inOutputIterator2's
set ofvalue_types
.OutputIterator1 – is a model of Output Iterator.
OutputIterator2 – is a model of Output Iterator.
- Returns:
A
pair
p
such thatp.first
is the end of the output range of keys, and such thatp.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 tooperator<
.- 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)
containsm
elements that are equivalent to each other and if[keys_first2, keys_last2)
containsn
elements that are equivalent to them, the lastmax(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 atvalues_first1
orvalues_first2
) to the values output range.This version of
set_difference_by_key
compares key elements usingoperator<
.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
See also
See also
See also
See also
- 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
andInputIterator2
have the samevalue_type
,InputIterator1's
value_type
is a model of LessThan Comparable, the ordering onInputIterator1's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator,
InputIterator2
andInputIterator1
have the samevalue_type
,InputIterator2's
value_type
is a model of LessThan Comparable, the ordering onInputIterator2's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator3 – is a model of Input Iterator, and
InputIterator3's
value_type
is convertible to a type inOutputIterator2's
set ofvalue_types
.InputIterator4 – is a model of Input Iterator, and
InputIterator4's
value_type
is convertible to a type inOutputIterator2's
set ofvalue_types
.OutputIterator1 – is a model of Output Iterator.
OutputIterator2 – is a model of Output Iterator.
- Returns:
A
pair
p
such thatp.first
is the end of the output range of keys, and such thatp.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 tooperator<
.- 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)
containsm
elements that are equivalent to each other and if[keys_first2, keys_last2)
containsn
elements that are equivalent to them, the lastmax(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 atvalues_first1
orvalues_first2
) to the values output range.This version of
set_difference_by_key
compares key elements using a function objectcomp
.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 thethrust::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
See also
See also
See also
See also
- 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
andInputIterator2
have the samevalue_type
,InputIterator1's
value_type
is a model of LessThan Comparable, the ordering onInputIterator1's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator,
InputIterator2
andInputIterator1
have the samevalue_type
,InputIterator2's
value_type
is a model of LessThan Comparable, the ordering onInputIterator2's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator3 – is a model of Input Iterator, and
InputIterator3's
value_type
is convertible to a type inOutputIterator2's
set ofvalue_types
.InputIterator4 – is a model of Input Iterator, and
InputIterator4's
value_type
is convertible to a type inOutputIterator2's
set ofvalue_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 thatp.first
is the end of the output range of keys, and such thatp.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 tocomp
.- 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)
containsm
elements that are equivalent to each other and if[keys_first2, keys_last2)
containsn
elements that are equivalent to them, the lastmax(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 atvalues_first1
orvalues_first2
) to the values output range.This version of
set_difference_by_key
compares key elements using a function objectcomp
.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
See also
See also
See also
See also
- 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
andInputIterator2
have the samevalue_type
,InputIterator1's
value_type
is a model of LessThan Comparable, the ordering onInputIterator1's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator,
InputIterator2
andInputIterator1
have the samevalue_type
,InputIterator2's
value_type
is a model of LessThan Comparable, the ordering onInputIterator2's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator3 – is a model of Input Iterator, and
InputIterator3's
value_type
is convertible to a type inOutputIterator2's
set ofvalue_types
.InputIterator4 – is a model of Input Iterator, and
InputIterator4's
value_type
is convertible to a type inOutputIterator2's
set ofvalue_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 thatp.first
is the end of the output range of keys, and such thatp.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 tocomp
.- 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 appearsm
times in[keys_first1, keys_last1)
andn
times in[keys_first2, keys_last2)
(wherem
may be zero), then it appearsmin(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 usingoperator<
.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 thethrust::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
See also
See also
See also
See also
Note
Unlike the other key-value set operations,
set_intersection_by_key
is unique in that it has novalues_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
andInputIterator2
have the samevalue_type
,InputIterator1's
value_type
is a model of LessThan Comparable, the ordering onInputIterator1's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator,
InputIterator2
andInputIterator1
have the samevalue_type
,InputIterator2's
value_type
is a model of LessThan Comparable, the ordering onInputIterator2's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator3 – is a model of Input Iterator, and
InputIterator3's
value_type
is convertible to a type inOutputIterator2's
set ofvalue_types
.OutputIterator1 – is a model of Output Iterator.
OutputIterator2 – is a model of Output Iterator.
- Returns:
A
pair
p
such thatp.first
is the end of the output range of keys, and such thatp.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 tooperator<
.- 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 appearsm
times in[keys_first1, keys_last1)
andn
times in[keys_first2, keys_last2)
(wherem
may be zero), then it appearsmin(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 usingoperator<
.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
See also
See also
See also
See also
Note
Unlike the other key-value set operations,
set_intersection_by_key
is unique in that it has novalues_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
andInputIterator2
have the samevalue_type
,InputIterator1's
value_type
is a model of LessThan Comparable, the ordering onInputIterator1's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator,
InputIterator2
andInputIterator1
have the samevalue_type
,InputIterator2's
value_type
is a model of LessThan Comparable, the ordering onInputIterator2's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator3 – is a model of Input Iterator, and
InputIterator3's
value_type
is convertible to a type inOutputIterator2's
set ofvalue_types
.OutputIterator1 – is a model of Output Iterator.
OutputIterator2 – is a model of Output Iterator.
- Returns:
A
pair
p
such thatp.first
is the end of the output range of keys, and such thatp.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 tooperator<
.- 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 appearsm
times in[keys_first1, keys_last1)
andn
times in[keys_first2, keys_last2)
(wherem
may be zero), then it appearsmin(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 objectcomp
.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 thethrust::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
See also
See also
See also
See also
Note
Unlike the other key-value set operations,
set_intersection_by_key
is unique in that it has novalues_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
andInputIterator2
have the samevalue_type
,InputIterator1's
value_type
is a model of LessThan Comparable, the ordering onInputIterator1's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator,
InputIterator2
andInputIterator1
have the samevalue_type
,InputIterator2's
value_type
is a model of LessThan Comparable, the ordering onInputIterator2's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator3 – is a model of Input Iterator, and
InputIterator3's
value_type
is convertible to a type inOutputIterator2's
set ofvalue_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 thatp.first
is the end of the output range of keys, and such thatp.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 tocomp
.- 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 appearsm
times in[keys_first1, keys_last1)
andn
times in[keys_first2, keys_last2)
(wherem
may be zero), then it appearsmin(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 objectcomp
.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
See also
See also
See also
See also
Note
Unlike the other key-value set operations,
set_intersection_by_key
is unique in that it has novalues_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
andInputIterator2
have the samevalue_type
,InputIterator1's
value_type
is a model of LessThan Comparable, the ordering onInputIterator1's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator,
InputIterator2
andInputIterator1
have the samevalue_type
,InputIterator2's
value_type
is a model of LessThan Comparable, the ordering onInputIterator2's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator3 – is a model of Input Iterator, and
InputIterator3's
value_type
is convertible to a type inOutputIterator2's
set ofvalue_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 thatp.first
is the end of the output range of keys, and such thatp.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 tocomp
.- 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)
containsm
elements that are equivalent to each other and[keys_first2, keys_last1)
containsn
elements that are equivalent to them, then|m - n|
of those elements shall be copied to the output range: the lastm - n
elements from[keys_first1, keys_last1)
ifm > n
, and the lastn - m
of these elements from[keys_first2, keys_last2)
ifm < 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 atvalues_first1
orvalues_first2
) to the values output range.This version of
set_symmetric_difference_by_key
compares key elements usingoperator<
.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 thethrust::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
See also
See also
See also
See also
- 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
andInputIterator2
have the samevalue_type
,InputIterator1's
value_type
is a model of LessThan Comparable, the ordering onInputIterator1's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator,
InputIterator2
andInputIterator1
have the samevalue_type
,InputIterator2's
value_type
is a model of LessThan Comparable, the ordering onInputIterator2's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator3 – is a model of Input Iterator, and
InputIterator3's
value_type
is convertible to a type inOutputIterator2's
set ofvalue_types
.InputIterator4 – is a model of Input Iterator, and
InputIterator4's
value_type
is convertible to a type inOutputIterator2's
set ofvalue_types
.OutputIterator1 – is a model of Output Iterator.
OutputIterator2 – is a model of Output Iterator.
- Returns:
A
pair
p
such thatp.first
is the end of the output range of keys, and such thatp.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 tooperator<
.- 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)
containsm
elements that are equivalent to each other and[keys_first2, keys_last1)
containsn
elements that are equivalent to them, then|m - n|
of those elements shall be copied to the output range: the lastm - n
elements from[keys_first1, keys_last1)
ifm > n
, and the lastn - m
of these elements from[keys_first2, keys_last2)
ifm < 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 atvalues_first1
orvalues_first2
) to the values output range.This version of
set_symmetric_difference_by_key
compares key elements usingoperator<
.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
See also
See also
See also
See also
- 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
andInputIterator2
have the samevalue_type
,InputIterator1's
value_type
is a model of LessThan Comparable, the ordering onInputIterator1's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator,
InputIterator2
andInputIterator1
have the samevalue_type
,InputIterator2's
value_type
is a model of LessThan Comparable, the ordering onInputIterator2's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator3 – is a model of Input Iterator, and
InputIterator3's
value_type
is convertible to a type inOutputIterator2's
set ofvalue_types
.InputIterator4 – is a model of Input Iterator, and
InputIterator4's
value_type
is convertible to a type inOutputIterator2's
set ofvalue_types
.OutputIterator1 – is a model of Output Iterator.
OutputIterator2 – is a model of Output Iterator.
- Returns:
A
pair
p
such thatp.first
is the end of the output range of keys, and such thatp.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 tooperator<
.- 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)
containsm
elements that are equivalent to each other and[keys_first2, keys_last1)
containsn
elements that are equivalent to them, then|m - n|
of those elements shall be copied to the output range: the lastm - n
elements from[keys_first1, keys_last1)
ifm > n
, and the lastn - m
of these elements from[keys_first2, keys_last2)
ifm < 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 atvalues_first1
orvalues_first2
) to the values output range.This version of
set_symmetric_difference_by_key
compares key elements using a function objectcomp
.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 thethrust::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
See also
See also
See also
See also
- 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
andInputIterator2
have the samevalue_type
,InputIterator1's
value_type
is a model of LessThan Comparable, the ordering onInputIterator1's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator,
InputIterator2
andInputIterator1
have the samevalue_type
,InputIterator2's
value_type
is a model of LessThan Comparable, the ordering onInputIterator2's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator3 – is a model of Input Iterator, and
InputIterator3's
value_type
is convertible to a type inOutputIterator2's
set ofvalue_types
.InputIterator4 – is a model of Input Iterator, and
InputIterator4's
value_type
is convertible to a type inOutputIterator2's
set ofvalue_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 thatp.first
is the end of the output range of keys, and such thatp.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 tocomp
.- 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)
containsm
elements that are equivalent to each other and[keys_first2, keys_last1)
containsn
elements that are equivalent to them, then|m - n|
of those elements shall be copied to the output range: the lastm - n
elements from[keys_first1, keys_last1)
ifm > n
, and the lastn - m
of these elements from[keys_first2, keys_last2)
ifm < 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 atvalues_first1
orvalues_first2
) to the values output range.This version of
set_symmetric_difference_by_key
compares key elements using a function objectcomp
.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
See also
See also
See also
See also
- 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
andInputIterator2
have the samevalue_type
,InputIterator1's
value_type
is a model of LessThan Comparable, the ordering onInputIterator1's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator,
InputIterator2
andInputIterator1
have the samevalue_type
,InputIterator2's
value_type
is a model of LessThan Comparable, the ordering onInputIterator2's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator3 – is a model of Input Iterator, and
InputIterator3's
value_type
is convertible to a type inOutputIterator2's
set ofvalue_types
.InputIterator4 – is a model of Input Iterator, and
InputIterator4's
value_type
is convertible to a type inOutputIterator2's
set ofvalue_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 thatp.first
is the end of the output range of keys, and such thatp.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 tocomp
.- 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)
containsm
elements that are equivalent to each other and if[keys_first2, keys_last2)
containsn
elements that are equivalent to them, then allm
elements from the first range shall be copied to the output range, in order, and thenmax(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 atvalues_first1
orvalues_first2
) to the values output range.This version of
set_union_by_key
compares key elements usingoperator<
.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 thethrust::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
See also
See also
See also
See also
- 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
andInputIterator2
have the samevalue_type
,InputIterator1's
value_type
is a model of LessThan Comparable, the ordering onInputIterator1's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator,
InputIterator2
andInputIterator1
have the samevalue_type
,InputIterator2's
value_type
is a model of LessThan Comparable, the ordering onInputIterator2's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator3 – is a model of Input Iterator, and
InputIterator3's
value_type
is convertible to a type inOutputIterator2's
set ofvalue_types
.InputIterator4 – is a model of Input Iterator, and
InputIterator4's
value_type
is convertible to a type inOutputIterator2's
set ofvalue_types
.OutputIterator1 – is a model of Output Iterator.
OutputIterator2 – is a model of Output Iterator.
- Returns:
A
pair
p
such thatp.first
is the end of the output range of keys, and such thatp.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 tooperator<
.- 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)
containsm
elements that are equivalent to each other and if[keys_first2, keys_last2)
containsn
elements that are equivalent to them, then allm
elements from the first range shall be copied to the output range, in order, and thenmax(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 atvalues_first1
orvalues_first2
) to the values output range.This version of
set_union_by_key
compares key elements usingoperator<
.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
See also
See also
See also
See also
- 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
andInputIterator2
have the samevalue_type
,InputIterator1's
value_type
is a model of LessThan Comparable, the ordering onInputIterator1's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator,
InputIterator2
andInputIterator1
have the samevalue_type
,InputIterator2's
value_type
is a model of LessThan Comparable, the ordering onInputIterator2's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator3 – is a model of Input Iterator, and
InputIterator3's
value_type
is convertible to a type inOutputIterator2's
set ofvalue_types
.InputIterator4 – is a model of Input Iterator, and
InputIterator4's
value_type
is convertible to a type inOutputIterator2's
set ofvalue_types
.OutputIterator1 – is a model of Output Iterator.
OutputIterator2 – is a model of Output Iterator.
- Returns:
A
pair
p
such thatp.first
is the end of the output range of keys, and such thatp.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 tooperator<
.- 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)
containsm
elements that are equivalent to each other and if[keys_first2, keys_last2)
containsn
elements that are equivalent to them, then allm
elements from the first range shall be copied to the output range, in order, and thenmax(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 atvalues_first1
orvalues_first2
) to the values output range.This version of
set_union_by_key
compares key elements using a function objectcomp
.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 thethrust::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
See also
See also
See also
See also
- 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
andInputIterator2
have the samevalue_type
,InputIterator1's
value_type
is a model of LessThan Comparable, the ordering onInputIterator1's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator,
InputIterator2
andInputIterator1
have the samevalue_type
,InputIterator2's
value_type
is a model of LessThan Comparable, the ordering onInputIterator2's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator3 – is a model of Input Iterator, and
InputIterator3's
value_type
is convertible to a type inOutputIterator2's
set ofvalue_types
.InputIterator4 – is a model of Input Iterator, and
InputIterator4's
value_type
is convertible to a type inOutputIterator2's
set ofvalue_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 thatp.first
is the end of the output range of keys, and such thatp.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 tocomp
.- 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)
containsm
elements that are equivalent to each other and if[keys_first2, keys_last2)
containsn
elements that are equivalent to them, then allm
elements from the first range shall be copied to the output range, in order, and thenmax(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 atvalues_first1
orvalues_first2
) to the values output range.This version of
set_union_by_key
compares key elements using a function objectcomp
.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
See also
See also
See also
See also
- 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
andInputIterator2
have the samevalue_type
,InputIterator1's
value_type
is a model of LessThan Comparable, the ordering onInputIterator1's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator2 – is a model of Input Iterator,
InputIterator2
andInputIterator1
have the samevalue_type
,InputIterator2's
value_type
is a model of LessThan Comparable, the ordering onInputIterator2's
value_type
is a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2's
value_type
is convertable to a type inOutputIterator's
set ofvalue_types
.InputIterator3 – is a model of Input Iterator, and
InputIterator3's
value_type
is convertible to a type inOutputIterator2's
set ofvalue_types
.InputIterator4 – is a model of Input Iterator, and
InputIterator4's
value_type
is convertible to a type inOutputIterator2's
set ofvalue_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 thatp.first
is the end of the output range of keys, and such thatp.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 tocomp
.- 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 ifi
andj
are any two valid iterators in[first, last)
such thati
precedesj
, 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 bysort
.This version of
sort
compares objects usingoperator<
.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 thethrust::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
See also
- 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, andRandomAccessIterator's
value_type
is a model of LessThan Comparable, and the ordering relation onRandomAccessIterator'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 ifi
andj
are any two valid iterators in[first, last)
such thati
precedesj
, 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 bysort
.This version of
sort
compares objects usingoperator<
.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
See also
- 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, andRandomAccessIterator's
value_type
is a model of LessThan Comparable, and the ordering relation onRandomAccessIterator'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 ifi
andj
are any two valid iterators in[first, last)
such thati
precedesj
, 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 bysort
.This version of
sort
compares objects using a function objectcomp
.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
See also
- 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, andRandomAccessIterator's
value_type
is convertible toStrictWeakOrdering's
first_argument_type
andsecond_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 ifi
andj
are any two valid iterators in[first, last)
such thati
precedesj
, 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 bysort
.This version of
sort
compares objects using a function objectcomp
.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
See also
- 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, andRandomAccessIterator's
value_type
is convertible toStrictWeakOrdering's
first_argument_type
andsecond_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 likesort:
it sorts the elements in[first, last)
into ascending order, meaning that ifi
andj
are any two valid iterators in[first, last)
such thati
precedesj
, 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, ifx
andy
are elements in[first, last)
such thatx
precedesy
, and if the two elements are equivalent (neitherx < y
nory < x
) then a postcondition ofstable_sort
is thatx
still precedesy
.This version of
stable_sort
compares objects usingoperator<
.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 thethrust::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
See also
- 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, andRandomAccessIterator's
value_type
is a model of LessThan Comparable, and the ordering relation onRandomAccessIterator'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 likesort:
it sorts the elements in[first, last)
into ascending order, meaning that ifi
andj
are any two valid iterators in[first, last)
such thati
precedesj
, 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, ifx
andy
are elements in[first, last)
such thatx
precedesy
, and if the two elements are equivalent (neitherx < y
nory < x
) then a postcondition ofstable_sort
is thatx
still precedesy
.This version of
stable_sort
compares objects usingoperator<
.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
See also
- 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, andRandomAccessIterator's
value_type
is a model of LessThan Comparable, and the ordering relation onRandomAccessIterator'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 likesort:
it sorts the elements in[first, last)
into ascending order, meaning that ifi
andj
are any two valid iterators in[first, last)
such thati
precedesj
, 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, ifx
andy
are elements in[first, last)
such thatx
precedesy
, and if the two elements are equivalent (neitherx < y
nory < x
) then a postcondition ofstable_sort
is thatx
still precedesy
.This version of
stable_sort
compares objects using a function objectcomp
.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
See also
- 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, andRandomAccessIterator's
value_type
is convertible toStrictWeakOrdering's
first_argument_type
andsecond_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 likesort:
it sorts the elements in[first, last)
into ascending order, meaning that ifi
andj
are any two valid iterators in[first, last)
such thati
precedesj
, 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, ifx
andy
are elements in[first, last)
such thatx
precedesy
, and if the two elements are equivalent (neitherx < y
nory < x
) then a postcondition ofstable_sort
is thatx
still precedesy
.This version of
stable_sort
compares objects using a function objectcomp
.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
See also
- 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, andRandomAccessIterator's
value_type
is convertible toStrictWeakOrdering's
first_argument_type
andsecond_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 ifi
andj
are any two valid iterators in[keys_first, keys_last)
such thati
precedesj
, andp
andq
are iterators in[values_first, values_first + (keys_last - keys_first))
corresponding toi
andj
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 bysort_by_key
.This version of
sort_by_key
compares key objects usingoperator<
.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 thethrust::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
See also
- 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, andRandomAccessIterator1's
value_type
is a model of LessThan Comparable, and the ordering relation onRandomAccessIterator1'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 ifi
andj
are any two valid iterators in[keys_first, keys_last)
such thati
precedesj
, andp
andq
are iterators in[values_first, values_first + (keys_last - keys_first))
corresponding toi
andj
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 bysort_by_key
.This version of
sort_by_key
compares key objects usingoperator<
.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
See also
- 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, andRandomAccessIterator1's
value_type
is a model of LessThan Comparable, and the ordering relation onRandomAccessIterator1'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 ifi
andj
are any two valid iterators in[keys_first, keys_last)
such thati
precedesj
, andp
andq
are iterators in[values_first, values_first + (keys_last - keys_first))
corresponding toi
andj
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 bysort_by_key
.This version of
sort_by_key
compares key objects using a function objectcomp
.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 thethrust::host
execution policy for parallelization.The keys are sorted in descending order using thegreater<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
See also
- 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, andRandomAccessIterator1's
value_type
is convertible toStrictWeakOrdering's
first_argument_type
andsecond_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 ifi
andj
are any two valid iterators in[keys_first, keys_last)
such thati
precedesj
, andp
andq
are iterators in[values_first, values_first + (keys_last - keys_first))
corresponding toi
andj
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 bysort_by_key
.This version of
sort_by_key
compares key objects using a function objectcomp
.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
See also
- 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, andRandomAccessIterator1's
value_type
is convertible toStrictWeakOrdering's
first_argument_type
andsecond_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 ifi
andj
are any two valid iterators in[keys_first, keys_last)
such thati
precedesj
, andp
andq
are iterators in[values_first, values_first + (keys_last - keys_first))
corresponding toi
andj
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, ifx
andy
are elements in[keys_first, keys_last)
such thatx
precedesy
, and if the two elements are equivalent (neitherx < y
nory < x
) then a postcondition ofstable_sort_by_key
is thatx
still precedesy
.This version of
stable_sort_by_key
compares key objects usingoperator<
.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 thethrust::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
See also
- 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, andRandomAccessIterator1's
value_type
is a model of LessThan Comparable, and the ordering relation onRandomAccessIterator1'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 ifi
andj
are any two valid iterators in[keys_first, keys_last)
such thati
precedesj
, andp
andq
are iterators in[values_first, values_first + (keys_last - keys_first))
corresponding toi
andj
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, ifx
andy
are elements in[keys_first, keys_last)
such thatx
precedesy
, and if the two elements are equivalent (neitherx < y
nory < x
) then a postcondition ofstable_sort_by_key
is thatx
still precedesy
.This version of
stable_sort_by_key
compares key objects usingoperator<
.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
See also
- 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, andRandomAccessIterator1's
value_type
is a model of LessThan Comparable, and the ordering relation onRandomAccessIterator1'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 ifi
andj
are any two valid iterators in[keys_first, keys_last)
such thati
precedesj
, andp
andq
are iterators in[values_first, values_first + (keys_last - keys_first))
corresponding toi
andj
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, ifx
andy
are elements in[keys_first, keys_last)
such thatx
precedesy
, and if the two elements are equivalent (neitherx < y
nory < x
) then a postcondition ofstable_sort_by_key
is thatx
still precedesy
.This version of
stable_sort_by_key
compares key objects using the function objectcomp
.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 thethrust::host
execution policy for parallelization. The keys are sorted in descending order using thegreater<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
See also
- 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, andRandomAccessIterator1's
value_type
is convertible toStrictWeakOrdering's
first_argument_type
andsecond_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 ifi
andj
are any two valid iterators in[keys_first, keys_last)
such thati
precedesj
, andp
andq
are iterators in[values_first, values_first + (keys_last - keys_first))
corresponding toi
andj
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, ifx
andy
are elements in[keys_first, keys_last)
such thatx
precedesy
, and if the two elements are equivalent (neitherx < y
nory < x
) then a postcondition ofstable_sort_by_key
is thatx
still precedesy
.This version of
stable_sort_by_key
compares key objects using the function objectcomp
.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
See also
- 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, andRandomAccessIterator1's
value_type
is convertible toStrictWeakOrdering's
first_argument_type
andsecond_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 iteratori
in the range[first + 1, last)
, the difference of*i
and*(i - 1)
is assigned to*(result + (i - first))
.This version of
adjacent_difference
usesoperator-
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 thethrust::device
execution policy:Remark
Note that
result
is permitted to be the same iterator asfirst
. 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
- 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
andy
are objects ofInputIterator's
value_type
, thenx
-is
defined, andInputIterator's
value_type
is convertible to a type inOutputIterator's
set ofvalue_types
, and the return type ofx - y
is convertible to a type inOutputIterator's
set ofvalue_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 iteratori
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 functionbinary_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 thethrust::device
execution policy:Remark
Note that
result
is permitted to be the same iterator asfirst
. 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
- 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 toBinaryFunction's
first_argument_type
andsecond_argument_type
, andInputIterator's
value_type
is convertible to a type inOutputIterator's
set ofvalue_types
.OutputIterator – is a model of Output Iterator.
BinaryFunction's –
result_type
is convertible to a type inOutputIterator's
set ofvalue_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 iteratori
in the range[first + 1, last)
, the difference of*i
and*(i - 1)
is assigned to*(result + (i - first))
.This version of
adjacent_difference
usesoperator-
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 asfirst
. 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
- 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
andy
are objects ofInputIterator's
value_type
, thenx
-is
defined, andInputIterator's
value_type
is convertible to a type inOutputIterator's
set ofvalue_types
, and the return type ofx - y
is convertible to a type inOutputIterator's
set ofvalue_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 iteratori
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 functionbinary_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 asfirst
. 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
- 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 toBinaryFunction's
first_argument_type
andsecond_argument_type
, andInputIterator's
value_type
is convertible to a type inOutputIterator's
set ofvalue_types
.OutputIterator – is a model of Output Iterator.
BinaryFunction's –
result_type
is convertible to a type inOutputIterator's
set ofvalue_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 invokinggen
, 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 functionrand
using thethrust::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
- 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 toForwardIterator's
value_type
.
-
template<typename ForwardIterator, typename Generator>
void generate(ForwardIterator first, ForwardIterator last, Generator gen)# generate
assigns the result of invokinggen
, 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 functionrand
.#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
- 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 toForwardIterator'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 invokinggen
, a function object that takes no arguments, to each element in the range[first,first + n)
. The return value isfirst + 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 functionrand
using thethrust::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
- 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 inOutputIterator's
set ofvalue_types
.
-
template<typename OutputIterator, typename Size, typename Generator>
OutputIterator generate_n(OutputIterator first, Size n, Generator gen)# generate_n
assigns the result of invokinggen
, a function object that takes no arguments, to each element in the range[first,first + n)
. The return value isfirst + n
.The following code snippet demonstrates how to fill a
host_vector
with random numbers, using the standard C library functionrand
.#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
- 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 inOutputIterator's
set ofvalue_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 ofsequence
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 thethrust::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 ifx
andy
are objects ofForwardIterator's
value_type
, thenx + y
is defined, and ifT
isForwardIterator's
value_type
, thenT(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 ofsequence
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 ifx
andy
are objects ofForwardIterator's
value_type
, thenx + y
is defined, and ifT
isForwardIterator's
value_type
, thenT(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 ofsequence
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 thethrust::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 ifx
andy
are objects ofForwardIterator's
value_type
, thenx + y
is defined, and ifT
isForwardIterator's
value_type
, thenT(0)
is defined.T – is a model of Assignable, and
T
is convertible toForwardIterator'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 ofsequence
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 ifx
andy
are objects ofForwardIterator's
value_type
, thenx + y
is defined, and ifT
isForwardIterator's
value_type
, thenT(0)
is defined.T – is a model of Assignable, and
T
is convertible toForwardIterator'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 ofsequence
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 thethrust::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 ifx
andy
are objects ofForwardIterator's
value_type
, thenx + y
is defined, and ifT
isForwardIterator's
value_type
, thenT(0)
is defined.T – is a model of Assignable, and
T
is convertible toForwardIterator'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 ofsequence
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 ifx
andy
are objects ofForwardIterator's
value_type
, thenx + y
is defined, and ifT
isForwardIterator's
value_type
, thenT(0)
is defined.T – is a model of Assignable, and
T
is convertible toForwardIterator'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 firstn
non-positive integers using thethrust::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 ifx
andy
are objects ofForwardIterator's
value_type
, thenx + y
is defined, and ifT
isForwardIterator's
value_type
, thenT(0)
is defined.UnaryOperation – is a model of Unary Function and
UnaryFunction's
result_type
is convertible toOutputIterator'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 firstn
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 ifx
andy
are objects ofForwardIterator's
value_type
, thenx + y
is defined, and ifT
isForwardIterator's
value_type
, thenT(0)
is defined.UnaryOperation – is a model of Unary Function and
UnaryFunction's
result_type
is convertible toOutputIterator'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 iteratori
in the range [first
,last
) the operationop(*i)
is performed and the result is assigned to*o
, whereo
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 thethrust::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 toUnaryFunction's
argument_type
.OutputIterator – is a model of Output Iterator.
UnaryFunction – is a model of Unary Function and
UnaryFunction's
result_type
is convertible toOutputIterator's
value_type
.
- Returns:
The end of the output sequence.
- Pre:
first
may equalresult
, 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 iteratori
in the range [first
,last
) the operationop(*i)
is performed and the result is assigned to*o
, whereo
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 toUnaryFunction's
argument_type
.OutputIterator – is a model of Output Iterator.
UnaryFunction – is a model of Unary Function and
UnaryFunction's
result_type
is convertible toOutputIterator's
value_type
.
- Returns:
The end of the output sequence.
- Pre:
first
may equalresult
, 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 iteratori
in the range [first1
,last1
) andj = first + (i - first1)
in the range [first2
,last2
) the operationop(*i,*j)
is performed and the result is assigned to*o
, whereo
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 thethrust::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 toBinaryFunction's
first_argument_type
.InputIterator2 – is a model of Input Iterator and
InputIterator2's
value_type
is convertible toBinaryFunction'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 toOutputIterator's
value_type
.
- Returns:
The end of the output sequence.
- Pre:
first1
may equalresult
, but the range[first1, last1)
shall not overlap the range[result, result + (last1 - first1))
otherwise.- Pre:
first2
may equalresult
, 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 iteratori
in the range [first1
,last1
) andj = first + (i - first1)
in the range [first2
,last2
) the operationop(*i,*j)
is performed and the result is assigned to*o
, whereo
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 toBinaryFunction's
first_argument_type
.InputIterator2 – is a model of Input Iterator and
InputIterator2's
value_type
is convertible toBinaryFunction'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 toOutputIterator's
value_type
.
- Returns:
The end of the output sequence.
- Pre:
first1
may equalresult
, but the range[first1, last1)
shall not overlap the range[result, result + (last1 - first1))
otherwise.- Pre:
first2
may equalresult
, 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 predicatepred(*i)
is evaluated. If this predicate evaluates totrue
, the result ofop(*i)
is assigned to*o
, whereo
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 thethrust::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 toPredicate's
argument_type
, andInputIterator's
value_type
is convertible toUnaryFunction's
argument_type
.ForwardIterator – is a model of Forward Iterator.
UnaryFunction – is a model of Unary Function and
UnaryFunction's
result_type
is convertible toOutputIterator's
value_type
.Predicate – is a model of Predicate.
- Returns:
The end of the output sequence.
- Pre:
first
may equalresult
, 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 predicatepred(*i)
is evaluated. If this predicate evaluates totrue
, the result ofop(*i)
is assigned to*o
, whereo
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 toPredicate's
argument_type
, andInputIterator's
value_type
is convertible toUnaryFunction's
argument_type
.ForwardIterator – is a model of Forward Iterator.
UnaryFunction – is a model of Unary Function and
UnaryFunction's
result_type
is convertible toOutputIterator's
value_type
.Predicate – is a model of Predicate.
- Returns:
The end of the output sequence.
- Pre:
first
may equalresult
, 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 predicatepred(*s)
is evaluated, wheres
is the corresponding input iterator in the range[stencil, stencil + (last - first) )
. If this predicate evaluates totrue
, the result ofop(*i)
is assigned to*o
, whereo
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 thethrust::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 toUnaryFunction's
argument_type
.InputIterator2 – is a model of Input Iterator and
InputIterator2's
value_type
is convertible toPredicate's
argument_type
.ForwardIterator – is a model of Forward Iterator.
UnaryFunction – is a model of Unary Function and
UnaryFunction's
result_type
is convertible toOutputIterator's
value_type
.Predicate – is a model of Predicate.
- Returns:
The end of the output sequence.
- Pre:
first
may equalresult
, but the range[first, last)
shall not overlap the range[result, result + (last - first))
otherwise.- Pre:
stencil
may equalresult
, 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 predicatepred(*s)
is evaluated, wheres
is the corresponding input iterator in the range[stencil, stencil + (last - first) )
. If this predicate evaluates totrue
, the result ofop(*i)
is assigned to*o
, whereo
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 toUnaryFunction's
argument_type
.InputIterator2 – is a model of Input Iterator and
InputIterator2's
value_type
is convertible toPredicate's
argument_type
.ForwardIterator – is a model of Forward Iterator.
UnaryFunction – is a model of Unary Function and
UnaryFunction's
result_type
is convertible toOutputIterator's
value_type
.Predicate – is a model of Predicate.
- Returns:
The end of the output sequence.
- Pre:
first
may equalresult
, but the range[first, last)
shall not overlap the range[result, result + (last - first))
otherwise.- Pre:
stencil
may equalresult
, 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)
andj = first2 + (i - first1)
in the range[first2, first2 + (last1 - first1) )
, the predicatepred(*s)
is evaluated, wheres
is the corresponding input iterator in the range[stencil, stencil + (last1 - first1) )
. If this predicate evaluates totrue
, the result ofbinary_op(*i,*j)
is assigned to*o
, whereo
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 thethrust::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 toBinaryFunction's
first_argument_type
.InputIterator2 – is a model of Input Iterator and
InputIterator2's
value_type
is convertible toBinaryFunction'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 toOutputIterator's
value_type
.Predicate – is a model of Predicate.
- Returns:
The end of the output sequence.
- Pre:
first1
may equalresult
, but the range[first1, last1)
shall not overlap the range[result, result + (last1 - first1))
otherwise.- Pre:
first2
may equalresult
, but the range[first2, first2 + (last1 - first1))
shall not overlap the range[result, result + (last1 - first1))
otherwise.- Pre:
stencil
may equalresult
, 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)
andj = first2 + (i - first1)
in the range[first2, first2 + (last1 - first1) )
, the predicatepred(*s)
is evaluated, wheres
is the corresponding input iterator in the range[stencil, stencil + (last1 - first1) )
. If this predicate evaluates totrue
, the result ofbinary_op(*i,*j)
is assigned to*o
, whereo
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 toBinaryFunction's
first_argument_type
.InputIterator2 – is a model of Input Iterator and
InputIterator2's
value_type
is convertible toBinaryFunction'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 toOutputIterator's
value_type
.Predicate – is a model of Predicate.
- Returns:
The end of the output sequence.
- Pre:
first1
may equalresult
, but the range[first1, last1)
shall not overlap the range[result, result + (last1 - first1))
otherwise.- Pre:
first2
may equalresult
, but the range[first2, first2 + (last1 - first1))
shall not overlap the range[result, result + (last1 - first1))
otherwise.- Pre:
stencil
may equalresult
, 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 valuevalue
to every element in the range[first, last)
. That is, for every iteratori
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 thethrust::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
See also
- 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 toForwardIterator's
value_type
.
- template<typename ForwardIterator, typename T> __host__ __device__ void fill (ForwardIterator first, ForwardIterator last, const T &value)
fill
assigns the valuevalue
to every element in the range[first, last)
. That is, for every iteratori
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
See also
- 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 toForwardIterator'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 valuevalue
to every element in the range[first, first+n)
. That is, for every iteratori
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 thethrust::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
See also
- 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 inOutputIterator's
set ofvalue_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 valuevalue
to every element in the range[first, first+n)
. That is, for every iteratori
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
See also
- 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 inOutputIterator's
set ofvalue_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 functionthrust::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, thenuninitialized_fill
creates copies ofx
in that range. That is, for each iteratori
in the range[first, last)
,uninitialized_fill
creates a copy ofx
in the location pointed toi
by callingForwardIterator'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 thethrust::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
See also
See also
See also
See also
- 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, andForwardIterator's
value_type
has a constructor that takes a single argument of typeT
.
-
template<typename ForwardIterator, typename T>
void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T &x)# In
thrust
, the functionthrust::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, thenuninitialized_fill
creates copies ofx
in that range. That is, for each iteratori
in the range[first, last)
,uninitialized_fill
creates a copy ofx
in the location pointed toi
by callingForwardIterator'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
See also
See also
See also
See also
- 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, andForwardIterator's
value_type
has a constructor that takes a single argument of typeT
.
- 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 functionthrust::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, thenuninitialized_fill
creates copies ofx
in that range. That is, for each iteratori
in the range[first, first+n)
,uninitialized_fill
creates a copy ofx
in the location pointed toi
by callingForwardIterator'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 thethrust::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
See also
See also
See also
See also
- 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, andForwardIterator's
value_type
has a constructor that takes a single argument of typeT
.
- Returns:
first+n
-
template<typename ForwardIterator, typename Size, typename T>
ForwardIterator uninitialized_fill_n(ForwardIterator first, Size n, const T &x)# In
thrust
, the functionthrust::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, thenuninitialized_fill
creates copies ofx
in that range. That is, for each iteratori
in the range[first, first+n)
,uninitialized_fill
creates a copy ofx
in the location pointed toi
by callingForwardIterator'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
See also
See also
See also
See also
- 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, andForwardIterator's
value_type
has a constructor that takes a single argument of typeT
.- 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 objectf
to each element in the range[first, last)
;f's
return value, if any, is ignored. Unlike the C++ Standard Template Library functionstd::for_each
, this version offers no guarantee on order of execution. For this reason, this version offor_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 athrust::device_vector
using thethrust::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
- 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 toUnaryFunction'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 objectf
to each element in the range[first, first + n)
;f's
return value, if any, is ignored. Unlike the C++ Standard Template Library functionstd::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 adevice_vector
using thethrust::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
- 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 toUnaryFunction'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 objectf
to each element in the range[first, last)
;f's
return value, if any, is ignored. Unlike the C++ Standard Template Library functionstd::for_each
, this version offers no guarantee on order of execution. For this reason, this version offor_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 adevice_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
- 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 toUnaryFunction'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 objectf
to each element in the range[first, first + n)
;f's
return value, if any, is ignored. Unlike the C++ Standard Template Library functionstd::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 adevice_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
- 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 toUnaryFunction'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 toold_value
withnew_value
. That is: for every iteratori
, if*i == old_value
then it performs theassignment *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 adevice_vector
with another using thethrust::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
See also
See also
- 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 ofT
may be compared for equality with objects ofForwardIterator's
value_type
, andT
is convertible toForwardIterator'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 toold_value
withnew_value
. That is: for every iteratori
, if*i == old_value
then it performs theassignment *i = new_value
.The following code snippet demonstrates how to use
replace
to replace a value of interest in adevice_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
See also
See also
- 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 ofT
may be compared for equality with objects ofForwardIterator's
value_type
, andT
is convertible toForwardIterator'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 whichpred
returnstrue
withnew_value
. That is: for every iteratori
, ifpred(*i)
istrue
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 adevice_vector's
negative elements with0
using thethrust::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
See also
See also
- 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 totrue
.
- Template Parameters:
DerivedPolicy – The name of the derived execution policy.
ForwardIterator – is a model of Forward Iterator,
ForwardIterator
is mutable, andForwardIterator's
value_type
is convertible toPredicate's
argument_type
.Predicate – is a model of Predicate.
T – is a model of Assignable, and
T
is convertible toForwardIterator'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 whichpred
returnstrue
withnew_value
. That is: for every iteratori
, ifpred(*i)
istrue
then it performs the assignment*i = new_value
.The following code snippet demonstrates how to use
replace_if
to replace adevice_vector's
negative elements with0
.#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
See also
See also
- 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 totrue
.
- Template Parameters:
ForwardIterator – is a model of Forward Iterator,
ForwardIterator
is mutable, andForwardIterator's
value_type
is convertible toPredicate's
argument_type
.Predicate – is a model of Predicate.
T – is a model of Assignable, and
T
is convertible toForwardIterator'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 whichpred(*s)
returnstrue
withnew_value
. That is: for every iteratori
in the range[first, last)
, ands
in the range[stencil, stencil + (last - first))
, ifpred(*s)
istrue
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 adevice_vector's
element with0
when its corresponding stencil element is less than zero using thethrust::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
See also
See also
- 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 totrue
.
- 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 toPredicate's
argument_type
.Predicate – is a model of Predicate.
T – is a model of Assignable, and
T
is convertible toForwardIterator'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 whichpred(*s)
returnstrue
withnew_value
. That is: for every iteratori
in the range[first, last)
, ands
in the range[stencil, stencil + (last - first))
, ifpred(*s)
istrue
then it performs the assignment*i = new_value
.The following code snippet demonstrates how to use
replace_if
to replace adevice_vector's
element with0
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
See also
See also
- 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 totrue
.
- 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 toPredicate's
argument_type
.Predicate – is a model of Predicate.
T – is a model of Assignable, and
T
is convertible toForwardIterator'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 toold_value
is not copied;new_value
is copied instead.More precisely, for every integer
n
such that0 <= 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
See also
See also
See also
- 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 totrue
.
- 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 withInputIterator's
value_type
, andT
is convertible toOutputIterator's
value_type
.
- Returns:
result + (last-first)
- Pre:
first
may equalresult
, 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 toold_value
is not copied;new_value
is copied instead.More precisely, for every integer
n
such that0 <= 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
See also
See also
See also
- 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 totrue
.
- Template Parameters:
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 withInputIterator's
value_type
, andT
is convertible toOutputIterator's
value_type
.
- Returns:
result + (last-first)
- Pre:
first
may equalresult
, 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 whichpred
istrue
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
ifpred(*(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
See also
See also
- 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 totrue
.
- Template Parameters:
DerivedPolicy – The name of the derived execution policy.
InputIterator – is a model of Input Iterator, and
InputIterator's
value_type
is convertible toPredicate'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 toOutputIterator's
value_type
.
- Returns:
result + (last-first)
- Pre:
first
may equalresult
, 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 whichpred
istrue
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
ifpred(*(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
See also
See also
- 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 totrue
.
- Template Parameters:
InputIterator – is a model of Input Iterator, and
InputIterator's
value_type
is convertible toPredicate'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 toOutputIterator's
value_type
.
- Returns:
result + (last-first)
- Pre:
first
may equalresult
, 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 causespred
to betrue
is not copied;new_value
is copied instead.More precisely, for every integer
n
such that0 <= n < last-first
,replace_copy_if
performs the assignment*(result+n) = new_value
ifpred(*(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
See also
- 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 totrue
.
- 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 toPredicate'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 toOutputIterator's
value_type
.
- Returns:
result + (last-first)
- Pre:
first
may equalresult
, but the ranges[first, last)
and[result, result + (last - first))
shall not overlap otherwise.- Pre:
stencil
may equalresult
, 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 causespred
to betrue
is not copied;new_value
is copied instead.More precisely, for every integer
n
such that0 <= n < last-first
,replace_copy_if
performs the assignment*(result+n) = new_value
ifpred(*(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
See also
- 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 totrue
.
- Template Parameters:
InputIterator1 – is a model of Input Iterator.
InputIterator2 – is a model of Input Iterator and
InputIterator2's
value_type
is convertible toPredicate'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 toOutputIterator's
value_type
.
- Returns:
result + (last-first)
- Pre:
first
may equalresult
, but the ranges[first, last)
and[result, result + (last - first))
shall not overlap otherwise.- Pre:
stencil
may equalresult
, 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 finitecomplex
it returns the argument. Forcomplexs
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 returnedcomplex
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 returnedcomplex
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 returnedcomplex
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 returnedcomplex
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 returnedcomplex
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 returnedcomplex
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 returnedcomplex
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 returnedcomplex
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 returnedcomplex
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 returnedcomplex
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 returnedcomplex
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 returnedcomplex
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 returnedcomplex
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 returnedcomplex
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 tostd::complex
. It is functionally identical to it, but can also be used in device code whichstd::complex
currently cannot.- Template Parameters:
T – The type used to hold the real and imaginary parts. Should be
float
ordouble
. 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 firstdevice_vector
of interest.y
The seconddevice_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 adevice_vector
may vary dynamically; memory management is automatic. The memory associated with adevice_vector
resides in the memory accessible to devices.See also
See also
See also
universal_vector
-
template<typename T, typename Alloc>
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 theTHRUST_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 ofthrust::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[] = { 0, 1, 2 }; thrust::for_each(thrust::host, vec, vec + 3, printf_functor()); // 0 1 2 is printed to standard output in some unspecified order
See also
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 theTHRUST_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 asthrust::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 athrust::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 ofthrust::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
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 theTHRUST_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 specializesfor_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
See also
-
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 theTHRUST_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 specializesfor_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
See also
-
static const detail::host_t host#
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 Predicatepred
as an argument and returns a new Adaptable Predicate that represents the negation ofpred
. That is: ifpred
is an object of a type which models Adaptable Predicate, then the the type of the resultnpred
ofnot1(pred)
is also a model of Adaptable Predicate andnpred(x)
always returns the same value as!pred(x)
.See also
See also
- Parameters:
pred – The Adaptable Predicate to negate.
- Template Parameters:
Predicate – is a model of Adaptable Predicate.
- Returns:
A new object,
npred
such thatnpred(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 Predicatepred
as an argument and returns a new Adaptable Binary Predicate that represents the negation ofpred
. That is: ifpred
is an object of a type which models Adaptable Binary Predicate, then the the type of the resultnpred
ofnot2(pred)
is also a model of Adaptable Binary Predicate andnpred(x,y)
always returns the same value as!pred(x,y)
.See also
See also
- 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 thatnpred(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 nestedtypedefs
. Thosetypedefs
are provided by the base classunary_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
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 nestedtypedefs
. Thosetypedefs
are provided by the base classbinary_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
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: iff
is an object of classunary_negate<AdaptablePredicate>
, then there exists an objectpred
of classAdaptablePredicate
such thatf(x)
always returns the same value as!pred(x)
. There is rarely any reason to construct aunary_negate
directly; it is almost always easier to use the helper function not1.See also
-
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: iff
is an object of classbinary_negate<AdaptablePredicate>
, then there exists an objectpred
of classAdaptableBinaryPredicate
such thatf(x,y)
always returns the same value as!pred(x,y)
. There is rarely any reason to construct abinary_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.
-
template<typename T = void>
struct plus# - #include <functional.h>
plus
is a function object. Specifically, it is an Adaptable Binary Function. Iff
is an object of classplus<T>
, andx
andy
are objects of classT
, thenf(x,y)
returnsx+y
.The following code snippet demonstrates how to use
plus
to sum two device_vectors offloats
.#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
- Template Parameters:
T – is a model of Assignable, and if
x
andy
are objects of typeT
, thenx+y
must be defined and must have a return type that is convertible toT
.
-
template<typename T = void>
struct minus# - #include <functional.h>
minus
is a function object. Specifically, it is an Adaptable Binary Function. Iff
is an object of classminus<T>
, andx
andy
are objects of classT
, thenf(x,y)
returnsx-y
.The following code snippet demonstrates how to use
minus
to subtract a device_vector offloats
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
- Template Parameters:
T – is a model of Assignable, and if
x
andy
are objects of typeT
, thenx-y
must be defined and must have a return type that is convertible toT
.
-
template<typename T = void>
struct multiplies# - #include <functional.h>
multiplies
is a function object. Specifically, it is an Adaptable Binary Function. Iff
is an object of classmultiplies<T>
, andx
andy
are objects of classT
, thenf(x,y)
returnsx*y
.The following code snippet demonstrates how to use
multiplies
to multiply two device_vectors offloats
.#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
- Template Parameters:
T – is a model of Assignable, and if
x
andy
are objects of typeT
, thenx*y
must be defined and must have a return type that is convertible toT
.
-
template<typename T = void>
struct divides# - #include <functional.h>
divides
is a function object. Specifically, it is an Adaptable Binary Function. Iff
is an object of classdivides<T>
, andx
andy
are objects of classT
, thenf(x,y)
returnsx/y
.The following code snippet demonstrates how to use
divides
to divide one device_vectors offloats
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
- Template Parameters:
T – is a model of Assignable, and if
x
andy
are objects of typeT
, thenx/y
must be defined and must have a return type that is convertible toT
.
-
template<typename T = void>
struct modulus# - #include <functional.h>
modulus
is a function object. Specifically, it is an Adaptable Binary Function. Iff
is an object of classmodulus<T>
, andx
andy
are objects of classT
, thenf(x,y)
returnsx % y
.The following code snippet demonstrates how to use
modulus
to take the modulus of one device_vectors offloats
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
- Template Parameters:
T – is a model of Assignable, and if
x
andy
are objects of typeT
, thenx % y
must be defined and must have a return type that is convertible toT
.
-
template<typename T = void>
struct negate# - #include <functional.h>
negate
is a function object. Specifically, it is an Adaptable Unary Function. Iff
is an object of classnegate<T>
, andx
is an object of classT
, thenf(x)
returns-x
.The following code snippet demonstrates how to use
negate
to negate the elements of a device_vector offloats
.#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
- Template Parameters:
T – is a model of Assignable, and if
x
is an object of typeT
, then-x
must be defined and must have a return type that is convertible toT
.
-
template<typename T = void>
struct square# - #include <functional.h>
square
is a function object. Specifically, it is an Adaptable Unary Function. Iff
is an object of classsquare<T>
, andx
is an object of classT
, thenf(x)
returnsx*x
.The following code snippet demonstrates how to use
square
to square the elements of a device_vector offloats
.#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
- Template Parameters:
T – is a model of Assignable, and if
x
is an object of typeT
, thenx*x
must be defined and must have a return type that is convertible toT
.
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. Iff
is an object of classequal_to<T>
andx
andy
are objects of classT
, thenf(x,y)
returnstrue
ifx == y
andfalse
otherwise.See also
- 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. Iff
is an object of classnot_equal_to<T>
andx
andy
are objects of classT
, thenf(x,y)
returnstrue
ifx != y
andfalse
otherwise.See also
- 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. Iff
is an object of classgreater<T>
andx
andy
are objects of classT
, thenf(x,y)
returnstrue
ifx > y
andfalse
otherwise.See also
- 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. Iff
is an object of classless<T>
andx
andy
are objects of classT
, thenf(x,y)
returnstrue
ifx < y
andfalse
otherwise.See also
- 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. Iff
is an object of classgreater_equal<T>
andx
andy
are objects of classT
, thenf(x,y)
returnstrue
ifx >= y
andfalse
otherwise.See also
- 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. Iff
is an object of classless_equal<T>
andx
andy
are objects of classT
, thenf(x,y)
returnstrue
ifx <= y
andfalse
otherwise.See also
- 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. Iff
is an object of classlogical_and<T>
andx
andy
are objects of classT
(whereT
is convertible tobool
) thenf(x,y)
returnstrue
if and only if bothx
andy
aretrue
.See also
- 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. Iff
is an object of classlogical_or<T>
andx
andy
are objects of classT
(whereT
is convertible tobool
) thenf(x,y)
returnstrue
if and only if eitherx
ory
aretrue
.See also
- 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. Iff
is an object of classlogical_not<T>
andx
is an object of classT
(whereT
is convertible tobool
) thenf(x)
returnstrue
if and only ifx
isfalse
.The following code snippet demonstrates how to use
logical_not
to transform a device_vector ofbools
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
- 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. Iff
is an object of classbit_and<T>
, andx
andy
are objects of classT
, thenf(x,y)
returnsx&y
.The following code snippet demonstrates how to use
bit_and
to take the bitwise AND of one device_vector ofints
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
- Template Parameters:
T – is a model of Assignable, and if
x
andy
are objects of typeT
, thenx&y
must be defined and must have a return type that is convertible toT
.
-
template<typename T = void>
struct bit_or# - #include <functional.h>
bit_or
is a function object. Specifically, it is an Adaptable Binary Function. Iff
is an object of classbit_and<T>
, andx
andy
are objects of classT
, thenf(x,y)
returnsx|y
.The following code snippet demonstrates how to use
bit_or
to take the bitwise OR of one device_vector ofints
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
- Template Parameters:
T – is a model of Assignable, and if
x
andy
are objects of typeT
, thenx|y
must be defined and must have a return type that is convertible toT
.
-
template<typename T = void>
struct bit_xor# - #include <functional.h>
bit_xor
is a function object. Specifically, it is an Adaptable Binary Function. Iff
is an object of classbit_and<T>
, andx
andy
are objects of classT
, thenf(x,y)
returnsx^y
.The following code snippet demonstrates how to use
bit_xor
to take the bitwise XOR of one device_vector ofints
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
- Template Parameters:
T – is a model of Assignable, and if
x
andy
are objects of typeT
, thenx^y
must be defined and must have a return type that is convertible toT
.
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 argumentx
, and returnsx
.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
- 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. Iff
is an object of classmaximum<T>
andx
andy
are objects of classT
f(x,y)
returnsx
ifx > y
andy
, 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
See also
min
See also
- 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. Iff
is an object of classminimum<T>
andx
andy
are objects of classT
f(x,y)
returnsx
ifx < y
andy
, 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
See also
max
See also
- 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
See also
See also
-
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
See also
See also
-
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
withthrust::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.
-
constexpr thrust::detail::functional::placeholder<0>::type _1#
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 firsthost_vector
of interest.y
The secondhost_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 ahost_vector
may vary dynamically; memory management is automatic. The memory associated with ahost_vector
resides in memory accessible to hosts.See also
See also
universal_vector
-
template<typename T, typename Alloc>
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:
T1 – is a model of Equality Comparable.
T2 – is a model of Equality Comparable.
- Returns:
true
if and only ifx.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:
T1 – is a model of LessThan Comparable.
T2 – is a model of LessThan Comparable.
- Returns:
true
if and only ifx.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:
T1 – is a model of Equality Comparable.
T2 – is a model of Equality Comparable.
- 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:
T1 – is a model of LessThan Comparable.
T2 – is a model of LessThan Comparable.
- Returns:
true
if and only ify < 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:
T1 – is a model of LessThan Comparable.
T2 – is a model of LessThan Comparable.
- 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:
T1 – is a model of LessThan Comparable.
T2 – is a model of LessThan Comparable.
- 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 twopair
s.- 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 froma
andb
.
-
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 ofT1
.T1
’s type is provided bypair::first_type
.T2 – The type of
pair's
second object type. There are no requirements on the type ofT2
.T2
’s type is provided bypair::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 apair's
first_type
orsecond_type
in its nested type,type
.This metafunction returns the type of a
tuple's
N
th element.See also
See also
- 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 returns2
, the number of elements of apair
, in its nested data member,value
.This metafunction returns the number of elements of a
tuple
type of interest.See also
See also
- 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 ofa
tob
and the contents ofb
toa
. 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 atuple
element of interest.The following code snippet demonstrates how to use
get
to print the value of atuple
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
See also
- 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
N
th 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 aconst
reference to atuple
element of interest.The following code snippet demonstrates how to use
get
to print the value of atuple
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
See also
- Parameters:
t – A reference to a
tuple
of interest.- Template Parameters:
N – The index of the element of interest.
- Returns:
A
const
reference tot's
N
th 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 newtuple
object from a single object.- Parameters:
t0 – The object to copy from.
- Returns:
A
tuple
object with a single member which is a copy oft0
.
- 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 newtuple
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 oft0
andt1
.
- template<typename T0> inline __host__ __device__ tuple< T0 & > tie (T0 &t0)
This version of
tie
creates a newtuple
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 tot0
.
- template<typename T0, typename T1> inline __host__ __device__ tuple< T0 &, T1 & > tie (T0 &t0, T1 &t1)
This version of
tie
creates a newtuple
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 tot0
andt1
.
- 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 twotuple
s.- 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 apair's
first_type
orsecond_type
in its nested type,type
.This metafunction returns the type of a
tuple's
N
th element.See also
See also
- 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 returns2
, the number of elements of apair
, in its nested data member,value
.This metafunction returns the number of elements of a
tuple
type of interest.See also
See also
- 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 thetuple
. Consequently, tuples are heterogeneous, fixed-size collections of values. An instantiation oftuple
with two arguments is similar to an instantiation ofpair
with the same two arguments. Individual elements of atuple
may be accessed with theget
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
See also
See also
See also
tuple_element
See also
tuple_size
See also
- Template Parameters:
TN – The type of the
N
tuple
element. Thrust’stuple
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-levelthrust
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 returningr
values successively produced by the base engine and ends by discardingp-r
such values. The engine’s state is the state of its base engine followed by the number of calls tooperator()
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
orminstd_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:
Let
Y = X_{i-s}- X_{i-r} - c
.Set
X_i
toy = T mod m
. Setc
to1
ifY < 0
, otherwise setc
to0
.
This algorithm corresponds to a modular linear function of the form
TA(x_i) = (a * x_i) mod b
, whereb
is of the formm^r - m^s + 1
anda = 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
orranlux48_base
, which are instances ofsubtract_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
.
-
template<typename Engine, size_t p, size_t r>
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 value9901578
.
-
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 value88229545517833
.
-
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 value3535848941
.
-
typedef minstd_rand default_random_engine#
An implementation-defined “default” random number engine.
Note
default_random_engine
is currently an alias forminstd_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 value1043618065
.
-
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 value399268537
.
-
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 value7937952
.
-
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 value192113843633948
.
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
iflhs
is equal torhs
;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
iflhs
is not equal torhs
;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
iflhs
is equal torhs
;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
iflhs
is not equal torhs
;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 returningr
values successively produced by the base engine and ends by discardingp-r
such values. The engine’s state is the state of its base engine followed by the number of calls tooperator()
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
iflhs
is equal torhs
;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
iflhs
is not equal torhs
;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
iflhs
is equal torhs
;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
iflhs
is not equal torhs
;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
iflhs
is equal torhs
;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
iflhs
is not equal torhs
;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
orminstd_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:
Let
Y = X_{i-s}- X_{i-r} - c
.Set
X_i
toy = T mod m
. Setc
to1
ifY < 0
, otherwise setc
to0
.
This algorithm corresponds to a modular linear function of the form
TA(x_i) = (a * x_i) mod b
, whereb
is of the formm^r - m^s + 1
anda = 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
orranlux48_base
, which are instances ofsubtract_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
iflhs
is equal torhs
;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
iflhs
is not equal torhs
;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
iflhs
is equal torhs
;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
iflhs
is not equal torhs
;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
iflhs
is equal torhs
;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
iflhs
is not equal torhs
;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.
-
namespace random#
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-levelthrust
namespace for easy access.-
namespace __THRUST_DEVICE_SYSTEM_NAMESPACE#
-
namespace __THRUST_HOST_SYSTEM_NAMESPACE#
-
namespace __THRUST_DEVICE_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 fromerror_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, whileerror_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
forerrc::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 associatederror_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 thansystem_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 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#
-
enumerator address_family_not_supported#
-
enum errc_t#
-
class error_category#
Functions
-
inline const error_category &generic_category(void)#
Note
The object’s
default_error_condition
andequivalent
virtual functions shall behave as specified for the classerror_category
. The object’sname
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 POSIXerrno
valueposv
, the function shall returnerror_condition(ev,generic_category())
. Otherwise, the function shall returnerror_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 classerror_category
. The object’sname
virtual function shall return a pointer to the string"system"
. The object’sdefault_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
forerrc::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 fromerror_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, whileerror_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 associatederror_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 thansystem_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
-
enumerator address_family_not_supported
-
enum errc_t
-
namespace system#