cout-based debugging

239 views
Skip to first unread message

Dong Liang

unread,
Jan 8, 2024, 12:47:21 PM1/8/24
to TMB Users
Hi,
I remember reading on the group that lines containing std∷cout were optimized out from the cpp file by the TMB∷compile function.
In the development stage, these std∷cout can be handy for debugging.
Is there a way to keep these std∷cout results while running the optimization (e.g. optim or nlminb)?
Please see attached a very simple example from the reference manual. How to show the trace outputs from cout in R-console after line 12?

Thanks
Dong
matern2.R
matern.cpp
matern.rData

Ben Bolker

unread,
Jan 8, 2024, 12:51:46 PM1/8/24
to tmb-...@googlegroups.com
Hmm. Is Rprintf() also optimized out? (I prefer Rprintf() to cout
anyway, but I appreciate that it doesn't necessarily serve exactly the
same function)
> --
> To post to this group, send email to us...@tmb-project.org. Before
> posting, please check the wiki and issuetracker at
> https://github.com/kaskr/adcomp/ <https://github.com/kaskr/adcomp/>.
> Please try to create a simple repeatable example to go with your
> question (e.g issues 154, 134, 51). Use the issuetracker to report bugs.
> ---
> You received this message because you are subscribed to the Google
> Groups "TMB Users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to tmb-users+...@googlegroups.com
> <mailto:tmb-users+...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/tmb-users/b319d0a8-1108-48e7-aa3f-3e92e59a24acn%40googlegroups.com <https://groups.google.com/d/msgid/tmb-users/b319d0a8-1108-48e7-aa3f-3e92e59a24acn%40googlegroups.com?utm_medium=email&utm_source=footer>.

Andrea Havron

unread,
Jan 8, 2024, 2:48:25 PM1/8/24
to Ben Bolker, tmb-...@googlegroups.com
I have used Rcout before (replace std::cout with Rcout). Note that your Rcout statements will get printed four times. From my understanding, this is due to the four AD types of a TMB model that track value, 1st, 2nd, and 3rd derivatives (Kasper correct me if I'm wrong here). I haven't tried Rprintf().

To post to this group, send email to us...@tmb-project.org. Before posting, please check the wiki and issuetracker at https://github.com/kaskr/adcomp/. Please try to create a simple repeatable example to go with your question (e.g issues 154, 134, 51). Use the issuetracker to report bugs.

---
You received this message because you are subscribed to the Google Groups "TMB Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tmb-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tmb-users/7d62fd4c-b0c1-4922-988f-b85aab1e23d9%40gmail.com.

Dong Liang

unread,
Jan 8, 2024, 3:10:20 PM1/8/24
to TMB Users

Both Rprintf and Rcout results showed up in the R-Console after running MakeADFun, but none appeared iteratively during the optim run. How can I see the intermediate results during the iterations?

Thanks
Dong

Andrea Havron

unread,
Jan 8, 2024, 3:21:40 PM1/8/24
to Dong Liang, TMB Users
I'm not sure how to get Rcout statements to print with every iteration.

If you are trying to track parameter values though, you can set tracepar = TRUE, that is:
obj <- MakeADFun(data, parameters, DLL="matern")
obj$env$tracepar <- TRUE

Dong Liang

unread,
Jan 12, 2024, 11:13:45 AM1/12/24
to TMB Users
Thanks, but tracepar does not trace random effects. Neither does it trace "generated values" (functions of parameters) with every iteration.
Thanks
Dong

Katie Paulson

unread,
Jun 20, 2024, 4:13:39 PM6/20/24
to TMB Users
Hi all -- I'm hoping to do something similar. In R I have obj <- TMB::MakeADFun(...). I want to run obj$fn(obj$par) and have some of functions of parameters (from the middle of my .cpp code) printed to my R console to help with debugging. I added a Rcout line to my .cpp code but nothing is printed to my R console when I run obj$fn(obj$par). Please let me know if you found a solution to this, or if there's another suggestion for debugging where I want to view values from the middle of my .cpp code.
Thanks!
Katie

Kasper Kristensen

unread,
Jun 21, 2024, 9:40:02 AM6/21/24
to TMB Users
The tricky thing about cout based debugging is that your C++ template is only processed a few times for the initial parameters when you create the internal 'tape' of the model by running MakeADFun(...). The cout statements do not become part of the tape, so you won't see any output when running 'obj$fn()'.
Here's a workaround:

Start by running obj$fn(par) to update the internal parameters (or skip this step if you wish to debug from a state where your program stopped with an error). Then grab the corresponding full parameter vector and evaluate your C++ template for this parameter using 'double' types:

lp <- obj$env$last.par        ## Grab most recent full parameter
obj$env$f(lp, type="double")  ## Evaluate template using 'double' types to execute cout statements

Katie Paulson

unread,
Jun 24, 2024, 1:25:06 PM6/24/24
to TMB Users
Hmm I tried this and I'm still not getting cout statements printed to my R console. Here's the code I have in my objective function:

  std::cout<<"Matrix SigmaInv:";
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 2; j++) {
      std::cout<<SigmaInv(i,j);
    }
  }

Kasper Kristensen

unread,
Jun 24, 2024, 2:05:17 PM6/24/24
to TMB Users
Please see previous answer in this thread about using Rcout rather than std::cout.

Katie Paulson

unread,
Jun 27, 2024, 5:47:16 PM6/27/24
to TMB Users
This in my .cpp code:  
Rcout<<"Matrix SigmaInv:";

  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 2; j++) {
      Rcout<<SigmaInv(i,j);
    }
 }

plus your suggestion from the R console:
lp <- obj$env$last.par
obj$env$f(lp, type="double")

does not print anything other than the value of the objective function at these parameter values (which for me is NaN which is why I'm trying to do some debugging).

Thank you again for your time and help on this.
Katie
Reply all
Reply to author
Forward
0 new messages