Category Archives: Technology

Getting the most data speed out of your cell phone

You may have noticed there have been very few posts here. There’s a reason for that. The first and foremost is that sending my rants in to the void has not been as personally cathartic as I’d hoped. My other goal for the blog, which actually has been somewhat successful, was to simply provide a vehicle for putting information out on the web that I thought might be useful for people, and that I couldn’t find elsewhere. Based on the traffic stats, those posts have actually been worthwhile, and my only reason for not doing more of this kind of post has been that I’ve been too busy playing with my son, finishing up my projects at MIT, and trying to get a job (in that order).

So, going forward, I’m just going to focus on the second category of posts (though I reserve the right to devolve to the first occasionally). This blog was getting too negative, anyway. In that spirit, here’s a particularly useful trick I just figured out while sitting in a coffee shop working remotely.

I recently gave up my nice window office since I was feeling guilty about taking up a nice spot but only working part time. So, I’ve been doing a lot of work remotely, usually from a coffee shop given that working at home just isn’t very productive when there’s an adorable toddler running around begging to be hugged. So, I splurged and decided to start paying the extra $20 a month to use my phone as an internet connection for my computer. This is becoming a pretty common thing, and Sprint even offers phones that will create a WiFi network on the fly (I use Bluetooth with my iPhone). I expect this will become even more common once the iPhone hits Verizon, as Apple will reportedly allow this version of their phone to create WiFi hotspots, too.

I would typically just leave my phone laying flat on the table next to my laptop. However, giving it a minute of thought, this is actually pretty dumb, for two reasons. First, having the phone so close to the laptop is probably not smart, as computers are notorious spewers of electromagnetic interference at pretty much every frequency imaginable. In theory, they should be shielded, but nothing is perfect and between the memory data rates and the processor clock speeds, a computer pretty much has the cell phone spectrum covered directly, if not with overtones. So, keep the cell phone away form the computer at least a foot or so.

Most importantly, however, leaving the cell phone flat on a table is a bad idea because it puts the antenna horizontal, whereas cell phone signals are polarized vertically. (What this means, if you’re not a fan of electromagnetics, is that the electrons in the cell phone tower antenna are being shaken up and down, not side-to-side. Radio waves are really just a way of keeping track of how electrons interact with each other. Without anything interfering, the electrons in your cell phone’s antenna will be wiggled in the same orientation and frequency as those in the cell tower antenna. However, antennas are designed for their electrons to be wiggled in a certain direction (it’s almost always along the long axis of the antenna) and a cell phone’s antenna is oriented with the assumption that the user is holding it upright against their ear.) Once I realized this, I put my phone up against a nearby wall so that it was standing straight up and down (as if somebody were holding it) and my data rates nearly doubled.

So, if you’re using your cell phone as an internet connection, keep it a bit away from the computer and prop it up so it’s vertical. Keeping it vertical in your pocket probably isn’t a great idea, since your body is pretty good at blocking radio. If you find this helps, please let me know in the comments. Right now my experience alone isn’t very statistically significant, to say the least.

Google saves the world five seconds at a time

Google just announced new technology that will cause search results to be predictively presented to the user as they type each letter of their search, saving the user from having to type their entire search, or hit enter after each search. According to Google’s Vice President, this will save the user an average of “two to five seconds…” from each search. This is what they have been working on as their next generation of search. It’s impressive, to be sure.

But does it strike anyone else as odd that we, as a society, are putting our technological efforts and resources into developing technologies that save us seconds each day when we can’t, as a society, get it together to develop infrastructure and transportation technology that will save us hours from commuting? It seems perverse that most Google engineers probably sit in California traffic for over an hour each day so that they can come into work and make sure nobody has to wait more than 15 seconds for a search result.

Moving…

In the coming months, I’m going to be leaving MIT. I’m not sure how long my access to my computer account will last, so I’m starting the process of moving everything over. For those of you that access this blog via an RSS feed, nothing will change. However, if you have this blog bookmarked, please change the bookmark to point to www.jonbirge.net. For now, that will still put you here. In the future, who knows?

Interesting article on high frequency trading patterns

The Atlantic recently carried a fascinating article on the odd patterns in stock market data caused by mysterious high frequency trading algorithms which post and remove bids and asks on stocks with no intention of ever buying the stock in question. My best personal theory is that these mysterious bids and asks are simply ways of “pinging” the market electronic infrastructure for information about the speed and timing of the hardware running it. High frequency trading has become so fast that firms do their best to but their computers as close as possible to the exchange’s servers.

My guess is these bid patterns are just acting as clocks. To quote one of the experts in the article, the speed of light actually starts to matter. Given that the timing of trades can mean millions in profits for some of these firms, it stands to reason they would try to keep close tabs on the speed of their connection to the exchanges. By sending in a sequence of false bids (bids so far away from the current price and retracted so quickly they will never result in a buy) they can safely figure out how long it takes an order to go from their computer to the exchange’s computer. They use a sequence (ramp) of increases prices simply to keep track of all the different orders and their timing. (If they used the same price repeatedly, they’d have no way of knowing which order was which.)

My next best theory is that they are probing the HFT algorithms of other firms, trying to either reverse engineer other trading programs (which would give them a decided advantage) or search for some way to induce other programs to act in a way that can be exploited, like a big robot battle played with real money. They are trying to see if they can predict what others’ programs will do in a way that doesn’t require them risking any money. Frankly, I don’t see this as too likely, given that it would be very poor programming for anybody to let their HFT algorithm respond to ludicrous bids.

A final, rather crazy possibility, is that these false bids are a way to communicate between market participants over public, non-traceable channels. If encrypted information were embedded in the timing or changes of the bids, it would be a good way for firms to collude in a way that would be nearly impossible to trace: everybody is allowed to post bids, and every is allowed to read them. Unless you knew how to decode the information encoded, you’d have no way of proving information was being exchanged between parties. And what better avenue of communication between financial firms than the stock market’s public quote system?

One thing that is certain is that this frenetic activity, with hundreds of quotes a second being generated and cancelled, is clearly meant for the “benefit” of other computers, either at the same firm or another. It’s a fascinating phenomenon. I’m not sure what it portends for the markets, however. As people leave the market in droves (as evidenced by 13 straight months of mutual fund outflows) I wonder what will happen when the only people left aren’t people.

If one were into conspiracies…

Our country is running massive deficits, financed by foreign government purchases of Treasury instruments. It’s not clear how long we can keep finding buyers for our debt while paying virtually zero interest. This is especially so when the stock market is going up, presenting an attractive (at least in theory) return compared to government bonds. In a rising stock market, people are less apt to head for the safety of bonds. So, what’s a government to do? Devalue the stock market. Now that the stock market has gone up for long enough such that companies like Goldman Sachs and Bank of America have been able to recapitalize themselves via trading profits, now might be a good time to crash the market back down and drive up demand for government bonds. A great way to do that would be for somebody in the administration to propose legislation that would ban stock market investments by some of the largest investors in the stock market: commercial banks.

I’m not saying this is one of the intents behind the Volker rule, but it does work out quite well. I’ll also point out that the Volker rule imposes even more restrictions on bank trading than Glass-Steagall ever had. All in all, a very nice way to keep the stock market form overheating without having to raise interest rates, a perfect bit of finesse whereby borrowing costs stay down for the government without having to tighten credit and potentially derail whatever recovery we’ve got going here. While I’m obviously joking about a conspiracy theory, I do believe it’s true that a stock market drop may be necessary to containing borrowing costs for the government. Thus, they may not be so quick to try to take efforts to prop it up.

Twitter… for men

With recent advent of Woofer, the Twitter clone that requires the use of 1400 characters, I’d like to add my own entry into the foray.

Introducing Twitless: Twitter for Men. “140 Characters. A month.”

You can use your 140 characters any way you want, but if you use ’em up early, you’ll just have to wait until next month, chatty Kathy.

Twitless. Because real men only talk when they have something to say.

Accelerating code using GCC’s prefetch extension

I recently started playing with GCC’s prefetch builtin, which allows the programmer to explicitly tell the processor to load given memory locations in cache. You can optionally inform the compiler of the locality of the data (i.e. how much priority the CPU should give to keep that piece of data around for later use) as well as whether or not the memory location will be written to. Remarkably, the extension is very straighforward to use (if not to use correctly) and simply requires calling the __builtin_prefetch function with a pointer to the memory location to be loaded.

It turns out that in certain situations, tremendous speed-ups of several factors can be obtained with this facility. In fact, I’m amazed that I haven’t read more about this. In particular, when memory is being loaded “out of sequence” in a memory bandwidth-constrained loop, you can often benefit a great deal from explicit prefetch instructions. For example, I am currently working on a program which has has two inners loops in sequence. First, an array is traversed one way, and then it is traversed in reverse. The details of why this is done aren’t important (it’s an optical transfer matrix computation, if you’re interested) but the salient aspect of the code is that the computation at each iteration is not that great, and so memory bandwidth is the main issue. Here is the relevent section of code where the arrays are accessed in reverse:

/*
* Step backward through structure, calculating reverse matrices.
*/
for (dx = n-1; dx > 0; dx--)
{
Trev1[dx] = Trev1[dx+1]*Tlay1[dx] + Trev2[dx+1]*conj(Tlay2[dx]);
Trev2[dx] = Trev1[dx+1]*Tlay2[dx] + Trev2[dx+1]*conj(Tlay1[dx]);
dTrev1[dx] = dTrev1[dx+1]*Tlay1[dx] + dTrev2[dx+1]*conj(Tlay2[dx]) +
Trev1[dx+1]*dTlay1[dx] + Trev2[dx+1]*conj(dTlay2[dx]);
dTrev2[dx] = dTrev1[dx+1]*Tlay2[dx] + dTrev2[dx+1]*conj(Tlay1[dx]) +
Trev1[dx+1]*dTlay2[dx] + Trev2[dx+1]*conj(dTlay1[dx]);
}

Despite having exactly same number of operations in the forward and reverse loops, it turns out that the vast majority of time was being spend in this second (reverse) loop!

Why? Well, I can’t be entirely certain, but I assume that when memory is accessed, the chip loads not just the single floating point double being requested, but an entire cache line starting at that address. Thus, the data for the next couple of iterations is always loaded into L1 cache ahead of time when you’re iterating forward in address space. However, in the reverse loop, the chip isn’t smart enough to notice that I’m going backwards (nor should it be) and so it has to wait for the data to come from either L2 or main memory every single iteration. By adding a few simple prefetch statements to the second loop, however, the time spent in this section of code went way down. Here is the new code for the second loop:

/*
* Step backward through structure, calculating reverse matrices.
*/
for (dx = n-1; dx > 0; dx--)
{
Trev1[dx] = Trev1[dx+1]*Tlay1[dx] + Trev2[dx+1]*conj(Tlay2[dx]);
Trev2[dx] = Trev1[dx+1]*Tlay2[dx] + Trev2[dx+1]*conj(Tlay1[dx]);
__builtin_prefetch(Trev1+dx-1,1);
__builtin_prefetch(Trev2+dx-1,1);
__builtin_prefetch(Tlay1+dx-1);
__builtin_prefetch(Tlay2+dx-1);
dTrev1[dx] = dTrev1[dx+1]*Tlay1[dx] + dTrev2[dx+1]*conj(Tlay2[dx]) +
Trev1[dx+1]*dTlay1[dx] + Trev2[dx+1]*conj(dTlay2[dx]);
dTrev2[dx] = dTrev1[dx+1]*Tlay2[dx] + dTrev2[dx+1]*conj(Tlay1[dx]) +
Trev1[dx+1]*dTlay2[dx] + Trev2[dx+1]*conj(dTlay1[dx]);
}

The prefetch instructions tell the processor to request the next loop’s data, so that the data is making its way through the memory bus while the current computation is being done in parallel. In this case, this section of code ran over three times as fast with the prefetch instructions! About the easiest optimization you’ll ever make. (The second argument given to the prefetch instruction indicates that the memory in question will be written to.)

When playing around with prefetch, you just have to experiment with how much to fetch and how far in advance you need to issue the fetch. Too far in advance and you increase overhead and run the risk of having the data drop out of cache before you need it (L1 cache is very small). Too late and the data won’t have arrived on the bus by the time you need it.

Why did I not prefetch the dTrev1 and dTrev2 memory locations? Well, I tried and it didn’t help. I really have no idea why. Maybe I exceeded the memory bandwidth, and so there was no point in loading it in. I then tried loading it in even earlier (two loops ahead) and that didn’t help. Perhaps in that case the cache got overloaded. Who knows? Cache optimization is a black art. But when it works, the payoff can be significant. It’s a technique that is worth exploring whenever you are accessing memory in a loop, especially out of order.