Course Newsgroup:
The course newsgroup is uw.cs.cs472. Please post questions there, especially those whose answers will be of general interest to the class. For instructions on how to read and post to a newsgroup, check out the FAQ maintained by MFCF.
Matlab:
The IST group maintains a nice set of online references for Matlab including a tutorial.For many of you the C Programming Language will be new. Some of you are coming from Applied Math and will be using Unix and C for the first time; you will need to set aside some extra time to familiarize yourself with the new tools. In addition to these notes, don't forget the general resources provided by MFCF and also the MFCF Consultants who are there to help you with general problems.
Using C is not difficult, but neither is using it incorrectly or using it badly. If you know Perl, C++ or Java then you already know most of the basic syntax, since C is a parent of all of these languages. It is much more primitive, however. It was selected for the course because it is relatively simple, and is frequently used in real life for scientific programming.
Scientific software is extra tricky in that it rarely fails with a simple error. Scientific code is very good at producing answers which look right, but which are quite wrong. Careful programming style (keep it simple) and careful debugging are the key to saving yourself time.
Some resources to get you started with the C Programming Language:
In C it is very easy to write programs that exceed array bounds or which leak memory. Always check your programs using purify. Always correct all of the problems which purify reports.
For more information on purify, see the manual pages by typing
man purifyand
man purerunand read the IBM/Rational Purify Plus information page. In particular, you might want to read
Purify is available in the undergraduate environment under the directory
/software/purify-2002/bin
There is an option built into the makefile we supply for you that compiles your programs with purify. Purify will tell you clearly where there are memory problems in your program and (particularly) where there are none.
Programs which leak memory or run over array bounds are incorrect and will be marked as such.
If you are coding at home then valgrind is a
good,
open source and free alternative to purify.
To build the programs you will submit for your assignments we have provided a makefile. The make command searches for a file of this name in the current directory, then uses the instructions in that file to build an executable program. Normally, you will never have to compile a program using the C compiler; you should only ever need make and the makefile we provide.
To write the code for this course we strongly recommend you follow this general pattern:
unzip assignX.zip(changing the assignX.zip file name, of course).
make debugwhich will create an executable file programDebug in the working directory. To run this program the command is
purerun ./programDebugor without purify (after there are no more errors from purify):
./programDebug
make optwhich produces the program programOpt. This version is free of all of the extra code and overhead produced for debugging. You run it with
./programOpt
The debugging program gdb or the graphical xxgdb is available on the MFCF undergrad environment. If you are working from home you may wish to try the powerful debugger DDD.
Unfortunately, the graphical debugger xxgdb is not really state of the art, but it should be adequate for our purposes.
You have both the GNU and the Sun C compilers available. Try using both of them to compile your code, since they have different strengths and weaknesses and may tell you slightly different things if there are problems.
Before debugging, limit the resources a program can use. You can do this on csh shell with (for example)
limit cputime 30
limit datasize 1024
which stops the program if it takes more than 30 seconds of CPU time or if it wants more than 1024K of memory for data. If you use the ksh shell the commands would become
ulimit -c 30
ulimit -d 1024
Doing this prevents a run-away program.
Run your program with nice. This limits the priority a bit so that you don't swamp other users, but won't affect your actual total CPU time usage. For example:
nice ./programOpt
Create test scripts using your shell (either csh or ksh). This is done by making a simple text file with commands in it, then executing the text file using
source myTextFile
if you use csh or
. myTextFile
if you use ksh. This way you can make sure that, if your tests use command line arguments, that your tests are exactly repeatable.
Learn to use the autocompletion and command line editing functions of your favourite shell. Autocompletion lets you type the first few letters of a command or a file name and press a key like TAB or ESC to have the shell write the rest of it out for you. Command line editing lets the shell use vi or emacs style commands as if you were editing a line of text in those editors. Both features are in most modern shells (tcsh or ksh) and can save a lot of time and headaches.