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

How to evaluate size of an input object?

anishtain4
Beginner
701 Views
It happens a lot of time that one of the inputs of a function or subroutine are not declared (almost always) like

function hh(a)
real,intent(in)::a(:)

but you have to define a lot of arrayes that are dependent on the size of a, so on every line you have to write x(size(a)), and it becomes wose when you are going to define multidimensional arrays, like x(size(a),size(a)),xh(size(a),size(a))
well it makes the code a little messy, is there any way to assign this size to an integer and use it?
0 Kudos
1 Solution
mecej4
Honored Contributor III
701 Views
Although what you desire is not allowed in the Fortran language, you can achieve almost the same thing by using the preprocessor. Here is a version of your function that you may examine to see if the idea will work for you. You see slightly neater code, and the compiler sees the same messy code as before, which the preprocessor pass feeds it.

[fortran]function hh(a)
#define SZa size(a)
real,intent(in)::a(:)
! local arrays
real :: b(SZa)
integer :: ix(SZa,SZa)
hh=0
do i=1,SZa
b(i) = a(i)*a(i)
if(a(i).gt.0)then
ix(i,i)=1
hh=hh+a(i)
else
ix(i,i)=0
endif
end do
return
end function hh
[/fortran]
Compile with ifort /Qfpp. You may also use Fortran style preprocessor directives instead of C style.

Do note that using a preprocessor does have its disadvantages and you should guard against them.

View solution in original post

0 Kudos
3 Replies
Steven_L_Intel1
Employee
701 Views
Nope. Sorry. By the time you "assign" anything, you can't declare the bounds.
0 Kudos
mecej4
Honored Contributor III
702 Views
Although what you desire is not allowed in the Fortran language, you can achieve almost the same thing by using the preprocessor. Here is a version of your function that you may examine to see if the idea will work for you. You see slightly neater code, and the compiler sees the same messy code as before, which the preprocessor pass feeds it.

[fortran]function hh(a)
#define SZa size(a)
real,intent(in)::a(:)
! local arrays
real :: b(SZa)
integer :: ix(SZa,SZa)
hh=0
do i=1,SZa
b(i) = a(i)*a(i)
if(a(i).gt.0)then
ix(i,i)=1
hh=hh+a(i)
else
ix(i,i)=0
endif
end do
return
end function hh
[/fortran]
Compile with ifort /Qfpp. You may also use Fortran style preprocessor directives instead of C style.

Do note that using a preprocessor does have its disadvantages and you should guard against them.
0 Kudos
Arjen_Markus
Honored Contributor II
701 Views
Well, you could try this:

real function hh( a )
real, dimension(:) :: a

hh = hhh( a, size(a) )
contains
real function hhh( a, sza )
real, dimension(:) :: a
real, dimension(sza, sza, sza) :: work ! Automatic array for workspace
...
end function hhh
end function hh

Whether that improves your code sufficiently is up to you, but it avoids the repetition
of size(a) and size(a,1), size(a,2) in case of multidimensional arrays.

You could even leave out thedeclaration of a in hhh (and the argument), as it is
host-associated from the containing function hh.

Regards,

Arjen
0 Kudos
Reply