close

以下將示範:

1.在std::vector裡面建立好資料結構

(請視個人需求自行建立vector,以下以自訂的class為例)

2.以建好的資料初始一個新的thrust::device_vector

thrust::device_vector<data> D((ite->samples.begin()),(ite->samples.end()));
// ite是自行建立的vector的iterator,sample是該class內的成員(型別為data)

3.使用thrust::sort()排序

thrust::sort(D.begin(),D.end(),compare_by_feature_value<data>());

4.把排序好的傳回來

 thrust::copy(D.begin(),D.end(),ite->samples.begin());

 

參閱這個範例,知道跟std::sort一樣可以使用自訂參數compare
https://github.com/thrust/thrust/blob/master/examples/sort.cu

 

使用thrust::sort(D.begin(),D.end(),compare_by_feature_value<data>)遇到的問題

(data為自訂的class)

  • Function pointers and function template parameters are not supported in sm_1x.

明明使用的是Tesla M2050,compute capability是2.0

不應該有這問題才對

稍微查了,才知道fermi-based的compiler預設是使用sm_10

所以需要在nvcc後面補上 -arch=sm_20

=>這問題解掉了。

  • 如何在user-defined function裡面使用template
    (這裡是以data class中的value排序。紅字部分不應該更動)

    template <class T>
    struct compare_by_feature_value
    {

    __host__ __device__  // 要讓device跟host都可以使用此function
    bool operator()(T a, T b)  // 將compare_by_feature_value的 () 作 overloading
    {
    return a.value < b.value;
    }

    };



目前是使用這樣的設定

可能是每次都要把資料由std::vector 初始新的device_vector再copy回來耗的時間太多

比較起來跟原本使用std::sort的差異只有一倍左右

arrow
arrow
    全站熱搜

    holmes310524 發表在 痞客邦 留言(0) 人氣()