Parallelizaton for stdlist problem

  • Thread starter Thread starter xxh418
  • Start date Start date
AI Thread Summary
Using OpenMP to parallelize a loop in C++ with a std::list is problematic due to the nature of linked lists, which do not support random access. The iterator-based approach does not allow for effective parallelization since OpenMP requires integer indices for loop iterations. To achieve parallelism, the list must be divided into sub-lists, allowing each thread to process a separate segment independently. This method can improve performance, as direct access to list elements in parallel is not feasible with the standard iterator approach.
xxh418
Messages
9
Reaction score
0
Hi all,
I am trying to using OpenMP to parallelize a loop in C++.
The iterator for the loop is defined by std::list using C++ standard library containers.
The following is an example:


for (std::list<int>::iterator it=mylist.begin() ; it != mylist.end(); ++it)
std::cout << ' ' << *it;

However, I found I can not use OpenMP command to do the parallelzation (#pragma). The simulation runs very slowly. For OpenMP, the first index in the for loop must be a integer for parallelzation. Does anyone encounter the same problem and have any idea how to solve it? Thank you very much!
 
Technology news on Phys.org
You want to parallelize "cout"s? I don't see how this is supposed to work.
 
The basic idea of a "list" is that the entries are linked in sequence, by pointers.

All the "nice" C++ syntax like iterators, the ++ operator, etc isn't "magic". All it does is hide the details from you (which is useful, because you don't have to change the code if you replace the list by a diferent data structure).

The only way to parallelize access to the list entries would be to split it into several sub-lists.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
75
Views
6K
Replies
12
Views
2K
Replies
5
Views
3K
Replies
3
Views
2K
Replies
2
Views
2K
Back
Top