<commit_before>#include "sequential.h"
#include "utils.h"
#include "tools.h"
#include "stdlib.h"
#include "control.h"
#include "profiler.h"
#include "groups.h"
#include "CL/cl.h"

// Calculate Q and R matrixes for QR Decomposition of matrix
void QR(cl_float* R, cl_float* Q, cl_uint arrayWidth, cl_uint arrayHeight)
{
	cl_float a;
	cl_float b;
	cl_float c;
	cl_float s;
	cl_float r;
	cl_float Rnew1[2048];
	cl_float Rnew2[2048];
	cl_float Qnew1[2048];
	cl_float Qnew2[2048];
	for (size_t j = 0; j < arrayWidth; j++)
	{
		for (size_t i = arrayHeight - 1; i > j; i--)
		{
			// Calculate Givens rotations
			a = R[arrayWidth * (i - 1) + j];
			b = R[arrayWidth * i + j];
			r = sqrt(a * a + b * b);
			c = a / r;
			s = -b / r;
			// Zero out elements in R matrix
			for (size_t k = j; k < arrayWidth; k++)
			{
				Rnew1[k] = R[arrayWidth * (i - 1) + k] * c - R[arrayWidth * i + k] * s;
				Rnew2[k] = R[arrayWidth * (i - 1) + k] * s + R[arrayWidth * i + k] * c;
			}
			// Copy new values back to R matrix
			for (size_t k = j; k < arrayWidth; k++)
			{
				R[arrayWidth * (i - 1) + k] = Rnew1[k];
				R[arrayWidth * i + k] = Rnew2[k];
			}
			// Update Q matrix
			for (size_t k = 0; k < arrayHeight; k++)
			{
				Qnew1[k] = Q[arrayHeight * (i - 1) + k] * c + Q[arrayHeight * i + k] * s;
				Qnew2[k] = -Q[arrayHeight * (i - 1) + k] * s + Q[arrayHeight * i + k] * c;
			}
			for (size_t k = 0; k < arrayHeight; k++)
			{
				Q[arrayHeight * (i - 1) + k] = Qnew1[k];
				Q[arrayHeight * i + k] = Qnew2[k];
			}
		}
	}
}

// Tests QR
int Test_QR(ResultsStruct* results)
{
	const size_t arrayWidth = 3;
	const size_t arrayHeight = 5;

	float* A = (float*)malloc(sizeof(float)*arrayWidth*arrayHeight);
	float* Q = (float*)malloc(sizeof(float)*arrayHeight*arrayHeight);


	float Atmp[] = { 1.000000, 0.000000,  0.000000,
					 1.000000, 1.000000,  1.000000,
					 1.000000, 2.000000,  4.000000,
					 1.000000, 3.000000,  9.000000,
					 1.000000, 4.000000, 16.000000 };
	std::cout << "A: " << std::endl;
	tools::printMatrix<float>(Atmp, arrayHeight, arrayWidth);

	// Initialize A
	for (size_t i = 0; i < arrayWidth * arrayHeight; ++i)
		A[i] = Atmp[i];

	// Initialize Q
	tools::CreateIdentityMatrix(arrayHeight, Q);

	// Perform QR Decomposition
	ProfilerStruct profiler;
	profiler.Start();
	QR(A, Q, arrayWidth, arrayHeight);
	profiler.Stop();
	float runTime = profiler.Log();

	std::cout << std::endl;
	std::cout << "R: " << std::endl;
	tools::printMatrix<float>(A, arrayHeight, arrayWidth);

	std::cout << std::endl;
	std::cout << "Q: " << std::endl;
	tools::printMatrix<float>(Q, arrayHeight, arrayHeight);

	free(A);
	free(Q);

	results->WindowsRunTime = (double)runTime;
	results->HasWindowsRunTime = true;
	return 0;
}

