gpt4 book ai didi

c++ - 优化直方图更新

转载 作者:行者123 更新时间:2023-12-02 10:24:55 25 4
gpt4 key购买 nike

我正在更新一个直方图,它使用具有16个bin的简单整数数组来表示,如下所示。

const int binSize = 4096;
int histogram[16];

unsigned short inData[1024]; // This is my input data. Short is 16 bits
for(int i = 0; i < 1024; ++i)
{
++histogram[inData[i] / binSize];
}

我经常运行此操作,因此成为瓶颈,因为DSP无法并行处理此循环,因为无法同时更新多个bin。我该如何优化呢?

我正在TI DSP C6000系列上运行此代码。

最佳答案

举例说明注释的含义:

#include <array>
#include <algorithm>
#include <boost/range/adaptor/transformed.hpp>
using Histogram = std::array<int, 16>;

Histogram from_short(short num)
{
Histogram result;
result[num / 4096] = 1;
return result;
}

Histogram add(const Histogram & lhs, const Histogram & rhs)
{
Histogram result;
for (size_t i = 0; i < 16; ++i) { result[i] = lhs[i] + rhs[i]; }
return result;
}

auto singles = indata | boost::adaptors::transformed(from_short);
Histogram histogram = std::reduce(begin(singles), end(singles), Histogram{}, add);

另外一个选项:
std::sort(begin(indata), end(indata));
short * previous = begin(indata);
for (size_t i = 0; i < 15; ++i)
{
short * current = std::lower_bound(indata, 4096 * (i + 1));
histogram[i] = std::distance(previous, current);
previous = current;
}
histogram[16] = std::distance(previous, end(indata));

关于c++ - 优化直方图更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43663096/

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