Skip to content

Assert macros: minimize spacings between values in context window #2263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 7, 2018

Conversation

mark-poscablo
Copy link
Contributor

@mark-poscablo mark-poscablo commented Aug 6, 2018

Following up on #2257, the spacing between consecutive values in the context window (when values differ at some point) has been reduced in this change. It was previously hardcoded as 10 space chars - now, the output field width is the maximum between the string lengths of the dim0 position, the output value, and the reference value, so any unnecessary spaces are removed.

This also fixes a bug when u8 (unsigned char) is used. Previously, the context displays the character representation of the value. It's fixed now so it displays the number representation instead.

In addtion, this also changes the implementation of ravelIdx() from an explicit multiply-and-accumulate to using std::inner_product()

Here's a sample error message:

VALUE DIFFERS at (100):
Viewing slice (95:105), dims are (200)
        95     96     97     98     99     [100]    101    102    103    104    105    
 out: { 2.2345 2.2345 2.2345 2.2345 2.2345 [1]      2.2345 2.2345 2.2345 2.2345 2.2345 }
gold: { 2.2345 2.2345 2.2345 2.2345 2.2345 [2.2345] 2.2345 2.2345 2.2345 2.2345 2.2345 }
  Actual diff: 1.2344999313354492
Expected diff: 0.0010000000474974513

Using "normal" equality for u8 (unsigned char) type:

VALUE DIFFERS at (125, 82):
Viewing slice (120:130, 82), dims are (200, 100)
        120 121 122 123 124 [125] 126 127 128 129 130 
 out: { 173 50  245 245 75  [2]   10  10  76  239 188 }
gold: { 173 50  245 245 75  [138] 10  10  76  239 188 }

And using c32 (complex float):

VALUE DIFFERS at (3, 0, 1, 2):
Viewing slice (0:8, 0, 1, 2), dims are (200, 5, 4, 3)
        0                    1                     2                      [3]                    4                    5                    6                    7                    8                    
 out: { (0.478302, 0.220143) (0.0281664, 0.524339) (0.0122882, 0.0084334) [(2, 0)]               (0.181692, 0.274965) (0.372441, 0.669381) (0.022832, 0.717884) (0.373681, 0.060741) (0.824519, 0.816422) }
gold: { (0.478302, 0.220143) (0.0281664, 0.524339) (0.0122882, 0.0084334) [(0.024198, 0.999783)] (0.181692, 0.274965) (0.372441, 0.669381) (0.022832, 0.717884) (0.373681, 0.060741) (0.824519, 0.816422) }

Copy link
Member

@umar456 umar456 left a comment

Choose a reason for hiding this comment

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

Looks good. This might not be the most efficient way to do this but it works. It would be nice if the values were right-aligned instead of left.

// as numbers

template<typename T>
std::string operator+(const T& val) {
Copy link
Member

Choose a reason for hiding this comment

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

This is not necessary. You can remove this function. You only need to overload for the af::cfloat and af::cdouble.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 That makes sense. The whole point is just to avoid the + being interpreted as an addition for af complex types, and the stream << operator is already overloaded for those types, so simply returning the same value is enough.

}

