1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- #include <mpi.h>
- #include <string>
- #include <cstring>
- #include <functional>
- #include <iostream>
- int main(int argc, char *argv[]) {
- int rank, size;
- char proc_name[2048];
- int proc_name_len;
- MPI_Comm node, head;
- MPI_Init(&argc, &argv);
- MPI_Comm_rank(MPI_COMM_WORLD, &rank);
- MPI_Comm_size(MPI_COMM_WORLD, &size);
- // node comm
- MPI_Get_processor_name(proc_name, &proc_name_len);
- std::string proc_name_str(proc_name, proc_name_len);
- int proc_name_hash = std::hash<std::string>{}(proc_name_str);
- // std::cout << proc_name_str << ", " << proc_name_hash << std::endl;
- MPI_Comm_split(MPI_COMM_WORLD, proc_name_hash, -1, &node);
- int node_rank, node_size;
- MPI_Comm_rank(node, &node_rank);
- MPI_Comm_size(node, &node_size);
- // head comm
- int head_rank, head_size;
- int headornot = node_rank == 0;
- MPI_Comm_split(MPI_COMM_WORLD, headornot, -1, &head);
- MPI_Comm_rank(head, &head_rank);
- MPI_Comm_size(head, &head_size);
- //std::cout << "rank:" << rank << ", head_rank:" << head_rank << ", head_size:" << head_size << std::endl;
- char message[2048] = "default string";
- printf("rank:%d, is_head:%d, node:%s, node_rank:%d, msg:%s\n", rank, headornot, proc_name, node_rank, message);
- // bcast in head
- if (head_rank == 0 && headornot) {
- strcpy(message, "hello from root");
- }
- if (headornot) {
- MPI_Bcast((void*) message, 16, MPI_CHAR, 0, head);
- }
- // bcast in node
- MPI_Bcast((void*) message, 16, MPI_CHAR, 0, node);
- printf("rank:%d, is_head:%d, node:%s, node_rank:%d, msg:%s\n", rank, headornot, proc_name, node_rank, message);
- MPI_Finalize();
- }
|