-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Concurrent simulation runner #946
Conversation
… share the same simulation object
@Yuri05 here just an example how it could look like (this code is for the older version and the signatures of the methods are different, but the idea from
|
And should all simulations be executed only once or multiple times with different parameter sets? |
} | ||
|
||
public IReadOnlyList<string> VariableParameterPaths { get; private set; } | ||
|
||
public IReadOnlyList<string> VariableMoleculePaths { get; private set; } | ||
|
||
public object Clone() | ||
{ | ||
var xml = _simModelExporter.ExportSimModelXml(_modelCoreSimulation, new SimulationRunOptions().SimModelExportMode); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wow.. what's going on here?😳
What are you trying to achieve? If feels like you are not working at the right abstraction level here
… for running simulations concurrently
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is looking very good. A few questions
while (data.TryDequeue(out var datum)) | ||
{ | ||
//Invoke the action on it and store the result | ||
var result = await action.Invoke(coreIndex, cancellationToken, datum); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the cancellationToken needs to be checked here I believe. It is the responsibility of the caller to throw if the action has been cancelled
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right... adding the fix now
await Task.WhenAll(tasks); | ||
//all tasks are completed. Can return results | ||
|
||
return results.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not return results? Just curious
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So we return a Task you could await. Again, from R you cannot await so the ConcurrentSimulationRunner does not use this feature but from other parts of the code you could have async/await
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not what I mean.results is already a dictionary. Why are your returning another dictionary based of this? The async/await is created for you automatically because you are using await /async in your code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To the best of my knowledge, ConcurrentDictionary and Dictionary are different classes. They do not share a common interface or anything like that. We need a ConcurrentDictionary to avoid concurrent access issues but beyond ConcurrencyManager class, we do not need it anymore so we should turn it into a Dictionary. Similarly, we use a ConcurrentQueue. I have just updated the signature so it accepts a IEnumerable instead of a ConcurrentQueue since this data structure is only needed as a robust implementation but should not be affecting the signature of the method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To the best of my knowledge, ConcurrentDictionary and Dictionary are different classes. They do not share a common interface or anything like that.
oh yeah they share IDictionary or IReadOnlyDictionary.
IReadOnlyDictionary is what we should return in that case anyways so that the caller does not mutate (at least not without explicit casting) the results
|
||
public SimulationResults[] RunConcurrently() | ||
{ | ||
var results = _concurrentManager.RunAsync<IModelCoreSimulation, SimulationResults>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should not we create the instance of ConcurrentMAnager here? why is it injected?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why creating a new object every time. Also, with injection you decouple the implementations. Whoever is using the ConcurrentSimulationRunner could decide on the implementation of the ConcurrencyManager to use by just injecting it. Why should we create the object ourselves in the code and break the inversion of control?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did not say NEW. I say ensuring that we create a. new instance here so that there is no risk of memory leaks. Typically using a factory method to retrieve a new instance in a Dispose construct. That way, if something goes wrong, we clear after ourselves.
So we do not create the object ourselves, we do not break inversion of control. We just ensure that we deal with memory leaks intrinsically by not holding references to objects
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. Sorry, I did not understood before. I will add the changes for this.
{ | ||
var results = _concurrentManager.RunAsync<IModelCoreSimulation, SimulationResults>( | ||
NumberOfCores, | ||
new System.Threading.CancellationToken(false), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what does the false do?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The cconcurrentSimulationRunner is never canceling the run since it will be used from R. I am not sure if this should be a feature (canceling the call). I am not sure either on how to do it from R in case it is possible. If this is something we should include, please tell me and I will include it. Still, the ConcurrencyManager does support canceling since it could be used from somewhere else.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My question is: What does the false do? :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initializes the state of the token as not cancelled.
if (_simulationBatches.Count > 0) | ||
return null; | ||
|
||
return Enumerable.Empty<SimulationResults>().ToArray(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Array.Empty :)
|
||
private async Task<SimulationResults> runSimulation(int coreIndex, CancellationToken cancellationToken, IModelCoreSimulation simulation) | ||
{ | ||
return await Api.GetSimulationRunner().RunAsync(new SimulationRunArgs() { Simulation = simulation, SimulationRunOptions = SimulationRunOptions }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
np need to Async away. Just return the Task
//Currently we only allow for running simulations or simulation batches, but not both | ||
if (_simulationBatches.Count > 0 && _simulations.Count > 0) | ||
//Temporal Exception. We should allow for mixing both use cases but we need to discuss first | ||
throw new Exception("You already have Simulation and SimulationBatch objects and should not mix, please invoke Clear to start adding objects from a fresh start"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Never throw Exception when coming from us. Use OSPSuiteException (unless you intend to see the whole call stack etc... Here this is clearly more for the user to see
Also error in Error files?
SimulationResults[] RunConcurrently(); | ||
} | ||
|
||
public class ConcurrentSimulationRunner : IConcurrentSimulationRunner |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would make this guy umplement IDisposable and ensure that Dispose called Clear if not done already
This is the snippet we use
private bool _disposed;
public void Dispose()
{
if (_disposed) return;
Cleanup();
GC.SuppressFinalize(this);
_disposed = true;
}
~ConcurrentSimulationRunner ()
{
Cleanup();
}
protected virtual void Cleanup()
{
}
var results = _concurrentManager.RunAsync( | ||
NumberOfCores, | ||
new CancellationToken(false), | ||
new List<IModelCoreSimulation>(_simulations), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you pass a New List?
No description provided.