Опубликован: 18.12.2012 | Доступ: свободный | Студентов: 272 / 55 | Длительность: 03:25:00
Лекция 3:

Создание параллельных программ

< Лекция 2 || Лекция 3: 12 || Лекция 4 >

Пример (1)

Всем images установить значения переменных.



Пример (2)

Две копии программы рисующие разные случайные геометрические фигуры. Первая – прямоугольники, Вторая – окружности.


Вариант программы

program  prog2
  use ifqwin
  integer, parameter :: dx = 10, dy = 10
  do
    call random_number(x);  call random_number(y)
    call random_number(r);  call random_number(g)
    call random_number(b)

   ires2 = setcolorrgb(rgbtointeger(int(r*255), &  
                                    int(g*255), &
                                    int(b*255)))

    if (this_image() == 1) ires2 = rectangle(3,int2(x*800), &
                int2(y*600),int2(x*800)+dx, int2(y*600)+dy)

    if (this_image() == 2) ires2 = ellipse(3,int2(x*800),   &
                int2(y*600),int2(x*800)+dx, int2(y*600)+dy)
  end do  
end
        

SYNC ALL

Барьерная синхронизация. Копии программы приостанавливаются на барьере до тех пор, пока на барьере не окажутся все копии программы.

program CAF
  integer, parameter :: M = 100
  real A(M,M)[*]

  if (this_image() == 1) then
    call random_number(A)
    A = real(int(A*10))
    do k = 2,num_images()  ! каждый узнал об изменении
      A[k] = A
    end  do
  end if
  sync all
...
    

Пример SYNC ALL

 integer :: i, k1, k2
  integer(8) sum = 0, k[*]    ! k - coarray-переменная

  if (this_image() == 1) then ! --- план для вычислений
    k1 = 1; k2 = 100
  end if

  if (this_image() == 2) then
    k1 = 101; k2 = 100000
  end if

  do i = k1,k2 ! --- расчет каждой копией программы
    k[this_image()] = k[this_image()] + i
  end do
  SYNC ALL  ! дожидаемся всех, сколько каждая копия насчитала
    
  if (this_image() == 1) then ! --- сбор результатов 
    do i = 1,num_images() 
      sum = sum + k[i]
    end do
    write(*,*) sum    ! 5000050000
  end if 

end
        

SYNC IMAGES

SYNC IMAGES(image_set)
    

Синхронизация избранных копий программы,

image_set – массив номеров копий программы или просто номер копии
image_set(*) - означает все копии
if (this_image() == 5) then
   ! image 5 ожидает других images, 
   ! чтобы завершить использование данных.
   sync images(*)
else
   sync images(5) 
   ! Другие images ожидают image 5, 
   ! чтобы установить данные, 
   ! но не ожидают никакого другого image.
end if
    

CRITICAL

critical
  ! операторы, выполняемые в любой момент
  ! времени только одной копией программы
end critical
    
program CAF
implicit none

complex Y(50)[1,1:*]

  write(*,*) "This is ", this_image(), " copy of program "
  write(*,*) "Image index = ", image_index(Y,[1,this_image()])
  write(*,*) "Coordinates = ", this_image(Y)
  write(*,*)

end program CAF
    
Результаты печати на экране "перепутаны"!
 This is            1  copy of program
 This is            2  copy of program
 Index =            2
 Coordinates =            1           2

 Index =            1
 Coordinates =            1           1
    
 This is            2  copy of program
 This is            1  copy of program
 Index =            2
 Coordinates =            1           2

 Index =            1
 Coordinates =            1           1
    
 This is            2  copy of program
 This is            1  copy of program
 Index =            1
 Coordinates =            1           1

 Index =            2
 Coordinates =            1           2
    

Экран – критическая секция.

program CAF
implicit none

complex Y(50)[1,1:*]
critical
  write(*,*) "This is ", this_image(), " copy of program "
  write(*,*) "Image index = ", image_index(Y,[1,this_image()])
  write(*,*) "Coordinates = ", this_image(Y)
  write(*,*)
end critical
end program CAF
    
 This is            1  copy of program
 Image index =            1
 Coordinates =            1           1

 This is            2  copy of program
 Image index =            2
 Coordinates =            1           2
    

Задания

Написать программу нахождения максимума в одномерном массиве.

< Лекция 2 || Лекция 3: 12 || Лекция 4 >