Implicit constructor in CUDA kernel call

后端 未结 2 1238
执念已碎
执念已碎 2021-01-21 13:24

I\'m trying to pass some POD to a kernel which has as parameters some non-POD, and has non explicit constructors. Idea behind that is: allocate some memory on the host, pass the

2条回答
  •  南方客
    南方客 (楼主)
    2021-01-21 13:47

    You have to see a constructor just like a normal method. If you qualify it with __host__, then you'll be able to call it host-side. If you qualify it with __device__, you'll be able to call it device-side. If you qualify it with both, you'll be able to call it on both sides.

    What happens when you do dosome<<<1, 2>>>(2, 3); is that the two objects are implictly constructed (because your constructor is not explicit, so maybe that's confusing you too) host side and then memcpy'd to the device. There is no copy-constructor involved in the process.

    Let's illustrate this:

        __global__ void dosome(Test a, Test b) {
            a.calc();
            b.calc();
        }
    
        int main(int argc, char **argv) {
            dosome<<<1, 2>>>(2, 3); // Constructors must be at least __host__
            return 0;
        }
    
    // Outputs:
    Construct 2 (from the host side)
    Construct 3 (from the host side)
    

    Now if you change your kernel to take ints instead of Test:

    __global__ void dosome(int arga, int argb) {
        // Constructors must be at least __device__
        Test a(arga);
        Test b(argb);
        a.calc();
        b.calc();
    }
    
    int main(int argc, char **argv) {
        dosome<<<1, 2>>>(2, 3);
        return 0;
    }
    
    // Outputs:
    Construct 2 (from the device side)
    Construct 3 (from the device side)
    

提交回复
热议问题