Advertisement
albusmw

Untitled

Apr 4th, 2020
499
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '''<summary>Class to calculate 2D matrix statistics multi-threaded.</summary>
  2. '''<remarks>Calculation is done by buidling a vector with all possible entries (only 2^16 length).</remarks>
  3. Public Class cStatMultiThread_UInt16
  4.  
  5.     Private Const OneUInt64 As UInt64 = CType(1, UInt64)
  6.  
  7.     '''<summary>The real image data.</summary>
  8.    Public ImageData(,) As UInt16
  9.  
  10.     '''<summary>Object for each thread.</summary>
  11.    Public Class cStateObj
  12.         Friend XOffset As Integer = -1
  13.         Friend YOffset As Integer = -1
  14.         Friend HistDataBayer As New Collections.Generic.Dictionary(Of Int64, UInt64)
  15.         Friend Done As Boolean = False
  16.     End Class
  17.  
  18.     '''<summary>Perform a calculation with 4 threads, one for each bayer channel.</summary>
  19.    Public Sub Calculate(ByRef Results(,) As cStateObj)
  20.  
  21.         'Data are processed
  22.        Dim StObj(3) As cStateObj
  23.         For Idx As Integer = 0 To StObj.GetUpperBound(0)
  24.             StObj(Idx) = New cStateObj
  25.         Next Idx
  26.         StObj(0).XOffset = 0 : StObj(0).YOffset = 0
  27.         StObj(1).XOffset = 0 : StObj(1).YOffset = 1
  28.         StObj(2).XOffset = 1 : StObj(2).YOffset = 0
  29.         StObj(3).XOffset = 1 : StObj(3).YOffset = 1
  30.  
  31.         'Start all threads
  32.        For Each Slice As cStateObj In StObj
  33.             System.Threading.ThreadPool.QueueUserWorkItem(New System.Threading.WaitCallback(AddressOf HistoCalc), Slice)
  34.         Next Slice
  35.  
  36.         'Join all threads
  37.        Do
  38.             'System.Threading.Thread.Sleep(1)
  39.            Dim AllDone As Boolean = True
  40.             For Each Slice As cStateObj In StObj
  41.                 If Slice.Done = False Then
  42.                     AllDone = False : Exit For
  43.                 End If
  44.             Next Slice
  45.             If AllDone Then Exit Do
  46.         Loop Until 1 = 0
  47.  
  48.         'Collect all results
  49.        ReDim Results(1, 1)
  50.         Results(0, 0) = StObj(0)
  51.         Results(0, 1) = StObj(1)
  52.         Results(1, 0) = StObj(2)
  53.         Results(1, 1) = StObj(3)
  54.  
  55.     End Sub
  56.  
  57.     '''<summary>Histogramm calculation itself - the histogram of one bayer channel is calculated.</summary>
  58.    Private Sub HistoCalc(ByVal Arguments As Object)
  59.  
  60.         Dim StateObj As cStateObj = CType(Arguments, cStateObj)
  61.         StateObj.Done = False
  62.  
  63.         'Init count object with 0
  64.        Dim HistCount(UInt16.MaxValue) As UInt64
  65.         For Idx As Integer = 0 To HistCount.GetUpperBound(0)
  66.             HistCount(Idx) = 0
  67.         Next Idx
  68.  
  69.         'Count one bayer part
  70.        Dim XOffsets As New Collections.Generic.List(Of Integer)
  71.         For IdxX As Integer = StateObj.XOffset To ImageData.GetUpperBound(0) - 1 + StateObj.XOffset Step 2
  72.             XOffsets.Add(IdxX)
  73.         Next IdxX
  74.         Threading.Tasks.Parallel.ForEach(XOffsets, Sub(IdxX)
  75.                                                        For IdxY As Integer = StateObj.YOffset To ImageData.GetUpperBound(1) - 1 + StateObj.YOffset Step 2
  76.                                                            HistCount(ImageData(IdxX, IdxY)) += OneUInt64
  77.                                                        Next IdxY
  78.                                                    End Sub)
  79.  
  80.         'Form return value
  81.        StateObj.HistDataBayer = New Collections.Generic.Dictionary(Of Int64, UInt64)
  82.         For Idx As UInt16 = 0 To CUShort(HistCount.GetUpperBound(0))
  83.             If HistCount(Idx) > 0 Then StateObj.HistDataBayer.Add(Idx, HistCount(Idx))
  84.             If Idx = HistCount.GetUpperBound(0) Then Exit For
  85.         Next Idx
  86.  
  87.         StateObj.Done = True
  88.  
  89.     End Sub
  90.  
  91. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement