|
| 1 | +/** |
| 2 | + * |
| 3 | + * growvector imitates the behavior of appending to a vector. |
| 4 | + * |
| 5 | + * Try the following (on a P-processor machine): |
| 6 | + * |
| 7 | + * growvector 1 100000 8 |
| 8 | + * growvector P 100000 8 |
| 9 | + * |
| 10 | + * Written for Fall 2011 by 6.172 Staff |
| 11 | +*/ |
| 12 | + |
| 13 | + |
| 14 | +#include <stdio.h> |
| 15 | +#include <stdlib.h> |
| 16 | + |
| 17 | +#include "fred.h" |
| 18 | +#include "cpuinfo.h" |
| 19 | +#include "timer.h" |
| 20 | + |
| 21 | +#include "../wrapper.cpp" |
| 22 | + |
| 23 | +// This class just holds arguments to each thread. |
| 24 | +class workerArg { |
| 25 | +public: |
| 26 | + workerArg (int objSize, int iterations) |
| 27 | + : _objSize (objSize), |
| 28 | + _iterations (iterations) |
| 29 | + {} |
| 30 | + |
| 31 | + char** _array; |
| 32 | + int _arraySize; |
| 33 | + int _objSize; |
| 34 | + int _iterations; |
| 35 | +}; |
| 36 | + |
| 37 | + |
| 38 | +#if defined(_WIN32) |
| 39 | +extern "C" void worker (void * arg) |
| 40 | +#else |
| 41 | +extern "C" void * worker (void * arg) |
| 42 | +#endif |
| 43 | +{ |
| 44 | + workerArg * w = (workerArg *) arg; |
| 45 | + w->_arraySize = 8; |
| 46 | + w->_array = (char**) CUSTOM_MALLOC(w->_arraySize * sizeof(char*)); |
| 47 | + |
| 48 | + for (int i = 0; i < w->_iterations; i++) { |
| 49 | + if (i == w->_arraySize) { |
| 50 | + // Grow the array |
| 51 | + w->_arraySize *= 2; |
| 52 | + w->_array = (char**) CUSTOM_REALLOC(w->_array, w->_arraySize * sizeof(char*)); |
| 53 | + |
| 54 | + // Write everything in the array |
| 55 | + for (int j = 0; j < i; j++) { |
| 56 | + int size; |
| 57 | + if (j && !(j & (j - 1))) { |
| 58 | + size = j; |
| 59 | + } else { |
| 60 | + size = w->_objSize; |
| 61 | + } |
| 62 | + for (int k = 0; k < size; k++) { |
| 63 | + w->_array[j][k] = (char) k; |
| 64 | + volatile char ch = w->_array[j][k]; |
| 65 | + ch++; |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + int size; |
| 71 | + if (i && !(i & (i - 1))) { |
| 72 | + // Alocate bigger block for power of 2 |
| 73 | + size = i; |
| 74 | + } else { |
| 75 | + size = w->_objSize; |
| 76 | + } |
| 77 | + w->_array[i] = (char*) CUSTOM_MALLOC(size); |
| 78 | + } |
| 79 | + |
| 80 | + for (int i = 0; i < w->_iterations; i++) { |
| 81 | + // Write everything in the array |
| 82 | + int size; |
| 83 | + if (i && !(i & (i - 1))) { |
| 84 | + size = i; |
| 85 | + } else { |
| 86 | + size = w->_objSize; |
| 87 | + } |
| 88 | + for (int k = 0; k < size; k++) { |
| 89 | + w->_array[i][k] = (char) k; |
| 90 | + volatile char ch = w->_array[i][k]; |
| 91 | + ch++; |
| 92 | + } |
| 93 | + CUSTOM_FREE(w->_array[i]); |
| 94 | + } |
| 95 | + |
| 96 | + CUSTOM_FREE(w->_array); |
| 97 | + delete w; |
| 98 | + |
| 99 | + end_thread(); |
| 100 | + |
| 101 | +#if !defined(_WIN32) |
| 102 | + return NULL; |
| 103 | +#endif |
| 104 | +} |
| 105 | + |
| 106 | + |
| 107 | +int main (int argc, char * argv[]) |
| 108 | +{ |
| 109 | + int nthreads; |
| 110 | + int iterations; |
| 111 | + int objSize; |
| 112 | + int repetitions; |
| 113 | + |
| 114 | + if (argc > 3) { |
| 115 | + nthreads = atoi(argv[1]); |
| 116 | + iterations = atoi(argv[2]); |
| 117 | + objSize = atoi(argv[3]); |
| 118 | + } else { |
| 119 | + fprintf (stderr, "Usage: %s nthreads iterations objSize\n", argv[0]); |
| 120 | + return 1; |
| 121 | + } |
| 122 | + |
| 123 | + HL::Fred * threads = new HL::Fred[nthreads]; |
| 124 | + HL::Fred::setConcurrency (HL::CPUInfo::getNumProcessors()); |
| 125 | + |
| 126 | + int i; |
| 127 | + |
| 128 | + HL::Timer t; |
| 129 | + t.start(); |
| 130 | + |
| 131 | + for (i = 0; i < nthreads; i++) { |
| 132 | + workerArg * w = new workerArg (objSize, iterations); |
| 133 | + threads[i].create (&worker, (void *) w); |
| 134 | + } |
| 135 | + for (i = 0; i < nthreads; i++) { |
| 136 | + threads[i].join(); |
| 137 | + } |
| 138 | + t.stop(); |
| 139 | + |
| 140 | + delete [] threads; |
| 141 | + |
| 142 | + printf ("Time elapsed = %f seconds.\n", (double) t); |
| 143 | + end_program(); |
| 144 | + return 0; |
| 145 | +} |
0 commit comments