Safiron

Test parallelism

Feb 24th, 2017
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.26 KB | None | 0 0
  1.         private void UpdateFrame()
  2.         {
  3.             frameView.DisplayNewFrame(acquisition.frameDataShorts,
  4.                     mfvm.cameras[openCamIndex].sensorWidth,
  5.                     mfvm.cameras[openCamIndex].sensorHeight,
  6.                     mfvm.cameras[openCamIndex].bitDepth,
  7.                     acquisition.stream_size, false);
  8.            
  9.             frameView.fvvm.frameNumApp = (UInt16)acquisition.frameInfoLatest.FrameNr;
  10.  
  11.             List<Tuple<UInt16, UInt16, UInt64>> taskResults =
  12.                     new List<Tuple<UInt16, UInt16, UInt64>>();
  13.  
  14.             Tuple<UInt16, UInt16, UInt64> result = null;
  15.  
  16.             // Count cores
  17.             int coreCount = 4;
  18.  
  19.             // Calculating segment size
  20.             int bufferSegmentSize = acquisition.frameDataShorts.Length / coreCount;
  21.             // Creating Tasks
  22.             Task[] tasks = new Task[coreCount];
  23.             Object myLock = new Object();
  24.  
  25.             Stopwatch sw = Stopwatch.StartNew();
  26.  
  27.             for (int i = 0; i < tasks.Length; i++)
  28.             {
  29.                 int j = i;
  30.                 tasks[j] = Task.Factory.StartNew(
  31.                     () =>
  32.                     {
  33.                         lock (myLock)
  34.                         {
  35.                             taskResults.Add(GetSubFrameStats(j * bufferSegmentSize,
  36.                                 (j + 1) * bufferSegmentSize));
  37.                         }
  38.                     });
  39.             }
  40.             Task.WaitAll(tasks);
  41.  
  42.             result = CompareSubFrameStats(taskResults);
  43.             sw.Stop();
  44.  
  45.  
  46.             frameView.fvvm.frameMin = result.Item1;
  47.             frameView.fvvm.frameMax = result.Item2;
  48.             frameView.fvvm.frameMean = (UInt16)(result.Item3 /
  49.                     (UInt64)acquisition.frameDataShorts.Length);
  50.  
  51.         }
  52.  
  53.         private Tuple<UInt16, UInt16, UInt64> CompareSubFrameStats(
  54.                 List<Tuple<UInt16, UInt16, UInt64>> stats)
  55.         {
  56.             UInt16 tempMin = (UInt16)(Math.Pow(2, mfvm.cameras[openCamIndex].bitDepth) - 1);
  57.             UInt16 tempMax = 0;
  58.             UInt64 tempSum = 0;
  59.  
  60.             for (int i = 0; i < stats.Count; i++)
  61.             {
  62.                 if (stats[i].Item1 < tempMin)
  63.                     tempMin = stats[i].Item1;
  64.  
  65.                 if (stats[i].Item2 > tempMax)
  66.                     tempMax = stats[i].Item2;
  67.  
  68.                 tempSum += stats[i].Item3;
  69.             }
  70.  
  71.             return new Tuple<UInt16, UInt16, UInt64>(tempMin, tempMax, tempSum);
  72.         }
  73.  
  74.         private Tuple<UInt16, UInt16, UInt64> GetSubFrameStats(int startIndex, int endIndex)
  75.         {
  76.             UInt16 tempMin = (UInt16)(Math.Pow(2, mfvm.cameras[openCamIndex].bitDepth) - 1);
  77.             UInt16 tempMax = 0;
  78.             UInt64 tempSum = 0;
  79.  
  80.             for (int i = startIndex; i < endIndex; i++)
  81.             {
  82.                 if (acquisition.frameDataShorts[i] < tempMin)
  83.                     tempMin = acquisition.frameDataShorts[i];
  84.  
  85.                 if (acquisition.frameDataShorts[i] > tempMax)
  86.                     tempMax = acquisition.frameDataShorts[i];
  87.  
  88.                 tempSum += acquisition.frameDataShorts[i];
  89.             }
  90.  
  91.             return new Tuple<UInt16, UInt16, UInt64>(tempMin, tempMax, tempSum);
  92.         }
Advertisement
Add Comment
Please, Sign In to add comment