#include #include #include #define BUFSIZE 100000 #define ARRSIZE 10000000 long timespecDiff(struct timespec *timeA_p, struct timespec *timeB_p) { return ((timeA_p->tv_sec * 1000000000) + timeA_p->tv_nsec) - ((timeB_p->tv_sec * 1000000000) + timeB_p->tv_nsec); } int main() { FILE *f; struct timespec start, end; /* test standard formated writes */ f = fopen("out1.txt","w"); clock_gettime(CLOCK_MONOTONIC, &start); for (int i = 0; i < ARRSIZE; i++) { fprintf(f, "%.15e\n", (double)i); } clock_gettime(CLOCK_MONOTONIC, &end); long timeElapsed = timespecDiff(&end, &start); printf("Time elapsed serial: %.3f s\n", (double)timeElapsed / 1e9); fclose(f); /* parallel version */ f = fopen("out2.txt","w"); clock_gettime(CLOCK_MONOTONIC, &start); #pragma omp parallel { int j = 0; char *tmp = malloc((BUFSIZE * 22 + 1)*sizeof(char)); #pragma omp for ordered schedule(dynamic,BUFSIZE) for (int i = 0; i < ARRSIZE; i++) { sprintf(tmp + j * 22, "%.15e\n", (double)i); j++; if(j % BUFSIZE == 0) { #pragma omp ordered { fwrite(tmp, sizeof(char) * 22 * BUFSIZE, 1, f); } j = 0; } } free(tmp); } clock_gettime(CLOCK_MONOTONIC, &end); timeElapsed = timespecDiff(&end, &start); printf("Time elapsed parallel: %.3f s\n", (double)timeElapsed / 1e9); return 0; }