Method and apparatus for converting CUDA-C to ATHREAD-C

By using a CUDA-C to ATHREAD-C conversion method, and leveraging the data affine transformation algorithm and the ANTLR tool to generate ATHREAD-C code, the complexity and accuracy issues of CUDA-C code porting on domestic heterogeneous many-core processors are resolved, achieving efficient code conversion and performance improvement.

CN114625374BActive Publication Date: 2026-11-17QINGDAO NAT LAB FOR MARINE SCI & TECH DEV CENT
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202210248764.7
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-03-14
Publication Date
2026-11-17
Estimated Expiration
2042-03-14

AI Technical Summary

Technical Problem

Existing technologies make it difficult to efficiently port high-performance computing and artificial intelligence computing code to domestically produced heterogeneous many-core processors, especially the conversion of CUDA-C code to ATHREAD-C, which suffers from high complexity, accuracy and scalability issues in manual porting.

Method used

A method for converting CUDA-C to ATHREAD-C was designed, including a data affine transformation algorithm, a performance-priority memory usage strategy, and a pre-implemented CUDA primitive transformation mapping table. An abstract syntax tree was automatically generated using the ANTLR language translation tool to generate ATHREAD-C code.

Benefits of technology

It enabled the rapid and accurate porting of CUDA-C code to domestically produced supercomputers, ensuring data accuracy and software scalability while improving code performance.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114625374B_ABST
    Figure CN114625374B_ABST
Patent Text Reader

Abstract

This application discloses a method and apparatus for converting CUDA-C to ATHREAD-C. The CUDA-C to ATHREAD-C conversion method includes: obtaining CUDA kernel code; obtaining a preset conversion method; automatically generating an abstract syntax tree (AST) of the CUDA code using the ANTLR language translation tool; and generating ATHREAD code based on the AST and the preset conversion method. The CUDA-C to ATHREAD-C conversion method disclosed in this application establishes a hardware correspondence for heterogeneous accelerators and designs a software automatic conversion architecture based on a compiler front-end. It fully inherits the parallel algorithm structure of CUDA-C, eliminating the need to redesign parallel algorithms on domestic supercomputers, ensuring the accuracy of the ported data and the scalability of the software. The approximate hardware correspondence and the matching memory usage principles ensure the basic performance of the converted code.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of high-performance computing technology, and in particular to a method and apparatus for converting CUDA-C to ATHREAD-C. Background Technology

[0002] Currently, the increased efficiency of high-performance computing (HPC) and artificial intelligence (AI) computing relies heavily on GPUs for acceleration. GPUs provide tens of thousands of computing cores, enabling parallel computation of massive amounts of data and computational demands, significantly improving operational efficiency and gaining widespread application in these fields. In the global supercomputing rankings released in November 2020, 11 of the top 20 supercomputing centers used NVIDIA GPUs. On the software side, GPUs have gained widespread support in over 600 large-scale mainstream applications, including atmospheric physics and oceanography models, industrial design and manufacturing, life sciences, and artificial intelligence. On the other hand, the development of the software ecosystem has long been a bottleneck for supercomputers based on the domestically produced heterogeneous many-core architecture of the Sunway supercomputer. Due to the limitations of domestic heterogeneous many-core architectures, which are more focused on low-level programming models, mainstream AI and scientific computing applications cannot be directly run on domestic supercomputers. Manual porting is highly complex, and accuracy and scalability are difficult to guarantee. Furthermore, to leverage the performance advantages of domestic supercomputers, it is essential to fully utilize slave cores and develop and refactor algorithms for efficient task parallelism or data parallelism. This places higher demands on porting.

[0003] At the code porting level, much research currently focuses on converting serial C language to CUDA language, based on the polyhedral model in the compiler front-end. A typical example is the C-to-CUDA source code converter proposed by Baskaran. Source code conversion for heterogeneous computing is commonly seen in CUDA language to OpenCL language conversion, with the CU2CL source code converter proposed by Gabriel Martinez et al. being a typical example. The latest achievement in code conversion for domestic heterogeneous many-core processors is the OpenCL to Athread source code converter proposed by Wu Mingchuan et al. However, its work-group conversion mechanism for slave kernels is not suitable for kernel code in large and complex domain applications, fails to fully utilize slave kernel data reuse to improve performance, and does not consider commonly used OpenCL primitive conversion strategies. Summary of the Invention

[0004] Therefore, to address the issue of rapid porting on the Sunway domestic many-core supercomputer, this application's CUDA-C to ATHREAD-C conversion method designs a pre-defined conversion method between CUDA-C and ATHREAD-C (including a data affine transformation algorithm, a performance-priority memory usage strategy, and a pre-implemented CUDA primitive conversion mapping table). Based on the open-source ANTLR language translation tool, CUDA-C is automatically converted into an abstract syntax tree. Using pre-designed conversion principles, host code, kernel code, and general-purpose code based on ATHREAD-C are automatically generated, effectively solving the aforementioned problems.

[0005] On the one hand, the CUDA-C to ATHREAD-C conversion method includes:

[0006] Obtain the CUDA kernel code;

[0007] Get the preset conversion method;

[0008] The ANTLR language translation tool automatically generates the abstract syntax tree for CUDA code.

[0009] ATHREAD code is generated based on the abstract syntax tree and the preset transformation method.

[0010] Optionally, the preset conversion method includes: data affine transformation and CUDA code inheritance algorithm.

[0011] Optionally, the preset conversion method includes: a kernel data reuse and memory optimization algorithm.

[0012] Optionally, the preset conversion method includes: CUDA primitive conversion mapping method.

[0013] Optionally, generating ATHREAD code based on the abstract syntax tree and a preset transformation method includes:

[0014] The preset transformation method is implanted into the corresponding node of the abstract syntax tree to generate ATHREAD code.

[0015] This application also provides a CUDA-C to ATHREAD-C conversion device, the CUDA-C to ATHREAD-C conversion device comprising:

[0016] A kernel code acquisition module, which is used to acquire CUDA kernel code;

[0017] A mapping model construction module is used to construct a hardware mapping model of GPU and Sunway domestic many-core as acceleration units;

[0018] A preset conversion method generation module is used to generate a preset conversion method based on the hardware mapping model.

[0019] Abstract syntax tree generation module, which is used to automatically generate abstract syntax trees for CUDA code using the ANTLR language translation tool;

[0020] A conversion module is used to generate ATHREAD code based on the abstract syntax tree and a preset conversion method.

[0021] Optionally, the preset conversion method includes: data affine conversion and CUDA code inheritance algorithm, kernel data reuse and memory optimization algorithm, and CUDA primitive conversion mapping method.

[0022] Optionally, the ATHREAD code includes ATHREAD host code, ATHREAD device code, and ATHREAD general auxiliary code.

[0023] The CUDA-C to ATHREAD-C conversion method disclosed in this application establishes the hardware correspondence of heterogeneous accelerators, and designs an automatic software conversion architecture based on the compiler front-end. It fully inherits the parallel algorithm structure of CUDA-C, eliminating the need to redesign the parallel algorithm on domestic supercomputers, thus ensuring the accuracy of the ported data and the scalability of the software. The approximate correspondence in hardware and the matching memory usage principles ensure the basic performance of the converted code. Attached Figure Description

[0024] The embodiments described below with reference to the accompanying drawings are exemplary and intended to explain and illustrate this application, and should not be construed as limiting the scope of protection of this application.

[0025] Figure 1 This is a flowchart illustrating an embodiment of the CUDA-C to ATHREAD-C conversion method disclosed in this application.

[0026] Figure 2 This is the overall design framework diagram of the CUDA-C to ATHREAD-C conversion method of this application.

[0027] Figure 3 This is a framework diagram of the hardware mapping model of this application.

[0028] Figure 4 This is a flowchart illustrating the data affine transformation and CUDA code inheritance algorithm.

[0029] Figure 5 This is a schematic diagram of the conversion algorithm for CUDA index classes.

[0030] Figure 6 This is a flowchart illustrating the kernel data reuse and memory optimization algorithms.

[0031] Figure 7 This is a schematic diagram of the data index array algorithm.

[0032] Figure 8 This is a flowchart illustrating the process of generating ATHREAD code based on the abstract syntax tree and a preset transformation method. Detailed Implementation

[0033] To make the objectives, technical solutions, and advantages of this application clearer, the technical solutions in the embodiments of this application will be described in more detail below with reference to the accompanying drawings.

[0034] The following is for reference. Figures 1-7 This application provides a detailed description of an embodiment of the CUDA-C to ATHREAD-C conversion method. This embodiment offers a CUDA-C to ATHREAD-C conversion method including:

[0035] Step 1: Obtain the CUDA kernel code;

[0036] Step 2: Obtain the preset conversion method;

[0037] Step 3: Automatically generate the abstract syntax tree of CUDA code using the ANTLR language translation tool;

[0038] Step 4: Generate ATHREAD code based on the abstract syntax tree and the preset conversion method.

[0039] The CUDA-C to ATHREAD-C conversion method disclosed in this application establishes the hardware correspondence of heterogeneous accelerators, and designs an automatic software conversion architecture based on the compiler front-end. It fully inherits the parallel algorithm structure of CUDA-C, eliminating the need to redesign the parallel algorithm on domestic supercomputers, thus ensuring the accuracy of the ported data and the scalability of the software. The approximate correspondence in hardware and the matching memory usage principles ensure the basic performance of the converted code.

[0040] In this embodiment, the preset conversion method includes: data affine transformation and CUDA code inheritance algorithm.

[0041] In this embodiment, the preset conversion method includes: a kernel data reuse and memory optimization algorithm.

[0042] In this embodiment, the preset conversion method includes: CUDA primitive conversion mapping method.

[0043] In this embodiment, the preset conversion method includes: the Sunway-assisted algorithm.

[0044] In this embodiment, generating ATHREAD code based on the abstract syntax tree and a preset transformation method includes:

[0045] The preset transformation method is implanted into the corresponding node of the abstract syntax tree to generate ATHREAD code.

[0046] See Figure 8 In this embodiment, the ANTLR language translation tool is used to automatically generate the abstract syntax tree of CUDA code;

[0047] The specific implementation process of generating ATHREAD code based on the abstract syntax tree and preset conversion methods is as follows;

[0048] 1. Automatically generate the abstract syntax tree of CUDA code using the ANTLR language translation tool:

[0049] The ANTLR source code analysis tool is used to parse the CUDA kernel functions and generate an abstract syntax tree.

[0050] 2. The first traversal of the abstract syntax tree involves preprocessing the parameter list data of the CUDA kernel functions using... Figure 5 The memory hierarchy optimization algorithm shown forms a linked list of storage types for parameter variables.

[0051] 3. Based on the thread block index array cudaGrid[3] of the CUDA kernel function, and following the principle of Formula 1, manually calculate and obtain cudaGridNew[3] suitable for the Sunway CPE.

[0052] 4. The abstract syntax tree is traversed for the second time. Before traversing the function body, a data affine transformation algorithm is inserted to generate the index arrays of sunwayGrid[3] and sunwayBlock[3].

[0053] 5. Based on the linked list of parameter variables' storage types, insert the corresponding initialization operations or DMA operations into the parameter variables.

[0054] 6. Insert a for loop statement with a loop size equal to the product of the sunwayBlock array.

[0055] 7. Use the CUDA primitive transformation mapping in Table 1 to transform the CUDA index class primitives.

[0056] 8. Inherit and convert CUDA code until the for loop ends at __syncthreads().

[0057] 9. Output the transformed abstract syntax tree and generate new ATHREAD code.

[0058] The following examples further illustrate this application in detail. It is understood that these examples do not constitute any limitation on this application.

[0059] See Figure 2 The overall design framework of this invention is as follows: Figure 2 As shown. First, this invention constructs a hardware mapping model using GPUs and the domestically developed Sunway many-core CPU as acceleration units. Based on this, a data affine transformation algorithm and a kernel data reuse algorithm are designed. For the FFT library frequently used in CUDA, a corresponding FFT kernel library implementation is designed in the domestically developed many-core CPU. Primitive mapping relationships are designed according to the CUDA API and primitive usage methods. Using the ANTLR language translation tool, an abstract syntax tree of the CUDA code is automatically generated. The above transformation algorithms are then implanted into the corresponding nodes, thereby generating the final ATHREAD host code, device code, and general auxiliary code.

[0060] See Figure 3 In this embodiment, the heterogeneous hardware mapping model between the GPU and the Sunway domestic many-core CPU is as follows: Figure 3 As shown:

[0061] 1. The multi-core CPU and the Sunway MPE serve the same function as the main core of the heterogeneous computing system. They are responsible for the logical part of the application, the data interaction required by the host and devices, and the startup of the device kernel.

[0062] 2. The GPU's SM and Sunway CPE serve as execution units of the accelerator, with the same function: handling the data-intensive computation portion of the application.

[0063] There is also a corresponding relationship between the GPU and the memory architecture of the Sunway domestic many-core supercomputer:

[0064] 1. The shared memory in the GPU SM and the local memory in the Sunway CPE have the same function. Variables defined within it have a faster access speed and cannot be accessed by other SMs or CPEs.

[0065] 2. The GPU's global memory can function similarly to the Sunway's shared local memory. The main characteristic of the GPU's global memory is that it can be accessed by all threads, but the access speed is relatively slow. The Sunway's shared local memory is slightly slower than a single local memory, but faster than the main core cache, and can be accessed by all CPEs on the Sunway. Replacing the GPU's global memory with shared local memory will yield a significant performance advantage.

[0066] 3. The DRAM of the multi-core CPU and the main memory of the Sunway supercomputer serve the same purpose; data transfer between the master and slave cores is completed based on the main memory. Compared to the DRAM of a multi-core CPU, the main memory of the Sunway supercomputer can be accessed by slave cores and has an additional L2 cache, which can be used for remote access to the main memory via GLD.

[0067] See Figure 4 The data affine transformation and CUDA code inheritance algorithm in this application specifically refer to:

[0068] CUDA manages data through a grid, which consists of three-dimensional thread blocks, each of which is itself composed of three-dimensional threads. To this end, CUDA provides a series of primitives, which are used to index data on threads, thereby achieving a Single Instruction Multiple Thread (SIMT) parallel computing model. ATHREAD manages data through multi-level parallelism. Data is first distributed to the Sunway MPE via MPI, and the MPE further distributes the data to the CPE, thus completing multi-level parallel computing. The data affine transformation algorithm of this invention is as follows:

[0069] 1. Based on the data allocated to MPE by ATHREAD, the three-dimensional thread block array used by CUDA is proportionally quantized and scaled down to generate a new three-dimensional thread block array cudaGridNew. The corresponding formula is as follows:

[0070] (cudaGrid[0]*cudaGrid[1]*cudaGrid[2]) / (cudaGridNew[0]*cudaGridNew[1]*cudaGridNew[2])=cudaGridData / sunwayMPEData cduaGrid[0]:cduaGrid[1]:cduaGrid[2]=cudaGridNew[0]:cudaGridNew[1]:cudaGridNew[0]

[0071] Where cudaGrid is a CUDA three-dimensional thread block array, cudaGridData is the total data of grid in CUDA, and sunwayMPEData is the data of a single node of the Sunway MPE supercomputer.

[0072] 2. The three-dimensional thread block in cudaGridNew must be less than the number of Sunway CPEs (64). If this is not met, conversion is required. Considering that the smallest organizational unit of CUDA programming is based on warp, and many parallel algorithms are also based on warp as the basic unit, for the safety and reliability of parallel algorithms, we use warp (32 threads) as the unit. By adjusting the size of the CUDA three-dimensional threads, we adjust the value of cudaGridNew until their product meets the requirement of being less than 64, thus calculating the required data index array. The algorithm list is as follows: Figure 7 Where cudaBlock is a CUDA three-dimensional thread array, and sunwayBlock and sunwayGrid are data index arrays used in ATHREAD after affine transformation.

[0073] This invention fully considers the conversion strategies required for parallel thread programming within CUDA thread blocks and sequential programming within the Sunway single CPE. First, an affine transformation algorithm is used to perform affine transformations on the CUDA thread block array and the thread array. Second, during the automatic translation of CUDA kernel functions, a for loop is inserted inside the function to convert CUDA data index primitives using the converted Sunway thread index array, and this is then embedded into the code. Finally, inheriting all CUDA code, only data type class primitives and API primitives are converted accordingly, thus realizing the conversion of parallel execution of thread blocks into sequential execution within the CPE. See the overall design for details. Figure 4 .

[0074] In this embodiment, the CUDA primitive transformation mapping method is specifically as follows:

[0075] The conversion algorithms for CUDA index classes are shown in the table below:

[0076]

[0077] Data type conversions, such as CUDA defining the float2 type, need to be converted to floattemp for ATHREAD. This conversion only requires corresponding adjustments to the syntax tree, which is mainly an engineering issue and belongs to existing technology, so it will not be elaborated here.

[0078] In this embodiment, the kernel data reuse and memory optimization algorithm specifically includes:

[0079] To simulate the use of CUDA global memory, this solution designs a shared local memory between slave cores and a mutex lock between slave cores. This shared local memory can be accessed by all slave cores, requiring only one DMA transfer, facilitating data exchange between slave cores and achieving slave-level data reuse. The design of the slave core mutex lock corresponds to CUDA's atomic operation primitives, resolving memory access conflicts. This aligns with the main characteristics of CUDA global memory. In terms of performance, the access latency of the slave core shared memory is only slightly higher than that of the slave core local memory, and it is more efficient than accessing main memory.

[0080] For data used by the CUDA kernel, the ANTLR abstract syntax tree is traversed to identify its usage characteristics, and corresponding memory types are assigned to maximize the optimization of kernel performance. The specific algorithm is as follows: Figure 6 As shown.

[0081] This application also provides a CUDA-C to ATHREAD-C conversion device, which includes a kernel code acquisition module, a mapping model construction module, a preset conversion method generation module, an abstract syntax tree generation module, and a conversion module. The kernel code acquisition module is used to acquire CUDA kernel code; the mapping model construction module is used to construct a hardware mapping model of GPU and Sunway domestic many-core as acceleration units; the preset conversion method generation module is used to generate a preset conversion method based on the hardware mapping model; the abstract syntax tree generation module is used to automatically generate an abstract syntax tree of CUDA code using the ANTLR language translation tool; and the conversion module is used to generate ATHREAD code based on the abstract syntax tree and the preset conversion method.

[0082] In this embodiment, the ATHREAD code includes ATHREAD host code, ATHREAD device code, and ATHREAD general auxiliary code.

[0083] The above description is merely a specific embodiment of this application, but the scope of protection of this application is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the technical scope disclosed in this application should be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.

Claims

1. A method for converting CUDA-C to ATHREAD-C, the method comprising: The CUDA-C to ATHREAD-C conversion method includes: Obtain the CUDA kernel code; Get the preset conversion method; The ANTLR language translation tool automatically generates the abstract syntax tree for CUDA code. ATHREAD code is generated based on the abstract syntax tree and the preset conversion method; The preset conversion method includes: data affine transformation and CUDA code inheritance algorithm; The preset conversion method includes: a kernel data reuse and memory optimization algorithm; The preset conversion method includes: CUDA primitive conversion mapping method; Generating ATHREAD code based on the abstract syntax tree and preset transformation methods includes: The preset transformation method is implanted into the node corresponding to the abstract syntax tree to generate ATHREAD code; Data affine transformation and CUDA code inheritance algorithms include: CUDA manages data through a grid, which consists of three-dimensional thread blocks, and each block is composed of three-dimensional threads. To this end, CUDA provides a series of primitives and uses these primitives to index data on threads, thereby achieving a single-instruction multi-threaded parallel computing model. ATHREAD manages data through multi-level parallelism. Data is first distributed to the Sunway MPE via MPI, and the MPE further distributes the data to the CPE, thus completing multi-level parallel computing. Based on the data allocated to MPE by ATHREAD, the three-dimensional thread block array used by CUDA is proportionally quantized and scaled down to generate a new three-dimensional thread block array cudaGridNew. The corresponding formula is as follows: ; Where cudaGrid is a CUDA three-dimensional thread block array, cudaGridData is the total data of the grid in CUDA, and sunwayMPEData is the data of a single node of the Sunway MPE supercomputer; The three-dimensional thread block in cudaGridNew needs to be less than the number of Godson CPEs. If it does not meet the requirement, conversion is needed. Considering that the minimum organization unit of CUDA programming is based on warp, and a large number of parallel algorithms are also based on the basic unit of warp for operation, from the safety and reliability of parallel algorithms, in warp units, the size of the CUDA three-dimensional thread is adjusted, and the value of cudaGridNew is adjusted until the requirement of less than 64 is met, thereby calculating the required data index array.

2. A CUDA-C to ATHREAD-C conversion device, used in the CUDA-C to ATHREAD-C conversion method as described in claim 1, characterized in that, The CUDA-C to ATHREAD-C conversion device includes: A kernel code acquisition module, which is used to acquire CUDA kernel code; A mapping model construction module is used to construct a hardware mapping model of GPU and Sunway domestic many-core as acceleration units; A preset conversion method generation module is used to generate a preset conversion method based on the hardware mapping model. Abstract syntax tree generation module, which is used to automatically generate abstract syntax trees for CUDA code using the ANTLR language translation tool; A conversion module is used to generate ATHREAD code based on the abstract syntax tree and a preset conversion method.