我正在对张量进行一些迭代计算。我必须将张量映射到计算中的矩阵。我用于矩阵运算的库是“Eigen”。
类Tensor的描述:
类SVD的描述:
该计划的描述:
错误:
错误“free():invalid pointer”出现在第二次迭代中标有(*)的行。
void SOLVE::solve(){
Tensor S=this->tensor_; // initial tensor with index {"u","d","l","r"}
std::vector<std::string> row1{"u","l"};
std::vector<std::string> col1{"d","r"};
std::vector<std::string> row2{"u","r"};
std::vector<std::string> col2{"d","l"};
Tensor S1;
Tensor S2;
Tensor S3;
Tensor S4;
SVD svd;
for(int i=0;i<2;i++){
std::cout << i<< "check" << std::endl;
Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> matrix1=
transform_to_matrix(S,row1,col1); // (*) map to matrix by the sets of
// indices
std::cout << i<< "check" << std::endl;
svd.set_matrix(matrix1); // input matrix to do SVD
svd.solve(); // solve the SVD
S1=svd.get_ltensor("u","l","e"); // "u", "l", "e" are for index
// labeling
S3=svd.get_rtensor("f","r","d"); // "f", "r", "d" are for index
// labeling
Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic>matrix2=
transform_to_matrix(S,row2,col2); // map to matrix by another sets of
// indices
svd.set_matrix(matrix2); // update to new matrix
svd.solve(); // solve the SVD problem again
S4=svd.get_ltensor("u","r","g");
S2=svd.get_rtensor("h","d","l");
S1.set_index("u","a"); // rename the index "u" to "a"
S1.set_index("l","n");
S1.set_index("e","r");
S2.set_index("l","b");
S2.set_index("d","a");
S2.set_index("h","u");
S3.set_index("r","b");
S3.set_index("d","m");
S3.set_index("f","l");
S4.set_index("u","m");
S4.set_index("r","n");
S4.set_index("g","d");
// Indice of
// S1: {"a","n","r"}
// S2: {"b","a","u"}
// S3: {"b","m","l"}
// S4: {"m","n","d"}
S=S1*S2*S3*S4; // indices of S: {"u","d","l","r"}
}
}
Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic>
transform_to_matrix
(const Tensor &S,
std::vector<std::string> row,
std::vector<std::string> col)
{
// This function will map a DxDxDxD tensor to a D^2xD^2 matrix,
// according to the row indices and col indices required.
// Example:
// row={"u","d"}, col={"l","r"}
// The indices "u" and "d" are grouped into the row
// The indices "l" and "r" are grouped into the col
Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> matrix;
... // some mapping procedures.
std::cout << matrix << std::endl;
return matrix;
}
节目输出:
0check
... (a 4*4 matrix is printed here)
0check
1check
... (a 16*16 matrix is printed here)
Error message is printed here: "free(): invalid pointer:"
打印出16x16矩阵,所以一切都很好,直到函数transform_to_matrix()结束。所以问题是第二次迭代中的“矩阵”不能接受来自transform_to_matrix()的返回。 为什么会这样?我该如何解决?
更新:
错误有时是“free():next next(fast):”有时“free():无效指针”。
使用gdb进行调试
输出:
0check
... (a 4*4 matrix is printed here)
0check
1check
... (a 16*16 matrix is printed here)
Error message is printed here: "free(): invalid next size (fast): 0x00000000006c5570"
...
Program received signal SIGABRT, Aborted.
0x00007ffff71a1428 in __GI_raise (sig=sig@entry=6) at
../sysdeps/unix/sysv/linux/raise.c:54
54 ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
键入bt:
#0 0x00007ffff71a1428 in __GI_raise (sig=sig@entry=6) at../sysdeps/unix/sysv/linux/raise.c:54
#1 0x00007ffff71a302a in __GI_abort () at abort.c:89
#2 0x00007ffff71e37ea in __libc_message (do_abort=do_abort@entry=2,
fmt=fmt@entry=0x7ffff72fce98 "*** Error in `%s': %s: 0x%s ***\n") at
../sysdeps/posix/libc_fatal.c:175
#3 0x00007ffff71ec37a in malloc_printerr (ar_ptr=<optimized out>,
ptr=<optimized out>, str=0x7ffff72fcf10 "free(): invalid next size
(fast)", action=3) at malloc.c:5006
#4 _int_free (av=<optimized out>, p=<optimized out>, have_lock=0) at
malloc.c:3867
#5 0x00007ffff71f053c in __GI___libc_free (mem=<optimized out>) at
malloc.c:2968
#6 0x0000000000404cbe in __gnu_cxx::new_allocator<int>::deallocate
(this=0x7fffffffcc90, __p=0x6c5570) at
/usr/include/c++/5/ext/new_allocator.h:110
#7 0x00000000004048b0 in std::allocator_traits<std::allocator<int>
>::deallocate (__a=..., __p=0x6c5570, __n=2) at
/usr/include/c++/5/bits/alloc_traits.h:517
#8 0x0000000000404268 in std::_Vector_base<int, std::allocator<int>
>::_M_deallocate (this=0x7fffffffcc90, __p=0x6c5570, __n=2) at
/usr/include/c++/5/bits/stl_vector.h:178
#9 0x0000000000403813 in std::_Vector_base<int, std::allocator<int>
>::~_Vector_base (this=0x7fffffffcc90, __in_chrg=<optimized out>) at
/usr/include/c++/5/bits/stl_vector.h:160
#10 0x0000000000407fdd in std::vector<int, std::allocator<int>
>::~vector (this=0x7fffffffcc90, __in_chrg=<optimized out>) at
/usr/include/c++/5/bits/stl_vector.h:425
#11 0x0000000000467795 in transform_to_matrix (t=..., row=std::vector
of length 2, capacity 2 = {...},
col=std::vector of length 2, capacity 2 = {...}) at helper.cpp:189
#12 0x000000000040bcc2 in SOLVE::solve (this=0x7fffffffe1e0) at
solve.cpp:22
#13 0x0000000000402279 in main () at main.cpp:43
#13:
t.solve(); // call solve(); main.cpp:43
#12:
Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> matrix1=transform_to_matrix(S,row1,col1); // give error in the second iteration; solve.cpp:22
#11:
std::vector<int> col_arr(col_size,0); // col_size=2 ; helper.cpp:189