Wednesday, December 29, 2010

Vector Addition using SIMD

#include 

#define VECTOR_SIZE         4
typedef float v4sf __attribute__ ((vector_size(sizeof(float)*VECTOR_SIZE))); // vector of four singl
e floats

typedef union f4vector
{
    v4sf    v;
    float   f[VECTOR_SIZE];
} f4vector;


void print_vector (f4vector *v)
{
    printf("%f,%f,%f,%f\n", v->f[0], v->f[1], v->f[2], v->f[3]);
}

int main()
{
    union f4vector a, b, c;

    a.v = (v4sf){1., 2., 3., 4.};
    b.v = (v4sf){5., 6., 7., 8.};
    c.v = a.v + b.v;

    print_vector(&a);
    print_vector(&b);
    print_vector(&c);
}

Compile with the following command:
gcc -ggdb -mtune=pentium3 -march=pentium3 -c -O3 -ffast-math -mfpmath=sse -msse5 sse.c

To test, just link the object code to binary:

gcc -lm sse.o -o sse

$ ./sse
1.000000,2.000000,3.000000,4.000000
5.000000,6.000000,7.000000,8.000000
6.000000,8.000000,10.000000,12.000000

The assembled code:

$ objdump -dS ./sse.o | grep -2 c.v | tail -8
  7c:   0f 58 c1                addps  %xmm1,%xmm0
  7f:   0f 29 45 c8             movaps %xmm0,-0x38(%ebp)
--
 120:   f2 0f 11 44 24 04       movsd  %xmm0,0x4(%esp)
 126:   e8 00 00 00 00          call   12b <_main+0xdb>
        c.v = a.v + b.v;

        print_vector(&a);

As we can see, it's very optimized where adding 4 components of vector a and b is done in one SSE instruction (addps) instead of multiple instructions if we don't use -msse and -mfpmath=sse



How fast is the program?

$ time ./sse
1.000000,2.000000,3.000000,4.000000
5.000000,6.000000,7.000000,8.000000
6.000000,8.000000,10.000000,12.000000

real    0m0.109s
user    0m0.046s
sys     0m0.030s

Thursday, December 23, 2010

Mac OSX Lion: Another Windows remake?

Apple has just updated its website and now it announces that they plan to release another Mac OS-X named "Lion".  A sneak peak to the features, some of the features are not really "wow" me and even seems too-old to be a breakthrough.  For example, "LauchPad".  Windows xx has had it for long time as "Desktop icons".  Another one is "Mission Control" which the similar feature has been in Windows 7 for awhile.

