2015年10月19日 星期一

qsort in c library

qsort shouldn't use a compare function like
int cmp(const void* a, const void* b){
    return *(int*)a - *(int*)b;
}
it would easily cause a overflow, like 2000000000-(-2000000000), it would  overflow and become a negative number.

instead, we should use a compare function like
int cmp(const void* a, const void* b){
    if(*(int*)a > *(int*)b)return 1;
    else if(*(int*)a < *(int*)b)return -1;
    return 0;
}

2015年10月7日 星期三

SSD optimizing in linux

If your are using ext4 as file system in SSD, you may consider add discard and noatime parameter in /etc/fstab.
discard makes it trim. And noatime makes it not modify the last read time information for file.

And you can change CFS i/o scheduler to noop scheduler.
The approach is to modify the /etc/default/grub, add elevator=noop in kernel parameter.
And do
update-grub
Or if you have many storage devices not only SSD but also motion driven devices, you can  edit or create a file in
/etc/udev/rules.d/60-schedulers.rules
contains
# set noop scheduler for non-rotating disks 
ACTION=="add|change", KERNEL=="sd[a-z]", ATTR{queue/rotational}=="0", ATTR{queue/scheduler}="noop" 
# set cfq scheduler for rotating disks 
ACTION=="add|change", KERNEL=="sd[a-z]", ATTR{queue/rotational}=="1", ATTR{queue/scheduler}="cfq"
And use
cat /sys/block/sda/queue/scheduler
to see the scheduler status.

2015年10月3日 星期六

test disk speed

hdparm -Tt /dev/sda

mount /dev/sda at /test
dd if=/dev/zero of=/test bs=1M count=1024

iotop
iometer
iozone

2015年9月25日 星期五

An easy way to parallel your c code by using openmp

if your code has low dependencies between each others, you can just use openmp to parallelize your code.
In ubuntu, openmp is installed defaultly, so we don't need to install anything.
In your code, add
#include <omp.h>
in headers.
And add
#pragma omp parallel for
before your for loop.
like

And compile with flag -fopenmp.
gcc ctest.c -fopenmp .....
Then it would run like this.