Least Action

Nontrivializing triviality..and vice versa.

Archive for the ‘Programming’ Category

NVIDIA CUDA on Fedora 20 x86-64 with NVIDIA Optimus

leave a comment »

So after weeks of experimentation, and frustration, I think I have finally gotten CUDA to work on my MSI GE 70 2OE-071 laptop. As you may know, NVIDIA Optimus is a technology that enables power-sharing and switching between Intel HD and NVIDIA GPUs on laptops. For some reason, MSI and some other manufacturers have disabled the option to disable Optimus technology from within the BIOS. Now as far as I know, Linux has shaky support for Optimus, and while I had no trouble with any Linux distro on my previous Dell XPS laptops (which did have Optimus) I just couldn’t get NVIDIA discrete graphics to work on my MSI “gaming” laptop, which is a shame, because much of my work involves (or has involved) the use of CUDA, and I am not eager to spend my programming time on Windows.

This is not an exhaustive guide, and I invite suggestions and corrections from users who may stumble upon this blog post. I tried to reproduce the scenario on my own laptop in order to make sure I didn’t miss anything.

Typically there are six stages in NVIDIA CUDA installation on Linux:

1. Get rid of the nouveau driver if you are installing the proprietary NVIDIA driver by hand.
2. Reboot, get to a text mode (sadly, init doesn’t work anymore, so if you’re a old Unix user like me, you have to google this up before you start playing with your system).
3. Install the NVIDIA device driver.
4. Reboot, make sure a basic X configuration works (known as the “graphical mode” nowadays).
5. Install the CUDA toolkit, and set PATH and LD_LIBRARY_PATH variables.
6. Compile CUDA samples.

Now I believe much of the errors you will face in steps 1 through 6 are from a partial understanding of the way the X Server and Linux work, and that is based on my own experience. Playing with Linux can be a humbling experience even for someone who has used it for over a decade! But this is quite a lot of fun if you can document everything, and remember what you did to get something to work. It also helps if you have a system to play with, so if you have issues with installing and re-installing the OS n times before you can get to perfection, then this guide isn’t for you. Sorry!

Stuff I had to get to run the CUDA samples:
gcc-g++ in Fedora
libXmu-devel in Fedora (this installs the libXmu libraries)



Written by Vivek

April 20, 2014 at 19:16

Posted in CUDA, Linux, Programming

Article: How to Learn Emacs: A Hand-drawn One-pager for Beginners » sacha chua :: living an awesome life

leave a comment »

How to Learn Emacs: A Hand-drawn One-pager for Beginners » sacha chua :: living an awesome life


Written by Vivek

May 19, 2013 at 10:56

Posted in Linux, Programming

Gnuplot with C/C++

leave a comment »

Sometimes, it is convenient to call gnuplot from within your own C/C++ code rather than having the code create a data file, and manually execute gnuplot every time to re-plot using the contents of the file. This is easy if you use Linux: just use a pipe. For instance:

#include <iostream>
#include <stdio.h>
#include <cstdlib>

using namespace std;

