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.
142 lines
4.2 KiB
142 lines
4.2 KiB
#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);
|
|
}
|
|
|