[Rinside-commits] r272 - in pkg: . inst inst/examples/mpi

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Jan 24 02:38:35 CET 2013


Author: edd
Date: 2013-01-24 02:38:35 +0100 (Thu, 24 Jan 2013)
New Revision: 272

Added:
   pkg/inst/examples/mpi/rinside_mpi_sample4.cpp
Modified:
   pkg/ChangeLog
   pkg/inst/NEWS.Rd
Log:
Added new example contributed by Nicholas Pezolano and Martin Morgan


Modified: pkg/ChangeLog
===================================================================
--- pkg/ChangeLog	2012-12-05 22:21:52 UTC (rev 271)
+++ pkg/ChangeLog	2013-01-24 01:38:35 UTC (rev 272)
@@ -1,3 +1,8 @@
+2013-01-23  Dirk Eddelbuettel  <edd at debian.org>
+
+	* inst/examples/mpi/rinside_mpi_sample4.cpp: Added new example
+	contributed by Nicholas Pezolano and Martin Morgan
+
 2012-12-05  Dirk Eddelbuettel  <edd at debian.org>
 
 	* DESCRIPTION: Release 0.2.10
@@ -19,7 +24,7 @@
 	* inst/examples/standard/rinside_sample9.cpp: Disabled as passing
 	external C/C++ function through simplified interface currently borked
 	* inst/examples/standard/rinside_module_sample0.cpp: Idem
-	
+
 2012-11-08  Dirk Eddelbuettel  <edd at debian.org>
 
 	* inst/examples/standard/rinside_sample11.cpp: added include for

Modified: pkg/inst/NEWS.Rd
===================================================================
--- pkg/inst/NEWS.Rd	2012-12-05 22:21:52 UTC (rev 271)
+++ pkg/inst/NEWS.Rd	2013-01-24 01:38:35 UTC (rev 272)
@@ -2,6 +2,13 @@
 \title{News for Package 'RInside'}
 \newcommand{\cpkg}{\href{http://CRAN.R-project.org/package=#1}{\pkg{#1}}}
 
+\section{Changes in RInside version 0.2.11 (2013-12-31)}{
+  \itemize{
+    \item Added new MPI example with worker functions and RInside
+    instance, kindly contributed by Nicholas Pezolano and Martin Morgan
+  }
+}
+
 \section{Changes in RInside version 0.2.10 (2012-12-05)}{
   \itemize{
     \item Adjusted to change in R which requires turning checking of the
@@ -9,7 +16,7 @@
     as in the Wt examples. As there are have been no side-effects, this
     is enabled by default on all platforms (with the exception of Windows).
     \item Added new \sQuote{threads} example directory with a simple
-    example based on a Boost mutex use example.
+    example based on a Boost mutex example.
     \item Disabled two examples (passing an external function down)
     which do not currently work; external pointer use should still work.
   }

Added: pkg/inst/examples/mpi/rinside_mpi_sample4.cpp
===================================================================
--- pkg/inst/examples/mpi/rinside_mpi_sample4.cpp	                        (rev 0)
+++ pkg/inst/examples/mpi/rinside_mpi_sample4.cpp	2013-01-24 01:38:35 UTC (rev 272)
@@ -0,0 +1,141 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4;  tab-width: 8; -*-
+//
+// Simple mpi example: Usage of RInside with a Master-Slave Model with worker
+//
+// MPI C API version of file contributed by Nicholas Pezolano and Martin Morgan 
+//
+// Copyright (C) 2010 - 2013  Dirk Eddelbuettel
+// Copyright (C)        2013  Nicholas Pezolano
+// Copyright (C)        2013  Martin Morgan
+//
+// GPL'ed 
+
+#include <mpi.h>
+#include <RInside.h>
+#include <string>
+#include <vector>
+#include <iostream>
+
+#define WORKTAG 1
+#define DIETAG 2
+
+/* Local functions */
+static void master(void);
+static void slave(RInside &R);
+static int get_next_work_item(int &work, const int size_work, std::vector<int> &data);
+static void do_work(int work,int &result,RInside &R);
+static void initalize(RInside &R);
+
+int itr = 0;
+
+int main(int argc, char **argv){
+    int myrank;
+    MPI_Init(&argc, &argv);
+    MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
+    RInside R(argc, argv); 
+
+
+    if (myrank == 0) {
+	master();
+    } else {
+	initalize(R);
+	slave(R);
+    }
+
+    MPI_Finalize();
+    return 0;
+}
+
+static void initalize(RInside &R){
+    //load the following R library on every R instance
+    std::string R_libs ="suppressMessages(library(random));";
+    R.parseEvalQ(R_libs); 
+}
+
+static void master(void){
+    int ntasks, rank;
+    std::vector<int> data;
+    int work;
+    int result;
+    int sum;
+    MPI_Status status;
+
+    //create some test "data" to pass around
+    for(int i = 0; i< 10; i++){
+	data.push_back(i);
+    }
+
+    const int size_work = (int)data.size();
+    
+    MPI_Comm_size(MPI_COMM_WORLD, &ntasks);
+
+    for (rank = 1; rank < ntasks; ++rank) {
+	get_next_work_item(work,size_work,data);
+	MPI_Send(&work,1,MPI_INT,rank, WORKTAG,MPI_COMM_WORLD);
+    }
+
+    int ret = get_next_work_item(work,size_work,data);
+    while (ret == 0) {
+	MPI_Recv(&result,1,MPI_INT,MPI_ANY_SOURCE,MPI_ANY_TAG,MPI_COMM_WORLD,&status);
+	sum += result;
+	MPI_Send(&work,1,MPI_INT,status.MPI_SOURCE,WORKTAG,MPI_COMM_WORLD);
+
+	ret = get_next_work_item(work,size_work,data);
+    }
+
+    for (rank = 1; rank < ntasks; ++rank) {
+	MPI_Recv(&result, 1, MPI_INT, MPI_ANY_SOURCE,
+		 MPI_ANY_TAG, MPI_COMM_WORLD, &status);
+	sum += result;
+    }
+
+    for (rank = 1; rank < ntasks; ++rank) {
+	MPI_Send(0, 0, MPI_INT, rank, DIETAG, MPI_COMM_WORLD);
+    }
+  
+    std::cout << "sum of all iterations = " << sum << std::endl;
+}
+
+static void slave(RInside &R) {
+    int work;
+    int result;
+    MPI_Status status;
+
+    while (1) {
+
+	MPI_Recv(&work, 1, MPI_INT, 0, MPI_ANY_TAG,
+		 MPI_COMM_WORLD, &status);
+
+	if (status.MPI_TAG == DIETAG) {
+	    return;
+	}
+
+	do_work(work,result,R);
+
+	MPI_Send(&result, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
+    }
+}
+
+
+static int get_next_work_item(int &work,const int size_work, std::vector<int> &data) {
+    if (itr >= size_work) {
+	return -1;
+    }
+
+    work = data[itr];  
+    
+    itr++;
+    std::cout << "iteration = " << itr << std::endl;  
+
+    return 0;
+}
+
+static void do_work(int work,int &result,RInside &R){
+
+    //create a random number on every slave iteration
+    R["work"] = work;
+    std::string Rcmd = "work <- sample(1:10, 1)";
+    Rcpp::NumericVector M = R.parseEval(Rcmd);
+    
+    result = M(0);
+}



More information about the Rinside-commits mailing list