Wednesday, July 22, 2015

Building Hardware optimized ATLAS on Ubuntu 14.04

The Automatically Tuned Linear Algebra Software (ATLAS) is used by many scientific software packages such as Mathworks Matlab, Julia, Kaldi (speech recognition), and many more. ATLAS can be optimized to the current hardware with a few simple steps, giving much higher performance than the default available packages.

This guide helps you create and install an optimized Debian package on Ubuntu 14.04, but it might work similarly on other GNU Linux distributions. One problem with Ubuntu 14.04 on Intel processors is that it uses a new kernel module called intel_pstate for managing the CPU frequency. This driver will lower the CPU frequency for the idle CPUs (throttling) even if the profile is set to "performance". Unfortunately ATLAS will not build if the CPU frequency is not fixed to the maximum allowed. The workaround is to disable this module in the boot loader (GRUB) temporarily for the build. Here are the steps to follow:

  1. Configure the boot loader to disable the intel_pstate module:
    • edit /etc/default/grub and add the intel_pstate=disable option to the GRUB_CMDLINE_LINUX_DEFAULT variable. In my case the line before and after the change looked like this:
      GRUB_CMDLINE_LINUX_DEFAULT="quiet splash nomdmonddf nomdmonisw"
      GRUB_CMDLINE_LINUX_DEFAULT="quiet splash nomdmonddf nomdmonisw intel_pstate=disable"
    • make the change active by running:
      sudo grub-mkconfig -o /boot/grub/grub.cfg
    • reboot
  2. run the following commands:
    sudo apt-get source atlas
    sudo apt-get build-dep atlas
    sudo apt-get install devscripts gfortran f2c libcnf-dev
    cd atlas-*/
    sudo fakeroot debian/rules custom
  3. the last step (that can take a long time to finish) will build ATLAS and create a Debian package file one level up. Install the package with:
    sudo dpkg -i ../libatlas3-base_*.deb
  4. restore the /etc/default/grub file to the original, run the sudo grub-mkconfig -o /boot/grub/grub.cfg command and reboot.
