multithreading - Parallel processing - Connected Data -
problem
summary: parallely apply function f each element of array f not thread safe.
i have set of elements e process, lets queue of them. want process these elements in parallel using same function f( e ).
now, ideally call map based parallel pattern, problem has following constraints.
- each element contains pair of 2 objects.( e = (a,b) )
- two elements may share object. ( e1 = (a1,b1); e2 = (a1, b2) )
- the function f cannot process 2 elements share object. e1 , e2 cannot processing in parallel.
what right way of doing this?
my thoughts so,
- trivial thought: keep set of active , bs, , start processing element when no other thread using or b.
- so, when give element thread add , bs active set.
- pick first element, if elements not in active set spawn new thread , otherwise push of queue of elements.
- do till queue empty.
- will cause deadlock ? ideally when processing on elements become available right?
2.-the other thought make graph of these connected objects. each node represents object (a / b) . each element edge connecting & b, , somehow process data such know elements never overlapping.
questions
- how can achieve best?
- is there standard pattern ?
- is there problem these approaches?
- not necessary, if tell tbb methods use, that'll great.
the "best" approach depends on lot of factors here:
- how many elements "e" have , how work needed f(e). --> check if it's worth work elements in parallel (if need lot of locking , don't have work do, you'll slow down process working in parallel)
- is there possibility change design can make f(e) multi-threading safe?
- how many elements "a" , "b" there? there logic elements "e" share specific versions of , b? --> if can sort elements e separate lists each , b appears in single list, can process these lists parallel without further locking.
- if there many different a's , b's , don't share many of them, may want trivial approach lock each "a" , "b" when entering , wait until lock.
whenever "lock , wait" multiple locks it's important take locks in same order (e.g. first , b second) because otherwise may run deadlocks. locking order needs observed everywhere (a single place in whole application uses different order can cause deadlock)
edit: if "try lock" need ensure order same. otherwise can cause lifelock:
- thread 1 locks a
- thread 2 locks b
- thread 1 tries lock b , fails
- thread 2 tries lock , fails
- thread 1 releases lock a
- thread 2 releases lock b
- goto 1 , repeat...
chances happens "endless" relatively slim, should avoided anyway
edit 2: principally guess i'd split e(ax, bx) different lists based on ax (e.g 1 list e's share same a). process these lists in parallel locking of "b" (there can still "trylock" , continue if required b used.
Comments
Post a Comment