c - MPI_Gather to char array -
i'm trying write elaborate hello world program using mpi. idea root process sends message other processes, in turn sends message root containing process id. want send several character arrays root process , print it.
the problem don't manage collect messages variable in root process. compile errors or unwanted output. feel i'm making basic error in manipulating char arrays ... i'd glad if me out!
the code far:
// hello world using mpi #include <stdio.h> #include <math.h> #include <mpi.h> #include <string.h> int main(int argc, char **argv){ // define useful global constants int my_id, root_process, num_procs, rc; mpi_status status; char[] final_message; // <---- how should define final message? can in way? // let master process process 0 root_process = 0; // now, replicate process create parallel processes: rc = mpi_init(&argc, &argv); // find out process id , how many processes started: rc = mpi_comm_rank(mpi_comm_world, &my_id); rc = mpi_comm_size(mpi_comm_world, &num_procs); // broadcast master's message slaves char message1[20]; strcpy(message1, "hello, world!"); rc = mpi_bcast(&message1, 14, mpi_char, root_process, mpi_comm_world); // test: each slave displays message1 along process is. //printf("i'm process %i, message is: %.13s \n",my_id,message1); // master collects return message each slave, including process id. char message2[31]; char message22[2]; strcpy(message2, "master, i'm process number ");// %i \n",&my_id ); //28 sprintf(message22,"%d\n",my_id); strcat(message2,message22); rc = mpi_gather(message2, 1, mpi_char, final_message, 1, mpi_char, root_process, mpi_comm_world); // display message collected master. printf(final_message); // close down process rc = mpi_finalize(); }
Comments
Post a Comment