Example #1
0
        private void RefreshDevicesList(OpenCLManager oclManager)
        {
            devicesAvailable.Clear();
            int c = 0;

            if (oclManager != null)
            {
                foreach (var device in oclManager.Context.Devices)
                {
                    string deviceName = device.Vendor + ":" + device.Name;
                    deviceSettings.Add(new OpenCLDeviceSettings(this)
                    {
                        name = deviceName, index = c, mode = 1, UseDevice = false
                    });
                    devicesAvailable.Add(deviceName);
                    c++;
                }
            }
            DevicesAvailable = devicesAvailable;    //refresh list
            if (devicesAvailable.Count > 0)
            {
                OpenCLDevice = 0;
            }
            else
            {
                this.openCLDevice = -1;
            }
        }
Example #2
0
    //functions
    public static void initScan(OpenCLManager OCLManager, CommandQueue cqCommandQueue, int numDataShift, int chunkSize)
    {
        Debug.Log(" ...loading Scan.cl and creating scan program then build it\n");
        Program cpProgram;

        for (int i = 0; i < WORKGROUP_SIZE_AVAILABLE.Length; i++)
        {
            OCLManager.BuildOptions = "-D WORKGROUP_SIZE=" + WORKGROUP_SIZE_AVAILABLE[i];
            OCLManager.Defines      = "";
            try{
                TextAsset srcKernel = Resources.Load("OclKernel/Scan") as TextAsset;
                cpProgram = OCLManager.CompileSource(srcKernel.text);                   // TODO : seperate 4 file to reuse binary
                srcKernel = null;
            }
            catch (OpenCLBuildException e) {
                string log = "CL Kernel Error: ";
                for (int j = 0; j < e.BuildLogs.Count; j++)
                {
                    log += e.BuildLogs[j];
                }
                Debug.LogError(log);

                throw;
                return;
            }

            ckScanExclusiveLocal1Array[i] = cpProgram.CreateKernel("scanExclusiveLocal1");
            ckScanExclusiveLocal2Array[i] = cpProgram.CreateKernel("scanExclusiveLocal2");
            ckUniformUpdateArray[i]       = cpProgram.CreateKernel("uniformUpdate");
        }
        cpProgram = null;

        /* TODO : check
         *  Debug.Log( " ...checking minimum supported workgroup size\n");
         *  //Check for work group size
         *  cl_device_id device;
         *  uint szScanExclusiveLocal1, szScanExclusiveLocal2, szUniformUpdate;
         *  ciErrNum  = clGetCommandQueueInfo(cqParamCommandQue, CL_QUEUE_DEVICE, sizeof(cl_device_id), &device, NULL);
         *  ciErrNum |= clGetKernelWorkGroupInfo(ckScanExclusiveLocal1,  device, CL_KERNEL_WORK_GROUP_SIZE, sizeof(uint), &szScanExclusiveLocal1, NULL);
         *  ciErrNum |= clGetKernelWorkGroupInfo(ckScanExclusiveLocal2, device, CL_KERNEL_WORK_GROUP_SIZE, sizeof(uint), &szScanExclusiveLocal2, NULL);
         *  ciErrNum |= clGetKernelWorkGroupInfo(ckUniformUpdate, device, CL_KERNEL_WORK_GROUP_SIZE, sizeof(uint), &szUniformUpdate, NULL);
         *
         *  if( (szScanExclusiveLocal1 < WORKGROUP_SIZE) || (szScanExclusiveLocal2 < WORKGROUP_SIZE) || (szUniformUpdate < WORKGROUP_SIZE) ){
         *      Debug.Log("ERROR: Minimum work-group size %u required by this application is not supported on this device.\n", WORKGROUP_SIZE);
         *      return false;
         *  }
         */

        Debug.Log(" ...allocating internal buffers\n");
        // allocate offset data
        OFS_DATA_SIZE = (1 << (numDataShift - 9));      // (max_voxel_num/512)
        ofsDataArray  = new int[OFS_DATA_SIZE + SUM_DATA_SIZE];
        hofsDataArray = GCHandle.Alloc(ofsDataArray, GCHandleType.Pinned);
        d_Buffer      = OCLManager.Context.CreateBuffer(MemFlags.READ_WRITE, (BUFFER_LENGTH + OFS_DATA_SIZE + SUM_DATA_SIZE) * sizeof(uint));
    }
