Tuesday, November 6, 2012

CUDA and Fortran

Installing CUDA on Linux Mint 13 MATE 64 Bit

These notes are based on these notes,
http://sn0v.wordpress.com/2012/05/11/installing-cuda-on-ubuntu-12-04


First thing, download the Ubuntu 11.0 64 bit version of CUDA 5.0
https://developer.nvidia.com/cuda-downloads

This downloads an installer script.  When I did this, the file was called (cuda_5.0.35_linux_64_ubuntu11.10-1.run).  This master install script contains scripts to install the a) CUDA Toolkit, b) Nvidia display driver and, c) SDK code samples.

The main points for this install are the following,
1) You can use either your own Nvidia display driver or the provided one.
2) The samples are best installed after the other two items

First install the requisite graphics packages,
sudo apt-get install freeglut3-dev build-essential libx11-dev libxmu-dev libxi-dev libgl1-mesa-glx libglu1-mesa libglu1-mesa-dev

Next split the install script into its three components.  This will be useful later.
sh cuda_5.0.35_linux_64_ubuntu11.10-1.run -extract=<your/path>


If you are going to use the display driver that comes with the installer then ... 



Add the following lines to the file /etc/modprobe.d/blacklist.conf 
blacklist amd76x_edac
blacklist vga16fb
blacklist nouveau
blacklist rivafb
blacklist nvidiafb
blacklist rivatv

Remove whatever nvidia drivers are present on your system.
sudo apt-get remove --purge nvidia*
Now, in order to install a new display driver we can't be running the X-server so log out and press Ctrl+Alt+F1 to switch to a text based login (you may have to switch to Ctrl+Alt+F2 if this dies when you kill the mdm service).  Next, change to the directory where the install script is.    First we need to shut down the X-server in Linux Mint
sudo service mdm stop
And now run the install script,
sudo cuda_5.0.35_linux_64_ubuntu11.10-1.run
When I ran this the default install location is in /usr/local and the tool kit and the display driver were installed successfully but the samples will fail.  At this point, you can reboot the machine and with any luck the new display driver will be in action.


If you are going to use the display driver you already have then ... 



Simply run the individual script that installs the cuda tool kit (i.e. the one that is NOT the display driver or the samples).  You shouldn't have to log out of your current X-window session.  You WILL most likely need to copy (or link) the libcuda.so file from wherever your current driver is keeping it to the install directory you chose for the cuda tool kit.  For example on my system I did,
 ln -s /usr/lib/nvidia-current-updates/libcuda.so.304.43 /usr/local/cuda-5.0/lib64/libcuda.so
 and
 ln -s /usr/lib/nvidia-current-updates/libcuda.so.304.43 /usr/local/cuda-5.0/lib64/libcuda.so.1


No matter what display driver your are using ...



Now we need to update the PATH and LD_LIBRARY_PATH environment variables
export PATH=/usr/local/cuda-5.0/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-5.0/lib64:$LD_LIBRARY_PATH
Now that the Toolkit is installed and the paths are set we can install the samples by running the separate install script we extracted above.  All of the samples can be build by running make in the Samples directory.  Some of them require MPI so you should install the openmpi packages from the repository.  Run some of the samples to make sure everything is good.  A really cool one is
cuda-5.0/samples/5_Simulations/smokeParticles
 Now you should be ready to go!


New GT Setup - Laptop

Started configuring my work machines for Georgia Tech today.  Going to go over the laptop in this post.  Workstation on deck.  The laptop is a Lenovo T530 with an Nvidia NVS 5400M GPU and an integrated Intel GPU. 

For the linux distribution I chose Linux Mint 13 (Maya) MATE 64 bit edition
http://www.linuxmint.com/download.php

This is a nice distro.  It is a continuation of Gnome 2.0 and if you liked that desktop manager you'll like this.  For the install I disabled the dedicated card in BIOS and proceeded with the initial install.  After the OS was installed (but before running any updates) I installed the Nvidia drivers provided in the Ubuntu repository.  I had to be careful to make sure that the proper versions were installed.  What worked the best for me was,

nvidia-current (295.40-0ubuntu1.1)
nvidia-settings  (295.33-0ubuntu1)

I then rebooted the laptop, disabled the integrated GPU and activated the dedicated GPU in BIOS and was up and running.  Running the software update might change the Nvidia packages that are being used so make sure the above are the ones that are active.   The reason for going into the BIOS is that this dedicated GPU comes with Optimus technology which can switch between the Intel and Nvidia cards to save battery life.  This doesn't really work in Linux.  You can try an application called bumblebee to make this work, but for me the extra battery life isn't worth the hassle.  Going to try some CUDA tests soon.

Here are some commands for the .bashrc file,
alias rm='rm -i'
alias emacs='emacs --reverse-video'


export PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[01;34m\] \W \$\[\033[00m\] '

Monday, December 12, 2011

f2py on cosma

Turns out you need to use the --f90exec= option on cosma as opposed to the -fcompiler=.  The executable can be determined by echo $FC after you've loaded a module in.  The rest of the options are standard f2py stuff.
f2py --f90exec=[$FC] -c -m fortran_code fortran_code.f90

Sunday, December 4, 2011

More Calling Fortran 90 from Python

Wrote some more Fortran to be compiled with f2py and called from Python.  Ran into some issues and solutions.  The first had to do with variable precision.  I usually use the selected_real_kind() type functions to define the precision of the variables.  This is a tricky thing for f2py to parse so I ended up using the simple mapping real(4) for 32 bit reals and real(8) for 64 bit reals.  Second, it turns out you have to be careful using automatic and assumed shape arrays.  Its easiest for f2py to parse the fortran if the size of assumed shape and automatic arrays are included in the fortran subroutine argument list.  f2py is even nice enough to make the size an optional argument in the python version of the function.  For example, this subroutine was giving me segmentation faults,


subroutine calc_arr( inarr,outarr )  
real(8),dimension(:),intent(in) :: inarr
real(8),dimension(size(inarr)-1),intent(out) :: outarr
outarr = inarr(2:nn) - inarr(1:nn-1) 
end subroutine calc_arr 


This one works fine,

subroutine calc_arr( inarr,nn,outarr )  
real(8),dimension(0:nn-1),intent(in) :: inarr
integer(4),intent(in) :: nn
real(8),dimension(0:nn-2),intent(out) :: outarr
outarr = inarr(1:nn-1) - inarr(0:nn-2) 
end subroutine calc_arr 

The extra argument helps f2py determine the array sizes.  In addition, you can use the nice fortran feature of being able to label the array entries starting with whatever index you like (including zero to make them more like python arrays).  I also learned you should stay away from the logical type in Fortran as I don't think there is a solid mapping into a C/f2py type and it gave me unpredictable results. Simply use integers instead of bools for logical variables.





Thursday, November 3, 2011

Compiling HDF5 1.8.6

Installed HDF5 on yinzer today.  Just some notes about the config.  This is what I used,

sudo /home/galtay/Downloads/linux-programs/hdf5/hdf5-1.8.6/configure FC=gfortran-4.5 --enable-fortran --prefix=/opt/hdf5-1.8.6-gfortran-4.5
sudo make
sudo make install

Compiling OpenMPI 1.4.4


Installed OpenMPI on yinzer today.  Just some notes about the config.  This is what I used,

sudo ~/Downloads/linux-programs/openmpi-1.4.4/configure --prefix=/opt/openmpi-1.4.4-gfortran-4.5 F77=gfortran-4.5 FC=gfortran-4.5
sudo make all install