Having a variable typed class member depending on type of template C++

时间:2022-12-05 21:54:58

I'm trying to write a template class for managing pointers on Host and Nvidia GPU, similar to boost::shared_ptr. For doing this, I need to define 2 pointers, one for Host and one for GPU in the class.

我正在尝试编写一个模板类来管理Host和Nvidia GPU上的指针,类似于boost :: shared_ptr。为此,我需要定义2个指针,一个用于Host,另一个用于类中的GPU。

template<typename T>
class GPUSharedPtr
{
  protected:

  T *cpu_pointer;
  T *gpu_pointer;
}

The problem with CUDA is that it defines it's own types for std::complex<double> and std::complex<float>, which are cuDoubleComplex and cuComplex respectively.

CUDA的问题在于它为std :: complex 和std :: complex 定义了它自己的类型,它们分别是cuDoubleComplex和cuComplex。

template<typename std::complex<double> >
class GPUSharedPtr
{
  protected:

  std::complex<double> *cpu_pointer;
  cuDoubleComplex *gpu_pointer;
}


template<typename std::complex<float> >
class GPUSharedPtr
{
  protected:

  std::complex<float> *cpu_pointer;
  cuComplex *gpu_pointer;
}

How can I have different typed pointers for GPU in these two cases?

在这两种情况下,如何为GPU提供不同类型的指针?

Thanks!

谢谢!

2 个解决方案

#1


1  

Why not adding both as template parameters :

为什么不将两者都添加为模板参数:

template<typename CPU_T, typename GPU_T>
class GPUSharedPtr
{
  protected:
      CPU_T *cpu_pointer;
      GPU_T *gpu_pointer;
};

int main()
{
    GPUSharedPtr<std::complex<double> , cuDoubleComplex> g1;
    GPUSharedPtr<std::complex<float> , cuComplex> g2;
    return 0;
}

Or (if it makes more sense in you case), you can use tag dispatching as suggested by MooingDuck to properly default the value of the GPU pointer type :

或者(如果在你的情况下更有意义),你可以使用MooingDuck建议的标签调度来正确默认GPU指针类型的值:

template<class CPU_T> struct DefaultGPU_T { typedef CPU_T type;};
template<> struct DefaultGPU_T<std::complex<double>> { typedef cuDoubleComplex type;};
template<> struct DefaultGPU_T<std::complex<float>> { typedef cuComplex type;};

template<typename CPU_T, typename GPU_T=typename DefaultGPU_T<CPU_T>::type>

class GPUSharedPtr
{
protected:
    CPU_T *cpu_pointer;
    GPU_T *gpu_pointer;
};

int main()
{
    GPUSharedPtr<int> g0;
    GPUSharedPtr<std::complex<double>> g1;
    GPUSharedPtr<std::complex<float>> g2;
    return 0;
}

Live demo.

现场演示。

#2


0  

The simplest, most reasonable solution is probably to take two template arguments, with the second being assigned the first parameter as default:

最简单,最合理的解决方案可能是采用两个模板参数,第二个参数默认为第一个参数:

template<typename T, typename GPUType = T>
class GPUSharedPtr
{
  protected:

  T *cpu_pointer;
  GPUType *gpu_pointer;
};

Then you can typedef e.g.

然后你可以输入dede,例如

typedef GPUSharedPtr<std:.complex<float>, cuComplex> ComplexPtr;

#1


1  

Why not adding both as template parameters :

为什么不将两者都添加为模板参数:

template<typename CPU_T, typename GPU_T>
class GPUSharedPtr
{
  protected:
      CPU_T *cpu_pointer;
      GPU_T *gpu_pointer;
};

int main()
{
    GPUSharedPtr<std::complex<double> , cuDoubleComplex> g1;
    GPUSharedPtr<std::complex<float> , cuComplex> g2;
    return 0;
}

Or (if it makes more sense in you case), you can use tag dispatching as suggested by MooingDuck to properly default the value of the GPU pointer type :

或者(如果在你的情况下更有意义),你可以使用MooingDuck建议的标签调度来正确默认GPU指针类型的值:

template<class CPU_T> struct DefaultGPU_T { typedef CPU_T type;};
template<> struct DefaultGPU_T<std::complex<double>> { typedef cuDoubleComplex type;};
template<> struct DefaultGPU_T<std::complex<float>> { typedef cuComplex type;};

template<typename CPU_T, typename GPU_T=typename DefaultGPU_T<CPU_T>::type>

class GPUSharedPtr
{
protected:
    CPU_T *cpu_pointer;
    GPU_T *gpu_pointer;
};

int main()
{
    GPUSharedPtr<int> g0;
    GPUSharedPtr<std::complex<double>> g1;
    GPUSharedPtr<std::complex<float>> g2;
    return 0;
}

Live demo.

现场演示。

#2


0  

The simplest, most reasonable solution is probably to take two template arguments, with the second being assigned the first parameter as default:

最简单,最合理的解决方案可能是采用两个模板参数,第二个参数默认为第一个参数:

template<typename T, typename GPUType = T>
class GPUSharedPtr
{
  protected:

  T *cpu_pointer;
  GPUType *gpu_pointer;
};

Then you can typedef e.g.

然后你可以输入dede,例如

typedef GPUSharedPtr<std:.complex<float>, cuComplex> ComplexPtr;