标签: thrust

是否存在一些 Thrust::device_vector 等效库,可以在 CUDA 内核中使用?

Throw::device_vector 的自动内存管理确实很有用,唯一的缺点是无法在内核代码中使用它。

我在互联网上查找,刚刚找到了矢量库,例如推力,它处理来自主机代码的设备内存。是否存在内核向量库?如果没有,拥有这样一个图书馆是不是一个坏主意?

c++ cuda vector device thrust

1
推荐指数
1
解决办法
770
查看次数

混合 Thrust 和 cuBLAS 会产生意外的输出结果

我喜欢推力库,尤其是它如何很好地隐藏了 cudaMalloc、cudaFree 等的复杂性。

我想对矩阵的所有列求和。所以我使用了 cuBlas 的“cublasSgemv”并将我的矩阵乘以一个向量。这是我的代码:

void sEarColSum(std::vector<float>& inMatrix, int colSize)
{
    cublasHandle_t handle; // CUBLAS context
    float al = 1.0f; // al =1
    float bet = 1.0f; // bet =1
    int rowSize = inMatrix.size() / colSize;

    float *devOutputPtr = thrust::raw_pointer_cast(thrust::device_malloc<float>(colSize));

    thrust::device_vector<float> deviceT2DMatrix(inMatrix.begin(), inMatrix.end());
    float* device2DMatrixPtr = thrust::raw_pointer_cast(deviceT2DMatrix.data());

    thrust::device_vector<float> deviceVector(rowSize, 1.0f);
    float* deviceVecPtr = thrust::raw_pointer_cast(deviceVector.data());

    cublasCreate(&handle);
    cublasSgemv(handle, CUBLAS_OP_N, colSize, rowSize, &al, device2DMatrixPtr, colSize, deviceVecPtr, 1, &bet, devOutputPtr, 1);

    std::vector<float> outputVec(colSize);
    cudaMemcpy(outputVec.data(), devOutputPtr, outputVec.size() * sizeof(float), cudaMemcpyDeviceToHost);

    for (auto elem : …
Run Code Online (Sandbox Code Playgroud)

c++ cuda thrust cublas

1
推荐指数
1
解决办法
667
查看次数

Thrust转换抛出错误:"bulk_kernel_by_value:遇到非法内存访问"

我对CUDA/Thrust很新,并且在代码片段方面存在问题.为了使它更容易,我把它修剪到最低限度.代码如下:

struct functor{
functor(float (*g)(const float&)) : _g{g} {}

__host__ __device__ float operator()(const float& x) const { 
        return _g(x);
    }
private:
    float (*_g)(const float&);
};

__host__ __device__ float g(const float& x){return 3*x;}

int main(void){
thrust::device_vector<float> X(4,1);
thrust::transform(X.begin(), X.end(), X.begin(), functor(&g));
}
Run Code Online (Sandbox Code Playgroud)

我的想法是我可以将任何函数传递给仿函数,因此我可以将该函数应用于Vector中的每个元素.不幸的是,我不确定为什么我会得到描述的错误.我编译-w -O3 -shared -arch=sm_20 -std=c++11 -DTHRUST_DEBUG

我很感谢你能给我的任何帮助:)

c++ cuda thrust c++11

1
推荐指数
1
解决办法
778
查看次数

使用 CUDA 减少排列在大向量中的多个等长块

我正在寻找一种快速方法来减少排列为大向量的多个相同长度的块。我有 N 个子数组(连续元素),它们排列在一个大数组中。每个子数组都有固定的大小:k。所以整个数组的大小是:N*K

我正在做的是调用内核N次。每次它计算子数组的减少如下:我将迭代大向量中包含的所有子数组:

    for(i=0;i<N;i++){
       thrust::device_vector< float > Vec(subarray, subarray+k);
       float sum = thrust::reduce(Vec.begin(), Vec.end(), (float)0, thrust::plus<float>());
       printf("sum %f\n",sum);
 }
Run Code Online (Sandbox Code Playgroud)

对于纯 CUDA 我会这样做(伪代码):

 for(i=0;i<N;i++){
        reduction_kernel(subarray)

         }
Run Code Online (Sandbox Code Playgroud)

您是否有另一种解决方案来一次性执行连续子数组的缩减?使用纯 CUDA 或 Thrust

cuda gpgpu nvidia reduction thrust

1
推荐指数
1
解决办法
1275
查看次数

通过构造函数将 device_vector 存储在函子中?

我正在尝试将 a 存储thrust::device_vector在函子内。简单的解释如下:

struct StructOperator : public thrust::unary_function<float, int>  {
  int num_;
  thrust::device_vector<int> v_test;

  explicit StructOperator(thrust::device_vector<int> const& input_v) :
    v_test(input_v), num_(input_v.size()) {};

  __host__ __device__
   float operator()(int index) {
      // magic happens
   }
};
Run Code Online (Sandbox Code Playgroud)

无法编译 -nvcc一直说不允许__host__从 a调用 a __host__ __device__。我见过这个问题 - 这是实现这一目标的唯一方法吗?

cuda functor thrust

1
推荐指数
1
解决办法
173
查看次数

CUDA:如何使用推力进行矩阵乘法?

我是 CUDA 和 Thrust 的新手,我正在尝试实现矩阵乘法,我想仅使用推力算法来实现这一点,因为我想避免手动调用内核。

有没有办法可以有效地实现这一目标?(至少没有使用 2 个嵌套的 for 循环)

还是我必须辞职并调用 CUDA 内核?

//My data
thrust::device_vector<float> data(n*m);
thrust::device_vector<float> other(m*r);
thrust::device_vector<float> result(n*r);

// To make indexing faster, not really needed
transpose(other);

// My current approach
for (int i = 0; i < n; ++i)
{
   for (int j = 0; j < r;++j)
   {
       result[i*r+ j] = thrust::inner_product(data.begin()+(i*m), data.begin()+((i+1)*m),other+(j*m), 0.0f);
   }
}
Run Code Online (Sandbox Code Playgroud)

c++ cuda thrust

1
推荐指数
1
解决办法
1999
查看次数

如何知道推力的结果中有多少个元素::partition_copy

我正在尝试使用推力库的 partition_copy 函数对数组进行分区。

我看过传递指针的例子,但我需要知道每个分区中有多少元素。

我尝试过的是将设备向量作为 OutputIterator 参数传递,如下所示:

#include <thrust/device_vector.h>
#include <thrust/device_ptr.h>
#include <thrust/partition.h>

struct is_even {
    __host__ __device__ bool operator()(const int &x) {
        return (x % 2) == 0;
    }
};

int N;
int *d_data;
cudaMalloc(&d_data, N*sizeof(int));

//... Some data is put in the d_data array

thrust::device_ptr<int> dptr_data(d_data);

thrust::device_vector<int> out_true(N);
thrust::device_vector<int> out_false(N);

thrust::partition_copy(dptr_data, dptr_data + N, out_true, out_false, is_even());
Run Code Online (Sandbox Code Playgroud)

当我尝试编译时出现此错误:

error: class "thrust::iterator_system<thrust::device_vector<int, thrust::device_allocator<int>>>" has no member "type"
      detected during instantiation of "thrust::pair<OutputIterator1, OutputIterator2> thrust::partition_copy(InputIterator, InputIterator, OutputIterator1, OutputIterator2, Predicate) [with …
Run Code Online (Sandbox Code Playgroud)

c++ cuda gpu thrust

1
推荐指数
1
解决办法
158
查看次数

推力for_each示例没有运行

我正在尝试运行http://docs.thrust.googlecode.com/hg/group__modifying.html中描述的每个示例,但在编译和运行时遇到错误.

我使用以下文件:fe.cu:

#include <thrust/for_each.h>
#include <thrust/device_vector.h>
#include <stdio.h>

struct printf_functor{
    __host__ __device__
    void operator()(int x){
        printf("%d\n");
    }
};

int main(){
    thrust::device_vector<int> d_vec(3);
    d_vec[0] = 0; d_vec[1] = 1; d_vec[2] = 2;
    thrust::for_each(d_vec.begin(), d_vec.end(), printf_functor());
}
Run Code Online (Sandbox Code Playgroud)

我编译nvcc -arch=sm_20 fe.cu.

当我使用./a.out运行时,我得到以下输出:

terminate called after throwing an instance of 'thrust::system::system_error'
  what():  unspecified launch failure
Aborted
Run Code Online (Sandbox Code Playgroud)

以下是用于运行代码的GPU上的一些信息:

   --- General Information for device 0 ---
Name:  Tesla C2075
Compute capability:  2.0
Clock rate:  1147000
Device copy overlap:  Enabled
Kernel execution timeout …
Run Code Online (Sandbox Code Playgroud)

cuda thrust

0
推荐指数
1
解决办法
926
查看次数

使用nvcc编译器使用-G参数进行编译时,GPU性能不佳

我正在做一些测试,我意识到在编译时使用-G参数会给我一个糟糕的表现,而不是没有它.

我查看了Nvidia的文档:

--device-debug (-G)                         
    Generate debug information for device code. 
Run Code Online (Sandbox Code Playgroud)

但它并没有帮助我知道为什么给我这么糟糕的表现.它在哪里产生这个调试信息?何时?这可能是造成这种糟糕表现的原因?

debugging performance gpu nvcc thrust

0
推荐指数
1
解决办法
686
查看次数

在不同的GPU上初始化struct

我有一个看起来像这样的结构

struct LstmLayer {
  int deviceId;
  thrust::device_vector <real_t> W;
  thrust::device_vector <real_t> gradW;

  LstmLayer() : deviceId(0) {}

  LstmLayer(int __deviceId__) : deviceId(__deviceId__) {}

  void setDevice(int __deviceId__) { deviceId = __deviceId__; }

  void init(bool initParams) {
    W.resize(4*lstmSize * 2*lstmSize);
    gradW.resize(4*lstmSize * 2*lstmSize);

    if (initParams) GPU_Random_Vector(W);
  }
}
Run Code Online (Sandbox Code Playgroud)

现在我想初始化一个数组LstmLayer,每个元素都在不同的GPU设备上.我这样做如下

  struct LstmLayer lstmLayers[MAX_NUM_LSTM_LAYERS];

  for (int i = 0; i < numLstmLayers; ++i) {
    CUDA_SAFE_CALL(cudaSetDevice(i));
    lstmLayers[i].setDevice(i);
    lstmLayers[i].init(true);
  }
Run Code Online (Sandbox Code Playgroud)

运行此程序会出现以下错误

terminate called after throwing an instance of 'thrust::system::system_error'
  what():  driver shutting down
Run Code Online (Sandbox Code Playgroud)

请告诉我我的代码有什么问题以及如何正确执行?先谢谢你.

c c++ cuda thrust multiple-gpu

0
推荐指数
1
解决办法
311
查看次数

标签 统计

thrust ×10

cuda ×9

c++ ×6

gpu ×2

c ×1

c++11 ×1

cublas ×1

debugging ×1

device ×1

functor ×1

gpgpu ×1

multiple-gpu ×1

nvcc ×1

nvidia ×1

performance ×1

reduction ×1

vector ×1