An example using mpich in a cluster (updated 10.2016)
Here is a C program that shows an example of setting up a different task for each processor core in a cluster of computers. It's very simple but shows the minimum needed to be useful.
/*
domath.c - test of mpich on multi-processor system
k theis 3/31/2016
to run:
mpicc -o domath domath.c -lm
mv domath /mnt/nfs/dist/domath
cd /mnt/nfs/dist/
mpiexec -n 6 -f machinefile ./domath
make sure machinefile exists in /mnt/nfs/dist
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <mpi.h>
#define PI 3.141592654
int do_sin(void){
double n, res;
FILE *sout;
int err;
sout = fopen("sin.txt","w");
if (sout == NULL) return 1;
fprintf(sout,"Sin Table\n\n");
err = system("/bin/uname -a > sin_s.txt"); // show machine this is on
n = 0.0;
while (n <= PI/2.0){
res = sin(n);
fprintf(sout,"\nN: %f Res: %f",n*(90.0/(PI/2.0)),res);
n += 1.0/90.0;
}
fprintf(sout,"\n");
fflush(sout);
fclose(sout);
return 0;
}
int do_cos(void){
double n, res;
FILE *cout;
int err;
cout = fopen("cos.txt","w");
if (cout == NULL) return 1;
fprintf(cout,"Cos Table\n\n");
err = system("/bin/uname -a > cos_s.txt"); // show machine this is on
n = 0.0;
while (n <= PI/2.0){
res = cos(n);
fprintf(cout,"\nN: %f Res: %f",n*(90.0/(PI/2.0)),res);
n += 1.0/90.0;
}
fprintf(cout,"\n");
fflush(cout);
fclose(cout);
return 0;
}
int do_tan(void){
double n, res;
FILE *tout;
int err;
tout = fopen("tan.txt","w");
if (tout == NULL) return 1;
fprintf(tout,"Tan Table\n\n");
err = system("/bin/uname -a > tan_s.txt"); // show machine this is on
n = 0.0;
while (n <= PI/2.0){
res = tan(n);
fprintf(tout,"\nN: %f Res: %f",n*(90.0/(PI/2.0)),res);
n += 1.0/90.0;
}
fprintf(tout,"\n");
fflush(tout);
fclose(tout);
return 0;
}
int do_log10(void){
double n, res;
FILE *l10out;
int err;
l10out = fopen("log10.txt","w");
if (l10out == NULL) return 1;
fprintf(l10out,"Log Table Base 10\n\n");
err = system("/bin/uname -a > log10_s.txt"); // show machine this is on
n = 0.0;
while (n <= 100.0){
res = log10(n);
fprintf(l10out,"\nN: %f Res: %f",n,res);
n += 0.5;
}
fprintf(l10out,"\n");
fflush(l10out);
fclose(l10out);
return 0;
}
int do_log(void){
double n, res;
FILE *lout;
int err;
lout = fopen("log.txt","w");
if (lout == NULL) return 1;
fprintf(lout,"Log Table\n\n");
err = system("/bin/uname -a > log_s.txt"); // show machine this is on
fprintf(lout,"\nsys err = %d",err);
n = 0.0;
while (n <= 100.0){
res = log(n);
fprintf(lout,"\nN: %f Res: %f",n,res);
n += 0.5;
}
fprintf(lout,"\n");
fflush(lout);
fclose(lout);
return 0;
}
int main(int argc, char **argv){
int my_id, root_process, ierr, num_procs;
MPI_Status status;
ierr = MPI_Init(&argc, &argv);
ierr = MPI_Comm_rank(MPI_COMM_WORLD, &my_id);
ierr = MPI_Comm_size(MPI_COMM_WORLD, &num_procs);
if (my_id == 0) printf("\n# of Processes: %d",num_procs);
if (my_id == 1) do_sin();
if (my_id == 2) do_cos();
if (my_id == 3) do_tan();
if (my_id == 4) do_log10();
if (my_id == 5) do_log();
if (my_id == 0) printf("\n");
ierr = MPI_Finalize();
exit(0);
}
/*
domath.c - test of mpich on multi-processor system
k theis 3/31/2016
to run:
mpicc -o domath domath.c -lm
mv domath /mnt/nfs/dist/domath
cd /mnt/nfs/dist/
mpiexec -n 6 -f machinefile ./domath
make sure machinefile exists in /mnt/nfs/dist
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <mpi.h>
#define PI 3.141592654
int do_sin(void){
double n, res;
FILE *sout;
int err;
sout = fopen("sin.txt","w");
if (sout == NULL) return 1;
fprintf(sout,"Sin Table\n\n");
err = system("/bin/uname -a > sin_s.txt"); // show machine this is on
n = 0.0;
while (n <= PI/2.0){
res = sin(n);
fprintf(sout,"\nN: %f Res: %f",n*(90.0/(PI/2.0)),res);
n += 1.0/90.0;
}
fprintf(sout,"\n");
fflush(sout);
fclose(sout);
return 0;
}
int do_cos(void){
double n, res;
FILE *cout;
int err;
cout = fopen("cos.txt","w");
if (cout == NULL) return 1;
fprintf(cout,"Cos Table\n\n");
err = system("/bin/uname -a > cos_s.txt"); // show machine this is on
n = 0.0;
while (n <= PI/2.0){
res = cos(n);
fprintf(cout,"\nN: %f Res: %f",n*(90.0/(PI/2.0)),res);
n += 1.0/90.0;
}
fprintf(cout,"\n");
fflush(cout);
fclose(cout);
return 0;
}
int do_tan(void){
double n, res;
FILE *tout;
int err;
tout = fopen("tan.txt","w");
if (tout == NULL) return 1;
fprintf(tout,"Tan Table\n\n");
err = system("/bin/uname -a > tan_s.txt"); // show machine this is on
n = 0.0;
while (n <= PI/2.0){
res = tan(n);
fprintf(tout,"\nN: %f Res: %f",n*(90.0/(PI/2.0)),res);
n += 1.0/90.0;
}
fprintf(tout,"\n");
fflush(tout);
fclose(tout);
return 0;
}
int do_log10(void){
double n, res;
FILE *l10out;
int err;
l10out = fopen("log10.txt","w");
if (l10out == NULL) return 1;
fprintf(l10out,"Log Table Base 10\n\n");
err = system("/bin/uname -a > log10_s.txt"); // show machine this is on
n = 0.0;
while (n <= 100.0){
res = log10(n);
fprintf(l10out,"\nN: %f Res: %f",n,res);
n += 0.5;
}
fprintf(l10out,"\n");
fflush(l10out);
fclose(l10out);
return 0;
}
int do_log(void){
double n, res;
FILE *lout;
int err;
lout = fopen("log.txt","w");
if (lout == NULL) return 1;
fprintf(lout,"Log Table\n\n");
err = system("/bin/uname -a > log_s.txt"); // show machine this is on
fprintf(lout,"\nsys err = %d",err);
n = 0.0;
while (n <= 100.0){
res = log(n);
fprintf(lout,"\nN: %f Res: %f",n,res);
n += 0.5;
}
fprintf(lout,"\n");
fflush(lout);
fclose(lout);
return 0;
}
int main(int argc, char **argv){
int my_id, root_process, ierr, num_procs;
MPI_Status status;
ierr = MPI_Init(&argc, &argv);
ierr = MPI_Comm_rank(MPI_COMM_WORLD, &my_id);
ierr = MPI_Comm_size(MPI_COMM_WORLD, &num_procs);
if (my_id == 0) printf("\n# of Processes: %d",num_procs);
if (my_id == 1) do_sin();
if (my_id == 2) do_cos();
if (my_id == 3) do_tan();
if (my_id == 4) do_log10();
if (my_id == 5) do_log();
if (my_id == 0) printf("\n");
ierr = MPI_Finalize();
exit(0);
}
Comments
Post a Comment