11# ' comm.chunk
22# '
3- # ' Given a total number of items comm.chunk splits the number into equal
4- # ' chunks. Warious options are possible when the number does not split evenly
5- # ' into chunks.
3+ # ' Given a total number of items, comm.chunk splits the number into
4+ # ' chunks. Tailored especially for situations in SPMD style
5+ # ' programming. Warious options are possible when the number does not
6+ # ' split evenly into chunks. The output form can be either a number or
7+ # ' a vector of items.
68# '
79# ' @param N
8- # ' The number of items to split into equal chunks.
10+ # ' The number of items to split into chunks.
911# ' @param form
10- # ' Output a chunk as a sigle "number", as a "vector" of items from 1:N, or as
11- # ' an "IOpair" giving offsets and lengths in a file.
12+ # ' Output a chunk as a single "number", as a "vector" of contiguous
13+ # ' items from 1:N, or as an "iopair" giving offset and length in a
14+ # ' file.
1215# ' @param type
13- # ' Either "balance" the chunks so they differ by no more than 1 item or force
14- # ' as many as possible to be "equal" with possibly one or more smaller or even
15- # ' zero size chunks.
16+ # ' Either "balance" the chunks so they differ by no more than 1 item (used most
17+ # ' frequently for best balance) or force as many as possible to be "equal" with
18+ # ' possibly one or more smaller or even zero size chunks (required by ScaLAPACK's
19+ # ' block-cyclic layouts).
1620# ' @param lo.side
17- # ' If exact balance is not possible, put the lower chunks on the "left" or on
18- # ' the "right".
21+ # ' If exact balance is not possible, put the smaller chunks on the "left" (low ranks)
22+ # ' or on the "right" (high ranks) .
1923# ' @param all.rank
2024# ' FALSE returns only the chunk output for rank r. TRUE returns a vector of
2125# ' length p (when form="number"), and a list of length p (when form="vector")
2226# ' each containing the output for the corresponding rank.
2327# ' @param p
24- # ' The number of chunks (defaults to comm.size() ).
28+ # ' The number of chunks (processors) Defaults to comm.size().
2529# ' @param rank
2630# ' The rank of returned chunk (defaults to comm.rank()). Note that ranks are
2731# ' numbered from 0 to p-1, whereas the list elements for all.rank=TRUE are
3438# '
3539# ' @examples
3640# ' ## Note that the p and rank parameters are provided by comm.size() and
37- # ' ## comm.rank(), respectively, when running in parallel with pbdMPI and
38- # ' ## need not be specified.
41+ # ' ## comm.rank(), respectively, when running SPMD in parallel. Normally, they
42+ # ' ## are not specified unless testing in serial mode .
3943# ' library(pbdIO)
4044# '
4145# ' comm.chunk(16, all.rank=TRUE, p=5)
4448# ' comm.chunk(16, p=5, rank=0)
4549# '
4650# ' @export
47- comm.chunk <- function (N , form = " number" , type = " balance" , lo.side = " left " ,
51+ comm.chunk <- function (N , form = " number" , type = " balance" , lo.side = " right " ,
4852 all.rank = FALSE , p = comm.size(), rank = comm.rank()) {
4953
50- form <- comm.match.arg(tolower( form ) , c(" number" , " vector" , " IOpair " ))
51- type <- comm.match.arg(tolower( type ) , c(" balance" , " equal" ))
52- lo.side <- comm.match.arg(tolower( lo.side ) , c(" left " , " right " ))
54+ form <- comm.match.arg(form , c(" number" , " vector" , " iopair " ))
55+ type <- comm.match.arg(type , c(" balance" , " equal" , " ldim " , " bldim " ))
56+ lo.side <- comm.match.arg(lo.side , c(" right " , " left " ))
5357 if (! is.logical(all.rank ) || length(all.rank ) != 1 || is.na(all.rank ))
5458 comm.stop(" argument 'all.rank' must be a bool" )
5559 if (! is.numeric(p ) || p < 1 )
5660 comm.stop(" argument 'p' must be a positive integer" )
57- if (! is.numeric(rank ) || rank < 0 || rank > = comm.size() )
58- comm.stop(" argument 'rank' must be an integer from 0 to comm.size() -1" )
61+ if (! is.numeric(rank ) || rank < 0 || rank > = p )
62+ comm.stop(" argument 'rank' must be an integer from 0 to p -1" )
5963
6064 p <- as.integer(p )
6165 rank <- as.integer(rank )
@@ -75,7 +79,7 @@ comm.chunk <- function(N, form="number", type="balance", lo.side="left",
7579 } else if (lo.side == " left" ) {
7680 items [(p - rem + 1 ): p ] <- base + 1
7781 }
78- } else if (type == " equal" ) {
82+ } else if (type == " equal" | type == " ldim " ) { # # fix for bldim!!!
7983 items <- items + 1
8084 rem <- p * (base + 1 ) - N
8185 if (lo.side == " right" ) {
@@ -111,7 +115,7 @@ comm.chunk <- function(N, form="number", type="balance", lo.side="left",
111115 if (all.rank ) ret <- lapply(1 : length(items_base ),
112116 function (i ) lapply(items , seq_len )[[i ]] + items_base [i ])
113117 else ret <- items_base [rank + 1 ] + seq_len(items [rank + 1 ])
114- } else if (form == " IOpair " ) {
118+ } else if (form == " iopair " ) {
115119 offset <- c(0 , cumsum(items )[- p ])
116120 ret <- c(offset [rank + 1 ], items [rank + 1 ])
117121 } else ret <- NULL
0 commit comments