2011-11-11 13 views
0

私はCUDAがアプリケーションを高速化するために提供した利点を直接見ることができるCUDAコードを書きたいと思っていました。ここclock_gettime()でタイミングが狂うCUDA

コードがないことすべては、2つの2^23の長さの整数ベクトル、ホスト上の一方と同一のデバイス上に作成され、Iは推力(http://code.google.com/p/thrust/

簡単に使用して書かれているCUDAコードでありますお互いに分け合い、並べ替えます。また、それぞれの時間を測定しようとします。

宿主ベクター上では、私はstd::sortを使用する。デバイスベクトル上で私はthrust::sortを使います。コンパイルのために

は、私が使用

NVCC sortcompare.cu -lrt

端子のプログラムの出力は

デスクトップです:

./a.out

ホスト時間は19です。 224622882秒

撮影時間は19です。 321644143秒

デスクトップ:

述べたように最初のstd :: coutの文が19.224秒後に生成されます。しかし、最初の std :: coutステートメントの後には、すぐにの後に、2番目のstd :: coutステートメント(19.32秒といっても)が生成されます。私はあなたがclock_settimeの戻り値をチェックしていないクーダ4.0とNVIDIA GTX 570コンピュート能力を2.0

#include<iostream> 
    #include<vector> 
    #include<algorithm> 
    #include<stdlib.h> 

    //For timings 
    #include<time.h> 
    //Necessary thrust headers 
    #include<thrust/sort.h> 
    #include<thrust/host_vector.h> 
    #include<thrust/device_vector.h> 
    #include<thrust/copy.h> 


    int main(int argc, char *argv[]) 
    { 
     int N=23; 
     thrust::host_vector<int>H(1<<N);//create a vector of 2^N elements on host 
     thrust::device_vector<int>D(1<<N);//The same on the device. 
     thrust::host_vector<int>dummy(1<<N);//Copy the D to dummy from GPU after sorting 

     //Set the host_vector elements. 
     for (int i = 0; i < H.size(); ++i) { 
      H[i]=rand();//Set the host vector element to pseudo-random number. 
     } 

     //Sort the host_vector. Measure time 
     // Reset the clock 
     timespec ts_host; 
     ts_host.tv_sec = 0; 
     ts_host.tv_nsec = 0; 
     clock_settime(CLOCK_PROCESS_CPUTIME_ID, &ts_host);//Start clock 

      thrust::sort(H.begin(),H.end()); 

     clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts_host);//Stop clock 
     std::cout << "\nHost Time taken is: " << ts_host.tv_sec<<" . "<< ts_host.tv_nsec <<" seconds" << std::endl; 


     D=H; //Set the device vector elements equal to the host_vector 
     //Sort the device vector. Measure time. 
     timespec ts_device; 
     ts_device.tv_sec = 0; 
      ts_device.tv_nsec = 0; 
     clock_settime(CLOCK_PROCESS_CPUTIME_ID, &ts_device);//Start clock 

      thrust::sort(D.begin(),D.end()); 
      thrust::copy(D.begin(),D.end(),dummy.begin()); 


     clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts_device);//Stop clock 
     std::cout << "\nDevice Time taken is: " << ts_device.tv_sec<<" . "<< ts_device.tv_nsec <<" seconds" << std::endl; 

     return 0; 
    } 
+0

質問が不明です。あなたは、あなたの2つの時間が0.1秒違うと言いました。その違いは、人間の目にはほとんど気付かれません。問題は何ですか? –

+0

質問を明確にするために編集しました。 – smilingbuddha

+0

ありがとうございます。さて、あなたの問題は明らかにCUDAやThrustとは関係がないので、これらのタグを削除し、サンプルコードを単純化することをお勧めします。 –

答えて

1

を使用しています、私はにclock_gettimeでの測定のために異なるtime_stampsを使用していることに注意してください()すなわちts_hostとts_device

。おそらくerrnoがEPERMまたはEINVALに設定されていると思います。ドキュメントを読んで、常にあなたの戻り値をチェックしてください!

もし私が正しいとすれば、あなたはあなたがそう思っているように時計をリセットしていないので、2番目のタイミングは最初のタイミングに累積され、さらにカウントしたくない余分なものがあります。

これを行う正しい方法は、clock_gettimeのみを呼び出し、計算結果を保存して終了時刻から元の時刻を差し引くことです。

関連する問題