Changes

Jump to: navigation, search

DPS915/M-N-M

1,372 bytes added, 11:09, 7 March 2013
Assignment 2
}
</pre>
<h3>Version of prime generator running on GPU</h3>
<pre>
 
# include <cmath> // This library enable the use of sqrt.
# include <iostream>
using namespace std;
void primenum(long double); // Prototype...
int c = 0;
int main(){
long double x = 0;
cout<<"\n This program will generate all prime numbers up to the"
<<"\n number you have entered below...\n";
cout<<"\n Please enter a number: ";
cin>> x;
cout<<"\n Here are all the prime numbers up to "<<x<<".\n";
primenum(x); //function invocation...
cout<<endl<<"\nThere are "<<c
<<" prime numbers less than or equal to "<<x<<".\n\n";
return 0;
}
// This function will determine the primenumbers up to num.
void primenum(long double x){
//Array to hold generated primes on host
int *primes_h = new int[x];
//Device array to hold the primes on the device
int *primes_d = new int[x];
//allocate device mem
cudaMalloc((void**)&primes_d, x * sizeof(int));
//Kernal goes here
//error checking
//copy the array holding primes from device to host
cudaMemcpy(primes_h, primes_d, x * sizeof(int), cudaMemcpyDeviceToHost);
//display the primes
for(int i=0; i<x ; i++){
cout<<primes_h[i]<<endl;
}
getchar();
}
</pre>
=== Assignment 3 ===
1
edit

Navigation menu