Program in parallel

Hi all

I need to write a parallel program which is performs the operations below for each integer number in the range of 1 to 10000
and also:

  1. Find out if the numbers are prime or square numbers.
  2. Display the prime numbers in an array
  3. Display the square numbers in a different array

program should be in C, I have example but how to make this work in parallel?

#include <iostream> 

#define N 10000 

using namespace std; 

int main() 
{
	int i,j; 
	bool orFirst[N+1], 
	     orSquare[N+1]; 
	for (i=1;i<=N;i++) 
	{
		orFirst=true; 
		orSquare=false; 
	}
	orFirst[1]=false; 
	for (i=2;i*i<=N;i++) 
		if (orFirst==true) 
			for (j=i*i;j<=N;j+=i) 


				orFirst[j]=false; 
	for (i=1;i*i<=N;i++) 
		orSquare[i*i]=true; 
	cout<<"First No in range 1-10000:\n"; 
	for (i=1;i<=N;i++) 
		if (orFirst==true) 
			cout<<i<<" "; 
	cout<<"\n\nSquare No in the range of 1-10000:\n"; 
	for (i=1;i<=N;i++) 
		if (orSquare==true)
			cout<<i<<" ";
	cin.get(); 
	return 0; 
}

The simplest way would be to run four copies of the program, each checking a different range of numbers.

Thanks for reply
but a the end I need to dispaly al of them ... how to do this?
Sorry but Im new in C

The program appears to already display them. Do you need to display them a different way?

ok I changed to this... what you think?

#include <cmath> 
#include <iostream> 

#define N 10000 

using namespace std; i

int main() 
{
	int i,j, 
		square; 
	bool orFirst[N+1], 
		 orSquare[N+1]; 
	#pragma omp parallel for 
	for (i=1;i<=N;i++) 
	{
		square=sqrt(i); 
		#pragma omp sections
		{
			#pragma omp section 
			{
				orFirst=true; 
				for (j=2;j<=square;j++) 
					if (i%j==0) 
					{
						orFirst=false; 
						break; 
					}
			}
			#pragma omp section 
			{
				if (square*square==i) 
					orSquare=true; 
				else 
					orSquare=false; 
			}
		}
	}
	orFirst[1]=false; 
	cout<<"First No in range 1-10000: ";
	for (i=1;i<=N;i++) 
		if (orFirst==true) 
			cout<<i<<" "; 
	cout<<"\n\nSquare No in the range of 1-10000: ";
	for (i=1;i<=N;i++) 
		if (orSquare==true) 
			cout<<i<<" "; 
	cin.get(); 
	return 0; 
}

I suggest to use threading and run each cacluation in a separated thread

I know that the program is ok... but maybe someone know how to calculate:

  • processing time.
  • speed up and efficiency.

How much time does it take to run in series?

How much time does it take to run in parallel?

That`s the question I have no clue where to start... How to measure that?

time commandname ?

Thanks

---------- Post updated 04-29-12 at 03:58 PM ---------- Previous update was 04-28-12 at 08:32 PM ----------

one more question
How to compile this code using gcc?

Im able to compile this code on windows but when I try on Unix show error

I also change the header files to :
#include <math.h>
#include <stdio.h>
#include <omp.h>

In what way does it "show error"?

Can you show your code as you have it now?

UNIX code can generally be ported to Windows, but if you write code using Windows-only features that will be difficult to port to UNIX.

ok I resolve the problem ....
by mistake I used different header file

Thanks for help