public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Théo Cavignac" <theo.cavignac@gmail.com>
To: fortran@gcc.gnu.org
Subject: Optimization of spread
Date: Thu, 3 Nov 2022 11:48:07 +0100	[thread overview]
Message-ID: <CALzWbOyT=NDKyr+11GYgbqJ41dN5sUoUhbwBNNPuss9hhPzfMw@mail.gmail.com> (raw)

Hello,
I am currently writing some numerical code in Fortran 2003 and I want
to use the spread intrinsic because having used NumPy heavily for the
past few years, it feels natural to use such an array primitive.
I naturally wondered what would be the effect on performance and found
this on Stack Overflow: https://stackoverflow.com/a/55732905/6324751

TLDR: spread is as fast, if not faster than a do loop, when using
ifort. However, it is significantly slower (up to 100% in my
microbenchmarks) with gfortran 12.2.0.

Investigating the matter a bit more, I noticed that ifort recognize
the pattern and essentially produce the same code for both the do loop
and the spread call, while gfortran “naively” call spread, even with
-O3.

Here is a demonstration on godbolt.org: https://godbolt.org/z/dcYEPj8bP

So, my question is: is this something that could be better optimized?
I wonder if simply allowing the compiler to inline spread wouldn't
already enable further optimizations that would lead to the same kind
of performance as found in ifort.
I also think other array intrinsic may benefit from this effort if
similar strategies can be applied.
While I have never been contributing to GCC, but I would be willing to
do this implementation if it is in the reach of my C++ skills, and if
someone can point me in the right direction.

Regards,
Théo

             reply	other threads:[~2022-11-03 10:47 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-03 10:48 Théo Cavignac [this message]
2022-11-03 21:54 ` Mikael Morin
2022-11-03 22:04   ` Thomas Koenig
2022-11-16 10:12     ` Théo Cavignac

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CALzWbOyT=NDKyr+11GYgbqJ41dN5sUoUhbwBNNPuss9hhPzfMw@mail.gmail.com' \
    --to=theo.cavignac@gmail.com \
    --cc=fortran@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).