Skip to content

OpenMP problems in .onLoad #77

Closed
@aitap

Description

@aitap

Hello!

The current omp_thread_count() tries to start as many OpenMP threads as feasible and then count them one by one:

rsparse/src/utils.cpp

Lines 86 to 89 in 695d4eb

#ifdef _OPENMP
#pragma omp parallel reduction(+ : n)
#endif
n += 1;

This results in a NOTE on CRAN and has caused problems for at least two of the reverse dependencies of rsparse (tidymodels/textrecipes#251, recent R-pkg-devel thread) when the threads started by rsparse:::.onLoad caused more than 250% average CPU usage.

I think you can obtain the desired "default" number of threads without starting a parallel group, by choosing the minimal value between omp_get_max_threads() (which is documented to take OMP_NUM_THREADS into account if set) and omp_get_thread_limit() (which takes into account the additional OMP_THREAD_LIMIT).

Additionally, it would be beneficial to only set options(rsparse_omp_threads=...) when it is not already set:

options("rsparse_omp_threads" = n_omp_threads)

This will let your users set the option in their .Rprofile without having to load your package first.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions