Initial Commit
This commit is contained in:
commit
42b2451d22
4 changed files with 83 additions and 0 deletions
2
Makefile
Normal file
2
Makefile
Normal file
|
@ -0,0 +1,2 @@
|
|||
hello: main.cpp
|
||||
g++ -o hello -lOpenCL main.cpp
|
BIN
hello
Executable file
BIN
hello
Executable file
Binary file not shown.
6
lesson1_kernel.cl
Normal file
6
lesson1_kernel.cl
Normal file
|
@ -0,0 +1,6 @@
|
|||
#pragma OPENCL EXTENSION cl_khr_byte_addressable_store : enable
|
||||
__constant char hw[] = "Hello World\n";
|
||||
__kernel void hello(__global char * out) {
|
||||
size_t tid = get_global_id(0);
|
||||
out[tid] = hw[tid];
|
||||
}
|
75
main.cpp
Normal file
75
main.cpp
Normal file
|
@ -0,0 +1,75 @@
|
|||
#include <utility>
|
||||
#include <CL/cl.hpp>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <iterator>
|
||||
|
||||
const std::string hw("Hello World!");
|
||||
|
||||
inline void checkErr(cl_int err, const char* name) {
|
||||
if(err != CL_SUCCESS) {
|
||||
std::cerr << "ERROR: " << name << " (" << err << ")" << std::endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
cl_int err;
|
||||
|
||||
std::vector<cl::Platform> platformList;
|
||||
cl::Platform::get(&platformList);
|
||||
checkErr(platformList.size() != 0 ? CL_SUCCESS : -1, "cl::Platform::get");
|
||||
|
||||
std::cerr << "Number of platforms: " << platformList.size() << std::endl;
|
||||
|
||||
for(int i = 0; i < platformList.size(); i++) {
|
||||
std::string platformVendor;
|
||||
platformList[i].getInfo((cl_platform_info)CL_PLATFORM_VENDOR, &platformVendor);
|
||||
std::cerr << "Platform #" << i << " Vendor: " << platformVendor << std::endl;
|
||||
std::string platformName;
|
||||
platformList[i].getInfo((cl_platform_info)CL_PLATFORM_NAME, &platformName);
|
||||
std::cerr << "Platform #" << i << " Name: " << platformName << std::endl;
|
||||
}
|
||||
|
||||
cl_context_properties cprops[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties)(platformList[1])(), 0 };
|
||||
cl::Context context(CL_DEVICE_TYPE_GPU, cprops, NULL, NULL, &err);
|
||||
checkErr(err, "Context::Context()");
|
||||
|
||||
char* outH = new char [hw.length()+1];
|
||||
cl::Buffer outCL(context, CL_MEM_WRITE_ONLY | CL_MEM_USE_HOST_PTR, hw.length()+1, outH, &err);
|
||||
checkErr(err, "Buffer::Buffer()");
|
||||
|
||||
std::vector<cl::Device> devices;
|
||||
devices = context.getInfo<CL_CONTEXT_DEVICES>();
|
||||
checkErr(devices.size() > 0 ? CL_SUCCESS : -1, "devices.size() > 0");
|
||||
|
||||
std::ifstream file("lesson1_kernel.cl");
|
||||
checkErr(file.is_open() ? CL_SUCCESS : -1, "lesson1_kernel.cl");
|
||||
|
||||
std::string programSourceString(std::istreambuf_iterator<char>(file), (std::istreambuf_iterator<char>()));
|
||||
cl::Program::Sources programSource(1, std::make_pair(programSourceString.c_str(), programSourceString.length() + 1));
|
||||
cl::Program program(context, programSource);
|
||||
err = program.build(devices, "");
|
||||
checkErr(err, "Program::Build()");
|
||||
|
||||
cl::Kernel kernel(program, "hello", &err);
|
||||
checkErr(err, "Kernel::Kernel()");
|
||||
err = kernel.setArg(0, outCL);
|
||||
checkErr(err, "Kernel::setArg()");
|
||||
|
||||
cl::CommandQueue queue(context, devices[0], 0, &err);
|
||||
checkErr(err, "CommandQueue::CommandQueue()");
|
||||
cl::Event event;
|
||||
err = queue.enqueueNDRangeKernel(kernel, cl::NullRange, cl::NDRange(hw.length() + 1), cl::NDRange(1, 1), NULL, &event);
|
||||
checkErr(err, "CommandQUeue::enqueueNDRangeKernel()");
|
||||
|
||||
event.wait();
|
||||
err = queue.enqueueReadBuffer(outCL, CL_TRUE, 0, hw.length() + 1, outH);
|
||||
checkErr(err, "CommandQueue::enqueueReadBuffer");
|
||||
std::cout << outH;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue