Skipping a part of the code according to command line arguement

171 views Asked by At

I recently wrote a FORTRAN code which includes a part where I have used bubble-sort algorithm. Now, it takes much time due to the sorting of a very large files(~7/8 mins). However, mostly input files are arranged in such a way that sorting could be skipped and save some time. For which GO TO and CONTINUE is my friend. But I don't exactly know where to put these construct.

variable declaration

!****************** Name of the *.inp file from command line *****************
numargs=iargc()
IF (numargs.eq.3) THEN
  CALL getarg(1,inputfile)
  WRITE(*,*) ' Input: ',trim(inputfile)
  CALL getarg(2,outputfile)
  WRITE(*,*) 'Output: ',trim(outputfile)
  CALL getarg(3,carg)
  WRITE(*,*) 'Sorting invoked...'

ELSE IF (numargs.eq.2) THEN
  CALL getarg(1,inputfile)
  WRITE(*,*) ' Input: ',trim(inputfile)
  CALL getarg(2,outputfile)
  WRITE(*,*) 'Output: ',trim(outputfile)

**maybe IF statement for skipping the sorting part using GO TO (how?? IDK.)**

ELSE
  WRITE(*,*) '****** The input and output filenames are missing! ******'
  CALL exit(0)
END IF


INQUIRE(file=inputfile,exist=lex)

IF(.not.lex) THEN
 WRITE(*,*) '**** file ', trim(inputfile), ' Does not exist!'
 CALL exit(-1)
ENDIF

INQUIRE(file=outputfile,exist=lex)

IF(lex) THEN
  WRITE(*,*) '**** file ', trim(outputfile), ' exists!'
  WRITE(*,*) 'Delete and run the program again'
 CALL exit(-1)
ENDIF 

**estimation of records in input file**

**reading input file**

**bubble sort** (to be skipped or not depending upon command line argument)

**some more computation**

END    

I hope I was clear enough! looking forward to your suggestions and guidance.

PT

1

There are 1 answers

1
Ian Bush On BEST ANSWER

I wouldn't use goto. I'd rather do something akin to the following

Logical :: sort_needed

sort_needed = .True.

... skip first part ...

ELSE IF (numargs.eq.2) THEN
  CALL getarg(1,inputfile)
  WRITE(*,*) ' Input: ',trim(inputfile)
  CALL getarg(2,outputfile)
  WRITE(*,*) 'Output: ',trim(outputfile)

  sort_needed = .False.

ELSE
  WRITE(*,*) '****** The input and output filenames are missing! ******'
  CALL exit(0)
END IF

... skip to sort ....

If( sort_needed ) Then
   Call sort( appropriate_arguments )
End If 

...

End

In general avoid goto, it tends to lead to confusing code. While I'm not a zealot in modern Fortran it's very rarely the best way - in fact I honestly can't remember the last time I used it in anger

A few other things

1) This one is easy enough to follow but get into the habit of posting complete code, rather than fragments. More often than not it clarifies what you are trying to ask.

2) Bubble sort is a horribly inefficient algorithm. At least use something like selection sort as long as the files are not to big. However it is likely that something like a good quick or heap sort will save you lots of time in the long run

3) Fortran has standard ways of accessing the command line, and iargc and getarg are NOT the way of doing it. I suggest you look up and use command_argument_count and get_command_argument, any good, up to date Fortran book will cover these