Click here to Skip to main content
15,896,557 members
Articles / Programming Languages / CUDA

GPGPU on Accelerating Wave PDE

Rate me:
Please Sign up or sign in to vote.
4.93/5 (50 votes)
13 Oct 2012CPOL41 min read 50.2K   1.4K   57  
A Wave PDE simulation using GPGPU capabilities
/*******************************************************************************
						Wave PDE CPU Simulation

	Author: Alesiani Marco (marco.diiga@gmail.com @marcodiiga)
	
 Redistribution and use in source and binary forms, with or
 without modification, are permitted for commercial and
 non-commercial purposes, provided that the following
 conditions are met:

 * Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
 * The names of the developers or contributors may not be used to
   endorse or promote products derived from this software without
   specific prior written permission.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
   CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
   EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

	See the attached documentation for more information

/******************************************************************************/
#include <string.h>

// This class handles matrix objects
class sMatrix
{
public:
	int rows, columns;
	float *values;

	sMatrix(int height, int width)
	{
		if (height == 0 || width == 0)
			throw "Matrix constructor has 0 size";

		rows = height;
		columns = width;

		values = new float[rows*columns];
	}

	// Copy constructor, this is needed to perform a deep copy (not a shallow one)
	sMatrix(const sMatrix& mt)
	{
		this->rows = mt.rows;
		this->columns = mt.columns;

		this->values = new float[rows*columns];

		// Copy the values
		memcpy(this->values, mt.values, this->rows*this->columns*sizeof(float));
	}

	~sMatrix()
	{
		delete [] values;
	}

	// Allows matrix1 = matrix2
	sMatrix& operator= (sMatrix const& m)
	{
		if(m.rows != this->rows || m.columns != this->columns)
		{
			throw "Size mismatch";
		}

		memcpy(this->values,m.values,this->rows*this->columns*sizeof(float));

		return *this; // Since "this" continues to exist after the function call, it is perfectly legit to return a reference
	}
	
	// Allows both matrix(3,3) = value and value = matrix(3,3)
	float& operator() (const int row, const int column)
	{
		// May be suppressed to slightly increase performances
		if (row < 0 || column < 0 || row > this->rows || column > this->columns)
			throw "Size mismatch";

		return values[row*columns+column]; // Since the float value continues to exist after the function call, it is perfectly legit to return a reference
	}

	// Allows scalar*matrix (e.g. 3*matrix) for each element
	sMatrix operator* (const float scalar)
	{
		sMatrix result(this->rows, this->columns);
		// Multiply each value by the scalar
		for(int i=0; i<rows*columns; i++)
		{
				result.values[i] = this->values[i]*scalar;
		}
		return result; // Copy constructor
	}

	// Allows matrix+matrix (if same size)
	sMatrix operator+ (const sMatrix& mt)
	{
		if (this->rows != mt.rows || this->columns != mt.columns)
			throw "Size mismatch";

		sMatrix result(this->rows, this->columns);
		// Sum each couple of values
		for(int i=0; i<rows; i++)
		{
			for(int j=0; j<columns; j++)
				result.values[i*columns+j] = this->values[i*columns+j] + mt.values[i*columns+j];
		}
		return result; // Copy constructor
	}
};

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Italy Italy
I'm a Computer Science Engineer and I've been programming with a large variety of technologies for years. I love writing software with C/C++, CUDA, .NET and playing around with reverse engineering

Comments and Discussions