简体   繁体   中英

How do get to the bottom with a OpenCL kernel build error using Cloo with .NET?

I am currently using VS2013. I have created a very simple C# solution that aims to use the Cloo with a simple class:

    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

    }
}

And while I have no problems building the solution in Visual Studio I keep getting an " OpenCL error code detected: BuildProgramFailure ." when trying to build the program using " program.Build(null, null, null, IntPtr.Zero) ". The kernel code looks fine but I have been at this for a couple of nights now and can't seem to figure out what is going on. As you can probably tell I am very new to Cloo, I have gotten a few ideas from examples online but I am probably doing something wrong at a very simple level. Any suggestions would be great.

I've downloaded all the latest OpenCL drivers for my graphics cards.

Thanks

You should check the error return code from program.Build (if it returns one; I don't know your framework that is wrapping the OpenCL API).

To get more details about why the build failed, you need to call whatever your framework puts around clGetProgramBuildInfo with the cl_program_build_info parameter set to CL_PROGRAM_BUILD_LOG . That will tell you what the compile error is and what line it occurred on.

Just guessing as to the erros:

  1. I don't think __local int n is right, it should just be int n .

  2. I don't think __local char* data1, __local char* data2 is correct either; these should probably be __global char* data1, __global char* data2 (local memory is an advanced topic).

To get specific kernel errors you should try catch program.Build and call GetBuildLog. Something like this:

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*************************");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM