6 #define EPSILON 0.00000001
8 int main(int argc, char**argv)
11 double *vector, sum=0., norm, etalon;
12 int rank, size, grank, gsize, rsize;
13 int vsize=20, lvsize, rlvsize;
14 int i, k1, k2, imin, imax, nb;
18 char port_name [MPI_MAX_PORT_NAME];
19 char port_name_clt [MPI_MAX_PORT_NAME];
20 std::string service = "SERVICE";
24 std::string sargv = argv[i];
25 if(sargv.find("-debug")!=std::string::npos)
27 else if(sargv.find("-vsize")!=std::string::npos)
28 vsize = atoi(argv[++i]);
31 MPI_Init( &argc, &argv );
33 MPI_Comm_size( MPI_COMM_WORLD, &size );
34 MPI_Comm_rank( MPI_COMM_WORLD, &rank );
36 MPI_Barrier(MPI_COMM_WORLD);
38 MPI_Errhandler_set(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
40 MPI_Open_port(MPI_INFO_NULL, port_name);
41 if ( MPI_Lookup_name((char*)service.c_str(), MPI_INFO_NULL, port_name_clt) == MPI_SUCCESS ) {
43 std::cout << "[" << rank << "] I am client: I get the service " << service << " !" << std::endl;
44 MPI_Close_port( port_name );
46 else if ( MPI_Publish_name((char*)service.c_str(), MPI_INFO_NULL, port_name) == MPI_SUCCESS ) {
48 std::cout << "[" << rank << "] I am server: I've managed to publish the service " << service << " !" << std::endl;
51 else if ( MPI_Lookup_name((char*)service.c_str(), MPI_INFO_NULL, port_name_clt) == MPI_SUCCESS ) {
53 std::cout << "[" << rank << "] I am client: I get the service " << service << " !" << std::endl;;
54 MPI_Close_port( port_name );
58 std::cout << "[" << rank << "] ERROR!!!" << std::endl;
65 while ( i != TIMEOUT ) {
67 if ( MPI_Lookup_name((char*)service.c_str(), MPI_INFO_NULL, port_name_clt) == MPI_SUCCESS ) {
69 std::cout << "[" << rank << "] I am client: I get the service " << service << " !" << std::endl;
76 std::cout << "[" << rank << "] Waiting too long exiting !" << std::endl;
81 MPI_Errhandler_set(MPI_COMM_WORLD, MPI_ERRORS_ARE_FATAL);
82 MPI_Bcast(&srv,1,MPI_INT,0,MPI_COMM_WORLD);
84 MPI_Comm_accept( port_name, MPI_INFO_NULL, 0, MPI_COMM_WORLD, &icom );
86 MPI_Comm_connect(port_name_clt, MPI_INFO_NULL, 0, MPI_COMM_WORLD, &icom );
88 MPI_Intercomm_merge(icom,!srv,&com);
90 MPI_Comm_rank( com, &grank );
91 MPI_Comm_size( com, &gsize );
94 lvsize = ((rank+1)*vsize) / size - (rank*vsize) / size;
95 vector = (double*)malloc(lvsize*sizeof(double));
96 indg = (int*)malloc(lvsize*sizeof(int));
99 for(i=0;i<lvsize;i++){
100 indg[i] = (rank*vsize)/size + i;
105 vector[i] = 2. * sin( (rank*vsize)/size + i );
106 sum += vector[i]*vector[i];
111 MPI_Reduce(&sum,&norm,1,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD);
115 std::cout << "[" << grank << "] norm=" << norm << std::endl;
119 for(i=0;i<rsize;i++){
120 rlvsize = ((i+1)*vsize) / rsize - (i*vsize) / rsize;
121 k1 = (i*vsize)/rsize;
122 k2 = ((i+1)*vsize)/rsize -1;
124 if( (k1 <= indg[lvsize-1]) && (k2 >= indg[0]) ){
126 if( indg[0] > imin ) imin = indg[0];
128 if( indg[lvsize-1] < imax) imax = indg[lvsize-1];
130 nb = imax - imin + 1;
131 MPI_Send( &nb, 1, MPI_INT, i+size, 100, com );
132 MPI_Send( vector+imin-indg[0], nb, MPI_DOUBLE, i+size, 200, com );
135 MPI_Recv( &nb, 1, MPI_INT, i, 100, com, &status );
136 MPI_Recv( vector+imin-indg[0], nb, MPI_DOUBLE, i, 200, com, &status );
145 for(i=0;i<lvsize;i++){
147 std::cout << "[" << rank << "] vector[" << i << "]=" << vector[i] << std::endl;
148 sum += vector[i]*vector[i];
150 MPI_Reduce(&sum,&norm,1,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD);
154 std::cout << "[" << grank << "] norm=" << norm << std::endl;
161 MPI_Recv(&etalon, 1, MPI_DOUBLE,size,400,com, &status);
162 MPI_Send(&norm,1,MPI_DOUBLE, size, 300, com);
166 MPI_Send(&norm,1,MPI_DOUBLE, 0, 400, com);
167 MPI_Recv(&etalon, 1, MPI_DOUBLE,0,300,com, &status);
173 MPI_Comm_disconnect( &com );
175 MPI_Unpublish_name((char*)service.c_str(), MPI_INFO_NULL, port_name);
176 MPI_Close_port( port_name );
184 if(fabs(norm-etalon)/norm < EPSILON ){
186 std::cout << "OK" << std::endl;
191 std::cout << "KO" << std::endl;