📄 testmatrixmatrixoperations.cpp
字号:
// TestMatrixMatrixOperations.cpp: implementation of the TestMatrixMatrixOperations class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "shadow.h"
#include "TestMatrixMatrixOperations.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#include "CommonHW.h"
#include "pbuffer.h"
#include "DenseMatrixOnGPU.h"
#include "DenseMatrixMatrixOperation.h"
void TestclMatMat()
{
CDenseMatrixOnGPU A, B, Mresult;
int i,j;
float *MA = new float[DIM_TEST*DIM_TEST];
float *MB = new float[DIM_TEST*DIM_TEST];
float *MR = new float[DIM_TEST*DIM_TEST];
/////input data
for(i=0;i<DIM_TEST;i++)
{
for(j=0;j<DIM_TEST;j++)
{
MA[i*DIM_TEST+j] = i*0.01 + j*0.01;
MB[i*DIM_TEST+j] = i*0.02 + j*0.02;
}
}
///////////////////////////////////////////////////////
//// Initialize matrix
//// for matrix*matrix
A.SetData(MA, DIM_TEST, DIM_TEST);
B.SetData(MB, DIM_TEST, DIM_TEST);
Mresult.SetData(NULL, DIM_TEST, DIM_TEST);
int Width = A.GetWidth();
int Height = A.GetHeight();
////// Render To Texture with Float PBuffer1
if( resultBuffer != NULL)
{
delete resultBuffer;
resultBuffer = NULL;
}
resultBuffer = new CFloatPBuffer(Width, Height);
resultBuffer->Initialize();
resultBuffer->BeginCapture();
//////////////////////////////////////////////
///// Clear the framebuffer firstly to avoid the previous computation
///// This step is important to avoid computation errors
// The viewing settings.
glShadeModel( GL_FLAT );
glDisable ( GL_DEPTH_TEST );
glDisable(GL_CULL_FACE);
glClearColor( 0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0,0,Width,Height);
glMatrixMode ( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0, Width, 0, Height, -1.0, 1.0 );
glMatrixMode ( GL_MODELVIEW );
glLoadIdentity();
TEST_RESULT_START
//clMatMatWithRC(A, B, Mresult);
//clMatMatWithBlend(A, B, Mresult);
clMatMatFP(A, B, Mresult);
//clMatMatBlockFP(A, B, Mresult);
//clMatMat4ChannelFP(A, B, Mresult);
TEST_RESULT_END
resultBuffer->EndCapture();
FILE *fp = fopen("testTime.txt","w");
fprintf(fp,"\nMatrix*Matrix on CPU time=%f\n", time_cost);
int w,h;
///Verify the result
TEST_RESULT_START
float *vm = Mresult.GetData(h,w);
TEST_RESULT_END
int k;
float diff = 0;
for(i=0;i<w;i++)
{
for(j=0;j<w;j++)
{
MR[i*w+j] = 0;
for(k=0;k<w;k++)
MR[i*w+j] += MA[i*w+k]*MB[k*w+j];
diff += vm[i*w+j] - MR[i*w+j];
fprintf(fp,"i=%d,j=%d, GPU=%f, CPU=%f, diff=%f\n",i,j,vm[i*w+j], MR[i*w+j], vm[i*w+j]-MR[i*w+j]);
}
}
fprintf(fp,"DIFF=%f\n", diff);
delete[] vm;
fclose(fp);
delete[] MA;
delete[] MB;
delete[] MR;
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -