Wednesday, May 13, 2009

Matrix Multiplication using pthread

Following code shows how pthreads can be used for matrix multiplication. Pthread is a POSIX standard for threads. The standard defines an API for creating and manipulating threads.You can download the code here.

/******************************************************************************
* Matrix Multiplication Program
* Heshan Suriyaarachchi
* ABOUT:
* Matrix multiplication is done via pthreads
*
******************************************************************************/

#include <stdio.h>
#include <sys/types.h>
#include <pthread.h>

#define MAX_THREAD 20 /* number of threads */
#define NDIM 200 /* number of NDIMs */
double a[NDIM][NDIM];
double b[NDIM][NDIM];
double c[NDIM][NDIM];

typedef struct
{
int id; /* identification */
int noproc; /* process */
int dim; /* number of threads */
double (*a)[NDIM][NDIM],(*b)[NDIM][NDIM],(*c)[NDIM][NDIM];
} parm;

void mm(int me_no, int noproc, int n, double a[NDIM][NDIM], double b[NDIM][NDIM], double c[NDIM][NDIM])
{
int i,j,k;
double sum;
i=me_no;
while (i<n) {

for (j = 0; j < n; j++) {
sum = 0.0;
for (k = 0; k < n; k++) {
sum = sum + a[i][k] * b[k][j];
}
c[i][j] = sum;

}
i+=noproc;
}
}

void * worker(void *arg)
{
parm *p = (parm *) arg;
mm(p->id, p->noproc, p->dim, *(p->a), *(p->b), *(p->c));
}


void main(int argc, char *argv[])
{
int j, k, noproc, me_no;
double sum;
double t1, t2;

pthread_t *threads;

parm *arg;
int n, i;

for (i = 0; i < NDIM; i++)
for (j = 0; j < NDIM; j++)
{
a[i][j] = i + j;
b[i][j] = i + j;
}

if (argc != 2)
{
printf("Usage: %s n\n where n is no. of thread\n", argv[0]);
exit(1);
}
n = atoi(argv[1]);

if ((n < 1) || (n > MAX_THREAD))
{
printf("The no of thread should between 1 and %d.\n", MAX_THREAD);
exit(1);
}
threads = (pthread_t *) malloc(n * sizeof(pthread_t));

arg=(parm *)malloc(sizeof(parm)*n);
/* setup barrier */

/* Start up thread */

/* Spawn thread */
for (i = 0; i < n; i++)
{
arg[i].id = i;
arg[i].noproc = n;
arg[i].dim = NDIM;
arg[i].a = &a;
arg[i].b = &b;
arg[i].c = &c;
pthread_create(&threads[i], NULL, worker, (void *)(arg+i));
}

for (i = 0; i < n; i++)
{
pthread_join(threads[i], NULL);

}
/* print_matrix(NDIM); */
check_matrix(NDIM);
free(arg);
}

print_matrix(dim)
int dim;
{
int i,j;

printf("The %d * %d matrix is\n", dim,dim);
for(i=0;i<dim;i++){
for(j=0;j<dim;j++)
printf("%lf ", c[i][j]);
printf("\n");
}
}

check_matrix(dim)
int dim;
{
int i,j,k;
int error=0;

printf("Now checking the results\n");
for(i=0;i<dim;i++)
for(j=0;j<dim;j++) {
double e=0.0;

for (k=0;k<dim;k++)
e+=a[i][k]*b[k][j];

if (e!=c[i][j]) {
printf("(%d,%d) error\n",i,j);
error++;
}
}
if (error)
printf("%d elements error\n",error);
else
printf("success\n");
}


Monday, May 11, 2009

Ubuntu Jaunty Jackalope

Ubuntu 9.04 aka Jaunty Jackalope was released on April. You can download it from here.

Monday, May 4, 2009

Setting up NS2 in Ubuntu

1. Download ns-allinone-2.33.tar from here.

2. Place it in somewhere and extract it.
$ cd /home/installations
$ tar -xvf ns-allinone-2.33.tar
3. Download & install some packages from repository.
$ sudo apt-get install build-essential autoconf automake libxmu-dev
4. Install the ns2.
$ cd ns-allinone-2.33
$ ./install
5. Edit paths.
$ gedit ~/.bashrc
Add follwoing lines on that file.
# LD_LIBRARY_PATH
OTCL_LIB=/home/installations/ns-allinone-2.33/otcl-1.13
NS2_LIB=/home/installations/ns-allinone-2.33/lib
X11_LIB=/usr/X11R6/lib
USR_LOCAL_LIB=/usr/local/lib
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:
$OTCL_LIB:$NS2_LIB:$X11_LIB:$USR_LOCAL_LIB

# TCL_LIBRARY
TCL_LIB=/home/installations/ns-allinone-2.33/tcl8.4.18/library
USR_LIB=/usr/lib
export TCL_LIBRARY=$TCL_LIB:$USR_LIB

# PATH
XGRAPH=/home/installations/ns-allinone-2.33/bin:/home/installations/ns-allinone-2.33/tcl8.4.18/unix:/home/installations/ns-allinone-2.33/tk8.4.18/unix
NS=/home/installations/ns-allinone-2.33/ns-2.33/
NAM=/home/installations/ns-allinone-2.33/nam-1.13/
export PATH=$PATH:$XGRAPH:$NS:$NAM
6. Validate it.
$ cd ns-2.33
$ ./validate
7. Create a symlink.
$ sudo ln -s /home/installations/ns-allinone-2.33/ns-2.33/ns /usr/bin/ns
8. Run it.
$ ns

Tuesday, April 21, 2009

Google Summer of Code Accepted Projects Announced

Yesterday I received an email from Google stating that my project proposal for Google Summer of Code 2009 has been selected. I am thrilled of being selected as a GSoC student for Python Software Foundation. I would like to take this oppertunity to thank Jim Baker and Keith Chapman for helping me to iron out my proposal. My mentor for this project will be Matt Boersma. Frank Wierzbicki and Jim Baker will be actively involved in this project as well. I am looking foreward to having a great summer with GSoC.

Thursday, April 2, 2009

Nenapiyasa TV Program on ITN

නැණ පියස TV program is telecast on ITN every Saturday at 5.30 pm. It discusses what is happening in the IT industry of Sri Lanka. Last year we had the opportunity to participate in one of it's discussions on Google Summer of Code. Nishantha and I represented UCSC & Vajira and Hiranya represented University of Moratuwa for this discussion as Google Summer of Code students.


Wednesday, April 1, 2009

Online Sinhala Unicode Keyboard

Although I have been using this for a while, I didn't have a chance to blog about it. It is developed by Language Technology Research Laboratory (LTRL) of UCSC. With the help of this tool you can easily blog or update your status messages in Sinhala.

I'm a SlideShare RockStar - PRANK!

Today, I received an email from SlideShare team stating
Hi heshans,

We've noticed that your slideshow on SlideShare has been getting a LOT of views in the last 24 hours. Great job ... you must be doing something right. ;-)

Why don't you tweet or blog this? Use the hashtag #bestofslideshare so we can track the conversation.

Congratulations,
-SlideShare Team

It was the presentation I did for our junior batch on January. Upto now it had 50058 number of views. :) I thought that "would this much of people would want to look at my presentation?"

Irony
My stay at the limelight was short lived. Since it was the 1st of April ,I needed to verify whether this was correct or incorrect, before posting the blog. So I went googling to find out whether others have received similar emails from Slideshare. Then found out that it was a April 1st prank.

Verdict
SlideShare was very useful for me, when I wanted to embed my powerpoint presentations to my blog. It is very sad that they have used a cheap way to market their website like that. In reality they do not have to market their website because everyone who uses SlideShare knows it's true value. In the end the marketing guys of SlideShare managed to tarnish the reputation of SlideShare.

Nice going guys you did great job!