I'm having difficulties passing the right amount of characters to other processes using scatter. I'm trying to open a file which contains 6 file names and splitting it up and sending a file name to each process. I would look to send two file names to each process and have three processes going at once, but right now I'm just trying to do one. Below is my code, any suggestions you could give I'd greatly appreciate, I've been fiddling with these characters and scattering them for awhile now and am getting pretty frustrated.
Here is what is actually in the txt document I'm reading in right away:
data/file0.txt
data/file1.txt
data/file2.txt
data/file3.txt
data/file4.txt
data/file5.txt
Somewhere it isn't getting that last 't'
Here is the output:
data/file2.tx
data/file3.tx
data/file4.tx
data/file5.tx
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <mpi.h>
int main(int argc, char **argv){
int p;
int id;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &p);
MPI_Comm_rank(MPI_COMM_WORLD, &id);
if(id==0) { // Process 0
FILE *infile;
infile = fopen("data/data/filelist.txt", "r");
if (infile == NULL) {
printf("I couldn't open file.\n");
}
int ch = 0;
char fileList[84];
int sizeCount = 0;
while(ch != EOF){
//while(ch != '\n'){
fileList[sizeCount] = ch;
ch = getc(infile);
//printf("%c",ch);
sizeCount++;
//}
}
fclose(infile);
sizeCount -= 6;
int lineSize = sizeCount/6;
printf("sizeCount: %d\n",sizeCount);
printf("lineSize: %d\n",lineSize);
char files[5][(sizeCount/6)];
int counter = 0;
for(int i = 0; i < 6; i++){
for(int j = 0; j <= lineSize; j++){
files[i][j] = fileList[counter];
//printf("%c", files[i][j]);
counter++;
}
}
printf("counter:%d", counter);
printf("\n");
char receiveArray[lineSize*2];
MPI_Scatter(files,lineSize*2,MPI_CHAR,receiveArray,lineSize*2,MPI_CHAR,0,MPI_COMM_WORLD);
//do stuff as process 0
}else { // All other processes
int id;
MPI_Comm_rank(MPI_COMM_WORLD, &id);
char receiveArray[28];
MPI_Scatter(NULL,28,MPI_CHAR,receiveArray,28,MPI_CHAR,0,MPI_COMM_WORLD);
//
//if(id==1) sleep(1);
//else if(id==2) sleep(2);
//else if(id==3) sleep(3);
for(int i = 0; i <= sizeof(receiveArray); i++){
printf("%c",receiveArray[i]);
}
printf("\n");
//
//char fileName[23] = "data/data/filelist0.txt";
// for(int i = 0; i < sizeof(fileName); i++){
// printf("%c", receiveArray[i]);
// }
printf("\n");
/*
FILE *infile;
infile = fopen(fileName, "r");
if (infile == NULL) {
printf("\nI couldn't open file.\n");
}
int ch = 0;
while(ch != EOF){
ch = getc(infile);
printf("%c",ch);
}
*/
}
MPI_Finalize();
}