Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $ cat t503.cu
- #include <stdio.h>
- #include <thrust/host_vector.h>
- #include <thrust/device_vector.h>
- #include <thrust/inner_product.h>
- #include <thrust/complex.h>
- #include <thrust/execution_policy.h>
- int main(int argc, char **argv)
- {
- int vec_size = atoi(argv[1]);
- int iterations = atoi(argv[2]);
- float milliseconds = 0;
- cudaEvent_t start, stop;
- cudaEventCreate(&start);
- cudaEventCreate(&stop);
- thrust::host_vector< thrust::complex<float> > host_x( vec_size );
- thrust::generate(host_x.begin(), host_x.end(), rand);
- thrust::host_vector< thrust::complex<float> > host_y( vec_size );
- thrust::generate(host_y.begin(), host_y.end(), rand);
- printf("vector size = %lu bytes\n", vec_size * sizeof(thrust::complex<float>));
- cudaEventRecord(start);
- thrust::device_vector< thrust::complex<float> > device_x = host_x;
- thrust::device_vector< thrust::complex<float> > device_y = host_y;
- cudaEventRecord(stop);
- cudaEventSynchronize(stop);
- cudaEventElapsedTime(&milliseconds, start, stop);
- printf("copy (device)\t\t%f ms\n", milliseconds);
- cudaEventRecord(start);
- for(int i = 0; i < iterations; ++i)
- {
- thrust::inner_product(thrust::cuda::par, device_x.begin(), device_x.end(), device_y.begin(), thrust::complex<float>(0.0f,0.0f) );
- }
- cudaEventRecord(stop);
- cudaEventSynchronize(stop);
- cudaEventElapsedTime(&milliseconds, start, stop);
- printf("inner_product (device)\t%f ms\n", milliseconds/iterations);
- cudaEventRecord(start);
- for(int i = 0; i < iterations; ++i)
- {
- thrust::inner_product(thrust::host, host_x.begin(), host_x.end(), host_y.begin(), thrust::complex<float>(0.0f,0.0f) );
- }
- cudaEventRecord(stop);
- cudaEventSynchronize(stop);
- cudaEventElapsedTime(&milliseconds, start, stop);
- printf("inner_product (host)\t%f ms\n", milliseconds/iterations);
- return 0;
- }
- $ nvcc -O3 -arch=sm_20 -o t503 t503.cu
- $ ./t503 3100000 1000
- vector size = 24800000 bytes
- copy (device) 25.308224 ms
- inner_product (device) 2.992532 ms
- inner_product (host) 30.961256 ms
- $
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement