static private void WorkerMain() { try { _nJobIndx = 0; int nCounter = 0; (new Logger()).WriteNotice("main worker started"); //Logger.Timings cTimings = new helpers.Logger.Timings("discom:WorkerMain:profiling"); while (true) { _cDisComProcessing = _aqQueue.Dequeue(); //DNF if (_cDisComProcessing._cInfo is MergeInfo) { _cMergeInfo = (MergeInfo)_cDisComProcessing._cInfo; #if !CUDATEST if (_cMergeInfo.bLayer1CopyToBackground) { Array.Copy(_cDisComProcessing._aLayers[1], _cDisComProcessing._aLayers[0], _cDisComProcessing._aLayers[0].Length); } else { Array.Clear(_cDisComProcessing._aLayers[0], 0, _cDisComProcessing._aLayers[0].Length); } #endif } else { _cMergeInfo = null; } if (100 < nCounter++) { //cTimings.TotalRenew(); //profiling nCounter = 0; _nJobIndx = 1; //(new Logger()).WriteNotice("profiling begin [id:" + _cDisComProcessing.GetHashCode() + "][max_indx:" + _cDisComProcessing._nMaxTasksIndx + "][total_threads:" + _aMREDone.Length + "]"); } else { _nJobIndx = 0; } //_cDisComProcessing._cMREDone.Reset(); foreach (ManualResetEvent cMRE in _aMREDone) { cMRE.Reset(); } //if (_nJobIndx == 1) //profiling // cTimings.Restart("done reset"); foreach (ManualResetEvent cMRE in _aMREStart) { cMRE.Set(); } //if (_nJobIndx == 1) //profiling // cTimings.Restart("start set"); for (int nIndx = 0; nIndx < _aMREDone.Length; nIndx += 64) { ManualResetEvent.WaitAll(_aMREDone.Skip(nIndx).Take(64).ToArray()); } _cDisComProcessing._cMREDone.Set(); //if (_nJobIndx == 1) //profiling //{ // (new Logger()).WriteNotice("profiling end [id:" + _cDisComProcessing.GetHashCode() + "][max_indx:" + _cDisComProcessing._nMaxTasksIndx + "][total_jobs_done = " + _cDisComProcessing._nJobsDone + "]"); // cTimings.Stop("merge done"); //} } } catch (ThreadInterruptedException) { } catch (Exception ex) { (new Logger()).WriteError(ex); } }
static private void WorkerMain() { try { //(new Logger()).WriteNotice("[id:" + GetHashCode() + ":" + nID + "][total:" + nThreadsTotalQty + "][start]"); while (true) { _cDisComProcessing = _aqQueue.Dequeue(); _cDisComProcessing._cMREDone.Reset(); foreach (ManualResetEvent cMRE in _aMREDone) cMRE.Reset(); foreach (ManualResetEvent cMRE in _aMREStart) cMRE.Set(); for (int nIndx = 0; nIndx < _aMREDone.Length; nIndx += 64) ManualResetEvent.WaitAll(_aMREDone.Skip(nIndx).Take(64).ToArray()); _cDisComProcessing._cMREDone.Set(); } //(new Logger()).WriteNotice("[id:" + GetHashCode() + ":" + nID + "][total:" + nThreadsTotalQty + "][stop]"); } catch (ThreadInterruptedException) { } catch (Exception ex) { (new Logger()).WriteError(ex); } }
//static public void Init() //{ // Init(Environment.ProcessorCount / 2); //} static public void Init(int nThreadsQty) { lock (_aqQueue) { if (_bInited) { return; } #if CUDATEST (new Logger()).WriteError("MERGING CUDA TEST IS ON!!!"); #endif if (null == _aAlphaMap) { _cDisComProcessing = null; _aAlphaMap = new byte[byte.MaxValue - 1, byte.MaxValue + 1, byte.MaxValue + 1]; int nResult; for (byte nAlpha = 1; 255 > nAlpha; nAlpha++) { for (ushort nBackground = 0; 256 > nBackground; nBackground++) { for (ushort nForeground = 0; 256 > nForeground; nForeground++) { if (255 < (nResult = (int)((float)(nAlpha * (nForeground - nBackground)) / 255 + nBackground + 0.5))) { nResult = 255; } _aAlphaMap[nAlpha - 1, nBackground, nForeground] = (byte)nResult; } } } //nPixelAlpha = (byte)((float)nFGColorAlpha * nPixelAlpha / 255 + 0.5); _aAlphaMap2 = new byte[byte.MaxValue - 1, byte.MaxValue - 1]; for (byte nFGColorAlpha = 1; 255 > nFGColorAlpha; nFGColorAlpha++) // мможно использовать симметрию умножения, но х с ней пока { for (byte nPixelAlpha = 1; 255 > nPixelAlpha; nPixelAlpha++) { _aAlphaMap2[nFGColorAlpha - 1, nPixelAlpha - 1] = (byte)((float)nFGColorAlpha * nPixelAlpha / 255 + 0.5); } } //nFGColorAlpha = (byte)(nFGColorAlpha * (1 - _cDisComProcessing._aLayers[nLayerIndx - 1][nMaskIndx] / 255f) + 0.5); _aAlphaMap3 = new byte[byte.MaxValue, byte.MaxValue - 1]; for (ushort nFGColorAlpha = 1; 256 > nFGColorAlpha; nFGColorAlpha++) { for (byte nMask = 1; 255 > nMask; nMask++) { _aAlphaMap3[nFGColorAlpha - 1, nMask - 1] = (byte)(nFGColorAlpha * ((255 - nMask) / 255f) + 0.5); } } _nThreadsQty = (ulong)nThreadsQty; (new Logger()).WriteNotice("[threads=" + _nThreadsQty + "][ProcessorCount=" + Environment.ProcessorCount + "][pref_cout=" + PixelsMap.Preferences.nDisComThreadsQty + "]"); _aThreads = new Thread[_nThreadsQty]; _cThreadMain = new Thread(new ThreadStart(WorkerMain)); _cThreadMain.IsBackground = true; _cThreadMain.Priority = ThreadPriority.AboveNormal; _cThreadMain.Start(); Thread cThread = null; _aMREStart = new ManualResetEvent[_nThreadsQty]; _aMREDone = new ManualResetEvent[_nThreadsQty]; for (ushort nIndx = 0; _nThreadsQty > nIndx; nIndx++) { _aMREStart[nIndx] = new ManualResetEvent(false); _aMREDone[nIndx] = new ManualResetEvent(false); cThread = new Thread(new ParameterizedThreadStart(Worker)); //(new ParameterizedThreadStart(Worker)); _aThreads[nIndx] = cThread; cThread.IsBackground = true; cThread.Priority = ThreadPriority.AboveNormal; cThread.Start(nIndx); } } _bInited = true; } }
static public void Init() { lock (_aqQueue) { if (null == _aAlphaMap) { _cDisComProcessing = null; _aAlphaMap = new byte[byte.MaxValue - 1, byte.MaxValue + 1, byte.MaxValue + 1]; int nResult; for (byte nAlpha = 1; 255 > nAlpha; nAlpha++) { for (ushort nBackground = 0; 256 > nBackground; nBackground++) { for (ushort nForeground = 0; 256 > nForeground; nForeground++) { if (255 < (nResult = (int)((float)(nAlpha * (nForeground - nBackground)) / 255 + nBackground + 0.5))) nResult = 255; _aAlphaMap[nAlpha - 1, nBackground, nForeground] = (byte)nResult; } } } int nThreadsQty = Environment.ProcessorCount * 2; // DNF *2 _aThreads = new Thread[nThreadsQty]; _cThreadMain = new Thread(new ThreadStart(WorkerMain)); _cThreadMain.IsBackground = true; _cThreadMain.Priority = ThreadPriority.Highest; _cThreadMain.Start(); Thread cThread = null; _aqTasks = new Dictionary<DisCom, int>(); _aMREStart = new ManualResetEvent[nThreadsQty]; _aMREDone = new ManualResetEvent[nThreadsQty]; for (ushort nIndx = 0; nThreadsQty > nIndx; nIndx++) { _aMREStart[nIndx] = new ManualResetEvent(false); _aMREDone[nIndx] = new ManualResetEvent(false); cThread = new Thread(new ParameterizedThreadStart(Worker)); _aThreads[nIndx] = cThread; cThread.IsBackground = true; cThread.Priority = ThreadPriority.Highest; cThread.Start(nIndx); } } } }
static private int GetTask(DisCom cDC) { lock (cDC) { if (_aqTasks[cDC] < cDC._nMaxTasksIndx) return _aqTasks[cDC]++; else return int.MaxValue; } }
public void Merge(List<PixelsMap> aPMs) { if (null == aPMs) throw new Exception("PixelsMap array is null"); //ConsistencyCheck(); if (0 < aPMs.Count) { List<PixelsMap> aPMsActual = new List<PixelsMap>(); DisCom.MergeInfo cMergeInfo = new DisCom.MergeInfo(); List<DisCom.LayerInfo> aLayerInfos = new List<DisCom.LayerInfo>(); DisCom.LayerInfo cLayerInfo; cMergeInfo.nBackgroundSize = _stArea.nWidth * _stArea.nHeight; cMergeInfo.nBackgroundWidth = _stArea.nWidth; cMergeInfo.nBackgroundHight = _stArea.nHeight; cMergeInfo.nBackgroundAlphaType = (byte)eAlpha; for (int nIndx = 0; nIndx < aPMs.Count; nIndx++) { lock (aPMs[nIndx]._cSyncRoot) { if (aPMs[nIndx]._bDisposed) continue; aPMs[nIndx]._bProcessing = true; } cLayerInfo = aPMs[nIndx].Intersect(new Area(0, 0, _stArea.nWidth, _stArea.nHeight)); //не stBase т.к. нужны относительные координаты, а не абсолютные if (0 > (cLayerInfo.nBackgroundStop - cLayerInfo.nBackgroundStart)) //если пересечения FG и BG нет..... { aPMs[nIndx].Dispose(); continue; } aPMsActual.Add(aPMs[nIndx]); aLayerInfos.Add(cLayerInfo); } if (0 < aPMsActual.Count) { cMergeInfo.nLayersQty = (ushort)(aLayerInfos.Count + 1); cMergeInfo.aLayerInfos = aLayerInfos.ToArray(); #if CUDA if (bCUDA) { Command cCmd = new Command(Command.ID.Merge, this); cCmd.ahParameters.Add(typeof(List<PixelsMap>), aPMsActual); cCmd.ahParameters.Add(typeof(DisCom.MergeInfo), cMergeInfo); _aqCommands.Enqueue(cCmd); cCmd.cMRE.WaitOne(); return; } #endif List<byte[]> aDPs = new List<byte[]>(); if (1 > _nID) throw new Exception("background PixelsMap have to be allocated for Merge"); aDPs.Add(_aBytes); for (int nIndx = 0; nIndx < aPMsActual.Count; nIndx++) aDPs.Add(aPMsActual[nIndx]._aBytes); DisCom cDisCom = new DisCom(); Logger.Timings cTiming = new helpers.Logger.Timings("BTL:pixelmap:"); cDisCom.FrameMerge(cMergeInfo, aDPs); cTiming.Stop("merge > 35", 35); cDisCom.Dispose(); for (int nIndx = 0; nIndx < aPMsActual.Count; nIndx++) { lock (aPMsActual[nIndx]._cSyncRoot) aPMsActual[nIndx]._bProcessing = false; aPMsActual[nIndx].Dispose(); } } } }
public static void DisComInit() { DisCom.Init(Preferences.nDisComThreadsQty); }
public void Merge(List <PixelsMap> aPMs, bool bHighPriority) { if (null == aPMs) { throw new Exception("PixelsMap array is null"); } //ConsistencyCheck(); if (0 < aPMs.Count) { _cTiming.TotalRenew(); List <PixelsMap> aPMsActual = new List <PixelsMap>(); DisCom.MergeInfo cMergeInfo = new DisCom.MergeInfo(); List <DisCom.LayerInfo> aLayerInfos = new List <DisCom.LayerInfo>(); DisCom.LayerInfo cLayerInfo; cMergeInfo.nBackgroundSize = _stArea.nWidth * _stArea.nHeight; cMergeInfo.nBackgroundWidth_4 = 4 * _stArea.nWidth; cMergeInfo.nBackgroundWidth = _stArea.nWidth; cMergeInfo.nBackgroundHight = _stArea.nHeight; cMergeInfo.nBackgroundAlphaType = (byte)eAlpha; if (aPMs[0].stArea == _stArea) { cMergeInfo.bLayer1CopyToBackground = true; } for (int nIndx = 0; nIndx < aPMs.Count; nIndx++) { lock (aPMs[nIndx]._cSyncRoot) { if (aPMs[nIndx]._bDisposed) { continue; } aPMs[nIndx]._bProcessing = true; } cLayerInfo = aPMs[nIndx].Intersect(new Area(0, 0, _stArea.nWidth, _stArea.nHeight)); //не stBase т.к. нужны относительные координаты, а не абсолютные if (0 > cLayerInfo.nCropBottomLineInBG) //если пересечения FG и BG нет..... { aPMs[nIndx].Dispose(); continue; } aPMsActual.Add(aPMs[nIndx]); cLayerInfo.nBytesQty = (int)aPMs[nIndx]._nBytesQty; aLayerInfos.Add(cLayerInfo); } _cTiming.Restart("layer infos"); if (0 < aPMsActual.Count) { cMergeInfo.nLayersQty = (ushort)(aLayerInfos.Count + 1); cMergeInfo.aLayerInfos = aLayerInfos.ToArray(); #if CUDA if (stMergingMethod.eDeviceType > 0) { Command cCmd = new Command(Command.ID.Merge, this); cCmd.ahParameters.Add(typeof(List <PixelsMap>), aPMsActual); cCmd.ahParameters.Add(typeof(DisCom.MergeInfo), cMergeInfo); _ahMergingHash_CommandsQueue[stMergingMethod.nHash].Enqueue(cCmd); cCmd.cMRE.WaitOne(); _cTiming.Stop("merge", stMergingMethod.ToString(), 40); return; } #endif List <byte[]> aDPs = new List <byte[]>(); if (1 > _nID) { throw new Exception("background PixelsMap have to be allocated for Merge"); } aDPs.Add(_aBytes.aBytes); for (int nIndx = 0; nIndx < aPMsActual.Count; nIndx++) { aDPs.Add(aPMsActual[nIndx]._aBytes.aBytes); } DisCom cDisCom = new DisCom(); //DNF //(new Logger()).WriteDebug2("CPU_MERGE [high=" + bHighPriority + "][aPMs=" + aPMs.Count + "][" + aPMs[0].stArea.nWidth + "x" + aPMs[0].stArea.nHeight + "--" + aPMs[0].stArea.nLeft + ":" + aPMs[0].stArea.nTop + "][bytes=" + aPMs[0]._nBytesQty + "]"); _cTiming.Restart("before CPUm"); cDisCom.FrameMerge(cMergeInfo, aDPs, bHighPriority); _cTiming.Restart("CPU merge"); cDisCom.Dispose(); for (int nIndx = 0; nIndx < aPMsActual.Count; nIndx++) { lock (aPMsActual[nIndx]._cSyncRoot) aPMsActual[nIndx]._bProcessing = false; aPMsActual[nIndx].Dispose(); } } _cTiming.Stop("merge", "disposing", 40); } }