Skip to content

Documented the use of OpenMP and parallelization in C code - #6018

Merged
MichaelChirico merged 22 commits into
Rdatatable:masterfrom
Anirban166:openmp
Sep 3, 2024
Merged

MichaelChirico merged 22 commits into
Rdatatable:masterfrom
Anirban166:openmp

Conversation

@Anirban166

Copy link
Copy Markdown
Member

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:

‘between.c’ - between()
‘cj.c’ - CJ()
‘coalesce.c’ - fcoalesce()
‘fifelse.c’ - fifelse()
‘fread.c’ - fread()
‘forder.c’, ‘fsort.c’, and ‘reorder.c’ - forder() and related
‘froll.c’, ‘frolladaptive.c’, and ‘frollR.c’ - froll() and family
‘fwrite.c’ - fwrite()
‘gsumm.c’ - GForce in various places, see GForce
‘nafill.c’ - nafill()
‘subset.c’ - Used in ‘[.data.table’ subsetting
‘types.c’ - Internal testing usage

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-utils manual, and for reproducing them (to test formatting or checking things in the man-page style), one can use rstudioapi::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 :) How my changes look like in the man page

…felse() and nafill() (taking longer to read and understand fread due to its size, and will probably make more changes to these too)
…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

codecov Bot commented Mar 23, 2024 •

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.53%. Comparing base (b4ae3ef) to head (2005487).
Report is 185 commits behind head on master.

❗ Current head 2005487 differs from pull request most recent head a56c7a9

Please upload reports for the commit a56c7a9 to get more accurate results.

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.
📢 Have feedback on the report? Share it here.

@jangorecki

Copy link
Copy Markdown
Member

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.

@Anirban166

Copy link
Copy Markdown
Member Author

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).

@MichaelChirico

Copy link
Copy Markdown
Member

I agree with Jan. This is a welcome change but very prone to becoming outdated.

I think all documentation could use that, or a 'last updated' time point (considering that everything is subject to change in the future).

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.

@MichaelChirico
MichaelChirico requested review from tdhock and removed request for MichaelChirico March 25, 2024 04:05
@Anirban166

Copy link
Copy Markdown
Member Author

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.

Thanks for explaining! That makes sense, and I see the distinction now.

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?).

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).

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.

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)

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.

Sure, and Toby mentioned this as something to work on earlier so it would certainly be appropriate.

@MichaelChirico

Copy link
Copy Markdown
Member

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)

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

@Anirban166

Copy link
Copy Markdown
Member Author

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:

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.

@tdhock

tdhock commented Mar 26, 2024

Copy link
Copy Markdown
Member

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.
Untitled
Would be nice to have some explanation about why/when we would expect speedups with large ncol/ncol.

@MichaelChirico

Copy link
Copy Markdown
Member

From a user perspective I would like the docs to tell us if we get parallel speedups with large nrow or ncol.

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
@Anirban166

Copy link
Copy Markdown
Member Author

From a user perspective I would like the docs to tell us if we get parallel speedups with large nrow or ncol.

Would be nice to have some explanation about why/when we would expect speedups with large ncol/ncol.

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)

@tdhock

tdhock commented Jun 1, 2024

Copy link
Copy Markdown
Member

@Nj221102 can you please review?

@Anirban166

Copy link
Copy Markdown
Member Author

@ben-schwen If possible, would you like to review this Ben?

Comment thread man/openmp-utils.Rd Outdated
\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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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. }

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True

…s already does a better job at explaining than what I wrote
@Anirban166

Copy link
Copy Markdown
Member Author

@MichaelChirico Also, could you review this? (once more now, but for either merging/closing - I'm cool either way!)

@MichaelChirico

Copy link
Copy Markdown
Member

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:

  • Put it in a Wiki. This is still sort of user-facing.
  • Put it in a document like CRAN_release in .dev folder
  • Put it in the comments above the corresponding routine in the src/ code

Right now I think the 3rd option is best -- the comments then live closest to where they're useful. Any other thoughts?

@tdhock

tdhock commented Sep 3, 2024

Copy link
Copy Markdown
Member

user only cares when a function is taking 2+-dimensional input, which dimension is parallelized? (most often rows vs. columns).

yes please I would like to see something on man pages in this case. (fread and fwrite)

for other cases, comments are ok.

@jangorecki

Copy link
Copy Markdown
Member

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
@Anirban166

Copy link
Copy Markdown
Member Author

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:

  • Put it in a Wiki. This is still sort of user-facing.
  • Put it in a document like CRAN_release in .dev folder
  • Put it in the comments above the corresponding routine in the src/ code

Right now I think the 3rd option is best -- the comments then live closest to where they're useful. Any other thoughts?

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!

Comment thread man/openmp-utils.Rd Resolved
@Anirban166

Copy link
Copy Markdown
Member Author

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?

@Anirban166

Copy link
Copy Markdown
Member Author

yes please I would like to see something on man pages in this case. (fread and fwrite)

I'm assuming you still want them in their man pages right? or would what Michael did here suffice?

@MichaelChirico

Copy link
Copy Markdown
Member

do you guys think rest of that information (or for the other routines) is worth retaining?

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.

@Anirban166

Copy link
Copy Markdown
Member Author

in the man page it was more useful as there's more separation between the comment & the logic

Same thoughts (especially at the time of writing)

But if you feel strongly feel free to add back into the comments.

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)

@MichaelChirico

Copy link
Copy Markdown
Member

I'm assuming you still want them in their man pages right? or would what Michael did here suffice?

That's a good point. Yes, let's make the mention in the corresponding man page directly, that will be most useful to users.

Comment thread man/openmp-utils.Rd
Comment on lines +44 to +48
\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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for me this is sufficient documentation of whether the parallelization happens on rows or columns

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 :)

@MichaelChirico
MichaelChirico merged commit c3ba954 into Rdatatable:master Sep 3, 2024
@MichaelChirico

Copy link
Copy Markdown
Member

Thanks all!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants