0

在我的 OpenCL 代码的主机部分中,我有一堆

  ret = clEnqueueWriteBuffer(command_queue, ...
  ret = clEnqueueWriteBuffer(command_queue, ...
  ret = clEnqueueWriteBuffer(command_queue, ...
  ...

而不是每次都做

  if (ret != CL_SUCCESS)
    {
        printf("Error: Failed to write to source array!\n");
        exit(1);
    }

我想知道是否有办法在某种程度上等待一个“ret”无法成功退出。

我知道(只是知道,从未使用过)signal()andraise()函数,但我不确定我是否可以在这里使用它们。

4

3 回答 3

2

最好的方法是保持简单,在每次调用后使用自定义错误消息粘贴“if”语句。听起来很多,但看看“try/catch”块需要的代码量。

“信号”用于处理硬件警报和进程间通信。使用它们来伪造异常将带您进入一个痛苦和痛苦的世界。

鉴于“try/catch”只是一个严重伪装的“goto”,您可以尝试:

  ret = clEnqueueWriteBuffer(command_queue, ...
  if (ret) goto catcher;
  ret = clEnqueueWriteBuffer(command_queue, ... 
  if (ret) goto catcher;
  ret = clEnqueueWriteBuffer(command_queue, ...
  if (ret) goto catcher;
  goto finalizer;
catcher:
  printf("Error: Failed to write to source array!\n");
        exit(1);
    }
finalizer:
   ..........
于 2013-09-05T01:36:03.790 回答
2

有什么问题吗:

if ((ret = clEnqueueWriteBuffer(command_queue, ...)) != CL_SUCCESS ||
    (ret = clEnqueueWriteBuffer(command_queue, ...)) != CL_SUCCESS ||
    (ret = clEnqueueWriteBuffer(command_queue, ...)) != CL_SUCCESS)
{
    printf("Error: Failed to write to source array!\n");
    exit(1);
}
else
    ...do whatever it is that depends on everything else having succeeded...

而且,如果错误报告代码不使用 中的值ret,那么您可以不进行赋值并将其简化为:

if (clEnqueueWriteBuffer(command_queue, ...) != CL_SUCCESS ||
    clEnqueueWriteBuffer(command_queue, ...) != CL_SUCCESS ||
    clEnqueueWriteBuffer(command_queue, ...) != CL_SUCCESS)
{
    printf("Error: Failed to write to source array!\n");
    exit(1);
}
else
    ...do whatever it is that depends on everything else having succeeded...
于 2013-09-05T03:15:50.833 回答
0

在 C 中处理异常的最佳方法是利用 perror 调用来获取最后执行的操作的结果/状态。

这是一个参考: http ://en.cppreference.com/w/c/io/perror

于 2013-09-05T02:39:38.530 回答