cp-wtimewrap-bench.c 3.32 KB
Newer Older
1
2
3
4
5
/*
 *  (C) 2009 by Argonne National Laboratory.
 *      See COPYRIGHT in top-level directory.
 */

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
/* Benchmark to measure Chutzpah overhead.  This is an MPI program that uses
 * exactly one process to perform I/O on /dev/zero for testing purposes
 */

/* Arguments: an integer specifying the number of iterations to run of each
 * test phase
 */

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <mpi.h>

extern ssize_t __real_write(int fd, const void *buf, size_t count);
extern int __real_gethostname(char *name, size_t namelen);

int __wrap_gethostname(char *name, size_t namelen)
{
    int ret;
    double time1, time2;
    double time3;

    time1 = MPI_Wtime();
    ret = __real_gethostname(name, namelen);
    time2 = MPI_Wtime();

    time3 = time2-time1;

    return(ret);
}

ssize_t __wrap_write(int fd, const void *buf, size_t count)
{
    ssize_t ret;
    double time1, time2;
    double time3;

    time1 = MPI_Wtime();
    ret = __real_write(fd, buf, count);
    time2 = MPI_Wtime();

    time3 = time2-time1;

    return(ret);
}

int main(int argc, char **argv) 
{
    int nprocs;
    int mynod;
    int i;
    int ret;
    int fd;
    int iters;
    double time1, time2, write1, write2, host1, host2;
    char onechar;
    char host[256];

    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 benchmark should be run with exactly one process.\n");
        }
        MPI_Finalize();
        return(-1);
    }

    /* parse argument for number of iterations */
    if(argc != 2)
    {
        fprintf(stderr, "Usage: %s <number of iterations>\n", argv[0]);
        MPI_Finalize();
        return(-1);
    }

    ret = sscanf(argv[1], "%d", &iters);
    if(ret != 1)
    {
        fprintf(stderr, "Usage: %s <number of iterations>\n", argv[0]);
        MPI_Finalize();
        return(-1);
    }

    /* first measure cost of wtime */
    time1 = MPI_Wtime();
    for(i=0; i<iters; i++)
    {
        write1 = MPI_Wtime();
    }
    time2 = MPI_Wtime();

    sleep(1);

    /* open dev null */
    fd = open("/dev/null", O_RDWR);
    if(fd < 0)
    {
        perror("open");
        MPI_Finalize();
        return(-1);
    }

    /* write a single byte */
    write1 = MPI_Wtime();
    for(i=0; i<iters; i++)
    {
        ret = write(fd, &onechar, 1);
        if(ret < 0)
        {
            perror("write");
            MPI_Finalize();
            return(-1);
        }
    }
    write2 = MPI_Wtime();

    close(fd);
 
    sleep(1);
 
    /* get hostname */
    host1 = MPI_Wtime();
    for(i=0; i<iters; i++)
    {
        ret = gethostname(host, 256);
        if(ret < 0)
        {
            perror("gethostname");
            MPI_Finalize();
            return(-1);
        }
    }
    host2 = MPI_Wtime();


    /* print out some timing info */
    printf("#<op>\t<iters>\t<total (s)>\t<per op (s)>\n");
    printf("wtime\t%d\t%.9f\t%.9f\n", iters, time2-time1, (time2-time1)/(double)iters);
    printf("write\t%d\t%.9f\t%.9f\n", iters, write2-write1, (write2-write1)/(double)iters);
    printf("gethostname\t%d\t%.9f\t%.9f\n", iters, host2-host1, (host2-host1)/(double)iters);

    MPI_Finalize();
    return(0);
}