Thursday, August 14, 2014

OpenMP from Matlab

To get OpenMP support for a Matlab mex function you need to use the following compiler flags. Suppose testp.cpp is your cpp file with the mex function, you need to compile as follows:
mex testp.cpp CFLAGS="\$CFLAGS -fopenmp" LDFLAGS="\$LDFLAGS -fopenmp" CXXOPTIMFLAGS="\$CXXOPTIMFLAGS -fopenmp".
Here is a simple mex function to test this.
//testp.cpp
#include
#include "mex.h"

void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray*prhs[] )
{
unsigned int threadid, nthreads;
#pragma omp parallel private(threadid) shared(nthreads) num_threads(8)
{
threadid = omp_get_thread_num();

if (threadid==0) {
nthreads = omp_get_num_threads();

}
}
mexPrintf("num_threads = %d\n",nthreads);
}

Wednesday, August 19, 2009

Interfacing Matlab and C/C++ using direct dll invocation

Suppose I have an algorithm the implementation of which if run from Matlab is going to take forever. Fortunately I got an implementation of this in c/c++ and would like to invoke this c++ code from Matlab. There are quite a few conventional ways of doing this.
1) invoking the c++ exe file from Matlab passing the right arguments. This is not very attractive since the exe has to be loaded everytime it is going to be used and I am planning to use it atleast a million times in experiment.

2) writing a mex function wrapper for over the c++ implementation and then call the function from matlab. I went quite far with this approach, but the farther I went, it looked lesser appealing due to the following points:
a) there are only a few compilers supported by matlab for building c/c++ files (using the mcc setup). I am using matlab 2006, and it has only lcc and microsoft visual studio without .net supported. I installed another Borland c++ lightweight compiler for the purpose, but the c++ syntax rules were a bit different in it from vc++ syntax and more over I had to convert my other library inclusions to borland omp format using coff2omp.exe utility. Overall it was a pain.
b) I needed to learn a lot of matlab memory allocation and deallocation apis to use the mex files. And I wanted a very fast way of coding this up.
c) The VC++ IDE was useless other than just for coding up my c++ code. It would be nicer if I could use the vc++ ide for building and debugging the code.

3) The above problems motivated me to look at the following very elegant solution, which is to build the c++ algorithm into a dll file and load the dll inside matlab, later calling the function directly.

Here are the steps to do this:
1) Write up your code in VC++ and build it to a dll.  (In case of Linux, you need to build the code to a shared library object using g++ -shared option).
a) You need to change the "configuration type" in the project properties to dll for this.
b) Also, you need to add a __declspec (dllexport) to the function you would like export to matlab. For ex:

__declspec (dllexport) double* MyMatlabFunction(char*, int*, int );

Place all such exports in a single header file, say functionexports.h (this will be required later).
c) Build the code and create a dll, (say mycode.dll) using vc++.

2) load the dll in matlab.
a) loadlibrary(), function in matlab loads a dll. the syntax is given below:
>loadlibrary('mycode', 'functionexports.h');
Note that you need to pass in the dll name and the headerfile referred above to the loadlibrary call. You can check if the library is already loaded using the "libisloaded('mycode.dll')" call and use unloadlibrary('mycode.dll'); to unload a library (which might be needed if you want to rebuild your dll from vc++).

b) Once the dll is loaded, use the calllib() function to invoke the exported dll function. The syntax is as follows:
[outargs]=calllib('libname', 'funname', inargs);
ex: [dptr]= calllib('mycode', 'MyMatlabFunction', 'somestring', x, y);
Thats it.

c) a little tricky part here is to operate on the arguments to and from the function. A good tutorial on how to do this is given in the link here .

A simple example that calls a c++ function that takes three arguments; a string, an integer pointer and an integer, and returning a double pointer is given below:
Matlab call Example:
function [sift, n]=ComputeSift(imgfilename, disp)
tic;
if ~libisloaded('siftlight')
loadlibrary('siftlight', 'siftlight.h');
end

sift=0; n=0;
nptr=libpointer('int32Ptr', n);
siftptr=calllib('siftlight', 'mysift', imgfilename, fptr, disp);

if fptr.Value > 0
setdatatype(siftptr, 'doublePtr', 128, fptr.Value);
sift=siftptr.Value;
n=fptr.Value;
end

t=toc;
fprintf('sift took %f seconds...\n', t);
end

The solution is fast and elegant, you can use the whole power of VC++ for building and debugging, and there is very less to learn (you should just know how to pass in and take back arguments from the call). Hope this information is helpful.

Monday, April 27, 2009

Jane's Paradox!

Taken from the book Hyperspace-by Michio Kaku

A baby girl is mysteriously dropped off at an orphanage in Cleveland in 1945. “Jane” grows up lonely and dejected, not knowing who her parents are, until one day in 1963 she is strangely attracted to a drifter. She falls in love with him. But just when things are finally looking up for Jane, a series of disasters strike. First, she becomes pregnant by the drifter, who then disappears. Second, during the complicated delivery, doctors find that Jane has both sets of sex organs, and to save her life, they are forced to surgically convert “her” to a “him.” Finally, a mysterious stranger kidnaps her baby from the delivery room.

Reeling from these disasters, rejected by society, scorned by fate, “he” becomes a drunkard and drifter. Not only has Jane lost her parents and her lover, but he has lost his only child as well. Years later, in 1970, he stumbles into a lonely bar, called Pop’s Place, and spills out his pathetic story to an elderly bartender. The sympathetic bartender offers the drifter the chance to avenge the stranger who left her pregnant and abandoned, on the condition that he join the “time travelers corps.” Both of them enter a time machine, and the bartender drops off the drifter in 1963. The drifter is strangely attracted to a young orphan woman, who subsequently becomes pregnant.

The bartender then goes forward 9 months, kidnaps the baby girl from the hospital, and drops off the baby in an orphanage back in 1945. Then the bartender drops off the thoroughly confused drifter in 1985, to enlist in the time travelers corps. The drifter eventually gets his life together, becomes a respected and elderly member of the time travelers corps, and then disguises himself as a bartender and has his most difficult mission: a date with destiny, meeting a certain drifter at Pop’s Place in 1970.

The question is: Who is Jane’s mother, father, grandfather, grand mother, son, daughter, granddaughter, and grandson? :) 


Wednesday, November 19, 2008

A new perspective to Astrology!

Astrology, by definition, is about predicting the future of a person based on the positions of some celestial objects. Pure astrologists theorize that there is a 'soul' or 'energy' that gets added to the world when you are born and the state of this energy can be deduced or predicted as a function of time. This is the limit of my knowledge about astrology and probably cognitive readers can put some light on this later. But my point does not really deal with going beyond this definition anyways.

"Do I believe in Astrology"? This is a question that I used to think about when I was a kid. What could be a possible relation between celestial bodies and human nature when looking from a scientific perspective? How do you really quantify human mood ? It was some 20 years ago, when I read upon a topic in some science magazine that my Mom brought me. It was on the activities of Pineal glands and occurance of flight accidents. Pineal glands are some glands in our body which are called 'biological clocks', since they control our temporal functions like aging, fertilization etc. These glands are set when we are born and they go through some 'activities' with some frequency. The claim in the magazine was that there is some relation between the birthday of the flight captian and the accident dates. Well, that was an interesting thought and seemed to give a possible scientific explanation (atleast for a kid), stating that you are trying to model the state of your Pineal gland (which basically controls your mood) as a function of the position of celestial bodies. I am not very sure how much is this true though!

Well, now I have grown up and have come a long way travelling through time for the past 20 years, and I had more or less rejected any other possible explanations of my astrological relations. Actually, I had stopped believing in astrology, since finding a relation between some biological activities and position of celestial objects was a bit too overwroughted and fancy from an engineering or mathematical perspective. It was a few years back, when this anecdotal event happened. I was in Hyderabad and a friend visited me. It was early in the morning and he was reading the newspaper, when I fancily asked him what was intersting about my horoscope that day! He perused through the paper and started laughing saying that I was going to have a big trouble because of a friend that day! We laughed it across and went ahead with our day! Everything was normal, when towards evening my friend started getting blisters on his face!! He was actually taking care of another friend who had chicken pox a few days earlier! Well, such prognostications happen many a times in our lives and we just passes it aside believing mere coincidence. But is there any deeper meaning to it ? Can we build a theory between astrology and the rest of the scientific knowledge ?

