global pairwise | R Documentation |
This function provides global pairwise evaluations.
comm.pairwise(X, pairid.gbd = NULL,
FUN = function(x, y, ...){ return(as.vector(dist(rbind(x, y), ...))) },
..., diag = FALSE, symmetric = TRUE, comm = .pbd_env$SPMD.CT$comm)
X |
a common matrix across ranks, or a gbd matrix. (See details.) |
pairid.gbd |
a pair-wise id in a gbd format. (See details.) |
FUN |
a function to be evaluated for given pairs. |
... |
extra variables for |
diag |
if matching the same elements, |
symmetric |
if matching upper triangular elements. TRUE for
|
comm |
a communicator number. |
This function evaluates the objective function
FUN(X[i,], X[j, ])
(usually distance of two elements)
on any given pair (i, j)
of a matrix X
.
The input X
should be in common across all ranks if pairid.gbd
is provided, e.g. from comm.pairwise()
.
i.e. X
is exactly the same in every ranks, but
pairid.gbd
is different and in gbd format indicating the row pair
(i, j)
should be evaluated. The returning gbd matrix is ordered
and indexed by pairid.gbd
.
Note that checking consistence of X
across all ranks is not
implemented within this function since that drops performance and
may be not accurate.
The input X
should be a gbd format in row major blocks
(i.e. X.gbd
) if pairid.gbd
is NULL
. A internal
pair indices will be built implicitly for evaluation. The returning
gbd matrix is ordered and indexed by X.gbd
.
This function returns a common matrix with 3 columns
named i
, j
, and value
. Each value
is the
returned value and computed by FUN(X[i,], X[j,])
where
(i, j)
is the global index as ordered in a distance matrix
for i-th row and j-th columns.
Wei-Chen Chen wccsnow@gmail.com, George Ostrouchov, Drew Schmidt, Pragneshkumar Patel, and Hao Yu.
Programming with Big Data in R Website: https://pbdr.org/
comm.pairwise()
, and
comm.dist()
.
## Not run:
### Save code in a file "demo.r" and run with 2 processors by
### SHELL> mpiexec -np 2 Rscript demo.r
spmd.code <- "
### Initialize
suppressMessages(library(pbdMPI, quietly = TRUE))
### Examples.
comm.set.seed(123456, diff = FALSE)
X <- matrix(rnorm(10), ncol = 2)
id.matrix <- comm.allpairs(nrow(X))
### Method original.
dist.org <- dist(X)
### Method 1.
dist.common <- comm.pairwise(X, pairid.gbd = id.matrix)
### Method 2.
# if(comm.rank() != 0){
# X <- matrix(0, nrow = 0, ncol = 4)
# }
X.gbd <- comm.as.gbd(X) ### The other way.
dist.gbd <- comm.pairwise(X.gbd)
### Verify.
d.org <- as.vector(dist.org)
d.1 <- do.call(\"c\", allgather(dist.common[, 3]))
d.2 <- do.call(\"c\", allgather(dist.gbd[, 3]))
comm.print(all(d.org == d.1))
comm.print(all(d.org == d.2))
### Finish.
finalize()
"
# execmpi(spmd.code, nranks = 2L)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.