Parallel iterator version of VM engine This function fills out the global state and then unlocks the mutexes used to control the threads in module.cpp::th_worker prepareThreads() must be called beforehand (or else `self` could not be const).
| 340 | // used to control the threads in module.cpp::th_worker |
| 341 | // prepareThreads() must be called beforehand (or else `self` could not be const). |
| 342 | static int |
| 343 | vm_engine_iter_parallel(NpyIter *iter, const NumExprObject *self, |
| 344 | bool need_output_buffering, int *pc_error, |
| 345 | char **errorMessage) |
| 346 | { |
| 347 | if (errorMessage == NULL) return -1; |
| 348 | |
| 349 | // Threads are prepared for execution in prepareThreads() |
| 350 | BENCH_TIME(8); |
| 351 | Py_BEGIN_ALLOW_THREADS; |
| 352 | DIFF_TIME(8); |
| 353 | |
| 354 | BENCH_TIME(4); |
| 355 | // Synchronization point for all threads (wait for initialization) |
| 356 | pthread_mutex_lock(&gs.count_threads_mutex); |
| 357 | BENCH_RANGE(150, gs.n_thread); |
| 358 | if (gs.count_threads < gs.n_thread) { |
| 359 | gs.count_threads++; |
| 360 | // printf( "Main thread init %d\n", gs.count_threads ); |
| 361 | do { |
| 362 | pthread_cond_wait(&gs.count_threads_cv, &gs.count_threads_mutex); |
| 363 | } while (gs.barrier_passed == BARRIER_HALT); |
| 364 | } |
| 365 | else { |
| 366 | gs.barrier_passed = BARRIER_PASS; |
| 367 | pthread_cond_broadcast(&gs.count_threads_cv); |
| 368 | } |
| 369 | pthread_mutex_unlock(&gs.count_threads_mutex); |
| 370 | DIFF_TIME(4); |
| 371 | BENCH_TIME(5); |
| 372 | // Synchronization point for all threads (wait for finalization) |
| 373 | pthread_mutex_lock(&gs.count_threads_mutex); |
| 374 | if (gs.count_threads > 0) { |
| 375 | gs.count_threads--; |
| 376 | // printf( "Main thread finalize %d\n", gs.count_threads ); |
| 377 | do { |
| 378 | pthread_cond_wait(&gs.count_threads_cv, &gs.count_threads_mutex); |
| 379 | } while (gs.barrier_passed == BARRIER_PASS); |
| 380 | } |
| 381 | else { |
| 382 | gs.barrier_passed = BARRIER_HALT; |
| 383 | pthread_cond_broadcast(&gs.count_threads_cv); |
| 384 | } |
| 385 | pthread_mutex_unlock(&gs.count_threads_mutex); |
| 386 | DIFF_TIME(5); |
| 387 | |
| 388 | BENCH_TIME(9); |
| 389 | Py_END_ALLOW_THREADS; |
| 390 | DIFF_TIME(9); |
| 391 | |
| 392 | return gs.ret_code; |
| 393 | } |
| 394 | |
| 395 | static int |
| 396 | run_interpreter(NumExprObject *self, NpyIter *iter, NpyIter *reduce_iter, |
no test coverage detected
searching dependent graphs…