Skip to content

Possible missing sanity check in bli_determine_blocksize_b_sub() #148

Closed

Description

@hominhquan

In bli_determine_blocksize_b_sub() - from frame/base/bli_blksz.c:285, I think a sanity check should be added for the case dim_left_now == 0. The reason is as follow :

dim_t bli_determine_blocksize_b_sub(....)
{

// Compute how much of the matrix dimension is left, including the
// chunk that will correspond to the blocksize we are computing now.
dim_left_now = dim - i;

// Sanity check : if dim_left_now is zero then return zero without
// going any further.
// For bli_determine_blocksize_f_sub(), this check was somehow luckily done
// by comparing to b_max. Here, since we intelligently return b_alg if
// dim_left_now multiple of b_alg, we must take care of the case where
// dim_left_now is zero, because zero modulo anything is zero.
if ( dim_left_now == 0 )
	return 0;

dim_at_edge = dim_left_now % b_alg;

// If dim_left_now is a multiple of b_alg, we can safely return b_alg
// without going any further.
if ( dim_at_edge == 0 )
	return b_alg;

}

Activity

  1. fgvanzee commented on Aug 4, 2017

    @fgvanzee
    Member

    @hominhquan Can you give an example of when this sanity check would be needed? The only case I can think of in which it would apply is if computation is performed on a matrix where the initial dimension being partitioned is equal to zero. (But this situation should actually be handled by higher-level code, so the sanity check would never be used there either.)

  2. devinamatthews commented on Aug 4, 2017

    @devinamatthews
    Member

    @fgvanzee it depends what the assumptions and invariants of the function are. For example, it would seem reasonable to assume that i+(return value) <= dim which is currently violated. A different use of this function without the higher-level checking would then fail.

    I would suggest (as a longer-term robustness plan) to document these assumptions and invariants through assertions (a la what @hominhquan has suggested) and through unit tests.

  3. fgvanzee commented on Aug 4, 2017

    @fgvanzee
    Member

    @devinamatthews Could you elaborate on why one would assume i+(return value) <= dim to be a reasonable invariant for this blocksize-returning function?

    EDIT: Nevermind. I misread the inequality. But, could you explain why it is violated?

  4. hominhquan commented on Aug 4, 2017

    @hominhquan
    ContributorAuthor

    @fgvanzee Devin pointed out the problem. In fact, I'm using this function to determine the block size for the iteration n+1 while computing iteration n :

     next_b_alg = bli_determine_blocksize( direct, i+b_alg, my_end, a,            
                                       bli_cntl_bszid( cntl ), cntx );
     if ( next_b_alg > 0 ) { do some stuff here, like prefetch next block ... }
    

    And in this case, the sanity check ensures that : if I'm computing the last block (n), so I will get zero for the next block size (n+1), because the iteration n+1 does not even exist (thanks to the for loop of the invariant) and I should stop prefetching data here.

    EDIT : with the for-loop of the invariant function, the runtime only knows it has reached the end when it comes back to the for-loop and re-evaluates the condition. However, a code which wants to prefetch data for the next iteration should be pre-aware of this, to make a decision to prefetch data or not.

  5. devinamatthews commented on Aug 4, 2017

    @devinamatthews
    Member
  6. devinamatthews commented on Aug 4, 2017

    @devinamatthews
    Member
  7. fgvanzee commented on Aug 4, 2017

    @fgvanzee
    Member

    @devinamatthews @hominhquan For some reason, I completely missed that you wanted the function to return 0 when i == dim. If so, I see how it is violated now: the function continues to return b_alg.

    @hominhquan I agree that your proposed special case handling is beneficial.

  8. fgvanzee commented on Aug 5, 2017

    @fgvanzee
    Member

    @hominhquan I've applied your suggested patch in 60a1eeb. Thanks for your feedback.

  9. hominhquan commented on Aug 6, 2017

    @hominhquan
    ContributorAuthor

    @fgvanzee With pleasure.

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