Self-restarting MathKernel - is it possible in Mathematica?

前端 未结 6 1296
慢半拍i
慢半拍i 2021-02-01 10:06

This question comes from the recent question \"Correct way to cap Mathematica memory use?\"

I wonder, is it possible to programmatically restart MathKernel keeping the c

6条回答
  •  Happy的楠姐
    2021-02-01 10:42

    I have a similar requirement when I run a CUDAFunction for a long loop and CUDALink ran out of memory (similar here: https://mathematica.stackexchange.com/questions/31412/cudalink-ran-out-of-available-memory). There's no improvement on the memory leak even with the latest Mathematica 10.4 version. I figure out a workaround here and hope that you may find it's useful. The idea is that you use a bash script to call a Mathematica program (run in batch mode) multiple times with passing parameters from the bash script. Here is the detail instruction and demo (This is for Window OS):

    • To use bash-script in Win_OS you need to install cygwin (https://cygwin.com/install.html).
    • Convert your mathematica notebook to package (.m) to be able to use in script mode. If you save your notebook using "Save as.." all the command will be converted to comments (this was noted by Wolfram Research), so it's better that you create a package (File->New-Package), then copy and paste your commands to that.
    • Write the bash script using Vi editor (instead of Notepad or gedit for window) to avoid the problem of "\r" (http://www.linuxquestions.org/questions/programming-9/shell-scripts-in-windows-cygwin-607659/).

    Here is a demo of the test.m file

    str=$CommandLine;
    len=Length[str];
    Do[
    If[str[[i]]=="-start",
    start=ToExpression[str[[i+1]]];
    Pause[start];
    Print["Done in ",start," second"];
    ];
    ,{i,2,len-1}];
    

    This mathematica code read the parameter from a commandline and use it for calculation. Here is the bash script (script.sh) to run test.m many times with different parameters.

    #c:\cygwin64\bin\bash
    for ((i=2;i<10;i+=2))
    do
    math -script test.m -start $i
    done
    

    In the cygwin terminal type "chmod a+x script.sh" to enable the script then you can run it by typing "./script.sh".

提交回复
热议问题