Better HN
topnewbestaskshowjobs
Log inSign up

throwaway_26758

-1 karma · joined Dec 11, 2016 (9y ago) · 4 submissions

Submissions

comment 9y ago · link · parent

I was going for a low-hanging fruit example without resorting to bit-twiddling which might confuse many readers, but since you insist I'll repeat my point that going for plain C is great. And that compilers, if properly guided, can optimize better than humans. Following code generates essentially the same code as the GCC builtin intrinsic.

  uint64_t reverse_actually_fast_c(const uint64_t n, const uint64_t k) {
          uint64_t r = 0;
          r = ((n & 0xFF00000000000000) >> 56) |
              ((n & 0x00FF000000000000) >> 40) |
              ((n & 0x0000FF0000000000) >> 24) |
              ((n & 0x000000FF00000000) >>  8) |
              ((n & 0x00000000FF000000) <<  8) |
              ((n & 0x0000000000FF0000) << 24) |
              ((n & 0x000000000000FF00) << 40) |
              ((n & 0x00000000000000FF) << 56);
          r = (r & 0xF0F0F0F0F0F0F0F0) >> 4 | (r & 0x0F0F0F0F0F0F0F0F) << 4;
          r = (r & 0xCCCCCCCCCCCCCCCC) >> 2 | (r & 0x3333333333333333) << 2;
          r = (r & 0xAAAAAAAAAAAAAAAA) >> 1 | (r & 0x5555555555555555) << 1;
          return r >> (64 - k);
      }

comment 9y ago · link · parent

> I find this particular argument unconvincing, because those aren't real yet.

While the 'ideal' of writing any half-attempted but correct specification, then superoptimizing to the absolute minimal instruction count is unfeasible today, I counter that the attitude that these basic technologies are fantasy is the AI effect in play here. Compilers routinely employ techniques that were considered the holy-grail of program synthesis and superoptimization several years ago, but due to the moving target mindset, we've discounted how capable these suites are becoming.

One of the major issues is actually a PEBCAK: programmers don't realize what the standard actually affords.

What is truly undefined behavior in your code that will give the go-ahead to a compiler to fully work around what you wrote?

Or how about the way you wrote that loop creates a chain of memory dependencies that artificially constrains how the compiler is able to rearrange memory accesses and vectorize.

And especially important here is optimization coupled with testing. You wrote your algorithm with while loops and no explicit stopping condition, so even though you could transform the code into a properly bounded for loop and profit, now the optimizer and bounded model checker can't unroll your loop in certainty for checking if a different implementation is actually perfectly functionally identical. Now the compiler can't actually make the optimizing transforms it wishes it could because you failed at your job. And on top of that, you can't refactor while having the machine verify that nothing functionally changed -- a potential regression nightmare.

I'm not advocating we forget about how to optimize our code, in fact it's quite the opposite. Human programmers need to fully understand what assumptions and guarantees are built into a language in order to be proficient at giving a specification where optimization automation is possible.

Sticking to standard C presents a rather unchanging simple 'virtual machine' model (terminology abused there), rather than deal with the constant flux of small hardware architecture changes that chaotically renders your past wisdom obsolete.

comment 9y ago · link · parent

Thanks for bringing up specialized optimization tools such as Halide. Recently there's been a proliferation of domain-specific languages targeting this automatic optimization-from-intentions paradigm. While it's great for testing new ideas isolated from the complexity of modern day compiling suites (academic excursions), actually devoting your codebase to these tools is a huge burden for future maintainability, and eliminates the possibility of future improvements provided outside the small development effort of a single academic group.

Many of the techniques given here (and elsewhere in the glut of DSLs) are quietly folded into mainstream C compilers within a short timeframe -- first as requiring explicit hints just like the DSLs, then secondly simplified to allow the compiler to automatically infer hints from well-structured code relying on transformation guarantees of standard C idioms.

So if you stick with strict C and come to understand the implicit assumptions/guarantees the standard dictates while conveying structure in your code, in time the compiler suites will march forward in improvements that come with each update 'for free.'

And most importantly, update your testing infrastructure to be formally verified each refactoring (such as with CBMC) so that there's no regression problems caused by compiler updates with free reign!

comment 9y ago · link · parent

I always cringe when developers compare compiler-optimized C to 'hand rolled' assembly. In the dawning age of SAT-based program synthesis and vectorization-aware superoptimizers, it's extremely anachronistic. It only shows a lack of awareness that in reality your job as a programmer is not to give instructions to a machine, but to provide enough help that a machine can create its own instructions better than you can.

For example, let's take one critical function here: bit reversal. The C code provided (renamed variables so it's easy to consistently compare) has an impressive speedup just going from -O1 to higher optimization levels. And then, on top of that speedup with a decent optimizer, we can again halve the amount of time spent in this function by providing better intentions to the compiler which allow it to perform automatic vectorization. Note this is all in standard C, and makes the code simpler to understand (and maintain) by humans as well.

    uint64_t reverse_slow(uint64_t n, uint64_t k)
    {
            uint64_t r, i;
            for (r = 0, i = 0; i < k; ++i, n >>= 1)
                    r = (r << 1) | (n & 1);
            return r;
    }

    uint64_t reverse_fast(const uint64_t n, const uint64_t k)
    {
            uint64_t r, i;
            for (r = 0, i = 0; i < k; ++i)
                    r |= ((n >> i) & 1) << (k - i - 1);
            return r;
    }
Lesson: do your job as a human and provide guidance, let the machines do their job at taking your advice and running wild with creativity. It helps you both become more effective.