Witam, w jaki sposób w tym kodzie mogę pobierać słowo zapisywane w buforze przeznaczonym dla poszczególnego procesu, skoro nie wiem jakiej długości jest to wyraz. Miejsce jest za komentowane w kodzie...
Jedyne co przychodzi mi na myśl to pobieranie po znaku do wystąpienia pustego znaku(w tym przypadku '\n') zapisywanie tych znaków do nowej tablicy i przekazywanie tej tablicy dopiero do if_anagram. Czy istnieje jakiś inny prostszy sposób ?
#include <stdio.h>
#include <mpi.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
#include <conio.h>
bool if_anagram(char *a, char *b)
{
int dl1 = strlen(a), dl2 = strlen(b);
if(dl1!=dl2) return false;
if(strcmp(a,b) == 0) return false;
int licz[0x100]={};
for(int i=0;i<dl1;i++)
licz[(unsigned char) a[i]]++;
for(int i=0;i<dl1;i++)
licz[(unsigned char) b[i]]--;
for(int i=0;i<256;i++)
if(licz[i]!=0)
return false;
return true;
}
void parprocess(MPI_File *in, char *a, const int rank, const int size, const int overlap) {
MPI_Offset globalstart;
int mysize;
char *chunk;
/* read in relevant chunk of file into "chunk",
* which starts at location in the file globalstart
* and has size mysize
*/
{
MPI_Offset globalend;
MPI_Offset filesize;
// figure out who reads what
MPI_File_get_size(*in, &filesize);
filesize--; // get rid of text file eof
mysize = filesize/size;
globalstart = rank * mysize;
globalend = globalstart + mysize - 1;
if (rank == size-1) globalend = filesize-1;
//add overlap to the end of everyone's chunk except last proc...
if (rank != size-1)
globalend += overlap;
mysize = globalend - globalstart + 1;
// allocate memory
chunk = malloc( (mysize + 1)*sizeof(char));
// everyone reads in their part
MPI_File_read_at_all(*in, globalstart, chunk, mysize, MPI_CHAR, MPI_STATUS_IGNORE);
chunk[mysize] = '\0';
}
/*
* everyone calculate what their start and end *really* are by going
* from the first newline after start to the first newline after the
* overlap region starts (eg, after end - overlap + 1)
*/
int locstart=0, locend=mysize-1;
if (rank != 0) {
while(chunk[locstart] != '\n') locstart++;
locstart++;
}
if (rank != size-1) {
locend-=overlap;
while(chunk[locend] != '\n') locend++;
}
mysize = locend-locstart+1;
// "Process" our chunk by replacing non-space characters with '1' for
// rank 1, '2' for rank 2, etc...
//char b[101];
//int j=0;
for (int i=locstart; i<=locend; i++) {
char c = chunk[i];
//TEN FRAGMENT KODU
}
return;
}
int main(int argc, char** argv) {
MPI_File in;
int rank, size;
int ierr;
const int overlap = 100;
char a[101];
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
if (argc != 3) {
if (rank == 0) fprintf(stderr, "Usage: %s infilename word\n", argv[0]);
MPI_Finalize();
exit(1);
}
ierr = MPI_File_open(MPI_COMM_WORLD, argv[1], MPI_MODE_RDONLY, MPI_INFO_NULL, &in);
if (ierr) {
if (rank == 0) fprintf(stderr, "%s: Couldn't open file %s\n", argv[0], argv[1]);
MPI_Finalize();
exit(2);
}
strcpy(a, argv[2]);
/* if (ierr) {
if (rank == 0) fprintf(stderr, "%s: Put a word to share anagram! \n");
MPI_Finalize();
exit(3);
}*/
// printf("Word: %s \n", a);
parprocess(&in, a, rank, size, overlap);
MPI_File_close(&in);
MPI_Finalize();
return 0;
}