Blame src/mpi/romio/test/coll_test.c

Packit Service c5cf8c
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
Packit Service c5cf8c
/*
Packit Service c5cf8c
 *  (C) 2001 by Argonne National Laboratory.
Packit Service c5cf8c
 *      See COPYRIGHT in top-level directory.
Packit Service c5cf8c
 */
Packit Service c5cf8c
#include "mpi.h"
Packit Service c5cf8c
#include <stdlib.h>
Packit Service c5cf8c
#include <string.h>
Packit Service c5cf8c
#include <stdio.h>
Packit Service c5cf8c
Packit Service c5cf8c
/* A 32^3 array. For other array sizes, change array_of_gsizes below. */
Packit Service c5cf8c
Packit Service c5cf8c
/* Uses collective I/O. Writes a 3D block-distributed array to a file
Packit Service c5cf8c
   corresponding to the global array in row-major (C) order, reads it
Packit Service c5cf8c
   back, and checks that the data read is correct. */
Packit Service c5cf8c
Packit Service c5cf8c
/* The file name is taken as a command-line argument. */
Packit Service c5cf8c
Packit Service c5cf8c
/* Note that the file access pattern is noncontiguous. */
Packit Service c5cf8c
Packit Service c5cf8c
void handle_error(int errcode, const char *str);
Packit Service c5cf8c
Packit Service c5cf8c
void handle_error(int errcode, const char *str)
Packit Service c5cf8c
{
Packit Service c5cf8c
    char msg[MPI_MAX_ERROR_STRING];
Packit Service c5cf8c
    int resultlen;
Packit Service c5cf8c
    MPI_Error_string(errcode, msg, &resultlen);
Packit Service c5cf8c
    fprintf(stderr, "%s: %s\n", str, msg);
Packit Service c5cf8c
    MPI_Abort(MPI_COMM_WORLD, 1);
Packit Service c5cf8c
}
Packit Service c5cf8c
Packit Service c5cf8c
int main(int argc, char **argv)
Packit Service c5cf8c
{
Packit Service c5cf8c
    MPI_Datatype newtype;
Packit Service c5cf8c
    int i, ndims, array_of_gsizes[3], array_of_distribs[3];
Packit Service c5cf8c
    int order, nprocs, j, len;
Packit Service c5cf8c
    int array_of_dargs[3], array_of_psizes[3];
Packit Service c5cf8c
    int *readbuf, *writebuf, mynod, *tmpbuf, array_size;
Packit Service c5cf8c
    MPI_Count bufcount;
Packit Service c5cf8c
    char *filename;
Packit Service c5cf8c
    int errs = 0, toterrs;
Packit Service c5cf8c
    MPI_File fh;
Packit Service c5cf8c
    MPI_Status status;
Packit Service c5cf8c
    MPI_Request request;
Packit Service c5cf8c
    MPI_Info info = MPI_INFO_NULL;
Packit Service c5cf8c
    int errcode;
Packit Service c5cf8c
Packit Service c5cf8c
    MPI_Init(&argc, &argv);
Packit Service c5cf8c
    MPI_Comm_rank(MPI_COMM_WORLD, &mynod);
Packit Service c5cf8c
    MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
Packit Service c5cf8c
Packit Service c5cf8c
/* process 0 takes the file name as a command-line argument and
Packit Service c5cf8c
   broadcasts it to other processes */
Packit Service c5cf8c
    if (!mynod) {
Packit Service c5cf8c
        i = 1;
Packit Service c5cf8c
        while ((i < argc) && strcmp("-fname", *argv)) {
Packit Service c5cf8c
            i++;
Packit Service c5cf8c
            argv++;
Packit Service c5cf8c
        }
Packit Service c5cf8c
        if (i >= argc) {
Packit Service c5cf8c
            fprintf(stderr, "\n*#  Usage: coll_test -fname filename\n\n");
Packit Service c5cf8c
            MPI_Abort(MPI_COMM_WORLD, 1);
Packit Service c5cf8c
        }
Packit Service c5cf8c
        argv++;
Packit Service c5cf8c
        len = strlen(*argv);
Packit Service c5cf8c
        filename = (char *) malloc(len + 1);
Packit Service c5cf8c
        strcpy(filename, *argv);
Packit Service c5cf8c
        MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
Packit Service c5cf8c
        MPI_Bcast(filename, len + 1, MPI_CHAR, 0, MPI_COMM_WORLD);
Packit Service c5cf8c
    } else {
Packit Service c5cf8c
        MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
Packit Service c5cf8c
        filename = (char *) malloc(len + 1);
Packit Service c5cf8c
        MPI_Bcast(filename, len + 1, MPI_CHAR, 0, MPI_COMM_WORLD);
Packit Service c5cf8c
    }
Packit Service c5cf8c
Packit Service c5cf8c
Packit Service c5cf8c
/* create the distributed array filetype */
Packit Service c5cf8c
    ndims = 3;
Packit Service c5cf8c
    order = MPI_ORDER_C;
Packit Service c5cf8c
Packit Service c5cf8c
    array_of_gsizes[0] = 32;
Packit Service c5cf8c
    array_of_gsizes[1] = 32;
Packit Service c5cf8c
    array_of_gsizes[2] = 32;
Packit Service c5cf8c
Packit Service c5cf8c
    array_of_distribs[0] = MPI_DISTRIBUTE_BLOCK;
Packit Service c5cf8c
    array_of_distribs[1] = MPI_DISTRIBUTE_BLOCK;
Packit Service c5cf8c
    array_of_distribs[2] = MPI_DISTRIBUTE_BLOCK;
Packit Service c5cf8c
Packit Service c5cf8c
    array_of_dargs[0] = MPI_DISTRIBUTE_DFLT_DARG;
Packit Service c5cf8c
    array_of_dargs[1] = MPI_DISTRIBUTE_DFLT_DARG;
Packit Service c5cf8c
    array_of_dargs[2] = MPI_DISTRIBUTE_DFLT_DARG;
Packit Service c5cf8c
Packit Service c5cf8c
    for (i = 0; i < ndims; i++)
Packit Service c5cf8c
        array_of_psizes[i] = 0;
Packit Service c5cf8c
    MPI_Dims_create(nprocs, ndims, array_of_psizes);
Packit Service c5cf8c
Packit Service c5cf8c
    MPI_Type_create_darray(nprocs, mynod, ndims, array_of_gsizes,
Packit Service c5cf8c
                           array_of_distribs, array_of_dargs,
Packit Service c5cf8c
                           array_of_psizes, order, MPI_INT, &newtype);
Packit Service c5cf8c
    MPI_Type_commit(&newtype);
Packit Service c5cf8c
Packit Service c5cf8c
/* initialize writebuf */
Packit Service c5cf8c
Packit Service c5cf8c
    MPI_Type_size_x(newtype, &bufcount);
Packit Service c5cf8c
    bufcount = bufcount / sizeof(int);
Packit Service c5cf8c
    writebuf = (int *) malloc(bufcount * sizeof(int));
Packit Service c5cf8c
    for (i = 0; i < bufcount; i++)
Packit Service c5cf8c
        writebuf[i] = 1;
Packit Service c5cf8c
Packit Service c5cf8c
    array_size = array_of_gsizes[0] * array_of_gsizes[1] * array_of_gsizes[2];
Packit Service c5cf8c
    tmpbuf = (int *) calloc(array_size, sizeof(int));
Packit Service c5cf8c
    MPI_Irecv(tmpbuf, 1, newtype, mynod, 10, MPI_COMM_WORLD, &request);
Packit Service c5cf8c
    MPI_Send(writebuf, bufcount, MPI_INT, mynod, 10, MPI_COMM_WORLD);
Packit Service c5cf8c
    MPI_Wait(&request, &status);
Packit Service c5cf8c
Packit Service c5cf8c
    j = 0;
Packit Service c5cf8c
    for (i = 0; i < array_size; i++)
Packit Service c5cf8c
        if (tmpbuf[i]) {
Packit Service c5cf8c
            writebuf[j] = i;
Packit Service c5cf8c
            j++;
Packit Service c5cf8c
        }
Packit Service c5cf8c
    free(tmpbuf);
Packit Service c5cf8c
Packit Service c5cf8c
    if (j != bufcount) {
Packit Service c5cf8c
        fprintf(stderr, "Error in initializing writebuf on process %d\n", mynod);
Packit Service c5cf8c
        MPI_Abort(MPI_COMM_WORLD, 1);
Packit Service c5cf8c
    }
Packit Service c5cf8c
/* end of initialization */
Packit Service c5cf8c
Packit Service c5cf8c
    /* write the array to the file */
Packit Service c5cf8c
    errcode = MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_CREATE | MPI_MODE_RDWR, info, &fh;;
Packit Service c5cf8c
    if (errcode != MPI_SUCCESS)
Packit Service c5cf8c
        handle_error(errcode, "MPI_File_open");
Packit Service c5cf8c
Packit Service c5cf8c
    errcode = MPI_File_set_view(fh, 0, MPI_INT, newtype, "native", info);
Packit Service c5cf8c
    if (errcode != MPI_SUCCESS)
Packit Service c5cf8c
        handle_error(errcode, "MPI_File_set_view");
Packit Service c5cf8c
Packit Service c5cf8c
    errcode = MPI_File_write_all(fh, writebuf, bufcount, MPI_INT, &status);
Packit Service c5cf8c
    if (errcode != MPI_SUCCESS)
Packit Service c5cf8c
        handle_error(errcode, "MPI_File_write_all");
Packit Service c5cf8c
    errcode = MPI_File_close(&fh;;
Packit Service c5cf8c
    if (errcode != MPI_SUCCESS)
Packit Service c5cf8c
        handle_error(errcode, "MPI_File_close");
Packit Service c5cf8c
Packit Service c5cf8c
    if (!mynod) {
Packit Service c5cf8c
        /* wkl suggests potential for false " No Errors" if both read
Packit Service c5cf8c
         * and write use the same file view */
Packit Service c5cf8c
        /* solution: rank 0 reads entire file and checks write values */
Packit Service c5cf8c
        errcode = MPI_File_open(MPI_COMM_SELF, filename, MPI_MODE_RDONLY, info, &fh;;
Packit Service c5cf8c
        if (errcode != MPI_SUCCESS)
Packit Service c5cf8c
            handle_error(errcode, "MPI_File_open");
Packit Service c5cf8c
Packit Service c5cf8c
        readbuf = (int *) malloc(array_size * sizeof(int));
Packit Service c5cf8c
        errcode = MPI_File_read(fh, readbuf, array_size, MPI_INT, &status);
Packit Service c5cf8c
        if (errcode != MPI_SUCCESS)
Packit Service c5cf8c
            handle_error(errcode, "MPI_File_read");
Packit Service c5cf8c
Packit Service c5cf8c
        errcode = MPI_File_close(&fh;;
Packit Service c5cf8c
        if (errcode != MPI_SUCCESS)
Packit Service c5cf8c
            handle_error(errcode, "MPI_File_close");
Packit Service c5cf8c
Packit Service c5cf8c
        for (i = 0; i < array_size; i++)
Packit Service c5cf8c
            if (readbuf[i] != i) {
Packit Service c5cf8c
                errs++;
Packit Service c5cf8c
                fprintf(stderr, "Error: write integer %d but read %d\n", i, readbuf[i]);
Packit Service c5cf8c
                break;
Packit Service c5cf8c
            }
Packit Service c5cf8c
        free(readbuf);
Packit Service c5cf8c
    }
Packit Service c5cf8c
    MPI_Barrier(MPI_COMM_WORLD);
Packit Service c5cf8c
Packit Service c5cf8c
    /* now read it back */
Packit Service c5cf8c
    readbuf = (int *) malloc(bufcount * sizeof(int));
Packit Service c5cf8c
    errcode = MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_CREATE | MPI_MODE_RDWR, info, &fh;;
Packit Service c5cf8c
    if (errcode != MPI_SUCCESS)
Packit Service c5cf8c
        handle_error(errcode, "MPI_File_open");
Packit Service c5cf8c
Packit Service c5cf8c
    errcode = MPI_File_set_view(fh, 0, MPI_INT, newtype, "native", info);
Packit Service c5cf8c
    if (errcode != MPI_SUCCESS)
Packit Service c5cf8c
        handle_error(errcode, "MPI_File_set_view");
Packit Service c5cf8c
    errcode = MPI_File_read_all(fh, readbuf, bufcount, MPI_INT, &status);
Packit Service c5cf8c
    if (errcode != MPI_SUCCESS)
Packit Service c5cf8c
        handle_error(errcode, "MPI_File_read_all");
Packit Service c5cf8c
    errcode = MPI_File_close(&fh;;
Packit Service c5cf8c
    if (errcode != MPI_SUCCESS)
Packit Service c5cf8c
        handle_error(errcode, "MPI_File_close");
Packit Service c5cf8c
Packit Service c5cf8c
    /* check the data read */
Packit Service c5cf8c
    for (i = 0; i < bufcount; i++) {
Packit Service c5cf8c
        if (readbuf[i] != writebuf[i]) {
Packit Service c5cf8c
            errs++;
Packit Service c5cf8c
            fprintf(stderr, "Process %d, readbuf %d, writebuf %d, i %d\n", mynod, readbuf[i],
Packit Service c5cf8c
                    writebuf[i], i);
Packit Service c5cf8c
        }
Packit Service c5cf8c
    }
Packit Service c5cf8c
Packit Service c5cf8c
    MPI_Allreduce(&errs, &toterrs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
Packit Service c5cf8c
    if (mynod == 0) {
Packit Service c5cf8c
        if (toterrs > 0) {
Packit Service c5cf8c
            fprintf(stderr, "Found %d errors\n", toterrs);
Packit Service c5cf8c
        } else {
Packit Service c5cf8c
            fprintf(stdout, " No Errors\n");
Packit Service c5cf8c
        }
Packit Service c5cf8c
    }
Packit Service c5cf8c
Packit Service c5cf8c
    MPI_Type_free(&newtype);
Packit Service c5cf8c
    free(readbuf);
Packit Service c5cf8c
    free(writebuf);
Packit Service c5cf8c
    free(filename);
Packit Service c5cf8c
Packit Service c5cf8c
    MPI_Finalize();
Packit Service c5cf8c
    return 0;
Packit Service c5cf8c
}