Views
No views yet
1#include <thrust/host_vector.h>
2#include <thrust/device_vector.h>
3#include <thrust/generate.h>
4#include <thrust/sort.h>
5#include <thrust/copy.h>
6#include <thrust/random.h>
7
8int main() {
9 // Generate 32M random numbers serially.
10 thrust::default_random_engine rng(1337);
11 thrust::uniform_int_distribution<int> dist;
12 thrust::host_vector<int> h_vec(32 << 20);
13 thrust::generate(h_vec.begin(), h_vec.end(), [&] { return dist(rng); });
14
15 // Transfer data to the device.
16 thrust::device_vector<int> d_vec = h_vec;
17
18 // Sort data on the device.
19 thrust::sort(d_vec.begin(), d_vec.end());
20
21 // Transfer data back to host.
22 thrust::copy(d_vec.begin(), d_vec.end(), h_vec.begin());
23}1#include <thrust/host_vector.h>
2#include <thrust/device_vector.h>
3#include <thrust/generate.h>
4#include <thrust/reduce.h>
5#include <thrust/functional.h>
6#include <thrust/random.h>
7
8int main() {
9 // Generate random data serially.
10 thrust::default_random_engine rng(1337);
11 thrust::uniform_real_distribution<double> dist(-50.0, 50.0);
12 thrust::host_vector<double> h_vec(32 << 20);
13 thrust::generate(h_vec.begin(), h_vec.end(), [&] { return dist(rng); });
14
15 // Transfer to device and compute the sum.
16 thrust::device_vector<double> d_vec = h_vec;
17 double x = thrust::reduce(d_vec.begin(), d_vec.end(), 0, thrust::plus<int>());
18}1#include <thrust/host_vector.h>
2#include <thrust/device_vector.h>
3#include <thrust/generate.h>
4#include <thrust/async/copy.h>
5#include <thrust/async/reduce.h>
6#include <thrust/functional.h>
7#include <thrust/random.h>
8#include <numeric>
9
10int main() {
11 // Generate 32M random numbers serially.
12 thrust::default_random_engine rng(123456);
13 thrust::uniform_real_distribution<double> dist(-50.0, 50.0);
14 thrust::host_vector<double> h_vec(32 << 20);
15 thrust::generate(h_vec.begin(), h_vec.end(), [&] { return dist(rng); });
16
17 // Asynchronously transfer to the device.
18 thrust::device_vector<double> d_vec(h_vec.size());
19 thrust::device_event e = thrust::async::copy(h_vec.begin(), h_vec.end(),
20 d_vec.begin());
21
22 // After the transfer completes, asynchronously compute the sum on the device.
23 thrust::device_future<double> f0 = thrust::async::reduce(thrust::device.after(e),
24 d_vec.begin(), d_vec.end(),
25 0.0, thrust::plus<double>());
26
27 // While the sum is being computed on the device, compute the sum serially on
28 // the host.
29 double f1 = std::accumulate(h_vec.begin(), h_vec.end(), 0.0, thrust::plus<double>());
30}include/thrust. This will be suitable for most users.git clone --recursive https://github.com/NVIDIA/thrust.gitfind_package. See the CMake README for more
information. Thrust can also be added via add_subdirectory or tools like
the CMake Package Manager.-I<thrust repo root>)-I<thrust repo root>/dependencies/libcudacxx/)-I<thrust repo root>/dependencies/cub/)-DTHRUST_HOST_SYSTEM=THRUST_HOST_SYSTEM_XXX,
where XXX is CPP (serial, default), OMP (OpenMP), or TBB (Intel TBB)-DTHRUST_DEVICE_SYSTEM=THRUST_DEVICE_SYSTEM_XXX, where XXX is
CPP, OMP, TBB, or CUDA (default).1# Clone Thrust and CUB repos recursively:
2git clone --recursive https://github.com/NVIDIA/thrust.git
3cd thrust
4
5# Build and run tests and examples:
6ci/local/build.bash1# Clone Thrust and CUB repos recursively:
2git clone --recursive https://github.com/NVIDIA/thrust.git
3cd thrust
4
5# Create build directory:
6mkdir build
7cd build
8
9# Configure -- use one of the following:
10cmake .. # Command line interface.
11ccmake .. # ncurses GUI (Linux only).
12cmake-gui # Graphical UI, set source/build directories in the app.
13
14# Build:
15cmake --build . -j ${NUM_JOBS} # Invokes make (or ninja, etc).
16
17# Run tests and examples:
18ctestCPP host system, CUDA accelerated device system, and
C++14 standard are used.
This can be changed in CMake and via flags to ci/local/build.bash