Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@alalek
Copy link
Member

@alalek alalek commented Dec 7, 2016

Compiler optimizer might reorder instructions for statements like these:

double re = (double)dataA[j*stepA]*dataB[j*stepB] -
            (double)dataA[(j+1)*stepA]*dataB[(j+1)*stepB];
double im = (double)dataA[j*stepA]*dataB[(j+1)*stepB] +
            (double)dataA[(j+1)*stepA]*dataB[j*stepB];
dataC[j*stepC] = (float)re; dataC[(j+1)*stepC] = (float)im;

In case of mixed read/write instructions result becomes wrong.

const float* dataA = (const float*)srcA.data;
const float* dataB = (const float*)srcB.data;
float* dataC = (float*)dst.data;

@alalek
Copy link
Member Author

alalek commented Dec 14, 2016

👍

@opencv-pushbot opencv-pushbot merged commit 7bceef7 into opencv:2.4 Dec 14, 2016
@alalek alalek mentioned this pull request Dec 15, 2016
Copy link
Contributor

@zchrissirhcz zchrissirhcz left a comment

Choose a reason for hiding this comment

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

So, the key for inplace checking and avoid compiler optimization, is to check two Mat's data, instead of two InputArray/OutputArray's obj?

i.e. the follwing approaches, which is correct?

void som_function(InputArray _src, OutputArray _dst)
{
    Mat src = _src.getMat( );
    Mat dst = _dst.getMat( );
    // check approach 1
    if ( _src.getObj( ) == _dst.getObj( ) ) {
        _src.copyTo(src);
    }
    // check approach 2
    if ( src.data == dst.data ) {
        _src.copyTo(src);
    }
}

// correct inplace support
// Case 'dst.data == srcA.data' is handled by implementation,
// because it is used frequently (filter2D, matchTemplate)
if (dst.data == srcB.data)
Copy link
Contributor

Choose a reason for hiding this comment

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

dst is a new created Mat, would dst.data equal to srcB.data? Since srcB = _srcB.getMat, and _srcB is passing from external calling, I don't think if (dst.data == srcB.data) is necessary.
@alalek

Copy link
Member Author

Choose a reason for hiding this comment

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

dst is a new created Mat

.create() do not reallocate buffer if requested size is the same. Some problems come from this behavior.

@zchrissirhcz
Copy link
Contributor

(Review this pull from cvtColor performance issue in #6760 )

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

Labels

port/backport done Label for maintainers. Authors of PR can ignore this

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants