简体   繁体   中英

Multithreading Forwards/Backwards for a CRF

I am attempting to multithread the forwards-backwards algorithm to find marginal probabilities. This will be used as a submodule for training a CRF. The below is pseudocode for the forwards segment of CRF training on a single example (sourced from here ).

for i in 0 .. T-1:
    for j in 1 ... N:
        for k in 1 ... N:
            p = alpha[(i-1)][k] + trans[k][j] + obvs[j][i]
            alpha[i][j] = logadd(alpha[i][j], p)

My plan for using N threads (reasonable for my use case with N being at most 10) to compute columns of the alpha matrix independently, and syncing with a barrier upon completion of computing each row actually runs SLOWER than the original sequential code.

I believe that the overhead of synchronization mechanisms is the root of this problem, as I am using threads from a pool and reusable barriers for all operations. Is there a better design that I should consider, or does the small size of N not justify parallelizing on computing columns of alpha?

The reason for my slowdown was using the multithreading library instead of the multiprocessing library from Python. Due to the nature of the GIL (Global Interpreter Lock), only one thread was actually running at a time. This is quite ineffective for a computationally bounded program that does not require any IO operations. Context switching only served to cause my code to run slower. Sharing synchronization primitives between different processes is another problem entirely, and I am not sure how to deal with that yet...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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