{"id":32200758,"url":"https://github.com/kylebaron/mrgsim.parallel","last_synced_at":"2025-10-29T11:37:50.205Z","repository":{"id":43115217,"uuid":"211356057","full_name":"kylebaron/mrgsim.parallel","owner":"kylebaron","description":"Parallel simulation with mrgsolve and futures","archived":false,"fork":false,"pushed_at":"2025-09-10T17:50:51.000Z","size":674,"stargazers_count":5,"open_issues_count":4,"forks_count":1,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-10-22T03:55:29.099Z","etag":null,"topics":["future","mrgsolve","parallelization"],"latest_commit_sha":null,"homepage":"https://kylebaron.github.io/mrgsim.parallel","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kylebaron.png","metadata":{"files":{"readme":"README.Rmd","changelog":"NEWS.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2019-09-27T16:04:37.000Z","updated_at":"2025-09-10T17:50:55.000Z","dependencies_parsed_at":"2025-09-08T16:34:12.427Z","dependency_job_id":null,"html_url":"https://github.com/kylebaron/mrgsim.parallel","commit_stats":{"total_commits":139,"total_committers":1,"mean_commits":139.0,"dds":0.0,"last_synced_commit":"7dc191089f75ce467e0783421863b9fc411c68a2"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/kylebaron/mrgsim.parallel","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kylebaron%2Fmrgsim.parallel","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kylebaron%2Fmrgsim.parallel/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kylebaron%2Fmrgsim.parallel/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kylebaron%2Fmrgsim.parallel/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kylebaron","download_url":"https://codeload.github.com/kylebaron/mrgsim.parallel/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kylebaron%2Fmrgsim.parallel/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":280376551,"owners_count":26320276,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-22T02:00:06.515Z","response_time":63,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["future","mrgsolve","parallelization"],"created_at":"2025-10-22T03:55:32.512Z","updated_at":"2025-10-22T03:55:33.741Z","avatar_url":"https://github.com/kylebaron.png","language":"R","readme":"---\ntitle: \"\"\noutput: github_document\n---\n\n```{r,setup,include=FALSE}\nknitr::opts_chunk$set(comment = '.', message=FALSE, warning = FALSE, \n                      fig.path=\"man/figures/README-\")\n```\n\n# mrgsim.parallel\n\u003c!-- badges: start --\u003e\n\u003c!-- badges: end --\u003e\n\n## Overview\nmrgsolve.parallel facilitates parallel simulation with \nmrgsolve in R.  The future and parallel packages provide the parallelization.  \n\nThere are 2 main workflows:\n\n1. Split a `data_set` into chunks by ID, simulate the chunks in parallel, then \n   assemble the results back to a single data frame.\n1. Split an `idata_set` (individual-level parameters) into chunks by row, \n   simulate the chunks in parallel, then assemble the results back to a single\n   data frame.\n\nThe nature of the parallel backend requires some overhead to get the \nparallel simulation done.  So, it will take a reasonably-sized job to see \na speed increase and small jobs will likely take *longer* with parallelization.\nBut jobs taking more than a handful of seconds could benefit from this type \nof parallelization.\n\n\n```{r,include = FALSE}\noptions(mrgsolve.soloc = \"build\")\n```\n\n## Backend \n\n```{r}\nlibrary(dplyr)\n\nlibrary(future)\n\nlibrary(mrgsim.parallel)\n\noptions(future.fork.enable = TRUE, parallelly.fork.enable = TRUE, mc.cores = 4L)\n```\n## First workflow: split and simulate a data set\n\n```{r}\nmod \u003c- modlib(\"pk2cmt\", end = 168*8, delta = 1)\n\ndata \u003c- expand.ev(amt = 100*seq(1,2000), ii = 24, addl = 27*2+2) \n\ndata \u003c- mutate(data, CL = runif(n(), 0.7, 1.3))\n\nhead(data)\n\ndim(data)\n```\n\nWe can simulate in parallel with the future package or the parallel package like this:\n```{r}\nplan(multisession, workers = 4L)\nsystem.time(ans1 \u003c- future_mrgsim_d(mod, data, nchunk = 4L))\n\nplan(multicore, workers = 4L)\nsystem.time(ans1b \u003c- future_mrgsim_d(mod, data, nchunk = 4L))\n\n\nsystem.time(ans2 \u003c- mc_mrgsim_d(mod, data, nchunk = 4L))\n```\n\nTo compare an identical simulation done without parallelization\n```{r}\nsystem.time(ans3 \u003c- mrgsim_d(mod,data))\n```\n\n```{r}\nidentical(ans2,as.data.frame(ans3))\n```\n\n\n## Second workflow: split and simulate a batch of parameters\n\nBackend and the model\n```{r}\nplan(multisession, workers = 6)\n\nmod \u003c- modlib(\"pk1cmt\", end = 168*4, delta = 1)\n```\n\nFor this workflow, we have a set of parameters (`idata`) along with an \nevent object that gets applied to all of the parameters\n```{r}\nidata \u003c- tibble(CL = runif(4000, 0.5, 1.5), ID = seq_along(CL))\n\nhead(idata)\n```\n\n```{r}\ndose \u003c- ev(amt = 100, ii = 24, addl = 27)\n\ndose\n```\n\nRun it in parallel\n```{r}\nsystem.time(ans1 \u003c- mc_mrgsim_ei(mod, dose, idata, nchunk = 6))\n```\n\nAnd without parallelization\n\n```{r}\nsystem.time(ans2 \u003c- mrgsim_ei(mod, dose, idata, output = \"df\"))\n\nidentical(ans1,ans2)\n```\n\n## Utility functions \n\nYou can access the chunking functions for your own parallel workflows\n\n```{r}\ndose \u003c- ev_seq(ev(amt = 100), ev(amt = 50, ii = 12, addl = 2))\ndose \u003c- ev_rep(dose, 1:5)\n\ndose\n\nchunk_by_id(dose, nchunk = 2)\n```\n\nSee also: `chunk_by_row`\n\n## Do a dry run to check the overhead of parallelization\n\n```{r}\nplan(transparent)\nsystem.time(x \u003c- fu_mrgsim_d(mod, data, nchunk = 8, .dry = TRUE))\n\nplan(multisession, workers = 8L)\nsystem.time(x \u003c- fu_mrgsim_d(mod, data, nchunk = 8, .dry = TRUE))\n\n```\n\n## Pass a function to post process on the worker\n\nFirst check the range of times from the previous example\n\n```{r}\nsummary(ans1$time)\n```\n\nThe post-processing function has arguments the simulated data and the \nmodel object\n```{r}\npost \u003c- function(sims, mod) {\n  filter(sims, time \u003e 600)  \n}\n\ndose \u003c- ev(amt = 100, ii = 24, addl = 27)\n\nans3 \u003c- mc_mrgsim_ei(mod, dose, idata, nchunk = 6, .p = post)\n```\n\n```{r}\nsummary(ans3$time)\n\n```\n\nThe main use case here is to summarize or some how decrease the volume of data\nbefore returning the combined simulations.  In case memory is able to handle\nthe simulation volume, this post-processing could be done on the combined\ndata as well.\n\n\n\u003chr\u003e\n\n## More info\n\nSee [inst/docs/stories.md (on GitHub only)](inst/docs/stories.md) for more details.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkylebaron%2Fmrgsim.parallel","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkylebaron%2Fmrgsim.parallel","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkylebaron%2Fmrgsim.parallel/lists"}