"Fossies" - the Fresh Open Source Software Archive

Member "heaplayers-351/benchmarks/cache-scratch/cache-scratch.cpp" (7 Oct 2003, 3671 Bytes) of package /linux/misc/old/heaplayers_3_5_1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "cache-scratch.cpp" see the Fossies "Dox" file reference documentation.

    1 ///-*-C++-*-//////////////////////////////////////////////////////////////////
    2 //
    3 // Hoard: A Fast, Scalable, and Memory-Efficient Allocator
    4 //        for Shared-Memory Multiprocessors
    5 // Contact author: Emery Berger, http://www.cs.umass.edu/~emery
    6 //
    7 // Copyright (c) 1998-2003, The University of Texas at Austin.
    8 //
    9 // This library is free software; you can redistribute it and/or modify
   10 // it under the terms of the GNU Library General Public License as
   11 // published by the Free Software Foundation, http://www.fsf.org.
   12 //
   13 // This library is distributed in the hope that it will be useful, but
   14 // WITHOUT ANY WARRANTY; without even the implied warranty of
   15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   16 // Library General Public License for more details.
   17 //
   18 //////////////////////////////////////////////////////////////////////////////
   19 
   20 /**
   21  * @file cache-scratch.cpp
   22  *
   23  * cache-scratch is a benchmark that exercises a heap's cache-locality.
   24  * An allocator that allows multiple threads to re-use the same small
   25  * object (possibly all in one cache-line) will scale poorly, while
   26  * an allocator like Hoard will exhibit near-linear scaling.
   27  *
   28  * Try the following (on a P-processor machine):
   29  *
   30  *  cache-scratch 1 1000 1 1000000
   31  *  cache-scratch P 1000 1 1000000
   32  *
   33  *  cache-scratch-hoard 1 1000 1 1000000
   34  *  cache-scratch-hoard P 1000 1 1000000
   35  *
   36  *  The ideal is a P-fold speedup.
   37 */
   38 
   39 
   40 #include <stdio.h>
   41 #include <stdlib.h>
   42 
   43 #include "fred.h"
   44 #include "cpuinfo.h"
   45 #include "timer.h"
   46 
   47 // This class just holds arguments to each thread.
   48 class workerArg {
   49 public:
   50   workerArg (char * obj, int objSize, int repetitions, int iterations)
   51     : _object (obj),
   52       _objSize (objSize),
   53       _iterations (iterations),
   54       _repetitions (repetitions)
   55   {}
   56 
   57   char * _object;
   58   int _objSize;
   59   int _iterations;
   60   int _repetitions;
   61 };
   62 
   63 
   64 #if defined(_WIN32)
   65 extern "C" void worker (void * arg)
   66 #else
   67 extern "C" void * worker (void * arg)
   68 #endif
   69 {
   70   // free the object we were given.
   71   // Then, repeatedly do the following:
   72   //   malloc a given-sized object,
   73   //   repeatedly write on it,
   74   //   then free it.
   75   workerArg * w = (workerArg *) arg;
   76   delete w->_object;
   77   for (int i = 0; i < w->_iterations; i++) {
   78     // Allocate the object.
   79     char * obj = new char[w->_objSize];
   80     // Write into it a bunch of times.
   81     for (int j = 0; j < w->_repetitions; j++) {
   82       for (int k = 0; k < w->_objSize; k++) {
   83     obj[k] = (char) k;
   84     volatile char ch = obj[k];
   85     ch++;
   86       }
   87     }
   88     // Free the object.
   89     delete [] obj;
   90   }
   91   delete w;
   92 
   93 #if !defined(_WIN32)
   94   return NULL;
   95 #endif
   96 }
   97 
   98 
   99 int main (int argc, char * argv[])
  100 {
  101   int nthreads;
  102   int iterations;
  103   int objSize;
  104   int repetitions;
  105 
  106   if (argc > 4) {
  107     nthreads = atoi(argv[1]);
  108     iterations = atoi(argv[2]);
  109     objSize = atoi(argv[3]);
  110     repetitions = atoi(argv[4]);
  111   } else {
  112     fprintf (stderr, "Usage: %s nthreads iterations objSize repetitions\n", argv[0]);
  113     return 1;
  114   }
  115 
  116   HL::Fred * threads = new HL::Fred[nthreads];
  117   HL::Fred::setConcurrency (HL::CPUInfo::getNumProcessors());
  118 
  119   int i;
  120 
  121   // Allocate nthreads objects and distribute them among the threads.
  122   char ** objs = new char * [nthreads];
  123   for (i = 0; i < nthreads; i++) {
  124     objs[i] = new char[objSize];
  125   }
  126 
  127   HL::Timer t;
  128   t.start();
  129 
  130   for (i = 0; i < nthreads; i++) {
  131     workerArg * w = new workerArg (objs[i], objSize, repetitions / nthreads, iterations);
  132     threads[i].create (&worker, (void *) w);
  133   }
  134   for (i = 0; i < nthreads; i++) {
  135     threads[i].join();
  136   }
  137   t.stop();
  138 
  139   delete [] threads;
  140   delete [] objs;
  141 
  142   printf ("Time elapsed = %f seconds.\n", (double) t);
  143   return 0;
  144 }