Blame test/mpi/io/async.c

Packit 0848f5
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
Packit 0848f5
/*
Packit 0848f5
 *  (C) 2001 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 <stdlib.h>
Packit 0848f5
#include <errno.h>
Packit 0848f5
#include "mpitest.h"
Packit 0848f5
Packit 0848f5
/*
Packit 0848f5
static char MTEST_Descrip[] = "Test contig asynchronous I/O";
Packit 0848f5
*/
Packit 0848f5
Packit 0848f5
#define DEFAULT_SIZE (65536)
Packit 0848f5
Packit 0848f5
/* Uses asynchronous I/O. Each process writes to separate files and
Packit 0848f5
   reads them back. The file name is taken as a command-line argument,
Packit 0848f5
   and the process rank is appended to it.*/
Packit 0848f5
Packit 0848f5
static void handle_error(int errcode, char *str)
Packit 0848f5
{
Packit 0848f5
    char msg[MPI_MAX_ERROR_STRING];
Packit 0848f5
    int resultlen;
Packit 0848f5
    MPI_Error_string(errcode, msg, &resultlen);
Packit 0848f5
    fprintf(stderr, "%s: %s\n", str, msg);
Packit 0848f5
    MPI_Abort(MPI_COMM_WORLD, 1);
Packit 0848f5
}
Packit 0848f5
Packit 0848f5
int main(int argc, char **argv)
Packit 0848f5
{
Packit 0848f5
    int *buf, i, rank, nints, len, err;
Packit 0848f5
    char *filename = 0, *tmp;
Packit 0848f5
    int errs = 0;
Packit 0848f5
    int SIZE = DEFAULT_SIZE;
Packit 0848f5
    MPI_File fh;
Packit 0848f5
    MPI_Status status;
Packit 0848f5
#ifdef MPIO_USES_MPI_REQUEST
Packit 0848f5
    MPI_Request request;
Packit 0848f5
#else
Packit 0848f5
    MPIO_Request request;
Packit 0848f5
#endif
Packit 0848f5
Packit 0848f5
    MTest_Init(&argc, &argv);
Packit 0848f5
    MPI_Comm_rank(MPI_COMM_WORLD, &rank;;
Packit 0848f5
Packit 0848f5
Packit 0848f5
/* process 0 takes the file name as a command-line argument and
Packit 0848f5
   broadcasts it to other processes */
Packit 0848f5
    if (!rank) {
Packit 0848f5
        i = 1;
Packit 0848f5
        argv++;
Packit 0848f5
        /* Skip unrecognized arguments */
Packit 0848f5
        while (i < argc) {
Packit 0848f5
            if (strcmp(*argv, "-fname") == 0) {
Packit 0848f5
                argv++;
Packit 0848f5
                i++;
Packit 0848f5
                len = (int) strlen(*argv);
Packit 0848f5
                filename = (char *) malloc(len + 10);
Packit 0848f5
                MTEST_VG_MEM_INIT(filename, (len + 10) * sizeof(char));
Packit 0848f5
                strcpy(filename, *argv);
Packit 0848f5
            }
Packit 0848f5
            else if (strcmp(*argv, "-size") == 0) {
Packit 0848f5
                argv++;
Packit 0848f5
                i++;
Packit 0848f5
                SIZE = strtol(*argv, 0, 10);
Packit 0848f5
                if (errno) {
Packit 0848f5
                    fprintf(stderr, "-size requires a numeric argument\n");
Packit 0848f5
                    MPI_Abort(MPI_COMM_WORLD, 1);
Packit 0848f5
                }
Packit 0848f5
                else if (SIZE <= 0) {
Packit 0848f5
                    fprintf(stderr, "-size requires a positive value\n");
Packit 0848f5
                }
Packit 0848f5
            }
Packit 0848f5
            else {
Packit 0848f5
                i++;
Packit 0848f5
                argv++;
Packit 0848f5
            }
Packit 0848f5
        }
Packit 0848f5
Packit 0848f5
        if (!filename) {
Packit 0848f5
            /* Use a default filename of testfile */
Packit 0848f5
            len = 8;
Packit 0848f5
            filename = (char *) malloc(len + 10);
Packit 0848f5
            MTEST_VG_MEM_INIT(filename, (len + 10) * sizeof(char));
Packit 0848f5
            strcpy(filename, "testfile");
Packit 0848f5
        }
Packit 0848f5
        MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
Packit 0848f5
        MPI_Bcast(filename, len + 10, MPI_CHAR, 0, MPI_COMM_WORLD);
Packit 0848f5
        MPI_Bcast(&SIZE, 1, MPI_INT, 0, MPI_COMM_WORLD);
Packit 0848f5
    }
Packit 0848f5
    else {
Packit 0848f5
        MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
Packit 0848f5
        filename = (char *) malloc(len + 10);
Packit 0848f5
        MPI_Bcast(filename, len + 10, MPI_CHAR, 0, MPI_COMM_WORLD);
Packit 0848f5
        MPI_Bcast(&SIZE, 1, MPI_INT, 0, MPI_COMM_WORLD);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
Packit 0848f5
    /*     printf("Starting (size=%d, file=%s)...\n", SIZE, filename); fflush(stdout); */
Packit 0848f5
Packit 0848f5
    buf = (int *) malloc(SIZE);
Packit 0848f5
    nints = SIZE / sizeof(int);
Packit 0848f5
    for (i = 0; i < nints; i++)
Packit 0848f5
        buf[i] = rank * 100000 + i;
Packit 0848f5
Packit 0848f5
    /* each process opens a separate file called filename.'myrank' */
Packit 0848f5
    tmp = (char *) malloc(len + 10);
Packit 0848f5
    strcpy(tmp, filename);
Packit 0848f5
    sprintf(filename, "%s.%d", tmp, rank);
Packit 0848f5
    free(tmp);
Packit 0848f5
Packit 0848f5
    err = MPI_File_open(MPI_COMM_SELF, filename, MPI_MODE_CREATE | MPI_MODE_RDWR,
Packit 0848f5
                        MPI_INFO_NULL, &fh;;
Packit 0848f5
    if (err != MPI_SUCCESS)
Packit 0848f5
        handle_error(err, "MPI_File_open");
Packit 0848f5
Packit 0848f5
    err = MPI_File_set_view(fh, 0, MPI_INT, MPI_INT, (char *) "native", MPI_INFO_NULL);
Packit 0848f5
    if (err != MPI_SUCCESS)
Packit 0848f5
        handle_error(err, "MPI_File_set_view");
Packit 0848f5
    err = MPI_File_iwrite(fh, buf, nints, MPI_INT, &request);
Packit 0848f5
    if (err != MPI_SUCCESS)
Packit 0848f5
        handle_error(err, "MPI_File_iwrtie");
Packit 0848f5
#ifdef MPIO_USES_MPI_REQUEST
Packit 0848f5
    MPI_Wait(&request, &status);
Packit 0848f5
#else
Packit 0848f5
    MPIO_Wait(&request, &status);
Packit 0848f5
#endif
Packit 0848f5
    MPI_File_close(&fh;;
Packit 0848f5
Packit 0848f5
    /* reopen the file and read the data back */
Packit 0848f5
Packit 0848f5
    for (i = 0; i < nints; i++)
Packit 0848f5
        buf[i] = 0;
Packit 0848f5
    err = MPI_File_open(MPI_COMM_SELF, filename, MPI_MODE_CREATE | MPI_MODE_RDWR,
Packit 0848f5
                        MPI_INFO_NULL, &fh;;
Packit 0848f5
    if (err != MPI_SUCCESS)
Packit 0848f5
        handle_error(err, "MPI_File_open (read)");
Packit 0848f5
    err = MPI_File_set_view(fh, 0, MPI_INT, MPI_INT, (char *) "native", MPI_INFO_NULL);
Packit 0848f5
    if (err != MPI_SUCCESS)
Packit 0848f5
        handle_error(err, "MPI_File_set_view (read)");
Packit 0848f5
    err = MPI_File_iread(fh, buf, nints, MPI_INT, &request);
Packit 0848f5
    if (err != MPI_SUCCESS)
Packit 0848f5
        handle_error(err, "MPI_File_iread");
Packit 0848f5
#ifdef MPIO_USES_MPI_REQUEST
Packit 0848f5
    MPI_Wait(&request, &status);
Packit 0848f5
#else
Packit 0848f5
    MPIO_Wait(&request, &status);
Packit 0848f5
#endif
Packit 0848f5
    MPI_File_close(&fh;;
Packit 0848f5
Packit 0848f5
    /* check if the data read is correct */
Packit 0848f5
    for (i = 0; i < nints; i++) {
Packit 0848f5
        if (buf[i] != (rank * 100000 + i)) {
Packit 0848f5
            errs++;
Packit 0848f5
            if (errs < 25) {
Packit 0848f5
                fprintf(stderr, "Process %d: error, read %d, should be %d\n", rank, buf[i],
Packit 0848f5
                        rank * 100000 + i);
Packit 0848f5
            }
Packit 0848f5
            else if (errs == 25) {
Packit 0848f5
                fprintf(stderr, "Reached maximum number of errors to report\n");
Packit 0848f5
            }
Packit 0848f5
        }
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    free(buf);
Packit 0848f5
    free(filename);
Packit 0848f5
Packit 0848f5
    MTest_Finalize(errs);
Packit 0848f5
    MPI_Finalize();
Packit 0848f5
    return 0;
Packit 0848f5
}