Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. #include <omp.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. #define THREAD_COUNT 4
  6. #define SIZE 2000
  7.  
  8. int main (int argc, char *argv[]) {
  9.  
  10. double** mat2 = new double*[SIZE];
  11. double** mat = new double*[SIZE];
  12. double** result = new double*[SIZE]; // after multiplying vec and mat, we will get a new vector of the same SIZE
  13. int i,j,k;
  14. double start, end;
  15. double realCPUTime, userCPUTime;
  16.  
  17. // show number of threads before we actually set the thread number
  18. printf("Number of active threads is: %d.\n", omp_get_thread_num());
  19.  
  20. // set the specified number of threads to execute the parallel segment
  21. omp_set_num_threads(THREAD_COUNT);
  22.  
  23. for(i = 0; i < SIZE; i++){
  24. mat[i] = new double[SIZE];
  25. mat2[i] = new double[SIZE];
  26. result[i] = new double[SIZE];
  27. }
  28.  
  29. printf("Initializing arrays with random numbers.\n");
  30. for(i = 0; i < SIZE; i++){
  31. for(j = 0; j < SIZE; j++){
  32. mat[i][j] = (double)rand() / 100;
  33. mat2[i][j] = (double)rand() / 100;
  34. result[i][j] = 0;
  35. }
  36. }
  37.  
  38. start = omp_get_wtime();
  39. /* This creates a team of threads; each thread has own copy of variables tid, i and j, but share variables result, vec and mat */
  40. #pragma omp parallel private(i, j, k, start, end) shared(result, mat2, mat, realCPUTime)
  41. {
  42. #pragma omp for
  43. for(i = 0; i < SIZE; i++){
  44. start = omp_get_wtime();
  45. for(j = 0; j < SIZE; j++){
  46. for(k = 0; k < SIZE; k++){
  47. result[i][j] += mat2[i][k] * mat[k][j];
  48. }
  49. }
  50. end = omp_get_wtime();
  51. realCPUTime += end - start;
  52. }
  53. }
  54. end = omp_get_wtime();
  55.  
  56. userCPUTime = end - start;
  57.  
  58. printf("User CPU time took %f seconds.\n", userCPUTime);
  59. printf("Real CPU time took %f seconds.\n", realCPUTime);
  60.  
  61. double mflops = SIZE/(userCPUTime*1000000);
  62. mflops *= SIZE * SIZE * 2;
  63.  
  64. printf("MFLOPS: %f\n", mflops);
  65.  
  66. return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement