2016-03-03 87 views
2

我正試圖在cub中創建自己的掃描運算符。它現在正在工作,但只適用於小於1024的數組,這使我認爲它只適用於一個塊。這裏是我的代碼:定義自定義掃描運算符

#include "cub/cub.cuh" 
using namespace cub; 


typedef int mytype; 

struct CustomMin 
{ 
    template <typename T> 
    __host__ __device__ 
    CUB_RUNTIME_FUNCTION __forceinline__ 
    mytype operator()(const T &a, const T &b) const { 
     return (b < a) ? b : a; 
    } 
}; 


int main(int argc, char *argv[]) 
{ 

    int num_items = 512;  
    mytype *h_in; 
    mytype *h_out;   
    CustomMin min_op; 
    const size_t size = num_items * sizeof(mytype); 
    h_in = (mytype*)malloc(size); 
    h_out = (mytype*)malloc(size); 
    mytype *d_in = NULL; 
    cudaMalloc(&d_in, size); 
    mytype *d_out = NULL; 
    cudaMalloc(&d_out, size); 
    for (int i = 0; i < num_items; i++) { 
     h_in[i] = i; 
    } 
    cudaMemcpy(d_in, h_in, size, cudaMemcpyHostToDevice); 
    void *d_temp_storage = NULL; 
    size_t temp_storage_bytes = 0; 
    DeviceScan::InclusiveScan(d_temp_storage, temp_storage_bytes, d_in, d_out, min_op, num_items); 
    cudaMalloc(&d_temp_storage, temp_storage_bytes); 
    DeviceScan::InclusiveScan(d_temp_storage, temp_storage_bytes, d_in, d_out, min_op, num_items); 
    cudaMemcpy(h_out, d_out, size, cudaMemcpyDeviceToHost); 
    printf("done!\n"); 
    return 0; 
} 

它始終掛起爲更大的輸入尺寸。

+0

我無法編譯此代碼。你能否包含一個完整的代碼,我可以編譯而不需要添加任何東西或改變任何東西。例如你的'mytype'是未定義的,就像你的'g_allocator'一樣。如果您不確定,那麼將您發佈的內容複製到新文件中並嘗試編譯。修復它以便編譯,然後編輯回問題。 –

+1

你以前的代碼掛在這裏: '因爲(i = 0; i

+0

感謝您的檢查。我想通過使用sm_35編譯TitanX上的代碼它會掛起,但它會與sm_52一起使用。我想這是CUB的問題。 – Mems

回答

2

幼崽1.4.1我能夠重現編譯這樣當掛起:

nvcc -arch=sm_35 -o t25 t25.cu 

在發佈代碼改變num_items到2048

根據我的測試後,出現的問題將在cub 1.5.1中修復。請更新到最新的CUB版本。