int main(){
// Code for gnuplot pipe
FILE *pipe = popen("gnuplot -persist", "w");
fprintf(pipe, "\n");

// Your code goes here

// Don't forget to close the pipe
return 0;

Note that we have had to use C routines to call gnuplot. But there’s a problem with this code: gnuplot will generate the plot only when the pipe is closed. So if you have multiple plot statements, or if you want to refresh the same plot (as for instance, in an animation scenario) you will find that the above approach will result in the plots getting asynchronously updated right at the end.

So how does one ensure that after every plot command, a plot is actually generated (or an existing one refreshed) Immediately? The key is to flush the buffer using fflush. Simply insert


below every plot call to gnuplot. If you have an animation, then in order to make the transitions smoother, you might want to include a delay subroutine. A simple one can be built using the clock routines in time.h. For an example, click here.

Thanks to Vidushi Sharma for bringing these issues to my attention.

Written by Vivek

June 22, 2012 at 14:13

Customizing GNOME3

leave a comment »

The purpose of this post is to help you configure GNOME3 and make it look more ‘zazzy’ than it is. Most of these tweaks have been inspired by the archlinux forums and FP Murphy’s blog, which is the source for learning how to customize GNOME3. I thought it would be a good idea to put it all in one place, mostly for my own reference. Note that I have tested all this on Natty 64-bit. I also thank Charles Bowman for his help and inputs over the past few weeks.

The gnome-tweak-tool does not let you change much, unless you add extensions to the gnome-shell, which really open up the world for a tweaker. I won’t go into the anatomy of an extension, as FP Murphy does an excellent job in explaining it. I will just assume that you know enough about it. If you don’t, you will still be able to get all these tweaks to work, but you won’t understand how they work, and believe me, it won’t be quite as much fun!

Extensions are installed in


but if they are to be made accessible to all users, then they should go in


You can pick either configuration. I will work with ~/.local/share/gnome-shell/extensions from now on, because I am running all this on a single user system. Remember that ~/ is a mnemonic for your home directory. Instead of using the long /home/user_id/, I will just refer to it as ~/ from now on.

One way to play with extensions is to get hold of Charles Bowman’s gnatty pack. It is available from http://cid-32ec6e89f4aa803b.office.live.com/self.aspx/Linux/gNatty%20Pack.tar.gz?wa=wsignin1.0&sa=92561471. Since several extensions seem to be around, with different names, I will just go over the ones I have gotten to work one by one.

Some words of caution before we begin the expedition: some extensions are not compatible with others, and if you install two mutually incompatible extensions, it is possible that the Gnome Shell will not load, and you will be asked to log out. This can be fixed by moving the bad extension out of ~/.local/share/gnome-shell/extensions or simply renaming it:

mv ~/.local/share/gnome-shell/extensions/bad_extension ~/.local/share/gnome-shell/extensions/bad_extension.old

It turns out that this works because each extension has a file inside called ‘metadata.json’ with a field called a unique id (uuid) which must match the name of the directory. Changing the directory name using the mv command above will create a disparity between uuid and the directory name, which will prevent the extension from loading.

If you install a whole bunch of extensions together, it will obviously be hard to debug any problem, because you don’t know which extension is the culprit. So it is best to install one extension at a time, and restart the GNOME Shell. This can be achieved either by logging out and logging back in, or pressing Alt + F2 and typing r and pressing Enter. This restarts the shell. Sometimes though, restarting the shell this way results in spurious errors which may suggest that the extension is messing up something. So, I do prefer to double-check by logging out and logging back in again, though this is relatively cumbersome as it will close all open applications.

With this preliminary knowledge, we are ready to embark on the first step. The basic algorithm is to create two files, extension.js and metadata.json, for every extension. Each extension resides in its unique directory (folder) in ~/.local/share/gnome-shell/extensions/.

Extension #1: Move Clock

The default position of the clock in Gnome is in the center of the top bar. This is irritating for those of us who are used to the old Gnome2 interface where the clock would appear on the right. This is easily fixed.

cd ~/.local/share/gnome-shell/extensions
mkdir Move_Clock@rmy.pobox.com
cd Move_Clock@rmy.pobox.com

Now create two files using your favorite editor, extension.js and metadata.json with the following contents:


const St = imports.gi.St;

const Main = imports.ui.main;

function main() {
    let children = Main.panel._rightBox.get_children();
    Main.panel._rightBox.insert_actor(Main.panel._dateMenu.actor, children.length-1);

    Main._nWorkspacesChanged = function() {};
    Main._queueCheckWorkspaces = function() {};
    Main._checkWorkspaces = function() {};


"shell-version": ["3.0"],
"uuid": "Move_Clock@rmy.pobox.com",
"name": "Move Clock",
"description": "Move clock to left of status menu button"

Restart the shell. The clock should now be visible on the right hand side of the top bar. We can customize it further, to display the time and date in a detailed fashion.

First start gnome-tweak-tool, and click on Shell. You should see a window which looks like this:

gnome-tweak-tool shell tab

You can toggle the ON/OFF switches for ‘Show date in clock’ and ‘Show week date in the calendar’.

Some more date and time settings can be changed using gnome-control-center. Type


in a terminal window and click on Date and Time. Note that this tool does not allow you to show a seconds counter in the clock. If you wish to display seconds, fire up a gnome-terminal window and type

gsettings set org.gnome.shell.clock show-seconds true

This will display a live ticking seconds counter.

Note that the status of presently installed gnome-shell-extensions can be determined from and changed within gnome-tweak-tool. Every time you install an extension manually and restart the shell, by default it is enabled.

Under Construction

Written by Vivek

June 1, 2011 at 15:09

Posted in Linux, Programming

Evaluation of Complex Error Functions erf(z) using GSL/C

with 5 comments

For a small QFT calculation, I needed to numerically evaluate the imaginary error function erfi(x) = erf(i x). But it turns out that GSL (and most other numerical recipe code I could find) can only deal with erf(x), where x is real. Here’s a poor man’s implementation of erf(z) through a standard Taylor expansion,

erf(z) = \frac{2}{\sqrt{\pi}}\sum_{n = 0}^{\infty}\frac{(-1)^n z^{2n+1}}{n!(2n+1)}

The catch here is to deal with propagation of errors in the complex Taylor series, and to also somehow benchmark the results. Well, I haven’t been able to think about this yet, but I was able to confirm that for real arguments, my Taylor series code is about as good as the GSL error function gsl_sf_erf(x).

I am working on a CUDA implementation of this now, because in my project, I need to perform a numerical integration over the error function, which is quite intensive even for Mathematica. For now, I’m just sharing the serial implementation. (Hint: if you can write wrapper for each of the gsl functions inside my Taylor series calculating method, you can embed them within a __global__ kernel call through a struct. Alternatively — and less fun — you can just parallelize the for loop.)

/* Function to compute erf(z) using a Taylor series expansion
/* Author: Vivek Saxena
/* Last updated: January 28, 2011 21:11 hrs */
#include <gsl/gsl_complex_math.h>
#include <gsl/gsl_sf_erf.h>
#include <gsl/gsl_sf_pow_int.h>
#include <gsl/gsl_sf_gamma.h>
#include <stdio.h>

#define PI 3.1415926543
double cz = 2/sqrt(PI);
const int TERMS = 10; // no of terms to use in the Taylor series

gsl_complex erfTaylor( gsl_complex z, int trunc ){
  gsl_complex res = gsl_complex_rect(0,0),
  num  = gsl_complex_rect(0,0),
  den  = gsl_complex_rect(1,0),
  snum = gsl_complex_rect(1,0),
  temp = gsl_complex_rect(0,0);
  for(int i = 0; i < trunc; i++){
     snum = gsl_complex_rect( cz * gsl_sf_pow_int(-1, i), 0 );
     num  = gsl_complex_mul(snum, gsl_complex_pow_real(z, 2*i+1));
     den  = gsl_complex_rect((2*i + 1)*gsl_sf_fact(i),0);
     temp = gsl_complex_div(num, den);
     res  = gsl_complex_add(res, temp);

 return res;

int main ( void ){
   printf( "Real error function\n\n");
   for ( float i = 0; i <= 1; i += 0.01 ){
     float gslerror = gsl_sf_erf(i);
     float taylor   = GSL_REAL( erfTaylor( gsl_complex_rect(i, 0), 10 ) );
     printf("erf(%f): gsl = %f, taylor = %f, mag error = %f\n", i, gslerror, taylor, abs(gslerror-taylor));

   gsl_complex t, arg;
   printf( "\n\nImaginary error function\n\n");
   for (float i = 0; i <= 1; i += 0.01 ){
      /* this would be your generic argument z in erf(z).
       * so if z = x + iy, then
       * arg = gsl_complex_rect(x, y);
      arg = gsl_complex_rect(0, i);
      t   = erfTaylor( arg, TERMS );
      printf("erf(%f + i %f) = %f + i %f\n", GSL_REAL(arg), GSL_IMAG(arg), GSL_REAL(t), GSL_IMAG(t));
   return 0;

Written by Vivek

January 28, 2011 at 21:12