What causes code to run in the GPU?

123 Views Asked by At

I know my code is running in the GPU because performance profiler says so, but I'm getting mixed info about what actually causes it to run in the GPU. This reputable Microsoft developer says parallel_for is CPU and parallel_for_each is GPU. This reputable Microsoft developer implies parallel_for and parallel_for_each are interchangeable (with slight changes in how they are used) but doesn't even mention the GPU or C++amp, although he does compare both to OpenMP. MSDN has articles in each vein as well. Is it which 'restrict' clause one uses? I guess I could do some experiments, but that's not the official word. Any comments will be appreciated.

1

There are 1 best solutions below

0
Yaron Bental On

If you want to be sure you are running on the GPU select the GPU to run your code on.

std::vector<accelerator> accs = accelerator::get_all();


//std::wcout << "Found " << accs.size() << " C++ AMP accelerator(s):" << std::endl;
//std::for_each(accs.cbegin(), accs.cend(), [](const accelerator& a)
//{
//  if (a.get_is_emulated() == false)
//  {
//      std::wcout << "  " << a.device_path << std::endl
//          << " description   " << a.description
//          << " device_path   " << a.device_path
//          << " is_emulated   " << a.get_is_emulated()
//          << std::endl << std::endl;
//  }
//});

But to be more specific the restrict key word will attempt to run on the accelerator if one is available. if it is not the same code will run on the CPU yourself. In some of our project we detect the accelerator (GPU) with the most memory and make it the "chosen" accelerator.

Hope this helps.