Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.

Learning how to code

ignacio82
Beginner
5,288 Views
Hi,

I'm trying to learn how to program in fortran. I'm reading "Numerical Methods in Economics by Kenneth L. Judd"

Can someone recomend me a forum to ask questions (very basic questions) about how to code?

Thanks a lot

Ignacio
0 Kudos
43 Replies
mrentropy1
Beginner
2,763 Views
Ignacio,

I don't have an answer to your question but I can comment on your choice of language.

Hope I don't get in trouble for saying this here, but if you're just learning how to code, and especially if you're more interested in economics than engineering and physics, then perhaps Fortran isn't the place to start --- unless you have a good reason for choosing it (?).

In my rather limited understanding of the world of economics, quantitative finance, etc, I'd be suprised at employers looking for Fortran experience -correct me if I'm wrong. As cool as I think modern (90/95/2003) Fortran is, it's more often seen in physics and engineering than finance &c.

Employers in eco/finance/quant - even the number-crunchers - will more likely want experience based in a more functional and/or object-oriented language, rather than an imperitive language like Fortran. Or, if they want something imperitive, it will be C. They also may like Matlab experience it seems.

This means languages like Smalltalk, C++, Scala, OCaml, maybe C# or F#, Python, and maybe even some Lisp derivative. A good strong basis in C is always a good thing, and C++ will be very broadly sought; competence in other languages like Smalltalk will be less broadly useful but also might help net that one job where that's what they're looking for (same can be said for Fortran, too).

Anyway....

IMO by far the best language for learning programming is Python. It also happens to be a fantastic language for learning numerical methods, which it sounds is what you're after - mind you it's not a good language for actually *implementing* numerical methods since it's sloooow, but it's good for *learning* them. It can be programmed in imperitive or functional style, it's object-oriented, and it's insanely ubiquitous as well.

The other cool thing about Python is that it has many syntactical features in common with modern Fortran. :)

Here's what I recommend for the python setup on Ubuntu. Of course you'll already have python, but you'll also want the following:
- ipython
- numpy (numerical python)
- scipy (scientific python)
- matplotlib (for 2D matlab-like plotting)
- mayavi2 (for 3D plotting)

This will give you an interactive graphical environment where you can play around wth numerical methods and get immediate visible results. It's a lot like Matlab - not a copy, mind you, but similar functionality. And FREE.

All of this is super super easy to install using Ubuntu's package manager.

