![](/img/trans.png)
[英]Executing a MATLAB script from Java gives EXCEPTION_ACCESS_VIOLATION error
[英]cusp::io::write_matrix_market_file() gives Access Violation Exception
我正在嘗試使用CUSP將Matlab的272 x 544雙矩陣讀取為ELLPACK格式的稀疏矩陣。 到目前為止,我唯一能找到的方法是將數據讀入mxArray,將mxArray讀入double *數組(使用mxGetPr()),然后將值復制到cusp :: array2d。 到達那里后,我需要將array2d寫入.mtx文件,然后將其讀入coo_matrix,最后將其轉換為ell_matrix。 即使對我來說,這聽起來也很愚蠢,但是鑒於CUSP的文檔很少,這是我能想到的最好的。 如果有人推薦一種更好的方法,我將非常感謝。
無論如何,我現在面臨的問題是,在讀取array2d中的數據並嘗試使用cusp :: write_matrix_market_file()將其寫入.mtx文件后,我收到“未處理的異常:寫入位置訪問沖突”錯誤消息。 我嘗試從array2d中打印一些值,然后再嘗試將它們寫入文件,只是為了確保所有內容都到位並且這些值似乎可以很好地存儲在array2d中。 我也嘗試將類型更改為float,但錯誤仍然存在。 這是我完成此任務的代碼:
#include <thrust/version.h>
#include <cusp/version.h>
#include <cusp/ell_matrix.h>
#include <cusp/print.h>
#include <cusp/io/matrix_market.h>
#include <cusp/array2d.h>
#include <cusp/coo_matrix.h>
int main(int argc, char** argv)
{
const mxArray* mx_g_tra;
//Reading data from Matlab
mx_g_tra = openMatFile2("C:\\out.mat", ndir, "out.tra");
double* g_tra = mxGetPr(mx_g_tra);
sizeG_tra=(int)mxGetNumberOfElements(mx_g_tra);
const mwSize* traDims= mxGetDimensions(mx_g_tra);
//get matrix dimensions
tra_W= traDims[0];
tra_H= traDims[1];
printf("\nAcquired %d TRA elements as %d x %d from Maltab.\n", sizeG_tra, tra_W, tra_H);
cusp::array2d<float, cusp::host_memory> TRA(traDims[0], traDims[1]);
if(g_tra != NULL)
{
for(int i=0; i< traDims[0]; i++)
{
for(int j=0; j<traDims[1]; j++)
{TRA(i,j) = (float)g_tra[i*traDims[1]+j];
if(TRA(i,j) != 0) printf("\nTRA(%d, %d) = %g", i, j, TRA(i,j));}
}
}
cusp::io::write_matrix_market_file(TRA, "C:\\TRA.mtx");
在出現異常時,調用堆棧指向:cusp :: io :: detail :: write_value>,float>(std :: basic_ofstream>&output = {...},const float&value =)行116 + 0x5字節的C ++
編輯:快速更新:我通過修改文件“ matrix_market.inl”擺脫了異常,它們有一個怪異的嵌套循環,該循環通過遍歷矩陣cols然后再次cols將值寫入文件(從不考慮num_rows)顯然,因為我的矩陣是如果不是正方形,則代碼正在嘗試訪問不存在的位置。這是修改后的代碼:
for(size_t j = 0; j < mtx.num_cols; j++)
{
// Modified below: mtx.num_cols --> mtx.num_rows
for(size_t i = 0; i < mtx.num_rows; i++)
{
write_value(output, mtx(i,j));
output << "\n";
}
}
我不確定這從.mtx文件讀取時如何影響不同格式的構造函數
這段代碼為我工作,展示了如何直接從array2d轉換為coo或ell:
編輯:更新的代碼示例以顯示cusp::is_valid_matrix()
用法
#include <stdio.h>
#include <cusp/verify.h>
#include <cusp/ell_matrix.h>
#include <cusp/io/matrix_market.h>
#include <cusp/array2d.h>
#include <cusp/coo_matrix.h>
int main(int argc, char** argv)
{
// initial matrix
cusp::array2d<float, cusp::host_memory> E(4, 3);
E(0,0) = 1.000e+00; E(0,1) = 0.000e+00; E(0,2) = 0.000e+00;
E(1,0) = 0.000e+00; E(1,1) = 1.050e+01; E(1,2) = 0.000e+00;
E(2,0) = 0.000e+00; E(2,1) = 0.000e+00; E(2,2) = 2.500e-01;
E(3,0) = 0.000e+00; E(3,1) = 2.505e+02; E(3,2) = 0.000e+00;
cusp::coo_matrix<int, float, cusp::host_memory> coo(E);
cusp::ell_matrix<int, float, cusp::host_memory> ell(E);
if (!cusp::is_valid_matrix(coo)) {printf("Invalid COO\n"); return 1;}
if (!cusp::is_valid_matrix(ell)) {printf("Invalid ELL\n"); return 1;}
cusp::io::write_matrix_market_file(coo, "COO.mtx");
cusp::io::write_matrix_market_file(ell, "ELL.mtx");
return 0;
}
只要行數大於cols數,就不會有異常,代碼也不會出現問題。 我遇到的問題是因為行數是cols數的一半。 因此,當文件“ matrix_market.inl”中的write_value假定我的矩陣是一個正方形矩陣,並且在兩個循環中僅考慮num_cols時,我得到了錯誤。 實際上,引發異常時的i值為273(我的272 x 544矩陣中不存在的第一行)。 我通過在“ matrix_market.inl”代碼中編輯write_matrix_market_stream來解決此問題,如下所示:
template <typename Matrix, typename Stream>
void write_matrix_market_stream(const Matrix& mtx, Stream& output, cusp::array2d_format)
{
typedef typename Matrix::value_type ValueType;
bool is_complex = thrust::detail::is_same<ValueType, cusp::complex<typename norm_type<ValueType>::type> >::value;
if (is_complex)
output << "%%MatrixMarket matrix array complex general\n";
else
output << "%%MatrixMarket matrix array real general\n";
output << "\t" << mtx.num_rows << "\t" << mtx.num_cols << "\n";
for(size_t j = 0; j < mtx.num_cols; j++)
{
// EDIT needed here
// Modified below: mtx.num_cols --> mtx.num_rows
for(size_t i = 0; i < mtx.num_rows; i++)
{
write_value(output, mtx(i,j));
output << "\n";
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.