Blame test/mpi/errors/pt2pt/truncmsg2.c

Packit 0848f5
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
Packit 0848f5
/*
Packit 0848f5
 *  (C) 2008 by Argonne National Laboratory.
Packit 0848f5
 *      See COPYRIGHT in top-level directory.
Packit 0848f5
 */
Packit 0848f5
/*
Packit 0848f5
 * This program was sent in as an example that did not perform as expected.
Packit 0848f5
 * The program has a bug in that it is sending 3 characters but receiving
Packit 0848f5
 * three integers, which is not a valid MPI program (the type signatures
Packit 0848f5
 * must match).  However, a good MPI implementation will handle this
Packit 0848f5
 * gracefully, which is why this test is included in the error directory
Packit 0848f5
 */
Packit 0848f5
#include <stdio.h>
Packit 0848f5
#include <stdlib.h>
Packit 0848f5
#include <string.h>
Packit 0848f5
Packit 0848f5
#include "mpi.h"
Packit 0848f5
#include "mpitest.h"
Packit 0848f5
Packit 0848f5
#define BUFSIZE 128
Packit 0848f5
Packit 0848f5
#define FAIL_ERROR       123
Packit 0848f5
#define RESFAIL_ERROR    123
Packit 0848f5
#define INTERNAL_ERROR   123
Packit 0848f5
Packit 0848f5
void if_error(const char *function, const char *data, int ret);
Packit 0848f5
void if_error(const char *function, const char *data, int ret)
Packit 0848f5
{
Packit 0848f5
    if (ret == 0)
Packit 0848f5
        return;
Packit 0848f5
Packit 0848f5
    if (data)
Packit 0848f5
        printf("%s for %s returned %d (%#x)\n", function, data, ret, ret);
Packit 0848f5
    else
Packit 0848f5
        printf("%s returned %d (%#x)\n", function, ret, ret);
Packit 0848f5
Packit 0848f5
    exit(INTERNAL_ERROR);
Packit 0848f5
}
Packit 0848f5
Packit 0848f5
int main(int argc, char *argv[])
Packit 0848f5
{
Packit 0848f5
    int ret, errs = 0;
Packit 0848f5
    char *src, *sendrec;
Packit 0848f5
    int bufsize = BUFSIZE;
Packit 0848f5
Packit 0848f5
    int myrank, nprocs;
Packit 0848f5
    int i;
Packit 0848f5
    MPI_Status status;
Packit 0848f5
Packit 0848f5
    int small_non_contig_struct_count = 3;
Packit 0848f5
    int small_non_contig_struct_blocklens[] = { 1, 1, 1 };
Packit 0848f5
    MPI_Aint small_non_contig_struct_disps[] = { 0, 2, 4 };
Packit 0848f5
    MPI_Datatype small_non_contig_struct_types[] = { MPI_CHAR, MPI_CHAR, MPI_CHAR };
Packit 0848f5
    MPI_Datatype small_non_contig_struct_type;
Packit 0848f5
Packit 0848f5
    int contig_indexed_count = 3;
Packit 0848f5
    int contig_indexed_blocklens[] = { 1, 2, 1 };
Packit 0848f5
    int contig_indexed_indices[] = { 4, 8, 16 };
Packit 0848f5
    int contig_indexed_inner_type = MPI_INT;
Packit 0848f5
    int contig_indexed_type;
Packit 0848f5
Packit 0848f5
    MTest_Init(&argc, &argv);
Packit 0848f5
    ret = MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
Packit 0848f5
    ret = MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
Packit 0848f5
Packit 0848f5
    if (nprocs < 2) {
Packit 0848f5
        printf("Need at least 2 procs\n");
Packit 0848f5
        exit(RESFAIL_ERROR);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    ret = MPI_Type_struct(small_non_contig_struct_count,
Packit 0848f5
                          small_non_contig_struct_blocklens,
Packit 0848f5
                          small_non_contig_struct_disps,
Packit 0848f5
                          small_non_contig_struct_types, &small_non_contig_struct_type);
Packit 0848f5
    if_error("MPI_Type_struct", "small_non_contig_struct_type", ret);
Packit 0848f5
Packit 0848f5
    ret = MPI_Type_commit(&small_non_contig_struct_type);
Packit 0848f5
    if_error("MPI_Type_commit", "small_non_contig_struct_type", ret);
Packit 0848f5
Packit 0848f5
    ret = MPI_Type_indexed(contig_indexed_count, contig_indexed_blocklens,
Packit 0848f5
                           contig_indexed_indices, contig_indexed_inner_type, &contig_indexed_type);
Packit 0848f5
    if_error("MPI_Type_indexed", "contig_indexed_type", ret);
Packit 0848f5
Packit 0848f5
    ret = MPI_Type_commit(&contig_indexed_type);
Packit 0848f5
    if_error("MPI_Type_commit", "contig_indexed_type", ret);
Packit 0848f5
Packit 0848f5
Packit 0848f5
    ret = MPI_Alloc_mem(bufsize, MPI_INFO_NULL, &src;;
Packit 0848f5
Packit 0848f5
    if (ret != 0) {
Packit 0848f5
        printf("MPI_Alloc_mem src = #%x\n", ret);
Packit 0848f5
        exit(INTERNAL_ERROR);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    ret = MPI_Alloc_mem(bufsize, MPI_INFO_NULL, &sendrec);
Packit 0848f5
Packit 0848f5
    if (ret != 0) {
Packit 0848f5
        printf("MPI_Alloc_mem sendrec buf = #%x\n", ret);
Packit 0848f5
        exit(INTERNAL_ERROR);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
Packit 0848f5
    for (i = 0; i < bufsize; i++) {
Packit 0848f5
        src[i] = (char) i + 1;
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    memset(sendrec, 0, bufsize);
Packit 0848f5
Packit 0848f5
    MPI_Barrier(MPI_COMM_WORLD);
Packit 0848f5
    if (myrank == 1) {
Packit 0848f5
        MPI_Send(src, 1, small_non_contig_struct_type, 0, 0xabc, MPI_COMM_WORLD);
Packit 0848f5
    }
Packit 0848f5
    else {
Packit 0848f5
        MPI_Errhandler_set(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
Packit 0848f5
        ret = MPI_Recv(sendrec, 1, contig_indexed_type, 1, 0xabc, MPI_COMM_WORLD, &status);
Packit 0848f5
        if (ret == MPI_SUCCESS) {
Packit 0848f5
            printf("MPI_Recv succeeded with non-matching datatype signature\n");
Packit 0848f5
            errs++;
Packit 0848f5
        }
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    MPI_Barrier(MPI_COMM_WORLD);
Packit 0848f5
Packit 0848f5
    MPI_Type_free(&small_non_contig_struct_type);
Packit 0848f5
    MPI_Type_free(&contig_indexed_type);
Packit 0848f5
Packit 0848f5
    MPI_Free_mem(src);
Packit 0848f5
    MPI_Free_mem(sendrec);
Packit 0848f5
Packit 0848f5
    MTest_Finalize(errs);
Packit 0848f5
    MPI_Finalize();
Packit 0848f5
Packit 0848f5
    return 0;
Packit 0848f5
}