Advertisement
mahmoodn

mm_omp_sig

Apr 7th, 2022
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <omp.h>
  5. #include <signal.h>
  6.  
  7. #include <sys/time.h>
  8.  
  9. #define N 6000
  10.  
  11. void multiplyMatrices(double first[][N], double second[][N], double result[][N])
  12. {
  13. // Multiplying first and second matrices and storing it in result
  14. int i,j,k;
  15. #pragma omp parallel for
  16. for (i = 0; i < N; ++i) {
  17. for (j = 0; j < N; ++j) {
  18. for (k = 0; k < N; ++k) {
  19. result[i][j] += first[i][k] * second[k][j];
  20. }
  21. }
  22. }
  23. }
  24.  
  25. void fill_matrices(double first[][N], double second[][N], double result[][N])
  26. {
  27. srand(time(NULL)); // randomize seed
  28. for (int i = 0; i < N; i++){
  29. for (int j = 0; j < N; j++){
  30. first[i][j] = rand() % 10;
  31. second[i][j] = rand() % 10;
  32. result[i][j] = 0;
  33. }
  34. }
  35. }
  36. void print(double first[][N], double second[][N], double result[][N])
  37. {
  38. srand48(time(NULL)); // randomize seed
  39. printf("First:\n");
  40. for (int i = 0; i < N; i++){
  41. printf("[ ");
  42. for (int j = 0; j < N; j++){
  43. printf("%f ", first[i][j]);
  44. }
  45. printf("]\n");
  46. }
  47. printf("\nSecond:\n");
  48. for (int i = 0; i < N; i++){
  49. printf("[ ");
  50. for (int j = 0; j < N; j++){
  51. printf("%f ", second[i][j]);
  52. }
  53. printf("]\n");
  54. }
  55. printf("\nResult:\n");
  56. for (int i = 0; i < N; i++){
  57. printf("[ ");
  58. for (int j = 0; j < N; j++){
  59. printf("%f ", result[i][j]);
  60. }
  61. printf("]\n");
  62. }
  63. }
  64.  
  65. int main(int argc, char *argv[])
  66. {
  67. int max_thread = omp_get_num_procs();
  68. int nthread;
  69. char *p;
  70. if (argc == 1) {
  71. printf("No nthread, assumig %d threads\n", max_thread);
  72. nthread = max_thread;
  73. } else {
  74. nthread = strtol(argv[1], &p, 10);
  75. if (nthread > max_thread) {
  76. printf("Using %d threads\n", max_thread);
  77. nthread = max_thread;
  78. } else
  79. printf("Using %d threads\n", nthread);
  80. }
  81. double start1, start2, stop1, stop2, execution_time1, execution_time2;
  82. omp_set_num_threads(nthread);
  83. static double first[N][N], second[N][N], result[N][N];
  84.  
  85. int pid_t = getpid();
  86. printf("PID = %d\n", pid_t);
  87. kill(pid_t, SIGSTOP);
  88.  
  89. start1 = omp_get_wtime();
  90. fill_matrices(first, second, result);
  91. stop1 = omp_get_wtime();
  92.  
  93.  
  94.  
  95. start2 = omp_get_wtime();
  96. multiplyMatrices(first, second, result);
  97. stop2 = omp_get_wtime();
  98.  
  99. execution_time1 = stop2 - start1;
  100. execution_time2 = stop2 - start2;
  101. //print(first, second, result);
  102. printf("Total execution Time in seconds: %.10lf\n", execution_time1 );
  103. printf("MM execution Time in seconds: %.10lf\n", execution_time2 );
  104. return 0;
  105. }
  106.  
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement