Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <omp.h>
  3. #include <cmath>
  4. #include <time.h>
  5. #include <stdio.h>
  6.  
  7. #define MAXSIZE 3000
  8.  
  9. void create_array(double array[][MAXSIZE], int n) {
  10.     for (int i=0; i<n; i++) {
  11.         //std::cout << omp_get_thread_num() << std::endl;
  12.         for (int j = 0; j < n; j++) {
  13.             array[i][j] = i*(atan(j) +exp(-(i+j)));
  14.         }
  15.     }
  16. }
  17.  
  18. void create_array_parallel(double array[][MAXSIZE], int n) {
  19. #pragma omp parallel for
  20.     for (int i=0; i<n; i++) {
  21.         //std::cout << omp_get_thread_num() << std::endl;
  22.         for (int j = 0; j < n; j++) {
  23.             array[i][j] = i*(atan(j) +exp(-(i+j)));
  24.         }
  25.     }
  26. }
  27.  
  28. double array[MAXSIZE][MAXSIZE];
  29.  
  30. int main() {
  31.     double start, finish, time_single_thread, time_multi_thread;
  32.  
  33.     start = omp_get_wtime();
  34.     create_array(array, MAXSIZE);
  35.     finish = omp_get_wtime();
  36.     time_single_thread = finish - start;
  37.     printf("Total runtime with single thread = %lg\n", time_single_thread);
  38.  
  39.     start = omp_get_wtime();
  40.     create_array_parallel(array, MAXSIZE);
  41.     finish = omp_get_wtime();
  42.     time_multi_thread = finish - start;
  43.     printf("Total runtime with multiple threads = %lg\n", time_multi_thread);
  44.     printf("Speed up = %lg\n", time_single_thread / time_multi_thread);
  45.     printf("Efficiency = %lg\n", time_single_thread / time_multi_thread / 2);
  46.  
  47.     getchar();
  48.  
  49.     return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement