Contoh | Godbolt | Dokumentasi |
---|
Thrust adalah pustaka algoritma paralel C++ yang menginspirasi pengenalan algoritma paralel ke Perpustakaan Standar C++. Antarmuka tingkat tinggi Thrust sangat meningkatkan produktivitas pemrogram sekaligus memungkinkan portabilitas kinerja antara GPU dan CPU multicore. Itu dibangun di atas kerangka pemrograman paralel yang sudah ada (seperti CUDA, TBB, dan OpenMP). Ini juga menyediakan sejumlah fasilitas tujuan umum serupa dengan yang ditemukan di Perpustakaan Standar C++.
Perpustakaan Standar NVIDIA C++ adalah proyek sumber terbuka; ini tersedia di GitHub dan disertakan dalam NVIDIA HPC SDK dan CUDA Toolkit. Jika Anda telah menginstal salah satu SDK tersebut, tidak diperlukan instalasi tambahan atau flag compiler untuk menggunakan libcu++.
Dorongan paling baik dipelajari melalui contoh.
Contoh berikut menghasilkan nomor acak secara serial dan kemudian mentransfernya ke perangkat paralel tempat nomor tersebut diurutkan.
# include < thrust/host_vector.h >
# include < thrust/device_vector.h >
# include < thrust/generate.h >
# include < thrust/sort.h >
# include < thrust/copy.h >
# include < thrust/random.h >
int main () {
// Generate 32M random numbers serially.
thrust::default_random_engine rng ( 1337 );
thrust::uniform_int_distribution< int > dist;
thrust::host_vector< int > h_vec ( 32 << 20 );
thrust::generate (h_vec. begin (), h_vec. end (), [&] { return dist (rng); });
// Transfer data to the device.
thrust::device_vector< int > d_vec = h_vec;
// Sort data on the device.
thrust::sort (d_vec. begin (), d_vec. end ());
// Transfer data back to host.
thrust::copy (d_vec. begin (), d_vec. end (), h_vec. begin ());
}
Lihat di Godbolt
Contoh ini menunjukkan penghitungan jumlah beberapa bilangan acak secara paralel:
# include < thrust/host_vector.h >
# include < thrust/device_vector.h >
# include < thrust/generate.h >
# include < thrust/reduce.h >
# include < thrust/functional.h >
# include < thrust/random.h >
int main () {
// Generate random data serially.
thrust::default_random_engine rng ( 1337 );
thrust::uniform_real_distribution< double > dist (- 50.0 , 50.0 );
thrust::host_vector< double > h_vec ( 32 << 20 );
thrust::generate (h_vec. begin (), h_vec. end (), [&] { return dist (rng); });
// Transfer to device and compute the sum.
thrust::device_vector< double > d_vec = h_vec;
double x = thrust::reduce (d_vec. begin (), d_vec. end (), 0 , thrust::plus< int >());
}
Lihat di Godbolt
Contoh ini menunjukkan cara melakukan pengurangan secara asinkron:
# include < thrust/host_vector.h >
# include < thrust/device_vector.h >
# include < thrust/generate.h >
# include < thrust/async/copy.h >
# include < thrust/async/reduce.h >
# include < thrust/functional.h >
# include < thrust/random.h >
# include < numeric >
int main () {
// Generate 32M random numbers serially.
thrust::default_random_engine rng ( 123456 );
thrust::uniform_real_distribution< double > dist (- 50.0 , 50.0 );
thrust::host_vector< double > h_vec ( 32 << 20 );
thrust::generate (h_vec. begin (), h_vec. end (), [&] { return dist (rng); });
// Asynchronously transfer to the device.
thrust::device_vector< double > d_vec (h_vec. size ());
thrust::device_event e = thrust::async::copy (h_vec. begin (), h_vec. end (),
d_vec. begin ());
// After the transfer completes, asynchronously compute the sum on the device.
thrust::device_future< double > f0 = thrust::async::reduce (thrust::device. after (e),
d_vec. begin (), d_vec. end (),
0.0 , thrust::plus< double >());
// While the sum is being computed on the device, compute the sum serially on
// the host.
double f1 = std::accumulate (h_vec. begin (), h_vec. end (), 0.0 , thrust::plus< double >());
}
Lihat di Godbolt
Thrust adalah perpustakaan khusus header; tidak perlu membangun atau menginstal proyek kecuali Anda ingin menjalankan pengujian unit Thrust.
CUDA Toolkit menyediakan rilis terbaru kode sumber Thrust di include/thrust
. Ini cocok untuk sebagian besar pengguna.
Pengguna yang ingin berkontribusi pada Thrust atau mencoba fitur-fitur baru harus mengkloning repositori Thrust Github secara rekursif:
git clone --recursive https://github.com/NVIDIA/thrust.git
Untuk proyek berbasis CMake, kami menyediakan paket CMake untuk digunakan dengan find_package
. Lihat CMake README untuk informasi lebih lanjut. Thrust juga dapat ditambahkan melalui add_subdirectory
atau alat seperti CMake Package Manager.
Untuk proyek non-CMake, kompilasi dengan:
-I<thrust repo root>
)-I<thrust repo root>/dependencies/libcudacxx/
)-I<thrust repo root>/dependencies/cub/
)-DTHRUST_HOST_SYSTEM=THRUST_HOST_SYSTEM_XXX
, dengan XXX
adalah CPP
(serial, default), OMP
(OpenMP), atau TBB
(Intel TBB)-DTHRUST_DEVICE_SYSTEM=THRUST_DEVICE_SYSTEM_XXX
, dengan XXX
adalah CPP
, OMP
, TBB
, atau CUDA
(default). Thrust menggunakan sistem build CMake untuk membuat pengujian unit, contoh, dan pengujian header. Untuk membangun Thrust sebagai pengembang, disarankan agar Anda menggunakan sistem pengembangan terkontainer kami:
# Clone Thrust and CUB repos recursively:
git clone --recursive https://github.com/NVIDIA/thrust.git
cd thrust
# Build and run tests and examples:
ci/local/build.bash
Itu melakukan hal yang sama dengan yang berikut ini, tetapi dalam lingkungan yang bersih dalam container yang memiliki semua dependensi yang diinstal:
# Clone Thrust and CUB repos recursively:
git clone --recursive https://github.com/NVIDIA/thrust.git
cd thrust
# Create build directory:
mkdir build
cd build
# Configure -- use one of the following:
cmake .. # Command line interface.
ccmake .. # ncurses GUI (Linux only).
cmake-gui # Graphical UI, set source/build directories in the app.
# Build:
cmake --build . -j ${NUM_JOBS} # Invokes make (or ninja, etc).
# Run tests and examples:
ctest
Secara default, sistem host CPP
serial, sistem perangkat akselerasi CUDA
, dan standar C++14 digunakan. Ini dapat diubah di CMake dan melalui flag ke ci/local/build.bash
Informasi lebih lanjut tentang mengonfigurasi build Thrust Anda dan membuat permintaan penarikan dapat ditemukan di bagian kontribusi.
Thrust adalah proyek sumber terbuka yang dikembangkan di GitHub. Thrust didistribusikan di bawah Lisensi Apache v2.0 dengan Pengecualian LLVM; beberapa bagian didistribusikan di bawah Lisensi Apache v2.0 dan Lisensi Boost v1.0.