0

There is my code. It does compile without any errors, but it doesn't output anything. When I open program.exe it instantly is closed even if I add "cin.get()" at the end of program. When used in terminal it doesnt cout anything. echo $? returns a false value. I have other similar programs (for testing OpenMP) which are also compiled but works as intended. It must some kind of problem with gcc. Any ideas?

#include <iostream>
#include <string>
#include <iomanip>
#include <omp.h>
#include <cstdio> 
using namespace std;

const long num_steps = 300'000'000;

int main(int argc, char *argv[])
{

    cout << setprecision(15) << fixed;
    const unsigned num_threads = stoi(argv[1]);
    int i, nthreads;
    long double pi, sum[num_threads];
    long double step = 1.0 / num_steps;
    omp_set_num_threads(num_threads);
    #pragma omp parallel
    {
        int i, id, nthrds;
        long double x;
        id = omp_get_thread_num();
        nthrds = omp_get_num_threads();
        if (id == 0) nthreads = nthrds;
        for (i = id, sum[id] = 0.0; i < num_steps; i += nthrds)
        {
            x = (i + 0.5) * step;
            sum[id] += 4.0 / (1.0 + x * x);
        }
    }
    for (i = 0, pi = 0.0; i < nthreads; ++i) pi += step * sum[i];
    cout << "PI = " << pi << endl;
    cout << "PI = "
         << "3.14159265358979323846264338327950288419716939937510582097494" << endl;

}

This is my tasks.json:

"version": "2.0.0",
    "tasks": [
      {
        "label": "program01",
        "type": "shell",
        "command": "g++",
        "args": ["-std=c++2a", "-fopenmp", "-Wall", "-o", "program", "main.cpp"],
        "group": {
          "kind": "build",
          "isDefault": true
        },
        "problemMatcher": []
  • 2
    Welcome to Stack Overflow! It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and [Debugging Guide](http://idownvotedbecau.se/nodebugging/) – NathanOliver May 19 '20 at 16:35
  • 2
    Technically your program is invalid, as it uses a GCC-specific non-portable extension [variable-length arrays](https://en.wikipedia.org/wiki/Variable-length_array). Use `std::vector` instead. – Some programmer dude May 19 '20 at 16:36
  • 1
    btw there are uncountable programs that you can compile without getting a compiler error while they are still wrong. Not getting a compiler error does not mean much – 463035818_is_not_an_ai May 19 '20 at 16:52

1 Answers1

0

The problem is here: long double pi, sum[num_threads]; num_threads must be a compile time constant and it is not, since you're getting its value at runtime.

If you want to get its value at runtime create a dynamic array, like so:

int i, nthreads;
long double pi;
long double* sum = new long double[num_threads];

then execute myProgram.exe 8.

KeyC0de
  • 4,728
  • 8
  • 44
  • 68