Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
29286 Discussions

converting between REAL and COMPLEX array variables

david_sallngc_com
2,522 Views
Hi!

I am looking for a fast way to convert between REAL and COMPLEX variables. The following code snippet shows you what I want to do,


real rtmp1(10), rtmp2(10)
complex ctmp(5)

! load with data.
do i = 1,10
rtmp1(i) = dble(i)
end do

! load the complex array.
ctmp(1:5) = cmplx(rtmp1(1:10:2), rtmp1(2:10:2))

... do some calculations ...

! assign the complex data toa realarray
rtmp2(1:10:2) = real(ctmp)
rtmp(2:10:2) = imag(ctmp)

This will give me rtmp2 = rtmp1 which is what I am looking for in this example.

Is there a faster way of doing this, i.e., without having to copy data using array slicing? Is there a way I can use pointers to point to the start of a memory address?


Thank you very much for your help.

Sincerely,

David
0 Kudos
2 Replies
rasa
Beginner
2,522 Views
There was a recent thread on c.l.f where I had asked for help on a similar topic.

http://groups.google.com/group/comp.lang.fortran/browse_thread/thread/c4a783f99fe3276/6a364d586f9b9a0e

You might have to hack using C_PTR if you want to avoid a full copy. F2008 standard has z%RE and z%IM but I don't think they are implemented in the compiler yet.

HTH.

0 Kudos
Martyn_C_Intel
Employee
2,522 Views
If you are willing to use old-fashioned Fortran77, you can simply equivalence rtmp1 to ctmp. Then all you need to do is load rtmp, and the data is immeduately available in ctmp.

equivalence (ctmp(1),rtmp1(1))

At the end, you could probably just call a simple copy routine, if you ensure that any type checking is disabled (no explicit interface). The compiler would likely inline the routine. Data would be passed by reference, with no descriptor; inside the routine, both input and output would be declared with the same data type:

call mycopy(ctmp,rtmp2,10)

subroutine mycopy(a,b,n)
real a(n),b(n)
do i=1,n
b(i)=a(i)
enddo
end

Purists won't like this, for respectable reasons, but a lot of code used to be written this way.
There are dangers if you use such a routine to copy integers, (eg signalling NaNs get converted to quiet NaNs), but I'm not aware of any issues here, though of course you'd want to test.
And you can't use F90 array notation or assumed shape arrays for the actual or dummy arguments.

For a debug build, you'd get warnings unless youturned off interface generation and/or checking for this routine.
0 Kudos
Reply