CUDA program transplantation method, device, electronic device and storage medium
By analyzing the compilation and build process information of CUDA programs, CUDA code was gradually converted into HIP code, and the compilation and build rules were modified, solving the problem of porting CUDA programs on AMD GPUs and achieving efficient and accurate cross-platform compatibility.
Patent Information
- Application Number
- CN202110222487.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-02-25
- Publication Date
- 2025-01-14
- Estimated Expiration
- 2041-02-25
AI Technical Summary
In existing technologies, the lack of a holistic approach for migrating CUDA programs from NVIDIA GPUs to AMD GPUs makes porting large applications difficult, and the fragmented tools result in numerous errors in compilation and build tools, affecting the accuracy and efficiency of the porting process.
By analyzing the compilation and build process information of CUDA programs, the CUDA code in the code files is gradually converted into HIP code, and the compilation and build rules are modified to ensure cross-platform compatibility. Debugging and verification are carried out step by step to reduce the amount of changes required per operation.
It improves the efficiency and accuracy of porting CUDA programs to the ROCm platform, reduces the complexity of compilation errors, and achieves compatibility with NVIDIA and AMD GPUs.
Smart Images

Figure CN112882751B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of code transplantation, and in particular to a CUDA program transplantation method, device, electronic device and storage medium. Background Art
[0002] Since the launch of the General Purpose Graphic Process Unit (GPGPU) and its supporting driver and development tool CUDA (Compute Unified Device Architecture), GPU (Graphics Processing Unit) has been widely used in the field of general computing. In recent years, AMD (Advanced Micro Devices) has launched a number of GPU accelerator cards for general computing. However, programs written in CUDA can only run on NVIDIA's GPU accelerator cards, so a large number of existing CUDA programs cannot run on AMD's GPU cards, and many existing programs have compatibility issues between NVIDIA GPU and AMD GPU.
[0003] AMD also provides supporting drivers and development tools, namely ROCm (Radeon Open Compute platform). To facilitate the porting of existing CUDA programs to ROCm, AMD also provides the HIP transcoding tool. HIP is an encapsulation layer located on the upper layer of the ROCm software stack, providing a unified API (Application Programming Interface) to achieve compatibility with CUDA and ROCm. The HIP API is very similar to the CUDA API. By converting the CUDA API to the HIP API, the same set of code can be compatible with the two major mainstream GPUs of NVIDIA and AMD.
[0004] However, the existing transplantation technology is to transcode the code files in the program separately through scattered tools with specific functions, and there is no overall method, especially for large applications. Large applications generally have complex compilation and construction rules, which do not belong to the CUDA platform or ROCm platform itself. Therefore, the existing automatic transcoding tools cannot automatically transcode all the codes of large applications efficiently and accurately due to the scattered and messy tool functions. If there is no reasonable method, directly converting all CUDA-related codes of large applications into HIP codes through existing automatic transcoding tools will often encounter many problems such as compilation and construction tool errors, involving various links such as compilation, linking, and running, which are difficult to locate and troubleshoot, affecting the accuracy and efficiency of transplantation, so transplantation is very difficult. Summary of the invention
[0005] In view of this, the purpose of the embodiments of the present application is to provide a CUDA program porting method, device, electronic device and storage medium to improve the problem in the prior art of converting all CUDA related codes of a large application program into HIP code in CUDA program code porting that the tools and steps are complicated and scattered, affecting the accuracy and efficiency of porting and making porting difficult.
[0006] An embodiment of the present application provides a CUDA program migration method, the method comprising: obtaining a code file and a compilation and construction rule of a CUDA program to be migrated from a CUDA platform; determining the compilation and construction process information of the CUDA program; converting the CUDA-related code in the file to be transcoded in the code file into a HIP code to obtain a transcoded file; modifying the compilation and construction rule corresponding to the transcoded file based on the compilation and construction process information, and using the transcoded file and the modified compilation and construction rule as program source code; compiling the program source code based on the compilation and construction rule, and migrating the compiled and correctly running program source code to the ROCm platform.
[0007] In the above implementation, the compatibility of HIP with CUDA and ROCm platforms is utilized to port the CUDA program into a cross-platform program compatible with both NVIDIA and AMD GPUs. In addition to HIP transcoding of CUDA code, the compilation build information is also analyzed and modified. HIP code conversion can be performed on CUDA-related codes of large applications, thereby improving the efficiency of CUDA program porting.
[0008] Optionally, determining the compilation and construction process information of the CUDA program includes: determining each code module in the code file based on the logical structure of the CUDA code; determining a first correspondence between each code module and the compilation and construction tool based on the compilation and construction tool code; and using the first correspondence as the compilation and construction process information.
[0009] Optionally, before converting the CUDA-related code in the to-be-transcoded file in the code file into the HIP code, the method further comprises: determining the to-be-transcoded file that needs to be transcoded by a code analysis tool.
[0010] In the above implementation, the to-be-transcoded files that need to be transcoded in the code files are determined by the code analysis tool, thereby improving the overall conversion efficiency and accuracy of the code files.
[0011] Optionally, converting the CUDA-related code in the file to be transcoded in the code file into HIP code includes: converting the part of the file to be transcoded corresponding to each code module in the code file from CUDA code to HIP code through a code conversion tool, and using the HIP code corresponding to each code module as the corresponding transcoding module.
[0012] In the above implementation, the CUDA code is converted into the HIP code by a code conversion tool, thereby ensuring the compatibility of the CUDA platform and the ROCm platform of the transcoded file.
[0013] Optionally, modifying the compilation and construction rules corresponding to the transcoding file based on the compilation and construction process information includes: after converting each code module in the code file into a corresponding transcoding module, the file name is not changed, and based on the compilation and construction process information, the original compiler used by the code module corresponding to the transcoding module is modified to a specified compiler; the compilation options of the original compiler are modified to the compilation options corresponding to the specified compiler; the second correspondence between each transcoding module and the specified compiler, and the third correspondence between each transcoding module and the compilation options are used as the compilation and construction rules corresponding to each transcoding module, and the compilation and construction rules corresponding to the transcoding file are a set of compilation and construction rules corresponding to all transcoding modules.
[0014] In the above implementation, the compiler and compilation options are replaced for the compiler corresponding to each code module, thereby realizing the overall transcoding compilation of the CUDA code when the application is large, while not changing the original file name, reducing the changes in the compilation and construction tools and improving the compilation efficiency.
[0015] Optionally, compiling the program source code based on the compilation construction rule includes: compiling the transcoding module based on the compilation construction rule corresponding to the transcoding module each time a transcoding module and a compilation construction rule corresponding to the transcoding module are determined.
[0016] In the above implementation, transcoding and compilation are performed based on each transcoding module, and the transcoding of the code file of the CUDA program is carried out step by step and debugged step by step. Since the amount of single-step modification is not large, it is also easy to locate and troubleshoot, avoiding the complex and difficult compilation problems caused by modifying the code file at one time, and then modifying the corresponding compilation build tool code, and finally attempting to compile in a unified manner, thereby improving the correctness and simplicity of the overall compilation.
[0017] Optionally, the method further includes: debugging and verifying the program source code based on the ROCm platform; after the program source code passes the debugging and verification of the ROCm platform, migrating the program source code as a whole back to the CUDA platform for retesting.
[0018] In the above implementation, the program source code is debugged and verified through the ROCm platform and the CUDA platform respectively, ensuring that the changes made on the ROCm platform do not affect the correct compilation and operation under the CUDA platform, thereby improving its compatibility.
[0019] The embodiment of the present application provides a CUDA program transplantation device, which includes: a code acquisition module, which is used to obtain a code file and a compilation and construction rule of a CUDA program to be transplanted from a CUDA platform; a compilation and construction analysis module, which is used to determine the compilation and construction process information and the compilation and construction rule of the CUDA program; a transcoding module, which is used to convert the CUDA related code in the to-be-transcoded file in the code file into HIP code to obtain a transcoded file; a compilation and construction rule modification module, which is used to modify the compilation and construction rule corresponding to the transcoded file based on the compilation and construction process information, and use the transcoded file and the modified compilation and construction rule as program source code; a compilation module, which is used to compile the program source code based on the compilation and construction rule, and migrate the program source code that has been compiled and can run correctly to the ROCm platform.
[0020] In the above implementation, the compatibility of HIP with CUDA and ROCm platforms is utilized to port the CUDA program into a cross-platform program compatible with both NVIDIA and AMD GPUs. In addition to HIP transcoding of CUDA code, the compilation build information is also analyzed and modified. HIP code conversion can be performed on CUDA-related codes of large applications, thereby improving the efficiency of CUDA program porting.
[0021] Optionally, the compilation and construction analysis module is specifically used to: determine each code module in the code file based on the logical structure of the CUDA code; determine the first correspondence between each code module and the compilation and construction tool based on the compilation and construction tool code; and use the first correspondence as the compilation and construction process information.
[0022] Optionally, the transcoding module is further used to: determine the to-be-transcoded file that needs to be transcoded by using a code analysis tool.
[0023] In the above implementation, the to-be-transcoded files that need to be transcoded in the code files are determined by the code analysis tool, thereby improving the overall conversion efficiency and accuracy of the code files.
[0024] Optionally, the transcoding module is specifically used to: convert the part of the file to be transcoded corresponding to each code module in the code file from CUDA code to HIP code through a code conversion tool, and use the HIP code corresponding to each code module as the corresponding transcoding module.
[0025] In the above implementation, the CUDA code is converted into the HIP code by a code conversion tool, thereby ensuring the compatibility of the CUDA platform and the ROCm platform of the transcoded file.
[0026] Optionally, the compilation and construction rule modification module is specifically used to: after converting each code module in the code file into a corresponding transcoding module, the file name is not changed, and based on the compilation and construction process information, the original compiler used by the code module corresponding to the transcoding module is modified to a specified compiler; the compilation options of the original compiler are modified to the compilation options corresponding to the specified compiler; the second correspondence between each transcoding module and the specified compiler, and the third correspondence between each transcoding module and the compilation options are used as the compilation and construction rules corresponding to each transcoding module, and the compilation and construction rules corresponding to the transcoding file are the set of compilation and construction rules corresponding to all transcoding modules.
[0027] In the above implementation, the compiler and compilation options are replaced for the compiler corresponding to each code module, thereby realizing the overall transcoding compilation of the CUDA code when the application is large, while not changing the original file name, reducing the changes in the compilation and construction tools and improving the compilation efficiency.
[0028] Optionally, the compiling module is specifically configured to: compile the transcoding module based on the compiling construction rule corresponding to the transcoding module each time a transcoding module and a compiling construction rule corresponding to the transcoding module are determined.
[0029] In the above implementation, transcoding and compilation are performed based on each transcoding module, and the transcoding of the code file of the CUDA program is carried out step by step and debugged step by step. Since the amount of single-step modification is not large, it is also easy to locate and troubleshoot, avoiding the complex and difficult compilation problems caused by modifying the code file at one time, and then modifying the corresponding compilation build tool code, and finally attempting to compile in a unified manner, thereby improving the correctness and simplicity of the overall compilation.
[0030] Optionally, the CUDA program transplantation device further includes: a debugging and verification module, which is used to debug and verify the program source code based on the ROCm platform; after the program source code passes the debugging and verification of the ROCm platform, the program source code is migrated as a whole back to the CUDA platform for retesting.
[0031] In the above implementation, the program source code is debugged and verified through the ROCm platform and the CUDA platform respectively to ensure that the changes made on the ROCm platform do not affect the correct compilation and operation under the CUDA platform, thereby ensuring its compatibility.
[0032] An embodiment of the present application further provides an electronic device, which includes a memory and a processor, wherein the memory stores program instructions, and when the processor reads and runs the program instructions, the steps in any of the above implementation methods are executed.
[0033] An embodiment of the present application further provides a readable storage medium, wherein the readable storage medium stores computer program instructions, and when the computer program instructions are read and executed by a processor, the steps in any of the above implementation methods are executed. BRIEF DESCRIPTION OF THE DRAWINGS
[0034] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings required for use in the embodiments of the present application will be briefly introduced below. It should be understood that the following drawings only show certain embodiments of the present application and therefore should not be regarded as limiting the scope. For ordinary technicians in this field, other related drawings can be obtained based on these drawings without paying creative work.
[0035] Figure 1 A flowchart of a CUDA program transplantation method provided in an embodiment of the present application.
[0036] Figure 2 A flowchart of a compilation and construction process information analysis step provided in an embodiment of the present application.
[0037] Figure 3 A flowchart of the steps of modifying compilation construction rules provided in an embodiment of the present application.
[0038] Figure 4 A flowchart of another CUDA program transplantation method provided in an embodiment of the present application.
[0039] Figure 5 A schematic diagram of a module of a CUDA program transplantation device provided in an embodiment of the present application.
[0040] Icons: 20-CUDA program transplantation device; 21-code acquisition module; 22-compilation and construction analysis module; 23-transcoding module; 24-compilation and construction rule modification module; 25-compilation module. DETAILED DESCRIPTION
[0041] The technical solutions in the embodiments of the present application will be described below in conjunction with the accompanying drawings in the embodiments of the present application.
[0042] The applicant has found through research that the CUDA code search and automatic code conversion tools provided by AMD can help search for the code that needs to be converted in the application and automatically convert it. However, in the actual transplantation process, large-scale applications often encounter many problems due to their own complexity. For example, large-scale applications generally have complex compilation and construction systems, which do not belong to the CUDA or ROCm platform itself. The existing technology is just a scattered tool with specific functions, and there is no overall method. Therefore, the existing automatic transcoding tools are powerless. Directly converting all CUDA-related codes of large-scale applications into HIP codes through existing technologies often encounters many problems, involving various links such as compilation, linking, and running, which are difficult to locate and troubleshoot, so the transplantation is very difficult.
[0043] In order to solve the above problems, the present application embodiment provides a CUDA program migration method, please refer to Figure 1 , Figure 1 A flowchart of a CUDA program transplantation method provided in an embodiment of the present application, the specific steps of which may be as follows:
[0044] Step S11: Obtain the code file and compile and build rules of the CUDA program to be ported from the CUDA platform.
[0045] A commonly used application source code is usually an entire project, including code files, compilation and construction rule-related files that control the compilation and construction process, and may also include program description files, test files, etc. This embodiment obtains code files and compilation and construction rule-related files therefrom.
[0046] It should be understood that since correctly running CUDA code is the basis for the successful porting of the application to the ROCm platform, before obtaining the code file of the CUDA program to be ported, the correct operation of the CUDA program is first implemented on the CUDA platform.
[0047] Generally, well-maintained CUDA programs are easier to compile on the CUDA platform. After successful compilation on the CUDA platform, run appropriate test cases and check the results. After verifying that the results are correct, it can be determined that the CUDA program can run correctly on the CUDA platform.
[0048] Specifically, verify the running correctness of the CUDA program on the CUDA platform, run the program's own unit test, regression test and other test sets, or run commonly used standard examples, and compare the program running results with the known correct results to verify the correctness of the CUDA program.
[0049] Existing compilation tools are basically for searching and automatically transcoding CUDA-related code files, which is convenient enough for single files and small applications, but large applications have complex compilation and construction systems, which are difficult for existing compilation tools to automatically handle. Therefore, this embodiment then determines the compilation and construction process information of the CUDA program through step S12, so that the compilation and construction process information can be modified during transcoding.
[0050] Step S12: Determine the compiling and building process information of the CUDA program.
[0051] Larger CUDA programs are usually composed of many code files in a certain logical structure, and there are complex dependencies between the various code parts. Different applications may use different compilation and construction tools, but the functions of these compilation and construction tools are generally the same, which are to process file dependency structures, specify compilers and compilation parameters, specify external dependency libraries, specify linkers and link parameters, etc. Analyzing the compilation and construction tools can obtain compilation and construction process information.
[0052] Specifically, please refer to Figure 2 , Figure 2 A flowchart of a compilation and construction process information analysis step provided in an embodiment of the present application.
[0053] Step S121: Based on the logical structure of the CUDA code, determine each code module in the code file.
[0054] Optionally, the logical structure of the CUDA code in this embodiment can divide each code module according to the file directory of the code file, and regard a single code file at a certain level in the file directory of the code file as a code module.
[0055] Step S122: determining a first correspondence between each code module and the compiling and building tool based on the compiling and building tool code.
[0056] The compilation and construction tools corresponding to the CUDA code in this embodiment are generally different compilation and construction tools called by the Nvcc compilation driver.
[0057] Step S123: using the first corresponding relationship as compiling and building process information.
[0058] Optionally, the compilation and construction process information in this embodiment may include, in addition to the first corresponding relationship, relevant parameter settings of the compilation and construction tool.
[0059] It should be understood that when executing step S13 to transcode the code file, HIP for transcoding needs to be used. HIP is an open source heterogeneous parallel computing framework on AMD. HIP is relatively independent of the ROCm platform and can be directly installed on a computer equipped with NVIDIA GPU and CUDA through the installation package provided by AMD. There is no need to install other components of ROCm. Therefore, this embodiment installs the HIP runtime library on the execution device of the CUDA program transplantation method, and transplants the code on the CUDA platform to the ROCm platform through the HIP runtime library. Optionally, the installation of the HIP runtime library can be performed before or after step S11 or step S12.
[0060] Before transcoding a code file, it is necessary to determine which parts of the code file need to be transcoded. In this embodiment, a code analysis tool may be used to determine the code that needs to be converted in the CUDA code file.
[0061] Optionally, the above-mentioned code analysis tool may include findcode.sh and / or hipexamine-perl.sh, wherein findcode.sh can search for code files with specified suffixes, and hipexamine-perl.sh can scan the target file and identify CUDA codes that can be automatically converted and codes that are suspected to be CUDA programs but will not be automatically converted. In this embodiment, findcode.sh and hipexamine-perl.sh are used in combination to search for code files that need to be converted, and the determined files to be transcoded are recorded. Step S13: Convert the CUDA-related code in the file to be transcoded in the code file into HIP code to obtain a transcoded file.
[0062] Optionally, in this embodiment, CUDA transcoding may be performed using a code conversion tool, and the code conversion tool may be hipify-perl in the HIP tool. The hipify-perl tool may convert CUDA code in a single code file into HIP code.
[0063] In this embodiment, the to-be-transcoded files that need to be transcoded in the code files can be transcoded according to the search results of findcode.sh and hipexamine-perl.sh.
[0064] Optionally, the transcoding process in this embodiment may be to transcode individual code modules in the file to be transcoded one by one.
[0065] Optionally, in this embodiment, the files to be transcoded may be determined and transcoded by using a code analysis tool and a code transcoding tool other than the HIP, or the files to be transcoded may be determined and transcoded by manual selection.
[0066] Since existing tools are basically for searching and automatically transcoding CUDA-related code files, they are convenient enough for single files and small applications, but large applications have complex compilation and construction rules, which are difficult for existing tools to automatically handle. This method proposes the importance of compilation and construction rules when porting large applications, and modifies the compilation and construction rules through step S14.
[0067] Step S14: modifying the compilation and construction rules corresponding to the transcoded file based on the compilation and construction process information, and using the transcoded file and the modified compilation and construction rules as program source code.
[0068] Please refer to Figure 3 , Figure 3 A flowchart of a compilation construction rule modification step provided in an embodiment of the present application, step S14 may be specifically as follows:
[0069] Step S141: after converting each code module in the code file into a corresponding transcoding module, the file name is not changed, and based on the compilation and construction process information, the original compiler used by the code module corresponding to the transcoding module is modified to a specified compiler.
[0070] Optionally, the compiler used by the above-mentioned non-transcoded code module is usually a compiler called by Nvcc, and the specified compiler after modification and replacement is usually a compiler called by hipcc.
[0071] Without changing the file name of the original CUDA code file, the changes to the compilation and construction tools can be reduced, thereby improving the overall compilation efficiency.
[0072] Step S142: modify the compilation options of the original compiler to the compilation options corresponding to the specified compiler.
[0073] Optionally, the above compilation options may be compilation options related to code generation, output file, preprocessing, language and miscellaneous items.
[0074] Step S143: the second correspondence between each transcoding module and the specified compiler, and the third correspondence between each transcoding module and the compilation option are used as the compilation construction rule corresponding to each transcoding module, and the compilation construction rule corresponding to the transcoding file is a set of compilation construction rules corresponding to all transcoding modules.
[0075] Optionally, in this embodiment, the transcoding of each code module and the modification of the compilation construction rule of each transcoding module may also be performed module by module.
[0076] Step S15: Compile the program source code based on the compilation construction rules.
[0077] It should be understood that in the prior art, when compiling for the CUDA platform, all code files that need to be transcoded can be modified at one time, and then the corresponding compilation and construction tool codes are modified, and finally the compilation is attempted in a unified manner. If the CUDA program is directly migrated to the ROCm platform for overall transcoding and compilation, the amount of single changes is large, and the cause of the error is difficult to find. When porting large applications, many problems such as complex error reporting and low efficiency are often encountered. Various problems are intertwined, making the porting work difficult.
[0078] Therefore, when compiling the transcoding file, the present embodiment can compile the transcoding modules individually and one by one based on the compilation and construction rule corresponding to each transcoding module when each transcoding module and the compilation and construction rule corresponding to the transcoding module are determined, and the code files are gradually modified and compiled. Even if problems are encountered in the process, the characteristic that the HIP code can be compiled and run on both CUDA and ROCm platforms is utilized, and the CUDA code is gradually modified to the HIP code on the CUDA platform, thereby reducing the amount of single modification, reducing the difficulty of transplantation, and narrowing the scope of investigation when problems are encountered.
[0079] When the compilation fails, you can return to the step of determining the file to be transcoded and execute the subsequent steps again to eliminate the error.
[0080] Since the embodiment of the present application is to port the CUDA platform program to the ROCm platform, the compatibility and running stability of the ported object need to be verified on both platforms. Figure 4 , Figure 4 A flowchart of another CUDA program transplantation method provided in an embodiment of the present application is provided. The CUDA program transplantation method includes program verification steps for a CUDA platform and a ROCm platform, which may be specifically as follows:
[0081] Step S16: Verify the correctness of the program source code on the CUDA platform.
[0082] Each time a single code module is modified and compiled, an executable program is obtained, and the executable program is tested again through the test example in step S11 to ensure that the program running result is correct, otherwise return to step S14 to re-check the problem and recompile.
[0083] Since step S16 is still performed on the CUDA platform, if the application depends on external GPU library files, the original CUDA platform library files, such as cufft, cublas, etc., are still used.
[0084] Optionally, steps S14-S16 are an iterative process. In this embodiment, the next step is executed after all CUDA code files that need to be transcoded are converted into HIP codes.
[0085] Step S17: Migrate the program source code to the ROCm platform for compilation and debugging.
[0086] After the HIP program is correctly run on the CUDA platform, the program source code is migrated to the ROCm platform for further compilation and debugging to obtain an executable program. On the one hand, some HIP APIs supported by the ROCm platform may be slightly different from those supported by CUDA. In this case, conditional compilation is required to specify different code branches for compilation on different platforms. On the other hand, if the application depends on an external GPU library, it needs to be replaced with the corresponding library on the ROCm platform, such as cufft replaced with rocfft, cublas replaced with rocblas, etc. The above operations are achieved by modifying the compilation and construction tools.
[0087] Step S18: Verify the correctness of the executable program on the ROCm platform.
[0088] Optionally, after debugging and compiling to obtain the ROCm platform executable program under the ROCm platform based on step S17, the executable program is tested using the test example in step S11 to ensure that the program running result is correct, otherwise return to step S17 to continue debugging.
[0089] In this embodiment, the CUDA program is first transcoded into a HIP program on the CUDA platform, and the GPU library files of the CUDA platform can be retained without modification. Then, the program is migrated to the ROCm platform for debugging. The HIP code of the application program itself has been proven to be usable for the most part, and only a small number of APIs and external GPU library calls need to be debugged, which reduces the difficulty of porting and achieves compatibility with both NVIDIA and AMD GPUs.
[0090] Step S19: Migrate the program source code that has been compiled and run correctly on the ROCm platform to the CUDA platform for retesting.
[0091] After the HIP program is running correctly on the ROCm platform, the code is migrated back to the CUDA platform and tested again to confirm that the changes made on the ROCm platform do not affect the correct compilation and operation on the CUDA platform.
[0092] After step S19, the CUDA application is transplanted to the HIP, and the program is compatible with both NVIDIA and AMD GPUs. Optionally, to further improve performance, the present embodiment can further optimize the code on both CUDA and ROCm platforms.
[0093] In order to cooperate with the above-mentioned CUDA program transplantation method provided in the embodiment of the present application, the embodiment of the present application also provides a CUDA program transplantation device 20.
[0094] Please refer to Figure 5 , Figure 5 A schematic diagram of a module of a CUDA program transplantation device provided in an embodiment of the present application.
[0095] The CUDA program transplantation device 20 comprises:
[0096] A code acquisition module 21 is used to acquire the code file and compile and build rules of the CUDA program to be ported from the CUDA platform;
[0097] A compile and build analysis module 22, used to determine the compile and build process information of the CUDA program;
[0098] A transcoding module 23, configured to convert the CUDA-related code in the file to be transcoded in the code file into a HIP code to obtain a transcoded file;
[0099] A compiling and building rule modifying module 24 is used to modify the compiling and building rule corresponding to the transcoded file based on the compiling and building process information, and use the transcoded file and the corresponding modified compiling and building rule file as program source code;
[0100] The compiling module 25 is used to compile the program source code based on the compiling construction rules, and migrate the compiled and correctly running program source code to the ROCm platform.
[0101] Optionally, the compilation and construction analysis module 22 is specifically used to: determine each code module in the code file based on the logical structure of the CUDA code; determine the first correspondence between each code module and the compilation and construction tool based on the compilation and construction tool code; and use the first correspondence as the compilation and construction process information.
[0102] Optionally, the transcoding module 23 is further used to determine the to-be-transcoded file that needs to be transcoded by using a code analysis tool.
[0103] Optionally, the transcoding module 23 is specifically used to: convert the part of the file to be transcoded corresponding to each code module in the code file from CUDA code to HIP code through a code conversion tool, and use the HIP code corresponding to each code module as the corresponding transcoding module.
[0104] Optionally, the compilation and construction rule modification module 24 is specifically used for: not changing the file name after converting each code module in the code file into a corresponding transcoding module, and modifying the original compiler used by the code module corresponding to the transcoding module to a specified compiler based on the compilation and construction process information; modifying the compilation options of the original compiler to the compilation options corresponding to the specified compiler; using the second correspondence between each transcoding module and the specified compiler, and the third correspondence between each transcoding module and the compilation options as the compilation and construction rules corresponding to each transcoding module, and the compilation and construction rules of the transcoding file are the set of compilation and construction rules corresponding to all transcoding modules.
[0105] Optionally, the compiling module 25 is specifically configured to: compile a transcoding module based on the compiling construction rule corresponding to the transcoding module each time a transcoding module and a compiling construction rule corresponding to the transcoding module are determined.
[0106] Optionally, the CUDA program transplantation device 20 further includes: a debugging and verification module, which is used to debug and verify the program source code based on the ROCm platform; after the program source code passes the debugging and verification of the ROCm platform, the program source code is migrated as a whole back to the CUDA platform for retesting.
[0107] An embodiment of the present application further provides an electronic device, which includes a memory and a processor, wherein the memory stores program instructions, and when the processor reads and runs the program instructions, it executes the steps of any one of the CUDA program porting methods provided in this embodiment.
[0108] It should be understood that the electronic device may be a personal computer (PC), a tablet computer, a smart phone, a personal digital assistant (PDA), or other electronic device with logic computing functions.
[0109] The embodiment of the present application further provides a readable storage medium, wherein the readable storage medium stores computer program instructions, and when the computer program instructions are read and executed by a processor, the steps in the CUDA program porting method are executed.
[0110] In summary, the embodiments of the present application provide a CUDA program porting method, device, electronic device and storage medium, the method comprising: obtaining a code file and a compilation and construction rule of a CUDA program to be ported from a CUDA platform; determining the compilation and construction process information of the CUDA program; converting the CUDA-related code in the file to be transcoded in the code file into a HIP code to obtain a transcoded file; modifying the compilation and construction rule corresponding to the transcoded file based on the compilation and construction process information, and using the transcoded file and the corresponding modified compilation and construction rule file as program source code; compiling the program source code based on the compilation and construction rule, and migrating the compiled and correctly running program source code to the ROCm platform.
[0111] In the above implementation, the compatibility of HIP with CUDA and ROCm platforms is utilized to port the CUDA program into a cross-platform program compatible with both NVIDIA and AMD GPUs. In addition to HIP transcoding of CUDA code, the compilation build information is also analyzed and modified. HIP code conversion can be performed on CUDA-related codes of large applications, thereby improving the efficiency of CUDA program porting.
[0112] In several embodiments provided in the present application, it should be understood that the disclosed device can also be implemented in other ways. The device embodiments described above are merely schematic, for example, the block diagrams in the accompanying drawings show the possible architecture, functions and operations of the devices according to the multiple embodiments of the present application. In this regard, each box in the block diagram can represent a module, a program segment or a part of a code, and the module, a program segment or a part of the code contains one or more executable instructions for implementing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two consecutive boxes can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram, and the combination of the block diagrams, can be implemented with a dedicated hardware-based system that performs a specified function or action, or can be implemented with a combination of dedicated hardware and computer instructions.
[0113] In addition, the functional modules in the various embodiments of the present application may be integrated together to form an independent part, or each module may exist separately, or two or more modules may be integrated to form an independent part.
[0114] If the function is implemented in the form of a software function module and sold or used as an independent product, it can be stored in a computer-readable storage medium. Therefore, this embodiment also provides a computer program instruction stored in a readable storage medium, and when the computer program instruction is read and executed by a processor, the steps in any method described in the block data storage method are executed. Based on this understanding, the technical solution of the present application is essentially or the part that contributes to the prior art or the part of the technical solution can be embodied in the form of a software product, which is stored in a storage medium and includes several instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, RanDom Access Memory), disk or optical disk and other media that can store program codes.
[0115] The above description is only an embodiment of the present application and is not intended to limit the scope of protection of the present application. For those skilled in the art, the present application may have various changes and variations. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present application should be included in the scope of protection of the present application. It should be noted that similar reference numerals and letters represent similar items in the following drawings, so once an item is defined in one drawing, it does not need to be further defined and explained in the subsequent drawings.
[0116] The above is only a specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any technician familiar with the technical field can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be covered by the protection scope of the present application.
[0117] It should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the statement "include..." do not exclude the presence of other identical elements in the process, method, article or device including the elements.
Claims
1. A CUDA program transplantation method, characterized in that: include: Obtain the code files and compilation and construction rules of the CUDA program to be ported from the CUDA platform; Determine the compilation and construction process information of the CUDA program; Converting the CUDA-related code in the file to be transcoded in the code file into HIP code to obtain a transcoded file; Modify the compilation and construction rules corresponding to the transcoded file based on the compilation and construction process information, and use the transcoded file and the modified compilation and construction rules as program source code; Compile the program source code based on the compilation construction rule, and migrate the compiled and correctly running program source code to the ROCm platform; The determining of the compiling and building process information of the CUDA program includes: Based on the logical structure of the CUDA code, determining each code module in the code file; Determine a first correspondence between each code module and the compile and build tool based on the compile and build tool code; The first corresponding relationship is used as the compilation and construction process information.
2. The method according to claim 1, characterized in that: Before converting the CUDA-related code in the to-be-transcoded file in the code file into the HIP code, the method further includes: The to-be-transcoded file that needs to be transcoded is determined by a code analysis tool.
3. The method according to claim 2, characterized in that The converting the CUDA related code in the file to be transcoded in the code file into the HIP code comprises: The part of the file to be transcoded corresponding to each code module in the code file is converted from CUDA code to HIP code by a code conversion tool, and the HIP code corresponding to each code module is used as the corresponding transcoding module.
4. The method according to claim 3, characterized in that The modifying the compilation and construction rules corresponding to the transcoded file based on the compilation and construction process information includes: After converting each code module in the code file into a corresponding transcoding module, the file name is not changed, and based on the compilation and construction process information, an original compiler used by the code module corresponding to the transcoding module is modified to a specified compiler; Modify the compilation options of the original compiler to the compilation options corresponding to the specified compiler; The second correspondence between each transcoding module and the specified compiler, and the third correspondence between each transcoding module and the compilation option are used as the compilation construction rule corresponding to each transcoding module, and the compilation construction rule corresponding to the transcoding file is a set of compilation construction rules corresponding to all transcoding modules.
5. The method according to claim 4, characterized in that The compiling the program source code based on the compilation construction rule includes: Each time a transcoding module and a compile and construct rule corresponding to the transcoding module are determined, the transcoding module is compiled based on the compile and construct rule corresponding to the transcoding module.
6. The method according to claim 1, characterized in that The method further comprises: Debug and verify the program source code based on the ROCm platform; After the program source code passes the debugging and verification of the ROCm platform, the program source code is migrated as a whole back to the CUDA platform for retesting.
7. A CUDA program transplantation device, characterized in that: The device comprises: A code acquisition module is used to obtain the code files and compilation and construction rules of the CUDA program to be ported from the CUDA platform; A compile and build analysis module, used to determine the compile and build process information of the CUDA program; A transcoding module, used for converting CUDA related codes in the file to be transcoded in the code file into HIP codes to obtain a transcoded file; A compiling and building rule modification module, used to modify the compiling and building rule corresponding to the transcoded file based on the compiling and building process information, and use the transcoded file and the modified compiling and building rule as program source code; A compiling module, used to compile the program source code based on the compiling construction rule, and migrate the compiled and correctly running program source code to the ROCm platform; The compilation and construction analysis module is specifically used to determine each code module in the code file based on the logical structure of the CUDA code; determine the first correspondence between each code module and the compilation and construction tool based on the compilation and construction tool code; and use the first correspondence as the compilation and construction process information.
8. An electronic device, characterized in that: The electronic device comprises a memory and a processor, wherein the memory stores program instructions, and when the processor runs the program instructions, the steps in the method according to any one of claims 1 to 6 are executed.
9. A storage medium, characterized in that: The storage medium stores computer program instructions, and when the computer program instructions are executed by a processor, the steps in the method according to any one of claims 1 to 6 are executed.
Citation Information
Patent Citations
Reactor neutron transport calculation method based on domestic accelerator card
CN110704106A
Heterogeneous application transplantation method and device and computer equipment
CN111610976A