[J3] Passing an element of an assumed shape array to an assumed size dummy array

Malcolm Cohen malcolm at nag-j.co.jp
Wed May 14 02:08:54 UTC 2025


Hi Pierre,

 

Yes, in solution (2) I was unclear. I meant for the pointer array to describe the same object that you get from sequence association, which is contiguous (and you still need to pass the correct leading-dimension and striding info, as the called routine expects). All that saves you is the need to pass the offset separately, so it’s probably not a big win in most situations.

 

But of course if you really want to convert the code to use assumed shape, there is also

(3) convert the called routine to expect an assumed-shape array instead of the kludgey sequence-association with manual leading-dimension offsetting and striding.

 

I would expect that in the vast majority of case (probably not all!), the obvious assumed-shape array handling, without manual leading-dimensioning and striding, would have similar performance to the old manual version. And will be simpler to read.

 

But yeah, if you’re going to have sequence association in the call tree anyway, the simplest thing to do is just to leave that as is.

 

Cheers,

-- 

..............Malcolm Cohen, NAG Oxford/Tokyo.

 

From: J3 <j3-bounces at mailman.j3-fortran.org> On Behalf Of Pierre Hugonnet via J3
Sent: Tuesday, May 13, 2025 6:12 PM
To: General J3 interest list <j3 at mailman.j3-fortran.org>
Cc: Pierre Hugonnet <pieru at ugo235.fr>
Subject: Re: [J3] Passing an element of an assumed shape array to an assumed size dummy array

 

Hi,

In solution 2) the pointer array wouldn't be contiguous (unless I didn't get your point), thus resulting in copy-in/copy-out, which is what we want to avoid here.

The solution 1) would work, but in practice I am calling some library routines and have no control on their interfaces. So, a wrapper routine should be written, with the whole array declared as explicit shape or assumed size, then passing the appropriate array element to the library routine. But:
- this is more code to write (not that I am lazy, but more code is also more room for bugs)
- I faced the problem because I was converting the dummy arguments of an existing code from explicit size to assumed-shape. Because of this restriction, one way or another I have to revert to explicit size (either keeping the original code as is, or in a wrapper routine), that is to a less modern feature, and still have to rely on sequence association at some point (the wrapper routine will call the library routine by passing an array element anyway).

Cheers

 

 

Le 12.05.2025 13:10, Malcolm Cohen via J3 a écrit :

There are multiple ways to do this:

 

1.	Pass the whole assumed-shape array (no copying) to the assumed-size array, with the element number you want to start at as a separate argument. You're already passing leading-dimension etc info so IMO that's not a big deal.
2.	Use rank-remapping pointer assignment to construct the view you want, and pass that.

 

All these old uses of sequence association were (as admitted in that thread) hard to understand and thus maintain. I do not think that extending sequence association to additional cases is a productive direction. We want things to be more reliable, not just add more stuff that works like the old F66/77 stuff did, which was notorious for bugs in programs when passing arguments.

 

Cheers,

-- 

................Malcolm Cohen, NAG Oxford/Tokyo.

 

From: J3 <j3-bounces at mailman.j3-fortran.org <mailto:j3-bounces at mailman.j3-fortran.org> > On Behalf Of Pierre Hugonnet via J3
Sent: Monday, May 12, 2025 6:41 PM
To: General J3 interest list <j3 at mailman.j3-fortran.org <mailto:j3 at mailman.j3-fortran.org> >
Cc: Pierre Hugonnet <pieru at ugo235.fr <mailto:pieru at ugo235.fr> >
Subject: Re: [J3] Passing an element of an assumed shape array to an assumed size dummy array

 

There are actually cases where passing an element and counting on sequence association is still useful, and where passing an array section instead would result in copy-in/copy-out, thus hurting the performances. I have given an example here: 

https://fortran-lang.discourse.group/t/array-element-actual-argument-assumed-size-dummy/9649/3?u=pieru

Pierre

 

 

Le 11.05.2025 21:22, Van Snyder via J3 a écrit :

On Sat, 2025-05-10 at 21:07 -0700, Robert Corbett via J3 wrote:

There is no incompatibility with FORTRAN 66 or FORTRAN 77.  The restriction on passing a scalar actual argument to an array dummy arguments applies only to assumed-shape, pointer, or polymorphic arrays (see the first dotted item in paragraph 14 of Subclause 15.5.2.5).  Therefore, the only incompatibility with FORTRAN 66 or FORTRAN 77 codes is with FORTRAN 66 or FORTRAN 77 codes that include assumed-shape, pointer, or polymorphic arrays.

 

Would it be useful to carve out an exception to use an element of a contiguous assumed-shape array (or pointer) as the start of a sequence association?

 

Probably not, because the actual argument is necessarily in a scope using stuff from Fortran 90 onward so there's no excuse not to write x(10:).

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.j3-fortran.org/pipermail/j3/attachments/20250514/3d759674/attachment.htm>


More information about the J3 mailing list