0

I have this piece of fortran code (there are 6 before call and 5 before &):

           call mpi_send( v(1), nx, MPI_DOUBLE_PRECISION,
          &               prev, myid-1, comm, ierr )

And I am looking for a way to extract nx and myid-1 to replace them by respectively cnt and tag (ideally aligned with call just above) like so:

           cnt = nx
           tag = myid-1
           call mpi_send( v(1), cnt, MPI_DOUBLE_PRECISION,
          &               prev, tag, comm, ierr )

cnt (or nx) is always the second argument of mpi_send. tag (or myid-1) is always the 5th argument of mpi_send.

I understand I have to play with the s substitute sed option, combined with capture, and "insert above" command... This may/could look like:

  sed -i '/mpi_send(.*, \(.*\), .*&.*, \(.*\)/{
s//\1/
i\
      cnt = \1
      tag = \2 
}' file.f

... Which doesn't work: I can't get a way to get that together.

EDIT

Ideally looking for a solution that could handle all cases like :

           call mpi_recv( mv_buf, nx, MPI_DOUBLE_PRECISION, prev, myid,
          &               comm, status, ierr )

That should be replaced by

           cnt = nx
           tag = myid
           call mpi_recv( mv_buf, cnt, MPI_DOUBLE_PRECISION, prev, tag,
          &               comm, status, ierr )

EDIT

Ideally need to handle lots of other slightly different changing cases (where patterns to replace may change) and where indentation may change in if blocks (6 before if, and so, more than 6 before call mpi_send).

         if ( myid .lt. nprocs-1 ) then
            call mpi_send( v((np-1)*nx+1), nx, MPI_DOUBLE_PRECISION,
        &                  next, myid+1, comm, ierr )
          endif

Can somebody help?

4
  • How are nx and myid-1 determined? Are their names fixed, or their positons fixed? Commented Aug 23, 2022 at 18:23
  • position fixed: see updated description above Commented Aug 23, 2022 at 19:07
  • But function name is different this time. Is that intended to be run on all functions independent of their names? Commented Aug 23, 2022 at 19:12
  • I thought I could have a regexp per function name: looks already complicated enough!... :D Anyway, I'd like to get something that works for one function, so that, I can adapt it to other functions according to needs. Commented Aug 23, 2022 at 19:16

2 Answers 2

1

This ugly sed command should work for all functions whose names start with mpi_:

sed -i.orig '/^ *call mpi_.*(/{
N
s/\( *\)\([^,]*,\)\([^,]*\),\(\([^,]*,\)\{2\}\)\([^,]*\)\(.*\)/\1cnt = \3\
\1tag = \6\
\1\2 cnt,\4 tag\7/
}' file.f
Sign up to request clarification or add additional context in comments.

7 Comments

Waow!... Seems to work! Can you explain the black magic behind? :D
I get misalignment (cnt and tag offseted w.r.t call mpi_). Could be possible get alignment too?
@FGHP I see 6 blanks before cnt and tag on my computer. Isn't that correct? How many blanks should there have been?
On target code, mpi_send/recv may be in if statements: in theses case, call mpi_ has more than 6 ` ` behind (say 8 or 9 depending on the way the code is written). I'll add an EDIT section to the initial question
I think I get it and updated the answer.
|
0

In awk it would be something like this:

awk '/mpi_send/ {process=1; print "cnt = nx\ntag = myid-1"; gsub(/nx/, "cnt");print; next} process {gsub("myid-1", "tag"); print; process = 0; next} 1' file.f

For example if file.f contained:

call mpi_send( v(1), nx, MPI_DOUBLE_PRECISION,
        &               prev, myid-1, comm, ierr )
call something_else( v(1), nx, MPI_DOUBLE_PRECISION,
        &               prev, myid-1, comm, ierr )

it would return:

$ awk '/mpi_send/ {process=1; print "cnt = nx\ntag = myid-1"; gsub(/nx/, "cnt");print; next} process {gsub("myid-1", "tag"); print; process = 0; next} 1' file.f
cnt = nx
tag = myid-1
call mpi_send( v(1), cnt, MPI_DOUBLE_PRECISION,
        &               prev, tag, comm, ierr )
call something_else( v(1), nx, MPI_DOUBLE_PRECISION,
        &               prev, myid-1, comm, ierr )

It can be slightly improved to use more future-proof regexes but should work in most cases.

1 Comment

I get the idea, but, cnt/tag may not always be nx/myid-1: I updated the initial description of the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.