buy Synthroid online consultation us Female Cialis online order saturday delivery buy Augmentin without prescription buy Cephalexin without prescription to ship overnight buying Ampicillin without a prescription purchase Clomid without prescription purchase Retin-A Gel 0.1% online without prescription purchase online Levaquin without rx purchase online prescription Flomax purchase Tretinoin Cream 0.05% online with overnight delivery Lisinopril without a presciption best Hydrochlorothiazide online pill Phenteramine pharmacy No prescripton Plavix Hydrochlorothiazide with free fedex overnight ordering Cephalexin over the counter online pharmacies no prescription Robaxin Buspar non perscription Baclofen with free fedex overnight Norvasc overnight no consult Buy Accutane online without a perscription cheap Lexapro for sale online no prescription required how to order Clomid online without a rx order Trazodone amex online without prescription Female Cialis deliver to uk fed ex overnight Tetracycline order online no membership overnight Doxycycline overnight online order prescription free Strattera Wellbutrin SR no dr contact cheap Cephalexin without rx Valtrex overnight no consult order Zyprexa pharmacy Flomax without a perscription shipped overnight express Atarax no s consult purchase Cephalexin paypal without prescription Buying Alesse online without a perscription purchase online rx Norvasc without purchase Zyprexa no visa without prescription purchase online Diflucan without rx purchase Tamoxifen money purchase Clomid without prescription mexico Order Viagra Plus online purchase online rx Tretinoin Cream 0.05% without cheap purchase Valtrex purchase Lexapro online without prescription Trazodone without prescriptions buy Fluoxetine without rx from us pharmacy Colchicine without prescription medications buy Nolvadex no visa without rx Amoxil with free fedex overnight Generic Female Cialis no prescription buy Hydrochlorothiazide without a rx overnight shipping Propranolol online no rx overnight Norvasc no doctors prescription Diflucan with no perscription and delivered over night online ordering Medrol Cheap Augmentin no script order Amoxil without prescription to ship overnight Online Motilium no perscription how to order Buspar online without prescription purchase Tadacip without rx to ship overnight order Xenical for cash on delivery purchase Tetracycline without prescription to ship overnight Zithromax buy fedex how to buy Lexapro online without rx order Alesse no prescription Buy Abilify without prescription Synthroid 40 mg online Buy perscription Trazodone online Xenical online consultant buy Valtrex amex online without prescription Order Revatio without a perscription from us purchase cheap online Elavil Zoloft without presciption buy Fluoxetine overnight delivery how to order Medrol online without a prescription Strattera online with next day shipping cheapest Aciphex available online best Cytotec online pill Seroquel no prior script Cephalexin with free dr consultation Buy Cheap generic Levaquin Buy Zovirax cost Buying Tadacip online without a perscription buy Abilify without Online consultation for Diflucan purchase rx Clavamox without Online pharmacies no perscription Lisinopril Elavil without dr order Glucophage online with overnight delivery purchase Wellbutrin SR cod next day delivery No script Furosemide Nizagara cod next day delivery where to purchase generic Nizagara online without a prescription Nizagara without presciption purchase Nizagara without a rx online purchase Nizagara amex online without rx Buspar online uk Where to buy generic Celebrex online without a perscription buy Baclofen without Female Cialis with free fedex overnight buy Female Cialis without a prescription overnight delivery Female Cialis fedex Female Cialis with free dr consultation buy cheap Female Cialis without prescription order Furosemide no visa Furosemide shipped overnight no prescription Furosemide without prescription mexico cheap order rx Furosemide where to purchase generic Hydrochlorothiazide online without a prescription how to purchase Hydrochlorothiazide online without rx Hydrochlorothiazide shipped over night without a perscription generic Buspar tablets purchase Buspar without rx to ship overnight Buspar cheap overnight fedex purchasing Buspar online without prescription generic Buspar buy 40 mg overnight Norvasc without a prescription free Norvasc how to buy Diflucan on line Generic Diflucan no prescription purchase Diflucan free consultation purchase Diflucan without a prescription overnight shipping buy rx Diflucan without order Hydrochlorothiazide without prescription to ship overnight purchase cheap online Hydrochlorothiazide purchase Prednisolone money purchase purchase Prednisolone money purchase free fedex delivery Prednisolone Prednisolone without prescription shipped overnight express buy Prednisolone without rx from us pharmacy buy online rx Norvasc without where to purchase generic Norvasc online without a prescription where can i purchase Norvasc no rx order Amitriptyline without rx needed buy Amitriptyline no visa without rx buy discount Amitriptyline online No script Amitriptyline purchase Amitriptyline no prescription cheap Online pharmacies no perscription Cephalexin buy Cephalexin pay pal without prescription Cephalexin overnight fed ex no prescription purchase Cephalexin visa online ordering Ventolin buy cheap online pharmacy Ventolin purchase Ventolin overnight delivery Strattera without a prescription canadian order Strattera overnight delivery how to get a Strattera prescription Strattera online no prescription overnight purchase Strattera without rx needed Nizagara shipped by cash on delivery ordering Hydrochlorothiazide online without a prescription purchase Hydrochlorothiazide prescription online fedex Hydrochlorothiazide without priscription Hydrochlorothiazide overnight delivery fed ex Hydrochlorothiazide no script fedex order Ventolin 100 mg without prescription order Ventolin without a prescription overnight shipping how to get a Brand Levitra rx Brand Levitra online with no perscription or membership Brand Levitra with no perscription overnight shipping Brand Levitra fedex shipping Brand Levitra without prescription medications purchasing Nizagara without a script buy Nizagara cod next day delivery how to purchase Nizagara online without a prescription buy Nizagara without a rx overnight shipping online pharmacy Cytotec no prescription Cytotec 40 mg online purchase online prescription Cytotec Phenergan without prescription overnight shipping Phenergan with free dr consultation Buy Phenergan next day delivery Phenergan without prescriptions Buy Phenergan online buy Flagyl ER without a rx overnight shipping Flagyl ER without prescriptions Buy Voltaren cost purchasing Voltaren online without prescription Buy perscription Voltaren online buy Voltaren next day delivery Overnight Voltaren without a perscription purchase online prescription Cytotec without canadian prescriptions Celebrex cheap Diflucan by money order Where to buy generic Diflucan online without a perscription Diflucan pharmacy generic Diflucan fedex order Diflucan amex online without prescription Flagyl ER 100 mg Flagyl ER online buy saturday delivery Flagyl ER shipped overnight without a prescription Lipitor overnight delivery fed ex purchase Lipitor without Lipitor with consult where can i buy Lipitor without a rx order Lipitor for cash on delivery Celebrex online buy saturday delivery How to get perscription of Celebrex Buy Celebrex online no membership fedex Celebrex overnight without a rx no perscription VPXL buy VPXL 250 mg Buy VPXL online overnight purchasing Wellbutrin SR with overnight delivery buy Wellbutrin SR without a prescription or membership Online Wellbutrin SR no perscription Wellbutrin SR with overnight fedex cheap Wellbutrin SR for sale online no prescription required order Glucophage without rx needed ordering Glucophage over the counter Online us Glucophage Norvasc 40 mg online buy Norvasc amex online without rx Norvasc online prescriptions with no membership Norvasc without a perscription or buy Norvasc free consultation purchase VPXL no visa without prescription buy Proventil without rx purchase Proventil online with overnight delivery buy Clomid without Overnight Clomid without a perscription Clomid online order saturday delivery cheap purchase Clomid where to buy Clomid no prescription no fees c.o.d Glucophage Glucophage overnight online free Baclofen buy Baclofen without rx from us pharmacy ordering Cipro over the counter Cipro shipped COD order Cipro no visa without rx Cipro without dr buy Cipro online consultation us Buy generic Proventil online Proventil doctor consult purchase Metformin pay pal online without prescription Metformin Online us Metformin no prescriptions needed for Metformin Baclofen overnight fed ex no perscription order Baclofen for cash on delivery order Hydrochlorothiazide no visa without rx Order Lisinopril online without a perscription order Cephalexin without rx needed purchase Cytotec paypal without prescription buy online rx VPXL without buy discount Proventil online no perscription Baclofen

Archive for January, 2012

Coding Best Practice: 5 Whys applied to bug fixing

Sunday, January 22nd, 2012

When you find a bug, fixing it is not enough. You should take a moment or two to think about how the bug was able to occur. Frequently, a single bug report (particularly in more complex systems) hints at a number of underlying issues.

I generally try to find at least three independent fixes to ensure that a given bug couldn’t occur again. You could think of this as defense in depth or defensive programming, but my original inspiration to apply this came from the 5-whys principle. In my case, I use 3, but the exact number isn’t the important part.

Good questions to reflect on:
1) Was there an earlier point in code where we could have noticed something was going wrong? Can I improve the error reporting anywhere along the call trace?
2) Can I refactor the interface or code to make this class of mistakes less likely? Can I better document what the interface is?
3) Is there another way I could hit this same error case? If so, can I quickly (with low risk) fix that one too?
4) What test could I write which would find this error? (Is it worth the time?)
5) Are there simple code changes which would have made debugging this much faster?

Coding Best Practice: The role of comments in source code

Monday, January 16th, 2012

The purpose of a comment in source code is to document why you are doing something or to provide a quick summary of a non-obvious algorithm. It is not to describe what you are doing. Generally, a skilled programmer - i.e. hopefully your peers - can figure out what you’re doing from code just fine. The often confusing part is why. (i.e. “Is this intentional or a bug? If it’s intentional, why is it needed?”.

Good example comments:
“An implementation of merge sort. See http://en.wikipedia.org/wiki/Merge_sort for an overview.”

“There are two obvious implementations here:
a) describe
b) describe
Benchmarking (using the test cases in tests/mybench/*.cxx) shows that using option 1 faster by ~15%. ”

Bad example comments:

“This is broken. (with no explanation or testcase)”
(some complicated bit of code here)

“Sorting an array”
std::sort(vec.begin(), vec.end());

“(none)”
(massive block of hard to read code here)

“(none)”
(edge case or tricky non-obvious behavior)

If you find yourself writing lots of comments (or none), you’re probably “doing it wrong”. Go read up on self-documenting code and start practicing. Remember that self documenting code isn’t writing code without comments; it’s writing code where the comments are executable code themselves.

Please note: Nothing above should be read to discourage the use of function documentation. That’s a separate topic which I may mention later.

Introduction to extending Clang/LLVM

Tuesday, January 10th, 2012

I’ve spent the first part of today watching some of the videos from the LLVM Dev Meeting that occurred back in November. (I really wish I’d been able to attend!) The first talk I watched was the Extending Clang talk by Doug Gregor with Apple. If you’re thinking about playing around with Clang, I strongly suggest you watch this video. I’ve spent the last few months hacking on clang for a language extension I’m working on and this was by far the best introduction I’ve seen. I really wish I’d come across this before I spent hours learning it myself. :)

Other useful links:

Reflections from a crash course in OpenCL

Sunday, January 1st, 2012

Over the last few months, I’ve had an opportunity to spend some time playing with OpenCL. In short, we’re trying to use a GPU to accelerate garbage collection for Java. (Once the work is published, I’ll post more here.) We’ve implemented a simple graph traversal algorithm on an AMD chip using OpenCL. This article doesn’t talk about that effort directly, but instead focuses on a few of the lessons we learned the hard way while getting up to speed on OpenCL. (So I remember them for next time!)

This has been a group effort, but the content, opinions, and mistakes herein are all my own.

Stability & Dev Environment

The first and most important lesson we learned was that each developer needs a dedicated test machine which is not their primary development box. This box needs to be local. When debugging OpenCL programs on real hardware, it is shockingly easy to lock up the entire box. On multiple occasions, we had to perform hard power cycles on our test machine to get it into a usable state.

Even when the box didn’t lock up entirely, a crashed program with a OpenCL kernel outstanding has a bad tendency to prevent future kernels from being executed. Supposedly, there should be a time out that will terminate a run away program, but we never saw this happen in practice. Instead, we ended up rebooting the box quite frequently.

In a related vein, we quickly started replacing every while-loop with a for-loop (over a large, but fixed number of iterations). This allows you to (sometimes) recover from what would otherwise have been an infinitely loop without rebooting the box.

Another important note is that the documentation available from Khronos is a best incomplete and in a couple cases potentially wrong. Many of the function descriptions don’t provide relevant details about usage and none of them provide useful examples. (Can can get some of the latter from the AMD and NVIDIA SDKs.) I strongly suggest searching Google for examples before taking the documentation at its word.

OpenCL does not appear to support a mechanism to forceably abort a kernel. Nor does it support an assertion mechanism. Nor does it have any form of debug logging (i.e. printf or the like.) The only way to exit a kernel function is to return from the main kernel function with all threads. Unfortunately, this means that error reporting - even for cases where you can easily tell what happened - is extremely hard. I don’t have a great solution. We ended up writing data into global memory - so the CPU could access it after termination - and then trying to exit cleanly. This worked sometimes, but was error prone to say the least.

I haven’t played with the various debuggers and emulators available, but I suspect that would help greatly in debugging.

Synchronization

OpenCL has different synchronization models for threads within a workgroup vs across workgroups on the same device. As far as I can tell, there is no synchronization available between kernels running on different devices on the same machine. (You can use the CPU to coordinate starting and stopping kernels of course.)

Barriers apply only to threads within a single workgroup. The CLK_LOCAL/GLOBAL_MEM_FENCE parameters enforce memory consistency within a single workgroup, not across workgroups. Note that you can have a barrier - where all threads stop - but not have a consistent view of memory if you don’t pass the appropriate flags.

ALL threads within a workgroup must encounter the same barrier. If even a single thread does not, the program will hang indefinitely. (And require a hard reboot of the machine.) This is unpleasant to debug to say the least.

Atomic operations are the only way to synchronize between workgroups. To avoid memory contention (and thus serialization of requests), you probably want only a single thread per workgroup to execute the atomic operation. Doing this requires an additional synchronization (using a barrier within the workgroup and a temporary local memory value) to get all threads within a workgroup consistent.

Be careful about which versions of the atomic functions you use. OpenCL provides 32 bit vs 64 bit and local vs shared memory versions. The ones we used - which unfortunately are extensions not part of the language, but thankfully seem pretty common - were cl_khr_int64_base_atomics and cl_khr_int64_extended_atomics. I’ve read some reports that the atomic_op functions don’t function the same as the atom_op versions. I can’t find confirmation of this in the documentation, but we used the atom_op versions just in case. Another gotcha is that some cards apparently don’t support the local versions. Check your documentation carefully since by some reports the functions will simply fail silently.

Note that it is unclear whether the atomic operations on global memory are visible by the CPU, different GPUs, or merely different workgroups on the same device. I haven’t spent much time digging through the documentation, but if this matters to you, check! The one thing that is clear from the documentation is that atomic operations executed by different GPUs on a shared address are not guaranteed to be atomic!

Infrastructure

To get good performance - even just to minimize testing time - you should probably be using precompiled files. (Note: These are not binary files and can not be moved between machines. They are purely a caching mechanism.) You’ll need a mechanism - hash, command line parameter, build system, etc.. - to make sure your cached files stay in sync with your source code of course.

Having a separate program which sanity checks your files - i.e. part of your build system - will save you time in the long run. If I get time, I’ll clean the hacky mess I’ve been using and post it here.

Generally, the best way to get data from the CPU to the GPU (at least on our setup) is to use CL_MEM_USE_HOST_PTR. There seems to be a lot of confusion on exactly what this does, the top Google results appear inaccurate, and the documentation isn’t super clear, but some micro benchmarks gave much better results than for either of the other two options. (As always, you can not assume that the CPU and GPU have consistent views of this data or that it’ll be mapped to the same address on both platforms. All synchronization with the GPU kernels has to be explicit.) It’s also unclear to me if OpenCL is required to copy the data back into the host memory after termination or that region can be entirely stale. That wasn’t important for our case, so I never tested it. The documentation is unclear. The best discussion I’ve seen is here, but even that’s somewhat unclear on the finer points.

Depending on what you’re doing, you may find some of the various utility libraries useful - COPRTHR: STDCL, SOCL, or oclUtils from the NIVIDIA SDK. The only one of these I’ve used is the oclUitls files which were moderately useful.

Conclusion

I hope this was useful to you. If you have corrections, or suggestions, please feel free to contact me.