Example #1
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");
        }
Example #2
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");
        }
Example #3
0
        private void RefreshDevicesList( )
        {
            var oclManager = new OpenCLManager();

            devicesAvailable.Clear();
            for (var id = 0; id < OpenCL.GetPlatforms().Length; id++)
            {
                oclManager.CreateDefaultContext(id, DeviceType.ALL);
                int c = 0;
                if (oclManager != null)
                {
                    foreach (var device in oclManager.Context.Devices)
                    {
                        devicesAvailable.Add(c + ": " + device.Vendor + " - " + device.Name);
                        c++;
                    }
                }
            }
            DevicesAvailable = devicesAvailable;
        }
Example #4
0
        public unsafe void InitTasks()
        {
            if (!inited)
            {
                if (OpenCL.NumberOfPlatforms < 1)
                {
                    throw new Exception("no opencl platforms found");
                }

                int groupSize = _settings.DeviceType == OpenCLDeviceType.CPU ? 1 : _settings.GroupSize;
                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; // 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 BuildOptions string is passed directly to clBuild and can be used to do debug builds etc
                OCLMan.BuildOptions = "";
                OCLMan.SourcePath   = System.IO.Path.GetDirectoryName(GetType().Assembly.Location);
                OCLMan.BinaryPath   = System.IO.Path.Combine(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "CUE Tools"), "OpenCL");
                int platformId = 0;
                if (_settings.Platform != null)
                {
                    platformId = -1;
                    string platforms = "";
                    for (int i = 0; i < OpenCL.NumberOfPlatforms; i++)
                    {
                        var platform = OpenCL.GetPlatform(i);
                        platforms += " \"" + platform.Name + "\"";
                        if (platform.Name.Equals(_settings.Platform, StringComparison.InvariantCultureIgnoreCase))
                        {
                            platformId = i;
                            break;
                        }
                    }
                    if (platformId < 0)
                    {
                        throw new Exception("unknown platform \"" + _settings.Platform + "\". Platforms available:" + platforms);
                    }
                }
                OCLMan.CreateDefaultContext(platformId, (DeviceType)_settings.DeviceType);

                this.stridesPerTask = (int)OCLMan.Context.Devices[0].MaxComputeUnits * npar * 8;

                // 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 GROUP_SIZE " + groupSize.ToString() + "\n" +
                    "#define CLPARITY_VERSION \"" + vendor_string + "\"\n" +
#if DEBUG
                    "#define DEBUG\n" +
#endif
                    (_settings.DeviceType == OpenCLDeviceType.CPU ? "#define CLPARITY_CPU\n" : "") +
                    _settings.Defines + "\n";

                var exts = new string[] { "cl_khr_local_int32_base_atomics", "cl_khr_local_int32_extended_atomics", "cl_khr_fp64", "cl_amd_fp64" };
                foreach (string extension in exts)
                {
                    if (OCLMan.Context.Devices[0].Extensions.Contains(extension))
                    {
                        OCLMan.Defines += "#pragma OPENCL EXTENSION " + extension + ": enable\n";
                        OCLMan.Defines += "#define HAVE_" + extension + "\n";
                    }
                }

                try
                {
                    openCLProgram = OCLMan.CompileFile("parity.cl");
                }
                catch (OpenCLBuildException ex)
                {
                    string buildLog = ex.BuildLogs[0];
                    throw ex;
                }
                //using (Stream kernel = GetType().Assembly.GetManifestResourceStream(GetType(), "parity.cl"))
                //using (StreamReader sr = new StreamReader(kernel))
                //{
                //    try
                //    {
                //        openCLProgram = OCLMan.CompileSource(sr.ReadToEnd()); ;
                //    }
                //    catch (OpenCLBuildException ex)
                //    {
                //        string buildLog = ex.BuildLogs[0];
                //        throw ex;
                //    }
                //}
#if TTTTKJHSKJH
                var openCLPlatform = OpenCL.GetPlatform(0);
                openCLContext = openCLPlatform.CreateDefaultContext();
                using (Stream kernel = GetType().Assembly.GetManifestResourceStream(GetType(), "parity.cl"))
                    using (StreamReader sr = new StreamReader(kernel))
                        openCLProgram = openCLContext.CreateProgramWithSource(sr.ReadToEnd());
                try
                {
                    openCLProgram.Build();
                }
                catch (OpenCLException)
                {
                    string buildLog = openCLProgram.GetBuildLog(openCLProgram.Devices[0]);
                    throw;
                }
#endif

                task1  = new CLParityTask(openCLProgram, this, groupSize, this.npar, this.stride, this.stridesPerTask);
                task2  = new CLParityTask(openCLProgram, this, groupSize, this.npar, this.stride, this.stridesPerTask);
                inited = true;
            }
        }