source: tests/zombies/string-perf/prog.cfa @ 433e2c3

Last change on this file since 433e2c3 was 08ed947, checked in by Michael Brooks <mlbrooks@…>, 3 years ago

Roll up of string changes for performance testing/improvement, and a couple API features supporting them.

String API changes:
Defining a tuning knob to control the heap growth policy (relapaces former 10% hardcode, downgraded to a default)
Implementing findFrom (allowing find-non-first); leaving find as find-first.

String implementation perf improvements:
Calling C-malloc directly instead of via CFA-alloc.
Replacings loops that copy with memmove calls.
Replacings loops that search for a value with memchr calls.

String perf testing realized:
Makefile supporting several prog-*.cfa, chosen by OPERATION value (implies prog.cfa changes to support the adjusted protocol)
Adjusting the starter/accumulater declarations in PEQ and PTA to behave consistently in cfa v cpp.
Adding tests: allocation, find, normalize, pass-by-val, pass-by-x.
Adding helper shell scripts for: generating flame graphs, collecting/crunching allocation stats using Mubeen's malloc wrappers

  • Property mode set to 100644
File size: 4.2 KB
Line 
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
45#if defined IMPL_CFA_HL
46  #include <string.hfa>
47#elif defined IMPL_CFA_LL
48  #include <string_res.hfa>
49#endif
50
51#if defined CFA_NOSHARE
52  #include <string_sharectx.hfa>
53  #define STRING_SHARING_CONTROL \
54    string_sharectx c = { NO_SHARING };
55#else
56  #define STRING_SHARING_CONTROL
57#endif
58
59#if defined IMPL_CFA
60  #include <math.hfa>
61  extern "C" {
62    void malloc_stats( void );
63  }
64#elif defined IMPL_CXX
65  #include <algorithm>
66  using std::min;
67  #include <malloc.h>
68#endif
69
70#include <time.h>
71#include <stdlib.h> // atoi
72#include <string.h> // strlen, only during setup
73
74#if defined IMPL_STL || defined IMPL_BUHR94
75    #define PRINT(s) std::cout << s << std::endl
76#elif defined IMPL_CFA_HL || defined IMPL_CFA_LL
77    #define PRINT(s) sout | s;
78#else
79    #error Unhandled print case
80#endif
81
82double meanLen(int N, char ** strings) {
83    int totalLen = 0;
84    for (int i = 0 ; i < N; i ++) {
85        totalLen += strlen(strings[i]);
86    }
87    return (double)totalLen / (double)N;
88}
89
90volatile int checkthis = 0;
91#define MAYBE( op ) if (checkthis) { op; }
92
93int main( int argc, char ** argv ) {
94
95    STRING_SHARING_CONTROL
96
97
98    const char * usage_args = "ConcatsPerReset ExecTimeSecs Corpus...";
99    const int static_arg_posns = 3;
100
101    int concatsPerReset = -1, execTimeSecs = -1;
102
103    switch (min(argc, static_arg_posns)) {
104      case 3: execTimeSecs = atoi(argv[2]);
105      case 2: concatsPerReset = atoi(argv[1]);
106    }
107
108    int corpuslen = argc - static_arg_posns;
109    char ** corpus = argv + static_arg_posns;
110
111    if (execTimeSecs < 1 || concatsPerReset < 1 || corpuslen < 1) {
112      printf("usage: %s %s\n", argv[0], usage_args);
113      printf("output:\nconcatsPerReset,corpusItemCount,corpusMeanLenChars,concatDoneActualCount,execTimeActualSec\n");
114      exit(1);
115    }
116
117    double meanCorpusLen = meanLen(corpuslen, corpus);
118
119    clock_t start, end_target, end_actual;
120
121    #if defined IMPL_CFA_LL && defined OP_PTA
122        string_res pta_ll_temp;
123    #endif
124
125    #if defined IMPL_CFA_LL
126      #define DECLS \
127        const char* initval = "starter"; \
128        string_res accum = initval;
129    #else
130      #define DECLS \
131        const char* initval = "starter"; \
132        string accum = initval;
133    #endif
134
135    #if defined ALLOC_REUSE
136      DECLS
137      #define RESET \
138        accum = initval;
139    #elif defined ALLOC_FRESH
140      #define RESET \
141        DECLS
142    #else
143      #error bad alloc
144    #endif
145
146    start = clock();
147    end_target = start + CLOCKS_PER_SEC * execTimeSecs;
148    volatile unsigned int t = 0;
149    for ( ; t % 100 != 0 || clock() < end_target ; t += 1 ) {
150            RESET
151            for ( volatile unsigned int i = 0; i < concatsPerReset; i += 1 ) {
152              MAYBE( PRINT(accum) )
153              char *toAppend = corpus[i % corpuslen]; // ? corpus[rand() % corpuslen]
154              #if defined OP_PTA && defined IMPL_CFA_LL
155                 pta_ll_temp = accum;
156                 pta_ll_temp += toAppend;
157                 accum = pta_ll_temp;
158              #elif defined OP_PTA
159                 accum = accum + toAppend;
160              #elif defined OP_PEQ
161                 accum += toAppend;
162              #else
163                 #error Bad OP_
164              #endif
165            }
166    }
167    end_actual = clock();
168    unsigned int concatsDone = t * concatsPerReset;
169    double elapsed = ((double) (end_actual - start)) / CLOCKS_PER_SEC;
170    printf("%d,%d,%f,%d,%f\n", concatsPerReset, corpuslen, meanCorpusLen, concatsDone, elapsed);
171
172    // malloc_stats();
173
174    return 0;
175}
Note: See TracBrowser for help on using the repository browser.