MPI_Bcast廣播函式, 這個是一個很有趣的功能. 將要變更的資料變數及內容傳送給其它的接收
/* C Example */
#include <mpi.h>
#include <stdio.h>
int main (int argc, char* argv[]){
int rank, size;
double t1;
void *v;
int flag;
int mytable[1] = { 0 };
MPI_Init (&argc, &argv); /* starts MPI */
MPI_Comm_rank (MPI_COMM_WORLD, &rank); /* get current process id */
MPI_Comm_size (MPI_COMM_WORLD, &size); /* get number of processes */
t1 = MPI_Wtime();
MPI_Attr_get( MPI_COMM_WORLD, MPI_WTIME_IS_GLOBAL, &v, &flag );
printf( "Hello world from process %d of %d, time = %f, global time = %d\n", rank, size, MPI_Wtime() - t1, flag);
mytable[0] = rank + 100;
MPI_Barrier(MPI_COMM_WORLD);
MPI_Bcast(&mytable[0], 1, MPI_INT, atoi(argv[1]), MPI_COMM_WORLD);
printf("%d/%d: mytable[0] = %d\n", rank, size, mytable[0]);
MPI_Barrier(MPI_COMM_WORLD);
MPI_Finalize();
return 0;
}
mpirun -np 5 ./bcast 0執行結果1
Starting MPI_Init...
Starting MPI_Init...
Starting MPI_Init...
Starting MPI_Init...
Starting MPI_Init...
[0] Ending MPI_Init
[0] Starting MPI_Comm_rank...
[0] Ending MPI_Comm_rank
[0] Starting MPI_Comm_size...
[0] Ending MPI_Comm_size
[0] Starting MPI_Attr_get...
[0] Ending MPI_Attr_get
Hello world from process 0 of 5, time = 0.000011, global time = 1
[0] Starting MPI_Barrier...
[0] Ending MPI_Barrier
[0] Starting MPI_Bcast...
[0] Starting MPI_Bcast with count = 1
[0] Ending MPI_Bcast
0/5: mytable[0] = 100
[0] Starting MPI_Barrier...
[0] Ending MPI_Barrier
[0] Starting MPI_Finalize...
[0] Ending MPI_Finalize
[1] Ending MPI_Init
[1] Starting MPI_Comm_rank...
[1] Ending MPI_Comm_rank
[1] Starting MPI_Comm_size...
[1] Ending MPI_Comm_size
[1] Starting MPI_Attr_get...
[1] Ending MPI_Attr_get
Hello world from process 1 of 5, time = 0.000012, global time = 1
[1] Starting MPI_Barrier...
[1] Ending MPI_Barrier
[1] Starting MPI_Bcast...
[1] Starting MPI_Bcast with count = 1
[1] Ending MPI_Bcast
1/5: mytable[0] = 100
[1] Starting MPI_Barrier...
[1] Ending MPI_Barrier
[1] Starting MPI_Finalize...
[1] Ending MPI_Finalize
[2] Ending MPI_Init
[2] Starting MPI_Comm_rank...
[2] Ending MPI_Comm_rank
[2] Starting MPI_Comm_size...
[2] Ending MPI_Comm_size
[2] Starting MPI_Attr_get...
[2] Ending MPI_Attr_get
Hello world from process 2 of 5, time = 0.000012, global time = 1
[2] Starting MPI_Barrier...
[2] Ending MPI_Barrier
[2] Starting MPI_Bcast...
[2] Starting MPI_Bcast with count = 1
[2] Ending MPI_Bcast
2/5: mytable[0] = 100
[2] Starting MPI_Barrier...
[2] Ending MPI_Barrier
[2] Starting MPI_Finalize...
[3] Ending MPI_Init
[3] Starting MPI_Comm_rank...
[3] Ending MPI_Comm_rank
[3] Starting MPI_Comm_size...
[3] Ending MPI_Comm_size
[3] Starting MPI_Attr_get...
[3] Ending MPI_Attr_get
Hello world from process 3 of 5, time = 0.000010, global time = 1
[3] Starting MPI_Barrier...
[3] Ending MPI_Barrier
[3] Starting MPI_Bcast...
[3] Starting MPI_Bcast with count = 1
[3] Ending MPI_Bcast
3/5: mytable[0] = 100
[3] Starting MPI_Barrier...
[3] Ending MPI_Barrier
[3] Starting MPI_Finalize...
[4] Ending MPI_Init
[4] Starting MPI_Comm_rank...
[4] Ending MPI_Comm_rank
[4] Starting MPI_Comm_size...
[4] Ending MPI_Comm_size
[4] Starting MPI_Attr_get...
[4] Ending MPI_Attr_get
Hello world from process 4 of 5, time = 0.000009, global time = 1
[4] Starting MPI_Barrier...
[4] Ending MPI_Barrier
[4] Starting MPI_Bcast...
[4] Starting MPI_Bcast with count = 1
[4] Ending MPI_Bcast
4/5: mytable[0] = 100
[4] Starting MPI_Barrier...
[4] Ending MPI_Barrier
[4] Starting MPI_Finalize...
[2] Ending MPI_Finalize
[3] Ending MPI_Finalize
[4] Ending MPI_Finalize
mpirun -np 5 ./bcast 1執行結果2
Starting MPI_Init...
Starting MPI_Init...
Starting MPI_Init...
Starting MPI_Init...
Starting MPI_Init...
[0] Ending MPI_Init
[0] Starting MPI_Comm_rank...
[0] Ending MPI_Comm_rank
[0] Starting MPI_Comm_size...
[0] Ending MPI_Comm_size
[0] Starting MPI_Attr_get...
[0] Ending MPI_Attr_get
Hello world from process 0 of 5, time = 0.000017, global time = 1
[0] Starting MPI_Barrier...
[0] Ending MPI_Barrier
[0] Starting MPI_Bcast...
[0] Starting MPI_Bcast with count = 1
[0] Ending MPI_Bcast
0/5: mytable[0] = 101
[0] Starting MPI_Barrier...
[0] Ending MPI_Barrier
[0] Starting MPI_Finalize...
[1] Ending MPI_Init
[1] Starting MPI_Comm_rank...
[1] Ending MPI_Comm_rank
[1] Starting MPI_Comm_size...
[1] Ending MPI_Comm_size
[1] Starting MPI_Attr_get...
[1] Ending MPI_Attr_get
Hello world from process 1 of 5, time = 0.000010, global time = 1
[1] Starting MPI_Barrier...
[1] Ending MPI_Barrier
[1] Starting MPI_Bcast...
[1] Starting MPI_Bcast with count = 1
[1] Ending MPI_Bcast
1/5: mytable[0] = 101
[1] Starting MPI_Barrier...
[1] Ending MPI_Barrier
[1] Starting MPI_Finalize...
[1] Ending MPI_Finalize
[2] Ending MPI_Init
[2] Starting MPI_Comm_rank...
[2] Ending MPI_Comm_rank
[2] Starting MPI_Comm_size...
[2] Ending MPI_Comm_size
[2] Starting MPI_Attr_get...
[2] Ending MPI_Attr_get
Hello world from process 2 of 5, time = 0.000008, global time = 1
[2] Starting MPI_Barrier...
[2] Ending MPI_Barrier
[2] Starting MPI_Bcast...
[2] Starting MPI_Bcast with count = 1
[2] Ending MPI_Bcast
2/5: mytable[0] = 101
[2] Starting MPI_Barrier...
[2] Ending MPI_Barrier
[2] Starting MPI_Finalize...
[3] Ending MPI_Init
[3] Starting MPI_Comm_rank...
[3] Ending MPI_Comm_rank
[3] Starting MPI_Comm_size...
[3] Ending MPI_Comm_size
[3] Starting MPI_Attr_get...
[3] Ending MPI_Attr_get
Hello world from process 3 of 5, time = 0.000008, global time = 1
[3] Starting MPI_Barrier...
[3] Ending MPI_Barrier
[3] Starting MPI_Bcast...
[3] Starting MPI_Bcast with count = 1
[3] Ending MPI_Bcast
3/5: mytable[0] = 101
[3] Starting MPI_Barrier...
[3] Ending MPI_Barrier
[3] Starting MPI_Finalize...
[4] Ending MPI_Init
[4] Starting MPI_Comm_rank...
[4] Ending MPI_Comm_rank
[4] Starting MPI_Comm_size...
[4] Ending MPI_Comm_size
[4] Starting MPI_Attr_get...
[4] Ending MPI_Attr_get
Hello world from process 4 of 5, time = 0.000009, global time = 1
[4] Starting MPI_Barrier...
[4] Ending MPI_Barrier
[4] Starting MPI_Bcast...
[4] Starting MPI_Bcast with count = 1
[4] Ending MPI_Bcast
4/5: mytable[0] = 101
[4] Starting MPI_Barrier...
[4] Ending MPI_Barrier
[4] Starting MPI_Finalize...
[0] Ending MPI_Finalize
[2] Ending MPI_Finalize
[3] Ending MPI_Finalize
[4] Ending MPI_Finalize
沒有留言:
張貼留言