Skip to content
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 batch #513

Merged
merged 28 commits into from
May 10, 2021
Merged

Concurrent simulation batch #513

merged 28 commits into from
May 10, 2021

Conversation

PavelBal
Copy link
Member

@PavelBal PavelBal commented May 5, 2021

No description provided.

@PavelBal PavelBal requested a review from msevestre May 5, 2021 12:31
@PavelBal
Copy link
Member Author

PavelBal commented May 5, 2021

Here is a proposal how to use simulation batches:

  1. Create SimulationBatch(es), defining the simulation to run and parameter and initial values to vary
sim1 <- loadTestSimulation("simple", loadFromCache = TRUE)
sim2 <- loadTestSimulation("simple2", loadFromCache = TRUE)
parameters <- c("Organism|Liver|Volume", "R1|k1")
molecules <- "Organism|Liver|A"

simulationBatch1 <- createSimulationBatch(simulation = sim1,
    parametersOrPaths = parameters,
    moleculesOrPaths = molecules)
simulationBatch2 <- createSimulationBatch(simulation = sim2,
    parametersOrPaths = parameters,
    moleculesOrPaths = molecules)
  1. Add run values to simulation batches. Currently, only one ran value set can be added per method call - if required, it could be extended to passing multiple parameter and initial values sets. Every added run values set gets an ID that can be later be used to get the right result.
#Ids of run values
ids <- c()
ids[[1]] <- simulationBatch1$addRunValues(parameterValues = c(1, 2), initialValues = 1)
ids[[2]] <- simulationBatch1$addRunValues(parameterValues = c(1.6, 2.4), initialValues = 3)
ids[[3]] <- simulationBatch2$addRunValues(parameterValues = c(4, 2), initialValues = 4)
ids[[4]] <- simulationBatch2$addRunValues(parameterValues = c(2.6, 4.4), initialValues = 5)
  1. Run the batches. The results are grouped by the simulation batches (i.e., the output is a list with a list of SimulationResult per batch). Every entry of SimulationResult is named by the ID of of run values set.

res <- runSimulationBatches(simulationBatches = list(simulationBatch1, simulationBatch2)) resultsOfFirstBatch <- res[[1]] resultsOfSecondBatch <- res[[2]]

  1. Running the batches removes all added run values, so next run would result in no results.

R/error-checks.R Outdated Show resolved Hide resolved
R/messages.R Show resolved Hide resolved
@msevestre
Copy link
Member

@PavelBal
Copy link
Member Author

PavelBal commented May 5, 2021

@msevestre Yes, it is using the old Core... And if I replace the libraries, it fails because of PK-Sim.

@msevestre
Copy link
Member

ok let me check this out

@msevestre
Copy link
Member

I am update the lib now to make sure we are using the right version of everything. Let' see

@msevestre
Copy link
Member

I was trying to fix this and it was not letting me push lol.. I understand now

@codecov-commenter
Copy link

codecov-commenter commented May 5, 2021

Codecov Report

Merging #513 (af34c0a) into develop (46864ad) will increase coverage by 0.06%.
The diff coverage is 90.41%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #513      +/-   ##
===========================================
+ Coverage    88.37%   88.43%   +0.06%     
===========================================
  Files           72       72              
  Lines         1359     1418      +59     
