gpt4 book ai didi

c - 使用 OpenMP 锁定例程实现计数信号量

转载 作者:行者123 更新时间:2023-12-01 22:16:12 24 4
gpt4 key购买 nike

我正在尝试实现 counting semaphores使用 OpenMP 的锁定结构,但在尝试在关键区域内使用 omp_set_lock() 时遇到问题(程序挂起)。

我正在使用一个简单的生产者-消费者程序来测试实现。这是我想到的:

#include <omp.h>
#include <stdio.h>

#define N 50

int semaphore_count = 0;
omp_lock_t semaphore_lock;

int A[N];

void semaphore_increment()
{
int my_id = omp_get_thread_num();

#pragma omp critical
{
printf("[%lf][%d] semaphore_count %d --> %d\n", omp_get_wtime(), my_id,
semaphore_count, semaphore_count + 1);

semaphore_count++;

if(semaphore_count == 1) {
// Semaphore was previously locked, so unlock it.
printf("[%lf][%d] Releasing lock.\n", omp_get_wtime(), my_id);
omp_unset_lock(&semaphore_lock);
}
}
}

void semaphore_decrement()
{
int my_id = omp_get_thread_num();
#pragma omp critical
{
printf("[%lf][%d] semaphore_count: %d\n", omp_get_wtime(), my_id,
semaphore_count);

if (semaphore_count == 0) {
printf("[%lf][%d] Sleeping\n", omp_get_wtime(), my_id);
omp_set_lock(&semaphore_lock);
}
else {
printf("[%lf][%d] Working\n", omp_get_wtime(), my_id);
// Creating a critical region here instead of in the beginning of
// the function solves the problem.
// #pragma omp critical
// {
semaphore_count--;
// }
if (semaphore_count == 0) {
omp_set_lock(&semaphore_lock);
}
}
}
}

void produce() {
for (int i = 0; i < N; ++i) {
A[i] = i;
#pragma omp flush
semaphore_increment();
}
}

void consume() {
int sum = 0;
for (int i = 0; i < N; ++i) {
semaphore_decrement();
sum += A[i];
}

printf("Sum is: %d\n", sum);
}

int main() {

omp_init_lock(&semaphore_lock);
omp_set_lock(&semaphore_lock);

#pragma omp parallel
{
#pragma omp single nowait
produce();

#pragma omp single nowait
consume();
}

omp_destroy_lock(&semaphore_lock);
return 0;
}

每次消费者线程进入休眠状态时,该版本的程序都会挂起。如果我修改它以将关键区域减少到代码的较小部分(如程序中的注释所示),那么它就可以工作。

我不明白的是:为什么会发生这种情况?看起来只增加信号量的生产者线程停止运行,然后一切都挂起,但我不明白为什么。

最佳答案

这个答案是错误(请参阅评论)。我将其留在这里作为如何做到这一点的示例。

<小时/>

正如 EOF 的评论中所述,问题中显示的程序是错误的,因为它存在关于 semaphore_count 变量更新的竞争条件,因为这可能在两个不同的关键部分中同时发生。

我最终用以下函数替换了函数 semaphore_incrementsemaphore_decrement,该函数可以根据作为 操作传入的值来递增或递减信号量值.

void semaphore_update(int operation) {
int my_id = omp_get_thread_num();
int set_lock = 0;

#pragma omp critical
{
if (operation == 0) { // Decrement operation
if (semaphore_count == 0) {
set_lock = 1;
}
else {
semaphore_count--;
if (semaphore_count == 0) {
// Locking here won't actually lock the current thread, only set
// the semaphore so that the *next* thread will be put to sleep.
set_lock = 1;
}
}
}
else { // Increment operation
semaphore_count++;
if(semaphore_count == 1) {
// Semaphore was previously locked, so unlock it.
omp_unset_lock(&semaphore_lock);
}
}
}

// The actual call to omp_set_lock has to happen outside the critical region
// otherwise any threads trying to unlock the semaphore won't be able to
// get access to the critical region.
if (set_lock) {
omp_set_lock(&semaphore_lock);
}
}

关于c - 使用 OpenMP 锁定例程实现计数信号量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34257569/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com