template<>
std::string operator+(const af::cfloat& val) {
Copy link
Member

Choose a reason for hiding this comment

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

This function should just return the same value. It shouldn't convert to a string. That is done by the operator<<

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍


template<>
std::string operator+(const af::cdouble& val) {
std::ostringstream os;
Copy link
Member

Choose a reason for hiding this comment

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

Same as above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

std::ostringstream tmpOs;

uint dim0 = dim0Start + i;
if (dim0 == coords[0])
Copy link
Member

Choose a reason for hiding this comment

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

Use braces even if there is only one statement in the body of the if condition.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

std::vector<int> valsWidths;
std::vector<std::string> ctxDim0;
std::vector<std::string> ctxOutVals;
std::vector<std::string> ctxGoldVals;
Copy link
Member

Choose a reason for hiding this comment

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

Reserve these vectors if you know the size ahead of time.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 Yeah that's faster than having vectors resize from time to time as it grows


int maxWidth = std::max<int>(dim0Len, outLen);
maxWidth = std::max<int>(maxWidth, goldLen);
valsWidths.push_back(maxWidth);
Copy link
Member

Choose a reason for hiding this comment

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

Is it necessary to store the lengths of each of the values? I would have thought max would be enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I suppose the max of all the string lengths would be enough, but I thought if there's one value that's abnormally long (compared to the rest of the values), or if the indices are long compared to the values, then there would be a lot of unnecessary spaces as well. Thus I decided to just use the maximum per element between the index, the out value, and the reference value. What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

It's not a big deal. This code shouldn't be running all the time anyway.


// Display dim0 positions
// Display dim0 positions, output values, and reference values

Copy link
Member

Choose a reason for hiding this comment

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

Extra line.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

} else
os << std::setw(valsWidth) << std::left << i << " ";
for (uint i = 0; i < (dim0End - dim0Start); ++i) {
os << std::setw(valsWidths[i]) << std::left << ctxDim0[i]
Copy link
Member

Choose a reason for hiding this comment

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

Would you need the extra space at the end if you added one to the setw value?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 Turns out that I don't!


// Get dim0 positions and out/reference values for the context window
// Also get the max string length between the position and out/ref values
// per item so that it can be used later as the field width for
Copy link
Member

Choose a reason for hiding this comment

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

Extra space at the beginning of the comment line.

@mark-poscablo
Copy link
Contributor Author

Here are the sample error messages after the changes:

VALUE DIFFERS at (100):
Viewing slice (95:105), dims are (200)
         95     96     97     98     99     [100]    101    102    103    104    105    
 out: {  2.2345 2.2345 2.2345 2.2345 2.2345      [1] 2.2345 2.2345 2.2345 2.2345 2.2345}
gold: {  2.2345 2.2345 2.2345 2.2345 2.2345 [2.2345] 2.2345 2.2345 2.2345 2.2345 2.2345}
  Actual diff: 1.2344999313354492
Expected diff: 0.0010000000474974513
VALUE DIFFERS at (125, 82):
Viewing slice (120:130, 82), dims are (200, 100)
         120 121 122 123 124 [125] 126 127 128 129 130 
 out: {  173  50 245 245  75   [2]  10  10  76 239 188}
gold: {  173  50 245 245  75 [138]  10  10  76 239 188}
VALUE DIFFERS at (3, 0, 1, 2):
Viewing slice (0:8, 0, 1, 2), dims are (200, 5, 4, 3)
         0                    1                     2                      [3]                    4                    5                    6                    7                    8                    
 out: {  (0.478302, 0.220143) (0.0281664, 0.524339) (0.0122882, 0.0084334)               [(2, 0)] (0.181692, 0.274965) (0.372441, 0.669381) (0.022832, 0.717884) (0.373681, 0.060741) (0.824519, 0.816422)}
gold: {  (0.478302, 0.220143) (0.0281664, 0.524339) (0.0122882, 0.0084334) [(0.024198, 0.999783)] (0.181692, 0.274965) (0.372441, 0.669381) (0.022832, 0.717884) (0.373681, 0.060741) (0.824519, 0.816422)}

@mark-poscablo
Copy link
Contributor Author

I had to fix the uneven border spaces near the braces haha

@umar456
Copy link
Member

umar456 commented Aug 7, 2018

Maybe the indices should be right/center aligned as well. the complex example looks weird.

@mark-poscablo
Copy link
Contributor Author

Haha true. Here's the outputs now:

VALUE DIFFERS at (100):
Viewing slice (95:105), dims are (200)
            95     96     97     98     99    [100]    101    102    103    104    105
 out: { 2.2345 2.2345 2.2345 2.2345 2.2345      [1] 2.2345 2.2345 2.2345 2.2345 2.2345 }
gold: { 2.2345 2.2345 2.2345 2.2345 2.2345 [2.2345] 2.2345 2.2345 2.2345 2.2345 2.2345 }
  Actual diff: 1.2344999313354492
Expected diff: 0.0010000000474974513
VALUE DIFFERS at (125, 82):
Viewing slice (120:130, 82), dims are (200, 100)
        120 121 122 123 124 [125] 126 127 128 129 130
 out: { 173  50 245 245  75   [2]  10  10  76 239 188 }
gold: { 173  50 245 245  75 [138]  10  10  76 239 188 }
VALUE DIFFERS at (3, 0, 1, 2):
Viewing slice (0:8, 0, 1, 2), dims are (200, 5, 4, 3)
                           0                     1                      2                    [3]                    4                    5                    6                    7                    8
 out: { (0.478302, 0.220143) (0.0281664, 0.524339) (0.0122882, 0.0084334)               [(2, 0)] (0.181692, 0.274965) (0.372441, 0.669381) (0.022832, 0.717884) (0.373681, 0.060741) (0.824519, 0.816422) }
gold: { (0.478302, 0.220143) (0.0281664, 0.524339) (0.0122882, 0.0084334) [(0.024198, 0.999783)] (0.181692, 0.274965) (0.372441, 0.669381) (0.022832, 0.717884) (0.373681, 0.060741) (0.824519, 0.816422) }

@9prady9 9prady9 merged commit 5d02259 into arrayfire:master Aug 7, 2018
@mark-poscablo mark-poscablo deleted the fix-context-spacings branch October 29, 2018 17:51
@mlloreda mlloreda added this to the v3.6.2 milestone Nov 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants