链接已复制
> Is there a specific algorithm for rewriting a do=while loop in c into a for loop?
It depends. :o)
We really have to know what your do/while loop is doing. I know of one case where the loop we were trying to parallelizewas running "until it was done" which made it hard to split into halves.
One of our developers analyzed the code and realized that generating the data was quick so it didn't matter if it ran serially, and the data for eachiteration was relatively small. In addition, he could determine whether he had enough data without having processed all of it. He rewrotethe loopto buffer the generated data into "chunks." Either he gathered a full chunk, or just a partial chunk. Either way, he now knew the bounds of the problem and could use a cilk_for to process the data.
The key here was that generating a chunk's worth of data was relatively quick so it didn't matter that it was run serially. It was processing the data that was compute-intensive. If that doesn't describe your problem, you'll need a different approach.
- Barry