# Closing sleeping kernels in parallel computation

I want to close the kernels which have completed their jobs in parallel computation. I have a test code as following:

LaunchKernels[5]

func[i_] := (
Print["func:", i];
Pause[2 i];
Print[$$KernelID, " Complete"]; ) ParallelDo[func[i]; CloseKernels[$$KernelID], {i, 1, 5}]


The output I get is this:

(kernel 12) func:1

(kernel 11) func:2

(kernel 10) func:3

(kernel 9) func:4

(kernel 8) func:5

(kernel 12) 12 Complete

(kernel 12) CloseKernels::subnopar :  Parallel programming is not available in subkernels of another parallel computation.

(kernel 11) 11 Complete

(kernel 11) CloseKernels::subnopar :  Parallel programming is not available in subkernels of another parallel computation.

(kernel 10) 10 Complete

(kernel 10) CloseKernels::subnopar :  Parallel programming is not available in subkernels of another parallel computation.

(kernel 9) 9 Complete

(kernel 9) CloseKernels::subnopar :  Parallel programming is not available in subkernels of another parallel computation.

(kernel 8) 8 Complete

(kernel 8) CloseKernels::subnopar :  Parallel programming is not available in subkernels of another parallel computation.


How to resolve these errors?

Note: I need to do parallel computation and close the kernels which have completed their jobs so that I can assign them more jobs. I don't want to close them all simultaneously because some of them complete their jobs before others(like in the example with Pause function), the computation is of that nature. I'm doing very heavy computation and can't do it sequentially.

• You did not address the comments under your previous question: mathematica.stackexchange.com/q/219903/12 It still sounds like you simply should not be doing this. Did you set the parallel functions to use the finest granularity? – Szabolcs Apr 20 at 16:00
• Finest grained will not work because I don't need to fine grain parallelization. I need to distribute 100 samples of population data analysis over 100 kernels (the maximum capacity of my workstation). 1 sample takes anywhere from 2 to 24 hours depending on some random parameters. The kernels which take more time is not because of problem with parallelization, it's because of NIntegrate. I just want to utilize the kernels which keep sleeping after finishing their jobs, while those which take more time can continue to complete their previous job. – Divyajyoti Apr 20 at 17:29
• I am not going to argue with you, but you might want to read up on what Method -> "FinestGrained" actually does. It will save you some trouble. – Szabolcs Apr 20 at 17:31