On Thursday, 28 March 2024 at 14:07:43 UTC, Salih Dincer wrote:
On Thursday, 28 March 2024 at 11:50:38 UTC, rkompass wrote:

Turning back to this: Are there similarly simple libraries for C, that allow for
parallel computation?

You can achieve parallelism in C using libraries such as OpenMP, which provides a set of compiler directives and runtime library routines for parallel programming.

Here’s an example of how you might modify the code to use OpenMP for parallel processing:

```c
 . . .

  #pragma omp parallel for reduction(+:result)
  for (int s = ITERS; s >= 0; s -= STEPS) {
    result += leibniz(s);
  }
 . . . ```
To compile this code with OpenMP support, you would use a command like gcc -fopenmp your_program.c. This tells the GCC compiler to enable OpenMP directives. The #pragma omp parallel for directive tells the compiler to parallelize the loop, and the reduction clause is used to safely accumulate the result variable across multiple threads.

SDB@79

Nice, thank you.
It worked endlessly until I saw I had to correct the `for` to
  `for (int s = ITERS; s > ITERS-STEPS; s--)`
Now the result is:
```
3.1415926535897936
Execution time: 0.212483 (seconds).
```
This result is sooo similar!

I didn't know that OpenMP programming could be that easy.
Binary size is 16K, same order of magnitude, although somewhat less.
D advantage is gone here, I would say.

Reply via email to