Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

Eureka! -- Archimedes


devel / comp.lang.fortran / spread vs do

SubjectAuthor
* spread vs doevan
+* Re: spread vs doRon Shepard
|+- Re: spread vs dogah4
|`* Re: spread vs doevan
| `* Re: spread vs dogah4
|  `- Re: spread vs doRon Shepard
`* Re: spread vs dopehache
 `- Re: spread vs dogah4

1
spread vs do

<d730066d-dc2b-431e-ab57-868a933e9bf7n@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2372&group=comp.lang.fortran#2372

  copy link   Newsgroups: comp.lang.fortran
X-Received: by 2002:ad4:596b:0:b0:46b:cc90:5a87 with SMTP id eq11-20020ad4596b000000b0046bcc905a87mr2298839qvb.59.1655197756621;
Tue, 14 Jun 2022 02:09:16 -0700 (PDT)
X-Received: by 2002:a81:5754:0:b0:310:57b7:23d7 with SMTP id
l81-20020a815754000000b0031057b723d7mr4337962ywb.58.1655197756321; Tue, 14
Jun 2022 02:09:16 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.fortran
Date: Tue, 14 Jun 2022 02:09:16 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=84.65.21.68; posting-account=cMjuxAoAAAB-6i-xpTpQzwpy2_ugQ9Ne
NNTP-Posting-Host: 84.65.21.68
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <d730066d-dc2b-431e-ab57-868a933e9bf7n@googlegroups.com>
Subject: spread vs do
From: evangelou@gmail.com (evan)
Injection-Date: Tue, 14 Jun 2022 09:09:16 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 3327
 by: evan - Tue, 14 Jun 2022 09:09 UTC

Hi all. I was reading the discussion on
Why is the Fortran intrinsic function "spread" often slower than explicit iteration
https://stackoverflow.com/questions/55717904/why-is-the-fortran-intrinsic-function-spread-often-slower-than-explicit-iterat
and wanted to try it myself. I modified the code provided by Steve Lionel in the answer (see the end of this message) and got the following timings (compiled using gfortran -O3, but similar results with -O0, version 9.4.0)
Iteration 1 4.42133093
Spread 1 0.309983253
Iteration 3 0.212992191
Spread 3 0.917150021
So it seems that the answer to the question should you use spread or do is it depends on the dimension that you are iterating over. Is there a consistent optimal code for performing these type of calculations?

-------------------------------------------------------------------
module benchmarks
implicit none
integer, parameter :: n=500
integer :: j
real :: d2(n,n)
real :: d3(n,n,n)
contains
! Iteration 1
subroutine benchmark_i1(res,n)
integer n
real, intent(out) :: res(n,n,n)
do j = 1, n
res(j,:,:) = d2*d3(j,:,:)
end do
end subroutine
! Spread 1
subroutine benchmark_s1(res,n)
integer n
real, intent(out) :: res(n,n,n)
res = d3*spread(d2, 1, n)
end subroutine
!Iteration 3
subroutine benchmark_i3(res,n)
integer n
real, intent(out) :: res(n,n,n)
do j = 1, n
res(:,:,j) = d2*d3(:,:,j)
end do
end subroutine
! Spread 3
subroutine benchmark_s3(res,n)
integer n
real, intent(out) :: res(n,n,n)
res = d3*spread(d2, 3, n)
end subroutine
end module

program main
use benchmarks
real :: tstart,tend
real :: res(n,n,n)
call random_number(d2)
call random_number(d3)
! Iteration
call cpu_time(tstart)
call benchmark_i1(res,n)
call cpu_time(tend)
write(*,*) 'Iteration 1', tend-tstart, sum(res)
! Spread
call cpu_time(tstart)
call benchmark_s1(res,n)
call cpu_time(tend)
write(*,*) 'Spread 1', tend-tstart, sum(res)
! Iteration
call cpu_time(tstart)
call benchmark_i3(res,n)
call cpu_time(tend)
write(*,*) 'Iteration 3', tend-tstart, sum(res)
! Spread
call cpu_time(tstart)
call benchmark_s3(res,n)
call cpu_time(tend)
write(*,*) 'Spread 3', tend-tstart, sum(res)
end program

Re: spread vs do

<sE1qK.70478$X_i.7558@fx18.iad>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2373&group=comp.lang.fortran#2373

  copy link   Newsgroups: comp.lang.fortran
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!feeder1.feed.usenet.farm!feed.usenet.farm!peer03.ams4!peer.am4.highwinds-media.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx18.iad.POSTED!not-for-mail
MIME-Version: 1.0
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:91.0)
Gecko/20100101 Thunderbird/91.10.0
Subject: Re: spread vs do
Content-Language: en-US
Newsgroups: comp.lang.fortran
References: <d730066d-dc2b-431e-ab57-868a933e9bf7n@googlegroups.com>
From: nospam@nowhere.org (Ron Shepard)
In-Reply-To: <d730066d-dc2b-431e-ab57-868a933e9bf7n@googlegroups.com>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 37
Message-ID: <sE1qK.70478$X_i.7558@fx18.iad>
X-Complaints-To: abuse@easynews.com
Organization: Forte - www.forteinc.com
X-Complaints-Info: Please be sure to forward a copy of ALL headers otherwise we will be unable to process your complaint properly.
Date: Tue, 14 Jun 2022 10:17:43 -0500
X-Received-Bytes: 3206
 by: Ron Shepard - Tue, 14 Jun 2022 15:17 UTC

On 6/14/22 4:09 AM, evan wrote:
> Hi all. I was reading the discussion on
> Why is the Fortran intrinsic function "spread" often slower than explicit iteration
> https://stackoverflow.com/questions/55717904/why-is-the-fortran-intrinsic-function-spread-often-slower-than-explicit-iterat
> and wanted to try it myself. I modified the code provided by Steve Lionel in the answer (see the end of this message) and got the following timings (compiled using gfortran -O3, but similar results with -O0, version 9.4.0)
> Iteration 1 4.42133093
> Spread 1 0.309983253
> Iteration 3 0.212992191
> Spread 3 0.917150021
> So it seems that the answer to the question should you use spread or do is it depends on the dimension that you are iterating over. Is there a consistent optimal code for performing these type of calculations?

Whoever told you that do loops were to be avoided at all costs?

Many times, the do loops are simpler, for both the compiler and a human,
to understand. In these cases, the spread() code is literally written so
that memory allocation is required for an intermediate quantity. The
compiler can then sometimes realize that the allocation is not
necessary, and then decide if there are other reasons, such as
sequential memory accesses, where it would be good to do it that way
anyway. This is probably one of those cases. Sometimes the compiler is
getting it right, other times not. It can also depend on the array
dimensions. For small arrays, it might not matter if you step through an
array the wrong way, but for big arrays it could be critical.

This of course will depend on the compiler and which optimizations are
enabled at the time. It may also change when you update to newer
versions of the same compiler. There have been a dozen gfortran updates
since 9.4.0.

If this is a critical part of your runtime, then this is something that
you should check periodically. You might include both algorithms in your
source code, and switch back and forth between them when your testing
shows that the optimal approach has changed since the last time you
tested it. This is one of the reasons why a preprocessor that supports
conditional compilation is useful in the language.

$.02 -Ron Shepard

Re: spread vs do

<1b3a55eb-b30e-4809-83c3-6f39437ccbbfn@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2374&group=comp.lang.fortran#2374

  copy link   Newsgroups: comp.lang.fortran
X-Received: by 2002:a05:6214:19cc:b0:46b:9918:2225 with SMTP id j12-20020a05621419cc00b0046b99182225mr4098080qvc.77.1655224176619;
Tue, 14 Jun 2022 09:29:36 -0700 (PDT)
X-Received: by 2002:a05:6902:110c:b0:634:62c4:ab7e with SMTP id
o12-20020a056902110c00b0063462c4ab7emr6019935ybu.539.1655224175622; Tue, 14
Jun 2022 09:29:35 -0700 (PDT)
Path: i2pn2.org!i2pn.org!aioe.org!news.uzoreto.com!2.eu.feeder.erje.net!feeder.erje.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.fortran
Date: Tue, 14 Jun 2022 09:29:35 -0700 (PDT)
In-Reply-To: <sE1qK.70478$X_i.7558@fx18.iad>
Injection-Info: google-groups.googlegroups.com; posting-host=2601:602:9700:4689:c024:a2e6:2f6f:28a4;
posting-account=gLDX1AkAAAA26M5HM-O3sVMAXdxK9FPA
NNTP-Posting-Host: 2601:602:9700:4689:c024:a2e6:2f6f:28a4
References: <d730066d-dc2b-431e-ab57-868a933e9bf7n@googlegroups.com> <sE1qK.70478$X_i.7558@fx18.iad>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <1b3a55eb-b30e-4809-83c3-6f39437ccbbfn@googlegroups.com>
Subject: Re: spread vs do
From: gah4@u.washington.edu (gah4)
Injection-Date: Tue, 14 Jun 2022 16:29:36 +0000
Content-Type: text/plain; charset="UTF-8"
 by: gah4 - Tue, 14 Jun 2022 16:29 UTC

On Tuesday, June 14, 2022 at 8:17:49 AM UTC-7, Ron Shepard wrote:

(snip)

> Whoever told you that do loops were to be avoided at all costs?

It seems to be a rumor that has been going on for years.

Well, at least back to when array expressions, and especially some
of the more interestnig array intrinsic functions, were added to the
standard, there is interest in using them.

Well, even longer ago than that, some would initialize arrays:

DATA X/1000000*1.234/

which on many systems writes one million values into the object program,
and then loads them from disk to run the program, much slower than a
DO loop, or after it was added, array assignment:

X = 1.234

After array expressions were added, compilers were pretty good at
doing simple assignment at least as well as the DO loop, and maybe
faster.

But some took it as a challenge to figure out how to write a complicated
array expression, to do what could be done in a simple DO loop.

Now, say you want to find out if an array has any negative elements:

DO I=1,1000000
IF(X(I).LT.0) GOTO 10
END DO

or

IF(ANY(X<0)) GOTO 10

The array expression looks nice, and maybe compilers now will
figure it out. But for many years the compiler would generate a
temporary array of LOGICAL values 1000000 element long,
and then check to see if ANY were .TRUE.. The nice thing about
the DO loop is that it exits the loop as soon as the first one is found.

OK, maybe some compilers now will figure that one out. How about:

IF(ANY(SIN(X)) < 0) GOTO 10

I suspect you can always make a complicated array expression that
a compiler won't figure out has an easy loop with an exit test.

I don't remember now any of the complicated array expressions
I have seen over the years, but there are complicated ones!

Re: spread vs do

<d653db21-37a9-4ac6-bfad-dd8ac8e5183cn@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2375&group=comp.lang.fortran#2375

  copy link   Newsgroups: comp.lang.fortran
X-Received: by 2002:a0c:e887:0:b0:464:5710:7f77 with SMTP id b7-20020a0ce887000000b0046457107f77mr4669547qvo.68.1655225865468;
Tue, 14 Jun 2022 09:57:45 -0700 (PDT)
X-Received: by 2002:a05:6902:100d:b0:665:f7e:10c4 with SMTP id
w13-20020a056902100d00b006650f7e10c4mr6357646ybt.494.1655225865271; Tue, 14
Jun 2022 09:57:45 -0700 (PDT)
Path: i2pn2.org!i2pn.org!aioe.org!news.mixmin.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.fortran
Date: Tue, 14 Jun 2022 09:57:44 -0700 (PDT)
In-Reply-To: <sE1qK.70478$X_i.7558@fx18.iad>
Injection-Info: google-groups.googlegroups.com; posting-host=84.65.21.68; posting-account=cMjuxAoAAAB-6i-xpTpQzwpy2_ugQ9Ne
NNTP-Posting-Host: 84.65.21.68
References: <d730066d-dc2b-431e-ab57-868a933e9bf7n@googlegroups.com> <sE1qK.70478$X_i.7558@fx18.iad>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <d653db21-37a9-4ac6-bfad-dd8ac8e5183cn@googlegroups.com>
Subject: Re: spread vs do
From: evangelou@gmail.com (evan)
Injection-Date: Tue, 14 Jun 2022 16:57:45 +0000
Content-Type: text/plain; charset="UTF-8"
 by: evan - Tue, 14 Jun 2022 16:57 UTC

On Tuesday, 14 June 2022 at 16:17:49 UTC+1, Ron Shepard wrote:
> Whoever told you that do loops were to be avoided at all costs?

No one told me that. I am looking for a consistent way to program such calculations.

Re: spread vs do

<b72797c7-4842-4944-8ea8-d139ea9eaa5an@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2376&group=comp.lang.fortran#2376

  copy link   Newsgroups: comp.lang.fortran
X-Received: by 2002:a05:622a:50d:b0:304:f438:c464 with SMTP id l13-20020a05622a050d00b00304f438c464mr5277858qtx.323.1655229809860;
Tue, 14 Jun 2022 11:03:29 -0700 (PDT)
X-Received: by 2002:a25:cc4e:0:b0:65c:854b:c111 with SMTP id
l75-20020a25cc4e000000b0065c854bc111mr6368968ybf.218.1655229809557; Tue, 14
Jun 2022 11:03:29 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.fortran
Date: Tue, 14 Jun 2022 11:03:29 -0700 (PDT)
In-Reply-To: <d653db21-37a9-4ac6-bfad-dd8ac8e5183cn@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=2601:602:9700:4689:c024:a2e6:2f6f:28a4;
posting-account=gLDX1AkAAAA26M5HM-O3sVMAXdxK9FPA
NNTP-Posting-Host: 2601:602:9700:4689:c024:a2e6:2f6f:28a4
References: <d730066d-dc2b-431e-ab57-868a933e9bf7n@googlegroups.com>
<sE1qK.70478$X_i.7558@fx18.iad> <d653db21-37a9-4ac6-bfad-dd8ac8e5183cn@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <b72797c7-4842-4944-8ea8-d139ea9eaa5an@googlegroups.com>
Subject: Re: spread vs do
From: gah4@u.washington.edu (gah4)
Injection-Date: Tue, 14 Jun 2022 18:03:29 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 1988
 by: gah4 - Tue, 14 Jun 2022 18:03 UTC

On Tuesday, June 14, 2022 at 9:57:47 AM UTC-7, evan wrote:
> On Tuesday, 14 June 2022 at 16:17:49 UTC+1, Ron Shepard wrote:
> > Whoever told you that do loops were to be avoided at all costs?

> No one told me that. I am looking for a consistent way to program such calculations.

There is an old saying, credited to D. Knuth:

"premature optimization is the root of all evil"

Unless it makes a significant difference in actual time, as in more time
than it takes to discuss, write in the way that is most readable.

As array expressions get more complicated, they get less readable in
a different way than DO loops. It isn't always so obvious, then, but you
can decide for yourself, which one you find more readable.

Re: spread vs do

<JM9qK.239000$zgr9.170812@fx13.iad>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2382&group=comp.lang.fortran#2382

  copy link   Newsgroups: comp.lang.fortran
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx13.iad.POSTED!not-for-mail
MIME-Version: 1.0
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:91.0)
Gecko/20100101 Thunderbird/91.10.0
Subject: Re: spread vs do
Content-Language: en-US
Newsgroups: comp.lang.fortran
References: <d730066d-dc2b-431e-ab57-868a933e9bf7n@googlegroups.com>
<sE1qK.70478$X_i.7558@fx18.iad>
<d653db21-37a9-4ac6-bfad-dd8ac8e5183cn@googlegroups.com>
<b72797c7-4842-4944-8ea8-d139ea9eaa5an@googlegroups.com>
From: nospam@nowhere.org (Ron Shepard)
In-Reply-To: <b72797c7-4842-4944-8ea8-d139ea9eaa5an@googlegroups.com>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 21
Message-ID: <JM9qK.239000$zgr9.170812@fx13.iad>
X-Complaints-To: abuse@easynews.com
Organization: Forte - www.forteinc.com
X-Complaints-Info: Please be sure to forward a copy of ALL headers otherwise we will be unable to process your complaint properly.
Date: Tue, 14 Jun 2022 19:32:40 -0500
X-Received-Bytes: 1927
X-Original-Bytes: 1794
 by: Ron Shepard - Wed, 15 Jun 2022 00:32 UTC

On 6/14/22 1:03 PM, gah4 wrote:
[...]
> There is an old saying, credited to D. Knuth:
>
> "premature optimization is the root of all evil"
>
> Unless it makes a significant difference in actual time, as in more time
> than it takes to discuss, write in the way that is most readable.

Yes, clear code is a goal, but there is also algorithm choice. I have
always interpreted Knuth's comment in that way. If you start with the
wrong algorithm, say one that scales as N**2 rather than N, and then you
spend a lot of time trying to optimize register usage and memory access
and so on for that wrong algorithm, then you are reluctant to throw away
all of that work and implement the right algorithm.

In poker, there is a similar expression, "Don't throw good money after
bad." The idea is that you get invested in a bad hand, then you don't
want to stop, you want to keep betting on it.

$.02 -Ron Shepard

Re: spread vs do

<NsTvyZxe3Gcz0HhxmIlJs2rFBzU@jntp>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2390&group=comp.lang.fortran#2390

  copy link   Newsgroups: comp.lang.fortran
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!pasdenom.info!from-devjntp
Message-ID: <NsTvyZxe3Gcz0HhxmIlJs2rFBzU@jntp>
JNTP-Route: news2.nemoweb.net
JNTP-DataType: Article
Subject: Re: spread vs do
References: <d730066d-dc2b-431e-ab57-868a933e9bf7n@googlegroups.com>
Newsgroups: comp.lang.fortran
JNTP-HashClient: DJVBsOKY6bLPf2z0Gr8Dq4YOFao
JNTP-ThreadID: d730066d-dc2b-431e-ab57-868a933e9bf7n@googlegroups.com
JNTP-Uri: http://news2.nemoweb.net/?DataID=NsTvyZxe3Gcz0HhxmIlJs2rFBzU@jntp
User-Agent: Nemo/0.999a
JNTP-OriginServer: news2.nemoweb.net
Date: Thu, 16 Jun 22 13:15:42 +0000
Organization: Nemoweb
JNTP-Browser: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0
Injection-Info: news2.nemoweb.net; posting-host="57a7697b711e1390e43e8d98aa87a8bb5a5fec17"; logging-data="2022-06-16T13:15:42Z/6989744"; posting-account="44@news2.nemoweb.net"; mail-complaints-to="newsmaster@news2.nemoweb.net"
JNTP-ProtocolVersion: 0.21.1
JNTP-Server: PhpNemoServer/0.94.5
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-JNTP-JsonNewsGateway: 0.96
From: pehache.7@gmail.com (pehache)
 by: pehache - Thu, 16 Jun 2022 13:15 UTC

Le 14/06/2022 à 11:09, evan a écrit :
> Hi all. I was reading the discussion on
> Why is the Fortran intrinsic function "spread" often slower than explicit
> iteration
>
> https://stackoverflow.com/questions/55717904/why-is-the-fortran-intrinsic-function-spread-often-slower-than-explicit-iterat
> and wanted to try it myself. I modified the code provided by Steve Lionel in the
> answer (see the end of this message) and got the following timings (compiled using
> gfortran -O3, but similar results with -O0, version 9.4.0)
> Iteration 1 4.42133093
> Spread 1 0.309983253
> Iteration 3 0.212992191
> Spread 3 0.917150021
> So it seems that the answer to the question should you use spread or do is it
> depends on the dimension that you are iterating over. Is there a consistent
> optimal code for performing these type of calculations?
>
> -------------------------------------------------------------------
> module benchmarks
> implicit none
> integer, parameter :: n=500
> integer :: j
> real :: d2(n,n)
> real :: d3(n,n,n)
> contains
> ! Iteration 1
> subroutine benchmark_i1(res,n)
> integer n
> real, intent(out) :: res(n,n,n)
> do j = 1, n
> res(j,:,:) = d2*d3(j,:,:)
> end do
> end subroutine
> ! Spread 1
> subroutine benchmark_s1(res,n)
> integer n
> real, intent(out) :: res(n,n,n)
> res = d3*spread(d2, 1, n)
> end subroutine
> !Iteration 3
> subroutine benchmark_i3(res,n)
> integer n
> real, intent(out) :: res(n,n,n)
> do j = 1, n
> res(:,:,j) = d2*d3(:,:,j)
> end do
> end subroutine
> ! Spread 3
> subroutine benchmark_s3(res,n)
> integer n
> real, intent(out) :: res(n,n,n)
> res = d3*spread(d2, 3, n)
> end subroutine
> end module
>
> program main
> use benchmarks
> real :: tstart,tend
> real :: res(n,n,n)
> call random_number(d2)
> call random_number(d3)
> ! Iteration
> call cpu_time(tstart)
> call benchmark_i1(res,n)
> call cpu_time(tend)
> write(*,*) 'Iteration 1', tend-tstart, sum(res)
> ! Spread
> call cpu_time(tstart)
> call benchmark_s1(res,n)
> call cpu_time(tend)
> write(*,*) 'Spread 1', tend-tstart, sum(res)
> ! Iteration
> call cpu_time(tstart)
> call benchmark_i3(res,n)
> call cpu_time(tend)
> write(*,*) 'Iteration 3', tend-tstart, sum(res)
> ! Spread
> call cpu_time(tstart)
> call benchmark_s3(res,n)
> call cpu_time(tend)
> write(*,*) 'Spread 3', tend-tstart, sum(res)
> end program

Your i1 case is the worst one for a good reason : in each pass of the loop
you are working on res(j,:,:) and d3(j,:,:), which is are non-contiguous
array slices across arrays of 125 10^6 elements, that is 600 MB. This is
highly inefficient in terms of memory access.

If you want to express the very same calculations with loops, the most
efficient way is:

subroutine benchmark_i1(res,n)
integer n
real, intent(out) :: res(n,n,n)
do i3 = 1, n
do i2 = 1, n
do i1 = 1, n
res(i1,i2,i3) = d2(i2,i3)*d3(i1,i2,i3)
end do
end do
end do
end subroutine

You may replace the inner loop with an array syntax that any not-too-dumb
compiler can optimize without needing a temporary array:

subroutine benchmark_i1(res,n)
integer n
real, intent(out) :: res(n,n,n)
do i3 = 1, n
do i2 = 1, n
res(:,i2,i3) = d2(i2,i3)*d3(:,i2,i3)
end do
end do
end subroutine

In the s1 and s3 cases the code will spend a bit of time to allocate and
populate a large temporary array as a result of spread(), but once done
the compiler knows how to order the hidden loops in the most efficient
way.

So generally speaking the most important thing is to know of to order the
loops.

Regarding do loops versus spread() :

- array syntaxes can be more or less readable than the equivalent do
loops, depending on the cases. But I tend to find spread() less readable
and generally prefer do loops. But it's also a matter of personal
preferences.

- apart maybe for some trivial cases, spread() will always be less
efficient than the equivalent do loops, because most of time the compiler
has to allocate and populate a temporary array (and in addition this can
result in memory issues if this is a large array)

Re: spread vs do

<1ef9c793-bec2-4e77-90af-5e2c3255a882n@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2391&group=comp.lang.fortran#2391

  copy link   Newsgroups: comp.lang.fortran
X-Received: by 2002:a5d:5a04:0:b0:21a:20b9:afbe with SMTP id bq4-20020a5d5a04000000b0021a20b9afbemr5016391wrb.550.1655391313267;
Thu, 16 Jun 2022 07:55:13 -0700 (PDT)
X-Received: by 2002:a05:6902:154d:b0:665:503b:12c5 with SMTP id
r13-20020a056902154d00b00665503b12c5mr5811604ybu.612.1655391312738; Thu, 16
Jun 2022 07:55:12 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.128.87.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.fortran
Date: Thu, 16 Jun 2022 07:55:12 -0700 (PDT)
In-Reply-To: <NsTvyZxe3Gcz0HhxmIlJs2rFBzU@jntp>
Injection-Info: google-groups.googlegroups.com; posting-host=2601:602:9700:4689:b007:dee0:b435:7ca9;
posting-account=gLDX1AkAAAA26M5HM-O3sVMAXdxK9FPA
NNTP-Posting-Host: 2601:602:9700:4689:b007:dee0:b435:7ca9
References: <d730066d-dc2b-431e-ab57-868a933e9bf7n@googlegroups.com> <NsTvyZxe3Gcz0HhxmIlJs2rFBzU@jntp>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <1ef9c793-bec2-4e77-90af-5e2c3255a882n@googlegroups.com>
Subject: Re: spread vs do
From: gah4@u.washington.edu (gah4)
Injection-Date: Thu, 16 Jun 2022 14:55:13 +0000
Content-Type: text/plain; charset="UTF-8"
 by: gah4 - Thu, 16 Jun 2022 14:55 UTC

On Thursday, June 16, 2022 at 6:15:46 AM UTC-7, pehache wrote:

(snip)
> - array syntaxes can be more or less readable than the equivalent do
> loops, depending on the cases. But I tend to find spread() less readable
> and generally prefer do loops. But it's also a matter of personal
> preferences.
> - apart maybe for some trivial cases, spread() will always be less
> efficient than the equivalent do loops, because most of time the compiler
> has to allocate and populate a temporary array (and in addition this can
> result in memory issues if this is a large array)

Yes, this is the big problem with complicated array expressions.

I suspect compilers are getting better, but many will use a temporary
array, even though a person could easily see how to avoid one.

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor