Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Sunday, 2 January 2011

Parallel Computing Using Intel TBB in Linux

What is Parallel Computing???

Parallel computing is a computation method that run some calculations simultaneously (based on wikipedia). Using this method, the cpu optimally used, one processor do different calculation with the other processors. It will make the calculation faster. But we can't use it directly, we have to separate sequential calculation and parallel calculation then optimized it of course. 


Here I want to share the information about using intel TBB in ubuntu for parallel computing. Intel TBB is C library for parallel computing. It build based on C++ template so it is easy to use. In this blog I will give an example of using parallel_for loop. we can use it either in one dimension loop or two dimensions loop. But, in this kind of loop must be independent each other so there will be no stack or overflow.

For example of one dimension looping, supposed we have a serial routine calculation below:

    void SerialApplyFoo( float a[], size_t n ) {

         for( size_t i=0; i++) Foo(a[i]);

    }

Foo is a function that has independent loop iteration.
The iteration is from size_t = 0 till size_t = n-1
By using tbb::parallel_for we can separate iteration into chunks and run on separate thread. First, we have to build a class that has operator to proceed the chunks. Here is the example of the class:

    #include "tbb/blocked_range.h"
    class ApplyFoo {
    float *const my_a;
    public:
        void operator( )( const blocked_range(left bracket)size_t(right bracket)&r ) const {
            float *a = my_a;
            for( size_t i=r.begin(); i!=r.end( );i++ )
                Foo(a[i]);
        }
    ApplyFoo( float a[] ) :
    my_a(a)
    {}
    };

A blocked_range is a template class provided by the library. It describes a one-dimensional iteration space over type T. Class parallel_for works with other kinds of iteration spaces, too. The library provides blocked_range2d for two-dimensional spaces. After you have written the loop body as a body object, invoke the template function parallel_for, as shown below:

    #include "tbb/parallel_for.h"
    void ParallelApplyFoo( float a[], size_t n ) {
        parallel_for(blocked_range(0,n,YouPickAGrainSize), ApplyFoo(a) );
    }

The blocked_range constructed here represents the entire iteration space from 0 to n–1, which parallel_for divides into subspaces for each processor. The general form of the constructor is:

    blocked_range(begin,end,grainsize)

The T specify the value type. The begin represent the start of the iteration and the end represent the end of iteration while grainsize is the size of our chunks. The begin and end argument specify the iteration space in half-open interval style [begin,end). 

For the example of 2 dimension loop, I will show you the following code, this is from my assignment. I build it on a single file so it easier to read.



If we run this code it will produce the result (after you plot it, I'm using gnuplot) below:



Reference:
[1] Wikipedia, Parallel Computing, http://en.wikipedia.org/wiki/Parallel_computing
[2] James Reinders, 2007, Intel Threading Building Blocks, O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.

ps: I'm sorry i just put image code because it disturb the html code for this blog. If you want the code feel free to contact me and of course if there are some questions or critics. 

Enjoy it!!!

Friday, 24 September 2010

How to Create Linux USB Flashdisk Boot in Windows from ISO Image

Actually i want to share what is the advantage of creating boot-able USB flasdisk, there are:
  1. no need cd/dvd  room (because we use USB)
  2. faster than boot from cd
  3. more durable (there is no cyclic redundancy error)
  4. etc. (hehehe)
first of all, you have to download unetbootin-linux from here. then just run it from windows as administrator. this software has compatibility with windows xp, vista and win7. the step to create bootable flashdisk are:
  1. set the type of linux distro and its version.
  2. checklist Disk image and choose iso image file.
  3. choose the drive where the USB flasdisk mounted.
  4. click ok then see that your USB has turn into boot-able linux live cd.
  5. restart your PC then set boot setting to USB drive.
  6. enjoy it!

ps: i've used this way and now i run Ubuntu 10.04 lucid 64bit.

Saturday, 18 September 2010

How to Install Scilab Image Video Processing Toolbox in Linux Ubuntu

First of all, before installing SIVP toolbox, you've to install package below:
  1. Scilab (of course)
  2. OpenCV (1.0.0 or earlier)
To install scilab you can use either from source or binary file. Both have the same result. You can also use package manager to install this software. If you are advance user, i recommend you to use source or binary. If you use binary file, just put the package in the directory that you want to use. to execute these file, open terminal, enter the directory /scilab-5.2.x/bin and run scilab by write:

 $./scilab

 you can directly run scilab.

if you want to use package manager go to system/administration/synaptic package manager  on your ubuntu desktop and write scilab. then checklist the following:


scilab
scilab-cli
scilab-data
scilab-include
scilab-doc
scilab-full-bin
then apply...

after these process you can use scilab and it will appear in Application/Programming/ on your desktop.
>>Check it out Please..

To install OpenCV you can also use package manager or source file. If you use package manager, do as follows:
Go to Synaptic Package Manager, search for “opencv” and install the main “opencv” package and the following lib files:
libcv4
libcv-dev
libcvaux4
libcvaux-dev
libhighgui4
libhighgui-dev
opencv-doc
then apply..

After installing all the packages, open a terminal & type this code:
$export LD_LIBRARY_PATH=/home/opencv/lib
$export PKG_CONFIG_PATH=/home/opencv/lib/pkgconfig

To check the path where opencv & other lib files are stored, do:
$ pkg-config --cflags opencv
(output will come as)
-I/usr/include/opencv 

$ pkg-config --libs opencv
(output will come as)
-lcxcore -lcv -lhighgui -lcvaux -lml

These paths are needed to compile your opencv programs
Now You're Ready to Install SIVP Toolbox..
You've to download the SIVP source from this. Extract that file then copy it to directory where you put scilab. Put it in Scilab-5.2.x/share/scilab/contrib/

Before run this file you have to change permission to the fil:
/contrib/sivp/builder.sce by write
sudo chmod a+x builder.sce

Then run scilab and write:
cd 'SCI/contrib/sivp' (this is to enter sivp directory)
exec 'builder.sce'

After re-run scilab you can run this toolbox.
>>Check This Out..

ps: i've tried this way and i success (after 3 days, cause i'm really newbie in using linux)...