Changes between Version 9 and Version 10 of YM_Course_2009/Lab8


Ignore:
Timestamp:
Jul 5, 2009, 3:31:27 PM (15 years ago)
Author:
jazz
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • YM_Course_2009/Lab8

    v9 v10  
    109109 * 用 mpicc 編譯 test1.c
    110110{{{
    111 ym000@bio001:~$ mpicc -o test1 test1.c
     111ym24@bio001:~$ mpicc -o test1 test1.c
    112112}}}
    113113 * 用 mpiexec 執行 test1 程式
    114114{{{
    115 ym000@bio001:~$ mpiexec -n 1 ./test1
     115ym24@bio001:~$ mpiexec -n 1 ./test1
    116116This is machine 0 of 1 name = bio001
    117 ym000@bio001:~$ mpiexec -n 12 ./test1
    118 }}}
     117ym24@bio001:~$ mpiexec -n 12 ./test1
     118}}}
     119 * here is test2.c
     120{{{
     121ym24@bio001:~$ cat << EOF > test2.c
     122#include <mpi.h>
     123#include <stdio.h>
     124main(int argc,char **argv) {
     125  int n, myrank, numprocs;
     126  MPI_Status status;
     127  MPI_Init(&argc,&argv);
     128  MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
     129  MPI_Comm_rank(MPI_COMM_WORLD,&myrank);
     130
     131  /* node 0 will send the first message */
     132  if(myrank == 0) {
     133    n = myrank;
     134    MPI_Send(&n, 1, MPI_INT, 1, 99, MPI_COMM_WORLD);
     135    printf("[Ndde %d]「%d」 >> [Node %d]\n\n", myrank, n, myrank+1);
     136  }
     137
     138  /* node 1 to node n-2 will send message to the next node */
     139  if(myrank>0 && myrank<numprocs-1) {
     140    MPI_Recv(&n, 1, MPI_INT, myrank-1, 99, MPI_COMM_WORLD, &status);
     141    printf("[Node %d] << 「%d」[Node %d]\n", myrank, n, status.MPI_SOURCE);
     142    n = myrank; MPI_Send(&n, 1, MPI_INT, myrank+1, 99, MPI_COMM_WORLD);
     143    printf("[Ndde %d]「%d」 >> [Node %d]\n\n", myrank, n, myrank+1);
     144  }
     145
     146 /* the final node n-1 will not send any message but receive*/
     147  if(myrank==numprocs-1) {
     148    MPI_Recv(&n, 1, MPI_INT, myrank-1, 99, MPI_COMM_WORLD, &status);
     149    printf("[Node %d] << 「%d」[Node %d]\n", myrank, n, status.MPI_SOURCE);
     150    }
     151
     152  MPI_Finalize();
     153}
     154EOF
     155}}}
     156 * 用 mpicc 編譯 test2.c
     157{{{
     158ym24@bio001:~$ mpicc -o test2 test2.c
     159}}}
     160 * 用 mpiexec 執行 test2 程式
     161{{{
     162ym24@bio001:~$ mpiexec -n 12 ./test2
     163}}}
     164 * here is test3.c
     165{{{
     166ym24@bio001:~$ cat << EOF > test3.c
     167/* Program:
     168 *   每個 node 將訊息傳送給 node 0,由,node 0 統一印出
     169 * History:
     170 *   2008-06-12 BETA
     171 *   2008-06-17 更改顯示方式,並增加註解
     172 */
     173
     174#include <stdio.h>
     175#include <mpi.h>
     176#include <string.h>
     177
     178main(int argc, char **argv)
     179{
     180  int myrank, i, numprocs;
     181  char message[20];
     182  MPI_Status status;
     183  MPI_Init(&argc, &argv);
     184  MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
     185  MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
     186
     187  /* Node 0 will do the following */
     188  if(myrank == 0)
     189  {
     190    /* receive messages from other nodes */
     191    for(i = 1; i < numprocs; i++)
     192    {
     193      MPI_Recv(message, 20, MPI_CHAR, i, 99, MPI_COMM_WORLD, &status);
     194      printf("[Node 0] << 「%s」[Node %d] \n", message, status.MPI_SOURCE);
     195    }
     196  }
     197
     198  /* other Nodes will do the following */
     199  if(myrank != 0)
     200  {
     201    /* send node's rank to Node 0 */
     202    sprintf(message, "[%d]", myrank);
     203    MPI_Send(message, 20, MPI_CHAR, 0, 99, MPI_COMM_WORLD);
     204    printf("[Node %d]「%s」 >> [Node 0]\n", myrank, message);
     205  }
     206  MPI_Finalize();
     207}
     208EOF
     209}}}
     210 * 用 mpicc 編譯 test3.c
     211{{{
     212ym24@bio001:~$ mpicc -o test2 test3.c
     213}}}
     214 * 用 mpiexec 執行 test3 程式
     215{{{
     216ym24@bio001:~$ mpiexec -n 12 ./test3
     217}}}
     218 * here is test4
     219{{{
     220ym24@bio001:~$ cat << EOF > test4.c
     221/* Program:
     222 *   mpich_example 內建範例,計算 pi 。
     223 * History:
     224 *   2008-04-11 BETA
     225 *   2008-06-19 增加可重複輸入欲計算之精準度
     226 *   2008-06-23 加入 MPI_Barrier 以確保每個 node 在接受 n 後才執行
     227 */
     228
     229#include "mpi.h"
     230#include <stdio.h>
     231#include <math.h>
     232#include <time.h>
     233
     234double f( double );
     235double f( double a )
     236{
     237    return (4.0 / (1.0 + a*a));
     238}
     239
     240int main( int argc, char *argv[])
     241{
     242    int done = 0, n, myid, numprocs, i=0;
     243    double PI25DT = 3.141592653589793238462643;
     244    double mypi, pi, h, sum, x;
     245    double startwtime = 0.0, endwtime;
     246    int  namelen;
     247    char processor_name[MPI_MAX_PROCESSOR_NAME];
     248    MPI_Init(&argc,&argv);
     249    MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
     250    MPI_Comm_rank(MPI_COMM_WORLD,&myid);
     251    MPI_Get_processor_name(processor_name,&namelen);
     252    fprintf(stderr,"Process %d on %s\n",
     253            myid, processor_name);
     254    n = 0;
     255    while (!done)
     256    {
     257        /* 由 node 0 將使用者輸入的值送給其它的 node  */
     258        if (myid == 0)
     259        {
     260            printf("Enter the number of intervals: (0 quits) ");
     261            scanf("%d", &n);
     262            startwtime = MPI_Wtime();
     263        }
     264
     265        /* 這非常重要,所有的 node 必需在此同步,才可以收到使用者輸入的 n */
     266        MPI_Barrier(MPI_COMM_WORLD);
     267
     268        /* 將 n 送給其它的 node  */
     269        MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
     270        if (n == 0)
     271            done = 1;
     272        else
     273        {
     274
     275            /* 此為計算 pi 的演算法 */
     276            h   = 1.0 / (double) n;
     277            sum = 0.0;
     278            for (i = myid + 1; i <= n; i += numprocs)
     279            {
     280                x = h * ((double)i - 0.5);
     281                sum += f(x);
     282            }
     283            mypi = h * sum;
     284
     285            /* 將算完的結果傳給 node 0 加總 */
     286            MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
     287            if (myid == 0)
     288            {
     289                printf("pi is approximately %.16f, Error is %.16f\n",
     290                       pi, fabs(pi - PI25DT));
     291                endwtime = MPI_Wtime();
     292                printf("wall clock time = %f\n",
     293                       endwtime-startwtime);
     294            }
     295        }
     296    }
     297    MPI_Finalize();
     298    return 0;
     299}
     300EOF
     301}}}
     302 * 用 mpicc 編譯 test4.c
     303{{{
     304ym24@bio001:~$ mpicc -o test2 test4.c
     305}}}
     306 * 用 mpiexec 執行 test4 程式
     307{{{
     308ym24@bio001:~$ mpiexec -n 12 ./test4
     309}}}