Skip to content

Commit

Permalink
Updated the VTS nuget package to include parallel, added CPU count to…
Browse files Browse the repository at this point in the history
… allow parallel processing on a specified number of CPUs, updated the help
  • Loading branch information
lmalenfant committed Jun 10, 2021
1 parent def7fe5 commit 08ef669
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 100 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,4 @@ Vts.MonteCarlo.CommandLineApplication.Test/Vts.MonteCarlo.CommandLineApplication
Vts.MonteCarlo.PostProcessor/Vts.MonteCarlo.PostProcessor.csproj
Vts.MonteCarlo.PostProcessor.Test/Vts.MonteCarlo.PostProcessor.Test.csproj
.vs/
Vts.Benchmark/
61 changes: 46 additions & 15 deletions Vts.MonteCarlo.CommandLineApplication.Test/ProgramTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.IO;
using System.Reflection;
//using BenchmarkDotNet.Running;
using NUnit.Framework;
using Vts.IO;

Expand Down Expand Up @@ -34,6 +35,7 @@ public class ProgramTests
"two_layer_ROfRho",
"two_layer_ROfRho_with_db",
"voxel_ROfXAndY_FluenceOfXAndYAndZ",
"surface_fiber_detector"
};
private List<string> listOfInfilesThatRequireExistingResultsToRun = new List<string>()
{
Expand Down Expand Up @@ -392,20 +394,49 @@ public void validate_fluorescence_emission_infile_runs_successfully()
Assert.IsTrue(Directory.Exists("fluorescence_emission_AOfXAndYAndZ_source_infinite_cylinder"));
Assert.IsTrue(File.Exists("fluorescence_emission_AOfXAndYAndZ_source_infinite_cylinder/ROfXAndY"));
}
// removed because not a good way to text whether MCCL is taking longer to execute.
///// <summary>
///// Test to keep an eye on if the MC execution time is growing.
///// First test simple infile with
///// </summary>
//[Test]
//public void verify_timing_of_execution()
//{
// string[] arguments = new string[] { "infile=infile_infinite_cylinder_ROfRho_FluenceOfRhoAndZ.txt" };
// Stopwatch stopwatch = Stopwatch.StartNew();
// Program.Main(arguments);
// stopwatch.Stop();
// // verify infile gets written to output folder
// Assert.Less(stopwatch.ElapsedMilliseconds, 9000);
//}

/// <summary>
/// test to verify output folder created when parallel processing invoked
/// </summary>
[Test]
public void validate_output_folder_created_when_parallel_processing_invoked()
{
string[] arguments = new string[] // use infile that hasn't created folder in these tests
{
"infile=infile_two_layer_ROfRho.txt", "cpucount=4",
};
Program.Main(arguments);
Assert.IsTrue(Directory.Exists("two_layer_ROfRho"));
// verify infile gets written to output folder
Assert.IsTrue(File.Exists("two_layer_ROfRho/two_layer_ROfRho.txt"));
}
/// <summary>
/// test to verify cpucount gets changed to 1 if infile specifies database
/// </summary>
[Test]
public void validate_cpucount_modified_to_1_if_infile_specifies_database()
{
string[] arguments = new string[] // use infile that specifies database
{
"infile=infile_pMC_one_layer_ROfRho_DAW.txt", "cpucount=4",
};
Program.Main(arguments);
Assert.IsTrue(Directory.Exists("pMC_one_layer_ROfRho_DAW"));
}
/// <summary>
/// This test relies on the attribute [Benchmark] applied to
/// ParallelMonteCarloSimulationRunSingleInParallel()
/// Benchmark does not work with unit tests yet
/// </summary>
[Test]
public void run_Benchmark_for_timing()
{
string[] arguments = new string[] // use infile that hasn't created folder in these tests
{
"infile=infile_two_layer_ROfRho.txt", "cpucount=4",
};
//summary = BenchmarkRunner.Run<Program.Main(arguments)>();
//Console.WriteLine(summary);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
<PackageReference Include="nunit" Version="3.13.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
Expand Down
46 changes: 33 additions & 13 deletions Vts.MonteCarlo.CommandLineApplication/MonteCarloSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,29 +154,36 @@ public static ValidationResult ValidateSimulationInput(SimulationInput input)
return SimulationInputValidation.ValidateInput(input);
}

public static void RunSimulation(SimulationInput input, string outputFolderPath)
public static void RunSimulation(SimulationInput input, string outputFolderPath, int numberOfCPUs)
{
var mc = new MonteCarloSimulation(input);

// locate root folder for output, creating it if necessary
var path = string.IsNullOrEmpty(outputFolderPath)
? Path.GetFullPath(Directory.GetCurrentDirectory())
var path = string.IsNullOrEmpty(outputFolderPath)
? Path.GetFullPath(Directory.GetCurrentDirectory())
: Path.GetFullPath(outputFolderPath);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}

// locate destination folder for output, creating it if necessary
var resultsFolder = Path.Combine(path, input.OutputName);
if (!Directory.Exists(resultsFolder))
{
Directory.CreateDirectory(resultsFolder);
}

mc.SetOutputPathForDatabases(path);

SimulationOutput detectorResults = mc.Run();
SimulationOutput detectorResults;
if (numberOfCPUs > 1)
{
var parallelMC = new ParallelMonteCarloSimulation(input, numberOfCPUs);
//parallelMC.SetOutputPathForDatabases(path); // no inheritance of MonteCarloSimulation class
detectorResults = parallelMC.RunSingleInParallel();
}
else
{
var mc = new MonteCarloSimulation(input);
mc.SetOutputPathForDatabases(path);
detectorResults = mc.Run();
}

input.ToFile(Path.Combine(resultsFolder, input.OutputName + ".txt"));

Expand All @@ -187,16 +194,29 @@ public static void RunSimulation(SimulationInput input, string outputFolderPath)
}
}

///// <summary>
///// Runs multiple Monte Carlo simulations in parallel using all available CPU cores
///// </summary>
//public static void RunSimulations(IEnumerable<SimulationInput> inputs, string outputFolderPath)
//{
// var options = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount};
// Parallel.ForEach(inputs, options, (input, state, index) =>
// {
// input.Options.SimulationIndex = (int)index;
// RunSimulation(input, outputFolderPath, 1);
// });
//}

/// <summary>
/// Runs multiple Monte Carlo simulations in parallel using all available CPU cores
/// </summary>
public static void RunSimulations(IEnumerable<SimulationInput> inputs, string outputFolderPath)
public static void RunSimulations(IEnumerable<SimulationInput> inputs, string outputFolderPath, int numberOfCPUs)
{
var options = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount};
var options = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount };
Parallel.ForEach(inputs, options, (input, state, index) =>
{
input.Options.SimulationIndex = (int)index;
RunSimulation(input, outputFolderPath);
RunSimulation(input, outputFolderPath, numberOfCPUs);
});
}
}
Expand Down
Loading

0 comments on commit 08ef669

Please sign in to comment.