cudaMallocManaged with vector > C++ - NVIDIA CUDA

后端 未结 1 1600
轮回少年
轮回少年 2021-01-28 04:18

I am in the process of implementing multithreading through a NVIDIA GeForce GT 650M GPU for a simulation I have created. In order to make sure everything works properly, I have

1条回答
  •  后悔当初
    2021-01-28 04:41

    It isn't possible to do what you are trying to do.

    To allocate a vector using managed memory you would have to write your own implementation of an allocator which inherits from std::allocator_traits and calls cudaMallocManaged under the hood. You can then instantiate a std::vector using your allocator class.

    Also note that your CUDA kernel code is broken in that you can't use std::vector in device code.

    Note that although the question has managed memory in view, this is applicable to other types of CUDA allocation such as pinned allocation.

    As another alternative, suggested here, you could consider using a thrust host vector in lieu of std::vector and use a custom allocator with it. A worked example is here in the case of pinned allocator (cudaMallocHost/cudaHostAlloc).

    0 讨论(0)
提交回复
热议问题