Blame test/mpi/cxx/spawn/namepubx.cxx

Packit 0848f5
/* -*- Mode: C++; c-basic-offset:4 ; -*- */
Packit 0848f5
/*
Packit 0848f5
 *
Packit 0848f5
 *  (C) 2003 by Argonne National Laboratory.
Packit 0848f5
 *      See COPYRIGHT in top-level directory.
Packit 0848f5
 */
Packit 0848f5
#include "mpi.h"
Packit 0848f5
#include "mpitestconf.h"
Packit 0848f5
#ifdef HAVE_IOSTREAM
Packit 0848f5
// Not all C++ compilers have iostream instead of iostream.h
Packit 0848f5
#include <iostream>
Packit 0848f5
#ifdef HAVE_NAMESPACE_STD
Packit 0848f5
// Those that do often need the std namespace; otherwise, a bare "cout"
Packit 0848f5
// is likely to fail to compile
Packit 0848f5
using namespace std;
Packit 0848f5
#endif
Packit 0848f5
#else
Packit 0848f5
#include <iostream.h>
Packit 0848f5
#endif
Packit 0848f5
#ifdef HAVE_STRING_H
Packit 0848f5
#include <string.h>
Packit 0848f5
#endif
Packit 0848f5
#include "mpitestcxx.h"
Packit 0848f5
Packit 0848f5
int main( int argc, char *argv[] )
Packit 0848f5
{
Packit 0848f5
    int errs = 0;
Packit 0848f5
    char *port_name, *port_name_out;
Packit 0848f5
    char serv_name[256];
Packit 0848f5
    int merr, mclass;
Packit 0848f5
    const char *errmsg;
Packit 0848f5
    int msglen;
Packit 0848f5
    int rank;
Packit 0848f5
Packit 0848f5
    MTest_Init( );
Packit 0848f5
Packit 0848f5
    port_name = new char [MPI::MAX_PORT_NAME];
Packit 0848f5
    port_name_out = new char [MPI::MAX_PORT_NAME];
Packit 0848f5
  
Packit 0848f5
    rank = MPI::COMM_WORLD.Get_rank();
Packit 0848f5
Packit 0848f5
    /* Note that according to the MPI standard, port_name must
Packit 0848f5
       have been created by MPI_Open_port.  For current testing
Packit 0848f5
       purposes, we'll use a fake name.  This test should eventually use
Packit 0848f5
       a valid name from Open_port */
Packit 0848f5
Packit 0848f5
    strcpy( port_name, "otherhost:122" );
Packit 0848f5
    strcpy( serv_name, "MyTest" );
Packit 0848f5
Packit 0848f5
    MPI::COMM_WORLD.Set_errhandler( MPI::ERRORS_THROW_EXCEPTIONS );
Packit 0848f5
Packit 0848f5
    if (rank == 0) {
Packit 0848f5
	try {
Packit 0848f5
	    MPI::Publish_name( serv_name, MPI::INFO_NULL, port_name );
Packit 0848f5
	} catch (MPI::Exception e) {
Packit 0848f5
	    errs++;
Packit 0848f5
	    errmsg = e.Get_error_string();
Packit 0848f5
	    cout << "Error in Publish_name " << errmsg << "\n";
Packit 0848f5
	}
Packit 0848f5
Packit 0848f5
	MPI::COMM_WORLD.Barrier();
Packit 0848f5
	MPI::COMM_WORLD.Barrier();
Packit 0848f5
	
Packit 0848f5
	try {
Packit 0848f5
	    MPI::Unpublish_name( serv_name, MPI::INFO_NULL, port_name );
Packit 0848f5
	} catch (MPI::Exception e) {
Packit 0848f5
	    errs++;
Packit 0848f5
	    errmsg = e.Get_error_string();
Packit 0848f5
	    cout << "Error in Unpublish name " << errmsg << "\n";
Packit 0848f5
	}
Packit 0848f5
    }
Packit 0848f5
    else {
Packit 0848f5
	MPI::COMM_WORLD.Barrier();
Packit 0848f5
Packit 0848f5
	merr = MPI::SUCCESS;
Packit 0848f5
	try {
Packit 0848f5
	    MPI::Lookup_name( serv_name, MPI::INFO_NULL, port_name_out );
Packit 0848f5
	} catch ( MPI::Exception e ) {
Packit 0848f5
	    errs++;
Packit 0848f5
	    merr   = e.Get_error_code();
Packit 0848f5
	    errmsg = e.Get_error_string();
Packit 0848f5
	    cout << "Error in Lookup name " << errmsg << "\n";
Packit 0848f5
	}
Packit 0848f5
	if (merr == MPI::SUCCESS) {
Packit 0848f5
	    if (strcmp( port_name, port_name_out )) {
Packit 0848f5
		errs++;
Packit 0848f5
		cout << "Lookup name returned the wrong value (" << 
Packit 0848f5
		    port_name_out << ")\n";
Packit 0848f5
	    }
Packit 0848f5
	}
Packit 0848f5
Packit 0848f5
	MPI::COMM_WORLD.Barrier();
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
Packit 0848f5
    MPI::COMM_WORLD.Barrier();
Packit 0848f5
    
Packit 0848f5
    merr = MPI::SUCCESS;
Packit 0848f5
    port_name_out[0] = 0;
Packit 0848f5
    try {
Packit 0848f5
	MPI::Lookup_name( serv_name, MPI::INFO_NULL, port_name_out );
Packit 0848f5
    } catch (MPI::Exception e) {
Packit 0848f5
	merr = e.Get_error_code();
Packit 0848f5
    }
Packit 0848f5
    if (!merr) {
Packit 0848f5
	errs++;
Packit 0848f5
	cout << "Lookup name returned name after it was unpublished\n";
Packit 0848f5
	cout << "The name returned was " << port_name_out << "\n";
Packit 0848f5
    }
Packit 0848f5
    else {
Packit 0848f5
	/* Must be class MPI::ERR_NAME */
Packit 0848f5
	mclass = MPI::Get_error_class( merr );
Packit 0848f5
	if (mclass != MPI::ERR_NAME) {
Packit 0848f5
	    char *lerrmsg;
Packit 0848f5
	    lerrmsg = new char [MPI::MAX_ERROR_STRING];
Packit 0848f5
	    errs++;
Packit 0848f5
	    MPI::Get_error_string( merr, lerrmsg, msglen );
Packit 0848f5
	    cout << "Lookup name returned the wrong error class (" << mclass <<
Packit 0848f5
		"), msg " << lerrmsg << "\n";
Packit 0848f5
	    delete [] lerrmsg;
Packit 0848f5
	}
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    delete [] port_name;
Packit 0848f5
    delete [] port_name_out;
Packit 0848f5
Packit 0848f5
    MTest_Finalize( errs );
Packit 0848f5
    MPI::Finalize();
Packit 0848f5
    return 0;
Packit 0848f5
}