【发布时间】:2021-03-13 17:52:15
【问题描述】:
我想将 cv::cuda::GpuMat 传递给推力函数,我发现 this function 可用于创建索引 cv::cuda::GpuMat 的推力迭代器。我在以下代码中有 2 个问题:
#include <stdio.h>
#include<io.h>
#include <stdlib.h>
#include <fstream>
#include<string>
#include<iostream>
#include <time.h>
#include <vector>
#include <cuda_runtime.h>
#include "device_launch_parameters.h"
#include <thrust/host_vector.h>
#include<thrust/functional.h>
#include <thrust/device_vector.h>
#include <thrust/generate.h>
#include <thrust/sort.h>
#include <thrust/copy.h>
#include<opencv2/opencv.hpp>
#include<cudaimgproc.hpp>
#include<opencv2/cudafilters.hpp>
#include<opencv2/cudaarithm.hpp>
template<typename T>
struct greater_than_value_pred
{
T value;
greater_than_value_pred(T value) : value(value) {}
__host__ __device__
bool operator()(T v) const { return v > value; }
};
using namespace std;
int main()
{
int compare_vari = 20;
cv::cuda::GpuMat d_data_open(1, 100, CV_32SC2);
// Thrust compatible begin and end iterators to channel 0 of this matrix
auto idxBegin = GpuMatBeginItr<int>(d_data_open, 1);
auto idxEnd = GpuMatEndItr<int>(d_data_open, 1);
// Fill the index channel with a sequence of numbers from 0 to 100
thrust::sequence(idxBegin, idxEnd);
d_iter = thrust::find_if(idxBegin, idxEnd, greater_than_value_pred<int>(compare_vari));
/*Use thrust::device_data to test */
thrust::device_vector<int> d_thrust_data;
thrust::device_vector<int>::iterator d_iter;
thrust::sequence(d_thrust_data.begin(), d_thrust_data.end());
d_iter = thrust::find_if(d_thrust_data.begin(), d_thrust_data.end(), greater_than_value_pred<int>(compare_vari));
//std::cout << *d_iter << std::endl;
}
问题 1:
此代码无法遵守:d_iter = thrust::find_if(idxBegin, idxEnd, greater_than_value_pred<double>(compare_vari));
使用 idxBegin 和 idxEnd 可以很好地与thrust::sequence()、sort() 等一起使用,但是当将它们应用于thrust::find_if() 时,它会提示“没有运算符'=' 与这些操作数匹配”。
问题 2:
*//std::cout << *d_iter << std::endl;*
我想通过上面的代码检查结果,但是在调试时它说 abort() 已被调用。我想知道 d_iter 是否在设备内存中,如果是,我需要将其复制到主机内存中吗?但是我不知道如何将迭代器复制到主机内存中。
任何帮助将不胜感激。
【问题讨论】: