Blame test/mpi/io/async_any.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 "mpitest.h"
Packit 0848f5
Packit 0848f5
/*
Packit 0848f5
static char MTEST_Descrip[] = "Test asynchronous I/O w/ multiple completion";
Packit 0848f5
*/
Packit 0848f5
Packit 0848f5
#define SIZE (65536)
Packit 0848f5
#define NUMOPS 10
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
int main(int argc, char **argv)
Packit 0848f5
{
Packit 0848f5
    int *buf, i, rank, nints, len;
Packit 0848f5
    char *filename, *tmp;
Packit 0848f5
    int errs = 0;
Packit 0848f5
    MPI_File fh;
Packit 0848f5
    MPI_Status statuses[NUMOPS];
Packit 0848f5
    MPI_Request requests[NUMOPS];
Packit 0848f5
Packit 0848f5
    MTest_Init(&argc, &argv);
Packit 0848f5
    MPI_Comm_rank(MPI_COMM_WORLD, &rank;;
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
        while ((i < argc) && strcmp("-fname", *argv)) {
Packit 0848f5
            i++;
Packit 0848f5
            argv++;
Packit 0848f5
        }
Packit 0848f5
        if (i >= argc) {
Packit 0848f5
            /* Use a default filename of testfile */
Packit 0848f5
            len = 8;
Packit 0848f5
            filename = (char *) malloc(len + 10);
Packit 0848f5
            memset(filename, 0, (len + 10) * sizeof(char));
Packit 0848f5
            strcpy(filename, "testfile");
Packit 0848f5
            /*
Packit 0848f5
             * fprintf(stderr, "\n*#  Usage: async_any -fname filename\n\n");
Packit 0848f5
             * MPI_Abort(MPI_COMM_WORLD, 1);
Packit 0848f5
             */
Packit 0848f5
        }
Packit 0848f5
        else {
Packit 0848f5
            argv++;
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
        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
    }
Packit 0848f5
    else {
Packit 0848f5
        MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
Packit 0848f5
        filename = (char *) malloc(len + 10);
Packit 0848f5
        MTEST_VG_MEM_INIT(filename, (len + 10) * sizeof(char));
Packit 0848f5
        MPI_Bcast(filename, len + 10, MPI_CHAR, 0, MPI_COMM_WORLD);
Packit 0848f5
    }
Packit 0848f5
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
Packit 0848f5
    MPI_File_open(MPI_COMM_SELF, filename, MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh;;
Packit 0848f5
    MPI_File_set_view(fh, 0, MPI_INT, MPI_INT, (char *) "native", MPI_INFO_NULL);
Packit 0848f5
    for (i = 0; i < NUMOPS; i++) {
Packit 0848f5
        MPI_File_iwrite(fh, buf, nints, MPI_INT, &(requests[i]));
Packit 0848f5
    }
Packit 0848f5
    MPI_Waitall(NUMOPS, requests, statuses);
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
    MPI_File_open(MPI_COMM_SELF, filename, MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh;;
Packit 0848f5
    MPI_File_set_view(fh, 0, MPI_INT, MPI_INT, (char *) "native", MPI_INFO_NULL);
Packit 0848f5
    for (i = 0; i < NUMOPS; i++) {
Packit 0848f5
        MPI_File_iread(fh, buf, nints, MPI_INT, &(requests[i]));
Packit 0848f5
    }
Packit 0848f5
    MPI_Waitall(NUMOPS, requests, statuses);
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
            fprintf(stderr, "Process %d: error, read %d, should be %d\n", rank, buf[i],
Packit 0848f5
                    rank * 100000 + i);
Packit 0848f5
        }
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    free(buf);
Packit 0848f5
    free(filename);
Packit 0848f5
    free(tmp);
Packit 0848f5
Packit 0848f5
    MTest_Finalize(errs);
Packit 0848f5
    MPI_Finalize();
Packit 0848f5
    return 0;
Packit 0848f5
}