以下示例中如何避免代码复制?C++/库达
How avoid code replication in the following example? C++ / Cuda
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
。然后基于增量并在此基础上添加。
我的加法最多需要比你的加法多移动一次,根据你矩阵的内部结构,移动是免费的。
相关文章:
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 使用strcpy将char数组的元素复制到另一个数组
- 是否可以初始化不可复制类型的成员变量(或基类)
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 复制列表初始化的隐式转换的等级是多少
- 当从函数参数中的临时值调用复制构造函数时
- 有可能在Armadillo中复制MATLAB circshift方法吗
- 复制几乎为空的数组的最快方法
- 以下示例中如何避免代码复制?C++/库达
- 如果有一个模板构造函数只有一个泛型参数,为什么我必须有一个复制构造函数
- 为什么需要复制构造函数,在哪些情况下它们非常有用
- 不能将复制初始化与隐式转换的多个步骤一起使用
- 当有分配器意识的容器被复制/移动时,反弹分配器是否被复制/移走
- 为什么复制而不是移动数据元素?
- 文件系统:复制功能的速度秘诀是什么
- 使用仅使用一次的变量调用的复制构造函数.这可能是通过调用move构造函数进行编译器优化的情况吗
- 为什么类中的ostringstream类型的成员会导致";调用隐含删除复制构造函数";错误
- 使lambda不可复制/不可移动