gpt4 book ai didi

c - 使用 CMake 时链接 PETSc 程序时出现的问题

转载 作者:行者123 更新时间:2023-12-04 04:40:03 28 4
gpt4 key购买 nike

我在 Ubuntu 13.04 64 位上,我正在尝试使用 CMake 在 PETSc 中构建一个“Hello world”程序。

我有以下程序 solve1.c (松散地基于 PETSc 示例中的 ex1.c),它解决了一个简单的线性方程组:

/* minimal example of solving a linear equation system in petsc
(in serial) */
/* based on http://www.mcs.anl.gov/petsc/petsc-current/src/ksp/ksp/examples/tutorials/ex1.c.html */
#include <petscksp.h>
#define SIZE 3
int main(int argc,char **argv) {
Vec x,b; /* approx solution, rhs */
Mat A; /* linear system matrix */
KSP ksp; /* linear solver context */
PC pc; /* preconditioner context */
int size;
PetscInt col[3],i;
PetscScalar temp[SIZE];

PetscInitialize(&argc,&argv,PETSC_NULL,PETSC_NULL);
MPI_Comm_size(PETSC_COMM_WORLD,&size);
if(size!=1) PetscPrintf(PETSC_COMM_WORLD,"warning, %d threads\n",size);
/* create vectors */
VecCreate(PETSC_COMM_WORLD,&x);
VecSetSizes(x,PETSC_DECIDE,SIZE);
VecSetFromOptions(x);
VecDuplicate(x,&b);
temp[0]=14.5; temp[1]=54; temp[2]=0.423;
for(i=0;i<3;i++) VecSetValues(b,1,&i,&temp[i],INSERT_VALUES);
/* need to assemble after setting values! do necessary
message passing etc to propagate matrix to all ranks */
VecAssemblyBegin(b);
VecAssemblyEnd(b);
/* create matrix */
MatCreate(PETSC_COMM_WORLD,&A);
MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,SIZE,SIZE);
MatSetFromOptions(A);
MatSetUp(A);
temp[0]=1; temp[1]=1; temp[2]=1;
col[0]=0; col[1]=1; col[2]=2;
for(i=0;i<3;i++) {
MatSetValues(A,1,&i,3,col,temp,INSERT_VALUES);
temp[i]=0;
}
/* need to assemble matrix for the same reasons as above */
MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
/* linear solver context! */
KSPCreate(PETSC_COMM_WORLD,&ksp);
/* operator is A matrix, also set matrix for preconditioning here */
KSPSetOperators(ksp,A,A,DIFFERENT_NONZERO_PATTERN);
/* get pc context from ksp context */
KSPGetPC(ksp,&pc);
/* set preconditioner type */
PCSetType(pc,PCJACOBI);
KSPSetTolerances(ksp,1e-6,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);
KSPSetFromOptions(ksp);
/* solve! */
KSPSolve(ksp,b,x);
/* display everything */
MatView(A,PETSC_VIEWER_STDOUT_WORLD);
VecView(b,PETSC_VIEWER_STDOUT_WORLD);
VecView(x,PETSC_VIEWER_STDOUT_WORLD);
KSPView(ksp,PETSC_VIEWER_STDOUT_WORLD);
/* get rid of everything */
KSPDestroy(&ksp);
VecDestroy(&x);
VecDestroy(&b);
PetscFinalize();
return 0;
}

这是我尝试创建 CMakeLists.txt (基于网络上的各种 PETSc 教程和项目):
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)

PROJECT(helloworld)

