00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00019
00020
00021
00022
00023
00024
00025
00026
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;
00042 int nobjects = 300000;
00043 int nthreads = 1;
00044 int work = 0;
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
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 }