Skip to content

Feature Request: get multithreading status at runtime #362

Closed

Description

@jeremiedbb

Hi,

maybe it's already there but I haven't found any way to know at runtime what is the status of multithreading, i.e. openmp, pthreads or no multithreading. I could be nice to have that for runtime introspection.

Also, when built with no multithreading, the functions bli_thread_get(set)_num_threads act as if multithreading was enabled. For instance

bli_thread_get_num_threads()
-1
bli_thread_set_num_threads(2)
bli_thread_get_num_threads()
2

is confusing. wdyt ?

thanks

Activity

  1. fgvanzee commented on Nov 8, 2019

    @fgvanzee
    Member

    maybe it's already there but I haven't found any way to know at runtime what is the status of multithreading, i.e. openmp, pthreads or no multithreading. I could be nice to have that for runtime introspection.

    This more or less exists. The following code snippet is taken from the testsuite to show how it can be done:

        // Describe the threading implementation.
        if      ( bli_info_get_enable_openmp()   ) sprintf( impl_str, "openmp" );
        else if ( bli_info_get_enable_pthreads() ) sprintf( impl_str, "pthreads" );
        else    /* threading disabled */           sprintf( impl_str, "disabled" );

    Also, when built with no multithreading, the functions bli_thread_get(set)_num_threads act as if multithreading was enabled.

    I'll address this in a subsequent post.

  2. fgvanzee commented on Nov 8, 2019

    @fgvanzee
    Member

    Also, when built with no multithreading, the functions bli_thread_get(set)_num_threads act as if multithreading was enabled.

    This is actually a more complicated topic than it may appear.

    First, let's start with the fact that, even when multithreading is enabled, bli_thread_set_num_threads() does not return the number of threads actively running, but rather [something whose meaning is closer to] the number that will be requested when multithreading is engaged. If you look at it through that lens, the current behavior is more understandable: your example code requests two threads, but the implementation is ignoring your request (because it can't comply).

    Second, even with this knowledge in mind, you would be forgiven for thinking that bli_thread_get_num_threads() returns the number of threads previously requested to execute. But that's not quite right. It's merely the value that was most recently submitted via bli_thread_set_num_threads(). So what's the nuance? BLIS exposes the individual ways of parallelism to extract for each loop in the matrix multiplication algorithm it employs. You can set these individual ways of parallelism without setting the total number via bli_thread_set_num_threads(). So what happens when you set both (ie: the manual way and the automatic way)? The manual, individual loop-specific values take priority, and the total number, whatever it may have been set to, is ignored. But in the interim, you can still query the parameters corresponding to both manual and automatic ways, and they may appear contradictory.

    Going back to your original question, I can understand why someone new to BLIS might be confused by the behavior of these APIs. One could imagine a patch to narrowly address your concern in the case of a single-threaded build of BLIS, one that disallows the changing of the internal data structure if multithreading is disabled. To recycle your example code, this would result in:

    // printf() of bli_thread_get_num_threads();
    -1
    bli_thread_set_num_threads(2);
    // printf() of bli_thread_get_num_threads();
    -1

    We would probably also need to do the same for the individual loop-level parameters:

    // printf() of bli_thread_get_jc_nt();
    1
    bli_thread_set_ways( 2, 1, 1, 1, 1 );
    // printf() of bli_thread_get_jc_nt();
    1

    @devinamatthews @jeffhammond @dnparikh What do you think?

  3. fgvanzee commented on Nov 8, 2019

    @fgvanzee
    Member

    @devinamatthews @jeffhammond @dnparikh Just to review, here are the options currently on the table:

    1. The status quo asserts that, for single-threaded builds of BLIS, setting the number of threads has a visible effect (i.e., querying via bli_thread_get_num_threads() suggests your change was applied), but that effect is ignored by the implementation during execution of multithreading-eligible operations (e.g. currently level-3 operations).
    2. A possible alternative (described in the previous post) asserts that, for single-threaded builds of BLIS, setting the number of threads has no visible effect (i.e., querying via bli_thread_get_num_threads() suggests your change was not applied) because the request to change/set the number of threads itself is ignored.
  4. jeremiedbb commented on Nov 12, 2019

    @jeremiedbb
    Author

    This more or less exists. The following code snippet is taken from the testsuite to show how it can be done

    Great thanks !

  5. jeremiedbb commented on Nov 12, 2019

    @jeremiedbb
    Author

    bli_thread_get_num_threads() returns the number of threads previously requested to execute

    My confusion might come from me trying to compare with other blas implementations. For instance mkl_get_max_threads is documented as number of threads available to use. So for mkl it makes sense that when using the sequential threading layer, mkl_set_num_threads has no effect on mkl_get_max_threads.

    I think your proposition would be less confusing for newbie users like me :)
    But maybe just mentioning it the multithreading documentation would be enough.

  6. fgvanzee commented on Nov 12, 2019

    @fgvanzee
    Member

    @jeremiedbb I've updated the language in the Multithreading.md document in 8f399c8:

    Note: Please be aware of what happens if you try to specify both the automatic and manual ways, as it could otherwise confuse new users. Here are the important points:

    • Regardless of which broad method is used, if multithreading is specified via both the automatic and manual ways, the values set via the manual way will always take precedence.
    • Specifying parallelism for even one loop counts as specifying the manual way (in which case the ways of parallelism for the remaining loops will be assumed to be 1).
    • If you have specified multithreading via both the automatic and manual ways, BLIS will not complain if the values are inconsistent with one another. (For example, you may request 8 total threads be used while also specifing 4 ways of parallelism within each of two matrix multiplication loops, for a total of 16 ways.) Furthermore, you will be able to query these inconsistent values via the runtime API both before and after multithreading executes.
    • If multithreading is disabled, you may still specify multithreading values via either the manual or automatic ways. However, BLIS will silently ignore all of these values. A BLIS library that is built with multithreading disabled at configure-time will always run sequentially (from the prespective of a single application thread).
  7. jeremiedbb commented on Nov 13, 2019

    @jeremiedbb
    Author

    That's great, thanks a lot !

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions