2015年11月28日 星期六

MPI_Bcast

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

沒有留言:

一個小故事讓我們明白資金流通的意義

“又是炎熱小鎮慵懶的一天。太陽高掛,街道無人,每個人都債台高築,靠信用度日。這時,從外地來了一位有錢的旅客,他進了一家旅館,拿出一張1000 元鈔票放在櫃檯,說想先看看房間,挑一間合適的過夜,就在此人上樓的時候---- 店主抓了這張1000 元鈔,跑到隔壁屠戶那裡支付了他欠的肉錢...