Example #3
0
    private static void CreateOclManagers()     // Create an OpenCLManager for each platform
    {
        instances = new List <OpenCLManager>();

        int nPlatforms = OpenCL.NumberOfPlatforms;

        Debug.Log("[OCLLOG]:Platform Count:" + nPlatforms);
        if (nPlatforms <= 0)
        {
            Debug.LogError("[OCLLOG]:OpenCLIs[NOT]Available");
            return;
        }

        for (int pIdx = 0; pIdx < nPlatforms; pIdx++)
        {
            OpenCLNet.Platform platform = OpenCLNet.OpenCL.GetPlatform(pIdx);
            OpenCLNet.Device[] devices  = platform.QueryDevices(OpenCLNet.DeviceType.ALL);
            int nDevices = devices.Length;
            if (nDevices <= 0)
            {
                Debug.LogError("[OCLLOG]:No OpenCL devices found that matched filter criteria on Platform_" + platform.Name);
                continue;
            }
            // We might use the whole devices as CreateContext para, but we don't do so.
            // Here create a oclMan for each device because
            // the same platform will build kernel for each device in its context and can be failed
            // (such as apple, failed in building kernel on HD6630M, succeeded on intel cpu)
            for (int dIdx = 0; dIdx < nDevices; dIdx++)
            {
                try{
                    Debug.Log("[OCLLOG]:Creating OCL on Platform_" + platform.Name + "+Device_" + devices[dIdx].Name);
                    OpenCLManager oclMan = new OpenCLManager();
                    oclMan.SourcePath = OclSourcePath;
                    oclMan.BinaryPath = OclBinaryPath;

                    IntPtr[] properties = new IntPtr[]
                    {
                        (IntPtr)ContextProperties.PLATFORM, platform,
                        IntPtr.Zero
                    };
                    OpenCLNet.Device[] devicesEnum = new Device[] { devices[dIdx] };
                    oclMan.CreateContext(platform, properties, devicesEnum);
                    instances.Add(oclMan);
                }
                catch (Exception e) {
                    Debug.Log("[OCLLOG]Exception at Platform_" + platform.Name + "+Device_" + devices[dIdx].Name + ":" + e.Message);
                }
            }
        }
    }
Example #4
0
        public KeySearcherSettings(KeySearcher ks, OpenCLManager oclManager)
        {
            keysearcher = ks;
            RefreshDevicesList(oclManager);

            CoresAvailable.Clear();
            for (int i = -1; i < Environment.ProcessorCount; i++)
            {
                CoresAvailable.Add((i + 1).ToString());
            }
            CoresUsed = Environment.ProcessorCount - 1;

            chunkSize = 21;

            KeyManager = new SimpleKeyManager("");
        }
Example #5
0
    public static bool InitOclFromOpt()     // true: Succeeded; false: failed and use CPU instead
    {
        ActiveOclMan = null;
        ActiveOclCQ  = null;
        if (0 == string.Compare(OptCpuMask, 0, CurOclOpt, 0, OptCpuMask.Length))
        {
            Debug.Log("[OCLLOG]Succeed to init with " + CurOclOpt);
            return(true);
        }

        if (instances == null)
        {
            CreateOclManagers();
        }
        try{
            foreach (OpenCLManager oclMan in instances)
            {
                string platformName16 = oclMan.Platform.Name.Length > 16 ? oclMan.Platform.Name.Substring(0, 16) : oclMan.Platform.Name.PadRight(16, ' ');
                foreach (CommandQueue cq in oclMan.CQ)
                {
                    string opt = (OptOclMask + "|" + platformName16 + "|" + cq.Device.Name).Replace(' ', '_');
                    if (0 == string.Compare(opt, CurOclOpt))
                    {
                        ActiveOclMan = oclMan;
                        ActiveOclCQ  = cq;
                        if (oclMarchingCube.InitMC())
                        {
                            Debug.Log("[OCLLOG]Succeed to init with " + CurOclOpt);
                            return(true);
                        }
                        else
                        {
                            throw new Exception("Failed to init with " + CurOclOpt);
                        }
                    }
                }
            }
        }
        catch {}

        Debug.Log("[OCLLOG]Fallback to CPU because of failure to init with " + CurOclOpt);
        ActiveOclMan = null;
        ActiveOclCQ  = null;
        CurOclOpt    = OptCpuMask;
        return(false);
    }
