Advertisement
Guest User

CodeXL

a guest
Nov 30th, 2015
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.68 KB | None | 0 0
  1. // Celem tego programu jest prezentacja pomiaru i analizy
  2. //efektywnosci programu za pomocš  CodeAnalyst(tm).
  3. // Implementacja mnożenia macierzy jest realizowana za pomoca typowego
  4. // algorytmu podręcznikowego.
  5. #define _CRT_SECURE_NO_WARNINGS
  6. #include <stdio.h>
  7. #include <time.h>
  8. #include <windows.h>
  9. #include "omp.h"
  10. #include <fstream>
  11. #include <iostream>
  12. #include <string>
  13. #include <vector>
  14. #include <sstream>
  15. #include <streambuf>
  16.  
  17. #define USE_MULTIPLE_THREADS true
  18. #define MAXTHREADS 128
  19. int NumThreads;
  20. double start;
  21. using namespace std;
  22. static const int ROWS = 5;     // liczba wierszy macierzy
  23. static const int COLUMNS = 5;  // lizba kolumn macierzy
  24.  
  25. float matrix_a[ROWS][COLUMNS];    // lewy operand
  26. float matrix_b[ROWS][COLUMNS];    // prawy operand
  27. float matrix_r[ROWS][COLUMNS];    // wynik
  28. float matrix_good[ROWS][COLUMNS];    // 100% poprawny wynik
  29.  
  30. FILE *result_file;
  31.  
  32. void initialize_matrices()
  33. {
  34.     // zdefiniowanie zawarosci poczatkowej macierzy
  35.     //#pragma omp parallel for
  36.     for (int i = 0; i < ROWS; i++) {
  37.         for (int j = 0; j < COLUMNS; j++) {
  38.             matrix_a[i][j] = (float)rand() / RAND_MAX;
  39.             matrix_b[i][j] = (float)rand() / RAND_MAX;
  40.             matrix_r[i][j] = 0.0;
  41.         }
  42.     }
  43. }
  44. void show(){
  45.     for (int k = 0; k < COLUMNS; k++)
  46.         for (int i = 0; i < ROWS; i++)
  47.             cout << matrix_a[i][k] << endl;
  48. }
  49.  
  50. void splitString(string s, float **matrix, int column_count){
  51.  
  52.     istringstream iss(s);
  53.     vector<string> tokens;
  54.     copy(istream_iterator<string>(iss),
  55.         istream_iterator<string>(),
  56.         back_inserter(tokens));
  57.     int x, y;
  58.     for (int i = 0; i < tokens.size(); i++)
  59.     {
  60.         y = i % column_count;
  61.         x = (int)(i / column_count);
  62.  
  63.         matrix[x][y] = stof(tokens[i].c_str());
  64.         //cout << matrix[i] << endl;
  65.     }
  66. }
  67.  
  68. void load(){
  69.  
  70.     ifstream t("macierz.txt");
  71.     string data((std::istreambuf_iterator<char>(t)),
  72.         std::istreambuf_iterator<char>());
  73.     t.close();
  74.  
  75.     ifstream plik;
  76.     plik.open("macierz.txt");
  77.     if (plik.good() == true)
  78.     {
  79.  
  80.  
  81.         int flag = 0;
  82.         int a = 0;
  83.         int sep_pos = -1;
  84.         char ch;
  85.         cout << "Uzyskano dostep do pliku!" << endl;
  86.         while (!plik.eof()){
  87.             plik.get(ch);
  88.             if ((ch == '#') && (!flag)){
  89.                 sep_pos = a;
  90.                 flag = 1;
  91.                 //cout << "chuj" << a;
  92.                 break;
  93.             }
  94.             a++;
  95.         }
  96.  
  97.         if (flag){
  98.             //cout << sep_pos;
  99.  
  100.             cout << data.substr(0, sep_pos);
  101.             cout << endl << data.substr(sep_pos + 1);
  102.  
  103.             float **mat = new float*[99];
  104.             float **mat2 = new float*[99];
  105.             for (int i = 0; i < 99; i++) {
  106.                 mat[i] = new float[99];
  107.                 mat2[i] = new float[99];
  108.             }
  109.  
  110.             splitString(data.substr(0, sep_pos-1), mat, 5);
  111.             splitString(data.substr(sep_pos+1), mat2,5);
  112.  
  113.         }
  114.        
  115.     }
  116.     else cout << "Dostep do pliku zostal zabroniony!" << endl;
  117.  
  118.     plik.close();
  119. }
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126. void check(){
  127.     for (int j = 0; j < COLUMNS; j++)
  128.         for (int k = 0; k < COLUMNS; k++)
  129.             for (int i = 0; i < ROWS; i++)
  130.                 matrix_r[i][j] += matrix_a[i][k] * matrix_b[k][j];
  131.  
  132.     memcpy(&matrix_good, &matrix_r, sizeof(matrix_r));
  133. }
  134.  
  135. void initialize_matricesZ()
  136. {
  137.     // zdefiniowanie zawarosci poczatkowej macierzy
  138. #pragma omp parallel for
  139.     for (int i = 0; i < ROWS; i++) {
  140.         for (int j = 0; j < COLUMNS; j++) {
  141.             matrix_r[i][j] = 0.0;
  142.         }
  143.     }
  144. }
  145. void print_result()
  146. {
  147.     // wydruk wyniku
  148.     for (int i = 0; i < ROWS; i++) {
  149.         for (int j = 0; j < COLUMNS; j++) {
  150.             fprintf(result_file, "%6.4f ", matrix_r[i][j]);
  151.         }
  152.         fprintf(result_file, "\n");
  153.     }
  154. }
  155.  
  156.  
  157. void JKI1()
  158. {
  159.     // mnozenie macierzy
  160. #pragma omp parallel for
  161.     for (int j = 0; j < COLUMNS; j++)
  162.         for (int k = 0; k < COLUMNS; k++)
  163.             for (int i = 0; i < ROWS; i++)
  164.                 matrix_r[i][j] += matrix_a[i][k] * matrix_b[k][j];
  165.     memcmp(&matrix_r, &matrix_good, sizeof(matrix_good));
  166. }
  167.  
  168. void JKI2()
  169. {
  170.     // mnozenie macierzy
  171.     for (int j = 0; j < COLUMNS; j++)
  172. #pragma omp parallel for
  173.         for (int k = 0; k < COLUMNS; k++)
  174.             for (int i = 0; i < ROWS; i++)
  175.                 matrix_r[i][j] += matrix_a[i][k] * matrix_b[k][j];
  176.     memcmp(&matrix_r, &matrix_good, sizeof(matrix_good));
  177. }
  178.  
  179. void JKI3()
  180. {
  181.     // mnozenie macierzy
  182.  
  183.     for (int j = 0; j < COLUMNS; j++)
  184.         for (int k = 0; k < COLUMNS; k++)
  185. #pragma omp parallel for
  186.             for (int i = 0; i < ROWS; i++)
  187.                 matrix_r[i][j] += matrix_a[i][k] * matrix_b[k][j];
  188.     memcmp(&matrix_r, &matrix_good, sizeof(matrix_good));
  189. }
  190.  
  191. void print_elapsed_time()
  192. {
  193.     double elapsed;
  194.     double resolution;
  195.  
  196.     // wyznaczenie i zapisanie czasu przetwarzania
  197.     elapsed = (double)clock() / CLK_TCK;
  198.     resolution = 1.0 / CLK_TCK;
  199.     printf("Czas: %8.4f sec \n",
  200.         elapsed - start);
  201.  
  202.     fprintf(result_file,
  203.         "Czas wykonania programu: %8.4f sec (%6.4f sec rozdzielczosc pomiaru)\n",
  204.         elapsed - start, resolution);
  205. }
  206.  
  207. int main(int argc, char* argv[])
  208. {
  209. /*  //   start = (double) clock() / CLK_TCK ;
  210.     if ((result_file = fopen("classic.txt", "a")) == NULL) {
  211.         fprintf(stderr, "nie mozna otworzyc pliku wyniku \n");
  212.         perror("classic");
  213.         return(EXIT_FAILURE);
  214.     }
  215.  
  216.  
  217.     //Determine the number of threads to use
  218.     if (USE_MULTIPLE_THREADS) {
  219.         SYSTEM_INFO SysInfo;
  220.         GetSystemInfo(&SysInfo);
  221.         NumThreads = SysInfo.dwNumberOfProcessors;
  222.         if (NumThreads > MAXTHREADS)
  223.             NumThreads = MAXTHREADS;
  224.     }
  225.     else
  226.         NumThreads = 1;
  227.     fprintf(result_file, "Klasyczny algorytm mnozenia macierzy, liczba watkow %d \n", NumThreads);
  228.     printf("liczba watkow  = %d\n\n", NumThreads);
  229.  
  230.     initialize_matrices();
  231.     start = (double)clock() / CLK_TCK;
  232.     JKI1();
  233.     printf("JKI1 ");
  234.     print_elapsed_time();
  235.     initialize_matricesZ();
  236.     start = (double)clock() / CLK_TCK;
  237.     JKI2();
  238.     printf("JKI2 ");
  239.     print_elapsed_time();
  240.     initialize_matricesZ();
  241.     start = (double)clock() / CLK_TCK;
  242.     JKI3();
  243.     printf("JKI3 ");
  244.     print_elapsed_time();
  245.  
  246.     fclose(result_file);
  247. */
  248.     load();
  249.    
  250.     system("PAUSE");
  251.     return(0);
  252.    
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement