File: opal_fifo.c

package info (click to toggle)
openmpi 3.1.3-11
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 118,572 kB
  • sloc: ansic: 628,972; f90: 17,993; makefile: 13,761; sh: 7,051; java: 6,360; perl: 3,215; cpp: 2,225; python: 1,350; lex: 988; fortran: 52; tcl: 12
file content (265 lines) | stat: -rw-r--r-- 7,323 bytes parent folder | download
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
/*
 * Copyright (c) 2014      Los Alamos National Security, LLC. All rights
 *                         reserved.
 * Copyright (c) 2018      IBM Corporation.  All rights reserved.
 * $COPYRIGHT$
 *
 * Additional copyrights may follow
 *
 * $HEADER$
 */

#include "opal_config.h"
#include <assert.h>

#include "support.h"
#include "opal/class/opal_fifo.h"
#include "opal/runtime/opal.h"
#include "opal/constants.h"

#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>

#include <sys/time.h>

#define OPAL_FIFO_TEST_THREAD_COUNT 8
#define ITERATIONS 1000000
#define ITEM_COUNT 100
#define ITEMS_PER_LOOP 30

#if !defined(timersub)
#define timersub(a, b, r) \
    do {                  \
        (r)->tv_sec = (a)->tv_sec - (b)->tv_sec;        \
        if ((a)->tv_usec < (b)->tv_usec) {              \
            (r)->tv_sec--;                              \
            (a)->tv_usec += 1000000;                    \
        }                                               \
        (r)->tv_usec = (a)->tv_usec - (b)->tv_usec;     \
    } while (0)
#endif

static void *thread_test (void *arg) {
    opal_fifo_t *fifo = (opal_fifo_t *) arg;
    opal_list_item_t *item;
    struct timeval start, stop, total;
    double timing;

    gettimeofday (&start, NULL);
    for (int i = 0 ; i < ITERATIONS ; ++i) {
        item = opal_fifo_pop_atomic (fifo);
        if (NULL != item) {
            (void) opal_fifo_push_atomic (fifo, item);
        }
    }
    gettimeofday (&stop, NULL);

    timersub(&stop, &start, &total);

    timing = ((double) total.tv_sec + (double) total.tv_usec * 1e-6) / (double) ITERATIONS;

    printf ("Atomics thread finished. Time: %d s %d us %d nsec/poppush\n", (int) total.tv_sec,
            (int)total.tv_usec, (int)(timing / 1e-9));

    pthread_exit(NULL);
}

static void *thread_test_exhaust (void *arg) {
  opal_fifo_t *fifo = (opal_fifo_t *) arg;
  opal_list_item_t *items[ITEMS_PER_LOOP];
  struct timeval start, stop, total;
  int item_count = 0;
  double timing;

  gettimeofday (&start, NULL);

  for (int i = 0 ; i < ITERATIONS ; i += ITEMS_PER_LOOP) {
    for (int j = 0 ; j < ITEMS_PER_LOOP ; ++j) {
      items[j] = opal_fifo_pop_atomic (fifo);
      if (items[j]) {
	++item_count;
      }
    }

    for (int j = 0 ; j < ITEMS_PER_LOOP ; ++j) {
      if (items[j]) {
	(void) opal_fifo_push_atomic (fifo, items[j]);
      }
    }
  }

  gettimeofday (&stop, NULL);

  timersub(&stop, &start, &total);

  timing = ((double) total.tv_sec + (double) total.tv_usec * 1e-6) / (double) item_count;

  fprintf (stderr, "Exhaustive atomics thread finished. Popped %d items. Time: %d s %d us %d nsec/poppush\n", item_count,
	   (int) total.tv_sec, (int)total.tv_usec, (int)(timing / 1e-9));

  return NULL;
}

static bool check_fifo_consistency (opal_fifo_t *fifo, int expected_count)
{
    volatile opal_list_item_t *volatile item;
    int count;

    for (count = 0, item = fifo->opal_fifo_head.data.item ; item != &fifo->opal_fifo_ghost ;
         item = opal_list_get_next(item), count++);

    return count == expected_count;
}