Along with scipy, you'll get f2py, which is Pearu Peterson's fortran-to-python interface generator. With f2py, you can take your Fortran module source code (so long as it doesn't have derived types, and some other limitations - altough the f2py team is working on adding these features) and compile it to a python module, like so, where "foo_mod" is the name of the python module you'd like to generate, and the underlying Fortran module is called bar_mod:

$ f2py -c --fcompiler=intelem -m foo_mod yercode.f90

(Oh by the way - if you are new to linux, get used to the command line; see Applications -> Accessories -> Terminal on standard Ubuntu 9.04 install.... you probably already knew that tho....a book on the bash shell isn't a bad idea either...)

Start up ipython from command line and start using your Fortran like so:

$ ipython -wthread <---- good habit to get into calling ipython w/ -wthread flag, but not necessary


In [1]: import foo_mod

In [2]: foo_mod.bar_mod.func()
(etc etc)

...where your Fortran code is (for example):

module bar_mod
contains
function func()

end function func
end module bar_mod

This works pretty well in my experience. That way you can also learn Fortran, and have a nice graphical front end to plot out your calculations on the fly and get immediate feedback, which is always a good thing when you're learning stuff. You can do the same thing for Python integration with C or C++ instead of Fortran, using SWIG, Pyrex, or any number of other packages.

And, if you decide to continue with Fortran, then welcome to the club. I have heard many stories where mentioning the word "Fortran" on the resume landed the candidate the job, so it can be a definite asset. Just depends on the field and the niche...

Good luck,
Peter
0 Kudos
mrentropy1
Beginner
2,763 Views
Ignacio,

Hey, I checked out some of the code from that book at:

http://bucky.stanford.edu/numericalmethods/PUBCODE/DEFAULT.HTM

(hope it's ok to post that link here....)

Very cool. OK so Judd's book looks like it makes heavy use of the netlib repositories. This is good.

Most of the netlib code is written in Fortran 77, and the rest is typically either Fortran 90+, C, or C++.

Despite anything I said previously, if you are doing numerical stuff of this nature then knowing Fortran 77 is a *DEFINITE* asset. However, I still would not recommend it as a first and only language - just my opinion. On the other hand, Fortran 77, however, is rather simple and easy to read, so it's pretty easy to pick it up fast enough to start to make sense of the code in this book..... In fact you will likely encounter people in econ/quant finance who are completely unaware that under the hood they are actually using quite a bit of Fortran 77. :)

BTW I doubt you'll encounter any problem at all using f2py to compile netlib Fortran 77 code to a python module. Same for using SWIG for any C or C++ code you encounter. PLUS, with numpy/scipy, you may actually find that somebody else has already done it for you - much of the netlib repository code has been built into scipy.

Peter
0 Kudos
ignacio82
Beginner
2,763 Views
Thanks for the reply.
The reason I want (need) to learn fortran is because I'm going to take a class next semester that is going to use it (advanced macro in a phd program).
Thanks again
0 Kudos
roddur
Beginner
2,763 Views
Quoting - ignacio82
Hi,

Can someone recomend me a forum to ask questions (very basic questions) about how to code?

Here itself you will find good people to help you!
Also, you can try google groups:comp.lang.fortran
0 Kudos
ignacio82
Beginner
2,763 Views
Quoting - roddur
Here itself you will find good people to help you!
Also, you can try google groups:comp.lang.fortran
I wasn't sure about asking here...
This is the first thing I'm trying to do (i guess is easy)

1. Write a subroutine (procedure, etc.), called ADDXY, that takes as input two scalars, x and y, and
produces x + y as output.
Write a program that reads two scalars, x and y applies ADDXY to x and y, and prints the answers in a readable fashion. "THE SUM OF x AND y IS "

If someone can help me I would really apreciate it :)

Thanks!
0 Kudos
Kevin_D_Intel
Employee
2,763 Views
Here's a start.

[plain]program sample
  real x,y,res

  print *,"Input a value for x"
  read *,x
  print *,"Input a value for y"
  read *,y

  call ADDXY(x,y,res)

  print *,"THE SUM OF x AND y IS ",res
end

subroutine ADDXY(x,y,res)
  real x,y,res
  res=x+y
end
[/plain]

$ ifort -V sample.f90
Intel Fortran Intel 64 Compiler Professional for applications running on Intel 64, Version 11.1 Build 20090630 Package ID: l_cprof_p_11.1.046
Copyright (C) 1985-2009 Intel Corporation. All rights reserved.

Intel Fortran 11.1-2536
GNU ld version 2.17.50.0.6-5.el5 20061020

$ ./a.out
Input a value for x
1
Input a value for y
2
THE SUM OF x ANDy IS 3.000000
0 Kudos
mrentropy1
Beginner
2,763 Views
Ignacio,

Apologies for the big core dump earlier - too much coffee.

As Kevin Davis showed, this forum is a nice place to start. I don't know of anything comparable offhand.

Some comments:

(1) Re books: Looks like you'll be doing mostly Fortran 77. Modern Fortran textbooks treat the 90/95/2003 standards. Fortran 90 is very different from Fortran 77. You probably need something geared specifically for 77. One book I've found handy is "The Essentials of Fortran" by D. Smolarski. It's super cheap, small, and just covers the basics.

(2) Re editors: As I said earlier, on Ubuntu, gedit is probably a good place to start.

(3) Get comfortable working on the command line in linux. For example, that's where you call ifort from. Have you gotten that to work yet, for a simple program?

(4) There are tons of language reference books, but precious few that treat practical aspects of programming, like how do you deal with separate compilation (combining multiple bits of code into one program), linking libraries, debuggers, etc. I recommend "A Scientist's and Engineer's Guide to Workstations and Supercomputers", by Landau and Fink. It may be out of publication for all I know; my copy came with a floppy (!) - it's an old book. Chapter 8 is an excellent intro to the practical aspects of writing Fortran on a unix (and linux) platform.

That's for starters.

Before TOO long you'll want to figure out:

(5) How to actually plot your data instead of just looking at numbers on a screen. For this you can use Intel's array visualizer, although it's not supported anymore: http://www.intel.com/cd/software/products/asmo-na/eng/compilers/226277.htm OR, you can use python, f2py, and matplotlib together, but this might be a bit complicated if you're starting out. Also there's a ton of other stuff out there - pgplot and gnuplot are two examples. For starters, you can just have your code dump numbers to a file and then plot them with pgplot or gnuplot.

Eventually (but not now) you will want to learn:

(6) How to use libraries in your code.

(7) How to automate building (compiling, linking) programs using "make" or something else like "scons".

(8) How to keep track of changes you make in your code - this is called version control. You could use bzr, hg, or svn, but the easiest by far for a person working in isolation is cvs.

(9) How to use a debugger.

(10) How to use a more powerful editor, or an IDE if you want (but I don't suggest an IDE for starting out).

Peter

Note added later: Intel's Array Visualizer is for Windows has NOT been ported to linux. (Ooops!)
0 Kudos
ignacio82
Beginner
2,763 Views

Thanks a lot :-) This is a great way to learn for me. I really really apriciate the help.
Here are my questions.

1) I'm runin:
[plain]/opt/intel/Compiler/11.1/046/bin/intel64/ifort -V sample.f90
[/plain]
Is it ok if I create a symbolic link in /usr/bin so i just run ifort ? To do it is :
[plain]sudo ln -s /opt/intel/Compiler/11.1/046/bin/intel64/ifort /usr/bin/ifort

[/plain]
?

2)What is the option -V ?

3) The first line in the code is "program sample", you don't need to put ! on front to say that is a comment?

4) The second line says that x,y,res are real numbers?

5) You use a lot "*," what is this for?

6) After i compile it to run it i do ./a.out . Is there a way to make it something different like ADDXY.out ?

THANKS A LOT AGAIN!!!!
0 Kudos
ignacio82
Beginner
2,763 Views

Hi Peter,

No need to apology, you are all being very nice with me :-)

1) I'm going to get one of those book in a couple of weeks when I'm back in the US (I'm in argentina right now and books here are expensives...)

2)I'm using emacs22-x.

3)I'm confortable with the command line, I've being using linux since 1999 (i'm a user not a linux guru)

4)Eventually I think I'm going to use matlab to plot (we use it last semester)

5-10) I'm going to learn all that in a near future, be ready for questions ;-)

Thanks everyone

Ignacio
0 Kudos
mrentropy1
Beginner
2,763 Views
Ignacio,

Creating a symbolic link to ifort won't typically work. It breaks stuff. Instead you should source the appropriate shell scripts that setup the environment variables for you so ifort's in your path.

For example, in my .bashrc I have:
[shell]# Intel compiler stuff:
source /opt/intel/Compiler/11.1/046/bin/iccvars.sh intel64
source /opt/intel/Compiler/11.1/046/bin/ifortvars.sh intel64[/shell]
If you're using csh or tcsh instead of bash then you need to source the csh/tcsh versions of these scripts instead, which are in the same directory. If you're using ksh or zsh, then I dunno.... :)

Regards,
Peter
0 Kudos
ignacio82
Beginner
2,763 Views
Quoting - mrentropy
Ignacio,

Creating a symbolic link to ifort won't typically work. It breaks stuff. Instead you should source the appropriate shell scripts that setup the environment variables for you so ifort's in your path.

For example, in my .bashrc I have:
[shell]# Intel compiler stuff:
source /opt/intel/Compiler/11.1/046/bin/iccvars.sh intel64
source /opt/intel/Compiler/11.1/046/bin/ifortvars.sh intel64[/shell]
If you're using csh or tcsh instead of bash then you need to source the csh/tcsh versions of these scripts instead, which are in the same directory. If you're using ksh or zsh, then I dunno.... :)

