C# Deserialization of a List<> Returns a List with “null” objects

In .NET, the List<> class is serializable. However, you should note that during a call to the deserialization constructor of the objects the list contains, the list will be empty. All operations on the newly deserialized objects (such as value checking and so on) need to be performed in another function marked by the [OnDeserializedAttribute] attribute. More info on this can be found here.

CMake Unable to Find AMD’s ACML Files Under Windows

One popular BLAS implementation under Windows is AMD’s ACML.
CMake has some modules that can find certain required dependency libraries. As far as I know, under Windows the “FindBLAS” and “FindLapack” modules are unable to locate AMD’s ACML libraries. This is because ACML > v4.0 does not include the “mv” related packages anymore.

I looked around a little bit and I found a patched version of these files here and here. However, I couldn’t get them to work. If anyone was able to get them to work, comment below.

Cannot Find “getopt.h” File When Compiling Under Windows

Often times, issues arise when compiling C/C++ code developed for Linux under Windows. One annoying problem is when the code requires some header which is only available in the POSIX API. A header commonly used for parsing the command line arguments is getopt.h. Unfortunately, this header is only available under Linux. After some digging around, I found a port of this header file for Windows here.

In case the repository went down in the future, I’ve pasted the code here. All credits go to the original author. Click on the link below for the full code.

Continue reading

Rotate a 3D object around its center and own axes in WPF 3D

In WPF 3D, various transformations could be applied to an object. One particular problem that I occasionally run into is when I want to apply a rotation transformation to an object and rotate it around its own center and axes.

The way I do it is as follows. First I apply 3 identity RotateTransform3D‘s to the object with AxisAngleRotation3D‘s objects underneath: one for the X axis, one for the Y axis and one for the Z axis. Then whenever I want to rotate the object around a certain axis, I obtain the corresponding RotateTransform3D object, set its center according to the (possibly) translated center of the object, and apply the rotation angle to the underlying AxisAngleRotation3D object. Some code will make this more clear.

First apply the 3 identity transforms to the object:

Let’s say the function SetRotation is to be used for setting the rotation of the object. It will work as follows:

Orient a Transform to a Specific Direction in Unity 3D

If you are too lazy to do the math to orient an object to a specific direction, this tip is useful for your.

The Transform objects in unity have the Forward, Up and Right properties. You probably don’t know that they are not only accessors but also mutators! You can simply assign a direction vector to an object’s Forward direction and the object will face that direction:

This can be handy sometimes, especially when you want to use the Kinect sensor inside the game. If the players arm direction signifies the orientation of something (eg. a sword), this tip saves you some time.

C/C++: Code Not Working under a Certain Build Configuration

If your C/C++ code works fine under a certain build configuration (eg. Release) but not under another (eg. Debug) or simply works fine when built with a certain compiler, it is a sign that the code is not robust and some small detail which depends on compiler optimization is producing undefined behavior.

For instance, my code was working fine under Linux (using G++) and also Visual C++ 2013 (using the Release) configuration, but was giving me a hard time under the “Debug” configuration in VC++. Turned out that the compiler optimization under “Release” was preventing a destructor from being called. Since the destructor was never called, no memory leak was occurring. Building under “Debug” would have disabled that optimization and the code would have crashed with a memory leak error!

This just emphasizes how important it is to test the code thoroughly and detect those parts that could lead to undefined behavior.

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!

ArrayFire is Now Open Source!

To my surprise, the CUDA library ArrayFire is now open source and licensed under BSD 3-Clause License which means that commercial use is permitted!

ArrayFire is a production oriented library which greatly reduces CUDA application development time. The repository is hosted on GitHub and is located here.

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.

Introduction:

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