// Solves for coefficients in equation R*x=Q*b where
// R and Q are from QR Decomposition
// x is desired coefficients
// b is column vector of sampled data
// @param[in] R upper triangular matrix of size MxN (from QR Decomposition)
// @param[in] Qtb Q*b column vector of size Mx1
// @param[in] Dim coefficients to solve for - should match N columns size of R
// @param[out] Result is where x is stored
void BackSub(cl_float* R, cl_float* Qtb, size_t Dim, cl_float* Result)
{<commit_msg>Updated Test_QR() to transpose Q matrix.<commit_after>#include "sequential.h"
#include "utils.h"
#include "tools.h"
#include "stdlib.h"
#include "control.h"
#include "profiler.h"
#include "groups.h"
#include "CL/cl.h"

// Calculate Q and R matrixes for QR Decomposition of matrix
void QR(cl_float* R, cl_float* Q, cl_uint arrayWidth, cl_uint arrayHeight)
{
	cl_float a;
	cl_float b;
	cl_float c;
	cl_float s;
	cl_float r;
	cl_float Rnew1[2048];
	cl_float Rnew2[2048];
	cl_float Qnew1[2048];
	cl_float Qnew2[2048];
	for (size_t j = 0; j < arrayWidth; j++)
	{
		for (size_t i = arrayHeight - 1; i > j; i--)
		{
			// Calculate Givens rotations
			a = R[arrayWidth * (i - 1) + j];
			b = R[arrayWidth * i + j];
			r = sqrt(a * a + b * b);
			c = a / r;
			s = -b / r;
			// Zero out elements in R matrix
			for (size_t k = j; k < arrayWidth; k++)
			{
				Rnew1[k] = R[arrayWidth * (i - 1) + k] * c - R[arrayWidth * i + k] * s;
				Rnew2[k] = R[arrayWidth * (i - 1) + k] * s + R[arrayWidth * i + k] * c;
			}
			// Copy new values back to R matrix
			for (size_t k = j; k < arrayWidth; k++)
			{
				R[arrayWidth * (i - 1) + k] = Rnew1[k];
				R[arrayWidth * i + k] = Rnew2[k];
			}
			// Update Q matrix
			for (size_t k = 0; k < arrayHeight; k++)
			{
				Qnew1[k] = Q[arrayHeight * (i - 1) + k] * c + Q[arrayHeight * i + k] * s;
				Qnew2[k] = -Q[arrayHeight * (i - 1) + k] * s + Q[arrayHeight * i + k] * c;
			}
			for (size_t k = 0; k < arrayHeight; k++)
			{
				Q[arrayHeight * (i - 1) + k] = Qnew1[k];
				Q[arrayHeight * i + k] = Qnew2[k];
			}
		}
	}
}

// Tests QR
int Test_QR(ResultsStruct* results)
{
	const size_t arrayWidth = 3;
	const size_t arrayHeight = 5;

	float* A = (float*)malloc(sizeof(float)*arrayWidth*arrayHeight);
	float* Q = (float*)malloc(sizeof(float)*arrayHeight*arrayHeight);


	float Atmp[] = { 1.000000, 0.000000,  0.000000,
					 1.000000, 1.000000,  1.000000,
					 1.000000, 2.000000,  4.000000,
					 1.000000, 3.000000,  9.000000,
					 1.000000, 4.000000, 16.000000 };
	std::cout << "A: " << std::endl;
	tools::printMatrix<float>(Atmp, arrayHeight, arrayWidth);

	// Initialize A
	for (size_t i = 0; i < arrayWidth * arrayHeight; ++i)
		A[i] = Atmp[i];

	// Initialize Q
	tools::CreateIdentityMatrix(arrayHeight, Q);

	// Perform QR Decomposition
	ProfilerStruct profiler;
	profiler.Start();
	QR(A, Q, arrayWidth, arrayHeight);
	profiler.Stop();
	float runTime = profiler.Log();

	std::cout << std::endl;
	std::cout << "R: " << std::endl;
	tools::printMatrix<float>(A, arrayHeight, arrayWidth);

	std::cout << std::endl;
	std::cout << "Q: " << std::endl;
	float* Qtmp = (float*)malloc(sizeof(float)*arrayHeight*arrayHeight);
	tools::TransposeMatrix(Q, arrayHeight, arrayHeight, Qtmp);
	tools::printMatrix<float>(Qtmp, arrayHeight, arrayHeight);

	free(A);
	free(Q);
	free(Qtmp);

	results->WindowsRunTime = (double)runTime;
	results->HasWindowsRunTime = true;
	return 0;
}

// Solves for coefficients in equation R*x=Q*b where
// R and Q are from QR Decomposition
// x is desired coefficients
// b is column vector of sampled data
// @param[in] R upper triangular matrix of size MxN (from QR Decomposition)
// @param[in] Qtb Q*b column vector of size Mx1
// @param[in] Dim coefficients to solve for - should match N columns size of R
// @param[out] Result is where x is stored
void BackSub(cl_float* R, cl_float* Qtb, size_t Dim, cl_float* Result)
{
{'fraction_non_alphanumeric': 0.16391455840024238, 'fraction_numerical': 0.04544765944553855, 'mean_word_length': 3.2786779001944266, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2954366', 'n_tokens_mistral': 3022, 'n_tokens_neox': 2572, 'n_words': 737}