Regards,
Peter
I'm not a linux guru :'(.
I'm using whatever ubuntu use by default (no idea what)
Should I edit " ~/.bashrc " and add those 2 lines at the end? is this going to let me run the compiler from any directory as if i had a link in /usr/bin ?
Sorry for my ignorance :'(

Thanks
0 Kudos
Kevin_D_Intel
Employee
2,763 Views

Ubuntu default is bash, so yes, add the two lines as Peter showed to your .bashrc and then execute: source ~/.bashrc

The changes will allow you to just type 'ifort' from any directory (and 'icc' if you need the Intel C++ compiler) without specifying the full path to the compiler.

By adding this to the .bashrc, the same setup will exist when you logout and log back onto your system. The 'source' command is just a short-cut to activate the changes and avoid logging out and logging back in.
0 Kudos
ignacio82
Beginner
2,763 Views
I'm trying to change a little the code and is not working :'(
Can somebody explaing me what I'm doing wrong? and how to fix it?
[plain]!Exersice one JUDD page 27
program one
  real x,y,add, prod, division 

  print *,"Input a value for x"
  read *,x
  print *,"Input a value for y"
  read *,y

  call ADDXY(x,y,add)

  print *,"THE SUM OF x AND y IS ",add
end

  call MXY(x,y,prod)

  print *,"THE product OF x AND y IS ",prod
end

  call DXY(x,y,division)

  print *,"THE quotient OF x AND y IS ",division
end


subroutine ADDXY(x,y,add)
  real x,y,add
  add=x+y
end

subroutine MXY(x,y,prod)
  real x,y,prod
  prod=x*y
end

subroutine DXY(x,y,division)
  real x,y,division
  division=x/y
end

[/plain]

This is the error I get

[shell]~/Desktop/FORTRAN/CODE/JUDD/INTRODUCTION$ ifort -V one.f90
Intel Fortran Intel 64 Compiler Professional for applications running on Intel 64, Version 11.1    Build 20090630 Package ID: l_cprof_p_11.1.046
Copyright (C) 1985-2009 Intel Corporation.  All rights reserved.
FOR NON-COMMERCIAL USE ONLY

 Intel Fortran 11.1-2536
one.f90(15): warning #5427: Program may contain only one main entry routine
  call MXY(x,y,prod)
--^
one.f90(17): warning #5427: Program may contain only one main entry routine
  print *,"THE product OF x AND y IS ",prod
--^
one.f90(18): warning #5427: Program may contain only one main entry routine
end
^
one.f90(15): error #5507: There are multiple unnamed main program declarations in this file
  call MXY(x,y,prod)
--^
compilation aborted for one.f90 (code 1)
[/shell]


Thanks for the help
0 Kudos
rsruss888
Beginner
2,763 Views
I'm also learning how to code, great thread!
0 Kudos
Kevin_D_Intel
Employee
2,763 Views

You have too many END statements in the main program. Simply delete lines 13, 18.
0 Kudos
ignacio82
Beginner
2,763 Views

You have too many END statements in the main program. Simply delete lines 13, 18.

Thanks!!! :-)

One more question.

If I use x=6 y=3 the when i run the program i get "the sum of x and y is" but i would like "the sum of 6 and 3 is"

How do you do that?

Thanks!!!
0 Kudos
Kevin_D_Intel
Employee
2,763 Views
Quoting - ignacio82

One more question.

If I use x=6 y=3 the when i run the program i get "the sum of x and y is" but i would like "the sum of 6 and 3 is"

How do you do that?

A quick method sans any attempt to format the real values is to change the print statement to this:

print *,"THE SUM OF ",x," AND ",y," IS ",res

Which produces:

THE SUM OF 6.000000 AND 3.000000 IS 9.000000

If you want a bit nicer output, you'll need some formatting. One method is to use the following WRITE instead of PRINT.

write (*,'("THE SUM OF ",F6.1," AND ",F6.1," IS ",F6.1)') x,y,res

Which produces:

THE SUM OF 6.0 AND 3.0 IS 9.0

0 Kudos
ignacio82
Beginner
2,763 Views

That is great :-)

Questions:
1) What is F6.1 ?
2) Can somebody answer my questions 2 to 6 in the reply #8?

Thanks!!!
0 Kudos
mrentropy1
Beginner
2,763 Views
Quoting - ignacio82

That is great :-)

Questions:
1) What is F6.1 ?
2) Can somebody answer my questions 2 to 6 in the reply #8?

Thanks!!!

(1) F6.1 is a format specifier. It means "print a real number (that's the 'F' part), make the field (the space on the screen used to print it) 6 characters wide, and display one digit after the decimal place". Why does 'F' mean "print a real number"? Well, you might want to print a real number in "F"ixed format, or in "E"xponential format. Hence, F and E are the two basic format descriptors for printing real numbers. A third is "ES" for, true scientific notation.

Incidentally all computer languages have this problem about how to print out real (or floating-point) numbers, because USUALLY you don't want to know the number to the full equivalent decimal precision to the internal binary representation of the number. So there's always some default behavior and then some formating syntax to learn.

(2.2) The -V flag is just an (undocumented) verbose "version" flag that tells ifort that - in addition to whatever else it's doing - to print out all the version info you might need if you're gonna ask support for help. (Anybody correct me if I'm wrong on this one.)

(2.6) Yes, there is a way to do this: the "-o" flag.
Instead of doing
$ ifort myprog.f

do this:
$ifort -o myprog myprog.f

and your executable will be named "myprog" instead of "a.out". Some people prefer to append a .x suffix on their executables but most people don't.

Why, you might ask, do you have to type "myprog" twice? One reason is that in most circumstances you're not going to be compiling just one source file into an executable, you'll be compiling - or linking, rather - several.

Eventually you will want to learn about the "-c" flag, which makes an *object* file, so you can do precisely that. This will require learning the difference between compiling and linking.

P.S. BTW technically a.out is also an object file, but it's an executable object file as opposed to a relocatable object file, and the latter are just called "object files" as opposed to "executables", by most people anyway....
0 Kudos
ignacio82
Beginner
2,653 Views
Thanks :-) I think I almost fully understand this first exercise. My last question is about the "*," that is all over the code. Can somebody explain that? Thanks

New exercise
[cpp]2. Write a routine FEVAL that takes as input a function of two variables F and two scalars x and y and
produces as output the value of F(x, y). Write a program that reads two scalars x and y, applies FEVAL
to  ADDXY together with the scalars x and y, and prints the answers in a readable fashion.[/cpp]

Thanks!
0 Kudos
Reply