Speeding up compilation on Ubuntu with Qt Creator

Are you reading random stuff on the web while waiting for your C++ compilation to finish? Then you have come to the right place. In this post I will tell you about two really nice tweaks you may do to speed up your compilations, namely ccache and the make -j flag, and how you may set these up in Qt Creator.

ccache

ccache is a clever tool that wraps you compiler (g++ or mpicxx) and understands whether the file you are compiling has been compiled before with exactly the same contents and settings. If it has, it just returns a cached result.

Unlike regular make, ccache is extremely good at detecting the true state of what you are compiling. I have never had any trouble with ccache.

This really speeds up the compilation when you are using make clean, especially if you are switching git branches. In other words, it is a much simpler solution to achieve fast compilation with git branches than to create separate build folders for each branch.

To enable ccache, install it with

sudo apt-get install ccache

and add the following to your .pro file:

QMAKE_CXX = ccache g++

Replace g++ with mpicxx if you are using MPI.

The make -j flag

I realized when compiling the Qt source that make has a -j flag that enables threaded compilation on all available processors on the machine. This also speeds up compilation significantly, and I made a 3.55x performance gain on a 4 core CPU. To enable this flag, go to the Projects view in Qt Creator and add the following arguments to the make build step:

-j

This should look something like this afterwards:

This is how your project settings should look like after adding the -j option to make.

This is how your project settings should look like after adding the -j option to make.

If you prefer not to use all available processors for compilation, you may add a number after -j to set the number of processors. For instance make -j 3 would compile with 3 processors.

Posted in C++, Computational Physics, Programming, Qt | Tagged , , , , , , | Leave a comment

Limiting the width of Wikipedia-articles when reading on widescreens

I’m using a widescreen display most of the day, which makes it quite useful to have two browser windows open or one browser window and a text-editor side-by-side. But sometimes I keep the browser window maximized, which is a bad thing for sites like Wikipedia. The articles get dragged out in full width, making the lines a bit too long to read comfortably:

Way too long lines to read comfortably.

Way too long lines to read comfortably.

Thankfully, it is quite easy to fix this. I came over one solution with a bookmarklet, but this requires you to click a button for each article. If you have logged in to Wikipedia, however, you may add your own custom CSS that will appear on all articles by clicking Preferences > Appearance > Custom CSS.

In the editor that shows up, add the following:

body {
    max-width: 65em;
    margin-left: auto;
    margin-right: auto;
    position: relative;
    float: none;
}

After this change, Wikipedia pages are limited to the width of 65em (approximately 1000 px):

Wikipedia with a much better width.

Wikipedia with a much better width.

If you want to add this to all pages on the web, you should consider a bookmarklet like the one mentioned here.

Posted in Other | Tagged , , , , , , , | Leave a comment

Setting up UnitTest++ with Qt Creator

When you want to make sure that your code is working properly, it is a good idea to divide it into smaller, independent pieces that may be tested individually. A unit test is a short  code that tests a smallest possible portion of your application. It is a good idea to write tests as you go and it can even be useful to write a test before you even implement the function that will be tested.

With the combination of UnitTest++ and Qt Creator, I’m now able to write unit tests while working on the code and also have some nice visual indication about failed tests during the build step of my project:

qt-creator-unit-tests1

During my search for a good setup for testing my applications I realized there were a few needs that I wanted to satisfy:

  1. Creating new unit tests should be dead-easy to do. I want to spend as little time as possible on reading documentation about the testing framework.
  2. Tests should be run immediately after a new build of the source code, automatically and without the extra hassle to remember to run the tests.
  3. Tests should provide visual feedback with an easy way to get to where the tests fail. This should show up in the IDE or some other useful GUI tool.
  4. The testing framework should be fairly easy to install, especially on Ubuntu. This is because I want to be able to promote it to my fellow students.

Continue reading

Posted in C++, Computational Physics, Programming, Qt, Ubuntu | Tagged , , , | Leave a comment

Working with percolation clusters in Python

We’re working on a new project in FYS4460 about percolation. In the introduction of this project, we are given a few commands to help us demonstrate a few properties of percolation clusters using MATLAB.

As the Python-fan I am, I of course had to see if I could find equivalent commands in Python, and thankfully that was quite easy. Below I will summarize the commands that will generate a random matrix of filled and unfilled areas, label each cluster in this matrix and calculate the area of each such cluster. Finally, we’ll draw a bounding box around the largest cluster.

