handle_h_cu_file.py 5.0 KB
Newer Older
Z
zhangchunle 已提交
1
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
2
#
Z
zhangchunle 已提交
3 4 5
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
6
#
Z
zhangchunle 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
8
#
Z
zhangchunle 已提交
9 10 11 12 13 14
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

15
import os
Z
zhangchunle 已提交
16
import queue
17
import sys
Z
zhangchunle 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
import threading
import time

taskQueue = queue.Queue()


def worker(fun):
    while True:
        temp = taskQueue.get()
        fun(temp)
        taskQueue.task_done()


def threadPool(threadPoolNum):
    threadPool = []
    for i in range(threadPoolNum):
34 35 36 37 38 39
        thread = threading.Thread(
            target=worker,
            args={
                doFun,
            },
        )
Z
zhangchunle 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
        thread.daemon = True
        threadPool.append(thread)
    return threadPool


def get_h_file_md5(rootPath):
    h_cu_files = '%s/tools/h_cu_files.log' % rootPath
    f = open(h_cu_files)
    lines = f.readlines()
    for line in lines:
        line = line.strip()
        os.system('md5sum %s >> %s/tools/h_cu_md5.log' % (line, rootPath))


def insert_pile_to_h_file(rootPath):
    h_cu_files = '%s/tools/h_cu_files.log' % rootPath
    f = open(h_cu_files)
    lines = f.readlines()
    for line in lines:
        line = line.strip()
        func = line.replace('/', '_').replace('.', '_')
        os.system('echo "\n#ifndef _PRECISE%s_\n" >> %s' % (func.upper(), line))
        os.system('echo "#define _PRECISE%s_" >> %s' % (func.upper(), line))
        os.system('echo "\n#include <cstdio>\n" >> %s' % line)
        os.system(
            'echo "__attribute__((constructor)) static void calledFirst%s()\n{" >> %s'
66 67
            % (func, line)
        )
Z
zhangchunle 已提交
68
        os.system(
R
risemeup1 已提交
69
            'echo \'    fprintf(stderr,"precise test map fileeee: %%s\\\\n", __FILE__);\n}\' >> %s'
70 71
            % line
        )
Z
zhangchunle 已提交
72 73 74
        os.system('echo "\n#endif" >> %s' % line)


75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
def add_simple_cxx_test(rootPath):
    variant_test_path = '%s/paddle/utils/variant_test.cc' % rootPath
    variant_test_cmakeflie_path = '%s/paddle/utils/CMakeLists.txt' % rootPath
    if os.path.exists(variant_test_path) and os.path.exists(
        variant_test_cmakeflie_path
    ):
        simple_test_path = '%s/paddle/utils/simple_precision_test.cc' % rootPath
        os.system('touch %s' % simple_test_path)
        os.system(
            "echo '#include \"gtest/gtest.h\"\n' >> %s" % simple_test_path
        )
        os.system(
            'echo "TEST(interface_test, type) { }\n" >> %s' % simple_test_path
        )
        os.system('echo "cc_test(" >> %s' % variant_test_cmakeflie_path)
        os.system(
            'echo "  simple_precision_test" >> %s' % variant_test_cmakeflie_path
        )
        os.system(
            'echo "  SRCS simple_precision_test.cc" >> %s'
            % variant_test_cmakeflie_path
        )
        os.system('echo "  DEPS gtest)\n" >> %s' % variant_test_cmakeflie_path)


Z
zhangchunle 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112
def remove_pile_from_h_file(rootPath):
    h_cu_files = '%s/tools/h_cu_files.log' % rootPath
    f = open(h_cu_files)
    lines = f.readlines()
    count = 12
    for line in lines:
        line = line.strip()
        while count > 0:
            os.system("sed -i '$d' %s" % line)
            count = count - 1
        count = 12


Z
zhangchunle 已提交
113 114 115 116 117
def get_h_cu_file(file_path):
    rootPath = file_path[0]
    dir_path = file_path[1]
    filename = file_path[2]
    ut = filename.replace('^', '').replace('$', '').replace('.log', '')
118 119 120 121 122 123 124 125
    ut_path = "%s/build/ut_map/%s" % (rootPath, ut)
    if os.path.exists(ut_path):
        os.system(
            "cat %s/%s | grep 'precise test map fileeee:'| uniq >> %s/build/ut_map/%s/related_%s.txt"
            % (dir_path, filename, rootPath, ut, ut)
        )
    else:
        print("%s has failed,no has direcotory" % ut)
Z
zhangchunle 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157


def doFun(file_path):
    get_h_cu_file(file_path)


def main(rootPath, dir_path):
    """
    get useful message
    """
    startTime = int(time.time())
    test_h_cu_dict = {}
    pool = threadPool(23)
    for i in range(pool.__len__()):
        pool[i].start()
    files = os.listdir(dir_path)
    for filename in files:
        file_path = [rootPath, dir_path, filename]
        taskQueue.put(file_path)
    taskQueue.join()
    endTime = int(time.time())
    print('analy h/cu file cost Time: %s' % (endTime - startTime))


if __name__ == "__main__":
    func = sys.argv[1]
    if func == 'get_h_file_md5':
        rootPath = sys.argv[2]
        get_h_file_md5(rootPath)
    elif func == 'insert_pile_to_h_file':
        rootPath = sys.argv[2]
        insert_pile_to_h_file(rootPath)
158
        add_simple_cxx_test(rootPath)
Z
zhangchunle 已提交
159 160 161 162
    elif func == 'analy_h_cu_file':
        dir_path = sys.argv[2]
        rootPath = sys.argv[3]
        main(rootPath, dir_path)
Z
zhangchunle 已提交
163 164 165
    elif func == 'remove_pile_from_h_file':
        rootPath = sys.argv[2]
        remove_pile_from_h_file(rootPath)