The R Project SVN R

Rev

Rev 73228 | Rev 81140 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 73228 Rev 73309
Line 462... Line 462...
462
at once, to ensure that the number of forked jobs does not exceed the number
462
at once, to ensure that the number of forked jobs does not exceed the number
463
of selected CPUs. As soon as a child process has finished, the next
463
of selected CPUs. As soon as a child process has finished, the next
464
available job for the CPU is forked.
464
available job for the CPU is forked.
465
 
465
 
466
The following code example demonstrates how the execution
466
The following code example demonstrates how the execution
467
time can be reduced by assigning the elements of \code{X} to specific CPUs.
467
time can be reduced by assigning the elements of \code{X} to specific CPUs,
-
 
468
not on Windows, where \code{mc.cores} must remain at 1.
468
 
469
 
-
 
470
%%     setting eval=TRUE  would cost ca. 1.5 minutes every time this is built
469
%%         set eval=FALSE  to save ~ 40 seconds
471
%%     also,   .Platform$OS.type == "Windows"  cannot use mc.cores = 2
470
<<affinity-ex, eval=TRUE>>=
472
<<affinity-ex, eval=FALSE>>=
471
## Exemplary variance filter executed on three different matrices in parallel.
473
## Exemplary variance filter executed on three different matrices in parallel.
472
## Can be used in gene expression analysis as a prefilter
474
## Can be used in gene expression analysis as a prefilter
473
## for the number of covariates.
475
## for the number of covariates.
474
 
476
 
475
library(parallel)
477
library(parallel)
476
n <- 300   # observations
478
n <- 300   # observations
477
p <- 5000  # covariates (orig. 20000 is more realistic; limit time here)
479
p <- 20000 # covariates
478
 
480
 
479
## Different sized matrices as filter inputs
481
## Different sized matrices as filter inputs
480
## Matrix A and B form smaller work loads
482
## Matrix A and B form smaller work loads
481
## while matrix C forms a bigger workload (2*p)
483
## while matrix C forms a bigger workload (2*p)
482
library(stats)
484
library(stats)
Line 499... Line 501...
499
## CPU mapping: A and B run on CPU 1 while C runs on CPU 2:
501
## CPU mapping: A and B run on CPU 1 while C runs on CPU 2:
500
affinity <- c(1,1,2)
502
affinity <- c(1,1,2)
501
system.time(
503
system.time(
502
  mclapply(X = list(A,B,C), FUN = varFilter,
504
  mclapply(X = list(A,B,C), FUN = varFilter,
503
           mc.preschedule = FALSE, affinity.list = affinity))
505
           mc.preschedule = FALSE, affinity.list = affinity))
-
 
506
##   user  system elapsed
-
 
507
## 34.909   0.873  36.720
-
 
508
 
504
 
509
 
505
## mclapply without affinity.list
510
## mclapply without affinity.list
506
system.time(
511
system.time(
507
  mclapply(X = list(A,B,C), FUN = varFilter, mc.cores = 2,
512
  mclapply(X = list(A,B,C), FUN = varFilter, mc.cores = 2,
508
           mc.preschedule = FALSE) )
513
           mc.preschedule = FALSE) )
-
 
514
##   user  system elapsed
-
 
515
## 72.893   1.588  55.982
-
 
516
 
509
 
517
 
510
## mclapply with prescheduling
518
## mclapply with prescheduling
511
system.time(
519
system.time(
512
   mclapply(X = list(A,B,C), FUN = varFilter, mc.cores = 2,
520
   mclapply(X = list(A,B,C), FUN = varFilter, mc.cores = 2,
513
            mc.preschedule = TRUE) )
521
            mc.preschedule = TRUE) )
-
 
522
##   user  system elapsed
-
 
523
## 53.455   1.326  53.399
514
@
524
@
515
 
525
 
516
Instead of using \code{affinity.list} for runtime optimization,
526
Instead of using \code{affinity.list} for runtime optimization,
517
it can also be used to simply restrict the computation
527
it can also be used to simply restrict the computation
518
to specific CPUs as in the following example.
528
to specific CPUs as in the following example.