/* advice-- this para is hardcore technical :) you can probably skip to the next section if you don't understand them*/
Now, let me get to my point.  My idea is not to see why astrology works, but trying to figure out what could be a possible mathematical framework for it to work. Suppose we have a huge database of the birthday of people and their mood for everyday or say possibly every unit of time. Suppose we have a huge astronomical database of the positions of various celestial objects. Suppose we do a cross-correlation between these two datasets, do a dimensionality reduction and project the data onto the principal axis? More mathematically, we can confine our mood of people to whether they are happy or not; where, say happy is greater than being sad. Later doing some kind of an isotonic regression to map it as a function. Here, the principal axis of projection will provide us with the most important celestial object whose path influences our horoscopes the most. The approach is just what we use in Machine learning for prediction or regression, except that now we are trying to draw a parallel between a scientific technique and some fringe-science. Just like the case that the predictions by machine learning techniques will not give us an absolute answer, we know that horoscopes also will not (Possibly because we are not considering all the directions of variations). People have been collecting astronomical data and associating it with human nature since ages and when the dataset approaches infinity, the relationships become more and more apparant (assuming there is a relationship), and we are seeing this, anyways  (looking back at my story) :) .

Ok! So what next? Well, in this world of mass communication and internet, it might not be a big problem to collect the moods of people (let say we just use the status of Gtalk or Facebook :) especially the similies :P ) and getting the birthdays of people is even more trivial. We have an 'infinite' number of people using these programs and a company like Google can very well get this information in no time. Astronomical data is always there since ages. Now, from the above process if some useful pattern comes out then probably we can make better predictions about the mood of people! Hmm... that might be a bit scary! but atleast its nice to think it will work :) Well, it should work, since the horoscopes I belive to some extend does work and "seemingly there is some relation"!

In conclusion, the target of this blog is not to prove anything, but to hint that using Machine learning techniques could possibly put some light on saying that there is something to be believed in horoscopes, and trying to find the exact connection between human nature and celestial bodies is worth a try!! 

Wednesday, September 17, 2008

Translogical Sentimentalism!

Huh! What in heaven's sake does that mean ? Oh well, I just went about neologistic in an eccentric mood in naming a new form of literature I just thought about! Well, I am not strictly "positive definite" on whether it is a new form of literature, but probably, in the sense that I am trying to combine both my left and right brain activities in a cohesive framework, it could be! An illustration of this idea is depicted in the story here.

Another example which I just thought about is the following! Well, forgive my thoughts in random directions!

Patterns of distances: Like my friend once pointed out, humans can't really digest random events. They always try to find patterns in them and tries to "predict" what could be next! The following are the distances of myself from my home in ascending order of time which I had thought about in a rather pensive mood of homesickenss. (O(x) means "in the order of")

0 ~ O(0) - 1980 - unborn
8kms ~ O(10) - 1985 - home to school
165kms ~ O(100) - 1998 - home to college
1330kms ~ O(1000) - 2005 - home to Hyderabad (for work)
13832kms ~ O(10000)- 2007 - home to Minneapolis

Now where could I be next to satisfy the geometric progression ? Doesn't it look like a Translogical or Pseudo mathematical sentimentalism ? :P where you bring about logic and sentiments into one framework ?

Saturday, March 22, 2008

Time Travel !

A year back, during one of those coffee table chats, in the cafetaria of Microsoft, this imaginative idea of 'the third eye' popped up! Won't it be a great add-on for a human to have a mechanical camera eye, which will record everything in the world he sees! Well, there can be immensible possibilities with such an eye, but jokingly we were basically looking for an appropriate position to place that eye on the face and finally decided to put it in the un-utilized space at the center of the forehead! This later formed the basis for my cartoon "The third eye, everything is useful atleast twice" :)

Obviously, one of the main reasons I would love to have something like this would be, to see again those cherishing great moments of my life once again! Thinking about this further, what if we recorded not only the visual feedback from our eyes, but also the complete brain activity ? A kind of brain photo or whatever you call it! Its like an active snapshot of the brain activity for the duration of recording!

Elaborating it further, anologus to a video camera recording visual scenes, lets assume that we have a brain camera that records brain activies. Since recording brain activities include recording of the nerve signals from all the sensory points (that includes eyes, skin, ear, nose, tongue...). This, I will call the brain activity photo (BAP :) ). Now, if we have a mechaism of feeding the BAP back to the brain, may be in the subconscious level or as a dream (so that the actual functioning of the brain is unaffected), then wont this bring a feeling of time travel ?

How does it be equivalent to a time travel? Well, if you would formulate a Turing test for time travel, then one possibility would be to ask the testee what time he thinks he is in! And feeding the right set of BAP signals to his brain would obviously make him belive that he is actually in that particular timeframe! So I believe this is a kind of timetravel itself ! Well, there are psychological problems of belief that might come into picture here, like do you believe in the dream you see when you are seeing it or does your subconscious tell you that you are actually seeing just a dream and its not real? In that case, the model Turing test for timetravel might fail! But anyways, thats a different story!

Concluding, this is like getting a four dimensional photo of oneself, having all the three dimensional space cordinates and the fourth dimensional time cordinate. Anyways, this is just a feeling of time travel (which is actually real relative to the person) and is perfectly coherent unlike the quantum mechanical time travel paradoxes :)

Monday, January 21, 2008

What good is consciousness to machines?

The blog below is a peek at my own continuing thoughts to understand some terms and is written to evoke a discussion (in case!) or criticism!

Its been a while since I have been trying to define this term and recently my focus went over a paradigm shift. What use does it serve in defining 'consciousness'? The major reason for this thought make over was the "feeling" that the genesis of ideas are not "normally" in the realm of consciousness, but somewhere outside it. I don't know where ideas evolve, but from my own experiences, its certainly not inside consciousness. A conscious mind is just a verifier of ideas.

These thoughts lead me to another point. Mind/consciousness is just something that arises out of itself! Its hard to explain this weird self-referencing definition, but my efforts are underway! You can't define self-consciousness, since anything you use to define it is a matter of itself. Going by Godel-ian kind of explanations, you can't define it since you are inside your own consciousness when you try defining it and to define anything you need to use the materials outside itself!

Now, what are ideas? And how do they evolve? An idea, if you ask me, is a solution to a problem! In that sense, if you can find a problem, then that problem forms an antagonistic definition of an idea. When you think of solving a problem, unless you have already seen the solution and the solution is already a part of your consciousness, the computation happens elsewhere! I don't know where this happens, and thus I would call it a peripheral computation. Mind or consciousness has no control over this computation, except that it can feed in motivation, perseverance, concentration and guidance. When a solution arises out of some "random" firing of neurons, the mind acts as a verifier and checks the feasibility of the solution!

By the above argument, I am not discarding the possibility of the influence of existing knowledge in the finding of a solution. It certainly is guided, but my point is that consciousness has nothing to do with the finding of a solution or the thought process!

Now, what do I think, could be the after effect of such a conclusion? It means that self-consciousness is just an influencing parameter for a machine to solve problems, but is not a required parameter, but peripheral computation is... whatever that means ! Sometimes people call it "intuition" !!

Corollary to these thoughts:
1) If everything happens through peripheral computation and it is based on randomness, then why is the world so deterministic ? Why do people think alike in most of the situations, if everything is equally probable?

2) Randomness is just a shield over our inability to understand what lies beneath. Thus it is not random, but is something guided. In that sense, doesn't it mean that the origin of ideas is not random, which means that what I am thinking right now is not some random thought but is an after effect of a configuration which at any cost is unavoidable due to the last series of incidents! In such an extreme sense of completely mechanical self-awareness, is it required to do something special than the conventional things, in programming a machine to act like humans? Does the turing test make any sense in such a framework?

3) Though this question does not make any sense in this context, but am just curious to know the answer: "What could be the smallest computer program that is self aware"?