简体   繁体   English

使用OpenMP处理单链表

[英]Process singly linked list with OpenMP

The code I want to parallelize has the following simple form: 我要并行化的代码具有以下简单形式:

for(const ListItem* item = myList; item; item = item->getNext())
  doSthWith(item);

I compile on MSVC 2013 with omp 2.0 support. 我在具有omp 2.0支持的MSVC 2013上进行编译。 Is it possible to still parallelize this in a clean and efficient way? 是否有可能以一种干净有效的方式并行化它? First problem I stumbled upon is that I need an integral loop counter. 我偶然发现的第一个问题是我需要一个积分回路计数器。 I could obviously just use an int and assign the pointer inside the loop but this already starts feeling like a kind of dirty workaround. 我显然可以只使用int并在循环内分配指针,但这已经开始感觉像是一种肮脏的解决方法。 Is iterating types like this even supported my the omp standard or might this end up in UB anyway? 这样的迭代类型是否甚至支持我的omp标准,还是最终可能会在UB中使用?

Perhaps not really an answer, but I can't format code in a comment... 也许不是真正的答案,但我无法在注释中设置代码格式...

Assuming that the list is not being modified, you could write something somewhat unpleasant like this (typed in here, not compiled, not tested). 假设列表没有被修改,您可以编写一些令人不愉快的内容(在此处键入,未编译,未测试)。

#pragma omp parallel
{
    int nThreads = omp_get_num_threads();
    int me       = omp_get_thread_num();
    int itemNo   = 0;

    for(const ListItem* item = myList; 
        item; 
        (item = item->getNext()), itemNo++)
    {
        if (itemNo%nThreads == me)
        {
            doSthWith(item);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM