partshared.c 2.87 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <mpi.h>

#define FILE_COUNT (64)

int main (int argc, char **argv)
{
    int rc;
    int world_rank;
    int world_size;
    int sub_rank;
    int count;
    char* zerobuf;
    double stime;
    double etime;
    double ttime;
    double maxtime;
    char fname[256];
    MPI_Offset offset;
    MPI_Comm sub_comm;
    MPI_File fh;
    MPI_Status status;

    count = 1024*1024*50;

    zerobuf = malloc(count);
    assert(zerobuf); 
    memset(zerobuf, 0, count);

    rc = MPI_Init(&argc, &argv);
    if (rc != MPI_SUCCESS)
    {
        fprintf(stderr, "MPI_Init failed: %d\n", rc);
    }

    rc = MPI_Comm_rank (MPI_COMM_WORLD, &world_rank);
    if (rc != MPI_SUCCESS)
    {
        fprintf(stderr, "MPI_Comm_rank failed: %d\n", rc);
    }

    rc = MPI_Comm_size (MPI_COMM_WORLD, &world_size);
    if (rc != MPI_SUCCESS)
    {
        fprintf(stderr, "MPI_Comm_size failed: %d\n", rc);
    }

    if ((world_size % FILE_COUNT) && (world_rank == 0))
    {
        fprintf(stderr, "Not evenly disible node size\n");
    }

    rc = MPI_Comm_split(MPI_COMM_WORLD,
                        (world_rank%FILE_COUNT),
                        world_rank,
                        &sub_comm);
    if (rc != MPI_SUCCESS)
    {
        fprintf(stderr, "MPI_Comm_split failed: %d\n", rc);
    }

    rc = MPI_Comm_rank(sub_comm, &sub_rank);
    if (rc != MPI_SUCCESS)
    {
        fprintf(stderr, "MPI_Comm_rank2 failed: %d\n", rc);
    }

    memset(fname, 0, sizeof(fname));

    if (sub_rank == 0)
    {
        sprintf(fname, "%s.%d", "partfile", world_rank);
    }

    MPI_Barrier(sub_comm);

    MPI_Bcast(fname, sizeof(fname), MPI_BYTE, 0, sub_comm);

    MPI_Barrier(MPI_COMM_WORLD);

    stime = MPI_Wtime();

    rc = MPI_File_open(sub_comm,
                       fname,
                       (MPI_MODE_CREATE|MPI_MODE_DELETE_ON_CLOSE|MPI_MODE_RDWR),
                       MPI_INFO_NULL,
                       &fh);
    if (rc != MPI_SUCCESS)
    {
        fprintf(stderr, "MPI_File_open failed: %d\n", rc);
    }

    offset = sub_rank * count;

    rc = MPI_File_write_at(fh, offset, zerobuf, count, MPI_BYTE, &status);
    if (rc != MPI_SUCCESS)
    {
        fprintf(stderr, "MPI_File_write_at failed: %d\n", rc);
    }

    rc = MPI_File_close(&fh);
    if (rc != MPI_SUCCESS)
    {
        fprintf(stderr, "MPI_File_close failed: %d\n", rc);
    }

    etime = MPI_Wtime();
    ttime = etime-stime;

    MPI_Comm_free (&sub_comm);

    MPI_Barrier(MPI_COMM_WORLD);

    rc= MPI_Reduce(&ttime, &maxtime, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);

   if (world_rank == 0)
   {
       double total_Mbytes = ((double)count * (double)world_size) / (1024.0*1024.0);
       printf("slowest time: %lf\n", maxtime);
       printf("total Mbytes: %lf\n", total_Mbytes);
       printf("rate: %lf\n", (total_Mbytes/maxtime));
   }

    MPI_Finalize();

    return 0;
}