|
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 |
|
|
Packit |
0848f5 |
#include "mpi.h"
|
|
Packit |
0848f5 |
#include <stdlib.h>
|
|
Packit |
0848f5 |
#include <string.h>
|
|
Packit |
0848f5 |
#include <stdio.h>
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
/* A 32^3 array. For other array sizes, change array_of_gsizes below. */
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
/* Uses nonblocking collective I/O. Writes a 3D block-distributed array to
|
|
Packit |
0848f5 |
a file corresponding to the global array in row-major (C) order, reads it
|
|
Packit |
0848f5 |
back, and checks that the data read is correct. */
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
/* Note that the file access pattern is noncontiguous. */
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
void handle_error(int errcode, const char *str);
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
void handle_error(int errcode, const 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 |
MPI_Datatype newtype;
|
|
Packit |
0848f5 |
int i, ndims, array_of_gsizes[3], array_of_distribs[3];
|
|
Packit |
0848f5 |
int order, nprocs, j, len;
|
|
Packit |
0848f5 |
int array_of_dargs[3], array_of_psizes[3];
|
|
Packit |
0848f5 |
int *readbuf, *writebuf, mynod, *tmpbuf, array_size;
|
|
Packit |
0848f5 |
MPI_Count bufcount;
|
|
Packit |
0848f5 |
char *filename;
|
|
Packit |
0848f5 |
int errs = 0, toterrs;
|
|
Packit |
0848f5 |
MPI_File fh;
|
|
Packit |
0848f5 |
MPI_Status status;
|
|
Packit |
0848f5 |
MPI_Request request;
|
|
Packit |
0848f5 |
MPI_Info info = MPI_INFO_NULL;
|
|
Packit |
0848f5 |
int errcode;
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
MPI_Init(&argc, &argv);
|
|
Packit |
0848f5 |
MPI_Comm_rank(MPI_COMM_WORLD, &mynod);
|
|
Packit |
0848f5 |
MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
/* process 0 broadcasts the file name to other processes */
|
|
Packit |
0848f5 |
if (!mynod) {
|
|
Packit |
0848f5 |
filename = "testfile";
|
|
Packit |
0848f5 |
len = strlen(filename);
|
|
Packit |
0848f5 |
MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
|
|
Packit |
0848f5 |
MPI_Bcast(filename, len + 1, 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 + 1);
|
|
Packit |
0848f5 |
MPI_Bcast(filename, len + 1, MPI_CHAR, 0, MPI_COMM_WORLD);
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
/* create the distributed array filetype */
|
|
Packit |
0848f5 |
ndims = 3;
|
|
Packit |
0848f5 |
order = MPI_ORDER_C;
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
array_of_gsizes[0] = 32;
|
|
Packit |
0848f5 |
array_of_gsizes[1] = 32;
|
|
Packit |
0848f5 |
array_of_gsizes[2] = 32;
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
array_of_distribs[0] = MPI_DISTRIBUTE_BLOCK;
|
|
Packit |
0848f5 |
array_of_distribs[1] = MPI_DISTRIBUTE_BLOCK;
|
|
Packit |
0848f5 |
array_of_distribs[2] = MPI_DISTRIBUTE_BLOCK;
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
array_of_dargs[0] = MPI_DISTRIBUTE_DFLT_DARG;
|
|
Packit |
0848f5 |
array_of_dargs[1] = MPI_DISTRIBUTE_DFLT_DARG;
|
|
Packit |
0848f5 |
array_of_dargs[2] = MPI_DISTRIBUTE_DFLT_DARG;
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
for (i = 0; i < ndims; i++)
|
|
Packit |
0848f5 |
array_of_psizes[i] = 0;
|
|
Packit |
0848f5 |
MPI_Dims_create(nprocs, ndims, array_of_psizes);
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
MPI_Type_create_darray(nprocs, mynod, ndims, array_of_gsizes,
|
|
Packit |
0848f5 |
array_of_distribs, array_of_dargs,
|
|
Packit |
0848f5 |
array_of_psizes, order, MPI_INT, &newtype);
|
|
Packit |
0848f5 |
MPI_Type_commit(&newtype);
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
/* initialize writebuf */
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
MPI_Type_size_x(newtype, &bufcount);
|
|
Packit |
0848f5 |
bufcount = bufcount / sizeof(int);
|
|
Packit |
0848f5 |
writebuf = (int *) malloc(bufcount * sizeof(int));
|
|
Packit |
0848f5 |
for (i = 0; i < bufcount; i++)
|
|
Packit |
0848f5 |
writebuf[i] = 1;
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
array_size = array_of_gsizes[0] * array_of_gsizes[1] * array_of_gsizes[2];
|
|
Packit |
0848f5 |
tmpbuf = (int *) calloc(array_size, sizeof(int));
|
|
Packit |
0848f5 |
MPI_Irecv(tmpbuf, 1, newtype, mynod, 10, MPI_COMM_WORLD, &request);
|
|
Packit |
0848f5 |
MPI_Send(writebuf, bufcount, MPI_INT, mynod, 10, MPI_COMM_WORLD);
|
|
Packit |
0848f5 |
MPI_Wait(&request, &status);
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
j = 0;
|
|
Packit |
0848f5 |
for (i = 0; i < array_size; i++)
|
|
Packit |
0848f5 |
if (tmpbuf[i]) {
|
|
Packit |
0848f5 |
writebuf[j] = i;
|
|
Packit |
0848f5 |
j++;
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
free(tmpbuf);
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
if (j != bufcount) {
|
|
Packit |
0848f5 |
fprintf(stderr, "Error in initializing writebuf on process %d\n", mynod);
|
|
Packit |
0848f5 |
MPI_Abort(MPI_COMM_WORLD, 1);
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
/* end of initialization */
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
/* write the array to the file */
|
|
Packit |
0848f5 |
errcode = MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_CREATE | MPI_MODE_RDWR, info, &fh;;
|
|
Packit |
0848f5 |
if (errcode != MPI_SUCCESS)
|
|
Packit |
0848f5 |
handle_error(errcode, "MPI_File_open");
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
errcode = MPI_File_set_view(fh, 0, MPI_INT, newtype, "native", info);
|
|
Packit |
0848f5 |
if (errcode != MPI_SUCCESS)
|
|
Packit |
0848f5 |
handle_error(errcode, "MPI_File_set_view");
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
errcode = MPI_File_iwrite_all(fh, writebuf, bufcount, MPI_INT, &request);
|
|
Packit |
0848f5 |
if (errcode != MPI_SUCCESS)
|
|
Packit |
0848f5 |
handle_error(errcode, "MPI_File_iwrite_all");
|
|
Packit |
0848f5 |
MPI_Wait(&request, &status);
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
errcode = MPI_File_close(&fh;;
|
|
Packit |
0848f5 |
if (errcode != MPI_SUCCESS)
|
|
Packit |
0848f5 |
handle_error(errcode, "MPI_File_close");
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
if (!mynod) {
|
|
Packit |
0848f5 |
/* wkl suggests potential for false " No Errors" if both read
|
|
Packit |
0848f5 |
* and write use the same file view */
|
|
Packit |
0848f5 |
/* solution: rank 0 reads entire file and checks write values */
|
|
Packit |
0848f5 |
errcode = MPI_File_open(MPI_COMM_SELF, filename, MPI_MODE_RDONLY, info, &fh;;
|
|
Packit |
0848f5 |
if (errcode != MPI_SUCCESS)
|
|
Packit |
0848f5 |
handle_error(errcode, "MPI_File_open");
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
readbuf = (int *) malloc(array_size * sizeof(int));
|
|
Packit |
0848f5 |
errcode = MPI_File_read(fh, readbuf, array_size, MPI_INT, &status);
|
|
Packit |
0848f5 |
if (errcode != MPI_SUCCESS)
|
|
Packit |
0848f5 |
handle_error(errcode, "MPI_File_read");
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
errcode = MPI_File_close(&fh;;
|
|
Packit |
0848f5 |
if (errcode != MPI_SUCCESS)
|
|
Packit |
0848f5 |
handle_error(errcode, "MPI_File_close");
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
for (i = 0; i < array_size; i++)
|
|
Packit |
0848f5 |
if (readbuf[i] != i) {
|
|
Packit |
0848f5 |
errs++;
|
|
Packit |
0848f5 |
fprintf(stderr, "Error: write integer %d but read %d\n", i, readbuf[i]);
|
|
Packit |
0848f5 |
break;
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
free(readbuf);
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
MPI_Barrier(MPI_COMM_WORLD);
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
/* now read it back */
|
|
Packit |
0848f5 |
readbuf = (int *) malloc(bufcount * sizeof(int));
|
|
Packit |
0848f5 |
errcode = MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_CREATE | MPI_MODE_RDWR, info, &fh;;
|
|
Packit |
0848f5 |
if (errcode != MPI_SUCCESS)
|
|
Packit |
0848f5 |
handle_error(errcode, "MPI_File_open");
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
errcode = MPI_File_set_view(fh, 0, MPI_INT, newtype, "native", info);
|
|
Packit |
0848f5 |
if (errcode != MPI_SUCCESS)
|
|
Packit |
0848f5 |
handle_error(errcode, "MPI_File_set_view");
|
|
Packit |
0848f5 |
errcode = MPI_File_iread_all(fh, readbuf, bufcount, MPI_INT, &request);
|
|
Packit |
0848f5 |
if (errcode != MPI_SUCCESS)
|
|
Packit |
0848f5 |
handle_error(errcode, "MPI_File_iread_all");
|
|
Packit |
0848f5 |
MPI_Wait(&request, &status);
|
|
Packit |
0848f5 |
errcode = MPI_File_close(&fh;;
|
|
Packit |
0848f5 |
if (errcode != MPI_SUCCESS)
|
|
Packit |
0848f5 |
handle_error(errcode, "MPI_File_close");
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
/* check the data read */
|
|
Packit |
0848f5 |
for (i = 0; i < bufcount; i++) {
|
|
Packit |
0848f5 |
if (readbuf[i] != writebuf[i]) {
|
|
Packit |
0848f5 |
errs++;
|
|
Packit |
0848f5 |
fprintf(stderr, "Process %d, readbuf %d, writebuf %d, i %d\n",
|
|
Packit |
0848f5 |
mynod, readbuf[i], writebuf[i], i);
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
MPI_Allreduce(&errs, &toterrs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
|
|
Packit |
0848f5 |
if (mynod == 0) {
|
|
Packit |
0848f5 |
if (toterrs > 0) {
|
|
Packit |
0848f5 |
fprintf(stderr, "Found %d errors\n", toterrs);
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
else {
|
|
Packit |
0848f5 |
fprintf(stdout, " No Errors\n");
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
MPI_Type_free(&newtype);
|
|
Packit |
0848f5 |
free(readbuf);
|
|
Packit |
0848f5 |
free(writebuf);
|
|
Packit |
0848f5 |
if (mynod)
|
|
Packit |
0848f5 |
free(filename);
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
MPI_Finalize();
|
|
Packit |
0848f5 |
return 0;
|
|
Packit |
0848f5 |
}
|