stack/hopr

writing between 0 and 1

The Republic

without comments

the republic

Jai Hind

This is how we start our day on our Republic Day.

This is the day our nascent nation formalized her existence. We defined our vision, our constitution, our meaning.

We celebrate every r-day with our show of strength in national capital and other such places. That feels mesmerizing as we view it. People from all sorts of armed forces marching. Displaying the latest, fastest and strongest of the military weapons. It fills the heart with pride to be part of such a great nation.

This only lasts a day. Pride does not solve problems. It does not feed the hungry. It does not correct the inefficiencies. It does not tackle terrorism. We’ve some big problems at hand. Along with our strengths, these weaknesses must be addressed on this day and until the time we come over them.

We are driven by emotion. Pride is one of them. We carry the pride of nuclear weapons, second fastest growing economy, rich culture. Forgetting the other numbers: half the population living in poverty, half illiterate as well, porous security all over the country. Emotions are temporary. These problems are not. Fortunately, some people are working on it. They need huge help: people, money, other resources on greater scale to solve these really big problems. We need to work.

When we say “Jai Hind”, that must mean commitment to the country on the whole till the time we regain our golden age.

Written by Amol

January 26th, 2010 at 11:10 am

Posted in India

Tagged with

Near Life

with 2 comments

This happened last evening.

Me and my friend, I call him LSP, were riding on a bike. He was driving. We were headed for inner parts of the city to do some emergency shopping. LSP is a smooth rider. He’s always calm and cool. I’m generally not very comfortable traveling on 2 wheelers, but since it was LSP I had agreed. Just before we started LSP and I had a brief discussion about how the pillion rider on his bike should stabilize himself. I told him that I generally I hold on to something, like bike’s tail. LSP was in disagreement. He said, since it was his bike and he was the driver, I should not hold onto anything, just rest my hands on my knees and relax. So, I relaxed.

Out from our peaceful residential area, we were entering the heavy traffic roads of the city. Fortunately the weather was nice. So, both of us were generally in a good mood. LSP was riding to his heart as usual. Overtaking vehicle after vehicle, swirling around cars and other bikes. He was in control of the vehicle. I kept my hands on my knees, without having any special urge to hold onto something.

We were approaching a traffic light. It is a confusing merge of 5 roads. Confusing, because 5 roads come together at different angles here. Three roads merge at an angle of 30-40 degrees to each other. One roads passes straight through, and another road comes in at right angle from the opposite side. The situation is complicated by a three branch flyover passing from above this junction. I had negotiated this place before, from all angles and under different traffic conditions. LSP has been driving around this place for years.

I wasn’t paying special attention to signal color or the traffic in general. It was not my job at the time. A billboard to the side of the road had caught my eye. So, I was thinking about it. I didn’t know LSP was doing some deep thinking himself. A white Ford Fiesta was right ahead of us, about 20 feet from us. We were traveling at around 40kmph. LSP was a sharp negotiator of vehicles. We were getting close to the car, both of us never noticed that the car had suddenly become stationary, its brake lights turned on. We were still traveling at 40kmph. The distance between the car and us was vanishing. We were merely 3 feet away from car, and LSP realized we were not going to make the curve around the car. He braked hard. It was too late.

We rammed into the car’s rear. A loud clicking noise of bikes front mud-guard crushing into pieces. I was already in the air, not really sure what was happening. But sure of one thing, I had zero control over my body, my mind and my life at that instant. The time seemed to have slowed to a 1000 times than normal. Mid-air, I noticed, LSP holding onto the handle of the bike, being pushed ahead onto the petrol tank. He was frantically and almost without conscious thought trying to control his bike. Then, I saw the white boot of the car. I was going to land on it. My only hope was not to land head first. Both of us were without helmets. My hand moved forward and pushed my body away from the car. My left leg scratched against the ground. I rotated around LSP, looking directly into his eyes. I saw the same feeling of helplessness and shock in his eyes. In that moment, I also tried to assess how much damage LSP was going to have and if I could help him in some way. It looked like he was about to stabilize. I kept rotating. My right hand was now stretched far along to balance my body. And I regained balance, I stood up. I was standing just behind the car and beside the point where the bike had hit the car. The bike was still engaged with the rear of the car. LSP was in upright position himself, holding onto the bike.

The rest of the world drowned out. I could only think of me, LSP, the car and the bike. The traffic, the people around us were lost. Me and LSP just stood there looking at each other, not believing what had happened. Both trying hard to get out of shock and both happy that we were unscathed. It was an unusual feeling. It was like I was 100% alive in that moment. Like Tyler Durden says after the self staged car crash in the movie Fight Club: “it was a near life experience”.

Written by Amol

October 13th, 2009 at 5:12 pm

Posted in life

Tagged with , ,

The Chase

with one comment

They say the thrill is in the chase. Have you ever thought why would it be so?

Some of my free time over the weekend allowed me to do some free thinking and I did. The chase, the pursuit or the journey often turns out to be more exciting and worth remembering than the goal, the target or the destination. There are a couple of good reasons why it might be so. It’s the way we are wired.

Have you ever been driving on the road and overtaken by another vehicle? Sometimes, you feel the urge to pursue the person who just took you over and take him/her over. May be he/she overtook you in a wrong manner, in a smaller car, it’s the gender thing or sometimes just plain will to win. It kicks in. Then, what do you do? You push yourself and the vehicle. Tracking the car that just passed you. You judge the distance, start planning how you are going to maneuver the chase and the final takedown. You get your focus on your target. Maybe break a few traffic rules in pursuit. You take some calculated and some not so calculated risks and keep pushing. Many times you get near to taking over your target, but miss. You get upset. Reform your strategy, and push further. The excitement gets to the peak as you start tailing the car you are trying to overtake. It’s sheer adrenaline rush. But, then the car ahead of you speeds further, leaving you a bit behind. You feel the letdown. The road ahead is getting narrow, in turn narrowing your chances of overtaking your target. Some insecurity sets in. You begin  to wonder if you’ll ever overtake that car. You might even question the abilities of your car. May be if you had a better maneuverable, more powerful car you could make it. Or may be you need to be a better driver. You probably fight for a second not to get depressed for wrong reasons. Some cognitive dissonance might also set in. You might try to convince yourself that the car may not be worth pursuing and you should probably just cruise along your way like nothing happened. Or what’s the point in racing at all? And just in time, road broadens into a 3 lane highway. The driver ahead of you slows down slightly for whatever reason. Leaving just enough gap to get through. Now, you’re on automatic instinctive response. Without hesitating, you slam on the accelerator and leave the car behind before a blink. You win.

The moment you overtake the person, you feel liberated, happy and like a winner. It’s momentary. Then, it’s over. You’re back to driving as routine task. The thrill os over. That’s why the thrill is in the chase.

Watching a nature oriented channel like “Discovery” or “National Geographic”, can give you some of the most natural examples of the chase. A cheetah chasing a deer at cutthroat speed. The pursuit, the deer running for its life and the pursuer, the cheetah chasing his chance at lunch after a few days of hunger. It’s pure instinct at play. And it’s not very different from the car chase. The cheetah is constantly tracking its prey, judging the distance, the direction, the terrain and even the wind speed. He wants a perfect opportunity at hitting his prey where it hurts the most. Else, he stays hungry for a while. It can be very depressing and weakening for cheetah to be hungry for days. His muscle needs the meat and he is out to get it. Cheetah doesn’t do the reasoning as much a driver would do, but he’s getting his adrenaline rush as closes in on his prey. Suddenly, the deer takes a 90 degree turn. Cheetah tries to follow, but due to his weight follows on a greater radius of turning. The distance between then increases. I can’t exactly imagine how a cheetah might contemplate giving up, but he sure does that for a while. But, his hunger keeps him going strong. He knows he can’t get far, he is losing energy. In a ditch attempt to get to his food, he gathers all his strength and takes the risk. He jumps hard and lands on the deer’s back. The deer is down. But, fighting for its life. Cheetah struggles to keep his grip tight and get onto the deer’s neck. They struggle for a while raising a small cloud of dust. That’s the fight between survival and hunger. Cheetah uses the raw power of his jaw to bring the deer down. The deer finally surrenders. The hunger wins.

And it’s over. The next task of ripping apart the deer and eating it boring and cheetah is going to do that slowly. The thrill just died with the deer.

I drove to Goa last month with a friend. Calangute beach was the destination. Most of the preps were about the journey. Getting the car serviced. Loading up the right amount of food. Planning the route. We started early morning. The initial drive was exciting. As the day passed, the drive got mundane. We drove on highways, narrower ghat sections and through small towns. Taking ample food and beverage breaks. As we were about to reach, we were lost in some dense forest. We took a wrong turn somewhere and ended up in the middle of nowhere. No vehicles passed us from either side. And it was getting dark. Some amount of worry set its foot in our minds. We started wondering about hazards of the jungle, some dangerous animals, being robbed by jungle gangs or something worse unknown. We kept going. We were running out of fuel too. If we were not to find any civilization or a petrol pump in the next 20-30km, we would make jungle our home for a while. The road was getting curvy and going into deeper jungles. It started raining soon. Blocking visibility beyond 10 feet. We did not have the luxury of fog lamps. We kept driving at very low speeds. Soon, a deadly turn grabbed my attention. It was “S” shaped, steep, dark and narrow with water flowing across the road. It was as if we were driving into the unknown. My mind was wandering too much. Bodies tired. Focusing was hard. So, with great control over mind and the car i negotiated the curve. And everything cleared. Jungle thinned. Clouds let in some light and to our pleasure, we found a small tea stall tucked beneath a giant tree of some kind.  Some locals huddled there waiting for the bus, drinking tea and chatting idly. We pulled over, jumped out of the cars and asked for hot tea, directions and current location. While gulping down the hot tea, we discovered that were not on the wrong path. It was just our fatigued minds and the jungle. Local people were humble and helpful. We chatted a while about the local culture, population, etc. And drove on. In about half an hour, we hit the beach. The journey was over.

The white-blue combo of the beach put in a peaceful state of mind. And it completely masked the thrill of the journey. I couldn’t decide which one I desired more.

Written by Amol

October 3rd, 2009 at 5:41 pm

Posted in Uncategorized

Tagged with ,

Cloud 100

without comments

You push the accelerator. Shift hard and fast. click-1-click-2-click-3-click-4-click-5. You are on the fast lane. Your eyes scan the road far and beyond. The needle blows past 100. The life begins. You mind: absolutely focused. Your breath: slow and steady. Eyes: staring straight cold. Everything clears off your thought process and you know only one thing: speed.

There’s something extraordinary about physical speed. And there’s even something better about your machine zooming past 100. I don’t exactly know what it is, but it feels good. May be it’s raw focus kicking out of instinct to survive, because a minor error would take you very very near to death. Or it’s the adrenaline rushing through your body, preparing your body for high energy action. Or it’s just the man in control of the machine.

I have always felt more alive when I cross 100. It’s stays with you for a while. It’s addictive, it’s seductive and sometimes just plain vindictive. If I have to go by the definition of nirvana(Wikipedia: “the state of being free from suffering, and the cycle of rebirth; as well as mental fetters, mental confusion”), I feel it above 100.

In those moments above 100, I feel out of this world; I’m free from the bondage to reality. All the emotion, happiness or anger; frustration or joy fades away. All the judgment only hovers around the speed. It’s liberating.

Mathematically, with every 10km that I add to my speed above 100 is approximately doubling the probability of a lethal mistake. That doesn’t stop me from adding to the speed. It just makes my focus narrower as the needle pushes more clockwise. The distractions just fall off the mind and it focuses straight ahead. This is my machine and speed assisted yoga.

At such a land speed, I’m always reminded of the following words from “the people that we love”:

speed kills coming down the mountain
speed kills coming down the street
speed kills with presence of mind and
speed kills if you know what I mean

Speed kills. That’s true. So, remember the lines above all the time you’re above 100. Because, when they call a car Porsche 911 widowmaker, they mean it.

Written by Amol

August 1st, 2009 at 9:22 am

Posted in speed

Tagged with ,

Event Horizon

with one comment

Using Windows Event Objects to Sync / Queue Parallel Tasks

Windows Event Objects is a wonderful mechanism provided to sync a lot of things happening in parallel. Particularly, in controlling a lot of things happening in parallel. Recently, I did just the same.

The scenario here is piece of code which downloads a lot of files. But, this piece of code can be deadly as it can downloads a lot of files. It has virtually no limit. You ask it to download 10 files, it will, you ask for 100, it sure will. This may be good for the client, but may choke the server to death. That’s not very good for the server. Hence, we need to limit the number of files being downloaded to some decent number.

One option, is to fire a certain number of downloads and keep waiting on them in a loop. As soon as one finishes, start the next one. Only downside: the loop. Looping continuously without sleeping can be painful for the processor. We can sleep for say, 100ms, between consecutive polls. But, this adds latency between the time that one file finishes and the next one begins. That is, there could be a maximum delay of 100ms between one file finishing and the next one starting. Latency is a waste of time.

Using event objects takes out the latency part from the above polling logic. We create events for each of the downloading files and then wait on those events. The thread waiting on an event is unblocked immediately after an event becomes signaled (an event can be signaled by a downloading thread). Thus, no latency.

The sample program, events.cpp is demonstrating such a strategy. The function “parallel_download” takes a list of files to be downloaded and and the file count. Here, we’re only simulating a download, as doing a real download is out of scope of this algorithm. The thread “download_file” downloads (simulates downloading) a file.

Pasting function “parallel_download” here. The code + comments explain the logic.

void parallel_download(int file_list[], int file_count)
{

	HANDLE event_handle_list[max_parallel_downloads];
	vector<file_data> file_data_list;
	DWORD dret;

	//create a list of files to be downloaded
	for(int i = 0; i < file_count; i++)
	{

		file_data_list.push_back(file_data(i, file_list[i]));
	}

	int download_started_count;

	bool less_than_max_files = max_parallel_downloads > file_count;
	int max_start_count = less_than_max_files ? file_count : max_parallel_downloads;		//in case file count < max parallel downloads

	//first, start download of max permissible no. of files

	for(download_started_count = 0; download_started_count < max_start_count; download_started_count++)
	{

		event_handle_list[download_started_count] = file_data_list[download_started_count].event_handle;
		_beginthread(download_file, 0, (void*)(&file_data_list[download_started_count]));
	}

	//wait for any one file to finish if not less than max no. of files permissible
	dret = WaitForMultipleObjects(max_start_count, event_handle_list, less_than_max_files, 300000);		

	if(!less_than_max_files)
	{

		bool last_wait;
		//one of the files has finished downloading, so start another one
		while(download_started_count + 1 < file_count)
		{

			download_started_count++;

			event_handle_list[dret - WAIT_OBJECT_0] = file_data_list[download_started_count].event_handle;

			_beginthread(download_file, 0, (void*)(&file_data_list[download_started_count]));

			last_wait = (download_started_count + 1) == file_count;		//if last wait, wait for all to finish, else wait for any one to finish

			dret = WaitForMultipleObjects(max_start_count, event_handle_list, last_wait, 300000);
		}
	}

	cout<<"\n all files finished\n";
}

Using the sample program

1. Three test functions are provided: test1(), test2() and test3(), call them one by one or all at once to see different sync conditions.

2. Set the const “max_parallel_downloads” to the number you want to limit the donwlodas to. (default = 5).

Note

1. The critical section variable is being used to sync output to stdout. It’s not part of the algorithm. without a critical section, the output may get all garbled in presence of multiple verbose threads.

Written by Amol

April 7th, 2009 at 2:34 pm

Posted in code

Tagged with ,