Fix UnitTest add cudamatrix add and mul

This commit is contained in:
kradchen
2023-11-01 14:31:29 +08:00
parent fe0abf8ee6
commit 029b86013e
13 changed files with 1108 additions and 617 deletions

View File

@@ -9,7 +9,7 @@
#include <cuda_runtime.h>
#include "CudaMatrixPrivate.cuh"
using namespace Aurora;
namespace Aurora{
CudaMatrix::CudaMatrix(std::shared_ptr<float> aData, std::vector<int> aInfo, ValueType aValueType)
: mValueType(aValueType)
@@ -241,8 +241,66 @@ bool CudaMatrix::setBlockValue(int aDim,int aBeginIndx, int aEndIndex,float valu
return true;
}
CudaMatrix CudaMatrix::operator+(float aScalar) const{
if (isComplex())
{
std::cerr<<"Complex matrix not support operator+(float aScalar)"<<std::endl;
return CudaMatrix();
}
float* data = nullptr;
unsigned long long size = getDataSize() * getValueType();
cudaMalloc((void**)&data, sizeof(float) * size);
auto out = CudaMatrix::fromRawData(data, getDimSize(0), getDimSize(1), getDimSize(2), getValueType());
unaryAdd(getData(),aScalar,out.getData(),getDataSize());
return out;
}
CudaMatrix operator+(float aScalar, const CudaMatrix &aMatrix){
if (aMatrix.isComplex())
{
std::cerr<<"Complex matrix not support operator+(float aScalar)"<<std::endl;
return CudaMatrix();
}
float* data = nullptr;
unsigned long long size = aMatrix.getDataSize() * aMatrix.getValueType();
cudaMalloc((void**)&data, sizeof(float) * size);
auto out = CudaMatrix::fromRawData(data, aMatrix.getDimSize(0), aMatrix.getDimSize(1), aMatrix.getDimSize(2), aMatrix.getValueType());
unaryAdd(aMatrix.getData(),aScalar,out.getData(),aMatrix.getDataSize());
return out;
}
CudaMatrix& operator+(float aScalar, CudaMatrix &&aMatrix){
if (aMatrix.isComplex())
{
std::cerr<<"Complex matrix not support operator+(float aScalar)"<<std::endl;
return aMatrix;
}
unaryAdd(aMatrix.getData(),aScalar,aMatrix.getData(),aMatrix.getDataSize());
return aMatrix;
}
CudaMatrix& operator+(CudaMatrix &&aMatrix,float aScalar){
if (aMatrix.isComplex())
{
std::cerr<<"Complex matrix not support operator+(float aScalar)"<<std::endl;
return aMatrix;
}
unaryAdd(aMatrix.getData(),aScalar,aMatrix.getData(),aMatrix.getDataSize());
return aMatrix;
}
CudaMatrix CudaMatrix::operator+(const CudaMatrix &aMatrix) const{
if (this->getDataSize() != aMatrix.getDataSize()) return CudaMatrix();
if (this->getDataSize() != aMatrix.getDataSize()) {
std::cerr<<"operator+ must with Same DataSize, now the matrix0 size is "<<this->getDataSize()
<<" and the matrix1 size is "<<aMatrix.getDataSize()<<std::endl;
return CudaMatrix();
}
if (this->isComplex() != aMatrix.isComplex()) {
std::cerr<<"operator+ must with Data type, now the matrix0 type is "<<(this->isComplex()?"Comples":"Real")
<<" and the matrix1 type is "<<(aMatrix.isComplex()?"Comples":"Real")<<std::endl;
return CudaMatrix();
}
float* data = nullptr;
unsigned long long size = getDataSize() * getValueType();
cudaMalloc((void**)&data, sizeof(float) * size);
@@ -250,4 +308,128 @@ CudaMatrix CudaMatrix::operator+(const CudaMatrix &aMatrix) const{
unaryAdd(this->getData(),aMatrix.getData(),out.getData(),this->getDataSize());
return out;
}
CudaMatrix CudaMatrix::operator+(CudaMatrix &&aMatrix) const{
if (this->getDataSize() != aMatrix.getDataSize()) {
std::cerr<<"operator+ must with Same DataSize, now the matrix0 size is "<<this->getDataSize()
<<" and the matrix1 size is "<<aMatrix.getDataSize()<<std::endl;
return CudaMatrix();
}
if (this->isComplex() != aMatrix.isComplex()) {
std::cerr<<"operator+ must with Data type, now the matrix0 type is "<<(this->isComplex()?"Comples":"Real")
<<" and the matrix1 type is "<<(aMatrix.isComplex()?"Comples":"Real")<<std::endl;
return CudaMatrix();
}
unaryAdd(this->getData(),aMatrix.getData(),aMatrix.getData(),this->getDataSize());
return aMatrix;
}
CudaMatrix operator+(CudaMatrix &&aMatrix,CudaMatrix &aOther){
if (aOther.getDataSize() != aMatrix.getDataSize()) {
std::cerr<<"operator+ must with Same DataSize, now the matrix0 size is "<<aMatrix.getDataSize()
<<" and the matrix1 size is "<<aOther.getDataSize()<<std::endl;
return CudaMatrix();
}
if (aOther.isComplex() != aMatrix.isComplex()) {
std::cerr<<"operator+ must with Data type, now the matrix0 type is "<<(aMatrix.isComplex()?"Comples":"Real")
<<" and the matrix1 type is "<<(aOther.isComplex()?"Comples":"Real")<<std::endl;
return CudaMatrix();
}
unaryAdd(aOther.getData(),aMatrix.getData(),aMatrix.getData(),aOther.getDataSize());
return aMatrix;
}
// mul
CudaMatrix CudaMatrix::operator*(float aScalar) const{
if (isComplex())
{
std::cerr<<"Complex matrix not support operator+(float aScalar)"<<std::endl;
return CudaMatrix();
}
float* data = nullptr;
unsigned long long size = getDataSize() * getValueType();
cudaMalloc((void**)&data, sizeof(float) * size);
auto out = CudaMatrix::fromRawData(data, getDimSize(0), getDimSize(1), getDimSize(2), getValueType());
unaryMul(getData(),aScalar,out.getData(),getDataSize());
return out;
}
CudaMatrix operator*(float aScalar, const CudaMatrix &aMatrix){
if (aMatrix.isComplex())
{
std::cerr<<"Complex matrix not support operator+(float aScalar)"<<std::endl;
return CudaMatrix();
}
float* data = nullptr;
unsigned long long size = aMatrix.getDataSize() * aMatrix.getValueType();
cudaMalloc((void**)&data, sizeof(float) * size);
auto out = CudaMatrix::fromRawData(data, aMatrix.getDimSize(0), aMatrix.getDimSize(1), aMatrix.getDimSize(2), aMatrix.getValueType());
unaryMul(aMatrix.getData(),aScalar,out.getData(),aMatrix.getDataSize());
return out;
}
CudaMatrix& operator*(float aScalar, CudaMatrix &&aMatrix){
if (aMatrix.isComplex())
{
std::cerr<<"Complex matrix not support operator+(float aScalar)"<<std::endl;
return aMatrix;
}
unaryMul(aMatrix.getData(),aScalar,aMatrix.getData(),aMatrix.getDataSize());
return aMatrix;
}
CudaMatrix& operator*(CudaMatrix &&aMatrix,float aScalar){
if (aMatrix.isComplex())
{
std::cerr<<"Complex matrix not support operator+(float aScalar)"<<std::endl;
return aMatrix;
}
unaryMul(aMatrix.getData(),aScalar,aMatrix.getData(),aMatrix.getDataSize());
return aMatrix;
}
CudaMatrix CudaMatrix::operator*(const CudaMatrix &aMatrix) const{
if (this->getDataSize() != aMatrix.getDataSize()) {
std::cerr<<"operator+ must with Same DataSize, now the matrix0 size is "<<this->getDataSize()
<<" and the matrix1 size is "<<aMatrix.getDataSize()<<std::endl;
return CudaMatrix();
}
if (this->isComplex() != aMatrix.isComplex()) {
std::cerr<<"operator+ must with Data type, now the matrix0 type is "<<(this->isComplex()?"Comples":"Real")
<<" and the matrix1 type is "<<(aMatrix.isComplex()?"Comples":"Real")<<std::endl;
return CudaMatrix();
}
float* data = nullptr;
unsigned long long size = getDataSize() * getValueType();
cudaMalloc((void**)&data, sizeof(float) * size);
auto out = CudaMatrix::fromRawData(data, getDimSize(0), getDimSize(1), getDimSize(2), getValueType());
unaryMul(this->getData(),aMatrix.getData(),out.getData(),this->getDataSize());
return out;
}
CudaMatrix CudaMatrix::operator*(CudaMatrix &&aMatrix) const{
if (this->getDataSize() != aMatrix.getDataSize()) {
std::cerr<<"operator+ must with Same DataSize, now the matrix0 size is "<<this->getDataSize()
<<" and the matrix1 size is "<<aMatrix.getDataSize()<<std::endl;
return CudaMatrix();
}
if (this->isComplex() != aMatrix.isComplex()) {
std::cerr<<"operator+ must with Data type, now the matrix0 type is "<<(this->isComplex()?"Comples":"Real")
<<" and the matrix1 type is "<<(aMatrix.isComplex()?"Comples":"Real")<<std::endl;
return CudaMatrix();
}
unaryMul(this->getData(),aMatrix.getData(),aMatrix.getData(),this->getDataSize());
return aMatrix;
}
CudaMatrix operator*(CudaMatrix &&aMatrix,CudaMatrix &aOther){
if (aOther.getDataSize() != aMatrix.getDataSize()) {
std::cerr<<"operator+ must with Same DataSize, now the matrix0 size is "<<aMatrix.getDataSize()
<<" and the matrix1 size is "<<aOther.getDataSize()<<std::endl;
return CudaMatrix();
}
if (aOther.isComplex() != aMatrix.isComplex()) {
std::cerr<<"operator+ must with Data type, now the matrix0 type is "<<(aMatrix.isComplex()?"Comples":"Real")
<<" and the matrix1 type is "<<(aOther.isComplex()?"Comples":"Real")<<std::endl;
return CudaMatrix();
}
unaryMul(aOther.getData(),aMatrix.getData(),aMatrix.getData(),aOther.getDataSize());
return aMatrix;
}
}
#endif // USE_CUDA