Update (2017-04-02):
I have continued development of Heapy on GitHub.
The command line usage has been tweaked. Please see the github for more up to date usage docs.
This post remains the best/only documentation of Heapys inner workings which remain the same – but the code is now slightly out of date.

I’ve created a simple but hopefully effective heap profiler for windows C/C++ applications called Heapy.

Heapy requires no modifications to the program to be profiled. With a very quick setup it can profile 32 or 64 bit windows C/C++ applications. Heapy will list the top allocation sites of your application every few seconds – helping you track down memory leaks and giving you a better insight into what parts of you program are using memory.

My last post was about creating a tiny just in time (JIT) compiler for mathematical expressions. This new project builds on that work. JITted mathematical expressions were the first problem to solve on the way to making a toy programming language for image processing. I have called this new project Pixslam, the sourcode and a decent amount of documentation are on GitHub.

Recently I have been interested in code generation. Specifically generating native x86-64 code on the fly.

In the future I hope to present useful and novel applications of code generation – but for now I just want to demonstrate the simplest thing I think you can call just-in-time (JIT) code generation without blushing.

Skip right to the code on GitHub or continue reading for a full description.

My solution was one of the more self contained and interesting pieces of my work at uni. So I put it up on GitHub.

I was amazed at how much faster this algorithm was compared to the earlier genetic algorithm based approaches I tried during the course. The only non vanilla part of the implementation is an approximation for the Math.pow function in java which sped up the whole program up by a decent amount.

It’s been a while since my last post. I would have probably had more time for extra curricular projects if I didn’t spend so much of my life waiting for C++ applications to compile! In fact I’ve created a tool to help with exactly that…

The problem

C/C++ projects often compile painfully slowly. A large cause of this problem is “#include” statements. One included headers drags in others which drag in yet more – one combinatorial explosion later you’re left twiddling your thumbs waiting a project to compile (since hundreds of headers can take a while to open and compile).

One way to avoid headers dragging into too many other headers with them is to use forward declaration and dynamic allocation. This allows us to remove some include directives from headers. Removing an unnecessary include from a popular header car really help compile times.

This is a very quick post describing how to fix an annoyance with Matlab under Linux. I thought I’d share this with the internet to hopefully save the next person who has this trouble some time.

On Linux Matlab R2011b annoyingly requires the fairly old gcc 4.3.4 to compile C/C++ “mex” functions. This is a problem for recent versions of Ubuntu 64-bit. If you attempt to use a more recent version of gcc you get errors like “libstdc++.so.6: version `GLIBCXX_3.4.11′ not found” when trying to run the mex files. This is because matlab uses it’s own libc/stdc++, and only includes them for older compilers.

The fix is to install gcc 3.4 and point matlab to it by editing mexopts.sh. Annoyingly there is no package for gcc 3.4 on Ubuntu 11.10, so you have to build it yourself. The reason for this blog post is that the build isn’t quite “textbook” and it took me a while to get it to work. Hopefully this guide might save some googleing soul the hassle, and I’ll be able to point co-workers to it when they upgrade.

The other day I was playing around in Matlab, and although I can’t remember what I set out to do I did end up making a small lossy audio compression/decompression system! It seemed like a good topic for a blog post.

The discrete cosine transformation

Before I show the code I’ll have to very briefly introduce the discrete cosine transform (DCT). We should be able to ignore the maths and implementation of the DCT and treat it as a magic box which comes with Matlab or octave. If your interested in the details (and they are interesting) this book is a great place to start if you want more depth than wikipedia offers.

Don’t worry too much about that expression. We just need note that the DCT represents the original signal as a sum of cosines, and that the coefficients specify the amplitude of these cosines.

If we have the DCT coefficients we can transform them back to the original sequence with the inverse discrete cosine transform (IDCT). This could be calculated with the above expression but more efficient algorithms exist for both the DCT and IDCT (these algorithms are based on the fast Fourier transform, which is again an interesting topic that I won’t get into).

One of my recent interests has been in solving problems with various kinds of satisfiability solvers. As an introduction to this idea I want to demonstrate how to create a sudoku solver with almost no effort by reduction to a common satisfiability problem (CNF-SAT) and using an existing solver (MiniSat).

Solving sudoku by reduction to CNF-sat is hardly a new idea, I’m sure a quick google would show various approaches. If the mathematical notation below looks a little scary perhaps try reading the code further down, it really is quite simple if you can read propositional formulas.

A quick sudoku recap

A sudoku puzzle is a 9×9 grid of cells, split into 9 non overlapping 3×3 “boxes”. Some of these will be labelled with a digit from 1 to 9, others will be blank. The aim is to label the remaining cells so that every row, column and box contains the digits 1 through 9.

Unsolved sudoku grid

Solved sudoku gird

CNF-SAT

Perhaps the most well known satisfiability problem is propositional satisfiability: given a formula in propositional logic can we find assignments for all the variables which makes the formula true? A subset of this problem is satisfiability on formulas in conjunctive normal form (CNF-SAT). A formula is in conjunctive normal form if it is a conjunction of clauses, where a clause is a disjunction of literals (a literal is just a variable or its negation), e.g