Saturday, October 30, 2010

Run MPI Programs in Eclipse with 1-Click

  1. Click on the project name in Project Explorer.
  2. Right click and choose "Properties"
  3. Choose "Run/Debug Settings"
  4. Edit one of the launch configurations or create a new one.
  5. Under the tab "Main", change C/C++ Application to be "/usr/local/bin/mpirun"
  6. Go to the tab "Arguments", change the program arguments to be "-np 4 ${workspace_loc:project_name}/Debug/project_name"
Then you can press "Ctrl+F11" to run your MPI program in Eclipse.

Open MPI Installation

In Ubuntu, key in the following command:

 In order to test if your setup is correct, download this sample open MPI file
Then execute the following commands:
where the parameter -np specifies the number of cores you would like to run the test file.

Tuesday, September 7, 2010

How to Use OpenGL with Qt4 in Ubuntu 10.04

When opening an example about OpenGL in Qt Creator, compiling it and running it, you may encounter several errors regarding missing files, such as gl.h and glu.h. They are OpenGL headers. To install the corresponding libraries and headers in Ubuntu 10.04, type the following commands:
  • sudo apt-get install libgl1-mesa-dev 
  • sudo apt-get install libglu1-mesa-dev
The information about Mesa can be found here:
Although the above two commands only request for two libraries, the whole mesa system will be installed as well.

Mesa3D is one of the implementation of OpenGL API. For others, see

Tuesday, June 8, 2010

How to Use Fortran Code in C++

Declare the Fortran function first:

extern "C"{
    // double precision dot product:
    double ddot_(
            const int*        n,      // dimension
            const double*     dx,     // []vector x
            const int*        incx,   // index increment of each access of x
            const double*     dy,     // []vector y
            const int*        incy    // index increment of each access of y
    );
}

Then use it.

Sunday, June 6, 2010

How to Publish Source Code in Blogger

There are several ways to do the job. But probably the simplest one is described here. Many ways involve having another host server. The method suggested in the link is based on "Syntaxhighligher." Simply tweak the template HTML file as the following instructions:
  1. Go to Layout -> Edit HTML 
  2. Add the following lines right before </head>
    <link href='http://syntaxhighlighter.googlecode.com/svn/trunk/Styles/SyntaxHighlighter.css' rel='stylesheet' type='text/css'/> <script language='javascript' src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shCore.js'/> <script language='javascript' src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushCpp.js'/>
     
  3. Add the following lines right before </body>
    <script language="javascript">

    dp.SyntaxHighlighter.BloggerMode();

    dp.SyntaxHighlighter.HighlightAll('code');

    </script>


  4. Save the template. Then you are done.


How to publish source code?
  1. Type your article as usual.
  2. Copy your code to here.
  3. Copy your encoded code from the link to where you edit your article.
  4. Enclose the code by
    <pre name="code" class="cpp">
    </pre>


  5. Then publish your post as usual.

Saturday, June 5, 2010

Install and Link GotoBlas in Eclipse CDT

What is BLAS?
BLAS is the acronym of "Basic Linear Algebra Subprograms." It computes Level 1 (vector-vector), Level 2 (matrix-vector) and Level 3 (matrix-matrix) BLAS operations which are the basis of higher level operations such as linear system solve, least-squared solve, eigen-value decomposition and QR decomposition which are computed in Lapack. The debut of BLAS dates back to 1979 [Lawson1979] written in Fortran and later many higher level packages such as Eispack, Linpack (around 197x-198x) and Lapack (since 1992) built on top of it. Lapack (in Fortran as well) gradually becomes a success and widely used in many programs although many competitors showed up later aiming at its infamous interface (mostly due to the deficiency of Fortran), but Lapack's position in numerical computation seems never shaken by its competitors. Since BLAS is the core of Lapack, BLAS becomes an inevitably important component. (Note 1) Now the term "BLAS" is more like a concept than a specific interface or an implementation: any library which provides Level 1 to Level 3 BLAS operations can be called as a BLAS. For example, Boost::uBlas is a pure C++ implementation of Level 1 to Level 3 BLAS not with a different interface. For backward compatibility, only the one with an "exact" BLAS interface, or a "BLAS implementation" is compatible with Lapack hence interesting. Others implemented with "BLAS functions"  (e.g. uBlas) rather than a "BLAS interface" may grow up as an inevitable library only when an important higher level library relies on it, just as the relationship between Lapack and Blas. (A new and interesting Lapack competitor is Flame, which is also based on BLAS.)

What is GotoBlas?
GotoBlas2 is a Fortran and assembly code implementation of Blas (interface). It is considered the fastest (or one of the few fastest) Blas libraries on the earth. The original developer is Kazushige Goto, who was a research associate in University of Texas at Austin [1]. GotoBlas is manually optimized by writing architecture specific assembly code. Its performance is usually compared with another very fast Blas library: ATLAS. Contrast to the manual optimization strategy of GotoBlas, ATLAS automatically executes several subroutines during its installation on a single machine in order to find the best parameters (e.g. cache sizes). Some experiments report that GotoBlas outperforms ATLAS, for example in [2] and [3].

Why GotoBlas?
  1. GotoBlas is very fast as mentioned before. If speed is your first priority
  2. It supports multi-threads.  
  3. It is available in two licenses: commercial license and open source license. Re-distribution is not allowed but you can always download it here.
  4. Lapack is a widely-acceptable dependency library that has been a "must" for many machines for the numerical computation purpose. Even when your software users don't prefer installing a new library, they can still evaluate your code by using the original Lapack and Blas libraries on their machines.
  5. Some Lapack functions are built along with GotoBlas, such as some driver routines: LU linear system solve (xGESV), the inverse of a positive definite matrix (xPOTRI),  some computational routines: LU decomposition (xGETRF) and Cholesky factorization (xPOTRF), ans some auxiliary routines. If all you need are to solve a linear system and to do some BLAS operations, then installing GotoBlas suffices.
  6. The installation is very easy.
How to install GotoBlas?
Very easy. Simply download it, decompress it, make it and move it to the preferred folder. The detailed steps are in the following:
  1. Here I use GotoBLAS2-1.13 as an example.
  2. Install a Fortran compiler if there is none in your computer (if it is in ubuntu)
    >> sudo apt-get install gfortran
  3. Download it here.
  4. >> tar -xvf GotoBLAS2-1.13.tar.gz
  5. >> cd GotoBLAS2
  6. Edit Makefile.rule. Follow the comment instructions.
  7. >> make
  8. The command "make" will compile the source code according to the rule you wrote in Makefile.rule. It will generate four files in the same folder:
    libgoto2.a
    libgoto2_architecture.13.a
    libgoto2.so
    libgoto2_architecture.13.so
    The first one is the symbolic link to the second one because a general compiler usually searches for a name like it instead of the second one. The second one is the static library itself. architecture changes according to your computer. The third and the last one are the dynamic counterpart of the first two. See here for the knowledge of .a and .so libraries.
  9. If all you need is the static library. Simply move libgoto2_architecture.13.a to /usr/lib and create a symbolic link to that:
    >> sudo ln -s libgoto2_architecture.13.a libgoto2.a
    Then you are done.
  10. If you need the dynamic link library, then you need some extra steps. The older version of GotoBlas doesn't compile a dynamic link library by default. But even if this 1.13 version provides .so library, it seems not working properly. Instead, we can still build a dynamic library ourselves:
    >> mkdir temp

    >> cp libgoto2_architecture-r1.13.a temp
    >> cd temp
    >> ar x libgoto2_architecture-r1.13.a
    >> gfortran -shared -lpthread -Wl,-soname,libgoto2.so -o libgoto2.so.1.13 *.o

    The meaning of the last command:
    1. gfortran: use gfortran to compile
    2. -shared: create a shared object
    3. -lpthread: use the pthread library. No harm to keep it there if your GotoBLAS is single threaded.
    4. -Wl: the options separated by comma after -Wl will be passed to the linker, ld in gcc.
    5. -soname,libgoto2.so: passed to ld. It specifies the internal name libgoto2.so of the library which will be used and linked by other programs accordingly. See here for more ideas about ld.
    6. -o libgoto2.so.1.13: the file name after compilation
    7. *.o: all .o files will be compiled
  11. Move libgoto2.so.1.13 to the folder you prefer, usually /usr/lib, and create a symbolic link to that
    >> sudo ln -s libgoto2.so.1.13 libgoto2.so 
  12. And tell the linker ld that you add a new dynamic library by entering
    >> sudo ldconfig
    So the linker can find it when a program requests to link the new library. Then you are done.
