Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. /*
  2. * Sample program to test runtime of simple matrix multiply
  3. * with and without OpenMP on gcc-4.3.3-tdm1 (mingw)
  4. *
  5. * (c) 2009, Rajorshi Biswas
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <time.h>
  11. #include <assert.h>
  12.  
  13. #include <omp.h>
  14.  
  15.  
  16. int main(int argc, char **argv)
  17. {
  18. int i,j,k;
  19. int n;
  20. double temp;
  21. double start, end, run;
  22.  
  23. printf("omp_get_max_threads: %d\n", omp_get_max_threads());
  24.  
  25. printf("Enter dimension ('N' for 'NxN' matrix) (100-2000): ");
  26. scanf("%d", &n);
  27.  
  28. #ifdef _OPENMP
  29. printf("OpenMP presente\n");
  30. #endif
  31.  
  32. assert( n >= 100 && n <= 2000 );
  33.  
  34. int **arr1 = malloc( sizeof(int*) * n);
  35. int **arr2 = malloc( sizeof(int*) * n);
  36. int **arr3 = malloc( sizeof(int*) * n);
  37.  
  38. for(i=0; i<n; ++i) {
  39. arr1[i] = malloc( sizeof(int) * n );
  40. arr2[i] = malloc( sizeof(int) * n );
  41. arr3[i] = malloc( sizeof(int) * n );
  42. }
  43.  
  44. printf("Populating array with random values...\n");
  45. srand( time(NULL) );
  46.  
  47. for(i=0; i<n; ++i) {
  48. for(j=0; j<n; ++j) {
  49. arr1[i][j] = (rand() % n);
  50. arr2[i][j] = (rand() % n);
  51. }
  52. }
  53.  
  54. printf("Completed array init.\n");
  55. printf("Crunching without OMP...");
  56. fflush(stdout);
  57. start = omp_get_wtime();
  58.  
  59. for(i=0; i<n; ++i) {
  60. for(j=0; j<n; ++j) {
  61. temp = 0;
  62. for(k=0; k<n; ++k) {
  63. temp += arr1[i][k] * arr2[k][j];
  64. }
  65. arr3[i][j] = temp;
  66. }
  67. }
  68.  
  69. end = omp_get_wtime();
  70. printf(" took %f seconds.\n", end-start);
  71. printf("Crunching with OMP...");
  72. fflush(stdout);
  73. start = omp_get_wtime();
  74.  
  75. printf("\n\n");
  76. #pragma omp parallel
  77. {
  78. printf("omp_get_thread_num: %d\n",
  79. omp_get_thread_num());
  80. }
  81. printf("\n\n");
  82.  
  83. #pragma omp parallel for private( k, j, temp)
  84. for(i=0; i<n; ++i) {
  85. if (i == 0) {
  86. printf("omp_get_num_threads: %d\n",
  87. omp_get_num_threads());
  88. }
  89. for(j=0; j<n; ++j) {
  90. temp = 0;
  91. for(k=0; k<n; ++k) {
  92. temp += arr1[i][k] * arr2[k][j];
  93. }
  94. arr3[i][j] = temp;
  95. }
  96. }
  97.  
  98.  
  99. end = omp_get_wtime();
  100. printf(" took %f seconds.\n", end-start);
  101.  
  102. system("pause");
  103. return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement