首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在OpenCL内核构建错误的情况下使用Cloo和.NET进行底层构建呢?

如何在OpenCL内核构建错误的情况下使用Cloo和.NET进行底层构建呢?
EN

Stack Overflow用户
提问于 2017-07-20 20:13:19
回答 2查看 486关注 0票数 1

我目前正在使用VS2013。我已经创建了一个非常简单的C#解决方案,它的目标是将Cloo与一个简单的类一起使用:

代码语言:javascript
复制
    using System;
using System.Collections.Concurrent;
using System.Threading.Tasks;
using System.IO;
using Cloo;

namespace ClooTest
{
    class Test
    {
        public static void main_entry(byte[] args)
        {
            ComputePlatform platform = ComputePlatform.Platforms[0];

            ComputeContext context = new ComputeContext(ComputeDeviceTypes.Gpu,
            new ComputeContextPropertyList(platform), null, IntPtr.Zero);

            ComputeCommandQueue queue = new ComputeCommandQueue(context,
            context.Devices[0], ComputeCommandQueueFlags.None);

            // get and build the source code
            ComputeProgram program = new ComputeProgram(context, new string[] { kernelCode });
            program.Build(null, null, null, IntPtr.Zero);

            // pick the fucntion
            ComputeKernel kernel = program.CreateKernel("add_data");

            // create a ten integer array and its length
            byte[] data1 = new byte[] { 1, 2, 3, 4, 5 };
            byte[] data2 = new byte[] { 1, 2, 3, 4, 5 };
            int size = data1.Length;

            /* allocate memory */
            ComputeBuffer<byte> data1buffer = new ComputeBuffer<byte>(context,
            ComputeMemoryFlags.ReadWrite, data1);

            ComputeBuffer<byte> data2buffer = new ComputeBuffer<byte>(context,
            ComputeMemoryFlags.ReadWrite, data2);

            kernel.SetMemoryArgument(0, data1buffer); // set the byte array
            kernel.SetMemoryArgument(1, data2buffer); // set the byte array
            kernel.SetValueArgument(2, size); // set third argument as size;

            // execute
            queue.ExecuteTask(kernel, null);

            // wait for completion
            queue.Finish();

            // output results
            for (int i = 0; i < size; i++)
                Console.WriteLine(data1[i].ToString());
        }
        #region Kernels
        private static string kernelCode = @"__kernel void add_data(__local char* data1, __local char* data2, __local int n)
                        {
                            data1[0]=data1[0]+data2[0];
                        }";

        #endregion

    }
}

虽然在Visual中构建解决方案没有问题,但我仍然得到一个“检测到的OpenCL错误代码:BuildProgramFailure”。当尝试使用"program.Build(null,IntPtr.Zero)“构建程序时。内核代码看起来很好,但我已经做了几个晚上了,似乎无法弄清楚到底是怎么回事。正如你可能会说我对Cloo非常陌生,我从网上的例子中得到了一些想法,但是我可能在一个非常简单的层次上做了一些错误的事情。任何建议都会很好。

我下载了我的显卡的所有最新的OpenCL驱动程序。

谢谢

EN

回答 2

Stack Overflow用户

发布于 2017-07-20 22:30:35

您应该检查来自program.Build的错误返回代码(如果它返回一个;我不知道您的框架正在包装OpenCL API)。

要了解构建失败的更多细节,需要调用框架中设置为clGetProgramBuildInfocl_program_build_info参数为CL_PROGRAM_BUILD_LOG的任何内容。这将告诉您编译错误是什么以及它发生在哪一行。

只是猜错了:

  1. 我不认为__local int n是对的,应该是int n
  2. 我也不认为__local char* data1, __local char* data2是正确的;这些可能应该是__global char* data1, __global char* data2 (本地内存是一个高级主题)。
票数 1
EN

Stack Overflow用户

发布于 2019-11-05 15:07:07

要获得特定的内核错误,您应该尝试捕捉program.Build并调用GetBuildLog。就像这样:

代码语言:javascript
复制
try{
   program.Build(null, null, null, IntPtr.Zero);
}
catch (Exception ex)
{
   String buildLog = program.GetBuildLog(context.Devices[0]);
   Console.WriteLine("\n********** Build Log **********\n" + buildLog + "\n*************************");
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45224377

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档