gpt4 book ai didi

c++ - parallel_for_each 上下文中的数组复制

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:52:29 27 4
gpt4 key购买 nike

非常是 AMP C++ 的新手。如果我在“parallel_for_each”函数中使用“memcpy”,一切正常,但我知道这不是最佳做法。我尝试使用“copy_to”,但它引发了异常。下面是一个简化的代码,重点是我遇到麻烦的问题。提前致谢。

typedef std::vector<DWORD> CArrDwData;

class CdataMatrix
{
public:
CdataMatrix(int nChCount) : m_ChCount(nChCount)
{
}

void SetSize(UINT uSize)
{
// MUST be multiple of m_ChCount*DWORD
ASSERT(uSize%sizeof(DWORD) == 0);
m_PackedLength = uSize/sizeof(DWORD);
m_arrChannels.resize(m_ChCount*m_PackedLength);
}

UINT GetChannelPackedLen() const
{
return m_PackedLength;
}
const LPBYTE GetChannelBuffer(UINT uChannel) const
{
CArrDwData::const_pointer cPtr = m_arrChannels.data() + m_PackedLength*uChannel;

return (const LPBYTE)cPtr;
}

public:
CArrDwData m_arrChannels;

protected:
UINT m_ChCount;
UINT m_PackedLength;
};

void CtypDiskHeader::ParalelProcess()
{
const int nJobs = 6;
const int nChannelCount = 3;
UINT uAmount = 250000;
int vch;

CArrDwData arrCompData;

// Check buffers sizes
ASSERT((~uAmount & 0x00000003) == 3); // DWORD aligned
const UINT uInDWSize = uAmount/sizeof(DWORD); // in size give in DWORDs

CdataMatrix arrChData(nJobs);

arrCompData.resize(nJobs*uInDWSize);
vector<int> a(nJobs);
for(vch = 0; vch < nJobs; vch++)
a[vch] = vch;

arrChData.SetSize(uAmount+16); // note: 16 bytes or 4 DWORDs larger than uInDWSize

accelerator_view acc_view = accelerator().default_view;

Concurrency::extent<2> eIn(nJobs, uInDWSize);
Concurrency::extent<2> eOut(nJobs, arrChData.GetChannelPackedLen());

array_view<DWORD, 2> viewOut(eOut, arrChData.m_arrChannels);

array_view<DWORD, 2> viewIn(eIn, arrCompData);

concurrency::parallel_for_each(begin(a), end(a), [&](int vch)
{
vector<DWORD>::pointer ptr = (LPDWORD)viewIn(vch).data();
LPDWORD bufCompIn = (LPDWORD)ptr;
ptr = viewOut(vch).data();
LPDWORD bufExpandedIn = (LPDWORD)ptr;

if(ConditionNotOk())
{
// Copy raw data bufCompIn to bufExpandedIn

// Works fine, but not the best way, I suppose:
memcpy(bufExpandedIn, bufCompIn, uAmount);

// Raises exception:
//viewIn(vch).copy_to(viewOut(vch));
}
else
{
// Some data processing here
}
});
}

最佳答案

是我的错。在原始代码中,viewOut(vch) 的范围比 viewIn(vch) 的范围大一点。使用这种方式,它会引发异常“runtime_exception”。捕获它时,它会提供以下消息 xcp.what() = "Failed to copy because extents do not match"。

我修复了替换原始代码的代码:viewIn(vch).copy_to(viewOut(vch).section(viewIn(vch).extent));它只复制源范围,这就是我所需要的。但只编译没有受限制的 AMP。

关于c++ - parallel_for_each 上下文中的数组复制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26042477/

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