public static List <List <ProcessOutput> > process(SimArguments args) { ParallelizationManager trdMgr = new ParallelizationManager(args); List <List <ProcessOutput> > res = new List <List <ProcessOutput> >(); Parallel.For(0, args.argFiles.Count, trdMgr.parallelArgFiles, fileIdx => { ArgFile af = args.argFiles[fileIdx]; AppSettings.WriteLogLine("Processing Arg File:" + af.FileName); // load graph file AGameGraph graph = null; AppSettings.WriteLogLine("loading graph..."); string graphVal = af.processParams[0][AppConstants.AppArgumentKeys.GRAPH_FILE.key]; Utils.Exceptions.ConditionalTryCatch <Exception>(() => { if (graphVal.EndsWith(FileExtensions.GRAPH_FILE)) { graph = AGameGraph.loadGraph(File.ReadAllLines(FileUtils.TryFindingFile(graphVal))); //graph = new GridGameGraph(af.processParams[0][AppConstants.AppArgumentKeys.GRAPH_FILE.key]); } else { graph = AGameGraph.loadGraph(graphVal); } }, (Exception ex) => { AppSettings.WriteLogLine("couldn't load graph file:" + ex.Message); }); AppSettings.WriteLogLine("graph loaded. processing..."); // populate output table (ValuesCount is the amount of different values from param file, each should have separate theory/sim/optimizer process) res.Add(AlgorithmUtils.getRepeatingValueList <ProcessOutput>(af.processParams.ValuesCount)); populateResults(res.Last(), af.processParams, trdMgr.parallelArgValues, trdMgr.parallelSimRuns, graph); }); return(res); }
public ParallelizationManager(SimArguments args) { parallelArgFiles.MaxDegreeOfParallelism = args.maxParallelArgFiles; int maxSimRunThreadCount = 1; // max allowed thread count of ALL given arg files foreach (var argFile in args.argFiles) { int argThreads; try { argThreads = Int32.Parse(AppConstants.AppArgumentKeys.THREAD_COUNT.tryRead(argFile.processParams[0])); } catch (Exception ex) { throw new Exception("couldn't parse field AppConstants.AppArgumentKeys.THREAD_COUNT from arg file:" + argFile.FileName); } maxSimRunThreadCount = Math.Max(maxSimRunThreadCount, argThreads); } if (args.argFiles.Count >= maxSimRunThreadCount * 2) { // if outer loop may be parallelized well enough, we use only it - it's the most efficient way for parallelization, perhaps because we minimize concurrent mem. allocations parallelArgValues.MaxDegreeOfParallelism = maxSimRunThreadCount; parallelSimRuns.MaxDegreeOfParallelism = 1; } else if (args.argFiles.Count == 1) { parallelArgValues.MaxDegreeOfParallelism = 1; parallelSimRuns.MaxDegreeOfParallelism = maxSimRunThreadCount; } else { // in this case, we might use extra threads (might be a problem if we want to use all cpu cores except for 1) parallelArgValues.MaxDegreeOfParallelism = 2; parallelSimRuns.MaxDegreeOfParallelism = maxSimRunThreadCount / 2; } }
static void Main(string[] argv) { SimArguments args = new SimArguments(argv); if (args.cmdLineOnly) { AppSettings.setLogWriteFunc((string s) => Console.WriteLine(s), args.verboseLvl > 0); foreach (string s in args.errorLog) { AppSettings.WriteLogLine(s); } Utils.Exceptions.ConditionalTryCatch <Exception>(() => { var resultsPerArgFile = SimProcess.process(args); for (int i = 0; i < resultsPerArgFile.Count; ++i) { int repetetionCount = int.Parse(AppConstants.AppArgumentKeys.SIMULATION_REPETETION_COUNT. tryRead(args.argFiles[i].processParams[0])); string outFolder = AppConstants.AppArgumentKeys.OUTPUT_FOLDER. tryRead(args.argFiles[i].processParams[0]); string filename = args.argFiles[i].FileName.Split(new char[] { '\\' }).Last(); filename = filename.Substring(0, filename.LastIndexOf(".")); filename = filename.Split(new char[] { '\\', '/' }).Last(); string processFileName = outFolder + "\\" + filename + "_rep" + repetetionCount.ToString() + "." + AppConstants.FileExtensions.PROCESS_OUTPUT; string theoryFileName = outFolder + "\\" + filename + "_thry" + "." + AppConstants.FileExtensions.PROCESS_OUTPUT; string optimizerFileName = outFolder + "\\" + filename + "_optimizer" + "." + AppConstants.FileExtensions.PROCESS_OUTPUT; FileUtils.writeTable(toTable(resultsPerArgFile[i], (ProcessOutput p) => { return(p.processOutput); }), processFileName); FileUtils.writeTable(toTable(resultsPerArgFile[i], (ProcessOutput p) => { return(p.theoryOutput); }), theoryFileName); FileUtils.writeTable(toTable(resultsPerArgFile[i], (ProcessOutput p) => { return(p.optimizerOutput); }), optimizerFileName); } }, (Exception ex) => { AppSettings.WriteLogLine("Process failed due to an exception in: "); AppSettings.WriteLogLine(ex.StackTrace); AppSettings.WriteLogLine("\nWith Message :"); AppSettings.WriteLogLine(ex.Message); }); return; } // if graphical: Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Utils.Exceptions.ConditionalTryCatch <Exception>(() => { Application.Run(new AdvRoutingPublicMain()); }, (Exception ex) => { string inner = ""; if (ex.InnerException != null) { inner = ex.InnerException.Message + "," + ex.InnerException.StackTrace; } MessageBox.Show(ex.Message + " , " + ex.StackTrace + ", inner exception:" + inner); } ); }