source: doc/theses/mike_brooks_MMath/benchmarks/list/driver.c @ 011c29e

Last change on this file since 011c29e was 2b01f8e, checked in by Mike Brooks <mlbrooks@…>, 15 months ago

Adjust LL perf to use a random layout of nodes in memory

  • Property mode set to 100644
File size: 8.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, DefaultCheckDonePeriod = 1000, DefaultExperimentDurOpCount = -1, DefaultSeed = 5 };
20    #define TRACE(tp)
21#else
22    enum { DefaultNumNodes = 10, DefaultExperimentDurSec = 1, DefaultCheckDonePeriod = 2, DefaultExperimentDurOpCount = 20, DefaultSeed = 5 };
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    void * bobs_getCurrentLoc() {
66        return BFX_DEREF_POS(B_UserItem, lst, observedItem);
67    }
68    int bobs_getCurrentVal() {
69        B_UserItem * curUI = BFX_DEREF_POS(B_UserItem, lst, observedItem);
70        return curUI->userdata[17];
71    }
72
73    enum bobs_op_movement_t bobs_op_movement = OP_MOVEMENT;
74    enum bobs_op_polarity_t bobs_op_polarity = OP_POLARITY;
75)
76
77unsigned int uDefaultPreemption() {
78        return 0;
79}
80
81int main(int argc, const char *argv[]) {
82
83    const char * usage_args = "[ExperimentDurSec [CheckDonePeriod [NumNodes [ExperimentDurOpCount [Seed]]]]]";
84    const int static_arg_posns = 5;
85
86    unsigned int ExperimentDurSec     = DefaultExperimentDurSec;
87    unsigned int CheckDonePeriod      = DefaultCheckDonePeriod;
88    unsigned int NumNodes             = DefaultNumNodes;
89    size_t       ExperimentDurOpCount = DefaultExperimentDurOpCount;
90    unsigned int Seed                 = DefaultSeed;
91
92    switch (((argc - 1) < static_arg_posns) ? (argc - 1) : static_arg_posns) {
93      case 5: Seed = atoi(argv[5]);
94      case 4: ExperimentDurOpCount = atol(argv[4]);
95      case 3: NumNodes = atoi(argv[3]);
96      case 2: CheckDonePeriod = atoi(argv[2]);
97      case 1: ExperimentDurSec = atoi(argv[1]);
98    }
99
100    if (ExperimentDurSec == 0 || CheckDonePeriod == 0 || NumNodes == 0 || ExperimentDurOpCount == 0 || Seed == 0 ) {
101        printf("usage: %s %s\n", argv[0], usage_args);
102        return -1;
103    }
104
105  #ifdef DISABLE_CLOCK_RECHECK
106    if (ExperimentDurSec != -1) {
107        printf("Error: experiment compiled as fixed-work only.  ExperimentDurSec (currently %d) must be set to -1.\nUsage: %s %s\n", ExperimentDurSec, argv[0], usage_args);
108        return -1;
109    }
110  #endif
111
112  #ifdef DISABLE_SHUFFLING_INDIRECTION
113    #define INSERTPOS(insertNum) insertNum
114  #else
115    // To ensure random memory order of nodes being inserted, do so according to a shuffled ordering of them.
116    unsigned int * insertOrdShuf = (unsigned int *) malloc( (size_t)NumNodes * sizeof(unsigned int) );
117    if (!insertOrdShuf) {
118        printf("malloc request for %zd bytes for insertOrdShuf refused\n", (size_t)NumNodes * (size_t)sizeof(unsigned int));
119        return 1;
120    }
121    // Fill with the ordinals (iota)
122    for (int i = 0; i < NumNodes; i++) {
123        insertOrdShuf[i] = i;
124    }
125    // Dummy "Seed" of -1 means skip the shuffle: measure overhead of insertOrdShuf indirection
126    if (Seed != -1) {
127        // Shuffle
128        srand(Seed);
129        for (unsigned int i = 0; i < NumNodes; i++) {
130            unsigned int nodesRemaining = NumNodes - i;
131            unsigned int swapWith = i + rand() % nodesRemaining;
132            unsigned int tempValue = insertOrdShuf[swapWith];
133            insertOrdShuf[swapWith] = insertOrdShuf[i];
134            insertOrdShuf[i] = tempValue;
135        }
136    }
137    #define INSERTPOS(insertNum) insertOrdShuf[insertNum]
138  #endif
139
140    ui = (B_UserItem*) malloc( (size_t)NumNodes * (size_t)sizeof(B_UserItem) );
141    if (!ui) {
142        printf("malloc request for %zd bytes for ui refused\n", (size_t)NumNodes * (size_t)sizeof(B_UserItem));
143        return 1;
144    }
145    memset(ui, 0, (size_t)NumNodes * (size_t)sizeof(B_UserItem));
146
147    listedItems = (BFX_LISTED_ELEM_T(B_UserItem)*)malloc( (size_t)NumNodes * (size_t)sizeof(BFX_LISTED_ELEM_T(B_UserItem)) );
148    if (!listedItems) {
149        printf("malloc request for %zd bytes for listedItems refused\n", (size_t)NumNodes * (size_t)sizeof(BFX_LISTED_ELEM_T(B_UserItem)));
150        return 1;
151    }
152    memset(listedItems, 0, (size_t)NumNodes * (size_t)sizeof(BFX_LISTED_ELEM_T(B_UserItem)));
153
154    // Fill with demo data
155    for (unsigned int i = 0; i < NumNodes; i++) {
156        B_UserItem * curUI = & ui[INSERTPOS(i)];
157        curUI->userdata[17] = i;
158    }
159
160    BFX_INIT(B_UserItem, lst);
161
162    bobs_init(NumNodes);
163
164    // BOP_ADDFOO(lst, iters, insNo, item)
165    // BOP_REMFOO(lst, iters, remNo)
166    //   lst    lvalue of the list head being added to / removed from
167    //   iters  array of ADD return values, in logical-insert order; on ADD, is valid at [0..insNo)
168    //   insNo  counter of the ADD calls (logical insert number)
169    //   remNo  counter of the REM calls (logical remove number)
170    //   item   lvalue of the item being added
171    // Logical insert number 0 and remove number n-1 are given with a distinguished hook.
172    // Logical insert numbers [1,n) and remove numbers [0,n-1) are pumped by the basic SUT hooks.
173    // This pattern lets BOP cartridges that measure element-level operations know statically when there is a reference element in the list.
174    // The driver owns the relationship between a logical insert number and the location of the `item` argument within `ui`.  (Scattered for randomness.)
175    // The BOP cartridge owns the relationship between logical remove number and any choice of an item in iters.  (Defines stack vs queue.)
176
177    // Default init/teardown is insert/remove
178    // Cartridges whose SUT insert/remove actions work on empty lists need not provide special-case ones.
179    #ifndef BOP_INIT
180    #define BOP_INIT(lst, iters, insNo, item) BOP_INSERT(lst, iters, insNo, item)
181    #endif
182    #ifndef BOP_TEARDOWN
183    #define BOP_TEARDOWN(lst, iters, remNo) BOP_REMOVE(lst, iters, remNo)
184    #endif
185
186    double elapsed_sec = 0;
187    clock_t start = clock();
188
189    size_t privateOpsCompleted = 0;
190
191    while (elapsed_sec <= (double) ExperimentDurSec && privateOpsCompleted < ExperimentDurOpCount) {
192        for ( int t = 0; t < CheckDonePeriod; t += 1 ) {
193            TRACE('a')
194            listedItems[0] =
195                BOP_INIT(lst, listedItems, 0, ui[INSERTPOS(0)]);
196            TRACE('b')
197            for ( int privateCurInsert = 1;
198                  (bobs_prog_inserting = privateCurInsert, privateCurInsert < NumNodes);
199                  privateCurInsert += 1
200                ) {
201                TRACE('-')
202                listedItems[privateCurInsert] =
203                    BOP_INSERT( lst, listedItems, privateCurInsert, ui[INSERTPOS(privateCurInsert)] );
204                TRACE('+')
205            }
206            TRACE('c')
207            for ( int privateCurRemove = 1;
208                  (bobs_prog_removing = privateCurRemove, privateCurRemove < NumNodes);
209                  privateCurRemove += 1
210                ) {
211                TRACE('-')
212                BOP_REMOVE( lst, listedItems, privateCurRemove-1 );
213                TRACE('+')
214            }
215            TRACE('D')
216            BOP_TEARDOWN(lst, listedItems, NumNodes-1);
217            TRACE('d')
218
219            privateOpsCompleted += NumNodes;
220
221            bobs_prog_rollover_flag = 1;
222            TRACE('e')
223            bobs_prog_inserting = 0;
224            bobs_prog_removing = 0;           
225            bobs_ops_completed = privateOpsCompleted;
226            TRACE('f')
227            bobs_prog_rollover_flag = 0;
228            TRACE('g')
229        }
230      #ifndef DISABLE_CLOCK_RECHECK
231        clock_t end = clock();
232        elapsed_sec = ((double)(end - start)) / ((double)CLOCKS_PER_SEC);
233      #endif
234    }
235    #ifdef DISABLE_CLOCK_RECHECK
236    {
237        clock_t end = clock();
238        elapsed_sec = ((double)(end - start)) / ((double)CLOCKS_PER_SEC);
239    }
240    #endif
241
242    double mean_op_dur_ns = elapsed_sec / ((double)bobs_ops_completed) * 1000 * 1000 * 1000;
243    printf("%s,%zd,%f,%f\n", argv[0], bobs_ops_completed, elapsed_sec, mean_op_dur_ns);
244
245    free(ui);
246    free(listedItems);
247    free(insertOrdShuf);
248}
Note: See TracBrowser for help on using the repository browser.