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:
#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
staticlong 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.
@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?