Last active
June 3, 2017 01:35
-
-
Save caiofcm/84b28f70f77c6bf50f526e4810ccd5e1 to your computer and use it in GitHub Desktop.
Convert c++ file (header and source) from regular double to adouble in order to use ADOLC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # # Convertion TOOL: from double to adouble | |
| # | |
| # ## Description: | |
| # | |
| # In the header/source files mark double that should be considered as adouble as: | |
| # `double\*a*\` | |
| # The conversion tool will simple change those declaration to `adouble` | |
| # | |
| # ## Usage: | |
| # | |
| # It is a CLI tool with the commands (PYTHON 3 only): | |
| # | |
| # `python convertAdolc.py -h | |
| # | |
| # Usage: convertAdolc.py [options] | |
| # | |
| # Options: | |
| # -h, --help show this help message and exit | |
| # -f FILE, --file=FILE define file name to convert | |
| # -b, --both Convert both header/source also adjust includes | |
| # | |
| # ## Example | |
| # | |
| # Header File: `powerDouble.h` | |
| # | |
| # `double/*a*/ power(double/*a*/ x, double a, int n);` | |
| # | |
| # From command line run: `python(3) convertAdolc.py -f powerDouble.h | |
| # | |
| # IMPORTANT: only file by file converting is working (do not use -b flag!) | |
| import re | |
| import os | |
| import optparse | |
| def readData(pathfile): | |
| dir = os.path.dirname(__file__) | |
| with open (pathfile, "r") as myfile: | |
| data = myfile.read() | |
| return data | |
| return 'ERROR' | |
| def getFileNameNoExt(filename): | |
| rg = r"^(([A-Z]:)?[\.]?[\\{1,2}/]?.*[\\{1,2}/])*(.+)\.(.+)" | |
| rsltSerch = re.search(rg, filename) | |
| if rsltSerch is not None: | |
| filePath = rsltSerch.group(1) | |
| filenoext = rsltSerch.group(3) | |
| else: | |
| filenoext = os.path.basename(filename) | |
| return filenoext, filePath | |
| def convertActiveDouble(filepath, isHeader = False, isSource=False): | |
| data = readData(filepath) | |
| # Check if adol is included already: | |
| if (isHeader): | |
| rgex = r"#include\s+<adolc/adolc.h>" | |
| isInclRe = re.compile(rgex) | |
| allDeactive = isInclRe.search(data) | |
| if (allDeactive is None): | |
| includeStr = "#include <adolc/adolc.h>" | |
| data = includeStr + data | |
| if (isSource): | |
| filehead = getFileNameNoExt(filepath) + ".h" | |
| rgex = r'#include "' + filehead + '"' | |
| isInclRe = re.compile(rgex) | |
| allDeactive = isInclRe.search(data) | |
| data = isInclRe.sub(r'#include "ad_' + filehead + '"', data) | |
| # Not Activated Pattern | |
| rgex = r"(double)\s+(\w+)\s*/\*n\*/" | |
| deActRe = re.compile(rgex) | |
| allDeactive = deActRe.findall(data) | |
| # Activated Pattern | |
| rgex = r"(double)\s*(\w*)\s*/\*a\*/" | |
| actRe = re.compile(rgex) | |
| allActive = actRe.findall(data) | |
| # Substitute activated Pattern: | |
| data = actRe.sub(r"a\1\2", data) | |
| # Objects (as class or functions) to be renamed | |
| # rgex = r"/\*\s*ADCONVERT:\s(\w+);\1*" | |
| rgex = r"@AD@\s*(\w+)" | |
| objCnv = re.compile(rgex) | |
| allObjCnv = objCnv.findall(data) | |
| # srslt = objCnv.search(data) | |
| # Pattern for all of those objects: | |
| for tx in allObjCnv: | |
| rgex = r"(" + tx + r")" | |
| rc = re.compile(rgex) | |
| listrg = rc.findall(data) | |
| data = rc.sub(r"ad_\1", data) | |
| # Save to a new file | |
| print('ADOL Converting: '+os.path.basename(filepath)) | |
| with open("ad_"+ os.path.basename(filepath), "w") as text_file: | |
| print(data, file=text_file) | |
| return | |
| def convertHeaderAndSource(filename): | |
| # Get file name without the extension: | |
| filenoext, path2file = getFileNameNoExt(filename) | |
| # print(rsltSerch.group(1)) | |
| # Run for the header file ! HAS TO BE .h ONLY! | |
| convertActiveDouble(path2file + filenoext + ".h", isHeader=True) | |
| # Run for the source file ! HAS TO BE .cpp ONLY!~ | |
| convertActiveDouble(path2file + filenoext + ".cpp", isSource=True) | |
| def convert2Adol(): | |
| p = optparse.OptionParser() | |
| p.add_option('--file', '-f', help="define file name to convert") # default="" | |
| p.add_option('--both', '-b', action="store_false", help="Convert both header/source also adjust includes") | |
| options, arguments = p.parse_args() | |
| if options.both is None: | |
| convertActiveDouble(options.file) | |
| else: | |
| convertHeaderAndSource(options.file) | |
| if __name__ == '__main__': | |
| convert2Adol() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment