Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#pragma task untied errors #28

Open
tianyizhangcs opened this issue Sep 13, 2018 · 0 comments
Open

#pragma task untied errors #28

tianyizhangcs opened this issue Sep 13, 2018 · 0 comments

Comments

@tianyizhangcs
Copy link
Member

tianyizhangcs commented Sep 13, 2018

tests/omp/tests/src/omp-task-tree.c getting wrong results. But work well if not using untied tasks
Ex1:

#include <stdio.h>
#include <stdlib.h>

int spawn_children( int depth, int num_children ) {
    int partial_sum[num_children];
    if( depth == 0) {
        return 1;
    }
    int i;
    for( i = 0; i < num_children; i++ ) { 
#pragma omp task untied firstprivate(i) shared(partial_sum)
        partial_sum[i] = spawn_children( depth-1, num_children );
    }
#pragma omp taskwait
    int sum = 0;
    for(i = 0; i < num_children; i++) {
        //printf("partial sum = %d\n", partial_sum[i]);
        sum += partial_sum[i];
    }
    return sum;
}

int main(int argc, char **argv) 
{
    int depth = 2;
    int breadth = 1;
    int total = 0;
    if(argc > 1) {
         depth = atoi(argv[1]);
    }
    if(argc > 2) {
        breadth = atoi(argv[2]);
    }
#pragma omp parallel
#pragma omp single nowait
#pragma omp task untied
    total = spawn_children(depth, breadth);

    printf("depth   %d\n", depth);
    printf("breadth %d\n", breadth);
    printf("total   %d\n", total);
    return 0;
}

Ex2:

/*
 * fibonnaci test
 * select tasking or sequential implementation on command line
 *
 * Author: Deepak
 */

#include <sys/time.h>
#include <stdio.h>

long fib1(int k);
long fib2(int k);

//int num_tasks = 0;
int cutoff = 26;


int main(int argc, char* argv[])
{
    struct timeval t1;
    struct timeval t2;

    int input;
    long s,u;
    long f;
    double m;

    if (argc != 2 && argc != 3) {
        //fprintf(stderr, "Usage: ./fib <input> <cutoff>\n");
        input = 24;
        cutoff = 0;
        //return 1;
    } else {
        input = atoi(argv[1]);
        if(argc == 3) {
            cutoff= atoi(argv[2]);
        }
    }

    gettimeofday(&t1, NULL);

#pragma omp parallel
    {
#pragma omp master
        {
#pragma omp task untied shared(f)
            {
                f = fib1(input);
            }
        }
    }

    gettimeofday(&t2, NULL);
    printf("fib(%d) = %d\n",input, f);

    s = t2.tv_sec - t1.tv_sec;
    u = t2.tv_usec - t1.tv_usec;
    m = (s*1000 + u/1000.0)  + 0.5;
    printf("cutoff = %d\n", cutoff);
    printf("time = %.2lfms\n", m );
    return 0;

}


long fib1(int k)
{
    long p2,p1;

    if (k == 2) return 1;
    if (k < 2){
        return k;
    }
    if(k < cutoff) {
        return fib1(k-1) + fib1(k-2);
    }

#pragma omp task untied shared(p2)
    {
        p2 = fib1(k-2);
    }

#pragma omp task untied shared(p1)
    {
        p1 = fib1(k-1);
    }
    /*
#pragma omp atomic
num_tasks += 2;
*/
#pragma omp taskwait
    return (p2+p1);
}

long fib2(int k)
{
    int i;
    long p2,p1,p0;

    if (k == 2) return 1;
    if (k < 2) return k;

    printf("0 1 1 ");

    p2 = 1;
    p1 = 2;
    p0 = p1;
    for (i = 3; i < k; i++) {
        p0 = p2+p1;
        p2 = p1;
        p1 = p0;
        printf("%d ", p0);
    }

    printf("\n");

    return p0;
}

@tianyizhangcs tianyizhangcs changed the title omp-task-tree undefined behavior #pragma task untied errors Sep 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant