diff --git a/DergunPiotr-WaskoDominik/zad2/Makefile b/DergunPiotr-WaskoDominik/zad2/Makefile new file mode 100644 index 0000000..400d486 --- /dev/null +++ b/DergunPiotr-WaskoDominik/zad2/Makefile @@ -0,0 +1,5 @@ +macierz_mpi: macierz_mpi.cpp + mpicxx -Wall -o macierz_mpi macierz_mpi.cpp + +clean: + rm -rf macierz_mpi diff --git a/DergunPiotr-WaskoDominik/zad2/Ttiming.h b/DergunPiotr-WaskoDominik/zad2/Ttiming.h new file mode 100644 index 0000000..93d6e19 --- /dev/null +++ b/DergunPiotr-WaskoDominik/zad2/Ttiming.h @@ -0,0 +1,40 @@ +#if !defined(DEF_TTIMING) +#define DEF_TTIMING +#include + +class TTiming +{ +protected: + struct timeval start; + struct timeval stop; + void getTime(timeval &tv); + +public: + TTiming(void); + + void Begin(void); + long End(void); +}; + +inline TTiming::TTiming(void) +{ + +} + +inline void TTiming::Begin(void) +{ + getTime(start); +} + +inline long TTiming::End(void) +{ + getTime(stop); + return ((stop.tv_sec-start.tv_sec) * 1000 + (stop.tv_usec-start.tv_usec)/1000.0) + 0.5; +} + +inline void TTiming::getTime(timeval &tv) +{ + gettimeofday(&tv,NULL); +} + +#endif diff --git a/DergunPiotr-WaskoDominik/zad2/dane/czas.jpg b/DergunPiotr-WaskoDominik/zad2/dane/czas.jpg new file mode 100644 index 0000000..f549aca Binary files /dev/null and b/DergunPiotr-WaskoDominik/zad2/dane/czas.jpg differ diff --git a/DergunPiotr-WaskoDominik/zad2/dane/przyspieszenie.jpg b/DergunPiotr-WaskoDominik/zad2/dane/przyspieszenie.jpg new file mode 100644 index 0000000..9d00fe6 Binary files /dev/null and b/DergunPiotr-WaskoDominik/zad2/dane/przyspieszenie.jpg differ diff --git a/DergunPiotr-WaskoDominik/zad2/dane/wykres.gnuplot b/DergunPiotr-WaskoDominik/zad2/dane/wykres.gnuplot new file mode 100644 index 0000000..5beba86 --- /dev/null +++ b/DergunPiotr-WaskoDominik/zad2/dane/wykres.gnuplot @@ -0,0 +1,19 @@ +#set terminal x11 +set terminal jpeg +set xrange [0:16] +set yrange [0:6] + +set xlabel "Liczba watkow [n]" +set ylabel "Przyspieszenie [n]" + +set out "przyspieszenie.jpg" +plot \ + "wyniki.txt" using 1:3 with points ls 3 lc rgb "red" title "przyspieszenie", \ + "wyniki.txt" using 1:3 with lines ls 3 lc rgb "blue" notitle + +set out "czas.jpg" +set ylabel "Czas obliczen [ms]" +set yrange [0:16000] +plot \ + "wyniki.txt" using 1:2 with points ls 3 lc rgb "red" title "czas", \ + "wyniki.txt" using 1:2 with lines ls 3 lc rgb "blue" notitle \ No newline at end of file diff --git a/DergunPiotr-WaskoDominik/zad2/dok.tex b/DergunPiotr-WaskoDominik/zad2/dok.tex new file mode 100644 index 0000000..d0e4612 --- /dev/null +++ b/DergunPiotr-WaskoDominik/zad2/dok.tex @@ -0,0 +1,117 @@ +\documentclass[a4paper,12pt]{article} +\usepackage{amsmath} +\usepackage{amssymb} +\usepackage[polish]{babel} +\usepackage{polski} +\usepackage[utf8]{inputenc} +\usepackage{indentfirst} +\usepackage{geometry} +\usepackage{array} +\usepackage[pdftex]{color,graphicx} +\usepackage{subfigure} +\usepackage{afterpage} +\usepackage{setspace} +\usepackage{color} +\usepackage{wrapfig} +\usepackage{listings} +\usepackage{datetime} + +\renewcommand{\onehalfspacing}{\setstretch{1.6}} + +\geometry{tmargin=2.5cm,bmargin=2.5cm,lmargin=2.5cm,rmargin=2.5cm} +\setlength{\parindent}{1cm} +\setlength{\parskip}{0mm} + +\newenvironment{lista}{ +\begin{itemize} + \setlength{\itemsep}{1pt} + \setlength{\parskip}{0pt} + \setlength{\parsep}{0pt} +}{\end{itemize}} + +\newcommand{\linia}{\rule{\linewidth}{0.4mm}} + +\definecolor{lbcolor}{rgb}{0.95,0.95,0.95} +\lstset{ + backgroundcolor=\color{lbcolor}, + tabsize=4, + language=C++, + captionpos=b, + tabsize=3, + frame=lines, + numbers=left, + numberstyle=\tiny, + numbersep=5pt, + breaklines=true, + showstringspaces=false, + basicstyle=\footnotesize, + identifierstyle=\color{magenta}, + keywordstyle=\color[rgb]{0,0,1}, + commentstyle=\color{Darkgreen}, + stringstyle=\color{red} + } + +\begin{document} + +\noindent +\begin{tabular}{|c|p{11cm}|c|} \hline +Grupa 1 & Piotr Dergun, Dominik Waśko & \ddmmyyyydate\today \tabularnewline +\hline +\end{tabular} + + +\section*{Zadanie 1 - Macierze OMP} + +Celem zadania jest obliczenie iloczynu dwóch macierzy prostokątnych na konkretnej liczbie wątków (dane te podane jako parametry programu). Istotą problemu są trzy pętle, których złożoność obliczeniowa jest O($n^3$) + +\begin{lstlisting} + #pragma omp parallel for default(none) shared(A, B, C) firstprivate(rozmiar)private(i, j) + for (i=0; i +#include +#include +#include "Ttiming.h" + +using namespace std; + +void array_alloc(long **&arr, long arrsize) +{ + try + { + arr = new long* [arrsize]; + + + arr[0] = new long [arrsize*arrsize]; + + for(long i = 1; i < arrsize; ++i) + arr[i] = arr[i-1] + arrsize; + + } + catch (bad_alloc& ex) + { + cerr << "Could not allocate memory for array" << endl; + exit(1); + } +} + +void array_destroy(long **arr) +{ + delete [] arr[0]; + delete [] arr; +} + +int main(int argc, char *argv[]) +{ + long **A=NULL, **B=NULL, **C=NULL; + long rozmiar=0; + char *endptr; + int threads_num=0; + TTiming tt; + long i, j;//,k; + + if (argc < 3) + { + cerr << "Usage: " << argv[0] << " " << endl; + exit(1); + } + + rozmiar = strtol(argv[2], &endptr, 10); + + if (*endptr) + { + cerr << "Invalid array size format" << endl; + exit(1); + } + + if (rozmiar <= 0 || rozmiar > 2000) + { + cerr << "The number of matrix dimension must be in range [1,2000]" << endl; + exit(1); + } + + threads_num = strtol(argv[1], &endptr, 10); + + if (*endptr) + { + cerr << "Invalid number of threads format" << endl; + exit(1); + } + + if (threads_num <= 0) + { + cerr << "The number of threads must be positive" << endl; + exit(1); + } + + //ustawienie odpowiedniej ilosci watkow + //omp_set_num_threads(threads_num); + + //alokacja macierzy + array_alloc(A, rozmiar); + array_alloc(B, rozmiar); + array_alloc(C, rozmiar); + + //wypelnienie macierzy A liczbami "losowymi" + for (long i=0; i