以下代码无法编译.错误消息是:
错误1:
error C3930: 'foo' : no overloaded function has restriction specifiers that are compatible with the ambient context ''
Run Code Online (Sandbox Code Playgroud)
错误2:
error C2660: 'f1' : function does not take 0 arguments
Run Code Online (Sandbox Code Playgroud)
错误3:
IntelliSense: amp-restricted function "int foo() restrict(amp)" (declared at line 5) must be called from an amp-restricted function
Run Code Online (Sandbox Code Playgroud)
该程序:
#include <amp.h>
#include <iostream>
using namespace std;
int foo() restrict(amp) { return 5; }
int f1(int x = foo()) restrict(amp) {
return x;
}
int main()
{
using namespace concurrency; …Run Code Online (Sandbox Code Playgroud) 我正在试验F#中的C++ AMP库,作为使用GPU并行工作的一种方式.但是,我得到的结果似乎并不直观.
在C++中,我创建了一个带有一个函数的库,它使用AMP对数组中的所有数字进行平方:
extern "C" __declspec ( dllexport ) void _stdcall square_array(double* arr, int n)
{
// Create a view over the data on the CPU
array_view<double,1> dataView(n, &arr[0]);
// Run code on the GPU
parallel_for_each(dataView.extent, [=] (index<1> idx) restrict(amp)
{
dataView[idx] = dataView[idx] * dataView[idx];
});
// Copy data from GPU to CPU
dataView.synchronize();
}
Run Code Online (Sandbox Code Playgroud)
(代码改编自Igor Ostrovsky 在MSDN上的博客.)
然后我编写了以下F#来比较任务并行库(TPL)和AMP:
// Print the time needed to run the given function
let time f =
let s = new …Run Code Online (Sandbox Code Playgroud) 我在 C++ AMP 中处理大型数组(超过 65536 个元素)时遇到问题。我正在使用 C++ amp 来计算多边形列表的法线、切线和双切线向量。输入由位置数组(每个位置 3 个浮点数)和 uv 坐标数组(每个顶点 2 个浮点数)组成。在我的parallel_for_each函数中,我计算法线、切线和双切线(每组3个顶点各1个)。我将它们写回数组(封装在 array_view 中)。该算法如下所示:
concurrency::extent<2> ePositions(positionsVector.size() / 3, 3);
concurrency::array_view<const float, 2> positions(ePositions, positionsVector);
concurrency::extent<2> eUVs(uvsVector.size() / 2, 2);
concurrency::array_view<const float, 2> UVs(eUVs, uvsVector);
concurrency::extent<2> eNormalDirections(normalDirectionsVector.size() / 3, 3);
concurrency::array_view<float, 2> normalDirections(eNormalDirections, normalDirectionsVector);
normalDirections.discard_data();
concurrency::extent<2> eTangentDirections(tangentDirectionsVector.size() / 3, 3);
concurrency::array_view<float, 2> tangentDirections(eTangentDirections, tangentDirectionsVector);
tangentDirections.discard_data();
concurrency::extent<2> eBitangentDirections(bitangentDirectionsVector.size() / 3, 3);
concurrency::array_view<float, 2> bitangentDirections(eBitangentDirections, bitangentDirectionsVector);
bitangentDirections.discard_data();
concurrency::parallel_for_each(eNormalDirections.tile<1, 3>(), [=](concurrency::tiled_index<1, 3> t_idx) restrict(amp)
{
< ... calculate the normals, tangents …Run Code Online (Sandbox Code Playgroud) 我想知道在GPU上处理字符串而不是数字是多么逼真?具体来说,我感兴趣的是使用C++ AMP来执行字符串数组和目标字符串之间的比较.
我已经开始使用基础知识,例如将wchar_t* strings[]函数传递给函数,但事实证明,您甚至无法创建类型小于的类型的视图int!
所以我的问题是 - 有没有最好的做法,或者这通常是个坏主意?我也对像warp divergence这样的东西感兴趣 - 例如,计算大型数组上的字符串长度有多高效?
我一直在学习C++ AMP,我所看到的一切都强调AMP在"加速器"上工作,而不仅仅是GPU.我认为今天所有加速器都是GPU,但未来可能会有不同类型的加速器.那还有什么可以成为加速器呢?现在正在开发新型加速器,还是AMP只是试图成为未来的证据?
我正在尝试使用 C++ AMP 计算矩阵。我使用宽度和高度为 3000 x 3000 的数组,并重复计算过程 20000 次:
//_height=_width=3000
extent<2> ext(_height,_width);
array<int, 2> GPU_main(ext,gpuDevice.default_view);
array<int, 2> GPU_res(ext,gpuDevice.default_view);
copy(_main, GPU_main);
array_view<int,2> main(GPU_main);
array_view<int,2> res(GPU_res);
res.discard_data();
number=20000;
for(int i=0;i<number;i++)
{
parallel_for_each(e,[=](index<2> idx)restrict(amp)
{
res(idx)=main(idx)+idx[0];//not depend from calculation type
}
array_view<TYPE, 2> temp=res;
res=main;
main=temp;
}
copy(main, _main);
Run Code Online (Sandbox Code Playgroud)
在计算之前,我将矩阵从主机内存复制到 GPU 内存,并创建一个array_view从 0 到 7 的代码行。
之后,我启动一个循环来计算某些操作并重复 20000 次。每次迭代我都会启动一个parallel_for_each使用 C++ AMP 进行计算的循环。
GPU计算速度非常快,但是当我将结果复制到主机时,array _main我发现这个操作需要很多时间,而且我发现如果我number从20000减少到2000,复制的时间也会减少。
为什么会出现这种情况,是同步问题吗?
背景:有关C++ AMP概述,请参阅Daniel Moth 最近的BUILD演讲.
只有在最后一次参考时他们才会打电话给array_view.synchronize().
在这些简单的例子中,是synchronize()不是需要呼叫?什么时候可以安全排除?我们能否相信parallel_for_each在没有它的情况下"同步"行为(w/r/t正在进行的代码)?
我开始尝试使用C++ AMP.我创建了一个简单的测试应用程序只是为了看看它能做什么,但结果对我来说非常令人惊讶.请考虑以下代码:
#include <amp.h>
#include "Timer.h"
using namespace concurrency;
int main( int argc, char* argv[] )
{
uint32_t u32Threads = 16;
uint32_t u32DataRank = u32Threads * 256;
uint32_t u32DataSize = (u32DataRank * u32DataRank) / u32Threads;
uint32_t* pu32Data = new (std::nothrow) uint32_t[ u32DataRank * u32DataRank ];
for ( uint32_t i = 0; i < u32DataRank * u32DataRank; i++ )
{
pu32Data[i] = 1;
}
uint32_t* pu32Sum = new (std::nothrow) uint32_t[ u32Threads ];
Timer tmr;
tmr.Start();
array< uint32_t, 1 > source( u32DataRank …Run Code Online (Sandbox Code Playgroud) 我在写一些C++ AMP代码时遇到了问题.我收录了一个样本.它在模拟加速器上运行良好,但崩溃了我硬件上的显示驱动程序(Windows 7,NVIDIA GeForce GTX 660,最新驱动程序),但我的代码没有任何问题.
我的代码有问题,还是硬件/驱动程序/编译器问题?
#include "stdafx.h"
#include <vector>
#include <iostream>
#include <amp.h>
int _tmain(int argc, _TCHAR* argv[])
{
// Prints "NVIDIA GeForce GTX 660"
concurrency::accelerator_view target_view = concurrency::accelerator().create_view();
std::wcout << target_view.accelerator.description << std::endl;
// lower numbers do not cause the issue
const int x = 2000;
const int y = 30000;
// 1d array for storing result
std::vector<unsigned int> resultVector(y);
Concurrency::array_view<unsigned int, 1> resultsArrayView(resultVector.size(), resultVector);
// 2d array for data for processing
std::vector<unsigned int> dataVector(x * y); …Run Code Online (Sandbox Code Playgroud) 我正在编写模板化的短矢量和小矩阵类,它们不限于具有2-3-4个元素,但可以具有任意数量的元素.
template <typename T, size_t N>
class ShortVector
{
public:
...
template <size_t I> T& get() { return m_data[I]; }
template <size_t I> const T& get() const { return m_data[I]; }
private:
T m_data[N];
};
Run Code Online (Sandbox Code Playgroud)
我希望访问接口是静态的,这样我就可以专门使用内置的向量寄存器来支持类的大小.(可能它们是AVX,C++ AMP或OpenCL向量.)问题是为这个类编写所有理想的运算符(一元 - ,+, - ,*,/,点,长度......)需要很多模板递归,我甚至没有实现矩阵向量和矩阵 - 矩阵乘法,我将需要嵌套递归.
现在我有非成员朋友操作符和私有成员类,具有各种静态函数,如
template <size_t I, typename T1, typename T2> struct Helpers
{
static void add(ShortVector& dst, const ShortVector<T1, N>& lhs, const ShortVector<T2, N>& rhs)
{
dst.get<I>() = lhs.get<I>() + rhs.get<I>();
Helpers<I - 1, T1, T2>::add(dst, lhs, rhs);
} …Run Code Online (Sandbox Code Playgroud)