Example #6
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 #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");
        }
Example #8
0
        /// <summary>
        /// Generates the OpenCL code and creates the kernel out of it.
        /// </summary>
        /// <param name="oclManager">The OpenCL manager to use.</param>
        /// <param name="keyTranslator">The KeyTranslator to use. This is important for mapping the key movements to code.</param>
        /// <returns>The Kernel</returns>
        public Kernel GetBruteforceKernel(OpenCLManager oclManager, IKeyTranslator keyTranslator)
        {
            //caching:
            if (keyTranslatorOfCode == keyTranslator)
            {
                return(openCLKernel);
            }

            try
            {
                var program = oclManager.CompileSource(CreateOpenCLBruteForceCode(keyTranslator));
                //keySearcher.GuiLogMessage(string.Format("Using OpenCL with (virtually) {0} threads.", keyTranslator.GetOpenCLBatchSize()), NotificationLevel.Info);
                openCLKernel = program.CreateKernel("bruteforceKernel");
                return(openCLKernel);
            }
            catch (Exception ex)
            {
                throw new Exception(Resources.An_error_occured_when_trying_to_compile_OpenCL_code__ + ex.Message);
            }
        }
Example #9
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 #10
0
 /* Создает новый объем размера size*size*size */
 public VoxelVolume(int size, OpenCLManager openClManager)
 {
     data         = new short[size * size * size];
     size_this    = size;
     manager_this = openClManager;
 }
