Debian Codegraph @

benchmarks.larceny/src/perm9.c

raw
1 /******************************************************************************
2 *
3 * Usage: permsort n
4 *
5 * This benchmark is in four parts. Each tests a different aspect of
6 * the memory system.
7 *
8 * perm8 storage allocation
9 * 10perm8 storage allocation and deallocation
10 * sumperms sequential traversal
11 * mergesort side effects
12 *
13 * The perm8 benchmark generates a list of all 40320 permutations of
14 * the first 8 integers, allocating 149912 pairs (typically 1199296 bytes),
15 * all of which goes into the generated list. (That is, the perm8
16 * benchmark generates absolutely no garbage.) The generated permutations
17 * are in order of a grey code that bears no obvious relationship to a
18 * lexicographic order.
19 *
20 * The sumperms benchmark computes the sum of the permuted integers
21 * over all permutations.
22 *
23 * The 10perm8 benchmark repeats the perm8 benchmark 10 times, so it
24 * allocates and reclaims 1499120 pairs (typically 11,992,960 bytes).
25 *
26 * The mergesort! benchmark destructively sorts the generated permutations
27 * into lexicographic order, allocating no storage whatsoever.
28 ******************************************************************************/
29
30 /* Implementation
31 * Translated from Standard ML and from Scheme. This translation turns
32 * tail recursion into loops wherever possible.
33 *
34 * A number of switches (#defined below) controls the program:
35 * - 'car' and 'cdr' are always open-coded, and 'cons' is open-coded by
36 * turning on the switch INLINE_CONS.
37 * - Normally malloc() and free() are used to manage memory, but turning
38 * on FAST_ALLOC makes the program utilize a custom allocator and
39 * deallocator.
40 * - To print the results of the permutation and of the sort, turn on
41 * the switch 'PRINT'.
42 * - To gather allocation/deallocation statistics, turn on STATS.
43 *
44 * The custom allocator allocates linearly from a segment of free
45 * storage until that segment is exhausted.
46 * Then it allocates from a free list of previously allocated storage
47 * that has been reclaimed, until that free list becomes exhausted.
48 * If both the segment of free storage and the free list are exhausted,
49 * then the allocator creates a new segment of free storage.
50 */
51
52 #define MACINTOSH 0
53 #define FREE_IS_BROKEN 1 /* Sun's CC thinks free takes a (char *). */
54
55 #include <stdlib.h>
56 #include <stdio.h>
57
58 #if MACINTOSH
59 #include <Time.h>
60 #else
61 #include <sys/time.h>
62 #include <sys/resource.h>
63 #endif
64
65 /* Customization macros */
66
67 #define INLINE_CONS 1 /* Set to 1 to inline CONS calls */
68 #ifdef FAST_ALLOCATOR
69 #define FAST_ALLOC 1 /* Set to 1 for custom allocation */
70 #else
71 #define FAST_ALLOC 0
72 #endif
73 #define DONT_FREE 0 /* Set to 1 to avoid all deallocation */
74 #define STATS 0 /* Set to 1 for allocation statistics */
75 #define PRINT 0 /* Set to 1 to print perms and sort */
76
77 #if MACINTOSH
78 #define SEGSIZE 1000 /* MPW 3.2 can't malloc > 64K at once ?!? */
79 #else
80 #define SEGSIZE 10000 /* Used only for custom allocation. */
81 #endif
82
83 /* The macro version does not allow nested CONS calls. */
84
85 #if FAST_ALLOC
86
87 #if INLINE_CONS
88
89 #define int_CONS( x, y, dest ) \
90 do { \
91 ONEMORECONS(); \
92 if (int_segment_free-- > 0) { \
93 int_segment->num = (x); \
94 int_segment->cdr = (y); \
95 (dest) = int_segment++; \
96 } \
97 else if (free_int_list != 0) { \
98 int_list int_list_result = free_int_list; \
99 free_int_list = free_int_list->cdr; \
100 int_list_result->num = (x); \
101 int_list_result->cdr = (y); \
102 (dest) = int_list_result; \
103 } \
104 else { \
105 (dest) = int_cons( (x), (y) ); \
106 }} while(0)
107
108 #define perm_CONS( x, y, dest ) \
109 do { \
110 ONEMORECONS(); \
111 if (perm_segment_free-- > 0) { \
112 perm_segment->car = (x); \
113 perm_segment->cdr = (y); \
114 (dest) = perm_segment++; \
115 } \
116 else if (free_perm_list != 0) { \
117 perm_list perm_list_result = free_perm_list; \
118 free_perm_list = free_perm_list->cdr; \
119 perm_list_result->car = (x); \
120 perm_list_result->cdr = (y); \
121 (dest) = perm_list_result; \
122 } \
123 else { \
124 (dest) = perm_cons( (x), (y) ); \
125 }} while(0)
126
127 #else /* not INLINE_CONS */
128
129 #define int_CONS( A, B, D ) D = int_cons( A, B )
130 #define perm_CONS( A, B, D ) D = perm_cons( A, B )
131
132 #endif /* INLINE_CONS */
133
134 #define int_RECLAIM(x) \
135 x->cdr = free_int_list; \
136 free_int_list = x
137 #define perm_RECLAIM(x) \
138 x->cdr = free_perm_list; \
139 free_perm_list = x
140
141 #else /* not FAST_ALLOC */
142
143 #if INLINE_CONS
144
145 #define int_CONS( x, y, dest ) \
146 do { \
147 int_list TMP = (int_list) malloc( sizeof( struct int_cell ) ); \
148 TMP->num = (x); \
149 TMP->cdr = (y); \
150 dest = TMP; \
151 ONEMORECONS(); \
152 } while(0)
153 #define perm_CONS( x, y, dest ) \
154 do { \
155 perm_list TMP = (perm_list) malloc( sizeof( struct perm_cell ) ); \
156 TMP->car = (x); \
157 TMP->cdr = (y); \
158 dest = TMP; \
159 ONEMORECONS(); \
160 } while(0)
161
162 #else /* not INLINE_CONS */
163
164 #define int_CONS( A, B, D ) D = int_cons( A, B )
165 #define perm_CONS( A, B, D ) D = perm_cons( A, B )
166
167 #endif /* INLINE_CONS */
168
169 #if FREE_IS_BROKEN
170 #define int_RECLAIM(x) free((char *) x);
171 #define perm_RECLAIM(x) free((char *) x);
172 #else
173 #define int_RECLAIM(x) free(x);
174 #define perm_RECLAIM(x) free(x);
175 #endif
176
177 #endif /* FAST_ALLOC */
178
179 #if STATS
180 #define ONEMORECONS() conscount++
181 #define ONEMORESEG() segcount++
182 #define ONEMOREFREE() freecount++
183 int conscount = 0;
184 int freecount = 0;
185 int segcount = 0;
186 #else
187 #define ONEMORECONS()
188 #define ONEMOREFREE()
189 #define ONEMORESEG()
190 #endif
191
192 struct int_cell {
193 struct int_cell *cdr;
194 int num;
195 };
196
197 typedef struct int_cell *int_list;
198
199 struct perm_cell {
200 struct perm_cell *cdr;
201 int_list car;
202 };
203
204 typedef struct perm_cell *perm_list;
205
206 perm_list permutations( int_list );
207
208 long sumperms( perm_list );
209 long factorial ( int );
210
211 int_list int_cons( int, int_list );
212 perm_list perm_cons( int_list, perm_list );
213 void reclaim_storage( perm_list );
214
215 void message( char * );
216 void printints( int_list );
217 void printperms( perm_list );
218 int int_list_length( int_list );
219 int perm_list_length( perm_list );
220
221 /* These variables are used by the custom memory allocator.
222 They are declared here because the inline CONS macros need them. */
223
224 #if FAST_ALLOC
225 static int_list free_int_list = 0;
226 static perm_list free_perm_list = 0;
227 struct int_cell *int_segment = 0;
228 struct perm_cell *perm_segment = 0;
229 int int_segment_free = 0;
230 int perm_segment_free = 0;
231 #endif
232
233 int main( int argc, char **argv )
234 {
235 int n, nn;
236 int_list one_to_n;
237 perm_list m, m2;
238 long sum;
239 int k;
240
241 #if MACINTOSH
242 n = 9;
243 #else
244 if (argc != 2 || sscanf( argv[1], "%d%c", &n ) != 1) {
245 n = 9;
246 }
247 #endif
248 printf( "n = %d\n\n", n );
249
250 /* Create the list [1, 2, ..., n]. */
251 one_to_n = 0;
252 nn = n;
253 while (nn > 0) {
254 int_CONS( nn--, one_to_n, one_to_n );
255 }
256
257 m = permutations( one_to_n );
258 #if PRINT
259 printperms( m );
260 #endif
261
262 for ( k = 5; k > 0; --k ) {
263 /*
264 * For C only, we are recreating the list [1, 2, ..., n] each time.
265 * This means C will allocate more storage than the garbage-collected
266 * languages, but it avoids sharing of structure between the results
267 * of each iteration. Shared structure would hurt C a lot more than
268 * a little extra allocation.
269 */
270 one_to_n = 0;
271 nn = n;
272 while (nn > 0) {
273 int_CONS( nn--, one_to_n, one_to_n );
274 }
275 m2 = permutations( one_to_n );
276 reclaim_storage( m );
277 m = m2;
278 }
279
280 sum = sumperms( m );
281 if (sum != (n * (n + 1) * factorial (n)) / 2)
282 printf ("*** wrong result ***\n");
283 reclaim_storage( m );
284
285 #if STATS
286 printf( "Pairs allocated: %d\n", conscount );
287 printf( "Pairs deallocated: %d\n", freecount );
288 printf( "Segments allocated: %d\n\n", segcount );
289 #endif
290
291 return 0;
292 }
293
294 /******************************************************************************
295 *
296 * permutations
297 *
298 ******************************************************************************/
299
300 perm_list perms; /* local to these functions */
301 int_list x; /* local to these functions */
302
303 int_list revloop( int_list x, int n, int_list y )
304 {
305 while (n-- != 0) {
306 int_CONS( x->num, y, y );
307 x = x->cdr;
308 }
309 return y;
310 }
311
312 int_list tail( int_list l, int n )
313 {
314 while (n-- > 0) {
315 l = l->cdr;
316 }
317 return l;
318 }
319
320 void F( int n )
321 {
322 x = revloop( x, n, tail( x, n ) );
323 perm_CONS( x, perms, perms );
324 }
325
326 void P( int n )
327 {
328 int j;
329
330 if (n > 1) {
331 for ( j = n-1; j != 0; --j ) {
332 P( n-1 );
333 F( n );
334 }
335 P( n-1 );
336 }
337 }
338
339 perm_list permutations( int_list the_x )
340 {
341 x = the_x;
342 perm_CONS( the_x, 0, perms );
343 P( int_list_length( the_x ) );
344 return perms;
345 }
346
347 /******************************************************************************
348 *
349 * sumperms
350 *
351 ******************************************************************************/
352
353 long sumperms( perm_list x ) {
354 int_list y;
355 long sum = 0;
356
357 for (; x != 0; x = x->cdr)
358 for (y = x->car; y != 0; y = y->cdr)
359 sum = sum + y->num;
360
361 return sum;
362 }
363
364 /******************************************************************************
365 *
366 * factorial
367 *
368 ******************************************************************************/
369
370 long factorial( int n ) {
371 int f = 1;
372 while (n > 0) {
373 f = n * f;
374 n = n - 1;
375 }
376 return f;
377 }
378
379 /******************************************************************************
380 *
381 * Storage allocation and deallocation.
382 *
383 ******************************************************************************/
384
385 int_list int_cons( int n, int_list y )
386 {
387 int_list int_list_result;
388 ONEMORECONS();
389
390 #if FAST_ALLOC
391 if (int_segment_free-- > 0) {
392 int_segment->num = n;
393 int_segment->cdr = y;
394 return int_segment++;
395 }
396 else if (free_int_list != 0) {
397 int_list_result = free_int_list;
398 free_int_list = free_int_list->cdr;
399 int_list_result->num = n;
400 int_list_result->cdr = y;
401 return int_list_result;
402 }
403 else {
404 ONEMORESEG();
405 /* printf("Allocating a segment for int lists\n"); */
406 int_segment = (struct int_cell *)
407 malloc( sizeof( struct int_cell ) * SEGSIZE );
408 int_segment_free = SEGSIZE - 1;
409 int_segment->num = n;
410 int_segment->cdr = y;
411 return int_segment++;
412 }
413
414 #else
415
416 int_list_result = (int_list) malloc( sizeof( struct int_cell ) );
417 int_list_result->num = n;
418 int_list_result->cdr = y;
419 return int_list_result;
420 #endif
421 }
422
423 perm_list perm_cons( int_list x, perm_list y )
424 {
425 perm_list perm_list_result;
426 ONEMORECONS();
427
428 #if FAST_ALLOC
429 if (perm_segment_free-- > 0) {
430 perm_segment->car = x;
431 perm_segment->cdr = y;
432 return perm_segment++;
433 }
434 else if (free_perm_list != 0) {
435 perm_list_result = free_perm_list;
436 free_perm_list = free_perm_list->cdr;
437 perm_list_result->car = x;
438 perm_list_result->cdr = y;
439 return perm_list_result;
440 }
441 else {
442 ONEMORESEG();
443 /* printf("Allocating a segment for perm lists\n"); */
444 perm_segment = (struct perm_cell *)
445 malloc( sizeof( struct perm_cell ) * SEGSIZE );
446 perm_segment_free = SEGSIZE - 1;
447 perm_segment->car = x;
448 perm_segment->cdr = y;
449 return perm_segment++;
450 }
451
452 #else
453
454 perm_list_result = (perm_list) malloc( sizeof( struct perm_cell ) );
455 perm_list_result->car = x;
456 perm_list_result->cdr = y;
457 return perm_list_result;
458 #endif
459 }
460
461 /* Storage is reclaimed by a two-pass process of computing reference
462 * counts, then sweeping. These two passes rely upon the following:
463 *
464 * * The given perm_list, including all storage accessible from it,
465 * is garbage. That is, it does not share storage with any live
466 * structure and is not referred to by any live variables.
467 * * The given perm_list contains no cycles. The permutations may
468 * share structure with each other, but the structure that links
469 * them together is unshared.
470 * * Each permutation in the given perm_list is a permutation of
471 * positive integers.
472 */
473
474 void reclaim_storage( perm_list perms ) {
475
476 perm_list p, q;
477 int_list x, y;
478
479 #if DONT_FREE
480
481 return;
482
483 #else
484 #if FAST_ALLOC
485 for ( p = perms; (p != 0) && (p->car != 0); ) {
486 for ( x = p->car; (x != 0) && (x->num != 0); ) {
487 y = x;
488 x = x->cdr;
489 y->num = 0;
490 int_RECLAIM(y);
491 ONEMOREFREE();
492 }
493 q = p;
494 p = p->cdr;
495 q->car = 0;
496 perm_RECLAIM(q);
497 ONEMOREFREE();
498 }
499 #else
500 /* Compute reference counts.
501 * During this pass, a positive integer in x->num means the node pointed to
502 * by x has not yet been marked. A zero in x->num means the node pointed to
503 * by x has been marked to account for one reference.
504 * A -1 means two references, -2 means three references, and so on.
505 */
506
507 for ( p = perms; p != 0; p = p->cdr ) {
508 for ( x = p->car; x != 0; x = x->cdr ) {
509 if ( x->num > 0 )
510 x->num = 0;
511 else --(x->num);
512 }
513 }
514
515 /* Sweep, freeing storage. */
516
517 for ( p = perms; p != 0; ) {
518 for ( x = p->car; x != 0; ) {
519 if ( x->num == 0 ) {
520 y = x;
521 x = x->cdr;
522 int_RECLAIM(y);
523 ONEMOREFREE();
524 }
525 else {
526 (x->num)++;
527 x = x->cdr;
528 }
529 }
530 q = p;
531 p = p->cdr;
532 perm_RECLAIM(q);
533 ONEMOREFREE();
534 }
535 #endif
536 #endif
537 }
538
539
540 /******************************************************************************
541 *
542 * Miscellaneous.
543 *
544 ******************************************************************************/
545
546 /* Prints a string for debugging. */
547
548 void message( char *msg ) {
549 printf( msg );
550 printf( "\n" );
551 }
552
553 /* Prints a list for debugging. */
554
555 void printints ( int_list l ) {
556 putchar( '(' );
557 while (l != 0) {
558 printf( "%d", l->num );
559 l = l->cdr;
560 if (l != 0) putchar( ' ' );
561 }
562 printf( ")\n" );
563 }
564
565 void printperms( perm_list perms ) {
566 while (perms != 0) {
567 printints( perms->car );
568 perms = perms->cdr;
569 }
570 printf( "\n" );
571 }
572
573 #define LENGTH_BODY \
574 { \
575 int n = 0; \
576 while (l != 0) { \
577 l = l->cdr; \
578 n++; \
579 } \
580 return n; \
581 }
582
583 int int_list_length( int_list l )
584 LENGTH_BODY
585
586 int perm_list_length( perm_list l )
587 LENGTH_BODY
588
589
590 #if MACINTOSH
591 /* Returns time in ms since an arbitrary origin. */
592 long timenow() {
593 unsigned long t = __tickcount();
594 return ((t & 0x000fffff) * 1000) / CLOCKS_PER_SEC;
595 }
596 #else
597
598 #if 0
599 /* Returns elapsed time in ms since start of process */
600 long timenow()
601 {
602 struct timeval tv;
603 struct timezone tz;
604
605 gettimeofday( &tv, &tz );
606 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
607 }
608 #endif
609
610 #if 0
611 /* Returns user + sys time in ms since start of process */
612
613 long timenow()
614 {
615 struct rusage r;
616
617 getrusage( RUSAGE_SELF, &r );
618 return (r.ru_utime.tv_sec * 1000 + r.ru_utime.tv_usec / 1000)
619 + (r.ru_stime.tv_sec * 1000 + r.ru_stime.tv_usec / 1000);
620 }
621 #endif
622
623 /* Returns user time in ms since start of process */
624
625 long timenow()
626 {
627 struct rusage r;
628
629 getrusage( RUSAGE_SELF, &r );
630 return (r.ru_utime.tv_sec * 1000 + r.ru_utime.tv_usec / 1000);
631 }
632
633 #endif /* MACINTOSH */
634
635 /* eof */