Test Categories for Visual C++

Wednesday, April 30, 2014 – 9:52 pm

Recently I’ve been using the Visual Studio unit test runner for C++ and came across a trick for adding categories to C++ tests. While the test framework supports traits, a little work makes them much more usable.

    BEGIN_TEST_METHOD_ATTRIBUTE(methodName)
        TEST_METHOD_ATTRIBUTE(L"TestCategory", "my_category")
    END_TEST_METHOD_ATTRIBUTE()
    TEST_METHOD(methodName)
    {
        // ... 
    }

That’s a lot of code to  just add a category to each test.

You can add a specific macro to shorten all of this up, especially if you’re adding categories to all of your tests.

    #define TEST_METHOD_CATEGORY(methodName, category)          \
        BEGIN_TEST_METHOD_ATTRIBUTE(methodName)                 \
            TEST_METHOD_ATTRIBUTE(L"TestCategory", L#category)  \
        END_TEST_METHOD_ATTRIBUTE()                             \
        TEST_METHOD(methodName)

Now you an write tests like this:

    TEST_METHOD_CATEGORY(testMethod, "my_category")
    {
        // ...
    }

Better still you can do the same thing for classes. In this case the BEGIN_CLASS_METHOD_ATTRIBUTE can be used within another macro.

    #define TEST_CLASS_CATEGORY(className, category)                \
        TEST_CLASS(className)                                       \
        {                                                           \
            BEGIN_TEST_CLASS_ATTRIBUTE()                            \
                TEST_CLASS_ATTRIBUTE(L"TestCategory", L#category)   \
            END_TEST_CLASS_ATTRIBUTE()

Note that the MSDN page is incorrect in it’s documentation of this attribute. It is used inside the class and does not take any arguments.

Now you can add a category to a class and, if needed, override the category for individual methods.

    TEST_CLASS_CATEGORY(the_tests, "foo")
    // No '{' required, part of the macro.
        TEST_METHOD_CATEGORY(test, "bar")
        {
            // Test in category 'bar'.
            Assert::Fail(L"Ooops");
        }

        TEST_METHOD(test2)
        {
            // Test in category 'foo'.
            Assert::Fail(L"Ooops");
        }
    };

I’ve used this to organize the tests in the C++AMP Algorithms Library, so you can look what I did there if you want more examples.

Once You Know, You Newegg

Using C++ Classes with C++ AMP

Friday, January 31, 2014 – 6:25 pm

You can use classes with C++ AMP but you have to understand the limitations the CPU/GPU hardware model places on how and were you can use them. The C++ AMP Book says the following:

References and pointers (to a compatible type) may be used locally but cannot be captured by a lambda. Function pointers, pointer-to-pointer, and the like are not allowed; neither are static or global variables.

Classes must meet more rules if you wish to use instances of them. They must have no virtual functions or virtual inheritance. Constructors, destructors, and other non-virtual functions are allowed. The member variables must all be of compatible types, which could of course include instances of other classes as long as those classes meet the same rules. The actual code in your amp-compatible function is not running on a CPU and therefore can’t do certain things that you might be used to doing:

  • recursion
  • pointer casting
  • use of virtual functions
  • new or delete
  • RTTI or dynamic casting
  • goto
  • throw, try, or catch
  • access to globals or statics
  • inline assembler

In some ways this is a little bit misleading as it refers to classes that can be used within a C++ AMP kernel. Here’s an example of a class that meets these requirements.

   1: class stuff
   2: {
   3: public:
   4:      int a;
   5:  
   6:      stuff() : a(0) {}
   7:      stuff(int v) restrict(amp, cpu) : a(v) { }
   8: };

Note that stuff also has a constructor marked with restrict(amp, cpu). This allows your code to create stuff instances in both CPU and GPU code (line 8 in the code below).

You can also create classes that use C++ AMP within a C++ class. In the example below test_case::test_amp() executes an AMP kernel. This works pretty much how you’d expect.

The gotcha comes when you declare a method on the test_case class that you want to call from within the AMP kernel. There is no way to share the class’ this pointer between the CPU and the GPU so methods that are declared as restrict(amp) must also be declared static. So here amp_method is static.

   1: class test_case
   2: {
   3: public:
   4:      test_case() { }
   5:  
   6:      static stuff amp_method(stuff s) restrict(amp, cpu)
   7:      {
   8:          return stuff(s.a * s.a);
   9:      };
  10:  
  11:      void test_amp()
  12:      {
  13:          concurrency::array_view<stuff, 1> data(100);
  14:          concurrency::parallel_for_each(data.extent,
  15:              [data](concurrency::index<1> idx) restrict(amp)
  16:          {
  17:              data[idx] = amp_method(data[idx]);
  18:          });
  19:          data.synchronize();
  20:      };          
  21:  
  22:      void test_cpu()
  23:      {
  24:          std::vector<stuff> data(100, 0);
  25:          for (auto& d : data)
  26:          {
  27:              d = amp_method(d);
  28:          }
  29:      }
  30: };

Hopefully this clears up any confusion. This is the first of several blog posts I have lined up on C++ AMP and some of the things I’m working on for the C++ AMP Algorithms Library.

Tier 3 Acquired by CentryLink

Tuesday, November 19, 2013 – 10:34 am

“Tier 3, a Bellevue cloud computing startup backed with $18.5 from Ignition, Madrona and Intel Ventures, has been sold to CenturyLink.” – Geekwire

Tier-3-becomes-CenturyLink-1024x463

While I’ve only been here a few months and can’t really take any of the credit for any of this. I am really excited, as Tier 3’s self-service platform, combined with CenturyLink’s infrastructure should be a force to be reckoned with!

AMP Algorithms Library now supports Visual Studio 2013

Monday, November 11, 2013 – 9:30 am

With the help of the C++ AMP development team (thanks Meng & Lukasz) I’ve been able to resolve the issues I was having building the Library on VS 2013 and shipped a new release.

The AMP Algorithms Library 0.9.3 release includes minor updates on 0.9.2.

  • Support for Visual Studio 2013 (and 2012)
  • Map-reduce sample code.
  • Support for logical_ functors.
  • Improved pre-checkin build script.

I’m hoping to get the next major release done by the new year as I plan to work on it over the holiday breaks. This should include a number of new additions to the algroithms.

C++AMP Samples Updated for Visual Studio 2013

Wednesday, October 23, 2013 – 3:04 pm

Visual Studio 2013 shipped last week and I found some time to look at the new C++AMP features over the weekend. Most importantly to make sure that the book samples still work. The 1.0 (VS 2013 compatible) release fixes a couple of warnings in the DX Util library and one issue with the C++ AMP upgrade.

writeonly_texture_view<T, N> has been deprecated in the 2013 release. The texture_view<T, N> template class now includes this functionality. You can read more about this on the Parallel Programming and Native Code blog.

You’ll find the following in several places in the Chapter 11 sample code.

#if (_MSC_VER >= 1800)
   
texture_view<int, 2> outputTxVw(outputTx);
#else
   
writeonly_texture_view<int, 2> outputTxVw(outputTx);
#endif

Note: The C++AMP Algorithms Library has not yet been upgraded to VS 2013. I’m working on that.

Seattle Code Camp 2013: Decks and Code Available

Monday, October 7, 2013 – 8:11 am

Several of the other speakers have made their decks and other resources available online.

Seattle Code Camp 2013

Saturday, September 28, 2013 – 8:27 pm

It’s been a great few weeks since starting at Tier 3. I spent two weeks at the Tier 3 Hack House in Utah. Spent two weeks hanging out with the fantastic Tier 3 engineering team and doing a bit of riding at the same time.

I got back in time to give two talks at my favorite local conference, Seattle Code Camp. I only made it for half the day but met up with several old friends and had some great conversations with some of the attendees. Good times!

Here are the slide decks, source code and other resources for the talks I gave at this year’s Seattle Code Camp.

If anyone found or picked up my Apple Thunderbolt to VGA adaptor I’ve love to get it back. You can contact me or the Code Camp organizers. There’s a six pack in it for you.

Not Your Mother’s C++

Download Slide DeckMore and more apps are build for phones and tablets with less powerful processors and limited battery life. If you want to develop for these devices then it’s important to consider performance when building apps that users will love. C++ has a reputation for been hard to read, let alone write. You paid for the performance C++ gave you with late nights chasing memory leaks and crashes. C++ has moved on in the last few years with new language features, libraries and programming idioms. These all make many of the pitfalls of C++ much easier to avoid. This talk will give an overview of the new features in C++11. Including; how to not worry about memory management (too much), use libraries for graphics, math and data structures, and build apps in a few hundred lines of readable code.

Resources

You can download the code samples. Eventually I’ll get them up on GitHub but right now it’s just a zip file. The samples assume that Cinder is installed in yC:\Src\Download\cinder. If you install it somewhere else you will need to modify the project settings.

  • Cinder – The graphics library I used for the GUI in the Life app.
  • Boost – A popular library that implements lots of useful features. I used this to implement file access in the Life app.
  • Standard Library (STL)  – Part of the C++ standard.

The C++11 standard is relatively new, so most compilers don’t support 100% of the standard. C++11 compiler support shootout: Visual Studio, GCC, Clang, Intel gives an overview of which compilers support which features. VS 2013 adds more support for C++11.

You can find videos of sessions from Going Native 2013 online.

Scott Meyer’s books and overview of C++11 are all well worth reading. When I returned to C++ these were the books that got me started.

Someone asked me about game programming books after the talk. Here’s the one I’v been reading Introduction to 3D Game Programming with DirectX 11.

Writing for Developers

Download Slide DeckMaybe you want to write a programming book, maybe just a blog post or some documentation for your team or the users of an open source project. All the great code you wrote is no good if no one else can understand and use it. Learn about the beginning, middle and end of writing anything and what it takes to write great sample code to go along with all those words.

Resources

On Writing: A Memoir Of The Craft by Stephen King was one of the books I wrote when I started writing. Obviously it’s mostly focused on fictional writing but I still found it very valuable.

The Elements of Style, Fourth Edition by William Strunk Jr. and E.B. White is the gold standard when it comes to general questions of writing style.

Links to books on MSDN:

Parallel Programming with Microsoft .NET

Parallel Programming with Microsoft Visual C++

You can also find these books on Amazon along with my book on C++ AMP.

C++ AMP: Accelerated Massive Parallelism with Microsoft® Visual C++

0000101011011110 >> 12

Monday, September 23, 2013 – 6:53 am

What a week it’s been. I’ve spent the last couple of days at the Going Native 2013 conference right here on the Microsoft campus. Essentially this involves hanging out with some of the best C++ developers anywhere. This includes; the inventor of the language, many compiler writers and a lot of people who wrote various bits of the standard. Alongside them are a lot of professional 356 days a year C++ developers.

The really big question is who the hell let me get in there in the first place? I’m not proud, I paid. For a few hundred bucks this is the best conference deal ever. Three days of talks by some of the highest quality speakers and an incredibly savvy audience. I’m not going to say much more. Other than the presentations are already online and if you are interested in C++ at all then you should watch some of them.

So what’s with the weird post title?

tier3_logo120x120 Well, after nearly a decade at Microsoft I’ve decided it’s time to go do something else. I’ve joined the most excellent engineering team at Tier 3. The chance to work for a much smaller company with the likes of Jim Newkirk and Brad Wilson (the xUnit team) and Scott Densmore (ex patterns & practices group), and a cast of other high caliber people was too much to pass up. It’s also a very low friction environment which I’m really happy about.

Coding at the Hock HouseRight now I’m with most of the Tier 3 engineering team are in Utah for a hack house. Spending a couple of weeks hanging out and writing a lot of new code for Tier 3. I’ve also found time to get back on the bike after breaking my collar bone earlier in the summer.

You can figure out what the post title really means.

I’m not intending on ditching C++ for C# and (horror) Javascript. The C++ AMP Algorithms library is something I still intend to keep tinkering with. I’m even giving two talks at Seattle Code Camp on Modern C++ and writing for programmers.

New CQRS Book! Starts With Some Heavy Drinking with Greg Young

Saturday, September 14, 2013 – 11:40 pm

cqrsWhat happens when you go to an ALT.NET open conference in Vancouver with Bob and go drinking with Greg Young?

The obvious outcome is that on Saturday night you find yourself in a bar with Greg and some others drinking way more that is really advisable. Turns out that Greg can say “we’ll have another round” to any passing server while still holding another conversation, even what it’s not his turn to buy. Impressive.

Of course the end result is that I woke up the next morning with a stinking hangover and the vague feeling that we might have come up with a solution to the Turing halting problem. Unfortunately neither of us could decipher the incoherent scribbling on the napkin Bob brought home.

End of story right? No. One of the things that we did remember was that Greg convinced us that CQRS was a “good thing”. When we got back to Redmond we started to convince the patterns & practices group that CQRS was such a good thing that they should do something with it. Chris Tavares did some prototyping but things took time and one way or another, Chris, Bob and I all moved on to other things.

Thankfully Grigori Melnik, Eugenio Pace and Christopher Bennage and the rest of the patterns & practices team picked up the torch. They did the real work of engaging Greg and many other people in the development community.

You can imagine how pleased I was when I visited the patterns & practices team a few weeks ago to discover all this hard work had been turned into a book! Grigori gave me a copy and it looks really good. The sort of guidance that made me so proud to be involved with p&p for several years.

Where it All Started, Nearly 20 Years Ago

Monday, September 9, 2013 – 3:58 pm

University of Southampton Physics BuildingA friend reminded me about this. I think we actually set up a web server for the Astronomy group sometime around Christmas of 1993 but didn’t get it one the NCSA announcements page for several months.

Hum. To think you can create a whole career off of something you did when you were bored of some of your physics research. I guess life really does happen while you are making big plans for something else.

What’s New With NCSA Mosaic

Announcements

Feb 20th 1994

The University of Southampton Astronomy Group now provides an index of recent International Astronomical Union (IAU) telegrams. To avoid breaching the copyright on the circulars, the full text of the telegrams is only available locally. However, if you have your own legitimate source for these documents then this search tool will tell you which telegrams to look in for news on particular astronomical objects and events.

Taken from the archive of NCSA announcements.