我的硬件是 Intel Core I7 6700、32 GB RAM、Nvidia GT1030。我的软件是Windows 10 64位,Visual Studio 2012 professional。我买了《OpenCL并行编程开发手册》这本书。不幸的是我在编译程序时遇到问题。程序执行在行结束(第一次调用 clGetPlatformInfo)
error = clGetPlatformInfo(id, param_name, 0, NULL, ¶mSize);
#define CL_TARGET_OPENCL_VERSION 120
#include "windows.h"
#include <stdio.h>
#include <stdlib.h>
#include "CL/cl.h"
#include "CL/cl_platform.h"
#include "malloc.h"
#include "resconsts.h"
#include <iostream>
#include <fstream>
using namespace std;
static ofstream fout ("log_main_cpp.txt");
typedef unsigned char Byte;
void displayPlatformInfo(cl_platform_id id, cl_platform_info param_name,
const char* paramNameAsStr)
{
cl_int error = 0;
size_t paramSize = 0;
error = clGetPlatformInfo(id, param_name, 0, NULL, ¶mSize);
char* moreInfo = (char*) malloc(paramSize);
error = clGetPlatformInfo(id, param_name, paramSize, moreInfo, NULL);
if (error != CL_SUCCESS)
fout << "Unable to find any OpenCL Platform information";
fout << "paramNameAsStr = " << paramNameAsStr << endl
<< "moreInfo = " << moreInfo << endl;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
cl_platform_id *platforms;
cl_uint numOfPlatforms;
cl_int error;
error = clGetPlatformIDs(0, NULL, &numOfPlatforms);
if (error < 0) {
fout << "Unable to find any OpenCL platforms" << endl;
exit(1);
}
// Allocate memory for the number of installed platforms.
// alloca(...) occupies some stack space but is auromatically freed on return.
platforms = (cl_platform_id*) alloca (sizeof(cl_platform_id)*numOfPlatforms);
fout << "Number of OpenCL platforms found: " << numOfPlatforms << endl;
// We invoke the API 'clPlatformInfo' twice for each parameter we're trying
// to extract and we use the return value to create temporary data structures
// (on the stack) to store the returned information on the second invocation.
for (cl_uint i = 0; i < numOfPlatforms; i++) {
displayPlatformInfo(platforms[i], CL_PLATFORM_PROFILE,
"CL_PLATFORM_PROFILE");
displayPlatformInfo(platforms[i], CL_PLATFORM_VERSION,
"CL_PLATFORM_VERSION");
displayPlatformInfo(platforms[i], CL_PLATFORM_NAME,
"CL_PLATFORM_NAME");
displayPlatformInfo(platforms[i], CL_PLATFORM_VENDOR,
"CL_PLATFORM_VENDOR");
displayPlatformInfo(platforms[i], CL_PLATFORM_EXTENSIONS,
"CL_PLATFORM_EXTENSIONS");
}
return 0;
}
在“Linker->Input->Additional Dependency”中添加“OpenCL.lib”后程序编译成功。
你永远不会填充
platforms
,所以使用它是未定义的行为:
error = clGetPlatformIDs(numOfPlatforms, platforms, nullptr);
在
clGetPlatformInfo
上调用 malloc
之前,您还需要检查 paramSize
的结果,如果 clGetPlatformInfo
失败,则可能没有设置 paramSize
。