CUDA copy class object containg pointer to another class

I am trying to copy a class object containing pointers to another. In particular, I have a class LikelihoodConstructor which contains an array of pointers to another class, DataModel which contains an array ‘bins’ which im trying to access. essentially in the kernel I would like to run is the following :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>__global__ void test_kernel(LikelihoodConstructor * gpuClass, int length, int bins){
for (int i =0; i< length;i++){
for (int j = 0;j <bins; j++){
printf("Length %i, bin %i, val %in",i,j,gpuClass->MyModels[i]->nBins);
//what I want to access
printf("Length %i, bin %i, val %fn",i,j,gpuClass->MyModels[i]->bins[j]);
}
}
printf("Exit Kerneln");
}
</code>
<code>__global__ void test_kernel(LikelihoodConstructor * gpuClass, int length, int bins){ for (int i =0; i< length;i++){ for (int j = 0;j <bins; j++){ printf("Length %i, bin %i, val %in",i,j,gpuClass->MyModels[i]->nBins); //what I want to access printf("Length %i, bin %i, val %fn",i,j,gpuClass->MyModels[i]->bins[j]); } } printf("Exit Kerneln"); } </code>
__global__ void test_kernel(LikelihoodConstructor * gpuClass, int length, int bins){
    
    for (int i =0; i< length;i++){
        for (int j = 0;j <bins; j++){
            printf("Length %i, bin %i, val %in",i,j,gpuClass->MyModels[i]->nBins);
            
            //what I want to access
            printf("Length %i, bin %i, val %fn",i,j,gpuClass->MyModels[i]->bins[j]);
        }
    }
    printf("Exit Kerneln");   
}

The class structures are the following

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>
class DataModel{
public :
int nBins;
double * bins; //thing i want to access
};
class LikelihoodConstructor{//class to go to GPU
public:
DataModel** MyModels;
};
</code>
<code> class DataModel{ public : int nBins; double * bins; //thing i want to access }; class LikelihoodConstructor{//class to go to GPU public: DataModel** MyModels; }; </code>

class DataModel{
    public :
        int nBins;
        double * bins; //thing i want to access
};

class LikelihoodConstructor{//class  to go to GPU
    public:
        DataModel** MyModels;
   
     
};

The main in the following

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>int main(){
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> dis(-2,2);
int length = 2;
int nBins = 5;
//Initiate object on the CPU
LikelihoodConstructor cpuClass;
cpuClass.MyModels = new DataModel*[length];
for(int i = 0; i < length; ++i) {
cpuClass.MyModels[i] = new DataModel;
cpuClass.MyModels[i]->nBins = nBins;
cpuClass.MyModels[i]->bins = new double[nBins];
for (int j =0; j<nBins; j++){
cpuClass.MyModels[i]->bins[j] = dis(gen);
}
} // so we can prove that things are working
//Allocate storage for object onto GPU and copy host object to device
LikelihoodConstructor * gpuClass;
cudaMalloc(&gpuClass,sizeof(LikelihoodConstructor));
cudaMemcpy(gpuClass,&cpuClass,sizeof(LikelihoodConstructor),cudaMemcpyHostToDevice);
DataModel ** d_models;
d_models = new DataModel*[length];
for(int i = 0; i < length; ++i) {
//allocate memory for each data model
cudaMalloc(&d_models[i],sizeof(DataModel));
cudaCheckErrors("cudaMalloc failure"); // error checking
//copy models over
cudaMemcpy(d_models[i], cpuClass.MyModels[i],sizeof(DataModel),cudaMemcpyHostToDevice);
cudaCheckErrors("cudaMemcpy H2D failure");
//This is the bit Im trying to figure out
//allocate memory for the array in d_models[i]->bins
cudaMalloc(&(d_models[i]->bins), nBins*sizeof(double));
cudaCheckErrors("cudaMalloc failure"); // error checking
//copy the bin content over... **This fails to run**
cudaMemcpy(d_models[i]->bins, cpuClass.MyModels[i]->bins,nBins*sizeof(double),cudaMemcpyHostToDevice);
// cudaCheckErrors("cudaMemcpy H2D failure");
}
DataModel ** td_models;
//allocate the top level pointers
cudaMalloc(&td_models, length * sizeof(DataModel *));
cudaMemcpy(td_models, d_models, length * sizeof(DataModel *), cudaMemcpyHostToDevice);
//copy *pointer value* of td_par to appropriate location in top level object
cudaMemcpy(&(gpuClass->MyModels), &(td_models), sizeof(DataModel **), cudaMemcpyHostToDevice);
test_kernel<<<1,1>>>(gpuClass,length, nBins);
cudaCheckErrors("kernel launch failure");
cudaDeviceSynchronize();
//clean up
for(int i = 0; i < length; ++i) {
cudaFree(d_models[i]);
cudaFree(d_par[i]);
free(h_test[i]);
}
//free top level
cudaFree(td_models);
cudaFree(td_par);
cudaFree(gpuClass);
free(h_test);
return 0;
</code>
<code>int main(){ std::random_device rd; std::mt19937 gen(rd()); std::uniform_real_distribution<> dis(-2,2); int length = 2; int nBins = 5; //Initiate object on the CPU LikelihoodConstructor cpuClass; cpuClass.MyModels = new DataModel*[length]; for(int i = 0; i < length; ++i) { cpuClass.MyModels[i] = new DataModel; cpuClass.MyModels[i]->nBins = nBins; cpuClass.MyModels[i]->bins = new double[nBins]; for (int j =0; j<nBins; j++){ cpuClass.MyModels[i]->bins[j] = dis(gen); } } // so we can prove that things are working //Allocate storage for object onto GPU and copy host object to device LikelihoodConstructor * gpuClass; cudaMalloc(&gpuClass,sizeof(LikelihoodConstructor)); cudaMemcpy(gpuClass,&cpuClass,sizeof(LikelihoodConstructor),cudaMemcpyHostToDevice); DataModel ** d_models; d_models = new DataModel*[length]; for(int i = 0; i < length; ++i) { //allocate memory for each data model cudaMalloc(&d_models[i],sizeof(DataModel)); cudaCheckErrors("cudaMalloc failure"); // error checking //copy models over cudaMemcpy(d_models[i], cpuClass.MyModels[i],sizeof(DataModel),cudaMemcpyHostToDevice); cudaCheckErrors("cudaMemcpy H2D failure"); //This is the bit Im trying to figure out //allocate memory for the array in d_models[i]->bins cudaMalloc(&(d_models[i]->bins), nBins*sizeof(double)); cudaCheckErrors("cudaMalloc failure"); // error checking //copy the bin content over... **This fails to run** cudaMemcpy(d_models[i]->bins, cpuClass.MyModels[i]->bins,nBins*sizeof(double),cudaMemcpyHostToDevice); // cudaCheckErrors("cudaMemcpy H2D failure"); } DataModel ** td_models; //allocate the top level pointers cudaMalloc(&td_models, length * sizeof(DataModel *)); cudaMemcpy(td_models, d_models, length * sizeof(DataModel *), cudaMemcpyHostToDevice); //copy *pointer value* of td_par to appropriate location in top level object cudaMemcpy(&(gpuClass->MyModels), &(td_models), sizeof(DataModel **), cudaMemcpyHostToDevice); test_kernel<<<1,1>>>(gpuClass,length, nBins); cudaCheckErrors("kernel launch failure"); cudaDeviceSynchronize(); //clean up for(int i = 0; i < length; ++i) { cudaFree(d_models[i]); cudaFree(d_par[i]); free(h_test[i]); } //free top level cudaFree(td_models); cudaFree(td_par); cudaFree(gpuClass); free(h_test); return 0; </code>
int main(){
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_real_distribution<> dis(-2,2);
    int length = 2;
    int nBins  = 5;

    //Initiate object on the CPU
    LikelihoodConstructor cpuClass;
    cpuClass.MyModels = new DataModel*[length];

    for(int i = 0; i < length; ++i) {
        cpuClass.MyModels[i] = new DataModel;
        cpuClass.MyModels[i]->nBins = nBins;
        cpuClass.MyModels[i]->bins = new double[nBins];
        for (int j =0; j<nBins; j++){
            cpuClass.MyModels[i]->bins[j] = dis(gen);
        }
    } // so we can prove that things are working

    //Allocate storage for object onto GPU and copy host object to device
    LikelihoodConstructor * gpuClass;
    cudaMalloc(&gpuClass,sizeof(LikelihoodConstructor));
    cudaMemcpy(gpuClass,&cpuClass,sizeof(LikelihoodConstructor),cudaMemcpyHostToDevice);
    
    DataModel ** d_models;
    d_models = new DataModel*[length];

    for(int i = 0; i < length; ++i) {
        //allocate memory for each data model
        cudaMalloc(&d_models[i],sizeof(DataModel));
        cudaCheckErrors("cudaMalloc failure"); // error checking
        
        //copy models over
        cudaMemcpy(d_models[i], cpuClass.MyModels[i],sizeof(DataModel),cudaMemcpyHostToDevice);
        cudaCheckErrors("cudaMemcpy H2D failure");
        //This is the bit Im trying to figure out
        //allocate memory for the array in d_models[i]->bins
        cudaMalloc(&(d_models[i]->bins), nBins*sizeof(double));
        cudaCheckErrors("cudaMalloc failure"); // error checking

        //copy the bin content over... **This fails to run**
        cudaMemcpy(d_models[i]->bins, cpuClass.MyModels[i]->bins,nBins*sizeof(double),cudaMemcpyHostToDevice);
        // cudaCheckErrors("cudaMemcpy H2D failure");
    }

    DataModel ** td_models;
    //allocate the top level pointers
    cudaMalloc(&td_models, length * sizeof(DataModel *));
    cudaMemcpy(td_models, d_models, length * sizeof(DataModel *), cudaMemcpyHostToDevice);
    //copy *pointer value* of td_par to appropriate location in top level object
    cudaMemcpy(&(gpuClass->MyModels), &(td_models), sizeof(DataModel **), cudaMemcpyHostToDevice);

    test_kernel<<<1,1>>>(gpuClass,length, nBins);
    cudaCheckErrors("kernel launch failure");
    cudaDeviceSynchronize();

    //clean up
    for(int i = 0; i < length; ++i) {
        cudaFree(d_models[i]);
        cudaFree(d_par[i]);
        free(h_test[i]);
    }
    //free top level
    cudaFree(td_models);
    cudaFree(td_par);
    cudaFree(gpuClass);
    free(h_test);

    return 0;

Code Compiles fine but fails when accessing gpuClass->MyModels[i]->bins[j]

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>compute-sanitizer --leak-check full ./main
========= COMPUTE-SANITIZER
========= Error: process didn't terminate successfully
========= Target application returned an error
========= LEAK SUMMARY: 0 bytes leaked in 0 allocations
========= ERROR SUMMARY: 0 errors
</code>
<code>compute-sanitizer --leak-check full ./main ========= COMPUTE-SANITIZER ========= Error: process didn't terminate successfully ========= Target application returned an error ========= LEAK SUMMARY: 0 bytes leaked in 0 allocations ========= ERROR SUMMARY: 0 errors </code>
compute-sanitizer --leak-check full ./main
========= COMPUTE-SANITIZER
========= Error: process didn't terminate successfully
========= Target application returned an error
========= LEAK SUMMARY: 0 bytes leaked in 0 allocations
========= ERROR SUMMARY: 0 errors

Ultimately I cant quite figure out how to link the gpuClass to the DataModel Bins on the device.

Ive used this post to get most of the way but i just cant find a way to access the bins.

Ive also tried:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>for(int i = 0; i < length; ++i) {
cudaMemcpy(gpuClass->MyModels[i]->bins, cpuClass.MyModels[i]->bins,nBins*sizeof(double),cudaMemcpyHostToDevice);
}
</code>
<code>for(int i = 0; i < length; ++i) { cudaMemcpy(gpuClass->MyModels[i]->bins, cpuClass.MyModels[i]->bins,nBins*sizeof(double),cudaMemcpyHostToDevice); } </code>
for(int i = 0; i < length; ++i) {
        cudaMemcpy(gpuClass->MyModels[i]->bins, cpuClass.MyModels[i]->bins,nBins*sizeof(double),cudaMemcpyHostToDevice);
    }

New contributor

Seraphim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật