- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi all,
I'm triing to compile and run a fortran code with reshape function to reorder an array, but i found the values of the corresponding element are not equal in some cases(h and e in code). Below are the code and results.
program main
implicit none
real, pointer :: s(:,:,:)
real :: d(2,3,4),e(2,4,3),f(3,4,2),g(4,2,3),h(4,3,2)
allocate(s(3,2,4))
call random_number(s)
write(*,*) "s: ", s(3,:,1)
d = reshape(s,shape(d),order=(/2,1,3/))
write(*,*) "d: ", d(:,3,1)
f = reshape(s,shape(f),order=(/1,3,2/))
write(*,*) "f: ", f(3,1,:)
g = reshape(s,shape(g),order=(/3,2,1/))
write(*,*) "g: ", g(1,:,3)
h = reshape(s,shape(h),order=(/3,1,2/))
write(*,*) "h: ", h(1,3,:)
e = reshape(s,shape(e),order=(/2,3,1/))
write(*,*) "e: ", e(:,1,3)
end
results:
s: 0.3525161 0.8382882
d: 0.3525161 0.8382882
f: 0.3525161 0.8382882
g: 0.3525161 0.8382882
h: 0.3001758 4.9717721E-02
e: 0.7958636 4.0313378E-02
Thanks in advance,
best regards
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I reformulated your program as follows to make it a bit easier to trace what is happening:
program main
implicit none
integer, pointer :: s(:,:,:),
integer :: d(2,3,4),e(2,4,3),f(3,4,2),g(4,2,3),h(4,3,2), i
allocate(s(3,2,4))
s = reshape([(i,i=1,24)],shape(s))
write(*,*) "s: ", s(3,:,1)
d = reshape(s,shape(d),order=(/2,1,3/))
write(*,*) "d: ", d(:,3,1)
f = reshape(s,shape(f),order=(/1,3,2/))
write(*,*) "f: ", f(3,1,:)
g = reshape(s,shape(g),order=(/3,2,1/))
write(*,*) "g: ", g(1,:,3)
h = reshape(s,shape(h),order=(/3,1,2/))
write(*,*) "h: ", h(1,3,:)
e = reshape(s,shape(e),order=(/2,3,1/))
write(*,*) "e: ", e(:,1,3)
end
Both ifort and NAG Fortran give the same result:
s: 3 6
d: 3 6
f: 3 6
g: 3 6
h: 17 18
e: 9 21
I haven't traced through it to see why this is, but the evidence suggests that both compilers are correct.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
gfortran gives the same results also
gfortran -o repro repro.f90
./repro
s: 3 6
d: 3 6
f: 3 6
g: 3 6
h: 17 18
e: 9 21
gfortran --version
GNU Fortran (GCC) 12.0.1 20220413 (Red Hat 12.0.1-0)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
So
write(*,*) "s: ", s
gives
s: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
Now take this flat layout and store into H in Fortran order, Z dim first
Then you will find elements 3 and 6 in
write(*,*) "h: ", h(2,1,1), h(3,1,2)
gives
h: 3 6
exactly where you expect them in storage order. Simple way to to this: draw a 3D cube or grab a rubix cube. Storage order is 2 elements in Z, then go down column on cube
(1,1,1), (1,1,2), (2,1,1), (2,1,2), (3,1,1),(3,1,2) etc.
1. 2. 3. 4. 5. 6
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I leave it as an exercise to the student to derive the storage order for E
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page