Forum Discussion

dipawar's avatar
dipawar
New Contributor II
2 years ago

Parallel Processing in Business Rule

How to improve the business rule performance using Parallel.ForEach() in OneStream. 

  • ChrisLoran's avatar
    ChrisLoran
    Valued Contributor

    I would be very careful before jumping into using Parallel.ForEach(), without considering the context/background.

    For example, you might think of using Parallel.ForEach() over a list of Accounts or UD members : But you shouldn't be doing iterations over these sorts of members; should instead be using a DataBuffer. 

    One might think about doing a Parallel.ForEach() across the cells in a DataBuffer.  But a DataBuffer is an implementation of the .NET Dictionary object, with is not Multi-Thread safe. 

    So the largest use-case I can see for doing Parallel.ForEach() is not in a Finance BR, but in an Extender rule, where either
    a) you want to go through a DataTable , returned from an SQL query , and process the rows in parallel
    or
    b) you want to go through a list of Data Units, (or list of Entities for example) , and for each data unit, execute a CustomFinanceBusinessRule in parallel for these data units.

    Here's an example on how you would process a DataTable, processing multiple rows in parallel:

    * Note : You would need to add the line:
    Imports System.Threading.Tasks
    to your BR

     

    Dim strSQL As String = "SELECT * FROM <mytable>"
    Dim dbConnApp As DbConnInfoApp = BRApi.Database.CreateApplicationDbConnInfo(si)
    Using myTable As DataTable = BRApi.Database.ExecuteSql(dbConnApp, strSQL, False)
    	Dim myOptions As ParallelOptions = New ParallelOptions() With { .MaxDegreeOfParallelism = Environment.ProcessorCount}
    	Parallel.ForEach(myTable.AsEnumerable(), myOptions, Sub(currentRow As DataRow)
                                                   Me.ProcessRowParallel(si, currentRow)
                                               End Sub)
    End Using

     

     

     

    • dipawar's avatar
      dipawar
      New Contributor II

      If I want to apply the above logic for finance business rule type. Will it work?