"Hello world" in C++ AMP

We think of our matrix multiplication example as "Hello world", but it didn't sit quite well with me. It was missing the essential ingredients for a "Hello world" demo which are: to be simple, require a complete code listing, fit on a single slide, and output the text "Hello world". So in this blog post I'll share an alternative one that has those ingredients.


In Visual Studio 11 Beta (or later)

  1. Select "File" -> "New" -> "Project..." menu
  2. Select the "Empty Project" template and press "OK"
  3. Select the "Project" -> "Add New Item..." menu
  4. Select the "C++ File (.cpp)" item template and press "Add"

You now have an empty "Source.cpp" code file and have made no changes to any configuration of the project system or the compiler.

"Hello world" code

Type in (or copy paste) the following 14 lines of simplistic code and you are done!

  #include <iostream> 
  #include <amp.h> 
  using namespace concurrency; 
  int main() 
    int v[11] = {'G', 'd', 'k', 'k', 'n', 31, 'v', 'n', 'q', 'k', 'c'};

    array_view<int> av(11, v); 
    parallel_for_each(av.extent, [=](index<1> idx) restrict(amp) 
      av[idx] += 1; 

    for(unsigned int i = 0; i < 11; i++) 
      std::cout << static_cast<char>(av[i]); 

...and this is the "Hello world" output when run from the command line.

Hello world


The naive code above does not exhibit any performance benefits compared to a serial CPU implementation. The reason is that the amount of data is not large enough and the body of the loop is not performing expensive operations, to pay for the synchronization and data transfer overhead. The code does serve its purpose of understanding the main elements of the C++ AMP programming model, and that is left as an exercise to the reader; to aid you with that exercise, the program is using: index, extent, restrict, parallel_for_each, and array_view. You can also watch a screencast introducing these with the "Hello World" example.


Comments (9)
  1. Nice.  These type of posts are good for us just trying to wrap our head around AMP problems.

  2. I tried these instructions in the newest beta and I get an error says:

    1 IntelliSense: illegal parameter type "void *" for amp-restricted function "Concurrency::details::_Texture_descriptor::_Texture_descriptor(Concurrency::details::_Texture *_Texture_ptr) restrict(cpu,amp)" (declared at line 538) d:Program Files (x86)Microsoft Visual Studio 11.0VCincludeamprt.h 1466 16 Project1

  3. DanielMoth says:

    @Castaa, glad you liked it.

    @ “I tried these instructions in the newest beta and I get an error”, that is a harmless IntelliSense error that you can ignore (or filter out in that window) – sorry, known Beta error. If you get an actual compiler error that prevents building and executing, please let us know.

  4. Gabriel says:

    You can eliminate the IntelliSense error if you just include <amprt.h> after the #include <amp.h> line

  5. AlexisXu says:

    Failed to generate debug information when compiling the call graph for the concurrency::parallel_for_each at:

    what't wrong

  6. AlexisXu says:

    by the way, i have installed vs ultimate 2012

    Version 11.0.50727.1 RTMREL

  7. Do you get this error when compiling the "Hello world" code from the blog post above?

    Also, what is the configuration you are building "Win32/x64"? Also some details about your environment would be helpful.

  8. AlexisXu says:

    it seems my own computer can build and run Hello World, but the desktop with the same dev environment  can not. I could find the reason.

    Never mind, thanks for your rely.

  9. augusdi says:

    so good, thanks

Comments are closed.

Skip to main content