在用户编写的内核中推动

Ash*_*ppa 38 cuda thrust

我是Thrust的新手.我看到所有Thrust演示文稿和示例仅显示主机代码.

我想知道我是否可以将device_vector传递给我自己的内核?怎么样?如果是,内核/设备代码中允许的操作是什么?

tal*_*ies 50

正如它最初编写的那样,Thrust纯粹是一个主机端抽象.它不能在内核中使用.您可以将封装在内部的设备内存传递thrust::device_vector给您自己的内核,如下所示:

thrust::device_vector< Foo > fooVector;
// Do something thrust-y with fooVector

Foo* fooArray = thrust::raw_pointer_cast( &fooVector[0] );

// Pass raw array and its size to kernel
someKernelCall<<< x, y >>>( fooArray, fooVector.size() );
Run Code Online (Sandbox Code Playgroud)

你还可以使用push :: device_ptr和裸cuda设备内存指针来实现推力算法中未通过推力分配的设备内存.

编辑四年半后,根据@ JackOLantern的回答,推文1.8增加了一个顺序执行策略,这意味着你可以在设备上运行推力算法的单线程版本.注意,仍然不可能将推力设备向量直接传递到内核,并且设备向量不能直接用在设备代码中.

请注意,thrust::device在某些情况下,还可以使用执行策略将内核作为子网格启动并行推力执行.这需要单独的编译/设备链接和支持动态并行的硬件.我不确定这是否实际上支持所有推力算法,但肯定适用于某些算法.

  • 有可能的.在talonmies的例子中,`someKernelCall`可以修改`fooArray`.请注意,`fooArray`对应于`fooVector`中包含的数据. (3认同)

Jac*_*ern 16

这是我之前回答的更新.

从Thrust 1.8.1开始,CUDA Thrust原语可以与thrust::device执行策略结合,在利用CUDA 动态并行性的单个CUDA线程中并行运行.下面是一个例子.

#include <stdio.h>

#include <thrust/reduce.h>
#include <thrust/execution_policy.h>

#include "TimingGPU.cuh"
#include "Utilities.cuh"

#define BLOCKSIZE_1D    256
#define BLOCKSIZE_2D_X  32
#define BLOCKSIZE_2D_Y  32

/*************************/
/* TEST KERNEL FUNCTIONS */
/*************************/
__global__ void test1(const float * __restrict__ d_data, float * __restrict__ d_results, const int Nrows, const int Ncols) {

    const unsigned int tid = threadIdx.x + blockDim.x * blockIdx.x;

    if (tid < Nrows) d_results[tid] = thrust::reduce(thrust::seq, d_data + tid * Ncols, d_data + (tid + 1) * Ncols);

}

__global__ void test2(const float * __restrict__ d_data, float * __restrict__ d_results, const int Nrows, const int Ncols) {

    const unsigned int tid = threadIdx.x + blockDim.x * blockIdx.x;

    if (tid < Nrows) d_results[tid] = thrust::reduce(thrust::device, d_data + tid * Ncols, d_data + (tid + 1) * Ncols);

}

