OpenMP 强制任务创建的顺序。

huangapple go评论50阅读模式
英文:

OpenMP enforce the order in which tasks are created

问题

我对OpenMP还不熟悉,我认为我的用例并不简单,因此正在寻找方法的指导。

在OpenMP中是否有一种方法可以强制按照它们生成的顺序执行任务?

我想要实现的目标如下:

我的数据是一个数据包,其中包含序列号和传感器ID。

必须为每个传感器ID保持序列号的顺序,但不同的传感器ID可以并行执行。

在伪代码中 - 请原谅语法。我正在寻求概念上的理解。

#pragma parallel for ordered
for data in data_packet_by_sequence

#pragma task nowait
   omp_set_lock(data.sensor_id)
   process_data(data) // 必须按照data.sequence的顺序执行
   omp_unset_lock(data.sensor_id)

但问题是,处理数据的过程可能会按照不符合数据包的序列号的顺序安排。

我如何强制在处理传感器数据时,要尊重数据.sequence的顺序?

提前感谢您的帮助。

英文:

I am new to OpenMP and think my use case is not simple - hence looking for pointers on approach.

Is there a way to enforce execution of tasks in order they are spawned in OpenMP?

What I want to achieve is as follows

My data is a packet of data which contains a sequence number and sensor id.

The order of sequence number must be maintained for each sensor id but different sensor ids can execute in parallel.

In pseudo code - so please excuse the syntax. I am seeking conceptual understanding.

#pragma parallel for ordered 
for data in data_packet_by_sequence

#pragma task nowait
   omp_set_lock(data.sensor_id)
   process_data(data) // must execute in order of data.sequence 
   omp_unset_lock(data.sensor_id)

But the problem is that the process data might be schedule in a sequence that does not honor the sequence number of the packet

How can I enforce that when a sensor data is processed, it respects the data.sequence ordering?

Thanks in advance.

答案1

得分: 1

我认为depend子句是一个解决方案。我应该写一个注释,因为我不太确定这是否有效并且是否是一个合适的答案,但是在注释中编写(伪)代码很困难...

int dep[nbsensors];

#pragma omp parallel
#pragma omp single nowait
{
    for data in data_packet_by_sequence {
        #pragma omp task firstprivate(...) depend(inout:dep[data.sensor_id])
        process_data(data);
    }
}

解释:当运行具有depend(out:x)子句的任务时,它会阻止所有具有depend(in:x)子句的其他任务启动。因此,在具有depend(inout:x)子句的任务中(对于它们所有的x相同),在给定时间内只能运行一个任务。

你甚至不需要在dep[]中实际写入任何内容。

英文:

I think that the depend clause is a solution. I should rather write a comment because I am not really sure this works and is a proper answer, but writing (pseudo-)code in comments is difficult...

int dep[nbsensors];

#pragma omp parallel
#pragma omp single nowait
{
    for data in data_packet_by_sequence {
        #pragma omp task firstprivate(...) depend(inout:dep[data.sensor_id])
        process_data(data);
    }
}

Explanation: when a task that has a depend(out:x) clause is running, it prevents all other tasks with a depend(in:x) clause to start. As a consequence, among the tasks having a depend(inout:x) clause (same x for all of them), only one of them can run at a given time.

You don't even need to actually write something in dep[].

答案2

得分: 1

实际上有一个非常简单的解决方案,所有线程执行整个循环,但特定线程只处理一些选定的传感器:

#pragma omp parallel
{
    int nbthreads = omp_get_num_threads();
    int ithread = omp_get_thread_num();
    for data in data_packet_by_sequence {
        if ((data.sensor_id*nbthreads)/nbsensors == ithread) 
           process_data(data);
    }
}

这保证特定传感器的所有数据由同一线程按顺序处理。

英文:

There is actually a very simple solution, where all the threads execute the whole loop, but a given thread processes only some selected sensors:

#pragma omp parallel
{
    int nbthreads = omp_get_num_threads();
    int ithread = omp_get_thread_num();
    for data in data_packet_by_sequence {
        if ((data.sensor_id*nbthreads)/nbsensors == ithread) 
           process_data(data);
    }
}

This garantees that all the data from a given sensor are processed sequentially by the same thread

huangapple
  • 本文由 发表于 2023年6月2日 02:29:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76384728.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定