Program in parallel


 
Thread Tools Search this Thread
Top Forums Programming Program in parallel
# 1  
Old 04-12-2012
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?
Code:
#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[i]=true; 
		orSquare[i]=false; 
	}
	orFirst[1]=false; 
	for (i=2;i*i<=N;i++) 
		if (orFirst[i]==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[i]==true) 
			cout<<i<<" "; 
	cout<<"\n\nSquare No in the range of 1-10000:\n"; 
	for (i=1;i<=N;i++) 
		if (orSquare[i]==true)
			cout<<i<<" ";
	cin.get(); 
	return 0; 
}


Last edited by Scrutinizer; 04-13-2012 at 04:31 PM.. Reason: Changed [quote] tags to [code] tags....
# 2  
Old 04-12-2012
The simplest way would be to run four copies of the program, each checking a different range of numbers.
# 3  
Old 04-13-2012
Thanks for reply
but a the end I need to dispaly al of them ... how to do this?
Sorry but Im new in C
# 4  
Old 04-13-2012
The program appears to already display them. Do you need to display them a different way?
# 5  
Old 04-14-2012
Quote:
Originally Posted by Corona688
The simplest way would be to run four copies of the program, each checking a different range of numbers.
ok I changed to this... what you think?

Code:
#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[i]=true; 
				for (j=2;j<=square;j++) 
					if (i%j==0) 
					{
						orFirst[i]=false; 
						break; 
					}
			}
			#pragma omp section 
			{
				if (square*square==i) 
					orSquare[i]=true; 
				else 
					orSquare[i]=false; 
			}
		}
	}
	orFirst[1]=false; 
	cout<<"First No in range 1-10000: ";
	for (i=1;i<=N;i++) 
		if (orFirst[i]==true) 
			cout<<i<<" "; 
	cout<<"\n\nSquare No in the range of 1-10000: ";
	for (i=1;i<=N;i++) 
		if (orSquare[i]==true) 
			cout<<i<<" "; 
	cin.get(); 
	return 0; 
}

# 6  
Old 04-18-2012
I suggest to use threading and run each cacluation in a separated thread
# 7  
Old 04-28-2012
I know that the program is ok... but maybe someone know how to calculate:
- processing time.
- speed up and efficiency.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl program get a response before the program quits

I created a program, so a kid can practice there math on it. It dispenses varies math problems and the kid must input an answer. I also want it to grade the work they have done, but I can't find the best place for it to print out the grade. I have: if ( $response =~ m/^/ ) { $user_wants_to_quit... (1 Reply)
Discussion started by: germany1517
1 Replies

2. Programming

Parallel Processing Detection and Program Return Value Detection

Hey, for the purpose of a research project I need to know if a specific type of parallel processing is being utilized by any user-run programs. Is there a way to detect whether a program either returns a value to another program at the end of execution, or just utilizes any form of parallel... (4 Replies)
Discussion started by: azar.zorn
4 Replies

3. Homework & Coursework Questions

Calling compiled C program with Perl program

Long story short: I'm working inside of a Unix SSH under a bash shell. I have to code a C program that generates a random number. Then I have to call the compiled C program with a Perl program to run the C program 20 times and put all the generated random #s into a text file, then print that text... (1 Reply)
Discussion started by: jdkirby
1 Replies

4. Programming

Python program faster than C++ program.

I wrote a simple program that generates a random word 10,000,000 times. I wrote it in python, then in C++ and compared the two completion times. The python script was faster! Is that normal? Why would the python script be faster? I was under the impression that C++ was faster. What are some of... (2 Replies)
Discussion started by: cbreiny
2 Replies

5. Shell Programming and Scripting

FTP in parallel.

Hi, Can anyone let me know how I can run FTP scripts in parallel. I have a script for each file to perform an FTP from mainframe to Unix. I have many files that I need to transfer and each of them is thru a script. I want to run all the scripts in parallel to reduce the time taken for FTP.... (2 Replies)
Discussion started by: bornon2303
2 Replies

6. UNIX for Dummies Questions & Answers

Script to open program and send/execute command in program

Hi, i want to write a script that executes a program (exec?) . this program then requires a filename as input. how do i give it this input in the script so the program will be complete run and close by the script. e.g. exec prog.exe program then asks for filename "enter filename:"... (1 Reply)
Discussion started by: tuathan
1 Replies

7. Programming

A program to trace execution of another program

Hi, I wanted to know if i can write a program using switches and signals, etc to trace execution of other unix program which calls c program internally. If yes how? If not with signals and switches then are there any other methods apart from debugging with gdb/dbx. (3 Replies)
Discussion started by: jiten_hegde
3 Replies

8. UNIX for Dummies Questions & Answers

Parallel processing using AWK program

Hi All, could any expert tell me about writing a parallel processing program using AWK program is possible or not? Any example would be much appreciated... With Regards / Mysore Ganapati :confused: (5 Replies)
Discussion started by: ganapati
5 Replies

9. Shell Programming and Scripting

parallel processing

Hi I want to run two shell script files parallely. These two scripts are interacting with the database. can any body help on this Pls Regards Audippa naidu.M (3 Replies)
Discussion started by: audippa
3 Replies

10. Programming

running a parallel program

hi , i need to run a parallel program . for example; program1 { array=" the second program should called here : program 2" the execution should continue } the 2nd program should recieve an array of information as argument and it should... (4 Replies)
Discussion started by: bankpro
4 Replies
Login or Register to Ask a Question