【发布时间】:2020-04-27 17:31:52
【问题描述】:
考虑以下简单的 openMP 程序:
program hello
use OMP_LIB
implicit none
integer :: nthreads, tid
!$OMP PARALLEL PRIVATE(nthreads, tid)
tid = omp_get_thread_num()
nthreads = omp_get_num_threads()
! print *, "hello"
print *,"Thread:", tid, "of", nthreads
!$OMP END PARALLEL
end program hello
当我编译它时
gfortran -fopenmp hello.90 -o hello.x
我得到的输出为:
主题:2 / 4
主题:1 of 4
主题:4 个中的 0 个
主题:3 / 4
正如预期的那样。
现在如果我考虑以下CMakeLists.txt 文件
cmake_minimum_required(VERSION 2.8.9)
project (hello Fortran)
enable_language(Fortran)
IF(NOT CMAKE_BUILD_TYPE)
message("NO BUILD TYPE PROVIDED")
message("DEFAULT BUILD TYPE SET TO 'Release'")
set(CMAKE_BUILD_TYPE 'Release')
ENDIF()
IF(${CMAKE_BUILD_TYPE} MATCHES RELEASE OR ${CMAKE_BUILD_TYPE} MATCHES Release)
message("MAKING RELEASE BUILD")
set(CMAKE_Fortran_FLAGS_RELEASE "-fopenmp")
message("FLAGS: ${CMAKE_Fortran_FLAGS_RELEASE}")
ENDIF()
add_executable(hello.x hello.f90)
考虑到上面的文件是正确的,它应该添加 omp 标志来制作文件,因此编译和运行相同,但是
$ mkdir build; cd build
$ cmake ..
-- The Fortran compiler identification is GNU 6.1.0
-- Checking whether Fortran compiler has -isysroot
-- Checking whether Fortran compiler has -isysroot - yes
-- Checking whether Fortran compiler supports OSX deployment target flag
-- Checking whether Fortran compiler supports OSX deployment target flag - yes
-- Check for working Fortran compiler: /usr/local/bin/gfortran
-- Check for working Fortran compiler: /usr/local/bin/gfortran - works
-- Detecting Fortran compiler ABI info
-- Detecting Fortran compiler ABI info - done
-- Checking whether /usr/local/bin/gfortran supports Fortran 90
-- Checking whether /usr/local/bin/gfortran supports Fortran 90 - yes
NO BUILD TYPE PROVIDED
DEFAULT BUILD TYPE SET TO 'Release'
MAKING RELEASE BUILD
FLAGS: -fopenmp
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/user/Programs/omp/build
$
$ make
Scanning dependencies of target hello.x
[ 50%] Building Fortran object CMakeFiles/hello.x.dir/hello.f90.o
[100%] Linking Fortran executable hello.x
Undefined symbols for architecture x86_64:
"_omp_get_num_threads_", referenced from:
_MAIN__ in hello.f90.o
"_omp_get_thread_num_", referenced from:
_MAIN__ in hello.f90.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
make[2]: *** [hello.x] Error 1
make[1]: *** [CMakeFiles/hello.x.dir/all] Error 2
这与缺少-fopenmp 标志相同。如何添加?
【问题讨论】:
-
请不要链接到随时可能无法访问的外部文件。在此处复制并粘贴内容。它甚至没有那么长。只需复制和粘贴即可。
-
请注意,
cmake -LA仅显示 CACHE 的变量。您设置了“正常”变量CMAKE_Fortran_FLAGS_RELEASE,因此它不会存储在缓存中。但是,这样的变量设置应该可以工作......只要您正确设置它。你展示了这么少的代码,所以很难说什么是错的。请创建minimal reproducible example 并将其添加到问题帖子(不是链接)。