usermode/library/malloc-hoard-old/benchmarks/threadtest/threadtest.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 /*
00022   threadtest
00023 
00024   This program does nothing but generate a number of kernel threads
00025   that allocate and free memory, with a variable
00026   amount of "work" (i.e. cycle wasting) in between.
00027 */
00028 
00029 #ifndef _REENTRANT
00030 #define _REENTRANT
00031 #endif
00032 
00033 #include <iostream.h>
00034 #include <assert.h>
00035 #include <stdio.h>
00036 #include <stdlib.h>
00037 
00038 #include "arch-specific.h"
00039 #include "timer.h"
00040 
00041 int niterations = 50;   // Default number of iterations.
00042 int nobjects = 300000;  // Default number of objects.
00043 int nthreads = 1;       // Default number of threads.
00044 int work = 0;           // Default number of loop iterations.
00045 int size = 1;
00046 
00047 
00048 class Foo {
00049 public:
00050   Foo (void)
00051     : x (14),
00052       y (29)
00053     {}
00054 
00055   int x;
00056   int y;
00057 };
00058 
00059 
00060 extern "C" void * worker (void *)
00061 {
00062   int i, j;
00063   Foo ** a;
00064   a = new Foo * [nobjects / nthreads];
00065 
00066   for (j = 0; j < niterations; j++) {
00067 
00068     // printf ("%d\n", j);
00069     for (i = 0; i < nobjects / nthreads; i++) {
00070       a[i] = new Foo[size];
00071       for (volatile int d = 0; d < work; d++) {
00072         volatile int f = 1;
00073         f = f + f;
00074         f = f * f;
00075         f = f + f;
00076         f = f * f;
00077       }
00078       assert (a[i]);
00079     }
00080     
00081     for (i = 0; i < nobjects / nthreads; i++) {
00082       delete a[i];
00083       for (volatile int d = 0; d < work; d++) {
00084         volatile int f = 1;
00085         f = f + f;
00086         f = f * f;
00087         f = f + f;
00088         f = f * f;
00089       }
00090     }
00091   }
00092 
00093   delete [] a;
00094 
00095   return NULL;
00096 }
00097 
00098 #if defined(__sgi)
00099 #include <ulocks.h>
00100 #endif
00101 
00102 int main (int argc, char * argv[])
00103 {
00104   hoardThreadType * threads;
00105   
00106   if (argc >= 2) {
00107     nthreads = atoi(argv[1]);
00108   }
00109 
00110   if (argc >= 3) {
00111     niterations = atoi(argv[2]);
00112   }
00113 
00114   if (argc >= 4) {
00115     nobjects = atoi(argv[3]);
00116   }
00117 
00118   if (argc >= 5) {
00119     work = atoi(argv[4]);
00120   }
00121 
00122   if (argc >= 6) {
00123     size = atoi(argv[5]);
00124   }
00125 
00126   printf ("Running threadtest for %d threads, %d iterations, %d objects, %d work and %d size...\n", nthreads, niterations, nobjects, work, size);
00127 
00128   threads = new hoardThreadType[nthreads];
00129   hoardSetConcurrency (nthreads);
00130 
00131   Timer t;
00132 
00133   t.start ();
00134 
00135   int i;
00136   for (i = 0; i < nthreads; i++) {
00137     hoardCreateThread (threads[i], worker, NULL);
00138   }
00139 
00140   for (i = 0; i < nthreads; i++) {
00141     hoardJoinThread (threads[i]);
00142   }
00143   t.stop ();
00144 
00145   printf( "Time elapsed = %f\n", (double) t);
00146 
00147   delete [] threads;
00148 
00149   return 0;
00150 }

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