Skip to content

Commit f26b98d

Browse files
committed
OpenMP vignette, safer SP, and pass nThreads consistently + use simParam in internals; Closes gaynorr#271
1 parent 1d00ec9 commit f26b98d

78 files changed

Lines changed: 1267 additions & 301 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

NEWS.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@
66

77
* Improved documentation for `SimParam$finalizePop` field.
88

9+
* Added a short vignette explaining OpenMP support for parallelization.
10+
11+
* `SimParam$nThreads` now validates assignments. Setting it to `NULL` resets to `getNumThreads()`, and invalid values now fail with a clear error.
12+
13+
* Added optional `nThreads` arguments across OpenMP-enabled R functions and `SimParam` methods so thread counts can be controlled explicitly per call instead of only through `SimParam$nThreads` and is propagated across the package consistently.
14+
915
* Consolidated the use of RNG across the package to enable reproducibility. This is an internal change not visible to users.
1016

1117
* Made meiosis-related C++ RNG reproducible across serial and OpenMP execution by using `dqrng`. This is an internal change not visible to users, but will enable visible reproducibility.
1218

1319
* Fixed a reproducibility bug in `runMacs()` and `runMacs2()`: `set.seed()` can now reproduce MaCS founder simulations, including when chromosomes are simulated in parallel with OpenMP. This is an internal change not visible to users, but will enable visible reproducibility.
1420

15-
* Added optional `nThreads` arguments to standalone `MapPop` helper functions so thread counts can be controlled explicitly outside the `SimParam` workflow.
16-
1721
* Fixed a bug in `getGvE` with multiple traits.
1822

1923
# AlphaSimR 2.1.0

R/Class-Pop.R

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,8 @@ setMethod("length",
582582
#' @param rawPop an object of \code{\link{MapPop-class}} or
583583
#' \code{\link{NamedMapPop-class}}
584584
#' @param simParam an object of \code{\link{SimParam}}
585+
#' @param nThreads number of threads to use if OpenMP is available.
586+
#' If \code{NULL}, the number is obtained from \code{simParam$nThreads}.
585587
#' @param ... additional arguments used internally
586588
#'
587589
#' @return Returns an object of \code{\link{Pop-class}}
@@ -614,11 +616,16 @@ setMethod("length",
614616
#' pop@miscPop$tmp1 = sum(pop@misc$tmp1)
615617
#' pop@miscPop$tmp2 = sum(pop@misc$tmp2)
616618
#' @export
617-
newPop = function(rawPop,simParam=NULL,...){
619+
newPop = function(rawPop,simParam=NULL,nThreads=NULL,...){
618620
if(is.null(simParam)){
619621
simParam = get("SP",envir=.GlobalEnv)
620622
}
621-
return(.newPop(rawPop=rawPop,simParam=simParam,...))
623+
if(is.null(nThreads)){
624+
nThreads = simParam$nThreads
625+
}else{
626+
nThreads = as.integer(nThreads)
627+
}
628+
return(.newPop(rawPop=rawPop,simParam=simParam,nThreads=nThreads,...))
622629
}
623630

624631
#' @title Create new population
@@ -638,6 +645,8 @@ newPop = function(rawPop,simParam=NULL,...){
638645
#' @param maleParentPop optional population of male parents
639646
#' @param hist optional recombination history
640647
#' @param simParam an object of \code{\link{SimParam}}
648+
#' @param nThreads number of threads to use if OpenMP is available.
649+
#' If \code{NULL}, the number is obtained from \code{simParam$nThreads}.
641650
#' @param ... additional arguments passed to the \code{finalizePop}
642651
#' function in simParam
643652
#'
@@ -647,10 +656,15 @@ newPop = function(rawPop,simParam=NULL,...){
647656
.newPop = function(rawPop, id=NULL, mother=NULL, father=NULL,
648657
iMother=NULL, iFather=NULL, isDH=NULL,
649658
femaleParentPop=NULL, maleParentPop=NULL,
650-
hist=NULL, simParam=NULL,...){
659+
hist=NULL, simParam=NULL, nThreads=NULL,...){
651660
if(is.null(simParam)){
652661
simParam = get("SP",envir=.GlobalEnv)
653662
}
663+
if(is.null(nThreads)){
664+
nThreads = simParam$nThreads
665+
}else{
666+
nThreads = as.integer(nThreads)
667+
}
654668

655669
stopifnot(sapply(simParam$genMap,length)==rawPop@nLoci)
656670

@@ -720,7 +734,7 @@ newPop = function(rawPop,simParam=NULL,...){
720734

721735
if(simParam$nTraits>=1){
722736
tmp = getGvIndex(rawPop, simParam$traits, simParam$activeQtl,
723-
simParam$qtlIndex, simParam$nTraits, simParam$nThreads)
737+
simParam$qtlIndex, simParam$nTraits, nThreads)
724738

725739
gv = tmp[[1]]
726740
colnames(gv) = simParam$traitNames
@@ -786,6 +800,8 @@ newPop = function(rawPop,simParam=NULL,...){
786800
#'
787801
#' @param pop an object of \code{\link{Pop-class}}
788802
#' @param simParam an object of \code{\link{SimParam}}
803+
#' @param nThreads number of threads to use if OpenMP is available.
804+
#' If \code{NULL}, the number is obtained from \code{simParam$nThreads}.
789805
#'
790806
#' @return an object of \code{\link{Pop-class}}
791807
#'
@@ -805,10 +821,15 @@ newPop = function(rawPop,simParam=NULL,...){
805821
#' pop = resetPop(pop, simParam=SP)
806822
#'
807823
#' @export
808-
resetPop = function(pop,simParam=NULL){
824+
resetPop = function(pop,simParam=NULL,nThreads=NULL){
809825
if(is.null(simParam)){
810826
simParam = get("SP",envir=.GlobalEnv)
811827
}
828+
if(is.null(nThreads)){
829+
nThreads = simParam$nThreads
830+
}else{
831+
nThreads = as.integer(nThreads)
832+
}
812833
pop@nTraits = simParam$nTraits
813834

814835
# Extract names to add back at the end
@@ -828,7 +849,7 @@ resetPop = function(pop,simParam=NULL){
828849

829850
# Calculate genetic values
830851
for(i in seq_len(simParam$nTraits)){
831-
tmp = getGv(simParam$traits[[i]],pop,simParam$nThreads)
852+
tmp = getGv(simParam$traits[[i]],pop,nThreads)
832853
pop@gv[,i] = tmp[[1]]
833854
if(length(tmp)>1){
834855
pop@gxe[[i]] = tmp[[2]]

0 commit comments

Comments
 (0)