carga's picture

Crash with Cloo

I observe app crash when restoring program from binary. To be exact, CreateProgram works fine. But when I call CreateKernel -- it crashes with AccessViolationException.

Could you please help me to solve the issue? I have long source now, so compile-time is far out of comfortable zone... Maybe, sample?..

System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at Cloo.Bindings.CL10.CreateKernel(CLProgramHandle program, String kernel_name, ComputeErrorCode& errcode_ret)
   at Cloo.ComputeProgram.CreateKernel(String functionName) in f:\CS\Amdfy\Cloo\Source\ComputeProgram.cs:line 349

I'm playing with Cloo for some time and it's great! Thank you very much for sharing it.

Have a fast code!
Anton.
http://kyta.spb.ru


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

Please post your CreateProgram and CreateKernel calls.

carga's picture

This is full src to reproduce the issue. For simplicity it gets first platform, gets CPU device from it etc.

First run will compile kernel from src and then it will write resulting binary to file. It runs without any error.

Second (and subsequent runs) will load precompiled binary. CreateProgram executes in this case. But CreateKernel -- fails.

using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
 
using Cloo;
 
namespace TestClooBin
{
	class MainClass
	{
		private static string kernelSrc =
@"
kernel void VectorInit(global int* output)
{
	int gid = get_global_id(0);
    output[gid] = gid;
}
";
 
		public static void Main(string[] args)
		{
			ComputePlatform platform = ComputePlatform.Platforms.FirstOrDefault();
			ComputeContextPropertyList properties = new ComputeContextPropertyList(platform);
			ComputeDevice cd = (from t in platform.Devices where (t.Type == ComputeDeviceTypes.Cpu) select t).FirstOrDefault();
			ComputeContext context = new ComputeContext(new [] { cd }, properties, null, IntPtr.Zero);
 
			byte[] bin = null;
			ComputeProgram program = null;
            FileInfo aInfo = new FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location);
            string path = System.IO.Path.Combine(aInfo.DirectoryName, "kernel.clbin");
 
            if (System.IO.File.Exists(path))
            {
                var fInfo = new System.IO.FileInfo(path);
                bin = new byte[fInfo.Length];
                using (var fs = fInfo.OpenRead())
                using (var br = new System.IO.BinaryReader(fs))
                {
                    bin = br.ReadBytes(bin.Length);
                    br.Close();
                }
 
				List<byte[]> binaries = new List<byte[]>();
				binaries.Add(bin);
                program = new ComputeProgram(context, binaries, context.Devices);
 
                Console.WriteLine("[DEBUG] PlatformVendor: {0}; Device: {1} -- OK! LOADED FROM BINARY FILE!",
                    context.Platform.Vendor, context.Devices[0].Name);
            }
            else
            {
				program = new ComputeProgram(context, kernelSrc);
				try
				{
                	program.Build(context.Devices, null, null, IntPtr.Zero);
				}
				catch(Exception ex)
				{
					Console.WriteLine();
					Console.WriteLine(program.GetBuildLog(cd));
					Console.WriteLine();
 
					throw;
				}
 
				Console.WriteLine("[DEBUG] PlatformVendor: {0}; Device: {1} -- OK! COMPILED FROM SOURCE!",
                    context.Platform.Vendor, context.Devices[0].Name);
 
                if (program.Binaries.Count != 1)
                {
					string msg = "For some reason (program.Binaries.Count!=1). program.Binaries.Count: " + program.Binaries.Count;
                    Console.WriteLine(msg);
					throw new InvalidOperationException(msg);
                }
 
                bin = program.Binaries[0];
 
                if (System.IO.File.Exists(path))
                    System.IO.File.Delete(path);
                using (System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))
                using (System.IO.BinaryWriter bw = new System.IO.BinaryWriter(fs))
                {
                    bw.Write(bin, 0, bin.Length);
                    bw.Flush();
                    bw.Close();
                }
 
                Console.WriteLine("[DEBUG] Compiled binary (size {0:F3} kB) was saved to '{1}'.",
              		bin.Length / 1024.0, path);
            }
 
			const int size = 64;
			ComputeKernel kernel = program.CreateKernel("VectorInit");
			int[] output = new int[size];
			using(ComputeBuffer<int> buf = new ComputeBuffer<int>(context, ComputeMemoryFlags.CopyHostPointer, output))
			{
				kernel.SetMemoryArgument(0, buf);
				using(ComputeCommandQueue commands = new ComputeCommandQueue(context, cd, ComputeCommandQueueFlags.None))
				{
					commands.Execute(kernel, null, new long[]{ (long)size }, new long[]{ (long)size }, null);
					commands.Finish();
					commands.ReadFromBuffer(buf, ref output, true, null);
				}
			}
 
			for(int j=0; j<8; j++)
			{
				for(int k=0; k<8; k++)
				{
					Console.Write(output[j*8 + k] + ", ");
				}
				Console.WriteLine();
			}
		}
	}
}

I compiled it for Framework 4.5 in Linux and in Ubuntu. No luck.

Any idea?

PS Cloo v0.9.1

Have a fast code!
Anton.
http://kyta.spb.ru