gpt4 book ai didi

cuda - 从 device_vector 中删除元素

转载 作者:行者123 更新时间:2023-12-03 20:21:10 27 4
gpt4 key购买 nike

推力::device_vector 值

推力::设备向量键;

初始化后,keys 包含一些等于 -1 的元素。我想删除键中的元素和值的相同位置。

但是不知道水货怎么处理呢?

最佳答案

可能有很多方法可以做到这一点。一种可能的方式:

  • 使用 thrust::remove_if 的模板版本( documentation ),以键为模板,删除值中对应键为 -1 的元素。您将需要为谓词测试创建一个仿函数。
  • 使用 thrust::remove ( documentation ) 删除为 -1
  • 的值

    下面是一个例子:
    #include <iostream>
    #include <thrust/device_vector.h>
    #include <thrust/copy.h>
    #include <thrust/remove.h>
    #include <thrust/sequence.h>

    #define N 12
    typedef thrust::device_vector<int>::iterator dintiter;

    struct is_minus_one
    {
    __host__ __device__
    bool operator()(const int x)
    {
    return (x == -1);
    }
    };

    int main(){

    thrust::device_vector<int> keys(N);
    thrust::device_vector<int> values(N);

    thrust::sequence(keys.begin(), keys.end());
    thrust::sequence(values.begin(), values.end());

    keys[3] = -1;
    keys[9] = -1;

    dintiter nve = thrust::remove_if(values.begin(), values.end(), keys.begin(), is_minus_one());
    dintiter nke = thrust::remove(keys.begin(), keys.end(), -1);

    std::cout << "results values:" << std::endl;
    thrust::copy(values.begin(), nve, std::ostream_iterator<int>( std::cout, " "));
    std::cout << std::endl << "results keys:" << std::endl;
    thrust::copy(keys.begin(), nke, std::ostream_iterator<int>( std::cout, " "));
    std::cout << std::endl;

    return 0;
    }

    关于cuda - 从 device_vector 中删除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17593424/

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