Beispiel #1
0
        public Context(Device[] devices)
        {
            if (devices == null)
            {
                throw new ArgumentNullException("devices");
            }
            if (devices.Length == 0)
            {
                throw new ArgumentException("must contain at least one device", "devices");
            }
            if (devices.Any(device => device == null))
            {
                throw new ArgumentException("must not contain null values", "devices");
            }
            Platform = devices[0].Platform;
            if (devices.Any(device => device.Platform != Platform))
            {
                throw new ArgumentException("all devices must belong to the same platform");
            }
            IntPtr[] properties = new IntPtr[3];
            properties[0] = new IntPtr((Int32)ContextProperty.Platform);
            properties[1] = Platform.Handle;
            IntPtr[]   deviceHandles = devices.Select(device => device.Handle).ToArray();
            ReturnCode code;

            _callback = Callback;
            Handle    = NativeMethods.CreateContext(properties, deviceHandles.Length, deviceHandles, _callback, IntPtr.Zero, out code);
            OpenCLException.ThrowOnError(code);
            _devices = (Device[])devices.Clone();
        }
 public String GetValue()
 {
     if (_value == null)
     {
         IntPtr ptr = Marshal.AllocHGlobal(IntPtr.Size);
         try {
             ReturnCode code = _createCallback(_handle, _name, IntPtr.Zero, IntPtr.Zero, ptr);
             OpenCLException.ThrowOnError(code);
             IntPtr size = Marshal.ReadIntPtr(ptr);
             if (size.ToInt32() > 0)
             {
                 ptr = Marshal.AllocHGlobal(size);
                 _createCallback(_handle, _name, size, ptr, IntPtr.Zero);
                 _value = Marshal.PtrToStringAnsi(ptr, size.ToInt32() - 1);
             }
             else
             {
                 _value = "";
             }
         } finally {
             Marshal.FreeHGlobal(ptr);
         }
     }
     return(_value);
 }
Beispiel #3
0
        public void WaitForEvents(params EventObject[] events)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }
            if (events == null)
            {
                throw new ArgumentNullException("events");
            }
            if (events.Length == 0)
            {
                throw new ArgumentException("events");
            }
            IntPtr[] handles;
            Int32    handlesLength;

            if (!Helpers.TryGetEventHandles(events, out handles, out handlesLength))
            {
                throw new ArgumentException("events");
            }
            ReturnCode code = NativeMethods.WaitForEvents(handlesLength, handles);

            OpenCLException.ThrowOnError(code);
        }
Beispiel #4
0
        public void Flush()
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }
            ReturnCode code = NativeMethods.Flush(_handle);

            OpenCLException.ThrowOnError(code);
        }
Beispiel #5
0
        public void CompleteEnqueuedCommands()
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }
            ReturnCode code = NativeMethods.Finish(_handle);

            OpenCLException.ThrowOnError(code);
        }
Beispiel #6
0
        public static Platform[] GetPlatforms()
        {
            Int32      count;
            ReturnCode code = NativeMethods.GetPlatformIDs(0, null, out count);

            OpenCLException.ThrowOnError(code);
            IntPtr[] handles = new IntPtr[count];
            code = NativeMethods.GetPlatformIDs(count, handles, out count);
            OpenCLException.ThrowOnError(code);
            return(handles.Select(handle => new Platform(handle)).ToArray());
        }
Beispiel #7
0
        //public void ReadBuffer(Buffer source, Array destination) {
        //    if (_disposed) {
        //        throw new ObjectDisposedException(GetType().FullName);
        //    }
        //    if (source == null) {
        //        throw new ArgumentNullException("source");
        //    }
        //    if (destination == null) {
        //        throw new ArgumentNullException("destination");
        //    }
        //    GCHandle pinnedDestination = GCHandle.Alloc(destination, GCHandleType.Pinned);
        //    ReturnCode code = NativeMethods.ReadBuffer(
        //        _handle,
        //        source.Handle,
        //        OpenCLBoolean.True,
        //        IntPtr.Zero,
        //        new IntPtr(System.Buffer.ByteLength(destination)),
        //        pinnedDestination.AddrOfPinnedObject(),
        //        0,
        //        IntPtr.Zero,
        //        IntPtr.Zero
        //    );
        //    pinnedDestination.Free();
        //    OpenCLException.ThrowOnError(code);
        //}

        public EventObject StartReadBuffer(Buffer source, Array destination, Int64 destinationIndex, Int64 destinationCount, params EventObject[] predecessors)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (destination == null)
            {
                throw new ArgumentNullException("destination");
            }
            Int64 size = System.Buffer.ByteLength(destination);

            if (destinationIndex < 0 || destinationCount <= 0 || destinationIndex + destinationCount > size)
            {
                throw new ArgumentOutOfRangeException("destinationIndex/destinationCount");
            }
            Debug.Assert(destinationCount == (Int64)source.Size);
            IntPtr[] handles;
            Int32    handlesLength;

            if (!Helpers.TryGetEventHandles(predecessors, out handles, out handlesLength))
            {
                throw new ArgumentException("predecessors");
            }
            GCHandle    pinnedDestination = GCHandle.Alloc(destination, GCHandleType.Pinned);
            EventObject result            = null;

            try {
                IntPtr     newEvent;
                ReturnCode code = NativeMethods.BeginReadBuffer(
                    _handle,
                    source.Handle,
                    OpenCLBoolean.False,
                    IntPtr.Zero,
                    new IntPtr((Int64)destinationCount),
                    new IntPtr(pinnedDestination.AddrOfPinnedObject().ToInt64() + (Int64)destinationIndex),
                    handlesLength,
                    handles,
                    out newEvent
                    );
                OpenCLException.ThrowOnError(code);
                result = new EventObject(newEvent, pinnedDestination);
            } finally {
                if (result == null)
                {
                    pinnedDestination.Free();
                }
            }
            return(result);
        }
Beispiel #8
0
        public Buffer CreateBuffer(UInt64 size, BufferFlags flags)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }
            ReturnCode code;
            IntPtr     handle = NativeMethods.CreateBuffer(Handle, flags, new UIntPtr(size), IntPtr.Zero, out code);

            OpenCLException.ThrowOnError(code);
            return(new Buffer(handle, size));
        }
        public Kernel CreateKernel(String name)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }
            ReturnCode code;
            IntPtr     kernelHandle = NativeMethods.CreateKernel(Handle, name, out code);

            OpenCLException.ThrowOnError(code);
            return(new Kernel(kernelHandle));
        }
Beispiel #10
0
        public EventObject StartWriteBufferRect(Array source, Buffer destination, IntPtr hostOriginX, IntPtr hostOriginY, IntPtr hostOriginZ, IntPtr regionX, IntPtr regionY, IntPtr regionZ, IntPtr sizeX, IntPtr sizeXY, params EventObject[] predecessors)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (destination == null)
            {
                throw new ArgumentNullException("destination");
            }
            IntPtr[] handles       = null;
            Int32    handlesLength = 0;

            if (!Helpers.TryGetEventHandles(predecessors, out handles, out handlesLength))
            {
                throw new ArgumentException("predecessors");
            }
            GCHandle    pinnedSource = GCHandle.Alloc(source, GCHandleType.Pinned);
            EventObject result       = null;

            try {
                IntPtr     newEvent;
                ReturnCode code = NativeMethods.BeginWriteBufferRect(
                    _handle,
                    destination.Handle,
                    OpenCLBoolean.False,
                    new IntPtr[] { IntPtr.Zero, IntPtr.Zero, IntPtr.Zero },
                    new IntPtr[] { hostOriginX, hostOriginY, hostOriginZ },
                    new IntPtr[] { regionX, regionY, regionZ },
                    IntPtr.Zero,
                    IntPtr.Zero,
                    sizeX,
                    sizeXY,
                    pinnedSource.AddrOfPinnedObject(),
                    handlesLength,
                    handles,
                    out newEvent
                    );
                OpenCLException.ThrowOnError(code);
                result = new EventObject(newEvent, pinnedSource);
            } finally {
                if (result == null)
                {
                    pinnedSource.Free();
                }
            }
            return(result);
        }
Beispiel #11
0
        public CommandQueue(Context context, Device device, CommandQueueFlags flags)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (device == null)
            {
                throw new ArgumentNullException("device");
            }
            _context = context;
            _device  = device;
            ReturnCode code;

            _handle = NativeMethods.CreateCommandQueue(context.Handle, device.Handle, flags, out code);
            OpenCLException.ThrowOnError(code);
        }
Beispiel #12
0
 public TValue GetValue()
 {
     if (!_created)
     {
         Int32  size   = Marshal.SizeOf(typeof(TValue));
         IntPtr buffer = Marshal.AllocHGlobal(size);
         try {
             ReturnCode code = _createCallback(_handle, _name, new IntPtr(size), buffer, IntPtr.Zero);
             OpenCLException.ThrowOnError(code);
             _value   = (TValue)Marshal.PtrToStructure(buffer, typeof(TValue));
             _created = true;
         } finally {
             Marshal.FreeHGlobal(buffer);
         }
     }
     return(_value);
 }
Beispiel #13
0
        public Device[] GetDevices(DeviceType types)
        {
            Int32      count;
            ReturnCode code = NativeMethods.GetDeviceIDs(Handle, types, 0, null, out count);

            if (code == ReturnCode.DeviceNotFound)
            {
                return(new Device[0]);
            }
            else
            {
                OpenCLException.ThrowOnError(code);
                IntPtr[] handles = new IntPtr[count];
                code = NativeMethods.GetDeviceIDs(Handle, types, count, handles, out count);
                OpenCLException.ThrowOnError(code);
                return(handles.Select(handle => new Device(handle, this)).ToArray());
            }
        }
Beispiel #14
0
        public EventObject(IntPtr handle, GCHandle?pinnedArgumentHandle)
        {
            _handle = handle;
            _pinnedArgumentHandle = pinnedArgumentHandle;
            _lock                     = new Object();
            _callbacks                = new List <EventObjectCompletionCallback>();
            _queuedTimeCache          = new GetInfoStructCache <CommandProfilingInfo, UInt64>(NativeMethods.GetEventProfilingInfo, _handle, CommandProfilingInfo.Queued);
            _submittedTimeCache       = new GetInfoStructCache <CommandProfilingInfo, UInt64>(NativeMethods.GetEventProfilingInfo, _handle, CommandProfilingInfo.Submitted);
            _startedTimeCache         = new GetInfoStructCache <CommandProfilingInfo, UInt64>(NativeMethods.GetEventProfilingInfo, _handle, CommandProfilingInfo.Started);
            _finishedTimeCache        = new GetInfoStructCache <CommandProfilingInfo, UInt64>(NativeMethods.GetEventProfilingInfo, _handle, CommandProfilingInfo.Ended);
            _internalCallbackDelegate = Callback;
            ReturnCode code = NativeMethods.SetEventCallback(_handle, CommandExecutionStatus.Complete, _internalCallbackDelegate, IntPtr.Zero);

            OpenCLException.ThrowOnError(code);
            lock (rootCallbacks)
            {
                rootCallbacks.Add(this);
            }
        }
Beispiel #15
0
        public EventObject StartKernel(Kernel kernel, UInt64[] globalRange, UInt64[] localRange, params EventObject[] predecessors)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }
            if (kernel == null)
            {
                throw new ArgumentNullException("kernel");
            }
            if (globalRange == null)
            {
                throw new ArgumentNullException("globalRange");
            }
            Int32 dimension = globalRange.Length;

            if (localRange != null && localRange.Length != dimension)
            {
                throw new ArgumentException("globalRange and localRange must have the same dimensions");
            }
            IntPtr[] handles;
            Int32    handlesLength;

            if (!Helpers.TryGetEventHandles(predecessors, out handles, out handlesLength))
            {
                throw new ArgumentException("predecessors");
            }
            IntPtr     newEvent;
            ReturnCode code = NativeMethods.EnqueueNDRangeKernel(
                _handle,
                kernel.Handle,
                dimension,
                null,
                ToUIntPtrArray(globalRange),
                ToUIntPtrArray(localRange),
                handlesLength,
                handles,
                out newEvent
                );

            OpenCLException.ThrowOnError(code);
            return(new EventObject(newEvent, null));
        }
