Useful links


Easter calculator (Norwegian)
MPI tutorial
API search engine
PETSc documentation
Gnuplot - Not so frequently asked questions
Python - language reference (in norwegian)
Seven habits of effective text editing (Vim)

Check if a file exists in C/C++

In C/C++ there is a function stat in sys/stat.h that is normally used to get file attributes, and returns false if it failed. This article suggests using stat to check if a file exists instead of the perhaps more common and flawed method on using functions such as fopen. Be sure to check it out for an example on usage.

Using Vim to write text, latex etc.

Using Vim to write text can be annoying as it does not behave as a word processor: lines do not break when continuing beyond the window width. To make Vim behave more like a word processor you need to specify set the textwidth, then turn on linebreak. To turn these two options on type:

set textwidth=79
set linebreak
Reference: http://vimdoc.sourceforge.net/htmldoc/usr_25.html#25.1

Regex in Vim revisisted

Previously i wrote an example in Vim using regexs to add error checking to a line containing PETSc code. After some usage i found it to be too general, i wouldn't work correctly on lines ending with comments. After experimenting with ^txt2regex$ i came up to a better suiting regex:

:s_^\([ \t]*\)\([^/]*\|.*$\)_\1ierr = \2 CHKERRQ(ierr);_
The first group matches zero or more whitespaces or tabs, the second group matches either zero or more non-/ character or any character up to the last character. If you are having trouble with a regex, be sure to check out ^txt2regex$. A tool that interactively helps you convert human sentence to a regex. Output results are immediate, in several languages/programs including Vim. It does have its weaknesses, like undoing a typo, but its usefulness possibly outweighs them.

Regex/regexp in Vim

Working with an editor such as Vim has certain advantages over other editors. One of these advantages is using regexs inside the editor, then mapping it to an available key, say F11. I choose to do this on repetive tasks. I suggest others do the same, for three reasons: save time, avoid typos, and learn regexs. For instance when programming using the PETSc library, while searching for errors, i usually change lines like
        MatSetValues(...);
to
        ierr = MatSetValues(...); CHKERRQ(ierr);
For this one can use the regex:
:s_^\([ \t]*\)\([^/]*\|.*$\)_\1ierr = \2 CHKERRQ(ierr);_
Then reapply the regex either with & or mapping it to a key, then reapplying it with a stroke of the key.

Another very useful regex map is to map a regex to uncomment and comment a line. I use

:map <F3> :s_^_//_<cr>
command to map the F3 button to comment a line and
:map <F4> :s_^//__<cr>
to map uncomment a line using the F4 button.

For an overview over regexs from inside Vim see: vim-regex

SSH disconnecting?

Working over SSH from home might have serious stability issues, which is likely to be caused by NAT. If running the SSH command from Linux, one can either use
ssh -o TCPKeepAlive=yes
However this said to be spoofable, so a better solution would be to use
ssh -o TCPKeepAlive=no -o ServerAliveInterval=15
The ServerAliveInterval=15 option makes the client send requests to the server over the encrypted channel if no messages has been recieved in the last 15 seconds.

Reference: ssh-stable-con - includes a description for setting KeepAlive with Putty

Compiling Diffpack with other libraries

This is easiest done as described in "the diffpack book" by typing
Make MODE=opt link2dp > DpMakefile.defs
in a folder containing a Diffpack Makefile (directory created using Mkdir). This will generate variables defining header paths, and libraries needed to compile Diffpack code. To use, copy the DpMakefile.defs file to the directory with the Makefile, then add this line to the Makefile:
include DpMakefile.defs

Common C programming error

Quite often when compiling a C program one will get this error:
warning: return makes pointer from integer without a cast
For a novice programmer it may be difficult to figure out. When a function is not prototyped the return type defaults to int, which happens when a function name is misspelled. So check the spelling of the function name called on the line with the return statement it occured on.

Killing several processes

Logging on to the ifi server without X, then running Matlab as a background process, would open the non gui version. Several processes were spawned (close to 10), instead of manually deleting them as
ps |grep MATLAB
kill -9 PID
we can combine several commands to automatically kill all MATLAB instances:
kill -9 `ps | grep MATLAB | awk '{print $1}'`
The line above is also a very good example on basic awk usage.

This is where I found the original version of the line above: Kill on wikipedia.org

Powerful Unix/Linux Tools

Setting up Buildbot on fenics.org required alot of configuring. The Buildbot consisted of a so called multimaster (see BuildBrigade) running several BuildMaster-s each having a seperate master.cfg file. A total of 9 master.cfg files, not quite as much as the BuildBot on the Gnome project, but enough to be tedious to configure manually. I used find in a combination of sed to reconfigure. First i used one combination to change the path of /home/mads to /home/buildbot in the master.cfg files:

find . -name "master.cfg" -exec sed -i "s_/mads_/buildbot_g" {} \; -print
I also used a combination to rename the Builder named "x86-linux-normal" to "x86_linux":
find . -name "master.cfg" -exec sed -i "/^[^#]/ s/x86-linux-normal/x86_linux/g" {} \; -print
And finally I used it to start and stop the Buildslaves:
find . -name "*.tac" -execdir buildbot stop . \; -print
find . -name "*.tac" -execdir buildbot start . \; -print
I found these resource very useful in figuring out the details:

Sed tutorial
Find on wikipedia.org

About Mads Fredrik Skoge Hoel

Spring 2008 - I will be finnishing my master thesis. And on first of february, I will start working at Norske System Arkitekter.

Autumn 2007 - I was working on my master thesis at Simula Research Laboratory.

Spring 2007, I was group teacher in Numerical Computing (inf-mat2351). Though no longer updated, here is a webpage here, where you can find some possible solutions to some exercises.