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::cout << "This test only works with openmpi implementation" << std::endl;
29 std::string sargv = argv[i];
30 if(sargv.find("-debug")!=std::string::npos)
32 else if(sargv.find("-vsize")!=std::string::npos)
33 vsize = atoi(argv[++i]);
36 MPI_Init( &argc, &argv );
38 MPI_Comm_size( MPI_COMM_WORLD, &size );
39 MPI_Comm_rank( MPI_COMM_WORLD, &rank );
41 MPI_Barrier(MPI_COMM_WORLD);
43 MPI_Errhandler_set(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
45 MPI_Open_port(MPI_INFO_NULL, port_name);
46 if ( MPI_Lookup_name((char*)service.c_str(), MPI_INFO_NULL, port_name_clt) == MPI_SUCCESS ) {
48 std::cout << "[" << rank << "] I am client: I get the service " << service << " !" << std::endl;
49 MPI_Close_port( port_name );
51 else if ( MPI_Publish_name((char*)service.c_str(), MPI_INFO_NULL, port_name) == MPI_SUCCESS ) {
53 std::cout << "[" << rank << "] I am server: I've managed to publish the service " << service << " !" << std::endl;
56 else if ( MPI_Lookup_name((char*)service.c_str(), MPI_INFO_NULL, port_name_clt) == MPI_SUCCESS ) {
58 std::cout << "[" << rank << "] I am client: I get the service " << service << " !" << std::endl;;
59 MPI_Close_port( port_name );
63 std::cout << "[" << rank << "] ERROR!!!" << std::endl;
70 while ( i != TIMEOUT ) {
72 if ( MPI_Lookup_name((char*)service.c_str(), MPI_INFO_NULL, port_name_clt) == MPI_SUCCESS ) {
74 std::cout << "[" << rank << "] I am client: I get the service " << service << " !" << std::endl;
81 std::cout << "[" << rank << "] Waiting too long exiting !" << std::endl;
86 MPI_Errhandler_set(MPI_COMM_WORLD, MPI_ERRORS_ARE_FATAL);
87 MPI_Bcast(&srv,1,MPI_INT,0,MPI_COMM_WORLD);
89 MPI_Comm_accept( port_name, MPI_INFO_NULL, 0, MPI_COMM_WORLD, &icom );
91 MPI_Comm_connect(port_name_clt, MPI_INFO_NULL, 0, MPI_COMM_WORLD, &icom );
93 MPI_Intercomm_merge(icom,!srv,&com);
95 MPI_Comm_rank( com, &grank );
96 MPI_Comm_size( com, &gsize );
99 lvsize = ((rank+1)*vsize) / size - (rank*vsize) / size;
100 vector = (double*)malloc(lvsize*sizeof(double));
101 indg = (int*)malloc(lvsize*sizeof(int));
102 rsize = gsize - size;
104 for(i=0;i<lvsize;i++){
105 indg[i] = (rank*vsize)/size + i;
110 vector[i] = 2. * sin( (rank*vsize)/size + i );
111 sum += vector[i]*vector[i];
116 MPI_Reduce(&sum,&norm,1,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD);
120 std::cout << "[" << grank << "] norm=" << norm << std::endl;
124 for(i=0;i<rsize;i++){
125 rlvsize = ((i+1)*vsize) / rsize - (i*vsize) / rsize;
126 k1 = (i*vsize)/rsize;
127 k2 = ((i+1)*vsize)/rsize -1;
129 if( (k1 <= indg[lvsize-1]) && (k2 >= indg[0]) ){
131 if( indg[0] > imin ) imin = indg[0];
133 if( indg[lvsize-1] < imax) imax = indg[lvsize-1];
135 nb = imax - imin + 1;
136 MPI_Send( &nb, 1, MPI_INT, i+size, 100, com );
137 MPI_Send( vector+imin-indg[0], nb, MPI_DOUBLE, i+size, 200, com );
140 MPI_Recv( &nb, 1, MPI_INT, i, 100, com, &status );
141 MPI_Recv( vector+imin-indg[0], nb, MPI_DOUBLE, i, 200, com, &status );
150 for(i=0;i<lvsize;i++){
152 std::cout << "[" << rank << "] vector[" << i << "]=" << vector[i] << std::endl;
153 sum += vector[i]*vector[i];
155 MPI_Reduce(&sum,&norm,1,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD);
159 std::cout << "[" << grank << "] norm=" << norm << std::endl;
166 MPI_Recv(&etalon, 1, MPI_DOUBLE,size,400,com, &status);
167 MPI_Send(&norm,1,MPI_DOUBLE, size, 300, com);
171 MPI_Send(&norm,1,MPI_DOUBLE, 0, 400, com);
172 MPI_Recv(&etalon, 1, MPI_DOUBLE,0,300,com, &status);
178 MPI_Comm_disconnect( &com );
180 MPI_Unpublish_name((char*)service.c_str(), MPI_INFO_NULL, port_name);
181 MPI_Close_port( port_name );
189 if(fabs(norm-etalon)/norm < EPSILON ){
191 std::cout << "OK" << std::endl;
196 std::cout << "KO" << std::endl;