C++AMP学习(1)-入门

来源:互联网 发布:linux dma中断 编辑:程序博客网 时间:2024/05/18 03:48

简介:C++AMP是微软开发的GPU并行库,类似的编程模型还有大家熟悉的OpenGL和CUDA模型。相比于后两者,尽管效率不是特别高,但C++AMP具有易用,代码简洁,且兼容各类GPU设备等优点。其他也没什么了。

环境搭建:VS2012,13,15等IDE均可。外加电脑配置有一块Nvidia或AMD的GPU即可。

入门代码,检测设备是否支持AMP的代码

//===============================================================================//// Microsoft Press// C++ AMP: Accelerated Massive Parallelism with Microsoft Visual C++////===============================================================================// Copyright (c) 2012 Ade Miller & Kate Gregory.  All rights reserved.// This code released under the terms of the // Microsoft Public License (Ms-PL), http://ampbook.codeplex.com/license.//// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A// PARTICULAR PURPOSE.//===============================================================================#include <tchar.h>#include <SDKDDKVer.h>#include <iostream>#include <vector>#include <amp.h>using namespace concurrency;int _tmain(int argc, _TCHAR* argv[]){    std::vector<accelerator> accls = accelerator::get_all();    accls.erase(std::remove_if(accls.begin(), accls.end(), [](accelerator& a) {         return (a.device_path == accelerator::cpu_accelerator) || (a.device_path == accelerator::direct3d_ref);     }), accls.end());    if (accls.empty())    {        std::wcout << "No accelerators found that are compatible with C++ AMP" << std::endl << std::endl;        return 0;    }    std::wcout << std::endl << "Found " << accls.size()         << " accelerator device(s) that are compatible with C++ AMP:" << std::endl;    int n = 0;    std::for_each(accls.cbegin(), accls.cend(), [=, &n](const accelerator& a)    {        std::wcout << "  " << ++n << ": " << a.description             << ", has_display=" << (a.has_display ? "true" : "false")             << ", is_emulated=" << (a.is_emulated ? "true" : "false")             << std::endl;    });    std::wcout << std::endl;return 1;}

的电脑有一块GT540M,显示为支持AMP,显示如下。即使电脑不存

在支持的GPU,对于win8和VS2012以后的版本,系统会出现仿真加速

器,也可执行AMP程序,但效率不高。