clGetPlatformInfo分段错误

时间:2017-04-22 13:57:59

标签: c++ opencl

我有一个使用openCL的简单main.cpp文件。 (见最后) 我在Windows 10 Linux子系统(Ubuntu 14)下使用bash来让我更容易(我想)所以我不必在windows上使用mingw或其他东西。

无论如何,我使用以下代码编译了这个简单的main.cpp文件:

g++ -o main main.cpp -I "/mnt/c/Program Files (x86)/AMD APP SDK/3.0/include" -L "/mnt/c/Program Files (x86)/AMD APP SDK/3.0/lib/x86_64" -lOpenCL

我尝试运行程序时出现分段错误。它出现在第33行(error = clGetPlatformIDs)

的main.cpp

#include <stdio.h>
#include <stdlib.h>

#ifdef APPLE
#include <OpenCL/cl.h>
#else
#include <CL/cl.h>
#endif

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, &paramSize);

  char* moreInfo = (char*)alloca(sizeof(char)*paramSize);
  error = clGetPlatformInfo( id, param_name, paramSize, moreInfo, NULL);

  if (error != CL_SUCCESS) {
    perror("Unable to find any OpenCL Plaform Information");
    return;
  }
  printf("%s: %s\n", paramNameAsStr, moreInfo);
}

int main() {

  cl_platform_id* platforms;
  cl_uint numOfPlatforms;
  cl_int error;

  error = clGetPlatformIDs(0, NULL, &numOfPlatforms);
  if (error <0 ) {
    perror("Unable to find any openCL Platforms");
    exit(1);
  }
  printf("Number of OpenCL platform found: %d\n",numOfPlatforms);
  platforms = (cl_platform_id*) alloca(sizeof(cl_platform_id)* numOfPlatforms);

  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;
}

1 个答案:

答案 0 :(得分:0)

为平台输入值。

该值大于找到的平台数。

示例:

numOfPlatforms = 10;