/********/
/* MAIN */
/********/
int main() {

    const int Nrows = 64;
    const int Ncols = 2048;

    gpuErrchk(cudaFree(0));

//    size_t DevQueue;
//    gpuErrchk(cudaDeviceGetLimit(&DevQueue, cudaLimitDevRuntimePendingLaunchCount));
//    DevQueue *= 128;
//    gpuErrchk(cudaDeviceSetLimit(cudaLimitDevRuntimePendingLaunchCount, DevQueue));

    float *h_data       = (float *)malloc(Nrows * Ncols * sizeof(float));
    float *h_results    = (float *)malloc(Nrows *         sizeof(float));
    float *h_results1   = (float *)malloc(Nrows *         sizeof(float));
    float *h_results2   = (float *)malloc(Nrows *         sizeof(float));
    float sum = 0.f;
    for (int i=0; i<Nrows; i++) {
        h_results[i] = 0.f;
        for (int j=0; j<Ncols; j++) {
            h_data[i*Ncols+j] = i;
            h_results[i] = h_results[i] + h_data[i*Ncols+j];
        }
    }

    TimingGPU timerGPU;

    float *d_data;          gpuErrchk(cudaMalloc((void**)&d_data,     Nrows * Ncols * sizeof(float)));
    float *d_results1;      gpuErrchk(cudaMalloc((void**)&d_results1, Nrows         * sizeof(float)));
    float *d_results2;      gpuErrchk(cudaMalloc((void**)&d_results2, Nrows         * sizeof(float)));
    gpuErrchk(cudaMemcpy(d_data, h_data, Nrows * Ncols * sizeof(float), cudaMemcpyHostToDevice));

    timerGPU.StartCounter();
    test1<<<iDivUp(Nrows, BLOCKSIZE_1D), BLOCKSIZE_1D>>>(d_data, d_results1, Nrows, Ncols);
    gpuErrchk(cudaPeekAtLastError());
    gpuErrchk(cudaDeviceSynchronize());
    printf("Timing approach nr. 1 = %f\n", timerGPU.GetCounter());

    gpuErrchk(cudaMemcpy(h_results1, d_results1, Nrows * sizeof(float), cudaMemcpyDeviceToHost));

    for (int i=0; i<Nrows; i++) {
        if (h_results1[i] != h_results[i]) {
            printf("Approach nr. 1; Error at i = %i; h_results1 = %f; h_results = %f", i, h_results1[i], h_results[i]);
            return 0;
        }
    }

    timerGPU.StartCounter();
    test2<<<iDivUp(Nrows, BLOCKSIZE_1D), BLOCKSIZE_1D>>>(d_data, d_results1, Nrows, Ncols);
    gpuErrchk(cudaPeekAtLastError());
    gpuErrchk(cudaDeviceSynchronize());
    printf("Timing approach nr. 2 = %f\n", timerGPU.GetCounter());

    gpuErrchk(cudaMemcpy(h_results1, d_results1, Nrows * sizeof(float), cudaMemcpyDeviceToHost));

    for (int i=0; i<Nrows; i++) {
        if (h_results1[i] != h_results[i]) {
            printf("Approach nr. 2; Error at i = %i; h_results1 = %f; h_results = %f", i, h_results1[i], h_results[i]);
            return 0;
        }
    }

    printf("Test passed!\n");

}
Run Code Online (Sandbox Code Playgroud)

上面的例子执行矩阵行的减少与使用CUDA减少矩阵行的意义相同,但它与上面的帖子不同,即通过直接从用户编写的内核调用CUDA Thrust原语.此外,上述示例用于比较完成两个执行策略时相同操作的性能,即thrust::seqthrust::device.下面是一些显示性能差异的图表.

计时

加速比

性能已在Kepler K20c和Maxwell GeForce GTX 850M上进行了评估.


Jac*_*ern 13

我想提供这个问题的最新答案.

从Thrust 1.8开始,CUDA Thrust原语可以与thrust::seq执行策略结合,在单个CUDA线程中顺序运行(或在单个CPU线程内顺序运行).下面是一个例子.

如果你想在一个线程中进行并行执行,那么你可以考虑使用CUB,它提供可以在一个threadblock中调用的简化例程,只要你的卡能够实现动态并行.

以下是Thrust的示例

#include <stdio.h>

#include <thrust/reduce.h>
#include <thrust/execution_policy.h>

/********************/
/* CUDA ERROR CHECK */
/********************/
#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, char *file, int line, bool abort=true)
{
   if (code != cudaSuccess) 
   {
      fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
      if (abort) exit(code);
   }
}

__global__ void test(float *d_A, int N) {

    float sum = thrust::reduce(thrust::seq, d_A, d_A + N);

    printf("Device side result = %f\n", sum);

}

int main() {

    const int N = 16;

    float *h_A = (float*)malloc(N * sizeof(float));
    float sum = 0.f;
    for (int i=0; i<N; i++) {
        h_A[i] = i;
        sum = sum + h_A[i];
    }
    printf("Host side result = %f\n", sum);

    float *d_A; gpuErrchk(cudaMalloc((void**)&d_A, N * sizeof(float)));
    gpuErrchk(cudaMemcpy(d_A, h_A, N * sizeof(float), cudaMemcpyHostToDevice));

    test<<<1,1>>>(d_A, N);

}
Run Code Online (Sandbox Code Playgroud)


fab*_*ioM 6

如果您的意思是使用由推力分配/处理的数据,您可以,只需获取已分配数据的原始指针.

int * raw_ptr = thrust::raw_pointer_cast(dev_ptr);
Run Code Online (Sandbox Code Playgroud)

如果你想在内核中分配推力向量我从未尝试但我认为不会起作用,如果它有效,我认为它不会带来任何好处.

  • FabrizioM:我希望可以将 device_vector 传递到内核并在内核内部调用 size() 。目前看来这是不可能的。然后我将使用 raw_pointer_cast 并将大小作为单独的参数发送给内核。 (2认同)