|
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 |
#define COUNT (200)
|
|
Packit Service |
c5cf8c |
#undef TIMING
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
void handle_error(int errcode, const char *str);
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
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 |
}
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
/* tests shared file pointer functions */
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
int main(int argc, char **argv)
|
|
Packit Service |
c5cf8c |
{
|
|
Packit Service |
c5cf8c |
int *buf, i, rank, nprocs, len, sum;
|
|
Packit Service |
c5cf8c |
int global_sum;
|
|
Packit Service |
c5cf8c |
int errs = 0, toterrs, errcode;
|
|
Packit Service |
c5cf8c |
char *filename;
|
|
Packit Service |
c5cf8c |
MPI_File fh;
|
|
Packit Service |
c5cf8c |
MPI_Status status;
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
MPI_Init(&argc, &argv);
|
|
Packit Service |
c5cf8c |
MPI_Comm_rank(MPI_COMM_WORLD, &rank;;
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
double wr_stime, wr_etime, wr_time, wr_sumtime;
|
|
Packit Service |
c5cf8c |
double rd_stime, rd_etime, rd_time, rd_sumtime;
|
|
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 (!rank) {
|
|
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: shared_fp -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 |
filename = (char *) malloc(len + 10);
|
|
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 + 10, MPI_CHAR, 0, MPI_COMM_WORLD);
|
|
Packit Service |
c5cf8c |
} else {
|
|
Packit Service |
c5cf8c |
MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
|
|
Packit Service |
c5cf8c |
filename = (char *) malloc(len + 10);
|
|
Packit Service |
c5cf8c |
MPI_Bcast(filename, len + 10, MPI_CHAR, 0, MPI_COMM_WORLD);
|
|
Packit Service |
c5cf8c |
}
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
buf = (int *) malloc(COUNT * sizeof(int));
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
MPI_Comm_rank(MPI_COMM_WORLD, &rank;;
|
|
Packit Service |
c5cf8c |
MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
for (i = 0; i < COUNT; i++)
|
|
Packit Service |
c5cf8c |
buf[i] = COUNT * rank + i;
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
errcode = MPI_File_open(MPI_COMM_WORLD, filename,
|
|
Packit Service |
c5cf8c |
MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh;;
|
|
Packit Service |
c5cf8c |
if (errcode != MPI_SUCCESS) {
|
|
Packit Service |
c5cf8c |
handle_error(errcode, "MPI_File_open");
|
|
Packit Service |
c5cf8c |
}
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
wr_stime = MPI_Wtime();
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
errcode = MPI_File_write_ordered(fh, buf, COUNT, MPI_INT, &status);
|
|
Packit Service |
c5cf8c |
if (errcode != MPI_SUCCESS) {
|
|
Packit Service |
c5cf8c |
handle_error(errcode, "MPI_File_write_shared");
|
|
Packit Service |
c5cf8c |
}
|
|
Packit Service |
c5cf8c |
wr_etime = MPI_Wtime();
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
for (i = 0; i < COUNT; i++)
|
|
Packit Service |
c5cf8c |
buf[i] = 0;
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
MPI_Barrier(MPI_COMM_WORLD);
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
rd_stime = MPI_Wtime();
|
|
Packit Service |
c5cf8c |
errcode = MPI_File_seek_shared(fh, 0, MPI_SEEK_SET);
|
|
Packit Service |
c5cf8c |
if (errcode != MPI_SUCCESS) {
|
|
Packit Service |
c5cf8c |
handle_error(errcode, "MPI_File_seek_shared");
|
|
Packit Service |
c5cf8c |
}
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
errcode = MPI_File_read_ordered(fh, buf, COUNT, MPI_INT, &status);
|
|
Packit Service |
c5cf8c |
if (errcode != MPI_SUCCESS) {
|
|
Packit Service |
c5cf8c |
handle_error(errcode, "MPI_File_read_shared");
|
|
Packit Service |
c5cf8c |
}
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
rd_etime = MPI_Wtime();
|
|
Packit Service |
c5cf8c |
MPI_File_close(&fh;;
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
sum = 0;
|
|
Packit Service |
c5cf8c |
for (i = 0; i < COUNT; i++)
|
|
Packit Service |
c5cf8c |
sum += buf[i];
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
MPI_Allreduce(&sum, &global_sum, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
wr_time = wr_etime - wr_stime;
|
|
Packit Service |
c5cf8c |
rd_time = rd_etime - rd_stime;
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
MPI_Allreduce(&wr_time, &wr_sumtime, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
|
|
Packit Service |
c5cf8c |
MPI_Allreduce(&rd_time, &rd_sumtime, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
if (global_sum != (((COUNT * nprocs - 1) * (COUNT * nprocs)) / 2)) {
|
|
Packit Service |
c5cf8c |
errs++;
|
|
Packit Service |
c5cf8c |
fprintf(stderr, "Error: sum %d, global_sum %d, %d\n",
|
|
Packit Service |
c5cf8c |
sum, global_sum, (((COUNT * nprocs - 1) * (COUNT * nprocs)) / 2));
|
|
Packit Service |
c5cf8c |
}
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
free(buf);
|
|
Packit Service |
c5cf8c |
free(filename);
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
MPI_Allreduce(&errs, &toterrs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
|
|
Packit Service |
c5cf8c |
if (rank == 0) {
|
|
Packit Service |
c5cf8c |
if (toterrs > 0) {
|
|
Packit Service |
c5cf8c |
fprintf(stderr, "Found %d errors\n", toterrs);
|
|
Packit Service |
c5cf8c |
} else {
|
|
Packit Service |
c5cf8c |
fprintf(stdout, " No Errors\n");
|
|
Packit Service |
c5cf8c |
#ifdef TIMING
|
|
Packit Service |
c5cf8c |
fprintf(stderr, "nprocs: %d bytes: %d write: %f read %f\n",
|
|
Packit Service |
c5cf8c |
nprocs, COUNT * sizeof(int), wr_sumtime, rd_sumtime);
|
|
Packit Service |
c5cf8c |
#endif
|
|
Packit Service |
c5cf8c |
}
|
|
Packit Service |
c5cf8c |
}
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
MPI_Finalize();
|
|
Packit Service |
c5cf8c |
return 0;
|
|
Packit Service |
c5cf8c |
}
|