source: doc/theses/mike_brooks_MMath/benchmarks/list/driver.c @ 90a3a89

ADTast-experimental
Last change on this file since 90a3a89 was 0b66ef9, checked in by Michael Brooks <mlbrooks@…>, 15 months ago

Add linked list performance experiment

  • Property mode set to 100644
File size: 5.7 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 ( bobs_prog_inserting = 1; bobs_prog_inserting < NumNodes; bobs_prog_inserting += 1 ) { 
138                TRACE('-')
139                listedItems[bobs_prog_inserting] =
140                    BOP_INSERT( lst, ui, listedItems, bobs_prog_inserting );
141                TRACE('+')
142            }
143            TRACE('c')
144            for ( bobs_prog_removing = 1; bobs_prog_removing < NumNodes; bobs_prog_removing += 1 ) { 
145                TRACE('-')
146                BOP_REMOVE( lst, ui, listedItems, bobs_prog_removing-1 );
147                TRACE('+')
148            }
149            TRACE('D')
150            BOP_TEARDOWN(lst, ui, listedItems, NumNodes-1);
151            TRACE('d')
152
153            bobs_prog_rollover_flag = 1;
154            TRACE('e')
155            bobs_prog_inserting = 0;
156            bobs_prog_removing = 0;           
157            bobs_ops_completed += NumNodes;
158            TRACE('f')
159            bobs_prog_rollover_flag = 0;
160            TRACE('g')
161        }
162        clock_t end = clock();
163        elapsed_sec = ((double)(end - start)) / ((double)CLOCKS_PER_SEC);
164    }
165
166    double mean_op_dur_ns = elapsed_sec / ((double)bobs_ops_completed) * 1000 * 1000 * 1000;
167    printf("%s,%zd,%f,%f\n", argv[0], bobs_ops_completed, elapsed_sec, mean_op_dur_ns);
168
169    free(ui);
170    free(listedItems);
171}
Note: See TracBrowser for help on using the repository browser.