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_deletedeletes adevice_ptrallocated withdevice_new.See also
See also
- Parameters:
ptr – The
device_ptrto delete, assumed to have been allocated withdevice_new.n – The number of objects to destroy at
ptr. Defaults to1similar todevice_new.
-
inline void device_free(thrust::device_ptr<void> ptr)#
device_freedeallocates memory allocated by the functiondevice_malloc.The following code snippet demonstrates how to use
device_freeto 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_ptrpointing to memory to be deallocated.
-
template<typename T>
inline thrust::device_ptr<T> device_malloc(const std::size_t n)# This version of
device_mallocallocates sequential device storage for new objects of the given type.The following code snippet demonstrates how to use
device_mallocto allocate a range of device memory.#include <thrust/device_malloc.h> #include <thrust/device_free.h> ... // allocate some integers with device_malloc const int N = 100; thrust::device_ptr<int> int_array = thrust::device_malloc<int>(N); // manipulate integers ... // deallocate with device_free thrust::device_free(int_array);
See also
See also
- Parameters:
n – The number of objects of type T to allocate sequentially in device memory.
- Returns:
A
device_ptrto the newly allocated memory.
-
inline thrust::device_ptr<void> device_malloc(const std::size_t n)#
This version of
device_mallocallocates untyped sequential device storage.The following code snippet demonstrates how to use
device_mallocto allocate a range of device memory.#include <thrust/device_malloc.h> #include <thrust/device_free.h> ... // allocate some memory with device_malloc const int N = 100; thrust::device_ptr<void> void_ptr = thrust::device_malloc(N); // manipulate memory ... // deallocate with device_free thrust::device_free(void_ptr);
See also
See also
- Parameters:
n – The number of bytes to allocate sequentially in device memory.
- Returns:
A
device_ptrto the newly allocated memory.
-
template<typename T>
device_ptr<T> device_new(device_ptr<void> p, const size_t n = 1)# device_newimplements the placementnewoperator for types resident in device memory.device_newcallsT’s null constructor on a array of objects in device memory. No memory is allocated by this function.See also
- Parameters:
p – A
device_ptrto a region of device memory into which to construct one or manyTs.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_newimplements the placement new operator for types resident in device memory.device_newcallsT’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_ptrto a region of device memory into which to construct one or manyTs.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_newimplements the new operator for types resident in device memory. It allocates device memory large enough to holdnnew objects of typeT.- Parameters:
n – The number of objects to allocate. Defaults to
1.- Returns:
A
device_ptrto the newly allocated region of device memory.
- template<typename T> __host__ __device__ device_ptr< T > device_pointer_cast (T *ptr)
Create a
device_ptrfrom a raw pointer.- Template Parameters:
T – Any type.
- Parameters:
ptr – A raw pointer to a
Tin device memory.- Pre:
ptrpoints 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_ptrfrom anotherdevice_ptr.- Template Parameters:
T – Any type.
- Parameters:
dptr – A
device_ptrto aT.
- template<typename T> __host__ __device__ void swap (device_reference< T > &x, device_reference< T > &y)
swaps the value of one
device_referencewith another.xThe firstdevice_referenceof interest.yThe seconddevice_referenceof 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
mallocallocates untyped uninitialized storage associated with a given system.The following code snippet demonstrates how to use
mallocto 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:
DerivedPolicymust 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
mallocallocates typed uninitialized storage associated with a given system.The following code snippet demonstrates how to use
mallocto 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
Twhich 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
nelements of typeT; a null pointer otherwise. The pointer must be deallocated withthrust::free.- Pre:
DerivedPolicymust 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_bufferreturns a pointer to storage associated with a given Thrust system sufficient to store up tonobjects of typeT. If not enough storage is available to accomodatenobjects, an implementation may return a smaller buffer. The number of objects the returned buffer can accomodate is also returned.Thrust uses
get_temporary_bufferinternally when allocating temporary storage required by algorithm implementations.The storage allocated with
get_temporary_buffermust be returned to the system withreturn_temporary_buffer.The following code snippet demonstrates how to use
get_temporary_bufferto 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
Tthe storage should accomodate.
- Template Parameters:
DerivedPolicy – The name of the derived execution policy.
- Returns:
A pair
psuch thatp.firstis a pointer to the allocated storage andp.secondis the number of contiguous objects of typeTthat the storage can accomodate. If no storage can be allocated,p.firstif no storage can be obtained. The storage must be returned to the system usingreturn_temporary_buffer.- Pre:
DerivedPolicymust 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)
freedeallocates the storage previously allocated bythrust::malloc.The following code snippet demonstrates how to use
freeto 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. Ifptris null,freedoes nothing.
- Template Parameters:
DerivedPolicy – The name of the derived execution policy.
- Pre:
ptrshall 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_bufferdeallocates storage associated with a given Thrust system previously allocated byget_temporary_buffer.Thrust uses
return_temporary_bufferinternally when deallocating temporary storage required by algorithm implementations.The following code snippet demonstrates how to use
return_temporary_bufferto 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. Ifptris null,return_temporary_bufferdoes nothing.n –
- Template Parameters:
DerivedPolicy – The name of the derived execution policy.
- Pre:
pshall 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_castcreates 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_castcreates 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 forconstreferences, 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_castcreates 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 forconstreferences, 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_referenceacts as a reference-like object to an object stored in device memory.device_referenceis not intended to be used directly; rather, this type is the result of deferencing adevice_ptr. Similarly, taking the address of adevice_referenceyields adevice_ptr.device_referencemay often be used from host code in place of operations defined on its associatedvalue_type. For example, whendevice_referencerefers 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_thirteenin 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_referencein the previous example, because one is returned bydevice_vector'sbracket operator. A more natural way to print the value of adevice_vectorelement 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_referenceobjects 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'sxmember:#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_referencecannot directly be used in place of its referent object occurs when passing them as parameters to functions likeprintfwhich have varargs parameters. Because varargs parameters must be Plain Old Data, adevice_referenceto 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_ptris a pointer-like object which points to an object that resides in memory associated with the device system.device_ptrhas pointer semantics: it may be dereferenced safely from anywhere, including the host, and may be manipulated with pointer arithmetic.device_ptrcan 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_ptrmay be obtained viagetmember function or the raw_pointer_cast free function.Algorithms operating on
device_ptrtypes will automatically be dispatched to the device system.See also
See also
See also
See also
See also
See also
Note
device_ptris 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_allocatoris a device memory allocator that employs thedevice_mallocfunction for allocation.device_malloc_allocatoris deprecated in favor ofthrust::mrmemory resource-based allocators.See also
See also
See also
-
template<typename T>
class device_new_allocator# - #include <device_new_allocator.h>
device_new_allocatoris a device memory allocator that employs thedevice_newfunction for allocation.See also
See also
-
template<typename T, class MR>
class allocator : private mr::validator<MR># - #include <allocator.h>
An
mr::allocatoris 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::allocatoris 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_resourceand 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_resourceand must befinal(in C++11 and beyond).
Typedefs
-
template<typename T>
using universal_ptr = thrust::system::__THRUST_DEVICE_SYSTEM_NAMESPACE::universal_pointer<T># universal_ptrstores 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_ptrhas pointer semantics: it may be dereferenced safely from both hosts and devices and may be manipulated with pointer arithmetic.universal_ptrcan be created withuniversal_allocatoror by explicitly calling its constructor with a raw pointer.The raw pointer encapsulated by a
universal_ptrmay be obtained by either itsgetmethod or theraw_pointer_castfree function.See also
host_ptr For the documentation of the complete interface which is shared by
universal_ptr.See also
Note
universal_ptris 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, usingBookkeeperfor management of that cached and pooled memory, allowing to cache portions of memory inaccessible from the host.On a typical memory resource, calls to
allocateanddeallocateactually 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 thatUpstreamallocates 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,Upstreamcan 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_resourceis 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
allocateanddeallocateactually 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 iteratoriby the distancen. Ifn > 0it is equivalent to executing++intimes, and ifn < 0it is equivalent to executingintimes. Ifn == 0, the call has no effect.The following code snippet demonstrates how to use
advanceto 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'sdistance type.
- Pre:
nshall 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)
distancefinds the distance betweenfirstandlast, i.e. the number of times thatfirstmust be incremented until it is equal tolast.The following code snippet demonstrates how to use
distanceto 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
InputIteratormeets the requirements of random access iterator,lastshall be reachable fromfirstorfirstshall be reachable fromlast; otherwise,lastshall 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_iteratorcreates aconstant_iteratorfrom values given for both value and index. The type ofconstant_iteratormay be inferred by the compiler from the types of its parameters.See also
- Parameters:
x – The value of the returned
constant_iterator'sconstant value.i – The index of the returned
constant_iteratorwithin a sequence. The type of this parameter defaults toint. In the default case, the value of this parameter is0.
- Returns:
A new
constant_iteratorwith 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_iteratorcreates aconstant_iteratorusing only a parameter for the desired constant value. The value of the returnedconstant_iterator'sindex is set to0.See also
- Parameters:
x – The value of the returned
constant_iterator'sconstant value.- Returns:
A new
constant_iteratorwith constant value equal toxand index equal to0.
- template<typename Incrementable> inline __host__ __device__ counting_iterator< Incrementable > make_counting_iterator (Incrementable x)
make_counting_iteratorcreates acounting_iteratorusing an initial value for itsIncrementablecounter.- Parameters:
x – The initial value of the new
counting_iterator'scounter.- Returns:
A new
counting_iteratorwhose 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_iteratorcreates adiscard_iteratorfrom an optional index parameter.See also
- Parameters:
i – The index of the returned
discard_iteratorwithin a range. In the default case, the value of this parameter is0.- Returns:
A new
discard_iteratorwith index as given byi.
- template<typename ElementIterator, typename IndexIterator> __host__ __device__ permutation_iterator< ElementIterator, IndexIterator > make_permutation_iterator (ElementIterator e, IndexIterator i)
make_permutation_iteratorcreates apermutation_iteratorfrom anElementIteratorpointing to a range of elements to “permute” and anIndexIteratorpointing to a range of indices defining an indexing scheme on the values.See also
- Parameters:
e – An
ElementIteratorpointing to a range of values.i – An
IndexIteratorpointing to an indexing scheme to use one.
- Returns:
A new
permutation_iteratorwhich permutes the rangeebyi.
- template<typename BidirectionalIterator> __host__ __device__ reverse_iterator< BidirectionalIterator > make_reverse_iterator (BidirectionalIterator x)
make_reverse_iteratorcreates areverse_iteratorfrom aBidirectionalIteratorpointing to a range of elements to reverse.- Parameters:
x – A
BidirectionalIteratorpointing to a range to reverse.- Returns:
A new
reverse_iteratorwhich 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_iteratorcreates atransform_input_output_iteratorfrom anIteratoraInputFunctionand aOutputFunctionSee also
- Parameters:
io – An
Iteratorpointing to where the input toInputFunctionwill be read from and the result ofOutputFunctionwill be written toinput_function – An
InputFunctionto be executed on values read from the iteratoroutput_function – An
OutputFunctionto 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_iteratorcreates atransform_iteratorfrom anIteratorandAdaptableUnaryFunction.See also
- Parameters:
it – The
Iteratorpointing to the input range of the newly createdtransform_iterator.fun – The
AdaptableUnaryFunctionused to transform the range pointed to byitin the newly createdtransform_iterator.
- Returns:
A new
transform_iteratorwhich transforms the range atitbyfun.
- template<typename UnaryFunction, typename OutputIterator> transform_output_iterator< UnaryFunction, OutputIterator > __host__ __device__ make_transform_output_iterator (OutputIterator out, UnaryFunction fun)
make_transform_output_iteratorcreates atransform_output_iteratorfrom anOutputIteratorandUnaryFunction.See also
- Parameters:
out – The
OutputIteratorpointing to the output range of the newly createdtransform_output_iteratorfun – The
UnaryFunctiontransform the object before assigning it tooutby 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_iteratorcreates azip_iteratorfrom atupleof iterators.See also
- Parameters:
t – The
tupleof iterators to copy.- Returns:
A newly created
zip_iteratorwhich zips the iterators encapsulated int.
- template<typename... Iterators> inline __host__ __device__ zip_iterator< thrust::tuple< Iterators... > > make_zip_iterator (Iterators... its)
make_zip_iteratorcreates azip_iteratorfrom iterators.See also
- Parameters:
its – The iterators to copy.
- Returns:
A newly created
zip_iteratorwhich 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_iteratoris 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_iteratorsaves both memory capacity and bandwidth.The following code snippet demonstrates how to create a
constant_iteratorwhosevalue_typeisintand 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_iteratorwith thethrust::transformfunction to increment all elements of a sequence by the same value. We will create a temporaryconstant_iteratorwith the functionmake_constant_iteratorfunction 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_iteratoris 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_iteratorsaves memory capacity and bandwidth.The following code snippet demonstrates how to create a
counting_iteratorwhosevalue_typeisintand 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_iteratorwith thethrust::copy_iffunction to compute the indices of the non-zero elements of adevice_vector. In this example, we use themake_counting_iteratorfunction 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_iteratoris 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_iteratormay 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_iteratorto 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_adaptoris 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_adaptordirectly.To see how to use
iterator_adaptorto 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'stemplate parameters are optional. When omitted, or when the user specifiesthrust::use_defaultin its place,iterator_adaptorwill use a default type inferred fromBase.iterator_adaptor'sfunctionality 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_adaptoris a powerful tool for creating custom iterators directly. However, the large set of iterator semantics which must be satisfied for algorithm compatibility can makeiterator_adaptordifficult 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_facadeis 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_facadedefines 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_facadeis too primitive a tool to use for defining novel iterators. In these cases,iterator_adaptoror a specific fancy iterator should be used instead.iterator_facade'sfunctionality 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'sarithmetic operator free functions exist with the usual meanings but are omitted here for brevity.
-
class iterator_core_access#
- #include <iterator_facade.h>
iterator_core_accessis the class which user iterator types derived fromthrust::iterator_adaptororthrust::iterator_facademust 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_iteratoris an iterator which represents a pointer into a reordered view of a given range.permutation_iteratoris 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
Von which the “permutation” will be appliedthe reindexing scheme that defines how the elements of
Vwill be permuted.
Note that
permutation_iteratoris 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_iteratoronly 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_iteratoris completely defined by means of the past-the-end iterator to the indices.The following code snippet demonstrates how to create a
permutation_iteratorwhich 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_iteratoris an iterator which represents a pointer into a reversed view of a given range. In this way,reverse_iteratorallows backwards iteration through a bidirectional input range.It is important to note that although
reverse_iteratoris constructed from a given iterator, it points to the element preceding it. In this way, the past-the-endreverse_iteratorof a given range points to the element preceding the first element of the input range. By the same token, the firstreverse_iteratorof 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_iteratorwhich 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_vectorhave 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_iteratoris 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_iteratorwhich 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_iteratoris 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_iteratorfacilitates 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_iteratorwhich represents the result ofsqrtfapplied 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_iteratorwith thethrust::reducefunction to compute the sum of squares of a sequence. We will create temporarytransform_iteratorswith themake_transform_iteratorfunction 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_rootandsquare) inherits fromthrust::unary_function. Inheriting fromthrust::unary_functionensures that a functor is a validAdaptableUnaryFunctionand provides all the necessarytypedefdeclarations. Thetransform_iteratorcan also be applied to aUnaryFunctionthat does not inherit fromthrust::unary_functionusing an optional template argument. The following example illustrates how to use the third template argument to specify theresult_typeof 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_iteratoris 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_iteratorfacilitates 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_iteratorwhich appliessqrtfto 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_iteratoris an iterator which represents a pointer into a range oftupleswhose elements are themselves taken from atupleof 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_iteratoralso 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_iteratorwhich 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_iteratorcan be complex. The next code example demonstrates how to use themake_zip_iteratorfunction with themake_tuplefunction to avoid explicitly specifying the type of thezip_iterator. This example shows how to usezip_iteratorto 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_tagis 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_tagis 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_tagis 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_tagis 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_tagis 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_tagis 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_tagis 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_tagis 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_tagis 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_tagis 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)
findreturns the first iteratoriin the range[first, last)such that*i == valueorlastif 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'svalue_typeis equality comparable to typeT.T – is a model of EqualityComparable.
- Returns:
The first iterator
isuch that*i == valueorlast.
-
template<typename InputIterator, typename T>
InputIterator find(InputIterator first, InputIterator last, const T &value)# findreturns the first iteratoriin the range[first, last)such that*i == valueorlastif 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'svalue_typeis equality comparable to typeT.T – is a model of EqualityComparable.
- Returns:
The first iterator
isuch that*i == valueorlast.
- 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_ifreturns the first iteratoriin the range[first, last)such thatpred(*i)istrueorlastif 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
isuch thatpred(*i)istrue, orlast.
-
template<typename InputIterator, typename Predicate>
InputIterator find_if(InputIterator first, InputIterator last, Predicate pred)# find_ifreturns the first iteratoriin the range[first, last)such thatpred(*i)istrueorlastif 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
isuch 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_notreturns the first iteratoriin the range[first, last)such thatpred(*i)isfalseorlastif 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
isuch thatpred(*i)isfalse, orlast.
-
template<typename InputIterator, typename Predicate>
InputIterator find_if_not(InputIterator first, InputIterator last, Predicate pred)# find_if_notreturns the first iteratoriin the range[first, last)such thatpred(*i)isfalseorlastif 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
isuch 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)
mismatchfinds the first position where the two ranges[first1, last1)and[first2, first2 + (last1 - first1))differ. The two versions ofmismatchuse different tests for whether elements differ.This version of
mismatchfinds the first iteratoriin[first1, last1)such that*i == *(first2 + (i - first1))isfalse. The return value is apairwhose first element isiand whose second element is*(first2 + (i - first1)). If no such iteratoriexists, the return value is apairwhose first element islast1and 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'svalue_typeis equality comparable toInputIterator2'svalue_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)# mismatchfinds the first position where the two ranges[first1, last1)and[first2, first2 + (last1 - first1))differ. The two versions ofmismatchuse different tests for whether elements differ.This version of
mismatchfinds the first iteratoriin[first1, last1)such that*i == *(first2 + (i - first1))isfalse. The return value is apairwhose first element isiand whose second element is*(first2 + (i - first1)). If no such iteratoriexists, the return value is apairwhose first element islast1and 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'svalue_typeis equality comparable toInputIterator2'svalue_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)
mismatchfinds the first position where the two ranges[first1, last1)and[first2, first2 + (last1 - first1))differ. The two versions ofmismatchuse different tests for whether elements differ.This version of
mismatchfinds the first iteratoriin[first1, last1)such thatpred(*i, *(first2 + (i - first1))isfalse. The return value is apairwhose first element isiand whose second element is*(first2 + (i - first1)). If no such iteratoriexists, the return value is apairwhose first element islast1and 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)# mismatchfinds the first position where the two ranges[first1, last1)and[first2, first2 + (last1 - first1))differ. The two versions ofmismatchuse different tests for whether elements differ.This version of
mismatchfinds the first iteratoriin[first1, last1)such thatpred(*i, *(first2 + (i - first1))isfalse. The return value is apairwhose first element isiand whose second element is*(first2 + (i - first1)). If no such iteratoriexists, the return value is apairwhose first element islast1and 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_pointreturns an iterator pointing to the end of the true partition of a partitioned range.partition_pointrequires the input range[first,last)to be a partition; that is, all elements which satisfypredshall 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_pointis not redundant withfind_if_not.partition_point'sprecondition 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'svalue_typeis convertible toPredicate'sargument_type.Predicate – is a model of Predicate.
- Returns:
An iterator
midsuch 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_pointreturns an iterator pointing to the end of the true partition of a partitioned range.partition_pointrequires the input range[first,last)to be a partition; that is, all elements which satisfypredshall 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_pointis not redundant withfind_if_not.partition_point'sprecondition 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'svalue_typeis convertible toPredicate'sargument_type.Predicate – is a model of Predicate.
- Returns:
An iterator
midsuch 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_boundis 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_boundusesoperator<for comparison and returns the furthermost iteratoriin[first, last)such that, for every iteratorjin[first, i),*j < value.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
lower_boundto search for values in a ordered range using thethrust::deviceexecution 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_boundis 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_boundusesoperator<for comparison and returns the furthermost iteratoriin[first, last)such that, for every iteratorjin[first, i),*j < value.The following code snippet demonstrates how to use
lower_boundto 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_boundis 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_bounduses function objectcompfor comparison and returns the furthermost iteratoriin[first, last)such that, for every iteratorjin[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_boundto search for values in a ordered range using thethrust::deviceexecution 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'svalue_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_boundis 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_bounduses function objectcompfor comparison and returns the furthermost iteratoriin[first, last)such that, for every iteratorjin[first, i),comp(*j, value)istrue.The following code snippet demonstrates how to use
lower_boundto 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'svalue_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_boundis 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_boundusesoperator<for comparison and returns the furthermost iteratoriin[first, last)such that, for every iteratorjin[first, i),value < *jisfalse.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
upper_boundto search for values in a ordered range using thethrust::deviceexecution 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 < *iisfalse.
-
template<class ForwardIterator, class LessThanComparable>
ForwardIterator upper_bound(ForwardIterator first, ForwardIterator last, const LessThanComparable &value)# upper_boundis 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_boundusesoperator<for comparison and returns the furthermost iteratoriin[first, last)such that, for every iteratorjin[first, i),value < *jisfalse.The following code snippet demonstrates how to use
upper_boundto 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 < *iisfalse.
- 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_boundis 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_bounduses function objectcompfor comparison and returns the furthermost iteratoriin[first, last)such that, for every iteratorjin[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_boundto search for values in a ordered range using thethrust::deviceexecution 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'svalue_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_boundis 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_bounduses function objectcompfor comparison and returns the furthermost iteratoriin[first, last)such that, for every iteratorjin[first, i),comp(value, *j)isfalse.The following code snippet demonstrates how to use
upper_boundto 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'svalue_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_searchis a version of binary search: it attempts to find the element value in an ordered range[first, last). It returnstrueif an element that is equivalent tovalueis present in[first, last)andfalseif no such element exists. Specifically, this version returnstrueif and only if there exists an iteratoriin[first, last)such that*i < valueandvalue < *iare bothfalse.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
binary_searchto search for values in a ordered range using thethrust::deviceexecution 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:
trueif an equivalent element exists in[first, last), otherwisefalse.
-
template<class ForwardIterator, class LessThanComparable>
bool binary_search(ForwardIterator first, ForwardIterator last, const LessThanComparable &value)# binary_searchis a version of binary search: it attempts to find the element value in an ordered range[first, last). It returnstrueif an element that is equivalent tovalueis present in[first, last)andfalseif no such element exists. Specifically, this version returnstrueif and only if there exists an iteratoriin[first, last)such that*i < valueandvalue < *iare bothfalse.The following code snippet demonstrates how to use
binary_searchto 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:
trueif 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_searchis a version of binary search: it attempts to find the element value in an ordered range[first, last). It returnstrueif an element that is equivalent tovalueis present in[first, last)andfalseif no such element exists. Specifically, this version returnstrueif and only if there exists an iteratoriin[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_searchto search for values in a ordered range using thethrust::deviceexecution 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'svalue_type.StrictWeakOrdering – is a model of Strict Weak Ordering.
- Returns:
trueif 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_searchis a version of binary search: it attempts to find the element value in an ordered range[first, last). It returnstrueif an element that is equivalent tovalueis present in[first, last)andfalseif no such element exists. Specifically, this version returnstrueif and only if there exists an iteratoriin[first, last)such thatcomp(*i, value)andcomp(value, *i)are bothfalse.The following code snippet demonstrates how to use
binary_searchto 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'svalue_type.StrictWeakOrdering – is a model of Strict Weak Ordering.
- Returns:
trueif 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_rangeis a version of binary search: it attempts to find the element value in an ordered range[first, last). The value returned byequal_rangeis essentially a combination of the values returned bylower_boundandupper_bound:it returns apairof iteratorsiandjsuch thatiis the first position where value could be inserted without violating the ordering andjis 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_rangereturns apairof iterators[i, j), whereiis the furthermost iterator in[first, last)such that, for every iteratorkin[first, i),*k < value.jis the furthermost iterator in[first, last)such that, for every iteratorkin[first, j),value < *kisfalse. For every iteratorkin[i, j), neithervalue < *knor*k < valueistrue.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
equal_rangeto search for values in a ordered range using thethrust::deviceexecution 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
pairof 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_rangeis a version of binary search: it attempts to find the element value in an ordered range[first, last). The value returned byequal_rangeis essentially a combination of the values returned bylower_boundandupper_bound:it returns apairof iteratorsiandjsuch thatiis the first position where value could be inserted without violating the ordering andjis 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_rangereturns apairof iterators[i, j), whereiis the furthermost iterator in[first, last)such that, for every iteratorkin[first, i),*k < value.jis the furthermost iterator in[first, last)such that, for every iteratorkin[first, j),value < *kisfalse. For every iteratorkin[i, j), neithervalue < *knor*k < valueistrue.The following code snippet demonstrates how to use
equal_rangeto 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
pairof 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_rangeis a version of binary search: it attempts to find the element value in an ordered range[first, last). The value returned byequal_rangeis essentially a combination of the values returned bylower_boundandupper_bound:it returns apairof iteratorsiandjsuch thatiis the first position where value could be inserted without violating the ordering andjis 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_rangereturns apairof iterators[i, j).iis the furthermost iterator in[first, last)such that, for every iteratorkin[first, i),comp(*k, value)istrue.jis the furthermost iterator in[first, last)such that, for every iteratorkin[first, last),comp(value, *k)isfalse. For every iteratorkin[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_rangeto search for values in a ordered range using thethrust::deviceexecution 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'svalue_type.StrictWeakOrdering – is a model of Strict Weak Ordering.
- Returns:
A
pairof 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_rangeis a version of binary search: it attempts to find the element value in an ordered range[first, last). The value returned byequal_rangeis essentially a combination of the values returned bylower_boundandupper_bound:it returns apairof iteratorsiandjsuch thatiis the first position where value could be inserted without violating the ordering andjis 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_rangereturns apairof iterators[i, j).iis the furthermost iterator in[first, last)such that, for every iteratorkin[first, i),comp(*k, value)istrue.jis the furthermost iterator in[first, last)such that, for every iteratorkin[first, last),comp(value, *k)isfalse. For every iteratorkin[i, j), neithercomp(value, *k)norcomp(*k, value)istrue.The following code snippet demonstrates how to use
equal_rangeto 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'svalue_type.StrictWeakOrdering – is a model of Strict Weak Ordering.
- Returns:
A
pairof 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_boundis a vectorized version of binary search: for each iteratorvin[values_first, values_last)it attempts to find the value*vin 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_boundto search for multiple values in a ordered range using thethrust::deviceexecution 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'svalue_typeis LessThanComparable.OutputIterator – is a model of Output Iterator. and
ForwardIterator'sdifference_type is convertible toOutputIterator'svalue_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_boundis a vectorized version of binary search: for each iteratorvin[values_first, values_last)it attempts to find the value*vin 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_boundto 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'svalue_typeis LessThanComparable.OutputIterator – is a model of Output Iterator. and
ForwardIterator'sdifference_type is convertible toOutputIterator'svalue_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_boundis a vectorized version of binary search: for each iteratorvin[values_first, values_last)it attempts to find the value*vin 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_bounduses function objectcompfor comparison.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
lower_boundto 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'svalue_typeis comparable toForwardIterator'svalue_type.OutputIterator – is a model of Output Iterator. and
ForwardIterator'sdifference_type is convertible toOutputIterator'svalue_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_boundis a vectorized version of binary search: for each iteratorvin[values_first, values_last)it attempts to find the value*vin 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_bounduses function objectcompfor comparison.The following code snippet demonstrates how to use
lower_boundto 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'svalue_typeis comparable toForwardIterator'svalue_type.OutputIterator – is a model of Output Iterator. and
ForwardIterator'sdifference_type is convertible toOutputIterator'svalue_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_boundis a vectorized version of binary search: for each iteratorvin[values_first, values_last)it attempts to find the value*vin 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_boundto search for multiple values in a ordered range using thethrust::deviceexecution 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'svalue_typeis LessThanComparable.OutputIterator – is a model of Output Iterator. and
ForwardIterator'sdifference_type is convertible toOutputIterator'svalue_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_boundis a vectorized version of binary search: for each iteratorvin[values_first, values_last)it attempts to find the value*vin 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_boundto 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'svalue_typeis LessThanComparable.OutputIterator – is a model of Output Iterator. and
ForwardIterator'sdifference_type is convertible toOutputIterator'svalue_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_boundis a vectorized version of binary search: for each iteratorvin[values_first, values_last)it attempts to find the value*vin 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_bounduses function objectcompfor comparison.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
upper_boundto search for multiple values in a ordered range using thethrust::deviceexecution 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'svalue_typeis comparable toForwardIterator'svalue_type.OutputIterator – is a model of Output Iterator. and
ForwardIterator'sdifference_type is convertible toOutputIterator'svalue_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_boundis a vectorized version of binary search: for each iteratorvin[values_first, values_last)it attempts to find the value*vin 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_bounduses function objectcompfor comparison.The following code snippet demonstrates how to use
upper_boundto 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'svalue_typeis comparable toForwardIterator'svalue_type.OutputIterator – is a model of Output Iterator. and
ForwardIterator'sdifference_type is convertible toOutputIterator'svalue_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_searchis a vectorized version of binary search: for each iteratorvin[values_first, values_last)it attempts to find the value*vin an ordered range[first, last). It returnstrueif an element that is equivalent tovalueis present in[first, last)andfalseif no such element exists.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
binary_searchto search for multiple values in a ordered range using thethrust::deviceexecution 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'svalue_typeis LessThanComparable.OutputIterator – is a model of Output Iterator. and bool is convertible to
OutputIterator'svalue_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_searchis a vectorized version of binary search: for each iteratorvin[values_first, values_last)it attempts to find the value*vin an ordered range[first, last). It returnstrueif an element that is equivalent tovalueis present in[first, last)andfalseif no such element exists.The following code snippet demonstrates how to use
binary_searchto 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'svalue_typeis LessThanComparable.OutputIterator – is a model of Output Iterator. and bool is convertible to
OutputIterator'svalue_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_searchis a vectorized version of binary search: for each iteratorvin[values_first, values_last)it attempts to find the value*vin an ordered range[first, last). It returnstrueif an element that is equivalent tovalueis present in[first, last)andfalseif no such element exists. This version ofbinary_searchuses function objectcompfor comparison.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
binary_searchto search for multiple values in a ordered range using thethrust::deviceexecution 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'svalue_typeis LessThanComparable.OutputIterator – is a model of Output Iterator. and bool is convertible to
OutputIterator'svalue_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_searchis a vectorized version of binary search: for each iteratorvin[values_first, values_last)it attempts to find the value*vin an ordered range[first, last). It returnstrueif an element that is equivalent tovalueis present in[first, last)andfalseif no such element exists. This version ofbinary_searchuses function objectcompfor comparison.The following code snippet demonstrates how to use
binary_searchto 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'svalue_typeis LessThanComparable.OutputIterator – is a model of Output Iterator. and bool is convertible to
OutputIterator'svalue_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)
copycopies 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 integernfrom0tolast-first,copyperforms the assignment *(result+n) = *(first+n). Unlikestd::copy,copyoffers no guarantee on order of operation. As a result, callingcopywith 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
copyto copy from one range to another using thethrust::deviceparallelization 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'svalue_typemust be convertible toOutputIterator'svalue_type.OutputIterator – must be a model of Output Iterator.
- Returns:
The end of the destination sequence.
- Pre:
resultmay be equal tofirst, butresultshall 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_ncopies 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 integerifrom0ton,copyperforms the assignment *(result+i) = *(first+i). Unlikestd::copy_n,copy_noffers no guarantee on order of operation. As a result, callingcopy_nwith 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
copyto copy from one range to another using thethrust::deviceparallelization 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'svalue_typemust be convertible toOutputIterator'svalue_type.Size – is an integral type.
OutputIterator – must be a model of Output Iterator.
- Returns:
The end of the destination range.
- Pre:
resultmay be equal tofirst, butresultshall not be in the range[first, first + n)otherwise.
-
template<typename InputIterator, typename OutputIterator>
OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result)# copycopies 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 integernfrom0tolast-first,copyperforms the assignment *(result+n) = *(first+n). Unlikestd::copy,copyoffers no guarantee on order of operation. As a result, callingcopywith overlapping source and destination ranges has undefined behavior.The return value is
result+ (last-first).The following code snippet demonstrates how to use
copyto 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'svalue_typemust be convertible toOutputIterator'svalue_type.OutputIterator – must be a model of Output Iterator.
- Returns:
The end of the destination sequence.
- Pre:
resultmay be equal tofirst, butresultshall 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_ncopies 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 integerifrom0ton,copyperforms the assignment *(result+i) = *(first+i). Unlikestd::copy_n,copy_noffers no guarantee on order of operation. As a result, callingcopy_nwith overlapping source and destination ranges has undefined behavior.The return value is
result+n.The following code snippet demonstrates how to use
copyto 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'svalue_typemust be convertible toOutputIterator'svalue_type.Size – is an integral type.
OutputIterator – must be a model of Output Iterator.
- Returns:
The end of the destination range.
- Pre:
resultmay be equal tofirst, butresultshall 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_rangesswaps each of the elements in the range[first1, last1)with the corresponding element in the range[first2, first2 + (last1 - first1)). That is, for each integernsuch 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_rangesto swap the contents of twothrust::device_vectorsusing thethrust::deviceexecution 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'svalue_typemust be convertible toForwardIterator2'svalue_type.ForwardIterator2 – is a model of Forward Iterator, and
ForwardIterator2'svalue_typemust be convertible toForwardIterator1'svalue_type.
- Returns:
An iterator pointing to one position past the last element of the second sequence to swap.
- Pre:
first1may 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_rangesswaps each of the elements in the range[first1, last1)with the corresponding element in the range[first2, first2 + (last1 - first1)). That is, for each integernsuch 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_rangesto 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'svalue_typemust be convertible toForwardIterator2'svalue_type.ForwardIterator2 – is a model of Forward Iterator, and
ForwardIterator2'svalue_typemust be convertible toForwardIterator1'svalue_type.
- Returns:
An iterator pointing to one position past the last element of the second sequence to swap.
- Pre:
first1may 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_newallocates 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_copycreates a copy of[first, last)in that range. That is, for each iteratoriin the input,uninitialized_copycreates a copy of*iin the location pointed to by the corresponding iterator in the output range byForwardIterator'svalue_type'scopy 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_copyto initialize a range of uninitialized memory using thethrust::deviceexecution 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,
ForwardIteratoris mutable, andForwardIterator'svalue_typehas a constructor that takes a single argument whose type isInputIterator'svalue_type.
- Returns:
An iterator pointing to the last element of the output range.
- Pre:
firstmay 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_newallocates 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_copycreates a copy of[first, last)in that range. That is, for each iteratoriin the input,uninitialized_copycreates a copy of*iin the location pointed to by the corresponding iterator in the output range byForwardIterator'svalue_type'scopy constructor with *i as its argument.The following code snippet demonstrates how to use
uninitialized_copyto 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,
ForwardIteratoris mutable, andForwardIterator'svalue_typehas a constructor that takes a single argument whose type isInputIterator'svalue_type.
- Returns:
An iterator pointing to the last element of the output range.
- Pre:
firstmay 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_newallocates 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_ncreates a copy of[first, first + n)in that range. That is, for each iteratoriin the input,uninitialized_copy_ncreates a copy of*iin the location pointed to by the corresponding iterator in the output range byInputIterator'svalue_type'scopy 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_copyto initialize a range of uninitialized memory using thethrust::deviceexecution 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,
ForwardIteratoris mutable, andForwardIterator'svalue_typehas a constructor that takes a single argument whose type isInputIterator'svalue_type.
- Returns:
An iterator pointing to the last element of the output range.
- Pre:
firstmay 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_newallocates 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_ncreates a copy of[first, first + n)in that range. That is, for each iteratoriin the input,uninitialized_copy_ncreates a copy of*iin the location pointed to by the corresponding iterator in the output range byInputIterator'svalue_type'scopy constructor with *i as its argument.The following code snippet demonstrates how to use
uninitialized_copyto 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,
ForwardIteratoris mutable, andForwardIterator'svalue_typehas a constructor that takes a single argument whose type isInputIterator'svalue_type.
- Returns:
An iterator pointing to the last element of the output range.
- Pre:
firstmay 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)
gathercopies elements from a source array into a destination range according to a map. For each input iteratoriin the range[map_first, map_last), the valueinput_first[*i]is assigned to*(result + (i - map_first)).RandomAccessIteratormust permit random access.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
gatherto reorder a range using thethrust::deviceexecution policy for parallelization:Remark
gatheris 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'svalue_typemust be convertible toRandomAccessIterator'sdifference_type.RandomAccessIterator – must be a model of Random Access Iterator and
RandomAccessIterator'svalue_typemust be convertible toOutputIterator'svalue_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)# gathercopies elements from a source array into a destination range according to a map. For each input iteratoriin the range[map_first, map_last), the valueinput_first[*i]is assigned to*(result + (i - map_first)).RandomAccessIteratormust permit random access.The following code snippet demonstrates how to use
gatherto reorder a range.Remark
gatheris 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'svalue_typemust be convertible toRandomAccessIterator'sdifference_type.RandomAccessIterator – must be a model of Random Access Iterator and
RandomAccessIterator'svalue_typemust be convertible toOutputIterator'svalue_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_ifconditionally copies elements from a source array into a destination range according to a map. For each input iteratoriin 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)).RandomAccessIteratormust permit random access.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
gather_ifto gather selected values from an input range using thethrust::deviceexecution policy:Remark
gather_ifis 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'svalue_typemust be convertible toRandomAccessIterator'sdifference_type.InputIterator2 – must be a model of Input Iterator and
InputIterator2'svalue_typemust be convertible tobool.RandomAccessIterator – must be a model of Random Access iterator and
RandomAccessIterator'svalue_typemust be convertible toOutputIterator'svalue_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_ifconditionally copies elements from a source array into a destination range according to a map. For each input iteratoriin 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)).RandomAccessIteratormust permit random access.The following code snippet demonstrates how to use
gather_ifto gather selected values from an input range.Remark
gather_ifis 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'svalue_typemust be convertible toRandomAccessIterator'sdifference_type.InputIterator2 – must be a model of Input Iterator and
InputIterator2'svalue_typemust be convertible tobool.RandomAccessIterator – must be a model of Random Access iterator and
RandomAccessIterator'svalue_typemust be convertible toOutputIterator'svalue_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_ifconditionally copies elements from a source array into a destination range according to a map. For each input iteratoriin 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)).RandomAccessIteratormust permit random access.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
gather_ifto gather selected values from an input range based on an arbitrary selection function using thethrust::deviceexecution policy for parallelization:Remark
gather_ifis 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'svalue_typemust be convertible toRandomAccessIterator'sdifference_type.InputIterator2 – must be a model of Input Iterator and
InputIterator2'svalue_typemust be convertible toPredicate'sargument_type.RandomAccessIterator – must be a model of Random Access iterator and
RandomAccessIterator'svalue_typemust be convertible toOutputIterator'svalue_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_ifconditionally copies elements from a source array into a destination range according to a map. For each input iteratoriin 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)).RandomAccessIteratormust permit random access.The following code snippet demonstrates how to use
gather_ifto gather selected values from an input range based on an arbitrary selection function.Remark
gather_ifis 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'svalue_typemust be convertible toRandomAccessIterator'sdifference_type.InputIterator2 – must be a model of Input Iterator and
InputIterator2'svalue_typemust be convertible toPredicate'sargument_type.RandomAccessIterator – must be a model of Random Access iterator and
RandomAccessIterator'svalue_typemust be convertible toOutputIterator'svalue_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)
scattercopies elements from a source range into an output array according to a map. For each iteratoriin the range [first,last), the value*iis 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
scatterto reorder a range using thethrust::deviceexecution 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
scatteris 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'svalue_typemust be convertible toRandomAccessIterator'svalue_type.InputIterator2 – must be a model of Input Iterator and
InputIterator2'svalue_typemust be convertible toRandomAccessIterator'sdifference_type.RandomAccessIterator – must be a model of Random Access iterator.
- Pre:
The iterator
result + ishall not refer to any element referenced by any iteratorjin the range[first,last)for all iteratorsiin the range[map,map + (last - first)).- Pre:
The iterator
result + ishall not refer to any element referenced by any iteratorjin the range[map,map + (last - first))for all iteratorsiin 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)# scattercopies elements from a source range into an output array according to a map. For each iteratoriin the range [first,last), the value*iis 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
scatterto 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
scatteris 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'svalue_typemust be convertible toRandomAccessIterator'svalue_type.InputIterator2 – must be a model of Input Iterator and
InputIterator2'svalue_typemust be convertible toRandomAccessIterator'sdifference_type.RandomAccessIterator – must be a model of Random Access iterator.
- Pre:
The iterator
result + ishall not refer to any element referenced by any iteratorjin the range[first,last)for all iteratorsiin the range[map,map + (last - first)).- Pre:
The iterator
result + ishall not refer to any element referenced by any iteratorjin the range[map,map + (last - first))for all iteratorsiin 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_ifconditionally copies elements from a source range into an output array according to a map. For each iteratoriin the range[first, last)such that*(stencil + (i - first))is true, the value*iis 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_ifis 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'svalue_typemust be convertible toRandomAccessIterator'svalue_type.InputIterator2 – must be a model of Input Iterator and
InputIterator2'svalue_typemust be convertible toRandomAccessIterator'sdifference_type.InputIterator3 – must be a model of Input Iterator and
InputIterator3'svalue_typemust be convertible tobool.RandomAccessIterator – must be a model of Random Access iterator.
- Pre:
The iterator
result + ishall not refer to any element referenced by any iteratorjin the range[first,last)for all iteratorsiin the range[map,map + (last - first)).- Pre:
The iterator
result + ishall not refer to any element referenced by any iteratorjin the range[map,map + (last - first))for all iteratorsiin the range[map,map + (last - first)).- Pre:
The iterator
result + ishall not refer to any element referenced by any iteratorjin the range[stencil,stencil + (last - first))for all iteratorsiin the range[map,map + (last - first)).- Pre:
The expression
result[*i]shall be valid for all iteratorsiin 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_ifconditionally copies elements from a source range into an output array according to a map. For each iteratoriin the range[first, last)such that*(stencil + (i - first))is true, the value*iis 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_ifis 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'svalue_typemust be convertible toRandomAccessIterator'svalue_type.InputIterator2 – must be a model of Input Iterator and
InputIterator2'svalue_typemust be convertible toRandomAccessIterator'sdifference_type.InputIterator3 – must be a model of Input Iterator and
InputIterator3'svalue_typemust be convertible tobool.RandomAccessIterator – must be a model of Random Access iterator.
- Pre:
The iterator
result + ishall not refer to any element referenced by any iteratorjin the range[first,last)for all iteratorsiin the range[map,map + (last - first)).- Pre:
The iterator
result + ishall not refer to any element referenced by any iteratorjin the range[map,map + (last - first))for all iteratorsiin the range[map,map + (last - first)).- Pre:
The iterator
result + ishall not refer to any element referenced by any iteratorjin the range[stencil,stencil + (last - first))for all iteratorsiin the range[map,map + (last - first)).- Pre:
The expression
result[*i]shall be valid for all iteratorsiin 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_ifconditionally copies elements from a source range into an output array according to a map. For each iteratoriin the range[first, last)such thatpred(*(stencil + (i - first)))istrue, the value*iis 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_ifis 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'svalue_typemust be convertible toRandomAccessIterator'svalue_type.InputIterator2 – must be a model of Input Iterator and
InputIterator2'svalue_typemust be convertible toRandomAccessIterator'sdifference_type.InputIterator3 – must be a model of Input Iterator and
InputIterator3'svalue_typemust be convertible toPredicate'sargument_type.RandomAccessIterator – must be a model of Random Access iterator.
Predicate – must be a model of Predicate.
- Pre:
The iterator
result + ishall not refer to any element referenced by any iteratorjin the range[first,last)for all iteratorsiin the range[map,map + (last - first)).- Pre:
The iterator
result + ishall not refer to any element referenced by any iteratorjin the range[map,map + (last - first))for all iteratorsiin the range[map,map + (last - first)).- Pre:
The iterator
result + ishall not refer to any element referenced by any iteratorjin the range[stencil,stencil + (last - first))for all iteratorsiin the range[map,map + (last - first)).- Pre:
The expression
result[*i]shall be valid for all iteratorsiin 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_ifconditionally copies elements from a source range into an output array according to a map. For each iteratoriin the range[first, last)such thatpred(*(stencil + (i - first)))istrue, the value*iis 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_ifis 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'svalue_typemust be convertible toRandomAccessIterator'svalue_type.InputIterator2 – must be a model of Input Iterator and
InputIterator2'svalue_typemust be convertible toRandomAccessIterator'sdifference_type.InputIterator3 – must be a model of Input Iterator and
InputIterator3'svalue_typemust be convertible toPredicate'sargument_type.RandomAccessIterator – must be a model of Random Access iterator.
Predicate – must be a model of Predicate.
- Pre:
The iterator
result + ishall not refer to any element referenced by any iteratorjin the range[first,last)for all iteratorsiin the range[map,map + (last - first)).- Pre:
The iterator
result + ishall not refer to any element referenced by any iteratorjin the range[map,map + (last - first))for all iteratorsiin the range[map,map + (last - first)).- Pre:
The iterator
result + ishall not refer to any element referenced by any iteratorjin the range[stencil,stencil + (last - first))for all iteratorsiin the range[map,map + (last - first)).- Pre:
The expression
result[*i]shall be valid for all iteratorsiin 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)
reduceis a generalization of summation: it computes the sum (or some other binary operation) of all the elements in the range[first, last). This version ofreduceuses0as the initial value of the reduction.reduceis similar to the C++ Standard Template Library’sstd::accumulate. The primary difference between the two functions is thatstd::accumulateguarantees the order of summation, whilereducerequires associativity of the binary operation to parallelize the reduction.Note that
reducealso assumes that the binary reduction operator (in this case operator+) is commutative. If the reduction operator is not commutative thenthrust::reduceshould 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
reduceto compute the sum of a sequence of integers using thethrust::hostexecution 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
xandyare objects ofInputIterator'svalue_type, thenx + yis defined and is convertible toInputIterator'svalue_type. IfTisInputIterator'svalue_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)# reduceis a generalization of summation: it computes the sum (or some other binary operation) of all the elements in the range[first, last). This version ofreduceuses0as the initial value of the reduction.reduceis similar to the C++ Standard Template Library’sstd::accumulate. The primary difference between the two functions is thatstd::accumulateguarantees the order of summation, whilereducerequires associativity of the binary operation to parallelize the reduction.Note that
reducealso assumes that the binary reduction operator (in this case operator+) is commutative. If the reduction operator is not commutative thenthrust::reduceshould 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
reduceto 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
xandyare objects ofInputIterator'svalue_type, thenx + yis defined and is convertible toInputIterator'svalue_type. IfTisInputIterator'svalue_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)
reduceis a generalization of summation: it computes the sum (or some other binary operation) of all the elements in the range[first, last). This version ofreduceusesinitas the initial value of the reduction.reduceis similar to the C++ Standard Template Library’sstd::accumulate. The primary difference between the two functions is thatstd::accumulateguarantees the order of summation, whilereducerequires associativity of the binary operation to parallelize the reduction.Note that
reducealso assumes that the binary reduction operator (in this case operator+) is commutative. If the reduction operator is not commutative thenthrust::reduceshould 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
reduceto compute the sum of a sequence of integers including an intialization value using thethrust::hostexecution 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
xandyare objects ofInputIterator'svalue_type, thenx + yis defined and is convertible toT.T – is convertible to
InputIterator'svalue_type.
- Returns:
The result of the reduction.
-
template<typename InputIterator, typename T>
T reduce(InputIterator first, InputIterator last, T init)# reduceis a generalization of summation: it computes the sum (or some other binary operation) of all the elements in the range[first, last). This version ofreduceusesinitas the initial value of the reduction.reduceis similar to the C++ Standard Template Library’sstd::accumulate. The primary difference between the two functions is thatstd::accumulateguarantees the order of summation, whilereducerequires associativity of the binary operation to parallelize the reduction.Note that
reducealso assumes that the binary reduction operator (in this case operator+) is commutative. If the reduction operator is not commutative thenthrust::reduceshould 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
reduceto 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
xandyare objects ofInputIterator'svalue_type, thenx + yis defined and is convertible toT.T – is convertible to
InputIterator'svalue_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)
reduceis a generalization of summation: it computes the sum (or some other binary operation) of all the elements in the range[first, last). This version ofreduceusesinitas the initial value of the reduction andbinary_opas the binary function used for summation.reduceis similar to the C++ Standard Template Library’sstd::accumulate. The primary difference between the two functions is thatstd::accumulateguarantees the order of summation, whilereducerequires associativity ofbinary_opto parallelize the reduction.Note that
reducealso assumes that the binary reduction operator (in this casebinary_op) is commutative. If the reduction operator is not commutative thenthrust::reduceshould 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
reduceto compute the maximum value of a sequence of integers using thethrust::hostexecution 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'svalue_typeis convertible toT.T – is a model of Assignable, and is convertible to
BinaryFunction'sfirst_argument_typeandsecond_argument_type.BinaryFunction – is a model of Binary Function, and
BinaryFunction'sresult_typeis 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)# reduceis a generalization of summation: it computes the sum (or some other binary operation) of all the elements in the range[first, last). This version ofreduceusesinitas the initial value of the reduction andbinary_opas the binary function used for summation.reduceis similar to the C++ Standard Template Library’sstd::accumulate. The primary difference between the two functions is thatstd::accumulateguarantees the order of summation, whilereducerequires associativity ofbinary_opto parallelize the reduction.Note that
reducealso assumes that the binary reduction operator (in this casebinary_op) is commutative. If the reduction operator is not commutative thenthrust::reduceshould 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
reduceto 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'svalue_typeis convertible toT.T – is a model of Assignable, and is convertible to
BinaryFunction'sfirst_argument_typeandsecond_argument_type.BinaryFunction – is a model of Binary Function, and
BinaryFunction'sresult_typeis 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_keyis a generalization ofreduceto key-value pairs. For each group of consecutive keys in the range[keys_first, keys_last)that are equal,reduce_by_keycopies the first element of the group to thekeys_output. The corresponding values in the range are reduced using theplusand the result copied tovalues_output.This version of
reduce_by_keyuses the function objectequal_toto test for equality andplusto reduce values with equal keys.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
reduce_by_keyto compact a sequence of key/value pairs and sum values with equal keys using thethrust::hostexecution 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'svalue_typeis convertible toOutputIterator1'svalue_type.OutputIterator2 – is a model of Output Iterator and and
InputIterator2'svalue_typeis convertible toOutputIterator2'svalue_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_keyis a generalization ofreduceto key-value pairs. For each group of consecutive keys in the range[keys_first, keys_last)that are equal,reduce_by_keycopies the first element of the group to thekeys_output. The corresponding values in the range are reduced using theplusand the result copied tovalues_output.This version of
reduce_by_keyuses the function objectequal_toto test for equality andplusto reduce values with equal keys.The following code snippet demonstrates how to use
reduce_by_keyto 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'svalue_typeis convertible toOutputIterator1'svalue_type.OutputIterator2 – is a model of Output Iterator and and
InputIterator2'svalue_typeis convertible toOutputIterator2'svalue_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_keyis a generalization ofreduceto key-value pairs. For each group of consecutive keys in the range[keys_first, keys_last)that are equal,reduce_by_keycopies the first element of the group to thekeys_output. The corresponding values in the range are reduced using theplusand the result copied tovalues_output.This version of
reduce_by_keyuses the function objectbinary_predto test for equality andplusto reduce values with equal keys.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
reduce_by_keyto compact a sequence of key/value pairs and sum values with equal keys using thethrust::hostexecution 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'svalue_typeis convertible toOutputIterator1'svalue_type.OutputIterator2 – is a model of Output Iterator and and
InputIterator2'svalue_typeis convertible toOutputIterator2'svalue_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_keyis a generalization ofreduceto key-value pairs. For each group of consecutive keys in the range[keys_first, keys_last)that are equal,reduce_by_keycopies the first element of the group to thekeys_output. The corresponding values in the range are reduced using theplusand the result copied tovalues_output.This version of
reduce_by_keyuses the function objectbinary_predto test for equality andplusto reduce values with equal keys.The following code snippet demonstrates how to use
reduce_by_keyto 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'svalue_typeis convertible toOutputIterator1'svalue_type.OutputIterator2 – is a model of Output Iterator and and
InputIterator2'svalue_typeis convertible toOutputIterator2'svalue_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_keyis a generalization ofreduceto key-value pairs. For each group of consecutive keys in the range[keys_first, keys_last)that are equal,reduce_by_keycopies the first element of the group to thekeys_output. The corresponding values in the range are reduced using theBinaryFunctionbinary_opand the result copied tovalues_output. Specifically, if consecutive key iteratorsiand(i + 1) are such thatbinary_pred(*i, *(i+1))istrue, then the corresponding values are reduced to a single value withbinary_op.This version of
reduce_by_keyuses the function objectbinary_predto test for equality andbinary_opto reduce values with equal keys.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
reduce_by_keyto compact a sequence of key/value pairs and sum values with equal keys using thethrust::hostexecution 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'svalue_typeis convertible toOutputIterator1'svalue_type.OutputIterator2 – is a model of Output Iterator and and
InputIterator2'svalue_typeis convertible toOutputIterator2'svalue_type.BinaryPredicate – is a model of Binary Predicate.
BinaryFunction – is a model of Binary Function and
BinaryFunction'sresult_typeis convertible toOutputIterator2'svalue_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_keyis a generalization ofreduceto key-value pairs. For each group of consecutive keys in the range[keys_first, keys_last)that are equal,reduce_by_keycopies the first element of the group to thekeys_output. The corresponding values in the range are reduced using theBinaryFunctionbinary_opand the result copied tovalues_output. Specifically, if consecutive key iteratorsiand(i + 1) are such thatbinary_pred(*i, *(i+1))istrue, then the corresponding values are reduced to a single value withbinary_op.This version of
reduce_by_keyuses the function objectbinary_predto test for equality andbinary_opto reduce values with equal keys.The following code snippet demonstrates how to use
reduce_by_keyto 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'svalue_typeis convertible toOutputIterator1'svalue_type.OutputIterator2 – is a model of Output Iterator and and
InputIterator2'svalue_typeis convertible toOutputIterator2'svalue_type.BinaryPredicate – is a model of Binary Predicate.
BinaryFunction – is a model of Binary Function and
BinaryFunction'sresult_typeis convertible toOutputIterator2'svalue_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)
countfinds the number of elements in[first,last)that are equal tovalue. More precisely,countreturns the number of iteratorsiin[first, last)such that*i == value.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
countto count the number of instances in a range of a value of interest using thethrust::deviceexecution 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'svalue_typemust 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'svalue_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)# countfinds the number of elements in[first,last)that are equal tovalue. More precisely,countreturns the number of iteratorsiin[first, last)such that*i == value.The following code snippet demonstrates how to use
countto 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'svalue_typemust 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'svalue_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_iffinds the number of elements in[first,last)for which a predicate istrue. More precisely,count_ifreturns the number of iteratorsiin[first, last)such thatpred(*i) == true.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
countto count the number of odd numbers in a range using thethrust::deviceexecution 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'svalue_typemust be convertible toPredicate'sargument_type.Predicate – must be a model of Predicate.
- Returns:
The number of elements where
predistrue.
-
template<typename InputIterator, typename Predicate>
thrust::iterator_traits<InputIterator>::difference_type count_if(InputIterator first, InputIterator last, Predicate pred)# count_iffinds the number of elements in[first,last)for which a predicate istrue. More precisely,count_ifreturns the number of iteratorsiin[first, last)such thatpred(*i) == true.The following code snippet demonstrates how to use
countto 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'svalue_typemust be convertible toPredicate'sargument_type.Predicate – must be a model of Predicate.
- Returns:
The number of elements where
predistrue.
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)
equalreturnstrueif the two ranges[first1, last1)and[first2, first2 + (last1 - first1))are identical when compared element-by-element, and otherwise returnsfalse.This version of
equalreturnstrueif and only if for every iteratoriin[first1, last1),*i == *(first2 + (i - first1)).The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
equalto test two ranges for equality using thethrust::hostexecution 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'svalue_typeis a model of Equality Comparable, andInputIterator1'svalue_typecan be compared for equality withInputIterator2'svalue_type.InputIterator2 – is a model of Input Iterator, and
InputIterator2'svalue_typeis a model of Equality Comparable, andInputIterator2'svalue_typecan be compared for equality withInputIterator1'svalue_type.
- Returns:
true, if the sequences are equal;false, otherwise.
-
template<typename InputIterator1, typename InputIterator2>
bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2)# equalreturnstrueif the two ranges[first1, last1)and[first2, first2 + (last1 - first1))are identical when compared element-by-element, and otherwise returnsfalse.This version of
equalreturnstrueif and only if for every iteratoriin[first1, last1),*i == *(first2 + (i - first1)).The following code snippet demonstrates how to use
equalto 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'svalue_typeis a model of Equality Comparable, andInputIterator1'svalue_typecan be compared for equality withInputIterator2'svalue_type.InputIterator2 – is a model of Input Iterator, and
InputIterator2'svalue_typeis a model of Equality Comparable, andInputIterator2'svalue_typecan be compared for equality withInputIterator1'svalue_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)
equalreturnstrueif the two ranges[first1, last1)and[first2, first2 + (last1 - first1))are identical when compared element-by-element, and otherwise returnsfalse.This version of
equalreturnstrueif and only if for every iteratoriin[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
equalto compare the elements in two ranges modulo 2 using thethrust::hostexecution 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'svalue_typeis convertible toBinaryPredicate'sfirst_argument_type.InputIterator2 – is a model of Input Iterator, and
InputIterator2'svalue_typeis convertible toBinaryPredicate'ssecond_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)# equalreturnstrueif the two ranges[first1, last1)and[first2, first2 + (last1 - first1))are identical when compared element-by-element, and otherwise returnsfalse.This version of
equalreturnstrueif and only if for every iteratoriin[first1, last1),binary_pred(*i, *(first2 + (i - first1)))istrue.The following code snippet demonstrates how to use
equalto 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'svalue_typeis convertible toBinaryPredicate'sfirst_argument_type.InputIterator2 – is a model of Input Iterator, and
InputIterator2'svalue_typeis convertible toBinaryPredicate'ssecond_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_elementfinds the smallest element in the range[first, last). It returns the first iteratoriin[first, last)such that no other iterator in[first, last)points to a value smaller than*i. The return value islastif and only if[first, last)is an empty range.The two versions of
min_elementdiffer in how they define whether one element is less than another. This version compares objects usingoperator<. Specifically, this version ofmin_elementreturns the first iteratoriin[first, last)such that, for every iteratorjin[first, last),*j < *iisfalse.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'svalue_typeis 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_elementfinds the smallest element in the range[first, last). It returns the first iteratoriin[first, last)such that no other iterator in[first, last)points to a value smaller than*i. The return value islastif and only if[first, last)is an empty range.The two versions of
min_elementdiffer in how they define whether one element is less than another. This version compares objects usingoperator<. Specifically, this version ofmin_elementreturns the first iteratoriin[first, last)such that, for every iteratorjin[first, last),*j < *iisfalse.#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'svalue_typeis 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_elementfinds the smallest element in the range[first, last). It returns the first iteratoriin[first, last)such that no other iterator in[first, last)points to a value smaller than*i. The return value islastif and only if[first, last)is an empty range.The two versions of
min_elementdiffer in how they define whether one element is less than another. This version compares objects using a function objectcomp. Specifically, this version ofmin_elementreturns the first iteratoriin[first, last)such that, for every iteratorjin[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_elementto find the smallest element of a collection of key-value pairs using thethrust::hostexecution 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'svalue_typeis convertible to bothcomp'sfirst_argument_typeandsecond_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_elementfinds the smallest element in the range[first, last). It returns the first iteratoriin[first, last)such that no other iterator in[first, last)points to a value smaller than*i. The return value islastif and only if[first, last)is an empty range.The two versions of
min_elementdiffer in how they define whether one element is less than another. This version compares objects using a function objectcomp. Specifically, this version ofmin_elementreturns the first iteratoriin[first, last)such that, for every iteratorjin[first, last),comp(*j, *i)isfalse.The following code snippet demonstrates how to use
min_elementto 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'svalue_typeis convertible to bothcomp'sfirst_argument_typeandsecond_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_elementfinds the largest element in the range[first, last). It returns the first iteratoriin[first, last)such that no other iterator in[first, last)points to a value larger than*i. The return value islastif and only if[first, last)is an empty range.The two versions of
max_elementdiffer in how they define whether one element is greater than another. This version compares objects usingoperator<. Specifically, this version ofmax_elementreturns the first iteratoriin[first, last)such that, for every iteratorjin[first, last),*i < *jisfalse.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'svalue_typeis 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_elementfinds the largest element in the range[first, last). It returns the first iteratoriin[first, last)such that no other iterator in[first, last)points to a value larger than*i. The return value islastif and only if[first, last)is an empty range.The two versions of
max_elementdiffer in how they define whether one element is greater than another. This version compares objects usingoperator<. Specifically, this version ofmax_elementreturns the first iteratoriin[first, last)such that, for every iteratorjin[first, last),*i < *jisfalse.#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'svalue_typeis 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_elementfinds the largest element in the range[first, last). It returns the first iteratoriin[first, last)such that no other iterator in[first, last)points to a value larger than*i. The return value islastif and only if[first, last)is an empty range.The two versions of
max_elementdiffer in how they define whether one element is less than another. This version compares objects using a function objectcomp. Specifically, this version ofmax_elementreturns the first iteratoriin[first, last)such that, for every iteratorjin[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_elementto find the largest element of a collection of key-value pairs using thethrust::hostexecution 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'svalue_typeis convertible to bothcomp'sfirst_argument_typeandsecond_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_elementfinds the largest element in the range[first, last). It returns the first iteratoriin[first, last)such that no other iterator in[first, last)points to a value larger than*i. The return value islastif and only if[first, last)is an empty range.The two versions of
max_elementdiffer in how they define whether one element is less than another. This version compares objects using a function objectcomp. Specifically, this version ofmax_elementreturns the first iteratoriin[first, last)such that, for every iteratorjin[first, last),comp(*i, *j)isfalse.The following code snippet demonstrates how to use
max_elementto 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'svalue_typeis convertible to bothcomp'sfirst_argument_typeandsecond_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_elementfinds the smallest and largest elements in the range[first, last). It returns a pair of iterators(imin, imax)whereiminis the same iterator returned bymin_elementandimaxis the same iterator returned bymax_element. This function is potentially more efficient than separate calls tomin_elementandmax_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'svalue_typeis 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_elementfinds the smallest and largest elements in the range[first, last). It returns a pair of iterators(imin, imax)whereiminis the same iterator returned bymin_elementandimaxis the same iterator returned bymax_element. This function is potentially more efficient than separate calls tomin_elementandmax_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'svalue_typeis 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_elementfinds the smallest and largest elements in the range[first, last). It returns a pair of iterators(imin, imax)whereiminis the same iterator returned bymin_elementandimaxis the same iterator returned bymax_element. This function is potentially more efficient than separate calls tomin_elementandmax_element.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
minmax_elementto find the smallest and largest elements of a collection of key-value pairs using thethrust::hostexecution 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'svalue_typeis convertible to bothcomp'sfirst_argument_typeandsecond_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_elementfinds the smallest and largest elements in the range[first, last). It returns a pair of iterators(imin, imax)whereiminis the same iterator returned bymin_elementandimaxis the same iterator returned bymax_element. This function is potentially more efficient than separate calls tomin_elementandmax_element.The following code snippet demonstrates how to use
minmax_elementto 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'svalue_typeis convertible to bothcomp'sfirst_argument_typeandsecond_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_productcalculates an inner product of the ranges[first1, last1)and[first2, first2 + (last1 - first1)).Specifically, this version of
inner_productcomputes 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_productto compute the dot product of two vectors using thethrust::hostexecution 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
xis an object of typeOutputType, andyis an object ofInputIterator1'svalue_type, andzis an object ofInputIterator2'svalue_type, thenx + y * zis 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_productcalculates an inner product of the ranges[first1, last1)and[first2, first2 + (last1 - first1)).Specifically, this version of
inner_productcomputes 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_productto 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
xis an object of typeOutputType, andyis an object ofInputIterator1'svalue_type, andzis an object ofInputIterator2'svalue_type, thenx + y * zis 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_productcalculates an inner product of the ranges[first1, last1)and[first2, first2 + (last1 - first1)).This version of
inner_productis identical to the first, except that is uses two user-supplied function objects instead ofoperator+andoperator*.Specifically, this version of
inner_productcomputes 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'svalue_typeis convertible toBinaryFunction2'sfirst_argument_type.InputIterator2 – is a model of Input Iterator. and
InputIterator2'svalue_typeis convertible toBinaryFunction2'ssecond_argument_type.OutputType – is a model of Assignable, and
OutputTypeis convertible toBinaryFunction1'sfirst_argument_type.BinaryFunction1 – is a model of Binary Function, and
BinaryFunction1'sreturn_typeis convertible toOutputType.BinaryFunction2 – is a model of Binary Function, and
BinaryFunction2'sreturn_typeis convertible toBinaryFunction1'ssecond_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_productcalculates an inner product of the ranges[first1, last1)and[first2, first2 + (last1 - first1)).This version of
inner_productis identical to the first, except that is uses two user-supplied function objects instead ofoperator+andoperator*.Specifically, this version of
inner_productcomputes 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'svalue_typeis convertible toBinaryFunction2'sfirst_argument_type.InputIterator2 – is a model of Input Iterator. and
InputIterator2'svalue_typeis convertible toBinaryFunction2'ssecond_argument_type.OutputType – is a model of Assignable, and
OutputTypeis convertible toBinaryFunction1'sfirst_argument_type.BinaryFunction1 – is a model of Binary Function, and
BinaryFunction1'sreturn_typeis convertible toOutputType.BinaryFunction2 – is a model of Binary Function, and
BinaryFunction2'sreturn_typeis convertible toBinaryFunction1'ssecond_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_reducefuses thetransformandreduceoperations.transform_reduceis equivalent to performing a transformation defined byunary_opinto a temporary sequence and then performingreduceon the transformed sequence. In most cases, fusing these two operations together is more efficient, since fewer memory reads and writes are required.transform_reduceperforms a reduction on the transformation of the sequence[first, last)according tounary_op. Specifically,unary_opis applied to each element of the sequence and then the result is reduced to a single value withbinary_opusing the initial valueinit. Note that the transformationunary_opis not applied to the initial valueinit. The order of reduction is not specified, sobinary_opmust be both commutative and associative.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
transform_reduceto compute the maximum value of the absolute value of the elements of a range using thethrust::hostexecution 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 == 3See 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'svalue_typeis convertible toUnaryFunction'sargument_type.UnaryFunction – is a model of Unary Function, and
UnaryFunction'sresult_typeis convertible toOutputType.OutputType – is a model of Assignable, and is convertible to
BinaryFunction'sfirst_argument_typeandsecond_argument_type.BinaryFunction – is a model of Binary Function, and
BinaryFunction'sresult_typeis 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_reducefuses thetransformandreduceoperations.transform_reduceis equivalent to performing a transformation defined byunary_opinto a temporary sequence and then performingreduceon the transformed sequence. In most cases, fusing these two operations together is more efficient, since fewer memory reads and writes are required.transform_reduceperforms a reduction on the transformation of the sequence[first, last)according tounary_op. Specifically,unary_opis applied to each element of the sequence and then the result is reduced to a single value withbinary_opusing the initial valueinit. Note that the transformationunary_opis not applied to the initial valueinit. The order of reduction is not specified, sobinary_opmust be both commutative and associative.The following code snippet demonstrates how to use
transform_reduceto 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 == 3See 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'svalue_typeis convertible toUnaryFunction'sargument_type.UnaryFunction – is a model of Unary Function, and
UnaryFunction'sresult_typeis convertible toOutputType.OutputType – is a model of Assignable, and is convertible to
BinaryFunction'sfirst_argument_typeandsecond_argument_type.BinaryFunction – is a model of Binary Function, and
BinaryFunction'sresult_typeis 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_ofdetermines whether all elements in a range satify a predicate. Specifically,all_ofreturnstrueifpred(*i)istruefor every iteratoriin the range[first, last)andfalseotherwise.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_ofdetermines whether all elements in a range satify a predicate. Specifically,all_ofreturnstrueifpred(*i)istruefor every iteratoriin the range[first, last)andfalseotherwise.#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_ofdetermines whether any element in a range satifies a predicate. Specifically,any_ofreturnstrueifpred(*i)istruefor any iteratoriin the range[first, last)andfalseotherwise.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_ofdetermines whether any element in a range satifies a predicate. Specifically,any_ofreturnstrueifpred(*i)istruefor any iteratoriin the range[first, last)andfalseotherwise.#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_ofdetermines whether no element in a range satifies a predicate. Specifically,none_ofreturnstrueif there is no iteratoriin the range[first, last)such thatpred(*i)istrue, andfalseotherwise.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_ofdetermines whether no element in a range satifies a predicate. Specifically,none_ofreturnstrueif there is no iteratoriin the range[first, last)such thatpred(*i)istrue, andfalseotherwise.#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_partitionedreturnstrueif the given range is partitioned with respect to a predicate, andfalseotherwise.Specifically,
is_partitionedreturnstrueif[first, last)is empty of if[first, last)is partitioned bypred, i.e. if all elements that satisfypredappear 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'svalue_typeis convertible toPredicate'sargument_type.Predicate – is a model of Predicate.
- Returns:
trueif 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_partitionedreturnstrueif the given range is partitioned with respect to a predicate, andfalseotherwise.Specifically,
is_partitionedreturnstrueif[first, last)is empty of if[first, last)is partitioned bypred, i.e. if all elements that satisfypredappear 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'svalue_typeis convertible toPredicate'sargument_type.Predicate – is a model of Predicate.
- Returns:
trueif 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_sortedreturnstrueif the range[first, last)is sorted in ascending order, andfalseotherwise.Specifically, this version of
is_sortedreturnsfalseif for some iteratoriin the range[first, last - 1)the expression*(i + 1) < *iistrue.The algorithm’s execution is parallelized as determined by
exec.The following code demonstrates how to use
is_sortedto test whether the contents of adevice_vectorare stored in ascending order using thethrust::deviceexecution 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'svalue_typeis a model of LessThan Comparable, and the ordering on objects ofForwardIterator'svalue_typeis 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_sortedreturnstrueif the range[first, last)is sorted in ascending order, andfalseotherwise.Specifically, this version of
is_sortedreturnsfalseif for some iteratoriin the range[first, last - 1)the expression*(i + 1) < *iistrue.The following code demonstrates how to use
is_sortedto test whether the contents of adevice_vectorare 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'svalue_typeis a model of LessThan Comparable, and the ordering on objects ofForwardIterator'svalue_typeis 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_sortedreturnstrueif the range[first, last)is sorted in ascending order accoring to a user-defined comparison operation, andfalseotherwise.Specifically, this version of
is_sortedreturnsfalseif for some iteratoriin 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_sortedto test whether the contents of adevice_vectorare stored in descending order using thethrust::deviceexecution 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'svalue_typeis convertible to bothStrictWeakOrdering'sfirst_argument_typeandsecond_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_sortedreturnstrueif the range[first, last)is sorted in ascending order accoring to a user-defined comparison operation, andfalseotherwise.Specifically, this version of
is_sortedreturnsfalseif for some iteratoriin the range[first, last - 1)the expressioncomp(*(i + 1), *i)istrue.The following code snippet demonstrates how to use
is_sortedto test whether the contents of adevice_vectorare 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'svalue_typeis convertible to bothStrictWeakOrdering'sfirst_argument_typeandsecond_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_untilreturns the last iteratoriin[first,last]for which the range[first,last)is sorted usingoperator<. Ifdistance(first,last) < 2,is_sorted_untilsimply returnslast.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
is_sorted_untilto find the first position in an array where the data becomes unsorted using thethrust::hostexecution 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'svalue_typeis 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_untilreturns the last iteratoriin[first,last]for which the range[first,last)is sorted usingoperator<. Ifdistance(first,last) < 2,is_sorted_untilsimply returnslast.The following code snippet demonstrates how to use
is_sorted_untilto 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'svalue_typeis 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_untilreturns the last iteratoriin[first,last]for which the range[first,last)is sorted using the function objectcomp. Ifdistance(first,last) < 2,is_sorted_untilsimply returnslast.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
is_sorted_untilto find the first position in an array where the data becomes unsorted in descending order using thethrust::hostexecution 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'svalue_typeis convertible toCompare'sargument_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_untilreturns the last iteratoriin[first,last]for which the range[first,last)is sorted using the function objectcomp. Ifdistance(first,last) < 2,is_sorted_untilsimply returnslast.The following code snippet demonstrates how to use
is_sorted_untilto 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'svalue_typeis convertible toCompare'sargument_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)
mergecombines 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.mergeis 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
mergecompares elements usingoperator<.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
mergeto compute the merger of two sorted sets of integers using thethrust::hostexecution 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,
InputIterator1andInputIterator2have the samevalue_type,InputIterator1'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator1'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator2 – is a model of Input Iterator,
InputIterator2andInputIterator1have the samevalue_type,InputIterator2'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator2'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2'svalue_typeis convertable to a type inOutputIterator'sset 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)# mergecombines 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.mergeis 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
mergecompares elements usingoperator<.The following code snippet demonstrates how to use
mergeto 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,
InputIterator1andInputIterator2have the samevalue_type,InputIterator1'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator1'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator2 – is a model of Input Iterator,
InputIterator2andInputIterator1have the samevalue_type,InputIterator2'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator2'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2'svalue_typeis convertable to a type inOutputIterator'sset 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)
mergecombines 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.mergeis 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
mergecompares elements using a function objectcomp.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
mergeto compute the merger of two sets of integers sorted in descending order using thethrust::hostexecution 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'svalue_typeis convertable toStrictWeakCompare'sfirst_argument_type. andInputIterator1'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator2 – is a model of Input Iterator,
InputIterator2'svalue_typeis convertable toStrictWeakCompare'ssecond_argument_type. andInputIterator2'svalue_typeis convertable to a type inOutputIterator'sset 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)# mergecombines 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.mergeis 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
mergecompares elements using a function objectcomp.The following code snippet demonstrates how to use
mergeto 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'svalue_typeis convertable toStrictWeakCompare'sfirst_argument_type. andInputIterator1'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator2 – is a model of Input Iterator,
InputIterator2'svalue_typeis convertable toStrictWeakCompare'ssecond_argument_type. andInputIterator2'svalue_typeis convertable to a type inOutputIterator'sset 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_keyperforms a key-value merge. That is,merge_by_keycopies 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_keycopies 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_keyis 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_keyto compute the merger of two sets of integers sorted in ascending order using thethrust::hostexecution 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,
InputIterator1andInputIterator2have the samevalue_type,InputIterator1'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator1'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator2 – is a model of Input Iterator,
InputIterator2andInputIterator1have the samevalue_type,InputIterator2'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator2'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator3 – is a model of Input Iterator, and
InputIterator3'svalue_typeis convertible to a type inOutputIterator2'sset ofvalue_types.InputIterator4 – is a model of Input Iterator, and
InputIterator4'svalue_typeis convertible to a type inOutputIterator2'sset ofvalue_types.OutputIterator1 – is a model of Output Iterator.
OutputIterator2 – is a model of Output Iterator.
- Returns:
A
pairpsuch thatp.firstis the end of the output range of keys, and such thatp.secondis 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_keyperforms a key-value merge. That is,merge_by_keycopies 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_keycopies 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_keyis 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_keyto 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,
InputIterator1andInputIterator2have the samevalue_type,InputIterator1'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator1'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator2 – is a model of Input Iterator,
InputIterator2andInputIterator1have the samevalue_type,InputIterator2'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator2'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator3 – is a model of Input Iterator, and
InputIterator3'svalue_typeis convertible to a type inOutputIterator2'sset ofvalue_types.InputIterator4 – is a model of Input Iterator, and
InputIterator4'svalue_typeis convertible to a type inOutputIterator2'sset ofvalue_types.OutputIterator1 – is a model of Output Iterator.
OutputIterator2 – is a model of Output Iterator.
- Returns:
A
pairpsuch thatp.firstis the end of the output range of keys, and such thatp.secondis 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_keyperforms a key-value merge. That is,merge_by_keycopies 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_keycopies 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_keyis 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_keycompares key elements using a function objectcomp.The algorithm’s execution is parallelized using
exec.The following code snippet demonstrates how to use
merge_by_keyto compute the merger of two sets of integers sorted in descending order using thethrust::hostexecution 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'svalue_typeis convertable toStrictWeakCompare'sfirst_argument_type. andInputIterator1'svalue_typeis convertable to a type inOutputIterator1'sset ofvalue_types.InputIterator2 – is a model of Input Iterator,
InputIterator2'svalue_typeis convertable toStrictWeakCompare'ssecond_argument_type. andInputIterator2'svalue_typeis convertable to a type inOutputIterator1'sset ofvalue_types.InputIterator3 – is a model of Input Iterator, and
InputIterator3'svalue_typeis convertible to a type inOutputIterator2'sset ofvalue_types.InputIterator4 – is a model of Input Iterator, and
InputIterator4'svalue_typeis convertible to a type inOutputIterator2'sset 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
pairpsuch thatp.firstis the end of the output range of keys, and such thatp.secondis 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_keyperforms a key-value merge. That is,merge_by_keycopies 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_keycopies 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_keyis 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_keycompares key elements using a function objectcomp.The following code snippet demonstrates how to use
merge_by_keyto 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'svalue_typeis convertable toStrictWeakCompare'sfirst_argument_type. andInputIterator1'svalue_typeis convertable to a type inOutputIterator1'sset ofvalue_types.InputIterator2 – is a model of Input Iterator,
InputIterator2'svalue_typeis convertable toStrictWeakCompare'ssecond_argument_type. andInputIterator2'svalue_typeis convertable to a type inOutputIterator1'sset ofvalue_types.InputIterator3 – is a model of Input Iterator, and
InputIterator3'svalue_typeis convertible to a type inOutputIterator2'sset ofvalue_types.InputIterator4 – is a model of Input Iterator, and
InputIterator4'svalue_typeis convertible to a type inOutputIterator2'sset 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
pairpsuch thatp.firstis the end of the output range of keys, and such thatp.secondis 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)
reversereverses a range. That is: for everyisuch 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
reverseto reverse adevice_vectorof integers using thethrust::deviceexecution 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
BidirectionalIteratoris mutable.
-
template<typename BidirectionalIterator>
void reverse(BidirectionalIterator first, BidirectionalIterator last)# reversereverses a range. That is: for everyisuch that0 <= i <= (last - first) / 2, it exchanges*(first + i)and*(last - (i + 1)).The following code snippet demonstrates how to use
reverseto reverse adevice_vectorof 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
BidirectionalIteratoris 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_copydiffers fromreverseonly in that the reversed range is written to a different output range, rather than inplace.reverse_copycopies 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 everyisuch that0 <= i < (last - first),reverse_copyperforms 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_copyto reverse an inputdevice_vectorof integers to an outputdevice_vectorusing thethrust::deviceexecution 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'svalue_typeis convertible toOutputIterator'svalue_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_copydiffers fromreverseonly in that the reversed range is written to a different output range, rather than inplace.reverse_copycopies 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 everyisuch that0 <= i < (last - first),reverse_copyperforms the assignment*(result + (last - first) - i) = *(first + i).The return value is
result + (last - first)).The following code snippet demonstrates how to use
reverse_copyto reverse an inputdevice_vectorof 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'svalue_typeis convertible toOutputIterator'svalue_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)
partitionreorders the elements[first, last)based on the function objectpred, such that all of the elements that satisfypredprecede the elements that fail to satisfy it. The postcondition is that, for some iteratormiddlein the range[first, last),pred(*i)istruefor every iteratoriin the range[first,middle)andfalsefor every iteratoriin the range[middle, last). The return value ofpartitionismiddle.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
partitionto reorder a sequence so that even numbers precede odd numbers using thethrust::hostexecution 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'svalue_typeis convertible toPredicate'sargument_type, andForwardIteratoris 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)# partitionreorders the elements[first, last)based on the function objectpred, such that all of the elements that satisfypredprecede the elements that fail to satisfy it. The postcondition is that, for some iteratormiddlein the range[first, last),pred(*i)istruefor every iteratoriin the range[first,middle)andfalsefor every iteratoriin the range[middle, last). The return value ofpartitionismiddle.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
partitionto 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'svalue_typeis convertible toPredicate'sargument_type, andForwardIteratoris 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)
partitionreorders the elements[first, last)based on the function objectpredapplied to a stencil range[stencil, stencil + (last - first)), such that all of the elements whose corresponding stencil element satisfiespredprecede all of the elements whose corresponding stencil element fails to satisfy it. The postcondition is that, for some iteratormiddlein the range[first, last),pred(*stencil_i)istruefor every iteratorstencil_iin the range[stencil,stencil + (middle - first))andfalsefor every iteratorstencil_iin the range[stencil + (middle - first), stencil + (last - first)). The return value ofstable_partitionismiddle.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
partitionto reorder a sequence so that even numbers precede odd numbers using thethrust::hostexecution 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
ForwardIteratoris mutable.InputIterator – is a model of Input Iterator, and
InputIterator'svalue_typeis convertible toPredicate'sargument_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)# partitionreorders the elements[first, last)based on the function objectpredapplied to a stencil range[stencil, stencil + (last - first)), such that all of the elements whose corresponding stencil element satisfiespredprecede all of the elements whose corresponding stencil element fails to satisfy it. The postcondition is that, for some iteratormiddlein the range[first, last),pred(*stencil_i)istruefor every iteratorstencil_iin the range[stencil,stencil + (middle - first))andfalsefor every iteratorstencil_iin the range[stencil + (middle - first), stencil + (last - first)). The return value ofstable_partitionismiddle.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
partitionto 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
ForwardIteratoris mutable.InputIterator – is a model of Input Iterator, and
InputIterator'svalue_typeis convertible toPredicate'sargument_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_copydiffers frompartitiononly in that the reordered sequence is written to difference output sequences, rather than in place.partition_copycopies the elements[first, last)based on the function objectpred. All of the elements that satisfypredare copied to the range beginning atout_trueand 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_copyto separate a sequence into two output sequences of even and odd numbers using thethrust::hostexecution 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'svalue_typeis convertible toPredicate'sargument_typeandInputIterator'svalue_typeis convertible toOutputIterator1andOutputIterator2'svalue_types.OutputIterator1 – is a model of Output Iterator.
OutputIterator2 – is a model of Output Iterator.
Predicate – is a model of Predicate.
- Returns:
A
pairp such thatp.firstis the end of the output range beginning atout_trueandp.secondis 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_copydiffers frompartitiononly in that the reordered sequence is written to difference output sequences, rather than in place.partition_copycopies the elements[first, last)based on the function objectpred. All of the elements that satisfypredare copied to the range beginning atout_trueand 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_copyto 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'svalue_typeis convertible toPredicate'sargument_typeandInputIterator'svalue_typeis convertible toOutputIterator1andOutputIterator2'svalue_types.OutputIterator1 – is a model of Output Iterator.
OutputIterator2 – is a model of Output Iterator.
Predicate – is a model of Predicate.
- Returns:
A
pairp such thatp.firstis the end of the output range beginning atout_trueandp.secondis 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_copydiffers frompartitiononly in that the reordered sequence is written to difference output sequences, rather than in place.partition_copycopies the elements[first, last)based on the function objectpredwhich is applied to a range of stencil elements. All of the elements whose corresponding stencil element satisfiespredare copied to the range beginning atout_trueand 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_copyto separate a sequence into two output sequences of even and odd numbers using thethrust::hostexecution 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'svalue_typeis convertible toOutputIterator1andOutputIterator2'svalue_types.InputIterator2 – is a model of Input Iterator, and
InputIterator2'svalue_typeis convertible toPredicate'sargument_type.OutputIterator1 – is a model of Output Iterator.
OutputIterator2 – is a model of Output Iterator.
Predicate – is a model of Predicate.
- Returns:
A
pairp such thatp.firstis the end of the output range beginning atout_trueandp.secondis 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_copydiffers frompartitiononly in that the reordered sequence is written to difference output sequences, rather than in place.partition_copycopies the elements[first, last)based on the function objectpredwhich is applied to a range of stencil elements. All of the elements whose corresponding stencil element satisfiespredare copied to the range beginning atout_trueand 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_copyto 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'svalue_typeis convertible toOutputIterator1andOutputIterator2'svalue_types.InputIterator2 – is a model of Input Iterator, and
InputIterator2'svalue_typeis convertible toPredicate'sargument_type.OutputIterator1 – is a model of Output Iterator.
OutputIterator2 – is a model of Output Iterator.
Predicate – is a model of Predicate.
- Returns:
A
pairp such thatp.firstis the end of the output range beginning atout_trueandp.secondis 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_partitionis much likepartition: it reorders the elements in the range[first, last)based on the function objectpred, such that all of the elements that satisfypredprecede all of the elements that fail to satisfy it. The postcondition is that, for some iteratormiddlein the range[first, last),pred(*i)istruefor every iteratoriin the range[first,middle)andfalsefor every iteratoriin the range[middle, last). The return value ofstable_partitionismiddle.stable_partitiondiffers frompartitionin thatstable_partitionis guaranteed to preserve relative order. That is, ifxandyare elements in[first, last), andstencil_xandstencil_yare the stencil elements in corresponding positions within[stencil, stencil + (last - first)), andpred(stencil_x) == pred(stencil_y), and ifxprecedesy, then it will still be true afterstable_partitionthatxprecedesy.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
stable_partitionto reorder a sequence so that even numbers precede odd numbers using thethrust::hostexecution 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'svalue_typeis convertible toPredicate'sargument_type, andForwardIteratoris 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_partitionis much likepartition: it reorders the elements in the range[first, last)based on the function objectpred, such that all of the elements that satisfypredprecede all of the elements that fail to satisfy it. The postcondition is that, for some iteratormiddlein the range[first, last),pred(*i)istruefor every iteratoriin the range[first,middle)andfalsefor every iteratoriin the range[middle, last). The return value ofstable_partitionismiddle.stable_partitiondiffers frompartitionin thatstable_partitionis guaranteed to preserve relative order. That is, ifxandyare elements in[first, last), andstencil_xandstencil_yare the stencil elements in corresponding positions within[stencil, stencil + (last - first)), andpred(stencil_x) == pred(stencil_y), and ifxprecedesy, then it will still be true afterstable_partitionthatxprecedesy.The following code snippet demonstrates how to use
stable_partitionto 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'svalue_typeis convertible toPredicate'sargument_type, andForwardIteratoris 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_partitionis much likepartition:it reorders the elements in the range[first, last)based on the function objectpredapplied to a stencil range[stencil, stencil + (last - first)), such that all of the elements whose corresponding stencil element satisfiespredprecede all of the elements whose corresponding stencil element fails to satisfy it. The postcondition is that, for some iteratormiddlein the range[first, last),pred(*stencil_i)istruefor every iteratorstencil_iin the range[stencil,stencil + (middle - first))andfalsefor every iteratorstencil_iin the range[stencil + (middle - first), stencil + (last - first)). The return value ofstable_partitionismiddle.stable_partitiondiffers frompartitionin thatstable_partitionis guaranteed to preserve relative order. That is, ifxandyare elements in[first, last), such thatpred(x) == pred(y), and ifxprecedesy, then it will still be true afterstable_partitionthatxprecedesy.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
stable_partitionto reorder a sequence so that even numbers precede odd numbers using thethrust::hostexecution 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
ForwardIteratoris mutable.InputIterator – is a model of Input Iterator, and
InputIterator'svalue_typeis convertible toPredicate'sargument_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_partitionis much likepartition:it reorders the elements in the range[first, last)based on the function objectpredapplied to a stencil range[stencil, stencil + (last - first)), such that all of the elements whose corresponding stencil element satisfiespredprecede all of the elements whose corresponding stencil element fails to satisfy it. The postcondition is that, for some iteratormiddlein the range[first, last),pred(*stencil_i)istruefor every iteratorstencil_iin the range[stencil,stencil + (middle - first))andfalsefor every iteratorstencil_iin the range[stencil + (middle - first), stencil + (last - first)). The return value ofstable_partitionismiddle.stable_partitiondiffers frompartitionin thatstable_partitionis guaranteed to preserve relative order. That is, ifxandyare elements in[first, last), such thatpred(x) == pred(y), and ifxprecedesy, then it will still be true afterstable_partitionthatxprecedesy.The following code snippet demonstrates how to use
stable_partitionto 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
ForwardIteratoris mutable.InputIterator – is a model of Input Iterator, and
InputIterator'svalue_typeis convertible toPredicate'sargument_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_copydiffers fromstable_partitiononly in that the reordered sequence is written to different output sequences, rather than in place.stable_partition_copycopies the elements[first, last)based on the function objectpred. All of the elements that satisfypredare copied to the range beginning atout_trueand all the elements that fail to satisfy it are copied to the range beginning atout_false.stable_partition_copydiffers frompartition_copyin thatstable_partition_copyis guaranteed to preserve relative order. That is, ifxandyare elements in[first, last), such thatpred(x) == pred(y), and ifxprecedesy, then it will still be true afterstable_partition_copythatxprecedesyin the output.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
stable_partition_copyto reorder a sequence so that even numbers precede odd numbers using thethrust::hostexecution 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'svalue_typeis convertible toPredicate'sargument_typeandInputIterator'svalue_typeis convertible toOutputIterator1andOutputIterator2'svalue_types.OutputIterator1 – is a model of Output Iterator.
OutputIterator2 – is a model of Output Iterator.
Predicate – is a model of Predicate.
- Returns:
A
pairp such thatp.firstis the end of the output range beginning atout_trueandp.secondis 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_copydiffers fromstable_partitiononly in that the reordered sequence is written to different output sequences, rather than in place.stable_partition_copycopies the elements[first, last)based on the function objectpred. All of the elements that satisfypredare copied to the range beginning atout_trueand all the elements that fail to satisfy it are copied to the range beginning atout_false.stable_partition_copydiffers frompartition_copyin thatstable_partition_copyis guaranteed to preserve relative order. That is, ifxandyare elements in[first, last), such thatpred(x) == pred(y), and ifxprecedesy, then it will still be true afterstable_partition_copythatxprecedesyin the output.The following code snippet demonstrates how to use
stable_partition_copyto 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'svalue_typeis convertible toPredicate'sargument_typeandInputIterator'svalue_typeis convertible toOutputIterator1andOutputIterator2'svalue_types.OutputIterator1 – is a model of Output Iterator.
OutputIterator2 – is a model of Output Iterator.
Predicate – is a model of Predicate.
- Returns:
A
pairp such thatp.firstis the end of the output range beginning atout_trueandp.secondis 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_copydiffers fromstable_partitiononly in that the reordered sequence is written to different output sequences, rather than in place.stable_partition_copycopies the elements[first, last)based on the function objectpredwhich is applied to a range of stencil elements. All of the elements whose corresponding stencil element satisfiespredare copied to the range beginning atout_trueand all the elements whose stencil element fails to satisfy it are copied to the range beginning atout_false.stable_partition_copydiffers frompartition_copyin thatstable_partition_copyis guaranteed to preserve relative order. That is, ifxandyare elements in[first, last), such thatpred(x) == pred(y), and ifxprecedesy, then it will still be true afterstable_partition_copythatxprecedesyin the output.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
stable_partition_copyto reorder a sequence so that even numbers precede odd numbers using thethrust::hostexecution 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'svalue_typeis convertible toOutputIterator1andOutputIterator2'svalue_types.InputIterator2 – is a model of Input Iterator, and
InputIterator2'svalue_typeis convertible toPredicate'sargument_type.OutputIterator1 – is a model of Output Iterator.
OutputIterator2 – is a model of Output Iterator.
Predicate – is a model of Predicate.
- Returns:
A
pairp such thatp.firstis the end of the output range beginning atout_trueandp.secondis 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_copydiffers fromstable_partitiononly in that the reordered sequence is written to different output sequences, rather than in place.stable_partition_copycopies the elements[first, last)based on the function objectpredwhich is applied to a range of stencil elements. All of the elements whose corresponding stencil element satisfiespredare copied to the range beginning atout_trueand all the elements whose stencil element fails to satisfy it are copied to the range beginning atout_false.stable_partition_copydiffers frompartition_copyin thatstable_partition_copyis guaranteed to preserve relative order. That is, ifxandyare elements in[first, last), such thatpred(x) == pred(y), and ifxprecedesy, then it will still be true afterstable_partition_copythatxprecedesyin the output.The following code snippet demonstrates how to use
stable_partition_copyto 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'svalue_typeis convertible toOutputIterator1andOutputIterator2'svalue_types.InputIterator2 – is a model of Input Iterator, and
InputIterator2'svalue_typeis convertible toPredicate'sargument_type.OutputIterator1 – is a model of Output Iterator.
OutputIterator2 – is a model of Output Iterator.
Predicate – is a model of Predicate.
- Returns:
A
pairp such thatp.firstis the end of the output range beginning atout_trueandp.secondis 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_ifcopies elements from the range[first,last)to a range beginning atresult, except that any element which causespredto befalseis not copied.copy_ifis stable, meaning that the relative order of elements that are copied is unchanged.More precisely, for every integer
nsuch that0 <= n < last-first,copy_ifperforms the assignment*result = *(first+n)andresultis advanced one position ifpred(*(first+n)). Otherwise, no assignment occurs andresultis not advanced.The algorithm’s execution is parallelized as determined by
system.The following code snippet demonstrates how to use
copy_ifto perform stream compaction to copy even numbers to an output range using thethrust::hostparallelization 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'svalue_typeis convertible toPredicate'sargument_type.OutputIterator – is a model of Output Iterator.
Predicate – is a model of Predicate.
- Returns:
result + n, wherenis equal to the number of timespredevaluated totruein 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_ifcopies elements from the range[first,last)to a range beginning atresult, except that any element which causespredtofalseis not copied.copy_ifis stable, meaning that the relative order of elements that are copied is unchanged.More precisely, for every integer
nsuch that0 <= n < last-first,copy_ifperforms the assignment*result = *(first+n)andresultis advanced one position ifpred(*(first+n)). Otherwise, no assignment occurs andresultis not advanced.The following code snippet demonstrates how to use
copy_ifto 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'svalue_typeis convertible toPredicate'sargument_type.OutputIterator – is a model of Output Iterator.
Predicate – is a model of Predicate.
- Returns:
result + n, wherenis equal to the number of timespredevaluated totruein 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_ifcopies elements from the range[first,last)to a range beginning atresult, except that any element whose corresponding stencil element causespredto befalseis not copied.copy_ifis stable, meaning that the relative order of elements that are copied is unchanged.More precisely, for every integer
nsuch that0 <= n < last-first,copy_ifperforms the assignment*result = *(first+n)andresultis advanced one position ifpred(*(stencil+n)). Otherwise, no assignment occurs andresultis not advanced.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
copy_ifto perform stream compaction to copy numbers to an output range when corresponding stencil elements are even using thethrust::hostexecution 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'svalue_typeis convertible toPredicate'sargument_type.OutputIterator – is a model of Output Iterator.
Predicate – is a model of Predicate.
- Returns:
result + n, wherenis equal to the number of timespredevaluated totruein 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_ifcopies elements from the range[first,last)to a range beginning atresult, except that any element whose corresponding stencil element causespredto befalseis not copied.copy_ifis stable, meaning that the relative order of elements that are copied is unchanged.More precisely, for every integer
nsuch that0 <= n < last-first,copy_ifperforms the assignment*result = *(first+n)andresultis advanced one position ifpred(*(stencil+n)). Otherwise, no assignment occurs andresultis not advanced.The following code snippet demonstrates how to use
copy_ifto 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'svalue_typeis convertible toPredicate'sargument_type.OutputIterator – is a model of Output Iterator.
Predicate – is a model of Predicate.
- Returns:
result + n, wherenis equal to the number of timespredevaluated totruein 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)
removeremoves from the range[first, last)all elements that are equal tovalue. That is,removereturns an iteratornew_lastsuch 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.removeis stable, meaning that the relative order of elements that are not equal tovalueis unchanged.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
removeto remove a number of interest from a range using thethrust::hostexecution 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.
removedoes not destroy any iterators, and does not change the distance betweenfirstandlast. (There’s no way that it could do anything of the sort.) So, for example, ifVis a device_vector,remove(V.begin(), V.end(), 0)does not changeV.size():Vwill contain just as many elements as it did before.removereturns 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
ForwardIteratoris mutable.T – is a model of Equality Comparable, and objects of type
Tcan be compared for equality with objects ofForwardIterator'svalue_type.
- Returns:
A
ForwardIteratorpointing 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)# removeremoves from the range[first, last)all elements that are equal tovalue. That is,removereturns an iteratornew_lastsuch 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.removeis stable, meaning that the relative order of elements that are not equal tovalueis unchanged.The following code snippet demonstrates how to use
removeto 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.
removedoes not destroy any iterators, and does not change the distance betweenfirstandlast. (There’s no way that it could do anything of the sort.) So, for example, ifVis a device_vector,remove(V.begin(), V.end(), 0)does not changeV.size():Vwill contain just as many elements as it did before.removereturns 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
ForwardIteratoris mutable.T – is a model of Equality Comparable, and objects of type
Tcan be compared for equality with objects ofForwardIterator'svalue_type.
- Returns:
A
ForwardIteratorpointing 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_copycopies elements that are not equal tovaluefrom 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_copyto copy a sequence of numbers to an output range while omitting a value of interest using thethrust::hostexecution 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'svalue_typeis convertible to a type inOutputIterator'sset ofvalue_types.OutputIterator – is a model of Output Iterator.
T – is a model of Equality Comparable, and objects of type
Tcan be compared for equality with objects ofInputIterator'svalue_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_copycopies elements that are not equal tovaluefrom 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_copyto 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'svalue_typeis convertible to a type inOutputIterator'sset ofvalue_types.OutputIterator – is a model of Output Iterator.
T – is a model of Equality Comparable, and objects of type
Tcan be compared for equality with objects ofInputIterator'svalue_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_ifremoves from the range[first, last)every elementxsuch thatpred(x)istrue. That is,remove_ifreturns an iteratornew_lastsuch that the range[first,new_last)contains no elements for whichpredistrue. The iterators in the range[new_last,last)are all still dereferenceable, but the elements that they point to are unspecified.remove_ifis 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_ifto remove all even numbers from an array of integers using thethrust::hostexecution 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_ifdoes not destroy any iterators, and does not change the distance betweenfirstandlast. (There’s no way that it could do anything of the sort.) So, for example, ifVis a device_vector,remove_if(V.begin(), V.end(), pred)does not changeV.size():Vwill contain just as many elements as it did before.remove_ifreturns 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 whichpredevaluates totrueare removed from the sequence.
- Template Parameters:
DerivedPolicy – The name of the derived execution policy.
ForwardIterator – is a model of Forward Iterator,
ForwardIteratoris mutable, andForwardIterator'svalue_typeis convertible toPredicate'sargument_type.Predicate – is a model of Predicate.
- Returns:
A ForwardIterator pointing to the end of the resulting range of elements for which
predevaluated totrue.
-
template<typename ForwardIterator, typename Predicate>
ForwardIterator remove_if(ForwardIterator first, ForwardIterator last, Predicate pred)# remove_ifremoves from the range[first, last)every elementxsuch thatpred(x)istrue. That is,remove_ifreturns an iteratornew_lastsuch that the range[first,new_last)contains no elements for whichpredistrue. The iterators in the range[new_last,last)are all still dereferenceable, but the elements that they point to are unspecified.remove_ifis stable, meaning that the relative order of elements that are not removed is unchanged.The following code snippet demonstrates how to use
remove_ifto 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_ifdoes not destroy any iterators, and does not change the distance betweenfirstandlast. (There’s no way that it could do anything of the sort.) So, for example, ifVis a device_vector,remove_if(V.begin(), V.end(), pred)does not changeV.size():Vwill contain just as many elements as it did before.remove_ifreturns 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 whichpredevaluates totrueare removed from the sequence.
- Template Parameters:
ForwardIterator – is a model of Forward Iterator,
ForwardIteratoris mutable, andForwardIterator'svalue_typeis convertible toPredicate'sargument_type.Predicate – is a model of Predicate.
- Returns:
A ForwardIterator pointing to the end of the resulting range of elements for which
predevaluated 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_ifcopies elements from the range[first,last)to a range beginning atresult, except that elements for whichpredistrueare 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_ifto copy a sequence of numbers to an output range while omitting even numbers using thethrust::hostexecution 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 whichpredevaluates tofalseare not copied to the resulting sequence.
- Template Parameters:
DerivedPolicy – The name of the derived execution policy.
InputIterator – is a model of Input Iterator,
InputIterator'svalue_typeis convertible to a type inOutputIterator'sset ofvalue_types, andInputIterator'svalue_typeis convertible toPredicate'sargument_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_ifcopies elements from the range[first,last)to a range beginning atresult, except that elements for whichpredistrueare 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_ifto 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 whichpredevaluates tofalseare not copied to the resulting sequence.
- Template Parameters:
InputIterator – is a model of Input Iterator,
InputIterator'svalue_typeis convertible to a type inOutputIterator'sset ofvalue_types, andInputIterator'svalue_typeis convertible toPredicate'sargument_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_ifremoves from the range[first, last)every elementxsuch thatpred(x)istrue. That is,remove_ifreturns an iteratornew_lastsuch that the range[first, new_last)contains no elements for whichpredof 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_ifis 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_ifto remove specific elements from an array of integers using thethrust::hostexecution 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 whichpredevaluates totrueare removed from the sequence[first, last)
- Template Parameters:
DerivedPolicy – The name of the derived execution policy.
ForwardIterator – is a model of Forward Iterator and
ForwardIteratoris mutable.InputIterator – is a model of Input Iterator, and
InputIterator'svalue_typeis convertible toPredicate'sargument_type.Predicate – is a model of Predicate.
- Returns:
A ForwardIterator pointing to the end of the resulting range of elements for which
predevaluated 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_ifremoves from the range[first, last)every elementxsuch thatpred(x)istrue. That is,remove_ifreturns an iteratornew_lastsuch that the range[first, new_last)contains no elements for whichpredof 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_ifis stable, meaning that the relative order of elements that are not removed is unchanged.The following code snippet demonstrates how to use
remove_ifto 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 whichpredevaluates totrueare removed from the sequence[first, last)
- Template Parameters:
ForwardIterator – is a model of Forward Iterator and
ForwardIteratoris mutable.InputIterator – is a model of Input Iterator, and
InputIterator'svalue_typeis convertible toPredicate'sargument_type.Predicate – is a model of Predicate.
- Returns:
A ForwardIterator pointing to the end of the resulting range of elements for which
predevaluated 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_ifcopies elements from the range[first,last)to a range beginning atresult, except that elements for whichpredof the corresponding stencil value istrueare 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_ifto copy a sequence of numbers to an output range while omitting specific elements using thethrust::hostexecution 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 whichpredevaluates tofalseare not copied to the resulting sequence.
- Template Parameters:
DerivedPolicy – The name of the derived execution policy.
InputIterator1 – is a model of Input Iterator,
InputIterator1'svalue_typeis convertible to a type inOutputIterator'sset ofvalue_types.InputIterator2 – is a model of Input Iterator, and
InputIterator2'svalue_typeis convertible toPredicate'sargument_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_ifcopies elements from the range[first,last)to a range beginning atresult, except that elements for whichpredof the corresponding stencil value istrueare 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_ifto 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 whichpredevaluates tofalseare not copied to the resulting sequence.
- Template Parameters:
InputIterator1 – is a model of Input Iterator,
InputIterator1'svalue_typeis convertible to a type inOutputIterator'sset ofvalue_types.InputIterator2 – is a model of Input Iterator, and
InputIterator2'svalue_typeis convertible toPredicate'sargument_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,uniqueremoves all but the first element of the group. The return value is an iteratornew_lastsuch 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.uniqueis stable, meaning that the relative order of elements that are not removed is unchanged.This version of
uniqueusesoperator==to test for equality.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
uniqueto compact a sequence of numbers to remove consecutive duplicates using thethrust::hostexecution 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
ForwardIteratoris mutable, andForwardIterator'svalue_typeis 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,uniqueremoves all but the first element of the group. The return value is an iteratornew_lastsuch 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.uniqueis stable, meaning that the relative order of elements that are not removed is unchanged.This version of
uniqueusesoperator==to test for equality.The following code snippet demonstrates how to use
uniqueto 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
ForwardIteratoris mutable, andForwardIterator'svalue_typeis 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,uniqueremoves all but the first element of the group. The return value is an iteratornew_lastsuch 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.uniqueis stable, meaning that the relative order of elements that are not removed is unchanged.This version of
uniqueuses the function objectbinary_predto test for equality.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
uniqueto compact a sequence of numbers to remove consecutive duplicates using thethrust::hostexecution 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
ForwardIteratoris mutable, andForwardIterator'svalue_typeis convertible toBinaryPredicate'sfirst_argument_typeand toBinaryPredicate'ssecond_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,uniqueremoves all but the first element of the group. The return value is an iteratornew_lastsuch 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.uniqueis stable, meaning that the relative order of elements that are not removed is unchanged.This version of
uniqueuses the function objectbinary_predto test for equality.The following code snippet demonstrates how to use
uniqueto 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
ForwardIteratoris mutable, andForwardIterator'svalue_typeis convertible toBinaryPredicate'sfirst_argument_typeand toBinaryPredicate'ssecond_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_copycopies 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 iteratoriin the range, eitheri == for else*i == *(i-1). In the second, the test is an arbitraryBinaryPredicatebinary_pred:the elements in[f, l)are duplicates if, for every iteratoriin the range, eitheri == for elsebinary_pred(*i, *(i-1))istrue.This version of
unique_copyusesoperator==to test for equality.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
unique_copyto compact a sequence of numbers to remove consecutive duplicates using thethrust::hostexecution 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'svalue_typeis a model of Equality Comparable.OutputIterator – is a model of Output Iterator and and
InputIterator'svalue_typeis convertible toOutputIterator'svalue_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_copycopies 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 iteratoriin the range, eitheri == for else*i == *(i-1). In the second, the test is an arbitraryBinaryPredicatebinary_pred:the elements in[f, l)are duplicates if, for every iteratoriin the range, eitheri == for elsebinary_pred(*i, *(i-1))istrue.This version of
unique_copyusesoperator==to test for equality.The following code snippet demonstrates how to use
unique_copyto 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'svalue_typeis a model of Equality Comparable.OutputIterator – is a model of Output Iterator and and
InputIterator'svalue_typeis convertible toOutputIterator'svalue_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_copycopies 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_copyuses the function objectbinary_predto test for equality.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
unique_copyto compact a sequence of numbers to remove consecutive duplicates using thethrust::hostexecution 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'svalue_typeis a model of Equality Comparable.OutputIterator – is a model of Output Iterator and and
InputIterator'svalue_typeis convertible toOutputIterator'svalue_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_copycopies 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_copyuses the function objectbinary_predto test for equality.The following code snippet demonstrates how to use
unique_copyto 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'svalue_typeis a model of Equality Comparable.OutputIterator – is a model of Output Iterator and and
InputIterator'svalue_typeis convertible toOutputIterator'svalue_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_keyis a generalization ofuniqueto key-value pairs. For each group of consecutive keys in the range[keys_first, keys_last)that are equal,unique_by_keyremoves 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
pairof 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_keyusesoperator==to test for equality andproject1stto 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_keyto compact a sequence of key/value pairs to remove consecutive duplicates using thethrust::hostexecution 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
ForwardIterator1is mutable, andForwardIterator'svalue_typeis a model of Equality Comparable.ForwardIterator2 – is a model of Forward Iterator, and
ForwardIterator2is 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_keyis a generalization ofuniqueto key-value pairs. For each group of consecutive keys in the range[keys_first, keys_last)that are equal,unique_by_keyremoves 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
pairof 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_keyusesoperator==to test for equality andproject1stto reduce values with equal keys.The following code snippet demonstrates how to use
unique_by_keyto 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
ForwardIterator1is mutable, andForwardIterator'svalue_typeis a model of Equality Comparable.ForwardIterator2 – is a model of Forward Iterator, and
ForwardIterator2is 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_keyis a generalization ofuniqueto key-value pairs. For each group of consecutive keys in the range[keys_first, keys_last)that are equal,unique_by_keyremoves 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_keyuses the function objectbinary_predto test for equality andproject1stto 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_keyto compact a sequence of key/value pairs to remove consecutive duplicates using thethrust::hostexecution 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
ForwardIterator1is mutable, andForwardIterator'svalue_typeis a model of Equality Comparable.ForwardIterator2 – is a model of Forward Iterator, and
ForwardIterator2is 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_keyis a generalization ofuniqueto key-value pairs. For each group of consecutive keys in the range[keys_first, keys_last)that are equal,unique_by_keyremoves 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_keyuses the function objectbinary_predto test for equality andproject1stto reduce values with equal keys.The following code snippet demonstrates how to use
unique_by_keyto 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
ForwardIterator1is mutable, andForwardIterator'svalue_typeis a model of Equality Comparable.ForwardIterator2 – is a model of Forward Iterator, and
ForwardIterator2is 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_copyis a generalization ofunique_copyto key-value pairs. For each group of consecutive keys in the range[keys_first, keys_last)that are equal,unique_by_key_copycopies the first element of the group to a range beginning withkeys_resultand 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_copyusesoperator==to test for equality andproject1stto 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_copyto compact a sequence of key/value pairs and with equal keys using thethrust::hostexecution 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'svalue_typeis convertible toOutputIterator1'svalue_type.OutputIterator2 – is a model of Output Iterator and and
InputIterator2'svalue_typeis convertible toOutputIterator2'svalue_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_copyis a generalization ofunique_copyto key-value pairs. For each group of consecutive keys in the range[keys_first, keys_last)that are equal,unique_by_key_copycopies the first element of the group to a range beginning withkeys_resultand 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_copyusesoperator==to test for equality andproject1stto reduce values with equal keys.The following code snippet demonstrates how to use
unique_by_key_copyto 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'svalue_typeis convertible toOutputIterator1'svalue_type.OutputIterator2 – is a model of Output Iterator and and
InputIterator2'svalue_typeis convertible toOutputIterator2'svalue_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_copyis a generalization ofunique_copyto key-value pairs. For each group of consecutive keys in the range[keys_first, keys_last)that are equal,unique_by_key_copycopies the first element of the group to a range beginning withkeys_resultand 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_copyuses the function objectbinary_predto test for equality andproject1stto 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_copyto compact a sequence of key/value pairs and with equal keys using thethrust::hostexecution 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'svalue_typeis convertible toOutputIterator1'svalue_type.OutputIterator2 – is a model of Output Iterator and and
InputIterator2'svalue_typeis convertible toOutputIterator2'svalue_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_copyis a generalization ofunique_copyto key-value pairs. For each group of consecutive keys in the range[keys_first, keys_last)that are equal,unique_by_key_copycopies the first element of the group to a range beginning withkeys_resultand 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_copyuses the function objectbinary_predto test for equality andproject1stto reduce values with equal keys.The following code snippet demonstrates how to use
unique_by_key_copyto 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'svalue_typeis convertible toOutputIterator1'svalue_type.OutputIterator2 – is a model of Output Iterator and and
InputIterator2'svalue_typeis convertible toOutputIterator2'svalue_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_countcounts runs of equal elements in the range[first, last)with the same value,This version of
unique_countuses the function objectbinary_predto test for equality.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
unique_countto determine a number of runs of equal elements using thethrust::hostexecution 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'svalue_typeis convertible toBinaryPredicate'sfirst_argument_typeand toBinaryPredicate'ssecond_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_countcounts runs of equal elements in the range[first, last)with the same value,This version of
unique_countusesoperator==to test for equality.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
unique_countto determine the number of runs of equal elements using thethrust::hostexecution 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'svalue_typeis convertible toBinaryPredicate'sfirst_argument_typeand toBinaryPredicate'ssecond_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_countcounts runs of equal elements in the range[first, last)with the same value,This version of
unique_countuses the function objectbinary_predto test for equality.The following code snippet demonstrates how to use
unique_countto 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'svalue_typeis convertible toBinaryPredicate'sfirst_argument_typeand toBinaryPredicate'ssecond_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_countcounts runs of equal elements in the range[first, last)with the same value,This version of
unique_countusesoperator==to test for equality.The following code snippet demonstrates how to use
unique_countto 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'svalue_typeis convertible toBinaryPredicate'sfirst_argument_typeand toBinaryPredicate'ssecond_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_scancomputes an inclusive prefix sum operation. The term ‘inclusive’ means that each result includes the corresponding input operand in the partial sum. More precisely,*firstis assigned to*resultand the sum of*firstand*(first + 1)is assigned to*(result + 1), and so on. This version ofinclusive_scanassumes plus as the associative operator. When the input and output sequences are the same, the scan is performed in-place.inclusive_scanis similar tostd::partial_sumin the STL. The primary difference between the two functions is thatstd::partial_sumguarantees a serial summation order, whileinclusive_scanrequires 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_scanto compute an in-place prefix sum using thethrust::hostexecution 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'svalue_typeis convertible toOutputIterator'svalue_type.OutputIterator – is a model of Output Iterator, and if
xandyare objects ofOutputIterator'svalue_type, thenx + yis defined. IfTisOutputIterator'svalue_type, thenT(0)is defined.
- Returns:
The end of the output sequence.
- Pre:
firstmay equalresultbut 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_scancomputes an inclusive prefix sum operation. The term ‘inclusive’ means that each result includes the corresponding input operand in the partial sum. More precisely,*firstis assigned to*resultand the sum of*firstand*(first + 1)is assigned to*(result + 1), and so on. This version ofinclusive_scanassumes plus as the associative operator. When the input and output sequences are the same, the scan is performed in-place.inclusive_scanis similar tostd::partial_sumin the STL. The primary difference between the two functions is thatstd::partial_sumguarantees a serial summation order, whileinclusive_scanrequires 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'svalue_typeis convertible toOutputIterator'svalue_type.OutputIterator – is a model of Output Iterator, and if
xandyare objects ofOutputIterator'svalue_type, thenx + yis defined. IfTisOutputIterator'svalue_type, thenT(0)is defined.
- Returns:
The end of the output sequence.
- Pre:
firstmay equalresultbut 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_scancomputes 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_scanis similar tostd::partial_sumin the STL. The primary difference between the two functions is thatstd::partial_sumguarantees a serial summation order, whileinclusive_scanrequires 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_scanto compute an in-place prefix sum using thethrust::hostexecution 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'svalue_typeis convertible toOutputIterator'svalue_type.OutputIterator – is a model of Output Iterator and
OutputIterator'svalue_typeis convertible to bothAssociativeOperator'sfirst_argument_typeandsecond_argument_type.AssociativeOperator – is a model of Binary Function and
AssociativeOperator'sresult_typeis convertible toOutputIterator'svalue_type.
- Returns:
The end of the output sequence.
- Pre:
firstmay equalresultbut 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_scancomputes 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_scanis similar tostd::partial_sumin the STL. The primary difference between the two functions is thatstd::partial_sumguarantees a serial summation order, whileinclusive_scanrequires 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_scanint 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'svalue_typeis convertible toOutputIterator'svalue_type.OutputIterator – is a model of Output Iterator and
OutputIterator'svalue_typeis convertible to bothAssociativeOperator'sfirst_argument_typeandsecond_argument_type.AssociativeOperator – is a model of Binary Function and
AssociativeOperator'sresult_typeis convertible toOutputIterator'svalue_type.
- Returns:
The end of the output sequence.
- Pre:
firstmay equalresultbut 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_scancomputes 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,0is assigned to*resultand the sum of0and*firstis assigned to*(result + 1), and so on. This version ofexclusive_scanassumes plus as the associative operator and0as 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_scanto compute an in-place prefix sum using thethrust::hostexecution 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'svalue_typeis convertible toOutputIterator'svalue_type.OutputIterator – is a model of Output Iterator, and if
xandyare objects ofOutputIterator'svalue_type, thenx + yis defined. IfTisOutputIterator'svalue_type, thenT(0)is defined.
- Returns:
The end of the output sequence.
- Pre:
firstmay equalresultbut 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_scancomputes 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,0is assigned to*resultand the sum of0and*firstis assigned to*(result + 1), and so on. This version ofexclusive_scanassumes plus as the associative operator and0as 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'svalue_typeis convertible toOutputIterator'svalue_type.OutputIterator – is a model of Output Iterator, and if
xandyare objects ofOutputIterator'svalue_type, thenx + yis defined. IfTisOutputIterator'svalue_type, thenT(0)is defined.
- Returns:
The end of the output sequence.
- Pre:
firstmay equalresultbut 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_scancomputes 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,initis assigned to*resultand the sum ofinitand*firstis assigned to*(result + 1), and so on. This version ofexclusive_scanassumes 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_scanto compute an in-place prefix sum using thethrust::hostexecution 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'svalue_typeis convertible toOutputIterator'svalue_type.OutputIterator – is a model of Output Iterator, and if
xandyare objects ofOutputIterator'svalue_type, thenx + yis defined.T – is convertible to
OutputIterator'svalue_type.
- Returns:
The end of the output sequence.
- Pre:
firstmay equalresultbut 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_scancomputes 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,initis assigned to*resultand the sum ofinitand*firstis assigned to*(result + 1), and so on. This version ofexclusive_scanassumes 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'svalue_typeis convertible toOutputIterator'svalue_type.OutputIterator – is a model of Output Iterator, and if
xandyare objects ofOutputIterator'svalue_type, thenx + yis defined.T – is convertible to
OutputIterator'svalue_type.
- Returns:
The end of the output sequence.
- Pre:
firstmay equalresultbut 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_scancomputes 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,initis assigned to*resultand 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_scanto compute an in-place prefix sum using thethrust::hostexecution 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'svalue_typeis convertible toOutputIterator'svalue_type.OutputIterator – is a model of Output Iterator and
OutputIterator'svalue_typeis convertible to bothAssociativeOperator'sfirst_argument_typeandsecond_argument_type.T – is convertible to
OutputIterator'svalue_type.AssociativeOperator – is a model of Binary Function and
AssociativeOperator'sresult_typeis convertible toOutputIterator'svalue_type.
- Returns:
The end of the output sequence.
- Pre:
firstmay equalresultbut 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_scancomputes 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,initis assigned to*resultand 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'svalue_typeis convertible toOutputIterator'svalue_type.OutputIterator – is a model of Output Iterator and
OutputIterator'svalue_typeis convertible to bothAssociativeOperator'sfirst_argument_typeandsecond_argument_type.T – is convertible to
OutputIterator'svalue_type.AssociativeOperator – is a model of Binary Function and
AssociativeOperator'sresult_typeis convertible toOutputIterator'svalue_type.
- Returns:
The end of the output sequence.
- Pre:
firstmay equalresultbut 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_keycomputes 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_keyassumesequal_toas the binary predicate used to compare adjacent keys. Specifically, consecutive iteratorsiandi+1in 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_keyassumesplusas 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_keyusing thethrust::hostexecution 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'svalue_typeis convertible toOutputIterator'svalue_type.OutputIterator – is a model of Output Iterator, and if
xandyare objects ofOutputIterator'svalue_type, thenbinary_op(x,y)is defined.
- Returns:
The end of the output sequence.
- Pre:
first1may equalresultbut the range[first1, last1)and the range[result, result + (last1 - first1))shall not overlap otherwise.- Pre:
first2may equalresultbut 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_keycomputes 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_keyassumesequal_toas the binary predicate used to compare adjacent keys. Specifically, consecutive iteratorsiandi+1in 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_keyassumesplusas 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'svalue_typeis convertible toOutputIterator'svalue_type.OutputIterator – is a model of Output Iterator, and if
xandyare objects ofOutputIterator'svalue_type, thenbinary_op(x,y)is defined.
- Returns:
The end of the output sequence.
- Pre:
first1may equalresultbut the range[first1, last1)and the range[result, result + (last1 - first1))shall not overlap otherwise.- Pre:
first2may equalresultbut 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_keycomputes 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_keyuses the binary predicatepredto compare adjacent keys. Specifically, consecutive iteratorsiandi+1in 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_keyassumesplusas 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_keyusing thethrust::hostexecution 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'svalue_typeis convertible toOutputIterator'svalue_type.OutputIterator – is a model of Output Iterator, and if
xandyare objects ofOutputIterator'svalue_type, thenbinary_op(x,y)is defined.BinaryPredicate – is a model of Binary Predicate.
- Returns:
The end of the output sequence.
- Pre:
first1may equalresultbut the range[first1, last1)and the range[result, result + (last1 - first1))shall not overlap otherwise.- Pre:
first2may equalresultbut 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_keycomputes 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_keyuses the binary predicatepredto compare adjacent keys. Specifically, consecutive iteratorsiandi+1in 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_keyassumesplusas 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'svalue_typeis convertible toOutputIterator'svalue_type.OutputIterator – is a model of Output Iterator, and if
xandyare objects ofOutputIterator'svalue_type, thenbinary_op(x,y)is defined.BinaryPredicate – is a model of Binary Predicate.
- Returns:
The end of the output sequence.
- Pre:
first1may equalresultbut the range[first1, last1)and the range[result, result + (last1 - first1))shall not overlap otherwise.- Pre:
first2may equalresultbut 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_keycomputes 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_keyuses the binary predicatepredto compare adjacent keys. Specifically, consecutive iteratorsiandi+1in 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_keyuses the associative operatorbinary_opto 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_keyusing thethrust::hostexecution 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'svalue_typeis convertible toOutputIterator'svalue_type.OutputIterator – is a model of Output Iterator, and if
xandyare objects ofOutputIterator'svalue_type, thenbinary_op(x,y)is defined.BinaryPredicate – is a model of Binary Predicate.
AssociativeOperator – is a model of Binary Function and
AssociativeOperator'sresult_typeis convertible toOutputIterator'svalue_type.
- Returns:
The end of the output sequence.
- Pre:
first1may equalresultbut the range[first1, last1)and the range[result, result + (last1 - first1))shall not overlap otherwise.- Pre:
first2may equalresultbut 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_keycomputes 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_keyuses the binary predicatepredto compare adjacent keys. Specifically, consecutive iteratorsiandi+1in 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_keyuses the associative operatorbinary_opto 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'svalue_typeis convertible toOutputIterator'svalue_type.OutputIterator – is a model of Output Iterator, and if
xandyare objects ofOutputIterator'svalue_type, thenbinary_op(x,y)is defined.BinaryPredicate – is a model of Binary Predicate.
AssociativeOperator – is a model of Binary Function and
AssociativeOperator'sresult_typeis convertible toOutputIterator'svalue_type.
- Returns:
The end of the output sequence.
- Pre:
first1may equalresultbut the range[first1, last1)and the range[result, result + (last1 - first1))shall not overlap otherwise.- Pre:
first2may equalresultbut 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_keycomputes an exclusive segmented prefixThis version of
exclusive_scan_by_keyuses the value0to initialize the exclusive scan operation.This version of
exclusive_scan_by_keyassumesplusas 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_keyassumesequal_toas the binary predicate used to compare adjacent keys. Specifically, consecutive iteratorsiandi+1in the range[first1, last1belong 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_keyfor additional details.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
exclusive_scan_by_keyusing thethrust::hostexecution 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:
first1may equalresultbut the range[first1, last1)and the range[result, result + (last1 - first1))shall not overlap otherwise.- Pre:
first2may equalresultbut 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_keycomputes an exclusive segmented prefixThis version of
exclusive_scan_by_keyuses the value0to initialize the exclusive scan operation.This version of
exclusive_scan_by_keyassumesplusas 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_keyassumesequal_toas the binary predicate used to compare adjacent keys. Specifically, consecutive iteratorsiandi+1in the range[first1, last1belong 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_keyfor 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:
first1may equalresultbut the range[first1, last1)and the range[result, result + (last1 - first1))shall not overlap otherwise.- Pre:
first2may equalresultbut 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_keycomputes 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_keyuses the valueinitto 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_keyusing thethrust::hostexecution 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:
first1may equalresultbut the range[first1, last1)and the range[result, result + (last1 - first1))shall not overlap otherwise.- Pre:
first2may equalresultbut 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_keycomputes 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_keyuses the valueinitto 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:
first1may equalresultbut the range[first1, last1)and the range[result, result + (last1 - first1))shall not overlap otherwise.- Pre:
first2may equalresultbut 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_keycomputes 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_keyuses the valueinitto initialize the exclusive scan operation.This version of
exclusive_scan_by_keyuses the binary predicatebinary_predto compare adjacent keys. Specifically, consecutive iteratorsiandi+1in 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_keyusing thethrust::hostexecution 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:
first1may equalresultbut the range[first1, last1)and the range[result, result + (last1 - first1))shall not overlap otherwise.- Pre:
first2may equalresultbut 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_keycomputes 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_keyuses the valueinitto initialize the exclusive scan operation.This version of
exclusive_scan_by_keyuses the binary predicatebinary_predto compare adjacent keys. Specifically, consecutive iteratorsiandi+1in 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:
first1may equalresultbut the range[first1, last1)and the range[result, result + (last1 - first1))shall not overlap otherwise.- Pre:
first2may equalresultbut 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_keycomputes 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_keyuses the valueinitto initialize the exclusive scan operation.This version of
exclusive_scan_by_keyuses the binary predicatebinary_predto compare adjacent keys. Specifically, consecutive iteratorsiandi+1in 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_keyuses the associative operatorbinary_opto 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_keyusing thethrust::hostexecution 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'svalue_typeis convertible toOutputIterator'svalue_type.OutputIterator – is a model of Output Iterator, and if
xandyare objects ofOutputIterator'svalue_type, thenbinary_op(x,y)is defined.T – is convertible to
OutputIterator'svalue_type.BinaryPredicate – is a model of Binary Predicate.
AssociativeOperator – is a model of Binary Function and
AssociativeOperator'sresult_typeis convertible toOutputIterator'svalue_type.
- Returns:
The end of the output sequence.
- Pre:
first1may equalresultbut the range[first1, last1)and the range[result, result + (last1 - first1))shall not overlap otherwise.- Pre:
first2may equalresultbut 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_keycomputes 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_keyuses the valueinitto initialize the exclusive scan operation.This version of
exclusive_scan_by_keyuses the binary predicatebinary_predto compare adjacent keys. Specifically, consecutive iteratorsiandi+1in 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_keyuses the associative operatorbinary_opto 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'svalue_typeis convertible toOutputIterator'svalue_type.OutputIterator – is a model of Output Iterator, and if
xandyare objects ofOutputIterator'svalue_type, thenbinary_op(x,y)is defined.T – is convertible to
OutputIterator'svalue_type.BinaryPredicate – is a model of Binary Predicate.
AssociativeOperator – is a model of Binary Function and
AssociativeOperator'sresult_typeis convertible toOutputIterator'svalue_type.
- Returns:
The end of the output sequence.
- Pre:
first1may equalresultbut the range[first1, last1)and the range[result, result + (last1 - first1))shall not overlap otherwise.- Pre:
first2may equalresultbut 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_scanfuses thetransformandinclusive_scanoperations.transform_inclusive_scanis equivalent to performing a tranformation defined byunary_opinto a temporary sequence and then performing aninclusive_scanon 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*resultand 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.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
transform_inclusive_scanusing thethrust::hostexecution 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'svalue_typeis convertible tounary_op'sinput type.OutputIterator – is a model of Output Iterator.
UnaryFunction – is a model of Unary Function and accepts inputs of
InputIterator'svalue_type.UnaryFunction'sresult_type is convertable toOutputIterator'svalue_type.AssociativeOperator – is a model of Binary Function and
AssociativeOperator'sresult_typeis convertible toOutputIterator'svalue_type.
- Returns:
The end of the output sequence.
- Pre:
firstmay 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_scanfuses thetransformandinclusive_scanoperations.transform_inclusive_scanis equivalent to performing a tranformation defined byunary_opinto a temporary sequence and then performing aninclusive_scanon 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*resultand 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.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'svalue_typeis convertible tounary_op'sinput type.OutputIterator – is a model of Output Iterator.
UnaryFunction – is a model of Unary Function and accepts inputs of
InputIterator'svalue_type.UnaryFunction'sresult_type is convertable toOutputIterator'svalue_type.AssociativeOperator – is a model of Binary Function and
AssociativeOperator'sresult_typeis convertible toOutputIterator'svalue_type.
- Returns:
The end of the output sequence.
- Pre:
firstmay 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_scanfuses thetransformandexclusive_scanoperations.transform_exclusive_scanis equivalent to performing a tranformation defined byunary_opinto a temporary sequence and then performing anexclusive_scanon 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,initis assigned to*resultand 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.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
transform_exclusive_scanusing thethrust::hostexecution 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_scanbinary_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'svalue_typeis convertible tounary_op'sinput type.OutputIterator – is a model of Output Iterator.
UnaryFunction – is a model of Unary Function and accepts inputs of
InputIterator'svalue_type.UnaryFunction'sresult_type is convertable toOutputIterator'svalue_type.T – is convertible to
OutputIterator'svalue_type.AssociativeOperator – is a model of Binary Function and
AssociativeOperator'sresult_typeis convertible toOutputIterator'svalue_type.
- Returns:
The end of the output sequence.
- Pre:
firstmay 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_scanfuses thetransformandexclusive_scanoperations.transform_exclusive_scanis equivalent to performing a tranformation defined byunary_opinto a temporary sequence and then performing anexclusive_scanon 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,initis assigned to*resultand 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.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_scanbinary_op – The associatve operator used to ‘sum’ transformed values.
- Template Parameters:
InputIterator – is a model of Input Iterator and
InputIterator'svalue_typeis convertible tounary_op'sinput type.OutputIterator – is a model of Output Iterator.
UnaryFunction – is a model of Unary Function and accepts inputs of
InputIterator'svalue_type.UnaryFunction'sresult_type is convertable toOutputIterator'svalue_type.T – is convertible to
OutputIterator'svalue_type.AssociativeOperator – is a model of Binary Function and
AssociativeOperator'sresult_typeis convertible toOutputIterator'svalue_type.
- Returns:
The end of the output sequence.
- Pre:
firstmay 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_differenceconstructs 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_differenceperforms 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)containsmelements that are equivalent to each other and if[first2, last2)containsnelements 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_differencecompares elements usingoperator<.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
set_differenceto compute the set difference of two sets of integers sorted in ascending order using thethrust::hostexecution 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
includesSee 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,
InputIterator1andInputIterator2have the samevalue_type,InputIterator1'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator1'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator2 – is a model of Input Iterator,
InputIterator2andInputIterator1have the samevalue_type,InputIterator2'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator2'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2'svalue_typeis convertable to a type inOutputIterator'sset 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_differenceconstructs 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_differenceperforms 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)containsmelements that are equivalent to each other and if[first2, last2)containsnelements 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_differencecompares elements usingoperator<.The following code snippet demonstrates how to use
set_differenceto 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
includesSee 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,
InputIterator1andInputIterator2have the samevalue_type,InputIterator1'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator1'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator2 – is a model of Input Iterator,
InputIterator2andInputIterator1have the samevalue_type,InputIterator2'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator2'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2'svalue_typeis convertable to a type inOutputIterator'sset 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_differenceconstructs 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_differenceperforms 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)containsmelements that are equivalent to each other and if[first2, last2)containsnelements 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_differencecompares elements using a function objectcomp.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
set_differenceto compute the set difference of two sets of integers sorted in descending order using thethrust::hostexecution 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
includesSee 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'svalue_typeis convertable toStrictWeakCompare'sfirst_argument_type. andInputIterator1'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator2 – is a model of Input Iterator,
InputIterator2'svalue_typeis convertable toStrictWeakCompare'ssecond_argument_type. andInputIterator2'svalue_typeis convertable to a type inOutputIterator'sset 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_differenceconstructs 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_differenceperforms 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)containsmelements that are equivalent to each other and if[first2, last2)containsnelements 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_differencecompares elements using a function objectcomp.The following code snippet demonstrates how to use
set_differenceto 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
includesSee 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'svalue_typeis convertable toStrictWeakCompare'sfirst_argument_type. andInputIterator1'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator2 – is a model of Input Iterator,
InputIterator2'svalue_typeis convertable toStrictWeakCompare'ssecond_argument_type. andInputIterator2'svalue_typeis convertable to a type inOutputIterator'sset 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_intersectionconstructs 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_intersectionperforms 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 appearsmtimes in[first1, last1)andntimes in[first2, last2)(wheremmay be zero), then it appearsmin(m,n)times in the output range.set_intersectionis 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_intersectioncompares objects usingoperator<.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
set_intersectionto compute the set intersection of two sets of integers sorted in ascending order using thethrust::hostexecution 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
includesSee 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,
InputIterator1andInputIterator2have the samevalue_type,InputIterator1'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator1'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator2 – is a model of Input Iterator,
InputIterator2andInputIterator1have the samevalue_type,InputIterator2'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator2'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2'svalue_typeis convertable to a type inOutputIterator'sset 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_intersectionconstructs 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_intersectionperforms 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 appearsmtimes in[first1, last1)andntimes in[first2, last2)(wheremmay be zero), then it appearsmin(m,n)times in the output range.set_intersectionis 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_intersectioncompares objects usingoperator<.The following code snippet demonstrates how to use
set_intersectionto 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
includesSee 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,
InputIterator1andInputIterator2have the samevalue_type,InputIterator1'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator1'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator2 – is a model of Input Iterator,
InputIterator2andInputIterator1have the samevalue_type,InputIterator2'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator2'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2'svalue_typeis convertable to a type inOutputIterator'sset 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_intersectionconstructs 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_intersectionperforms 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 appearsmtimes in[first1, last1)andntimes in[first2, last2)(wheremmay be zero), then it appearsmin(m,n)times in the output range.set_intersectionis 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_intersectioncompares elements using a function objectcomp.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
set_intersectionto compute the set intersection of sets of integers sorted in descending order using thethrust::hostexecution 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
includesSee 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,
InputIterator1andInputIterator2have the samevalue_type,InputIterator1'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator1'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator2 – is a model of Input Iterator,
InputIterator2andInputIterator1have the samevalue_type,InputIterator2'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator2'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2'svalue_typeis convertable to a type inOutputIterator'sset 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_intersectionconstructs 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_intersectionperforms 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 appearsmtimes in[first1, last1)andntimes in[first2, last2)(wheremmay be zero), then it appearsmin(m,n)times in the output range.set_intersectionis 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_intersectioncompares elements using a function objectcomp.The following code snippet demonstrates how to use
set_intersectionto 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
includesSee 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,
InputIterator1andInputIterator2have the samevalue_type,InputIterator1'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator1'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator2 – is a model of Input Iterator,
InputIterator2andInputIterator1have the samevalue_type,InputIterator2'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator2'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2'svalue_typeis convertable to a type inOutputIterator'sset 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_differenceconstructs 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_differenceperforms 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)containsmelements that are equivalent to each other and[first2, last1)containsnelements that are equivalent to them, then|m - n|of those elements shall be copied to the output range: the lastm - nelements from[first1, last1)ifm > n, and the lastn - mof these elements from[first2, last2)ifm < n.This version of
set_unioncompares elements usingoperator<.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
set_symmetric_differenceto compute the symmetric difference of two sets of integers sorted in ascending order using thethrust::hostexecution 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
includesSee 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,
InputIterator1andInputIterator2have the samevalue_type,InputIterator1'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator1'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator2 – is a model of Input Iterator,
InputIterator2andInputIterator1have the samevalue_type,InputIterator2'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator2'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2'svalue_typeis convertable to a type inOutputIterator'sset 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_differenceconstructs 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_differenceperforms 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)containsmelements that are equivalent to each other and[first2, last1)containsnelements that are equivalent to them, then|m - n|of those elements shall be copied to the output range: the lastm - nelements from[first1, last1)ifm > n, and the lastn - mof these elements from[first2, last2)ifm < n.This version of
set_unioncompares elements usingoperator<.The following code snippet demonstrates how to use
set_symmetric_differenceto 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
includesSee 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,
InputIterator1andInputIterator2have the samevalue_type,InputIterator1'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator1'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator2 – is a model of Input Iterator,
InputIterator2andInputIterator1have the samevalue_type,InputIterator2'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator2'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2'svalue_typeis convertable to a type inOutputIterator'sset 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_differenceconstructs 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_differenceperforms 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)containsmelements that are equivalent to each other and[first2, last1)containsnelements that are equivalent to them, then|m - n|of those elements shall be copied to the output range: the lastm - nelements from[first1, last1)ifm > n, and the lastn - mof these elements from[first2, last2)ifm < n.This version of
set_unioncompares 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_differenceto compute the symmetric difference of two sets of integers sorted in descending order using thethrust::hostexecution 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
includesSee 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,
InputIterator1andInputIterator2have the samevalue_type,InputIterator1'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator1'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator2 – is a model of Input Iterator,
InputIterator2andInputIterator1have the samevalue_type,InputIterator2'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator2'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2'svalue_typeis convertable to a type inOutputIterator'sset 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_differenceconstructs 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_differenceperforms 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)containsmelements that are equivalent to each other and[first2, last1)containsnelements that are equivalent to them, then|m - n|of those elements shall be copied to the output range: the lastm - nelements from[first1, last1)ifm > n, and the lastn - mof these elements from[first2, last2)ifm < n.This version of
set_unioncompares elements using a function objectcomp.The following code snippet demonstrates how to use
set_symmetric_differenceto 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
includesSee 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,
InputIterator1andInputIterator2have the samevalue_type,InputIterator1'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator1'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator2 – is a model of Input Iterator,
InputIterator2andInputIterator1have the samevalue_type,InputIterator2'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator2'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2'svalue_typeis convertable to a type inOutputIterator'sset 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_unionconstructs 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_unionperforms 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)containsmelements that are equivalent to each other and if[first2, last2)containsnelements that are equivalent to them, then allmelements 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_unioncompares elements usingoperator<.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
set_unionto compute the union of two sets of integers sorted in ascending order using thethrust::hostexecution 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
includesSee 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,
InputIterator1andInputIterator2have the samevalue_type,InputIterator1'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator1'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator2 – is a model of Input Iterator,
InputIterator2andInputIterator1have the samevalue_type,InputIterator2'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator2'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2'svalue_typeis convertable to a type inOutputIterator'sset 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_unionconstructs 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_unionperforms 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)containsmelements that are equivalent to each other and if[first2, last2)containsnelements that are equivalent to them, then allmelements 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_unioncompares elements usingoperator<.The following code snippet demonstrates how to use
set_unionto 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
includesSee 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,
InputIterator1andInputIterator2have the samevalue_type,InputIterator1'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator1'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator2 – is a model of Input Iterator,
InputIterator2andInputIterator1have the samevalue_type,InputIterator2'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator2'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2'svalue_typeis convertable to a type inOutputIterator'sset 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_unionconstructs 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_unionperforms 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)containsmelements that are equivalent to each other and if[first2, last2)containsnelements that are equivalent to them, then allmelements 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_unioncompares elements using a function objectcomp.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
set_unionto compute the union of two sets of integers sorted in ascending order using thethrust::hostexecution 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
includesSee 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'svalue_typeis convertable toStrictWeakCompare'sfirst_argument_type. andInputIterator1'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator2 – is a model of Input Iterator,
InputIterator2'svalue_typeis convertable toStrictWeakCompare'ssecond_argument_type. andInputIterator2'svalue_typeis convertable to a type inOutputIterator'sset 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_unionconstructs 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_unionperforms 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)containsmelements that are equivalent to each other and if[first2, last2)containsnelements that are equivalent to them, then allmelements 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_unioncompares elements using a function objectcomp.The following code snippet demonstrates how to use
set_unionto 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
includesSee 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'svalue_typeis convertable toStrictWeakCompare'sfirst_argument_type. andInputIterator1'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator2 – is a model of Input Iterator,
InputIterator2'svalue_typeis convertable toStrictWeakCompare'ssecond_argument_type. andInputIterator2'svalue_typeis convertable to a type inOutputIterator'sset 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_keyperforms a key-value difference operation from set theory.set_difference_by_keyconstructs 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_keyperforms 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)containsmelements that are equivalent to each other and if[keys_first2, keys_last2)containsnelements 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_first1orvalues_first2) to the values output range.This version of
set_difference_by_keycompares key elements usingoperator<.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
set_difference_by_keyto compute the set difference of two sets of integers sorted in ascending order with their values using thethrust::hostexecution 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,
InputIterator1andInputIterator2have the samevalue_type,InputIterator1'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator1'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator2 – is a model of Input Iterator,
InputIterator2andInputIterator1have the samevalue_type,InputIterator2'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator2'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator3 – is a model of Input Iterator, and
InputIterator3'svalue_typeis convertible to a type inOutputIterator2'sset ofvalue_types.InputIterator4 – is a model of Input Iterator, and
InputIterator4'svalue_typeis convertible to a type inOutputIterator2'sset ofvalue_types.OutputIterator1 – is a model of Output Iterator.
OutputIterator2 – is a model of Output Iterator.
- Returns:
A
pairpsuch thatp.firstis the end of the output range of keys, and such thatp.secondis 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_keyperforms a key-value difference operation from set theory.set_difference_by_keyconstructs 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_keyperforms 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)containsmelements that are equivalent to each other and if[keys_first2, keys_last2)containsnelements 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_first1orvalues_first2) to the values output range.This version of
set_difference_by_keycompares key elements usingoperator<.The following code snippet demonstrates how to use
set_difference_by_keyto 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,
InputIterator1andInputIterator2have the samevalue_type,InputIterator1'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator1'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator2 – is a model of Input Iterator,
InputIterator2andInputIterator1have the samevalue_type,InputIterator2'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator2'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator3 – is a model of Input Iterator, and
InputIterator3'svalue_typeis convertible to a type inOutputIterator2'sset ofvalue_types.InputIterator4 – is a model of Input Iterator, and
InputIterator4'svalue_typeis convertible to a type inOutputIterator2'sset ofvalue_types.OutputIterator1 – is a model of Output Iterator.
OutputIterator2 – is a model of Output Iterator.
- Returns:
A
pairpsuch thatp.firstis the end of the output range of keys, and such thatp.secondis 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_keyperforms a key-value difference operation from set theory.set_difference_by_keyconstructs 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_keyperforms 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)containsmelements that are equivalent to each other and if[keys_first2, keys_last2)containsnelements 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_first1orvalues_first2) to the values output range.This version of
set_difference_by_keycompares 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_keyto compute the set difference of two sets of integers sorted in descending order with their values using thethrust::hostexecution 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,
InputIterator1andInputIterator2have the samevalue_type,InputIterator1'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator1'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator2 – is a model of Input Iterator,
InputIterator2andInputIterator1have the samevalue_type,InputIterator2'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator2'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator3 – is a model of Input Iterator, and
InputIterator3'svalue_typeis convertible to a type inOutputIterator2'sset ofvalue_types.InputIterator4 – is a model of Input Iterator, and
InputIterator4'svalue_typeis convertible to a type inOutputIterator2'sset 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
pairpsuch thatp.firstis the end of the output range of keys, and such thatp.secondis 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_keyperforms a key-value difference operation from set theory.set_difference_by_keyconstructs 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_keyperforms 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)containsmelements that are equivalent to each other and if[keys_first2, keys_last2)containsnelements 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_first1orvalues_first2) to the values output range.This version of
set_difference_by_keycompares key elements using a function objectcomp.The following code snippet demonstrates how to use
set_difference_by_keyto 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,
InputIterator1andInputIterator2have the samevalue_type,InputIterator1'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator1'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator2 – is a model of Input Iterator,
InputIterator2andInputIterator1have the samevalue_type,InputIterator2'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator2'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator3 – is a model of Input Iterator, and
InputIterator3'svalue_typeis convertible to a type inOutputIterator2'sset ofvalue_types.InputIterator4 – is a model of Input Iterator, and
InputIterator4'svalue_typeis convertible to a type inOutputIterator2'sset 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
pairpsuch thatp.firstis the end of the output range of keys, and such thatp.secondis 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_keyperforms a key-value intersection operation from set theory.set_intersection_by_keyconstructs 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_keyperforms 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 appearsmtimes in[keys_first1, keys_last1)andntimes in[keys_first2, keys_last2)(wheremmay be zero), then it appearsmin(m,n)times in the keys output range.set_intersection_by_keyis 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_keycompares objects usingoperator<.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
set_intersection_by_keyto compute the set intersection of two sets of integers sorted in ascending order with their values using thethrust::hostexecution 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_keyis unique in that it has novalues_first2parameter 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,
InputIterator1andInputIterator2have the samevalue_type,InputIterator1'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator1'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator2 – is a model of Input Iterator,
InputIterator2andInputIterator1have the samevalue_type,InputIterator2'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator2'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator3 – is a model of Input Iterator, and
InputIterator3'svalue_typeis convertible to a type inOutputIterator2'sset ofvalue_types.OutputIterator1 – is a model of Output Iterator.
OutputIterator2 – is a model of Output Iterator.
- Returns:
A
pairpsuch thatp.firstis the end of the output range of keys, and such thatp.secondis 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_keyperforms a key-value intersection operation from set theory.set_intersection_by_keyconstructs 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_keyperforms 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 appearsmtimes in[keys_first1, keys_last1)andntimes in[keys_first2, keys_last2)(wheremmay be zero), then it appearsmin(m,n)times in the keys output range.set_intersection_by_keyis 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_keycompares objects usingoperator<.The following code snippet demonstrates how to use
set_intersection_by_keyto 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_keyis unique in that it has novalues_first2parameter 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,
InputIterator1andInputIterator2have the samevalue_type,InputIterator1'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator1'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator2 – is a model of Input Iterator,
InputIterator2andInputIterator1have the samevalue_type,InputIterator2'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator2'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator3 – is a model of Input Iterator, and
InputIterator3'svalue_typeis convertible to a type inOutputIterator2'sset ofvalue_types.OutputIterator1 – is a model of Output Iterator.
OutputIterator2 – is a model of Output Iterator.
- Returns:
A
pairpsuch thatp.firstis the end of the output range of keys, and such thatp.secondis 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_keyperforms a key-value intersection operation from set theory.set_intersection_by_keyconstructs 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_keyperforms 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 appearsmtimes in[keys_first1, keys_last1)andntimes in[keys_first2, keys_last2)(wheremmay be zero), then it appearsmin(m,n)times in the keys output range.set_intersection_by_keyis 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_keycompares 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_keyto compute the set intersection of two sets of integers sorted in descending order with their values using thethrust::hostexecution 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_keyis unique in that it has novalues_first2parameter 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,
InputIterator1andInputIterator2have the samevalue_type,InputIterator1'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator1'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator2 – is a model of Input Iterator,
InputIterator2andInputIterator1have the samevalue_type,InputIterator2'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator2'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator3 – is a model of Input Iterator, and
InputIterator3'svalue_typeis convertible to a type inOutputIterator2'sset 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
pairpsuch thatp.firstis the end of the output range of keys, and such thatp.secondis 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_keyperforms a key-value intersection operation from set theory.set_intersection_by_keyconstructs 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_keyperforms 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 appearsmtimes in[keys_first1, keys_last1)andntimes in[keys_first2, keys_last2)(wheremmay be zero), then it appearsmin(m,n)times in the keys output range.set_intersection_by_keyis 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_keycompares objects using a function objectcomp.The following code snippet demonstrates how to use
set_intersection_by_keyto 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_keyis unique in that it has novalues_first2parameter 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,
InputIterator1andInputIterator2have the samevalue_type,InputIterator1'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator1'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator2 – is a model of Input Iterator,
InputIterator2andInputIterator1have the samevalue_type,InputIterator2'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator2'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator3 – is a model of Input Iterator, and
InputIterator3'svalue_typeis convertible to a type inOutputIterator2'sset 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
pairpsuch thatp.firstis the end of the output range of keys, and such thatp.secondis 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_keyperforms a key-value symmetric difference operation from set theory.set_difference_by_keyconstructs 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_keyperforms 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)containsmelements that are equivalent to each other and[keys_first2, keys_last1)containsnelements that are equivalent to them, then|m - n|of those elements shall be copied to the output range: the lastm - nelements from[keys_first1, keys_last1)ifm > n, and the lastn - mof 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_first1orvalues_first2) to the values output range.This version of
set_symmetric_difference_by_keycompares 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_keyto compute the symmetric difference of two sets of integers sorted in ascending order with their values using thethrust::hostexecution 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,
InputIterator1andInputIterator2have the samevalue_type,InputIterator1'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator1'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator2 – is a model of Input Iterator,
InputIterator2andInputIterator1have the samevalue_type,InputIterator2'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator2'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator3 – is a model of Input Iterator, and
InputIterator3'svalue_typeis convertible to a type inOutputIterator2'sset ofvalue_types.InputIterator4 – is a model of Input Iterator, and
InputIterator4'svalue_typeis convertible to a type inOutputIterator2'sset ofvalue_types.OutputIterator1 – is a model of Output Iterator.
OutputIterator2 – is a model of Output Iterator.
- Returns:
A
pairpsuch thatp.firstis the end of the output range of keys, and such thatp.secondis 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_keyperforms a key-value symmetric difference operation from set theory.set_difference_by_keyconstructs 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_keyperforms 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)containsmelements that are equivalent to each other and[keys_first2, keys_last1)containsnelements that are equivalent to them, then|m - n|of those elements shall be copied to the output range: the lastm - nelements from[keys_first1, keys_last1)ifm > n, and the lastn - mof 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_first1orvalues_first2) to the values output range.This version of
set_symmetric_difference_by_keycompares key elements usingoperator<.The following code snippet demonstrates how to use
set_symmetric_difference_by_keyto 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,
InputIterator1andInputIterator2have the samevalue_type,InputIterator1'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator1'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator2 – is a model of Input Iterator,
InputIterator2andInputIterator1have the samevalue_type,InputIterator2'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator2'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator3 – is a model of Input Iterator, and
InputIterator3'svalue_typeis convertible to a type inOutputIterator2'sset ofvalue_types.InputIterator4 – is a model of Input Iterator, and
InputIterator4'svalue_typeis convertible to a type inOutputIterator2'sset ofvalue_types.OutputIterator1 – is a model of Output Iterator.
OutputIterator2 – is a model of Output Iterator.
- Returns:
A
pairpsuch thatp.firstis the end of the output range of keys, and such thatp.secondis 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_keyperforms a key-value symmetric difference operation from set theory.set_difference_by_keyconstructs 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_keyperforms 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)containsmelements that are equivalent to each other and[keys_first2, keys_last1)containsnelements that are equivalent to them, then|m - n|of those elements shall be copied to the output range: the lastm - nelements from[keys_first1, keys_last1)ifm > n, and the lastn - mof 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_first1orvalues_first2) to the values output range.This version of
set_symmetric_difference_by_keycompares 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_keyto compute the symmetric difference of two sets of integers sorted in descending order with their values using thethrust::hostexecution 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,
InputIterator1andInputIterator2have the samevalue_type,InputIterator1'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator1'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator2 – is a model of Input Iterator,
InputIterator2andInputIterator1have the samevalue_type,InputIterator2'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator2'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator3 – is a model of Input Iterator, and
InputIterator3'svalue_typeis convertible to a type inOutputIterator2'sset ofvalue_types.InputIterator4 – is a model of Input Iterator, and
InputIterator4'svalue_typeis convertible to a type inOutputIterator2'sset 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
pairpsuch thatp.firstis the end of the output range of keys, and such thatp.secondis 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_keyperforms a key-value symmetric difference operation from set theory.set_difference_by_keyconstructs 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_keyperforms 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)containsmelements that are equivalent to each other and[keys_first2, keys_last1)containsnelements that are equivalent to them, then|m - n|of those elements shall be copied to the output range: the lastm - nelements from[keys_first1, keys_last1)ifm > n, and the lastn - mof 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_first1orvalues_first2) to the values output range.This version of
set_symmetric_difference_by_keycompares key elements using a function objectcomp.The following code snippet demonstrates how to use
set_symmetric_difference_by_keyto 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,
InputIterator1andInputIterator2have the samevalue_type,InputIterator1'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator1'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator2 – is a model of Input Iterator,
InputIterator2andInputIterator1have the samevalue_type,InputIterator2'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator2'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator3 – is a model of Input Iterator, and
InputIterator3'svalue_typeis convertible to a type inOutputIterator2'sset ofvalue_types.InputIterator4 – is a model of Input Iterator, and
InputIterator4'svalue_typeis convertible to a type inOutputIterator2'sset 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
pairpsuch thatp.firstis the end of the output range of keys, and such thatp.secondis 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_keyperforms a key-value union operation from set theory.set_union_by_keyconstructs 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_keyperforms 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)containsmelements that are equivalent to each other and if[keys_first2, keys_last2)containsnelements that are equivalent to them, then allmelements 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_first1orvalues_first2) to the values output range.This version of
set_union_by_keycompares 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_keyto compute the symmetric difference of two sets of integers sorted in ascending order with their values using thethrust::hostexecution 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,
InputIterator1andInputIterator2have the samevalue_type,InputIterator1'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator1'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator2 – is a model of Input Iterator,
InputIterator2andInputIterator1have the samevalue_type,InputIterator2'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator2'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator3 – is a model of Input Iterator, and
InputIterator3'svalue_typeis convertible to a type inOutputIterator2'sset ofvalue_types.InputIterator4 – is a model of Input Iterator, and
InputIterator4'svalue_typeis convertible to a type inOutputIterator2'sset ofvalue_types.OutputIterator1 – is a model of Output Iterator.
OutputIterator2 – is a model of Output Iterator.
- Returns:
A
pairpsuch thatp.firstis the end of the output range of keys, and such thatp.secondis 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_keyperforms a key-value union operation from set theory.set_union_by_keyconstructs 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_keyperforms 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)containsmelements that are equivalent to each other and if[keys_first2, keys_last2)containsnelements that are equivalent to them, then allmelements 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_first1orvalues_first2) to the values output range.This version of
set_union_by_keycompares key elements usingoperator<.The following code snippet demonstrates how to use
set_symmetric_difference_by_keyto 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,
InputIterator1andInputIterator2have the samevalue_type,InputIterator1'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator1'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator2 – is a model of Input Iterator,
InputIterator2andInputIterator1have the samevalue_type,InputIterator2'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator2'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator3 – is a model of Input Iterator, and
InputIterator3'svalue_typeis convertible to a type inOutputIterator2'sset ofvalue_types.InputIterator4 – is a model of Input Iterator, and
InputIterator4'svalue_typeis convertible to a type inOutputIterator2'sset ofvalue_types.OutputIterator1 – is a model of Output Iterator.
OutputIterator2 – is a model of Output Iterator.
- Returns:
A
pairpsuch thatp.firstis the end of the output range of keys, and such thatp.secondis 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_keyperforms a key-value union operation from set theory.set_union_by_keyconstructs 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_keyperforms 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)containsmelements that are equivalent to each other and if[keys_first2, keys_last2)containsnelements that are equivalent to them, then allmelements 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_first1orvalues_first2) to the values output range.This version of
set_union_by_keycompares 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_keyto compute the symmetric difference of two sets of integers sorted in descending order with their values using thethrust::hostexecution 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,
InputIterator1andInputIterator2have the samevalue_type,InputIterator1'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator1'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator2 – is a model of Input Iterator,
InputIterator2andInputIterator1have the samevalue_type,InputIterator2'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator2'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator3 – is a model of Input Iterator, and
InputIterator3'svalue_typeis convertible to a type inOutputIterator2'sset ofvalue_types.InputIterator4 – is a model of Input Iterator, and
InputIterator4'svalue_typeis convertible to a type inOutputIterator2'sset 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
pairpsuch thatp.firstis the end of the output range of keys, and such thatp.secondis 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_keyperforms a key-value union operation from set theory.set_union_by_keyconstructs 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_keyperforms 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)containsmelements that are equivalent to each other and if[keys_first2, keys_last2)containsnelements that are equivalent to them, then allmelements 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_first1orvalues_first2) to the values output range.This version of
set_union_by_keycompares key elements using a function objectcomp.The following code snippet demonstrates how to use
set_symmetric_difference_by_keyto 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,
InputIterator1andInputIterator2have the samevalue_type,InputIterator1'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator1'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator1'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator2 – is a model of Input Iterator,
InputIterator2andInputIterator1have the samevalue_type,InputIterator2'svalue_typeis a model of LessThan Comparable, the ordering onInputIterator2'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements, andInputIterator2'svalue_typeis convertable to a type inOutputIterator'sset ofvalue_types.InputIterator3 – is a model of Input Iterator, and
InputIterator3'svalue_typeis convertible to a type inOutputIterator2'sset ofvalue_types.InputIterator4 – is a model of Input Iterator, and
InputIterator4'svalue_typeis convertible to a type inOutputIterator2'sset 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
pairpsuch thatp.firstis the end of the output range of keys, and such thatp.secondis 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)
sortsorts the elements in[first, last)into ascending order, meaning that ifiandjare any two valid iterators in[first, last)such thatiprecedesj, then*jis not less than*i. Note:sortis not guaranteed to be stable. That is, suppose that*iand*jare 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
sortcompares objects usingoperator<.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
sortto sort a sequence of integers using thethrust::hostexecution 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,
RandomAccessIteratoris mutable, andRandomAccessIterator'svalue_typeis a model of LessThan Comparable, and the ordering relation onRandomAccessIterator'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements.
-
template<typename RandomAccessIterator>
void sort(RandomAccessIterator first, RandomAccessIterator last)# sortsorts the elements in[first, last)into ascending order, meaning that ifiandjare any two valid iterators in[first, last)such thatiprecedesj, then*jis not less than*i. Note:sortis not guaranteed to be stable. That is, suppose that*iand*jare 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
sortcompares objects usingoperator<.The following code snippet demonstrates how to use
sortto 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,
RandomAccessIteratoris mutable, andRandomAccessIterator'svalue_typeis a model of LessThan Comparable, and the ordering relation onRandomAccessIterator'svalue_typeis 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)
sortsorts the elements in[first, last)into ascending order, meaning that ifiandjare any two valid iterators in[first, last)such thatiprecedesj, then*jis not less than*i. Note:sortis not guaranteed to be stable. That is, suppose that*iand*jare 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
sortcompares 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::hostexecution 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,
RandomAccessIteratoris mutable, andRandomAccessIterator'svalue_typeis convertible toStrictWeakOrdering'sfirst_argument_typeandsecond_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)
sortsorts the elements in[first, last)into ascending order, meaning that ifiandjare any two valid iterators in[first, last)such thatiprecedesj, then*jis not less than*i. Note:sortis not guaranteed to be stable. That is, suppose that*iand*jare 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
sortcompares 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,
RandomAccessIteratoris mutable, andRandomAccessIterator'svalue_typeis convertible toStrictWeakOrdering'sfirst_argument_typeandsecond_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_sortis much likesort:it sorts the elements in[first, last)into ascending order, meaning that ifiandjare any two valid iterators in[first, last)such thatiprecedesj, then*jis not less than*i.As the name suggests,
stable_sortis stable: it preserves the relative ordering of equivalent elements. That is, ifxandyare elements in[first, last)such thatxprecedesy, and if the two elements are equivalent (neitherx < ynory < x) then a postcondition ofstable_sortis thatxstill precedesy.This version of
stable_sortcompares objects usingoperator<.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
sortto sort a sequence of integers using thethrust::hostexecution 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,
RandomAccessIteratoris mutable, andRandomAccessIterator'svalue_typeis a model of LessThan Comparable, and the ordering relation onRandomAccessIterator'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements.
-
template<typename RandomAccessIterator>
void stable_sort(RandomAccessIterator first, RandomAccessIterator last)# stable_sortis much likesort:it sorts the elements in[first, last)into ascending order, meaning that ifiandjare any two valid iterators in[first, last)such thatiprecedesj, then*jis not less than*i.As the name suggests,
stable_sortis stable: it preserves the relative ordering of equivalent elements. That is, ifxandyare elements in[first, last)such thatxprecedesy, and if the two elements are equivalent (neitherx < ynory < x) then a postcondition ofstable_sortis thatxstill precedesy.This version of
stable_sortcompares objects usingoperator<.The following code snippet demonstrates how to use
sortto 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,
RandomAccessIteratoris mutable, andRandomAccessIterator'svalue_typeis a model of LessThan Comparable, and the ordering relation onRandomAccessIterator'svalue_typeis 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_sortis much likesort:it sorts the elements in[first, last)into ascending order, meaning that ifiandjare any two valid iterators in[first, last)such thatiprecedesj, then*jis not less than*i.As the name suggests,
stable_sortis stable: it preserves the relative ordering of equivalent elements. That is, ifxandyare elements in[first, last)such thatxprecedesy, and if the two elements are equivalent (neitherx < ynory < x) then a postcondition ofstable_sortis thatxstill precedesy.This version of
stable_sortcompares 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::hostexecution 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,
RandomAccessIteratoris mutable, andRandomAccessIterator'svalue_typeis convertible toStrictWeakOrdering'sfirst_argument_typeandsecond_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_sortis much likesort:it sorts the elements in[first, last)into ascending order, meaning that ifiandjare any two valid iterators in[first, last)such thatiprecedesj, then*jis not less than*i.As the name suggests,
stable_sortis stable: it preserves the relative ordering of equivalent elements. That is, ifxandyare elements in[first, last)such thatxprecedesy, and if the two elements are equivalent (neitherx < ynory < x) then a postcondition ofstable_sortis thatxstill precedesy.This version of
stable_sortcompares 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,
RandomAccessIteratoris mutable, andRandomAccessIterator'svalue_typeis convertible toStrictWeakOrdering'sfirst_argument_typeandsecond_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_keyperforms a key-value sort. That is,sort_by_keysorts the elements in[keys_first, keys_last)and[values_first, values_first + (keys_last - keys_first))into ascending key order, meaning that ifiandjare any two valid iterators in[keys_first, keys_last)such thatiprecedesj, andpandqare iterators in[values_first, values_first + (keys_last - keys_first))corresponding toiandjrespectively, then*jis not less than*i.Note:
sort_by_keyis not guaranteed to be stable. That is, suppose that*iand*jare 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_keycompares key objects usingoperator<.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
sort_by_keyto sort an array of character values using integers as sorting keys using thethrust::hostexecution 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,
RandomAccessIterator1is mutable, andRandomAccessIterator1'svalue_typeis a model of LessThan Comparable, and the ordering relation onRandomAccessIterator1'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements.RandomAccessIterator2 – is a model of Random Access Iterator, and
RandomAccessIterator2is 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_keyperforms a key-value sort. That is,sort_by_keysorts the elements in[keys_first, keys_last)and[values_first, values_first + (keys_last - keys_first))into ascending key order, meaning that ifiandjare any two valid iterators in[keys_first, keys_last)such thatiprecedesj, andpandqare iterators in[values_first, values_first + (keys_last - keys_first))corresponding toiandjrespectively, then*jis not less than*i.Note:
sort_by_keyis not guaranteed to be stable. That is, suppose that*iand*jare 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_keycompares key objects usingoperator<.The following code snippet demonstrates how to use
sort_by_keyto 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,
RandomAccessIterator1is mutable, andRandomAccessIterator1'svalue_typeis a model of LessThan Comparable, and the ordering relation onRandomAccessIterator1'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements.RandomAccessIterator2 – is a model of Random Access Iterator, and
RandomAccessIterator2is 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_keyperforms a key-value sort. That is,sort_by_keysorts the elements in[keys_first, keys_last)and[values_first, values_first + (keys_last - keys_first))into ascending key order, meaning that ifiandjare any two valid iterators in[keys_first, keys_last)such thatiprecedesj, andpandqare iterators in[values_first, values_first + (keys_last - keys_first))corresponding toiandjrespectively, then*jis not less than*i.Note:
sort_by_keyis not guaranteed to be stable. That is, suppose that*iand*jare 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_keycompares 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_keyto sort an array of character values using integers as sorting keys using thethrust::hostexecution 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,
RandomAccessIterator1is mutable, andRandomAccessIterator1'svalue_typeis convertible toStrictWeakOrdering'sfirst_argument_typeandsecond_argument_type.RandomAccessIterator2 – is a model of Random Access Iterator, and
RandomAccessIterator2is 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_keyperforms a key-value sort. That is,sort_by_keysorts the elements in[keys_first, keys_last)and[values_first, values_first + (keys_last - keys_first))into ascending key order, meaning that ifiandjare any two valid iterators in[keys_first, keys_last)such thatiprecedesj, andpandqare iterators in[values_first, values_first + (keys_last - keys_first))corresponding toiandjrespectively, then*jis not less than*i.Note:
sort_by_keyis not guaranteed to be stable. That is, suppose that*iand*jare 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_keycompares key objects using a function objectcomp.The following code snippet demonstrates how to use
sort_by_keyto 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,
RandomAccessIterator1is mutable, andRandomAccessIterator1'svalue_typeis convertible toStrictWeakOrdering'sfirst_argument_typeandsecond_argument_type.RandomAccessIterator2 – is a model of Random Access Iterator, and
RandomAccessIterator2is 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_keyperforms a key-value sort. That is,stable_sort_by_keysorts the elements in[keys_first, keys_last)and[values_first, values_first + (keys_last - keys_first))into ascending key order, meaning that ifiandjare any two valid iterators in[keys_first, keys_last)such thatiprecedesj, andpandqare iterators in[values_first, values_first + (keys_last - keys_first))corresponding toiandjrespectively, then*jis not less than*i.As the name suggests,
stable_sort_by_keyis stable: it preserves the relative ordering of equivalent elements. That is, ifxandyare elements in[keys_first, keys_last)such thatxprecedesy, and if the two elements are equivalent (neitherx < ynory < x) then a postcondition ofstable_sort_by_keyis thatxstill precedesy.This version of
stable_sort_by_keycompares key objects usingoperator<.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
stable_sort_by_keyto sort an array of characters using integers as sorting keys using thethrust::hostexecution 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,
RandomAccessIterator1is mutable, andRandomAccessIterator1'svalue_typeis a model of LessThan Comparable, and the ordering relation onRandomAccessIterator1'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements.RandomAccessIterator2 – is a model of Random Access Iterator, and
RandomAccessIterator2is 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_keyperforms a key-value sort. That is,stable_sort_by_keysorts the elements in[keys_first, keys_last)and[values_first, values_first + (keys_last - keys_first))into ascending key order, meaning that ifiandjare any two valid iterators in[keys_first, keys_last)such thatiprecedesj, andpandqare iterators in[values_first, values_first + (keys_last - keys_first))corresponding toiandjrespectively, then*jis not less than*i.As the name suggests,
stable_sort_by_keyis stable: it preserves the relative ordering of equivalent elements. That is, ifxandyare elements in[keys_first, keys_last)such thatxprecedesy, and if the two elements are equivalent (neitherx < ynory < x) then a postcondition ofstable_sort_by_keyis thatxstill precedesy.This version of
stable_sort_by_keycompares key objects usingoperator<.The following code snippet demonstrates how to use
stable_sort_by_keyto 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,
RandomAccessIterator1is mutable, andRandomAccessIterator1'svalue_typeis a model of LessThan Comparable, and the ordering relation onRandomAccessIterator1'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements.RandomAccessIterator2 – is a model of Random Access Iterator, and
RandomAccessIterator2is 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_keyperforms a key-value sort. That is,stable_sort_by_keysorts the elements in[keys_first, keys_last)and[values_first, values_first + (keys_last - keys_first))into ascending key order, meaning that ifiandjare any two valid iterators in[keys_first, keys_last)such thatiprecedesj, andpandqare iterators in[values_first, values_first + (keys_last - keys_first))corresponding toiandjrespectively, then*jis not less than*i.As the name suggests,
stable_sort_by_keyis stable: it preserves the relative ordering of equivalent elements. That is, ifxandyare elements in[keys_first, keys_last)such thatxprecedesy, and if the two elements are equivalent (neitherx < ynory < x) then a postcondition ofstable_sort_by_keyis thatxstill precedesy.This version of
stable_sort_by_keycompares 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_keyto sort an array of character values using integers as sorting keys using thethrust::hostexecution 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,
RandomAccessIterator1is mutable, andRandomAccessIterator1'svalue_typeis convertible toStrictWeakOrdering'sfirst_argument_typeandsecond_argument_type.RandomAccessIterator2 – is a model of Random Access Iterator, and
RandomAccessIterator2is 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_keyperforms a key-value sort. That is,stable_sort_by_keysorts the elements in[keys_first, keys_last)and[values_first, values_first + (keys_last - keys_first))into ascending key order, meaning that ifiandjare any two valid iterators in[keys_first, keys_last)such thatiprecedesj, andpandqare iterators in[values_first, values_first + (keys_last - keys_first))corresponding toiandjrespectively, then*jis not less than*i.As the name suggests,
stable_sort_by_keyis stable: it preserves the relative ordering of equivalent elements. That is, ifxandyare elements in[keys_first, keys_last)such thatxprecedesy, and if the two elements are equivalent (neitherx < ynory < x) then a postcondition ofstable_sort_by_keyis thatxstill precedesy.This version of
stable_sort_by_keycompares key objects using the function objectcomp.The following code snippet demonstrates how to use
sort_by_keyto 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,
RandomAccessIterator1is mutable, andRandomAccessIterator1'svalue_typeis convertible toStrictWeakOrdering'sfirst_argument_typeandsecond_argument_type.RandomAccessIterator2 – is a model of Random Access Iterator, and
RandomAccessIterator2is 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_differencecalculates the differences of adjacent elements in the range[first, last). That is,*firstis assigned to*result, and, for each iteratoriin the range[first + 1, last), the difference of*iand*(i - 1)is assigned to*(result + (i - first)).This version of
adjacent_differenceusesoperator-to calculate differences.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
adjacent_differenceto compute the difference between adjacent elements of a range using thethrust::deviceexecution policy:Remark
Note that
resultis 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
xandyare objects ofInputIterator'svalue_type, thenx-isdefined, andInputIterator'svalue_typeis convertible to a type inOutputIterator'sset ofvalue_types, and the return type ofx - yis convertible to a type inOutputIterator'sset 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_differencecalculates the differences of adjacent elements in the range[first, last). That is,*firstis assigned to*result, and, for each iteratoriin the range[first + 1, last),binary_op(*i, *(i - 1))is assigned to*(result + (i - first)).This version of
adjacent_differenceuses the binary functionbinary_opto calculate differences.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
adjacent_differenceto compute the sum between adjacent elements of a range using thethrust::deviceexecution policy:Remark
Note that
resultis 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'svalue_typeis convertible toBinaryFunction'sfirst_argument_typeandsecond_argument_type, andInputIterator'svalue_typeis convertible to a type inOutputIterator'sset ofvalue_types.OutputIterator – is a model of Output Iterator.
BinaryFunction's –
result_typeis convertible to a type inOutputIterator'sset ofvalue_types.
- Returns:
The iterator
result + (last - first)
-
template<typename InputIterator, typename OutputIterator>
OutputIterator adjacent_difference(InputIterator first, InputIterator last, OutputIterator result)# adjacent_differencecalculates the differences of adjacent elements in the range[first, last). That is,*firstis assigned to*result, and, for each iteratoriin the range[first + 1, last), the difference of*iand*(i - 1)is assigned to*(result + (i - first)).This version of
adjacent_differenceusesoperator-to calculate differences.The following code snippet demonstrates how to use
adjacent_differenceto compute the difference between adjacent elements of a range.Remark
Note that
resultis 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
xandyare objects ofInputIterator'svalue_type, thenx-isdefined, andInputIterator'svalue_typeis convertible to a type inOutputIterator'sset ofvalue_types, and the return type ofx - yis convertible to a type inOutputIterator'sset 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_differencecalculates the differences of adjacent elements in the range[first, last). That is,*firstis assigned to*result, and, for each iteratoriin the range[first + 1, last),binary_op(*i, *(i - 1))is assigned to*(result + (i - first)).This version of
adjacent_differenceuses the binary functionbinary_opto calculate differences.The following code snippet demonstrates how to use
adjacent_differenceto compute the sum between adjacent elements of a range.Remark
Note that
resultis 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'svalue_typeis convertible toBinaryFunction'sfirst_argument_typeandsecond_argument_type, andInputIterator'svalue_typeis convertible to a type inOutputIterator'sset ofvalue_types.OutputIterator – is a model of Output Iterator.
BinaryFunction's –
result_typeis convertible to a type inOutputIterator'sset 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)
generateassigns 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_vectorwith random numbers, using the standard C library functionrandusing thethrust::hostexecution 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
ForwardIteratoris mutable.Generator – is a model of Generator, and
Generator'sresult_typeis convertible toForwardIterator'svalue_type.
-
template<typename ForwardIterator, typename Generator>
void generate(ForwardIterator first, ForwardIterator last, Generator gen)# generateassigns 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_vectorwith 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
ForwardIteratoris mutable.Generator – is a model of Generator, and
Generator'sresult_typeis convertible toForwardIterator'svalue_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_nassigns 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_vectorwith random numbers, using the standard C library functionrandusing thethrust::hostexecution 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'sresult_typeis convertible to a type inOutputIterator'sset ofvalue_types.
-
template<typename OutputIterator, typename Size, typename Generator>
OutputIterator generate_n(OutputIterator first, Size n, Generator gen)# generate_nassigns 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_vectorwith 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'sresult_typeis convertible to a type inOutputIterator'sset ofvalue_types.
- template<typename DerivedPolicy, typename ForwardIterator> __host__ __device__ void sequence (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, ForwardIterator first, ForwardIterator last)
sequencefills the range[first, last)with a sequence of numbers.For each iterator
iin the range[first, last), this version ofsequenceperforms the assignment*i = (i - first).The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
sequenceto fill a range with a sequence of numbers using thethrust::hostexecution 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,sequenceoffers 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
ForwardIteratoris mutable, and ifxandyare objects ofForwardIterator'svalue_type, thenx + yis defined, and ifTisForwardIterator'svalue_type, thenT(0)is defined.
-
template<typename ForwardIterator>
void sequence(ForwardIterator first, ForwardIterator last)# sequencefills the range[first, last)with a sequence of numbers.For each iterator
iin the range[first, last), this version ofsequenceperforms the assignment*i = (i - first).The following code snippet demonstrates how to use
sequenceto 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,sequenceoffers 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
ForwardIteratoris mutable, and ifxandyare objects ofForwardIterator'svalue_type, thenx + yis defined, and ifTisForwardIterator'svalue_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)
sequencefills the range[first, last)with a sequence of numbers.For each iterator
iin the range[first, last), this version ofsequenceperforms the assignment*i = init + (i - first).The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
sequenceto fill a range with a sequence of numbers starting from the value 1 using thethrust::hostexecution 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,sequenceoffers 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
ForwardIteratoris mutable, and ifxandyare objects ofForwardIterator'svalue_type, thenx + yis defined, and ifTisForwardIterator'svalue_type, thenT(0)is defined.T – is a model of Assignable, and
Tis convertible toForwardIterator'svalue_type.
-
template<typename ForwardIterator, typename T>
void sequence(ForwardIterator first, ForwardIterator last, T init)# sequencefills the range[first, last)with a sequence of numbers.For each iterator
iin the range[first, last), this version ofsequenceperforms the assignment*i = init + (i - first).The following code snippet demonstrates how to use
sequenceto 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,sequenceoffers 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
ForwardIteratoris mutable, and ifxandyare objects ofForwardIterator'svalue_type, thenx + yis defined, and ifTisForwardIterator'svalue_type, thenT(0)is defined.T – is a model of Assignable, and
Tis convertible toForwardIterator'svalue_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)
sequencefills the range[first, last)with a sequence of numbers.For each iterator
iin the range[first, last), this version ofsequenceperforms 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
sequenceto fill a range with a sequence of numbers starting from the value 1 with a step size of 3 using thethrust::hostexecution 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,sequenceoffers 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
ForwardIteratoris mutable, and ifxandyare objects ofForwardIterator'svalue_type, thenx + yis defined, and ifTisForwardIterator'svalue_type, thenT(0)is defined.T – is a model of Assignable, and
Tis convertible toForwardIterator'svalue_type.
-
template<typename ForwardIterator, typename T>
void sequence(ForwardIterator first, ForwardIterator last, T init, T step)# sequencefills the range[first, last)with a sequence of numbers.For each iterator
iin the range[first, last), this version ofsequenceperforms the assignment*i = init + step * (i - first).The following code snippet demonstrates how to use
sequenceto 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,sequenceoffers 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
ForwardIteratoris mutable, and ifxandyare objects ofForwardIterator'svalue_type, thenx + yis defined, and ifTisForwardIterator'svalue_type, thenT(0)is defined.T – is a model of Assignable, and
Tis convertible toForwardIterator'svalue_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)
tabulatefills the range[first, last)with the value of a function applied to each element’s index.For each iterator
iin the range[first, last),tabulateperforms 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
tabulateto generate the firstnnon-positive integers using thethrust::hostexecution 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
ForwardIteratoris mutable, and ifxandyare objects ofForwardIterator'svalue_type, thenx + yis defined, and ifTisForwardIterator'svalue_type, thenT(0)is defined.UnaryOperation – is a model of Unary Function and
UnaryFunction'sresult_typeis convertible toOutputIterator'svalue_type.
-
template<typename ForwardIterator, typename UnaryOperation>
void tabulate(ForwardIterator first, ForwardIterator last, UnaryOperation unary_op)# tabulatefills the range[first, last)with the value of a function applied to each element’s index.For each iterator
iin the range[first, last),tabulateperforms the assignment*i = unary_op(i - first).The following code snippet demonstrates how to use
tabulateto generate the firstnnon-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
ForwardIteratoris mutable, and ifxandyare objects ofForwardIterator'svalue_type, thenx + yis defined, and ifTisForwardIterator'svalue_type, thenT(0)is defined.UnaryOperation – is a model of Unary Function and
UnaryFunction'sresult_typeis convertible toOutputIterator'svalue_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
transformapplies 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 iteratoriin the range [first,last) the operationop(*i)is performed and the result is assigned to*o, whereois 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
transformto negate a range in-place using thethrust::hostexecution 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'svalue_typeis convertible toUnaryFunction'sargument_type.OutputIterator – is a model of Output Iterator.
UnaryFunction – is a model of Unary Function and
UnaryFunction'sresult_typeis convertible toOutputIterator'svalue_type.
- Returns:
The end of the output sequence.
- Pre:
firstmay 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
transformapplies 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 iteratoriin the range [first,last) the operationop(*i)is performed and the result is assigned to*o, whereois 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'svalue_typeis convertible toUnaryFunction'sargument_type.OutputIterator – is a model of Output Iterator.
UnaryFunction – is a model of Unary Function and
UnaryFunction'sresult_typeis convertible toOutputIterator'svalue_type.
- Returns:
The end of the output sequence.
- Pre:
firstmay 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
transformapplies 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 iteratoriin 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, whereois 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
transformto compute the sum of two ranges using thethrust::hostexecution 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'svalue_typeis convertible toBinaryFunction'sfirst_argument_type.InputIterator2 – is a model of Input Iterator and
InputIterator2'svalue_typeis convertible toBinaryFunction'ssecond_argument_type.OutputIterator – is a model of Output Iterator.
BinaryFunction – is a model of Binary Function and
BinaryFunction'sresult_typeis convertible toOutputIterator'svalue_type.
- Returns:
The end of the output sequence.
- Pre:
first1may equalresult, but the range[first1, last1)shall not overlap the range[result, result + (last1 - first1))otherwise.- Pre:
first2may 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
transformapplies 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 iteratoriin 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, whereois 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'svalue_typeis convertible toBinaryFunction'sfirst_argument_type.InputIterator2 – is a model of Input Iterator and
InputIterator2'svalue_typeis convertible toBinaryFunction'ssecond_argument_type.OutputIterator – is a model of Output Iterator.
BinaryFunction – is a model of Binary Function and
BinaryFunction'sresult_typeis convertible toOutputIterator'svalue_type.
- Returns:
The end of the output sequence.
- Pre:
first1may equalresult, but the range[first1, last1)shall not overlap the range[result, result + (last1 - first1))otherwise.- Pre:
first2may 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_ifconditionally 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
iin the range[first, last)the predicatepred(*i)is evaluated. If this predicate evaluates totrue, the result ofop(*i)is assigned to*o, whereois 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_ifto negate the odd-valued elements of a range using thethrust::hostexecution 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'svalue_typeis convertible toPredicate'sargument_type, andInputIterator'svalue_typeis convertible toUnaryFunction'sargument_type.ForwardIterator – is a model of Forward Iterator.
UnaryFunction – is a model of Unary Function and
UnaryFunction'sresult_typeis convertible toOutputIterator'svalue_type.Predicate – is a model of Predicate.
- Returns:
The end of the output sequence.
- Pre:
firstmay 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_ifconditionally 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
iin the range[first, last)the predicatepred(*i)is evaluated. If this predicate evaluates totrue, the result ofop(*i)is assigned to*o, whereois 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'svalue_typeis convertible toPredicate'sargument_type, andInputIterator'svalue_typeis convertible toUnaryFunction'sargument_type.ForwardIterator – is a model of Forward Iterator.
UnaryFunction – is a model of Unary Function and
UnaryFunction'sresult_typeis convertible toOutputIterator'svalue_type.Predicate – is a model of Predicate.
- Returns:
The end of the output sequence.
- Pre:
firstmay 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_ifconditionally 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
iin the range[first, last)the predicatepred(*s)is evaluated, wheresis the corresponding input iterator in the range[stencil, stencil + (last - first) ). If this predicate evaluates totrue, the result ofop(*i)is assigned to*o, whereois 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_ifusing thethrust::hostexecution 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'svalue_typeis convertible toUnaryFunction'sargument_type.InputIterator2 – is a model of Input Iterator and
InputIterator2'svalue_typeis convertible toPredicate'sargument_type.ForwardIterator – is a model of Forward Iterator.
UnaryFunction – is a model of Unary Function and
UnaryFunction'sresult_typeis convertible toOutputIterator'svalue_type.Predicate – is a model of Predicate.
- Returns:
The end of the output sequence.
- Pre:
firstmay equalresult, but the range[first, last)shall not overlap the range[result, result + (last - first))otherwise.- Pre:
stencilmay 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_ifconditionally 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
iin the range[first, last)the predicatepred(*s)is evaluated, wheresis the corresponding input iterator in the range[stencil, stencil + (last - first) ). If this predicate evaluates totrue, the result ofop(*i)is assigned to*o, whereois 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'svalue_typeis convertible toUnaryFunction'sargument_type.InputIterator2 – is a model of Input Iterator and
InputIterator2'svalue_typeis convertible toPredicate'sargument_type.ForwardIterator – is a model of Forward Iterator.
UnaryFunction – is a model of Unary Function and
UnaryFunction'sresult_typeis convertible toOutputIterator'svalue_type.Predicate – is a model of Predicate.
- Returns:
The end of the output sequence.
- Pre:
firstmay equalresult, but the range[first, last)shall not overlap the range[result, result + (last - first))otherwise.- Pre:
stencilmay 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_ifconditionally 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
iin the range[first1, last1)andj = first2 + (i - first1)in the range[first2, first2 + (last1 - first1) ), the predicatepred(*s)is evaluated, wheresis 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, whereois 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_ifusing thethrust::hostexecution 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'svalue_typeis convertible toBinaryFunction'sfirst_argument_type.InputIterator2 – is a model of Input Iterator and
InputIterator2'svalue_typeis convertible toBinaryFunction'ssecond_argument_type.ForwardIterator – is a model of Forward Iterator.
BinaryFunction – is a model of Binary Function and
BinaryFunction'sresult_typeis convertible toOutputIterator'svalue_type.Predicate – is a model of Predicate.
- Returns:
The end of the output sequence.
- Pre:
first1may equalresult, but the range[first1, last1)shall not overlap the range[result, result + (last1 - first1))otherwise.- Pre:
first2may equalresult, but the range[first2, first2 + (last1 - first1))shall not overlap the range[result, result + (last1 - first1))otherwise.- Pre:
stencilmay 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_ifconditionally 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
iin the range[first1, last1)andj = first2 + (i - first1)in the range[first2, first2 + (last1 - first1) ), the predicatepred(*s)is evaluated, wheresis 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, whereois 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'svalue_typeis convertible toBinaryFunction'sfirst_argument_type.InputIterator2 – is a model of Input Iterator and
InputIterator2'svalue_typeis convertible toBinaryFunction'ssecond_argument_type.ForwardIterator – is a model of Forward Iterator.
BinaryFunction – is a model of Binary Function and
BinaryFunction'sresult_typeis convertible toOutputIterator'svalue_type.Predicate – is a model of Predicate.
- Returns:
The end of the output sequence.
- Pre:
first1may equalresult, but the range[first1, last1)shall not overlap the range[result, result + (last1 - first1))otherwise.- Pre:
first2may equalresult, but the range[first2, first2 + (last1 - first1))shall not overlap the range[result, result + (last1 - first1))otherwise.- Pre:
stencilmay 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)
fillassigns the valuevalueto every element in the range[first, last). That is, for every iteratoriin[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
fillto set a thrust::device_vector’s elements to a given value using thethrust::deviceexecution 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
ForwardIteratoris mutable.T – is a model of Assignable, and
T'svalue_typeis convertible toForwardIterator'svalue_type.
- template<typename ForwardIterator, typename T> __host__ __device__ void fill (ForwardIterator first, ForwardIterator last, const T &value)
fillassigns the valuevalueto every element in the range[first, last). That is, for every iteratoriin[first, last), it performs the assignment*i = value.The following code snippet demonstrates how to use
fillto 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
ForwardIteratoris mutable.T – is a model of Assignable, and
T'svalue_typeis convertible toForwardIterator'svalue_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_nassigns the valuevalueto every element in the range[first, first+n). That is, for every iteratoriin[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
fillto set a thrust::device_vector’s elements to a given value using thethrust::deviceexecution 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'svalue_typeis convertible to a type inOutputIterator'sset 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_nassigns the valuevalueto every element in the range[first, first+n). That is, for every iteratoriin[first, first+n), it performs the assignment*i = value.The following code snippet demonstrates how to use
fillto 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'svalue_typeis convertible to a type inOutputIterator'sset 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_newallocates 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_fillcreates copies ofxin that range. That is, for each iteratoriin the range[first, last),uninitialized_fillcreates a copy ofxin the location pointed toiby callingForwardIterator'svalue_type'scopy constructor.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
uninitialized_fillto initialize a range of uninitialized memory using thethrust::deviceexecution 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,
ForwardIteratoris mutable, andForwardIterator'svalue_typehas 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_newallocates 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_fillcreates copies ofxin that range. That is, for each iteratoriin the range[first, last),uninitialized_fillcreates a copy ofxin the location pointed toiby callingForwardIterator'svalue_type'scopy constructor.The following code snippet demonstrates how to use
uninitialized_fillto 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,
ForwardIteratoris mutable, andForwardIterator'svalue_typehas 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_newallocates 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_fillcreates copies ofxin that range. That is, for each iteratoriin the range[first, first+n),uninitialized_fillcreates a copy ofxin the location pointed toiby callingForwardIterator'svalue_type'scopy constructor.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
uninitialized_fillto initialize a range of uninitialized memory using thethrust::deviceexecution 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,
ForwardIteratoris mutable, andForwardIterator'svalue_typehas 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_newallocates 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_fillcreates copies ofxin that range. That is, for each iteratoriin the range[first, first+n),uninitialized_fillcreates a copy ofxin the location pointed toiby callingForwardIterator'svalue_type'scopy constructor.The following code snippet demonstrates how to use
uninitialized_fillto 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,
ForwardIteratoris mutable, andForwardIterator'svalue_typehas 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_eachapplies the function objectfto each element in the range[first, last);f'sreturn 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_eachdoes 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_eachto print the elements of athrust::device_vectorusing thethrust::deviceparallelization 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'svalue_typeis convertible toUnaryFunction'sargument_type.UnaryFunction – is a model of Unary Function, and
UnaryFunctiondoes 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_napplies the function objectfto each element in the range[first, first + n);f'sreturn 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_nto print the elements of adevice_vectorusing thethrust::deviceparallelization 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'svalue_typeis convertible toUnaryFunction'sargument_type.Size – is an integral type.
UnaryFunction – is a model of Unary Function, and
UnaryFunctiondoes 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_eachapplies the function objectfto each element in the range[first, last);f'sreturn 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_eachdoes not return a copy of the function object.The following code snippet demonstrates how to use
for_eachto 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'svalue_typeis convertible toUnaryFunction'sargument_type.UnaryFunction – is a model of Unary Function, and
UnaryFunctiondoes 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_napplies the function objectfto each element in the range[first, first + n);f'sreturn 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_nto 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'svalue_typeis convertible toUnaryFunction'sargument_type.Size – is an integral type.
UnaryFunction – is a model of Unary Function, and
UnaryFunctiondoes 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)
replacereplaces every element in the range [first, last) equal toold_valuewithnew_value. That is: for every iteratori, if*i == old_valuethen it performs theassignment *i = new_value.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
replaceto replace a value of interest in adevice_vectorwith another using thethrust::deviceexecution 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
ForwardIteratoris mutable.T – is a model of Assignable,
Tis a model of EqualityComparable, objects ofTmay be compared for equality with objects ofForwardIterator'svalue_type, andTis convertible toForwardIterator'svalue_type.
-
template<typename ForwardIterator, typename T>
void replace(ForwardIterator first, ForwardIterator last, const T &old_value, const T &new_value)# replacereplaces every element in the range [first, last) equal toold_valuewithnew_value. That is: for every iteratori, if*i == old_valuethen it performs theassignment *i = new_value.The following code snippet demonstrates how to use
replaceto replace a value of interest in adevice_vectorwith 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
ForwardIteratoris mutable.T – is a model of Assignable,
Tis a model of EqualityComparable, objects ofTmay be compared for equality with objects ofForwardIterator'svalue_type, andTis convertible toForwardIterator'svalue_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_ifreplaces every element in the range[first, last)for whichpredreturnstruewithnew_value. That is: for every iteratori, ifpred(*i)istruethen 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_ifto replace adevice_vector'snegative elements with0using thethrust::deviceexecution 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,
ForwardIteratoris mutable, andForwardIterator'svalue_typeis convertible toPredicate'sargument_type.Predicate – is a model of Predicate.
T – is a model of Assignable, and
Tis convertible toForwardIterator'svalue_type.
-
template<typename ForwardIterator, typename Predicate, typename T>
void replace_if(ForwardIterator first, ForwardIterator last, Predicate pred, const T &new_value)# replace_ifreplaces every element in the range[first, last)for whichpredreturnstruewithnew_value. That is: for every iteratori, ifpred(*i)istruethen it performs the assignment*i = new_value.The following code snippet demonstrates how to use
replace_ifto replace adevice_vector'snegative 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,
ForwardIteratoris mutable, andForwardIterator'svalue_typeis convertible toPredicate'sargument_type.Predicate – is a model of Predicate.
T – is a model of Assignable, and
Tis convertible toForwardIterator'svalue_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_ifreplaces every element in the range[first, last)for whichpred(*s)returnstruewithnew_value. That is: for every iteratoriin the range[first, last), andsin the range[stencil, stencil + (last - first)), ifpred(*s)istruethen 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_ifto replace adevice_vector'selement with0when its corresponding stencil element is less than zero using thethrust::deviceexecution 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
ForwardIteratoris mutable.InputIterator – is a model of Input Iterator, and
InputIterator'svalue_typeis convertible toPredicate'sargument_type.Predicate – is a model of Predicate.
T – is a model of Assignable, and
Tis convertible toForwardIterator'svalue_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_ifreplaces every element in the range[first, last)for whichpred(*s)returnstruewithnew_value. That is: for every iteratoriin the range[first, last), andsin the range[stencil, stencil + (last - first)), ifpred(*s)istruethen it performs the assignment*i = new_value.The following code snippet demonstrates how to use
replace_ifto replace adevice_vector'selement with0when 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
ForwardIteratoris mutable.InputIterator – is a model of Input Iterator, and
InputIterator'svalue_typeis convertible toPredicate'sargument_type.Predicate – is a model of Predicate.
T – is a model of Assignable, and
Tis convertible toForwardIterator'svalue_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_copycopies elements from the range[first, last)to the range[result, result + (last-first)), except that any element equal toold_valueis not copied;new_valueis copied instead.More precisely, for every integer
nsuch that0 <= n < last-first,replace_copyperforms the assignment*(result+n) = new_valueif*(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_valueevaluates 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,
Tis a model of Equality Comparable,Tmay be compared for equality withInputIterator'svalue_type, andTis convertible toOutputIterator'svalue_type.
- Returns:
result + (last-first)- Pre:
firstmay 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_copycopies elements from the range[first, last)to the range[result, result + (last-first)), except that any element equal toold_valueis not copied;new_valueis copied instead.More precisely, for every integer
nsuch that0 <= n < last-first,replace_copyperforms the assignment*(result+n) = new_valueif*(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_valueevaluates totrue.
- Template Parameters:
InputIterator – is a model of Input Iterator.
OutputIterator – is a model of Output Iterator.
T – is a model of Assignable,
Tis a model of Equality Comparable,Tmay be compared for equality withInputIterator'svalue_type, andTis convertible toOutputIterator'svalue_type.
- Returns:
result + (last-first)- Pre:
firstmay 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_ifcopies elements from the range[first, last)to the range[result, result + (last-first)), except that any element for whichpredistrueis not copied;new_valueis copied instead.More precisely, for every integer
nsuch that 0 <= n < last-first,replace_copy_ifperforms the assignment*(result+n) = new_valueifpred(*(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'svalue_typeis convertible toPredicate'sargument_type.OutputIterator – is a model of Output Iterator.
Predicate – is a model of Predicate.
T – is a model of Assignable, and
Tis convertible toOutputIterator'svalue_type.
- Returns:
result + (last-first)- Pre:
firstmay 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_ifcopies elements from the range[first, last)to the range[result, result + (last-first)), except that any element for whichpredistrueis not copied;new_valueis copied instead.More precisely, for every integer
nsuch that 0 <= n < last-first,replace_copy_ifperforms the assignment*(result+n) = new_valueifpred(*(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'svalue_typeis convertible toPredicate'sargument_type.OutputIterator – is a model of Output Iterator.
Predicate – is a model of Predicate.
T – is a model of Assignable, and
Tis convertible toOutputIterator'svalue_type.
- Returns:
result + (last-first)- Pre:
firstmay 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_ifcopies elements from the range[first, last)to the range[result, result + (last-first)), except that any element whose corresponding stencil element causespredto betrueis not copied;new_valueis copied instead.More precisely, for every integer
nsuch that0 <= n < last-first,replace_copy_ifperforms the assignment*(result+n) = new_valueifpred(*(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'svalue_typeis convertible toPredicate'sargument_type.OutputIterator – is a model of Output Iterator.
Predicate – is a model of Predicate.
T – is a model of Assignable, and
Tis convertible toOutputIterator'svalue_type.
- Returns:
result + (last-first)- Pre:
firstmay equalresult, but the ranges[first, last)and[result, result + (last - first))shall not overlap otherwise.- Pre:
stencilmay 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_ifcopies elements from the range[first, last)to the range[result, result + (last-first)), except that any element whose corresponding stencil element causespredto betrueis not copied;new_valueis copied instead.More precisely, for every integer
nsuch that0 <= n < last-first,replace_copy_ifperforms the assignment*(result+n) = new_valueifpred(*(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'svalue_typeis convertible toPredicate'sargument_type.OutputIterator – is a model of Output Iterator.
Predicate – is a model of Predicate.
T – is a model of Assignable, and
Tis convertible toOutputIterator'svalue_type.
- Returns:
result + (last-first)- Pre:
firstmay equalresult, but the ranges[first, last)and[result, result + (last - first))shall not overlap otherwise.- Pre:
stencilmay 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
complexfrom 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
complexfrom 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
complexfrom 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
complexfrom 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
complexwith the specified magnitude and phase.- Parameters:
m – The magnitude of the returned
complex.theta – The phase of the returned
complexin radians.
- template<typename T> __host__ __device__ complex< T > proj (const T &z)
Returns the projection of a
complexon the Riemann sphere. For all finitecomplexit returns the argument. Forcomplexswith 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
complexargument.
- 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
complexnumbers.The value types of the two
complextypes should be compatible and the type of the returnedcomplexis 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
complexnumber.The value type of the
complexshould be compatible with the scalar and the type of the returnedcomplexis 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
complexnumber to a scalar.The value type of the
complexshould be compatible with the scalar and the type of the returnedcomplexis 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
complexnumbers.The value types of the two
complextypes should be compatible and the type of the returnedcomplexis 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
complexnumber.The value type of the
complexshould be compatible with the scalar and the type of the returnedcomplexis 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
complexnumber from a scalar.The value type of the
complexshould be compatible with the scalar and the type of the returnedcomplexis 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
complexnumbers.The value types of the two
complextypes should be compatible and the type of the returnedcomplexis 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
complexnumber 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
complexnumber.The value type of the
complexshould be compatible with the scalar and the type of the returnedcomplexis 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
complexnumbers.The value types of the two
complextypes should be compatible and the type of the returnedcomplexis 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
complexnumber by a scalar.The value type of the
complexshould be compatible with the scalar and the type of the returnedcomplexis 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
complexnumber.The value type of the
complexshould be compatible with the scalar and the type of the returnedcomplexis 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
complexargument.- Parameters:
y – The
complexargument.
- template<typename T> __host__ __device__ complex< T > operator- (const complex< T > &y)
Unary minus, returns the additive inverse (negation) of its
complexargument.- Parameters:
y – The
complexargument.
- template<typename T> __host__ __device__ complex< T > exp (const complex< T > &z)
Returns the complex exponential of a
complexnumber.- Parameters:
z – The
complexargument.
- template<typename T> __host__ __device__ complex< T > log (const complex< T > &z)
Returns the complex natural logarithm of a
complexnumber.- Parameters:
z – The
complexargument.
- template<typename T> __host__ __device__ complex< T > log10 (const complex< T > &z)
Returns the complex base 10 logarithm of a
complexnumber.- Parameters:
z – The
complexargument.
- 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
complexnumber raised to another.The value types of the two
complextypes should be compatible and the type of the returnedcomplexis 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
complexnumber raised to a scalar.The value type of the
complexshould be compatible with the scalar and the type of the returnedcomplexis 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
complexnumber.The value type of the
complexshould be compatible with the scalar and the type of the returnedcomplexis 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
complexnumber.- Parameters:
z – The
complexargument.
- template<typename T> __host__ __device__ complex< T > cos (const complex< T > &z)
Returns the complex cosine of a
complexnumber.- Parameters:
z – The
complexargument.
- template<typename T> __host__ __device__ complex< T > sin (const complex< T > &z)
Returns the complex sine of a
complexnumber.- Parameters:
z – The
complexargument.
- template<typename T> __host__ __device__ complex< T > tan (const complex< T > &z)
Returns the complex tangent of a
complexnumber.- Parameters:
z – The
complexargument.
- template<typename T> __host__ __device__ complex< T > cosh (const complex< T > &z)
Returns the complex hyperbolic cosine of a
complexnumber.- Parameters:
z – The
complexargument.
- template<typename T> __host__ __device__ complex< T > sinh (const complex< T > &z)
Returns the complex hyperbolic sine of a
complexnumber.- Parameters:
z – The
complexargument.
- template<typename T> __host__ __device__ complex< T > tanh (const complex< T > &z)
Returns the complex hyperbolic tangent of a
complexnumber.- Parameters:
z – The
complexargument.
- template<typename T> __host__ __device__ complex< T > acos (const complex< T > &z)
Returns the complex arc cosine of a
complexnumber.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
complexargument.
- template<typename T> __host__ __device__ complex< T > asin (const complex< T > &z)
Returns the complex arc sine of a
complexnumber.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
complexargument.
- template<typename T> __host__ __device__ complex< T > atan (const complex< T > &z)
Returns the complex arc tangent of a
complexnumber.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
complexargument.
- template<typename T> __host__ __device__ complex< T > acosh (const complex< T > &z)
Returns the complex inverse hyperbolic cosine of a
complexnumber.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
complexargument.
- template<typename T> __host__ __device__ complex< T > asinh (const complex< T > &z)
Returns the complex inverse hyperbolic sine of a
complexnumber.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
complexargument.
- template<typename T> __host__ __device__ complex< T > atanh (const complex< T > &z)
Returns the complex inverse hyperbolic tangent of a
complexnumber.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
complexargument.
-
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
complexnumber in the form (real, imaginary).- Parameters:
os – The output stream.
z – The
complexnumber 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
complexnumber from an input stream.The recognized formats are:
real
(real)
(real, imaginary)
The values read must be convertible to the
complex'svalue_type- Parameters:
is – The input stream.
z – The
complexnumber to set.
- template<typename T0, typename T1> __host__ __device__ bool operator== (const complex< T0 > &x, const complex< T1 > &y)
Returns true if two
complexnumbers 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
complexnumbers 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
complexnumbers 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
complexnumber 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
complexnumber 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
complexnumbers 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
complexnumbers 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
complexnumbers 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
complexnumber 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
complexnumber 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>
complexis the Thrust equivalent tostd::complex. It is functionally identical to it, but can also be used in device code whichstd::complexcurrently cannot.- Template Parameters:
T – The type used to hold the real and imaginary parts. Should be
floatordouble. 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.
xThe firstdevice_vectorof interest.yThe seconddevice_vectorof 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_vectoris 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_vectormay vary dynamically; memory management is automatic. The memory associated with adevice_vectorresides 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::hostis the default parallel execution policy associated with Thrust’s host backend system configured by theTHRUST_HOST_SYSTEMmacro.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::hostas 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::hosttargets 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::hostis implementation-defined.The following code snippet demonstrates how to use
thrust::hostto explicitly dispatch an invocation ofthrust::for_eachto the host backend system:#include <thrust/for_each.h> #include <thrust/execution_policy.h> #include <cstdio> struct printf_functor { __host__ __device__ void operator()(int x) { printf("%d\n", x); } }; ... int vec(3); vec[0] = 0; vec[1] = 1; vec[2] = 2; thrust::for_each(thrust::host, vec.begin(), vec.end(), printf_functor()); // 0 1 2 is printed to standard output in some unspecified order
See also
See also
thrust::device
-
constexpr detail::device_t device#
thrust::deviceis the default parallel execution policy associated with Thrust’s device backend system configured by theTHRUST_DEVICE_SYSTEMmacro.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::deviceas an algorithm parameter.Explicit dispatch can be useful in avoiding the introduction of data copies into containers such as
thrust::device_vectoror 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::malloctypically cannot be dereferenced by a GPU. For this reason, raw pointers allocated by host APIs should not be mixed with athrust::devicealgorithm invocation when the device backend is HIP.The type of
thrust::deviceis implementation-defined.The following code snippet demonstrates how to use
thrust::deviceto explicitly dispatch an invocation ofthrust::for_eachto 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_policyis the base class for all Thrust parallel execution policies which are derived from Thrust’s default host backend system configured with theTHRUST_HOST_SYSTEMmacro.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_policyto implement a backend which specializesfor_eachwhile 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_policyis the base class for all Thrust parallel execution policies which are derived from Thrust’s default device backend system configured with theTHRUST_DEVICE_SYSTEMmacro.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_policyto implement a backend which specializesfor_eachwhile 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)
not1is a helper function to simplify the creation of Adaptable Predicates: it takes an Adaptable Predicatepredas an argument and returns a new Adaptable Predicate that represents the negation ofpred. That is: ifpredis an object of a type which models Adaptable Predicate, then the the type of the resultnpredofnot1(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,
npredsuch thatnpred(x)always returns the same value as!pred(x).
- template<typename BinaryPredicate> __host__ __device__ binary_negate< BinaryPredicate > not2 (const BinaryPredicate &pred)
not2is a helper function to simplify the creation of Adaptable Binary Predicates: it takes an Adaptable Binary Predicatepredas an argument and returns a new Adaptable Binary Predicate that represents the negation ofpred. That is: ifpredis an object of a type which models Adaptable Binary Predicate, then the the type of the resultnpredofnot2(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,
npredsuch thatnpred(x,y)always returns the same value as!pred(x,y).
-
template<typename Argument, typename Result>
struct unary_function# - #include <functional.h>
unary_functionis 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. Thosetypedefsare 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_functionobsolete, 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_functionis 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. Thosetypedefsare 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_functionobsolete, 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_negateis a function object adaptor: it is an Adaptable Predicate that represents the logical negation of some other Adaptable Predicate. That is: iffis an object of classunary_negate<AdaptablePredicate>, then there exists an objectpredof classAdaptablePredicatesuch thatf(x)always returns the same value as!pred(x). There is rarely any reason to construct aunary_negatedirectly; 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_negateis a function object adaptor: it is an Adaptable Binary Predicate that represents the logical negation of some other Adaptable Binary Predicate. That is: iffis an object of classbinary_negate<AdaptablePredicate>, then there exists an objectpredof classAdaptableBinaryPredicatesuch thatf(x,y)always returns the same value as!pred(x,y). There is rarely any reason to construct abinary_negatedirectly; it is almost always easier to use the helper function not2.
Functions
- THRUST_BINARY_FUNCTOR_VOID_SPECIALIZATION_OP (plus,+)
Specialization of
plusfor type void.
- THRUST_BINARY_FUNCTOR_VOID_SPECIALIZATION_OP (minus, -)
Specialization of
minusfor type void.
- THRUST_BINARY_FUNCTOR_VOID_SPECIALIZATION_OP (multiplies, *)
Specialization of
multipliesfor type void.
- THRUST_BINARY_FUNCTOR_VOID_SPECIALIZATION_OP (divides,/)
Specialization of
dividesfor type void.
- THRUST_BINARY_FUNCTOR_VOID_SPECIALIZATION_OP (modulus, %)
Specialization of
modulusfor type void.
- THRUST_UNARY_FUNCTOR_VOID_SPECIALIZATION (negate, -THRUST_FWD(x))
Specialization of
negatefor type void.
-
template<typename T = void>
struct plus# - #include <functional.h>
plusis a function object. Specifically, it is an Adaptable Binary Function. Iffis an object of classplus<T>, andxandyare objects of classT, thenf(x,y)returnsx+y.The following code snippet demonstrates how to use
plusto 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
xandyare objects of typeT, thenx+ymust be defined and must have a return type that is convertible toT.
-
template<typename T = void>
struct minus# - #include <functional.h>
minusis a function object. Specifically, it is an Adaptable Binary Function. Iffis an object of classminus<T>, andxandyare objects of classT, thenf(x,y)returnsx-y.The following code snippet demonstrates how to use
minusto subtract a device_vector offloatsfrom 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
xandyare objects of typeT, thenx-ymust be defined and must have a return type that is convertible toT.
-
template<typename T = void>
struct multiplies# - #include <functional.h>
multipliesis a function object. Specifically, it is an Adaptable Binary Function. Iffis an object of classmultiplies<T>, andxandyare objects of classT, thenf(x,y)returnsx*y.The following code snippet demonstrates how to use
multipliesto 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
xandyare objects of typeT, thenx*ymust be defined and must have a return type that is convertible toT.
-
template<typename T = void>
struct divides# - #include <functional.h>
dividesis a function object. Specifically, it is an Adaptable Binary Function. Iffis an object of classdivides<T>, andxandyare objects of classT, thenf(x,y)returnsx/y.The following code snippet demonstrates how to use
dividesto divide one device_vectors offloatsby 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
xandyare objects of typeT, thenx/ymust be defined and must have a return type that is convertible toT.
-
template<typename T = void>
struct modulus# - #include <functional.h>
modulusis a function object. Specifically, it is an Adaptable Binary Function. Iffis an object of classmodulus<T>, andxandyare objects of classT, thenf(x,y)returnsx % y.The following code snippet demonstrates how to use
modulusto take the modulus of one device_vectors offloatsby 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
xandyare objects of typeT, thenx % ymust be defined and must have a return type that is convertible toT.
-
template<typename T = void>
struct negate# - #include <functional.h>
negateis a function object. Specifically, it is an Adaptable Unary Function. Iffis an object of classnegate<T>, andxis an object of classT, thenf(x)returns-x.The following code snippet demonstrates how to use
negateto 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
xis an object of typeT, then-xmust be defined and must have a return type that is convertible toT.
-
template<typename T = void>
struct square# - #include <functional.h>
squareis a function object. Specifically, it is an Adaptable Unary Function. Iffis an object of classsquare<T>, andxis an object of classT, thenf(x)returnsx*x.The following code snippet demonstrates how to use
squareto 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
xis an object of typeT, thenx*xmust be defined and must have a return type that is convertible toT.
Functions
- THRUST_BINARY_FUNCTOR_VOID_SPECIALIZATION_OP (equal_to,==)
Specialization of
equal_tofor type void.
- THRUST_BINARY_FUNCTOR_VOID_SPECIALIZATION_OP (not_equal_to, !=)
Specialization of
not_equal_tofor type void.
- THRUST_BINARY_FUNCTOR_VOID_SPECIALIZATION_OP (greater, >)
Specialization of
greaterfor type void.
- THRUST_BINARY_FUNCTOR_VOID_SPECIALIZATION_OP (less,<)
Specialization of
lessfor type void.
- THRUST_BINARY_FUNCTOR_VOID_SPECIALIZATION_OP (greater_equal, >=)
Specialization of
greater_equalfor type void.
- THRUST_BINARY_FUNCTOR_VOID_SPECIALIZATION_OP (less_equal,<=)
Specialization of
less_equalfor type void.
-
template<typename T = void>
struct equal_to# - #include <functional.h>
equal_tois 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. Iffis an object of classequal_to<T>andxandyare objects of classT, thenf(x,y)returnstrueifx == yandfalseotherwise.See also
- Template Parameters:
T – is a model of Equality Comparable.
-
template<typename T = void>
struct not_equal_to# - #include <functional.h>
not_equal_tois 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. Iffis an object of classnot_equal_to<T>andxandyare objects of classT, thenf(x,y)returnstrueifx != yandfalseotherwise.See also
- Template Parameters:
T – is a model of Equality Comparable.
-
template<typename T = void>
struct greater# - #include <functional.h>
greateris 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. Iffis an object of classgreater<T>andxandyare objects of classT, thenf(x,y)returnstrueifx > yandfalseotherwise.See also
- Template Parameters:
T – is a model of LessThan Comparable.
-
template<typename T = void>
struct less# - #include <functional.h>
lessis 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. Iffis an object of classless<T>andxandyare objects of classT, thenf(x,y)returnstrueifx < yandfalseotherwise.See also
- Template Parameters:
T – is a model of LessThan Comparable.
-
template<typename T = void>
struct greater_equal# - #include <functional.h>
greater_equalis 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. Iffis an object of classgreater_equal<T>andxandyare objects of classT, thenf(x,y)returnstrueifx >= yandfalseotherwise.See also
- Template Parameters:
T – is a model of LessThan Comparable.
-
template<typename T = void>
struct less_equal# - #include <functional.h>
less_equalis 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. Iffis an object of classless_equal<T>andxandyare objects of classT, thenf(x,y)returnstrueifx <= yandfalseotherwise.See also
- Template Parameters:
T – is a model of LessThan Comparable.
Functions
- THRUST_BINARY_FUNCTOR_VOID_SPECIALIZATION_OP (logical_and, &&)
Specialization of
logical_andfor type void.
- THRUST_BINARY_FUNCTOR_VOID_SPECIALIZATION_OP (logical_or,||)
Specialization of
logical_orfor type void.
- THRUST_UNARY_FUNCTOR_VOID_SPECIALIZATION (logical_not, !THRUST_FWD(x))
Specialization of
logical_notfor type void.
-
template<typename T = void>
struct logical_and# - #include <functional.h>
logical_andis 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. Iffis an object of classlogical_and<T>andxandyare objects of classT(whereTis convertible tobool) thenf(x,y)returnstrueif and only if bothxandyaretrue.See also
- Template Parameters:
T – must be convertible to
bool.
-
template<typename T = void>
struct logical_or# - #include <functional.h>
logical_oris 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. Iffis an object of classlogical_or<T>andxandyare objects of classT(whereTis convertible tobool) thenf(x,y)returnstrueif and only if eitherxoryaretrue.See also
- Template Parameters:
T – must be convertible to
bool.
-
template<typename T = void>
struct logical_not# - #include <functional.h>
logical_notis 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. Iffis an object of classlogical_not<T>andxis an object of classT(whereTis convertible tobool) thenf(x)returnstrueif and only ifxisfalse.The following code snippet demonstrates how to use
logical_notto transform a device_vector ofboolsinto 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_andfor type void.
- THRUST_BINARY_FUNCTOR_VOID_SPECIALIZATION_OP (bit_or,|)
Specialization of
bit_orfor type void.
- THRUST_BINARY_FUNCTOR_VOID_SPECIALIZATION_OP (bit_xor, ^)
Specialization of
bit_xorfor type void.
-
template<typename T = void>
struct bit_and# - #include <functional.h>
bit_andis a function object. Specifically, it is an Adaptable Binary Function. Iffis an object of classbit_and<T>, andxandyare objects of classT, thenf(x,y)returnsx&y.The following code snippet demonstrates how to use
bit_andto take the bitwise AND of one device_vector ofintsby 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
xandyare objects of typeT, thenx&ymust be defined and must have a return type that is convertible toT.
-
template<typename T = void>
struct bit_or# - #include <functional.h>
bit_oris a function object. Specifically, it is an Adaptable Binary Function. Iffis an object of classbit_and<T>, andxandyare objects of classT, thenf(x,y)returnsx|y.The following code snippet demonstrates how to use
bit_orto take the bitwise OR of one device_vector ofintsby 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
xandyare objects of typeT, thenx|ymust be defined and must have a return type that is convertible toT.
-
template<typename T = void>
struct bit_xor# - #include <functional.h>
bit_xoris a function object. Specifically, it is an Adaptable Binary Function. Iffis an object of classbit_and<T>, andxandyare objects of classT, thenf(x,y)returnsx^y.The following code snippet demonstrates how to use
bit_xorto take the bitwise XOR of one device_vector ofintsby 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
xandyare objects of typeT, thenx^ymust be defined and must have a return type that is convertible toT.
Functions
-
THRUST_UNARY_FUNCTOR_VOID_SPECIALIZATION(identity, THRUST_FWD(x))#
Specialization of
identityfor type void.
- THRUST_BINARY_FUNCTOR_VOID_SPECIALIZATION (maximum, t1< t2 ? THRUST_FWD(t2) :THRUST_FWD(t1))
Specialization of
maximumfor type void.
- THRUST_BINARY_FUNCTOR_VOID_SPECIALIZATION (minimum, t1< t2 ? THRUST_FWD(t1) :THRUST_FWD(t2))
Specialization of
minimumfor type void.
-
template<typename T = void>
struct identity# - #include <functional.h>
identityis a Unary Function that represents the identity function: it takes a single argumentx, and returnsx.The following code snippet demonstrates that
identityreturns 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>
maximumis a function object that takes two arguments and returns the greater of the two. Specifically, it is an Adaptable Binary Function. Iffis an object of classmaximum<T>andxandyare objects of classTf(x,y)returnsxifx > yandy, otherwise.The following code snippet demonstrates that
maximumreturns 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>
minimumis a function object that takes two arguments and returns the lesser of the two. Specifically, it is an Adaptable Binary Function. Iffis an object of classminimum<T>andxandyare objects of classTf(x,y)returnsxifx < yandy, otherwise.The following code snippet demonstrates that
minimumreturns 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>
project1stis 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
project1stfor two void arguments.
-
template<typename T1 = void, typename T2 = void>
struct project2nd# - #include <functional.h>
project2ndis 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
project2ndfor two void arguments.
-
namespace placeholders#
Facilities for constructing simple functions inline.
Objects in the
thrust::placeholdersnamespace may be used to create simple arithmetic functions inline in an algorithm invocation. Combining placeholders such as_1and_2with 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
_1and_2withthrust::transformto 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::_1is the placeholder for the first function parameter.
-
constexpr thrust::detail::functional::placeholder<1>::type _2#
thrust::placeholders::_2is the placeholder for the second function parameter.
-
constexpr thrust::detail::functional::placeholder<2>::type _3#
thrust::placeholders::_3is the placeholder for the third function parameter.
-
constexpr thrust::detail::functional::placeholder<3>::type _4#
thrust::placeholders::_4is the placeholder for the fourth function parameter.
-
constexpr thrust::detail::functional::placeholder<4>::type _5#
thrust::placeholders::_5is the placeholder for the fifth function parameter.
-
constexpr thrust::detail::functional::placeholder<5>::type _6#
thrust::placeholders::_6is the placeholder for the sixth function parameter.
-
constexpr thrust::detail::functional::placeholder<6>::type _7#
thrust::placeholders::_7is the placeholder for the seventh function parameter.
-
constexpr thrust::detail::functional::placeholder<7>::type _8#
thrust::placeholders::_8is the placeholder for the eighth function parameter.
-
constexpr thrust::detail::functional::placeholder<8>::type _9#
thrust::placeholders::_9is the placeholder for the ninth function parameter.
-
constexpr thrust::detail::functional::placeholder<9>::type _10#
thrust::placeholders::_10is 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.
xThe firsthost_vectorof interest.yThe secondhost_vectorof 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_vectoris 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_vectormay vary dynamically; memory management is automatic. The memory associated with ahost_vectorresides 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
pairsfor equality.- Parameters:
x – The first
pairto compare.y – The second
pairto compare.
- Template Parameters:
T1 – is a model of Equality Comparable.
T2 – is a model of Equality Comparable.
- Returns:
trueif 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
pairto compare.y – The second
pairto compare.
- Template Parameters:
T1 – is a model of LessThan Comparable.
T2 – is a model of LessThan Comparable.
- Returns:
trueif 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
pairto compare.y – The second
pairto compare.
- Template Parameters:
T1 – is a model of Equality Comparable.
T2 – is a model of Equality Comparable.
- Returns:
trueif 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
pairto compare.y – The second
pairto compare.
- Template Parameters:
T1 – is a model of LessThan Comparable.
T2 – is a model of LessThan Comparable.
- Returns:
trueif 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
pairto compare.y – The second
pairto compare.
- Template Parameters:
T1 – is a model of LessThan Comparable.
T2 – is a model of LessThan Comparable.
- Returns:
trueif 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
pairto compare.y – The second
pairto compare.
- Template Parameters:
T1 – is a model of LessThan Comparable.
T2 – is a model of LessThan Comparable.
- Returns:
trueif and only if!(x < y).
- template<typename T1, typename T2> inline __host__ __device__ void swap (pair< T1, T2 > &x, pair< T1, T2 > &y)
swapswaps the contents of twopairs.- Parameters:
x – The first
pairto swap.y – The second
pairto swap.
- template<typename T1, typename T2> inline __host__ __device__ pair< T1, T2 > make_pair (T1 x, T2 y)
This convenience function creates a
pairfrom 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
paircopied fromaandb.
-
template<typename T1, typename T2>
struct pair# - #include <pair.h>
pairis a generic data structure encapsulating a heterogeneous pair of values.- Template Parameters:
T1 – The type of
pair'sfirst object type. There are no requirements on the type ofT1.T1’s type is provided bypair::first_type.T2 – The type of
pair'ssecond 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'sfirst_typeorsecond_typein its nested type,type.This metafunction returns the type of a
tuple'sNth element.See also
See also
- Template Parameters:
N – This parameter selects the member of interest.
T – A
pairtype of interest.N – This parameter selects the element of interest.
T – A
tupletype 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
tupletype of interest.See also
See also
- Template Parameters:
Pair – A
pairtype of interest.T – A
tupletype of interest.
Functions
- template<typename Assignable1, typename Assignable2> inline __host__ __device__ void swap (Assignable1 &a, Assignable2 &b)
swapassigns the contents ofatoband the contents ofbtoa. This is used as a primitive operation by many other algorithms.The following code snippet demonstrates how to use
swapto 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
getfunction returns a reference to atupleelement of interest.The following code snippet demonstrates how to use
getto print the value of atupleelement.#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
tupleof interest.- Template Parameters:
N – The index of the element of interest.
- Returns:
A reference to
t'sNth 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
getfunction returns aconstreference to atupleelement of interest.The following code snippet demonstrates how to use
getto print the value of atupleelement.#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
tupleof interest.- Template Parameters:
N – The index of the element of interest.
- Returns:
A
constreference tot'sNth element.
- template<class T0> inline __host__ __device__ detail::make_tuple_mapper< T0 >::type make_tuple (const T0 &t0)
This version of
make_tuplecreates a newtupleobject from a single object.- Parameters:
t0 – The object to copy from.
- Returns:
A
tupleobject 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_tuplecreates a newtupleobject from two objects.Note
make_tuplehas 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
tupleobject with two members which are copies oft0andt1.
- template<typename T0> inline __host__ __device__ tuple< T0 & > tie (T0 &t0)
This version of
tiecreates a newtuplewhose single element is a reference which refers to this function’s argument.- Parameters:
t0 – The object to reference.
- Returns:
A
tupleobject 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
tiecreates a newtupleof references object which refers to this function’s arguments.Note
tiehas 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
tupleobject with two members which are references tot0andt1.
- 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)
swapswaps the contents of twotuples.- Parameters:
x – The first
tupleto swap.y – The second
tupleto 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'sfirst_typeorsecond_typein its nested type,type.This metafunction returns the type of a
tuple'sNth element.See also
See also
- Template Parameters:
N – This parameter selects the member of interest.
T – A
pairtype of interest.N – This parameter selects the element of interest.
T – A
tupletype 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
tupletype of interest.See also
See also
- Template Parameters:
Pair – A
pairtype of interest.T – A
tupletype 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>
tupleis 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 oftuplewith two arguments is similar to an instantiation ofpairwith the same two arguments. Individual elements of atuplemay be accessed with thegetfunction.The following code snippet demonstrates how to create a new
tupleobject 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
Ntupleelement. Thrust’stupletype currently supports up to ten elements.
Random Number Generation#
- group random
-
namespace random#
thrust::randomis 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-levelthrustnamespace 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_engineadapts 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 returningrvalues successively produced by the base engine and ends by discardingp-rsuch 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_engineinstance:#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-rwill 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_enginerandom 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_engineinstance:#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_randorminstd_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_enginerandom 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_distributionrandom number distribution produces floating point Normally distributed random numbers.The following code snippet demonstrates examples of using a
normal_distributionwith 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_enginerandom 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_itoy = T mod m. Setcto1ifY < 0, otherwise setcto0.
This algorithm corresponds to a modular linear function of the form
TA(x_i) = (a * x_i) mod b, wherebis of the formm^r - m^s + 1anda = 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_baseorranlux48_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_distributionrandom 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_distributionwith 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_distributionrandom number distribution produces floating point uniform random numbers from a half-open interval.The following code snippet demonstrates examples of using a
uniform_real_distributionwith 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_engineadapts 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_engineinstance:#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
ranlux24shall 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
ranlux48shall 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
taus88shall produce the value3535848941.
-
typedef minstd_rand default_random_engine#
An implementation-defined “default” random number engine.
Note
default_random_engineis 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_rand0shall 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_randshall 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
ranlux24random number engine.Note
The 10000th consecutive invocation of a default-constructed object of type
ranlux24_baseshall 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
ranlux48random number engine.Note
The 10000th consecutive invocation of a default-constructed object of type
ranlux48_baseshall 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_enginesfor equality.- Parameters:
lhs – The first
discard_block_engineto test.rhs – The second
discard_block_engineto test.
- Returns:
trueiflhsis 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_enginesfor inequality.- Parameters:
lhs – The first
discard_block_engineto test.rhs – The second
discard_block_engineto test.
- Returns:
trueiflhsis 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_ostreamto stream out to.e – The
discard_block_engineto 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_istreamto stream from.e – The
discard_block_engineto 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_enginesfor equality.- Parameters:
lhs – The first
xor_combine_engineto test.rhs – The second
xor_combine_engineto test.
- Returns:
trueiflhsis 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_enginesfor inequality.- Parameters:
lhs – The first
xor_combine_engineto test.rhs – The second
xor_combine_engineto test.
- Returns:
trueiflhsis 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_ostreamto stream out to.e – The
xor_combine_engineto 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_istreamto stream from.e – The
xor_combine_engineto 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_engineadapts 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 returningrvalues successively produced by the base engine and ends by discardingp-rsuch 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_engineinstance:#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-rwill 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_engineadapts 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_engineinstance:#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_enginesfor equality.- Parameters:
lhs – The first
linear_congruential_engineto test.rhs – The second
linear_congruential_engineto test.
- Returns:
trueiflhsis 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_enginesfor inequality.- Parameters:
lhs – The first
linear_congruential_engineto test.rhs – The second
linear_congruential_engineto test.
- Returns:
trueiflhsis 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_ostreamto stream out to.e – The
linear_congruential_engineto 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_istreamto stream from.e – The
linear_congruential_engineto 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_enginesfor equality.- Parameters:
lhs – The first
linear_feedback_shift_engineto test.rhs – The second
linear_feedback_shift_engineto test.
- Returns:
trueiflhsis 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_enginesfor inequality.- Parameters:
lhs – The first
linear_feedback_shift_engineto test.rhs – The second
linear_feedback_shift_engineto test.
- Returns:
trueiflhsis 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_ostreamto stream out to.e – The
linear_feedback_shift_engineto 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_istreamto stream from.e – The
linear_feedback_shift_engineto 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_enginesfor equality.- Parameters:
lhs – The first
subtract_with_carry_engineto test.rhs – The second
subtract_with_carry_engineto test.
- Returns:
trueiflhsis 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_enginesfor inequality.- Parameters:
lhs – The first
subtract_with_carry_engineto test.rhs – The second
subtract_with_carry_engineto test.
- Returns:
trueiflhsis 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_ostreamto stream out to.e – The
subtract_with_carry_engineto 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_istreamto stream from.e – The
subtract_with_carry_engineto 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_enginerandom 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_engineinstance:#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_randorminstd_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_enginerandom 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_enginerandom 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_itoy = T mod m. Setcto1ifY < 0, otherwise setcto0.
This algorithm corresponds to a modular linear function of the form
TA(x_i) = (a * x_i) mod b, wherebis of the formm^r - m^s + 1anda = 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_baseorranlux48_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_distributionsfor equality.- Parameters:
lhs – The first
normal_distributionto test.rhs – The second
normal_distributionto test.
- Returns:
trueiflhsis 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_distributionsfor inequality.- Parameters:
lhs – The first
normal_distributionto test.rhs – The second
normal_distributionto test.
- Returns:
trueiflhsis 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_ostreamto stream out to.d – The
normal_distributionto 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_istreamto stream from.d – The
normal_distributionto 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_distributionsfor equality.- Parameters:
lhs – The first
uniform_int_distributionto test.rhs – The second
uniform_int_distributionto test.
- Returns:
trueiflhsis 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_distributionsfor inequality.- Parameters:
lhs – The first
uniform_int_distributionto test.rhs – The second
uniform_int_distributionto test.
- Returns:
trueiflhsis 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_ostreamto stream out to.d – The
uniform_int_distributionto 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_istreamto stream from.d – The
uniform_int_distributionto 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_distributionsfor equality.- Parameters:
lhs – The first
uniform_real_distributionto test.rhs – The second
uniform_real_distributionto test.
- Returns:
trueiflhsis 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_distributionsfor inequality.- Parameters:
lhs – The first
uniform_real_distributionto test.rhs – The second
uniform_real_distributionto test.
- Returns:
trueiflhsis 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_ostreamto stream out to.d – The
uniform_real_distributionto 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_istreamto stream from.d – The
uniform_real_distributionto stream in.
- Returns:
is
-
template<typename RealType = double>
class normal_distribution : public detail::normal_distribution_base::type - #include <normal_distribution.h>
A
normal_distributionrandom number distribution produces floating point Normally distributed random numbers.The following code snippet demonstrates examples of using a
normal_distributionwith 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_distributionrandom 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_distributionwith 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_distributionrandom number distribution produces floating point uniform random numbers from a half-open interval.The following code snippet demonstrates examples of using a
uniform_real_distributionwith 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::systemis 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-levelthrustnamespace 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_categoryserves 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_categoryto support categories of errors in addition to those defined in the C++ International Standard.
-
class error_code#
- #include <error_code.h>
The class
error_codedescribes 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_conditiondescribes an object used to hold values identifying error conditions.Note
error_conditionvalues are portable abstractions, whileerror_codevalues 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_codeenum.
-
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_conditionenum.
- template<> errc_t > : public thrust::detail::true_type
- #include <error_code.h>
Specialization of
is_error_condition_enumforerrc::errc_t
-
class system_error : public std::runtime_error#
- #include <system_error.h>
The class
system_errordescribes 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_errorto report the error codes returned from device backends such as the HIP runtime.The following code listing demonstrates how to catch a
system_errorto 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_allocrather 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_conditionandequivalentvirtual functions shall behave as specified for the classerror_category. The object’snamevirtual 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
evcorresponds to a POSIXerrnovalueposv, 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
equivalentvirtual functions shall behave as specified for classerror_category. The object’snamevirtual function shall return a pointer to the string"system". The object’sdefault_error_conditionvirtual 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_codeenum.
-
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_conditionenum.
-
template<>
struct is_error_condition_enum<errc::errc_t> : public thrust::detail::true_type# - #include <error_code.h>
Specialization of
is_error_condition_enumforerrc::errc_t
-
class error_category
- #include <error_code.h>
The class
error_categoryserves 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_categoryto support categories of errors in addition to those defined in the C++ International Standard.
-
class error_code
- #include <error_code.h>
The class
error_codedescribes 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_conditiondescribes an object used to hold values identifying error conditions.Note
error_conditionvalues are portable abstractions, whileerror_codevalues are implementation specific.
-
class system_error : public std::runtime_error
- #include <system_error.h>
The class
system_errordescribes 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_errorto report the error codes returned from device backends such as the HIP runtime.The following code listing demonstrates how to catch a
system_errorto 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_allocrather 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#