mirror of https://github.com/davisking/dlib.git
Optimize pointer to matrix assignment (#2822)
* Optimize pointer to matrix assignment --------- Co-authored-by: Davis King <davis@dlib.net>
This commit is contained in:
parent
8fc180545c
commit
7736832caf
|
@ -448,6 +448,40 @@ namespace dlib
|
|||
return matrix_op<op>(op(ptr,nr,nc,stride));
|
||||
}
|
||||
|
||||
template <
|
||||
typename T,
|
||||
long NR,
|
||||
long NC,
|
||||
typename MM
|
||||
>
|
||||
typename enable_if<std::is_trivially_copyable<T>>::type matrix_assign (
|
||||
matrix<T,NR,NC,MM,row_major_layout>& dest,
|
||||
const matrix_exp<matrix_op<op_pointer_to_mat<T>>>& src
|
||||
)
|
||||
/*!
|
||||
An overload to catch statements of the form:
|
||||
some_matrix = mat(ptr,rows,cols, stride)
|
||||
and convert them into a memcpy(), which is a faster way to do the copy.
|
||||
!*/
|
||||
{
|
||||
if (dest.size() == 0) return;
|
||||
|
||||
// If the op_pointer_to_mat is referring to a contiguous block of memory then just one memcopy
|
||||
// is needed.
|
||||
if (src.ref().op.stride == dest.nc())
|
||||
{
|
||||
std::memcpy(&dest(0, 0), src.ref().op.ptr, dest.nr() * dest.nc() * sizeof(T));
|
||||
}
|
||||
else
|
||||
{
|
||||
// Otherwise memcpy() each row separately.
|
||||
for (long r=0; r<dest.nr(); r++)
|
||||
{
|
||||
std::memcpy(&dest(r, 0), &src(r,0), src.nc() * sizeof(T));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
}
|
||||
|
|
|
@ -1420,6 +1420,28 @@ namespace
|
|||
DLIB_TEST(mm(3) == 4);
|
||||
}
|
||||
|
||||
{
|
||||
const long n = 5;
|
||||
matrix<double> m1, m2;
|
||||
m1 = randm(n,n);
|
||||
m2 = randm(n,n);
|
||||
|
||||
m2 = mat(&m1(0,0),n,n);
|
||||
|
||||
DLIB_TEST(m1 == m2);
|
||||
}
|
||||
|
||||
{
|
||||
const long n = 5;
|
||||
matrix<double> m1, m2;
|
||||
m1 = randm(n,n);
|
||||
m2 = randm(n,n);
|
||||
|
||||
m2 = mat(&m1(0,0),n,n-1,n);
|
||||
|
||||
DLIB_TEST(m2 == subm(m1, 0, 0, n, n-1));
|
||||
}
|
||||
|
||||
{
|
||||
const long n = 5;
|
||||
matrix<double> m1, m2, m3, truth;
|
||||
|
|
Loading…
Reference in New Issue