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.

99 lines
1.8 KiB

  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cmath>
  4. #include <omp.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. if (argc < 3)
  33. {
  34. cerr << "Usage: " << argv[0] << " <n> <size>" << endl;
  35. exit(1);
  36. }
  37. rozmiar = strtol(argv[2], &endptr, 10);
  38. if (*endptr)
  39. {
  40. cerr << "Invalid array size format" << endl;
  41. exit(1);
  42. }
  43. if (rozmiar <= 0)
  44. {
  45. cerr << "The number of matrix dimension must be positive" << endl;
  46. exit(1);
  47. }
  48. threads_num = strtol(argv[1], &endptr, 10);
  49. if (*endptr)
  50. {
  51. cerr << "Invalid number of threads format" << endl;
  52. exit(1);
  53. }
  54. if (threads_num <= 0)
  55. {
  56. cerr << "The number of threads must be positive" << endl;
  57. exit(1);
  58. }
  59. //ustawienie odpowiedniej ilosci watkow
  60. omp_set_num_threads(threads_num);
  61. //alokacja macierzy
  62. array_alloc(A, rozmiar);
  63. array_alloc(B, rozmiar);
  64. array_alloc(C, rozmiar);
  65. //wypelnienie macierzy A liczbami "losowymi"
  66. for (long i=0; i<rozmiar; ++i)
  67. for (long j=0; j<rozmiar; ++j)
  68. A[i][j] = (long)(sin(i) * i * j) % 10; // postarac sie to zoptymalizowac!
  69. //wypelnienie macierzy B liczbami "losowymi"
  70. for (long i=0; i<rozmiar; ++i)
  71. for (long j=0; j<rozmiar; ++j)
  72. B[i][j] = (long)(cos(j) *(i+j)) % 10; // postarac sie to zoptymalizowac!
  73. //mnozenie macierzy - TO DO
  74. array_destroy(A);
  75. array_destroy(B);
  76. array_destroy(C);
  77. exit(0);
  78. }