From 04c1b04c2de997f010edee95e290421fc0c712ff Mon Sep 17 00:00:00 2001 From: pyvawtjzl Date: Wed, 12 Mar 2025 09:55:48 +0800 Subject: [PATCH] ADD file via upload --- mpi_hello.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 mpi_hello.c diff --git a/mpi_hello.c b/mpi_hello.c new file mode 100644 index 0000000..d4ae9b9 --- /dev/null +++ b/mpi_hello.c @@ -0,0 +1,76 @@ +/* File: + * mpi_hello.c + * + * Purpose: + * A "hello,world" program that uses MPI + * + * Compile: + * mpicc -g -Wall -std=C99 -o mpi_hello mpi_hello.c + * Usage: + * mpiexec -n ./mpi_hello + * + * Input: + * None + * Output: + * A greeting from each process + * + * Algorithm: + * Each process sends a message to process 0, which prints + * the messages it has received, as well as its own message. + * + * IPP2 Section 3.1 (pp. 90 and ff.) + */ +#include +#include /* For strlen */ +#include /* For MPI functions, etc */ + +const int MAX_STRING = 100; + +int main(void) { + char greeting[MAX_STRING]; /* String storing message */ + int comm_sz; /* Number of processes */ + int my_rank; /* My process rank */ + + /* Start up MPI */ + /* 第1处代码补全开始 */ + + /* 第1处代码补全结束 */ + + /* Get the number of processes */ + /* 第2处代码补全开始 */ + + /* 第2处代码补全结束 */ + + /* Get my rank among all the processes */ + /* 第3处代码补全开始 */ + + /* 第3处代码补全结束 */ + + if (my_rank != 0) { + /* Create message */ + sprintf(greeting, "Greetings from process %d of %d!", + my_rank, comm_sz); + /* Send message to process 0 */ + /* 第4处代码补全开始 */ + + /* 第4处代码补全结束 */ + } else { + /* Print my message */ + printf("Greetings from process %d of %d!\n", my_rank, comm_sz); + for (int q = 1; q < comm_sz; q++) { + /* Receive message from process q */ + /* 第5处代码补全开始 */ + + /* 第5处代码补全结束 */ + /* Print message from process q */ + printf("%s\n", greeting); + } + } + + /* Shut down MPI */ + /* 第6处代码补全开始 */ + + /* 第6处代码补全结束 */ + + return 0; +} /* main */