SET(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake-modules")

FIND_PACKAGE(PETSc REQUIRED)

INCLUDE_DIRECTORIES(${PETSC_INCLUDES})
ADD_DEFINITIONS(${PETSC_DEFINITIONS})

ADD_EXECUTABLE(solve1 solve1.c)

TARGET_LINK_LIBRARIES(solve1 ${PETSC_LIBRARIES})

另外,我有一个目录 cmake-modules我有文件的地方 CorrectWindowsPaths.cmake , FindPETSc.cmake , FindPackageMultipass.cmakeResolveCompilerPaths.cmake我从 https://github.com/jedbrown/cmake-modules 得到的(按照 PETSc FAQ 的推荐)。

我正在以这种方式构建项目(最初站在我项目的根目录中,即 /home/ruben/hello ):
mkdir build
cd build
cmake ..
make

不幸的是,make 命令因链接器错误而失败。这是 CMake 的输出(看起来不错):
-- The C compiler identification is GNU 4.8.1
-- The CXX compiler identification is GNU 4.8.1
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- petsc_lib_dir /home/ruben/petsc-3.4.2/arch-linux2-c-debug/lib
-- Recognized PETSc install with single library for all packages
-- Performing Test MULTIPASS_TEST_1_petsc_works_minimal
-- Performing Test MULTIPASS_TEST_1_petsc_works_minimal - Success
-- Minimal PETSc includes and libraries work. This probably means we are building with shared libs.
-- Found PETSc: /home/ruben/petsc-3.4.2/arch-linux2-c-debug/include;/home/ruben/petsc-3.4.2/include
-- Configuring done
-- Generating done
-- Build files have been written to: /home/ruben/hello/build

这是 make VERBOSE=1 的输出(这不好):
/usr/bin/cmake -H/home/ruben/hello -B/home/ruben/hello/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/ruben/hello/build/CMakeFiles /home/ruben/hello/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory `/home/ruben/hello/build'
make -f CMakeFiles/solve1.dir/build.make CMakeFiles/solve1.dir/depend
make[2]: Entering directory `/home/ruben/hello/build'
cd /home/ruben/hello/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/ruben/hello /home/ruben/hello /home/ruben/hello/build /home/ruben/hello/build /home/ruben/hello/build/CMakeFiles/solve1.dir/DependInfo.cmake --color=
Dependee "/home/ruben/hello/build/CMakeFiles/solve1.dir/DependInfo.cmake" is newer than depender "/home/ruben/hello/build/CMakeFiles/solve1.dir/depend.internal".
Dependee "/home/ruben/hello/build/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/home/ruben/hello/build/CMakeFiles/solve1.dir/depend.internal".
Scanning dependencies of target solve1
make[2]: Leaving directory `/home/ruben/hello/build'
make -f CMakeFiles/solve1.dir/build.make CMakeFiles/solve1.dir/build
make[2]: Entering directory `/home/ruben/hello/build'
/usr/bin/cmake -E cmake_progress_report /home/ruben/hello/build/CMakeFiles 1
[100%] Building C object CMakeFiles/solve1.dir/solve1.c.o
/usr/bin/cc -D__INSDIR__="" -I/home/ruben/petsc-3.4.2/arch-linux2-c-debug/include -I/home/ruben/petsc-3.4.2/include -o CMakeFiles/solve1.dir/solve1.c.o -c /home/ruben/hello/solve1.c
Linking C executable solve1
/usr/bin/cmake -E cmake_link_script CMakeFiles/solve1.dir/link.txt --verbose=1
/usr/bin/cc CMakeFiles/solve1.dir/solve1.c.o -o solve1 -rdynamic /home/ruben/petsc-3.4.2/arch-linux2-c-debug/lib/libpetsc.so -Wl,-rpath,/home/ruben/petsc-3.4.2/arch-linux2-c-debug/lib
/usr/bin/ld: CMakeFiles/solve1.dir/solve1.c.o: undefined reference to symbol 'MPI_Comm_size'
/usr/bin/ld: note: 'MPI_Comm_size' is defined in DSO /home/ruben/petsc-3.4.2/arch-linux2-c-debug/lib/libmpich.so.10 so try adding it to the linker command line
/home/ruben/petsc-3.4.2/arch-linux2-c-debug/lib/libmpich.so.10: could not read symbols: Invalid operation
collect2: error: ld returned 1 exit status
make[2]: *** [solve1] Error 1
make[2]: Leaving directory `/home/ruben/hello/build'
make[1]: *** [CMakeFiles/solve1.dir/all] Error 2
make[1]: Leaving directory `/home/ruben/hello/build'
make: *** [all] Error 2

PETSc FAQ建议看 CMakeLists.txt来自 Dohp项目。不幸的是,我发现这个 CMake 文件很难理解(Dohp 不是一个特别小的例子,它除了 PETSc 之外还有其他依赖项),而且我无法使用这个文件来修复我的链接器错误。

我的问题 : 我的 CMakeLists.txt怎么了我该如何解决? (或者,如果我的 CMake 文件是正确的:我还犯了什么其他错误?)

一些附加信息:我正在使用 PETSc 3.4.2(安装在 /home/ruben/petsc-3.4.2/ 中,与我想使用的 MPI 一起安装)、CMake 2.8.10.1、gcc 4.8.1,全部在 Intel i7 上的 Ubuntu 13.04 64 位-3930k。我能够使用常规的 makefile 编译和运行上述程序,并且我能够编译和运行我迄今为止尝试过的所有 PETSc 示例。

最佳答案

错误的原因是调用了错误的编译器。从make输出(开启VERBOSE)可以看出/usr/bin/cc被调用而不是支持 MPI 的编译器 (mpicc)

解决此问题的一种方法是告诉 CMake 使用哪个编译器。更新后的构建说明如下:

mkdir build
cd build
cmake -D CMAKE_C_COMPILER=${PETSC_DIR}/${PETSC_ARCH}/bin/mpicc ..
make

(如果这是一个 C++ 项目,则要设置的正确 CMake 变量将是 CMAKE_CXX_COMPILER。)

对于同一构建树中的后续构建,上面的 cmake 行可以替换为 cmake .. ,因为编译器路径现在存储在此构建树的配置文件中。

关于c - 使用 CMake 时链接 PETSc 程序时出现的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19023664/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com