int main (int argc, char *argv[]) {
    pthread_t threads[OPAL_FIFO_TEST_THREAD_COUNT];
    opal_list_item_t *item, *prev, *item2;
    struct timeval start, stop, total;
    opal_fifo_t fifo;
    bool success;
    double timing;
    int rc;

    rc = opal_init_util (&argc, &argv);
    test_verify_int(OPAL_SUCCESS, rc);
    if (OPAL_SUCCESS != rc) {
        test_finalize();
        exit (1);
    }

    test_init("opal_fifo_t");

    OBJ_CONSTRUCT(&fifo, opal_fifo_t);

    item = OBJ_NEW(opal_list_item_t);
    prev = opal_fifo_push_st (&fifo, item);
    if (&fifo.opal_fifo_ghost == prev) {
        test_success ();
    } else {
        test_failure (" opal_fifo_push_st on empty fifo");
    }

    item2 = opal_fifo_pop_st (&fifo);
    if (item == item2) {
        test_success ();
    } else {
        test_failure (" opal_fifo_pop_st");
    }

    OBJ_RELEASE(item);

    for (int i = 0 ; i < ITEM_COUNT ; ++i) {
        item = OBJ_NEW(opal_list_item_t);
        item->item_free = 0;
        opal_fifo_push_st (&fifo, item);
    }

    if (check_fifo_consistency (&fifo, ITEM_COUNT)) {
        test_success ();
    } else {
        test_failure (" opal_fifo_push_st(multiple items)");
    }

    gettimeofday (&start, NULL);
    for (int i = 0 ; i < ITERATIONS ; ++i) {
        item = opal_fifo_pop_st (&fifo);
        (void) opal_fifo_push_st (&fifo, item);
    }
    gettimeofday (&stop, NULL);

    timersub(&stop, &start, &total);

    timing = ((double) total.tv_sec + (double) total.tv_usec * 1e-6) / (double) ITERATIONS;

    if (check_fifo_consistency (&fifo, ITEM_COUNT)) {
        test_success ();
    } else {
        test_failure (" fifo push/pop");
    }

    printf ("Single thread test. Time: %d s %d us %d nsec/poppush\n", (int) total.tv_sec,
            (int)total.tv_usec, (int)(timing / 1e-9));

    thread_test (&fifo);

    if (check_fifo_consistency (&fifo, ITEM_COUNT)) {
        test_success ();
    } else {
        test_failure (" fifo push/pop single-threaded with atomics");
    }

    gettimeofday (&start, NULL);
    for (int i = 0 ; i < OPAL_FIFO_TEST_THREAD_COUNT ; ++i) {
        pthread_create (threads + i, NULL, thread_test, &fifo);
    }

    for (int i = 0 ; i < OPAL_FIFO_TEST_THREAD_COUNT ; ++i) {
        void *ret;

        pthread_join (threads[i], &ret);
    }
    gettimeofday (&stop, NULL);

    timersub(&stop, &start, &total);

    timing = ((double) total.tv_sec + (double) total.tv_usec * 1e-6) / (double) (ITERATIONS * OPAL_FIFO_TEST_THREAD_COUNT);

    if (check_fifo_consistency (&fifo, ITEM_COUNT)) {
        test_success ();
    } else {
        test_failure (" fifo push/pop multi-threaded with atomics");
    }

    printf ("All threads finished. Thread count: %d Time: %d s %d us %d nsec/poppush\n",
            OPAL_FIFO_TEST_THREAD_COUNT, (int) total.tv_sec, (int)total.tv_usec, (int)(timing / 1e-9));


    gettimeofday (&start, NULL);
    for (int i = 0 ; i < OPAL_FIFO_TEST_THREAD_COUNT ; ++i) {
        pthread_create (threads + i, NULL, thread_test_exhaust, &fifo);
    }

    for (int i = 0 ; i < OPAL_FIFO_TEST_THREAD_COUNT ; ++i) {
        void *ret;

        pthread_join (threads[i], &ret);
    }
    gettimeofday (&stop, NULL);

    timersub(&stop, &start, &total);

    timing = ((double) total.tv_sec + (double) total.tv_usec * 1e-6) / (double) (ITERATIONS * OPAL_FIFO_TEST_THREAD_COUNT);

    if (check_fifo_consistency (&fifo, ITEM_COUNT)) {
        test_success ();
    } else {
        test_failure (" fifo push/pop multi-threaded with atomics when there are insufficient items");
    }

    printf ("All threads finished. Thread count: %d Time: %d s %d us %d nsec/poppush\n",
            OPAL_FIFO_TEST_THREAD_COUNT, (int) total.tv_sec, (int)total.tv_usec, (int)(timing / 1e-9));

    success = true;
    for (int i = 0 ; i < ITEM_COUNT ; ++i) {
        item = opal_fifo_pop_st (&fifo);
        if (NULL == item) {
            success = false;
            break;
        }
        OBJ_RELEASE(item);
    }

    if (success) {
        test_success ();
    } else {
        test_failure (" fifo pop all items");
    }

    OBJ_DESTRUCT(&fifo);

    opal_finalize_util ();

    return test_finalize ();
}