Wie man Vektor zu Struktur in C++ setzt (msvc 2008).C++ put Vektor zu Struktur ohne Kopie
Zum Beispiel ...
Ich habe Header-Datei .hh:
template <typename dtype> struct c_Cudamat_Struct1 {
thrust::device_vector<dtype> deviceVector;
dtype * dataHost;
dtype * dataDevice;
int onDevice;
int onHost;
(void *) shape;
int size;
int isTrans; // 0 or 1
int ownsData;
int itemsize;
};
template <typename dtype> int thrust_copy_to_device(c_Cudamat_Struct1<dtype> * mat);
template int thrust_copy_to_device<double>(c_Cudamat_Struct1<double> * mat);
template int thrust_copy_to_device<float>(c_Cudamat_Struct1<float> * mat);
template int thrust_copy_to_device<long>(c_Cudamat_Struct1<long> * mat);
template int thrust_copy_to_device<int>(c_Cudamat_Struct1<int> * mat);
template int thrust_copy_to_device<char>(c_Cudamat_Struct1<char> * mat);
Und ich habe einige Haupt .cu Datei (?):
template <typename dtype> int thrust_copy_to_device(c_Cudamat_Struct1<dtype> * mat){
print_memory_gpu("1. Start:::");
thrust::device_vector<dtype> deviceVector(mat->dataHost, mat->dataHost + mat->size);
print_memory_gpu("2. Create deviceVector:::");
mat->deviceVector = deviceVector;
print_memory_gpu("3. Put deviceVector to mat:::");
mat->deviceVector=thrust_vector_fill_range<dtype>(mat->deviceVector, 1, mat->size);
print_memory_gpu("4. mat->deviceVector linspace:::");
return 0;
}
Aber seine Herstellung Kopie :
- Start::: Free/Total GPU memory (MB): 1541/-2048
- Create deviceVector::: Free/Total GPU memory (MB): 1160/-2048
- Put deviceVector to mat::: Free/Total GPU memory (MB): 778/-2048
- mat->deviceVector linspace::: Free/Total GPU memory (MB): 876/-2048
EDIT 000/999
Hervorhebungen was wichtig ist (andere, die ich mit Zeigern aus "mat->dataDevice
" behandelt) ...
template <typename dtype> int thrust_copy_to_device(c_Cudamat_Struct1<dtype> * mat){
print_memory_gpu("1. Start:::");
thrust::device_vector<dtype> deviceVector(mat->dataHost, mat->dataHost + mat->size);
print_memory_gpu("2. Create deviceVector:::");
mat->deviceVector = deviceVector;
print_memory_gpu("3. Put deviceVector to mat:::");
return 0;
}
Wie konnte ich diesen Teil ändern:
thrust::device_vector<dtype> deviceVector(mat->dataHost, mat->dataHost + mat->size);
print_memory_gpu("2. Create deviceVector:::");
mat->deviceVector = deviceVector;
zu etwas wie:
thrust::device_vector<dtype> mat->deviceVector(mat->dataHost, mat->dataHost + mat->size);
print_memory_gpu("2. Create deviceVector:::");
Das Problem ist, dass Zeiger in Ordnung sind, aber ich brauche diesen Vektor immer noch und mache ihn von struct verfügbar.
Soll ich Klasse anstelle dieser Struktur mit einigen schlauen Methoden erstellen?
EDIT 002/999
Simplest Lösung scheint statt struct Schaffung Klasse zu sein:
In Header .hh Datei: (?)
template <class dtype> class C_Cudamat_Struct{
private:
thrust::device_vector<dtype> deviceVector;
public:
C_Cudamat_Struct();
dtype * dataHost;
dtype * dataDevice;
int onDevice;
int onHost;
(void *) shape;
int size;
int isTrans; // 0 or 1
int ownsData;
int itemsize;
int set_device_vector();
};
template class C_Cudamat_Struct<double>;
template class C_Cudamat_Struct<float>;
template class C_Cudamat_Struct<long>;
template class C_Cudamat_Struct<int>;
In Haupt-.cu Datei:
template <class dtype> int C_Cudamat_Struct<dtype>::set_device_vector(){
print_memory_gpu("1. Start:::");
thrust::device_vector<dtype> deviceVector(dataHost, dataHost + size);
print_memory_gpu("2. Create deviceVector:::");
//mat->deviceVector = std::move (deviceVector);
print_memory_gpu("3. Put deviceVector to mat:::");
deviceVector=thrust_vector_fill_range<dtype>(deviceVector, 1, size);
print_memory_gpu("4. mat->deviceVector linspace:::");
return 0;
}
template <class dtype> C_Cudamat_Struct<dtype>::C_Cudamat_Struct(){
}
Ohne den Quellcode von thrust :: device_vector zu sehen, ist es schwer zu sagen, aber möglicherweise können Sie Kopieroperationen durch Verschiebungen ersetzen: 'mat-> deviceVector = std :: move (deviceVector);' –
@H. Guijt - Es wäre nett, Bit das gibt mir Fehler: Namespace "Std" hat kein Mitglied "move" –
Es ist in Sie könnten jedoch versucht haben, es für sich selbst zu googlen. –