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
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:
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.