我想将一个字符串传递给我的 GPU 并从 GPU 取回以打印它。这是为了理解目的——我知道,这个想法听起来毫无意义。
我试过了:
开放式CL:
__kernel void same_in_same_out_char(__global uchar * out, __constant uchar * in){
for (unsigned int ui=0; ui<3; ui++) out[ui]=in[ui];
}
C++:
#define __CL_ENABLE_EXCEPTIONS
#include <fstream>
#include <iostream>
#include <iterator>
#include <CL/cl.hpp>
#include <CL/opencl.h>
using namespace std;
int main () {
vector<cl::Platform> platforms;
vector<cl::Device> devices;
vector<cl::Kernel> kernels;
try {
// create platform
cl::Platform::get(&platforms);
platforms[0].getDevices(CL_DEVICE_TYPE_GPU, &devices);
// create context
cl::Context context(devices);
// create command queue
cl::CommandQueue queue(context, devices[0]);
// load opencl source
ifstream cl_file("inout.cl");
string cl_string(istreambuf_iterator<char>(cl_file), (istreambuf_iterator<char>()));
cl::Program::Sources source(1, make_pair(cl_string.c_str(),
cl_string.length() + 1));
// create program
cl::Program program(context, source);
// compile opencl source
program.build(devices);
// load named kernel from opencl source
cl::Kernel kernel(program, "same_in_same_out_char");
// create a message to send to kernel
const char pwd[] = "MAX";
cout << "char pwd[] : " << pwd << endl;
cl_uchar * password = (cl_uchar*) &pwd;
int bufferA_size = 3; // array size is 3
int bufferC_size = 3; // array size is 3
cout << " -- OpenCL -- " << endl;
// allocate device buffer to hold message
cl::Buffer bufferA(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(cl_uchar) * bufferA_size, password);
cl::Buffer bufferC(context, CL_MEM_WRITE_ONLY, sizeof(cl_uchar) * bufferC_size);
// set message as kernel argument
kernel.setArg(0, bufferC);
kernel.setArg(1, bufferA);
// execute kernel
queue.enqueueTask(kernel);
// wait for completion
queue.finish();
// ----------------------
cl_uint out_global[bufferC_size];
queue.enqueueReadBuffer(bufferC, CL_TRUE, 0, bufferC_size*sizeof(cl_uchar), &out_global);
cout << "Output \t\t:" << *out_global << endl << "Output[1..n] \t:";
for (unsigned int i=0; i<bufferC_size; i ++) cout << out_global[i] << " " ;
cout << endl;
} catch (cl::Error e) {
cout << endl << e.what() << " : " << e.err() << endl;
}
return 0;
}
但我失败了。输出是
输出:5783885
输出[1..n]:5783885 0 26
但不是
最大或 77 65 88
(对于 MAX)。
尊敬的马库斯