library(Rmpi)
mpi.spawn.Rslaves(needlog = FALSE)
mpi.bcast.cmd( id <- mpi.comm.rank() )
mpi.bcast.cmd( np <- mpi.comm.size() )
mpi.bcast.cmd( host <- mpi.get.processor.name() )
result <- mpi.remote.exec(paste("I am", id, "of", np, "running on", host))
print(unlist(result))
mpi.close.Rslaves(dellog = FALSE)
#include "mpi.h"
#include <stdio.h>
#include <math.h>
/* module load gcc */
/* module load mpich/ge/gcc/64/3.1 */
/* Compile me with 'mpicc -o cpi-parallel cpi-parallel.c' */
double f( double );
double f( double a )
{
return (4.0 / (1.0 + a*a));
}
int main( int argc, char *argv[])
{
int done = 0, n, myid, numprocs, i;
double PI25DT = 3.141592653589793238462643;
double mypi, pi, h, sum, x;
double startwtime = 0.0, endwtime;
int namelen;
char processor_name[MPI_MAX_PROCESSOR_NAME];
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
MPI_Comm_rank(MPI_COMM_WORLD,&myid);
MPI_Get_processor_name(processor_name,&namelen);
fprintf(stderr
,"Process %d on %s\n", myid, processor_name);
n = 0;
while (!done)
{
if (myid == 0)
{
/*
printf("Enter the number of intervals: (0 quits) ");
scanf("%d",&n);
*/
if (n==0) n=1024*numprocs; else n=0;
startwtime = MPI_Wtime();
}
MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
if (n == 0)
done = 1;
else
{
h = 1.0 / (double) n;
sum = 0.0;
for (i = myid + 1; i <= n; i += numprocs)
{
x = h * ((double)i - 0.5);
sum += f(x);
}
mypi = h * sum;
MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
if (myid == 0)
{
printf("pi is approximately %.16f, Error is %.16f\n", endwtime = MPI_Wtime();
printf("wall clock time = %f\n", endwtime-startwtime);
}
}
}
MPI_Finalize();
return 0;
}
bGlicmFyeShSbXBpKQptcGkuc3Bhd24uUnNsYXZlcyhuZWVkbG9nID0gRkFMU0UpCgptcGkuYm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ICAgIE1QSV9Db21tX3NpemUoTVBJX0NPTU1fV09STEQsJm51bXByb2NzKTsKICAgIE1QSV9Db21tX3JhbmsoTVBJX0NPTU1fV09STEQsJm15aWQpOwogICAgTVBJX0dldF9wcm9jZXNzb3JfbmFtZShwcm9jZXNzb3JfbmFtZSwmbmFtZWxlbik7CgogICAgZnByaW50ZihzdGRlcnIsIlByb2Nlc3MgJWQgb24gJXNcbiIsCgkgICAgbXlpZCwgcHJvY2Vzc29yX25hbWUpOwoKICAgIG4gPSAwOwogICAgd2hpbGUgKCFkb25lKQogICAgewogICAgICAgIGlmIChteWlkID09IDApCiAgICAgICAgewovKgogICAgICAgICAgICBwcmludGYoIkVudGVyIHRoZSBudW1iZXIgb2YgaW50ZXJ2YWxzOiAoMCBxdWl0cykgIik7CiAgICAgICAgICAgIHNjYW5mKCIlZCIsJm4pOwoqLwoJICAgIGlmIChuPT0wKSBuPTEwMjQqbnVtcHJvY3M7IGVsc2Ugbj0wOwoKCSAgICBzdGFydHd0aW1lID0gTVBJX1d0aW1lKCk7CiAgICAgICAgfQogICAgICAgIE1QSV9CY2FzdCgmbiwgMSwgTVBJX0lOVCwgMCwgTVBJX0NPTU1fV09STEQpOwogICAgICAgIGlmIChuID09IDApCiAgICAgICAgICAgIGRvbmUgPSAxOwogICAgICAgIGVsc2UKICAgICAgICB7CiAgICAgICAgICAgIGggICA9IDEuMCAvIChkb3VibGUpIG47CiAgICAgICAgICAgIHN1bSA9IDAuMDsKICAgICAgICAgICAgZm9yIChpID0gbXlpZCArIDE7IGkgPD0gbjsgaSArPSBudW1wcm9jcykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgeCA9IGggKiAoKGRvdWJsZSlpIC0gMC41KTsKICAgICAgICAgICAgICAgIHN1bSArPSBmKHgpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIG15cGkgPSBoICogc3VtOwoKICAgICAgICAgICAgTVBJX1JlZHVjZSgmbXlwaSwgJnBpLCAxLCBNUElfRE9VQkxFLCBNUElfU1VNLCAwLCBNUElfQ09NTV9XT1JMRCk7CgogICAgICAgICAgICBpZiAobXlpZCA9PSAwKQoJICAgIHsKICAgICAgICAgICAgICAgIHByaW50ZigicGkgaXMgYXBwcm94aW1hdGVseSAlLjE2ZiwgRXJyb3IgaXMgJS4xNmZcbiIsCiAgICAgICAgICAgICAgICAgICAgICAgcGksIGZhYnMocGkgLSBQSTI1RFQpKTsKCQllbmR3dGltZSA9IE1QSV9XdGltZSgpOwoJCXByaW50Zigid2FsbCBjbG9jayB0aW1lID0gJWZcbiIsCgkJICAgICAgIGVuZHd0aW1lLXN0YXJ0d3RpbWUpOwoJICAgIH0KICAgICAgICB9CiAgICB9CiAgICBNUElfRmluYWxpemUoKTsKCiAgICByZXR1cm4gMDsKfQ==