在下面的例子中如何避免代码复制? C++/库达

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

编辑:这段代码有效,但看起来有很多代码复制部分,我找不到解决这个问题的方法。

在 MatrixDevice class 中我想调用 kerne.cu 中的内核函数。 我将 MatrixDevice class 缩减为仅展示这个概念我是如何实际操作的。

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

matrixdevice.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);
}

在内核中,我声明了 MatrixCudaOpertions 的重载函数,并且任何函数中的代码都是相同的。 我用模板尝试了这个,但如果我需要在 MatrixDevice class.

中进行外部声明,它就不起作用了

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;
  }
};

对 3 个不同的操作使用 Add 这个词是不好的。一个是increment by,一个是add,最后一个是add into。

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

我的加法最多比你的加法多走一步,根据你矩阵的内部结构,一步是免费的。