-3

This is my program:

#include <stdio.h>
#include <time.h>
#include <omp.h>
#include <stdint.h>

#define NPTS 1000000000

void main() {
   uint64_t i;
   double a,b,c,pi,dt,mflops;
   struct timespec tstart,tend;
   clock_gettime(CLOCK_REALTIME,&tstart);
   a = 0.5;
   b = 0.75;
   c = 0.25;
   pi = 0;
   #pragma omp parallel for reduction(+:pi)
   for (i = 1; i <= NPTS; ++i)
      pi += a/((i-b)*(i-c));
   clock_gettime(CLOCK_REALTIME,&tend);
   dt = (tend.tv_sec+tend.tv_nsec/1e9)- 
(tstart.tv_sec+tstart.tv_nsec/1e9);
           mflops = NPTS*5.0/(dt*1e6);
 printf("NPTS = %ld, pi = %f, threads = 
  %d\n",NPTS,pi,omp_get_max_threads());
  printf("time = %f, estimated MFlops = %f\n",dt,mflops);
 }

I am trying to finish writing this OpenMP program to parallelize my pi program so that I can essentially test my serial and OpenMP program at 10,000,000 steps and compare it from there based on the number of threads.

1 Answers1

1

Since NPTS is a macro and not a proper constant, it defaults to being an int (which is the smallest type that can hold 10000000 among the candidate types int, long int, and long long int; See this answer for more). To print an int using printf, use %d and not %ld in the format string.

Alternatively, make it a constant of type long int by replacing the macro with const long int NPTS = 10000000;.

kotatsuyaki
  • 1,441
  • 3
  • 10
  • 17
  • It is also worth considering using the same type as your loop variable (`uint64_t`) – Laci Oct 11 '22 at 05:42