Pergunta

I'm just starting to learn openMP and I have the following...

    do 100 k=1,lines

    !$OMP PARALLEL DO PRIVATE(dotprod) REDUCTION(+:co(k),si(k))
     do 110,i=1,ION_COUNT
      dotprod=(rx(k)*x(i)+ry(k)*y(i)...)
      co(k)=co(k)+COS(dotprod)
      si(k)=si(k)+SIN(dotprod)
     110 continue
    !$OMP END PARALLEL DO

I've figured out (i think) that I need to do a reduction on co(k) and si(k) if I want to add them correctly, but as far as I can tell, you can't have an array like that within the reduction clause. How can I go about doing this?

Foi útil?

Solução

If I understand correctly, using temporary variables would work:

do 100 k=1,lines

co_tmp = 0.0
si_tmp = 0.0
!$OMP PARALLEL DO PRIVATE(dotprod) REDUCTION(+:co_tmp,si_tmp)
 do 110,i=1,ION_COUNT
  dotprod=(rx(k)*x(i)+ry(k)*y(i)...)
  co_tmp=co_tmp+COS(dotprod)
  si_tmp=si_tmp+SIN(dotprod)
 110 continue
!$OMP END PARALLEL DO

co(k) = co_tmp
si(k) = si_tmp

100 continue

Outras dicas

You could also use the associate block (F2003) or pointers (F90) to avoid the temporary variable. In any case I would use end do, as I see you are using the free form source so you have F90.

do k=1,lines
  c => co(k)
  s => si(k)  !use associate in Fortran 2003 here

  !$OMP PARALLEL DO PRIVATE(dotprod) REDUCTION(+:c,s)
  do i=1,ION_COUNT
    dotprod = (rx(k)*x(i) + ry(k)*y(i) ...)
    c = c + COS(dotprod)
    s = s + SIN(dotprod)
  end do
  !$OMP END PARALLEL DO
end do

Also think about moving the creation of threads out of the loop.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top