Skip to content

Commit 8c96956

Browse files
authored
Merge pull request #8 from pythonhealthdatascience/parallel
fix(parallel/seeds): switch to future.apply (which still works when knit), use future.seed, plus other little chores
2 parents b0dc180 + 2323517 commit 8c96956

28 files changed

+402
-296
lines changed

DESCRIPTION

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ Imports:
2424
rlang,
2525
tidyr,
2626
R6,
27-
tidyselect
27+
tidyselect,
28+
future,
29+
future.apply
2830
Suggests:
2931
testthat (>= 3.0.0),
3032
lintr,

NAMESPACE

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@ importFrom(dplyr,lead)
1313
importFrom(dplyr,mutate)
1414
importFrom(dplyr,n_distinct)
1515
importFrom(dplyr,summarise)
16+
importFrom(future,multisession)
17+
importFrom(future,plan)
18+
importFrom(future,sequential)
19+
importFrom(future.apply,future_lapply)
1620
importFrom(magrittr,"%>%")
17-
importFrom(parallel,clusterEvalQ)
18-
importFrom(parallel,clusterExport)
19-
importFrom(parallel,detectCores)
20-
importFrom(parallel,makeCluster)
21-
importFrom(parallel,parLapply)
22-
importFrom(parallel,stopCluster)
2321
importFrom(purrr,reduce)
2422
importFrom(rlang,.data)
2523
importFrom(simmer,add_generator)

