Understanding the sequence of events when calling the R dpois function

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Understanding the sequence of events when calling the R dpois function

Jason Serviss
Hello all,

I am trying to get a better understanding of the underlying code for the stats::dpois function in R and, specifically, what happens under the hood when it is called. I finally managed to track down the C course at: https://github.com/wch/r-source/blob/trunk/src/nmath/dpois.c. It would seem that the dpois C function is taking a double for each of the x and lambda arguments so I am a bit confused why I can provide a vector and matrix to dpois in R, e.g.

dpois(1:5, matrix(runif(2*5, 1, 10), nrow = 5)))

and get a matrix back with the results. Due to this, I was expecting to see a loop, or similar, in the underlying C source but… to no avail. So I have to conclude that either a) there is a step between when I call dpois in R and the C code getting executed that loops over the inputs or b) that there is a construct in the C code (my proficiency here is limited) that is called per input. Any help in enlightening me on what code is responsible for iterating over the multiple inputs (or if someone is feeling energetic, the exact stepwise code that is executed when calling dpois) would be greatly appreciated!!

Kind Regards,
Jason Serviss


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Understanding the sequence of events when calling the R dpois function

Berry, Charles


> On May 31, 2018, at 8:25 AM, Jason Serviss <[hidden email]> wrote:
>
> Hello all,
>
> I am trying to get a better understanding of the underlying code for the stats::dpois function in R and, specifically, what happens under the hood when it is called. I finally managed to track down the C course at: https://github.com/wch/r-source/blob/trunk/src/nmath/dpois.c. It would seem that the dpois C function is taking a double for each of the x and lambda arguments so I am a bit confused why I can provide a vector and matrix to dpois in R, e.g.
>
> dpois(1:5, matrix(runif(2*5, 1, 10), nrow = 5)))
>
> and get a matrix back with the results. Due to this, I was expecting to see a loop, or similar, in the underlying C source but… to no avail. So I have to conclude that either a) there is a step between when I call dpois in R and the C code getting executed that loops over the inputs or b) that there is a construct in the C code (my proficiency here is limited) that is called per input. Any help in enlightening me on what code is responsible for iterating over the multiple inputs (or if someone is feeling energetic, the exact stepwise code that is executed when calling dpois) would be greatly appreciated!!

Have a look at arithmetic.c. The math2 function handles calling dpois in a vectorized fashion.  

You need to trace how it is that the compiler knows to use dpois.c in constructing `C_dpois' which is what the R function dpois is calling.

If you know how to grep the sources for dpois, that will get you started. You will need to look over a bunch of #define's and a few C utilities to see how it all fits together.

If you dig in and find the `do { ... } while(0)' construct confusing you might look at

https://stackoverflow.com/questions/257418/do-while-0-what-is-it-good-for

HTH,

Chuck
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Understanding the sequence of events when calling the R dpois function

Greg Minshall-2
In reply to this post by Jason Serviss
Jason,

as Chuck Berry (to whom, *thanks* for 'do {...} while(0)'!) suggested,
using grep, or even grep executed from find, such as
----
find . -type f -exec grep -H "dpois" \{\} \; | less
----
(executed from the root of an R source tree), is your friend.

cheers, Greg

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Understanding the sequence of events when calling the R dpois function

Jason Serviss
Chuck and Greg,

Thanks a lot for your help! I have a much better understanding now of what is happening “under the hood”.

Kind Regards,
Jason


> On 31 May 2018, at 20:08, Greg Minshall <[hidden email]> wrote:
>
> Jason,
>
> as Chuck Berry (to whom, *thanks* for 'do {...} while(0)'!) suggested,
> using grep, or even grep executed from find, such as
> ----
> find . -type f -exec grep -H "dpois" \{\} \; | less
> ----
> (executed from the root of an R source tree), is your friend.
>
> cheers, Greg

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Understanding the sequence of events when calling the R dpois function

Clark Fitzgerald
Hi Jason,

Duncan Temple Lang answered a similar question for me last year and wrote
up the method here: http://dsi.ucdavis.edu/Notes/R/FindingNativeCodeInR.html

Basically the C level debugger can be very helpful for figuring out what's
happening.

Best,
Clark

On Fri, Jun 1, 2018 at 4:47 AM, Jason Serviss <[hidden email]> wrote:

> Chuck and Greg,
>
> Thanks a lot for your help! I have a much better understanding now of what
> is happening “under the hood”.
>
> Kind Regards,
> Jason
>
>
> > On 31 May 2018, at 20:08, Greg Minshall <[hidden email]> wrote:
> >
> > Jason,
> >
> > as Chuck Berry (to whom, *thanks* for 'do {...} while(0)'!) suggested,
> > using grep, or even grep executed from find, such as
> > ----
> > find . -type f -exec grep -H "dpois" \{\} \; | less
> > ----
> > (executed from the root of an R source tree), is your friend.
> >
> > cheers, Greg
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel