Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenMP task in java

I am fairly new with Java Threads, I normally use C with it comes to parallelization. To parallelize algorithm that has the same pattern as the one it follows:

void traverse(node* p)
{
    if (p->left)
        #pragma omp task // p is firstprivate by default
        traverse(p->left);
    if (p->right)
        #pragma omp task // p is firstprivate by default
        traverse(p->right);
}

I would use the task directive of openMP, for example.

Task Description

When a thread encounters a task construct, a task is generated from the code for the associated structured block. The encountering thread may immediately execute the task, or defer its execution. In the latter case, any thread in the team may be assigned the task. Completion of the task can be guaranteed using task synchronization constructs. A task construct may be nested inside an outer task, but the task region of the inner task is not a part of the task region of the outer task.

My question is:

How I could implement this same idea (task) with Java Threads?

like image 470
dreamcrash Avatar asked Jul 17 '26 16:07

dreamcrash


1 Answers

The pragmas of OpenMP do make parallelization a little easier.

In Java, you would first need to create a class that implements Runnable.
- example: public Class Traverse implements Runnable

You then just neew to create the class and call 'run' to start the thread.

private void traverse(node p) 
{
   Traverse t = null;

    if (p.left)
       t = new Traverse(p.left);
    if (p->right)
       t = new Traverse(p.right)

    t.run();   // start thread. this call will not wait for run to finishes
}
like image 119
BradRees Avatar answered Jul 20 '26 06:07

BradRees



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!