Repository navigation
Documented the use of OpenMP and parallelization in C code - #6018
Conversation
…tween(), CJ(), and fcoalesce()
…felse() and nafill() (taking longer to read and understand fread due to its size, and will probably make more changes to these too)
…ll schedule(dynamic) collapse(2))
…rite(), then a bit on gfroce (gsumm.c) and just the basics for subset.c
…ead(), added a minor bit to the gsumm.c part
…ondensed details for forder, fsort and reorder C files after some digging and understanding of the code)
… of all the 12 enlisted use cases, made some minor tweaks and corrections to statements
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #6018 +/- ##
=======================================
Coverage 97.53% 97.53%
=======================================
Files 80 80
Lines 14913 14915 +2
=======================================
+ Hits 14545 14547 +2
Misses 368 368 ☔ View full report in Codecov by Sentry. |
|
Regarding openmp utils manual. I would include the date there, something like "as of March 2024". It is because it doc will be getting outdated over time. |
|
At which spot in the file would it be ideal to place the date? I think all documentation could use that, or a 'last updated' time point (considering that everything is subject to change in the future). |
|
I agree with Jan. This is a welcome change but very prone to becoming outdated.
Not really. In particular, this is essentially documenting implementation details, not really user-facing behavior, which makes it much more likely to escape the normal PR process and wind up out of date, whereas other documentation will be updated by any relevant PRs. Honestly, I would remove all of the "Parallelism is used here to..." sections. Interested users should be reading the source to learn these things, and such granular detail of implementation detail is especially prone to become outdated (every time a loop is added/edited we're meant to update this documentation?). We could also devise some regression testing (a new GHA script) to at least ensure the list of routines where OpenMP is used is reflected in the cited list. With those reservations in mind I will re-assign to @tdhock who gave the most extensive comments on the linked issue #2687 and I think has a clearer idea in mind of what he wants the documentation to look like. |
…ince this is prone to being outdated
…y text accordingly
Thanks for explaining! That makes sense, and I see the distinction now.
I agree as in essence, the regular user would only be concerned with the usage and notes on how to extract more benefit, and not these implementation-specific details (even when keeping this updated could be possible).
This sounds like a good idea, but I'm not sure how that will pertain to testing regression standalone. Do you mean to update this list/manual by a grep for the functions where OpenMP is used for the GHA that does regression testing? (On a related note, I will share it this week in a new PR, just waiting for some feedback from Toby)
Sure, and Toby mentioned this as something to work on earlier so it would certainly be appropriate. |
In {lintr}, we have this repo-meta-tests action that does some consistency checks on the repo/codebase as a whole that aren't ideally suited to the package test suite: https://github.com/r-lib/lintr/blob/main/.github/workflows/repo-meta-tests.yaml |
Thanks for sharing - I went ahead and wrote a first draft for a step that updates such a list with filtered filenames (getting the names of routines or functions would be slightly trickier, but doable if you want me to proceed with this - let me know!). It makes sense to check such as part of the metadata as a consistency check, but I'm still not sure how it would be directly helpful for regression testing or what you mentioned earlier:
|
|
From a user perspective I would like the docs to tell us if we get parallel speedups with large nrow or ncol. I guess the answer is usually nrow, but sometimes I observe speedups with large ncol, for example see below https://rcdata.nau.edu/genomic-ml/atime/vignettes/compare-data.table-tidyverse.html with nrow=10 and ncol varying. |
That seems perfectly reasonable to me, and is also something that's not likely to change over time. |
…f having more rows vs having more columns after some testing (benchmarking) and research
I ran a benchmark and explored a bit to provide more information on that - Can you please review everything I wrote in this PR? (especially to check if I clarified enough in terms of the user-facing details, or more specifically what the user could take note of or try for better speedup gains) |
|
@Nj221102 can you please review? |
|
@ben-schwen If possible, would you like to review this Ben? |
| \item Having more and/or complex conditional logic when using \code{\link{fifelse}()} or \code{\link{subset}()} | ||
| } | ||
|
|
||
| \code{data.table} also incorporates throttling mechanisms to prevent the excessive creation of threads (can lead to significant overhead) for small data tasks. |
There was a problem hiding this comment.
I don't think this is specific enough to be useful. we already have
\item{throttle}{ 1024 (default) means that, roughly speaking, a single thread will be used when nrow(DT)<=1024, 2 threads when nrow(DT)<=2048, etc. The throttle is to speed up small data tasks (especially when repeated many times) by not incurring the overhead of managing multiple threads. Hence the number of threads is throttled (restricted) for small tasks. }
…s already does a better job at explaining than what I wrote
|
@MichaelChirico Also, could you review this? (once more now, but for either merging/closing - I'm cool either way!) |
|
I'm still a bit torn on how to proceed. Obviously this is nice research and it'd be a shame to waste it. OTOH I don't think most of this info should be recorded/shared with users -- it's really developer-facing utility. I think user only cares when a function is taking 2+-dimensional input, which dimension is parallelized? (most often rows vs. columns). So the question becomes where's most appropriate for this to live. Some options I can think of:
Right now I think the 3rd option is best -- the comments then live closest to where they're useful. Any other thoughts? |
yes please I would like to see something on man pages in this case. (fread and fwrite) for other cases, comments are ok. |
|
Afair non exported functions can have manual as well. Then we can keep that format and just have it hidden from the ordinary user. |
Proposed changes to OpenMP PR
I agree, and I like options 1 and 3 (with more preference towards the latter as well) Putting it in the Wiki would be easier to maintain as anybody can edit (and I agree that such implementation details should not be user-facing in general, but some users might be keen to know more) and is not inappropriate as a place to read some extra details, but having that information organized in their respective separate files near their code sections offers convenience, especially in the reader not having to skim through all the other functions altogether (which I think is sweet for helping save time). Thanks for making the changes! |
|
One thing that was not there in that PR was the section for speedup gains I wrote: In general, or as applicable to all the aforementioned use cases, better speedup can be expected when dealing with large datasets.
Having such data when using \code{\link{fread}()} or \code{\link{fwrite}()} (ones with significant speedups for larger file sizes) also means that while one part of the data is being read from or written to disk (I/O operations), another part can be simultaneously processed using multiple cores (parallel computations). This overlap reduces the total time taken for the read or write operation (as the system can perform computations during otherwise idle I/O time).
Apart from increasing the size of the input data, function-specific parameters when considered can benefit more from parallelization or lead to an increase in speedup. For instance, these can be:
\itemize{
\item Having a large number of groups when using \code{\link{forder}()} or a multitude of combinations when using \code{\link{CJ}()}
\item Having several missing values in your data when using \code{\link{fcoalesce}()} or \code{\link{nafill}()}
\item Using larger window sizes and/or time series data when using \code{\link{froll}()}
\item Having more and/or complex conditional logic when using \code{\link{fifelse}()} or \code{\link{subset}()}
}And then the respective lines I had for each routine that advise on using more rows vs columns for speedup. I'll proceed to add that piece of information for fread and fwrite in their respective man files as Toby suggested above, but do you guys think rest of that information (or for the other routines) is worth retaining? |
I'm assuming you still want them in their man pages right? or would what Michael did here suffice? |
I wasn't sure there's a lot of information there besides what should be clear from reading the source code, which is easier now that the comment appears alongside the implementation (in the man page it was more useful as there's more separation between the comment & the logic). But if you feel strongly feel free to add back into the comments. |
Same thoughts (especially at the time of writing)
Not at all, I was just curious to know if they were required or not (since aside from the use of OpenMP, Toby wanted to have information for the user on whether stuff like more rows vs. columns in the input data would be better in terms of speedup, for each routine) |
That's a good point. Yes, let's make the mention in the corresponding man page directly, that will be most useful to users. |
| \item\file{fread.c} - \code{\link{fread}(). Parallelized across row-based chunks of the file.} | ||
| \item\file{forder.c}, \file{fsort.c}, and \file{reorder.c} - \code{\link{forder}()} and related | ||
| \item\file{froll.c}, \file{frolladaptive.c}, and \file{frollR.c} - \code{\link{froll}()} and family | ||
| \item\file{fwrite.c} - \code{\link{fwrite}()} | ||
| \item\file{gsumm.c} - GForce in various places, see \link{GForce} | ||
| \item\file{fwrite.c} - \code{\link{fwrite}(). Parallelized across rows.} | ||
| \item\file{gsumm.c} - GForce in various places, see \link{GForce}. Parallelized across groups. |
There was a problem hiding this comment.
for me this is sufficient documentation of whether the parallelization happens on rows or columns
There was a problem hiding this comment.
I agree! (for me too that's enough)
That's a good point. Yes, let's make the mention in the corresponding man page directly, that will be most useful to users.
Appreciate you for considering this route!
Since I based that upon what Toby wanted here:
yes please I would like to see something on man pages in this case. (fread and fwrite)
And now that he agrees, I think we can move ahead on this consensus then :)
|
Thanks all! |

I did some research and tried to understand the use of OpenMP in the C files after filtering for #pragma omp - From what I saw and understood, I wrote a bit for each of the twelve use cases mentioned to be using parallelization that you see when using
?setDTthreads, or namely for these:I used a more straightforward 'Parallelism is used here to ...' and 'OpenMP is used here to ...' to start with before enlisting my key observations, but I tried to avoid repetition elsewhere whenever possible. I also included a small section on some factors that can contribute to improved speedup below those cases. Let me know if this is good enough, or if what I wrote aligns with the intended use!
This is in hopes of helping with or potentially closing #2687 which shed some light on having inadequate documentation for the same.
I made modifications to the
openmp-utilsmanual, and for reproducing them (to test formatting or checking things in the man-page style), one can userstudioapi::previewRd('path-to-your-data.table-installation/man/openmp-utils.Rd')to view inside RStudio after integrating my changes.(or click this in case you want to see how it looks here itself :)