以下问题可能很容易回答,但我在互联网上找不到任何解决方案。简而言之,我在课堂上放了一些petc函数调用。
以下等式求解器脚本可以正常工作:
static char help[] = "3x3-Equation system\n\n";
#include <petscksp.h>
#undef __FUNCT__
#define __FUNCT__ "main"
int main(int argc,char **argv)
{
PetscErrorCode ierr;
PetscMPIInt rank;
PetscInt i,j,N;
PetscInt l[] = {0,1,2}; // indices for right hand vector (rhv)
PetscScalar vec[] = {0.0,5.0,3.0}; //values of rhv
PetscScalar matrix[3][3]= {{-1.0,1.0,1.0},{1.0,-3.0,-2.0},{5.0,1.0,4.0}};
Vec b,x;
Mat A;
KSP ksp; /* linear solver context */
PC pc; /* preconditioner context */
PetscInitialize(&argc,&argv,(char*)0,help);
ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
PetscPrintf(PETSC_COMM_SELF,"[%d] rank\n",rank);
//Init vector
ierr = VecCreate(PETSC_COMM_WORLD,&b);CHKERRQ(ierr);
ierr = VecSetSizes(b,3,PETSC_DECIDE);CHKERRQ(ierr);
ierr = VecSetFromOptions(b);CHKERRQ(ierr);
ierr = VecDuplicate(b,&x);CHKERRQ(ierr);
//ierr = VecGetSize(x,&N);CHKERRQ(ierr);
//ierr = VecSet(x,vec[2]);CHKERRQ(ierr);
ierr = VecSetValues(b,3,l,vec,INSERT_VALUES);CHKERRQ(ierr);
ierr = VecAssemblyBegin(b);CHKERRQ(ierr);
ierr = VecAssemblyEnd(b);CHKERRQ(ierr);
//Init matrix
ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr);
ierr = MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,3,3);CHKERRQ(ierr);
ierr = MatSetFromOptions(A);CHKERRQ(ierr);
ierr = MatSetUp(A);CHKERRQ(ierr);
for(i = 0; i < 3; i++){
for(j = 0; j < 3; j++){
ierr = MatSetValues(A,1,&i,1,&j,&matrix[i][j],INSERT_VALUES);CHKERRQ(ierr);
}
}
ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
//Solve
ierr = KSPCreate(PETSC_COMM_WORLD,&ksp);CHKERRQ(ierr);
ierr = KSPSetOperators(ksp,A,A,DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr);
ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);
ierr = PCSetType(pc,PCJACOBI);CHKERRQ(ierr);
ierr = KSPSetTolerances(ksp,1.e-5,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr);
ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr);
ierr = KSPView(ksp,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
//View rhv
PetscPrintf(PETSC_COMM_SELF,"right hand vector:\n");
ierr = VecView(b,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
//View matrix
PetscPrintf(PETSC_COMM_SELF,"matrix:\n");
ierr = MatView(A,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
//View solution
PetscPrintf(PETSC_COMM_SELF,"solution:\n");
ierr = VecView(x,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
//Clean all
/*ierr = VecDestroy(&b);CHKERRQ(ierr);
ierr = VecDestroy(&x);CHKERRQ(ierr);
ierr = MatDestroy(&A);CHKERRQ(ierr);
*/
ierr = PetscFinalize();
return 0;
}
但是当我在一个类中外包代码时,会导致一些错误:
#include <petscksp.h>
static char help[] = "Easy equation solver\n\n";
class externSolver
{
public:
PetscErrorCode ierr;
PetscMPIInt rank;
Vec b,x;
Mat A;
KSP ksp; /* linear solver context */
PC pc; /* preconditioner context */
externSolver(int argc,char **argv)
{
ierr = PetscInitialize(&argc,&argv,(char*)0,help);CHKERRQ(ierr);
ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
ierr = PetscPrintf(PETSC_COMM_SELF,"[%d] rank\n",rank);CHKERRQ(ierr);
return;
}
~externSolver()
{
}
void generateMatrix(int cols, int rows)
{
ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr);
ierr = MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,cols,rows);CHKERRQ(ierr);
ierr = MatSetFromOptions(A);CHKERRQ(ierr);
ierr = MatSetUp(A);CHKERRQ(ierr);
}
void MatSetValues(PetscInt num_rows,PetscInt* rows,PetscInt num_cols,PetscInt* cols,PetscScalar* Value)
{
ierr = MatSetValues(A,num_rows,rows,num_cols,cols,Value,INSERT_VALUES);CHKERRQ(ierr);
}
void assemblyMatrix()
{
ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
}
void generateVectors()
{
ierr = VecCreate(PETSC_COMM_WORLD,&b);CHKERRQ(ierr);
ierr = VecSetSizes(b,3,PETSC_DECIDE);CHKERRQ(ierr);
ierr = VecSetFromOptions(b);CHKERRQ(ierr);
ierr = VecDuplicate(b,&x);CHKERRQ(ierr);
}
void VecSetValues(PetscInt num_rows,PetscInt* rows,PetscScalar* Value)
{
ierr = VecSetValues(b,num_rows,rows,Value,INSERT_VALUES);CHKERRQ(ierr);
}
void assemblyVectors()
{
ierr = VecAssemblyBegin(b);CHKERRQ(ierr);
ierr = VecAssemblyEnd(b);CHKERRQ(ierr);
}
void solve()
{
ierr = KSPCreate(PETSC_COMM_WORLD,&ksp);CHKERRQ(ierr);
ierr = KSPSetOperators(ksp,A,A,DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr);
ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);
ierr = PCSetType(pc,PCJACOBI);CHKERRQ(ierr);
ierr = KSPSetTolerances(ksp,1.e-5,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr);
ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr);
ierr = KSPView(ksp,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
}
void showAll()
{
//View right hand vec
PetscPrintf(PETSC_COMM_SELF,"right hand vec:\n");
ierr = VecView(b,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
//View matrix
PetscPrintf(PETSC_COMM_SELF,"matrix:\n");
ierr = MatView(A,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
//View solution
PetscPrintf(PETSC_COMM_SELF,"solution:\n");
ierr = VecView(x,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
}
void close()
{
//Clean up
ierr = VecDestroy(&b);CHKERRQ(ierr);
ierr = VecDestroy(&x);CHKERRQ(ierr);
ierr = MatDestroy(&A);CHKERRQ(ierr);
ierr = PetscFinalize();
}
};
这是调用类的脚本:
#include "externSolver.h"
int main(int argc,char **argv)
{
PetscInt l[] = {0,1,2};
PetscScalar vec[] = {0.0,5.0,3.0};
PetscScalar matrix[3][3]= {{-1.0,1.0,1.0},{1.0,-3.0,-2.0},{5.0,1.0,4.0}};
externSolver eS(argc,argv);
//Vector
eS.generateVectors();
eS.VecSetValues(3,l,vec);
eS.assemblyVectors();
//Matrix
eS.generateMatrix(3,3);
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
eS.MatSetValues(1,&i,1,&j,&matrix[i][j]);
}
}
eS.assemblyMatrix();
//Solve Problem
eS.solve();
eS.showAll();
eS.close();
return 0;
}
这些是建筑错误:
ex2_extern.cc:5中包含的文件:
externSolver.h: In constructor ‘externSolver::externSolver(int, char**)’:
externSolver.h:15: error: returning a value from a constructor
externSolver.h:16: error: returning a value from a constructor
externSolver.h:17: error: returning a value from a constructor
externSolver.h: In member function ‘void externSolver::generateMatrix(int, int)’:
externSolver.h:26: error: return-statement with a value, in function returning 'void'
externSolver.h:27: error: return-statement with a value, in function returning 'void'
externSolver.h:28: error: return-statement with a value, in function returning 'void'
externSolver.h:29: error: return-statement with a value, in function returning 'void'
externSolver.h: In member function ‘void externSolver::MatSetValues(PetscInt, PetscInt*, PetscInt, PetscInt*, PetscScalar*)’:
externSolver.h:33: error: no matching function for call to ‘externSolver::MatSetValues(_p_Mat*&, PetscInt&, PetscInt*&, PetscInt&, PetscInt*&, PetscScalar*&, InsertMode)’
externSolver.h:31: note: candidates are: void externSolver::MatSetValues(PetscInt, PetscInt*, PetscInt, PetscInt*, PetscScalar*)
externSolver.h:33: error: return-statement with a value, in function returning 'void'
externSolver.h: In member function ‘void externSolver::assemblyMatrix()’:
externSolver.h:37: error: return-statement with a value, in function returning 'void'
externSolver.h:38: error: return-statement with a value, in function returning 'void'
externSolver.h: In member function ‘void externSolver::generateVectors()’:
externSolver.h:42: error: return-statement with a value, in function returning 'void'
externSolver.h:43: error: return-statement with a value, in function returning 'void'
externSolver.h:44: error: return-statement with a value, in function returning 'void'
externSolver.h:45: error: return-statement with a value, in function returning 'void'
externSolver.h: In member function ‘void externSolver::VecSetValues(PetscInt, PetscInt*, PetscScalar*)’:
externSolver.h:49: error: no matching function for call to ‘externSolver::VecSetValues(_p_Vec*&, PetscInt&, PetscInt*&, PetscScalar*&, InsertMode)’
externSolver.h:47: note: candidates are: void externSolver::VecSetValues(PetscInt, PetscInt*, PetscScalar*)
externSolver.h:49: error: return-statement with a value, in function returning 'void'
externSolver.h: In member function ‘void externSolver::assemblyVectors()’:
externSolver.h:53: error: return-statement with a value, in function returning 'void'
externSolver.h:54: error: return-statement with a value, in function returning 'void'
externSolver.h: In member function ‘void externSolver::solve()’:
externSolver.h:58: error: return-statement with a value, in function returning 'void'
externSolver.h:59: error: return-statement with a value, in function returning 'void'
externSolver.h:60: error: return-statement with a value, in function returning 'void'
externSolver.h:61: error: return-statement with a value, in function returning 'void'
externSolver.h:62: error: return-statement with a value, in function returning 'void'
externSolver.h:63: error: return-statement with a value, in function returning 'void'
externSolver.h:64: error: return-statement with a value, in function returning 'void'
externSolver.h:65: error: return-statement with a value, in function returning 'void'
externSolver.h: In member function ‘void externSolver::showAll()’:
externSolver.h:71: error: return-statement with a value, in function returning 'void'
externSolver.h:76: error: return-statement with a value, in function returning 'void'
externSolver.h:80: error: return-statement with a value, in function returning 'void'
externSolver.h: In member function ‘void externSolver::close()’:
externSolver.h:85: error: cannot convert ‘_p_Vec**’ to ‘_p_Vec*’ for argument ‘1’ to ‘PetscErrorCode VecDestroy(_p_Vec*)’
externSolver.h:85: error: return-statement with a value, in function returning 'void'
externSolver.h:86: error: cannot convert ‘_p_Vec**’ to ‘_p_Vec*’ for argument ‘1’ to ‘PetscErrorCode VecDestroy(_p_Vec*)’
externSolver.h:86: error: return-statement with a value, in function returning 'void'
externSolver.h:87: error: cannot convert ‘_p_Mat**’ to ‘_p_Mat*’ for argument ‘1’ to ‘PetscErrorCode MatDestroy(_p_Mat*)’
externSolver.h:87: error: return-statement with a value, in function returning 'void'
ex2_extern.cc: In function ‘int main(int, char**)’:
ex2_extern.cc:14: error: ‘ierr’ was not declared in this scope
ex2_extern.cc:11: warning: unused variable ‘l’
ex2_extern.cc:12: warning: unused variable ‘vec’
ex2_extern.cc:13: warning: unused variable ‘matrix’
make: [ex2_extern.o] Error 1 (ignored)
问题出在哪里?
亲切的问候, 塞巴斯蒂安
答案 0 :(得分:2)
根据CHKERRQ
doc,你需要在函数内使用CHKERRV
返回void
或任何其他非整数数据类型。
答案 1 :(得分:0)
我只能假设您的CHKERRQ
宏扩展为类似
if (ierr != 0) return ierr;
您可以通过查看编译器消息的行来确定,然后找出该宏的作用。
我认为我不敢评论这段代码的风格。它只需要严肃的重写。使用C代码并使用类中的方法调用将其包装起来并不会使它成为C ++代码。