I'm writing some code using Fortran CoArrays (using caf and cafrun from OpenCoarrays) that requires having a coarray string of allocatable length. It can be exemplified by this code:
program CoarrayExample
character(:), allocatable :: string[:]
integer :: numImages, image, stringLength
stringLength = 50
allocate(character(stringLength) :: string[*])
numImages = num_images()
sync all
do image = 1, numImages
if (this_image() == image) then
write(string, '(A,I0)') "Image ", this_image()
end if
end do
sync all
if (this_image() == 1) then
do image = 1, numImages
write(*,*) string[image]
end do
end if
sync all
end program CoarrayExample
The code is expected to store a string copy for each image containing the text "Image <n>", where <n> is the number of the image. The first image is then expected to output the data to stdout.
The compiler has no trouble compiling (command: caf example.f90 -o example) but when running (command: cafrun -np 4 ./example), I get inconsistent behavior. Sometimes I only get empty lines at stdout, sometimes I get the strings but they are cut off, and some times I even get the error:
Image 1
--------------------------------------------------------------------------
Primary job terminated normally, but 1 process returned
a non-zero exit code. Per user-direction, the job has been aborted.
--------------------------------------------------------------------------
--------------------------------------------------------------------------
mpiexec noticed that process rank 0 with PID 0 on node pop-os exited on signal 11 (Segmentation fault).
--------------------------------------------------------------------------
Error: Command:
`/usr/bin/mpiexec -n 4 ./example`
failed to run.
All problems disappear when I remove the allocations. For example, this code works just fine:
program CoarrayExample
character(len=50) :: string[*]
integer :: numImages, image
numImages = num_images()
sync all
do image = 1, numImages
if (this_image() == image) then
write(string, '(A,I0)') "Image ", this_image()
end if
end do
sync all
if (this_image() == 1) then
do image = 1, numImages
write(*,*) string[image]
end do
end if
sync all
end program CoarrayExample
Any idea what the problem could be?