Fix MATLAB Error: “dlopen: cannot load any more object with static TLS”

On Linux, you may occasionally encounter the error “dlopen: cannot load any more object with static TLS” in MATLAB. This is a known bug since way back!

To fix, create a file “startup.m” in the directory that you start MATLAB from with the following content:

I know! It’s ugly… But it works!!

EDIT: Since it was not clear, the folder that you start MATLAB from is by default “~/Documents/MATLAB” under Linux. On Windows, that would be “Documents\MATLAB”.

Apache Always Redirect to HTTPS for SSL Websites

After getting an SSL certificate, it is usually good idea to redirect all http (port 80) connections on your website to https (port 443). This can be simply done in Apache. To do this, just go to your public_html folder and either create a .htaccess file and add these lines, or add them to the already existing .htaccess file:

Note: The Apache mod_rewrite must be enabled for this to work!

Tutorial : Use CUDA and C++11 Code in MATLAB

As it turns out, incorporating CUDA code in MATLAB can be easily done! 🙂

MATLAB provides functionality for loading arbitrary dynamic libraries and invoking their functions. This is especially easy for invoking C/C++ code in a MATLAB program. Such functionality is possible using the so called MEX functions.


Mex functions can be created with the mex command in MATLAB. Essentially, mex takes as input a C/C++ source file, invokes the default C/C++ compiler installed in the operating system (GCC or CL), and creates a mexa64 file (on a 64-bit machine) which can be used like any other MATLAB function.

The C/C++ file that is passed to mex must have the following included in it:

The arguments that are passed from MATLAB are accessible using the prhs parameter (which stands for parameters-right hand side). Any output that the gateway function generates can be returned using the plhs parameter (which stands for parameter-left hand side). The number of the arguments that are passed to the gateway function is stored in the nrhs parameter and the number of outputs that the MATLAB code expects from the gateway function is stored in the nlhs parameter. From this point on, I refer to the file containing the above code as the mex gateway file. Also, I will refer to the mexFunction above as the gateway function.

Continue reading Tutorial : Use CUDA and C++11 Code in MATLAB

CImg does not store pixels in the interleaved format


Took me hours before I found and read the documentation.

CImg stores pixels in a planer format (RRRR…..GGGG…..BBBB). For most tasks in CUDA, it’s much better to store the pixel values in the interleaved format (RGBRGBRGB……).

In order to do that, just call the permute_axes method of the CImg object:


After permutation, the width, height,  spectrum and depth values that are reported for CImg will all change.  To permute back (for displaying or saving) do this:


Where the values, are previously saved values (before doing any kind of permutation on the axes). This will undo any changes and now you can safely save the image or display it.

CImg instance from interleaved array (bitmap):

Now imagine you want to initialize a CImg object with an interleaved bitmap (say an OpenGL texture or what have you). In this case, you need to know the width and height of the image as well as the number of components. Also imagine that the spectrum is 1. To create a CImg object using this array you can do ( imageArray is the bitmap pointer):


Error: “incorrect inclusion of a cudart header file”

If you receive this error while compiling a CUDA program, it means that you have included a CUDA header file containing CUDA specific qualifiers (such as __device__)  in a *.cpp file.

CUDA header files with such qualifiers should ONLY be included in *.cu files.

This happened to me when I had #inlcude <common_functions.h> in my *.cpp file. Note that having this in a header file that will be linked to a *.cpp file will also result in the same error.