/// <summary> /// Actual run all of the requested trials from all of the specified settings files /// </summary> protected void DoRunTrials() { // arrays to hold currently running threads and trials Thread[] RunningThreads = new Thread[NThreads]; cModelTrial[] RunningTrials = new cModelTrial[NThreads]; try { // build list of run definitions RunDefinitions = new List <DataSet>(); for (int i = 0; i < mvarRunDefinitions.Count; i++) { //EER Create list of settings data from the XML settings file DataSet runDS = ReadSettingsFromXML(mvarRunDefinitions[i]); /* * //EER * //Use this to print out each row item in the datables * StringBuilder sb = new StringBuilder(); * //int nTables = runDS.Tables.Count; * string tableName = "Combined_Strategies"; * DataTable table = runDS.Tables[tableName]; * { * * //Column Data * System.Diagnostics.Debug.WriteLine(""); * int nCol = runDS.Tables[tableName].Columns.Count; * System.Diagnostics.Debug.WriteLine("Number of columns in " + tableName + " = " + nCol); * for (int m = 0; m < nCol; m++) * { * DataColumn dscol = runDS.Tables[tableName].Columns[m]; * int maxL = dscol.MaxLength; * string name = dscol.ColumnName; * System.Diagnostics.Debug.WriteLine("name = " + name + "; maxLength = " + maxL); * } * * //Row data * int nRows = runDS.Tables[tableName].Rows.Count; * System.Diagnostics.Debug.WriteLine(""); * System.Diagnostics.Debug.WriteLine("Number of rows in " + tableName + " = " + nRows); * for (int m = 0; m < nRows; m++) * { * DataRow dsrow = runDS.Tables[tableName].Rows[m]; * object[] arr = dsrow.ItemArray; * //int arrL = arr.Length; * //System.Diagnostics.Debug.WriteLine("arrL = " + arrL); * for (int j = 0; j < arr.Length; j++) * { * string rowitem = Convert.ToString(arr[j]); * System.Diagnostics.Debug.WriteLine("Row item " + j + " = " + rowitem); * } * } * System.Diagnostics.Debug.WriteLine(""); * } * //EER */ if (runDS != null) { DataRow drRunInfo = runDS.Tables["RunInfo"].Rows[0]; //EER /*DataRow dsrow = runDS.Tables["RunInfo"].Rows[0]; * object[] arr = dsrow.ItemArray; * for (int j = 0; j < arr.Length; j++) * { * string rowitem = Convert.ToString(arr[j]); * System.Diagnostics.Debug.WriteLine(" Row item " + j + " = " + rowitem); * }*/ DataRow drUserInfo = runDS.Tables["UserInfo"].Rows[0]; // adjust values in dataset if user wishes to override drRunInfo.BeginEdit(); if (!string.IsNullOrEmpty(this.RunName)) { if (mvarRunDefinitions.Count == 1) { drRunInfo["RunName"] = this.RunName; } } if (this.NTrials != null) { drRunInfo["NTrials"] = this.NTrials; } if (this.AbortOnRabiesDieOut != null) { drRunInfo["AbortOnRabiesDieOut"] = this.AbortOnRabiesDieOut; } if (this.RunLength != null) { drRunInfo["RunLength"] = this.RunLength; } if (this.UseRandomSeed != null) { drRunInfo["UseRandomSeed"] = this.UseRandomSeed; } if (this.RandomSeed != null) { drRunInfo["RandomSeed"] = this.RandomSeed; } drRunInfo.AcceptChanges(); drUserInfo.BeginEdit(); if (!string.IsNullOrEmpty(this.RunPerson)) { drUserInfo["UserName"] = this.RunPerson; } if (!string.IsNullOrEmpty(this.RunAffiliation)) { drUserInfo["UserOrganization"] = this.RunAffiliation; } if (!string.IsNullOrEmpty(this.RunContactInfo)) { drUserInfo["UserContactInfo"] = this.RunContactInfo; } drUserInfo.AcceptChanges(); // add dataset to list of RunDefinitions RunDefinitions.Add(runDS); } } // make sure output folder exists if (!string.IsNullOrEmpty(this.OutputFolder)) { if (this.OutputFolder != "!-current-!" && !Directory.Exists(this.OutputFolder)) { Directory.CreateDirectory(this.OutputFolder); } } // OK, we are now ready to begin building the threads for each trial we intend to run ModelTrials = new List <cModelTrial>(); TrialThreads = new List <Thread>(); for (int i = 0; i < RunDefinitions.Count; i++) { DataRow drRunInfo = RunDefinitions[i].Tables["RunInfo"].Rows[0]; //EER //Check what infor is in RunDefinitions[i].Tables["RunInfo"].Rows[0] /* * StringBuilder sb = new StringBuilder(); * object[] arr = drRunInfo.ItemArray; * for (int j = 0; j < arr.Length; j++) * { * sb.Append(Convert.ToString(arr[j])); * sb.Append("|"); * } * System.Diagnostics.Debug.WriteLine("i = " + i); * System.Diagnostics.Debug.WriteLine("RunInfo = " + sb.ToString()); * System.Diagnostics.Debug.WriteLine(""); */ //EER //EER /* * //How many tables in RunDefinitions[0]? * int numbTables = RunDefinitions[i].Tables.Count; * System.Diagnostics.Debug.WriteLine("Number of tables in RunDefinitions = " + numbTables); * //What are their names? and what is contained within? * // Print each table's name: * foreach (DataTable table in RunDefinitions[i].Tables) * { * System.Diagnostics.Debug.WriteLine(" Table name = " + table.TableName); * } */ int ntrials = Convert.ToInt32(drRunInfo["NTrials"]); bool userandomseed = Convert.ToBoolean(drRunInfo["UseRandomSeed"]); long randomseed = Convert.ToInt64(drRunInfo["RandomSeed"]); string RunName = drRunInfo["RunName"].ToString(); if (this.SeedFromClock != null) { if ((bool)this.SeedFromClock) { userandomseed = false; } } if (userandomseed) { cUniformRandom.SetSeed(randomseed); } else { cUniformRandom.SetSeed(); } //EER /* * //Confirm values * System.Diagnostics.Debug.WriteLine("ntrials = " + ntrials); * System.Diagnostics.Debug.WriteLine("userandomseed = " + userandomseed); * System.Diagnostics.Debug.WriteLine("randomseed = " + randomseed); * System.Diagnostics.Debug.WriteLine("RunName = " + RunName); * //ntrials = 1 * //userandomseed = False * //randomseed = 5344 * //RunName = testrun_rabies_wCell80km2L50W20K50_weekly_mvt */ // EER:cModelTrial takes in all 39 tables from the RunDefinitions[i] for (int j = 0; j < ntrials; j++) { // create the actual trial cModelTrial mt = new cModelTrial(RunDefinitions[i], mvarRunPerson, RunName, j + this.FirstTrialNumber, mvarAppReport, "", new cUniformRandom(), mvarIsUnix, mvarNoOutputExceptions); // set file names etc. if (!string.IsNullOrEmpty(this.SummaryFileName)) { mt.SummaryFileName = this.SummaryFileName; } if (!string.IsNullOrEmpty(this.RabiesFileName)) { mt.RabiesFileName = this.RabiesFileName; } mt.TabDelimitedOutput = this.TabDelimitedOutput; mt.NoAnimalDatabases = this.NoAnimalDatabases; // set input and output paths if these are set for the batch runner if (!string.IsNullOrEmpty(this.InputFolder)) { mt.InputFolder = this.InputFolder; } if (!string.IsNullOrEmpty(this.OutputFolder)) { mt.OutputFolder = this.OutputFolder; } mt.PrintYears = this.PrintYears; mt.PrintWeeks = (this.NThreads == 1 ? this.PrintWeeks : false); mt.ConfirmDatasources = this.ConfirmDatasources; // EER: Not all of the above updates from form data are active e.g. SummaryFileName string log = OutputFolder; // create thread for trial ModelTrials.Add(mt); TrialThreads.Add(new Thread(mt.RunModelTrial)); } } // now we have the threads defined, lets actually run individual threads int ThreadCounter = 0; while (ThreadCounter < TrialThreads.Count) { for (int i = 0; i < NThreads; i++) { // has this thread stopped if (RunningThreads[i] != null) { ThreadState tState = RunningThreads[i].ThreadState; if (tState == ThreadState.Stopped || tState == ThreadState.Aborted) { // the thread has stopped // raise an event to indicate this if (TrialCompleted != null) { TrialCompleted(RunningTrials[i].RunName, RunningTrials[i].TrialNumber, i, 100); } // set the thread to null RunningTrials[i].TrialProgress -= new cModelTrial.TrialProgessEvent(cBatchRunner_TrialProgress); RunningThreads[i] = null; } } if (RunningThreads[i] == null && ThreadCounter < TrialThreads.Count) { // start a thread in this position RunningThreads[i] = TrialThreads[ThreadCounter]; RunningTrials[i] = ModelTrials[ThreadCounter]; // attach event handler to the running trial RunningTrials[i].ThreadNumber = i; RunningTrials[i].TrialProgress += new cModelTrial.TrialProgessEvent(cBatchRunner_TrialProgress); // raise an event to indicate the start if (TrialStarted != null) { TrialStarted(RunningTrials[i].RunName, RunningTrials[i].TrialNumber, i, 0); } // start the thread RunningThreads[i].Start(); // increment the ThreadCounter ThreadCounter++; } } // now stop for 2 seconds Thread.Sleep(2000); } // now just let the various still running threads finish up bool ThreadsRunning = true; while (ThreadsRunning) { ThreadsRunning = false; for (int i = 0; i < NThreads; i++) { if (RunningThreads[i] != null) { ThreadState tState = RunningThreads[i].ThreadState; if (tState != ThreadState.Stopped && tState != ThreadState.Aborted) { ThreadsRunning = true; } else { // raise event for the finish of this thread if (TrialCompleted != null) { TrialCompleted(RunningTrials[i].RunName, RunningTrials[i].TrialNumber, i, 100); } RunningTrials[i].TrialProgress -= new cModelTrial.TrialProgessEvent(cBatchRunner_TrialProgress); } } } Thread.Sleep(2000); } // finally reset to non-running mode mvarIsRunning = false; } catch (ThreadAbortException) { // stop all running threads for (int i = 0; i < this.NThreads; i++) { if (RunningThreads[i] != null) { if (RunningThreads[i].ThreadState == ThreadState.Running) { RunningThreads[i].Abort(); } } } mvarIsRunning = false; } catch (Exception) { mvarIsRunning = false; throw; } }
/// <summary> /// EER: Actual run all of the requested trials from a settings files defined by the Excel template (i.e. ARM) /// </summary> protected void DoRunTrialsExcelInput() { //System.Diagnostics.Debug.WriteLine(""); //System.Diagnostics.Debug.WriteLine("cBatchRunner.cs: DoRunTrialsExcelInput()"); // arrays to hold currently running threads and trials Thread[] RunningThreads = new Thread[NThreads]; cModelTrial[] RunningTrials = new cModelTrial[NThreads]; try { //EER int cnt = mvarRunDefinitions.Count; //System.Diagnostics.Debug.WriteLine(" mvarRunDefinitions.Count = " + cnt); // Define dataset of run definitions DataSet runDS = cCSVSettingsTemplate.Parse(mvarRunDefinitions[0]); //Build the threads for running trials ModelTrials = new List <cModelTrial>(); TrialThreads = new List <Thread>(); int ntrials = Convert.ToInt32(runDS.Tables[4].Rows[12][1]); bool userandomseed = Convert.ToBoolean(runDS.Tables[4].Rows[9][1]); long randomseed = Convert.ToInt64(runDS.Tables[4].Rows[10][1]); string RunName = runDS.Tables[4].Rows[0][1].ToString(); mvarRunPerson = runDS.Tables[4].Rows[16][1].ToString(); //System.Diagnostics.Debug.WriteLine(" ntrials = " + ntrials); //System.Diagnostics.Debug.WriteLine(" userandomseed = " + userandomseed); //System.Diagnostics.Debug.WriteLine(" randomseed = " + randomseed); //System.Diagnostics.Debug.WriteLine(" RunName = " + RunName); //System.Diagnostics.Debug.WriteLine(" mvarRunPerson = " + mvarRunPerson); //EER Set the randomseed //If the user wants to use a random seed, than define the seed per input settings (i.e. randomseed) // otherwise, randomly set the seed if (userandomseed) { cUniformRandom.SetSeed(randomseed); long seed = cUniformRandom.Seed; //System.Diagnostics.Debug.WriteLine(" user defined seed = " + seed); } else { cUniformRandom.SetSeed(); long seed = cUniformRandom.Seed; //System.Diagnostics.Debug.WriteLine(" random seed = " + seed); } //EER The first argument in cModelTrial is for the running settings for (int j = 0; j < ntrials; j++) { // create the actual trial // EER: arguments for cModelTrial // runDS = excel template; will need to define how to use data in cModelTrial // mvarRunPerson, RunName are defined in DoRunTrialsInput() // this.FirstTrialNumber, mvarAppReport, mvarIsUnix, mvarNoOutputExceptions are defined from creating the cBatchRunner object in Form1 // "" there is not template database defined cModelTrial mt = new cModelTrial(runDS, mvarRunPerson, RunName, j + this.FirstTrialNumber, mvarAppReport, "", new cUniformRandom(), mvarIsUnix, mvarNoOutputExceptions); // set file names etc. //if (!string.IsNullOrEmpty(this.SummaryFileName)) mt.SummaryFileName = this.SummaryFileName; //if (!string.IsNullOrEmpty(this.RabiesFileName)) mt.RabiesFileName = this.RabiesFileName; //mt.TabDelimitedOutput = this.TabDelimitedOutput; //mt.NoAnimalDatabases = this.NoAnimalDatabases; // set input and output paths if these are set for the batch runner //if (!string.IsNullOrEmpty(this.InputFolder)) mt.InputFolder = this.InputFolder; //if (!string.IsNullOrEmpty(this.OutputFolder)) mt.OutputFolder = this.OutputFolder; //mt.PrintYears = this.PrintYears; //mt.PrintWeeks = (this.NThreads == 1 ? this.PrintWeeks : false); //mt.ConfirmDatasources = this.ConfirmDatasources; // EER //I probably don't need the above data //I can create the mt //Then a new RunModelTrialExcelTemplate to populate the model data with the settins and run // create thread for trial ModelTrials.Add(mt); TrialThreads.Add(new Thread(mt.RunModelTrialExcelTemplate)); } // now we have the threads defined, lets actually run individual threads // EER This is the section of code that actually uses RunModelTrialExcelTemplate from cModelTrial int ThreadCounter = 0; while (ThreadCounter < TrialThreads.Count) { for (int i = 0; i < NThreads; i++) { // has this thread stopped if (RunningThreads[i] != null) { ThreadState tState = RunningThreads[i].ThreadState; if (tState == ThreadState.Stopped || tState == ThreadState.Aborted) { // the thread has stopped // raise an event to indicate this if (TrialCompleted != null) { TrialCompleted(RunningTrials[i].RunName, RunningTrials[i].TrialNumber, i, 100); } // set the thread to null RunningTrials[i].TrialProgress -= new cModelTrial.TrialProgessEvent(cBatchRunner_TrialProgress); RunningThreads[i] = null; } } if (RunningThreads[i] == null && ThreadCounter < TrialThreads.Count) { // start a thread in this position RunningThreads[i] = TrialThreads[ThreadCounter]; RunningTrials[i] = ModelTrials[ThreadCounter]; // attach event handler to the running trial RunningTrials[i].ThreadNumber = i; RunningTrials[i].TrialProgress += new cModelTrial.TrialProgessEvent(cBatchRunner_TrialProgress); // raise an event to indicate the start if (TrialStarted != null) { TrialStarted(RunningTrials[i].RunName, RunningTrials[i].TrialNumber, i, 0); } // start the thread RunningThreads[i].Start(); // increment the ThreadCounter ThreadCounter++; } } // now stop for 2 seconds Thread.Sleep(2000); } // now just let the various still running threads finish up bool ThreadsRunning = true; while (ThreadsRunning) { ThreadsRunning = false; for (int i = 0; i < NThreads; i++) { if (RunningThreads[i] != null) { ThreadState tState = RunningThreads[i].ThreadState; if (tState != ThreadState.Stopped && tState != ThreadState.Aborted) { ThreadsRunning = true; } else { // raise event for the finish of this thread if (TrialCompleted != null) { TrialCompleted(RunningTrials[i].RunName, RunningTrials[i].TrialNumber, i, 100); } RunningTrials[i].TrialProgress -= new cModelTrial.TrialProgessEvent(cBatchRunner_TrialProgress); } } } Thread.Sleep(2000); } // finally reset to non-running mode mvarIsRunning = false; } catch (ThreadAbortException) { // stop all running threads for (int i = 0; i < this.NThreads; i++) { if (RunningThreads[i] != null) { if (RunningThreads[i].ThreadState == ThreadState.Running) { RunningThreads[i].Abort(); } } } mvarIsRunning = false; } catch (Exception) { mvarIsRunning = false; throw; } }