• 29 Nov 2009 /  Code, Technology

    I’ve had a couple discussions lately discussing the difference in speed between accessing data from memory and disk. In most of these conversations people were surprised to know that there is something faster than Main Memory that the programmer can take advantage of .

    First, let’s consider the memory hierarchy. If you do a google image search for memory hierarchy, you’ll see a few different (and very insightful) interpretations. In it’s simplest, it looks like this.

    A version of the memory hierachy. L0 = Level 0, L1 = Level 2, and so on. It should be noted that some high-end systems have a third SRAM Cache, which becomes L3. This causes L3 (Main Memory) to become L4 and L4 to become L5.

    A version of the memory hierarchy. L0 = Level 0, L1 = Level 2, and so on. It should be noted that some high-end systems have a third SRAM Cache, which becomes L3. This causes L3 (Main Memory) to become L4 and L4 to become L5. Also, please also keep in mine that the higher you move up the memory hierarchy, the more monetarily expensive things become (5 GB of RAM is more expensive than 5 GB of hard disk space).

    Main memory is DRAM, while Level 2 (L2) Cache is SRAM. Now, I hear you asking, “What about my SDRAM? Is that the best of both worlds?” No, it’s still DRAM. The S means something different.

    • DRAM: Dynamic Random Access Memory
    • SRAM: Static Random Access Memory
    • SDRAM: Synchronous Dynamic Random Access Memory
    • DDR SDRAM: Double Data Rate & one hell of an acronym.

    If you want to access something on your hard disk, it takes roughly 100,000 times longer than DRAM, and 1,000,000 times longer than SRAM, Making SRAM ~ 10x faster than DRAM.

    There are actually two, and in some machines even three, levels of SRAM Cache. L1 actually lives on-board the processor, while L2 (and the less common L3) live off-board. Although they’re typically all SRAM, in this post references to “SRAM” specifically mean L2 Cache.

    Now I hear you say “Awesome! I’m building this killer machine, and it’s got all the Main Memory it can handle, but I’ve still got some money to burn. Can I increase it’s SRAM too?” No. SRAM (L2 Cache) lives in a piece of additional hardware on your motherboard, you can’t really do anything to it.

    So what’s it all about then? In general, the majority of your accesses to memory will be very close, even adjacent, to your previous access. It won’t always be the case, but it probably will. Caches take advantage of this probability. If you access memory location [x], there is a high probability that you’re next memory access will be to memory location [x+1] or something similar. If you accessed an int in memory, which has a typical size of 4 bytes, then your next memory access will probably  be [x+4]. If L2 Cache is nearly 10x faster, as we know what you’re probably access next, we can speed things up by automatically moving what you’ll probably move into L2 Cache. When your program makes it’s next memory request, we check to see if it’s already in L2. If it is (cache hit) great! If it’s not, (cache miss), too bad – we goto main memory and swap that and it’s adjacent bytes, into L2 and proceed. A typical Intel Pentium chip has 128 KB – 2 MB of L2 Cache, and typically swaps data in and out of cache from main memory in 32 byte chunks.

    So how can a programmer take advantage of this? If you’re going to be accessing chunks of memory, try to access it as you know it appears spatially in memory.

    Let us consider the loop below, and assume for sake of example,

    • Length = 8
    • Any given array[i], an integer, is 4 bytes
    • L2 Cache swaps data in 16 byte chunks (as opposed to the actual 32, noted above. That’s 4 integers).
    • At the start of the loop, the L2 Cache was empty (cold cache).

    [cc lang="cpp"]
    int sumArray(int array[Length])
    {
    int sum = 0;
    for (int i = 0; i < Length; i++)
    sum += array[i];
    return sum;
    }
    [/cc]
    The below table represents cache hits and misses. White are cache hits, and red are cache misses.

    array[i] i=1 i=2 i=3 i=4 i=5 i=6 i=7 i=8
    Access Order 1 2 3 4 5 6 7 8

    This is the best that we can do. Now let us consider a 2 dimensional array, given the assumtions

    • Rows = 8
    • Columns = 4
    • C stores arrays/matrices in row-major order.
    • The rest is the same as the example above.

    [cc lang="c++"]
    int sumMatrix(int matrix[Columns][Rows])
    {
    int sum = 0;
    for (int col = 0; col < Columns; col++)
    for (int row = 0; row < Rows; row++)
    sum += matrix[col][row];
    return sum;
    }
    [/cc]

    Let’s look at those cache hits and misses. The values in the table body are the order in which matrix[col][row] was accessed.

    matrix[col][row] row=0
    row=1
    row=2
    row=3 row=4 row=5 row=6 row=7
    col = 0 1 2
    3 4 5 6 7 8
    col = 1 9 10 11 12 13 14 15 16
    col = 2 17 18 19 20 21 22 23 24
    col = 3 25 26 27 28 29 30 31 32

    Now let us consider the case where we do nothing except swap the order in which we access the matrix elements, and inspect the cache hit/miss table.
    [cc lang="c++"]
    int sumMatrix(int matrix[Columns][Rows])
    {
    int sum = 0;
    for (int row = 0; row < Rows; row++)
    for (int col = 0; col < Columns; row++)
    sum += matrix[col][row];
    return sum;
    }
    [/cc]

    matrix[col][row] row=0 row=1 row=2 row=3 row=4 row=5 row=6 row=7
    col=0 1 2 3 4 5 6 7 8
    col=1 9 10 11 12 13 14 15 16
    col=2 17 18 19 20 21 22 23 24
    col=3 25 26 27 28 29 30 31 32

    Every access is a cache miss! Meaning that we have to goto main memory for every access of the matrix. This will be approximately 9 times slower than if we hadn’t swapped the row/column order. This is because data is not actually stored as a two dimensional matrix in main memory – everything is a linear array, with row and column offsets being calculated. Because some data resides right next to other data in main memory, it will also appear this way when 32 bytes are moved into the cache. If you access them in the same order in which they’re stored in main memory, you can take advantage of the cache. If you don’t, you end up forcing cache two swap stuff in and out from main memory.

    John Carmack was once quoted as saying “Low level programming is good for the programmer’s soul”.

  • 25 Nov 2009 /  Life

    “Oh, I’m bad at math, haha“.

    I can’t count the number of times that I’ve heard this, and I’m sure you can’t either. I hear this all the time. I say I’m bad at math, but that’s because I had my [explative] handed to me on a plate in equations (which, as my calculus II professor put it, are “a whole new world of pain”). But when I hear a lot of people say that they’re bad at math, what they mean, is they can’t figure out 50% of 150. But that’s okay, cause they’re bad at math, haha.

    What I’d like to particularly note is that people usually laugh after saying they’re bad at math. In fact, it’s “cool” to suck at math. “Oh, that math crap is hard and is for nerds and losers. I hate it, haha” I hear too often. I can appreciate a certain aversion to academics (yes, there have been times when I have cursed class, any class). However,  math, especially at the algebra level, is a basic enough skill that everyone should have at least a basic understanding and ability. Furthermore, it should not be “cool” to hate it.

    My discrete math professor said something that I’ll never forget:

    “Why is it that people think it’s cool or funny to suck at math? That’s like saying ‘Oh, I can’t read, I suck at reading, haha.’ People are ashamed to say they can’t read.”

    The catalyst to this post was something that I saw over on Wil Wheaton’s blog today in which he indicated that he’s not as good at math as he used to be. Look at point 5 in his post, which expands upon

    “I mean, I’m a pretty smart guy, and there are times when I have to write down a math problem that I used to be able to do in my head, and some of the spelling and grammar errors I make are just embarrassing – and I’m a writer! So my idea is for an 8 week class that meets once or twice a week for a couple hours, that would be a mental tune up for guys like me.”

    This sounds like a fantastic idea, one which I’d probably attend. I love the idea that he wants to be better at math! In fact, I think that there might be something more here. There are all sorts of interest groups, organizations, and events that exist on a national level: hackerspaces, FabLab, MakerFair, ACM, IEEE, etc… Why can’t there be some sort of organization or group that offers adult oriented refresher course? I’d be more than happy to brainstorm with anyone who’d like to with me.

    Tags: ,

  • 20 Nov 2009 /  Life, Order vs Chaos

    The Signature.

    Yup, that’s it. It’s the worst security implement that mankind has ever invented. Worse than the secret handshake, worse than the ‘mother’s maiden name’, and worse than the easily picked old-school locks.

    I’ve always thought this – but what finally pushed me over the edge was when I was at the bank today. I deposited one check. I put the check amount on the deposit slip, but forgot to put it in the ‘total’ at the bottom. So the teller filled it in and handed the slip back to me to initial. I thought to myself “initialing here does absolutely jack squat”. I obliged, of course. After all, I do want the money to find its way into my account.

    But let’s go back to the idea in general. In technology, consumers want “perfect” security. They want a computer that’s “hacker proof, virus proof, and worm proof”. I know people who won’t purchase anything on sites like Amazon.com for fear of identity theft -”you never know who’s going to take your credit card number, boy… once it’s out there…it’s out there”. Yet, these same people use their credit card at a grocery store and are comfortable with the notion that their signature guarantees it was was a legitimate purchase. What’s even worse than real signatures are the signatures on those digital pads – they all look like absolute chicken scratch. What good does that really do, aside from meet some legal demand somewhere?

    Yes, I’ve heard “well, ‘they’ can do handwriting analysis to see if it was really you”. I’m sure ‘they’ can, but how often will things go that far? What if it was a rubber-stamped or digital signature? What if it was legitimately me, but I had [accidentally] slammed my hand in the car door before coming into the store?

    But signatures are not limited to bank deposit slips and credit card purchases. We use them every where, in all sorts of really important documents. How, in our modern world, is this still acceptable? In some cases, we need something better. In other cases – can we just forgo the whole thing all together?

    Tags: ,

  • 15 Nov 2009 /  Fun, Order vs Chaos

    I’ve always been a fan of Peter Gabriel’s “The Tower That Ate People”. I’m also a fan of dystopian-orwellian stories. While stumbling around on youtube I came accross this mashup of the 1927 silent film Metropolis. I’m pretty excited. I gotta go find a copy of this movie to watch now!

    Tags: ,

  • 09 Nov 2009 /  Fun

    I just created some web style buttons that I can use on my site, but I’m also offering them for anyone who’d like to use them. Please just don’t hotlink to my server, that’s all I really ask.

    There will hopefully be more forthcoming as well. Ack, I didn’t notice until I’d posted them here – I didn’t make their borders transparent (my site is white). Meh, it’s 3am. I’ll do it in the morning.
    They’re transparent now.

    There are also larger versions available. http://davidwkennedy.com/webbuttons.htm

    Have Fun!

    Update: Created some more today

    Tags: , , , , , , ,

  • 08 Nov 2009 /  Code

    Often times you may want to get the floor of a specific date. For example, you may want to see everything that has happened on your system since the beginning of today, or perhaps between the start of yesterday and the start of today. For simple queries, this is usually done by entering in a datetime manually. For example
    [cc lang="tsql"]
    SELECT * FROM fooTable WHERE [createdate] >= ‘2009-11-06′ AND [createdate] < '2009-11-07'
    [/cc]
    However, it's often the case that punching in manual values like this is just not practical. It'd be nice if you could just give it any date and it would find the floor of the day.

    You can do this fairly easily by taking advantage of T-SQL's DATEADD() and DATEDIFF() functions. To find the floor of a given datetime, do something similar to
    [cc lang="tsql"]
    SELECT DATEADD(dd, DATEDIFF(dd, 0, GETDATE()),0)
    [/cc]
    This will give you the floor of today. Replace GETDATE() with any datetime, including a variable, to get the floor of that day. This will return something of the form '2009-11-07 00:00:00' Additionally, replace dd with hh or mi for the floor of the hour or minute.

    Based on the code above, I use the function below to simplify getting the floors of various time periods.
    [cc lang="tsql"]
    CREATE FUNCTION fn_gettimefloor
    (
    @format VARCHAR(16),
    @datetime DATETIME
    )
    RETURNS DATETIME
    AS
    BEGIN
    IF @format in ('yy','yyyy','year')
    RETURN DATEADD(yy, DATEDIFF(yy, 0, @datetime),0)
    ELSE IF @format in ('qq','q', 'quarter')
    RETURN DATEADD(qq, DATEDIFF(qq, 0, @datetime),0)
    ELSE IF @format in ('mm','m','month')
    RETURN DATEADD(mm, DATEDIFF(mm, 0, @datetime),0)
    ELSE IF @format in ('dd','d','day')
    RETURN DATEADD(dd, DATEDIFF(dd, 0, @datetime),0)
    ELSE IF @format in ('wk','ww','week')
    RETURN DATEADD(wk, DATEDIFF(wk, 0, @datetime),0)
    ELSE IF @format in ('hh','hour')
    RETURN DATEADD(hh, DATEDIFF(hh, 0, @datetime),0)
    ELSE IF @format in ('mi', 'n','minute')
    RETURN DATEADD(mi, DATEDIFF(mi, 0, @datetime),0)
    ELSE IF @format in ('ss','s','second')
    RETURN DATEADD(ss, DATEDIFF(ss, 0, @datetime),0)
    ELSE IF @format in ('ms', 'millisecond')
    RETURN DATEADD(ms, DATEDIFF(ms, 0, @datetime),0)
    ELSE IF @format in ('mcs','microsecond')
    RETURN DATEADD(mcs, DATEDIFF(mcs, 0, @datetime),0)
    ELSE IF @format in ('ns','nanosecond')
    RETURN DATEADD(ns, DATEDIFF(ns, 0, @datetime),0)
    RETURN GETDATE()
    END
    GO
    [/cc]
    The above function can then be used in a way similar to the below example
    [cc lang="tsql"]
    select dbo.fn_gettimefloor('dd',GETDATE()) -- The floor of today
    select dbo.fn_gettimefloor('hh',@someDateVar) -- The floor of a datetime passed into the function
    select dbo.fn_gettimefloor('hh',DATEADD(hh, -5, GETDATE())) -- Get hour-floor of 5 hours ago
    [/cc]
    However, sometimes this isn't even enough. Let's say, for example, we want to break things up into 10 minute intervals. Get can further nest DATEADD()s and DATEDIFF()s. The below example is for the floor of every 10 minutes, but it is extendable to other date-types (days, hours, months, etc) and other time-intervals (10, 20, 3, etc).
    [cc lang="tsql"]
    select DATEADD(mi,DATEDIFF(mi,0,dateadd(mi,datediff(mi,0,getdate())/10*10,0)),0)
    [/cc]
    What's happening in the above code is, from the inside out, we

    1. Get today’s full datetime
    2. Get the difference between step 1 and zero (in minutes), which is an integer.
    3. We divide step 2 by 10 and then multiply that by 10, effectively flooring the value to the nearest number evenly divisible by 10.
    4. We add the integer from step 3 to the start of eternity (dateadd’ing to zero), and we’ve got the floor of the current 10 minute interval.
    5. After step 4, we have a datetime value, and we’re left with something like SELECT DATEADD(dd, DATEDIFF(dd, 0, <step 4 datetime>),0).

    So we’ve got a method to get the floor of a time period, a function that abstracts the mess into something easily used, and then a method to get the floor of a time-increment. I’ll leave it up to the ambitious reader to get the last part, the 10 minute example, into a function as well. :-)

    Tags: , , ,

  • 07 Nov 2009 /  Fun

    Earlier today, Kennedy was misspelled on The Guild’s page for Tinkerballa. No, I’m not somehow involved with Tinkerballa – the mention of Kennedy was no in reference to me. It was to Amy’s work with Jamie Kennedy, “Kicking it Old School”.

    I like to add some sort of pics to most posts – just cause pictures are good. I was going to post a screen shot of the misspelling (I know, lame, I just wanted caress my ego and see my own name – even if it is misspelled).

    However, theguild.com responded to my tweet announcing the misspelling, and correct the typo, before I could get a screen shot. The Guild’s awesome like.

    Tags: ,

  • 05 Nov 2009 /  Maine

    So, Maine’s been a trending topic on twitter for most of the day, due to the law allowing gay marriage being repealed (53% to 47%). Well, that, medical marijuana made some steps forward. But it was mostly the gay thing. I’d like to take a moment to discuss my home state.

    It was stated in a New York Times article “The Maine vote was particularly discouraging for gay-rights groups because it took place in New England, the region that has been the most open to same-sex marriage”. So, the first off, Maine is not like the rest of the New England states. Yes, it’s cold, yes we have snow, and lobster, and a funny accent. But no, we’re not urban, and our remocrats are not your typical New England democrats. It’s not the same New England that you meet when you walk out of Logan Int’l.

    After leaving Maine, I found that quite a few people think that Maine is urban, because that’s what the rest of the north east is. In the north east, you’ve got New York, Boston, Newark, and even Philly, Baltimore and DC (yeah, they’re not New England per se, but on a photo of the US at night, they’re part of the same light-mass). Maine has no major city. The entire state has roughly the same population as San Diego (city proper, not metro). Maine has trees, lots of them, and little else. While Connecticut, Massachusetts, and Rhode Island are mostly urban or suburban, Maine is the Alaska of the east coast.

    DSC04399

    Up North, on the way to Limestone, Maine.

    Maine’s politics are also quite different. As a gross oversimplification, democrats generally love trees, hate guns, and live in urban areas. The Maine’s population is mostly liberal, but they hate trees, love guns, and don’t live in an urban area.

    When I say hate trees, I simply mean, they’re not “tree huggeres”.  Yes, they care about the environment, like anyone, but they’re not too worried about it. To anyone who lives in Maine who cares to dispute that, I say come visit California – the land of hybrid cars, use your own cloth shopping bags, recycling more than you throw away, and people who fear that trees are becoming endangered.

    Despite Maine’s democrat majority, both of it’s senators (Susan Collins and Olympia Snowe) are both republicans. Snowe, in particular, commands quite a bit of influence in the senate. Although  a republican, she tends to be a more liberal one, as she supports legalized abortion, gay rights, and even voted for last month’s Finance Committee’s health care bill.

    Generally when people think “republican” or “small town in the sticks”, they think of the other. The two notions are loosely connected in the American mentality. This is not the case in Maine; you’ve got a bunch of small-town folks, who love their guns and country music, and also the Democratic Party.

    Despite having grown up there, I personally find it very difficult to anticipate how the state will stand on any given issue.

    Owl's Head Light, Rockland Harbor

    Owl's Head Light, Rockland Harbor

    The shorline a few feet from the house I grew up in

    The shorline a few feet from the house I grew up in

    Lobster Fishing

    Lobster Fishing

    Eastern Harbor Landing, South Addison, also near where I grew up.

    Eastern Harbor Landing, South Addison, also near where I grew up.

    Tags: , , ,

  • 04 Nov 2009 /  Fun

    So, I like Databases. I also like things that zoom and go fast. Best of both worlds: BMW Oracle Racing.

    Oracle’s teamed up with BMW to build this awesome boat, currently undergoing sea trials off the coast of San Diego.

    Sea Trials off San Diego

    BWM Oracle Racing 90

    She’s a 90 foot trimaran, capable of 2-2.5 times true wind speed (although some rumors have stated more).  NBC San Diego, in their report of the 3 November 09 broken mast, claimed that it is “The fastest sailing boat ever conceived”.

    Returning to San Diego

    Returning to San Diego

    Tags: , , , ,

  • 01 Nov 2009 /  Fun
    Please ignore the little star of light that makes this look like penguin-pumpkin-porn.<br/> I promise that was coincidental.

    Please ignore the strategically located little star of light that makes this look like penguin-pumpkin-porn. I promise that was coincidental. Otherwise, Happy Halloween!

    Tags: , , ,