===========================================
+ Hits          1201     1254      +53     
- Misses         158      164       +6     
Impacted Files Coverage Δ
R/get-net-task.R 100.00% <ø> (ø)
R/utilities-pk-parameter.R 100.00% <ø> (ø)
R/error-checks.R 92.72% <62.50%> (-3.57%) ⬇️
R/simulation-batch.R 88.23% <77.77%> (-11.77%) ⬇️
R/utilities-simulation.R 97.81% <96.22%> (-1.03%) ⬇️
R/messages.R 100.00% <100.00%> (ø)
R/utilities-units.R 93.58% <0.00%> (+0.08%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 99e01f9...af34c0a. Read the comment docs.

@msevestre msevestre linked an issue May 6, 2021 that may be closed by this pull request
Copy link
Member

@msevestre msevestre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the API is nice. I am wondering about a few things

1-the ptr
2-the dispose

R/error-checks.R Outdated
if (is.null(object)) {
if (nullAllowed) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return (nullAllowed)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hah, nice shortcut.

R/error-checks.R Outdated Show resolved Hide resolved
if (is.null(value)) {
if (nullAllowed) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you change this? Is this more readable to have two ifs instead of one? Not sure.
Also we have the same conde in ValidateIsInteger. I always try to exit early. Here, you are adding complexity I believe because of the imbricated ifs

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It took me some time to understand why I did it... I try to avoid testing for nullAllowed if value is not NULL. Probably completely insignificant, and actually much better to test

if (is.null(value) && nullAllowed)

Will do it this way.

UPDATE:: Actually no, I tried to avoid testing for is.null(value) twice.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@msevestre You decide which approach to take.

R/simulation-batch.R Show resolved Hide resolved
R/simulation-batch.R Outdated Show resolved Hide resolved
R/utilities-simulation.R Show resolved Hide resolved
for (simBatchIdx in seq_along(simulationBatches)) {
simBatch <- simulationBatches[[simBatchIdx]]
# Put the simulation into sim pointer id <-> sim map
simId <- deparse(rClr::clrGetExtPtr(simBatch$ref))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WHAT IS THIS? LEt's please have a batch return an id or sthg.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:D Yes then we need to add an ID field to OSPSuite.R.Services.SettingsForConcurrentRunSimulationBatch in core.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok I added one and it's being used now. Please review

simulationResults[which(resultsIdSimulationIdMap == simId)]
})
# Not sure this is required
rClr::clrCall(simulationRunner, "Dispose")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is an issue here. If this object is returned via a Task, it might be singleton and should NOT Be disposed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I don't have experience with this. Removing the dispose.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we might need this. A task should not be disposable. It might be something I need to discuss with @abdelr

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in short,if this is disposable, we should get a new instance everytime.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I checked. Dispose is fine and should be call in that case explicitly. Not a big deal if not but it will release stuff immediately.

R/utilities-simulation.R Outdated Show resolved Hide resolved
R/utilities-simulation.R Show resolved Hide resolved
Copy link
Member Author

@PavelBal PavelBal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@msevestre There are some open questions left - e.g. SimBatch should return an ID must be first implemented in Core. Please see my answers.

R/error-checks.R Outdated
if (is.null(object)) {
if (nullAllowed) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hah, nice shortcut.

if (is.null(value)) {
if (nullAllowed) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It took me some time to understand why I did it... I try to avoid testing for nullAllowed if value is not NULL. Probably completely insignificant, and actually much better to test

if (is.null(value) && nullAllowed)

Will do it this way.

UPDATE:: Actually no, I tried to avoid testing for is.null(value) twice.

if (is.null(value)) {
if (nullAllowed) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@msevestre You decide which approach to take.

#' ids[[1]] <- simulationBatch1$addRunValues(parameterValues = c(1, 2), initialValues = 1)
#' ids[[2]] <- simulationBatch1$addRunValues(parameterValues = c(1.6, 2.4), initialValues = 3)
#' ids[[3]] <- simulationBatch2$addRunValues(parameterValues = c(4, 2), initialValues = 4)
#' ids[[4]] <- simulationBatch2$addRunValues(parameterValues = c(2.6, 4.4), initialValues = 5)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was just an example with multiple batches. But as it is dontrun anyways, I update the example with two different simulations.

R/utilities-simulation.R Show resolved Hide resolved
for (simBatchIdx in seq_along(simulationBatches)) {
simBatch <- simulationBatches[[simBatchIdx]]
# Put the simulation into sim pointer id <-> sim map
simId <- deparse(rClr::clrGetExtPtr(simBatch$ref))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:D Yes then we need to add an ID field to OSPSuite.R.Services.SettingsForConcurrentRunSimulationBatch in core.

simulationResults[which(resultsIdSimulationIdMap == simId)]
})
# Not sure this is required
rClr::clrCall(simulationRunner, "Dispose")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I don't have experience with this. Removing the dispose.

R/utilities-simulation.R Outdated Show resolved Hide resolved
@PavelBal
Copy link
Member Author

@msevestre OK I think this is ready to merge.

@msevestre msevestre merged commit 607bbf1 into develop May 10, 2021
@msevestre msevestre deleted the concurrent-simulation-batch branch May 10, 2021 14:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

runSimulationConcurrently
3 participants