0

アプリケーションを高速化するためにCUDAが提供する利点を直接確認できるCUDAコードを書きたかったのです。

これが私がThrustを使って書いたCUDAコードです(http://code.google.com/p/thrust/

簡単に言うと、コードが行うのは、2つの2 ^ 23の長さの整数ベクトルを作成することです。1つはホスト上に、もう1つはデバイス上に互いに同一であり、それらを並べ替えます。また、それぞれの時間を測定します(試みます)。

ホストベクトルでは、を使用しますstd::sort。デバイスベクトルでは、を使用しますthrust::sort

コンパイルには使用しました

nvcc sortcompare.cu -lrt

端末でのプログラムの出力は次のとおりです。

デスクトップ:./ a.out

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

デバイスの所要時間は19です。321644143秒

デスクトップ:

最初のstd::coutステートメントは、前述のように19.224秒後に生成されます。ただし、2番目のstd :: coutステートメントは(19.32秒と表示されていても)最初のstd::coutステートメントの直後に生成されます。clock_gettime()の測定に異なるtime_stampsを使用したことに注意してください。つまり、ts_hostとts_deviceです。

私はCuda4.0とNVIDIAGTX570コンピューティング機能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;
    }
4

1 に答える 1

1

の戻り値をチェックしていませんclock_settimeerrnoおそらくEPERMまたはEINVALに設定されているため、失敗していると思います。ドキュメントを読み、常に戻り値を確認してください。

私が正しければ、あなたはあなたが思っているように時計をリセットしていません。したがって、2番目のタイミングは最初のタイミングと累積され、さらにいくつかの余分なものはまったく数えません。

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

于 2011-11-11T03:39:25.840 に答える