Skip to content

atomic for tasks #33

@pojeda

Description

@pojeda

Hi,

I wonder if there is support for atomic operations, especially in the context of tasks? I have extended a 2d integration from
one of our courses (https://uppmax.github.io/HPC-python/day4/parallel.html) using PyOMP:

import math
from time import perf_counter
from numba.openmp import njit
from numba.openmp import openmp_context as openmp

grid size

n = 100000

Task granularity - number of iterations per task

TASK_SIZE = 1000

@njit
def integration2d_omp(n):
h = math.pi / float(n)
total_sum = 0.0

with openmp("parallel"):
    with openmp("single"):
        # Create tasks for chunks of the outer loop
        num_tasks = (n + TASK_SIZE - 1) // TASK_SIZE
        
        for task_id in range(num_tasks):
            with openmp("task"):
                i_start = task_id * TASK_SIZE
                i_end = min(i_start + TASK_SIZE, n)
                local_sum = 0.0
                
                for i in range(i_start, i_end):
                    x = h * (i + 0.5)
                    for j in range(n):
                        y = h * (j + 0.5)
                        local_sum += math.sin(x + y)
                
                # Atomic update of the shared sum
                with openmp("atomic"):
                    total_sum += local_sum
        
        # Wait for all tasks to complete
        with openmp("taskwait"):
            pass

return h**2 * total_sum

if name == "main":
start = perf_counter()
integral = integration2d_omp(n)
end = perf_counter()
print(f"Integral value is {integral:e}, Error is {abs(integral - 0.0):e}")
print(f"Time spent: {end - start:.2f} sec")

However, the atomic seems not to be supported yet. Do you have any suggestions for using tasks for this problem?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions