[Rinside-commits] r108 - in pkg/inst/examples: . mpi
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Thu Feb 18 03:12:13 CET 2010
Author: edd
Date: 2010-02-18 03:12:13 +0100 (Thu, 18 Feb 2010)
New Revision: 108
Added:
pkg/inst/examples/mpi/
pkg/inst/examples/mpi/Makefile
pkg/inst/examples/mpi/rinside_mpi_sample0.cpp
pkg/inst/examples/mpi/rinside_mpi_sample1.cpp
Log:
add mpi examples initially contributed by Jianping Hua to rcpp-devel on Feb 8, 2010
Added: pkg/inst/examples/mpi/Makefile
===================================================================
--- pkg/inst/examples/mpi/Makefile (rev 0)
+++ pkg/inst/examples/mpi/Makefile 2010-02-18 02:12:13 UTC (rev 108)
@@ -0,0 +1,49 @@
+## -*- mode: make; tab-width: 8; -*-
+##
+## Simple Makefile for MPI use of RInside
+##
+## TODO:
+## proper configure for non-Debian file locations, [ Done ]
+## allow RHOME to be set for non-default R etc [ Done ]
+
+## comment this out if you need a different version of R,
+## and set set R_HOME accordingly as an environment variable
+R_HOME := $(shell R RHOME)
+
+sources := $(wildcard *.cpp)
+programs := $(sources:.cpp=)
+
+
+# OpenMPI header and libraries
+MPICPPFLAGS := $(shell mpic++ -showme:compile)
+MPILIBS := $(shell mpic++ -showme:link)
+
+## include headers and libraries for R
+RCPPFLAGS := $(shell $(R_HOME)/bin/R CMD config --cppflags)
+RLDFLAGS := $(shell $(R_HOME)/bin/R CMD config --ldflags)
+RBLAS := $(shell $(R_HOME)/bin/R CMD config BLAS_LIBS)
+RLAPACK := $(shell $(R_HOME)/bin/R CMD config LAPACK_LIBS)
+
+
+## include headers and libraries for Rcpp interface classes
+RCPPINCL := $(shell echo 'Rcpp:::CxxFlags()' | $(R_HOME)/bin/R --vanilla --slave)
+RCPPLIBS := $(shell echo 'Rcpp:::LdFlags()' | $(R_HOME)/bin/R --vanilla --slave)
+
+
+## include headers and libraries for RInside embedding classes
+RINSIDEINCL := $(shell echo 'RInside:::CxxFlags()' | $(R_HOME)/bin/R --vanilla --slave)
+RINSIDELIBS := $(shell echo 'RInside:::LdFlags()' | $(R_HOME)/bin/R --vanilla --slave)
+
+
+## compiler etc settings used in default make rules
+CXX := $(shell $(R_HOME)/bin/R CMD config CXX)
+CPPFLAGS := -Wall $(shell $(R_HOME)/bin/R CMD config CPPFLAGS)
+CXXFLAGS := $(MPICPPFLAGS) $(RCPPFLAGS) $(RCPPINCL) $(RINSIDEINCL) $(shell $(R_HOME)/bin/R CMD config CXXFLAGS)
+LDFLAGS = -s
+LDLIBS := $(MPILIBS) $(RLDFLAGS) $(RBLAS) $(RLAPACK) $(RCPPLIBS) $(RINSIDELIBS)
+
+all : $(programs)
+
+clean:
+ rm -vf $(programs)
+
Added: pkg/inst/examples/mpi/rinside_mpi_sample0.cpp
===================================================================
--- pkg/inst/examples/mpi/rinside_mpi_sample0.cpp (rev 0)
+++ pkg/inst/examples/mpi/rinside_mpi_sample0.cpp 2010-02-18 02:12:13 UTC (rev 108)
@@ -0,0 +1,32 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8; -*-
+//
+// Simple example showing in R console information about current node
+//
+// This file was contributed by Jianping Hua
+
+#include "mpi.h" // mpi header
+#include "RInside.h" // for the embedded R via RInside
+#include "Rcpp.h" // for the R / Cpp interface used for transfer
+
+int main(int argc, char *argv[]) {
+
+ // mpi related
+ int myrank, nodesize; // node information
+ MPI_Init(&argc,&argv); // mpi initialization
+ MPI_Comm_rank(MPI_COMM_WORLD, &myrank); // obtain current node rank
+ MPI_Comm_size(MPI_COMM_WORLD, &nodesize); // obtain total nodes running.
+
+ RInside R(argc, argv); // create an embedded R instance
+
+ std::stringstream txt;
+ txt << "Hello from node " << myrank // node information
+ << " of " << nodesize << " nodes!" << std::endl;
+ R.assign( txt.str(), "txt"); // assign string var to R variable 'txt'
+
+ std::string evalstr = "cat(txt)"; // show node information
+ R.parseEvalQ(evalstr); // eval the init string, ignoring any returns
+
+ MPI_Finalize(); // mpi finalization
+
+ exit(0);
+}
Added: pkg/inst/examples/mpi/rinside_mpi_sample1.cpp
===================================================================
--- pkg/inst/examples/mpi/rinside_mpi_sample1.cpp (rev 0)
+++ pkg/inst/examples/mpi/rinside_mpi_sample1.cpp 2010-02-18 02:12:13 UTC (rev 108)
@@ -0,0 +1,76 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8; -*-
+//
+// Simple mpi example: simulate sampling/averaging on multiple nodes and gathering the results.
+//
+// This file was contributed by Jianping Hua
+
+#include "mpi.h" // mpi header file
+#include "RInside.h" // for the embedded R via RInside
+#include "Rcpp.h" // for the R / Cpp interface used for transfer
+
+int main(int argc, char *argv[]) {
+
+ // mpi related
+ int myrank, nodesize; // node information
+ int sndcnt = 1, rcvcnt = 1; // # of elements in send/recv buffer
+ MPI_Init(&argc,&argv); // mpi initialization
+ MPI_Comm_rank(MPI_COMM_WORLD, &myrank); // obtain current node rank
+ MPI_Comm_size(MPI_COMM_WORLD, &nodesize); // obtain total nodes running.
+ double sendValue; // value to be collected in current node
+ double *allvalues = new double[nodesize]; // to save all results
+
+ // simulation info
+ // to sample from a uniform distribution
+ int rangeMin = 0, rangeMax = 10; // range of uniform distribution
+ int sampleSize = 2; // points in each sample
+
+ try {
+ RInside R(argc, argv); // create an embedded R instance
+ SEXP ans; // return value
+
+ std::stringstream txt;
+ txt << "x <- " << rangeMin << std::endl;
+ R.parseEvalQ( txt.str() ); // assign x with lower range of uniform distribution
+
+ txt << "y <- " << rangeMax << std::endl;
+ R.parseEvalQ( txt.str() ); // assign y with upper range of uniform distribution
+
+ txt << "n <- " << sampleSize << std::endl;
+ R.parseEvalQ( txt.str() ); // assign n with the size of sample
+
+ std::string evalstr = " mean(runif(n,x,y))"; // sampling, compute the mean
+ if ( R.parseEval(evalstr, ans) ) // eval the evalstr string, return results
+ throw std::runtime_error( "R cannot evalueate '" + evalstr + "'" );
+
+ RcppVector<double> m(ans); // convert SEXP variable to an RcppVector
+
+ sendValue = m( 0 ); // assign the return value to the variable to be gathered
+
+ //gather together values from all processes to allvalues
+ MPI_Gather(&sendValue, sndcnt, MPI_DOUBLE, allvalues, rcvcnt, MPI_DOUBLE, 0, MPI_COMM_WORLD);
+
+ // show what inidividual node's contribution
+ std::cout << "node " << myrank << " has mean " << m(0) << std::endl;
+
+ } catch(std::exception& ex) {
+ std::cerr << "Exception caught: " << ex.what() << std::endl;
+ } catch(...) {
+ std::cerr << "Unknown exception caught" << std::endl;
+ }
+
+ // show gathered results in node 0
+ if ( myrank == 0 ) {
+ std::cout << "values of all " << nodesize << " trials: " << std::endl;
+ for ( int i = 0; i < nodesize; i++ )
+ std::cout << allvalues[ i ] << ", ";
+ std::cout << std::endl;
+ }
+
+ // clean up
+ delete[] allvalues;
+
+ MPI_Finalize(); // mpi finalization
+
+ exit(0);
+}
+
More information about the Rinside-commits
mailing list