随着以下琐碎的删除
struct CudaDeleter{ void operator()(void * ptr) { cudaFree( ptr ); } };
Run Code Online (Sandbox Code Playgroud)
在使用nvcc编译的代码中使用删除器时出现以下错误.相同的删除程序与vs2012编译器一起工作正常
warning : "std::unique_ptr<_Ty, _Dx>::unique_ptr(
const std::unique_ptr<_Ty, _Dx>::_Myt &)
[with _Ty=const int, _Dx=cuda::CudaDeleter]"
error : function "cuda::CudaDeleter::operator()"
cannot be called with the given argument list
warning : "std::unique_ptr<_Ty, _Dx>::unique_ptr(
const std::unique_ptr<_Ty, _Dx>::_Myt &)
[with _Ty=float, _Dx=cuda::CudaDeleter]"
Run Code Online (Sandbox Code Playgroud)
@talonmies:智能指针仅使用此功能构建
template <typename T>
std::unique_ptr<T, CudaDeleter> make_unique(size_t size)
{
void * pMemory = nullptr;
check( cudaMalloc(&pMemory, size) );
return std::unique_ptr<T, CudaDeleter>( static_cast<T*>(pMemory) );
}
Run Code Online (Sandbox Code Playgroud)
以下适用于我.尝试下面的独立代码,如果它有效,那么你需要确定与你的代码的区别,如果没有,那么你的设置就会有所不同.
#include <iostream>
#include <memory>
struct CudaDeleter
{
void operator()(void *p)
{
std::cout << "Free..." << std::endl;
cudaError_t res = cudaFree(p);
if (res != cudaSuccess)
{
std::cout << "Error freeing: " << cudaGetErrorString(res) << std::endl;
}
}
};
template <typename T>
std::unique_ptr<T, CudaDeleter> make_unique(size_t size)
{
void *pMemory = nullptr;
std::cout << "Allocate..." << std::endl;
cudaError_t res = cudaMalloc(&pMemory, size);
if (res != cudaSuccess)
{
std::cout << "Error allocating pMemory: " << cudaGetErrorString(res) << std::endl;
throw;
}
return std::unique_ptr<T, CudaDeleter>(static_cast<T*>(pMemory));
}
int main(void)
{
{
std::cout << "Create..." << std::endl;
std::unique_ptr<float, CudaDeleter> x = make_unique<float>(100*sizeof(float));
std::cout << "Destroy..." << std::endl;
}
std::cout << "Done." << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
916 次 |
| 最近记录: |