Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29253 Discussions

Accessing all real or imaginary parts of an array at once.

OP1
New Contributor III
1,542 Views
Is it possible to access independently the real and imaginary parts of an array at once?

For instance, assume that Z is an array with 10 complex values, and RE and IM are arrays with 10 real values each. Is there a way to assign RE to the real part of Z, and IM to the imaginary part of Z at once, without resorting to a loop construct?

Or, say, I want to assign 1.0D+0 to all imaginary values of Z at once. Is it possible?

Thanks,

Olivier
0 Kudos
4 Replies
TimP
Honored Contributor III
1,542 Views
You certainly can write an array assignment which touches only the real or imaginary part of an array section. The compiler would make a decision whether to load and "refresh" both parts or only the part you touch, according to the level of optimization and architecture you specify. As it all has to be brought into cache anyway, it's not normally worth while from the programming point of view to attempt to dictate what happens at a lower level.
0 Kudos
OP1
New Contributor III
1,542 Views
Quoting - tim18
You certainly can write an array assignment which touches only the real or imaginary part of an array section. The compiler would make a decision whether to load and "refresh" both parts or only the part you touch, according to the level of optimization and architecture you specify. As it all has to be brought into cache anyway, it's not normally worth while from the programming point of view to attempt to dictate what happens at a lower level.

I am not sure I understand your first sentence. Is there such a construct in FORTRAN, which, if I write it in pseudo-code, would look like:

SET_IMAG_PART_OF(Z(1:10)) = 1.0D+0 (this should not modify the real part of Z)

or

SET_REAL_PART_OF(Z(1:10))= RE(1:10) (this should not modify the imaginary part of Z) ?


I can think of Z = DCMPLX(DREAL(Z),1.0D+0) but that doesn't look too elegant and probably entails more operations than really needed (but again the compiler is probably a lot smarter than whatI give him credit for).

I also know that complex arrays can be treated as real arrays of twice the size - but again this is just a trick.

Olivier
0 Kudos
Steven_L_Intel1
Employee
1,542 Views
There is such a construct - in the draft of Fortran 2008:

6.4.3 Complex parts
6 R614 complex-part-designator is designator % RE
7 or designator % IM
8 C621 (R614) The designator shall be of complex type.
9 1 If complex-part-designator is designator%RE it designates the real part of designator. If it is designator%IM
10 it designates the imaginary part of designator. The type of a complex-part-designator is real, and its kind and
11 shape are those of the designator.

NOTE 6.7
The following are examples of complex part designators:
impedance%re !-- Same value as REAL(impedance)
fft%im !-- Same value as AIMAG(fft)
x%im = 0.0 !-- Sets the imaginary part of X to zero

Sorry, not yet implemented in Intel Fortran.
0 Kudos
TimP
Honored Contributor III
1,542 Views
Quoting - opmkl

I can think of Z = DCMPLX(DREAL(Z),1.0D+0) but that doesn't look too elegant and probably entails more operations than really needed (but again the compiler is probably a lot smarter than whatI give him credit for).

I also know that complex arrays can be treated as real arrays of twice the size - but again this is just a trick.

I suppose Z = CMPLX(real(Z), 1.0D0, KIND(1.0D0)) would be more strictly correct, and the compiler still, in principle, has the opportunity to recognize you are touching only one part. As this operation should be vectorizable, but without good facility to skip over the untouched part, that may be good enough without attention to the detail.
As you say, the ability to access the even or odd elements of an array with the same storage association is well established in legacy practice, although ugly, and likely to provoke complaints from gen-interfaces. It's even possible that could vectorize with practically the same code.
0 Kudos
Reply