1

I have X amount of cores doing unique work in parallel, however, their output needs to be printed in order.

Object {
   Data data
   int order
}

I've tried putting the objects in a min heap after they're done with their parallel work, however, even that is too much of a bottleneck.

Is there any way I could have work done in parallel and guarantee the print order? Is there a known term for my problem? Have others encountered it before?

COOKIES
  • 434
  • 4
  • 14
  • 1
    One easy workaround is for each process/thread to write its own output in order and then, in a post processing step, to merge the individual files. This requires, though, that (a) there is a definition of the global order required and (b) each process must be able to order each of its individual outputs in that global order. A timestamp might suffice, if the times available to each process are the same within your required accuracy. – High Performance Mark Jan 13 '20 at 13:25
  • With all due respect, the argument of *indirectly **observed*** order or completion ( using timestamping or some other, stable, monotonic ordinal in the records-of-evidence ) **does not answer the problem** - You must be aware that it only helps to observe the happened order but in no means it does introduce a solution - enforcing the wished-to-have order into the flow of execution ( observing a resulting order may by chance, but need not as principle, match the expected order, the less imperatively or otherwise implements or puts in place the ***wished-to-have*-order**. – user3666197 Jan 13 '20 at 13:35

2 Answers2

1

Q : Is there a known term for my problem?

Yes, there is. A con·​tra·​dic·​tion:

Definition of contradiction

2a : a proposition, statement, or phrase that asserts or implies both the truth and falsity of something
// … both parts of a contradiction cannot possibly be true …
— Thomas Hobbes
2b : a statement or phrase whose parts contradict each other
// a round square is a contradiction in terms
3a : logical incongruity
3b : a situation in which inherent factors, actions, or propositions are inconsistent or contrary to one another

source: Merriam-Webster

Computer science, having borrowed the terms { PARALLEL | SERIAL | CONCURRENT } from the theory of systems, respects the distinctive ( and never overlapping ) properties of each such class of operations, where:

[PARALLEL] orchestration of units-of-work implies, that any and every work-unit: a) starts and b) gets executed and c) gets finished at the same time, i.e. all get into/out-of [PARALLEL]-section at once and being elaborated at the very same time, not otherwise.

[SERIAL] orchestration of units-of-work implies, that all work-units be processed in a one, static, known, particular order, starting work-unit(s) in such an order, just a (known)-next one after previous one has finished its work - i.e. one-after-another, not otherwise.

[CONCURRENT] orchestration of units-of-work permits to start more than one unit-of-work, if resources and system conditions permit (scheduler priorities obeyed), resulting in unknown order of execution and unknown time of completion, as both the former and the latter depend on unknown externalities (system conditions and (non)-availability of resources, that are/will be needed for a particular work-unit elaboration)


Whereas there is an a-priori known, inherently embedded sense of an ORDER in [SERIAL]-type of processing ( as it was already pre-wired into the units-of-work processing-orchestration-code ), it has no such meaning in either [CONCURRENT], where opportunistic scheduling makes a wished-to-have order an undeterministically random result from the system states, skewed by the coincidence of all other externalities, and the same wished-to-have order is principally singular value in true [PARALLEL] by definition, as all start/execute/finish at-the-same-time - so all units-of-work being executed in [PARALLEL] fashion have no other chance, but be both 1st and last at the same time.

Q : Is there any way I could have work done in parallel and guarantee the print order?

No, unless you intentionally or unknowingly violate the [PARALLEL] orchestration rules and re-enter a re-[SERIAL]-iser logic into the work-units, so as to imperatively enforce any such wished-to-have ordering, that is not known, the less natural for the originally [PARALLEL] work-units' orchestration ( as is a common practice in python - using a GIL-monopolist indoctrinated stepping - as an example of such step )

Q : Have others encountered it before?

Yes. Since 2011, each and every semester this or similar questions reappear here, on Stack Overflow at growing amounts every year.

halfer
  • 19,824
  • 17
  • 99
  • 186
user3666197
  • 1
  • 6
  • 50
  • 92
  • 1
    He said “Guarantee Print Order *After* Parallelism”. He’s not trying to run them in order, but rather just produce output in order once the parallel processing is done. – Rob Jan 14 '20 at 00:59
  • Yes, @Rob that is out of doubts, the point is, that **without some way to "*re-enter a re-[SERIAL]-iser logic* into the work-units" processing, the problem how to arrange ( print ) the results ( in some particular order ) remains principally undecidable**. Even if using a timestamp "datum", as was commented above, what order is the right order for any two or more equal timestamps? Using some monotonic "keying" makes the [PARALLEL] code depend on something like a "shared"-singleton monotonic-id#-provider, which may block & damages the so far valid principal independence of [PARALLEL] execution. – user3666197 Jan 14 '20 at 02:46
  • Yeah, the timestamp suggestion above is ill advised. But if you’re re-serializing, that means that you have some original sequence identifier. Perhaps an index in the original array, or whatever. So, you just save the results in a structure that captures the original order. E.g. if you are processing an array of items in parallel, just save the output in an array using the respective index. Or [here](https://stackoverflow.com/questions/39948082/long-cycle-blocks-application/39949292#39949292) is an example where you are processing pixels for output in a final image. Lots of ways to do it. – Rob Jan 14 '20 at 03:16
  • @Rob Mandelbrot example is a nice case: it is not parallel in any shape & fashion - fast divergent points in complex-plane are soon off-the limit & cease to get computed, while more "stable" points get iterated for way longer times.It can use an async, just-[CONCURRENT] (limits are just computing resources' availability) orchestration, not a true-[PARALLEL].The trick with pixels in arrays is that they have **no 1D-*order***, but rather an a-priori fixed 2D-placement and if no kernel/self-convolution dictates otherwise, their processing is independent & async by nature. No dependence = no order – user3666197 Jan 14 '20 at 12:30
  • @Rob as a positive counter-example,where a true-[PARALLEL] orchestration is a must and the one & the only one solution is - a control-system of 6D-robot ( having 6 DoF ). There, each & every one of the sextet of the 1D-motors' control-loop has to work in a true-[PARALLEL] fashion, as it is one & the only one coordinated motion, that the 6D-robot has to perform in 3D-space in due time & shape.No axis of motion can move/roll a tiny bit earlier or any later,but all have to move at the same time & as per the true-[PARALLEL]-processing dictated amounts of motion - otherwise things turn wreck havoc. – user3666197 Jan 14 '20 at 12:37
  • I respectfully suggest that your definition of “true parallel” is incorrect or, at best, far too narrow. There are [standard definitions for parallel/concurrent](https://stackoverflow.com/a/1050257/1271826). And your interpretation is out of sync with [Apple’s standard use of the terms](https://developer.apple.com/videos/play/wwdc2017/706/?time=197) and therefore is not relevant to a GCD question. Frankly, you’re debating a point that is, IMHO, not relevant to the OP’s underlying question. Hey, if you want to discuss further, I’m happy to do so, but, if so, maybe we should move this to chat. – Rob Jan 14 '20 at 18:53
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/205959/discussion-between-rob-and-user3666197). – Rob Jan 14 '20 at 18:53
1

Is there any way I could have work done in parallel and guarantee the print order?

Needless to say, we design parallelized routines with focus on an efficiency, but not constraining the order of the calculations. The printing of the results at the end, when everything is done, should dictate the ordering. In fact, parallel routines often do calculations in such a way that they’re conspicuously not in order (e.g., striding on each thread) to minimize thread and synchronization overhead.

The only question is how you structure the results to allow efficient storage and efficient, ordered retrieval. I often just use a mutable buffer or a pre-populated array. It’s very efficient in terms of both storage and retrieval. Or you can use a dictionary, too. It depends upon the nature of your Data. But I’d avoid the order property pattern in your result Object.

Just make sure you’re using optimized build if using standard Swift collections, as this can have a material impact on performance.

Rob
  • 415,655
  • 72
  • 787
  • 1,044