开发者

How to query string lengths with clGetPlatformInfo (and friends)?

开发者 https://www.devze.com 2023-04-06 05:18 出处:网络
Short version: Is there an official/correct way to query for the size of strings like CL_P开发者_高级运维LATFORM_VENDOR?

Short version: Is there an official/correct way to query for the size of strings like CL_P开发者_高级运维LATFORM_VENDOR?

Long version: Looking at OpenCL functions like clGetPlatformInfo I want to find out how much space to allocate for the result. The function has this signature:

cl_int clGetPlatformInfo(cl_platform_id platform,
    cl_platform_info param_name,
    size_t param_value_size,
    void *param_value,
    size_t *param_value_size_ret)

The docs say:

param_value_size

Specifies the size in bytes of memory pointed to by
param_value. This size in bytes must be greater
than or equal to size of return type specified in the
table below.

All of the return types are listed as char[]. I wanted to know how much space to reserve so I called it like this, where I pass 0 for param_value_size and NULL for param_value, hoping to get the correct size return in param_value_size_ret:

  size_t size = 0;
  l_success = clGetPlatformInfo(platform_id,
                                CL_PLATFORM_VENDOR, 0, NULL, &size);

  if( l_success != CL_SUCCESS)
  {
    printf("Failed getting vendor name size.\n");
    return -1;
  }
  printf("l_success = %d, size = %d\n", l_success, size);
  char* vendor = NULL;
  vendor = malloc(size);
  if( vendor )
  {
    l_success = clGetPlatformInfo(platform_id,
                                  CL_PLATFORM_VENDOR, size, vendor, &size);
    if( l_success != CL_SUCCESS )
    {
      printf("Failed getting vendor name.\n");
      return -1;
    }
    printf("Vendor name is '%s', length is %d\n", vendor, strlen(vendor));
  } else {
    printf("malloc failed.\n");
    return -1;
  }

It behaved as I had hoped, it return a size of 19 for the string, "NVIDIA Corporation" (size included null terminator) and strlen return 18. Is this the "right" way to query for parameter size or am I just getting lucky with my vendor's implementation? Has anyone seen this idiom fail on some vendor?

Edit: The bit that is tripping me up is this, "This size in bytes must be greater than or equal to size of return type", it seems like when I pass 0 and NULL the call should fail because that's not greater than or equal to the size of the returned value. I'm not sure why they say "return type".


Yes, it is the right way.

In the same doc you've mentioned:

If param_value is NULL, it is ignored.

And below:

Returns CL_SUCCESS if the function is executed successfully. Otherwise, it returns the following:

...

CL_INVALID_VALUE if param_name is not one of the supported values or if size in bytes specified by param_value_size is less than size of return type and param_value is not a NULL value.

So even if it is not stated explicitly, if param_value is NULL no error should be produced, so the code is supposed to work properly.

Here is a piece of code from Khronos OpenCL C++ bindings (specs). They too do it this way, and I think it counts as "official":

// Specialized GetInfoHelper for STRING_CLASS params
template <typename Func>
struct GetInfoHelper<Func, STRING_CLASS>
{
    static cl_int get(Func f, cl_uint name, STRING_CLASS* param)
    {
        ::size_t required;
        cl_int err = f(name, 0, NULL, &required);
        if (err != CL_SUCCESS) {
            return err;
        }

        char* value = (char*) alloca(required);
        err = f(name, required, value, NULL);
        if (err != CL_SUCCESS) {
            return err;
        }

        *param = value;
        return CL_SUCCESS;
    }
};


Note in the example that while asking for the value, the forth parameter has to be NULL. Otherwise some platforms answer a CL_INVALID_VALUE as return.

As the upfront query and malloc is somehow over the top, I use one large char buffer as workaround.

char vendor[10240];
l_success = clGetPlatformInfo( platform_id,
                     CL_PLATFORM_VENDOR, sizeof(vendor), vendor, NULL);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号