如何修复Gem5环境下LAPACK的Fortran文本解析错误?
LAPACK在Gem5仿真中输入解析错误的修复方案
问题背景
在Windows 10的WSL2 Docker环境中执行LAPACK的xlintstrfz二进制并通过< ztest_rfp.in重定向输入时,程序能正常完成测试;但将配置迁移到Gem5的Python脚本后,程序读取NRHS参数时触发Fortran运行时错误:
Fortran runtime error: Bad integer for item 3 in list input
错误位于zchkrfp.f第166行的READ( NIN, FMT = * )( NSVAL( I ), I = 1, NNS )语句,仿真仅能处理输入文件前几行就崩溃。
相关信息
Docker中正常运行命令及输出
./LIN/xlintstrfz < ztest_rfp.in
输出:
Tests of the COMPLEX*16 LAPACK RFP routines LAPACK VERSION 3.*.0 The following parameter values will be used: N : 0 1 2 3 5 6 10 11 50 NRHS: 1 2 15 TYPE: 1 2 3 4 5 6 7 8 9 Routines pass computational tests if test ratio is less than 30.00 Relative machine underflow is taken to be 0.222507-307 Relative machine overflow is taken to be 0.179769+309 Relative machine precision is taken to be 0.111022D-15 COMPLEX*16 RFP routines passed the tests of the error exits All tests for ZPF drivers passed the threshold ( 2304 tests run) All tests for ZLANHF auxiliary routine passed the threshold ( 384 tests run) All tests for the RFP conversion routines passed ( 72 tests run) All tests for ZTFSM auxiliary routine passed the threshold ( 7776 tests run) All tests for ZHFRK auxiliary routine passed the threshold ( 2592 tests run) End of tests Total time used = 0.71 seconds
Gem5的Python配置代码
binary = os.path.join( thispath, "../../../", "tests/test-progs/lapack/TESTING/LIN/xlintstrfz", ) process = Process() process.cmd = [binary] process.input = 'tests/test-progs/lapack/TESTING/ztest_rfp.in'
Gem5错误输出
gem5 Simulator System. https://www.gem5.org gem5 is copyrighted software; use the --copyright option for details. gem5 version 22.1.0.0 gem5 compiled May 18 2023 15:42:00 gem5 started May 24 2023 00:42:19 gem5 executing on 8fa8d1b910d8, pid 26054 command line: build/GCN3_X86/gem5.opt configs/learning_gem5/part1/simple.py Global frequency set at 1000000000000 ticks per second build/GCN3_X86/base/statistics.hh:280: warn: One of the stats is a legacy stat. Legacy stat is deprecated. 0: system.remote_gdb: listening for remote gdb on port 7008 Beginning simulation! build/GCN3_X86/sim/simulate.cc:192: info: Entering event queue @ 0. Starting simulation... Tests of the COMPLEX*16 LAPACK RFP routines LAPACK VERSION 3.*.0 The following parameter values will be used: N : 0 1 2 3 5 6 10 11 50 build/GCN3_X86/sim/mem_state.cc:443: info: Increasing stack size by one page. At line 166 of file zchkrfp.f (unit = 5, file = 'stdin') Fortran runtime error: Bad integer for item 3 in list input Error termination.
关键Fortran代码片段(zchkrfp.f第153行起)
* * Read the values of NRHS * READ( NIN, FMT = * )NNS IF( NNS.LT.1 ) THEN WRITE( NOUT, FMT = 9996 )' NNS', NNS, 1 NNS = 0 FATAL = .TRUE. ELSE IF( NNS.GT.MAXIN ) THEN WRITE( NOUT, FMT = 9995 )' NNS', NNS, MAXIN NNS = 0 FATAL = .TRUE. END IF READ( NIN, FMT = * )( NSVAL( I ), I = 1, NNS ) DO 30 I = 1, NNS IF( NSVAL( I ).LT.0 ) THEN WRITE( NOUT, FMT = 9996 )'NRHS', NSVAL( I ), 0 FATAL = .TRUE. ELSE IF( NSVAL( I ).GT.MAXRHS ) THEN WRITE( NOUT, FMT = 9995 )'NRHS', NSVAL( I ), MAXRHS FATAL = .TRUE. END IF 30 CONTINUE IF( NNS.GT.0 ) $ WRITE( NOUT, FMT = 9993 )'NRHS', ( NSVAL( I ), I = 1, NNS ) *
输入文件ztest_rfp.in内容
Data file for testing COMPLEX*16 LAPACK linear equation routines RFP format 9 Number of values of N (at most 9) 0 1 2 3 5 6 10 11 50 Values of N 3 Number of values of NRHS (at most 9) 1 2 15 Values of NRHS (number of right hand sides) 9 Number of matrix types (list types on next line if 0 < NTYPES < 9) 1 2 3 4 5 6 7 8 9 Matrix Types 30.0 Threshold value of test ratio T Put T to test the error exits
问题分析
核心原因是Gem5的process.input机制与shell的输入重定向<处理逻辑不同:
- Shell重定向是将文件作为标准输入流式传递,Fortran的自由格式读取
FMT=*能按预期识别换行和空白分隔的数值; - Gem5的
process.input直接传递文件内容时,可能存在行尾格式(如CRLF转LF不彻底)、缓冲区读取方式差异,导致Fortran读取到的内容不是预期的结构化数值流,进而在解析NRHS的第三个数值时出错。
修复方案
方案1:通过Shell命令执行(推荐)
修改Gem5的Python配置,让shell处理输入重定向,和Docker中的执行逻辑完全一致:
import os binary = os.path.join( thispath, "../../../", "tests/test-progs/lapack/TESTING/LIN/xlintstrfz", ) input_file = os.path.join( thispath, "../../../", "tests/test-progs/lapack/TESTING/ztest_rfp.in" ) process = Process() # 用bash -c执行带重定向的命令 process.cmd = ['bash', '-c', f'{binary} < {input_file}']
这种方式完全复用shell的输入处理逻辑,避免Gem5输入机制带来的兼容性问题。
方案2:修正输入文件换行格式
如果输入文件是Windows CRLF换行格式,WSL2和Gem5可能对换行解析不一致,导致Fortran读取时混入\r字符干扰数值解析。执行以下命令转换为Unix LF格式:
dos2unix tests/test-progs/lapack/TESTING/ztest_rfp.in
方案3:修改Fortran代码强制逐行解析
修改zchkrfp.f中的读取逻辑,先逐行读取字符串再解析数值,避免流式读取的格式问题:
* * Read the values of NRHS * CHARACTER*256 line READ( NIN, '(A)' ) line READ( line, * )NNS IF( NNS.LT.1 ) THEN WRITE( NOUT, FMT = 9996 )' NNS', NNS, 1 NNS = 0 FATAL = .TRUE. ELSE IF( NNS.GT.MAXIN ) THEN WRITE( NOUT, FMT = 9995 )' NNS', NNS, MAXIN NNS = 0 FATAL = .TRUE. END IF READ( NIN, '(A)' ) line READ( line, * )( NSVAL( I ), I = 1, NNS ) DO 30 I = 1, NNS IF( NSVAL( I ).LT.0 ) THEN WRITE( NOUT, FMT = 9996 )'NRHS', NSVAL( I ), 0 FATAL = .TRUE. ELSE IF( NSVAL( I ).GT.MAXRHS ) THEN WRITE( NOUT, FMT = 9995 )'NRHS', NSVAL( I ), MAXRHS FATAL = .TRUE. END IF 30 CONTINUE IF( NNS.GT.0 ) $ WRITE( NOUT, FMT = 9993 )'NRHS', ( NSVAL( I ), I = 1, NNS ) *
修改后重新编译LAPACK测试程序,再在Gem5中运行。
内容的提问来源于stack exchange,提问作者Nick Felker
相关产品推荐
相关产品推荐

