当前位置: 首页 > news >正文

windows C++ 并行编程-在 UWP 应用中使用 C++ AMP

可以在通用 Windows 平台 (UWP) 应用中使用 C++ AMP (C++ Accelerated Massive Parallelism) 在 GPU(图形处理单元)或其他计算加速器上执行计算。 但是,C++ AMP 不提供用于直接处理 Windows 运行时类型的 API,并且 Windows 运行时不提供 C++ AMP 包装器。 当你在代码(包括你自己创建的代码)中使用Windows 运行时类型时,必须将它们转换为与 C++ AMP 兼容的类型。

性能注意事项

如果使用的是 Visual C++ 组件扩展 C++/CX 创建通用 Windows 平台 (UWP) 应用,建议将普通旧数据 (POD) 类型与连续存储(例如 std::vector 或 C 样式数组)一起用于将与 C++ AMP 结合使用的数据。 相较于使用非 POD 类型或 Windows 运行时容器,这样有助于获得更高性能,因为不必进行封送处理。

在 C++ AMP 内核中,若要访问以这种方式存储的数据,只需将 std::vector 或数组存储包装在 concurrency::array_view 中,然后在 concurrency::parallel_for_each 循环中使用数组视图:

// simple vector addition example
std::vector<int> data0(1024, 1);
std::vector<int> data1(1024, 2);
std::vector<int> data_out(data0.size(), 0);concurrency::array_view<int, 1> av0(data0.size(), data0);
concurrency::array_view<int, 1> av1(data1.size(), data1);
concurrency::array_view<int, 1> av2(data_out.size(), data2);av2.discard_data();concurrency::parallel_for_each(av0.extent, [=](concurrency::index<1> idx) restrict(amp){av2[idx] = av0[idx] + av1[idx];});
排列 Windows 运行时类型

使用 Windows 运行时 API 时,最好对存储在 Windows 运行时容器(例如 Platform::Array<T>^)或复杂数据类型(例如通过使用 ref 关键字或 value 关键字声明的类或结构)中的数据使用 C++ AMP。 在这些情况下,必须执行额外的操作才能向 C++ AMP 提供数据。

Platform::Array<T>^

其中 T 是 POD 类型
当遇到 Platform::Array<T>^ 和 T 是 POD 类型时,只需使用 get 成员函数即可访问其基础存储:

Platform::Array<float>^ arr; // Assume that this was returned by a Windows Runtime API
concurrency::array_view<float, 1> av(arr->Length, &arr->get(0));

如果 T 不是 POD 类型,请使用以下部分中描述的技术将数据与 C++ AMP 配合使用。

Windows 运行时类型: 引用类和值类

C++ AMP 不支持复杂数据类型。 这包括非 POD 类型和使用 ref 关键字或 value 关键字声明的任何类型。 如果在 restrict(amp) 上下文中使用了不受支持的类型,就会生成编译时错误。

遇到不受支持的类型时,可以将其数据的所需部分复制到 concurrency::array 对象中。 除了让数据可供 C++ AMP 使用之外,这种手动复制的方法还能提高性能,因为可以最大限度扩大数据位置,并确保不会将不打算使用的数据复制到加速器。 可以使用暂存数组进一步提高性能,暂存数组是 concurrency::array 的一种特殊形式,它向 AMP 运行时提供一个提示:应该优化数组,以便在该数组和指定加速器上的其他数组之间频繁转换。

// pixel_color.h
ref class pixel_color sealed
{
public:pixel_color(Platform::String^ color_name, int red, int green, int blue){name = color_name;r = red;g = green;b = blue;}property Platform::String^ name;property int r;property int g;property int b;
};// Some other filestd::vector<pixel_color^> pixels (256);for (pixel_color ^pixel : pixels)
{pixels.push_back(ref new pixel_color("blue", 0, 0, 255));
}// Create the accelerators
auto cpuAccelerator = concurrency::accelerator(concurrency::accelerator::cpu_accelerator);
auto devAccelerator = concurrency::accelerator(concurrency::accelerator::default_accelerator);// Create the staging arrays
concurrency::array<float, 1> red_vec(256, cpuAccelerator.default_view, devAccelerator.default_view);
concurrency::array<float, 1>  blue_vec(256, cpuAccelerator.default_view, devAccelerator.default_view);// Extract data from the complex array of structs into staging arrays.
concurrency::parallel_for(0, 256, [&](int i){red_vec[i] = pixels[i]->r;blue_vec[i] = pixels[i]->b;});// Array views are still used to copy data to the accelerator
concurrency::array_view<float, 1> av_red(red_vec);
concurrency::array_view<float, 1> av_blue(blue_vec);// Change all pixels from blue to red.
concurrency::parallel_for_each(av_red.extent, [=](index<1> idx) restrict(amp){av_red[idx] = 255;av_blue[idx] = 0;});

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 【Python报错已解决】ValueError: cannot reindex from a duplicate axis
  • python之os处理文件和目录的函数
  • 每日定期分享诗歌
  • vue页面自适应 动态 postcss postcss-pxtorem
  • 网络压缩之参数量化(parameter quantization)
  • 毕业论文免费查重网站
  • Mysql高级篇(上)
  • 【HuggingFace Transformers】LlamaModel源码解析
  • 003、架构_详解(重点)
  • 【python计算机视觉编程——多视图几何】
  • pnpm快速入门
  • ComfyUI:基于差分扩散的像素级图像修改
  • 【Linux修行路】进程通信——消息队列、信号量
  • 计算之魂:持续于正确的因果链(一)
  • MySQL用户管理:用户管理、用户授权、用户权限撤销
  • github从入门到放弃(1)
  • HTTP中的ETag在移动客户端的应用
  • java第三方包学习之lombok
  • Java-详解HashMap
  • k个最大的数及变种小结
  • Linux学习笔记6-使用fdisk进行磁盘管理
  • MD5加密原理解析及OC版原理实现
  • Python学习之路13-记分
  • react-native 安卓真机环境搭建
  • Spring Security中异常上抛机制及对于转型处理的一些感悟
  • vue从创建到完整的饿了么(11)组件的使用(svg图标及watch的简单使用)
  • vue从创建到完整的饿了么(18)购物车详细信息的展示与删除
  • Vue官网教程学习过程中值得记录的一些事情
  • XML已死 ?
  • 包装类对象
  • 对象管理器(defineProperty)学习笔记
  • 回顾2016
  • 开源中国专访:Chameleon原理首发,其它跨多端统一框架都是假的?
  • 猫头鹰的深夜翻译:Java 2D Graphics, 简单的仿射变换
  • 前言-如何学习区块链
  • 数组大概知多少
  • LevelDB 入门 —— 全面了解 LevelDB 的功能特性
  • Mac 上flink的安装与启动
  • PostgreSQL 快速给指定表每个字段创建索引 - 1
  • PostgreSQL之连接数修改
  • 回归生活:清理微信公众号
  • #ubuntu# #git# repository git config --global --add safe.directory
  • #数学建模# 线性规划问题的Matlab求解
  • (13)DroneCAN 适配器节点(一)
  • (C语言)fgets与fputs函数详解
  • (done) ROC曲线 和 AUC值 分别是什么?
  • (env: Windows,mp,1.06.2308310; lib: 3.2.4) uniapp微信小程序
  • (HAL库版)freeRTOS移植STMF103
  • (html5)在移动端input输入搜索项后 输入法下面为什么不想百度那样出现前往? 而我的出现的是换行...
  • (备忘)Java Map 遍历
  • (六)c52学习之旅-独立按键
  • (六)Hibernate的二级缓存
  • (收藏)Git和Repo扫盲——如何取得Android源代码
  • (一)UDP基本编程步骤
  • (轉)JSON.stringify 语法实例讲解