Difference between revisions of "Team Name (Official)"

From CDOT Wiki
Jump to: navigation, search
(Assignment 2)
(Topic)
Line 78: Line 78:
 
Making parallel an application which calculates the first n primes.
 
Making parallel an application which calculates the first n primes.
  
 +
<pre>
 +
//DPS915 Assignment 1
 +
//
 +
//Graeme Smyth (#032-653-081)
 +
//Code written by Graeme Smyth, with inspiration from course Workshop 1
 +
//February 7th, 2013
  
 +
#include <iostream>
 +
#include <iomanip>
 +
#include <cstdlib>
 +
#include <cmath>
 +
#include <ctime>
 +
using namespace std;
 +
 +
bool isPrime(int x)
 +
{
 +
for(int a = 0; a < x; a++) //a is any integer [0,x)
 +
for(int b = 0; b < x; b++) //b is any integer [0,x)
 +
if(a * b == x) //If a * b = x, then x has two integers that multiply to form it,
 +
return false; //hence x is not prime
 +
 +
return true; //If we haven't returned by this point, x must be prime.
 +
}
 +
 +
void findPrimes(int* answers, int n)
 +
{
 +
for(int i = 1, primesFound = 0; primesFound < n; i++) //Keep going until we have found n primes
 +
if(isPrime(i)) //Test if "i" is prime
 +
answers[primesFound++] = i; //If it is, record it
 +
}
 +
 +
//Main takes one argument, integer n, and calculates the first n primes.
 +
int main(int argc, char* argv[])
 +
{
 +
time_t timeStart, timeEnd;
 +
 +
//Check argument count
 +
if (argc != 2)
 +
{
 +
cerr << "**invalid number of arguments**" << endl;
 +
return 1;
 +
}
 +
 +
int n = atoi(argv[1]);
 +
int* answers = new int[n];
 +
 +
timeStart = time(nullptr); //Start timing
 +
 +
findPrimes(answers, n);
 +
 +
timeEnd = time(nullptr); //Finish timing
 +
 +
delete answers;
 +
 +
cout << setprecision(3);
 +
cout << "Elapsed time : " << difftime(timeEnd, timeStart) << endl;
 +
}
 +
</pre>
  
 
====[[User:Rhotin | Roman Hotin]]====
 
====[[User:Rhotin | Roman Hotin]]====

Revision as of 23:43, 11 April 2013


GPU610/DPS915 | Student List | Group and Project Index | Student Resources | Glossary

Teamname.png

Project Name TBA

Team Members

  1. Graeme Smyth, Some responsibility
  2. Roman Hotin, Some other responsibility

Email All

Progress

Assignment 3

Assignment 3 was very successful.

We are submitting the files now (7:00pm April 11th).

This Wiki page will be updated with details before the due date (11:55pm April 12th).

Assignment 2

A2 featured a significantly improved prime-counting algorithm, coupled with GPU integration.


Going from A1 to A2, our code experienced a speed-up of 6,015,300% (or 60,154x faster (literally - A1 was that inefficient)).


Kernel code for A2:

__global__ void findPrimes(int* answer, int n)
{
	int i = threadIdx.x;
	int j = blockIdx.x * blockDim.x + threadIdx.x;
	__shared__ int blockSum[ntpb];

	int check = j+1;

	blockSum[i] = 0;
	__syncthreads();

	if(check >= 2 && check <= n)
	{
		bool flag = true;			//Assume prime
		for(int x = 2; x < check && flag; x++)	//Check all numbers 2 to "check"
			if(check % x == 0)		//If divisible by x
				flag = false;		//Found to not be prime


		if(flag)			//If prime
			blockSum[i] = 1;	//Add one to our numbers
	}

	__syncthreads();	//Ensure all threads are caught up

	for (int stride = blockDim.x >> 1; stride > 0; stride >>= 1)
	{
		if (i < stride)
			blockSum[i] += blockSum[i + stride];
		__syncthreads();
	}

	if (i == 0)
		answer[blockIdx.x] = blockSum[0];
}


Sample output from A2, for demonstration:

Gtsmyth A2.png



Demonstration of how efficient A2 runs for n = 1,000 (for comparison, A1 took 20.78 seconds).


A2 2.png

Assignment 1

Graeme Smyth

Topic

Making parallel an application which calculates the first n primes.

//DPS915 Assignment 1
//
//Graeme Smyth (#032-653-081)
//Code written by Graeme Smyth, with inspiration from course Workshop 1
//February 7th, 2013

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std;

bool isPrime(int x)
{
	for(int a = 0; a < x; a++)		//a is any integer [0,x)
		for(int b = 0; b < x; b++)	//b is any integer [0,x)
			if(a * b == x)			//If a * b = x, then x has two integers that multiply to form it,
				return false;		//hence x is not prime

	return true;	//If we haven't returned by this point, x must be prime.
}

void findPrimes(int* answers, int n)
{
	for(int i = 1, primesFound = 0; primesFound < n; i++)	//Keep going until we have found n primes
		if(isPrime(i))		//Test if "i" is prime
			answers[primesFound++] = i;		//If it is, record it
}

//Main takes one argument, integer n, and calculates the first n primes.
int main(int argc, char* argv[])
{
	time_t timeStart, timeEnd;
	
	//Check argument count
	if (argc != 2)
	{
		cerr << "**invalid number of arguments**" << endl;
		return 1;
	}

	int n = atoi(argv[1]);
	int* answers = new int[n];
	
	timeStart = time(nullptr);	//Start timing

	findPrimes(answers, n);

	timeEnd = time(nullptr);	//Finish timing

	delete answers;

	cout << setprecision(3);
	cout << "Elapsed time : " << difftime(timeEnd, timeStart) << endl;
}

Roman Hotin

Topic

encrypting text


#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <string>
#include <cctype>

using namespace std;
void Encrypt(string&);
string Decrypt(string strTarget);


int main(int argc, char* argv[]) {
	//initialize and get the string from the user
	string strTarget;
	cout << "Enter a string to encrypt: ";
	//getline(cin,strTarget);
	strTarget = argv[1];
	string temp(strTarget);
	Encrypt(strTarget);

	cout << "Encrypted: " << strTarget << endl;
	cout << "Decrypted: " << Decrypt(strTarget) << endl;

	return 0;
}

void Encrypt(string &strTarget)
{
	int len = strTarget.length();
	char a;
	string strFinal(strTarget);
	for (int i = 0; i <= (len-1); i++)
	{
		a = strTarget.at(i); 
		int b = (int)a; //get the ASCII value of 'a'
		b += 2; //Mulitply the ASCII value by 2
		if (b > 254) { b = 254; }
		a = (char)b; //Set the new ASCII value back into the char
		strFinal.insert(i , 1, a); //Insert the new Character back into the string
	}
	string strEncrypted(strFinal, 0, len);
	strTarget = strEncrypted;
}

string Decrypt(string strTarget)
{
	int len = strTarget.length();
	char a;
	string strFinal(strTarget);
	for (int i = 0; i <= (len-1); i++)
	{
		a = strTarget.at(i);
		int b = (int)a;
		b -= 2;

		a = (char)b;
		strFinal.insert(i, 1, a);
	}
	string strDecrypted(strFinal, 0, len);
	return strDecrypted;
}