如何获得thust :: unique()与推力矢量一起工作:: pair

发布于 2025-01-27 21:52:54 字数 2436 浏览 3 评论 0原文

我正在尝试制作thrust :: pair< int> gt; gt; int>使用推力:: unique(),但是所得的输出似乎并不像应该排序。这是所讨论的代码:

struct make_pair : public thrust::binary_function<int, int, thrust::pair<int, int>> {
    __host__ __device__
        thrust::pair<int, int> operator()(int x, int y) { return thrust::make_pair(x, y); }
};



std::vector<thrust::pair<int, int>> find_borders_launch(int src_width, int src_height, cv::cuda::GpuMat& d_src) {

    thrust::device_vector<int> d_focus_result(src_width * src_height * 9);
    thrust::device_vector<int> d_target_result(src_width * src_height * 9);

    int* d_focus = thrust::raw_pointer_cast(d_focus_result.data());
    int* d_target = thrust::raw_pointer_cast(d_target_result.data());

    dim3 num_blocks = {uint(src_width + 1), uint(src_height + 1)};
    dim3 threads_per_block = {9, 1, 1};
    int substep_size = sizeof(int);

    find_borders_kernel <<<num_blocks, threads_per_block>>> (src_width, src_height, d_src.data, d_src.step, d_focus, d_target, substep_size);


    thrust::device_vector<thrust::pair<int, int>> d_unique(d_focus_result.size());


    thrust::transform(d_focus_result.begin(), d_focus_result.end(), d_target_result.begin(), d_unique.begin(), make_pair() ); //seems to do its job just fine

    thrust::unique(thrust::device, d_unique.begin(), d_unique.end());

    std::vector<thrust::pair<int, int>> output(d_unique.size());
    thrust::copy(d_unique.begin(), d_unique.end(), output.begin() );
    return output;
}

输出看起来像:


0, 0
-1, -1
0, 0
-1, -1
0, 0
-1, -1
0, 0
-1, -1
0, 0
-1, -1
0, 0
-1, -1
0, 0
-1, -1
0, 0
-1, -1
0, 0
-1, -1
0, 0
-1, -1
0, 76
-1, -1
0, 76
0, 0
76, 0
-1, -1
76, 0
-1, -1
0, 0
-1, -1
0, 0
-1, -1
0, 0
-1, -1
0, 0
-1, -1
0, 0
-1, -1
76, 142
-1, -1
76, 142
0, 0
142, 76
-1, -1
142, 76
-1, -1
0, 0
-1, -1
0, 0
-1, -1
0, 0
-1, -1
0, 0
...
...
...


还有800万个条目。文档说数组的第一个n = num_unique_elements应该是所有唯一值,但显然不是。 我的第二个问题 - 我想做类似的事情来切断唯一向量末尾的垃圾值:

T* new_end = thrust::unique(thrust::device, d_unique.begin(), d_unique.end());

std::vector<thrust::pair<int, int>> output(int(new_end));
thrust::copy(d_unique.begin(), d_unique.begin() + new_end, output.begin() );

但是该代码会引发错误('t'实际上不是代码的一部分,而是代表我拥有的许多不同类型到目前为止尝试了)。我有一种完全错误的方式要解决这个问题的两个部分,并且基于我在Google上找不到任何问题的事实,我认为答案是显而易见的。

I'm trying to make a unique thrust device vector of thrust::pair<int, int> using thrust::unique(), but the resulting output doesn't seem to be sorted like it should be. Here's the code in question:

struct make_pair : public thrust::binary_function<int, int, thrust::pair<int, int>> {
    __host__ __device__
        thrust::pair<int, int> operator()(int x, int y) { return thrust::make_pair(x, y); }
};



std::vector<thrust::pair<int, int>> find_borders_launch(int src_width, int src_height, cv::cuda::GpuMat& d_src) {

    thrust::device_vector<int> d_focus_result(src_width * src_height * 9);
    thrust::device_vector<int> d_target_result(src_width * src_height * 9);

    int* d_focus = thrust::raw_pointer_cast(d_focus_result.data());
    int* d_target = thrust::raw_pointer_cast(d_target_result.data());

    dim3 num_blocks = {uint(src_width + 1), uint(src_height + 1)};
    dim3 threads_per_block = {9, 1, 1};
    int substep_size = sizeof(int);

    find_borders_kernel <<<num_blocks, threads_per_block>>> (src_width, src_height, d_src.data, d_src.step, d_focus, d_target, substep_size);


    thrust::device_vector<thrust::pair<int, int>> d_unique(d_focus_result.size());


    thrust::transform(d_focus_result.begin(), d_focus_result.end(), d_target_result.begin(), d_unique.begin(), make_pair() ); //seems to do its job just fine

    thrust::unique(thrust::device, d_unique.begin(), d_unique.end());

    std::vector<thrust::pair<int, int>> output(d_unique.size());
    thrust::copy(d_unique.begin(), d_unique.end(), output.begin() );
    return output;
}

and the output looks something like:


0, 0
-1, -1
0, 0
-1, -1
0, 0
-1, -1
0, 0
-1, -1
0, 0
-1, -1
0, 0
-1, -1
0, 0
-1, -1
0, 0
-1, -1
0, 0
-1, -1
0, 0
-1, -1
0, 76
-1, -1
0, 76
0, 0
76, 0
-1, -1
76, 0
-1, -1
0, 0
-1, -1
0, 0
-1, -1
0, 0
-1, -1
0, 0
-1, -1
0, 0
-1, -1
76, 142
-1, -1
76, 142
0, 0
142, 76
-1, -1
142, 76
-1, -1
0, 0
-1, -1
0, 0
-1, -1
0, 0
-1, -1
0, 0
...
...
...


and so on for eight million more entries. The documentation says that the first N = num_unique_elements of the array should be all the unique values, but they're clearly not.
My second issue- I want to do something like this to cut off the garbage values at the end of the unique vector:

T* new_end = thrust::unique(thrust::device, d_unique.begin(), d_unique.end());

std::vector<thrust::pair<int, int>> output(int(new_end));
thrust::copy(d_unique.begin(), d_unique.begin() + new_end, output.begin() );

but that code throws an error ('T' is not actually part of the code but stands for many different types I have tried so far). I have a feeling I'm going about both parts of this question in the complete wrong way, and based on the fact that I can't find any questions about it on google, I assume the answer is obvious for reasons I am ignorant of.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

潜移默化 2025-02-03 21:52:54

解决了Paleonix的评论,解决了。在运行唯一的唯一返回值之前,我必须对数组进行排序,并且想要进入throust :: device_vector&lt; throust :: pair&lt&lt&int,int&gt;&gt; :: iterator。
如果将来有人遇到同样的问题,这是更改的代码。我敢肯定这是超级笨拙的,但是它有效。

    thrust::device_vector<thrust::pair<int, int>> d_unique(d_focus_result.size());

    thrust::transform(d_focus_result.begin(), d_focus_result.end(), d_target_result.begin(), d_unique.begin(), make_pair() ); //seems to do its job just fine

    thrust::sort(thrust::device, d_unique.begin(), d_unique.end());

    thrust::device_vector<thrust::pair<int, int>>::iterator new_end  = thrust::unique(thrust::device, d_unique.begin(), d_unique.end());

    int new_length = thrust::distance(d_unique.begin(), new_end);

    std::vector<thrust::pair<int, int>> output(new_length);

    thrust::copy(d_unique.begin(), d_unique.begin() + new_length, output.begin() );

    return output;

solved, thanks to paleonix's comments. I had to sort the array before running unique on it, and the return value of unique wanted to go into thrust::device_vector<thrust::pair<int, int>>::iterator.
here's the changed code if anyone in the future comes across the same problem. I'm sure it's super janky, but it works.

    thrust::device_vector<thrust::pair<int, int>> d_unique(d_focus_result.size());

    thrust::transform(d_focus_result.begin(), d_focus_result.end(), d_target_result.begin(), d_unique.begin(), make_pair() ); //seems to do its job just fine

    thrust::sort(thrust::device, d_unique.begin(), d_unique.end());

    thrust::device_vector<thrust::pair<int, int>>::iterator new_end  = thrust::unique(thrust::device, d_unique.begin(), d_unique.end());

    int new_length = thrust::distance(d_unique.begin(), new_end);

    std::vector<thrust::pair<int, int>> output(new_length);

    thrust::copy(d_unique.begin(), d_unique.begin() + new_length, output.begin() );

    return output;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文