seek-test.c 4.01 KB
Newer Older
Philip Carns's avatar
Philip Carns committed
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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
/*
 *  (C) 2009 by Argonne National Laboratory.
 *      See COPYRIGHT in top-level directory.
 */

/* Test program to excercise both lseek and pwrite() for large and small
 * files
 */

/* Argument: path to place four sparse files: lseek-small.dat,
 * pwrite-small.dat, lseek-big.dat, and pwrite-big.dat.
 */

#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

#include <mpi.h>

int main(int argc, char **argv) 
{
    int nprocs;
    int mynod;
    int ret;
    int fd;
    char path_lseek_small[256];
    char path_lseek_big[256];
    char path_pwrite_small[256];
    char path_pwrite_big[256];
    off_t offset;
    off_t lseek_ret;
    char data;
    ssize_t pwrite_ret;

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &mynod);
    MPI_Comm_size(MPI_COMM_WORLD, &nprocs);

    /* we only want one proc */
    if(nprocs > 1)
    {
        if(mynod == 0)
        {
            fprintf(stderr, "Error: this test should be run with exactly one process.\n");
        }
        MPI_Finalize();
        return(-1);
    }

    if(argc != 2)
    {
        fprintf(stderr, "Usage: %s <path>\n", argv[0]);
        MPI_Finalize();
        return(-1);
    }

    /* generate name of target files */
    sprintf(path_lseek_small, "%s/lseek-small.dat", argv[1]);
    sprintf(path_lseek_big, "%s/lseek-big.dat", argv[1]);
    sprintf(path_pwrite_small, "%s/pwrite-small.dat", argv[1]);
    sprintf(path_pwrite_big, "%s/pwrite-big.dat", argv[1]);

    /* lseek and write on small file */
    fd = open(path_lseek_small, O_CREAT|O_EXCL|O_WRONLY, S_IRUSR|S_IWUSR);
    if(fd < 0)
    {
        perror("open");
        fprintf(stderr, "%s\n", path_lseek_small);
        MPI_Finalize();
        return(-1);
    }

    offset = 4095;
    lseek_ret = lseek(fd, offset, SEEK_SET);    
    if(lseek_ret != offset)
    {
        perror("lseek");
        fprintf(stderr, "%s\n", path_lseek_small);
        MPI_Finalize();
        return(-1);
    }

    ret = write(fd, &data, 1);
    if(ret < 0)
    {
        perror("write");
        fprintf(stderr, "%s\n", path_lseek_small);
        MPI_Finalize();
        return(-1);
    }
    close(fd);
 
    /* lseek and write on big file */
    fd = open(path_lseek_big, O_CREAT|O_EXCL|O_WRONLY, S_IRUSR|S_IWUSR);
    if(fd < 0)
    {
        perror("open");
        fprintf(stderr, "%s\n", path_lseek_big);
        MPI_Finalize();
        return(-1);
    }

    offset = 1024;
    offset *= 1024;
    offset *= 1024;
    offset *= 3;
    offset -= 1;
    lseek_ret = lseek(fd, offset, SEEK_SET);    
    if(lseek_ret != offset)
    {
        perror("lseek");
        fprintf(stderr, "%s\n", path_lseek_big);
        MPI_Finalize();
        return(-1);
    }

    ret = write(fd, &data, 1);
    if(ret < 0)
    {
        perror("write");
        fprintf(stderr, "%s\n", path_lseek_big);
        MPI_Finalize();
        return(-1);
    }
    
    close(fd);

    /* pwrite on small file */
    fd = open(path_pwrite_small, O_CREAT|O_EXCL|O_WRONLY, S_IRUSR|S_IWUSR);
    if(fd < 0)
    {
        perror("open");
        fprintf(stderr, "%s\n", path_pwrite_small);
        MPI_Finalize();
        return(-1);
    }

    offset = 4095;
    pwrite_ret = pwrite(fd, &data, 1, offset);    
    if(pwrite_ret != 1)
    {
        perror("pwrite");
        fprintf(stderr, "%s\n", path_pwrite_small);
        MPI_Finalize();
        return(-1);
    }
    close(fd);
 
    /* pwrite and write on big file */
    fd = open(path_pwrite_big, O_CREAT|O_EXCL|O_WRONLY, S_IRUSR|S_IWUSR);
    if(fd < 0)
    {
        perror("open");
        fprintf(stderr, "%s\n", path_pwrite_big);
        MPI_Finalize();
        return(-1);
    }

    offset = 1024;
    offset *= 1024;
    offset *= 1024;
    offset *= 3;
    offset -= 1;
    pwrite_ret = pwrite(fd, &data, 1, offset);    
    if(pwrite_ret != 1)
    {
        perror("pwrite");
        fprintf(stderr, "%s\n", path_pwrite_big);
        MPI_Finalize();
        return(-1);
    }
    
    close(fd);

    MPI_Finalize();
    return(0);
}