⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testsparsematrixvector3.cpp

📁 PDE simulator on GPU.
💻 CPP
字号:
// TestSparseMatrixVector3.cpp: implementation of the TestSparseMatrixVector3 class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "shadow.h"
#include "TestSparseMatrixVector3.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 "SparseMatrixOnGPU3.h"
#include "SparseMatrixVectorMultiply3.h"

void TestKrugerSparseMatVec()
{
	CVectorOnGPU x, Vresult;
	CKrugerMatrix SA;
	int i,j,k;

	int dim = 8;
	float M[] = {0.3,0,0.2,0,0.3,0,0.2,0,
				 0.1,0.4,0,0.1,0.1,0.4,0,0.1,
				 0,0.3,0.2,0,0,0.3,0.2,0,
				 0.6,0.2,0.1,0.8,0,0.3,0.2,0,
				 0.3,0,0.2,0,0.3,0,0.2,0,
				 0.1,0.4,0,0.1,0.1,0.4,0,0.1,
				 0,0.3,0.2,0,0,0.3,0.2,0,
				 0.6,0.2,0.1,0.8,0,0.3,0.2,0};

	float vx[] = {0.8,0.3,0.6,0.2,0.8,0.3,0.6,0.2};
	float Vr[8];

	///////////////////////////////////////////////////////
	//// Initialize vectors and matrix
TEST_RESULT_START
	x.SetData(vx,dim);
TEST_RESULT_END
	Vresult.SetData(NULL, dim);
	
	//// for sparse matrix-vector operation
	SA.SetData(M, dim);
	
	int Width = x.GetWidth();
	int Height = x.GetHeight();

	////// Render To Texture with Float PBuffer
	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
	KrugerSparseMatVectFP(SA, x, Vresult);
TEST_RESULT_END

	resultBuffer->EndCapture();

	int Dim_Vector;

TEST_RESULT_START	
	float *vr = Vresult.GetData(Dim_Vector);
TEST_RESULT_END

	///////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];
	}
TEST_RESULT_END

	fprintf(fp,"CPU time = %f\n", time_cost);
	
	fprintf(fp,"diff on GPU&CPU= %f\n", diff);

	fclose(fp);

	return;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -