1

I have a main program that has subroutine written using coarrays. The problem is that while running the code it treats the entire code (main + subroutine) as parallel code and runs it on all specified processors. For eg., the Below program prints "Hello from main" four times when running using 4 CPUs. I want that the main program runs on one CPU while when it encounters a subroutine that uses coarrays it runs on all specified CPUs.

Main program that call subroutine coarray_test

program main
implicit none
write (*,*) "Hello from main "
call coarray_test
end program

Subroutine coarray_test

subroutine coarray_test
implicit none
write (*,*) "Hello from Subroutine coarray_test "
return
end subroutine coarray_test

Command used for compile and execution

export FOR_COARRAY_NUM_IMAGES=4
ifort -g -coarray -o test.out main.f90 coarray_test.f90
./test.out

Output

 Hello from main 
 Hello from Subroutine coarray_test 
 Hello from main 
 Hello from Subroutine coarray_test 
 Hello from main 
 Hello from Subroutine coarray_test 
 Hello from main 
 Hello from Subroutine coarray_test

Expected Output

 Hello from main 
 Hello from Subroutine coarray_test 
 Hello from Subroutine coarray_test 
 Hello from Subroutine coarray_test 
 Hello from Subroutine coarray_test
francescalus
  • 30,576
  • 16
  • 61
  • 96
codegeek
  • 11
  • 2
  • 3
    Coarrays don't work like that - it's all or nothing. *Why* do you want to do this? Would it be possible just to wrap the bits you want to run on a single process with `if( this_image() == 0 )...`? – Ian Bush Jul 13 '22 at 08:35

1 Answers1

2

You are misunderstanding the CAF model. It is based, like MPI, on independent processes. So the whole code is executed by each process. If you want some part executed byonly one process you can say if (image==0).

But think:

  1. if only one process computes something, how are the other ones going to get that result?
  2. and what do you gain from only one process being active? The other ones are idle while they could be doing useful work.

Really, the bits that you want executed only once, probably need to be done redundantly on all processes. With the exeception of print statements and other I/O.

Victor Eijkhout
  • 5,088
  • 2
  • 22
  • 23