我正在尝试使用CUDA的推力库在CUDA中修改一个简单的动态矢量.但是我在屏幕上出现"launch_closure_by_value"错误,表明错误与某些同步过程有关.
由于此错误,无法进行简单的1D动态数组修改.
导致错误的我的代码段如下.
从.cpp文件我调用setIndexedGrid,它在System.cu中定义
float* a= (float*)(malloc(8*sizeof(float)));
a[0]= 0; a[1]= 1; a[2]= 2; a[3]= 3; a[4]= 4; a[5]= 5; a[6]= 6; a[7]= 7;
float* b = (float*)(malloc(8*sizeof(float)));
setIndexedGridInfo(a,b);
Run Code Online (Sandbox Code Playgroud)
System.cu的代码段:
void
setIndexedGridInfo(float* a, float*b)
{
thrust::device_ptr<float> d_oldData(a);
thrust::device_ptr<float> d_newData(b);
float c = 0.0;
thrust::for_each(
thrust::make_zip_iterator(thrust::make_tuple(d_oldData,d_newData)),
thrust::make_zip_iterator(thrust::make_tuple(d_oldData+8,d_newData+8)),
grid_functor(c));
}
Run Code Online (Sandbox Code Playgroud)
grid_functor在_kernel.cu中定义
struct grid_functor
{
float a;
__host__ __device__
grid_functor(float grid_Info) : a(grid_Info) {}
template <typename Tuple>
__device__
void operator()(Tuple t)
{
volatile float data = thrust::get<0>(t);
float pos = data + 0.1;
thrust::get<1>(t) …Run Code Online (Sandbox Code Playgroud) 如果我的机器没有CUDA GPU,我可以在OpenMP设备系统中使用Thrust吗?如果是这样,我还需要CUDA工具包吗?
在CUDA Thrust变换中,是否可以获取向量的索引,传递给函数内的operator()函数?
说,我们有,
struct op{
float operator()(const float& f){
//do something like return the index
}
};
vector<float> v(100);
thrust::transform(v.begin(),v.end(),v.begin(),op());
Run Code Online (Sandbox Code Playgroud)
如何在operator()中获取向量的索引?基本上我想要一个简单的方法在CUDA中制作一个单位矩阵.
我无法理解counting_iteratorCUDA 的推力库。它的目的是什么以及如何使用?它在其他编程语言(例如 C++)中也可用吗?
为什么下面的代码会在 main 的末尾崩溃?
#include <thrust/device_vector.h>
thrust::device_vector<float4> v;
int main(){
v.resize(1000);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误是:
terminate called after throwing an instance of 'thrust::system::system_error'
what(): unspecified driver error
Run Code Online (Sandbox Code Playgroud)
如果我使用host_vector而不是device_vector代码运行良好。
你认为这是一个推力错误,还是我在这里做错了什么?
我在带有 cuda 4.0 的 ubuntu 10.10 和带有 cuda 6.5 的 Windows 7 上尝试过。在这两种情况下,Thrust 版本都是 1.7。
谢谢
我需要计算
(a & b).count()
在大集合(> 10000)位向量(std::bitset<N>)中,其中N在2 ^ 10到2 ^ 16之间.
const size_t N = 2048;
std::vector<std::vector<char>> distances;
std::vector<std::bitset<N>> bits(100000);
load_from_file(bits);
for(int i = 0; i < bits.size(); i++){
for(int j = 0; j < bits.size(); j++){
distance[i][j] = (bits[i] & bits[j]).count();
}
}
Run Code Online (Sandbox Code Playgroud)
目前我依靠分块多线程和SSE/AVX来计算distances.幸运的是,我可以使用vpandAVX来计算&但我的代码仍在使用popcnt (%rax)并且循环来计算位数.
有没有办法(a & b).count()在GPU(nVidia 760m)上计算功能?理想情况下,我只会传递2块内存N.我正在寻找使用推力,但我找不到popcnt功能.
当前的CPU实现.
double validate_pooled(const size_t K) const{
int right = 0;
const size_t num_examples = …Run Code Online (Sandbox Code Playgroud) 我正在尝试在CUDA中做一个非常基本的例子。我想对浮点数列表进行简单的计算。
vh [x] * k1 + k2
目前,我正在尝试此操作,但无法正常工作:
代码1
#include <vector>
#include <iostream>
#include <thrust/transform.h>
#include <thrust/functional.h>
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
using namespace std;
using namespace thrust;
float k1 = 42, k2 = 7;
int main(void)
{
vector<float> vh = { 0, 1, 2, 3, 4, 5, 6, 7 };
device_vector<float> v = vh;
device_vector<float> v_out(v.size());
thrust::transform(v.begin(), v.end(), v_out.begin(), [=] __device__(float x) {
return x*k1 + k2;
});
for (size_t i = 0; i < v_out.size(); i++)
std::cout << v_out[i] …Run Code Online (Sandbox Code Playgroud) 如果我有两个cudaMalloced 数组,我可以通过简单地交换相关指针来交换它们而无需内存移动。
如果我有两个 CUDA Thrust device_vectors,比如说d_a和d_b,我可以使用第三个临时向量来交换它们,比如说d_c,但这将需要内存移动。
我的问题是:有没有办法在不移动内存的情况下交换 CUDA Thrust device_vectors?
我试图以这种方式访问向量元素
struct point
{
unsigned int x;
unsigned int y;
};
...
thrust::device_vector<point> devPoints(hPoints.begin(), hPoints.end());
for(thrust::device_vector<point>::iterator iter = devPoints.begin(); iter != devPoints.end(); iter++)
{
std::cout << iter->x << " " << iter->y << " " << std::endl; (1)
}
Run Code Online (Sandbox Code Playgroud)
device_vector已正确初始化.我收到以下错误:
error: expression must have pointer type (at 1)
error: no suitable user-defined conversion from "const thrust::detail::normal_iterator<thrust::device_ptr<point>>" to "thrust::device_ptr<point>" exists
detected during instantiation of "Pointer thrust::experimental::iterator_facade<Derived, Pointer, Value, Space, Traversal, Reference, Difference>::operator->() const [with Derived=thrust::detail::normal_iterator<thrust::device_ptr<point>>, Pointer=thrust::device_ptr<point>, Value=point, Space=thrust::detail::cuda_device_space_tag, Traversal=thrust::random_access_traversal_tag, Reference=thrust::device_reference<point>, Difference=ptrdiff_t]" …Run Code Online (Sandbox Code Playgroud) 我想使用Thrust库来计算CUDA中设备数组的前缀和.我的数组已分配cudaMalloc().我的要求如下:
main()
{
Launch kernel 1 on data allocated through cudaMalloc()
// This kernel will poplulate some data d.
Use thrust to calculate prefix sum of d.
Launch kernel 2 on prefix sum.
}
Run Code Online (Sandbox Code Playgroud)
我想在我的内核之间的某处使用Thrust所以我需要方法将指针转换为设备迭代器并返回.下面的代码有什么问题?
int main()
{
int *a;
cudaMalloc((void**)&a,N*sizeof(int));
thrust::device_ptr<int> d=thrust::device_pointer_cast(a);
thrust::device_vector<int> v(N);
thrust::exclusive_scan(a,a+N,v);
return 0;
}
Run Code Online (Sandbox Code Playgroud)