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 #include "arch-specific.h"
00046 #include "timer.h"
00047
00048 #include <iostream.h>
00049 #include <stdlib.h>
00050
00051
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
00069
00070
00071
00072 workerArg * w = (workerArg *) arg;
00073 for (int i = 0; i < w->_iterations; i++) {
00074
00075 char * obj = new char[w->_objSize];
00076
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
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 }