Beispiel #16
0
        public EventObject StartCopyBuffer(Buffer source, Buffer destination, params EventObject[] predecessors)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (destination == null)
            {
                throw new ArgumentNullException("destination");
            }
            if (source.Size != destination.Size)
            {
                throw new ArgumentException("sizes of arguments are not equal");
            }
            IntPtr[] handles       = null;
            Int32    handlesLength = 0;

            if (!Helpers.TryGetEventHandles(predecessors, out handles, out handlesLength))
            {
                throw new ArgumentException("predecessors");
            }
            IntPtr     newEvent;
            ReturnCode code = NativeMethods.CopyBuffer(
                _handle,
                source.Handle,
                destination.Handle,
                IntPtr.Zero,
                IntPtr.Zero,
                new IntPtr((Int64)source.Size),
                handlesLength,
                handles,
                out newEvent
                );

            OpenCLException.ThrowOnError(code);
            return(new EventObject(newEvent, null));
        }
Beispiel #17
0
        void SetArgument(Int32 index, Int64 size, IntPtr value)
        {
            ReturnCode code = NativeMethods.SetKernelArgument(Handle, index, new IntPtr(size), value);

            OpenCLException.ThrowOnError(code);
        }
Beispiel #18
0
 public CompiledProgram Compile(String source, String options)
 {
     if (_disposed)
     {
         throw new ObjectDisposedException(GetType().FullName);
     }
     if (source == null)
     {
         throw new ArgumentNullException("source");
     }
     lock (_lock) {
         ReturnCode code;
         IntPtr     handle = NativeMethods.CreateProgramWithSource(Handle, 1, new String[] { source }, new IntPtr[] { new IntPtr(source.Length) }, out code);
         OpenCLException.ThrowOnError(code);
         IntPtr[] devices = Array.ConvertAll(_devices, device => device.Handle);
         code = NativeMethods.BuildProgram(handle, devices.Length, devices, options, IntPtr.Zero, IntPtr.Zero);
         if (code == ReturnCode.BuildProgramFailure)
         {
             Console.WriteLine(source);
             String[] logs = Array.ConvertAll(_devices, device => {
                 //String log = null;
                 //IntPtr currentSize = new IntPtr(1);
                 //IntPtr actualSize;
                 //Boolean exit = false;
                 //do {
                 //    IntPtr characters = Marshal.AllocHGlobal(currentSize);
                 //    try {
                 //        System.Diagnostics.Debugger.Launch();
                 //        code = NativeMethods.GetProgramBuildInfoString(handle, device.Handle, ProgramBuildInfo.BuildLog, currentSize, characters, out actualSize);
                 //        OpenCLException.ThrowOnError(code);
                 //        if (currentSize == actualSize) {
                 //            log = Marshal.PtrToStringAnsi(characters, currentSize.ToInt32());
                 //            exit = true;
                 //        } else {
                 //            currentSize = actualSize;
                 //        }
                 //    } finally {
                 //        Marshal.FreeHGlobal(characters);
                 //    }
                 //} while (!exit);
                 IntPtr size      = IntPtr.Zero;
                 ReturnCode code2 = NativeMethods.GetProgramBuildInfoString(handle, device.Handle, ProgramBuildInfo.BuildLog, IntPtr.Zero, IntPtr.Zero, out size);
                 OpenCLException.ThrowOnError(code2);
                 IntPtr characters = Marshal.AllocHGlobal(size);
                 code2             = NativeMethods.GetProgramBuildInfoString(handle, device.Handle, ProgramBuildInfo.BuildLog, size, characters, out size);
                 OpenCLException.ThrowOnError(code2);
                 String log = Marshal.PtrToStringAnsi(characters, size.ToInt32());
                 Marshal.FreeHGlobal(characters);
                 Console.WriteLine(log);
                 return(log);
             });
             throw new BuildProgramFailureOpenCLException(logs);
         }
         else
         {
             try {
                 OpenCLException.ThrowOnError(code);
             } catch (OpenCLException) {
                 NativeMethods.ReleaseProgram(handle);
                 throw;
             }
             return(new CompiledProgram(handle));
         }
     }
 }