Created
January 14, 2014 19:51
-
-
Save koflerdavid/8424515 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__kernel void compare ( | |
__global char *inputA, | |
__global char *inputB, | |
__global int *output | |
) | |
{ | |
int id = get_global_id(0); | |
if (inputA[id] < inputB[id]) | |
output[id] = id + 1; | |
else if (inputA[id] > inputB[id]) | |
output[id] = - (id + 1); | |
else | |
output[id] = 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <sys/time.h> | |
#include <vector> | |
#ifdef __APPLE__ | |
#include "OpenCL/opencl.h" | |
#else | |
#include "CL/cl.h" | |
#endif | |
#include "helper.h" | |
using namespace std; | |
int main(int argc, const char *argv[]) { | |
vector<cl_platform_id> platform_ids = GetPlatformIds(); | |
for (cl_uint i = 0; i < platform_ids.size(); i++) { | |
cout << "\t (" << i << ") : " << GetPlatformName(platform_ids[i]) << endl; | |
} | |
vector<cl_device_id> device_ids = GetDeviceIds(platform_ids[0]); | |
for (cl_uint i = 0; i < device_ids.size(); i++) { | |
cout << "\t\t (" << i << ") : " << GetDeviceName(device_ids[0]) << endl; | |
} | |
cl_context context = GetContext(platform_ids[0], device_ids); | |
cl_command_queue queue = GetQueue(context, device_ids[0]); | |
cl_program program = GetProgram(context, device_ids, "assignment_3.cl"); | |
cl_kernel kernel = GetKernel(program, "compare"); | |
// start timing | |
timeval t1, t2; | |
gettimeofday(&t1, NULL); | |
// -------------------------------------------------------------------------------- | |
// DO STUFF HERE | |
// -------------------------------------------------------------------------------- | |
// stop timing | |
gettimeofday(&t2, NULL); | |
double elapsedTime = (t2.tv_sec - t1.tv_sec); | |
elapsedTime += (t2.tv_usec - t1.tv_usec); | |
cout << "time taken: " << elapsedTime << " us" << endl; | |
// clean up | |
clReleaseKernel(kernel); | |
clReleaseProgram(program); | |
clReleaseCommandQueue(queue); | |
clReleaseContext(context); | |
return EXIT_SUCCESS; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "helper.h" | |
using namespace std; | |
void CheckError(cl_int err) { | |
CheckError(err, ""); | |
} | |
void CheckError(cl_int err, const char *label) { | |
if (err != CL_SUCCESS) { | |
const char *name; | |
switch (err) { | |
case CL_SUCCESS: name = "Success!";break; | |
case CL_DEVICE_NOT_FOUND: name = "Device not found.";break; | |
case CL_DEVICE_NOT_AVAILABLE: name = "Device not available";break; | |
case CL_COMPILER_NOT_AVAILABLE: name = "Compiler not available";break; | |
case CL_MEM_OBJECT_ALLOCATION_FAILURE: name = "Memory object allocation failure";break; | |
case CL_OUT_OF_RESOURCES: name = "Out of resources";break; | |
case CL_OUT_OF_HOST_MEMORY: name = "Out of host memory";break; | |
case CL_PROFILING_INFO_NOT_AVAILABLE: name = "Profiling information not available";break; | |
case CL_MEM_COPY_OVERLAP: name = "Memory copy overlap";break; | |
case CL_IMAGE_FORMAT_MISMATCH: name = "Image format mismatch";break; | |
case CL_IMAGE_FORMAT_NOT_SUPPORTED: name = "Image format not supported";break; | |
case CL_BUILD_PROGRAM_FAILURE: name = "Program build failure";break; | |
case CL_MAP_FAILURE: name = "Map failure";break; | |
case CL_INVALID_VALUE: name = "Invalid value";break; | |
case CL_INVALID_DEVICE_TYPE: name = "Invalid device type";break; | |
case CL_INVALID_PLATFORM: name = "Invalid platform";break; | |
case CL_INVALID_DEVICE: name = "Invalid device";break; | |
case CL_INVALID_CONTEXT: name = "Invalid context";break; | |
case CL_INVALID_QUEUE_PROPERTIES: name = "Invalid queue properties";break; | |
case CL_INVALID_COMMAND_QUEUE: name = "Invalid command queue";break; | |
case CL_INVALID_HOST_PTR: name = "Invalid host pointer";break; | |
case CL_INVALID_MEM_OBJECT: name = "Invalid memory object";break; | |
case CL_INVALID_IMAGE_FORMAT_DESCRIPTOR: name = "Invalid image format descriptor";break; | |
case CL_INVALID_IMAGE_SIZE: name = "Invalid image size";break; | |
case CL_INVALID_SAMPLER: name = "Invalid sampler";break; | |
case CL_INVALID_BINARY: name = "Invalid binary";break; | |
case CL_INVALID_BUILD_OPTIONS: name = "Invalid build options";break; | |
case CL_INVALID_PROGRAM: name = "Invalid program";break; | |
case CL_INVALID_PROGRAM_EXECUTABLE: name = "Invalid program executable";break; | |
case CL_INVALID_KERNEL_NAME: name = "Invalid kernel name";break; | |
case CL_INVALID_KERNEL_DEFINITION: name = "Invalid kernel definition";break; | |
case CL_INVALID_KERNEL: name = "Invalid kernel";break; | |
case CL_INVALID_ARG_INDEX: name = "Invalid argument index";break; | |
case CL_INVALID_ARG_VALUE: name = "Invalid argument value";break; | |
case CL_INVALID_ARG_SIZE: name = "Invalid argument size";break; | |
case CL_INVALID_KERNEL_ARGS: name = "Invalid kernel arguments";break; | |
case CL_INVALID_WORK_DIMENSION: name = "Invalid work dimension";break; | |
case CL_INVALID_WORK_GROUP_SIZE: name = "Invalid work group size";break; | |
case CL_INVALID_WORK_ITEM_SIZE: name = "Invalid work item size";break; | |
case CL_INVALID_GLOBAL_OFFSET: name = "Invalid global offset";break; | |
case CL_INVALID_EVENT_WAIT_LIST: name = "Invalid event wait list";break; | |
case CL_INVALID_EVENT: name = "Invalid event";break; | |
case CL_INVALID_OPERATION: name = "Invalid operation";break; | |
case CL_INVALID_GL_OBJECT: name = "Invalid OpenGL object";break; | |
case CL_INVALID_BUFFER_SIZE: name = "Invalid buffer size";break; | |
case CL_INVALID_MIP_LEVEL: name = "Invalid mip-map level";break; | |
default: name = "Unknown"; | |
} | |
cerr << "OpenCL error occurred " << label << ": ("<< err << ") " << name << endl; | |
exit(err); | |
} | |
} | |
vector<cl_platform_id> GetPlatformIds(void) { | |
cl_uint count = 0; | |
clGetPlatformIDs(0, 0, &count); | |
if (count == 0) { | |
cerr << "No OpenCL platform found" << endl; | |
exit(EXIT_FAILURE); | |
} else { | |
cout << "Found " << count << " platforms" << endl; | |
} | |
vector<cl_platform_id> ids(count); | |
clGetPlatformIDs(count, ids.data(), 0); | |
return ids; | |
} | |
string GetPlatformName(cl_platform_id id) { | |
size_t size = 0; | |
clGetPlatformInfo(id, CL_PLATFORM_NAME, 0, 0, &size); | |
string name; | |
name.resize(size); | |
clGetPlatformInfo(id, CL_PLATFORM_NAME, size, const_cast<char *> (name.data()), 0); | |
return name; | |
} | |
vector<cl_device_id> GetDeviceIds(cl_platform_id platform_id) { | |
cl_uint count = 0; | |
clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_ALL, 0, 0, &count); | |
vector<cl_device_id> ids(count); | |
clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_ALL, count, ids.data(), 0); | |
return ids; | |
} | |
string GetDeviceName(cl_device_id id) { | |
size_t size = 0; | |
clGetDeviceInfo(id, CL_DEVICE_NAME, 0, 0, &size); | |
string name; | |
name.resize(size); | |
clGetDeviceInfo(id, CL_DEVICE_NAME, size, const_cast<char *> (name.data()), 0); | |
return name; | |
} | |
cl_context GetContext(cl_platform_id platform_id, vector<cl_device_id> device_ids) { | |
const cl_context_properties contextProperties [] = { | |
CL_CONTEXT_PLATFORM, reinterpret_cast<cl_context_properties> (platform_id), 0, 0 | |
}; | |
cl_int err = CL_SUCCESS; | |
cl_context context = clCreateContext(contextProperties, device_ids.size(), device_ids.data(), 0, 0, &err); | |
CheckError(err, "in GetContext"); | |
return context; | |
} | |
cl_command_queue GetQueue(cl_context context, cl_device_id device_id) { | |
cl_int err = CL_SUCCESS; | |
cl_command_queue queue = clCreateCommandQueue(context, device_id, 0, &err); | |
CheckError(err); | |
return queue; | |
} | |
cl_program GetProgram(cl_context context, vector<cl_device_id> device_ids, const char* name) { | |
// get program | |
ifstream in(name); | |
string source((istreambuf_iterator<char> (in)), istreambuf_iterator<char> ()); | |
size_t lengths[1] = {source.size()}; | |
const char* sources[1] = {source.data()}; | |
cl_int err = CL_SUCCESS; | |
cl_program program = clCreateProgramWithSource(context, 1, sources, lengths, &err); | |
CheckError(err, "at GetProgram"); | |
// build program | |
err = clBuildProgram(program, device_ids.size(), device_ids.data(), "", 0, 0); | |
if (err == CL_BUILD_PROGRAM_FAILURE) { | |
size_t log_size; | |
clGetProgramBuildInfo(program, device_ids.data()[0], CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size); | |
string log; | |
log.resize(log_size); | |
clGetProgramBuildInfo(program, device_ids.data()[0], CL_PROGRAM_BUILD_LOG, log_size, const_cast<char *> (log.data()), NULL); | |
cout << "OpenCL Build Program Failure:" << endl; | |
cout << log << endl; | |
exit(CL_BUILD_PROGRAM_FAILURE); | |
} | |
return program; | |
} | |
cl_kernel GetKernel(cl_program program, const char* name) { | |
cl_int err = CL_SUCCESS; | |
cl_kernel kernel = clCreateKernel(program, name, &err); | |
CheckError(err, "in GetKernel"); | |
return kernel; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __HELPER_H_ | |
#define __HELPER_H_ | |
#include <fstream> | |
#include <iostream> | |
#include <vector> | |
#ifdef __APPLE__ | |
#include "OpenCL/opencl.h" | |
#else | |
#include "CL/cl.h" | |
#endif | |
using namespace std; | |
void CheckError(cl_int err); | |
void CheckError(cl_int err, const char *label); | |
vector<cl_platform_id> GetPlatformIds(void); | |
string GetPlatformName(cl_platform_id id); | |
vector<cl_device_id> GetDeviceIds(cl_platform_id platform_id); | |
string GetDeviceName(cl_device_id id); | |
cl_context GetContext(cl_platform_id platform_id, vector<cl_device_id> device_ids); | |
cl_command_queue GetQueue(cl_context context, cl_device_id device_id); | |
cl_program GetProgram(cl_context context, vector<cl_device_id> device_ids, const char* name); | |
cl_kernel GetKernel(cl_program program, const char* name); | |
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CFLAGS = -lstdc++ -lOpenCL | |
AMDOCLSDK=/scratch/c703/c703432/amd_ocl/AMD-APP-SDK-v2.6-RC3-lnx64 | |
INC=-I$(AMDOCLSDK)/include | |
LIB=-L$(AMDOCLSDK)/lib/x86_64 | |
assignment_3: assignment_3.cpp helper.cpp | |
gcc $(INC) $(LIB) $(CFLAGS) -o $@ $^ | |
clean: | |
rm -f assignment_3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment