In parallel programming, most of the time the use of locks is good enough for the application. And when it is not, then you may need to resort to atomic weapons. While I can and have happily written my own lock implementations, its like the story of a lawyer redoing his kitchen himself. It is not a good use of the lawyer's time unless he's enjoying it.
That said, I have had to use atomic weapons against a compiler. The compiler happily reordered several memory operations in an unsafe way. Using fence instructions, I was able to prevent this reordering, while not seeing fences in the resulting assembly. I still wonder if there was some information I was not providing.
Regardless, the weapons are useful! And I can thank the following presentation for illuminating me to the particular weapon that was needed, Atomic Weapons. I have reviewed earlier work by Herb Sutter and he continues to garner my respect (not that he is aware), but nonetheless I suggest any low-level programmer be aware of the tools that are available, as well as the gremlins that lurk in these depths and might necessitate appropriate weaponry.
A discussion of how to do Computer Science well, particularly writing code and architecting program solutions.
Showing posts with label standard library. Show all posts
Showing posts with label standard library. Show all posts
Tuesday, September 16, 2014
Thursday, July 11, 2013
Book Review: Exceptional C++ Style 40 New Engineering Puzzles, ...
Generally, I do not write code in C++; however, on occasion (like when writing LLVM compiler passes), I am forced into using this language. I also more regularly find myself grading student assignments that have used C++. Particularly reading these assignments, I will be thankful to have read this book and better be able to express how students have violated the standards, as they have done in the past.
Were I forced to read a book directly on the C++ standards, let's just say I can think of lots of things I'd rather be doing. But while Exceptional C++ Style: 40 New Engineering Puzzles, Programming Problems, and Solutions exposed me to more of the standards, I never felt like I was reading quotes from the standard. Instead, I was listening to an interesting conversation about some real programming questions that just may require invoking the standards to answer.
I enjoyed chapters 20 and 21, as I appreciate the effort toward explaining how memory is allocated and structures laid out. Help dispel the ignorance that new / malloc are how the OS provides memory. And I then learned that new will throw an exception instead of returning NULL. Perhaps time to rewrite some code. Furthermore, I understand now why most C++ code uses preincrement on iterators.
It is not strictly a book on style, but instead this tome covers the style I care most about: good programming practices. I don't care which style convention you use, so long as you use one consistently. But for whatever style your code has, it had better be good code.
I recommend reading this book even if you do not regularly use C++. I will note that it is dated; however, unless you are now using C++11, the text is still timely and even if you are using C++11 I doubt that everything has changed (though I did notice the discussion of the auto keyword was out of date).
Were I forced to read a book directly on the C++ standards, let's just say I can think of lots of things I'd rather be doing. But while Exceptional C++ Style: 40 New Engineering Puzzles, Programming Problems, and Solutions exposed me to more of the standards, I never felt like I was reading quotes from the standard. Instead, I was listening to an interesting conversation about some real programming questions that just may require invoking the standards to answer.
I enjoyed chapters 20 and 21, as I appreciate the effort toward explaining how memory is allocated and structures laid out. Help dispel the ignorance that new / malloc are how the OS provides memory. And I then learned that new will throw an exception instead of returning NULL. Perhaps time to rewrite some code. Furthermore, I understand now why most C++ code uses preincrement on iterators.
It is not strictly a book on style, but instead this tome covers the style I care most about: good programming practices. I don't care which style convention you use, so long as you use one consistently. But for whatever style your code has, it had better be good code.
I recommend reading this book even if you do not regularly use C++. I will note that it is dated; however, unless you are now using C++11, the text is still timely and even if you are using C++11 I doubt that everything has changed (though I did notice the discussion of the auto keyword was out of date).
Monday, March 4, 2013
Maps and Red-Black Trees
When cooperating on a prior work of research, Brainy: effective selection of data structures, I learned about the cost of selecting particular data structures. I expect that every Computer Scientist knows the general cost of using standard data structures like trees and linked lists. Now, the extension that Brainy provided was to recognize that a "tree" could have different implementations and these implementations can have different costs for a given workload. I, and I expect others, learned about AVL, splay, red-black and other tree types.
All this is good until your type hides its implementation and you reason about it in abstract. A map is commonly misinterpreted by its implementation. A map is a key-value store, where a "key" has an associated "value". An address book can be understood as a map of name to address / phone number / etc.
Now, the map is assumed to be implemented as a hash table, to give O(1) look-up cost. However in the C++ standard library, map uses a red-black tree for O(log n). Before you go and request that the libraries be changed, understand that experimental measurements suggest that the red-black implementation wins out when n > 500, as the hash collisions dominate the idealized O(1) cost. This is the problem that Brainy attempts to solve: the programmer selects the functionality, e.g., map and Brainy selects the best implementation given your program and architecture.
So go ahead and use a map, but recognize that you may not have O(1) cost and definitely won't as n grows increasingly large.
All this is good until your type hides its implementation and you reason about it in abstract. A map is commonly misinterpreted by its implementation. A map is a key-value store, where a "key" has an associated "value". An address book can be understood as a map of name to address / phone number / etc.
Now, the map is assumed to be implemented as a hash table, to give O(1) look-up cost. However in the C++ standard library, map uses a red-black tree for O(log n). Before you go and request that the libraries be changed, understand that experimental measurements suggest that the red-black implementation wins out when n > 500, as the hash collisions dominate the idealized O(1) cost. This is the problem that Brainy attempts to solve: the programmer selects the functionality, e.g., map and Brainy selects the best implementation given your program and architecture.
So go ahead and use a map, but recognize that you may not have O(1) cost and definitely won't as n grows increasingly large.
Subscribe to:
Posts (Atom)