Ejemplo n.º 1
0
        /// <summary>
        /// Disposes of the resources that have been acquired by the command queue.
        /// </summary>
        /// <param name="disposing">Determines whether managed object or managed and unmanaged resources should be disposed of.</param>
        protected override void Dispose(bool disposing)
        {
            // Checks if the command queue has already been disposed of, if not, then the command queue is disposed of
            if (!IsDisposed)
            {
                CommandQueuesNativeApi.ReleaseCommandQueue(Handle);
            }

            // Makes sure that the base class can execute its dispose logic
            base.Dispose(disposing);
        }
Ejemplo n.º 2
0
        private void InitCL(String[] kernelSource, String[] kernelNames, string compileArguments)
        {
            lock (lockObj)
            {
                if (!hasClInitialized && clDevice != null)
                {
                    Result err;
                    var    devicesArray = new IntPtr[] { clDevice };
                    clContext = ContextsNativeApi.CreateContext(IntPtr.Zero, 1, devicesArray, IntPtr.Zero, IntPtr.Zero, out err);
                    if (err != Result.Success)
                    {
                        throw new OpenClException("Failed to create context!", err);
                    }

                    commandQueue = CommandQueuesNativeApi.CreateCommandQueue(clContext, clDevice, CommandQueueProperty.None, out err);
                    if (err != Result.Success)
                    {
                        throw new OpenClException("Failed to create command queue!", err);
                    }

                    IntPtr[] sourceList = kernelSource.Select(source => Marshal.StringToHGlobalAnsi(source)).ToArray();
                    clProgram = ProgramsNativeApi.CreateProgramWithSource(clContext, 1, sourceList, null, out err);
                    if (err != Result.Success)
                    {
                        throw new OpenClException("Failed to create program!", err);
                    }

                    err = ProgramsNativeApi.BuildProgram(clProgram, 1, new IntPtr[] { clDevice }, compileArguments, IntPtr.Zero, IntPtr.Zero);
                    if (err != Result.Success)
                    {
                        var infoBuffer = GetProgramBuildInformation <string>(clProgram, clDevice, ProgramBuildInformation.Log);
                        if (err != Result.Success)
                        {
                            throw new OpenClException("Failed to build program! " + (infoBuffer == null ? "?" : infoBuffer.ToString()), err);
                        }
                    }

                    foreach (var item in kernelNames)
                    {
                        kernels[item] = KernelsNativeApi.CreateKernel(clProgram, item, out err);
                        if (err != Result.Success)
                        {
                            throw new OpenClException("Failed to create kernel: " + item, err);
                        }
                    }

                    hasClInitialized = true;
                }
            }
        }
Ejemplo n.º 3
0
        //public void EnqueueAcquireGLObjects(MemoryObject[] objs)
        //{
        //    IntPtr waitEventPointer;
        //    Result result = EnqueuedCommandsNativeApi.EnqueueAcquireGLObjects(this.Handle, (uint)objs.Length, objs.Select(x=>x.Handle).ToArray(), 0, null, out waitEventPointer);

        //    // Checks if the kernel was enqueued successfully, if not, then an exception is thrown
        //    if (result != Result.Success)
        //        throw new OpenClException("The Gl Objects could not be acuired.", result);
        //}

        //public void EnqueueReleaseGLObjects(MemoryObject[] objs)
        //{
        //    IntPtr waitEventPointer;
        //    Result result = EnqueuedCommandsNativeApi.EnqueueReleaseGLObjects(this.Handle, (uint)objs.Length, objs.Select(x => x.Handle).ToArray(), 0, null, out waitEventPointer);

        //    // Checks if the kernel was enqueued successfully, if not, then an exception is thrown
        //    if (result != Result.Success)
        //        throw new OpenClException("The Gl Objects could not be acuired.", result);
        //}

        #endregion

        #region Public Static Methods

        /// <summary>
        /// Creates a new command queue for the specified context and device.
        /// </summary>
        /// <param name="context">The context for which the command queue is to be created.</param>
        /// <param name="device">The devices for which the command queue is to be created.</param>
        /// <exception cref="OpenClException">If the command queue could not be created, then an <see cref="OpenClException"/> exception is thrown.</exception>
        /// <returns>Returns the created command queue.</returns>
        public static CommandQueue CreateCommandQueue(Context context, Device device)
        {
            // Creates the new command queue for the specified context and device
            Result result;
            IntPtr commandQueuePointer = CommandQueuesNativeApi.CreateCommandQueue(context.Handle, device.Handle, 0, out result);

            // Checks if the command queue creation was successful, if not, then an exception is thrown
            if (result != Result.Success)
            {
                throw new OpenClException("The command queue could not be created.", result);
            }

            // Creates the new command queue object from the pointer and returns it
            return(new CommandQueue(commandQueuePointer));
        }
Ejemplo n.º 4
0
        public void CleanupCLResources()
        {
            if (hasClInitialized)
            {
                FlushWorkingCache();

                foreach (var item in kernels)
                {
                    KernelsNativeApi.ReleaseKernel(item.Value);
                }
                kernels.Clear();

                ProgramsNativeApi.ReleaseProgram(clProgram);
                CommandQueuesNativeApi.ReleaseCommandQueue(commandQueue);
                ContextsNativeApi.ReleaseContext(clContext);

                hasClInitialized = false;
            }
        }
Ejemplo n.º 5
0
 public void Flush()
 {
     CommandQueuesNativeApi.Flush(Handle);
 }
Ejemplo n.º 6
0
 internal void FlushCommandBuffer()
 {
     CommandQueuesNativeApi.Flush(commandQueue);
 }
Ejemplo n.º 7
0
        public void BlockUntilAllTasksDone()
        {
            var errCode = CommandQueuesNativeApi.Finish(commandQueue);

            ThrowOnError(errCode, String.Format("Failed clFinish() call"));
        }