私は現在、私のコードから面倒なcudaMallocHost/cudaFreeHostの一部を削除しようとしています。そうするために、私はstd :: vectorのみを使いたいと思っていますが、基本的なメモリは、固定されたcudaメモリ型でなければならないことが絶対に必要です。推論を使用して奇妙な実験:: pinned_allocatorのuda
しかし、私はスラストライブラリからthrust::system::cuda::experimental::pinned_allocator<>
を使用して奇妙な行動に直面しています:
//STL
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
//CUDA
#include <cuda_runtime.h>
#include <thrust/device_vector.h>
#include <thrust/transform.h>
#include <thrust/system/cuda/experimental/pinned_allocator.h>
#define SIZE 4
#define INITVAL 2
#define ENDVAL 4
//Compile using nvcc ./main.cu -o test -std=c++11
int main(int argc, char* argv[])
{
// init host
std::vector<float,thrust::system::cuda::experimental::pinned_allocator<float> > hostVec(SIZE);
std::fill(hostVec.begin(),hostVec.end(),INITVAL);
//Init device
thrust::device_vector<float> thrustVec(hostVec.size());
//Copy
thrust::copy(hostVec.begin(), hostVec.end(), thrustVec.begin());
//std::cout << "Dereferencing values of the device, values should be "<< INITVAL << std::endl;
std::for_each(thrustVec.begin(),thrustVec.end(),[](float in){ std::cout <<"val is "<<in<<std::endl;});
std::cout << "------------------------" << std::endl;
//Do Stuff
thrust::transform(thrustVec.begin(), thrustVec.end(), thrust::make_constant_iterator(2), thrustVec.begin(), thrust::multiplies<float>());
//std::cout << "Dereferencing values of the device, values should now be "<< ENDVAL << std::endl;
std::for_each(thrustVec.begin(),thrustVec.end(),[](float in){ std::cout <<"val is "<<in<<std::endl;});
std::cout << "------------------------" << std::endl;
//Copy back
thrust::copy(thrustVec.begin(), thrustVec.end(), hostVec.begin());
//Synchronize
//cudaDeviceSynchronize(); //makes the weird behaviour to go away
//Check result
//std::cout << "Dereferencing values on the host, values should now be "<< ENDVAL << std::endl;//Also makes the weird behaviour to go away
std::for_each(hostVec.begin(),hostVec.end(),[](float in){ std::cout <<"val is "<<in<<std::endl;});
return EXIT_SUCCESS;
}
:
val is 2
val is 2
val is 2
val is 2
------------------------
val is 4
val is 4
val is 4
val is 4
------------------------
val is 2
val is 4
val is 4
val is 4
をデバイスからホストへのコピーは思わない理由失敗する ?しかし、Nvvpは完全な細かいクロノグラムを示しています。
ちなみに7.5パッケージのNVCC/cuda/thrust、titanXカードのgcc(GCC)4.8.5を使用しています。
ご協力いただきありがとうございます。
アクセス可能なプラットフォームではこれを再現できません。デバイスからホストコピーの後にベクターを印刷する前に、同期呼び出しを追加するとどうなりますか? – talonmies
私はgtx680(計算機能3.0)でエラーを再現することもできます。実際に、cudaDeviceSynchronizeを追加することで、意図したとおりにコードが実行されます。私はthrust :: copyには同期動作があると信じていましたが、実際には推論の同期/非同期動作に関する情報はありません。http://thrust.github.io/doc/group__copying.html#ga24ccfaaa706a9163ec5117758fdb71b9 – Tobbey