Skip to content

Instantly share code, notes, and snippets.

@marcingolenia
Last active April 7, 2016 22:12
Show Gist options
  • Save marcingolenia/6f4360fb69432a7c5738814b323e723f to your computer and use it in GitHub Desktop.
Save marcingolenia/6f4360fb69432a7c5738814b323e723f to your computer and use it in GitHub Desktop.
typedef struct {
char fileName[1000];
int array[1000];
size_t used;
size_t size;
} FileFindings;
//jakiś tam kod...
int main( int argc, char ** argv )
{
FileFindings* fileFindings = NULL;
char keyWord[100];
char userPath[1000];
int filesCount = 0;
//Create a type for struct FileFindings
const int nitems = 4;
int blocklen[4] = {1000,1000,1,1};
MPI_Datatype types[4] = {MPI_CHAR, MPI_INT, MPI_INT, MPI_INT};
MPI_Datatype mpi_filefinding_type;
MPI_Aint disp[4];
//MPI INIT
int num_elements_per_proc;
int size, rank;
double time_initial,time_current,time;
MPI_Init(&argc, &argv);
disp[0] = offsetof(FileFindings, fileName);
disp[1] = offsetof(FileFindings, array);
disp[2] = offsetof(FileFindings, used);
disp[3] = offsetof(FileFindings, size);
MPI_Type_create_struct(nitems, blocklen, disp, types, &mpi_filefinding_type);
MPI_Type_commit(&mpi_filefinding_type);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (size < 2) {
printf("Requires at least two processes.\n");
exit(-1);
}
if(rank == 0) {
printf("Enter path to directory with files (ended with '/')\n");
scanf(" %[^\n]s",userPath);
printf("Keyword to search (max 100 characeters): ");
scanf(" %[^\n]s",keyWord);
if(strlen(userPath) < 2)
{
strcpy(userPath, "/home/marcin/Education/files/");
}
filesCount = getFilesCount(userPath);
fileFindings = fillFilePaths(userPath, filesCount);
if (argc != 2) {
num_elements_per_proc = filesCount;
}
else {
num_elements_per_proc = atoi(argv[1]);
}
printf("%d\n",filesCount);
printf("%d\n",num_elements_per_proc);
}
// For each process, create a buffer that will hold a subset of the entire
FileFindings *subfileFindings = malloc(sizeof(FileFindings) * num_elements_per_proc);
//Send necessary variables into all threads
MPI_Bcast(&userPath, 1000, MPI_CHAR, 0, MPI_COMM_WORLD);
MPI_Bcast(&keyWord, 100, MPI_CHAR, 0, MPI_COMM_WORLD);
MPI_Bcast(&T[0], 100, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(&filesCount, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Scatter(fileFindings, num_elements_per_proc, mpi_filefinding_type, subfileFindings,
num_elements_per_proc, mpi_filefinding_type, 0, MPI_COMM_WORLD);
printf("%s", subfileFindings[0].fileName); //KONIEC, wyświetla się dobrze tylko dla 1 procesu - strzelam że dal roota :(
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment