Fortran ASSOCIATE syntax to allow indexed access?
Is there a nice way to write a Fortran ASSOCIATE statement to turn this
FORALL (i = 2:n-2)
v(:,i) = v(:,i) + MATMUL(A, &
c(2)*u(:,i-2) + c(1)*u(:,i-1) + c(0)*u(:,i) + c(1)*u(:,i+1) + c(2)*u(:,i+2))
END FORALL
into something like the following
ASSOCIATE ( U => ..., V => ...)
FORALL (i = 2:n-2)
V(i) = V(i) + MATMUL(A, &
c(2)*U(i-2) + c(1)*U(i-1) + c(0)*U(i) + c(1)*U(i+1) + c(2)*U(i+2))
END开发者_开发知识库 FORALL
END ASSOCIATE
I'm staring at Adams et al's The Fortran 2003 Handbook section 8.2, but I can't see how to write the associate-name => selector
construct to allow for indexed access into the associate-name
.
Obviously what I'm going for is overkill for a couple of lines. I've got a bunch of 'em that I'd like to condense.
Unless I'm misreading things, I don't think this is possible. The specification says (section 8.1.4.3):
Within a SELECT TYPE or ASSOCIATE construct, each associating entity has the same rank as its associated selector.
and as far as I can see, you want a rank 1 associating entity (V
) and will need a rank 2
associated selector (to hold v
).
If the aim is to make the code shorter/nicer, I think the best way to achieve that does not need an ASSOCIATE
construct:
forall(i=2:n-2)
v(:,i) = v(:,i) + MATMUL(A,MATMUL(u(:,i-2:i+2),c([2,1,0,1,2]))
end forall
which, as long as the 5-sized coefficient array does not change, could be preinitialized as a
real :: c5(5)
c5 = c([2,1,0,1,2])
And then run in a single line as
forall(i=2:n-2) v(:,i) = v(:,i) + MATMUL(A,MATMUL(u(:,i-2:i+2),c5))
note that it must be lbound(u,2)<=0
.
精彩评论