0

I am trying to move an array of pointers to the device, where each pointer is pointing to a class object. However, I get a Segmentation fault at the line using cudaMemcpy. I am trying to follow the lines used in this post.

main.cu

#include "testclass.cuh"
#include <iostream>


__global__ void printtest(Test* test){
    printf("HELLO FROM CUDA\n");
    printf("CUDA1 : %i\n", test->hello);
    Test test2(6);
    printf("CUDA2 : %i\n", test2.hello);
    printf("BYEEE FROM CUDA\n");

}

int main(){
    printf("hello\n");
    Test* test = new Test(512);
    printf("CPU : %i\n", test->hello);
    Test* devtest;
    cudaMalloc(&devtest, sizeof(Test));
    cudaError_t err = cudaMemcpy(devtest, test, sizeof(Test), cudaMemcpyHostToDevice);
    if (err != cudaSuccess) {                                   
        fprintf(stderr, "Error %s at line %d in file %s\n",             
            cudaGetErrorString(err), __LINE__-3, __FILE__);
    }
    printtest<<<1, 1>>>(devtest);
    cudaDeviceSynchronize();



    printf("hello2\n");
    Test** test3 = new Test*[2];
    test3[0] = new Test(12299);
    test3[1] = new Test(234923);
    printf("CPU : %i\n", test3[0]->hello);
    Test** devtest3;
    cudaMalloc(&devtest3, 2*sizeof(Test*));
    printf("CPU2\n");
    err = cudaMemcpy(devtest3[0], test3[0], sizeof(Test), cudaMemcpyHostToDevice);
    if (err != cudaSuccess) {                                   
        fprintf(stderr, "Error %s at line %d in file %s\n",             
            cudaGetErrorString(err), __LINE__-3, __FILE__);
    }
    printf("CPU3\n");
    printtest<<<1, 1>>>(devtest3[0]);
    cudaDeviceSynchronize();
}

testclass.cu

#include "testclass.cuh"

__host__ __device__ Test::Test(int in){
    hello = in;
}

testclass.cuh

class Test {
public: 
    int hello;
    __host__ __device__ Test(int);
};
2
  • devtest3[0] is an uninitialized pointer, so you can't copy anything there. You need to allocate space for the Test objects, too, not just the pointers. Commented Oct 28, 2020 at 16:29
  • I see, thank you. I fixed the above by changing Test** devtest3; to Test* devtest[2]; and then mallocing cudaMalloc(&devtest3[0], sizeof(Test));. instead of cudaMalloc(&devtest3, 2*sizeof(Test*));. Commented Oct 28, 2020 at 16:36

1 Answer 1

1

Solved it using @molbdnilo 's comment.

main.cu

...
printf("hello2\n");
    Test** test3 = new Test*[2];
    test3[0] = new Test(12299);
    test3[1] = new Test(234923);
    printf("CPU : %i\n", test3[0]->hello);
    Test* devtest3[2];
    cudaMalloc(&devtest3[0], sizeof(Test));
    printf("CPU2\n");
    err = cudaMemcpy(devtest3[0], test3[0], sizeof(Test), cudaMemcpyHostToDevice);
    if (err != cudaSuccess) {                                   
        fprintf(stderr, "Error %s at line %d in file %s\n",             
            cudaGetErrorString(err), __LINE__-3, __FILE__);
    }
    printf("CPU3\n");
    printtest<<<1, 1>>>(devtest3[0]);
    cudaDeviceSynchronize();
...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.