Skip to content

Commit

Permalink
reverse left-right, documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
go-ski committed Jul 4, 2017
1 parent 228576b commit 85ac8ad
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
48 changes: 26 additions & 22 deletions R/comm.chunk.r
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
#' comm.chunk
#'
#' Given a total number of items comm.chunk splits the number into equal
#' chunks. Warious options are possible when the number does not split evenly
#' into chunks.
#' Given a total number of items, comm.chunk splits the number into
#' chunks. Tailored especially for situations in SPMD style
#' programming. Warious options are possible when the number does not
#' split evenly into chunks. The output form can be either a number or
#' a vector of items.
#'
#' @param N
#' The number of items to split into equal chunks.
#' The number of items to split into chunks.
#' @param form
#' Output a chunk as a sigle "number", as a "vector" of items from 1:N, or as
#' an "IOpair" giving offsets and lengths in a file.
#' Output a chunk as a single "number", as a "vector" of contiguous
#' items from 1:N, or as an "iopair" giving offset and length in a
#' file.
#' @param type
#' Either "balance" the chunks so they differ by no more than 1 item or force
#' as many as possible to be "equal" with possibly one or more smaller or even
#' zero size chunks.
#' Either "balance" the chunks so they differ by no more than 1 item (used most
#' frequently for best balance) or force as many as possible to be "equal" with
#' possibly one or more smaller or even zero size chunks (required by ScaLAPACK's
#' block-cyclic layouts).
#' @param lo.side
#' If exact balance is not possible, put the lower chunks on the "left" or on
#' the "right".
#' If exact balance is not possible, put the smaller chunks on the "left" (low ranks)
#' or on the "right" (high ranks).
#' @param all.rank
#' FALSE returns only the chunk output for rank r. TRUE returns a vector of
#' length p (when form="number"), and a list of length p (when form="vector")
#' each containing the output for the corresponding rank.
#' @param p
#' The number of chunks (defaults to comm.size()).
#' The number of chunks (processors) Defaults to comm.size().
#' @param rank
#' The rank of returned chunk (defaults to comm.rank()). Note that ranks are
#' numbered from 0 to p-1, whereas the list elements for all.rank=TRUE are
Expand All @@ -34,8 +38,8 @@
#'
#' @examples
#' ## Note that the p and rank parameters are provided by comm.size() and
#' ## comm.rank(), respectively, when running in parallel with pbdMPI and
#' ## need not be specified.
#' ## comm.rank(), respectively, when running SPMD in parallel. Normally, they
#' ## are not specified unless testing in serial mode.
#' library(pbdIO)
#'
#' comm.chunk(16, all.rank=TRUE, p=5)
Expand All @@ -44,18 +48,18 @@
#' comm.chunk(16, p=5, rank=0)
#'
#' @export
comm.chunk <- function(N, form="number", type="balance", lo.side="left",
comm.chunk <- function(N, form="number", type="balance", lo.side="right",
all.rank=FALSE, p=comm.size(), rank=comm.rank()) {

form <- comm.match.arg(tolower(form), c("number", "vector", "IOpair"))
type <- comm.match.arg(tolower(type), c("balance", "equal"))
lo.side <- comm.match.arg(tolower(lo.side), c("left", "right"))
form <- comm.match.arg(form, c("number", "vector", "iopair"))
type <- comm.match.arg(type, c("balance", "equal", "ldim", "bldim"))
lo.side <- comm.match.arg(lo.side, c("right", "left"))
if (!is.logical(all.rank) || length(all.rank) != 1 || is.na(all.rank))
comm.stop("argument 'all.rank' must be a bool")
if (!is.numeric(p) || p < 1)
comm.stop("argument 'p' must be a positive integer")
if (!is.numeric(rank) || rank < 0 || rank >= comm.size())
comm.stop("argument 'rank' must be an integer from 0 to comm.size()-1")
if (!is.numeric(rank) || rank < 0 || rank >= p)
comm.stop("argument 'rank' must be an integer from 0 to p-1")

p <- as.integer(p)
rank <- as.integer(rank)
Expand All @@ -75,7 +79,7 @@ comm.chunk <- function(N, form="number", type="balance", lo.side="left",
} else if(lo.side == "left") {
items[(p - rem + 1):p] <- base + 1
}
} else if(type == "equal") {
} else if(type == "equal" | type == "ldim") { ## fix for bldim!!!
items <- items + 1
rem <- p*(base + 1) - N
if(lo.side == "right") {
Expand Down Expand Up @@ -111,7 +115,7 @@ comm.chunk <- function(N, form="number", type="balance", lo.side="left",
if(all.rank) ret <- lapply(1:length(items_base),
function(i) lapply(items, seq_len)[[i]] + items_base[i])
else ret <- items_base[rank + 1] + seq_len(items[rank + 1])
} else if(form == "IOpair") {
} else if(form == "iopair") {
offset <- c(0, cumsum(items)[-p])
ret <- c(offset[rank + 1], items[rank + 1])
} else ret <- NULL
Expand Down
2 changes: 2 additions & 0 deletions demo/matrix.r
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ comm.print(names(beta))
a <- deltime(a, "lm.fit:")

xtx <- crossprod(xd_mm)
print(xtx)
xty <- crossprod(xd_mm, yd)
print(xty)

beta.coef <- solve(xtx, xty)
beta <- as.matrix(beta.coef)
Expand Down

0 comments on commit 85ac8ad

Please sign in to comment.