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
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 #include "arch-specific.h"
00049 #include "timer.h"
00050
00051 #include <iostream.h>
00052 #include <stdlib.h>
00053
00054
00055 class workerArg {
00056 public:
00057 workerArg (char * obj, int objSize, int repetitions, int iterations)
00058 : _object (obj),
00059 _objSize (objSize),
00060 _iterations (iterations),
00061 _repetitions (repetitions)
00062 {}
00063
00064 char * _object;
00065 int _objSize;
00066 int _iterations;
00067 int _repetitions;
00068 };
00069
00070
00071 extern "C" void * worker (void * arg)
00072 {
00073
00074
00075
00076
00077
00078 workerArg * w = (workerArg *) arg;
00079 delete w->_object;
00080 for (int i = 0; i < w->_iterations; i++) {
00081
00082 char * obj = new char[w->_objSize];
00083
00084 for (int j = 0; j < w->_repetitions; j++) {
00085 for (int k = 0; k < w->_objSize; k++) {
00086 obj[k] = (char) k;
00087 volatile char ch = obj[k];
00088 ch++;
00089 }
00090 }
00091
00092 delete [] obj;
00093 }
00094 delete w;
00095 return NULL;
00096 }
00097
00098
00099 int main (int argc, char * argv[])
00100 {
00101 int nthreads;
00102 int iterations;
00103 int objSize;
00104 int repetitions;
00105
00106 if (argc > 4) {
00107 nthreads = atoi(argv[1]);
00108 iterations = atoi(argv[2]);
00109 objSize = atoi(argv[3]);
00110 repetitions = atoi(argv[4]);
00111 } else {
00112 cerr << "Usage: " << argv[0] << " nthreads iterations objSize repetitions" << endl;
00113 exit(1);
00114 }
00115
00116 hoardThreadType * threads = new hoardThreadType[nthreads];
00117 hoardSetConcurrency (hoardGetNumProcessors());
00118
00119 int i;
00120
00121
00122 char ** objs = new char * [nthreads];
00123 for (i = 0; i < nthreads; i++) {
00124 objs[i] = new char[objSize];
00125 }
00126
00127 Timer t;
00128 t.start();
00129
00130 for (i = 0; i < nthreads; i++) {
00131 workerArg * w = new workerArg (objs[i], objSize, repetitions / nthreads, iterations);
00132 hoardCreateThread (threads[i], worker, (void *) w);
00133 }
00134 for (i = 0; i < nthreads; i++) {
00135 hoardJoinThread (threads[i]);
00136 }
00137 t.stop();
00138
00139 delete [] threads;
00140 delete [] objs;
00141
00142 cout << "Time elapsed = " << (double) t << " seconds." << endl;
00143 return 0;
00144 }