R/defaults.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ Defaults <- R6Class( # nolint
9393
#' the class without needing to run `Defaults[["new"]]()` every time.
9494
#'
9595
#' @return Instance of the Defaults class.
96-
#'
96+
#'
9797
#' @export
9898
#' @rdname defaults
9999

R/model.R

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
#'
33
#' @param run_number Integer representing index of current simulation run.
44
#' @param param_class Instance of Defaults containing model parameters.
5+
#' @param set_seed Whether to set seed within the model function (which we
6+
#' may not wish to do if being set elsewhere - such as done in trial()).
7+
#' Default is TRUE.
58
#'
69
#' @importFrom simmer trajectory seize timeout release simmer add_resource
710
#' @importFrom simmer add_generator run wrap get_mon_arrivals
@@ -12,7 +15,7 @@
1215
#' @return Named list with two tables: monitored arrivals and resources
1316
#' @export
1417

15-
model <- function(run_number, param_class) {
18+
model <- function(run_number, param_class, set_seed = TRUE) {
1619

1720
# Extract parameter list from the parameter class
1821
# It is important to do this within the model function (rather than
@@ -25,7 +28,9 @@ model <- function(run_number, param_class) {
2528
valid_inputs(run_number, param)
2629

2730
# Set random seed based on run number
28-
set.seed(run_number)
31+
if (set_seed) {
32+
set.seed(run_number)
33+
}
2934

3035
# Define the patient trajectory
3136
patient <- trajectory("appointment") %>%

R/trial.R

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
#' Run simulation for multiple replications, sequentially or in parallel.
22
#'
3-
#' Note: The parallel processing is implemented using `parLapply` because
4-
#' `mcLapply`` does not work on Windows and `future_lapply`` would often get
5-
#' stuck.
6-
#'
73
#' @param param_class Instance of Defaults containing model parameters.
84
#'
9-
#' @importFrom parallel detectCores makeCluster stopCluster clusterEvalQ
10-
#' @importFrom parallel clusterExport parLapply
5+
#' @importFrom future plan multisession sequential
6+
#' @importFrom future.apply future_lapply
117
#'
128
#' @return Named list with two tables: monitored arrivals and resources.
139
#' @export
@@ -17,35 +13,31 @@ trial <- function(param_class) {
1713
n_cores <- param_class[["get"]]()[["cores"]]
1814
n_runs <- param_class[["get"]]()[["number_of_runs"]]
1915

16+
# Determine the parallel execution plan
2017
if (n_cores == 1L) {
21-
22-
# Sequential execution
23-
results <- lapply(
24-
1L:n_runs,
25-
function(i) simulation::model(run_number = i, param_class = param_class)
26-
)
27-
18+
plan(sequential) # Sequential execution
2819
} else {
29-
# Parallel execution
30-
31-
# Create a cluster with specified number of cores
3220
if (n_cores == -1L) {
33-
cores <- detectCores() - 1L
21+
cores <- future::availableCores() - 1L
3422
} else {
3523
cores <- n_cores
3624
}
37-
cl <- makeCluster(cores)
38-
39-
# Ensure the cluster is stopped upon exit
40-
on.exit(stopCluster(cl))
41-
42-
# Run simulations in parallel
43-
results <- parLapply(
44-
cl, 1L:n_runs,
45-
function(i) simulation::model(run_number = i, param_class = param_class)
46-
)
25+
plan(multisession, workers = cores) # Parallel execution
4726
}
4827

28+
# Run simulations (sequentially or in parallel)
29+
# Mark set_seed as FALSE as we handle this using future.seed(), rather than
30+
# within the function, and we don't want to override future.seed
31+
results <- future_lapply(
32+
1L:n_runs,
33+
function(i) {
34+
simulation::model(run_number = i,
35+
param_class = param_class,
36+
set_seed = FALSE)
37+
},
38+
future.seed = 123456L
39+
)
40+
4941
# Combine the results from multiple replications into just two dataframes
5042
if (n_runs > 1L) {
5143
all_arrivals <- do.call(

man/model.Rd

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/trial.Rd

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

outputs/base_trial.csv

Lines changed: 100 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,101 @@
11
"","replication","arrivals","mean_waiting_time_nurse","mean_activity_time_nurse","utilisation_nurse"
2-
"1",1,15,0,8.92266571894842,0.371528668451366
3-
"2",2,16,0,7.7255587739498,0.399865009965906
4-
"3",3,14,0,7.55613765895507,0.34983454158788
5-
"4",4,19,1.53581418282632,8.0588103472346,0.604995664123677
6-
"5",5,21,0.0519866784761905,9.35135333211744,0.573584434011877
7-
"6",6,29,0.384083299862069,8.20993074704441,0.611272646882443
8-
"7",7,16,0,9.54055185249263,0.401321053724437
9-
"8",8,19,0.497575033078947,8.57860709328252,0.448350482951233
10-
"9",9,19,0,10.0769710427457,0.627360482435886
11-
"10",10,15,0,8.01416495697315,0.336149156108189
12-
"11",11,8,0,8.19813956226296,0.266580242951386
13-
"12",12,12,0,12.4423690398617,0.4411930714609
14-
"13",13,20,0.227177603555,9.34805206374257,0.515029185220157
15-
"14",14,18,0,5.23327658447847,0.385068311969305
16-
"15",15,22,0.0589624537590909,6.84200713184188,0.508430352633121
17-
"16",16,22,0.926212363481818,9.48213350689228,0.588485465605031
18-
"17",17,20,0.07705566903,9.27393115935178,0.536720444332965
19-
"18",18,26,0.0945137374423077,10.8162048914333,0.810276629606859
20-
"19",19,22,1.2570415664,7.78838345773335,0.561919275982066
21-
"20",20,14,0,10.5890431482814,0.46375500374331
22-
"21",21,18,0.0716436412222222,5.10846690914392,0.24044400368886
23-
"22",22,21,0.0591580490190476,9.02494565357537,0.503210923332613
24-
"23",23,21,0.175647701361905,8.22091740614917,0.508214600684611
25-
"24",24,22,0,6.39763986519681,0.378833989818279
26-
"25",25,19,0,8.5503512865253,0.447278181574428
27-
"26",26,21,0.783577928152381,7.45344603512225,0.662036006480184
28-
"27",27,20,1.2903625216,11.9739449424173,0.745215734677497
29-
"28",28,16,0.00683261825625,8.37759384824821,0.551655575186502
30-
"29",29,12,0,5.86264785410928,0.266510144455835
31-
"30",30,20,0.04474548582,9.01076862622505,0.571228028042687
32-
"31",31,16,0.06295110160625,10.0528316919784,0.486085855334318
33-
"32",32,18,0,6.14756691173725,0.349095910560405
34-
"33",33,18,0,5.99381148722807,0.3896454176773
35-
"34",34,20,0.0106671803,8.78699768495577,0.584918663395058
36-
"35",35,24,0.0375949580125,6.1250148346272,0.427273068939378
37-
"36",36,16,0,5.08653652591333,0.214553253790685
38-
"37",37,12,0,9.22254811747763,0.295193944486686
39-
"38",38,15,0,8.30040012220122,0.338457396220477
40-
"39",39,15,0,8.58648124293031,0.360080420090988
41-
"40",40,19,0.00535527148947368,10.7022239471977,0.528871131735171
42-
"41",41,18,0,7.57769866661476,0.436048290674667
43-
"42",42,21,0.266500101061905,10.7945944992205,0.589837388963679
44-
"43",43,17,0.397722008847059,8.89793112128514,0.509968362924036
45-
"44",44,25,0,6.40573070245996,0.418591631105296
46-
"45",45,18,0,6.05024950804647,0.283169912660256
47-
"46",46,17,0.179988911964706,8.67006372925668,0.503745498250098
48-
"47",47,17,0,8.04598031261248,0.496478425937344
49-
"48",48,17,0,9.9499986364565,0.563482722117011
50-
"49",49,17,0.264439412652941,8.02227432181301,0.385527495971647
51-
"50",50,14,0,8.76117602769676,0.335233570735889
52-
"51",51,20,0.28362357005,9.26156379184728,0.546198822373836
53-
"52",52,20,0,7.39720045605086,0.4143982432236
54-
"53",53,19,0.0938929813473684,8.56326729947669,0.45861277528473
55-
"54",54,14,0,10.659332903371,0.382246636436652
56-
"55",55,18,0,7.87964575416427,0.374564675074477
57-
"56",56,18,0,7.37112380014318,0.460975698751186
58-
"57",57,19,0,6.93446257276265,0.365720038954959
59-
"58",58,22,0.842384962627273,10.9114072850773,0.643990582947036
60-
"59",59,16,0,7.33445308964826,0.447441729120187
61-
"60",60,16,0.0918236140875,8.03323464120855,0.369527919200035
62-
"61",61,11,0,13.5779057867523,0.483989102395751
63-
"62",62,18,0.395429763688889,9.49766947771225,0.448792455864958
64-
"63",63,15,0,8.0549803191723,0.388190967309224
65-
"64",64,25,0.683926920868,10.5707000369387,0.808254250632857
66-
"65",65,12,0,9.2658170718876,0.441807668730405
67-
"66",66,14,0,7.86698203584097,0.387681107582835
68-
"67",67,23,0.23453886086087,10.5046831987208,0.703233950990321
69-
"68",68,17,1.14385153219412,7.18628445207151,0.551470182223506
70-
"69",69,18,0,9.32508026499198,0.424200095363553
71-
"70",70,15,0,9.88855559309646,0.462207362572994
72-
"71",71,18,0,7.07917744950834,0.378879954108974
73-
"72",72,22,0,5.05825135675878,0.303948460159635
74-
"73",73,16,0,6.54817416100394,0.276552161950288
75-
"74",74,22,0.0762762008636364,7.08570390399686,0.520615628616018
76-
"75",75,15,0,8.35517359316539,0.3462056554242
77-
"76",76,26,0.231123361023077,9.34246451197043,0.667359672400375
78-
"77",77,13,0,5.53465633946736,0.226673832853035
79-
"78",78,18,0,9.61261062598683,0.498084396177169
80-
"79",79,18,0.245395860016667,9.04738422111327,0.553657714791215
81-
"80",80,9,0,5.50085843079494,0.201336272341734
82-
"81",81,16,0,9.0184545999554,0.419145974185224
83-
"82",82,11,0,8.31245460334258,0.280365167135215
84-
"83",83,13,0.0655415281846154,8.43917168711658,0.471247105983744
85-
"84",84,14,0,5.66624868295414,0.25804810923752
86-
"85",85,13,0,8.59084061245018,0.304516812184508
87-
"86",86,17,0,5.91406597581937,0.280350493672794
88-
"87",87,20,0.205811518405,9.2933162848188,0.69631283888828
89-
"88",88,11,0,8.43495978322226,0.27987165805182
90-
"89",89,12,0,5.21308985165848,0.204204859226964
91-
"90",90,24,0.571064473258333,9.9324202708865,0.60481764623509
92-
"91",91,16,0,9.04945415537796,0.414276432818536
93-
"92",92,18,0,8.81995817950373,0.574697475286729
94-
"93",93,12,0.271884909941667,11.6058512500617,0.519983434440122
95-
"94",94,17,0.0601171019647059,7.56813893525244,0.362623085040635
96-
"95",95,11,0,9.75339371460961,0.437785712688619
97-
"96",96,19,0,11.1541294873282,0.558235643922686
98-
"97",97,12,0,7.37929923388839,0.291381665516697
99-
"98",98,14,0,10.0787670561435,0.447482270035736
100-
"99",99,16,0.14587324370625,7.78504644281919,0.562039183458945
101-
"100",100,19,0,7.04299845211555,0.360356855474523
2+
"1",1,21,0.172634907604762,10.7405465431143,0.6857207386012
3+
"2",2,16,0,7.10057623127618,0.339653664237136
4+
"3",3,13,0,6.88747631698383,0.264798148560583
5+
"4",4,16,0.01773150233125,9.28954224746827,0.454079770130564
6+
"5",5,17,0,4.78654797563128,0.266413092811991
7+
"6",6,18,0.393059339322222,8.11718767381037,0.58554969502398
8+
"7",7,25,0.111893570592,8.41810983409961,0.55033912691785
9+
"8",8,16,0,6.66989735996729,0.346967751006821
10+
"9",9,22,1.94776083503636,9.00372743303383,0.644303386268358
11+
"10",10,22,0.0487056471818182,8.99915934342635,0.566310989374246
12+
"11",11,13,0,6.7943906100343,0.259819956897941
13+
"12",12,13,0,6.07836862088896,0.259628997899959
14+
"13",13,14,0.157631317071429,7.94243342769353,0.355801959587693
15+
"14",14,22,0.175854982204545,9.71531458660121,0.598313695770614
16+
"15",15,18,0,9.39191265113015,0.546128420946744
17+
"16",16,13,0,5.90821888504484,0.228154581728323
18+
"17",17,17,0,6.06421073513055,0.321583877537017
19+
"18",18,12,0,6.77546466202225,0.210183884317542
20+
"19",19,24,0,5.44514836439291,0.366612655205927
21+
"20",20,15,0,6.18916668588235,0.265184977125269
22+
"21",21,12,0,4.61619341536056,0.232317570980903
23+
"22",22,19,0.116305906489474,10.2683047405045,0.512079315391267
24+
"23",23,21,0.0749881320190476,10.659018482097,0.698974517960742
25+
"24",24,24,0.4664847124,7.08382960437018,0.539403679605581
26+
"25",25,15,0,7.23255467683587,0.384654754001199
27+
"26",26,18,0,6.85819116541343,0.399796048788589
28+
"27",27,23,0.014511273173913,7.97383702009454,0.499049148145453
29+
"28",28,23,0,7.46106098517586,0.45851030026982
30+
"29",29,20,0.01418191837,11.503573117205,0.674316256541288
31+
"30",30,12,0,12.4820647109409,0.592051818635463
32+
"31",31,17,0,8.9585579247382,0.475338691956434
33+
"32",32,22,1.14093051919091,8.85676296326767,0.596681932320881
34+
"33",33,21,0.445679449809524,11.7769481804205,0.64686352151671
35+
"34",34,22,0.0558661645636364,7.59793643959547,0.4242058989872
36+
"35",35,20,0.279059747785,9.8092481090931,0.532472767720392
37+
"36",36,12,0,8.88627405823332,0.296645804805517
38+
"37",37,14,0,6.16763443942273,0.260984326563795
39+
"38",38,16,0,11.0374009522411,0.584349991863959
40+
"39",39,23,0,5.83910191928464,0.373047338281816
41+
"40",40,13,0,8.85535418008837,0.321048787172079
42+
"41",41,17,0,9.86153219518894,0.471157745370755
43+
"42",42,18,0.265085758005556,9.32962711420884,0.463630613451225
44+
"43",43,30,0.413022382443333,7.28345583792507,0.632923144372885
45+
"44",44,19,0,6.20265200760801,0.355604631521442
46+
"45",45,17,0,9.16558333047932,0.498215814669524
47+
"46",46,14,0,8.50358144323973,0.40451293404052
48+
"47",47,20,0,4.9416280548959,0.280518734931329
49+
"48",48,18,0.338606286466667,10.8625905555314,0.618974673076519
50+
"49",49,16,0.09190991038125,7.45822771304233,0.304140833413389
51+
"50",50,18,0.350054970055556,11.4160970539544,0.571846232578289
52+
"51",51,24,1.22428705825833,11.1141699595254,0.77435356741355
53+
"52",52,22,1.0747237336,10.9845036501615,0.762205674869442
54+
"53",53,24,0.2956247858125,8.29628122523315,0.593217310157795
55+
"54",54,20,0,7.42836215073758,0.437976044150862
56+
"55",55,25,0.701470431216,10.7697143197363,0.792437085562371
57+
"56",56,18,0,3.68742876166258,0.232531394741985
58+
"57",57,18,1.11819213068333,12.8372662960699,0.647481513718578
59+
"58",58,15,0,8.57061685914263,0.35528893436266
60+
"59",59,18,0,13.2698615975004,0.646487895791919
61+
"60",60,20,0.163873754455,5.73347085933515,0.344016064107376
62+
"61",61,19,0.291918888036842,8.89945109341046,0.454234823022512
63+
"62",62,13,0,8.98985445679083,0.372221663736418
64+
"63",63,24,0.0741459701458333,8.770849255326,0.661904843807603
65+
"64",64,16,0.6466449799375,10.8619650858589,0.653223825542272
66+
"65",65,16,0,8.65165295002729,0.428122599229444
67+
"66",66,19,0.331859246468421,9.72538181217374,0.482166296787823
68+
"67",67,15,0,8.90898881288831,0.491279299284598
69+
"68",68,13,0.0347675995307692,9.57747118609483,0.46226336902312
70+
"69",69,12,0,8.41563988505943,0.273131482072979
71+
"70",70,18,0.0100520341111111,7.22101186179044,0.362790629624835
72+
"71",71,13,0.0726815632923077,6.02743235273608,0.249677973975859
73+
"72",72,16,0,8.94662318051259,0.396719862555918
74+
"73",73,15,0,6.00094877202697,0.253757037388511
75+
"74",74,24,0.9257580821375,6.23331615319016,0.525902232465197
76+
"75",75,23,0,4.65243453860885,0.449184422355558
77+
"76",76,14,0,6.47874777951893,0.333694339335394
78+
"77",77,24,0.874672642745833,9.76727133229084,0.741477726930124
79+
"78",78,15,0,10.2072754254209,0.488768954419666
80+
"79",79,16,0.05332475145,7.08323650705356,0.440013578258183
81+
"80",80,19,0.324464807021053,12.0887824219051,0.594428948563637
82+
"81",81,24,1.02711901124583,8.82194799562383,0.579714838504693
83+
"82",82,23,0.848633635556522,7.33478428088739,0.612070570432051
84+
"83",83,19,0,6.72578993443048,0.4010223968927
85+
"84",84,17,0.0086244323,7.49703271709652,0.336173465508689
86+
"85",85,19,0.0206446611684211,9.03665844396845,0.466591100821903
87+
"86",86,25,0.708471232184,8.73863864542212,0.64752461004885
88+
"87",87,20,0.358855828215,8.97433462369758,0.495032753524222
89+
"88",88,13,0,14.1183180194591,0.56577858211405
90+
"89",89,15,0,8.51856460928998,0.368011304895727
91+
"90",90,21,0.019621619152381,9.41218877944077,0.572715294625353
92+
"91",91,24,0,7.43438548758834,0.552238861244287
93+
"92",92,16,2.12286939339375,12.4974164222768,0.655423578887326
94+
"93",93,23,0.56134304333913,10.612379013797,0.783901564319108
95+
"94",94,13,0,4.90019655529476,0.186899046605271
96+
"95",95,14,0,8.44998224756754,0.316635268100141
97+
"96",96,23,0.30675455233913,7.45276488235241,0.506751654132952
98+
"97",97,17,0.144732497258824,9.26537058668207,0.426026115330518
99+
"98",98,20,0.238843066105,9.94408414189002,0.538063695810092
100+
"99",99,17,0.112457353976471,9.82925194721256,0.550634397521811
101+
"100",100,16,0.2714164474625,7.80477320313942,0.422094474520408
-4.84 KB
Loading
31.2 KB
Loading

0 commit comments

Comments
 (0)