Have fun with sci.dog

Mac Air M1运行OpenLB

最近准备入坑OpenLB,刚好手头有一台Mac Air M1,想来用它学习下OpenLB挺不错的。在网上搜到有人成功在MAC上运行了OpenLB,于是乎,下载后,按照User Guider 运行。

下载地址:

https://www.openlb.net/download/

解压后,放置在用户文件夹

按照官方的User Guider安装编译组建

这段话,说了3个points

  • 安装Mac软件管理工具homebrew,M1芯片的和intel芯片的安装方法有区别,自行搜索即可
  • 安装gcc\make\openmpi\gnuplot等工具
  • 安装paraview

要注意,paraview要选择arm64的版本。

以上都很顺利。下边按照官方User Guider进行编译

1、复制配置文件

把config/cpu_gcc_openmpi.mk里的内容复制到config.mk

2、编译external第三方库

直接在根目标下,执行make,就可以编译第三方库。

坑就在这里出现了。

(1)clang不支持-march

clang: error: the clang compiler does not support '-march=native'

经过查询,应该配置config.mk中

CXXFLAGS        := -O3 -Wall -mcpu=apple-m1

直接指定处理器型号,具体可以查看clang支持的CPU型号

clang --print-supported-cpus
Apple clang version 14.0.0 (clang-1400.0.29.202)
Target: arm64-apple-darwin22.1.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
Available CPUs for this target:

	a64fx
	apple-a10
	apple-a11
	apple-a12
	apple-a13
	apple-a14
	apple-a7
	apple-a8
	apple-a9
	apple-latest
	apple-m1
	apple-s4
	apple-s5
	carmel
	cortex-a34
	cortex-a35
	cortex-a510
	cortex-a53
	cortex-a55
	cortex-a57
	cortex-a65
	cortex-a65ae
	cortex-a72
	cortex-a73
	cortex-a75
	cortex-a76
	cortex-a76ae
	cortex-a77
	cortex-a78
	cortex-a78c
	cortex-r82
	cortex-x1
	cyclone
	exynos-m3
	exynos-m4
	exynos-m5
	falkor
	generic
	kryo
	neoverse-e1
	neoverse-n1
	neoverse-n2
	neoverse-v1
	saphira
	thunderx
	thunderx2t99
	thunderx3t110
	thunderxt81
	thunderxt83
	thunderxt88
	tsv110

Use -mcpu or -mtune to specify the target's processor.

这里打印了clang v14支持的CPU及设定方法,用-mcpu或者-mtune设定

更改后,第一个错误搞定。再次编译,第二个错误出现了

(2)tinyxml库编译出错

./tinystr.h:82:50: error: use of undeclared identifier 'nullptr'
  TiXmlString ( const TiXmlString & copy) : rep_(nullptr)

显示tinystr.h的空指针关键词无法识别

这个错误调试了很久,其实问题不复杂。clang14肯定是支持C++17标准的,因此不可能无法识别nullptr关键字。因此,问题应该出在clang将tinystr当作C语言的风格去编译了。这说起来都是C/C++语言相互兼容留下的坑,导致以前写C++代码的人采用了C/C++混合风格。。

网上查了下tinyxml这个库,这个库是用来处理xml文件的,05年写的,太老旧了,对新版本的编译器支持不好。作者呢,已经重写了tinyxml2库,下载编译完全没有问题,可问题是openlb采用的还是05年的tinyxml。

第一个解决的思路上用tinyxml2源码替换tinyxml,结果失败,这两个库接口函数居然不同。。。

第二个思路就是看看如何让clang正确识别nullptr。其实很简单,tinyxml通过宏定义是采用C的头文件格式还是C++的头文件格式。

问题很清楚了,问题就出在tinystr.h这个头文件,这个头文件上C格式的,自然是不支持C++特性的,我们在编译器添加宏定义 TIXML_USE_STL后,直接就用C++的标准字符串库了,跳过了tinystr.h。再看看tinyxml.h第29行,已经定义了宏TIXML_USE_STL,因此,tinystr.h和tinyxml.cpp两个文件根本用不到!直接删除!OK,至此,external第三方库编译成功。

接着,去example里去编译算例吧,这就很简单了,进入直接make,成功。这里选择

/examples/laminar/cavity2d

编译后通过mpi运行,M1有8个核

mpirun -np 8 cavity2d

第3个错误来了

(3)openmpi环境变量错误

--------------------------------------------------------------------------
A system call failed during shared memory initialization that should
not have.  It is likely that your MPI job will now either abort or
experience performance degradation.

  Local host:  feifeiMac.local
  System call: unlink(2) /var/folders/mk/1tcsv31s053614ky_9_gzhxr0000gp/T//ompi.feifeiMac.502/pid.22244/1/vader_segment.feifeiMac.502.15580001.0
  Error:       No such file or directory (errno 2)
--------------------------------------------------------------------------
[feifeiMac.local:22244] 1 more process has sent help message help-opal-shmem-mmap.txt / sys call fail
[feifeiMac.local:22244] Set MCA parameter "orte_base_help_aggregate" to 0 to see all help / error messages

网上查询后,得到下边结果

https://github.com/open-mpi/ompi/issues/7393

正解在这里,大概是Mac默认的TMPDIR路径太长的问题。。

vi ~/.bash_profile        

给TMPDIR重新指定个路径

alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

export PATH=$PATH:/opt/homebrew/bin
export TMPDIR=/tmp  

更新环境变量

source ~/.bash_profile 

再次执行,OK。没问题了

结果如图所示。

赞(0)
未经允许不得转载:SciDog » Mac Air M1运行OpenLB

评论 抢沙发