Ejemplo n.º 1
0
        public void Initialize(int index)
        {
            // Query Devices, create a Context+Command Queue and compile a program
            context = devices[index].Platform.CreateDefaultContext();
            queue   = context.CreateCommandQueue(devices[index], CommandQueueProperties.PROFILING_ENABLE);
            Console.WriteLine(devices[index].Vendor + " " + devices[index].Name);
            // Load and build source+create a kernel
            OpenCLNet.Program programForTimeSeries = context.CreateProgramWithSource(processTimeSeriesSource);
            try
            {
                programForTimeSeries.Build();
            }
            catch
            {
                Console.WriteLine(programForTimeSeries.GetBuildLog(devices[index]));
            }
            kernelForTimeSeries = programForTimeSeries.CreateKernel("KernelFunction");

            OpenCLNet.Program programForDataSets = context.CreateProgramWithSource(processDataSetSource);
            try
            {
                programForDataSets.Build();
            }
            catch
            {
                Console.WriteLine(programForDataSets.GetBuildLog(devices[index]));
            }
            kernelForDataSets = programForDataSets.CreateKernel("KernelFunction");
            selectedDevice    = index;
            workGroupSize     = (int)devices[index].MaxWorkGroupSize;
        }
Ejemplo n.º 2
0
        public void ReleaseDeviceResources()
        {
            oclFullyInitialized = false;
            if (OCLSampler != null)
            {
                OCLSampler.Dispose();
                OCLSampler = null;
            }
            if (OCLInputImage != null)
            {
                OCLInputImage.Dispose();
                OCLInputImage = null;
            }

            if (OCLOutputImage != null)
            {
                OCLOutputImage.Dispose();
                OCLOutputImage = null;
            }

            if (FilterKernel != null)
            {
                FilterKernel.Dispose();
                FilterKernel = null;
            }

            if (oclProgram != null)
            {
                oclProgram.Dispose();
                oclProgram = null;
            }

            if (oclCQ != null)
            {
                oclCQ.Dispose();
                oclCQ = null;
            }

            if (oclContext != null)
            {
                oclContext.Dispose();
                oclContext = null;
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Create a OpenCLManager, configure it, and then create a context using all devices in platform 0,
        /// Once the context is up and running we compile our source file "OpenCLFunctions.cl"
        /// The Helper automatically compiles and creates kernels.
        /// We can then extract named kernels using the GetKernel method.
        ///
        /// For more advanced scenarios, one might use the functions in the Platform class
        /// to query devices, create contexts etc. Platforms can be enumerated using
        /// for( int i=0; i<OpenCL.NumberofPlatforms; i++ )
        ///     Platform p = OpenCL.GetPlatform(i);
        /// </summary>
        private void InitializeOpenCL()
        {
            if (OpenCL.NumberOfPlatforms == 0)
            {
                MessageBox.Show("OpenCL not available");
                Application.Exit();
            }

            OCLMan = new OpenCLManager();
            // Attempt to save binaries after compilation, as well as load precompiled binaries
            // to avoid compilation. Usually you'll want this to be true.
            OCLMan.AttemptUseBinaries = true;
            // Attempt to compile sources. This should probably be true for almost all projects.
            // Setting it to false means that when you attempt to compile "mysource.cl", it will
            // only scan the precompiled binary directory for a binary corresponding to a source
            // with that name. There's a further restriction that the compiled binary also has to
            // use the same Defines and BuildOptions
            OCLMan.AttemptUseSource = true;
            // Binary and source paths
            // This is where we store our sources and where compiled binaries are placed
            OCLMan.BinaryPath = @"OpenCL\bin";
            OCLMan.SourcePath = @"OpenCL\src";
            // If true, RequireImageSupport will filter out any devices without image support
            // In this project we don't need image support though, so we set it to false
            OCLMan.RequireImageSupport = false;
            // The Defines string gets prepended to any and all sources that are compiled
            // and serve as a convenient way to pass configuration information to the compilation process
            OCLMan.Defines = "#define MyCompany_MyProject_Define 1";
            // The BuildOptions string is passed directly to clBuild and can be used to do debug builds etc
            OCLMan.BuildOptions = "";

            OCLMan.CreateDefaultContext(0, DeviceType.ALL);

            OCLProgram = OCLMan.CompileFile("OpenCLFunctions.cl");

            for (int i = 0; i < OCLMan.Context.Devices.Length; i++)
            {
                comboBoxDeviceSelector.Items.Add(OCLMan.Context.Devices[i].Vendor + ":" + OCLMan.Context.Devices[i].Name);
            }
            comboBoxDeviceSelector.SelectedIndex = 0;

            CrossFadeKernel = OCLProgram.CreateKernel("CrossFade");
        }
Ejemplo n.º 4
0
        private void InitializeOpenCL()
        {
            if (OpenCL.NumberOfPlatforms == 0)
            {
                MessageBox.Show("OpenCL не поддерживается вашей системой!");
                Application.Exit();
            }

            manager = new OpenCLManager();
            manager.AttemptUseBinaries  = true;
            manager.AttemptUseSource    = true;
            manager.RequireImageSupport = false;
            manager.BuildOptions        = "";

            manager.CreateDefaultContext(0, DeviceType.ALL);

            // Компиляция OpenCL кода
            program = manager.CompileSource(Properties.Resources.DVR);
            kernel  = program.CreateKernel("DVR");
        }
Ejemplo n.º 5
0
 public void BuildOCLSource(string source)
 {
     oclProgram = oclContext.CreateProgramWithSource(source);
     oclProgram.Build();
     FilterKernel = oclProgram.CreateKernel("FilterImage");
 }
Ejemplo n.º 6
0
        public void ReleaseDeviceResources()
        {
            oclFullyInitialized = false;
            if (OCLSampler != null)
            {
                OCLSampler.Dispose();
                OCLSampler = null;
            }
            if (OCLInputImage != null)
            {
                OCLInputImage.Dispose();
                OCLInputImage = null;
            }

            if (OCLOutputImage != null)
            {
                OCLOutputImage.Dispose();
                OCLOutputImage = null;
            }

            if (FilterKernel != null)
            {
                FilterKernel.Dispose();
                FilterKernel = null;
            }

            if (oclProgram != null)
            {
                oclProgram.Dispose();
                oclProgram = null;
            }

            if (oclCQ != null)
            {
                oclCQ.Dispose();
                oclCQ = null;
            }

            if (oclContext != null)
            {
                oclContext.Dispose();
                oclContext = null;
            }
        }
Ejemplo n.º 7
0
        private void InitializeOpenCL()
        {
            if (OpenCL.NumberOfPlatforms == 0)
            {
                MessageBox.Show("OpenCL не поддерживается вашей системой!");
                Application.Exit();
            }

            manager = new OpenCLManager();
            manager.AttemptUseBinaries = true;
            manager.AttemptUseSource = true;
            manager.RequireImageSupport = false;
            manager.BuildOptions = "";

            manager.CreateDefaultContext(0, DeviceType.ALL);

            // Компиляция OpenCL кода
            program = manager.CompileSource(Properties.Resources.DVR);
            kernel = program.CreateKernel("DVR");
        }
Ejemplo n.º 8
0
 private void InitializeOpenCL()
 {
     if (OpenCL.NumberOfPlatforms == 0){
         MessageBox.Show("OpenCL is not supported on your system!");
         Application.Exit();
     }
     manager = new OpenCLManager();
     manager.AttemptUseBinaries = true;
     manager.AttemptUseSource = true;
     manager.RequireImageSupport = false;
     manager.BuildOptions = "";
     manager.CreateDefaultContext(0, DeviceType.ALL);
     // Compiling OpenCL code
     program = manager.CompileSource(Properties.Resources.DVR);
     kernel = program.CreateKernel("DVR");
 }
Ejemplo n.º 9
0
 public void BuildOCLSource(string source)
 {
     oclProgram = oclContext.CreateProgramWithSource(source);
     oclProgram.Build();
     FilterKernel = oclProgram.CreateKernel("FilterImage");
 }
Ejemplo n.º 10
0
        /// <summary>
        /// Create a OpenCLManager, configure it, and then create a context using all devices in platform 0,
        /// Once the context is up and running we compile our source file "OpenCLFunctions.cl"
        /// The Helper automatically compiles and creates kernels.
        /// We can then extract named kernels using the GetKernel method.
        /// 
        /// For more advanced scenarios, one might use the functions in the Platform class
        /// to query devices, create contexts etc. Platforms can be enumerated using
        /// for( int i=0; i<OpenCL.NumberofPlatforms; i++ )
        ///     Platform p = OpenCL.GetPlatform(i);
        /// </summary>
        private void InitializeOpenCL()
        {
            if (OpenCL.NumberOfPlatforms == 0)
            {
                MessageBox.Show("OpenCL not available");
                Application.Exit();
            }

            OCLMan = new OpenCLManager();
            // Attempt to save binaries after compilation, as well as load precompiled binaries
            // to avoid compilation. Usually you'll want this to be true.
            OCLMan.AttemptUseBinaries = true;
            // Attempt to compile sources. This should probably be true for almost all projects.
            // Setting it to false means that when you attempt to compile "mysource.cl", it will
            // only scan the precompiled binary directory for a binary corresponding to a source
            // with that name. There's a further restriction that the compiled binary also has to
            // use the same Defines and BuildOptions
            OCLMan.AttemptUseSource = true;
            // Binary and source paths
            // This is where we store our sources and where compiled binaries are placed
            OCLMan.BinaryPath = @"OpenCL\bin";
            OCLMan.SourcePath = @"OpenCL\src";
            // If true, RequireImageSupport will filter out any devices without image support
            // In this project we don't need image support though, so we set it to false
            OCLMan.RequireImageSupport = false;
            // The Defines string gets prepended to any and all sources that are compiled
            // and serve as a convenient way to pass configuration information to the compilation process
            OCLMan.Defines = "#define MyCompany_MyProject_Define 1";
            // The BuildOptions string is passed directly to clBuild and can be used to do debug builds etc
            OCLMan.BuildOptions = "";

            OCLMan.CreateDefaultContext(0, DeviceType.ALL);

            OCLProgram = OCLMan.CompileFile("OpenCLFunctions.cl");

            for (int i = 0; i < OCLMan.Context.Devices.Length; i++)
                comboBoxDeviceSelector.Items.Add(OCLMan.Context.Devices[i].Vendor+":"+OCLMan.Context.Devices[i].Name);
            comboBoxDeviceSelector.SelectedIndex = 0;

            CrossFadeKernel = OCLProgram.CreateKernel("CrossFade");
        }
Ejemplo n.º 11
0
        static void Main(string[] args)
        {
            for (int i = 0; i < OpenCL.NumberOfPlatforms; ++i)
            {
                Platform clPlatformTemp = OpenCL.GetPlatform(i);
                Console.WriteLine("Platform {0} -> {1}, {2}",
                                  i, clPlatformTemp.Vendor, clPlatformTemp.Name);

                Device[] clDevicesTemp = clPlatformTemp.QueryDevices(DeviceType.ALL);
                foreach (Device clDeviceTemp in clDevicesTemp)
                {
                    Console.WriteLine("-----> {0}, {1}, {2} Bytes, {3}",
                                      clDeviceTemp.Name, clDeviceTemp.DeviceType,
                                      clDeviceTemp.GlobalMemSize, clDeviceTemp.OpenCL_C_Version);
                }
            }


            Platform clPlatform = null;
            Device   clDevice   = null;

            for (int i = 0; i < OpenCL.NumberOfPlatforms; ++i)
            {
                clPlatform = OpenCL.GetPlatform(i);
                if (!clPlatform.Name.Contains("AMD") && !clPlatform.Name.Contains("NVIDIA") && !clPlatform.Name.Contains("INTEL"))
                {
                    continue;
                }


                Device[] clDevices = clPlatform.QueryDevices(DeviceType.GPU);

                clDevice = clDevices[0];

                break;
            }

            Console.WriteLine();

            Context clContext = null;

            clContext = clPlatform.CreateDefaultContext();

            CommandQueue clCommandQueue = null;

            clCommandQueue = clContext.CreateCommandQueue(clDevice, CommandQueueProperties.NONE);


            int[] ArrayA = { 1, 2 };
            int[] ArrayB = { 2, 3 };
            int[] ArrayC = new int[2];

            int N = 100;

            float[] array = new float[N];


            for (int i = 0; i < N; ++i)
            {
                array[i] = 1.0f * i / N;
            }


            GCHandle arrayHandle;

            arrayHandle = GCHandle.Alloc(array, GCHandleType.Pinned);

            GCHandle ArrayAHandle;

            ArrayAHandle = GCHandle.Alloc(array, GCHandleType.Pinned);
            GCHandle ArrayBHandle;

            ArrayBHandle = GCHandle.Alloc(array, GCHandleType.Pinned);
            GCHandle ArrayCHandle;

            ArrayCHandle = GCHandle.Alloc(array, GCHandleType.Pinned);



            Mem arrayBuffer = null;

            arrayBuffer = clContext.CreateBuffer(MemFlags.COPY_HOST_PTR,
                                                 array.Length * sizeof(float), arrayHandle.AddrOfPinnedObject());


            Mem ArrayABuffer = null;

            ArrayABuffer = clContext.CreateBuffer(MemFlags.COPY_HOST_PTR,
                                                  ArrayA.Length * sizeof(int), ArrayAHandle.AddrOfPinnedObject());

            Mem ArrayBBuffer = null;

            ArrayBBuffer = clContext.CreateBuffer(MemFlags.COPY_HOST_PTR,
                                                  ArrayB.Length * sizeof(int), ArrayBHandle.AddrOfPinnedObject());

            Mem ArrayCBuffer = null;

            ArrayCBuffer = clContext.CreateBuffer(MemFlags.COPY_HOST_PTR,
                                                  ArrayC.Length * sizeof(int), ArrayCHandle.AddrOfPinnedObject());

            //cos


            clCommandQueue.EnqueueReadBuffer(arrayBuffer, true, 0,
                                             array.Length * sizeof(float), arrayHandle.AddrOfPinnedObject());

            OpenCLNet.Program clProgram = null;
            clProgram = clContext.CreateProgramWithSource(File.ReadAllText("openCLkernels.cl"));
            clProgram.Build();

            Kernel clKernel = null;

            clKernel = clProgram.CreateKernel("Multiply");
            clKernel.SetArg(0, arrayBuffer);
            clKernel.SetArg(1, 25);

            Kernel clKernel2 = null;

            clKernel2 = clProgram.CreateKernel("Add");
            clKernel2.SetArg(0, ArrayABuffer);
            clKernel2.SetArg(1, ArrayBBuffer);
            clKernel2.SetArg(2, ArrayCBuffer);


            clCommandQueue.EnqueueNDRangeKernel(clKernel, 1, null, new int[] { N }, null);

            clCommandQueue.EnqueueReadBuffer(arrayBuffer, true, 0,
                                             array.Length * sizeof(float), arrayHandle.AddrOfPinnedObject());


            clCommandQueue.EnqueueNDRangeKernel(clKernel2, 1, null, new int[] { 2 }, null);
            clCommandQueue.EnqueueReadBuffer(ArrayABuffer, true, 0,
                                             ArrayA.Length * sizeof(int), ArrayAHandle.AddrOfPinnedObject());
            clCommandQueue.EnqueueReadBuffer(ArrayBBuffer, true, 0,
                                             ArrayB.Length * sizeof(int), ArrayBHandle.AddrOfPinnedObject());
            clCommandQueue.EnqueueReadBuffer(ArrayCBuffer, true, 0,
                                             ArrayC.Length * sizeof(int), ArrayCHandle.AddrOfPinnedObject());


            //    for (int i = 0; i < N; ++i)
            //      Console.Write("{0} ", array[i]);


            Console.WriteLine();
            Console.WriteLine();
            for (int i = 0; i < 2; i++)
            {
                Console.WriteLine("{0}", ArrayC[i]);
            }
        }