usermode/library/malloc-hoard-old/benchmarks/cache-thrash/cache-thrash.cpp

00001 
00002 //
00003 // Hoard: A Fast, Scalable, and Memory-Efficient Allocator
00004 //        for Shared-Memory Multiprocessors
00005 // Contact author: Emery Berger, http://www.cs.utexas.edu/users/emery
00006 //
00007 // Copyright (c) 1998-2000, The University of Texas at Austin.
00008 //
00009 // This library is free software; you can redistribute it and/or modify
00010 // it under the terms of the GNU Library General Public License as
00011 // published by the Free Software Foundation, http://www.fsf.org.
00012 //
00013 // This library is distributed in the hope that it will be useful, but
00014 // WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 // Library General Public License for more details.
00017 //
00019 
00020 /*
00021   cache-thrash.cpp
00022   ------------------------------------------------------------------------
00023   cache-thrash is a benchmark that exercises a heap's cache-locality.
00024   ------------------------------------------------------------------------
00025   @(#) $Id: cache-thrash.cpp,v 1.1 2000/04/19 16:23:30 emery Exp $
00026   ------------------------------------------------------------------------
00027   Emery Berger                    | <http://www.cs.utexas.edu/users/emery>
00028   Department of Computer Sciences |             <http://www.cs.utexas.edu>
00029   University of Texas at Austin   |                <http://www.utexas.edu>
00030   ========================================================================
00031 */
00032 
00033 /* Try the following (on a P-processor machine):
00034 
00035    cache-thrash 1 1000 1 1000000
00036    cache-thrash P 1000 1 1000000
00037 
00038    cache-thrash-hoard 1 1000 1 1000000
00039    cache-thrash-hoard P 1000 1 1000000
00040 
00041    The ideal is a P-fold speedup.
00042 */
00043 
00044 
00045 #include "arch-specific.h"
00046 #include "timer.h"
00047 
00048 #include <iostream.h>
00049 #include <stdlib.h>
00050 
00051 // This class just holds arguments to each thread.
00052 class workerArg {
00053 public:
00054   workerArg (int objSize, int repetitions, int iterations)
00055     : _objSize (objSize),
00056       _iterations (iterations),
00057       _repetitions (repetitions)
00058   {}
00059 
00060   int _objSize;
00061   int _iterations;
00062   int _repetitions;
00063 };
00064 
00065 
00066 extern "C" void * worker (void * arg)
00067 {
00068   // Repeatedly do the following:
00069   //   malloc a given-sized object,
00070   //   repeatedly write on it,
00071   //   then free it.
00072   workerArg * w = (workerArg *) arg;
00073   for (int i = 0; i < w->_iterations; i++) {
00074     // Allocate the object.
00075     char * obj = new char[w->_objSize];
00076     // Write into it a bunch of times.
00077     for (int j = 0; j < w->_repetitions; j++) {
00078       for (int k = 0; k < w->_objSize; k++) {
00079         obj[k] = (char) k;
00080         volatile char ch = obj[k];
00081         ch++;
00082       }
00083     }
00084     // Free the object.
00085     delete [] obj;
00086   }
00087   delete w;
00088   return NULL;
00089 }
00090 
00091 
00092 int main (int argc, char * argv[])
00093 {
00094   int nthreads;
00095   int iterations;
00096   int objSize;
00097   int repetitions;
00098 
00099   if (argc > 4) {
00100     nthreads = atoi(argv[1]);
00101     iterations = atoi(argv[2]);
00102     objSize = atoi(argv[3]);
00103     repetitions = atoi(argv[4]);
00104   } else {
00105     cerr << "Usage: " << argv[0] << " nthreads iterations objSize repetitions" << endl;
00106     exit(1);
00107   }
00108 
00109   hoardThreadType * threads = new hoardThreadType[nthreads];
00110   hoardSetConcurrency (hoardGetNumProcessors());
00111 
00112   int i;
00113 
00114   Timer t;
00115   t.start();
00116 
00117   for (i = 0; i < nthreads; i++) {
00118     workerArg * w = new workerArg (objSize, repetitions / nthreads, iterations);
00119     hoardCreateThread (threads[i], worker, (void *) w);
00120   }
00121   for (i = 0; i < nthreads; i++) {
00122     hoardJoinThread (threads[i]);
00123   }
00124   t.stop();
00125 
00126   delete [] threads;
00127 
00128   cout << "Time elapsed = " << (double) t << " seconds." << endl;
00129 }

Generated on Sat Apr 23 11:43:35 2011 for Mnemosyne by  doxygen 1.4.7