简体   繁体   English

OpenMP并行化并从列表向量中删除

[英]OpenMP parallelization and deleting from a vector of lists

gang. 团伙。 First, a high-level description of the problem & approach. 首先,对问题和方法进行高级描述。

I have a list containing images and pixel locations in each image - a list of lists. 我有一个列表,其中包含每个图像中的图像和像素位置 - 列表列表。 I want to pick n items at random from that list of images and for each image I want to iterate over k random pixel locations. 我想从该图像列表中随机选择n个项目,并且我希望在k个随机像素位置上迭代每个图像。 I want to do this in parallel. 我想要并行执行此操作。 For each processed pixel, I wish to delete it from the list. 对于每个处理过的像素,我希望从列表中删除它。

My approach is to distribute the images and pixel lists among all threads - so each thread has its own list of images and lists of pixel locations, but no two threads will be processing the same image at the same time. 我的方法是在所有线程中分配图像和像素列表 - 因此每个线程都有自己的图像列表和像素位置列表,但没有两个线程同时处理同一图像。 I store these into a vector. 我将这些存储到矢量中。

So let's say the code looks something like this: 所以让我们说代码看起来像这样:

struct MyObject
{
  // Image index on disk
  int imageIndex_;
  // List of x,y locations
  std::list< Point > pixels_;
};

std::vector< std::list < MyObject > > mainList(NUM_THREADS);

Then, mainList[0] would contain the images to be processed by thread with id 0. I launch threads the following way: #pragma omp parallel num_threads(numThreads_) and then they all run the same piece of code which samples images randomly from the thread's list of images. 然后, mainList[0]将包含要由ID为0的线程处理的图像。我按以下方式启动线程: #pragma omp parallel num_threads(numThreads_)然后它们都运行相同的代码片段,从线程的图像列表。

The problem is, when a pixel is processed and a thread erases it from the pixel list, such as mainList[0].begin()->pixels_.erase(someIter) , I sometimes get a assertion; 问题是,当像素被处理并且线程从像素列表中删除它时,例如mainList[0].begin()->pixels_.erase(someIter) ,我有时会得到一个断言; it traces to the delete operator. 它跟踪到删除操作符。

I know that writing to std::list is not thread-safe, but I was pretty sure that it is safe for a list of lists, where each list in the main list is accessed by one thread only. 我知道写入std :: list不是线程安全的,但我很确定它对于列表列表是安全的,其中主列表中的每个列表仅由一个线程访问。 I know that I provided limited code, but the problem boils down to deleting from a list of lists (or vector of lists) in parallel, when each thread has access to only one list at a time and the lists are not shared between threads. 我知道我提供了有限的代码,但问题归结为并行删除列表(或列表向量),当每个线程一次只能访问一个列表并且列表不在线程之间共享时。

What am I missing here? 我在这里错过了什么? Can I not delete from a vector of lists of lists in parallel? 我可以不从并行列表的向量中删除吗?

You provided too little info for me to guess the real problem, here are some thoughts: 你提供的信息太少,无法猜出真正的问题,这里有一些想法:

You seem to access invalidated iterators (ie delete one element twice). 您似乎访问无效的迭代器(即删除一个元素两次)。 This can be caused by a race condition OR by a bug in your code that really tries to delete an item more than once. 这可能是由竞争条件或代码中的错误引起的,该错误确实会多次尝试删除项目。 For example - are you sure that the random numbers you generated are unique? 例如 - 您确定您生成的随机数是唯一的吗? Do you apply the modulo operator to the generated random numbers after each element is removed to assure the index is valid? 在删除每个元素后,是否将模运算符应用于生成的随机数,以确保索引有效?

What I would check first is running the program with OpenMP disabled. 我首先要检查的是运行禁用OpenMP的程序。 Then you can decide if the assertion fails due to a race condition or due to another bug. 然后您可以决定断言是否因竞争条件或其他错误而失败。

On an unrelated note - you might want to use std::vector instead of std::list . 在一个不相关的注释 - 你可能想使用std::vector而不是std::list You are accessing random elements in the container and std::vector is optimized for random access. 您正在访问容器中的随机元素,并且std::vector已针对随机访问进行了优化。

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

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