openMP question

S

san

Hi,
I am not sure whether this is the right group to post this. Still here
I go:
I am new to OpenMP. I wrote a very simple code in C++.
The code is as below

#include <iostream.h>
#include <omp.h>
int main()
{
int nthreads, tid;
#pragma omp parallel num_threads(4)
{
int i = omp_get_thread_num();
cout << "Hello from thread " << i << endl;
}
return 0;
}

The output is:
Hello from thread 0

I am getting that line printed only once. Means there is only one
thread i.e. master thread.
What could be the reason for this. In what cases num_thread() or
omp_set_num_threads() fails.

Thanks.
 
J

John Carson

san said:
Hi,
I am not sure whether this is the right group to post this. Still here
I go:
I am new to OpenMP. I wrote a very simple code in C++.
The code is as below

Try

comp.parallel
 
M

Maik

<code>
#include "omp.h"
#include <iostream>

int main()
{

std::cout << "Max threads: " << omp_get_max_threads() << std::endl;

#pragma omp parallel for
for (int index=1; index<5000; index++)
{
int tid = omp_get_thread_num();
if (tid!=0)
std::cout << "Hello World from thread = " << tid <<
std::endl;
}

return 0;
}
</code>

compile it like
g++-4.2.0-alpha20060902 test.cc -fopenmp

and run it with
export OMP_NUM_THREADS=5
./a.out

greetz Maik
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top