Beispiel #1
0
        public static Cl.Program ToGPUClProgram(this Program irprog, Cl.Device device, Cl.Context context)
        {
            byte[] code = irprog.ToGPUClBinary(device);

            Cl.ErrorCode error;

            Cl.ErrorCode[] binariesStatus = { Cl.ErrorCode.InvalidBinary };

            Cl.Program program = Cl.CreateProgramWithBinary(
                context,
                1,
                new[] { device },
                new IntPtr[] { (IntPtr)code.Length },
                new Cl.InfoBufferArray(new Cl.InfoBuffer(code)),
                binariesStatus,
                out error
                );
            clSafeCall(error);
            clSafeCall(binariesStatus[0]);

            return(program);
        }
Beispiel #2
0
        public Program(Context context, Device[] devices, byte[][] binaries)
            : this()
        {
            if (context == Context.Null)
            {
                throw new ArgumentNullException("context");
            }
            if (devices == null)
            {
                throw new ArgumentNullException("devices");
            }
            if (binaries == null)
            {
                throw new ArgumentNullException("binaries");
            }
            if (devices.Length == 0)
            {
                throw new ArgumentException("devices is empty.", "devices");
            }
            if (devices.Length != binaries.Length)
            {
                throw new ArgumentException("binaries must be the same length as devices.", "binaries");
            }

            unsafe
            {
                var device_list = stackalloc IntPtr[devices.Length];

                for (int i = 0; i < devices.Length; ++i)
                {
                    device_list[i] = devices[i].Handle;
                }

                var lengths_list  = stackalloc UIntPtr[binaries.Length];
                var binary_list   = stackalloc byte *[binaries.Length];
                var binary_status = stackalloc int[binaries.Length];

                for (int i = 0; i < binaries.Length; ++i)
                {
                    lengths_list[i] = new UIntPtr((uint)binaries[i].Length);

                    var handle = GCHandle.Alloc(binaries[i], GCHandleType.Pinned);
                    binary_list[i] = (byte *)GCHandle.ToIntPtr(handle).ToPointer();
                }

                int errcode = 0;
                Handle = Cl.CreateProgramWithBinary(context.Handle,
                                                    (uint)devices.Length, device_list, lengths_list, binary_list, binary_status, &errcode);

                for (int i = 0; i < binaries.Length; ++i)
                {
                    var handel = GCHandle.FromIntPtr(new IntPtr(binary_list[i]));
                    handel.Free();
                }

                List <int> errors = new List <int>(binaries.Length);

                for (int i = 0; i < binaries.Length; ++i)
                {
                    if (binary_status[i] != Cl.SUCCESS)
                    {
                        errors.Add(i);
                    }
                }

                if (errors.Count != 0)
                {
                    throw new OpenCLException(string.Format("{0} {1} failed.",
                                                            errors.Count == 1 ? "Binary" : "Binaries",
                                                            string.Join(", ", errors)));
                }

                ClHelper.GetError(errcode);
            }
        }