struct sparseStruct
{
int numRow;
int numCol;
int numVal;
int *colPtr;
int *rowIdx;
double *value;
};
int *readMatrix(char *fname, sparseStruct *mat, int &numEmptyCol)
//从文件读入一个稀疏矩阵到mat中,返回一些空向量的IDs
{
char filename[FILENAME_LENGTH];
sprintf(filename, "%s%s", fname, "_dim");
std::ifstream dimFile(filename);//包含行数、列数、值的个数的文档
sprintf(filename, "%s%s", fname, "_row_ccs");
std::ifstream rowPtrFile(filename);//包含行ID的文档
sprintf(filename, "%s%s", fname, "_col_ccs");
std::ifstream colIdxFile(filename);//不明白是不是列ID的文档,这个文档的内容不太清楚。
sprintf(filename, "%s%s", filename, "_nz");
std::ifstream valFile(filename);//包含非零值的文档
cout << " Reading dimension file ... " << endl;
dimFile >> mat->numRow >> mat->numCol >> mat->numVal;//读入维度文件信息
dimFile.close();
mat->colPtr = new int[mat->numCol+1];
mat->rowIdx = new int[mat->numVal];
mat->value = new double[mat->numVal];
memoryUsed += (mat->numCol + 1 + mat->numVal) * sizeof(int) + mat->numVal * sizeof(double);//存储稀疏矩阵的空间值
int pre = -1;//不太清楚这一段是怎么存储空向量的
int *tempEmptyColId = new int[mat->numCol], *emptyColId;
numEmptyCol = 0;
cout << " Reading column pointer file ... " << endl;
for (int i = 0; i < mat->numCol+1; i++){
colIdxFile >> (mat->colPtr)[i];
if ((mat->colPtr)[i] == pre){
tempEmptyColId[numEmptyCol] = i - 1;
numEmptyCol++;
}
pre = (mat->colPtr)[i];
}
colIdxFile.close();
emptyColId = new int[numEmptyCol+1];
for(int i = 0; i < numEmptyCol; i++)
emptyColId[i] = tempEmptyColId[i];
emptyColId[numEmptyCol] = mat->numCol;
delete [] tempEmptyColId;
cout << " Reading row index file ... " << endl;读入行的ID
for (int i = 0; i < mat->numVal; i++)
rowPtrFile >> (mat->rowIdx)[i];
rowPtrFile.close();
cout << " Reading non-zero value file ... " << endl;读入非零的值
for (int i = 0; i < mat->numVal; i++)
valFile >> (mat->value)[i];
valFile.close();
cout << endl;
if (numEmptyCol == 0)
cout << " !!! No empty col found !!!" << endl;
else {
cout << " !!! " << numEmptyCol << " empty col(s) found !!!" << endl;
for(int i = 0; i < numEmptyCol; i++)
cout << emptyColId[i] << " ";
cout << endl;
}
return emptyColId;
}
sparseStruct *mat 是一个结构变量,存储从file中读取的稀疏矩阵,在Reading column pointer file 时遇到一些问题,比如一个稀疏矩阵
0 2 4 0 1
3 2 0 0 0
0 0 4 0 0
2 3 0 0 0
维度文件fname_dim的内容为4 5 8
行文件fname_row_ccs的内容应该是行的ID,因为从上述代码中可以看到它有8个值。
但是列文件fname_col_ccs的内容应该是什么?这个代码是怎么读入一个稀疏矩阵的?