Posted on September 21, 2011
I have just converted a few VIM themes to CSS that can be used with Pygments using the python script described here(GitHub repo).
If anyone is interested (you'll need this if you are using Jekyll), the GitHub repository with the CSS files can be found here and at the moment it contains the following VIM themes: desert, mustang, no_quarter, peaksea, railscasts, rdark, slate, wombat, nuvola.
Feel free to fork the repository and add your own converted themes, an example of the result using the rdark theme can be seen below, enjoy!
public static class IntTimes
implements ScalaObject
{
public void times(Function1 what)
{
Predef$.MODULE$.intWrapper(1).to(n).foreach$mVc$sp(what);
}
private final int n;
public IntTimes(int n)
{
this.n = n;
super();
}
}
Posted on September 21, 2011
After reading Seven Languages in Seven Weeks:A Pragmatic Guide to Learning Programming Languages i was left with the desire to know more about a couple of languages, namely Scala and Clojure. While i had some experiences with functional programming (Standard ML, Scheme) and other programming paradigms (Prolog) during the university, now some years have passed.
I strongly suggest you to buy the book even if you already know some of the described languages (Ruby, Io, Prolog, Scala, Erlang, Clojure, Haskell), it only gives a gist of these seven languages in a set of lessons ideally completed in three days (that with some individual unguided experimentation will fill a full week) but with its style will make you hungry for more.
If you need a suggestion on a Scala book, at the moment i'm going through Odersky's Programming in Scala and up until now it seems a good book even for those without prior experience in functional(ish) languages. Also, unlike other books, the first chapters are free of enigmatic examples that will only confuse beginners.
I'm practicing Scala solving some problems at Project Euler and for one of those i decided to implement something that resembled Ruby's .times and check the produced java bytecode to get a glimpse of scala's inner workings.
object scalatimes extends App{
implicit def int2inttimes(n:Int)=new IntTimes(n)
class IntTimes(n:Int){
def times(what:Int=>Unit){
(1 to n).foreach(what);
}
}
1000.times { n =>
val n2=n+1;
println(n2);
}
}
The implementation of times is quite trivial, this code makes use of implicit conversions to add to the Int type the new method through the implicit conversion of Int objects to the new IntTimes in the scope of the scalatimes object.
The times function simply uses the given function object of type Int=>Unit within a foreach cycle, the current index will be accessible inside the function body (i suppose that there are better ways to do this).
But what kind of java code does the scala compiler produces for this example?
A quick check shows that six class files are built:
- scalatimes
- scalatimes$
- scalatimes$$anonfun$1
- scalatimes$delayedInit$body
- scalatimes$Int2
- scalatimes$IntTimes
These files follow the usual java .class naming conventions,so the scala compiler produces a scalatimes class with a few inner classes. The class files can be decompiled with Jad but the decompiled result will be only partially complete. But anyway, this will help to understand what is happeing behind the scene.
As expected, the body of IntTimes is contained in an inner class with the same name:
public static class IntTimes
implements ScalaObject
{
public void times(Function1 what)
{
Predef$.MODULE$.intWrapper(1).to(n).foreach$mVc$sp(what);
}
private final int n;
public IntTimes(int n)
{
this.n = n;
super();
}
}
The times method expects a Function1 object as parameter, the body of the function passed to 1000.times() as parameter has been wrapped in an anonymous inner class (as per Jad comment):
/* anonymous class */
public static final class anonfun.cls1 extends scala.runtime.AbstractFunction1.mcVI.sp
implements Serializable
{
public final void apply(int n)
{
apply$mcVI$sp(n);
}
public void apply$mcVI$sp(int v1)
{
int n2 = v1 + 1;
Predef$.MODULE$.println(BoxesRunTime.boxToInteger(n2));
}
public final volatile Object apply(Object v1)
{
apply(BoxesRunTime.unboxToInt(v1));
return BoxedUnit.UNIT;
}
public static final long serialVersionUID;
static
{
0L;
serialVersionUID = 0L;
}
}
The two lines of code that compose the function object body passed to 1000.times() are clearly visible in apply$mcVI$sp(int v1). The main method of the App can be identified in the body of the apply() method of a static class that extends AbstractFunction0:
public static final class on0 extends AbstractFunction0
implements ScalaObject{
public final Object apply()
{
$outer.t1_$eq(System.currentTimeMillis());
$outer.int2inttimes(1000).outer(new ());
Predef$.MODULE$.println((new StringBuilder()).append("Time:").append(BoxesRunTime.boxToLong(System.currentTimeMillis() - $outer.t1())).toString());
return BoxedUnit.UNIT;
}
private final scalatimes$ $outer;
public on1.mcVI.sp(scalatimes$ $outer)
{
if($outer == null){
throw new NullPointerException();
} else {
this.$outer = $outer;
super();
return;
}
}
}
Function0, like the Fuction1 above, is a scala framework object, likely related to the class extended above, and some helper methods in scalatimes hints to the fact that it will be loaded using a delayedInit() call (by an App loader?).
And this ends for now my brief and superficial investigation on the structure of the code produced by the Scala compiler.
Posted on August 19, 2011
With tools like SSH, Git and RSync there are always new tips you don't know about or some clever scripts that can increase your productivity or simply save you a few keystrokes.
Today's tips come from a presentation given at YAPC Europe 2011 that have been summed up in this nice post.
The best one is obviously connection sharing, this allows you to share a single ssh connection channel with more than one ssh session, saving the few seconds needed to establish a new connection.
Just two config lines that you need to put in your ~/.ssh/config:
ControlMaster auto
ControlPath /tmp/ssh_mux_%h_%p_%r
As stated here, every user (e.g. root) that has access to your control path file will be potentially able to use your established connection, be aware of this.
Furthermore, it looks like that without additional Control directives, the disconnection of the master session who opened the first connection also disconnects the other slave sessions, to fix this, as described here you just need to let the connection persist for some time (any timeout would do i guess)
Glad to see that the suggestion to use SSH key authentication is there too, just remember to use a strong key password (and if you haven't done so already disable root login).
Be sure to checkout the Jumping Through Servers section, a good use of netcat.
On a related note, see Senko's Blog for a script that using inotify launches a command when a file in a directory has been changed. He has built it to automatically perform an rsync on selected directories when changes are detected, nice.
Posted on August 10, 2011
After a few years of shared hosting, a few days ago i moved this site on a Linode VPS and so far i am really happy with it, the performances are impressive, especially for what concern disk i/o, way better then what you get with other VPS providers.
With the new hosting i decided it was time to finally drop Wordpress and the boatload of plugins i was using and try an alternative solution that was more appropriate for a site with a small number of pages and no dynamic content like mine.
The old site was already using a caching plugin for WordPress, W3 Total Cache, to produce static pages (regenerated periodically), on the new site i am giving a try to Jekyll to remove some moving parts and simply serve a static site built offline with a generator.
So far i have no complains, i would have preferred if a basic layout with archive/categories/tags/etc... was bundled with Jekyll but checking the numerous examples available on github i've found my way through anyway, and built an initial skeletal layout in a few days than then evolved in what you are seeing now.
As my HTTP server of choice, i have opted for NGINX. Various independent benchmarks performed with different environments and configurations i have read lately seems to confirm that, with the right configuration, NGINX is one of the best http server for static content serving.
If interested you can read some of them here, here and here.
This kind of tests should be taken with a grain of salt, after all the only meaningful benchmark for you is the one you'll perform on your system with your configuration, here is an interesting discussion on the rules of benchmarking.
This is the configuration i am using at the moment, it's optimized for small files and it enables the file cache to reduce disk access and the number of i/o syscalls. I have performed a few tests on my own and this seems to perform quite well on the first two VPS tier of Linode.
user www-data;
worker_processes 2;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
open_file_cache max=1000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
sendfile off;
tcp_nodelay on;
keepalive_timeout 65;
server {
listen 80;
server_name <yourdomain>;
access_log /var/www/logs/<yourdomain>.access.log main;
error_log /var/www/logs/<yourdomain>.error.log;
root /var/www/sites/<yourdomain>;
index index.html index.htm;
}
}
On the NGINX wiki you can find a nice init.d script to launch the server but to manage its lifecycle i suggest to use upstart (installed by default on ubuntu) instead, it will manage your NGINX instance restarting it if it dies unexpectedly.
Create an upstart configuration file at /etc/init/nginx.conf:
description "nginx http daemon"
start on (filesystem and net-device-up IFACE=lo)
stop on runlevel [!2345]
env DAEMON=/usr/local/nginx/sbin/nginx
expect fork
respawn
pre-start script
$DAEMON -t
if [ $? -ne 0 ]
then exit $?
fi
end script
exec $DAEMON -c /etc/nginx/nginx.conf
Verify that upstart has located your new configuration file, if the output of the following command is empty you should check /var/log/daemon.log for errors in the config file:
initctl list | grep nginx
Now you can start NGINX with:
Your NGINX instance will now be respawned if it dies unexpectedly and this can easily be verified with:
killall nginx
ps -Al | grep nginx
Posted on September 6, 2010
I'm planning a series of post about OpenCL basics and its use ( simple kernels, smoke modelling with Navier-Stokes equation and usage with python) , but before that, a short post on upgrading drivers and basic environment configuration could be useful. Especially when done in conjunction with a kernel upgrade, the update of the ATI graphic drivers is a bit tricky.Unlike CUDA or Stream, OpenCL library is CPU/GPU architecture-independent, so this will be the only post where you'll read something limited to ATI card owners.
We'll use ATI Radeon 57xx logical schematics in the next post to understand how modern GPUs are structured internally but every consideration will also apply to GPUs from other vendors.
The recommended procedure to perform a driver update consists of three steps: uninstalling the old drivers, building custom .deb packages/installing them, build the initial configuration.So, first of all, uninstall the drivers and delete the obsolete fglrx packages:
sudo sh /usr/share/ati/fglrx-uninstall.sh
sudo apt-get remove --purge fglrx_* xorg-driver-fglrx
Download from ati.com the latest driver, generate .deb and install (using 10.04, same procedure applies to other recent builds):
sh ati-driver-installer-10-8-x86.x86_64.run --buildpkg Ubuntu/lucid
sudo dpkg -i fglrx*.deb
Initialize /etc/X11/xorg.conf (a backup is generated) and after this reboot/restart X:
sudo aticonfig --initial -f
To verify that everything has gone as expected (this will display the current driver version):
Regarding OpenCL environment configuration, the installation is easy and fast and the official ATI documentation does a good job.
Get the Stream SDK and the OpenCL ICD configuration (new in 2.2) from the developer site and follow these steps.Untar the SDK in a directory of your choice and add to your .bash_profile (or equivalent) these lines, customizing where needed:
export ATISTREAMSDKROOT=<...>/ati-stream-sdk-v2.2-lnx64export LD_LIBRARY_PATH=$ATISTREAMSDKROOT/lib/x86_64:$LD_LIBRARY_PATH
Untar the ICD configuration archive (will put vendor-specific configuration files in /etc/OpenCL/vendors):
cd /
sudo tar xfz icd-registration.tgz
To verify the successful installation, reload your .bash_profile (source ~/.bash_profile) go to .../ati-stream-sdk-v2.2-lnx64/samples/opencl , compile the opencl samples with make and then run from .../ati-stream-sdk-v2.2-lnx64/samples/opencl/bin/x86_64:
Verify that both CL_DEVICE_TYPE_CPU and CL_DEVICE_TYPE_GPU are present in the output.As last test and if you want to qualitatively evaluate performance differences, try Mandelbrot with --device cpu and --device gpu.