You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
2.1 KiB

  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cmath>
  4. #include "Ttiming.h"
  5. using namespace std;
  6. void array_alloc(long **&arr, long arrsize)
  7. {
  8. try
  9. {
  10. arr = new long* [arrsize];
  11. arr[0] = new long [arrsize*arrsize];
  12. for(long i = 1; i < arrsize; ++i)
  13. arr[i] = arr[i-1] + arrsize;
  14. }
  15. catch (bad_alloc& ex)
  16. {
  17. cerr << "Could not allocate memory for array" << endl;
  18. exit(1);
  19. }
  20. }
  21. void array_destroy(long **arr)
  22. {
  23. delete [] arr[0];
  24. delete [] arr;
  25. }
  26. int main(int argc, char *argv[])
  27. {
  28. long **A=NULL, **B=NULL, **C=NULL;
  29. long rozmiar=0;
  30. char *endptr;
  31. int threads_num=0;
  32. TTiming tt;
  33. long i, j;//,k;
  34. if (argc < 3)
  35. {
  36. cerr << "Usage: " << argv[0] << " <n> <size>" << endl;
  37. exit(1);
  38. }
  39. rozmiar = strtol(argv[2], &endptr, 10);
  40. if (*endptr)
  41. {
  42. cerr << "Invalid array size format" << endl;
  43. exit(1);
  44. }
  45. if (rozmiar <= 0 || rozmiar > 2000)
  46. {
  47. cerr << "The number of matrix dimension must be in range [1,2000]" << endl;
  48. exit(1);
  49. }
  50. threads_num = strtol(argv[1], &endptr, 10);
  51. if (*endptr)
  52. {
  53. cerr << "Invalid number of threads format" << endl;
  54. exit(1);
  55. }
  56. if (threads_num <= 0)
  57. {
  58. cerr << "The number of threads must be positive" << endl;
  59. exit(1);
  60. }
  61. //ustawienie odpowiedniej ilosci watkow
  62. //omp_set_num_threads(threads_num);
  63. //alokacja macierzy
  64. array_alloc(A, rozmiar);
  65. array_alloc(B, rozmiar);
  66. array_alloc(C, rozmiar);
  67. //wypelnienie macierzy A liczbami "losowymi"
  68. for (long i=0; i<rozmiar; ++i)
  69. for (long j=0; j<rozmiar; ++j)
  70. A[i][j] = (long)(sin(i) * i * j) % 10;
  71. //wypelnienie macierzy B liczbami "losowymi"
  72. for (long i=0; i<rozmiar; ++i)
  73. for (long j=0; j<rozmiar; ++j)
  74. B[i][j] = (long)(cos(j) *(i+j)) % 10;
  75. tt.Begin();
  76. //#pragma omp parallel for default(shared)
  77. //#pragma omp parallel for default(none) shared(A, B, C) firstprivate(rozmiar)private(i, j)
  78. for (i=0; i<rozmiar; ++i)
  79. for (j=0; j<rozmiar; ++j)
  80. {
  81. C[i][j] = 0;
  82. for (long k=0; k<rozmiar; ++k)
  83. C[i][j] += A[i][k]*B[k][j];
  84. }
  85. long elapsed = tt.End();
  86. cout << "Time: " << elapsed << " ms" << endl;
  87. array_destroy(A);
  88. array_destroy(B);
  89. array_destroy(C);
  90. exit(0);
  91. }