Implementing the code to sum numbers in serial and then in parallel using c and OpenMP include #include #include #include

Implementing the code to sum numbers in serial and then in parallel using c and OpenMP include

#include

#include

#include

#include

static const long Num_To_Add = 1000000000;

static const double Scale = 10.0 / RAND_MAX;

long add_serial(const char *numbers) {

    long sum = 0;

    for (long i = 0; i < Num_To_Add; i++) {         sum += numbers[i];     }     return sum; } long add_parallel(const char *numbers) {     long sum = 0;     return sum; } int main() {     char *numbers = malloc(sizeof(long) * Num_To_Add);     long chunk_size = Num_To_Add / omp_get_max_threads(); #pragma omp parallel num_threads(omp_get_max_threads())     {         int p = omp_get_thread_num();         unsigned int seed = (unsigned int) time(NULL) + (unsigned int) p;         long chunk_start = p * chunk_size;         long chunk_end = chunk_start + chunk_size;         for (long i = chunk_start; i < chunk_end; i++) {             numbers[i] = (char) (rand_r(&seed) * Scale);         }     }     struct timeval start, end;     printf("Timing sequential...n");     gettimeofday(&start, NULL);     long sum_s = add_serial(numbers);     gettimeofday(&end, NULL);     printf("Took %f secondsnn", end.tv_sec - start.tv_sec + (double) (end.tv_usec - start.tv_usec) / 1000000);     printf("Timing parallel...n");     gettimeofday(&start, NULL);     long sum_p = add_parallel(numbers);     gettimeofday(&end, NULL);     printf("Took %f secondsnn", end.tv_sec - start.tv_sec + (double) (end.tv_usec - start.tv_usec) / 1000000);     printf("Sum serial: %ldnSum parallel: %ld", sum_s, sum_p);     free(numbers);     return 0; }

Submit a Comment

Open chat