1              program EXAMPLE

2              include 'mpif.h'

3              integer my_id, np, comm, tag, count, ierr, n,q

4              integer i,j,k

5              integer a(100,100),b(100)

6              integer c(100,100),a1(100,100),a2(100,100)

7              integer status(MPI_STATUS_SIZE)

8              double precision t1,tfinish

9              call MPI_Init(ierr)

10             comm=MPI_COMM_WORLD

11             call MPI_Comm_rank(COMM, my_id, ierr)

12             call MPI_Comm_size(COMM, np, ierr)

13             print *, 'Process ', my_id, ' of ', np, ' is alive'

14             tag = 0

15             n=100

16             if (my_id.eq.0) then      

17               do i=1,n,1

18                 do j=1,n,1

19                   if (i.eq.j) then   

20                     a(j,i)=0

21                     b(i)=1        

22                  else

23                    a(j,i)=1 

24                     b(i)=0        

25                  end if 

26                 end do

27               end do

28             end if

29             open(unit=9,file='MMP.dat')

30             t1=MPI_Wtime()

31             call MPI_Bcast(n,1,MPI_INTEGER,0,COMM,ierr)

32             call MPI_Bcast(b,n,MPI_INTEGER,0,COMM,ierr)     

33             call MPI_Scatter(a,int(n*n/np),MPI_INTEGER,a1,

34           *      int(n*n/np),MPI_INTEGER,0,COMM,ierr)

35             do j=1,n,1

36               do i=1,n,1

37                 c(i,j)=0

38              end do

39             end do

40             do j=1,n,1

41               do i=1,int(n/np),1

42                do k=1,n,1

43                   c(i+int(n/np)*my_id,j)=

44           *       c(i+int(n/np)*my_id,j)+a1(k,i)*b(k)

45                end do

46              end do

47             end do

48             call MPI_Reduce(c,a2,n*n,MPI_INTEGER,MPI_SUM,

49           *      0, COMM,ierr)

50             if (my_id.eq.0) then

51               t1=MPI_Wtime()-t1     

52               Print *,'Dim = ',n,' the time is ',t1

53               write(9,*) 'Dim = ',n,' the time is ',t1

54             end if

55             close(9)     

56             call MPI_Finalize(ierr)

57             stop

58             end   

Рис. 56. Пример распараллеливания алгоритма умножения матрицы на вектор