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

Can I pass data to a module without copying?

Zhanghong_T_
Novice
308 Views
Hi all,
Recently a problem bored me. I defined a module contains a set of subroutines. Some data will pass to one subroutine of the module. I wish the data can be visited by many subroutine of the module. One way is to define a set of same structure variants and copy the data to these variants. But when the input data size is very large, this method will waste memory. Can I directly use the input variants and set it public (or they are global variants)in the module (I don't like to pass these variants during the subroutines of the module).
Thanks,
Zhanghong Tang
0 Kudos
7 Replies
Jugoslav_Dujic
Valued Contributor II
308 Views

Yes you can.

Note, however, that passing data as argument do NOT involve any copy of the memory (with some exceptions).

A typical designfor such issuesis that you provide the data declarations within the module but keep them PRIVATE, and then define one or more routines which are dedicated to Read/Write the data. In this way, the data are accessible only by the module itself, and only these mentioned Set/Get routinesare used to communicate with the "outer world". Your mileage may vary, of course.
Jugoslav
0 Kudos
Zhanghong_T_
Novice
308 Views

Thank you very much!

How should I modify the following program?

module test

contains

subroutine readdata(n, x, y)

implicit none

integer n

real*8 x(n), y(n)

call outputdata(n, x, y)

end subroutine

subroutine outputdata(n, x, y)

implicit none

integer n

real*8 x(n), y(n)

integer i

do i=1, n

write(*, *) x(i), y(i)

enddo

end subroutine
end module
program
use test
implicit none
integer:: n=100
real*8:: x(n), y(n)
call readdata(n, x, y)
end
0 Kudos
Jugoslav_Dujic
Valued Contributor II
308 Views

As is, that does not make much sense, because it does notever define x and y.But for example:

module test
private
integer:: n
real(8), allocatable:: x(:), y(:)
public readdata
public outputdata

contains
!--------------------------------
subroutine readdata(sfilename)

implicit none

character(*):: sfilename
integer:: i

open(11,file=sfilename,action='read')
read(11, "(i4)") n
allocate(x(n), y(n))
do i=1,n
read(11,"(2f10.0)") x(i), y(i)
end do
close(11)
end subroutine readdata
!--------------------------------
subroutine outputdata()

implicit none

integer i

do i=1, n
write(*, *) x(i), y(i)
enddo

end subroutine outputdata
!--------------------------------
subroutine freedata()
deallocate(x, y)
end subroutine freedata
end module test
!=====================================
program
use test
implicit none
character(100):: sfilename

call getarg(1, sfilename)
call readdata(sfilename)
call outputdata()
call freedata()
end program
Jugoslav

Message Edited by JugoslavDujic on 06-28-2004 02:01 PM

0 Kudos
Zhanghong_T_
Novice
308 Views

Thank you again!

I know the data can be passed by file, but when the size of arrays is up to several million or more, it will waste much time to write and read data. Is there any way to make it possible to define an entrance for the module, then data passed from the entrance and shared by the module?

Sincerely,

Zhanghong Tang

0 Kudos
Jugoslav_Dujic
Valued Contributor II
308 Views

Well, yes. I was just writing an illustration.

Jugoslav

0 Kudos
Zhanghong_T_
Novice
308 Views
I am waiting for your illustration...
Thank you!
0 Kudos
Jugoslav_Dujic
Valued Contributor II
308 Views
Well, you can declare X and Y public and access them from outside the module as any other variables...
...or you can make them POINTERs, and use assignment-operator => to point them to "real" data which come from elsewhere...
...but where do your "zillions" of data come from? Ultimately, they probably come from a file and get stored in a file. Or they're perhaps intermediate results from another calculation; in any case, it's a good idea to have just one module which deals with input/output of data.
Jugoslav
0 Kudos
Reply