diff --git a/005_thrust_algos/Makefile b/005_thrust_algos/Makefile new file mode 100644 index 0000000..a79bd9c --- /dev/null +++ b/005_thrust_algos/Makefile @@ -0,0 +1,9 @@ +all: hw + +hw: + nvcc -x cu -O3 -std=c++20 --extended-lambda -arch=sm_86 -I/usr/local/cuda-12.2/bin/../include ./algos.cu -o main -L/usr/local/cuda-12.2/bin/../lib64 + +clean: + rm -f main + +.PHONY: clean hw diff --git a/005_thrust_algos/algos.cu b/005_thrust_algos/algos.cu new file mode 100644 index 0000000..993ddb2 --- /dev/null +++ b/005_thrust_algos/algos.cu @@ -0,0 +1,91 @@ +#include +#include + +struct plus_one_functor { + __device__ + int operator()(int x) { + return x + 1; + } +}; + +void print_vector(thrust::device_vector v) { + thrust::for_each(v.begin(), v.end(), [] __host__ __device__(int val) { + printf("%d ", val); + }); + std::cout << std::endl; +} + +int main() { + // Sorting + { + std::vector h_vec{3, 1, 4, 2, 8, 5, 9, 12, 6}; + thrust::device_vector d_vec = h_vec; + thrust::sort( + thrust::device, + d_vec.begin(), + d_vec.end() + ); + print_vector(d_vec); + } + + // Filling + { + thrust::device_vector d_vec(10); + thrust::fill( + thrust::device, + d_vec.begin(), + d_vec.end(), + 101 + ); + print_vector(d_vec); + } + + // Sequence + { + thrust::device_vector d_vec(10); + thrust::sequence( + thrust::device, + d_vec.begin(), + d_vec.end() + ); + print_vector(d_vec); + } + + // Transform + { + thrust::device_vector d_vec(10); + thrust::sequence( + thrust::device, + d_vec.begin(), + d_vec.end() + ); + print_vector(d_vec); + // Now, we will add 1 to every element + thrust::transform( + thrust::device, + d_vec.begin(), + d_vec.end(), + d_vec.begin(), + plus_one_functor() + ); + print_vector(d_vec); + } + + // Merge + { + std::vector h_vec1 = {1, 2, 3, 4, 5}; + std::vector h_vec2 = {6, 7, 8, 9, 10}; + thrust::device_vector d_vec1 = h_vec1; + thrust::device_vector d_vec2 = h_vec2; + thrust::device_vector merged_vec(10); + thrust::merge( + thrust::device, + d_vec1.begin(), + d_vec1.end(), + d_vec2.begin(), + d_vec2.end(), + merged_vec.begin() + ); + print_vector(merged_vec); + } +}