Re: elemental function versus loop over function




Mark Morss schreef:

I have to perform a scalar function on each of the elements of a large
array. In general, for speed, does it matter much whether I loop over
the array or define an elemental function? One aspect of my problem is
that my array result depends not only on the input array, but on one or
two scalar inputs. So if I define an elemental function, before I can
call it with these arguments, my understanding is that I have to "blow
up" these scalars into constant-valued arrays of the same dimension as
my input array.

My preliminary assumption is that isn't worth doing this, and just
looping over the array is likely to be faster.

You can define an elemental function like:

real elemental function f( a, b, c )
...
end function f

and use it like this:

real, dimension(1000,1000) :: array, barray, result
real :: b, c

result = f(array, b, c)
result = f(array, b, barray)

All elements should be either conforming arrays or scalars.

I recently had the same issue. The compiler is quite happy to do this
and measuring the performance made clear that (in my case) there
was no significant overhead. (In fact, in some cases this solution
was faster than explicit do-loops)

Regards,

Arjen

.



Relevant Pages

  • Allow ELEMENTAL procedures with INTENT(IN) array arguments?
    ... The scalar arguments would still need to be ... and the shape of the RESULT of an ELEMENTAL function ... or a MODULE to get array data into an ELEMENTAL procedure if that data ... real, intent:: xpow! ...
    (comp.lang.fortran)
  • Re: elemental function versus loop over function
    ... the array or define an elemental function? ... So if I define an elemental function, ... my input array. ... arguments are scalar, you can easily use an elemental function. ...
    (comp.lang.fortran)
  • Re: Reference to derived type element by index?
    ... as a set of distinctively-named scalars. ... In another scope, the same common block ... would be a single array. ... algorithm and a specific application of the algorithm, ...
    (comp.lang.fortran)
  • Re: I want to parse @ArrayOfDays into @d1 through @d5
    ... condition on the loop indicated that the actual array length could ... -- sending the datasource, number of bins, and bin criteria as input ... My thought was to call a wrapper from the vb.net page, ... The sub-proc would parse the input array to ...
    (comp.databases.ms-sqlserver)
  • Re: returning list from function
    ... The Perl model for function call and return values is simple: ... single flat list of scalars. ... In this case it looks like instead of making an array, ... I guess, if current behavior is intentional, then the documentation has to be ...
    (comp.lang.perl.moderated)