我正在尝试学习 OpenCL 基础知识。
我认为这个代码在内核中:
out[ 1 & ((a+b)==(b+a)) ] = (char)1;
将产生与以下相同的结果:
out[ 1 ] = (char)1;
谁能告诉我为什么它会产生不同的结果?
这是我的内核的完整源代码:
#pragma OPENCL EXTENSION cl_khr_byte_addressable_store : enable
__kernel void hello(__global char * out)
{
size_t tid = get_global_id(0);
int a = tid & 0xff;
int b = (tid >> 8) & 0xff;
out[ 1 & ((a+b)==(b+a)) ] = (char)1;
}
如果我用“out[1]=(char)1;”替换最后一条语句 那么“out[0]”将不会被写入。但是上面的内核会将“1”写入out[0]。
编辑:这是我的 C++ 代码:
#include <utility>
#define __NO_STD_VECTOR // Use cl::vector instead of STL version
#include <CL/cl.hpp>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>
#include <iterator>
inline void checkErr(cl_int err, const char * name)
{
if (err != CL_SUCCESS) {
std::cerr << "ERROR: " << name
<< " (" << err << ")" << std::endl;
std::exit(EXIT_FAILURE);
}
}
int main()
{
cl_int err;
cl::vector< cl::Platform > platformList;
cl::Platform::get(&platformList);
checkErr(platformList.size()!=0 ? CL_SUCCESS : -1, "cl::Platform::get");
std::cerr << "Platform number is: " << platformList.size() << std::endl;
std::string platformVendor;
platformList[0].getInfo((cl_platform_info)CL_PLATFORM_VENDOR, &platformVendor);
std::cerr << "Platform is by: " << platformVendor << "\n";
cl_context_properties cprops[3] =
{CL_CONTEXT_PLATFORM, (cl_context_properties)(platformList[0])(), 0};
cl::Context context(
CL_DEVICE_TYPE_GPU,
cprops,
NULL,
NULL,
&err);
checkErr(err, "Conext::Context()");
unsigned char outH[2] = {0};
cl::Buffer outCL(
context,
CL_MEM_WRITE_ONLY | CL_MEM_USE_HOST_PTR,
sizeof(outH),
outH,
&err);
checkErr(err, "Buffer::Buffer()");
cl::vector<cl::Device> devices;
devices = context.getInfo<CL_CONTEXT_DEVICES>();
checkErr(
devices.size() > 0 ? CL_SUCCESS : -1, "devices.size() > 0");
std::ifstream file("condition1.cl");
checkErr(file.is_open() ? CL_SUCCESS:-1, "condition1.cl");
const std::string prog(std::istreambuf_iterator<char>(file), (std::istreambuf_iterator<char>()));
cl::Program::Sources source(1, std::make_pair(prog.c_str(), prog.length()+1));
cl::Program program(context, source);
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(65536),
cl::NDRange(1, 1),
NULL,
&event);
checkErr(err, "ComamndQueue::enqueueNDRangeKernel()");
event.wait();
err = queue.enqueueReadBuffer(
outCL,
CL_TRUE,
0,
sizeof(outH),
outH);
checkErr(err, "ComamndQueue::enqueueReadBuffer()");
for (int i = 0; i < sizeof(outH); i++)
std::cout << (int)outH[i] << " ";
std::string str;
std::getline(std::cin, str);
return EXIT_SUCCESS;
}
编辑2:
它至少不是由“未定义的行为”引起的。我也得到了这个内核代码的奇怪结果:
char result = 0;
for (int a = 0; a < 2; a++) {
for (int b = 0; b < 2; b++) {
if ((a+b) != (b+a))
result |= (1 << (a+2*b));
}
}
我希望“结果”会得到值 0。但它会得到值 6。如果我将 != 更改为 == 那么结果会得到值 9。所以在这段代码中,当“a”不等于"b" 则 (a+b) 不等于 (b+a)。
如果我更改代码并为“a”或“b”设置一个已知值,那么结果将是 0,如我所料。例如:
char result = 0;
int a = 1;
/*for (int a = 0; a < 2; a++)*/ {
for (int b = 0; b < 2; b++) {
if ((a+b) != (b+a))
result |= (1 << (a+2*b));
}
}