void busywait(void * msec) { long long waitmsec = *((long long*) msec); long long msbefore; long long msafter; struct timespec spec; clock_gettime(CLOCK_REALTIME, &spec); srand(spec.tv_nsec); long randomval = rand(); clock_gettime(CLOCK_REALTIME, &spec); msbefore = timespecToMs(spec); while(1) { if (randomval < 2135116019) { randomval = randomval + 1; } clock_gettime(CLOCK_REALTIME, &spec); msafter = timespecToMs(spec); if((msafter-msbefore) > waitmsec) { break; } } }
int main() { int numThreads = 1; struct timespec tspec; double x = 0.001643721971153, y = 0.8224676332988; double zoom = 0.5; long long withpool, before; colorPalette * c = color_createPalette(7); color_setColor(c, 0, 0, 0, 0); color_setColor(c, 0, 33, 109, 1); color_setColor(c, 255, 192, 0, 2); color_setColor(c, 255, 255, 255, 3); color_setColor(c, 255, 192, 0, 4); color_setColor(c, 96, 0, 16, 5); color_setColor(c, 0, 0, 0, 6); FILE *file; file = fopen("./bin/times_nopool.csv","a+"); /* apend file (add text to a file)*/ fprintf(file, "---mandelbrot time test, no pool---\n"); fprintf(file, "imageSize = 512x512, iterations = 1000\n"); fprintf(file, "format: numThreads, time\n\n"); for (numThreads = 1; numThreads <= 128; numThreads += 2) { //time clock_gettime(CLOCK_REALTIME, &tspec); before = timespecToMs(tspec); renderImageTime(x, y, zoom, c, numThreads); // withpool time clock_gettime(CLOCK_REALTIME, &tspec); withpool = timespecToMs(tspec) - before; // save withpool results to file fprintf(file,"%d, %d\n", numThreads*numThreads, withpool); /*writes*/ } fclose(file); return 0; }
int main(int argc, char *argv[]) { if ( argc != 3 ) { printf("usage: %s numtasks poolsize\n", argv[0]); exit(0); } int NUMTASKS = atoi(argv[1]); //1600 // 16000 int POOLSIZE = atoi(argv[2]); // 300 //868 struct timespec tspec; long long withpool, nopool, before; static long long waittime = 500; clock_gettime(CLOCK_REALTIME, &tspec); before = timespecToMs(tspec); struct threadpool * pool = threadpool_create(POOLSIZE); for (int i=0; i < NUMTASKS; ++i) { threadpool_enqueue(pool, busywait, &waittime); } threadpool_destroy(pool); clock_gettime(CLOCK_REALTIME, &tspec); withpool = timespecToMs(tspec) - before; clock_gettime(CLOCK_REALTIME, &tspec); before = timespecToMs(tspec); pthread_t threads[NUMTASKS]; for (int i=0; i < NUMTASKS; ++i) { pthread_create(&threads[i], NULL, busypthread, &waittime); } for(int i=0; i < NUMTASKS; ++i) { if(pthread_join(threads[i], NULL) != 0) { exit(EXIT_FAILURE); } } clock_gettime(CLOCK_REALTIME, &tspec); nopool = timespecToMs(tspec) - before; printf("Pool runtime: %lld\n",withpool); printf("Naked runtime: %lld\n",nopool); return 0; }
// sets diffTimeMs parameter as well (appalling...) bool waitingTooLong(struct timespec startTime, int threshold, time_t* diffTimeMs) { struct timespec endTime; clock_gettime(CLOCK_MONOTONIC, &endTime); *diffTimeMs = timespecToMs(diffTime(endTime, startTime)); if(*diffTimeMs > threshold) return true; return false; }