3

我已经使用工具将大约 90 多个 fortran 文件转换为 C 文件,我需要验证转换是否良好。

你能给我一些关于如何最好地确保通过翻译保留功能的想法吗?

4

3 回答 3

3

您需要执行这些 fortran 功能的验证测试。然后您针对 c 代码运行这些测试。

您可以使用单元测试技术/方法。事实上,我看不出你还能如何证明转换是正确的。

在许多单元测试方法中,您会使用与代码相同的语言编写测试,但在这种情况下,我非常强烈建议选择一种语言和一个代码库来执行两组功能。也不必担心尝试创建纯单元测试,而是使用这些技术来覆盖 fortran 代码应该处理的所有用途。

于 2012-07-04T11:28:28.390 回答
2

使用单元测试。

首先在 Fortran 代码上编写单元测试并检查它们是否都正确运行,然后用 C 重写它们并运行它们。

这种方法的问题是您还需要重写单元测试,而重构代码时通常不会这样做(API 更改除外)。这意味着除了实际代码之外,您最终也可能会调试移植的单元测试代码。

因此,编写包含最少逻辑的测试代码并且只将函数的结果写入文件可能会更好。然后你可以用 C 重写这个最小的测试代码,生成相同的文件并比较这些文件。

于 2012-07-04T11:29:15.663 回答
1

这是我为“类似”任务所做的(将 fortran 90 与 fortran 90 + OpenACC GPU 加速代码进行比较):

  1. 分析每个 Fortran 模块的输出。
  2. 将这些输出数组写入 .dat 文件。
  3. 将 .dat 文件复制到参考文件夹中。
  4. 将转换后的模块的输出写入文件(CSV 或二进制文件)。为方便起见,使用相同的文件名。
  5. 制作一个比较两个版本的python脚本。

我在 fortran 中使用了这样的便利函数(类似于 1D、2D 案例):

subroutine write3DToFile(path, array, n1, n2, n3)
   use pp_vardef
   use pp_service, only: find_new_mt
   implicit none

   !input arguments
   real(kind = r_size), intent(in) :: array(n1,n2,n3)
   character(len=*), intent(in) :: path
   integer(4) :: n1
   integer(4) :: n2
   integer(4) :: n3

   !temporary
   integer(4) :: imt

   call find_new_mt(imt)

   open(imt, file = path, form = 'unformatted', status = 'replace')
   write(imt) array
   close(imt)
 end subroutine write3DToFile

在 python 中,我使用以下脚本来读取二进制 Fortran 数据并进行比较。注意:由于您要转换为 C,您必须对其进行调整,以便您可以读取由 C 而不是 Fortran 生成的数据。

from optparse import OptionParser
import struct
import sys
import math

def unpackNextRecord(file, readEndianFormat, numOfBytesPerValue):
    header = file.read(4)
    if (len(header) != 4):
        #we have reached the end of the file
        return None

    headerFormat = '%si' %(readEndianFormat)
    headerUnpacked = struct.unpack(headerFormat, header)
    recordByteLength = headerUnpacked[0]
    if (recordByteLength % numOfBytesPerValue != 0):
        raise Exception, "Odd record length."
        return None
    recordLength = recordByteLength / numOfBytesPerValue

    data = file.read(recordByteLength)
    if (len(data) != recordByteLength):
        raise Exception, "Could not read %i bytes as expected. Only %i bytes read." %(recordByteLength, len(data))
        return None

    trailer = file.read(4)
    if (len(trailer) != 4):
        raise Exception, "Could not read trailer."
        return None
    trailerUnpacked = struct.unpack(headerFormat, trailer)
    redundantRecordLength = trailerUnpacked[0]
    if (recordByteLength != redundantRecordLength):
        raise Exception, "Header and trailer do not match."
        return None

    dataFormat = '%s%i%s' %(readEndianFormat, recordLength, typeSpecifier)
    return struct.unpack(dataFormat, data)

def rootMeanSquareDeviation(tup, tupRef):
    err = 0.0
    i = 0
    for val in tup:
        err = err + (val - tupRef[i])**2
        i = i + 1
    return math.sqrt(err)

##################### MAIN ##############################
#get all program arguments
parser = OptionParser()
parser.add_option("-f", "--file", dest="inFile",
                  help="read from FILE", metavar="FILE", default="in.dat")
parser.add_option("--reference", dest="refFile",
                  help="reference FILE", metavar="FILE", default="ref.dat")
parser.add_option("-b", "--bytesPerValue", dest="bytes", default="4")
parser.add_option("-r", "--readEndian", dest="readEndian", default="big")
parser.add_option("-v", action="store_true", dest="verbose")

(options, args) = parser.parse_args()

numOfBytesPerValue = int(options.bytes)
if (numOfBytesPerValue != 4 and numOfBytesPerValue != 8):
    print "Unsupported number of bytes per value specified."
    sys.exit()
typeSpecifier = 'f'
if (numOfBytesPerValue == 8):
    typeSpecifier = 'd'

readEndianFormat = '>'
if (options.readEndian == "little"):
    readEndianFormat = '<'

inFile = None
refFile = None
try: 
    #prepare files
    inFile = open(str(options.inFile),'r')
    refFile = open(str(options.refFile),'r')

    i = 0
    while True:
        passedStr = "pass"
        i = i + 1
        unpackedRef = None
        try: 
            unpackedRef = unpackNextRecord(refFile, readEndianFormat, numOfBytesPerValue)
        except(Exception), e:
            print "Error reading record %i from %s: %s" %(i, str(options.refFile), e)
            sys.exit()

        if (unpackedRef == None):
            break;

        unpacked = None
        try:
            unpacked = unpackNextRecord(inFile, readEndianFormat, numOfBytesPerValue)
        except(Exception), e:
            print "Error reading record %i from %s: %s" %(i, str(options.inFile), e)
            sys.exit()

        if (unpacked == None):
            print "Error in %s: Record expected, could not load record it" %(str(options.inFile))
            sys.exit()

        if (len(unpacked) != len(unpackedRef)):
            print "Error in %s: Record %i does not have same length as reference" %(str(options.inFile), i)
            sys.exit()

        #analyse unpacked data
        err = rootMeanSquareDeviation(unpacked, unpackedRef)
        if (abs(err) > 1E-08):
            passedStr = "FAIL <-------"
        print "%s, record %i: Mean square error: %e; %s" %(options.inFile, i, err, passedStr)

        if (options.verbose):
            print unpacked

except(Exception), e:
    print "Error: %s" %(e)

finally:
    #cleanup
    if inFile != None:
        inFile.close()

    if refFile != None:
        refFile.close()
于 2012-07-09T06:28:40.117 回答