Haskell: Unboxed vs. boxed

Posted by Moser on 03 Apr 2012

Because I did not find a good example (at least not on the first result page :D) for the performance difference between boxed and unboxed types in Haskell, I created one.

--boxed.hs
foo :: Int -> Int
foo 0 = 0
foo n = foo (n - 1)

main = print (foo 500000000)
--unboxed.hs
import GHC.Exts
foo :: Int# -> Int#
foo 0# = 0#
foo n = foo (n -# 1#)

main = print (I# (foo 500000000#))

And here is the quite impressive result:

$ ghc boxed.hs
$ ghc -XMagicHash unboxed.hs
$ time ./boxed
0
./boxed  16,34s user 0,03s system 99% cpu 16,397 total
$ time ./unboxed
0
./unboxed  0,85s user 0,00s system 99% cpu 0,865 total

Rails 3.1: Access compiled assets

Posted by Moser on 16 Sep 2011

I use wicked pdf for PDF generation in my rails app foxtrot mike. Wicked pdf has a helper method which copies the content of CSS files into the generated HTML. This does not work in Rails 3.1 because CSS files are generated by Sprockets. Fortunately it is possible to access the compiled asset files. When the asset pipeline is enabled, the application object has an attribute assets which returns a Sprockets::Environment.

The following code includes the contents of application.css in my layout which is used for PDF generation.

  %style{ :type => "text/css" }
    = Rails.application.assets['application.css'].to_s.html_safe

Evince with Tabs - Or rather "in Tabs"

Posted by Moser on 10 Feb 2011

Update: As mentioned in the comments, qpdfview solves the problem.

While I wrote my bachelors thesis a couple of months ago, I really got angry about Evince and the lack of a tab-enabled PDF viewer in Gnome. While I was (trying to) learn for a university course today, this anger returned. The lecturer felt like it was a good idea to arrange the content in more than 15 PDF files.

I ceased the attempt to learn and again started a search for a tabbed document viewer for Gnome. Several users requested a tab feature in evince on Ubuntu brainstorm and received - in my opinion - annoyingly arrogant answers. The technical aspects are not an argument, look at Gedit. The design-user-experience-blah reasons for not implementing this feature may be justified, but one could still implement it as an option and let the user decide. But whatever…

I did not exactly find a tabbed document viewer. But I found an acceptable workaround:

It is based on whatever browser you like (as long as it is supported by mozplugger, Chromium in my case) and embedding evince there. On Ubuntu mozplugger can be installed like so:

sudo aptitude install mozplugger

After you installed mozplugger and restarted your browser it should show up as a plugin. You can then follow the instructions in the blog post linked to above.

Because Evince has no option to hide the toolbar persistently, I removed it completely. To do this, edit ~/.gnome2/evince/evince_toolbar.xml to look like this:

<?xml version="1.0"?>
<toolbars version="1.0">
</toolbars>

I have furthermore configured Gnome to open PDFs in Chromium.

Switched to Jekyll

Posted by Moser on 15 Jan 2011

When I first installed WordPress I really liked it. But keeping it and it’s plugins up to date is a pain for me. Furthermore, running a PHP application feels kind of strange for me who feels disgust when thinking of this language. While my server does not run anything of great value, it is surely more secure not to run a dynamic application for something that is rather static.

So I decided to switch my blog to jekyll. The import of my old posts worked quite well, I just had to reformat them a bit. My layout just needed minor adaptions.

Permalinks of my WP blog were formatted according to the pattern moserei.de/index.php/:id/:slug. I could have generated a similar directory structure with jekyll, but I like the default format (moserei.de/:year/:month/:day/:slug). So I crafted a little rack app which redirects requests to old permalink URLs to the new ones.

class RedirectPermalinks
  Mapping = { '0' => "/",
              '3' => '/2008/09/15/started.html' 
               #... 
            }
  def call(env)
    id = "0"
    if env["QUERY_STRING"] =~ /p=([0-9]+)/ || 
       env["REQUEST_URI"] =~ /\/([0-9]+)\//
      id = $1
    end
    [ 301, 
      { "Content-Type" => "text/plain", 
        "Location" => "http://moserei.de#{id && Mapping[id] || ""}" }, 
      [""] ]
  end
end

It is deployed inside a directory called index.php and redirects requests to the abovementioned permalink URLs and to index.php/?p=:id to the corresponding articles. All other requests are redirected to /.

Comments are now managed by Disqus. Disqus has awesome tools for import and migration. I imported comments from a WXR file exported from WP and migrated the post URLs using a CSV file I extracted from WP’s database.

N-Body fun

Posted by Moser on 14 Dec 2010

Update: Replaced the GIF by a video.

We currently have to do a simple two-dimensional n-body simulation of gravitation for a university course in practical parallel programming. It uses MPI and OpenMP for parallelism. For benchmarks we were supplied with a model of a spiral galaxy which (in the original example) was passed by by an massive object. I tweaked the system so that this object now rather goes through the galaxy:

Some numbers: The model consists of 402 objects, a step of simulation is 1000 years long, the loop consists of 1.4M steps, a frame was generated every 2500 steps. This took about 25 minutes using 14 MPI processes (no OpenMP).

Find GnuPG revocation certificate on unmountable partition

Posted by Moser on 06 Jul 2010

When you encounter a hard disk crash like I did recently (my fault - dropped my laptop), one of the most important files to restore is your GnuPG private key ring or at least a revocation certificate for your public key(s). I did not have any backup :-(. But at least I had created a revocation certificate, which was now hidden somewhere on my unmountable partition. Scalpel, a file carving tool, helped me out. There is a Ubuntu package (and I believe there should be packages for most distributions):

sudo apt-get install scalpel

It’s configuration is in /etc/scalpel/scalpel.conf. It offers some GPG related rules, but none for a revocation certificate. I used these rules:

revoc	y	100000	-----BEGIN\040PGP\040PUBLIC\040KEY\040BLOCK-----\x0aVersion:\040GnuPG\040v1.4.10\040(GNU/Linux)\x0aComment:\040A\040revocation\040certificate\040should\040follow
revoc	y	100000	-----BEGIN\040PGP\040PUBLIC\040KEY\040BLOCK-----\x0aVersion:\040GnuPG\040v1.4.9\040(GNU/Linux)\x0aComment:\040A\040revocation\040certificate\040should\040follow
revoc	y	100000	-----BEGIN\040PGP\040PUBLIC\040KEY\040BLOCK-----\x0aVersion:\040GnuPG\040v1.4.8\040(GNU/Linux)\x0aComment:\040A\040revocation\040certificate\040should\040follow

I wasn’t quite sure which version of GPG I had installed at the time I created the keys, so I created rules for all possible versions. (See manpage for syntax explanation.)

It took about 2 hours to scan the image of my 250 GB hard drive and scalpel successfully restored my revocation certificate.

I also tried some of the supplied rules but I think they are not suitable for restoring files from an image of a whole hard drive. (Most of them just look for two bytes that mark the beginning of the file.)