Skip to content

Instantly share code, notes, and snippets.

@adhithyan15
Created March 8, 2016 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adhithyan15/14c8531f1e039cda5896 to your computer and use it in GitHub Desktop.
Save adhithyan15/14c8531f1e039cda5896 to your computer and use it in GitHub Desktop.
#include <omp.h>
#include <stdio.h>
#include "tournament.h"
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
int main(int argc, char **argv)
{
struct timeval start, end;
int t1, t2;
int t3, t4;
int iteration_factor;
int thread_count;
// Serial code
printf("This is the serial section\n");
thread_count = atoi(argv[1]);
iteration_factor = atoi(argv[2]);
int NUM_THREADS = thread_count;
omp_set_num_threads(NUM_THREADS);
initialize_tournament_barrier(NUM_THREADS);
int** data_storage;
int parallel_section_exec_times[iteration_factor];
data_storage = malloc(NUM_THREADS*sizeof(int*));
int k;
for(k = 0; k < NUM_THREADS; k++){
data_storage[k] = (int*) calloc(iteration_factor, sizeof(int));
}
int j = 0;
for( j = 0; j < iteration_factor; j++){
gettimeofday(&start, NULL);
t3 = start.tv_usec;
#pragma omp parallel
{
int num_threads = omp_get_num_threads();
int thread_num = omp_get_thread_num();
printf("Hello World from thread %d of %d.\n", thread_num, num_threads);
gettimeofday(&start, NULL);
t1 = start.tv_usec;
barrier();
gettimeofday(&end, NULL);
t2 = end.tv_usec;
data_storage[thread_num][j] = t2-t1;
printf("Goodbye World from thread %d of %d.\n", thread_num, num_threads);
}
gettimeofday(&end, NULL);
t4 = end.tv_usec;
parallel_section_exec_times[j] = t4-t3;
}
printf("Back in the serial section again\n");
FILE *fp;
char buf[500];
sprintf(buf, "results/tb_%d_%d.csv", thread_count, iteration_factor);
fp = fopen(buf, "w+");
double overall_avg = 0.0;
for(k = 0; k < NUM_THREADS; k++){
for(j = 0; j < iteration_factor; j++){
fprintf(fp, "%d", data_storage[k][j]);
fprintf(fp, ",");
}
int sum = 0;
double avg = 0.0;
int u;
for(u = 0; u < iteration_factor; u++){
sum = sum + data_storage[k][u];
}
avg = (double) sum/iteration_factor;
overall_avg = overall_avg + avg;
fprintf(fp, "%f", avg);
fprintf(fp, "\n");
}
overall_avg = (double) overall_avg/thread_count;
int u;
double exec_times_avg = 0.0;
for(u = 0; u < iteration_factor; u++){
fprintf(fp, "%d,", parallel_section_exec_times[u]);
exec_times_avg = parallel_section_exec_times[u] + exec_times_avg;
}
exec_times_avg = exec_times_avg/iteration_factor;
fprintf(fp, "%f\n", overall_avg);
fprintf(fp, "%f\n", exec_times_avg);
fclose(fp);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment