以下示例中如何避免代码复制?C++/库达

How avoid code replication in the following example? C++ / Cuda

本文关键字:复制 C++ 库达 代码 何避免      更新时间:2023-10-16

EDIT:这段代码正在运行,但看起来有很多代码复制部分,我找不到解决这个问题的方法。

在MatrixDevice类中,我想调用kerne.cu中的内核函数。我将MatrixDevice类简化为只显示这个概念——我实际上是如何做到的。

在MatricDevice中,我有一些函数可以将MatrixDevice与其他MatrixDevice或数字一起添加,这应该适用于不同的类型,在本例中使用float和double,模板应该没有问题,但我必须声明重载函数MatrixCudaOperations extern,因为我不能将.cu文件包含在.h/.cpp文件中。

矩阵设备.h

extern void MatrixCudaOperations(const float* a, const float* b, float* result, size_t rows, size_t cols, EOperation operation);
extern void MatrixCudaOperations(const float* a, float b, float* result, size_t rows, size_t cols, EOperation operation);
extern void MatrixCudaOperations(const double* a, const double* b, double* result, size_t rows, size_t cols, EOperation operation);
extern void MatrixCudaOperations(const double* a, double b, double* result, size_t rows, size_t cols, EOperation operation);

template<class T>
class MatrixDevice{
T* data;
size_t rows;
size_t cols;
MatrixDevice& Add(const MatrixDevice &other);
MatrixDevice& Add(T &other);
};
//Operations with MatrixDevice
//Add MatrixDevice to this
template<class T>
MatrixDevice& MatrixDevice::Add(const MatrixDevice &other){
MatrixCudaOperations(data, other.data, data, rows, cols, EOperation::ADD);
return *this;
} 
//Add two MatrixDevice and return the result as new MatrixDevice
template<class T>
MatrixDevice Add(const MatrixDevice &a, const MatrixDevice &b){
MatrixDevice result(a);
result.Add(b);
return result;
}
//Add two MatrixDevice to result MatrixDevice
template<class T>
void Add(const MatrixDevice &a, const MatrixDevice &b, MatrixDevice &result){
MatrixCudaOperations(a.data, b.data, result.data, a.rows, a.cols, EOperation::ADD);
}

//Operations with Number
//Add T number to this
template<class T>
MatrixDevice& MatrixDevice::Add(T &other){
MatrixCudaOperations(data, other, data, rows, cols, EOperation::ADD);
return *this;
} 
//Add T number to MatrixDevice and return the result as new MatrixDevice
template<class T>
MatrixDevice Add(const MatrixDevice &a, T &b){
MatrixDevice result(a);
result.Add(b);
return result;
}
//Add T number with MatrixDevice to result MatrixDevice
template<class T>
void Add(const MatrixDevice &a, T &b, MatrixDevice &result){
MatrixCudaOperations(a.data, b, result.data, a.rows, a.cols, EOperation::ADD);
}

在内核中,我为MatrixCudaOperations声明重载函数,并且任何函数中的代码都是相同的。我用模板尝试了一下,但如果我需要MatrixDevice类中的外部解密,它就不起作用。

kernel.cu

template<class T> __global__
void d_Add(const T* a, const T* b, T* result){
//code
}
template<class T> __global__
void d_Add(const T* a, T b, T* result){
//code
}
void MatrixCudaOperations(const float* a, const float* b, float* result, size_t rows, size_t cols, EOperation operation){
dim3 blocksize(rows, cols);
switch(operation){
case ADD:
d_Add<<<1,blocksize>>>(a, b, result);
break;
//other cases, subtract, multiply...
}
}
void MatrixCudaOperations(const float* a, float b, float* result, size_t rows, size_t cols, EOperation operation){
dim3 blocksize(rows, cols);
switch(operation){
case ADD:
d_Add<<<1,blocksize>>>(a, b, result);
break;
//other cases, subtract, multiply...
}
}
void MatrixCudaOperations(const double* a, const double* b, double* result, size_t rows, size_t cols, EOperation operation){
dim3 blocksize(rows, cols);
switch(operation){
case ADD:
d_Add<<<1,blocksize>>>(a, b, result);
break;
//other cases, subtract, multiply...
}
}
void MatrixCudaOperations(const double* a, double b, double* result, size_t rows, size_t cols, EOperation operation){
dim3 blocksize(rows, cols);
switch(operation){
case ADD:
d_Add<<<1,blocksize>>>(a, b, result);
break;
//other cases, subtract, multiply...
}
}

从顶部开始。

template<class T>
class MatrixDevice;
template<class T>
static T const& to_matrix_data( T const& t ) { return t; }
template<class T>
static T const* to_matrix_data( MatrixDevice<T> const& m ) { return m.data; }
template<class T, class Rhs>
void AddInto(MatrixDevice<T>& target, MatrixDevice<T> const& src, Rhs const& rhs) {
MatrixCudaOperations(src.data, to_matrix_data<T>(rhs), target.data, EOperation::ADD );
}
template<class T>
class MatrixDevice{
T* data;
size_t rows;
size_t cols;
template<class Rhs>
MatrixDevice& +=(const Rhs &other)& {
AddInto( *this, *this, other );
return *this;
}
template<class Rhs>
friend MatrixDevice operator+(MatrixDevice lhs, Rhs const& rhs) {
lhs += rhs;
return lhs;
}
};

将单词Add用于3个不同的操作是不好的。一个是递增,另一个是相加,最后一个是加进去。

所以我写了一个免费的模板函数AddInto。然后基于增量并在此基础上添加。

我的加法最多需要比你的加法多移动一次,根据你矩阵的内部结构,移动是免费的。