#include <iostream>
#include <cstdlib>
#include <boost/program_options.hpp>
#include <boost/compute/core.hpp>
#include <boost/compute/algorithm/copy.hpp>
#include <boost/compute/container/vector.hpp>
#include <boost/compute/type_traits/type_name.hpp>
#include <boost/compute/utility/source.hpp>
namespace compute = boost::compute;
namespace po = boost::program_options;
using compute::uint_;
const uint_ TILE_DIM = 32;
const uint_ BLOCK_ROWS = 8;
compute::kernel make_copy_kernel(const compute::context& context)
{
const char source[] = BOOST_COMPUTE_STRINGIZE_SOURCE(
__kernel void copy_kernel(__global const float *src, __global float *dst)
{
uint x = get_group_id(0) * TILE_DIM + get_local_id(0);
uint y = get_group_id(1) * TILE_DIM + get_local_id(1);
uint width = get_num_groups(0) * TILE_DIM;
for(uint i = 0 ; i < TILE_DIM ; i+= BLOCK_ROWS){
dst[(y+i)*width +x] = src[(y+i)*width + x];
}
}
);
std::stringstream options;
options << "-DTILE_DIM=" << TILE_DIM << " -DBLOCK_ROWS=" << BLOCK_ROWS;
compute::program program =
compute::program::build_with_source(source, context, options.str());
return program.create_kernel("copy_kernel");
}
compute::kernel make_naive_transpose_kernel(const compute::context& context)
{
const char source[] = BOOST_COMPUTE_STRINGIZE_SOURCE(
__kernel void naive_transpose(__global const float *src, __global float *dst)
{
uint x = get_group_id(0) * TILE_DIM + get_local_id(0);
uint y = get_group_id(1) * TILE_DIM + get_local_id(1);
uint width = get_num_groups(0) * TILE_DIM;
for(uint i = 0 ; i < TILE_DIM; i+= BLOCK_ROWS){
dst[x*width + y+i] = src[(y+i)*width + x];
}
}
);
std::stringstream options;
options << "-DTILE_DIM=" << TILE_DIM << " -DBLOCK_ROWS=" << BLOCK_ROWS;
compute::program program =
compute::program::build_with_source(source, context, options.str());
return program.create_kernel("naive_transpose");
}
compute::kernel make_coalesced_transpose_kernel(const compute::context& context)
{
const char source[] = BOOST_COMPUTE_STRINGIZE_SOURCE(
__kernel void coalesced_transpose(__global const float *src, __global float *dst)
{
__local float tile[TILE_DIM][TILE_DIM];
uint x = get_group_id(0) * TILE_DIM + get_local_id(0);
uint y = get_group_id(1) * TILE_DIM + get_local_id(1);
uint width = get_num_groups(0) * TILE_DIM;
for(uint i = 0 ; i < TILE_DIM; i+= BLOCK_ROWS){
tile[get_local_id(1)+i][get_local_id(0)] = src[(y+i)*width + x];
}
barrier(CLK_LOCAL_MEM_FENCE);
x = get_group_id(1) * TILE_DIM + get_local_id(0);
y = get_group_id(0) * TILE_DIM + get_local_id(1);
for(uint i = 0 ; i < TILE_DIM ; i+=BLOCK_ROWS){
dst[(y+i)*width + x] = tile[get_local_id(0)][get_local_id(1)+i];
}
}
);
std::stringstream options;
options << "-DTILE_DIM=" << TILE_DIM << " -DBLOCK_ROWS=" << BLOCK_ROWS;
compute::program program =
compute::program::build_with_source(source, context, options.str());
return program.create_kernel("coalesced_transpose");
}
compute::kernel make_coalesced_no_bank_conflicts_kernel(const compute::context& context)
{
const char source[] = BOOST_COMPUTE_STRINGIZE_SOURCE(
__kernel void coalesced_no_bank_conflicts(__global const float *src, __global float *dst)
{
__local float tile[TILE_DIM][TILE_DIM+1];
uint x = get_group_id(0) * TILE_DIM + get_local_id(0);
uint y = get_group_id(1) * TILE_DIM + get_local_id(1);
uint width = get_num_groups(0) * TILE_DIM;
for(uint i = 0 ; i < TILE_DIM; i+= BLOCK_ROWS){
tile[get_local_id(1)+i][get_local_id(0)] = src[(y+i)*width + x];
}
barrier(CLK_LOCAL_MEM_FENCE);
x = get_group_id(1) * TILE_DIM + get_local_id(0);
y = get_group_id(0) * TILE_DIM + get_local_id(1);
for(uint i = 0 ; i < TILE_DIM ; i+=BLOCK_ROWS){
dst[(y+i)*width + x] = tile[get_local_id(0)][get_local_id(1)+i];
}
}
);
std::stringstream options;
options << "-DTILE_DIM=" << TILE_DIM << " -DBLOCK_ROWS=" << BLOCK_ROWS;
compute::program program =
compute::program::build_with_source(source, context, options.str());
return program.create_kernel("coalesced_no_bank_conflicts");
}
bool check_transposition(const std::vector<float>& expectedResult,
uint_ size,
const std::vector<float>& transposedMatrix)
{
for(uint_ i = 0 ; i < size ; ++i){
if(expectedResult[i] != transposedMatrix[i]){
std::cout << "idx = " << i << " , expected " << expectedResult[i]
<< " , got " << transposedMatrix[i] << std::endl;
std::cout << "FAILED" << std::endl;
return false;
}
}
return true;
}
void generate_matrix(std::vector<float>& in, std::vector<float>& out, uint_ rows, uint_ cols)
{
for(uint_ i = 0 ; i < rows ; ++i){
for(uint_ j = 0 ; j < cols ; ++j){
in[i*cols + j] = i*cols + j;
}
}
for(uint_ j = 0; j < cols ; ++j){
for(uint_ i = 0 ; i < rows ; ++i){
out[j*rows + i] = in[i*cols + j];
}
}
}
#ifdef _WIN32
#define uint64_t unsigned __int64
#endif
int main(int argc, char *argv[])
{
po::options_description options("options");
options.add_options()
("help", "show usage instructions")
("rows", po::value<uint_>()->default_value(4096), "number of matrix rows")
("cols", po::value<uint_>()->default_value(4096), "number of matrix columns")
;
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, options), vm);
po::notify(vm);
if(vm.count("help")){
std::cout << options << std::endl;
return 0;
}
const uint_ rows = vm["rows"].as<uint_>();
const uint_ cols = vm["cols"].as<uint_>();
compute::device device = compute::system::default_device();
std::cout << "Device: " << device.name() << std::endl;
std::cout << "Matrix Size: " << rows << "x" << cols << std::endl;
std::cout << "Grid Size: " << rows/TILE_DIM << "x" << cols/TILE_DIM << " blocks" << std::endl;
std::cout << "Local Size: " << TILE_DIM << "x" << BLOCK_ROWS << " threads" << std::endl;
std::cout << std::endl;
#if defined(__APPLE__)
if(device.type() & compute::device::cpu) {
std::cout << "On OSX this example does not work on CPU devices" << std::endl;
return 0;
}
#endif
const size_t global_work_size[2] = {rows, cols*BLOCK_ROWS/TILE_DIM};
const size_t local_work_size[2] = {TILE_DIM, BLOCK_ROWS};
const uint_ size = rows * cols;
std::vector<float> h_input(size);
std::vector<float> h_output(size);
std::vector<float> expectedResult(size);
generate_matrix(h_input, expectedResult, rows, cols);
compute::context context(device);
compute::vector<float> d_input(size, context);
compute::vector<float> d_output(size, context);
compute::command_queue queue(context, device, compute::command_queue::enable_profiling);
compute::copy(h_input.begin(), h_input.end(), d_input.begin(), queue);
std::cout << "Testing copy_kernel:" << std::endl;
compute::kernel kernel = make_copy_kernel(context);
kernel.set_arg(0, d_input);
kernel.set_arg(1, d_output);
compute::event start;
start = queue.enqueue_nd_range_kernel(kernel, 2, 0, global_work_size, local_work_size);
queue.finish();
uint64_t elapsed = start.duration<boost::chrono::nanoseconds>().count();
std::cout << " Elapsed: " << elapsed << " ns" << std::endl;
std::cout << " BandWidth: " << 2*rows*cols*sizeof(float) / elapsed << " GB/s" << std::endl;
compute::copy(d_output.begin(), d_output.end(), h_output.begin(), queue);
check_transposition(h_input, rows*cols, h_output);
std::cout << std::endl;
std::cout << "Testing naive_transpose:" << std::endl;
kernel = make_naive_transpose_kernel(context);
kernel.set_arg(0, d_input);
kernel.set_arg(1, d_output);
start = queue.enqueue_nd_range_kernel(kernel, 2, 0, global_work_size, local_work_size);
queue.finish();
elapsed = start.duration<boost::chrono::nanoseconds>().count();
std::cout << " Elapsed: " << elapsed << " ns" << std::endl;
std::cout << " BandWidth: " << 2*rows*cols*sizeof(float) / elapsed << " GB/s" << std::endl;
compute::copy(d_output.begin(), d_output.end(), h_output.begin(), queue);
check_transposition(expectedResult, rows*cols, h_output);
std::cout << std::endl;
std::cout << "Testing coalesced_transpose:" << std::endl;
kernel = make_coalesced_transpose_kernel(context);
kernel.set_arg(0, d_input);
kernel.set_arg(1, d_output);
start = queue.enqueue_nd_range_kernel(kernel, 2, 0, global_work_size, local_work_size);
queue.finish();
elapsed = start.duration<boost::chrono::nanoseconds>().count();
std::cout << " Elapsed: " << elapsed << " ns" << std::endl;
std::cout << " BandWidth: " << 2*rows*cols*sizeof(float) / elapsed << " GB/s" << std::endl;
compute::copy(d_output.begin(), d_output.end(), h_output.begin(), queue);
check_transposition(expectedResult, rows*cols, h_output);
std::cout << std::endl;
std::cout << "Testing coalesced_no_bank_conflicts:" << std::endl;
kernel = make_coalesced_no_bank_conflicts_kernel(context);
kernel.set_arg(0, d_input);
kernel.set_arg(1, d_output);
start = queue.enqueue_nd_range_kernel(kernel, 2, 0, global_work_size, local_work_size);
queue.finish();
elapsed = start.duration<boost::chrono::nanoseconds>().count();
std::cout << " Elapsed: " << elapsed << " ns" << std::endl;
std::cout << " BandWidth: " << 2*rows*cols*sizeof(float) / elapsed << " GB/s" << std::endl;
compute::copy(d_output.begin(), d_output.end(), h_output.begin(), queue);
check_transposition(expectedResult, rows*cols, h_output);
std::cout << std::endl;
return 0;
}