📄 testsparsematrixvector2.cpp
字号:
// TestSparseMatrixVector2.cpp: implementation of the TestSparseMatrixVector2 class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "shadow.h"
#include "TestSparseMatrixVector2.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#include "CommonHW.h"
#include "pbuffer.h"
#include "VectorOnGPU.h"
#include "SparseMatrixOnGPU2.h"
#include "SparseMatrixVectorMultiply2.h"
void TestBrookSparseMatVec()
{
CBrookMatrix A;
CVectorOnGPU x, result;
int i,j,k=0;
int dim = 1024;//4;//
/*
float M[] = {3,0,0,2,
0,0,0,1,
0,4,0,0,
6,0,0,8};
float vx[] = {8,3,6,2};
float Vr[4];
float *M = new float[dim*dim];
for(i=0;i<dim;i++)
{
for(j=0;j<dim;j++)
{
M[i*dim+j] = (float)rand()/RAND_MAX;
if( i > j+5 || j > i+5)
{
M[i*dim+j] = 0;
k++;
}
}
}
float *vx = new float[dim];
float *Vr = new float[dim];
for(i=0;i<dim;i++)
vx[i] = (float)rand()/RAND_MAX;
*/
dim = 9;
float M[] ={ 0.00, -1.00 , 0.00 , -1.00 , 0.00 , 0.00 , 0.00 , 0.00 , 0.00 ,
-1.00 , 0.00 , -1.00 , 0.00 , -1.00 , 0.00 , 0.00 , 0.00 , 0.00 ,
0.00 , -1.00 , 0.00 , 0.00 , 0.00 , -1.00 , 0.00 , 0.00 , 0.00 ,
-1.00 , 0.00 , 0.00 , 0.00 , -1.00 , 0.00 , -1.00 , 0.00 , 0.00 ,
0.00 , -1.00 , 0.00 , -1.00 , 0.00 , -1.00 , 0.00 , -1.00 , 0.00 ,
0.00 , 0.00 , -1.00 , 0.00 , -1.00 , 0.00 , 0.00 , 0.00 , -1.00 ,
0.00 , 0.00 , 0.00 , -1.00 , 0.00 , 0.00 , 0.00 , -1.00 , 0.00 ,
0.00 , 0.00 , 0.00 , 0.00 , -1.00 , 0.00 , -1.00 , 0.00 , -1.00 ,
0.00 , 0.00, 0.00 , 0.00 , 0.00 , -1.00 , 0.00 , -1.00 , 0.00 };
float vx[] = {0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0};
float Vr[4];
A.SetMultiData(M, dim);
//A.SetData(M, dim);
x.SetData(vx, dim);
result.SetData(NULL, dim);
int Width = x.GetWidth();
int Height = x.GetHeight();
////// Render To Texture with Float PBuffer
for(i=0;i<2;i++)
{
if( pbuffer[i] != NULL)
{
delete pbuffer[i];
pbuffer[i] = NULL;
}
pbuffer[i] = new CFloatPBuffer(Width, Height);
pbuffer[i]->Initialize();
pbuffer[i]->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();
pbuffer[i]->EndCapture();
}
TEST_RESULT_START
//for(i=0;i<30;i++)
MultiBrookSparseMatVecFP(A, x, result);
//BrookSparseMatVecFP(A, x, result);
TEST_RESULT_END
float *vr = result.GetData(dim);
///////Verify the result
FILE *fp = fopen("testTime.txt","w");
fprintf(fp,"time = %f\n", time_cost/30);
fprintf(fp,"zero number= %d, ratio=%f\n", k, (float)k/(dim*dim));
TEST_RESULT_START
float diff = 0;
for(i=0;i<dim;i++)
{
Vr[i] = 0;
for(j=0;j<dim;j++)
Vr[i] += M[i*dim+j]*vx[j];
diff += vr[i] - Vr[i];
fprintf(fp,"%d CPU = %f, GPU = %f, diff=%f\n", i, Vr[i], vr[i], Vr[i]-vr[i]);
}
TEST_RESULT_END
fprintf(fp,"CPU time = %f\n", time_cost);
fprintf(fp,"diff on GPU&CPU= %f\n", diff);
fprintf(fp,"PassNum = %d,data width=%d\n", A.GetPassNum(), A.GetDataWidth());
/*
for(i=0;i<dim;i++)
{
for(j=0;j<dim;j++)
fprintf(fp,"%f ", M[i*dim+j]);
fprintf(fp,"\n");
}
*/
fclose(fp);
delete[] vr;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -