experchange > fortran

spectrum (01-07-19, 02:21 PM)
Hello,

Inspired by a recent question on StackOverflow,

Why does coarray with allocatable component creates segmentation faults when accessed from different image?


I have tried a bit simplified code that allocates an array component ("values(:)")
with different sizes on different images (specifically, size(values) = 2 for image 1
and 4 for other images).

program main
implicit none

type :: Array_t
integer, allocatable :: values(:)
end type
type(Array_t) :: array[*]

if (this_image() == 1) then
allocate(array% values( 2 ))
array % values(:) = this_image()
else
allocate(array% values( 4 ))
array % values(:) = this_image() * 100
endif
sync all

print *, this_image(), " values= ", array[ this_image() ] % values(:)
sync all

if (this_image() /= 1) then
array % values( 1:2 ) = array[ 1 ] % values( 1:2 ) ! (***)
endif
sync all

print *, this_image(), " values= ", array[ this_image() ] % values(:)
end

In the line (***), I am copying the array elements between different images.
On my environment (gfortran-8.2 + opencoarrays-2.3.1 + OpenMPI-3.1.3 on OSX10.11,
installed via Homebrew), this code compiles with no error and gives the result

$ caf test.f90
$ cafrun -np 4 ./a.out

3 values= 300 300 300 300
4 values= 400 400 400 400
1 values= 1 1
2 values= 200 200 200 200
1 values= 1 1
2 values= 1 1 200 200
3 values= 1 1 300 300
4 values= 1 1 400 400

so values( 1:2 ) on images 2,3,4 are modified as expected.

Is the above code standard-conforming...? (The OP of the above question
experiences segmentation fault on MacOS Mojave, but otherwise the environment
is very similar to mine. FWIW, the OP's original code also compiles and runs fine
on my environment (w/o segmentation fault).)
spectrum (01-07-19, 02:29 PM)
# Hmm, sorry, I guess this part "array[ this_image() ] % values(:)" can be written
simply as "array % values".
FortranFan (01-07-19, 04:45 PM)
On Monday, January 7, 2019 at 7:21:40 AM UTC-5, spectrum wrote:

> ..
> Is the above code standard-conforming...? ..


To me, the code looks conforming. Intel Fortran agrees: it compilers your code with no warnings or errors and the execution results agree with what you show.
spectrum (01-07-19, 07:34 PM)
On Monday, January 7, 2019 at 11:45:28 PM UTC+9, FortranFan wrote:
> To me, the code looks conforming. Intel Fortran agrees: it compilers your code with no warnings or errors and the execution results agree with what you show.


Thanks very much for trying the above code! Indeed, if two compilers work, the problem
might be related to OP's environment (i.e., Mojave) or installation method etc.
But more info may be necessary to say something about the latter...

By the way, I have also tried the following code, which involves "whole-array" assignment
(marked with (*)).

program main
implicit none

type :: Array_t
integer, allocatable :: values(:)
end type
type(Array_t) :: array[*]

allocate( array % values( this_image() ) )
array%values = this_image()
sync all

print *, "init: ", this_image(), " : ", array % values(:)
sync all

if (this_image() == 4) then
array % values = array[ 1 ] % values ! (*)
endif
sync all

print *, this_image(), " : ", array % values(:)
sync all
end

The result was as follows (again with gfortran-8.2 + opencoarrays):

init: 1 : 1
init: 2 : 2 2
init: 3 : 3 3 3
init: 4 : 4 4 4 4
1 : 1
2 : 2 2
3 : 3 3 3
4 : 1 268435456 0 268435456

(Here, the output lines have been reordered for readability.) I imagined that this kind
of whole-array operation might induce reallocation of an array on the LHS (here "values"). But in practice, only the copy seems to be performed
for 4 elements of the LHS (on image 4), by reading the value "1" and other 3
garbage values from the RHS (on image 1).
FortranFan (01-07-19, 08:46 PM)
On Monday, January 7, 2019 at 12:34:44 PM UTC-5, spectrum wrote:

> ..
> By the way, I have also tried the following code, which involves "whole-array" assignment
> (marked with (*)).
> ..
> array % values = array[ 1 ] % values ! (*)
> ..


I don't think this code conforms to the standard per section 10.2.1.2 Intrinsic assignment statement which has in paragraph 2:

21 2 If the variable in an intrinsic assignment statement is a coindexed object,
...
24 • the variable shall be conformable with expr

That is, I'm assuming an object without an explicit image selector which then is the object on the local image is a coindexed object too.
Similar Threads