How to link GotoBlas in gcc / gfortran / g++?
Simple. Just add a library option when you compile. For example in g++,
>> g++ -lgoto2 -o yourprogram yourcode.cpp
Notice that -lgoto2 will be interpreted as linking a library called libgoto2.a or libgoto2.so, so don't put both of them in the library search path (-L) or in /usr/lib altogether. That'll be confusing.

How to link GotoBlas in Eclipse
Let's go back to the title. That will be very easy again.
  1. Create a HelloWorld project.
  2. In Project Explorer, right click on your project name and choose Properties
  3. Go to C/C++ Build -> Settings
  4. In Tool Settings tab, go to GCC C++ Linker -> Libraries 
  5. In Libraries (-l), add "goto2"
  6. In Library search path (-L), add the path of libgoto2 if it is not in /usr/lib
  7. Then you are done!

Note 1:
We are never able to say a library is the best. A library lasts for many reasons: performance, interface, productivity, available licenses, dependencies, backward compatibility, etc, especially backward compatibility. A new library must provide other way better features such as way faster computation or way more efficient interfaces in order to persuade a software engineer to adopt at the risk of lower publicity because the new library may not be available or has some compatible problems on some machines, or the license may limit the use of the library, so as the software. A famous example of the license issue is about Qt. Before the Qt development company Trolltech was purchased by Nokia, the Qt library only provides GPL and proprietary licenses. The former says the source code should be open as the library you use is. The latter requires very high licensing fee. These two options limit the acceptance of Qt. Now Qt has a new option: LGPL, which says the use of the library is free if it is dynamically linked and the library can be distributed along with the software.  Lapack doesn't have this issue and is free available here.

Monday, May 24, 2010

Ubuntu 10.04 amd64 Desktop on RAID1 Installation

The following are categorized into three parts: general utilities, numerical research related and server utilities.

Install Ubuntu 10.04 on RAID 1


Just don't believe all the above (I put them in the end now) attempts fail. Later I realized I was not alone. Tons of bug reports were posted on-line, e.g. 1, 2, 3, 4, 5, 6, 7, 8. There are at least three different kinds of issues. Most likely there are some bugs in grub2 that cannot handle a RAID more than 2TB and erroneously attempts to access non-existent floating disks even though all simulated ones have been turned off in the bios. Hence a reasonable guess is that this issue is bios dependent (or motherboard dependent). The new version of grub2 is not totally compatible for different One way to workaround is to upgrade from 8.04 LTS to 10.04 LTS. Several has confirmed that would work (see here). This upgrade workaround also works on my Dell Precision T3500 64-bit machine.
  • Install Ubuntu 8.04 LTS on RAID1 through Ubuntu 8.04 LTS amd64 Alternate CD. The RAID disk can be created when you create two equal sized partitions in your system. A new item "Create software RAID" will show up.
  • Follow the instructions here.
    • update-manager --proposed
    • Install updates first.
    • Click "Upgrade" and follow the instructions.
General utilities:
  • Archivers

    • rar: sudo apt-get install rar
    • 7zip: sudo apt-get install p7zip-full
    • both automatically are associated with file-roller (the Gnome default archiver)

  • Multimedia

    • Go to System -> Administration -> Synaptic
    • Search for ubuntu-restricted-extra and install it

  • Latex

    • sudo apt-get install texlive-full
    • sudo apt-get install kile

  • Chinese input method

    • sudo apt-get install gcin
    • set up default input method manager: im-switch -s gcin
    • download the mapping table
      wget http://edt1023.sayya.org/gcin/noseeing-12.tar.gz
    • move the table to the gcin folder
      mv noseeing.gtab ~/.gcin
    • Logout and login

  • Instant messenger

    • sudo apt-get install kmess
    • enable history and turn off annoying pop-ups and sounds

  • Compiz theme and Emerald decoration

    • If following the above ubuntu 10.04 installation procedures, Compiz fusion theme utilities are mostly installed. Here are several missing pieces.
    • In System -> Administration -> Synaptic Package Manager,
      search for "Emerald" and install the only entry with the "decorative" keyword in its description.
    • Also in Synaptic, search for "compiz," find the "Compiz Fusion Icon" and install it.
    • sudo apt-get install compizconfig-settings-manager
    • Goto System -> Preference -> Startup Applications, add a new entry: fusion-icon --no-start

  • Skype
  • Dropbox
  • Visualbox
  • Tomboy
  • nxclient
Numerical research related:
  • MATLAB

    • Go to File -> Preferences -> Keyboard to change to Windows key binding style
    • Since mex in Matlab r2009a (both 32-bit and 64-bit versions) only looks for stdc++.so as a standard C++ library, we need to manually make a symbolic link to the ubuntu built-in library.
      cd /usr/lib
      sudo ln -s libstdc++.so.6 libstdc++.so

  • Sedumi 1.3

    • Add the extracted folder path into Matlab
    • Install Yalmip and run yalmiptest in Matlab. If there is no obvious errors, it's done. Otherwise run "install_sedumi" in Matlab.
      (Note: sometimes the precompiled version along with the sedumi download works fine. But sometimes only the locally compiled version by "install_sedumi" works. It seems always either one fails on a single machine. If you encounter Matlab crashes due to calling the precomiled sedumi, try install_sedumi, and vice versa.)

  • Yalmip R20100512

    • Extract the downloaded file
    • Add the paths listed in README.txt in Matlab
    • Run "yalmiptest" in Matlab 
     
  • spot 250210

    • Extract the file into the folder "spot" (It will complain later if it is not.)
    • Run "spot_install"
    • Run "spot_check" If the first three pass, it is done. It is because Sedumi cannot provide an optimal solution in the test cases. We only check paths and our setup functionality rather than accuracy here.

  • For others, see here.
Server utilities:
  • DynDNS update client (ddclient)

    • See this very decent step-by-step guide
    • Or you can try this.

  • OpenSSH

    • sudo apt-get install openssh-client
    • sudo apt-get isntall openssh-server
    • Edit /etc/ssh/sshd_config if necessary

  • FTP (SSH can do the job if not using public FTP)

    • See here
  • nxserver

      
Miscellaneous:
  • Mount partitions at startup

    • The natural and robust way is to modify /etc/fstab directly. For example, one line of my setup is 
    • LABEL=Spring    /media/Spring    ntfs-3g    rw,auto,nls=utf8,uid=1000,gid=1000,umask=0077,fmask=0077     0    0

      • LABEL=Spring: the name of the partition
      • /media/Spring: mount point
      • ntfs-3g: ntfs format with write ability
      • rw,auto,nls=utf8,uid=1000,gid=1000,umask=0077 (options)

        • rw: support read and write
        • auto: mount at startup
        • nls=utf8: support uft-8 in file and folder names
        • uid: user id, type "id" in the command line
        • gid: group id
        • umask: file and directory accessibility. It is computed in this way.
        • fmask and dmask are separate setups as umask.
      • 0: dump
      • 0: pass. check when reboot if 0.


Appendix:

Not working installation procedure on RAID1
  • See here.
    The following are excerpted from the instructions given above.
  • Use Ubuntu 10.04 to boot your system. Choose "try Ubuntu 10.04"
  • Go to System -> Administration -> GParted to make out two equal sized partitions (if you will use two partitions to create a RAID disk.) Make sure reserve some space for swap area.
  • sudo apt-get update
  • sudo apt-get install mdadm -> Choose "Local Only" when it prompts mail server installation.
  • sudo mdadm --create /dev/md1 --verbose --level=1 --raid-devices=2 /dev/sda1 /dev/sdb1
  • "Continue creating array?" answer: yes
  • Format raid disk: sudo mkfs.ext4 /dev/md1
  • Double click on the "Install Ubuntu 10.04 LTS" icon on the desktop.
  • Install regularly except choosing “Specify partitions manually (advanced)."
  • Specify root/ in md1. Then go through regular installation process.
  • Once the installation is done, DON'T REBOOT! Choose "Continue trying Ubuntu 10.04."
  • Now we are trying to install mdadm for our new installation. The following instructions just change the root from the installation CD to our new installation spot md1

    • Mount md1: mount /dev/md1 /target/ (you can also do it by going to "Places")
    • Bind the folders which will be altered by installing mdadm to this installation CD boot

      • mount --bind /dev/ /target/dev/
      • mount --bind /sys/ /target/sys/
      • mount --bind /proc/ /target/proc/
    • Change root to the new installation: chroot /target
  • sudo apt-get update
  • sudo apt-get install mdadm

    • Here since I don't need to use Postfix as my mail server. I choose Local again. Don't know whether it affects if I need a mail server later.
  • Last, install grub to both raid disks in case that when one of them is dead we can still boot from the other.

    • grub-install /dev/sda
    • grub-install /dev/sdb

Saturday, May 8, 2010

Install Kubuntu 10.04 directly instead of ubuntu + KDE desktop

I did the latter case first, but I encountered several following problems:
  1. gcin input method floating window went dead when the desktop effects are enabled. The dead window made me unable to see which character to choose and it hid a random area on the screen. (It was proved later that this is an issue of gcin. gcin by now cannot function normally in the plasma theme. Installing Kubuntu directly cannot solve this issue)
  2. Sometimes the background became back to Gnome background and there is no way to change it back rather than rebooting.
  3. It is obviously slower than Kubuntu itself.
  4. When the desktop effects are enabled, several kinds of windows cannot be displayed when using alt+tab to switch windows.
Hence it is not suggested to use both window managers on the same machine.

Here are some tweaks I did after installation kubuntu 10.04:
  1. Disable Intel CPU c-state 3 and 4 to stop hissing. (see Ubuntu 9.04, 9.10, 10.04 Installation tweaks in Misc.)
  2. Configure power manager of Thinkpad (see Ubuntu 9.04, 9.10, 10.04 Installation tweaks in Misc.)
  3. Disable annoying kwallet
  4. Configure terminal
  5. Install KMess and disable online-offline notification
  6. Enable desktop effects
  7. Right click on the "K" start menu and launch menu editor to add Matlab after installing Matlab in command lines.
  8. and many others...
After using Kubuntu 10.04 for a while, there are a couple of issues that I cannot solve so now I go back for Ubuntu 10.04...
  1. Mic doesn't work. 
  2. Wireless doesn't work. It seemed connected but only few packets are transmitted. In other words, it was not working properly.

    Tuesday, April 27, 2010

    Writing: Keywords

    This article shows a collection of terms that are usually erroneously used (by myself).

    computation time instead of computational time
    a large percentage of
    a 5% error
    3-D instead of 3D