An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120207/5ba3aa38/attachment.pl>
using mclapply (multi core apply) to do matrix multiplication
10 messages · Alaios, Rainer M Krug, Ernest Adrogué +1 more
7-02-2012, 00:29 (-0800); Alaios escriu:
Dear all, I am trying to multiply three different matrices and each matrice is of size 16384,16384 the normal %*% multiplciation operator has not finished one day now. As I am running a system with many cores (and it seems that R is using only one of those) I would like to write fast a brief function that converts the typical for loops of a matrix multiplication to a set of lapply sets (mclapply uses the lapply syntax but it applies the work to many cores). If my thinking is correct , in the sense that this will speed up things a lot, I want you to help me covert the first matrix in rows the second in columns and convert those in? a format that lapply would like to work with.
If I understand correctly, R uses a specialized library called BLAS to do matrix multiplications. I doubt re-implementing the matrix multiplication code at R-level would be any faster. What you can try is replace BLAS with a multicore version of BLAS although it's not easy if you have to compile it yourself. Also, you may try to re-think the problem you're trying to solve. Maybe there's a different approach that is less computation-intensive.
Cheers, Ernest
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120207/5e12eb30/attachment.pl>
On 07/02/12 11:31, Alaios wrote:
I would like to thank you Ernest for your answer. I guess that this is gonna be faster as right now R only sees one core. In my work there is a system with 64 cores and you can see only one working. If I understand it right a [m,n][n,k] matrix multiplication can be split into rows (from first matrice) and columns (from the second matrice) and then combine all the local results of each cpu together.
You definitaly can go this way, but I would STRONGLY recommend to search for "parallel BLAS", check in the R-admin manual the section "Linear Algebra" which deals with BLAS et al, and e.g. http://www.r-bloggers.com/compiling-64-bit-r-2-10-1-with-mkl-in-linux/ My guess is that a paralelization on the C level in the BLAS et al. library will be MUCH faster then a paralelization on R level. Also, there is a R-sig-hpc mailing list for these kind of questions. Cheers, Rainer
Would that be too weird for mclapply to handle? B.R Alex
________________________________ From: Ernest Adrogu?<nfdisco at gmail.com> To: r-help at r-project.org Sent: Tuesday, February 7, 2012 11:02 AM Subject: Re: [R] using mclapply (multi core apply) to do matrix multiplication 7-02-2012, 00:29 (-0800); Alaios escriu: Dear all, I am trying to multiply three different matrices and each matrice is of size 16384,16384 the normal %*% multiplciation operator has not finished one day now. As I am running a system with many cores (and it seems that R is using only one of those) I would like to write fast a brief function that converts the typical for loops of a matrix multiplication to a set of lapply sets (mclapply uses the lapply syntax but it applies the work to many cores). If my thinking is correct , in the sense that this will speed up things a lot, I want you to help me covert the first matrix in rows the second in columns and convert those in a format that lapply would like to work with. If I understand correctly, R uses a specialized library called BLAS to do matrix multiplications. I doubt re-implementing the matrix multiplication code at R-level would be any faster. What you can try is replace BLAS with a multicore version of BLAS although it's not easy if you have to compile it yourself. Also, you may try to re-think the problem you're trying to solve. Maybe there's a different approach that is less computation-intensive. ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology, UCT), Dipl. Phys. (Germany) Centre of Excellence for Invasion Biology Stellenbosch University South Africa Tel : +33 - (0)9 53 10 27 44 Cell: +33 - (0)6 85 62 59 98 Fax : +33 - (0)9 58 10 27 44 Fax (D): +49 - (0)3 21 21 25 22 44 email: Rainer at krugs.de Skype: RMkrug
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120207/9ae36496/attachment.pl>
On 07/02/12 12:02, Alaios wrote:
Thank you very much for your point... I hope I can find some easy to follow instructions as I do not have root permission
Me neither on pur cluster - but that won't stop you from compiling and installing R in your home directory. By doing this, you have even more control. Cheers and good luck, Rainer for the many cores system and our system administrator want
to have easy instructions to follow. Thanks a gain. ------------------------------------------------------------------------ *From:* Rainer M Krug <r.m.krug at gmail.com> *To:* Alaios <alaios at yahoo.com> *Cc:* Ernest Adrogu? <nfdisco at gmail.com>; "r-help at r-project.org" <r-help at r-project.org> *Sent:* Tuesday, February 7, 2012 11:44 AM *Subject:* Re: [R] using mclapply (multi core apply) to do matrix multiplication On 07/02/12 11:31, Alaios wrote:
> I would like to thank you Ernest for your answer. I guess that this > is gonna be faster as right now R only sees one core. In my work > there is a system with 64 cores and you can see only one working. If > I understand it right a [m,n][n,k] matrix multiplication can be split > into rows (from first matrice) and columns (from the second matrice) > and then combine all the local results of each cpu together.
You definitaly can go this way, but I would STRONGLY recommend to search for "parallel BLAS", check in the R-admin manual the section "Linear Algebra" which deals with BLAS et al, and e.g. http://www.r-bloggers.com/compiling-64-bit-r-2-10-1-with-mkl-in-linux/ My guess is that a paralelization on the C level in the BLAS et al. library will be MUCH faster then a paralelization on R level. Also, there is a R-sig-hpc mailing list for these kind of questions. Cheers, Rainer
> > Would that be too weird for mclapply to handle? > > B.R Alex > > > > ________________________________ From: Ernest > Adrogu?<nfdisco at gmail.com <mailto:nfdisco at gmail.com>> To:
r-help at r-project.org <mailto:r-help at r-project.org> Sent: Tuesday,
> February 7, 2012 11:02 AM Subject: Re: [R] using mclapply (multi core > apply) to do matrix multiplication > > 7-02-2012, 00:29 (-0800); Alaios escriu:
>> Dear all, I am trying to multiply three different matrices and >> each matrice is of size 16384,16384 the normal %*% multiplciation >> operator has not finished one day now. As I am running a system >> with many cores (and it seems that R is using only one of those) I >> would like to write fast a brief function that converts the typical >> for loops of a matrix multiplication to a set of lapply sets >> (mclapply uses the lapply syntax but it applies the work to many >> cores). >> >> If my thinking is correct , in the sense that this will speed up >> things a lot, I want you to help me covert the first matrix in >> rows the second in columns and convert those in a format that >> lapply would like to work with.
> > If I understand correctly, R uses a specialized library called BLAS > to do matrix multiplications. I doubt re-implementing the matrix > multiplication code at R-level would be any faster. What you can try > is replace BLAS with a multicore version of BLAS although it's not > easy if you have to compile it yourself. > > Also, you may try to re-think the problem you're trying to solve. > Maybe there's a different approach that is less > computation-intensive. > > > > > ______________________________________________ R-help at r-project.org
<mailto:R-help at r-project.org>
> mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do > read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
-- Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology, UCT), Dipl. Phys. (Germany) Centre of Excellence for Invasion Biology Stellenbosch University South Africa Tel : +33 - (0)9 53 10 27 44 Cell: +33 - (0)6 85 62 59 98 Fax : +33 - (0)9 58 10 27 44 Fax (D): +49 - (0)3 21 21 25 22 44 email: Rainer at krugs.de <mailto:Rainer at krugs.de> Skype: RMkrug
Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology, UCT), Dipl. Phys. (Germany) Centre of Excellence for Invasion Biology Stellenbosch University South Africa Tel : +33 - (0)9 53 10 27 44 Cell: +33 - (0)6 85 62 59 98 Fax : +33 - (0)9 58 10 27 44 Fax (D): +49 - (0)3 21 21 25 22 44 email: Rainer at krugs.de Skype: RMkrug
7-02-2012, 02:31 (-0800); Alaios escriu:
I would like to thank you Ernest for your answer. I guess that this is gonna be faster as right now R only sees one core. In my work there is a system with 64 cores and you can see only one working. If I understand it right a [m,n][n,k] matrix multiplication can be split into rows (from first matrice) and columns (from the second matrice) and then combine all the local results of each cpu together. Would that be too weird for mclapply to handle?
I never used mclapply, but anyway here's a matrix multiplication
function that uses lapply. Because the two lapply's are nested I don't
think you can parallelize the two... I would only make the second one
work with multiple cores
mmult <- function(a, b) {
a <- as.matrix(a)
b <- as.matrix(b)
if (ncol(a) != nrow(b))
stop('non-conforming matrices')
out <- lapply(1:ncol(b), function(j)
lapply(1:nrow(a), function(i) sum(a[i,] * b[,j])))
array(unlist(out), c(nrow(a), ncol(b)))
}
Also, I'm pretty sure that there are better algorithms.
If you do this it would be interesting if you measured the execution
time of the different alternatives and post the results :)
Cheers, Ernest
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120207/3fd9b5bf/attachment.pl>
7-02-2012, 03:32 (-0800); Alaios escriu:
I wouldl ike to thank you for your response. The hardest part in the installation is to find a BLAS library to install. If I understand it right once I install BLAS then I only need to change a flag in the ./configure of R installation.. Our system is running opensuse and has intel cores. according to the link here http://cran.r-project.org/doc/manuals/R-admin.html#BLAS I ahve to find a proper BLAS library to installll.. In the explanation for the different alternatives seem that most of those are not implemented any more and other require special configuration :(
This article includes an overview of different BLAS libraries along with benchmarks: http://cran.r-project.org/web/packages/gcbd/vignettes/gcbd.pdf It looks like using single-threaded ATLAS is already an improvement over LAPACK in most cases. I use Debian and it's straightforward to replace one with the other: you only have to install the libatlas3gf-base package and remove liblapack3gf and libblas3gf. Unfortunately, Debian does not include a multi-threaded version of ATLAS although they provide instructions on how to recompile the package yourself with multi-threading enabled. I don't know about SUSE, sorry.
Bye, Ernest
What is the nature of the matrices? Are they sparse or derived
from sparse matrices? If they are sparse, have you looked at the
packages available in R for sparse matrices?
library(sos)
summary(sp <- findFn('sparse', 999))
will identify help pages in contributed packages containing "sparse".
The primary one is "Matrix", but there are others.
If they are not sparse but are derived from sparse matrices, you
might be able to do some theoretical work. Of course, this only makes
sense if you have a specific class of problems that generates the
matrices, which seems plausible since you said you had square matrices
of dimension 2^14.
Hope this helps.
Spencer
On 2/7/2012 4:36 AM, Ernest Adrogu? wrote:
7-02-2012, 03:32 (-0800); Alaios escriu:
I wouldl ike to thank you for your response. The hardest part in the installation is to find a BLAS library to install. If I understand it right once I install BLAS then I only need to change a flag in the ./configure of R installation.. Our system is running opensuse and has intel cores. according to the link here http://cran.r-project.org/doc/manuals/R-admin.html#BLAS I ahve to find a proper BLAS library to installll.. In the explanation for the different alternatives seem that most of those are not implemented any more and other require special configuration :(
This article includes an overview of different BLAS libraries along with benchmarks: http://cran.r-project.org/web/packages/gcbd/vignettes/gcbd.pdf It looks like using single-threaded ATLAS is already an improvement over LAPACK in most cases. I use Debian and it's straightforward to replace one with the other: you only have to install the libatlas3gf-base package and remove liblapack3gf and libblas3gf. Unfortunately, Debian does not include a multi-threaded version of ATLAS although they provide instructions on how to recompile the package yourself with multi-threading enabled. I don't know about SUSE, sorry.
Spencer Graves, PE, PhD President and Chief Technology Officer Structure Inspection and Monitoring, Inc. 751 Emerson Ct. San Jos?, CA 95126 ph: 408-655-4567 web: www.structuremonitoring.com