Example #11
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;
            }
        }
    ////////////////////////////////////////////////////////////////////////////////
    // initialize marching cubes
    ////////////////////////////////////////////////////////////////////////////////
    public static bool InitMC()
    {
        try{
            OCLManager  = oclManager.ActiveOclMan;
            OCLComQueue = oclManager.ActiveOclCQ;

            try
            {
                oclScanLaucher.initScan(OCLManager, OCLComQueue, VOLUME_VOXEL_X_LEN_SHIFT + VOLUME_VOXEL_Y_LEN_SHIFT + VOLUME_VOXEL_Z_LEN_SHIFT, CHUNK_VOXEL_NUM);
            }
            catch (Exception e)
            {
                string log = "[OCLLOG]Kernel Error: ";
                OpenCLBuildException eocl = e as OpenCLBuildException;
                if (eocl != null)
                {
                    for (int i = 0; i < eocl.BuildLogs.Count; i++)
                    {
                        log += eocl.BuildLogs[i];
                    }
                }
                else
                {
                    log += e.Message;
                }
                Debug.LogError(log);
                throw;
            }

            ImageFormat imageFormat = new ImageFormat(CH_ORDER, ChannelType.UNORM_INT8);
            bImageFormatSupported = (!OCLComQueue.Device.Name.Contains("RV7") /*!bRV7xxGpu*/ &&
                                     OCLComQueue.Context.SupportsImageFormat(MemFlags.READ_ONLY, MemObjectType.IMAGE3D, imageFormat.ChannelOrder, imageFormat.ChannelType));
            //bImageFormatSupported = false;
            OCLManager.BuildOptions = "-cl-mad-enable";
            OCLManager.Defines      = "";
            Program cpProgram = null;
            while (true)
            {
                try
                {
                    string    mcKernelPathName = bImageFormatSupported ? "OclKernel/marchingCubes_kernel_img" : ("OclKernel/marchingCubes_kernel_u" + VOXEL_SIZE + "b");
                    TextAsset srcKernel        = Resources.Load(mcKernelPathName) as TextAsset;
                    Debug.Log("[OCLLOG]Build kernel:" + mcKernelPathName);
                    cpProgram = OCLManager.CompileSource(srcKernel.text);
                    srcKernel = null;
                }
                catch (Exception e)
                {
                    string log = "[OCLLOG]Kernel Error: ";
                    OpenCLBuildException eocl = e as OpenCLBuildException;
                    if (eocl != null)
                    {
                        for (int i = 0; i < eocl.BuildLogs.Count; i++)
                        {
                            log += eocl.BuildLogs[i];
                        }
                    }
                    else
                    {
                        log += e.Message;
                    }
                    Debug.LogError(log);
                    if (bImageFormatSupported)
                    {
                        bImageFormatSupported = false;
                        Debug.Log("[OCLLOG]Try to build kernel without img support:");
                        continue;
                    }

                    throw;
                }
                break;
            }
            classifyVoxelKernel      = cpProgram.CreateKernel("classifyVoxel");
            compactVoxelsKernel      = cpProgram.CreateKernel("compactVoxels");
            generateTriangles2Kernel = cpProgram.CreateKernel("generateTriangles2_vec3");
            cpProgram = null;
            Debug.Log("[OCLLOG]All kernels are ready.");

            if (bImageFormatSupported)
            {
                d_volume = OCLManager.Context.CreateImage3D(MemFlags.READ_ONLY | MemFlags.ALLOC_HOST_PTR,
                                                            imageFormat,
                                                            VOLUME_VOXEL_X_LEN_REAL, VOLUME_VOXEL_Y_LEN_REAL, VOLUME_VOXEL_Z_LEN_REAL,
                                                            0, 0, IntPtr.Zero);
            }
            else
            {
                isoValue = 128f;
                d_volume = OCLManager.Context.CreateBuffer(MemFlags.READ_ONLY | MemFlags.ALLOC_HOST_PTR,
                                                           VOLUME_VOXEL_X_LEN_REAL * VOLUME_VOXEL_Y_LEN_REAL * VOLUME_VOXEL_Z_LEN_REAL * VOXEL_SIZE);
            }

            // create VBOs --- now use 3 float, nvidia use 4 float originally.
            Vector3[] posArray    = new Vector3[MAX_VERTS];
            Vector2[] norm01Array = new Vector2[MAX_VERTS];
            Vector2[] norm2tArray = new Vector2[MAX_VERTS];
            hPosArray    = GCHandle.Alloc(posArray, GCHandleType.Pinned);
            hNorm01Array = GCHandle.Alloc(norm01Array, GCHandleType.Pinned);
            hNorm2tArray = GCHandle.Alloc(norm2tArray, GCHandleType.Pinned);
            d_pos        = OCLManager.Context.CreateBuffer(MemFlags.WRITE_ONLY | MemFlags.USE_HOST_PTR, MAX_VERTS * sizeof(float) * 3, hPosArray.AddrOfPinnedObject());
            d_norm01     = OCLManager.Context.CreateBuffer(MemFlags.WRITE_ONLY | MemFlags.USE_HOST_PTR, MAX_VERTS * sizeof(float) * 2, hNorm01Array.AddrOfPinnedObject());
            d_norm2t     = OCLManager.Context.CreateBuffer(MemFlags.WRITE_ONLY | MemFlags.USE_HOST_PTR, MAX_VERTS * sizeof(float) * 2, hNorm2tArray.AddrOfPinnedObject());

            // allocate device memory
            uint memSize = sizeof(uint) * MAX_VOXELS;
            d_voxelVerts        = OCLManager.Context.CreateBuffer(MemFlags.READ_WRITE, memSize);
            d_voxelVertsScan    = OCLManager.Context.CreateBuffer(MemFlags.READ_WRITE, memSize);
            d_voxelOccupied     = OCLManager.Context.CreateBuffer(MemFlags.READ_WRITE, memSize);
            d_voxelOccupiedScan = OCLManager.Context.CreateBuffer(MemFlags.READ_WRITE, memSize);
            d_compVoxelArray    = OCLManager.Context.CreateBuffer(MemFlags.READ_WRITE, memSize);

            oclMCStatus = 1;
        }catch {
            oclMCStatus = -1;
            Debug.LogError("[OCLLOG]OclMarchingCubes is not available.");
            return(false);
        }
        // Other const helper -- TODO : size should be computed according to IMG_FORMAT
        volumeZeroConst = new byte[CHUNK_VOXEL_NUM_REAL * 4];           // all zero
        numChunks       = 0;
        return(true);
    }