Continue reading

Posted in Computational Physics, Featured, Physics, Programming, Python | Tagged , , , , , | 4 Comments

Optimizing your C++ code for molecular dynamics

While working with the molecular dynamics project in FYS4460 I decided to learn more about how to optimize my C++ code for performance. As always, I follow Donald Knuth’s famous quote as a guideline to optimization:

“We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil”[2]

And this has proved to be as true as ever in my efforts to optimize my code. There are a bunch of things that I have tried that didn’t turn out to be as effective as I had thought, and some other that I would never think could be so important. I’ve listed most of these in this post so you too may learn from my experience. They are all listed in the order from most useful to most wasteful:

Continue reading

Posted in C++, Computational Physics, Efficiency, Physics, Programming | Tagged , , , , , , , , , , | Leave a comment

Setting up Ubuntu SDK on Kubuntu with backports enabled

Gah… So once again my temptation to install the latest and greatest causes a conflicting system setup. This time it was my attempt at installing the Ubuntu SDK on a Kubuntu system with backports enabled that conflicted. Backports are packages that are only available to newer versions of Ubuntu rebuilt for older versions, like when you want to use KDE 4.10 for Ubuntu 12.04. Although rare, when you want to install newer versions of other software you might end up with conflicts.

The result this time was the following error message upon installing the ubuntu-sdk package:

Unpacking qtchooser (from .../qtchooser_0.0.1~git20121229.g8f08405-0ubuntu1~precise1~test6_amd64.deb) ...
dpkg: error processing /var/cache/apt/archives/qtchooser_0.0.1~git20121229.g8f08405-0ubuntu1~precise1~test6_amd64.deb (--unpack):
 trying to overwrite '/usr/bin/qdbuscpp2xml', which is also in package libqt4-dev-bin 4:4.8.2+dfsg-2ubuntu1~precise1~ppa2
Processing triggers for man-db ...
Errors were encountered while processing:
 /var/cache/apt/archives/qtchooser_0.0.1~git20121229.g8f08405-0ubuntu1~precise1~test6_amd64.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)

Trying to fix the error by removing libqt4-dev-bin only resulted in a long line of dependencies that will eventually conflict with the kde-workspace package, and you really don’t want to remove that (unless you want to get rid of KDE altogether).

To fix this, I tried a multitude of other options, only to realize that I could install all other packages in ubuntu-sdk to get what was needed to start developing apps for Ubuntu Phone and Tablet. These were installed by the single command:

sudo apt-get install qtdeclarative5-dev libqt5xmlpatterns5-dev qtscript5-dev qttools5-dev libqt5webkit5-dev qt3d5-dev qtmultimedia5-dev libqt5svg5-dev libqt5graphicaleffects5 qmlscene qtdeclarative5-dev-tools qttools5-dev-tools qtlocation5-dev qtsensors5-dev qtpim5-dev qtcreator ubuntu-qtcreator-qt5libs ubuntu-qtcreator-plugins qt-components-ubuntu qt-components-ubuntu-demos qt-components-ubuntu-examples

After this, you may launch ubuntu-qtcreator from terminal. It won’t find the Qt5 installation however, but you can point it to it by going to Tools > Options > Build & Run > Qt Versions and adding /usr/lib/x86_64-linux-gnu/qt5/bin/qmake:

qt-options-qt5-ubuntu

Now you just need to add a new Kit pointing to the new Qt version. Before you do this, click OK and then open Options again to refresh your Qt versions available for Kits.

That’s it. Now you should be able to add a new Ubuntu UI project in Qt Creator and get started!

Posted in Programming, Qt, Ubuntu | Tagged , , , , , | Leave a comment

Getting the latest version of Ovito for Ubuntu

Ovito is a great tool to visualize atoms from molecular dynamics simulations and to perform some statistical analysis on the data. The tool is an alternative to other similar tools such as VMD and ParaView.

Ovito

The main window of Ovito in the newest version.

The current version of Ovito in the Ubuntu repositories (version 0.9.2) is sadly very outdated. Even though you of course may download the latest version from Ovito’s webpages and install it locally in your home folder, I often find it better to get a newer package version.

At the Computational Physics group’s PPA I have now uploaded the latest build of Ovito for Ubuntu in a package that will automatically replace the current Ovito version. Note that this is not a release version of Ovito, but the latest version fetched from Ovito’s sources (currently nicknamed 1.1.1.90). The reason is that Ovito’s creator, Alexander Stukowski, was kind enough to implement a suggestion we proposed to change the default input handler for the viewports to be the orbit input handler. This behavior feels so much more natural and makes the already great Ovito application even better.

To install the latest version of Ovito, all you need to do is to open up a terminal and type these lines:

sudo apt-add-repository ppa:comp-phys/stable
sudo apt-get install ovito

Note that the Computational Physics repository also contains newer versions of the Armadillo package, which will automatically be updated if you do not remove the repository after installing Ovito.

Posted in Computational Physics, Physics, Ubuntu | Tagged , , , , , , , | Leave a comment

Set “Open containing folder” to use Dolphin and PDF files to use Okular in Firefox on Kubuntu

If you, like me, are tired of having Firefox open up Nautilus for folders and Evince for PDFs in KDE on Ubuntu, you might want to change your default application settings. To fix this once and for all, open up /usr/share/applications/defaults.list and change the line with

application/pdf=evince.desktop

to

application/pdf=kde4/okular.desktop

Then, to set up Dolphin, change

inode/directory=nautilus.desktop

to

inode/directory=kde4/dolphin.desktop

And there you are! Courtesy of Jeremy’s comment on this post.

Posted in Ubuntu | Tagged , , , , , , , , | 2 Comments

Setting up Visual Studio to compile Botan

So I’m working with Qt5 on Windows and figured that since the Qt5 SDK is currently only compiled against Visual Studio 2010, I had to figure out how to build the Botan library without using MinGW.

The build instructions for Botan are mostly complete, but lacks one vital step, namely calling the vcvarsall.bat script. This is found in the Visual Studio 2010 install directory, under the subdirectory VC. Just call this on the command prompt before doing anything else and your path and library variables will be set up properly.

After running this, you only need to follow the steps for building on Windows by configuring the build for MSVC, which you find over at Botan’s homepage.

Posted in C++, Programming | Leave a comment

Setting up the buttons on Logitech G500 mouse in Kubuntu

I’ve become very happy with the Logitech G500 mouse. I like its responsiveness, the USB connection rather than wireless (no batteries, always responsive) and how it sits in my hand.

However, setting up the keys on my Kubuntu box (Ubuntu with KDE) wasn’t straightforward. However, I’ve found a mapping that I enjoy and thought I could share it with you. You may modify it further to customize it to your needs.

The setup is such that I switch desktops with the arrow thumb buttons and activate the Desktop Grid function with the middle thumb button. Other than that, the mapping is as usual. To make this work, I’m using the xte, xdotool and xbindkeys programs. To install these, just run

sudo apt-get install xautomation xdotool xbindkeys xbindkeys-config

Then you may just paste this at the end of a file named .xbindkeysrc in your home folder:

#Present Windows
"xdotool key --delay=60 super+s"
   b:10 + release

#Previous workspace
"xte 'keydown Control_L' 'keydown Alt_L' 'key Left' 'keyup Control_L' 'keyup Alt_L'"
   b:8 + release

#Next workspace
"xte 'keydown Control_L' 'keydown Alt_L' 'key Right' 'keyup Control_L' 'keyup Alt_L'"
   b:9 + release

#Move to previous workspace
"xte 'keydown Control_L' 'keydown Shift_L' 'keydown Alt_L' 'key Left' 'keyup Control_L' 'keyup Shift_L' 'keyup Alt_L'"
   Shift + b:8 + release

#Move to next
"xte 'keydown Control_L' 'keydown Shift_L' 'keydown Alt_L' 'key Right' 'keyup Control_L' 'keyup Shift_L' 'keyup Alt_L'"
   Shift + b:9 + release

After this, you just need to start xbindkeys. Do this by running xbindkeys in a terminal (you may want to add this to your startup applications).

That’s it! You have now configured the extra buttons and should be able to click your way around your desktops. If you are having any trouble, try to launch xbindkeys-config and have a look around to see if everything is configured correctly. From there you may also test the commands or configure more buttons if you’d like to.

Posted in Ubuntu | Leave a comment