我正在使用推力分区功能将数组划分为偶数和奇数.但是,当我尝试显示设备矢量时,它会显示随机值.请告诉我错误在哪里.我想我做的一切都是正确的.
#include<stdio.h>
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include<thrust/partition.h>
struct is_even
{
//const int toCom;
//is_even(int val):toCom(val){}
__device__
bool operator()(const int &x)
{
return x%2;
}
};
void main(){
thrust::host_vector<int> H(6);
for(int i =0 ; i<H.size();i++){
H[i] = i+1;
}
thrust::device_vector<int> D = H;
thrust::partition(D.begin(),D.end(),is_even());
for(int i =0 ;i< D.size();i++){
printf("%d,",D[i]);
}
getchar();
}
Run Code Online (Sandbox Code Playgroud) 我在这里得到一个恼人的消息,我不太确定我做错了什么.
float4 *IntsOnHost = new float4[ MAXX * (MAXY - 1) ];
//copy possible intersection points from device to host
CU_SAFE_CALL(cudaMemcpy(IntsOnHost,IntsOnDevToCpyToHost,(MAXX*(MAXY - 1)-1)*sizeof(float4),cudaMemcpyDeviceToHost));
thrust::device_vector<float4> IntsOnDev (IntsOnHost,IntsOnHost + (MAXX * (MAXY - 1)-1)*sizeof(float4));
//find the index of the smallest intersection point
thrust::device_vector<float4>::iterator it = thrust::min_element(IntsOnDev.begin(),IntsOnDev.end(),equalOperator());
Run Code Online (Sandbox Code Playgroud)
和谓词:
struct equalOperator
{
__host__ __device__
bool operator()(float4 x, float4 y)
{
return ( x.w > y.w );
}
};
Run Code Online (Sandbox Code Playgroud)
错误信息:
1> c:\ program files \nvidia gpu computing toolkit\cuda\v4.0\include\thrust\detail\device\generic\extrema.inl(104):error:function"equalOperator :: operator()"无法调用使用给定的参数列表
谢谢!
在C++中,为了创建一个包含10个整数向量的向量,我将执行以下操作:
std::vector< std::vector<int> > test(10);
Run Code Online (Sandbox Code Playgroud)
由于我认为Thrust使用与STL相同的逻辑,我尝试做同样的事情:
thrust::host_vector< thrust::host_vector<int> > test(10);
Run Code Online (Sandbox Code Playgroud)
但是我遇到了太多令人困惑的错误.我试过做:
thrust::host_vector< thrust::host_vector<int> > test;
Run Code Online (Sandbox Code Playgroud)
它工作,但我不能添加任何东西到这个向量.干
thrust::host_vector<int> temp(3);
test.push_back(temp);
Run Code Online (Sandbox Code Playgroud)
会给我同样的错误(太多了,不能粘贴在这里).
一般来说,使用Thrust时,它是否会影响使用host_vector和STL 之间的区别vector?
先感谢您
假设我有两个device_vector <byte>数组,d_keys并且d_data.
d_data例如,如果是扁平的2D 3x5阵列(例如{1,2,3,4,5,6,7,8,9,8,7,6,5,4,3})并且d_keys是1D阵列大小为5(例如{1,0,0,1,1}),我如何进行减少,这样如果相应的d_keys值为1 ,我最终只会按行添加值(例如,结束结果为{10,23,14})?
该sum_rows.cu例如允许我加入的每一个值d_data,但是这并不完全正确.
或者,我可以在每行的基础上使用a zip_iterator并一次合并d_keys一行d_data,然后执行a transform_reduce,仅在键值为1时添加,但是我必须循环遍历d_data数组.
我真正需要的是某种transform_reduce_by_key不是内置的功能,但肯定必须有一种方法来实现它!
我正在编写一个matrix模板类,可以打印到两个文件std::cout,即:
matrix<float> myMat;
...
myMat.cout(...) // print to std::cout
myMat.write("out.txt") // print to file
Run Code Online (Sandbox Code Playgroud)
两者都将共享一个共同的底层打印功能,我也试图将其作为模板实现,因为我已经看到了用于thrust::copy向两者std::cout和文件写入数据的不同示例.
下面是我所做的骨架,但它目前正在输出垃圾.任何人都可以指出我可能犯的一些错误吗?例如,我允许这样传球std::cout吗?
template <typename data_T> matrix {
...
template <typename out_T> int printTo(out_T &out, ...) {
data_T *start = ..., *end = ...;
...
thrust::copy(start, end, std::ostream_iterator<data_T>(out, " "));
...
}
int cout(...) {
...
printTo(std::cout, ...);
...
}
int write(char* path, ...) {
...
std::ofstream file;
file.open(path);
printTo(file, ...);
...
} …Run Code Online (Sandbox Code Playgroud) 我想用CUDA Thrust使用我的两张显卡进行计算.
我有两张图形卡.即使我在std :: vector中存储两个device_vectors,在单张卡上运行也能很好地适用于这两种卡.
如果我同时使用两个卡,则循环中的第一个循环起作用并且不会导致错误.第一次运行后,它会导致错误,可能是因为设备指针无效.
我不确定究竟是什么问题,或者如何使用两张卡进行计算.
最小代码示例:
std::vector<thrust::device_vector<float> > TEST() {
std::vector<thrust::device_vector<float> > vRes;
unsigned int iDeviceCount = GetCudaDeviceCount();
for(unsigned int i = 0; i < iDeviceCount; i++) {
checkCudaErrors(cudaSetDevice(i) );
thrust::host_vector<float> hvConscience(1024);
// first run works, runs afterwards cause errors ..
vRes.push_back(hvConscience); // this push_back causes the error on exec
}
return vRes;
}
Run Code Online (Sandbox Code Playgroud)
执行时出错:
terminate called after throwing an instance of 'thrust::system::system_error'
what(): invalid argument
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用CUDA Thrust来解决问题.
我有一个包含3元素的主机数组.是否有可能使用Thrust创建一个384元素的设备数组,其中3主机数组中的元素重复128次数(128 x 3 = 384)?
一般来说,从一个3元素数组开始,我如何使用Thrust生成一个大小的设备数组X,其中X = Y x 3,即Y重复次数是多少?
我试图在cuda中使用基数排序.在研究期间,我发现推力具有排序功能.
which sorting algorithm does thrust library use for sorting??
Is it Radix Sort???
Run Code Online (Sandbox Code Playgroud) 看看CUDA Thrust代码中的内核启动,它们似乎总是使用默认流.我可以让Thrust使用我选择的流吗?我错过了API中的内容吗?
我有一个5000x500矩阵,我想用cuda分别对每行进行排序.我可以使用arrayfire,但这只是关于thrust :: sort的for循环,这应该不高效.
https://github.com/arrayfire/arrayfire/blob/devel/src/backend/cuda/kernel/sort.hpp
for(dim_type w = 0; w < val.dims[3]; w++) {
dim_type valW = w * val.strides[3];
for(dim_type z = 0; z < val.dims[2]; z++) {
dim_type valWZ = valW + z * val.strides[2];
for(dim_type y = 0; y < val.dims[1]; y++) {
dim_type valOffset = valWZ + y * val.strides[1];
if(isAscending) {
thrust::sort(val_ptr + valOffset, val_ptr + valOffset + val.dims[0]);
} else {
thrust::sort(val_ptr + valOffset, val_ptr + valOffset + val.dims[0],
thrust::greater<T>());
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法融合推力操作,以便排序并行?实际上,我正在寻找的是融合循环迭代的通用方法.