Example #1
0
        public Event EnqueueCopyBuffer(
            Buffer source, ulong sourceOffset,
            Buffer destination, ulong destinationOffset, ulong count, Event[] events)
        {
            ClHelper.ThrowNullException(Handle);

            if (source == Buffer.Null)
            {
                throw new ArgumentNullException("source");
            }
            if (destination == Buffer.Null)
            {
                throw new ArgumentNullException("destination");
            }

            unsafe
            {
                int     num_events_in_wait_list = events == null ? 0 : events.Length;
                IntPtr *wait_list = stackalloc IntPtr[num_events_in_wait_list];
                for (int i = 0; i < num_events_in_wait_list; ++i)
                {
                    wait_list[i] = events[i].Handle;
                }
                if (events == null)
                {
                    wait_list = null;
                }

                IntPtr event_ptr = IntPtr.Zero;

                ClHelper.GetError(Cl.EnqueueCopyBuffer(Handle,
                                                       source.Handle, destination.Handle,
                                                       new UIntPtr(sourceOffset), new UIntPtr(sourceOffset), new UIntPtr(count),
                                                       (uint)num_events_in_wait_list, wait_list, &event_ptr));

                return(new Event(event_ptr));
            }
        }
Example #2
0
        public void FeedData(DataSet dataSet, int[] iExamples)
        {
#if TIMING_LAYERS
            Utils.InputFeedTimer.Start();
#endif
            int dataPointSize = dataSet.DataDimension;

            for (int m = 0; m < outputNeurons.MiniBatchSize; m++)
            {
#if OPENCL_ENABLED
                int iDataPoint = iExamples[m];

                OpenCLSpace.ClError = Cl.EnqueueCopyBuffer(OpenCLSpace.Queue,
                                                           dataSet.DataContainer[iDataPoint].Data,      // source
                                                           outputNeurons.ActivationsGPU,                // destination
                                                           (IntPtr)0,                                   // source offset (in bytes)
                                                           (IntPtr)(sizeof(float) * m * dataPointSize), // destination offset (in bytes)
                                                           (IntPtr)(sizeof(float) * dataPointSize),     // size of buffer to copy
                                                           0,
                                                           null,
                                                           out OpenCLSpace.ClEvent);
                OpenCLSpace.CheckErr(OpenCLSpace.ClError, "InputLayer.FeedData Cl.EnqueueCopyBuffer inputData");

                OpenCLSpace.ClError = Cl.ReleaseEvent(OpenCLSpace.ClEvent);
                OpenCLSpace.CheckErr(OpenCLSpace.ClError, "Cl.ReleaseEvent");

                // Dropout!

                if (dropoutParameter < 1.0)
                {
                    // Set kernel arguments
                    OpenCLSpace.ClError  = Cl.SetKernelArg(OpenCLSpace.InputDropout, 0, outputNeurons.ActivationsGPU);
                    OpenCLSpace.ClError |= Cl.SetKernelArg(OpenCLSpace.InputDropout, 1, (IntPtr)sizeof(int), nOutputUnits * outputNeurons.MiniBatchSize);
                    OpenCLSpace.ClError |= Cl.SetKernelArg(OpenCLSpace.InputDropout, 2, (IntPtr)sizeof(float), (float)dropoutParameter);
                    OpenCLSpace.ClError |= Cl.SetKernelArg(OpenCLSpace.InputDropout, 3, (IntPtr)sizeof(ulong), (ulong)Guid.NewGuid().GetHashCode());
                    OpenCLSpace.CheckErr(OpenCLSpace.ClError, "InputDropout: Cl.SetKernelArg");

                    // Run kernel
                    OpenCLSpace.ClError = Cl.EnqueueNDRangeKernel(OpenCLSpace.Queue,
                                                                  OpenCLSpace.InputDropout,
                                                                  1,
                                                                  null,
                                                                  dropoutGlobalWorkSizePtr,
                                                                  dropoutLocalWorkSizePtr,
                                                                  0,
                                                                  null,
                                                                  out OpenCLSpace.ClEvent);
                    OpenCLSpace.CheckErr(OpenCLSpace.ClError, "InputDropout: Cl.EnqueueNDRangeKernel");

                    OpenCLSpace.ClError = Cl.ReleaseEvent(OpenCLSpace.ClEvent);
                    OpenCLSpace.CheckErr(OpenCLSpace.ClError, "Cl.ReleaseEvent");

                    OpenCLSpace.ClError = Cl.Finish(OpenCLSpace.Queue);
                    OpenCLSpace.CheckErr(OpenCLSpace.ClError, "Cl.Finish");
                }
#else
                outputNeurons.SetHost(m, dataSet.Data[iExamples[m]]);
#endif
            }

#if OPENCL_ENABLED
            OpenCLSpace.ClError = Cl.Finish(OpenCLSpace.Queue);
            OpenCLSpace.CheckErr(OpenCLSpace.ClError, "Cl.Finish");
#endif

#if TIMING_LAYERS
            Utils.InputFeedTimer.Stop();
#endif
        }