Skip to content

Commit

Permalink
fix parcel
Browse files Browse the repository at this point in the history
  • Loading branch information
urbanjost committed Dec 27, 2021
1 parent e233de2 commit 0ec4a69
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 27 deletions.
52 changes: 25 additions & 27 deletions app/prep.f90
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ module M_fpp !@(
type(parcel_stack),public :: G_parcel_dictionary(500)

integer,save :: G_line_number=0
logical,save,public :: G_asis=.false.
logical,save,public :: G_expand=.false.
logical,save,public :: G_inparcel=.false.
integer,public :: G_iocount=0
integer,public :: G_parcelcount=0
integer,public :: G_io_total_lines=0
Expand Down Expand Up @@ -128,13 +127,11 @@ subroutine cond() !@(#)cond(3f): process conditional directive assumed to
!write(*,*)'OPTIONS='//trim(options)
!write(*,*)'UPOPTS='//trim(upopts)

if(G_asis.and.VERB.eq.'PARCEL')then
call parcel_case(options) ! end parcel ignoring options
return
elseif(G_asis)then
call write_out(trim(G_source)) ! write data line
return
elseif(G_write)then ! if processing lines in a logically selected region
if(G_write)then ! if processing lines in a logically selected region
if(G_inparcel.and.VERB.ne.'PARCEL')then
call write_out(trim(G_source)) ! write data line
return
endif
! process the directive
select case(VERB)
case(' ') ! entire line is a comment
Expand Down Expand Up @@ -248,7 +245,7 @@ subroutine parcel_case(opts) !@(#)parcel_case(3f): p
call dissect2('parcel','-oo ',opts) ! parse options and inline comment on input line
name=sget('parcel_oo')
if(name.eq.'')then
G_asis=.false.
G_inparcel=.false.
G_iout=G_iout_init
else
open(newunit=lun,iostat=ios,action='readwrite',status='scratch',iomsg=message)
Expand All @@ -258,7 +255,7 @@ subroutine parcel_case(opts) !@(#)parcel_case(3f): p
G_parcelcount=G_parcelcount+1
G_parcel_dictionary(G_parcelcount)%name=name
G_parcel_dictionary(G_parcelcount)%unit_number=lun
G_asis=.true.
G_inparcel=.true.
G_iout=lun
endif
endif
Expand Down Expand Up @@ -1794,12 +1791,13 @@ subroutine post(parcel_name) ! @(#)post(3f): switch to scratch file defined by
call stop_prep('*prep* ERROR(060) - ERROR REWINDING PARCEL:'//trim(G_source)//':'//trim(message))
endif

!d!do
!d! read(ifound,'(a)',iostat=ios)message
!d! if(ios.ne.0)exit
!d! write(*,*)'>>>'//trim(message)
!d!enddo
!d!rewind(unit=ifound,iostat=ios,iomsg=message)
!======= DEBUG
!do
! read(ifound,'(a)',iostat=ios)message
! if(ios.ne.0)exit
! write(*,*)'>>>'//trim(message)
!enddo
!rewind(unit=ifound,iostat=ios,iomsg=message)

G_iocount=G_iocount+1
if(G_iocount.gt.size(G_file_dictionary))then
Expand Down Expand Up @@ -2007,11 +2005,11 @@ subroutine help_usage(l_help)
' [--help] ',&
'DESCRIPTION ',&
' ',&
' By default the pre-processor prep(1) will interpret lines with "$" in column ',&
' one, and will output no such lines. Other input is conditionally written to ',&
' the output file based on the directives encountered in the input. It does ',&
' not support parameterized macros but does support string substitution and ',&
' the inclusion of free-format text blocks that may be converted to Fortran ',&
' The pre-processor prep(1) will interpret lines with "$" (by default) in ',&
' column one, and will output no such lines. Other input is conditionally ',&
' written to the output file based on the directives encountered in the input. ',&
' It does not support parameterized macros but does support string substitution',&
' and the inclusion of free-format text blocks that may be converted to Fortran',&
' comments or CHARACTER variable definitions while simultaneously being used ',&
' to generate documentation files. INTEGER or LOGICAL expressions may be used ',&
' to select output lines. ',&
Expand Down Expand Up @@ -2705,8 +2703,6 @@ subroutine set(line)
! create a dictionary with character keywords, values, and value lengths
! using the routines for maintaining a list

G_expand=.true.

call dissect2('set','-oo' ,line) ! parse options on input line
iend=index(line//' ',' ')
name=upper(line(:iend))
Expand All @@ -2717,7 +2713,7 @@ subroutine set(line)
write(G_iout,'(*("!",a,"==>","[",a,"]",/))')(trim(keywords(i)),values(i)(:counts(i)),i=1,size(keywords))
endif
else
val=line(min(iend+1,len(line)):)
val=line(min(iend+1,len(line)):)
! insert and replace entries
call update(name,val)
! remove some entries
Expand Down Expand Up @@ -3058,8 +3054,10 @@ program prep !@(#)prep(1f): prep
call notabs(line,G_source,ilast) ! expand tab characters and trim trailing ctrl-M from DOS files
endif

if(G_expand)then
call expand_variables(G_source)
if(.not.G_inparcel)then
if(size(keywords).ne.0)then
call expand_variables(G_source)
endif
endif

select case (line(1:1)) ! special processing for lines starting with 'd' or 'D'
Expand Down
3 changes: 3 additions & 0 deletions aux/txt2man
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ head=".\\\" Text automatically generated by txt2man
.nh
.\\\" disable justification (adjust text to left margin only)
.ad l
.\\\" set smaller margin and spacing options
.ta T 0.2i
.nr IN 0.2i
.\\\" -----------------------------------------------------------------"
# string "( " is causing trouble. Not sure why, remove space for now
#sed -e 's@( @(@g'|
Expand Down
106 changes: 106 additions & 0 deletions demos/zen.ff
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
program zen_of_fortran
use ISO_FORTRAN_ENV, only: stderr=>ERROR_UNIT, stdin=>INPUT_UNIT, stdout=>OUTPUT_UNIT
implicit none
integer,parameter :: io=stdout
! grokking Fortran:
$BLOCK WRITE
Grokking Fortran

Fast is better than slow
Slow is better than unmaintainable

Small functions are clearer than monolithic procedures
Simplicity is robustness
“Make everything as simple as possible – but no simpler” (Einstein)

Self-descriptive code is best
but comment

Make it look like the math

All inputs and outputs explicit
All inputs and outputs carefully named and clearly defined
the closer to the point of declaration the better
Be paranoid about parameters and check their validity coming and going

Array-oriented is better than object-oriented
Vector is better than loop
Matrix is better than vector
unless it’s complicated
Strided is better than scattered
Contiguous is better than strided
Broadcasting is a great idea
use where possible

Create tests large and small
and use them
Use every debug flag you have been given when developing

Write everything as if it was going to be reused
and share it
Package code and supporting files
like you were going to give them to a friend

“Rules are meant to be broken” (MacArthur, not Einstein)

Based on: "The Zen of Fortran", by Ondřej Čertík, John Pask
with poetic license taken by John S. Urban
Date: 2021-10-30

Inspired by Zen of Python 2 and Zen of NumPy 3,
by Travis Oliphant and Tim Peters
$BLOCK
end program zen_of_fortran
$!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
$BLOCK NULL
Nice. More controversial, but I might add a few personal preferences
(and then snitch this very nice list; what would be a preferred
attribution?) …

* self-descriptive code is best, but if you do not add comments and
references someone will wish you had (probably you)

* small functions are clearer than monolithic procedures (which are
sometimes justifiable for performance, but should be avoided)

* Use every debug flag you can while developing

* The more compilers you use the better

* Write everything as generically as you can and as if it was going to be reused (and share it)

* profile your #*?! code (mostly an HPC thing, but you would not believe
the things I find).

* test your input and output values whenever the overhead
is tolerable (make your routines paranoid!).

* make tests
+ then test at the scale your users will use your code at x 10
+ next, test at the scale your users will use your code at x 100
+ and then test to failure.

* package your code and supporting files like you were going to have to
give it to a friend.

Yours beautifully cover optimizations, but a will add these more mundane
items and put this on a few desks, I think.

certik

what would be a preferred attribution?

Probably:

Ondřej Čertík, John Pask, Travis Oliphant, Tim Peters

John and I did the Fortran version, Travis did the NumPy version which
inspired us and Tim did the Python version which inspired Travis and
us. I hope I didn’t forget about anybody.

wclodius
22m

FWIW Tim Peters worked on a Fortran compiler for Kendall Square Research in the lab 80s, before he became involved with Python.
$!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
$BLOCK

0 comments on commit 0ec4a69

Please sign in to comment.