This procedure was tested with ATLAS 3.10.1. Hope this works for you as well. Let me know how it goes in the comments.


  1. the building process took between 6 and 7 hours to complete on an Intel(R) Core(TM) i7-4930K CPU @ 3.40GHz.
  2. Although there were 12 such cores, setting the environmental variable DEB_BUILD_OPTIONS=parallel=12 has no effect on the building process or speed
  3. there are 6 deb files generated by the building process:
  4. when installing the libatlas-doc package I got the following errors:
    Processing triggers for doc-base (0.10.5) …
    Processing 3 added doc-base files…
    Error in `/usr/share/doc-base/atlas-install', line 14: all `Format' sections are invalid.
    Error in `/usr/share/doc-base/atlas-devel', line 10: all `Format' sections are invalid.
    Error in `/usr/share/doc-base/atlas-contrib', line 14: all `Format' sections are invalid.
    Note: `install-docs --verbose --check file_name' may give more details about the above errors.

Monday, September 29, 2014

"La variante di Lüneburg" and China's rice production

In 1993, Paolo Maurensig, an Italian writer from Gorizia, wrote a novel entitled "La variante di Lüneburg" (Adelphi, 1995, pp. 164, ISBN 88-459-0984-0). The novel is set in Nazi Germany during World War II and the main theme is the game of chess.

At the beginning of the story, Maurensig tells a legend according to which the game of chess was invented by a Chinese peasant with a formidable gift for mathematics. (There are different versions of this story, as far as I understand, the earliest written record is contained in the Shahnameh and takes place in India, instead.) The peasant asks the king, in exchange for the game he invented, for a quantity of rice equal to that obtained with the following procedure: First one grain of rice should be placed on the first square on the chess board, then two grains on the second square, than four on the third and so on, every time doubling the number of rice grains. The king accepts not realizing what he is agreeing on.

Let's try to calculate how much rice that would be. The series implied by the peasant has a more general form called geometric series and is well known in mathematics:
  s_m = \sum_{k=0}^m x^k
If we calculate the first steps in the sum we obtain:
  s_0 &=& 1 \\
  s_1 &=& 1+x \\
  s_2 &=& 1+x+x^2 \\
  \dots && \\
To see how this relates to our problem, we can set $x=2$ and see that the sum will be: $1+ 2+ 4+ 8+ \dots $

If we observe $s_1$ and $s_2$ in the previous equations, we see that we can write the second in terms of the first in two different ways:
  s_2 &=& s_1+x^2 \\
  s_2 &=& 1 + x (1+x) = 1 + x s_1.\\

In the first case, we grouped the first two terms in $s_2$, whereas in the second case we group the last two terms and realized that they shared a common factor $x$.

If we continue writing the terms of the sum for higher orders, we realize that what we obtained above is true in general:
  s_{m}   &=& 1+x+\dots+x^m \\
  s_{m+1} &=& 1+x+\dots+x^m+x^{m+1} = s_m+x^{m+1} \\
         &=& 1 + x (1+\dots+x^{m-1}+x^m) = 1 + x s_m, \\
which also means that the right-hand side of the last two equations above must be equal:
  s_m+x^{m+1} &=& 1 + x s_m,
and, therefore, rearranging:
  s_m &=& \frac{x^{m+1}-1}{x-1}.
This is the general solution for the sum of the geometric series. If we want to know how many grains of rice the king will have to give to the peasant, we need to substitute the values of $x$ and $m$. We already saw above that $x$ should be equal to 2. We also know that the chess board has 8 rows and 8 columns, giving 64 squares. Because we start with $s_0$ in the first square, we need to calculate the series for $m=63$, that will correspond to the last square:
$$ s_{63} = \frac{2^{64}-1}{2-1} = 18\,446\,744\,073\,709\,551\,615, $$
which in words would sound something like eighteen quintillions...

In 1999, China produced approximately 198 million tons of rice. Which corresponds to $198\,000\,000\,000\,000$ grams. If we assume for simplicity that the production is constant over the years and that one gram of rice is approximately 50 grains, the king will have to give the peasant the entire rice production in China for more than 18 hundred years.

Needless to say, when the king realized the mistake, he killed the peasant.

Wednesday, December 18, 2013

MS Kinect microphone array geometry

This post provides the geometrical parameters of the MS Kinect microphone array that are necessary if you want to do acoustic beamforming from the 4 channels without using the Microsoft SDK (for example if you are using Linux or libfreenect or OpenNI).

The image below is a drawing of the internal parts of the Kinect including the microphones in purple:
As you can see, the microphones are not spaced at regular intervals. It is possible to see the exact position of the microphones without disassembling the Kinect by simply opening the plastic covers at the bottom of the device with a flat screwdriver (see next pictures).
The microphones are sitting in the small rubber cubes highlighted in the following picture and that provide some mechanical isolation from the body of the device:
Finally, the next drawing shows the position of the microphones as measured with respect to a reference system with origin in the device's middle point, x-axis along the devices length and y-axis oriented as the perspective of the cameras. The drawing should be interpreted looking at the device from above (I used an XBox 360 Kinect MODEL 1473, the product codes returned by lsusb on linux are the following: 045e:02ae Camera, 045e:02ad Audio, 045e:02c2 Motor):

In text form, the coordinates (x,y) of each microphone in cm are:
channel 1: (11.3, 2)
channel 2: (-3.6, 2)
channel 3: (-7.6, 2)
channel 4: (-11.3, 2)
The assignment between microphones and channels in the audio device was obtained by playing a tone with my telephone next to each microphone and using libfreenect's wavrecord tool to record the audio. I then picked the channel with the highest sound intensity with the help of audacity (see screendump below)


some useful sox commands to handle the files returned by wavrecord:
1) merge the four channels in a single wav file (note however that the program you use to reproduce the resulting file might interpret some channels as surround or low frequency channels):
sox channel1.wav channel2.wav channel3.wav channel4.wav --channels 4 -M output_4ch.wav

2) create a stereo file by selecting only the extreme microphones (right and left channels correspond to right and left in the video from the camera):
sox channel1.wav channel4.wav -M --channels 2 output_stereo.wav

3) create a stereo file by mixing all four channels with weights determined by microphone position. Note that this not necessarily better than 2. If we call x the set of x coordinates for the mics, we have (in order of channels)
x = [11.3 -3.6 -7.6 -11.3]
if we call ldx the distance from the left microphone (ch1) normalized by the distance between the extreme microphones:
ldx = [0.0 0.6593 0.8363 1.0]
we can define, somewhat arbitrarily, the left weights lw as (1-ldx)^2 and additionally impose that the weights should sum to 1:
lw = [0.875 0.1016 0.0235 0.0]
If we repeat the same procedure for the right channel we obtain:
rw = [0.0 0.2037 0.3277 0.4686]
Then we can use sox to produce the mixture with the following command

sox channel1.wav channel2.wav channel3.wav channel4.wav -M --channels 2 output_stereo_mixture.wav remix 4v0.875,3v0.1016,2v0.0235 2v0.2037,3v0.3277,1v0.4686

Wednesday, August 11, 2010

LaTeX formulas in XFig figures using PDFLaTeX

this post is to explain how to insert mathematical formulas in figures created with XFig when using PDFLaTeX (and therefore when PSFrags is not an option). The procedure is actually so painless that I would recommend it even when PSFrags is available.
  1. when inserting the text in XFig, make sure you set the "Special text" flag. This can be done even after inserting the text by opening the Edit dialog window. You can mix normal text and math with the usual $ symbol.
  2. instead of exporting to PDF, export as "Combined PDF/LaTeX (both parts)". This will create two files figure00.pdf and figure00.pdf_t (assuming the name of the XFig file is figure00.fig).
  3. In your document, instead of using \includegraphics{figure00.pdf} include the _t file with \input{figure00.pdf_t}. If you want to resize the picture, you can use the command \resizebox as shown in the example below
  4. run pdflatex as usual.

Here are example files:

Some text before the figure.


#FIG 3.2 Produced by xfig version 3.2.5b
1200 2
2 1 0 1 0 7 50 -1 -1 0.000 0 0 -1 1 0 2
0 0 1.00 60.00 120.00
1800 5490 1800 1395
2 1 0 1 0 7 50 -1 -1 0.000 0 0 -1 1 0 2
0 0 1.00 60.00 120.00
315 5310 5535 5310
3 0 0 1 0 7 50 -1 -1 0.000 0 0 0 4
585 5220 1980 5130 3195 4320 4545 1440
0.000 1.000 1.000 0.000
4 0 0 50 -1 0 12 0.0000 6 210 735 5040 5625 $\\alpha$\001
4 0 0 50 -1 0 12 0.0000 6 210 945 1935 1665 $e^\\alpha$\001


Friday, August 6, 2010

TCP Window scaling may cause transfer problems

I noticed this problem first in Italy with an Alice Gate 2 Plus Wi-Fi modem and now I see a similar behaviour in Sweden with a NETGEAR Wireless Cable Voice Gateway modem used by ComHem. This made me think it was a good idea to rewrite in English my original post which was in Italian.

The symptoms (Alice Gate 2 Plus Wi-Fi):

- In GNU Linux this modem/router only works in modem modality, that is, by manually creating a pppoe connection with pppoeconf/pon/poff. If used as a router (automatic connection) web pages are only loaded partially or not loaded at all. Skype works in any case.
- In Windows 7, the router modality only works in combination with MS Internet Explorer (other browsers, such as Firefox and Chrome, show the same problem as in Linux)

The symptoms (NETGEAR Wireless Cable Voice Gateway):

- In GNU Linux the NETGEAR gateway works fine as long as browsing is concerned. When using SSH and many services using SSH, such as SVN, rsync, etc, sometimes the connection stalls and eventually times out
- I do not have a Windows computer in Sweden and could not check if the problem affects Windows as well, but, given the problems in saw in Italy, I would guess it does at least starting with Vista and newer versions.

The problem:

It seems that in both cases the routers do not handle properly the TCP option "window scaling" also called "receive window auto-tuning". This option is implemented in Linux and in Windows starting with Vista, and that is why the problem never occurred with Window XP or older.

The solution:

A solution is to disable window scaling in the TCP options, that is
- in GNU Linux open a terminal and run:
sudo sysctl net.ipv4.tcp_window_scaling=0
To make the option permanent, you have to add the line net.ipv4.tcp_window_scaling=0 in the /etc/sysctl.conf file.
- in Windows, start cmd as administrator and run:
netsh int tcp set global autotuninglevel=disabled

This may make the TCP protocol less efficient, so do this only if you encounter the problems described above.

Thursday, January 21, 2010

Che sia sempre maledetto

Aggiornamento 2011-01-28: Moonlight perde la sincronizzazione tra audio e video a meno che non si muova il mouse in continuazione sull'immagine. Un hack proposto da questo post è di installare i tool di xautomation con
sudo apt-get install xautomation
ed eseguire dal terminal:
while ((1)); do xte 'mousermove 1 1'; sleep 0.02; xte 'mousermove -1 -1'; sleep 0.02; done
e posizionare il mouse da qualche parte sull'immagine. Il comando muoverà il puntatore del mouse in continuazione, facendo funzionare magicamente la sincronia tra audio e video in moonlight. Rispetto alla soluzione riportata nel link qui sopra, ho aggiunto i comandi sleep che rendono il processo meno oneroso per il computer. Sul mio, il processo Xorg prende il 20% della cpu senza sleep e 5% della cpu con sleep.

Aggiornamento 2010-09-16: pare che, o più probabilmente Moonlight, abbia risolto il problema. È ora possibile installare il plugin cliccando su "installa Silverlight" e seguendo le istruzioni. Io ho provato solo con la versione 3.0 di Moonlight che funziona su Firefox e Chrome (Ubuntu 10.04 e versioni correnti dei browser). Peccato che il plugin usi molte più risorse di ogni altro plugin video abbia provato.

Aggirnamento 2010-05-14: il risentimento per chi gestisce il sito è sempre vivo, ma la soluzione qui sotto sfortunatamente non funziona più. Questa è la pagina più completa che ho trovato con soluzioni che potrebbero ancora funzionare, anche se il titolo riguarda le dirette.

oggi ci occupiamo di un altro problema che assilla gli utenti Linux (e spesso anche Mac) che vogliono vedere i contenuti online della Rai Radio Televisione Italiana. Purtroppo chi gestisce il sito, come precedentemente il sito di raiclicktv, non ha alcuna considerazione per l'accessibilità dei propri contenuti. Il sito cambia sistema di streaming ogni tot mesi e introduce limitazioni arbitrarie per chi non usa la combinazione standard di prodotti microsoft.

Si potrebbe obiettare: "avete scelto un sistema operativo meno usato, dovete essere pronti ad accettarne le conseguenze". In realtà io uso giornalmente siti simili a quello della rai (CNN, BBC, SVT, per fare alcuni esempi), e nessuno mi ha mai dato alcun problema. Inoltre, per quanto possa accettare che una piccola azienda concentri lo sviluppo dei propri prodotti sulle piattaforme più diffuse, questo non può essere vero per grandi aziende che, facendo informazione, dovrebbero aspirare a raggiungere un pubblico più vasto possibile. Questo a maggior ragione dal momento che soluzioni de facto standard sono disponibili al giorno d'oggi, come dimostrano gli esempi fatti in precedenza.


Il sito costringe gli utenti ad usare un software particolare (SilverLight) per fruire dei contenuti multimediali. Una versione linux esiste (MoonLight), ma il sito ne preclude l'utilizzo con dei controlli aleatori sul sistema operativo/browser...

La mia soluzione

Molti programmi disponibili in linux (se non tutti) sono in grado di riprodurre il formato di stream usato da L'unica cosa noiosa è ottenere l'URL giusto per ogni contenuto. La soluzione qui proposta semplifica questo passo.

Innanzi tutto dovrete installare un paio di piccole utilità se non le avete già nel sistema, eseguendo il comando seguente in un terminale (funziona su Ubuntu o distribuzioni basate su Debian):
sudo apt-get install xsel wget

Poi, ogni volta che state cercando di vedere un video su e avete ricevuto invece la scritta (nero su blu per cecarvi meglio): "Per visualizzare il contenuto video installa Moonlight da questo sito e poi torna sulla pagina.", voi:
1) Copiate l'URL della pagina web nel clipboard con Ctrl-C
2) Eseguite il comando:
wget -qO - `xsel --clipboard` | grep "videoURL =" | sed 's/^.*videoURL = \"//g' | sed 's/\";$//g' | xargs vlc

Il comando estrae l'URL del video e lo apre con vlc. Potete create un pulsante sulla barra menu che esegue il comando per semplificare ancora l'operazione, oppure cambiare vlc con il vostro programma multimediale preferito.

Naturalmente questa soluzione funzionerà finché alla rai non cambieranno la struttura HTML delle pagine web.

data la grande richiesta, per creare un pulsante nel pannello di gnome, create un file dal nome ~/.gnome2/panel2.d/default/launchers/ e copiateci il seguente testo:

#!/usr/bin/env xdg-open

[Desktop Entry]
Exec=sh -c "wget -qO - `xsel --clipboard` | grep \\"videoURL =\\" | sed 's/^.*videoURL = \\\\\\"//g' | sed 's/\\\\\\";$//g' | xargs vlc"
Comment[en_DK]=Apri il video della pagina il cui URL è nel clipboard
Comment=Apri il video della pagina il cui URL è nel clipboard

Poi fate log-out e di nuovo log-in.

Thursday, January 7, 2010

Alice Gate 2 Plus WiFi e TCP windows scaling

I miei genitori sono sfortunati abbonati di Alice ADSL Tutto incluso. Dopo un anno di sofferenze comincio a capire il motivo dei problemi continui (a patre l'iniziale consegna di un modem difettoso).

Il modem è un Dial Face (non Pirelli) e mostra nella pagina di configurazione ( le seguenti informazioni:

nome: Alice Gate 2 Plus Wi-Fi
hardware: S3PM-2
software: AGIA_1.2.0
smart card: non attiva

I sintomi:

- in GNU Linux il modem/router funziona correttamente solo come modem, cioè stabilendo manualmente una connessione pppoe con pppoeconf/pon/poff. Se usato come router (connessione automatica) le pagine web vengono caricate solo parzialmente o non caricate affatto. Skype funziona in ogni caso.
- in Windows 7, la modalità router funziona solo se si usa Internet Explorer (usando altri browser si hanno gli stessi problemi di Linux).

Il problema:

il modem/router non gestisce bene l'opzione TCP "window scaling" anche chiamata "receive window auto-tuning", adottata dal kernel di Linux e da Windows a partire da Vista.

La soluzione:

Disabilitare il window scaling nelle opzioni TCP, cioè
- in GNU Linux, apri un terminale ed esegui
sudo sysctl net.ipv4.tcp_window_scaling=0
Per rendere la modifica permanente, basta inserire la riga net.ipv4.tcp_window_scaling=0 nel file /etc/sysctl.conf.
- in Windows, avvia cmd come amministratore ed esegui
netsh int tcp set global autotuninglevel=disabled

Questo probabilmente rende il protocollo TCP meno efficiente.


è di usare l'"attrezzo" come un semplice modem stabilendo ogni volta connessioni pppoe, quando avrò tempo scriverò come rendere la connessione pppoe automatica in Windows (in Linux ancora non è semplice).

Per ulteriori informazioni clicca qui.