Solarian Programmer

My programming ramblings

Cooking guide for hackers

Posted on June 28, 2012 by Paul

If you are a home working hacker like me, I bet that you have some trouble maintaining a healthy life style. It is so easy to forget about your body when your mind is busy with coding or reading other peoples code. Some of us have problems keeping a nice waistline, others have bigger problems as secondary effects of a sedentary life.

Not long ago, empty bottles of Cola and piles of pizza boxes where an integral part of my room landscape for one or two weeks during some intense coding periods. Fortunately for me, I always revert to more healthy food at the end of my coding sessions. I’m one of those guys that loves cooking a good meal when he is not busy coding, playing or watching some movies. It’s summer now and the market is full of fresh fruits and vegetables, so I have no excuse to eat frozen pizza or preprocessed food.

►  Continue reading


Matrix multiplication on GPU using CUDA with CUBLAS, CURAND and Thrust

Posted on May 31, 2012 by Paul

The code for this tutorial is on GitHub: https://github.com/sol-prog/cuda_cublas_curand_thrust.

Matrix multiplication is an essential building block for numerous numerical algorithms, for this reason most numerical libraries implements matrix multiplication. One of the oldest and most used matrix multiplication implementation GEMM is found in the BLAS library. While the reference BLAS implementation is not particularly fast there are a number of third party optimized BLAS implementations like MKL from Intel, ACML from AMD or CUBLAS from NVIDIA.

In this post I’m going to show you how you can multiply two arrays on a CUDA device with CUBLAS. A typical approach to this will be to create three arrays on CPU (the host in CUDA terminology), initialize them, copy the arrays on GPU (the device on CUDA terminology), do the actual matrix multiplication on GPU and finally copy the result on CPU. Our first example will follow the above suggested algorithm, in a second example we are going to significantly simplify the low level memory manipulation required by CUDA using Thrust which aims to be a replacement for the C++ STL on GPU.

►  Continue reading


Mixed language programming - C++11 and Fortran 2008

Posted on May 11, 2012 by Paul

The code for this tutorial is on GitHub: https://github.com/sol-prog/mix_fortran_cpp.

Different programming languages have different strengths and while you can express any imaginable algorithm in C++11 sometimes you need to interface your code with legacy codes written in Fortran, or you want to use modern Fortran for his rich matrix operations. This post will present some simple examples of mixed Fortran 2008 and C++11 code, as a side note it is entirely possible to write these examples in pure Fortran or C++, coding every operations from scratch in C++ or using matrix libraries like Eigen.

Mixing Fortran with C (or C++) was a painful experience in the past and the exact mechanism was compiler dependent. Starting with Fortran 2003 you can use a standardized mechanism for interoperability with C, calling Fortran from C and vice versa can be done using the iso_c_binding Fortran module.

►  Continue reading


C++11 multithreading tutorial - part 3

Posted on May 9, 2012 by Paul

The code for this tutorial is on GitHub: https://github.com/sol-prog/threads.

In my previous two tutorials about C++11 threads:

we’ve seen that C++11 allows us to use a clean syntax (compared with the one used by POSIX) for managing multithread applications. The second tutorial presents an example of threads synchronization using mutex and atomic operations. In this tutorial I’m going to show you how to use a member function and lambda with threads.

We’ll start with a simple example of a C++11 thread with a member function:

►  Continue reading


Great book - The C++ Standard Library 2nd edition

Posted on April 24, 2012 by Paul

With the arrival of the new C++ standard last year, C++ has changed. In a sense C++11 is a new language while keeping backward compatibility with the old standards. You can run any valid C++98 or C++2003 program through a C++11 compiler and the code will pass without a problem, however if you take a modern piece of C++ code this could look quite different from the equivalent C++ code written ten years ago.

►  Continue reading


Vector addition benchmark in C, C++ and Fortran

Posted on April 11, 2012 by Paul

If you were involved in working with large arrays and linear algebra, you’ve probably heard mantras like A Fortran implementation can usually achieve more performance than C or C++ or C++ is slow for scientific computations, don’t use it!. Until recently I honestly believed that, at least for elementary linear algebra operations, like vector addition, e.g. C can easily beat C++ (specifically that working with C-arrays should be faster than the more elaborate vector data structure from C++) and Fortran will beat both. Please be aware that I’m not talking about which language is inherently superior to another, but rather about what is the actual state of their implementation on personal computers running Linux and Windows.

In order to test the above myths I’ve implemented a few simple codes for vector, one dimensional arrays, addition in C, C++ and Fortran.

►  Continue reading


Objective-C blocks on Windows with Clang

Posted on March 25, 2012 by Paul

In my last post I’ve shown you how to develop Objective-C code on Windows with Clang and GNUstep. I’ve also presented a small Objective-C program that uses the new ARC syntax.

Today I will show you how to compile on Windows an Objective-C program that contains blocks. Strictly speaking Apple’s blocks syntax is an extension to the C language.

►  Continue reading


Clang and Objective-C on Windows

Posted on March 21, 2012 by Paul

UPDATE 17 May 2014

Newer versions of Clang and LLVM seems to have a problem with GNUstep dev tools which are based on the now old gcc-4.6.1 compiler. I’ve changed the post to avoid this problem by using r181679 which works with GNUstep.

