1 | #if defined IMPL_STL_NA_NA
|
---|
2 | #define IMPL_STL
|
---|
3 | #endif
|
---|
4 |
|
---|
5 | #if defined IMPL_BUHR94_NA_NA
|
---|
6 | #define IMPL_BUHR94
|
---|
7 | #endif
|
---|
8 |
|
---|
9 | #if defined IMPL_STL
|
---|
10 | #include <string>
|
---|
11 | #include <iostream>
|
---|
12 | #include <cstdio>
|
---|
13 | using namespace std;
|
---|
14 | #define IMPL_CXX
|
---|
15 |
|
---|
16 | #elif defined IMPL_CFA_HL_SHARE
|
---|
17 | #define IMPL_CFA_HL
|
---|
18 | #define IMPL_CFA
|
---|
19 |
|
---|
20 | #elif defined IMPL_CFA_LL_SHARE
|
---|
21 | #define IMPL_CFA_LL
|
---|
22 | #define IMPL_CFA
|
---|
23 |
|
---|
24 | #elif defined IMPL_CFA_HL_NOSHARE
|
---|
25 | #define IMPL_CFA_HL
|
---|
26 | #define CFA_NOSHARE
|
---|
27 | #define IMPL_CFA
|
---|
28 |
|
---|
29 | #elif defined IMPL_CFA_LL_NOSHARE
|
---|
30 | #define IMPL_CFA_LL
|
---|
31 | #define CFA_NOSHARE
|
---|
32 | #define IMPL_CFA
|
---|
33 |
|
---|
34 | #elif defined IMPL_BUHR94
|
---|
35 | #include <iostream>
|
---|
36 | #include <cstdio>
|
---|
37 | #include "/u0/mlbrooks/usys1/sm/string/StringSharing/src/string.h"
|
---|
38 | #define IMPL_CXX
|
---|
39 |
|
---|
40 | #else
|
---|
41 | #error Bad IMPL_
|
---|
42 | #endif
|
---|
43 |
|
---|
44 | #if defined IMPL_CFA_HL
|
---|
45 | #include <string.hfa>
|
---|
46 | #include <fstream.hfa>
|
---|
47 | extern void TUNING_set_string_heap_liveness_threshold(double); // in string_res.hfa
|
---|
48 | #elif defined IMPL_CFA_LL
|
---|
49 | #include <string_res.hfa>
|
---|
50 | #include <fstream.hfa>
|
---|
51 | #endif
|
---|
52 |
|
---|
53 | #if defined CFA_NOSHARE
|
---|
54 | #include <string_sharectx.hfa>
|
---|
55 | #define STRING_SHARING_CONTROL \
|
---|
56 | string_sharectx c = { NO_SHARING };
|
---|
57 | #else
|
---|
58 | #define STRING_SHARING_CONTROL
|
---|
59 | #endif
|
---|
60 |
|
---|
61 | #if defined IMPL_CFA
|
---|
62 | #include <math.hfa>
|
---|
63 | extern "C" {
|
---|
64 | void malloc_stats( void );
|
---|
65 | }
|
---|
66 | #elif defined IMPL_CXX
|
---|
67 | #include <algorithm>
|
---|
68 | using std::min;
|
---|
69 | #include <malloc.h>
|
---|
70 | #endif
|
---|
71 |
|
---|
72 | #include <time.h>
|
---|
73 | #include <stdlib.h> // atoi
|
---|
74 | #include <string.h> // strlen, only during setup
|
---|
75 |
|
---|
76 | #if defined IMPL_STL || defined IMPL_BUHR94
|
---|
77 | #define PRINT(s) std::cout << s << std::endl
|
---|
78 | #elif defined IMPL_CFA_HL || defined IMPL_CFA_LL
|
---|
79 | #define PRINT(s) sout | s;
|
---|
80 | #else
|
---|
81 | #error Unhandled print case
|
---|
82 | #endif
|
---|
83 |
|
---|
84 | #if defined IMPL_CFA_LL
|
---|
85 | #define STRING_T string_res
|
---|
86 | #define ASSIGN_CHAR(str, idx, val) assignAt(str, idx, val)
|
---|
87 | #else
|
---|
88 | #define STRING_T string
|
---|
89 | #define ASSIGN_CHAR(str, idx, val) str[idx] = val
|
---|
90 | #endif
|
---|
91 |
|
---|
92 | double meanLen(int N, char ** strings) {
|
---|
93 | int totalLen = 0;
|
---|
94 | for (int i = 0 ; i < N; i ++) {
|
---|
95 | totalLen += strlen(strings[i]);
|
---|
96 | }
|
---|
97 | return (double)totalLen / (double)N;
|
---|
98 | }
|
---|
99 |
|
---|
100 | volatile int checkthis = 0;
|
---|
101 | #define MAYBE( op ) if (checkthis) { op; }
|
---|
102 |
|
---|
103 | int corpuslen = 0;
|
---|
104 | char ** corpus = (char**) 0;
|
---|
105 | size_t corpus_next_pos = 0;
|
---|
106 |
|
---|
107 | double repsPerLevel;
|
---|
108 | double repBalance = 0.0000001;
|
---|
109 |
|
---|
110 | clock_t start, endTarget, end_actual;
|
---|
111 | size_t allocationCountTarget = 0;
|
---|
112 |
|
---|
113 | size_t allocationCountActual = 0;
|
---|
114 | //size_t allocationBytesActual = 0;
|
---|
115 |
|
---|
116 | void helper( int depth ) {
|
---|
117 |
|
---|
118 | if (depth == 0) return;
|
---|
119 |
|
---|
120 | corpus_next_pos += 1;
|
---|
121 | corpus_next_pos %= corpuslen;
|
---|
122 |
|
---|
123 | STRING_T q = corpus[corpus_next_pos];
|
---|
124 | // ASSIGN_CHAR(q, 0, '@'); // Turns out my implementation is slow at this step. A separate test could work it. It's inessential to the allocation test, given the assumption that both string reps allocate eagerly in their heaps from a constant. In the STL, that assumption is upheld by my observation that commenting out this line didn't speed it up. In CFA-share, I know it to be true of the implementation.
|
---|
125 |
|
---|
126 | allocationCountActual += 1;
|
---|
127 | // allocationBytesActual += q`len;
|
---|
128 |
|
---|
129 | // if (depth > 0) {
|
---|
130 |
|
---|
131 | repBalance += repsPerLevel;
|
---|
132 | int curRepLimit = repBalance;
|
---|
133 | repBalance -= curRepLimit;
|
---|
134 |
|
---|
135 | for ( int i = 0 ; i < curRepLimit; i++ ) {
|
---|
136 | if ((allocationCountActual+1) % allocationCountTarget == 0 && clock() > endTarget) return;
|
---|
137 | helper(depth-1);
|
---|
138 | }
|
---|
139 | // }
|
---|
140 |
|
---|
141 | MAYBE(PRINT(q));
|
---|
142 | }
|
---|
143 |
|
---|
144 | int main( int argc, char ** argv ) {
|
---|
145 |
|
---|
146 | STRING_SHARING_CONTROL
|
---|
147 |
|
---|
148 |
|
---|
149 | const char * usage_args[] = {"Depth RepsPerLevel ExpansionThreshold ExecTimeSecs Corpus...",
|
---|
150 | "Depth RepsPerLevel ExpansionThreshold -w WorkAllocCount Corpus..."};
|
---|
151 | const int static_arg_posns = 5;
|
---|
152 | int used_arg_posns = static_arg_posns;
|
---|
153 |
|
---|
154 | int launchDepth = -1;
|
---|
155 | double expansionThreshold = -1.0;
|
---|
156 | int execTimeSecs = -1;
|
---|
157 |
|
---|
158 | switch (min(argc, static_arg_posns)) {
|
---|
159 | case 5: if ( strcmp(argv[4], "-w") == 0 ) {
|
---|
160 | used_arg_posns ++;
|
---|
161 | allocationCountTarget = atoi(argv[5]);
|
---|
162 | } else {
|
---|
163 | execTimeSecs = atoi(argv[4]);
|
---|
164 | }
|
---|
165 | case 4: expansionThreshold = atof(argv[3]);
|
---|
166 | case 3: repsPerLevel = atof(argv[2]);
|
---|
167 | case 2: launchDepth = atoi(argv[1]);
|
---|
168 | }
|
---|
169 |
|
---|
170 | corpuslen = argc - used_arg_posns;
|
---|
171 | corpus = argv + used_arg_posns;
|
---|
172 |
|
---|
173 | if (launchDepth < 1 || repsPerLevel < 1.0 || (execTimeSecs < 1 && allocationCountTarget < 1) || corpuslen < 1 ||
|
---|
174 | (expansionThreshold != -1.0 && (expansionThreshold <= 0.0 || expansionThreshold >= 1.0))) {
|
---|
175 | for (int u = 0; u < sizeof(usage_args) / sizeof(*usage_args); u++) {
|
---|
176 | printf("usage: %s %s\n", argv[0], usage_args[u]);
|
---|
177 | }
|
---|
178 | printf("output:\nxxx,corpusItemCount,corpusMeanLenChars,allocationCountActual,execTimeActualSec,topIters\n");
|
---|
179 | exit(1);
|
---|
180 | }
|
---|
181 |
|
---|
182 | if (expansionThreshold != -1.0 ) {
|
---|
183 | #if defined IMPL_CFA
|
---|
184 | TUNING_set_string_heap_liveness_threshold(expansionThreshold);
|
---|
185 | #else
|
---|
186 | printf("cannot set expansion threshold on non-CFA implementation");
|
---|
187 | exit(1);
|
---|
188 | #endif
|
---|
189 | }
|
---|
190 |
|
---|
191 | double meanCorpusLen = meanLen(corpuslen, corpus);
|
---|
192 |
|
---|
193 | // time driven experiment: re-check time every 10000 allocations
|
---|
194 | if (execTimeSecs > 0) allocationCountTarget = 10000;
|
---|
195 |
|
---|
196 | start = clock();
|
---|
197 | endTarget = start + CLOCKS_PER_SEC * max(0, execTimeSecs);
|
---|
198 |
|
---|
199 | size_t top_iters = 0;
|
---|
200 |
|
---|
201 | for(;;) {
|
---|
202 | #if defined OP_PALL
|
---|
203 | helper( launchDepth );
|
---|
204 | #else
|
---|
205 | #error Bad OP_
|
---|
206 | #endif
|
---|
207 |
|
---|
208 | top_iters++;
|
---|
209 |
|
---|
210 | if ((allocationCountActual+1) % allocationCountTarget == 0 && clock() > endTarget) break;
|
---|
211 | }
|
---|
212 | end_actual = clock();
|
---|
213 | double elapsed = ((double) (end_actual - start)) / CLOCKS_PER_SEC;
|
---|
214 | printf("xxx,%d,%f,%ld,%f,%ld\n", corpuslen, meanCorpusLen, allocationCountActual, elapsed, top_iters);
|
---|
215 |
|
---|
216 | // malloc_stats();
|
---|
217 |
|
---|
218 | return 0;
|
---|
219 | }
|
---|