| 207 | |
| 208 | |
| 209 | int numexpr_set_nthreads(int n_thread_new) { |
| 210 | // Set the number of threads in numexpr's VM |
| 211 | int n_thread_old = gs.n_thread; |
| 212 | int T, rc; |
| 213 | void *status; |
| 214 | |
| 215 | // printf("numexpr_set_nthreads #1, old =%d, new =%d\n", n_thread_old, n_thread_new ); |
| 216 | if( n_thread_new <= 0 ) { |
| 217 | fprintf(stderr, "Error. `nthreads` must be a positive integer"); |
| 218 | return -1; |
| 219 | } |
| 220 | if( n_thread_new == n_thread_old ) { |
| 221 | return n_thread_old; |
| 222 | } |
| 223 | |
| 224 | // Only join threads if they are not initialized or if our PID is |
| 225 | // different from that in pid var (probably means that we are a |
| 226 | // subprocess, and thus threads are non-existent). |
| 227 | if( gs.init_threads_done && gs.pid == getpid() ) { |
| 228 | // Tell all existing threads to finish |
| 229 | gs.barrier_passed = BARRIER_EXIT; |
| 230 | pthread_mutex_lock(&gs.count_threads_mutex); |
| 231 | // Ensure that workers never wait on the condition variable |
| 232 | gs.count_threads = n_thread_old; |
| 233 | // Wake all workers |
| 234 | pthread_cond_broadcast(&gs.count_threads_cv); |
| 235 | pthread_mutex_unlock(&gs.count_threads_mutex); |
| 236 | |
| 237 | for( T=0; T < n_thread_old; T++ ) { |
| 238 | // Join exiting threads |
| 239 | rc = pthread_join(gs.threads[T], &status); |
| 240 | if (rc) { |
| 241 | printf( "JOIN ERROR %d\n", rc ); |
| 242 | fprintf(stderr, "ERROR; return code from pthread_join() is %d\n", rc); |
| 243 | fprintf(stderr, " Error detail: %s\n", strerror(rc)); |
| 244 | exit(-1); |
| 245 | } |
| 246 | |
| 247 | } |
| 248 | pthread_mutex_unlock(&gs.count_threads_mutex); |
| 249 | pthread_mutex_unlock(&gs.count_mutex); |
| 250 | gs.count_threads = 0; |
| 251 | gs.n_thread = n_thread_new; |
| 252 | gs.init_threads_done = 0; |
| 253 | gs.barrier_passed = BARRIER_HALT; |
| 254 | } |
| 255 | else { |
| 256 | gs.n_thread = n_thread_new; |
| 257 | } |
| 258 | |
| 259 | // Launch a new pool of threads |
| 260 | reinit_threads(n_thread_old); |
| 261 | |
| 262 | return n_thread_old; |
| 263 | } |
| 264 | PyDoc_STRVAR(SetNumThreads__doc__, |
| 265 | "Sets a maximum number of threads to be used in operations. Returns old value.\n"); |
| 266 | static PyObject* |
no test coverage detected
searching dependent graphs…