source: doc/theses/mike_brooks_MMath/benchmarks/list/driver.c @ 34b6a7b6

ADTast-experimental
Last change on this file since 34b6a7b6 was fa6ca1a, checked in by Mike Brooks <mlbrooks@…>, 15 months ago

LL perf: reduce frequency of using volatile variables.

Significantly improves stability across reruns.
5-rep extreme outcomes are within 5% of each other, on 46 out of 51 test programs.
3 of those 5 exceptions, and the only ones more than 7% apart, are uC++.

  • Property mode set to 100644
File size: 5.9 KB
Line 
1#include <time.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5
6#include "observation.h"
7
8typedef struct B_UserItem
9    BFX_EXTRUSION_DECL(B_UserItem)
10{
11    BFX_INTRUSION(B_UserItem)
12    int userdata[64];
13}
14B_UserItem;
15
16BFX_EXTRUSION_FOLLOWUP(B_UserItem)
17
18#if defined(NDEBUG) || (defined(__cforall) && !defined(__CFA_DEBUG__))
19    enum { DefaultNumNodes = 1000, DefaultExperimentDurSec = 1, DefaultCheckClockFreq = 1000, DefaultExperimentDurOpCount = -1 };
20    #define TRACE(tp)
21#else
22    enum { DefaultNumNodes = 10, DefaultExperimentDurSec = 1, DefaultCheckClockFreq = 2, DefaultExperimentDurOpCount = 20 };
23    static const char * tp_filter
24    // = "";
25    // = "+ea";
26        = "*";
27    #define TRACE(tp) \
28        if (strcmp("*", tp_filter) == 0 || strchr(tp_filter, tp)) { \
29            printf("%c", tp); \
30            bobs_report(); \
31        }
32#endif
33
34static B_UserItem *ui = NULL;
35
36static BFX_LISTED_ELEM_T(B_UserItem) *listedItems = NULL;
37static BFX_LISTED_ELEM_T(B_UserItem) observedItem;
38
39static BFX_LIST_HEAD_T(B_UserItem) lst;
40
41
42MAYBE_EXTERN_C (
43
44    volatile size_t       bobs_ops_completed      = 0;
45    volatile unsigned int bobs_prog_inserting     = 0;
46    volatile unsigned int bobs_prog_removing      = 0;
47    volatile unsigned int bobs_prog_rollover_flag = 0;
48
49    void bobs_seek(unsigned int i) {
50        observedItem = listedItems[i];
51    }
52
53    void bobs_moveNext() {
54        observedItem = BFX_GET_AFTER(B_UserItem, lst, observedItem);
55    }
56
57    void bobs_movePrev() {
58        observedItem = BFX_GET_BEFORE(B_UserItem, lst, observedItem);
59    }
60
61    int bobs_hasCurrent() {
62        return BFX_IS_VALID_POS(B_UserItem, lst, observedItem);
63    }
64
65    int bobs_getCurrent() {
66        B_UserItem * curUI = BFX_DEREF_POS(B_UserItem, lst, observedItem);
67        return curUI->userdata[17];
68    }
69
70    enum bobs_op_movement_t bobs_op_movement = OP_MOVEMENT;
71    enum bobs_op_polarity_t bobs_op_polarity = OP_POLARITY;
72)
73
74int main(int argc, const char *argv[]) {
75
76
77    const char * usage_args = "[ExperimentDurSec [CheckClockFreq [NumNodes [ExperimentDurOpCount]]]]";
78    const int static_arg_posns = 4;
79
80    unsigned int ExperimentDurSec     = DefaultExperimentDurSec;
81    unsigned int CheckClockFreq       = DefaultCheckClockFreq;
82    unsigned int NumNodes             = DefaultNumNodes;
83    size_t       ExperimentDurOpCount = DefaultExperimentDurOpCount;
84
85    switch ((argc < static_arg_posns) ? argc : static_arg_posns) {
86      case 5: ExperimentDurOpCount = atoi(argv[4]);
87      case 4: NumNodes = atoi(argv[3]);
88      case 3: CheckClockFreq = atoi(argv[2]);
89      case 2: ExperimentDurSec = atoi(argv[1]);
90    }
91
92    if (ExperimentDurSec == 0 || CheckClockFreq == 0 || NumNodes == 0 || ExperimentDurOpCount == 0 ) {
93        printf("usage: %s %s\n", argv[0], usage_args);
94        return -1;
95    }
96
97    ui = (B_UserItem*) malloc( NumNodes * sizeof(B_UserItem) );
98    memset(ui, 0, NumNodes * sizeof(B_UserItem));
99
100    listedItems = (BFX_LISTED_ELEM_T(B_UserItem)*)malloc( NumNodes * sizeof(BFX_LISTED_ELEM_T(B_UserItem)) );
101    memset(listedItems, 0, NumNodes * sizeof(BFX_LISTED_ELEM_T(B_UserItem)));
102
103    for (int i = 0; i < NumNodes; i++) {
104        B_UserItem * curUI = & ui[i];
105        curUI->userdata[17] = i;
106    }
107
108    BFX_INIT(B_UserItem, lst);
109
110    bobs_init(NumNodes);
111
112    // BOP Convention:
113    // Action-number arguments are for the BOP cartridge to interpret.
114    // I.e. the driver assumes no relationship between BOP_INSERT(_,_,xx) and ui[xx].
115    // Logical insert number 0 and remove number n-1 are given with a distinguished hook.
116    // Logical insert numbers [1,n) and remove numbers [0,n-1) are pumped by the basic SUT hooks.
117    // This pattern lets BOP cartridges that measure element-level operations know statically when there is a reference element in the list.
118
119    // Default init/teardown is insert/remove
120    // Cartridges whose SUT insert/remove actions work on empty lists need not provide special-case ones.
121    #ifndef BOP_INIT
122    #define BOP_INIT(lst, ui, iters, i) BOP_INSERT(lst, ui, iters, i)
123    #endif
124    #ifndef BOP_TEARDOWN
125    #define BOP_TEARDOWN(lst, ui, iters, i) BOP_REMOVE(lst, ui, iters, i)
126    #endif
127
128    double elapsed_sec = 0;
129    clock_t start = clock();
130
131    while (elapsed_sec <= (double) ExperimentDurSec && bobs_ops_completed < ExperimentDurOpCount) {
132        for ( int t = 0; t < CheckClockFreq; t += 1 ) {
133            TRACE('a')
134            listedItems[0] =
135                BOP_INIT(lst, ui, listedItems, 0);
136            TRACE('b')
137            for ( int privateCurInsert = 1;
138                  (bobs_prog_inserting = privateCurInsert, privateCurInsert < NumNodes);
139                  privateCurInsert += 1
140                ) {
141                TRACE('-')
142                listedItems[privateCurInsert] =
143                    BOP_INSERT( lst, ui, listedItems, privateCurInsert );
144                TRACE('+')
145            }
146            TRACE('c')
147            for ( int privateCurRemove = 1;
148                  (bobs_prog_removing = privateCurRemove, privateCurRemove < NumNodes);
149                  privateCurRemove += 1
150                ) {
151                TRACE('-')
152                BOP_REMOVE( lst, ui, listedItems, privateCurRemove-1 );
153                TRACE('+')
154            }
155            TRACE('D')
156            BOP_TEARDOWN(lst, ui, listedItems, NumNodes-1);
157            TRACE('d')
158
159            bobs_prog_rollover_flag = 1;
160            TRACE('e')
161            bobs_prog_inserting = 0;
162            bobs_prog_removing = 0;           
163            bobs_ops_completed += NumNodes;
164            TRACE('f')
165            bobs_prog_rollover_flag = 0;
166            TRACE('g')
167        }
168        clock_t end = clock();
169        elapsed_sec = ((double)(end - start)) / ((double)CLOCKS_PER_SEC);
170    }
171
172    double mean_op_dur_ns = elapsed_sec / ((double)bobs_ops_completed) * 1000 * 1000 * 1000;
173    printf("%s,%zd,%f,%f\n", argv[0], bobs_ops_completed, elapsed_sec, mean_op_dur_ns);
174
175    free(ui);
176    free(listedItems);
177}
Note: See TracBrowser for help on using the repository browser.