Hi,
This is a hello world level program & question.
Questions
=========
1. The program seems to be running twice, i.e each line of output is
seen twice. why is this so?
2. The node ranks are confusing me..!!
Setup
=====
4 nodes, 3 single + 1 dual processor machine, all running LAM 7.0.6/MPI2
Linux kernels on 3 machines are 2.4.22, and one on 2.4.15.
Code
======
#include <iostream>
#include <fstream>
#include <string>
#include <mpi.h>
#include <mpi++.h>
using namespace std;
int main(int argc, char* argv[])
{
cout<<"Now starting mpi stuff..\n";
MPI_Init(&argc,&argv);
char* processor_name = new char [MPI_MAX_PROCESSOR_NAME];
int namelen;
MPI_Get_processor_name(processor_name,&namelen);
int rank = MPI_Comm_rank(MPI_COMM_WORLD,&rank); //rank of process
int p = MPI_Comm_size(MPI_COMM_WORLD,&p); //number of processes
//cerr<<"Hello World! I am Node rank "<<rank<<" . Number of processes
"<<p<<". Machine : "<<processor_name<<endl;
//Message passing vars
int tag=0; //tag for messages to print to a file
MPI_Status status;
int destrank=0;
cout<<"My rank before if is "<<rank<<endl;
cout<<"My name before if is "<<processor_name<<endl;
if(rank=!0)
{
cout<<"My rank in if is "<<rank<<endl;
cout<<"My name in if is "<<processor_name<<endl;
cout<<endl;
ofstream opfile("ishere.txt",ios::app);
opfile<<rank<<" Hi!!"<<endl;;
int a=10;
MPI_Send(&a,1,MPI_INT,destrank,0,MPI_COMM_WORLD);
}
else
{
cerr<<"here!!"<<endl;
int b;
MPI_Recv(&b,1,MPI_INT,1,0,MPI_COMM_WORLD,&status);
cout<<"Message received "<<b<<endl;
ofstream opfile("memmsg.txt");
opfile<<b;
}
MPI_Finalize();
return 0;
}
Commands
========
mpiCC -o hello hello.o -lmpi
mpirun -v -s n0 N C hello
lamnodes o/p
============
n0 memory:1:origin,this_node
n1 coral:1:
n2 max:2:
n3 liang:1:
Output
=======
-----------------
3995 hello running on n0 (o)
25678 hello running on n1
18686 hello running on n2
6519 hello running on n3
3996 hello running on n0 (o)
25679 hello running on n1
18687 hello running on n2
18688 hello running on n2
6520 hello running on n3
Now starting mpi stuff..
My rank before if is 0
My name before if is memory
My rank in if is 1
My name in if is memory
Now starting mpi stuff..
My rank before if is 0
My name before if is liang
My rank in if is 1
Now starting mpi stuff..
My rank before if is 0
My name before if is max
My rank in if is 1
My name in if is max
My name in if is liang
Now starting mpi stuff..
My rank before if is 0
My name before if is max
My rank in if is 1
My name in if is max
Now starting mpi stuff..
My rank before if is 0
My name before if is coral
My rank in if is 1
My name in if is coral
Now starting mpi stuff..
My rank before if is 0
My name before if is liang
My rank in if is 1
My name in if is liang
Now starting mpi stuff..
My rank before if is 0
My name before if is max
My rank in if is 1
My name in if is max
Now starting mpi stuff..
My rank before if is 0
My name before if is memory
My rank in if is 1
My name in if is memory
Now starting mpi stuff..
My rank before if is 0
My name before if is coral
My rank in if is 1
My name in if is coral
------------------------
Thanks,
Aditya Datey
Research Assistant
MEMLab, Department of Psychology
Syracuse University
http://memory.syr.edu
|