Example #13
0
    //functions
    public static void initScan(OpenCLManager OCLManager, CommandQueue cqCommandQueue, int numDataShift, int chunkSize)
    {
        OCLManager.BuildOptions = "";
        OCLManager.Defines      = "";
        Program cpProgram;

        try{
            TextAsset srcKernel = Resources.Load("OclKernel/ScanLargeArrays_Kernels") as TextAsset;
            Debug.Log("[OCLLOG]Build kernel:Scan");
            cpProgram = OCLManager.CompileSource(srcKernel.text);
            srcKernel = null;
        }
        catch (OpenCLBuildException e) {
            string log = "[OCLLOG]Kernel Error: ";
            for (int i = 0; i < e.BuildLogs.Count; i++)
            {
                log += e.BuildLogs[i];
            }
            Debug.LogError(log);

            throw;
            //return;
        }
        ckScanLargeKernel = cpProgram.CreateKernel("ScanLargeArrays");
        ckBlockAddiKernel = cpProgram.CreateKernel("blockAddition");
        ckPrefixSumKernel = cpProgram.CreateKernel("prefixSum");
        zeromemKernel     = cpProgram.CreateKernel("zeromem");
        cpProgram         = null;

        MaxGroupSize = (int)cqCommandQueue.Device.MaxWorkGroupSize;
        int maxWorkItemSize  = cqCommandQueue.Device.MaxWorkItemSizes[0].ToInt32();
        int maxWorkItemSize1 = cqCommandQueue.Device.MaxWorkItemSizes[1].ToInt32();
        int maxWorkItemSize2 = cqCommandQueue.Device.MaxWorkItemSizes[2].ToInt32();

        Debug.Log("[OCLLOG]SCAN MaxGroup:" + MaxGroupSize + " MaxWorkItem:" + maxWorkItemSize + "," + maxWorkItemSize1 + "," + maxWorkItemSize2);
        if (cqCommandQueue.Device.Name.Contains("RV7"))
        {
            MaxGroupSize = maxWorkItemSize = 32;
            Debug.Log("[OCLLOG]SCAN RV7xx lower MaxGroup:" + MaxGroupSize + "MaxWorkItem:" + maxWorkItemSize);
        }
#if UNITY_STANDALONE_OSX
        else
        {
            MaxGroupSize = maxWorkItemSize = maxWorkItemSize / 4;
            Debug.Log("[OCLLOG]SCAN Apple lower(/4) MaxGroup:" + MaxGroupSize + "MaxWorkItem:" + maxWorkItemSize);
        }
#endif
        if (maxWorkItemSize > chunkSize)
        {
            maxWorkItemSize = chunkSize;
        }
        blockSize = 1;          blockSizeShift = 0;
        while (blockSize < maxWorkItemSize)
        {
            blockSize <<= 1;                  blockSizeShift++;
        }
        //blockSize >>= 1;blockSizeShift--;
        blockSizeUnMask = ~(blockSize - 1);

        // compute buffer length and offset
        Mem  buffBlockSum;
        Mem  buffTmpOutput;
        bool bSumDataAllocated = false;
        blockSumBufferList.Add(null);   // Add a placeholder for inputBuffer
        outBufferList.Add(null);        // Add a placeholder
        if (numDataShift < blockSizeShift)
        {
            numDataShift = blockSizeShift;              // at least 1 even if blockSizeShift >= numDataShift
        }
        do
        {
            numDataShift -= blockSizeShift;
            buffBlockSum  = OCLManager.Context.CreateBuffer(MemFlags.READ_WRITE, (1 << numDataShift) * sizeof(uint));
            blockSumBufferList.Add(buffBlockSum);
            if (bSumDataAllocated == false)
            {
                OFS_DATA_SIZE     = (1 << numDataShift);
                ofsDataArray      = new int[OFS_DATA_SIZE + SUM_DATA_SIZE];
                hofsDataArray     = GCHandle.Alloc(ofsDataArray, GCHandleType.Pinned);
                buffTmpOutput     = OCLManager.Context.CreateBuffer(MemFlags.READ_WRITE, (OFS_DATA_SIZE + SUM_DATA_SIZE) * sizeof(uint));
                bSumDataAllocated = true;
            }
            else
            {
                buffTmpOutput = OCLManager.Context.CreateBuffer(MemFlags.READ_WRITE, (1 << numDataShift) * sizeof(uint));
            }
            outBufferList.Add(buffTmpOutput);
        }while(numDataShift > blockSizeShift);
#if CL_DEBUG
        houtArray = GCHandle.Alloc(outArray, GCHandleType.Pinned);
        hsumArray = GCHandle.Alloc(sumArray, GCHandleType.Pinned);
#endif
    }