#include #include #include void master(); void slave(); int main(int argc, char* argv[]){ int myid; int numprocs; int namelen; char processor_name[MPI_MAX_PROCESSOR_NAME]; int headnode=0; std::string sharedmemorylocation; std::string proc_name_str; MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD,&numprocs); MPI_Comm_rank(MPI_COMM_WORLD,&myid); MPI_Get_processor_name(processor_name,&namelen); proc_name_str = processor_name; if (myid == 0){ master(); } else{ slave(); } MPI_Finalize(); } void master(){ int numprocs; int rank; int myid; MPI_Comm_rank(MPI_COMM_WORLD,&myid); MPI_Status status; /* Find out how many processes there are in the default communicator */ MPI_Comm_size(MPI_COMM_WORLD, &numprocs); std::cout << "I am master on process: " << myid << "\n"; std::cout << "Creating a vector to broadcast to other cores.\n"; int numbytes; std::cout << "Enter the number of bytes to send: "; std::cin >> numbytes; std::string to_send_str = ""; for (int i = 0; i < numbytes; ++i){ to_send_str.append("a"); } // std::cout << to_send_str << "\n"; char* to_send = (char*)(to_send_str.c_str()); int vectorlengths[1] = {0}; vectorlengths[0] = to_send_str.size(); std::cout << "vectorlengths[0] = " << vectorlengths[0] << "\n"; // send the length of the string to be sent ahead of time to the other nodes // otherwise they won't be able to get the information correctly // as when the data is received, size of the vector being passed is needed MPI_Bcast(&vectorlengths,1, MPI_INT,0, MPI_COMM_WORLD); MPI_Bcast(&to_send[0], to_send_str.size(), MPI_CHAR, 0, MPI_COMM_WORLD); return; } void slave(){ int numrocs; int rank; int myid; MPI_Comm_rank(MPI_COMM_WORLD,&myid); MPI_Status status; std::cout << "I am a slave on process: " << myid << "\n"; int vectorlengths[1] = {0}; MPI_Bcast(&vectorlengths, 1, MPI_INT, 0, MPI_COMM_WORLD); // std::cout << "vectorlengths[0] = " << vectorlengths[0] << "\n"; char sent_chars[vectorlengths[0]]; MPI_Bcast(&sent_chars, vectorlengths[0], MPI_CHAR, 0, MPI_COMM_WORLD); // std::cout << "Sent a char array of size: " << vectorlengths[0] // << "\nYay!\n"; if (myid == 1){ std::cout << "Sent the following character array: \n"; std::cout << sent_chars << "\n" << "which is of size: "; std::cout << "vectorlengths[0] = " << vectorlengths[0] << "\n"; } return; }