给出以下代码片段,使用推力生成一种带CUDA的代码字典(CUDA的C++模板库):
thrust::device_vector<float> dCodes(codes->begin(), codes->end());
thrust::device_vector<int> dCounts(counts->begin(), counts->end());
thrust::device_vector<int> newCounts(counts->size());
for (int i = 0; i < dCodes.size(); i++) {
float code = dCodes[i];
int count = thrust::count(dCodes.begin(), dCodes.end(), code);
newCounts[i] = dCounts[i] + count;
//Had we already a count in one of the last runs?
if (dCounts[i] > 0) {
newCounts[i]--;
}
//Remove
thrust::detail::normal_iterator<thrust::device_ptr<float> > newEnd = thrust::remove(dCodes.begin()+i+1, dCodes.end(), code);
int dist = thrust::distance(dCodes.begin(), newEnd);
dCodes.resize(dist);
newCounts.resize(dist);
}
codes->resize(dCodes.size());
counts->resize(newCounts.size());
thrust::copy(dCodes.begin(), dCodes.end(), codes->begin());
thrust::copy(newCounts.begin(), newCounts.end(), counts->begin());
Run Code Online (Sandbox Code Playgroud)
问题是,通过使用CUDA视觉分析器,我注意到4个字节的多个副本.IMO这是由生成的
我正试图通过device_vector结构
struct point
{
unsigned int x;
unsigned int y;
}
Run Code Online (Sandbox Code Playgroud)
以下列方式执行某项功能:
void print(thrust::device_vector<point> &points, unsigned int index)
{
std::cout << points[index].y << points[index].y << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
myvector已正确初始化
print(myvector, 0);
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
error: class "thrust::device_reference<point>" has no member "x"
error: class "thrust::device_reference<point>" has no member "y"
Run Code Online (Sandbox Code Playgroud)
它出什么问题了?
我有一个640*480的向量,其中包含一组数字,我希望找到向量的每一行的最小和最大数量.
for(int i = 0; i < R; i++)
{
Begin = m_valBuffer.begin() + (i*C);
End = Begin+C;
rMinmax= minmax_element(Begin, End);
}
Run Code Online (Sandbox Code Playgroud)
然而,这是非常缓慢的,有什么办法可以加快速度吗?
我目前正在尝试使用CUDA来评估代表指数移动平均滤波器的差分方程.滤波器由以下差分方程描述
y[n] = y[n-1] * beta + alpha * x[n]
Run Code Online (Sandbox Code Playgroud)
其中alpha和beta常量定义为
alpha = (2.0 / (1 + Period))
beta = 1 - alpha
Run Code Online (Sandbox Code Playgroud)
如何操纵上述差分方程以获得该滤波器的系统响应?在GPU上实现此过滤器的有效方法是什么?
我正在研发GTX 570.
我有一个Cuda C++代码,它使用Thrust目前在单个GPU上正常工作.我现在想修改它为multi-gpu.我有一个主机功能,包括许多Thrust调用,可以对设备阵列进行排序,复制,计算差异等.我想使用每个GPU同时在它自己的(独立的)数组上运行这个Thrust调用序列.我已经读过返回值的Thrust函数是同步的,但是我可以使用OpenMP让每个主机线程调用一个在单独的GPU上运行的函数(使用Thrust调用)吗?
例如(在浏览器中编码):
#pragma omp parallel for
for (int dev=0; dev<Ndev; dev++){
cudaSetDevice(dev);
runthrustfunctions(dev);
}
void runthrustfunctions(int dev){
/*lots of Thrust functions running on device arrays stored on corresponding GPU*/
//for example this is just a few of the lines"
thrust::device_ptr<double> pos_ptr = thrust::device_pointer_cast(particle[dev].pos);
thrust::device_ptr<int> list_ptr = thrust::device_pointer_cast(particle[dev].list);
thrust::sequence(list_ptr,list_ptr+length);
thrust::sort_by_key(pos_ptr, pos_ptr+length,list_ptr);
thrust::device_vector<double> temp(length);
thrust::gather(list_ptr,list_ptr+length,pos_ptr,temp.begin());
thrust::copy(temp.begin(), temp.end(), pos_ptr);
Run Code Online (Sandbox Code Playgroud)
}`
我想我还需要将结构"particle [0]"存储在GPU 0上,粒子[1]存储在GPU 1等上,我猜这是不可能的.一个选项可能是为每个GPU案例使用"switch"和单独的代码.
我想知道这是一种正确的方法,还是有更好的方法?谢谢
我的计算结果遇到了一些麻烦,由于某种原因它们不正确,我检查了代码,看起来是正确的(虽然我会再次检查).
我的问题是,在调用推力后启动后,自定义cuda内核是同步还是异步,例如
thrust::sort_by_key(args);
arrangeData<<<blocks,threads>>>(args);
Run Code Online (Sandbox Code Playgroud)
完成arrangeData之后内核会运行thrust::sort吗?
如何将包含交错浮点数的设备数组转换为推力矢量运算的CUDA推力元组.
目的:我使用CUDA上的Marching Cubes生成一个粗略的顶点列表.输出是顶点列表,具有冗余且无连接.我希望得到一个唯一顶点的列表,然后得到这些独特顶点的索引缓冲区,所以我可以执行一些操作,如网格简化等...
float *devPtr; //this is device pointer that holds an array of floats
//6 floats represent a vertex, array size is vertsCount*6*sizeof(float).
//format is [v0x, v0y, v0z, n0x, n0y, n0z, v1x, v1y, v1z, n1x, ...]
typedef thrust::tuple<float, float, float, float, float, float> MCVertex;
thrust::device_vector<MCVertex> inputVertices(vertsCount);
//copy from *devPtr to inputVertices.
//use something like unique to get rid of redundancies.
thrust::unique(inputVertices.begin(), inputVertices.end());
Run Code Online (Sandbox Code Playgroud)
我如何实现副本,还是有其他更好的方法来做到这一点?
我正在尝试减少为我的用例计算reduce_by_key所需的内存.与值的数量(约1600万)相比,我有一个相对较少的唯一键(大约100-150).按键缩减示例显示分配给包含结果的device_vectors与输入的大小相同.是否总是有必要这样做?是否可以只分配尽可能多的内存来包含正确的输出?
我有一个CUDA程序,它使用thrust :: reduce来并行化和:例如,
thrust::device_ptr<double> tmp(aux);
double my_sum = thrust::reduce(tmp, tmp + G);
Run Code Online (Sandbox Code Playgroud)
在设备上double* aux指向G连续的双打.我需要将整个并行化程序的运行时间与没有并行计算的版本进行比较.有没有办法thrust::reduce在设备上只使用一个线程运行?全局转换将是最方便的选择.
我也在我的机器上安装了cuda 8.0(Linux SL7),我已经下载了推力1.8.1并用新的1.8.1替换了现有的推力库.
据我所知,从推力1.8开始支持并可以在内核中使用.我引用他们的网站:
Thrust 1.8.0引入了对CUDA __device__代码的算法调用的支持,对CUDA流的支持以及算法性能的改进.用户现在可以从CUDA __device__代码调用Thrust算法
但是,当我使用Nsight eclipse构建应用程序时,它会向我显示以下错误:
不允许从__global__函数("mykernel")调用__host__函数("thrust :: sort").
请问有什么建议吗?
这是我的代码:
#include <iostream>
#include <numeric>
#include <stdlib.h>
#include <stdio.h>
#include <cuda_runtime.h>
#include <cuda.h>
#include <thrust/sort.h>
#include <thrust/execution_policy.h>
__global__ void mykernel(int* a, int* b)
{
thrust::sort(a, a + 10);
}
int main(void)
{
int a[10] = { 0, 9, 7, 3, 1, 6, 4, 5, 2, 8 };
int b[10];
int *d_a, *d_c;
cudaMalloc((void**)&d_a, 10 * sizeof(int));
cudaMalloc((void**)&d_c, 10 * sizeof(int));
std::cout << "A\n";
for (int i …Run Code Online (Sandbox Code Playgroud)