Blame src/mpi/romio/test/large_array.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 <stdio.h>
Packit Service c5cf8c
#include <string.h>
Packit Service c5cf8c
#include <stdlib.h>
Packit Service c5cf8c
Packit Service c5cf8c
/* Writes a 4-Gbyte distributed array, reads it back, and then deletes the
Packit Service c5cf8c
   file. Uses collective I/O. */
Packit Service c5cf8c
/* The file name is taken as a command-line argument. */
Packit Service c5cf8c
/* Run it only on a machine with sufficient memory and a file system
Packit Service c5cf8c
   on which ROMIO supports large files, i.e., any file system created after
Packit Service c5cf8c
   1999 */
Packit Service c5cf8c
Packit Service c5cf8c
/* This program will work only if the MPI implementation defines MPI_Aint
Packit Service c5cf8c
   as a 64-bit integer. */
Packit Service c5cf8c
Packit Service c5cf8c
static 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
#define MPI_CHECK(fn) { int errcode; errcode = (fn); if (errcode != MPI_SUCCESS) handle_error(errcode, #fn); }
Packit Service c5cf8c
Packit Service c5cf8c
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, len, flag, err;
Packit Service c5cf8c
    int array_of_dargs[3], array_of_psizes[3];
Packit Service c5cf8c
    int *readbuf, *writebuf, mynod;
Packit Service c5cf8c
    MPI_Count bufcount;
Packit Service c5cf8c
    char filename[1024];
Packit Service c5cf8c
    MPI_File fh;
Packit Service c5cf8c
    MPI_Status status;
Packit Service c5cf8c
    MPI_Aint size_with_aint;
Packit Service c5cf8c
    MPI_Offset size_with_offset;
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: large_array -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
        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
        fprintf(stderr,
Packit Service c5cf8c
                "This program creates a 4 Gbyte file. Don't run it if you don't have that much disk space!\n");
Packit Service c5cf8c
    } else {
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
    }
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] = 1024;
Packit Service c5cf8c
    array_of_gsizes[1] = 1024;
Packit Service c5cf8c
    array_of_gsizes[2] = 4 * 1024 / sizeof(int);
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
/* check if MPI_Aint is large enough for size of global array.
Packit Service c5cf8c
   if not, complain. */
Packit Service c5cf8c
Packit Service c5cf8c
    size_with_aint = sizeof(int);
Packit Service c5cf8c
    for (i = 0; i < ndims; i++)
Packit Service c5cf8c
        size_with_aint *= array_of_gsizes[i];
Packit Service c5cf8c
    size_with_offset = sizeof(int);
Packit Service c5cf8c
    for (i = 0; i < ndims; i++)
Packit Service c5cf8c
        size_with_offset *= array_of_gsizes[i];
Packit Service c5cf8c
    if (size_with_aint != size_with_offset) {
Packit Service c5cf8c
        fprintf(stderr,
Packit Service c5cf8c
                "Can't use an array of this size unless the MPI implementation defines a 64-bit MPI_Aint\n");
Packit Service c5cf8c
        MPI_Abort(MPI_COMM_WORLD, 1);
Packit Service c5cf8c
    }
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
    if (!writebuf)
Packit Service c5cf8c
        fprintf(stderr, "Process %d, not enough memory for writebuf\n", mynod);
Packit Service c5cf8c
    for (i = 0; i < bufcount; i++)
Packit Service c5cf8c
        writebuf[i] = mynod * 1024 + i;
Packit Service c5cf8c
Packit Service c5cf8c
    /* write the array to the file */
Packit Service c5cf8c
    MPI_CHECK(MPI_File_open(MPI_COMM_WORLD, filename,
Packit Service c5cf8c
                            MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh));
Packit Service c5cf8c
    MPI_CHECK(MPI_File_set_view(fh, 0, MPI_INT, newtype, "native", MPI_INFO_NULL));
Packit Service c5cf8c
    MPI_CHECK(MPI_File_write_all(fh, writebuf, bufcount, MPI_INT, &status));
Packit Service c5cf8c
    MPI_CHECK(MPI_File_close(&fh));
Packit Service c5cf8c
Packit Service c5cf8c
    free(writebuf);
Packit Service c5cf8c
Packit Service c5cf8c
    /* now read it back */
Packit Service c5cf8c
    readbuf = (int *) calloc(bufcount, sizeof(int));
Packit Service c5cf8c
    if (!readbuf)
Packit Service c5cf8c
        fprintf(stderr, "Process %d, not enough memory for readbuf\n", mynod);
Packit Service c5cf8c
Packit Service c5cf8c
    MPI_CHECK(MPI_File_open(MPI_COMM_WORLD, filename,
Packit Service c5cf8c
                            MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh));
Packit Service c5cf8c
    MPI_CHECK(MPI_File_set_view(fh, 0, MPI_INT, newtype, "native", MPI_INFO_NULL));
Packit Service c5cf8c
    MPI_CHECK(MPI_File_read_all(fh, readbuf, bufcount, MPI_INT, &status));
Packit Service c5cf8c
    MPI_CHECK(MPI_File_close(&fh));
Packit Service c5cf8c
Packit Service c5cf8c
    /* check the data read */
Packit Service c5cf8c
    flag = 0;
Packit Service c5cf8c
    for (i = 0; i < bufcount; i++)
Packit Service c5cf8c
        if (readbuf[i] != mynod * 1024 + i) {
Packit Service c5cf8c
            fprintf(stderr, "Process %d, readbuf=%d, writebuf=%d\n", mynod, readbuf[i],
Packit Service c5cf8c
                    mynod * 1024 + i);
Packit Service c5cf8c
            flag = 1;
Packit Service c5cf8c
        }
Packit Service c5cf8c
    if (!flag)
Packit Service c5cf8c
        fprintf(stderr, "Process %d: data read back is correct\n", mynod);
Packit Service c5cf8c
Packit Service c5cf8c
    MPI_Type_free(&newtype);
Packit Service c5cf8c
    free(readbuf);
Packit Service c5cf8c
Packit Service c5cf8c
    MPI_Barrier(MPI_COMM_WORLD);
Packit Service c5cf8c
    if (!mynod) {
Packit Service c5cf8c
        err = MPI_File_delete(filename, MPI_INFO_NULL);
Packit Service c5cf8c
        if (err == MPI_SUCCESS)
Packit Service c5cf8c
            fprintf(stderr, "file deleted\n");
Packit Service c5cf8c
    }
Packit Service c5cf8c
Packit Service c5cf8c
    MPI_Finalize();
Packit Service c5cf8c
    return 0;
Packit Service c5cf8c
}