<div dir="ltr">Dear All,<br><br>Hi, <br>This is a prototype program which I am going to implement it in another program. I want to send objects. The program works very well when 'shapeset' is small. But when I increase number of CPU, and Shapeset size , I get the following error message:<br>
<br>"rank 0 in job 12 localhost.localdomain_60694 caused collective abort of all ranks<br> exit status of rank 0: killed by signal 9 "<br><br>I have a master process which works on Rank 0 and Slaves which are working on Other ranks. <br>
Master serialize (write all the information in a char buffer) a Shape object and send it through MPI::Send. Slaves are reciving the Objects and Deserialize it and call a function pointer to operate on the object. <br>Sorry the code is little bit long.<br>
<br>I really appreciate your help. I need some advices. I am working on Linux-Fedora with dual core cpu. <br><br>Sincerely,<br><br>-- <br>Gholamreza Sobhaninejad(Reza)<br>PhD candidate, University of Tokyo<br>Earthquake Research Institute<br>
Tel: 090-9807-2477<br><br><br>This is a code that I am trying to run it:<br>------------------------------------------------------------------------ <br>head.h :<br>#include <mpi.h><br>#include <stdio.h><br>#include <cstdlib><br>
#include <iostream><br>#include <string><br>#include <sstream><br>#include <fstream><br>#include <vector><br>#include <time.h><br><br>using namespace std;<br><br>const char delim = ' ';<br>
<br>#include "Node.h"<br>#include "Shape.h"<br><br>#include "MPI_Process.h"<br><br>------------------------------------------------------------------------<br>Node.h<br>#ifndef _NODE_H_<br>#define _NODE_H_<br>
<br>static const string Node_head="<\\Node>";<br>static const string Node_tail="<Node\\>";<br><br>class Node<br>{<br><br>public:<br> int id;<br> double x,y,z;<br> ///---- Constructur, Destructor, Copy Constructor<br>
<br> Node():x(0),y(0),z(0),id(0) {}<br> ~Node() {}<br> Node(int ID, double X=0, double Y=0, double Z=0) {id = ID; x=X; y=Y; z=Z;}<br> Node(const Node& anode) {id=<a href="http://anode.id">anode.id</a>; x=anode.x; y= anode.y; z=anode.z;}<br>
<br> //---- Mutator and Accessors<br> void SetID(int ID) {id = ID;}<br> void Set(int ID, double X=0, double Y=0, double Z=0) {id = ID; x=X; y=Y; z=Z;}<br><br> Node Get() {return *this;}<br><br><br><br><br> //---- Serialization and De serialization <br>
void Serialize(stringstream& out)<br> {<br> out << delim << Node_head << delim << this->id << delim << this->x << delim<br> << this->y << delim << this->z << delim <<Node_tail << '\n';<br>
}<br><br> void DeSerialize(stringstream& in)<br> {<br> string tmp;<br><br> in >> tmp >> this->id >>this->x >> this->y >> this->z >>tmp;<br><br> }<br>
<br><br>};<br><br>#endif<br>---------------------------------------------------------------------------------------------<br>MPI_Process.h :<br>#ifndef _MPI_PROCESS_H_<br>#define _MPI_PROCESS_H_<br><br>///---- Message Passing Tags<br>
<br>#define WORKTAG 1<br>#define LENTAG 3<br>#define DIETAG 2<br>#define SLAVE_GET_LENGTH 4<br>#define SLAVE_WORK_FINISH 5<br><br>class MPI_Process<br>{<br>private:<br> int* source_ID;<br> int* current_ID;<br> int* num_Process ;<br>
int* length ;<br><br> MPI::Status status;<br><br> void Probe_Message_Length();<br><br>public:<br> MPI_Process(void);<br> ~MPI_Process(void);<br><br> void Kill_Process(int rank);<br><br> //-- Set & Get<br>
<br> void Set_Num_Proc(int i);<br> int Get_Num_Proc();<br><br> void Set_SourceID(int i);<br> int Get_SourceID();<br><br> void Set_CurrentID(int i);<br> int Get_CurrentID();<br><br> int Send_Serializable_Object ( void* Object , <br>
int rank , <br> void (*pt2function)(void* Object,stringstream& stream)<br> );<br><br> int Recieve_Serializable_Object ( void* Object,<br>
void (*pt2function)(void* Object,stringstream& stream)<br> );<br><br>};<br><br><br>#endif<br>----------------------------------------------------------------------------------<br>
Shape.h :<br><br>#ifndef _SHAPE_H_<br>#define _SHAPE_H_<br><br><br>static const string Shape_head="<\\Shape>";<br>static const string Shape_tail="<Shape\\>";<br><br>string Int2Str(int num);<br>
<br><br>class Shape<br>{<br>public : <br> int id;<br> vector<Node> nodeset;<br><br> Shape() : id(0) {}<br> ~Shape() {nodeset.clear();}<br><br> Shape(const Shape& aShape);<br><br> void SetID(int ID) {id = ID;}<br>
void MakeShape();<br> void Serialize(stringstream& out);<br> void DeSerialize(stringstream& in);<br><br> void Write2File()<br> {<br> stringstream ss;<br><br> ofstream out( ("Shape" + Int2Str(this->id)+".shp").c_str());<br>
this->Serialize(ss);<br> out.write(ss.str().c_str(), ss.str().size());<br> out.close();<br> ss.clear();<br> }<br><br><br> static void Wrapper_To_MPI_SEND (void* Object, stringstream& out);<br>
<br> static void Wrapper_To_MPI_RECIVE (void* Object, stringstream& in);<br> <br>};<br><br>#endif<br>-------------------------------------------------------------------------------------------<br>MPI_Process.cpp :<br>
<br clear="all">#include "head.h"<br>#include <string.h><br>MPI_Process::MPI_Process(void){}<br><br>MPI_Process::~MPI_Process(void){}<br><br>void MPI_Process::Set_Num_Proc(int i)<br>{<br> this->num_Process = new int;<br>
*this->num_Process = i;<br>}<br><br>int MPI_Process::Get_Num_Proc()<br>{<br> return *this->num_Process;<br>}<br><br>void MPI_Process::Set_SourceID(int i)<br>{<br> this->source_ID = new int;<br> *this->source_ID = i;<br>
}<br><br>int MPI_Process::Get_SourceID()<br>{<br> return *this->source_ID;<br>}<br><br>void MPI_Process::Set_CurrentID(int i)<br>{<br> this->current_ID = new int; <br> *this->current_ID = i;<br>}<br><br>
void MPI_Process::Probe_Message_Length()<br>{<br> MPI::COMM_WORLD.Probe(*this->source_ID,WORKTAG,this->status);<br> this->length = new int(status.Get_count(MPI::CHAR) );<br>}<br><br><br>int MPI_Process::Send_Serializable_Object(void* Object, int rank, void (*pt2function)(void* Object,stringstream& stream))<br>
{<br> stringstream * stream;<br> char* buffer; <br> <br> int message_length;<br> <br> stream = new stringstream(); <br> <br> //-- Serialize <br> pt2function(Object,*stream);<br> <br> message_length = stream->str().length();<br>
buffer = new char[message_length];<br><br> <br> strcpy(buffer,stream->str().c_str());<br><br> MPI::COMM_WORLD.Send(buffer,message_length,MPI::CHAR,rank,WORKTAG);<br> <br> <br> delete stream;<br>
delete[] buffer;<br> <br> return message_length ;<br>}<br><br>int MPI_Process::Recieve_Serializable_Object(void* Object, void (*pt2function)(void* Object,stringstream& stream))<br>{ <br> stringstream * stream;<br>
char* buffer; <br><br> <br> this->Probe_Message_Length();<br> <br> buffer = new char[ *this->length ];<br> <br> if(*this->length == 1 ) cout << buffer <<endl;<br><br>
<br><br> MPI::COMM_WORLD.Recv(buffer,*this->length,MPI::CHAR, 0 ,WORKTAG);<br> <br> if(buffer[0] =='0') <br> {<br> delete stream;<br> delete[] buffer;<br> <br> return 0;<br>
}<br> else<br> {<br> stream = new stringstream(); <br><br> stream->str(buffer);<br><br> pt2function(Object,*stream);<br><br> stream->clear();<br> delete stream;<br>
delete[] buffer;<br> <br> <br> return *this->length;<br> }<br><br>}<br><br>void MPI_Process::Kill_Process(int rank)<br>{<br> char *buffer;<br> buffer = new char[1];<br>
buffer[0]='0';<br> cout << "Process No: " << rank << "is killed" << '\n';<br> MPI::COMM_WORLD.Send(&buffer,1,MPI::CHAR,rank,WORKTAG);<br>
delete[] buffer;<br> //MPI::COMM_WORLD.Sendrecv(&buffer,1,MPI::CHAR,rank,WORKTAG,&tmp,1,MPI::INT,rank,WORKTAG);<br>}<br>----------------------------------------------------------------------------------------------------------------------<br>
Shape.h :<br>#include "head.h"<br><br><br>Shape::Shape(const Shape& aShape)<br>{<br> /*this->id = aShape.id;<br> <br> for(vector<Node>::iterator i=aShape.nodeset.begin() ; i!=aShape.nodeset.end(); i++)<br>
this->nodeset.push_back(*i);*/<br>}<br>string Int2Str(int num)<br>{<br> stringstream ss;<br> ss << num;<br> return ss.str();<br>}<br><br>void Shape::MakeShape()<br>{<br> Node n;<br> for(register int i=0;i<10;i++) <br>
{<br> //n.Set(i,0.01*i,1.0*i,2.0*i);<br> n.Set(rand()%100,rand()%100,rand()%100,rand()%100);<br> this->nodeset.push_back(n);<br> }<br>}<br><br>void Shape::Serialize(stringstream& out)<br>
{<br> cout << "Start of Serialization Processes NO " << MPI::COMM_WORLD.Get_rank() <<endl;<br><br> out << Shape_head << '\n' <<delim << this->id << delim << (int) this->nodeset.size() << '\n';<br>
for(vector<Node>::iterator i=this->nodeset.begin(); i!= this->nodeset.end();i++) <br> i->Serialize(out);<br> out << Shape_tail <<'\n' ;<br>}<br><br>void Shape::DeSerialize(stringstream& in)<br>
{<br> cout << "Start of DeSerialization Processes NO " << MPI::COMM_WORLD.Get_rank() <<endl;<br><br> string tmp;<br> int number_of_nodes;<br> in >> tmp >> this->id >> number_of_nodes;<br>
this->nodeset.resize(number_of_nodes);<br> for(vector<Node>::iterator i=this->nodeset.begin(); i!=this->nodeset.end();i++) <br> i->DeSerialize(in);<br> in >> tmp; <br>}<br><br><br>
<br>void Shape::Wrapper_To_MPI_SEND(void* Object, stringstream& out)<br>{<br> Shape* myself = (Shape*) Object;<br> myself->Serialize(out);<br>}<br><br><br>void Shape::Wrapper_To_MPI_RECIVE(void *Object, std::stringstream& in)<br>
{<br> Shape* myself = (Shape*) Object;<br> myself->DeSerialize(in);<br> myself->Write2File();<br>}<br><br><br>-------------------------------------------------------------------------------------<br><br>main.cpp :<br>
#include "head.h"<br><br><br><br>void Slave()<br>{<br><br> MPI_Process *slave_process = new MPI_Process();<br> <br> slave_process->Set_SourceID(0);<br> slave_process->Set_CurrentID(MPI::COMM_WORLD.Get_rank());<br>
<br> Shape shape;<br><br> while(slave_process->Recieve_Serializable_Object( (void*) &shape, Shape::Wrapper_To_MPI_RECIVE) != 0)<br> {<br> cout << "Slave No: " << MPI::COMM_WORLD.Get_size() << " Finished Work." <<endl; <br>
}<br> cout << "Slave No: " << MPI::COMM_WORLD.Get_size() << " Quit." <<endl; <br>}<br><br> void Master(vector<Shape>& shapeset)<br>{<br> MPI_Process* master_Process = new MPI_Process();<br>
<br> int check(0),rank(1),nproc,counter(0);<br><br> master_Process ->Set_Num_Proc(MPI::COMM_WORLD.Get_size());<br> master_Process->Set_CurrentID(0);<br> master_Process->Set_SourceID(0);<br> nproc = master_Process->Get_Num_Proc();<br>
<br> <br> <br> vector<Shape>::iterator i(shapeset.begin());<br> cout << shapeset.size() << endl;<br> <br> for(counter; counter < (int) shapeset.size();counter++,rank++)<br>
{<br> if(i!=shapeset.end()) <br> {<br> if(rank > nproc-1) rank =1;<br> cout << "Master Send a Message with Lenght of :" << <br> master_Process->Send_Serializable_Object((void*) &(*i) ,rank, Shape::Wrapper_To_MPI_SEND) <br>
<< " to CPU Rank:" << rank <<endl;<br> i++;<br> }<br> }<br><br> cout <<" Start of Turning off System!" << endl;<br><br> for(rank=1; rank< nproc ; ++rank)<br>
{<br> cout << "sending Kill Message to Proc: " << rank <<endl;<br> master_Process->Kill_Process(rank);<br> }<br>}<br><br><br><br><br><br>int main(int argc, char ** argv)<br>
{<br> srand( (int) time(NULL));<br> <br><br> int rank,nproc,nwork;<br><br> //--- Start of MPI CALL<br> <br> MPI::Status status;<br> MPI::Init(argc,argv);<br><br> nproc=MPI::COMM_WORLD.Get_size();<br>
rank = MPI::COMM_WORLD.Get_rank();<br> <br> if(rank==0) <br> {<br> vector<Shape> shapeset;<br><br> shapeset.resize(10000);<br> int j=0;<br> for( vector<Shape>::iterator i=shapeset.begin(); i!=shapeset.end(); i++, ++j)<br>
i->SetID(j);<br><br> for( vector<Shape>::iterator i=shapeset.begin(); i!=shapeset.end(); i++)<br> i->MakeShape();<br> <br> cout << "Shape Initialization Finished" << endl;<br>
<br> cout << "Processor NO: " << rank << " Started" <<endl;<br><br> Master(shapeset);<br> }<br> else<br> {<br> cout << "Processor NO: " << rank << " Started" <<endl;<br>
Slave();<br> }<br> <br> MPI::Finalize();<br><br><br> return 0;<br>}<br><br><br><br>
</div>