This project is read-only.

BuildProgramFailure with AccessViolationException when reading logs

May 2, 2012 at 5:39 PM
Edited May 2, 2012 at 5:44 PM

I'm running the tests that come with in a WPF application and I'm getting the same error message when I try and build a program.

The CL error that I get is the "BuildProgramFailure" error and then when I try and run 

var log = Cl.GetProgramBuildInfo(program, _device, Cl.ProgramBuildInfo.Log, out error);

I get the following exceptions:

AccessViolationException - "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

This is the code that I'm running:

            Cl.ErrorCode error;

            Cl.Device _device = (from device in
                                      (from platform in Cl.GetPlatformIDs(out error)
                                       where Cl.GetPlatformInfo(platform, Cl.PlatformInfo.Name, out error).ToString() == "NVIDIA CUDA"
                                       select platform).First(), Cl.DeviceType.Gpu, out error)
                              select device).First();

            var _context = Cl.CreateContext(null, 1, new[] { _device }, null, IntPtr.Zero, out error);

            using (Cl.Program program = Cl.CreateProgramWithSource(_context, 1, new[] { CLProgramSource }, null, out error))
                var depthLength = new IntPtr(depthMap.Length);
                var depthTypeLength = new IntPtr(sizeof(short) * depthMap.Length);
                var retTypeLength = new IntPtr(sizeof(byte) * depthMap.Length * 4);

                error = Cl.BuildProgram(program, 1, new[] { _device }, string.Empty, null, IntPtr.Zero);

                var log = Cl.GetProgramBuildInfo(program, _device, Cl.ProgramBuildInfo.Log, out error);

                Cl.Kernel[] kernels = Cl.CreateKernelsInProgram(program, out error);
                Cl.Kernel kernel = kernels[0];

                Cl.Mem hDeviceMemA = Cl.CreateBuffer(_context, Cl.MemFlags.CopyHostPtr | Cl.MemFlags.ReadOnly, depthTypeLength, depthMap, out error);
                Cl.Mem hDeviceMemB = Cl.CreateBuffer(_context, Cl.MemFlags.CopyHostPtr | Cl.MemFlags.ReadOnly, retTypeLength, IntPtr.Zero, out error);

                Cl.CommandQueue cmdQueue = Cl.CreateCommandQueue(_context, _device, 0, out error);

                Cl.Event clevent;

                int intPtrSize = 0;
                intPtrSize = Marshal.SizeOf(typeof(IntPtr));
                error = Cl.SetKernelArg(kernel, 0, new IntPtr(intPtrSize), hDeviceMemA);
                error = Cl.SetKernelArg(kernel, 1, new IntPtr(intPtrSize), hDeviceMemB);

                error = Cl.EnqueueWriteBuffer(cmdQueue, hDeviceMemA, Cl.Bool.True, IntPtr.Zero, depthTypeLength, depthMap, 0, null, out clevent);
                error = Cl.EnqueueNDRangeKernel(cmdQueue, kernel, 1, null, new [] { depthLength }, null, 0, null, out clevent);

                error = Cl.EnqueueReadBuffer(cmdQueue, hDeviceMemB, Cl.Bool.True, IntPtr.Zero, retTypeLength, ret, 0, null, out clevent);


Any body experience the same thing or know where I can get more information about the issue?
May 5, 2012 at 7:36 PM

What version of the .NET runtime are you targeting? I know that one of my APIs had an issue with marshaling differences across 3.5 and 4.0