Let your heart guide you......It whispers so listen closely

Thursday, August 30, 2012

parallel computing in R

I don't know what would have happened to me and my research if I didn't knew R. It has always been the most co-operative tool. But  RBM was never happy with the speed of programs, specially when I am doing simulations etc. So, one fine day I started googling, the ways to speed up the programs. I invested one complete day and got decent increase in speed. I was very happy and excited that I actually managed something cool; but next day his reactions made me realise that it is not sufficient. 
This led to following two consequences:
i) I was kind of disheartened; so neither I tried going in to greater depth nor did I try formulating and recording the procedure, at least for my record.
ii) I started looking into package making in R (which he always wanted me to do). In last 2 years I have tried doing that at least 2-3 times, but always gave up. But to my surprise, this time I managed within a day (will share that too, soon).

So there I was.. Within 2 days, I had successfully managed implementing 2 reasonably clumsy techniques. But my laziness never allowed me to go ahead with it.

It was only when MGK poked me, I realised that I cannot recall any of it. I didn't knew which files to look into. But eventually I managed. Thanks to ASK, who helped me in reviving. Before I forget again, I made a point to re-search, re-view and document...

Few important notes:
> I have used Revolution R Community 6.0 . One can also try with other versions of R like R 2.15.1, but then you need to make sure if all the required libraries/packages are downloaded/installed. Revolution R has got all of them.

> The version of parallel computing I have used, employs the notion of foreach (a replacement for standard "for statement"). It says "do this to everything in this set", rather than "do this x times".

> We have to register a parallel backend, otherwise foreach will execute tasks sequentially.

%dopar% executes the R expression using the currently registered backend.

So here is a sample code:

packages that have to be loaded
library(parallel)
library(foreach)
library(iterators)
library(doParallel)

to detect the number of CPU cores on the current host
detectCores()  

Creates a set of copies (say 2) of R running in parallel
cl = makeCluster(2) 

registers the parallel backend with the ‘foreach’ package
registerDoParallel(cl)

# to check if the multiple core is registered. If not, a warning will be issued that it is running sequentially. However, warning is issued only once
z=foreach(i=1:10)%dopar% 
{  sample(c("H", "T"), 10000, replace=TRUE)
}

returns the number of execution workers in the currently registered doPar backend; should be same as input to makeCluster
getDoParWorkers()

# sample code
x=foreach(i=1:T,.combine='+')%dopar%
{    # a function which has to be executed T times
     vec=vecofderiv(N,M,orgtpm,orgtP)               
     I=vec%*%t(vec)
}
# x contains the sum of expression, on the last line of the loop (since argument to .combine is '+'). Here x is sum of I’s.

Some more notes:
 >   I am not sure, but my experimentation indicated that it is sufficient to register as many parallel backends as is the number of cores available. There was no improvement if I tried registering more backends than available no. of cores

>  Other arguments to .combine could be 'cbind', 'c', or some user defined function (not very sure about the usage). 

> Since the execution is parallel, make sure that the tasks are not related or dependent.

> This is not the only option available. Though I had to stick to this one as I was not able to exactly understand how to bring others to work, like doSNOW, which is a parallel adapter for SNOW (Simple Network of Workstations) package.  There were few which were not available for Windows, like doMC, which is a parallel adaptor for the multicore package. 


PS-1. Thanks are due to the authors of all the R help files I looked into; and offcourse the R team.
PS-2. Please post your valuable comments, so that we can get a better insight of the procedure.

Post PS: Hope to get back soon, with something light and random ;)

Tuesday, August 21, 2012

मैं हारी, जा-जा री ..

Few days back, some how I stumbled across the movie Khubsoorat. I had watched it long time back and had liked it for many reasons.. one of them is Kaffila-making (probably by Gulzar). All in all its a movie which can make you smile anytime. I had never realised before that this movie is by Hrishikesh Mukherjee. I have always been fan of his movies like Chupke-Chupke, Golmaal, Bawarchi ..now i have another one to add. All of them are comedy in such a homely and warm manner, that a person like me keeps on revisiting them. 

By the way its not the only reason.. for this post and the title. When I revisited Khubsoorat, I noticed the song Piya Bawri, which I overlooked when I saw it for the first time; probably i was too immature back then, to notice such master pieces (planning to come up with some more, which I went crazy for). 
It is a beautiful song written by Gulzar. But what made it more special was the classical part sung by Ashok Kumar in the starting. I was familiar with Rail Gaddi, but I never expected something like this. 

I have no knowledge of classical music, but I went totally gaga over it. I think sometimes it is possible to love something without knowing the science behind it.

Just a 2 min clip with that special part ..


I haven't posted the full song, since its available everywhere. The starting part is not included in the song ANYWHERE, its only there in the movie.. 

Enjoy :)

PS:1. @ MGK & ASK - I know something else was expected today; but some more re-search & re-vision of the facts is required. So will be there with it soon ;)
PS:2. @ Those who are stiill wondering about the title - You got to listen the clip.

Friday, August 17, 2012

stopped writing OR not been writing !!!!

I accept that I have not been writing lately; but one (and everyone) should note that it is NOT same as i have stopped writing. I was really furious and agitated after reading that remark. I don't know why, but it hurt me. It felt like I have been kicked out of the community :-| It is just 115 days since I wrote here. I know that makes almost 1/3rd of an year; but I was actually busy. Just like those, who think that I have stopped writing, all this while even in my mind dozens of post were written and deleted. I had almost formulated so many of them. But somehow, i could never open blogger. Yes I accept, there is no pending/incomplete post waiting to get published. I never picked up the pen, or better way, never tapped my fingers on key board.

There are so many things which were worth sharing, but which lost their importance or my enthusiasm to write now. Since today morning I was so sure that I have to write now, no matter what blah comes out.

Now since I am already back since last 5 minutes I can't resist doing the random babble. Those who are sensitive to noise may please use cotton plugs (or equivalently close this tab).

Around a month back, I almost got a strong feeling (which stayed for around a week) to make my blog private and do all the random not-so-good babble in it.. so that i don't have to answer any questions about what I have written. But the best part of my busy-ness (or rather lazinees) was not even opening the blogger to make it private, forget about writing posts :P

Right now so many random things are circling my mind

> Have been watching tooooo many movies. there is no end to my to-watch list and to-download list. I keep on looking on imdb and then torrent and then vlc and then again something else on imdb and so on.. and the cycle goes on. One fine day I SHOULD share the list of movies which I would definitely recommend.

> Managed to speed up my R program..which didn't help much.

> Was able to make a package in R (though its a very basic one). It took me ages to understand the required instructions which are not as bad as they appeared to be. So it was another wishful plan to end my blog's fasting days by sharing simpler set of instructions to develop a package in R.

> Looked at old problems with a new lens..and i think its helping.. or at least there is some line which has hope.. and remember hope is a good thing :)

> Gathered the courage to face the reality; if something cannot be changed, one has to deal with it. So do I.

> Got into ST-5 P. Enjoying it like anything. A friend was right, when she said, when you teach something you sideline your personal reading and studies, because every time you read, if you really do it, you keep on discovering new and new things from the same content.
..and by the by, teaching is the best job in world  :)

> Realised that my problems are much smaller than those of many people around. Its true..given a chance to exchange, I would prefer my own set.

> Too many people are interested to know about  the tracking issues with India post (or sometimes Zoomin), so much that they never let me feel low, that i am not writing and hence there are no visitors on my place :D

>>>>>>> Too sleepy now to continue.

Hope to babble more, soon  ;)