Opnemp code returns nothing?

I just started reading about openmp and was following with some sample codes, so I have the following simple example. But when I run the code on my terminal I get nothing? As if the code is still running with no output.
I use the following command to run:

1
2
g++ Parallel_sample.cpp -o test.o -fopenmp -g

The code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

#define _USE_MATH_DEFINES 
#include <cmath>
#include<math.h>
#include<stdio.h>

#include <cstring>
#include <sstream>
#include <string>
#include <iostream> 
#include <vector>
#include<omp.h>
#include<pthread.h>

// code calculate the integral by approx of area under curve 
static long num_steps= 100000;     double step;
#define PAD 8 
#define NUM_THREADS 2
int main(){
    
    int i, nthreads; double pi, sum[NUM_THREADS][PAD];
    step = 1.0/num_steps; 
    omp_set_num_threads(NUM_THREADS);
    double omp_get_wtime();
    #pragma omp parallel 
    {
        int i, id,nthrds;
        double x;
        id = omp_get_thread_num(); // what is my thread id 
        nthreads = omp_get_num_threads(); 
        if (id==0) nthreads=nthrds;
        
        for (i=id, sum[id][0]=0.0; i< num_steps; i=i+nthrds){
            x = (i+0.5)*step; //value of x at the middle of the rectangle 
            sum[id][0] += 4.0/(1.0+x*x); 
        }
    }
    double omp_get_wtime(); // an attempt of tracking time??
  

 
    
    for (i=0, pi=0.0; i<nthreads; i++)pi += sum[i][0] *step;

    return 0;
}

One reason your code doesn't print anything is because it doesn't contain any print statements.

Another reason your code doesn't print anything is
> double omp_get_wtime();
is not a function call.

This is a function call.
double my_wtime = omp_get_wtime();

Yet another reason your code doesn't print anything could be that you called your program 'test.o'.
If you're on some Linux/*BSD operating system, there is a shell command called 'test' which appears to do nothing, but does set it's exit status in various interesting ways.
Be sure to run your program as ./test.o to make sure you run what you think you're running.

FYI, .o files are the intermediate result of compiling individual source files. By convention, a complete program would be just say Parallel_sample (or Parallel_sample.exe for M$ types).

> As if the code is still running with no output.
It's running if you don't get your command prompt back.
g++ -fopenmp -o pi.exe pi.cpp

pi.exe


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <ctime>
#include "omp.h"                                           // OpenMP
using namespace std;

int main()
{
   const int N = 10000000;
   const double dx = 1.0 / N;

   for ( int nThreads = 1; nThreads <= 16; nThreads++ )
   {
      clock_t start = clock();
      double sum = 0.0;

      omp_set_num_threads( nThreads );                     // OpenMP
      #pragma omp parallel for reduction( + : sum )        // OpenMP
      for ( int i = 0; i < N; i++ )
      {
         double x = ( i + 0.5 ) * dx;                      // Mid-ordinate rule
         sum += 4.0 / ( 1.0 + x * x );                     // Numerical int. for 4*atan(1) = pi
      }

      clock_t end = clock();

      cout << "Threads: " << nThreads << "     Sum: " << sum << "     Time: " << (double)( end - start ) / CLOCKS_PER_SEC << " s\n";
   }
}


BTW, omp_get_wtime() is not guaranteed to be consistent across threads.
Last edited on
@lastchance
This works! I am completely new to openMP and Parallel computing in general, so I see you used #pragma omp parallel for reduction() instead of what I have been using to define a structure or the parallel region, which is: #pragma omp parallel . Is there a specific reason for this?
@Salem c

One reason your code doesn't print anything is because it doesn't contain any print statements.


That makes sense, however I was literally doing an example from the openMP exercise sets so I thought it should work as they claim.


This is a function call.
double my_wtime = omp_get_wtime();

You're right, that's my bad!
@againtry
Thanks!
Topic archived. No new replies allowed.