2012-08-30 4 views
2

나는 추력 라이브러리를 사용하여 CUDA에서 장치 배열의 접두어 합계를 계산하려고합니다. 내 배열은 cudaMalloc()으로 할당됩니다. 내 요구 사항은 다음과 같습니다원시 포인터와 thrust :: iterators 변환

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. 
} 

내가 장치 반복자에 대한 포인터를 변환하는 방법이 필요하므로 내 커널 사이 어딘가에 추력을 사용하려는 돌아 왔을때 코드는 다음의 잘못?

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;             
}      
+1

이 편집은 새로운 질문을하기위한 것입니까? – talonmies

답변

9

최근 편집에서 완전한 동작하는 예제는 다음과 같이 보일 것이다 :

#include <thrust/device_ptr.h> 
#include <thrust/device_vector.h> 
#include <thrust/scan.h> 
#include <thrust/fill.h> 
#include <thrust/copy.h> 
#include <cstdio> 

int main()               
{                 
    const int N = 16; 
    int * a; 
    cudaMalloc((void**)&a, N*sizeof(int)); 
    thrust::device_ptr<int> d = thrust::device_pointer_cast(a); 
    thrust::fill(d, d+N, 2); 
    thrust::device_vector<int> v(N);      
    thrust::exclusive_scan(d, d+N, v.begin()); 

    int v_[N]; 
    thrust::copy(v.begin(), v.end(), v_); 
    for(int i=0; i<N; i++) 
     printf("%d %d\n", i, v_[i]);  

    return 0;             
} 

일을 당신은 잘못있어 :

  1. 하지 어디서나 정의 N
  2. 전달을 원시 장치 포인터대신 adevice_vectorvexclusive_scanexclusive_scan에 전달
  3. 상기 입력 반복자보다 세부 사항에 적합한 반복기 v.begin()

이목d이 작동하도록 모두 부족했다. 그리고이하는 일 :

$ nvcc -arch=sm_12 -o thrust_kivekset thrust_kivekset.cu 
$ ./thrust_kivekset 

0 0 
1 2 
2 4 
3 6 
4 8 
5 10 
6 12 
7 14 
8 16 
9 18 
10 20 
11 22 
12 24 
13 26 
14 28 
15 30 

편집 :

thrust::device_vector.data()이 벡터의 첫 번째 요소를 가리키는 thrust::device_ptr를 반환합니다. thrust::device_ptr.get()은 원시 장치 포인터를 반환합니다. 따라서

cudaMemcpy(v_, v.data().get(), N*sizeof(int), cudaMemcpyDeviceToHost); 

thrust::copy(v, v+N, v_); 

이 예에서는 기능적으로 동등하다.

+0

device_vector에서 원시 포인터를 추출하는 방법은 무엇입니까? –

+0

그런 오류가 한 번 더 나타납니다 : 메모리 위치 0x0043f3a8의 thrust :: system :: system_error .. –

+0

내 편집을 참조하십시오. 기본적으로 이미 대답하고 받아 들인 질문 * 두 번 *을 다시 응답하면 아마도 투표가 순서대로 진행될 수 있습니다. – talonmies

3

은 원시 포인터 thrust::device_pointer_cast를 사용하여 thrust::device_ptrcudaMalloc()에서 얻은 변환합니다.

#include <thrust/device_ptr.h> 
#include <thrust/fill.h> 
#include <cuda.h> 

int main(void) 
{ 
    size_t N = 10; 

    // obtain raw pointer to device memory 
    int * raw_ptr; 
    cudaMalloc((void **) &raw_ptr, N * sizeof(int)); 

    // wrap raw pointer with a device_ptr 
    thrust::device_ptr<int> dev_ptr = thrust::device_pointer_cast(raw_ptr); 

    // use device_ptr in Thrust algorithms 
    thrust::fill(dev_ptr, dev_ptr + N, (int) 0);  

    // access device memory transparently through device_ptr 
    dev_ptr[0] = 1; 

    // free memory 
    cudaFree(raw_ptr); 

    return 0; 
} 

사용 thrust::inclusive_scan 또는 thrust::exclusive_scan 접두사 합계를 계산하기 위해 다음은 추력 문서의 예입니다.

http://code.google.com/p/thrust/wiki/QuickStartGuide#Prefix-Sums

+0

나는 시도했지만 나에게 도움이되지 않습니다 .. 질문에 소스 코드를 추가했습니다. 그것을보세요. –

관련 문제