UPDATE 13 May 2013

For your convenience, I’ve uploaded on github a binary version of Clang and GNUstep. My recommendation is to compile it yourself as described in the article. If you need a quick way to use Clang and GNUSstep, you can use the linked binary: https://github.com/sol-prog/Clang_GNUstep_Objective-C_for_Windows

If you want to start learning Objective-C on a Windows computer, you’ve come to the right place. This tutorial will show you how to install a compiler and the necessary frameworks to start hacking Objective-C on Windows today. Be warned that I’m not talking about developing the next Objective-C iPhone/iPad application on Windows, this is not possible at the time of this writing!

►  Continue reading


Add syntax highlighting to your static blog with Python and Pygments

Posted on March 19, 2012 by Paul

Recently I’ve converted my blog from WordPress to a static website generated with Jekyll, my only problem was with the way Jekyll handles syntax highlighting through Pygments, specifically with changing the background of more than a line of code. While doing this for a single line of code works perfectly, I wasn’t able to find a way to change the background of a group of lines, like for example the second and the fourth line from a piece of code.

Changing the background for a specific line of code is the equivalent of using a marker on a paper with printed code, it is especially important to have this kind of fine control over the details when you write about programming.

►  Continue reading


Using Git with Dropbox

Posted on March 15, 2012 by Paul

Sometimes you want to keep a git repository private, at least temporary, without owning a server or a paid GitHub account. If you have a free Dropbox account you can host your private repo there.

Open a Terminal window if you are on Linux or Mac, on Windows just start a Bash window from your Git installation. Your Dropbox folder is created by default on HOME for Linux and Mac, or on MyDocuments in Windows.

Navigate to your Dropbox folder:

1 cd ~
2 cd Dropbox

►  Continue reading


Starting with GNU sed on Mac OSX

Posted on March 7, 2012 by Paul

If you are a hard-core Linux user who just bought a shiny new Mac machine, you are going to have a big surprise realizing that not every command line skill you acquired over the years will be directly applicable in a Mac Terminal. Take for example the humble and useful sed editor, invaluable when you need to modify on the fly a bunch of text files. You probably assume you could just fire up a Terminal and get your work done. Well it is not so simple, Mac OSX has his roots in Unix just like Linux, but it has a different flavour of command line utilities, a BSD one. On the other hand Linux uses the GNU version of these command line tools. Small but subtle differences have accumulated over the years.

►  Continue reading


C++11 multithreading tutorial - part 2

Posted on February 27, 2012 by Paul

The code for this tutorial is on GitHub: https://github.com/sol-prog/threads.

In my last tutorial about using threads in C++11 we’ve seen that the new C++11 threads syntax is remarkably clean compared with the POSIX pthreads syntax. Using a few simple concepts we were able to build a fairly complex image processing example avoiding the subject of thread synchronization. In the second part of this introduction to multithreading programming in C++11 we are going to see how we can synchronize a group of threads running in parallel.

We’ll start with a quick remainder of how we can create a group of threads in C++11. In the last tutorial we’ve seen that we can store a group of threads in a classical C-type array, it is entirely possible to store our threads in a std::vector which is more in the spirit of C++11 and avoids the pitfalls of dynamical memory allocation with new and delete:

►  Continue reading


Top C++ books for beginners and intermediate programmers

Posted on January 2, 2012 by Paul

Updated 24 April 2018

Following is a list of C++ books I’ve compiled in the last years. This is by no means exhaustive and it is obviously subjective. If you feel that I’ve missed an important book feel free to drop me a comment and I will gladly update this list.

Please note that, if you have no previous programming experience, C++ can be a bit daunting. There are people that recommend you to start with a more palatable language like Python or Ruby. What I’ve noticed, from my experience, is that if you are motivated you can learn any programming language, the key is to pick a good book that will let you advance step by step in the language.

►  Continue reading


C++11 multithreading tutorial

Posted on December 16, 2011 by Paul

The code for this tutorial is on GitHub: https://github.com/sol-prog/threads.

In my previous tutorials I’ve presented some of the newest C++11 additions to the language: regular expressions, raw strings and lambdas.

Perhaps one of the biggest change to the language is the addition of multithreading support. Before C++11, it was possible to target multicore computers using OS facilities (pthreads on Unix like systems) or libraries like OpenMP and MPI.

This tutorial is meant to get you started with C++11 threads and not to be an exhaustive reference of the standard.

Creating and launching a thread in C++11 is as simple as adding the thread header to your C++ source. Let’s see how we can create a simple HelloWorld program with threads:

►  Continue reading


Upgrading my MacBook Pro

Posted on November 23, 2011 by Paul

Recently I’ve noticed that my MacBook Pro 13” 2010 is slower than usual so I decided to reinstall a fresh version of Lion and to be more selective with the apps I install. After all who needs six text editors, two office applications and more of these kind. This happened to me because every time I read about an app that looks interesting I install it, use it for a few days and usually forget about it.

I’ve also considered buying more RAM and a faster SSD harddrive directly from Apple but the price has cooled my enthusiasm, better buy a new laptop or … I could buy the harddrive and memory and install them myself.

►  Continue reading