I want to generate an string with each process and then gather everything. But the strings created in each process are created by appending ints and chars.
I'm still not able to gather everything correctly. I can print all the partial strings one by one, but If I try to print the rcv_string, I only get one partial string or maybe a Segmentation Fault.
I've tried putting zeros at the end of strings with memset, reserving memory for the strings dynamically and statically, ... But I don't find the way.
It would be great if someone knew how to inizialize the strings and do the gather properly for achieving the objective.
int main(int argc, char *argv[]) {
int rank;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
char *string; // ????????????
char *rcv_string; // ????????????
if (rank == 0) {
sprintf(string+strlen(string), "%dr%dg%db%dl\n",255,255,255,0);
}
else if (rank == 1) {
sprintf(string+strlen(string), "%dr%dg%db%dl\n",255,255,255,0);
}
else if (rank == 2) {
sprintf(string+strlen(string), "%dr%dg%db%dl\n",255,255,255,0);
}
else if (rank == 3) {
sprintf(string+strlen(string), "%dr%dg%db%dl\n",255,255,255,0);
}
else if (rank == 4) {
sprintf(string+strlen(string), "%dr%dg%db%dl\n",255,255,255,0);
}
else if (rank == 5) {
sprintf(string+strlen(string), "%dr%dg%db%dl\n",255,255,255,0);
}
MPI_Gather(string,???,MPI_CHAR,rcv_string,???,MPI_CHAR,0,MPI_COMM_WORLD);
if (rank == 0) {
printf("%s",rcv_string);
}
MPI_Finalize();
return 0;
}