Skip to content

Instantly share code, notes, and snippets.

@ACUVE

ACUVE/main.cpp Secret

Last active December 17, 2015 17:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ACUVE/03ce5ac6cdec1055eb21 to your computer and use it in GitHub Desktop.
Save ACUVE/03ce5ac6cdec1055eb21 to your computer and use it in GitHub Desktop.
#include <vector>
#include <algorithm>
#include <chrono>
#include <boost/compute.hpp>
namespace compute = boost::compute;
template< typename FUNC, typename... Args >
inline std::chrono::nanoseconds time( FUNC &&func, Args &&... args )
{
auto const start = std::chrono::high_resolution_clock::now();
func( std::forward< Args >( args )... );
auto const end = std::chrono::high_resolution_clock::now();
auto const duration = end - start;
return std::chrono::duration_cast< std::chrono::nanoseconds >( duration );
}
int main()
{
// get the default compute device
compute::device gpu = compute::system::default_device();
// create a compute context and command queue
compute::context ctx( gpu );
compute::command_queue queue( ctx, gpu );
// generate random numbers on the host
std::vector<float> host_vector( 200000000 );
// std::vector<float> host_vector( 1000 );
std::vector<float> host_result_vector( host_vector.size() );
// create vector on the device
compute::vector<float> device_vector( host_vector.size(), ctx );
auto generate_time = time(
[ & ]
{
std::generate( host_vector.begin(), host_vector.end(), rand );
}
).count();
// copy data to the device
auto copy_to_device_time = time(
[ & ]
{
compute::copy(
host_vector.begin(), host_vector.end(), device_vector.begin(), queue
);
}
).count();
// sort data on the device
auto sort_time = time(
[ & ]
{
compute::sort(
device_vector.begin(), device_vector.end(), queue
);
}
).count();
// copy data back to the host
auto copy_to_host_time = time(
[ & ]
{
compute::copy(
device_vector.begin(), device_vector.end(), host_result_vector.begin(), queue
);
}
).count();
// sort data on the host
auto sort_host_time = time(
[ & ]
{
std::sort( host_vector.begin(), host_vector.end() );
}
).count();
std::cout << "ocl result ok? " << std::is_sorted( host_result_vector.begin(), host_result_vector.end() ) << std::endl;
std::cout << "host result ok? " << std::is_sorted( host_vector.begin(), host_vector.end() ) << std::endl;
std::cout << "generate_time : " << generate_time << std::endl;
std::cout << "--------------" << std::endl;
std::cout << "copy_to_device_time: " << copy_to_device_time << std::endl;
std::cout << "sort_time : " << sort_time << std::endl;
std::cout << "copy_to_host_time : " << copy_to_host_time << std::endl;
std::cout << "↓ " << std::endl;
std::cout << "sort_device_time : " << copy_to_device_time + sort_time + copy_to_host_time << std::endl;
std::cout << "--------------" << std::endl;
std::cout << "sort_host_time : " << sort_host_time << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment