Thread overview
How to use #pragma omp parallel for collapse(n) in dlang?
Aug 13
ijet
6 days ago
Daniel Kozak
6 days ago
Daniel Kozak
August 13
How to use #pragma omp parallel for collapse(n) in dlang?
August 15
On Tuesday, 13 August 2019 at 08:41:07 UTC, ijet wrote:
> How to use #pragma omp parallel for collapse(n) in dlang?

I don’t understand the question.

Bastiaan.
6 days ago
On Tue, Aug 13, 2019 at 10:47 AM ijet via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote:
>
> How to use #pragma omp parallel for collapse(n) in dlang?

There is no OMP for de as I know, but you can just use
https://dlang.org/phobos/std_parallelism.html#.parallel
for each 'foreach'
6 days ago
On Thu, Aug 15, 2019 at 9:44 AM Daniel Kozak <kozzi11@gmail.com> wrote:
>
> On Tue, Aug 13, 2019 at 10:47 AM ijet via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote:
> >
> > How to use #pragma omp parallel for collapse(n) in dlang?
>
> There is no OMP for de as I know, but you can just use
> https://dlang.org/phobos/std_parallelism.html#.parallel
> for each 'foreach'

something like this:
C version:

#include <stdio.h>
#define CNT 400000

int isprime(int num)
{
     if (num <= 1) return 0;
     if (num % 2 == 0 && num > 2) return 0;
     for(int i = 3; i < num / 2; i+= 2)
     {
         if (num % i == 0)
             return 0;
     }
     return 1;
}


int fun(int a, int b)
{
return isprime(a) + isprime(b);
}

int main() {
int buff[CNT];
int i, j;
#pragma omp parallel for private(j) collapse(2)
for (i = 0; i < 4; i++)
    for (j = 0; j < 100000; j++)
buff[i*100000 + j] = fun(i, j);

for (int x = 0; x < CNT; x++)
{
printf("%d\n", buff[x]);
}
return 0;
}

D version:

import core.stdc.stdio;
import std.parallelism;
import std.range : iota;

enum CNT = 400000;

int isprime(int num)
{
     if (num <= 1) return 0;
     if (num % 2 == 0 && num > 2) return 0;
     for(int i = 3; i < num / 2; i+= 2)
     {
         if (num % i == 0)
             return 0;
     }
     return 1;
}


int fun(int a, int b)
{
    return isprime(a) + isprime(b);
}

int main(string[] args)
{
    int[CNT] buff = void;

    foreach (i; iota(0,4).parallel)
        foreach (j; iota(0, 100000).parallel)
            buff[i*100000 + j] = fun(i, j);

    for (int x = 0; x < CNT; x++)
    {
        printf("%d\n", buff[x]);
    }
    return 0;
}