C++ AMP 概述
来源:互联网 发布:sql报表编辑器 编辑:程序博客网 时间:2024/04/30 14:33
Windows 7、Windows 8、Windows Server 2008 R2 或 Windows Server 2012
DirectX 11 功能级别 11.0 或更高硬件
对于调试在软件模拟器,需要 Windows 8 或 Windows Server 2012。 对于调试在硬件,则必须安装您的图形卡的驱动程序。 有关更多信息,请参见调试 GPU 代码。
#include <iostream>void StandardMethod() { int aCPP[] = {1, 2, 3, 4, 5}; int bCPP[] = {6, 7, 8, 9, 10}; int sumCPP[5]; for (int idx = 0; idx < 5; idx++) { sumCPP[idx] = aCPP[idx] + bCPP[idx]; } for (int idx = 0; idx < 5; idx++) { std::cout << sumCPP[idx] << "\n"; }}
代码的最重要部分如下所示:
数据:该数据包括三个数组。 所有具有相同的秩 (1) 和长度 (5)。
迭代:第一个 for 循环用于重复提供框架通过数组中的元素。要执行计算总和的代码在第一 for 包含块。
索引:idx 变量的访问数组的各个元素。
使用 C++ AMP,您可以编写以下代码。
#include <amp.h>#include <iostream>using namespace concurrency;const int size = 5;void CppAmpMethod() { int aCPP[] = {1, 2, 3, 4, 5}; int bCPP[] = {6, 7, 8, 9, 10}; int sumCPP[size]; // Create C++ AMP objects. array_view<const int, 1> a(size, aCPP); array_view<const int, 1> b(size, bCPP); array_view<int, 1> sum(size, sumCPP); sum.discard_data(); parallel_for_each( // Define the compute domain, which is the set of threads that are created. sum.extent, // Define the code to run on each thread on the accelerator. [=](index<1> idx) restrict(amp) { sum[idx] = a[idx] + b[idx]; } ); // Print the results. The expected output is "7, 9, 11, 13, 15". for (int i = 0; i < size; i++) { std::cout << sum[i] << "\n"; }}
相同的基本元素存在,但是,使用 C++ AMP 构造:
数据:您使用 C++ 数组构造三个 C++ AMP array_view 对象。 您提供四个值构造 array_view 对象:数据值、秩、元素类型和 array_view 对象的长度在每个维度的。 该级别和类型将作为类型参数。 该数据和长度将作为构造函数参数。 在此示例中,传递给构造函数的 C++ 一维数组。 数组的秩和长度用于构造矩形在 array_view 对象的数据,并且,数据值用于填充数组。 c 运行库还包括 array 类,使用接口本文上类似于 array_view 选件类和稍后对此进行讨论。
迭代:parallel_for_each 函数 (C++ AMP) " 用于重复提供框架通过数据元素或 计算字段。 在此示例中,的计算字段由 sum.extent指定。 要执行的代码在 lambda 表达式或 核函数包含。 restrict(amp) 指示 C++ AMP 可以加快使用 C++ 语言的子集。
索引:index 类 变量,idx,声明了级别一个匹配 array_view 对象的级别。 通过使用索引,您可以访问 array_view 对象的各个元素。
索引选件类
int aCPP[] = {1, 2, 3, 4, 5};array_view<int, 1> a(5, aCPP);index<1> idx(2);std::cout << a[idx] << "\n"; // Output: 3
下面的示例创建一 array_view 两维对象指定元素 = 1 行和列 = 2 的两维索引。 在 index 构造函数的第一个参数是行,元素,第二个参数是列元素。 输出为 6。
int aCPP[] = {1, 2, 3, 4, 5, 6};array_view<int, 2> a(2, 3, aCPP);index<2> idx(1, 2);std::cout << a[idx] << "\n";// Output: 6
下面的示例创建一 array_view 三维对象指定元素深度为 0,= 1 行和列 = 3 的三维索引。 通知第一个参数是深度元素,第二个参数是行元素,因此,第三个参数是列元素。 输出为 8。
int aCPP[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};array_view<int, 3> a(2, 3, 4, aCPP); // Specifies the element at 3, 1, 0.index<3> idx(0, 1, 3); std::cout << a[idx] << "\n";// Output: 8
区域选件类
int aCPP[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};// There are 3 rows and 4 columns, and the depth is two.array_view<int, 3> a(2, 3, 4, aCPP); std::cout << "The number of columns is " << a.extent[2] << "\n";std::cout << "The number of rows is " << a.extent[1] << "\n";std::cout << "The depth is " << a.extent[0]<< "\n";std::cout << "Length in most significant dimension is " << a.extent[0] << "\n";
下面的示例创建包含维度和对象与前面示例的一 array_view 对象,但是,此示例使用 extent 对象而不是使用显式参数在 array_view 构造函数。
int aCPP[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24};extent<3> e(2, 3, 4);array_view<int, 3> a(e, aCPP);std::cout << "The number of columns is " << a.extent[2] << "\n";std::cout << "The number of rows is " << a.extent[1] << "\n";std::cout << "The depth is " << a.extent[0] << "\n";
数组选件类
std::vector<int> data(5);for (int count = 0; count < 5; count++) { data[count] = count;}array<int, 1> a(5, data.begin(), data.end());parallel_for_each( a.extent, [=, &a](index<1> idx) restrict(amp) { a[idx] = a[idx] * 10; });data = a;for (int i = 0; i < 5; i++) { std::cout << data[i] << "\n";}
array_view 选件类
数组和 array_view 比较
下表总结了相似和不同之处。array 和 array_view 选件类之间。
描述
数组选件类
array_view 选件类
当级别确定的
在编译时。
在编译时。
当确定的区域
运行时。
运行时。
形状
矩形。
矩形。
数据存储区
是数据容器。
是数据包装。
复制
在定义的显式和多层次复制。
隐式复制,在核函数访问。
检索数据
通过复制回对象的数组数据在 CPU 请线程。
由 array_view 对象的直接访问或通过调用 array_view::synchronize 方法 "继续访问数据在原始容器。
#include <amp.h>#include <iostream>using namespace concurrency;void AddArrays() { int aCPP[] = {1, 2, 3, 4, 5}; int bCPP[] = {6, 7, 8, 9, 10}; int sumCPP[5] = {0, 0, 0, 0, 0}; array_view<int, 1> a(5, aCPP); array_view<int, 1> b(5, bCPP); array_view<int, 1> sum(5, sumCPP); parallel_for_each( sum.extent, [=](index<1> idx) restrict(amp) { sum[idx] = a[idx] + b[idx]; } ); for (int i = 0; i < 5; i++) { std::cout << sum[i] << "\n"; }}
parallel_for_each 方法采用两个参数、计算字段和 lambda 表达式。
计算字段 是 extent 对象或定义设置线程对并行执行创建的 tiled_extent 对象。 一个线程对计算字段的每个元素生成。 在这种情况下,extent 对象一维并有五个元素。 因此,五个线程。
lambda 表达式 在每个线程定义代码运行。 获取子句,[=],指定 lambda 表达式访问的主体都会因值获取了变量,这是 a,b和 sum。 在此示例中,参数列表创建名为 idx的一维 index 变量。 idx[0] 的值为 0 在第一个线程并递增一个在每个后续的线程。restrict(amp) 指示 C++ AMP 可以加快使用 C++ 语言的子集。 在具有限制修饰符的功能的限制在 限制子句 (C++ AMP)所述。 有关更多信息,请参见 Lambda表达式语法。
lambda 表达式可以包含代码执行也可以调用单独的核函数。 核函数必须包含 restrict(amp) 修饰符。 下面的示例与前面的示例是等效的,但是,它调用单独的核函数。
#include <amp.h>#include <iostream>using namespace concurrency;void AddElements(index<1> idx, array_view<int, 1> sum, array_view<int, 1> a, array_view<int, 1> b) restrict(amp){ sum[idx] = a[idx] + b[idx];}void AddArraysWithFunction(){int aCPP[] = {1, 2, 3, 4, 5}; int bCPP[] = {6, 7, 8, 9, 10}; int sumCPP[5] = {0, 0, 0, 0, 0}; array_view<int, 1> a(5, aCPP); array_view<int, 1> b(5, bCPP); array_view<int, 1> sum(5, sumCPP); parallel_for_each( sum.extent, [=](index<1> idx) restrict(amp) { AddElements(idx, sum, a, b); } ); for (int i = 0; i < 5; i++) { std::cout << sum[i] << "\n"; }}
下图表示的采样数据二维数组让平铺。
// Sample data:int sampledata[] = { 2, 2, 9, 7, 1, 4, 4, 4, 8, 8, 3, 4, 1, 5, 1, 2, 5, 2, 6, 8, 3, 2, 7, 2};// The tiles:// 2 2 9 7 1 4// 4 4 8 8 3 4//// 1 5 1 2 5 2// 6 8 3 2 7 2// Averages:int averagedata[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };array_view<int, 2> sample(4, 6, sampledata);array_view<int, 2> average(4, 6, averagedata);parallel_for_each( // Create threads for sample.extent and divide the extent into 2 x 2 tiles. sample.extent.tile<2,2>(), [=](tiled_index<2,2> idx) restrict(amp) { // Create a 2 x 2 array to hold the values in this tile. tile_static int nums[2][2]; // Copy the values for the tile into the 2 x 2 array. nums[idx.local[1]][idx.local[0]] = sample[idx.global]; // When all the threads have executed and the 2 x 2 array is complete, find the average. idx.barrier.wait(); int sum = nums[0][0] + nums[0][1] + nums[1][0] + nums[1][1]; // Copy the average into the array_view. average[idx.global] = sum / 4; });for (int i = 0; i < 4; i++) { for (int j = 0; j < 6; j++) { std::cout << average(i,j) << " "; } std::cout << "\n";}// Output:// 3 3 8 8 3 3// 3 3 8 8 3 3// 5 5 2 2 4 4// 5 5 2 2 4 4
#include <amp.h>#include <amp_math.h>#include <iostream>using namespace concurrency;void MathExample(){double numbers[] = { 1.0, 10.0, 60.0, 100.0, 600.0, 1000.0 }; array_view<double, 1> logs(6, numbers); parallel_for_each( logs.extent, [=] (index<1> idx) restrict(amp) { logs[idx] = concurrency::fast_math::log10(logs[idx]); } ); for (int i = 0; i < 6; i++) { std::cout << logs[i] << "\n"; }}
texture 类 :可以使用纹理选件类创建纹理从内存或文件。 纹理类似于数组,因为它们包含数据,因此,它们类似于容器在标准模板库 (STL) 中有关分配和复制构造。 有关更多信息,请参见STL容器。 texture 选件类的模板参数是元素类型与该级别。 该级别可以是 1,2 或 3。 元素类型可以是本文后面将介绍的一个简短的向量类型。writeonly_texture_view 类 :提供对所有纹理的只读访问。
短的向量库 :定义一个基于 int、uint、float、double、准则或 unorm的设置长度为 2,3 和 4 的短的向量类型。
在 Windows 应用商店应用程序中使用 C++ AMP
演练:创建一个基本的窗口运行时组件是 C++ 和调用它从 JavaScript
Bing maps 行程优化程序、windows 存储 app 的 JavaScript 和 C++
如何在 C# 中使用 C++ AMP 使用运行时的窗口
如何在 C# 中使用 C++ AMP
从托管代码调用本机函数
GPU 活动关系图
GPU 活动(分页)
GPU 活动(此进程)
GPU 活动(其他进程)
通道(线程视图)
分析 C++ 的并发可视化工具的 AMP 代码
参考
其他资源
- C++ AMP 概述
- C++ AMP 概述
- C++ AMP 概述
- 回首C&&C++
- c&cpp常见问题
- const & %c
- const & %c
- C++&&调试技巧备份
- C\C++ sizeof 陷阱&&总结
- 01 Servlet & Jsp 技术概述
- 计算机通信c&c
- [C学习]赋值&逗号
- C#&..NET初学印象
- C++AMP介绍(一)
- C++AMP学习(1)-入门
- 用C语言实现的Base64编码&amp;解码
- C++ AMP: C++AMP的tiled_index线程编号属性笔记
- C 碎片八 结构体&枚举&联合
- 12枚或者13枚鸡蛋,有一个坏的,用天平3次称出来
- rsync: failed to connect to x.x.x.x: Connection refused (111)
- Java与C底层数据类型转换
- 动态语言跟静态语言
- ASP.NET MVC4.0发布网站
- C++ AMP 概述
- ActivityGroup + GridView 实现Tab分页标签
- android在adapter中调用activity跳转效果
- Android XcapClinet 二
- iPhone开发:单独添加.xib文件
- 获取分辨率 尺寸、样式
- Ubuntu下配置samba实现文件夹共享
- 归并排序的递归实现
- JAVA反射中类的构造函数,成员变量,成员方法的使用