Skip to main content
replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/
Source Link

I want to instrument a Mathematica script so that, upon receiving any system signal (e.g. SIGINT, aka Ctrl-C) it will print1 the value returned by MaxMemoryUsed[].

I have read this earlier answerthis earlier answer and the LibraryLink docs, but I still can't figure out how to adapt the solution given in that answer so that my_handler can invoke MaxMemoryUsed, and print out its result. IOW, I can't figure out what to replace the ??? below with so that my_handler's printout shows the value returned by MaxMemoryUsed[]:

void my_handler (int s) {

    fprintf(stderr, "MaxMemoryUsed[] returned: %d\n", ???);

    /* ------------------------------------------------------- */

    fprintf(stderr, "Received signal: %d\n", s);

    /* additional signal-handling code follows */
}

(One other possible way to achieve what I'm trying to do would be something like an atexit mechanism, but I have not been able to find anything like that for Mathematica. Yet a third possibility would be to implement the script so that it periodically prints the value of MaxMemoryUsed to a file. Unfortunately, for most of the script's lifetime, it is evaluating a single, irreducible Mathematica expression, and I don't know how to implement frequent periodic updating of an output file under these circumstances. In any case, even if I knew how to do it, this last alternative is a distant second-best.)


1 preferably to stderr, but either stdout or some file is also OK.

I want to instrument a Mathematica script so that, upon receiving any system signal (e.g. SIGINT, aka Ctrl-C) it will print1 the value returned by MaxMemoryUsed[].

I have read this earlier answer and the LibraryLink docs, but I still can't figure out how to adapt the solution given in that answer so that my_handler can invoke MaxMemoryUsed, and print out its result. IOW, I can't figure out what to replace the ??? below with so that my_handler's printout shows the value returned by MaxMemoryUsed[]:

void my_handler (int s) {

    fprintf(stderr, "MaxMemoryUsed[] returned: %d\n", ???);

    /* ------------------------------------------------------- */

    fprintf(stderr, "Received signal: %d\n", s);

    /* additional signal-handling code follows */
}

(One other possible way to achieve what I'm trying to do would be something like an atexit mechanism, but I have not been able to find anything like that for Mathematica. Yet a third possibility would be to implement the script so that it periodically prints the value of MaxMemoryUsed to a file. Unfortunately, for most of the script's lifetime, it is evaluating a single, irreducible Mathematica expression, and I don't know how to implement frequent periodic updating of an output file under these circumstances. In any case, even if I knew how to do it, this last alternative is a distant second-best.)


1 preferably to stderr, but either stdout or some file is also OK.

I want to instrument a Mathematica script so that, upon receiving any system signal (e.g. SIGINT, aka Ctrl-C) it will print1 the value returned by MaxMemoryUsed[].

I have read this earlier answer and the LibraryLink docs, but I still can't figure out how to adapt the solution given in that answer so that my_handler can invoke MaxMemoryUsed, and print out its result. IOW, I can't figure out what to replace the ??? below with so that my_handler's printout shows the value returned by MaxMemoryUsed[]:

void my_handler (int s) {

    fprintf(stderr, "MaxMemoryUsed[] returned: %d\n", ???);

    /* ------------------------------------------------------- */

    fprintf(stderr, "Received signal: %d\n", s);

    /* additional signal-handling code follows */
}

(One other possible way to achieve what I'm trying to do would be something like an atexit mechanism, but I have not been able to find anything like that for Mathematica. Yet a third possibility would be to implement the script so that it periodically prints the value of MaxMemoryUsed to a file. Unfortunately, for most of the script's lifetime, it is evaluating a single, irreducible Mathematica expression, and I don't know how to implement frequent periodic updating of an output file under these circumstances. In any case, even if I knew how to do it, this last alternative is a distant second-best.)


1 preferably to stderr, but either stdout or some file is also OK.

added 23 characters in body
Source Link
Szabolcs
  • 236.5k
  • 31
  • 641
  • 1.3k

I want to instrument a Mathematica script so that, upon receiving any system signal (e.g. SIGINT, aka Ctrl-C) it will print1 the value returned by MaxMemoryUsed[].

I have read this earlier answer and the LibraryLink docs, but I still can't figure out how to adapt the solution given in that answer so that my_handler can invoke MaxMemoryUsed, and print out its result. IOW, I can't figure out what to replace the ??? below with so that my_handler's printout shows the value returned by MaxMemoryUsed[]:

void my_handler (int s) {

    fprintf(stderr, \"MaxMemoryUsed[] returned: %d\\n\", ???);

    /* ------------------------------------------------------- */

    fprintf(stderr, \"Received signal: %d\\n\", s);

    /* additional signal-handling code follows */
}
void my_handler (int s) {

    fprintf(stderr, "MaxMemoryUsed[] returned: %d\n", ???);

    /* ------------------------------------------------------- */

    fprintf(stderr, "Received signal: %d\n", s);

    /* additional signal-handling code follows */
}

(One other possible way to achieve what I'm trying to do would be something like an atexit mechanism, but I have not been able to find anything like that for Mathematica. Yet a third possibility would be to implement the script so that it periodically prints the value of MaxMemoryUsed to a file. Unfortunately, for most of the script's lifetime, it is evaluating a single, irreducible Mathematica expression, and I don't know how to implement frequent periodic updating of an output file under these circumstances. In any case, even if I knew how to do it, this last alternative is a distant second-best.)


1 preferably to stderr, but either stdout or some file is also OK.

I want to instrument a Mathematica script so that, upon receiving any system signal (e.g. SIGINT, aka Ctrl-C) it will print1 the value returned by MaxMemoryUsed[].

I have read this earlier answer and the LibraryLink docs, but I still can't figure out how to adapt the solution given in that answer so that my_handler can invoke MaxMemoryUsed, and print out its result. IOW, I can't figure out what to replace the ??? below with so that my_handler's printout shows the value returned by MaxMemoryUsed[]:

void my_handler (int s) {

    fprintf(stderr, \"MaxMemoryUsed[] returned: %d\\n\", ???);

    /* ------------------------------------------------------- */

    fprintf(stderr, \"Received signal: %d\\n\", s);

    /* additional signal-handling code follows */
}

(One other possible way to achieve what I'm trying to do would be something like an atexit mechanism, but I have not been able to find anything like that for Mathematica. Yet a third possibility would be to implement the script so that it periodically prints the value of MaxMemoryUsed to a file. Unfortunately, for most of the script's lifetime, it is evaluating a single, irreducible Mathematica expression, and I don't know how to implement frequent periodic updating of an output file under these circumstances. In any case, even if I knew how to do it, this last alternative is a distant second-best.)


1 preferably to stderr, but either stdout or some file is also OK.

I want to instrument a Mathematica script so that, upon receiving any system signal (e.g. SIGINT, aka Ctrl-C) it will print1 the value returned by MaxMemoryUsed[].

I have read this earlier answer and the LibraryLink docs, but I still can't figure out how to adapt the solution given in that answer so that my_handler can invoke MaxMemoryUsed, and print out its result. IOW, I can't figure out what to replace the ??? below with so that my_handler's printout shows the value returned by MaxMemoryUsed[]:

void my_handler (int s) {

    fprintf(stderr, "MaxMemoryUsed[] returned: %d\n", ???);

    /* ------------------------------------------------------- */

    fprintf(stderr, "Received signal: %d\n", s);

    /* additional signal-handling code follows */
}

(One other possible way to achieve what I'm trying to do would be something like an atexit mechanism, but I have not been able to find anything like that for Mathematica. Yet a third possibility would be to implement the script so that it periodically prints the value of MaxMemoryUsed to a file. Unfortunately, for most of the script's lifetime, it is evaluating a single, irreducible Mathematica expression, and I don't know how to implement frequent periodic updating of an output file under these circumstances. In any case, even if I knew how to do it, this last alternative is a distant second-best.)


1 preferably to stderr, but either stdout or some file is also OK.

added 153 characters in body
Source Link
kjo
  • 11.8k
  • 1
  • 33
  • 91

I want to instrument a Mathematica script so that, upon receiving any system signal (e.g. SIGINT, aka Ctrl-C) it will print (preferably to stderr, but stdout is OK too)1 the value returned by MaxMemoryUsed[].

I have read this earlier answer and the LibraryLink docs, but I still can't figure out how to adapt the solution given in that answer so that my_handler can invoke MaxMemoryUsed, and print out its result. IOW, I can't figure out what to replace the ??? below with so that my_handler's printout shows the value returned by MaxMemoryUsed[]:

void my_handler (int s) {

    fprintf(stderr, \"MaxMemoryUsed[] returned: %d\\n\", ???);

    /* ------------------------------------------------------- */

    fprintf(stderr, \"Received signal: %d\\n\", s);

    /* additional signal-handling code follows */
}
 

(One other possible way to achieve what I'm trying to do would be something like an atexit mechanism, but I have not been able to find anything like that for Mathematica. Yet a third possibility would be to implement the script so that it periodically prints the value of MaxMemoryUsed to a file. Unfortunately, for most of the script's lifetime, it is evaluating a single, irreducible Mathematica expression, and I don't know how to implement frequent periodic updating of an output file under these circumstances. In any case, even if I knew how to do it, this last alternative is a distant second-best.)


1 preferably to stderr, but either stdout or some file is also OK.

I want to instrument a Mathematica script so that, upon receiving any system signal (e.g. SIGINT, aka Ctrl-C) it will print (preferably to stderr, but stdout is OK too) the value returned by MaxMemoryUsed[].

I have read this earlier answer and the LibraryLink docs, but I still can't figure out how to adapt the solution given in that answer so that my_handler can invoke MaxMemoryUsed, and print out its result. IOW, I can't figure out what to replace the ??? below with so that my_handler's printout shows the value returned by MaxMemoryUsed[]:

void my_handler (int s) {

    fprintf(stderr, \"MaxMemoryUsed[] returned: %d\\n\", ???);

    /* ------------------------------------------------------- */

    fprintf(stderr, \"Received signal: %d\\n\", s);

    /* additional signal-handling code follows */
}

(One other possible way to achieve what I'm trying to do would be something like an atexit mechanism, but I have not been able to find anything like that for Mathematica. Yet a third possibility would be to implement the script so that it periodically prints the value of MaxMemoryUsed to a file. Unfortunately, for most of the script's lifetime, it is evaluating a single, irreducible Mathematica expression, and I don't know how to implement frequent periodic updating of an output file under these circumstances.)

I want to instrument a Mathematica script so that, upon receiving any system signal (e.g. SIGINT, aka Ctrl-C) it will print1 the value returned by MaxMemoryUsed[].

I have read this earlier answer and the LibraryLink docs, but I still can't figure out how to adapt the solution given in that answer so that my_handler can invoke MaxMemoryUsed, and print out its result. IOW, I can't figure out what to replace the ??? below with so that my_handler's printout shows the value returned by MaxMemoryUsed[]:

void my_handler (int s) {

    fprintf(stderr, \"MaxMemoryUsed[] returned: %d\\n\", ???);

    /* ------------------------------------------------------- */

    fprintf(stderr, \"Received signal: %d\\n\", s);

    /* additional signal-handling code follows */
}
 

(One other possible way to achieve what I'm trying to do would be something like an atexit mechanism, but I have not been able to find anything like that for Mathematica. Yet a third possibility would be to implement the script so that it periodically prints the value of MaxMemoryUsed to a file. Unfortunately, for most of the script's lifetime, it is evaluating a single, irreducible Mathematica expression, and I don't know how to implement frequent periodic updating of an output file under these circumstances. In any case, even if I knew how to do it, this last alternative is a distant second-best.)


1 preferably to stderr, but either stdout or some file is also OK.

Source Link
kjo
  • 11.8k
  • 1
  • 33
  • 91
Loading