Blame test/mpi/errors/comm/userdup.c

Packit 0848f5
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
Packit 0848f5
/*
Packit 0848f5
 *  (C) 2007 by Argonne National Laboratory.
Packit 0848f5
 *      See COPYRIGHT in top-level directory.
Packit 0848f5
 */
Packit 0848f5
#include "mpi.h"
Packit 0848f5
#include <stdio.h>
Packit 0848f5
#include <string.h>
Packit 0848f5
#include "mpitest.h"
Packit 0848f5
Packit 0848f5
/* Check that user-define error codes and classes are correctly handled by
Packit 0848f5
   the attribute copy routines.
Packit 0848f5
Packit 0848f5
   Note that this behavior is not required by the MPI specification
Packit 0848f5
   but is a quality of implementation issues - users will expect
Packit 0848f5
   to be able to control the class and code that comes back from
Packit 0848f5
   MPI_Comm_dup (and MPI_Comm_free) in this case.
Packit 0848f5
*/
Packit 0848f5
Packit 0848f5
void abortMsg(const char *, int);
Packit 0848f5
int copybomb_fn(MPI_Comm, int, void *, void *, void *, int *);
Packit 0848f5
Packit 0848f5
static int myErrClass, myErrCode;
Packit 0848f5
static int nCall = 0;
Packit 0848f5
Packit 0848f5
void abortMsg(const char *str, int code)
Packit 0848f5
{
Packit 0848f5
    char msg[MPI_MAX_ERROR_STRING];
Packit 0848f5
    int class, resultLen;
Packit 0848f5
Packit 0848f5
    MPI_Error_class(code, &class);
Packit 0848f5
    MPI_Error_string(code, msg, &resultLen);
Packit 0848f5
    fprintf(stderr, "%s: errcode = %d, class = %d, msg = %s\n", str, code, class, msg);
Packit 0848f5
    MPI_Abort(MPI_COMM_WORLD, code);
Packit 0848f5
}
Packit 0848f5
Packit 0848f5
int copybomb_fn(MPI_Comm oldcomm, int keyval, void *extra_state,
Packit 0848f5
                void *attribute_val_in, void *attribute_val_out, int *flag)
Packit 0848f5
{
Packit 0848f5
    int err;
Packit 0848f5
    /* We always fail to copy */
Packit 0848f5
    *flag = 1;
Packit 0848f5
Packit 0848f5
    /* Return either the class (as a code) or the code */
Packit 0848f5
    if (nCall == 0)
Packit 0848f5
        err = myErrClass;
Packit 0848f5
    else
Packit 0848f5
        err = myErrCode;
Packit 0848f5
    nCall++;
Packit 0848f5
    return err;
Packit 0848f5
}
Packit 0848f5
Packit 0848f5
Packit 0848f5
int main(int argc, char *argv[])
Packit 0848f5
{
Packit 0848f5
    MPI_Comm dupWorld, dup2;
Packit 0848f5
    int myRank, key, err, errs = 0;
Packit 0848f5
Packit 0848f5
    MTest_Init(&argc, &argv);
Packit 0848f5
Packit 0848f5
    MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
Packit 0848f5
Packit 0848f5
    /* Create my error class and code */
Packit 0848f5
    MPI_Add_error_class(&myErrClass);
Packit 0848f5
    MPI_Add_error_code(myErrClass, &myErrCode);
Packit 0848f5
    MPI_Add_error_string(myErrClass, (char *) "My error class");
Packit 0848f5
    MPI_Add_error_string(myErrCode, (char *) "My error code");
Packit 0848f5
Packit 0848f5
    MPI_Comm_dup(MPI_COMM_WORLD, &dupWorld);
Packit 0848f5
Packit 0848f5
    MPI_Comm_rank(MPI_COMM_WORLD, &myRank);
Packit 0848f5
    err = MPI_Comm_create_keyval(copybomb_fn, MPI_COMM_NULL_DELETE_FN, &key, NULL);
Packit 0848f5
    if (err) {
Packit 0848f5
        errs++;
Packit 0848f5
        abortMsg("Comm_create_keyval", err);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    err = MPI_Comm_set_attr(dupWorld, key, &myRank);
Packit 0848f5
    if (err) {
Packit 0848f5
        errs++;
Packit 0848f5
        abortMsg("Comm_set_attr", err);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    err = MPI_Comm_dup(dupWorld, &dup2);
Packit 0848f5
    if (err == MPI_SUCCESS) {
Packit 0848f5
        errs++;
Packit 0848f5
        fprintf(stderr, "Comm_dup did not fail\n");
Packit 0848f5
        MPI_Comm_free(&dup2);
Packit 0848f5
    }
Packit 0848f5
    else {
Packit 0848f5
        int eclass, resultLen;
Packit 0848f5
        char msg[MPI_MAX_ERROR_STRING];
Packit 0848f5
        /* Check for expected error class */
Packit 0848f5
        MPI_Error_class(err, &eclass);
Packit 0848f5
        if (eclass != myErrClass) {
Packit 0848f5
            errs++;
Packit 0848f5
            fprintf(stderr, "Unexpected error class = %d, expected user-defined class %d\n", eclass,
Packit 0848f5
                    myErrClass);
Packit 0848f5
        }
Packit 0848f5
        else {
Packit 0848f5
            MPI_Error_string(err, msg, &resultLen);
Packit 0848f5
            if (strcmp(msg, "My error class") != 0) {
Packit 0848f5
                errs++;
Packit 0848f5
                fprintf(stderr, "Unexpected error string %s\n", msg);
Packit 0848f5
            }
Packit 0848f5
        }
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    err = MPI_Comm_dup(dupWorld, &dup2);
Packit 0848f5
    if (err == MPI_SUCCESS) {
Packit 0848f5
        errs++;
Packit 0848f5
        fprintf(stderr, "Comm_dup did not fail (2)\n");
Packit 0848f5
        MPI_Comm_free(&dup2);
Packit 0848f5
    }
Packit 0848f5
    else {
Packit 0848f5
        int eclass, resultLen;
Packit 0848f5
        char msg[MPI_MAX_ERROR_STRING];
Packit 0848f5
        /* Check for expected error class */
Packit 0848f5
        MPI_Error_class(err, &eclass);
Packit 0848f5
        if (eclass != myErrClass) {
Packit 0848f5
            errs++;
Packit 0848f5
            fprintf(stderr, "Unexpected error class = %d, expected user-defined class %d\n", eclass,
Packit 0848f5
                    myErrClass);
Packit 0848f5
        }
Packit 0848f5
        if (err != myErrCode) {
Packit 0848f5
            errs++;
Packit 0848f5
            fprintf(stderr, "Unexpected error code = %d, expected user-defined code %d\n", err,
Packit 0848f5
                    myErrCode);
Packit 0848f5
        }
Packit 0848f5
        else {
Packit 0848f5
            MPI_Error_string(err, msg, &resultLen);
Packit 0848f5
            if (strcmp(msg, "My error code") != 0) {
Packit 0848f5
                errs++;
Packit 0848f5
                fprintf(stderr, "Unexpected error string %s, expected user-defined error string\n",
Packit 0848f5
                        msg);
Packit 0848f5
            }
Packit 0848f5
        }
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    MPI_Comm_free(&dupWorld);
Packit 0848f5
    MPI_Comm_free_keyval(&key);
Packit 0848f5
Packit 0848f5
    MTest_Finalize(errs);
Packit 0848f5
Packit 0848f5
    MPI_Finalize();
Packit 0848f5
    return 0;
Packit 0848f5
}