Blame test/mpi/io/i_hindexed_io.c

Packit 0848f5
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
Packit 0848f5
/*
Packit 0848f5
 *  (C) 2014 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 <unistd.h>
Packit 0848f5
Packit 0848f5
#define DATA_SIZE 324*4
Packit 0848f5
#define PAD 256
Packit 0848f5
#define HEADER 144
Packit 0848f5
#define BLK_COUNT 3
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
#define CHECK(fn) { int errcode; errcode = (fn); if (errcode != MPI_SUCCESS) handle_error(errcode, #fn); }
Packit 0848f5
Packit 0848f5
int main(int argc, char **argv)
Packit 0848f5
{
Packit 0848f5
    MPI_File fh;
Packit 0848f5
    MPI_Datatype file_type, mem_type;
Packit 0848f5
    int *data = NULL;
Packit 0848f5
    int *verify = NULL;
Packit 0848f5
    int data_size = DATA_SIZE;
Packit 0848f5
    int i, j, k, nr_errors = 0;
Packit 0848f5
    MPI_Aint disp[BLK_COUNT];
Packit 0848f5
    int block_lens[BLK_COUNT];
Packit 0848f5
    char *filename = "unnamed.dat";
Packit 0848f5
    MPI_Status status;
Packit 0848f5
    MPI_Request request;
Packit 0848f5
Packit 0848f5
    MPI_Init(&argc, &argv);
Packit 0848f5
    disp[0] = (MPI_Aint) (PAD);
Packit 0848f5
    disp[1] = (MPI_Aint) (data_size * 1 + PAD);
Packit 0848f5
    disp[2] = (MPI_Aint) (data_size * 2 + PAD);
Packit 0848f5
Packit 0848f5
    block_lens[0] = data_size;
Packit 0848f5
    block_lens[1] = data_size;
Packit 0848f5
    block_lens[2] = data_size;
Packit 0848f5
Packit 0848f5
    data = malloc(data_size);
Packit 0848f5
    verify = malloc(data_size * BLK_COUNT + HEADER + PAD);
Packit 0848f5
    for (i = 0; i < data_size / sizeof(int); i++)
Packit 0848f5
        data[i] = i;
Packit 0848f5
Packit 0848f5
    MPI_Type_create_hindexed_block(BLK_COUNT, data_size, disp, MPI_BYTE, &file_type);
Packit 0848f5
    MPI_Type_commit(&file_type);
Packit 0848f5
Packit 0848f5
    MPI_Type_create_hvector(BLK_COUNT, data_size, 0, MPI_BYTE, &mem_type);
Packit 0848f5
    MPI_Type_commit(&mem_type);
Packit 0848f5
Packit 0848f5
    if (1 < argc)
Packit 0848f5
        filename = argv[1];
Packit 0848f5
Packit 0848f5
    CHECK(MPI_File_open(MPI_COMM_WORLD, filename,
Packit 0848f5
                        MPI_MODE_RDWR | MPI_MODE_CREATE | MPI_MODE_DELETE_ON_CLOSE,
Packit 0848f5
                        MPI_INFO_NULL, &fh) != 0);
Packit 0848f5
Packit 0848f5
    CHECK(MPI_File_set_view(fh, HEADER, MPI_BYTE, file_type, "native", MPI_INFO_NULL));
Packit 0848f5
Packit 0848f5
    /* write everything */
Packit 0848f5
    CHECK(MPI_File_iwrite_at_all(fh, 0, data, 1, mem_type, &request));
Packit 0848f5
    MPI_Wait(&request, &status);
Packit 0848f5
Packit 0848f5
    /* verify */
Packit 0848f5
    CHECK(MPI_File_set_view(fh, 0, MPI_BYTE, MPI_BYTE, "native", MPI_INFO_NULL));
Packit 0848f5
    CHECK(MPI_File_iread_at_all(fh, 0,
Packit 0848f5
                                verify, (HEADER + PAD + BLK_COUNT * DATA_SIZE) / sizeof(int),
Packit 0848f5
                                MPI_INT, &request));
Packit 0848f5
    MPI_Wait(&request, &status);
Packit 0848f5
Packit 0848f5
    /* header and block padding should have no data */
Packit 0848f5
    for (i = 0; i < (HEADER + PAD) / sizeof(int); i++) {
Packit 0848f5
        if (verify[i] != 0) {
Packit 0848f5
            nr_errors++;
Packit 0848f5
            fprintf(stderr, "expected 0, read %d\n", verify[i]);
Packit 0848f5
        }
Packit 0848f5
    }
Packit 0848f5
    /* blocks are replicated */
Packit 0848f5
    for (j = 0; j < BLK_COUNT; j++) {
Packit 0848f5
        for (k = 0; k < (DATA_SIZE / sizeof(int)); k++) {
Packit 0848f5
            if (verify[(HEADER + PAD) / sizeof(int) + k + j * (DATA_SIZE / sizeof(int))]
Packit 0848f5
                != data[k]) {
Packit 0848f5
                nr_errors++;
Packit 0848f5
                fprintf(stderr, "expcted %d, read %d\n", data[k],
Packit 0848f5
                        verify[(HEADER + PAD) / sizeof(int) + k + j * (DATA_SIZE / sizeof(int))]);
Packit 0848f5
            }
Packit 0848f5
            i++;
Packit 0848f5
        }
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    MPI_File_close(&fh;;
Packit 0848f5
Packit 0848f5
    MPI_Type_free(&mem_type);
Packit 0848f5
    MPI_Type_free(&file_type);
Packit 0848f5
Packit 0848f5
    if (nr_errors == 0)
Packit 0848f5
        printf(" No Errors\n");
Packit 0848f5
Packit 0848f5
    MPI_Finalize();
Packit 0848f5
Packit 0848f5
    free(data);
Packit 0848f5
    free(verify);
Packit 0848f5
    return 0;
Packit 0848f5
}