Skip to content

Instantly share code, notes, and snippets.

@tanakamura
Created November 10, 2013 12:31
Show Gist options
  • Save tanakamura/7397685 to your computer and use it in GitHub Desktop.
Save tanakamura/7397685 to your computer and use it in GitHub Desktop.
#define __CL_ENABLE_EXCEPTIONS
#include <CL/cl.hpp>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/fcntl.h>
#include <iostream>
#include <sys/time.h>
#include <stdio.h>
double sec()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec + tv.tv_usec/1000000.0;
}
int
main(int argc, char **argv)
{
cl_int err = CL_SUCCESS;
if (argc < 2) {
return 1;
}
char *path = argv[1];
int len = strlen(path);
bool do_binary = true;
if (path[len-3] == '.' &&
path[len-2] == 'c' &&
path[len-1] == 'l')
{
do_binary = false;
}
try {
std::vector<cl::Platform> platforms;
cl::Platform::get(&platforms);
if (platforms.size() == 0) {
std::cout << "Platform size 0\n";
return -1;
}
cl_context_properties properties[] =
{ CL_CONTEXT_PLATFORM, (cl_context_properties)(platforms[0])(), 0};
cl::Context context(CL_DEVICE_TYPE_GPU, properties);
std::vector<cl::Device> devices = context.getInfo<CL_CONTEXT_DEVICES>();
cl::Program program_;
char *buffer;
int size;
{
struct stat st_buf;
stat(argv[1], &st_buf);
size = st_buf.st_size;
buffer=(char*)malloc(size);
int fd = open(argv[1], O_RDONLY);
read(fd, buffer, size);
close(fd);
}
cl::Program::Binaries binary;
cl::Program::Sources source;
if (do_binary) {
binary = cl::Program::Binaries(1, std::make_pair(buffer,size));
program_ = cl::Program(context, devices, binary);
} else {
source = cl::Program::Sources(1,
std::make_pair(buffer,size));
program_ = cl::Program(context, source);
}
cl::CommandQueue queue(context, devices[0], 0, &err);
int nloop = 16;
for (int i=0; i<nloop; i++) {
program_.build(devices);
cl::Kernel kernel(program_, "hello", &err);
cl::Event event;
queue.enqueueNDRangeKernel(
kernel,
cl::NullRange,
cl::NDRange(4,4),
cl::NullRange,
NULL,
&event);
event.wait();
}
double b = sec();
for (int i=0; i<nloop; i++) {
program_.build(devices);
cl::Event event;
cl::Kernel kernel(program_, "hello", &err);
queue.enqueueNDRangeKernel(
kernel,
cl::NullRange,
cl::NDRange(4,4),
cl::NullRange,
NULL,
&event);
event.wait();
}
double e = sec();
printf("%.20f\n", (e-b)/nloop);
} catch (cl::Error err) {
std::cerr
<< "ERROR: "
<< err.what()
<< "("
<< err.err()
<< ")"
<< std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment