parent
aec547116c
commit
93d06bb2b5
@ -0,0 +1,142 @@ |
||||
#include <mpi.h> |
||||
#include <stdio.h> |
||||
#include <malloc.h> |
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
|
||||
#define HAYSTACKSIZE 29 |
||||
#define WORDSIZE 10 |
||||
|
||||
int main(argc, argv) |
||||
int argc; |
||||
char *argv[]; |
||||
{ |
||||
int size, rank; |
||||
char haystack[HAYSTACKSIZE][WORDSIZE], needle[WORDSIZE], (*dataChunk)[WORDSIZE]; |
||||
int i, *sendcounts, *offsets, chunkSize, zvysok, *retbuf, *matches; |
||||
MPI_Datatype word; |
||||
|
||||
/* Initialize MPI */ |
||||
MPI_Init(&argc, &argv); |
||||
MPI_Type_contiguous(WORDSIZE, MPI_CHAR, &word); //vymedzenie noveho typu
|
||||
MPI_Type_commit(&word); //zaznamenanie noveho typu
|
||||
MPI_Comm_size(MPI_COMM_WORLD, &size); |
||||
MPI_Comm_rank(MPI_COMM_WORLD, &rank); |
||||
|
||||
chunkSize = HAYSTACKSIZE / size; //pocet slov na procesor
|
||||
zvysok = HAYSTACKSIZE % size; |
||||
|
||||
if (rank == 0) |
||||
{ |
||||
//tabulka mien
|
||||
strcpy(haystack[0], "Alexandra");
|
||||
strcpy(haystack[1], "Daniela");
|
||||
strcpy(haystack[2], "Drahoslav");
|
||||
strcpy(haystack[3], "Andrea");
|
||||
strcpy(haystack[4], "Antonia");
|
||||
strcpy(haystack[5], "Bohuslava");
|
||||
strcpy(haystack[6], "Severin");
|
||||
strcpy(haystack[7], "Alexej");
|
||||
strcpy(haystack[8], "Dasa");
|
||||
strcpy(haystack[9], "Malvina");
|
||||
strcpy(haystack[10], "Ernest");
|
||||
strcpy(haystack[11], "Rastislav");
|
||||
strcpy(haystack[12], "Radovan");
|
||||
strcpy(haystack[13], "Dobroslav");
|
||||
strcpy(haystack[14], "Kristina");
|
||||
strcpy(haystack[15], "Natasa");
|
||||
strcpy(haystack[16], "Bohdana");
|
||||
strcpy(haystack[17], "Drahomira");
|
||||
strcpy(haystack[18], "Dalibor");
|
||||
strcpy(haystack[19], "Vincent");
|
||||
strcpy(haystack[20], "Zora");
|
||||
strcpy(haystack[21], "Milos");
|
||||
strcpy(haystack[22], "Timotej");
|
||||
strcpy(haystack[23], "Gejza");
|
||||
strcpy(haystack[24], "Tamara");
|
||||
strcpy(haystack[25], "Bohus");
|
||||
strcpy(haystack[26], "Alfonz");
|
||||
strcpy(haystack[27], "Gaspar");
|
||||
strcpy(haystack[28], "Ema");
|
||||
strcpy(haystack[29], "Emil"); |
||||
|
||||
//printf("\nTabulka slov:\n\n");
|
||||
|
||||
//for(i = 0; i < HAYSTACKSIZE; i++)
|
||||
//{
|
||||
//printf("[%d] %s\n", i, haystack[i]);
|
||||
//}
|
||||
|
||||
//printf("\n");
|
||||
|
||||
strcpy(needle, "la"); //hladany retazec
|
||||
|
||||
matches = (int*) malloc(HAYSTACKSIZE * sizeof(int)); |
||||
|
||||
sendcounts = (int*) malloc(size*sizeof(int)); |
||||
offsets = (int*) malloc(size*sizeof(int)); |
||||
|
||||
for(i = 0; i < size; i++) |
||||
{ |
||||
if(i < zvysok) |
||||
{ |
||||
sendcounts[i] = chunkSize + 1; // pocet slov pre procesor
|
||||
offsets[i] = i * (chunkSize + 1);
|
||||
} |
||||
else |
||||
{ |
||||
sendcounts[i] = chunkSize; |
||||
offsets[i] = (zvysok*(chunkSize+1)) + ((i-zvysok) * chunkSize); |
||||
} |
||||
} |
||||
} |
||||
|
||||
MPI_Bcast(needle,WORDSIZE,MPI_CHAR,0,MPI_COMM_WORLD); |
||||
|
||||
chunkSize = rank < zvysok ? chunkSize+1:chunkSize; |
||||
dataChunk = (char(*)[WORDSIZE]) malloc(sizeof(char) * WORDSIZE * chunkSize); |
||||
|
||||
/*
|
||||
int MPI_Scatterv(void* sendbuf, int *sendcounts, |
||||
int *displs, MPI_Datatype sendtype, void* recvbuf, |
||||
int recvcount, MPI_Datatype recvtype, int root, |
||||
MPI_Comm comm); |
||||
*/ |
||||
|
||||
MPI_Scatterv(haystack, sendcounts, offsets, word, dataChunk, chunkSize, word, 0, MPI_COMM_WORLD); |
||||
|
||||
retbuf = (int*) malloc(chunkSize*sizeof(int)); |
||||
|
||||
for(i = 0; i < chunkSize; i++) |
||||
{ |
||||
//printf("rank %d: %s\n", rank, dataChunk[i]);
|
||||
retbuf[i] = 0; |
||||
if(strstr(dataChunk[i], needle) != NULL) |
||||
retbuf[i] = 1; |
||||
} |
||||
|
||||
|
||||
//printf("\n");
|
||||
|
||||
/*
|
||||
int MPI_Gatherv(void* sendbuf, int sendcount, MPI_Datatype sendtype, |
||||
void* recvbuf, int recvcounts, int *displs, MPI_Datatype recvtype, |
||||
int root, MPI_Comm comm); |
||||
*/ |
||||
|
||||
MPI_Gatherv(retbuf, chunkSize, MPI_INT, matches, sendcounts, offsets, MPI_INT, 0, MPI_COMM_WORLD); |
||||
|
||||
if(rank == 0) |
||||
{ |
||||
printf("Vysledok vyhladavania pre retazec '%s':\n", needle); |
||||
|
||||
for(i = 0; i < HAYSTACKSIZE; i++) |
||||
{ |
||||
if(matches[i] == 1) |
||||
printf("[%d] %s\n", i, haystack[i]); |
||||
} |
||||
} |
||||
|
||||
MPI_Finalize(); |
||||
return (EXIT_SUCCESS); |
||||
} |
Binary file not shown.
@ -0,0 +1,79 @@ |
||||
\documentclass[fleqn,12pt]{article} |
||||
\usepackage[utf8]{inputenc} |
||||
\usepackage[slovak]{babel} |
||||
\usepackage{amsmath} |
||||
% --------------------------------------------------------------- |
||||
% pri pouziti pdftex-u s obrazkami jpg pouzit nasledujuci riadok |
||||
\usepackage[pdftex]{color,graphicx} |
||||
% pri pouziti cslatex-u s obrazkami eps namiesto predosleho riadku |
||||
% pouzit nasledujuci riadok |
||||
% \usepackage{color,graphicx} |
||||
% ---------------------------------------------------------------- |
||||
\setlength\textheight{200mm} |
||||
\setlength\textwidth{160mm} |
||||
\oddsidemargin=0mm\evensidemargin=0mm |
||||
\sloppy |
||||
\begin{document} |
||||
\thispagestyle{empty} |
||||
\begin{center} |
||||
{\Large Katedra počítačov a informatiky FEI TU v Košiciach} |
||||
\end{center} |
||||
\vfill |
||||
\begin{center} |
||||
{\huge Paralelné programovanie} |
||||
\end{center} |
||||
|
||||
\begin{center} |
||||
{\large 2015/2016} |
||||
\end{center} |
||||
\vfill |
||||
|
||||
\begin{center} |
||||
{\large Peter Babič} \hfill {\large Počítačové modelovanie} |
||||
\end{center} |
||||
|
||||
\clearpage |
||||
|
||||
\setcounter{section}{3} |
||||
\section{Paralelné vyhľadanie reťazca v neutriedenej tabuľke} |
||||
|
||||
\subsection*{Paralelná dekompozícia problému} |
||||
|
||||
Neutriedená tabuľka slov (krstných mien mesiaca Január) je uložená do poľa. Master proces rozdelí tabuľku mien na približne rovnaké časti, ktoré sú rozoslané medzi dostupné procesy pomocou \verb|MPI_Scatterv|. Hľadaný výraz je zdieľadný medzi všetkými procesmi pomocou \verb|MPI_Bcast|. Mená, v ktorých sa hľadaný výraz vyskutuje sú získané master procesom pomocou \verb|MPI_Gatherv|. Po ukončení behu paralelnej časti algoritmu master proces vypíše výsledok na štandardný výstup. |
||||
|
||||
Program využije všetky dostupné vlákna. |
||||
|
||||
\subsection*{Spôsob využitia nových komunikátorov} |
||||
|
||||
Všetky procesy zahrnuté do riešenia sú súčasťou globálneho komunikátora \verb|MPI_COMM_WORLD|. |
||||
|
||||
\subsection*{Spôsob využitia topológie procesov} |
||||
|
||||
V riešení nepoli využité virtuálne topológie. |
||||
|
||||
\subsection*{Hodnotenie efektívnosti výpočtu} |
||||
|
||||
Experimentálne namerané výsledky behu sekvenčného a paralelného algoritmu na processore Intel Core2 Duo Processor T9500 so 4 GB priľahlej RAM sú uvedené v tabuľke \ref{t:efecti}. Vstupom je pole obsahujúce 30 slov. |
||||
|
||||
\begin{table}[h!] |
||||
\caption{Porovnanie sekvenčného (1) a paralelného (2+) algoritmu} |
||||
\label{t:efecti} |
||||
\centering |
||||
\begin{tabular}{l|rrrrrrrr} |
||||
\hline |
||||
Počet procesov & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\ |
||||
\hline |
||||
Čas v ms & 0.11 & 0.12 & 0.14 & 0.15 & 0.18 & 0.21 & 0.22 & 0.27 \\ |
||||
\hline |
||||
Vyťaženie CPU v \% & 74 & 87 & 92 & 95 & 98 & 110 & 128 & 140 \\ |
||||
\hline |
||||
\end{tabular} |
||||
\end{table} |
||||
|
||||
Výsledky naznačujú, že použitá implementácia algoritmu dosahuje najlepšie výsledky v sekvenčnej forme. S nárastom vlákien sa zvyšuje čas aj vyťaženosť CPU. |
||||
|
||||
|
||||
|
||||
\end{document} |
||||
|
||||
|
Loading…
Reference in new issue