| | #include <thrust/host_vector.h> |
| | #include <thrust/device_vector.h> |
| | #include <thrust/functional.h> |
| | #include <thrust/transform.h> |
| | #include <thrust/iterator/zip_iterator.h> |
| | #include <thrust/random.h> |
| |
|
| |
|
| | |
| | |
| | |
| | |
| | |
| |
|
| |
|
| |
|
| | |
| | typedef thrust::tuple<float,float,float> Float3; |
| |
|
| |
|
| | |
| | struct DotProduct : public thrust::binary_function<Float3,Float3,float> |
| | { |
| | __host__ __device__ |
| | float operator()(const Float3& a, const Float3& b) const |
| | { |
| | return thrust::get<0>(a) * thrust::get<0>(b) + |
| | thrust::get<1>(a) * thrust::get<1>(b) + |
| | thrust::get<2>(a) * thrust::get<2>(b); |
| | } |
| | }; |
| |
|
| |
|
| |
|
| | |
| | thrust::host_vector<float> random_vector(const size_t N, |
| | unsigned int seed = thrust::default_random_engine::default_seed) |
| | { |
| | thrust::default_random_engine rng(seed); |
| | thrust::uniform_real_distribution<float> u01(0.0f, 1.0f); |
| | thrust::host_vector<float> temp(N); |
| | for(size_t i = 0; i < N; i++) { |
| | temp[i] = u01(rng); |
| | } |
| | return temp; |
| | } |
| |
|
| |
|
| | int main(void) |
| | { |
| | |
| | const size_t N = 1000; |
| |
|
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | thrust::device_vector<float> A0 = random_vector(N); |
| | thrust::device_vector<float> A1 = random_vector(N); |
| | thrust::device_vector<float> A2 = random_vector(N); |
| |
|
| | thrust::device_vector<float> B0 = random_vector(N); |
| | thrust::device_vector<float> B1 = random_vector(N); |
| | thrust::device_vector<float> B2 = random_vector(N); |
| |
|
| | |
| | thrust::device_vector<float> result(N); |
| |
|
| |
|
| | |
| | |
| | |
| |
|
| |
|
| | |
| | |
| | typedef thrust::device_vector<float>::iterator FloatIterator; |
| | typedef thrust::tuple<FloatIterator, FloatIterator, FloatIterator> FloatIteratorTuple; |
| | typedef thrust::zip_iterator<FloatIteratorTuple> Float3Iterator; |
| |
|
| | |
| | Float3Iterator A_first = thrust::make_zip_iterator(thrust::make_tuple(A0.begin(), A1.begin(), A2.begin())); |
| | Float3Iterator A_last = thrust::make_zip_iterator(thrust::make_tuple(A0.end(), A1.end(), A2.end())); |
| | Float3Iterator B_first = thrust::make_zip_iterator(thrust::make_tuple(B0.begin(), B1.begin(), B2.begin())); |
| |
|
| | |
| | |
| | thrust::transform(A_first, A_last, B_first, result.begin(), DotProduct()); |
| |
|
| |
|
| | |
| | |
| | |
| | thrust::transform( thrust::make_zip_iterator(thrust::make_tuple(A0.begin(), A1.begin(), A2.begin())), |
| | thrust::make_zip_iterator(thrust::make_tuple(A0.end(), A1.end(), A2.end())), |
| | thrust::make_zip_iterator(thrust::make_tuple(B0.begin(), B1.begin(), B2.begin())), |
| | result.begin(), |
| | DotProduct() ); |
| |
|
| |
|
| |
|
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | std::cout << std::fixed; |
| | for(size_t i = 0; i < 4; i++) |
| | { |
| | Float3 a = A_first[i]; |
| | Float3 b = B_first[i]; |
| | float dot = result[i]; |
| |
|
| | std::cout << "(" << thrust::get<0>(a) << "," << thrust::get<1>(a) << "," << thrust::get<2>(a) << ")"; |
| | std::cout << " * "; |
| | std::cout << "(" << thrust::get<0>(b) << "," << thrust::get<1>(b) << "," << thrust::get<2>(b) << ")"; |
| | std::cout << " = "; |
| | std::cout << dot << std::endl; |
| | } |
| |
|
| | return 0; |
| | } |
| |
|
| |
|