gpt4 book ai didi

C++ AMP : array_view and index behaving strangely (unexpected values stored)

转载 作者:行者123 更新时间:2023-11-30 01:58:24 25 4
gpt4 key购买 nike

我写了一个小的测试函数,它的行为与我想要的不一样。

基本上,它应该读取一个数组并写回它的内容(稍后,当它起作用时,它应该做更多,但现在即使这样也会失败)。

调试 GPU 代码时,我看到前几次迭代(以某种方式并行执行......这对 GPU 来说可能有意义,但在调试时让我感到惊讶)工作正常......但是,在 1-2 之后Debug-Continues (F5),一些先前正确设置的值被 0 覆盖。我不太明白.. 当我再次在 CPU 上时,许多值是 0,即使它们不应该是 0(基本上,它们应该有原始数据,这是一个简单的测试序列)。

#include "stdafx.h"
#include <amp.h>

typedef unsigned char byte;

using namespace concurrency;

void AMPChangeBrightnessContrastWrapper2(byte* a, int len, float brightness, float contrast)
{
array_view<unsigned int> dst(len/4, (unsigned int*)a);
//dst.discard_data();
parallel_for_each(dst.extent, [=](index<1> idx) restrict(amp)
{
// split into bytes (in floats)
float temp1 = (dst[idx]) - (dst[idx] >> 8) * 256;
// this completely fails! float temp1 = dst[idx] & 0xFF;
float temp2 = (dst[idx] >> 8) - (dst[idx] >> 16) * 256;
float temp3 = (dst[idx] >> 16) - (dst[idx] >> 24) * 256;
float temp4 = (dst[idx] >> 24);
// convert back to int-array
dst[idx] = (int)(temp1 + temp2 * 256 + temp3 * 65536 + temp4 * 16777216);

});
//dst.synchronize();
}

int _tmain(int argc, _TCHAR* argv[])
{
const int size = 30000;
byte* a = new byte[size];

// generate some unique test sequence.. first 99 numbers are just 0..98
for (int i = 0; i < size; ++i)
a[i] = (byte)((i + i / 99) % 256);

AMPChangeBrightnessContrastWrapper2(a, size, -10.0f, 1.1f);

for (int i = 0; i < 50; ++i)
printf("%i, ", a[i]);
char out[20];
scanf_s("%s", out);
return 0;
}

如此简单(计划)的步骤:

  • 初始化数组
  • 将数组传递给 GPU(作为无符号整数数组)
  • 将每个unsigned int拆分为4个字节并存储在float中
  • (做一些计算,这里为简单起见省略)
  • 再次将 bytes-stored-in-floats 连接到原来的位置
  • (重复)

如果您想知道..那应该是颜色值..

结果是:

  • 一些值符合预期,但大多数值不同
  • 似乎特别是字节 0(每个 unsigned int)会有一个错误的值
  • 我首先尝试将 unsigned int->byte->float 转换为 & 0xFF,但似乎完全失败了

输出是(但应该只是从 0 开始递增的数字):

0, 1, 2, 3, 0, 5, 6, 7, 0, 9, 10, 11, 16, 13, 14, 15, 0, 17, 18, 19, 32, 21, 22, 23, 32, 25, 26, 27, 32, 29, 30, 31, 0, 33, 34, 35, 64, 37, 38, 39, 64, 41, 42, 43, 64, 45, 46, 47, 64, 49,

问题:

  • 为什么 &0xFF 有问题?
  • 为什么每个 unsigned int 的字节 0 都分配了一个奇怪的值?
  • 我想我不能创建字节数组 View ,我必须使用整数或 float ?
  • 最后注释掉 .synchronize 并没有改变任何东西 - 为什么会这样?

最佳答案

•I suppose I cannot create an array_view of bytes, I have to use ints or floats?

您不能创建字节数组或数组 View 。 C++ AMP 仅支持有限的 C++ 类型子集。您可以使用纹理而不是数组 View 。对于图像处理,这有几个优点,尤其是打包和解包要快得多,因为它是由 GPU 的硬件实现的。请参阅下面的完整示例。

•commenting out the .synchronize in the end did not change anything - how come?

您不需要 dst.synchronize(),因为 dst array_view 超出范围,这会导致数据隐式同步回 CPU 内存。顺便说一下,你不应该在函数的开头调用 dst.discard_data() 因为如果你这样做将意味着来自 a 的数据不会被复制到 GPU .

这是一个使用纹理<>的实现。注意事项:

  • 使用 unit_4 的纹理允许您打包和解包您的数据“免费”。
  • 使用 clamp() 比 if 子句更好,首先它使用了一个内部函数硬件经过优化。一般来说,内核中的分支是不好的,因为它会拖延所有warp 中的线程,即使它们将条件评估为 false。
  • 您需要两个纹理,因为与数组不同,它们不支持别名。
  • 我删除了一些临时变量。变量使用寄存器空间,这在GPU。您应该尽量减少对它的使用,以确保您的所有线程都可以执行不等待寄存器空间可用。
  • 使用 static_cast<> 显式转换意味着更少的编译器警告,通常被认为是成为优秀的(现代)C++ 风格。

还有代码...

void AMPChangeBrightnessContrastWrapper3(const byte* a, const int len, 
const float brightness, const float contrast)
{
const int pixel_len = len / 4;
graphics::texture<graphics::uint_4, 1> inputTx(pixel_len, a, len, 8u);
graphics::texture<graphics::uint_4, 1> outputTx(pixel_len, 8u);
graphics::writeonly_texture_view<graphics::uint_4, 1> outputTxVw(outputTx);

parallel_for_each( outputTxVw.extent, [=, &inputTx, &outputTx](index<1> idx)
restrict(amp)
{
const graphics::uint_4 v = inputTx[idx];

float tmp = static_cast<float>(v.r);
tmp = (tmp - 128) * contrast + brightness + 128;
tmp = direct3d::clamp(tmp, 0.0f, 255.0f);
const unsigned int temp1_ = static_cast<unsigned int>(tmp);

tmp = static_cast<float>(v.g);
tmp = (tmp - 128) * contrast + brightness + 128;
tmp = direct3d::clamp(tmp, 0.0f, 255.0f);
const unsigned int temp2_ = static_cast<unsigned int>(tmp);

tmp = static_cast<float>(v.b);
tmp = (tmp - 128) * contrast + brightness + 128;
tmp = direct3d::clamp(tmp, 0.0f, 255.0f);
const unsigned int temp3_ = static_cast<unsigned int>(tmp);

tmp = static_cast<float>(v.a);
tmp = (tmp - 128) * contrast + brightness + 128;
tmp = direct3d::clamp(tmp, 0.0f, 255.0f);
const unsigned int temp4_ = static_cast<unsigned int>(tmp);

outputTxVw.set(idx, graphics::uint_4(temp1_, temp2_, temp3_, temp4_));
});
copy(outputTx, (void*)a, len);
}

您可以在 AMP Book 中找到更多 C++ AMP 示例

关于C++ AMP : array_view and index behaving strangely (unexpected values stored),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17478632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com