Add Cuda matrix size function & unittest
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
#include <iostream>
|
||||
#include "CudaMatrix.h"
|
||||
#include "Function3D.h"
|
||||
#include "Function2D.h"
|
||||
#include "Function.h"
|
||||
#include "CudaMatrixPrivate.cuh"
|
||||
#include <cuda_runtime.h>
|
||||
|
||||
|
||||
|
||||
//必须在Eigen之前
|
||||
#include "AuroraDefs.h"
|
||||
@@ -61,14 +66,34 @@ Matrix Aurora::ones(int aRow, int aColumn, int aSlice) {
|
||||
size_t arraySize = rowSize * colSize* sliceSize;
|
||||
float* data = malloc(arraySize);
|
||||
float one = 1.0;
|
||||
cblas_scopy(arraySize,&one,0,data,1);
|
||||
cblas_scopy(arraySize,&one,0,data,1.0f);
|
||||
return Matrix::New(data,rowSize,colSize,aSlice);
|
||||
}
|
||||
|
||||
CudaMatrix Aurora::onesCuda(int aRow, int aColumn, int aSlice){
|
||||
if (aRow == 0 || aColumn == 0)
|
||||
{
|
||||
std::cerr<<"ones function can create matrix with dim unit cont =0";
|
||||
return CudaMatrix();
|
||||
}
|
||||
int rowSize = aRow;
|
||||
int colSize = aColumn;
|
||||
int sliceSize = aSlice == 0 ? 1 : aSlice;
|
||||
size_t arraySize = rowSize * colSize* sliceSize;
|
||||
float* data = nullptr;
|
||||
cudaMalloc((void**)&data,arraySize*sizeof(float));
|
||||
::thrustFill(data,data+arraySize,1.0f);
|
||||
return CudaMatrix::fromRawData(data,rowSize,colSize,sliceSize);
|
||||
}
|
||||
|
||||
Matrix Aurora::ones(int aSquareRow) {
|
||||
return Aurora::ones(aSquareRow, aSquareRow);
|
||||
}
|
||||
|
||||
CudaMatrix Aurora::onesCuda(int aSquareRow) {
|
||||
return Aurora::onesCuda(aSquareRow, aSquareRow);
|
||||
}
|
||||
|
||||
Matrix Aurora::zeros(int aRow, int aColumn, int aSlice) {
|
||||
if (aRow == 0 || aColumn == 0)
|
||||
{
|
||||
@@ -81,18 +106,39 @@ Matrix Aurora::zeros(int aRow, int aColumn, int aSlice) {
|
||||
size_t arraySize = rowSize * colSize* sliceSize;
|
||||
float* data = malloc(arraySize);
|
||||
float zero = 0.0;
|
||||
cblas_scopy(arraySize,&zero,0,data,1);
|
||||
cblas_scopy(arraySize,&zero,0,data,0.0f);
|
||||
return Matrix::New(data,rowSize,colSize,sliceSize);
|
||||
}
|
||||
|
||||
CudaMatrix Aurora::zerosCuda(int aRow, int aColumn, int aSlice) {
|
||||
if (aRow == 0 || aColumn == 0)
|
||||
{
|
||||
std::cerr<<"zeros function can create matrix with dim unit cont =0";
|
||||
return CudaMatrix();
|
||||
}
|
||||
int rowSize = aRow;
|
||||
int colSize = aColumn;
|
||||
int sliceSize = aSlice == 0 ? 1 : aSlice;
|
||||
size_t arraySize = rowSize * colSize* sliceSize;
|
||||
float* data = nullptr;
|
||||
cudaMalloc((void**)&data,arraySize*sizeof(float));
|
||||
::thrustFill(data,data+arraySize,0.0f);
|
||||
return CudaMatrix::fromRawData(data,rowSize,colSize,sliceSize);
|
||||
}
|
||||
|
||||
|
||||
Matrix Aurora::zeros(int aSquareRow) {
|
||||
return Aurora::zeros(aSquareRow, aSquareRow);
|
||||
}
|
||||
|
||||
CudaMatrix Aurora::zerosCuda(int aSquareRow) {
|
||||
return Aurora::zerosCuda(aSquareRow, aSquareRow);
|
||||
}
|
||||
|
||||
Matrix Aurora::size(const Matrix &aMatrix)
|
||||
{
|
||||
if (aMatrix.isScalar()){
|
||||
float * output = Aurora::malloc(1);
|
||||
float * output =nullptr;
|
||||
output[0]=1;
|
||||
return Matrix::New(output,1,1,1);
|
||||
}
|
||||
@@ -119,11 +165,50 @@ Matrix Aurora::size(const Matrix &aMatrix)
|
||||
}
|
||||
}
|
||||
|
||||
CudaMatrix Aurora::size(const CudaMatrix &aMatrix){
|
||||
float * output=nullptr;
|
||||
if (aMatrix.isScalar()){
|
||||
cudaMalloc((void**)&output,sizeof(float));
|
||||
auto outMatrix = CudaMatrix::fromRawData(output,1,1,1);
|
||||
outMatrix.setValue(0, 1);
|
||||
return outMatrix;
|
||||
}
|
||||
else if (aMatrix.isVector()){
|
||||
cudaMalloc((void**)&output,sizeof(float)*2);
|
||||
auto outMatrix = CudaMatrix::fromRawData(output,2,1,1);
|
||||
outMatrix.setValue(0, aMatrix.getDimSize(0));
|
||||
outMatrix.setValue(1, aMatrix.getDimSize(1));
|
||||
return outMatrix;
|
||||
}
|
||||
//3D
|
||||
else if (aMatrix.getDimSize(2)>1){
|
||||
cudaMalloc((void**)&output,sizeof(float)*3);
|
||||
auto outMatrix = CudaMatrix::fromRawData(output,3,1,1);
|
||||
outMatrix.setValue(0,aMatrix.getDimSize(0));
|
||||
outMatrix.setValue(1,aMatrix.getDimSize(1));
|
||||
outMatrix.setValue(2,aMatrix.getDimSize(2));
|
||||
return outMatrix;
|
||||
}
|
||||
//2D matrix
|
||||
else{
|
||||
cudaMalloc((void**)&output,sizeof(float)*2);
|
||||
auto outMatrix = CudaMatrix::fromRawData(output,2,1,1);
|
||||
outMatrix.setValue(0,aMatrix.getDimSize(0));
|
||||
outMatrix.setValue(1,aMatrix.getDimSize(1));
|
||||
return outMatrix;
|
||||
}
|
||||
}
|
||||
|
||||
int Aurora::size(const Matrix &aMatrix,int dims)
|
||||
{
|
||||
return aMatrix.getDimSize(dims-1);
|
||||
}
|
||||
|
||||
int Aurora::size(const CudaMatrix &aMatrix,int dims)
|
||||
{
|
||||
return aMatrix.getDimSize(dims-1);
|
||||
}
|
||||
|
||||
Matrix Aurora::meshgridInterp3(const Matrix& aX, const Matrix& aY, const Matrix& aZ, const Matrix& aV, const Matrix& aX1, const Matrix& aY1, const Matrix& aZ1,InterpnMethod aMethod, float aExtrapval)
|
||||
{
|
||||
std::vector<Matrix> zTemps;
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#include "Matrix.h"
|
||||
#include "Function1D.h"
|
||||
#include "CudaMatrix.h"
|
||||
|
||||
namespace Aurora {
|
||||
|
||||
@@ -16,6 +17,8 @@ namespace Aurora {
|
||||
*/
|
||||
Matrix ones(int aRow, int aColumn, int aSlice = 0);
|
||||
|
||||
CudaMatrix onesCuda(int aRow, int aColumn, int aSlice = 0);
|
||||
|
||||
/**
|
||||
* 创建全部为1的方阵
|
||||
* @param aSquareRow
|
||||
@@ -23,6 +26,8 @@ namespace Aurora {
|
||||
*/
|
||||
Matrix ones(int aSquareRow);
|
||||
|
||||
CudaMatrix onesCuda(int aSquareRow);
|
||||
|
||||
/**
|
||||
* 创建全部为0的数组,矩阵
|
||||
* @param aRow 行数,必须大于0
|
||||
@@ -32,18 +37,25 @@ namespace Aurora {
|
||||
*/
|
||||
Matrix zeros(int aRow, int aColumn, int aSlice = 0);
|
||||
|
||||
CudaMatrix zerosCuda(int aRow, int aColumn, int aSlice = 0);
|
||||
|
||||
/**
|
||||
* 创建全部为0的方阵
|
||||
* @param aSquareRow
|
||||
* @return 全部为0的方阵
|
||||
*/
|
||||
Matrix zeros(int aSquareRow);
|
||||
CudaMatrix zerosCuda(int aSquareRow);
|
||||
Matrix interp3(const Matrix& aX, const Matrix& aY, const Matrix& aZ, const Matrix& aV, const Matrix& aX1, const Matrix& aY1, const Matrix& aZ1,InterpnMethod aMethod);
|
||||
Matrix meshgridInterp3(const Matrix& aX, const Matrix& aY, const Matrix& aZ, const Matrix& aV, const Matrix& aX1, const Matrix& aY1, const Matrix& aZ1,InterpnMethod aMethod, float aExtrapval);
|
||||
Matrix interpn(const Matrix& aX, const Matrix& aY, const Matrix& aZ, const Matrix& aV, const Matrix& aX1, const Matrix& aY1, const Matrix& aZ1,InterpnMethod aMethod);
|
||||
|
||||
Matrix size(const Matrix &aMatrix);
|
||||
CudaMatrix size(const CudaMatrix &aMatrix);
|
||||
|
||||
int size(const Matrix &aMatrix,int dims);
|
||||
int size(const CudaMatrix &aMatrix,int dims);
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user