CUDA simulation method for the neoclassical annular viscosity torque of Tokamak
By decomposing and optimizing the new classic tokamak toroidal viscous torque simulation program through CUDA-based parallel computing technology, the performance bottleneck problem of traditional CPU computing methods in large-scale parallel computing tasks has been solved, and the computing speed and efficiency have been significantly improved.
Patent Information
- Application Number
- CN202410933145.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-12
- Publication Date
- 2024-09-24
- Estimated Expiration
- 2044-07-12
AI Technical Summary
Traditional CPU computing methods have performance bottlenecks when dealing with complex physical problems such as tokamak circumferential viscous torque, and are difficult to meet the high efficiency and high precision requirements of modern nuclear fusion research, especially in large-scale parallel computing tasks.
CUDA-based parallel computing technology is used to decompose the calculation program of Tokamak's new classic circumferential viscous torque simulation into multiple sub-units, identify and optimize high-consuming functions and high-load code segments, and use CUDA kernel functions to map tasks to the GPU In multiple thread blocks, shared memory is used to reduce data access delays and make full use of the parallel processing capabilities of the GPU.
It significantly improves the computing speed and efficiency of tokamak circumferential viscous torque simulation, solves the performance bottleneck problem of traditional CPU computing methods in large-scale parallel computing tasks, and ensures balanced allocation and optimization of computing resources.
Smart Images

Figure CN118502964B
Abstract
Description
Technical Field
[0001] The invention relates to the technical field of tokamak simulation, and in particular to a method for realizing simulation of neoclassical annular viscous torque of a tokamak based on CUDA acceleration. Background Art
[0002] The simulation of the tokamak device can help scientists understand the behavior and control mechanism of plasma through computational simulation. Traditional simulation methods are often limited by heavy computational load and slow processing speed when dealing with complex physical processes such as neoclassical toroidal viscosity torque. With the growth of computing needs, especially when simulating plasma dynamics, it is necessary to process large amounts of data and perform high-intensity calculations, which makes it difficult for traditional CPU computing methods to meet the high efficiency and high precision requirements of modern nuclear fusion research. Therefore, it is particularly urgent to find a new method that can significantly improve the computing performance of tokamak simulation. Driven by this demand, CUDA-based parallel computing technology has become an effective way to solve the above technical problems due to its high efficiency and low latency characteristics when processing large-scale parallel computing tasks.
[0003] In the prior art, simulations of tokamaks usually rely on high-performance computer systems to perform complex mathematical operations and data processing. As part of plasma physics simulations, the neoclassical annular viscosity torque simulation involves a large number of iterative calculations and data exchanges, and multi-core CPUs are usually used to process these computing tasks. These methods can provide stable computing performance when processing large-scale and complex computing tasks, supporting scientists in conducting basic simulation research.
[0004] However, existing CPU calculation methods have certain limitations when dealing with complex physical problems such as the annular viscous torque of a tokamak. Due to the small number of CPU cores and its design being more suitable for serial task processing, the CPU's processing speed is often unsatisfactory when encountering large-scale parallel computing requirements. In addition, with the improvement of simulation accuracy and the increase in the amount of calculation, the CPU's performance deficiency has begun to become a bottleneck restricting the development of nuclear tokamak annular viscous torque simulation research, which urgently requires a new calculation method to improve calculation efficiency and speed.
[0005] The above information disclosed in this Background section is only for enhancement of understanding of the background of the present disclosure and therefore it may contain information that does not constitute the prior art that is already known to one of ordinary skill in the art. Summary of the invention
[0006] The purpose of the present invention is to provide a method for simulating the neoclassical annular viscosity torque of a Tokamak based on CUDA acceleration to solve the problems raised in the above-mentioned background technology.
[0007] To achieve the above object, the present invention provides the following technical solutions:
[0008] A method for simulating the neoclassical annular viscous torque of a tokamak based on CUDA acceleration, the specific steps of which include:
[0009] Step 1: Decompose the original calculation program of the Tokamak neoclassical annular viscous torque simulation into multiple sub-units based on the physical process and calculation tasks and number them, obtain the function call graph and time consumption distribution of each sub-unit, identify the high-consumption functions in each sub-unit whose internal consumption time exceeds the time threshold, and find the slowest sub-unit in the original program;
[0010] Step 2: For the high-consumption function whose internal consumption time exceeds the time threshold in the slowest sub-unit, analyze each line of code of the high-consumption function line by line starting from the function entry of the high-consumption function and segment the code. According to the execution time of each code segment and the proportion of the time consumed by the function, identify the high-load code segment whose execution time exceeds the proportion threshold;
[0011] Step 3: Analyze the loops and function call codes in the high-load code segment, identify the data that has been read more than the read threshold but has not been modified, mark it as reused data, copy the reused data to the CUDA shared memory, modify the original code to read the reused data from the shared memory, and complete the optimization of the high-load code segment;
[0012] Step 4: Decompose the high-load code segment into multiple independent tasks that can be executed in parallel, determine the optimal number of thread blocks and threads, use CUDA kernel functions to map the decomposed independent tasks to different thread blocks, each thread block executes the same number of tasks, and wait for all threads to complete the execution steps;
[0013] Step 5: Use the performance analysis tool to measure the execution time of the slowest subunit before and after optimization to determine whether the optimization effect meets the requirements. If the requirements are met, execute the calculation program of the optimized Tokamak neoclassical annular viscous torque simulation to complete the simulation.
[0014] Furthermore, the subunits divided based on physical processes and computing tasks include: initialization subunit, annular viscosity torque calculation subunit, plasma state update subunit, boundary condition processing subunit, data collection and analysis subunit and output and visualization subunit.
[0015] Furthermore, the method for finding the slowest subunit in the original program is based on:
[0016] Obtain the function call graph and time consumption distribution of each sub-unit, identify the functions in each sub-unit that consume time exceeding the time threshold, and record the usage data of these functions, the usage data including the position of the sub-unit internal function in the call stack, the consumed time and the number of calls, and generate the execution time index of the sub-unit according to the usage data of each sub-unit. The specific logic of generating the execution time index of each sub-unit is as follows:
[0017]
[0018] in, Indicates the number The execution time index of the subunit of Indicates the number The number of functions in the subunits that consume time exceeding the time threshold, and Respectively indicate the number The subunit of The time consumed and the number of calls of the functions that consume time exceeding the time threshold;
[0019] If the function running time inside each sub-unit exceeds the preset time threshold, the function will be judged as a function that consumes time exceeding the time threshold. When identifying the slowest sub-unit, the execution time indexes of each sub-unit are compared, and the sub-unit with the largest execution time index is determined, and this sub-unit is set as the slowest sub-unit.
[0020] Furthermore, the logic for identifying the high-load code segment whose internal execution time of the slowest sub-unit exceeds the proportion threshold is as follows:
[0021] Obtain the execution time data of each code segment of the function. For each code segment in the function, calculate its time proportion, and then identify the high-load code segment. The specific logic is as follows:
[0022] like , then the code segment is judged to be a high-load code segment. If , then the line of code is judged to be a low-load code segment;
[0023] in, For the slowest subunit, The function that takes longer than the time threshold The execution time percentage of the code segment, is the percentage threshold, and , The calculation formula is:
[0024]
[0025] in, For the slowest subunit, The total execution time of the function that consumes more time than the time threshold. The calculation logic is the product of the single execution time and the number of calls. For the slowest subunit, The function that takes longer than the time threshold The total execution time of the code segment.
[0026] Furthermore, the optimization method for high-load code segments is:
[0027] Identify loops and function calls in the program code. For loops, observe the data access patterns within the loop body, looking for data that is read in each iteration but not modified within the loop body.
[0028] For function calls, check the function parameters and global variables used inside the function, identify the data passed as parameters but only read and not modified, and the global variables that are read more than 5 times but not modified in the function;
[0029] Data that is read more than five times but is not modified during the entire loop or function call is marked as reused data. In the CUDA kernel function, a shared memory variable is defined using the "shared" identifier, and the marked reused data is copied from the global memory to the shared memory and executed at the beginning of the loop or function call.
[0030] Furthermore, when determining the optimal number of thread blocks and threads, the number of thread blocks is selected to be a value closest to the maximum thread block size of the GPU, and each thread block is guaranteed to execute the same number of tasks.
[0031] Furthermore, the formula for determining the number of thread blocks is as follows:
[0032]
[0033] in, Represents the total number of independent tasks that can be executed in parallel. Indicates the maximum number of thread blocks supported by the GPU. Indicates the maximum number of threads that can be contained in a thread block. The number of thread blocks representing the final decision.
[0034] Furthermore, the method for judging whether the optimization effect meets the requirements is:
[0035] The performance analysis tool was used to measure the execution time of the slowest subunit before and after optimization, and the execution time was marked as and , the performance improvement ratio is calculated based on the execution time before and after optimization. The logic behind the performance improvement ratio is:
[0036]
[0037] in, Indicates the performance improvement rate, and Respectively represent the execution time of the slowest subunit before and after optimization;
[0038] when When , it is judged that the optimization effect meets the requirements.
[0039] Compared with the prior art, the present invention has the following beneficial effects:
[0040] The present invention effectively solves the problem of computing speed and efficiency by introducing CUDA-based parallel computing technology. First, by decomposing the original computing program into multiple sub-units and identifying the slowest sub-unit and the high-consumption functions inside it, the performance bottleneck can be accurately located, ensuring that subsequent optimization measures can act on the parts that really affect the computing efficiency in a targeted manner; secondly, for the identified high-consumption functions, the present invention further analyzes their code segments, finds out the high-load code segments with long execution time, and copies the reused data to the shared memory of CUDA, reducing the delay of data access, because the access speed of shared memory is much faster than that of global memory, thereby significantly improving the efficiency of data access;
[0041] The present invention decomposes a high-load code segment into multiple independent tasks that can be executed in parallel, and uses CUDA kernel functions to map these tasks to multiple thread blocks of the GPU, making full use of the large number of parallel processing cores of the GPU, greatly improving the computing speed. Each thread block executes the same number of tasks, ensuring balanced distribution of calculations, further optimizing the utilization of computing resources, and comparing the execution time before and after optimization through performance analysis tools to ensure that the optimization effect meets the requirements;
[0042] The present invention significantly improves the calculation speed and efficiency of the tokamak annular viscous torque simulation through precise performance analysis and targeted code optimization, combined with the parallel computing capability of CUDA, and effectively solves the performance bottleneck problem of traditional CPU calculation methods when processing large-scale parallel computing tasks. BRIEF DESCRIPTION OF THE DRAWINGS
[0043] Figure 1 It is a schematic diagram of the overall method flow of the present invention. DETAILED DESCRIPTION
[0044] In order to make the objectives, technical solutions and advantages of the present invention more clearly understood, the present invention is further described in detail below in conjunction with specific embodiments.
[0045] It should be noted that, unless otherwise defined, the technical terms or scientific terms used in the present invention should be understood by people with ordinary skills in the field to which the present invention belongs. The words "first", "second" and similar words used in the present invention do not indicate any order, quantity or importance, but are only used to distinguish different components. "Include" or "comprise" and similar words mean that the elements or objects appearing before the word include the elements or objects listed after the word and their equivalents, without excluding other elements or objects. "Connect" or "connected" and similar words are not limited to physical or mechanical connections, but may include electrical connections, whether direct or indirect. "Up", "down", "left", "right" and the like are only used to indicate relative positional relationships. When the absolute position of the described object changes, the relative positional relationship may also change accordingly.
[0046] Example:
[0047] See also Figure 1 , the present invention provides a technical solution:
[0048] A method for simulating the neoclassical annular viscous torque of a tokamak based on CUDA acceleration, the specific steps of which include:
[0049] Step 1: Decompose the original computational program of the tokamak neoclassical annular viscous torque simulation into multiple sub-units based on the physical process and computational tasks and number them, obtain the function call graph and time consumption distribution of each sub-unit, identify the high-consumption functions in each sub-unit whose internal consumption time exceeds the time threshold, and find the slowest sub-unit in the original program.
[0050] The new classical annular viscous torque simulation in this embodiment is to use numerical simulation to study the characteristics and effects of the new classical transport phenomenon and annular viscous torque of plasma in the tokamak. The parallel computing program refers to a computing program specially designed to handle these complex simulations. It can run in a multi-core or multi-processor parallel computing environment to improve computing efficiency. Parallel computing can share large-scale computing tasks to multiple processors at the same time, thereby greatly shortening the time required for simulation.
[0051] In this embodiment, step 1 provides a systematic and structured analysis method for performance optimization. By decomposing the entire computing program into multiple sub-units and compiling a detailed function call graph and time consumption distribution for each unit, the computing bottleneck can be accurately identified. This method is more sophisticated than the traditional overall performance analysis, and can reveal the performance interaction between different modules and point out which specific function calls are the main obstacles to performance. It can provide deeper insights, so that subsequent optimization work can be more targeted and improve the efficiency and effectiveness of optimization work.
[0052] By identifying the slowest subunits and high-cost functions, resources can be focused on optimizing those parts that contribute the most to performance improvement. This not only allows for the greatest performance improvement within limited time and resources, but also allows for more reliable and stable performance growth throughout the simulation.
[0053] In this embodiment, the original program of the tokamak neoclassical annular viscous torque simulation is decomposed into multiple sub-units. The main purpose is to improve the computing efficiency, especially in a parallel computing environment. This decomposition needs to comprehensively consider the complexity of the physical process, the logic of the computing flow, and the needs of parallel computing. This embodiment divides the original program based on the characteristics of the physical process and the computing task. The divided sub-units include: initialization sub-unit, annular viscous torque calculation sub-unit, plasma state update sub-unit, boundary condition processing sub-unit, data collection and analysis sub-unit and output and visualization sub-unit.
[0054] Furthermore, the initialization subunit is used to set initial conditions, including the initial distribution, temperature, density, and annular viscosity coefficient of the plasma. This subunit mainly processes input data and sets the initial state of the simulation. Initialization is usually performed sequentially, but can also be performed in parallel. The annular viscous torque calculation subunit is used to calculate the neoclassical annular viscous torque according to the current plasma state, which involves solving complex fluid dynamics equations. The calculation of the annular viscous torque is performed independently in different spatial regions and is suitable for parallel processing; the plasma state update subunit is used to update the velocity field, temperature field and density field of the plasma according to the calculated annular viscous torque, and is used to process the nonlinear characteristics and coupling effects of plasma physics.
[0055] The boundary condition processing subunit is used to process the conditions on the boundary of the simulation area, including reflection, absorption or periodic boundary conditions. The boundary conditions are crucial to the physical accuracy of the simulation. Different boundaries can be processed simultaneously, but attention should be paid to data dependency and consistency. The data collection and analysis subunit is used to collect simulation results, perform data analysis and post-processing, including calculating fluid flow characteristics and identifying unstable modes. It is mainly data intensive and may involve a large number of data reading and writing operations. Data analysis can be performed in parallel on data from multiple simulation cycles, but effective management of data flow and storage is required. The output and visualization subunit is used to output the simulation results as charts and visualization forms that can be used in scientific papers or reports. Numerical data needs to be converted into graphics or animations to intuitively display the simulation results. Different data sets can be processed and visualized independently, but the final integration may require serial operations. In this embodiment, each subunit needs to be designed to be independent enough to efficiently utilize parallel computing resources while ensuring the accuracy and stability of the entire simulation process.
[0056] In this embodiment, a performance analysis tool is used to analyze each subunit, obtain a function call graph and time consumption distribution of each subunit, identify functions in each subunit that consume time exceeding a time threshold, and record usage data of these functions, the usage data including the position of the subunit internal function in the call stack, the consumed time, and the number of calls, and generate an execution time index of the subunit according to the usage data of each subunit. The specific logic of generating the execution time index of each subunit is as follows:
[0057]
[0058] in, Indicates the number The execution time index of the subunit of Indicates the number The number of functions in the subunits that consume time exceeding the time threshold, and Respectively indicate the number The subunit of The time consumed and the number of times the function was called that consumed time exceeding the time threshold.
[0059] It reflects the contribution of the functions whose internal time consumption exceeds the time threshold in the subunit to the overall execution time. You can intuitively understand which subunits are performance bottlenecks. The larger the value, the longer the unit takes to complete. Associate the performance impact of this function, It reflects the activity level of the function in the program. The longer the function consumes and the more times it is called, the higher the execution time index of the subunit will be, indicating that the subunit has a greater impact on the overall performance. When the time consumed by the function or the number of times the function is called increases, the execution time index will also increase.
[0060] when When it is 1 (that is, the function is called only once), its exponential term is 0. For a function that is called only once, its execution time will be counted as the original value, but as the number of calls increases, this exponential term will increase, reflecting the superlinear effect of the number of calls on performance. The use of exponential functions highlights the nonlinear effect of the number of calls on performance, especially for frequently called functions.
[0061] Furthermore, if the time consumed for the function within each sub-unit to run exceeds a preset time threshold, the function will be judged as a function that consumes time exceeding the time threshold. When identifying the slowest sub-unit, the execution time indexes of each sub-unit will be compared, and the sub-unit with the largest execution time index will be determined, and this sub-unit will be set as the slowest sub-unit.
[0062] Step 2: For high-consumption functions whose internal consumption time exceeds the time threshold of the slowest sub-unit, analyze each line of code of the high-consumption function line by line starting from the function entry of the high-consumption function and segment the code. According to the ratio of the execution time of each code segment and the time consumed by the function, identify the high-load code segment whose execution time exceeds the ratio threshold.
[0063] In this embodiment, the execution time data of each code segment of the function is obtained, and for each code segment in the function, its time proportion is calculated, and then the high-load code segment is identified. The specific logic is as follows:
[0064] like , then the code segment is judged to be a high-load code segment. If , then the line of code is judged to be a low-load code segment;
[0065] in, For the slowest subunit, The function that takes longer than the time threshold The execution time percentage of the code segment, is the percentage threshold, and , The calculation formula is:
[0066]
[0067] in, For the slowest subunit, The total execution time of the function that consumes more time than the time threshold. The calculation logic is the product of the single execution time and the number of calls. For the slowest subunit, The function that takes longer than the time threshold The total execution time of the code segment. High-load code segments mean that these code segments take up more time in the entire function execution and are the key points for optimization.
[0068] By identifying and optimizing code segments that take up a high percentage of execution time, the execution efficiency of the entire function and even the entire program can be effectively improved, providing a clear direction for performance optimization, that is, to concentrate resources and efforts on optimizing those code segments that can most affect performance, rather than blindly trying to optimize all the code.
[0069] In this embodiment, the specific operation of segmenting the code is: identifying logically independent code blocks, which correspond to code segments that complete specific tasks, such as initialization, data processing, error handling, etc., decomposing the code into logically independent modules, each module is responsible for a specific function or task, and physically dividing the code into multiple parts according to the identified logical boundaries to achieve physical segmentation of the code, each part corresponds to a logical module, and each part of the code is regarded as a code segment.
[0070] Through physical segmentation, each logical module can be independently analyzed and optimized. In this way, performance bottlenecks can be identified more accurately and optimized for specific code segments instead of blindly adjusting the entire program. This method can more effectively improve the execution efficiency of the program. By identifying and optimizing high-load code segments in independent modules, the execution efficiency of the overall solution can be more effectively improved.
[0071] Step 3: Analyze the loops and function call codes in the high-load code segment, identify the data that has been read more than the read threshold but has not been modified, mark it as reused data, copy the reused data to the CUDA shared memory, modify the original code so that it reads the reused data from the shared memory, and complete the optimization of the high-load code segment.
[0072] In this embodiment, a performance analysis tool (such as NVIDIA Nsight Compute) is used to run the program, determine the loops and function call codes in the program code, and for the loops, observe the data access patterns in the loop body to find data that is read in each iteration but not modified in the loop body.
[0073] For function calls, check the function parameters and global variables used inside the function, identify the data passed as parameters but only read and not modified, and the global variables that are read more than 5 times but not modified within the function.
[0074] Data that is read more than five times but is not modified during the entire loop or function call is marked as reused data. In the CUDA kernel function, a shared memory variable is defined using the "shared" identifier, and the marked reused data is copied from the global memory to the shared memory and executed at the beginning of the loop or function call.
[0075] In the code segment, all operations that originally read reused data from global memory are changed to read from shared memory. This can reduce the number of global memory accesses, thereby reducing latency and improving bandwidth utilization.
[0076] Step 4: Decompose the high-load code segment into multiple independent tasks that can be executed in parallel, determine the optimal number of thread blocks and threads, use CUDA kernel functions to map the decomposed independent tasks to different thread blocks, each thread block executes the same number of tasks, and wait for all threads to complete the execution steps.
[0077] In this embodiment, when a high-load code segment is decomposed into multiple independent tasks that can be executed in parallel, the tasks that can be processed independently in the high-load code segment are found. If the tasks are independent, then the related processing can be executed in parallel, and the corresponding tasks can be decomposed into independent tasks. There is no data dependency between the independent tasks. Independently processed tasks mean that processing this part of the task does not need to wait for the results of other parts. For example, if the code segment operates on a large array, the array can be divided into multiple sub-arrays, and the operation of each sub-array is treated as an independent task.
[0078] When using CUDA for parallelization, you need to determine the number of thread blocks and threads. A thread block can contain multiple threads, and multiple thread blocks can be executed in parallel. The number of thread blocks is usually limited by the hardware characteristics of the GPU, such as the grid size.
[0079] In this embodiment, the number of thread blocks is selected to be a value closest to the maximum thread block size of the GPU, and each thread block is guaranteed to execute the same number of tasks to fully utilize the parallel processing capability of the GPU. Selecting each thread block to execute the same number of tasks can simplify the task allocation and synchronization mechanism while ensuring load balancing.
[0080] Furthermore, the thread block number decision formula is as follows:
[0081]
[0082] in, Represents the total number of independent tasks that can be executed in parallel. Indicates the maximum number of thread blocks supported by the GPU. Indicates the maximum number of threads that can be contained in a thread block. The number of thread blocks representing the final decision.
[0083] When using CUDA kernel functions to map tasks to thread blocks, the constructed kernel function allocates and executes independent tasks to each thread in the CUDA environment to fully utilize the parallel processing capabilities of the GPU. The specific logic is:
[0084] A CUDA kernel function named "process Tasks" is defined. This function is used to process each task in the passed data array. This function accepts two parameters: one is the pointer "data" pointing to the data array, and the other is "num Tasks" indicating the number of tasks in the data array.
[0085] Inside the kernel function, the unique index "idx" of each thread in the global task list is calculated by combining the index of the thread block the current thread is in "blockIdx.x", the number of threads in each thread block "blockDim.x", and the index of the thread in its thread block "threadIdx.x", according to the formula:
[0086]
[0087] The global index "idx" is used to determine the specific task that each thread should handle.
[0088] Check if the global index is within the range of the number of tasks. The corresponding thread will execute the task only if "idx" is less than the total number of tasks "num Tasks". This conditional check ensures that even if the number of threads assigned to the GPU exceeds the actual number of tasks, it will not cause out-of-bounds access or unnecessary calculations.
[0089] For each thread, if its corresponding task index is within the allowed range, it will execute the 'process Data' function to process its assigned task. The "process Data" function here is a schematic function name, representing the specific operation of processing data, and its implementation details will depend on the specific application.
[0090] After all kernel functions are executed, call the "cuda Device Synchronize()" function to synchronize all GPU operations to ensure that all data processing is completed correctly.
[0091] In this embodiment, the operation example program of the kernel function is:
[0092] __global__ void processTasks(DataType *data, int numTasks) {
[0093] int idx = blockIdx.x * blockDim.x + threadIdx.x;
[0094] if (idx < numTasks) {
[0095] processData(data[idx]);
[0096] }
[0097] }
[0098] This embodiment provides a specific mathematical model for determining the number of thread blocks and threads, and how to dynamically adjust the number of thread blocks and threads according to the number of tasks. This method can more effectively utilize GPU resources, reduce thread management overhead, improve the efficiency of parallel computing, and simplify the task allocation and management process.
[0099] In this way, the kernel function assigns an independent thread to each task. This parallel processing mode allows the GPU to process multiple tasks simultaneously, greatly improving computing efficiency.
[0100] Step 5: Use the performance analysis tool to measure the execution time of the slowest subunit before and after optimization to determine whether the optimization effect meets the requirements. If the requirements are met, execute the calculation program of the optimized Tokamak neoclassical annular viscous torque simulation to complete the simulation.
[0101] In this embodiment, NVIDIA Nsight Compute is selected as the performance analysis tool to measure the execution time of the slowest subunit before and after optimization, and the optimization effect is evaluated by comparing the execution time before and after optimization. If the execution time after optimization is significantly reduced, it indicates that the optimization is successful.
[0102] After confirming that the optimization effect of the slowest subunit meets the requirements, it is integrated back into the overall program, and the complete program is run in an actual physical simulation environment to execute the complete calculation program of the optimized tokamak neoclassical annular viscous torque simulation to ensure that the overall program can run normally and obtain the expected physical simulation results.
[0103] In this embodiment, the method for judging whether the optimization effect meets the requirements is:
[0104] The performance analysis tool was used to measure the execution time of the slowest subunit before and after optimization, and the execution time was marked as and , the performance improvement ratio is calculated based on the execution time before and after optimization. The logic behind the performance improvement ratio is:
[0105]
[0106] in, Indicates the performance improvement rate, and Respectively represent the execution time of the slowest subunit before and after optimization;
[0107] when When , it is judged that the optimization effect meets the requirements.
[0108] Through the above steps, it can be ensured that the optimized slowest sub-unit not only has a significant performance improvement, but also the accuracy and performance of the overall simulation are guaranteed and improved after being integrated back into the full program.
[0109] In this embodiment, firstly, by decomposing the original computing program into multiple sub-units and identifying the slowest sub-unit and the high-consumption functions inside it, the performance bottleneck can be accurately located, ensuring that subsequent optimization measures can act on the parts that really affect the computing efficiency in a targeted manner; secondly, for the identified high-consumption functions, the present invention further analyzes their code segments, finds out the high-load code segments with long execution time, and copies the reused data to the shared memory of CUDA, reducing the delay of data access, because the access speed of shared memory is much faster than that of global memory, thereby significantly improving the efficiency of data access.
[0110] The high-load code segment is decomposed into multiple independent tasks that can be executed in parallel, and these tasks are mapped to multiple thread blocks of the GPU using CUDA kernel functions, making full use of the large number of parallel processing cores of the GPU, greatly improving the computing speed. Each thread block executes the same number of tasks, ensuring the balanced distribution of calculations and further optimizing the utilization of computing resources. The execution time before and after optimization is compared through performance analysis tools to ensure that the optimization effect meets the requirements.
[0111] Through precise performance analysis and targeted code optimization, combined with the parallel computing capabilities of CUDA, the calculation speed and efficiency of the tokamak annular viscosity torque simulation have been significantly improved, effectively solving the performance bottleneck problem of traditional CPU calculation methods when processing large-scale parallel computing tasks.
[0112] The above formulas are all dimensionless and numerical calculations. The formula is a formula for the most recent real situation obtained by collecting a large amount of data and performing software simulation. The preset parameters in the formula are set by technicians in this field according to actual conditions.
[0113] The above embodiments may be implemented in whole or in part by software, hardware, firmware or any other combination thereof. When implemented by software, the above embodiments may be implemented in whole or in part in the form of a computer program product. Those skilled in the art may appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein may be implemented by electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed by hardware or software methods depends on the specific application and design constraints of the technical solution.
[0114] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, and may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0115] The above description is only a specific implementation manner 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 included in the protection scope of the present application.
Claims
1. A method for simulating the neoclassical annular viscous torque of a Tokamak based on CUDA acceleration, characterized in that: The specific steps include: Step 1: Decompose the original calculation program of the Tokamak neoclassical annular viscous torque simulation into multiple sub-units based on the physical process and calculation tasks and number them, obtain the function call graph and time consumption distribution of each sub-unit, identify the high-consumption functions in each sub-unit whose internal consumption time exceeds the time threshold, and find the slowest sub-unit in the original program; Step 2: For the high-consumption function whose internal consumption time exceeds the time threshold in the slowest sub-unit, analyze each line of code of the high-consumption function line by line starting from the function entry of the high-consumption function and segment the code. According to the execution time of each code segment and the proportion of the time consumed by the function, identify the high-load code segment whose execution time exceeds the proportion threshold; Step 3: Analyze the loops and function call codes in the high-load code segment, identify the data that has been read more than the read threshold but has not been modified, mark it as reused data, copy the reused data to the CUDA shared memory, modify the original code to read the reused data from the shared memory, and complete the optimization of the high-load code segment; Step 4: Decompose the high-load code segment into multiple independent tasks that can be executed in parallel, determine the optimal number of thread blocks and threads, use CUDA kernel functions to map the decomposed independent tasks to different thread blocks, each thread block executes the same number of tasks, and wait for all threads to complete the execution steps; Step 5: Use the performance analysis tool to measure the execution time of the slowest subunit before and after optimization to determine whether the optimization effect meets the requirements. If the requirements are met, execute the calculation program of the optimized Tokamak neoclassical annular viscous torque simulation to complete the simulation; When determining the optimal number of thread blocks and threads, the number of thread blocks is selected to be a value that is closest to the maximum thread block size of the GPU, and each thread block is guaranteed to execute the same number of tasks; The formula for determining the number of thread blocks is as follows: in, Represents the total number of decomposed independent tasks that can be executed in parallel, Indicates the maximum number of thread blocks supported by the GPU. Indicates the maximum number of threads that can be contained in a thread block. The number of thread blocks representing the final decision.
2. The method for simulating the new classical annular viscosity torque of a Tokamak based on CUDA acceleration according to claim 1 is characterized in that: The subunits divided based on physical processes and computing tasks include: initialization subunit, annular viscosity torque calculation subunit, plasma state update subunit, boundary condition processing subunit, data collection and analysis subunit, and output and visualization subunit.
3. The method for simulating the new classical annular viscous torque of a Tokamak based on CUDA acceleration according to claim 1 is characterized in that: The method for finding the slowest subunit in the original program is based on: Obtain the function call graph and time consumption distribution of each sub-unit, identify the functions in each sub-unit that consume time exceeding the time threshold, and record the usage data of these functions, the usage data including the position of the sub-unit internal function in the call stack, the consumed time and the number of calls, and generate the execution time index of the sub-unit according to the usage data of each sub-unit. The specific logic of generating the execution time index of each sub-unit is as follows: in, Indicates the number The execution time index of the subunit of Indicates the number The number of functions in the subunits that consume time exceeding the time threshold, and Respectively indicate the number The subunit of The time consumed and the number of calls of the functions that consume time exceeding the time threshold; If the function running time inside each sub-unit exceeds the preset time threshold, the function will be judged as a function that consumes time exceeding the time threshold. When identifying the slowest sub-unit, the execution time indexes of each sub-unit are compared, and the sub-unit with the largest execution time index is determined, and this sub-unit is set as the slowest sub-unit.
4. The method for simulating the neoclassical annular viscosity torque of a Tokamak based on CUDA acceleration according to claim 1 is characterized in that: The logic for identifying the high-load code segments whose internal execution time of the slowest sub-unit exceeds the proportion threshold is as follows: Obtain the execution time data of each code segment of the function. For each code segment in the function, calculate its time proportion, and then identify the high-load code segment. The specific logic is as follows: like , then the code segment is judged to be a high-load code segment. If , then the line of code is judged to be a low-load code segment; in, For the slowest subunit, The function that takes longer than the time threshold The execution time percentage of the code segment, is the percentage threshold, and , The calculation formula is: in, For the slowest subunit, The total execution time of the functions that consume time exceeding the time threshold is calculated by multiplying the single execution time by the number of calls. For the slowest subunit, The function that takes longer than the time threshold The total execution time of the code segment.
5. The method for simulating the new classical annular viscous torque of a Tokamak based on CUDA acceleration according to claim 4 is characterized in that: The optimization method for high-load code segments is: Identify loops and function calls in the program code. For loops, observe the data access patterns within the loop body, looking for data that is read in each iteration but not modified within the loop body. For function calls, check the function parameters and global variables used inside the function, identify the data passed as parameters but only read and not modified, and the global variables that are read more than 5 times but not modified in the function; Data that is read more than five times but is not modified during the entire loop or function call is marked as reused data. In the CUDA kernel function, a shared memory variable is defined using the "shared" identifier, and the marked reused data is copied from the global memory to the shared memory and executed at the beginning of the loop or function call.
6. The method for simulating the neoclassical annular viscosity torque of a Tokamak based on CUDA acceleration according to claim 1 is characterized in that: The method to determine whether the optimization effect meets the requirements is: The performance analysis tool was used to measure the execution time of the slowest subunit before and after optimization, and the execution time was marked as and , the performance improvement ratio is calculated based on the execution time before and after optimization. The logic behind the performance improvement ratio is: in, Indicates the performance improvement rate, and Respectively represent the execution time of the slowest subunit before and after optimization; when When , it is judged that the optimization effect meets the requirements.
