PVM上模擬分散式系統

摘要:將一個10*10的矩陣,分給5台電腦去做 並算出時間,要算出分給5台 4台 3台 2台 1台的時間

 

心得:這是我第一個程式.....也花了我很多時間去寫,大概花了10幾個小時吧!真的很辛苦,過程中也很@#% 但寫出來後很有出就感^^

 

  1 /*
  2 (c) mysterywho 
  3 compiler : pvm
  4 date : 15/10/2008
  5 */
  6 
  7 //master端
  8 #include "pvm3.h"
  9 #include <stdio.h>
 10 #include <time.h>
 11 #include <sys/times.h>
 12 
 13 #define MAX 10
 14 
 15 
 16  main()
 17   {
 18 
 19       int mytid,number,task_ids[10];
 20       int i,j,NPROCS=4;
 21       int row,index,count,count1;
 22       int A[MAX][MAX],B[MAX][MAX];
 23       int result[MAX][MAX];
 24     
 25       int gettimeofday(struct timeval *tv);
 26 
 27 
 28       struct timeval tpstart,tpend;
 29       float  timeuse;
 30 
 31 
 32 
 33 
 34       // enroll in PVM //
 35          mytid = pvm_mytid();
 36 
 37      gettimeofday(&tpstart,NULL);
 38   
 39 
 40       // define A[][]
 41          printf("A matrix");
 42          printf("\n");
 43 
 44          for( i=0 ; i<10 ; i++)
 45           {
 46              for( j=0 ; j<10 ; j++)
 47                 {
 48                      A[i][j]= rand() %10;
 49 
 50                      printf("%2d  " ,A[i][j] ) ;
 51                 }
 52                    printf("\n");
 53          }
 54 
 55 
 56 
 57      // define B[][]
 58          printf("B matrix");
 59           printf("\n");
 60 
 61            for( i=0 ; i<10 ; i++)
 62              {
 63              for( j=0 ; j<10 ; j++)
 64                {
 65                        B[i][j]= rand() % 10;
 66                        printf("%2d  ",B[i][j]);
 67                }
 68 
 69                printf("\n");
 70 
 71             }
 72 
 73                printf("\n");
 74 
 75 
 76 
 77 
 78 
 79 
 80     // spawn worker tasks //
 81        number = pvm_spawn("worker_mystery",NULL,PvmTaskDefault,"", NPROCS, task_ids);
 82        printf("The number of spawn workers is %d\n",number);
 83 
 84        //send data to worker tasks //
 85 
 86          index = 0;
 87          row = 0;
 88           for (i=0;i<NPROCS;i++)
 89              {
 90                if(10% NPROCS > i)
 91                    {
 92                 row = 10 / NPROCS + 1;
 93                 count = row*10;
 94                    }
 95 
 96                 else
 97                   {
 98                   row = 10 / NPROCS;
 99                   count = row*10;
100                   }
101 
102                   pvm_initsend(PvmDataDefault);
103                   pvm_pkint(&row,1,1);
104                   pvm_pkint(&index,1,1);
105                   pvm_pkint(&count,1,1);
106                   pvm_pkint(&A[index][0], count, 1);
107                   pvm_pkint(&B[0][0], 1001);
108                   pvm_send(task_ids[i], 4);
109 
110                   index = row + index ;
111 
112                   }
113 
114 
115         // wait and gather results //
116 
117           int  msgtype=7;
118 
119           for (i = 0; i < NPROCS; i++)
120             {
121                pvm_recv(task_ids[i],msgtype);
122                pvm_upkint(&index, 11);
123           //   pvm_upkint(&row, 1, 1);
124                pvm_upkint(&count1, 11);
125           //   pvm_upkint(&A[index][0],count1,1);
126                pvm_upkint(&result[index][0], count1, 1);
127 
128             }
129 
130         printf("C matrix");
131         printf("\n");
132 
133         for(i=0;i< 10;i++)
134           {
135           for(j=0;j< 10;j++)
136              {
137                printf(" %6d  ", result[i][j]);
138 
139              }
140            printf("\n");
141 
142           }
143          printf("\n");
144 
145    gettimeofday(&tpend,NULL);
146      timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+  tpend.tv_usec-tpstart.tv_usec;
147      timeuse/=1000000;
148     
149      printf("Used Time:%f\n",timeuse);
150 
151            printf("\n");
152 
153             pvm_exit();
154                               }
155 
156 
157 
158 //worker端
159 #include "pvm3.h"
160 #include <stdio.h>
161 #include <stdlib.h>
162 
163 #define MAX 10
164 
165 
166  main()
167  {
168    int A[MAX][MAX],B[MAX][MAX],C[MAX][MAX];
169    int mytid,master;
170    int i,j,k,m=0,count,count1;
171    int row,index;
172 
173 
174    // enroll in PVM //
175     mytid = pvm_mytid();
176  
177    // receive portion of array to be summed //
178 
179      pvm_recv(-1-1);
180      pvm_upkint(&row,1,1);
181      pvm_upkint(&index,1,1);
182      pvm_upkint(&count,1,1);
183      pvm_upkint(&A[index][0], count, 1);
184      pvm_upkint(&B[0][0], 1001);
185 
186      //compute //
187 
188     for(i=index;i<row+index;i++)
189       {
190        for(j=0;j<10;j++)
191         {
192           C[i][j]=0;
193           for(k=0;k<10;k++)
194             {
195              C[i][j]=A[i][k]*B[k][j]+C[i][j];
196             }
197          }
198        }
199 
200  
201     count1 = count;
202 
203     // send computed sum back to master //
204        master = pvm_parent();
205 
206        pvm_initsend(PvmDataRaw);
207        pvm_pkint(&index, 11);
208        pvm_pkint(&count1, 1,1);
209        pvm_pkint(&C[index][0],count1 ,1);
210        pvm_send(master,7);
211 
212          pvm_exit();
213          }
214 
215 
216 
posted @ 2009-03-10 23:47  春天播種,秋天才會收穫  阅读(300)  评论(0编辑  收藏  举报