Unfortunately, Apple has not revealed all the features they plan to put in OS-X.  Not sure if the upgrade worth the cost of upgrade (well, if it is only $25 upgrade I'll just go ahead and upgrade mine).

Sunday, November 14, 2010

Downsampling MP3 file

#!/usr/bin/tclsh

set ffmpeg [exec which ffmpeg]
puts "ffmpeg  = $ffmpeg"
if { [llength $argv] < 2 } {
    puts "\n$argv0  n bps\n"
    exit -1
}
set fi [lindex $argv 0]
set br [lindex $argv 1]
puts "$fi"
puts "target $br bps"

if { [regexp -all {(.*)\.([mM][pP]3)} $fi a b c] } {
    set fo "${b}_${br}bps.$c"
    puts "$fi ==> $fo, ext=$c, new bitrate=$br"
    set id3 [exec id3v2 -l $fi]
    set id3par ""
    if { [regexp -line {TIT2.*: (.*)\n} $id3 dummy title c] } { puts Title=$title }
    if { [regexp -line {TPE1.*: (.*)\n} $id3 dummy singer c] } { puts singer=$singer }
    if { [regexp -line {TALB.*: (.*)\n} $id3 dummy album c] } { puts album=$album }
    if { [regexp -line {TYER.*: (.*)\n} $id3 dummy year c] } { puts year=$year }
    if { [regexp -line {TCON.*\(([0-9]+)\)\n} $id3 dummy genre c] } { puts genre=$genre }
    set cmd "$ffmpeg -threads 16 -y -ab $br -i $fi $fo"
    if {![file exists $fo]} {
        if { [catch { set res [eval exec $cmd] fid }] } {
            #puts stderr "Could not execute $cmd"
            if {[info exists fid] } { puts stderr $fid" }
            #exit 1
        }
    }
    if {[info exists title]} { append idpar " --TIT2 \"$title\"" }
    if {[info exists singer]} { append idpar " --TPE1 \"$singer\"" }
    if {[info exists album]} { append idpar " --TALB \"$album\"" }
    if {[info exists year]} { append idpar " --TYER \"$year\"" }
    if {[info exists genre]} { append idpar " --TCON \"$genre\"" }
    set cmd "id3v2 $idpar $fo"
    puts $cmd
    if { [file exists $fo]} {
        eval exec $cmd
    }
} else {
    puts "Not an MP3 file"
}


PAM Security explained

PAM Security: http://articles.techrepublic.com.com/5100-10878_11-1055269.html

Login restriction/Limitation based on time schedue

This is how to limit a user (in this case, his name is "Joko") to access computer during weekdays and weekends at certain time duration.

  1. Edit file
    /etc/pam.d/common-account
    and add a line "account required pam_time.so"
  2. Edit file
    /etc/security/time.conf
    and add the following lines to the end of file.

#services  ttys users   times
#
# allow Joko to use computer during weekdays 3 pm-8:30pm OR weekends 9:00am - 9:00pm
* ; * ; joko; Wk1500-2030 | Wk0900-2100

This will not allow joko to login during weekdays before 3 pm or after 8:30 pm or weekends outside 9 am - 9 pm. To disallow certain service, replace '*' in the first entry with a PAM service name (e.g, login, etc.). Files in /etc/pam.d reflect the service names.

On my computer, the following files are the service names can be used:

-rw-r--r-- 1 root root 217 2010-07-05 04:57 atd
-rw-r--r-- 1 root root 167 2010-07-05 06:44 chage
-rw-r--r-- 1 root root 218 2010-07-05 06:44 chfn
-rw-r--r-- 1 root root 218 2010-07-05 06:44 chsh
lrwxrwxrwx 1 root root  17 2010-09-13 10:39 common-account -> common-account-pc
-rw-r--r-- 1 root root 378 2010-07-05 05:15 common-account.pam-config-backup
-rw-r--r-- 1 root root 446 2010-11-14 09:01 common-account-pc
lrwxrwxrwx 1 root root  14 2010-09-13 10:39 common-auth -> common-auth-pc
-rw-r--r-- 1 root root 448 2010-07-05 05:15 common-auth.pam-config-backup
-rw-r--r-- 1 root root 557 2010-11-14 09:01 common-auth-pc
lrwxrwxrwx 1 root root  18 2010-09-13 10:39 common-password -> common-password-pc
-rw-r--r-- 1 root root 855 2010-07-05 05:15 common-password.pam-config-backup
-rw-r--r-- 1 root root 506 2010-11-14 09:01 common-password-pc
lrwxrwxrwx 1 root root  17 2010-09-13 10:39 common-session -> common-session-pc
-rw-r--r-- 1 root root 435 2010-07-05 05:15 common-session.pam-config-backup
-rw-r--r-- 1 root root 573 2010-11-14 09:01 common-session-pc
-rw-r--r-- 1 root root 287 2010-07-05 04:56 crond
-rw-r--r-- 1 root root  56 2010-09-15 13:49 cups
-rw-r--r-- 1 root root 204 2010-07-05 16:24 gdm
-rw-r--r-- 1 root root 206 2010-07-05 16:24 gdm-autologin
-rw-r--r-- 1 root root 239 2010-07-05 08:06 gnomesu-pam
-rw-r--r-- 1 root root 216 2010-07-28 09:45 init
-rw-r--r-- 1 root root 419 2010-07-05 22:36 login
-rw-r--r-- 1 root root 251 2010-07-05 05:15 other
-rw-r--r-- 1 root root 133 2010-07-05 06:44 passwd
-rw-r--r-- 1 root root 165 2010-07-05 06:18 polkit
-rw-r--r-- 1 root root 165 2010-07-05 07:10 polkit-1
-rw-r--r-- 1 root root 173 2010-07-05 14:34 ppp
-rw-r--r-- 1 root root 481 2010-07-05 05:32 remote
-rw-r--r-- 1 root root 165 2010-09-15 14:14 samba
-rw-r--r-- 1 root root 209 2010-07-05 06:44 shadow
-rw-r--r-- 1 root root 165 2010-09-14 07:07 smtp
-rw-r--r-- 1 root root 268 2010-07-05 14:41 sshd
-rw-r--r-- 1 root root 239 2010-07-05 05:00 su
-rw-r--r-- 1 root root 203 2010-09-07 06:01 sudo
-rw-r--r-- 1 root root 239 2010-07-05 05:00 su-l
-rw-r--r-- 1 root root 172 2010-07-05 06:44 useradd
-rw-r--r-- 1 root root 569 2010-07-05 05:58 vsftpd
-rw-r--r-- 1 root root 241 2010-07-29 04:36 wxconsole
-rw-r--r-- 1 root root 204 2008-09-03 08:45 xdm
-rw-r--r-- 1 root root 166 2008-09-03 08:45 xdm-np
-rw-r--r-- 1 root root 265 2010-08-23 16:44 xen-api

A good solution to limit use of computer from kids during school days!