public Dictionary <long, List <ResultPosition> > ProcessPeriod( int PeriodID, List <Dictionary <string, Dictionary <int, Dictionary <long, Tuple <double, uint, List <float> > > > > > PeriodOfEventOccurrenceDRs, bool ShouldAllocate, int MaxConcurrencyContracts, params long[] ContractIDs) { ConcurrentDictionary <long, List <ResultPosition> > ContractIdToResultMap = new ConcurrentDictionary <long, List <ResultPosition> >(); #region Recursion List <long> IndependentContractIDs = GetIndependentContractIDs(ContractIDs); //int LevelConcurrency = Environment.ProcessorCount - 1; //int BatchSize = 100; //int TotalSize = IndependentContractIDs.Count; //Parallel.For(0, ((TotalSize / BatchSize) + 1), new ParallelOptions { MaxDegreeOfParallelism = MaxConcurrencyContracts }, i => Parallel.ForEach(IndependentContractIDs, new ParallelOptions { MaxDegreeOfParallelism = MaxConcurrencyContracts }, id => //UNCOMMENT FOR CONTRACTS IN PARALLEL //foreach (long id in IndependentContractIDs) // UNCOMMENT FOR CONTRACTS IN SEQUENCE { //int BatchUpper = ((i + 1) * BatchSize); //for (int j = i * BatchSize; j < BatchUpper && j < TotalSize; j++) //{ //long id = IndependentContractIDs[j]; var ced = GetContractData(id); if (!(ced.state == ProcessState.ContractGraphBuilt || ced.state == ProcessState.FMExecuted || ced.state == ProcessState.FMFailed)) { Console.WriteLine("Contract with ID : " + id + " is invalid and is not executable!"); //Logger.LogInfoFormatExt("Contract with ID : " + id + " is invalid and is not executable!"); return; } if (ced is PrimaryContractExposureData) { ced = new PrimaryContractExposureData((PrimaryContractExposureData)ced); } else { ced = new TreatyContractExposureData((TreatyContractExposureData)ced); } List <ResultPosition> Result = ProcessPeriodRecursor(PeriodID, ContractIdToResultMap, PeriodOfEventOccurrenceDRs, ced, ShouldAllocate); if (null != Result) { ContractIdToResultMap.TryAdd(id, Result); } //} } );//UNCOMMENT FOR CONTRACTS IN PARALLEL return(ContractIdToResultMap.ToDictionary(kvp => kvp.Key, kvp => kvp.Value)); #endregion }
private List <ResultPosition> ProcessPeriodRecursor( int PeriodID, ConcurrentDictionary <long, List <ResultPosition> > ContractIdToResultMap, List <Dictionary <string, Dictionary <int, Dictionary <long, Tuple <double, uint, List <float> > > > > > PeriodOfEventOccurrenceDRs, ContractExposureData node, bool ShouldAllocate) { if (node is TreatyContractExposureData) { #region Recursion var childrenIDs = ((TreatyContractExposureData)node).ChildContractExposureIDs; foreach (long id in childrenIDs) { var ced = GetContractData(id); if (ced is PrimaryContractExposureData) { ced = new PrimaryContractExposureData((PrimaryContractExposureData)ced); } else { ced = new TreatyContractExposureData((TreatyContractExposureData)ced); } List <ResultPosition> Result = ProcessPeriodRecursor(PeriodID, ContractIdToResultMap, PeriodOfEventOccurrenceDRs, ced, true); if (null != Result) { ContractIdToResultMap.TryAdd(id, Result); } } #endregion } if ((node.state == ProcessState.ContractGraphBuilt || node.state == ProcessState.FMExecuted || node.state == ProcessState.FMFailed)) { return(node.ExecuteFM(PeriodOfEventOccurrenceDRs, ShouldAllocate, ContractIdToResultMap.ToDictionary(kvp => kvp.Key, kvp => kvp.Value))); } return(null); }