A high-level integrated dynamic memory management method and device based on Cartesian tree

By adopting a dynamic memory management method based on Cartesian tree in FPGA accelerator, the problem of insufficient dynamic memory management in the existing technology is solved, efficient and flexible memory allocation and recycling are achieved, and the performance and development efficiency of FPGA accelerator are improved.

CN118550714BActive Publication Date: 2024-11-05ZHEJIANG LAB
View PDF 9 Cites 0 Cited by

Patent Information

Application Number
CN202411020646.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-07-29
Publication Date
2024-11-05
Estimated Expiration
2044-07-29

AI Technical Summary

Technical Problem

Existing HLS tools have insufficient efficiency, flexibility and robustness in dynamic memory management in FPGA accelerators, resulting in low memory resource utilization, large time delay, and excessive computing resource utilization, limiting the design and development of FPGA hardware accelerators.

Method used

Adopt a dynamic memory management method based on Cartesian tree, represent free memory blocks through nodes of Cartesian tree, divide and fusion operations using memory management requests, and pass three strategies (worst allocation, best matching, and better matching) Find target nodes, combine the degree of memory fragmentation and application space size selection strategy to achieve efficient and flexible memory allocation and recycling.

Benefits of technology

It improves the development efficiency of high-level comprehensive languages, realizes efficient and flexible dynamic memory management, improves the memory allocation mode of FPGA accelerator, and lowers the development threshold.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118550714B_ABST
    Figure CN118550714B_ABST
Patent Text Reader

Abstract

The present invention discloses a high-level integrated dynamic memory management method and device based on a Cartesian tree, comprising: dynamic memory management based on a Cartesian tree, wherein the nodes of the Cartesian tree correspond to free memory blocks, represented by [addr, size], wherein addr represents the address of the memory block, and size represents the size of the memory block. According to the memory management request, the target node, i.e., the target free memory block, is found on the Cartesian tree, and the target free memory block is divided or merged; after the operation is completed, the position of the nodes of the Cartesian tree is adjusted to meet the constraints of the Cartesian tree. The present invention utilizes the advantage of the Cartesian tree that both attributes are ordered, realizes efficient and flexible dynamic memory allocation and recovery, and improves the development efficiency of the high-level integrated language.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of high-level synthesis, and in particular to a high-level synthesis dynamic memory management method and device based on a Cartesian tree. Background Art

[0002] High-level synthesis (HLS) based on high-level languages ​​has brought opportunities for custom computing acceleration systems based on software architectures such as CPU (Central Processing Unit) to use FPGA (Field-Programmable Gate Array) for acceleration. At the same time, the easy-to-program feature of HLS has also accelerated the development of various accelerators. However, since various calculations involve a large number of dynamic data structures, there is a natural demand for dynamic memory management. However, since HLS only partially supports high-level languages, only a subset of high-level languages ​​can be synthesized, and dynamic memory management operations are not supported. This will result in many high-level language algorithms with good performance being unable to be directly accelerated by FPGA using HLS, and can only be manually reconstructed, rewritten and deleted through a large number of manual operations such as malloc() / free(). This not only severely limits the setting and deployment of existing results to the FPGA platform, but also sets a threshold for many researchers who understand various computing processes and optimization methods but are not familiar with the details of FPGA.

[0003] Existing HLS tools and HLS-oriented dynamic memory research cannot meet the needs of various accelerators in terms of memory allocation. The current solutions mainly include static memory allocation, dynamic memory management based on bitmaps, dynamic memory management based on sibling trees, and dynamic memory management based on linked lists.

[0004] Current FPGA accelerators use static memory allocation to handle memory management, resulting in extremely low robustness. First, static memory calls force programmers to estimate memory resource usage according to the worst case scenario, which greatly reduces memory resource utilization. At the same time, since FPGA accelerator cards are often deployed in data centers for a long time, memory usage is often difficult to estimate, which will cause memory errors at runtime and reduce robustness. Therefore, the static memory allocation method is not an efficient memory management method.

[0005] The allocation efficiency of the bitmap-based solution is extremely low and cannot meet the required efficiency requirements. Since a search algorithm with a time complexity of O(N) must be executed, and in the worst case, the entire bitmap must be searched, the search efficiency is very low, which will cause unacceptable accelerator pauses.

[0006] The flexibility and robustness of the solution based on the sibling tree are poor. First of all, although the time delay is greatly reduced based on bit vector operations, the logical resources consumed by bit vector operations will double as the management space doubles. When managing DRAM (Dynamic Random Access Memory) scale memory space, too many logical resources will be occupied and the computing resources of the accelerator will be squeezed, so the scalability is low. In addition, since the memory size must be allocated exponentially times 2, a large amount of memory space is wasted, which greatly reduces the robustness of the memory system.

[0007] The linked list-based solution also causes a high time delay due to the time complexity problem. Although the memory block-based representation reduces the total amount of search compared to the bitmap, it still cannot alleviate the order of magnitude disadvantage caused by the time complexity.

[0008] In summary, existing solutions cannot provide the memory allocation mode with high efficiency, flexibility and robustness required by FPGA accelerators. All solutions only achieve good results in one or two of the three. The lack of dynamic memory management greatly limits the design of FPGA hardware accelerators and sets a development threshold for researchers. Therefore, a method is urgently needed in high-level synthesis to achieve efficient, flexible and robust dynamic memory management to help complete the development of FPGA accelerators.

[0009] In summary, the present invention aims to provide a device and an operation method for high-performance, flexible and robust dynamic management of memory in high-level synthesis.

[0010] In addition, on the one hand, there are differences in understanding among those skilled in the art; on the other hand, the inventor studied a large number of documents and patents when making the present invention, but due to space limitations, not all details and contents are listed in detail. However, this does not mean that the present invention does not have the characteristics of these prior arts. On the contrary, the present invention already has all the characteristics of the prior art, and the applicant reserves the right to add relevant prior art to the background technology. Summary of the invention

[0011] The purpose of the present invention is to provide a high-level integrated dynamic memory management method and device based on Cartesian trees in view of the deficiencies in the prior art.

[0012] The object of the present invention is achieved through the following technical solution: a high-level integrated dynamic memory management method based on Cartesian trees, comprising:

[0013] Dynamic memory management is performed based on a Cartesian tree, where the nodes of the Cartesian tree correspond to free memory blocks and are represented by [addr, size], where addr represents the address of the memory block and size represents the size of the memory block.

[0014] According to the memory management request, the target node, that is, the target free memory block, is found on the Cartesian tree, and the target free memory block is divided or merged; after the operation is completed, the position of the Cartesian tree node is adjusted to meet the constraints of the Cartesian tree.

[0015] Furthermore, the memory management request includes a memory allocation request and a memory reclaim request;

[0016] If the memory management request is a memory allocation request, a partition operation is performed on the target free memory block;

[0017] If the memory management request is a memory reclaim request, a fusion operation is performed on the target free memory block.

[0018] Further, the step of finding the target node on the Cartesian tree includes:

[0019] If the memory management request is a memory allocation request, traverse from the root node of the Cartesian tree to find a node that meets the memory allocation requirement, which is the target node;

[0020] The dividing operation on the target free memory block includes: subtracting the memory allocation requirement size from the size value of the target node.

[0021] Furthermore, nodes that meet the memory allocation requirements are found through any one or more of the following three strategies:

[0022] The first strategy: If the root node meets the memory allocation requirements, directly use the root node as the target node;

[0023] Second strategy: Find a node with the smallest size that meets the memory allocation requirement; the finding of a node with the smallest size that meets the memory allocation requirement includes:

[0024] Start traversing from the root node of the Cartesian tree, use the root node as the search variable n, and determine whether the size of the current node is greater than the required memory block size. If the size of the current node is less than the required memory block size, the parent node of the current node will be used as the target node; if the current node is equal to the required memory block size, the current node is used as the target node; if the size of the current node is greater than the required memory block size, search for the left and right child nodes of the current node. If the sizes of both child nodes are less than the required memory block size, the current node is used as the target node; if the size of a child node is equal to the required memory block size, the child node is used as the target node; if only one of the child nodes has a size greater than the required memory block size, the larger child node is selected as the new loop variable n in the next loop until the target node is found; if the sizes of both child nodes are greater than the required memory block size, both child nodes are used as the new loop variable n in the next loop until the target node is found, and the two child nodes are taken as the search variables. The target node with the smallest size found by each of them is taken as the final target node.

[0025] The third strategy: start traversing from the root node of the Cartesian tree, use the root node as the search variable n, determine whether the size of the current node is greater than the required memory block size, if the size of the current node is less than the required memory block size, the parent node of the current node will be used as the target node; if the current node is equal to the required memory block size, the current node is used as the target node; if the size of the current node is greater than the required memory block size, search for the left and right child nodes of the current node, if the sizes of both child nodes are less than the required memory block size, the current node is used as the target node; if there is a child node whose size is equal to the required memory block size, the child node is used as the target node; if only one of the child nodes has a size greater than the required memory block size, select the larger child node as the new loop variable n in the next loop until the target node is found; if the sizes of both child nodes are greater than the required memory block size, select the smaller child node as the new loop variable n in the next loop until the target node is found.

[0026] Furthermore, it also includes a dynamic memory matching strategy selection mechanism;

[0027] The dynamic memory matching strategy selection mechanism includes: using the current heap memory fragmentation degree and the size of the memory allocation application space as measurement indicators to determine the strategy to be selected; the fragmentation degree is calculated by the following formula:

[0028]

[0029] in, It is the size of a free memory block in the heap memory. is the number of nodes in the Cartesian tree;

[0030] If the required memory block size is greater than m times of Frag, the second strategy is sampled, m>1; if the required memory block size is less than Frag, the first strategy is adopted; if the required memory block size is between 1 and m times of the Frag value, the third strategy is adopted.

[0031] Further, the step of finding the target node on the Cartesian tree includes:

[0032] If the memory management request is a memory reclaim request, traverse from the root node of the Cartesian tree to find a node adjacent to the released memory block;

[0033] The judgment logic of the node adjacent to the released memory block is: if the sum of the addr value and the size value of the current node is equal to the addr value of the released memory block, the node is marked as pre; if the sum of the addr value and the size value of the released memory block is equal to the addr value of the current node, the node is marked as suc; both the pre node and the suc node are target nodes;

[0034] The fusion operation on the target free memory block includes: if both the pre node and the suc node are found, the size value of the pre node is updated, the suc node is deleted, and the size value of the updated pre node is the sum of the size value of the original pre node, the size value of the suc node and the size value of the released memory block, and the addr value remains unchanged;

[0035] If only one target node is found, that is, one of the pre node and the suc node, the target node is updated, and the size value of the updated target node is the sum of the size value of the original target node and the size value of the released memory block; if the target node is the pre node, the addr value remains unchanged; if the target node is the pre node, the addr value is updated to the original addr value minus the size value of the released memory block;

[0036] If neither the pre node nor the suc node is found, a new node is created based on the addr value and size value of the released memory block.

[0037] Furthermore, the constraints of the Cartesian tree include: the size attribute is ordered by layer and is non-increasing or non-decreasing, and the addr attribute is ordered by sequence and is increasing or decreasing.

[0038] The present invention also provides a high-level integrated dynamic memory management device based on Cartesian trees, including off-chip storage, bus interface, FPGA accelerator, data loader, on-chip cache, and dynamic memory manager;

[0039] The off-chip storage is used as extended storage when the BRAM storage resources on the FPGA are insufficient, and is used to store the structure and node information of the Cartesian tree;

[0040] The bus interface is used to connect the accelerator and the dynamic memory manager;

[0041] The FPGA accelerator is used to issue a memory management request;

[0042] The dynamic memory manager is used to accept memory management requests and execute the above method;

[0043] The data loader is used to read the Cartesian tree nodes stored in the off-chip storage into the on-chip cache and write the updated Cartesian tree nodes into the off-chip storage;

[0044] The on-chip cache is used to temporarily store the Cartesian tree nodes written by the data loader and provide them to the dynamic memory manager.

[0045] Further, the off-chip storage is divided into a static storage area, a dynamic storage area and a Cartesian tree storage area;

[0046] The static storage area is used to store information that will not be released after being written.

[0047] The dynamic storage area is a dynamically managed memory area;

[0048] The Cartesian tree storage area stores Cartesian tree structure information.

[0049] Further, the dynamic memory manager includes registers, a memory allocator, a memory reclaimer and an allocation cache;

[0050] The register is used to temporarily store memory management requests;

[0051] The allocation buffer is used to temporarily store memory management requests to be processed;

[0052] The memory allocator is used to execute the memory allocation request and maintain the Cartesian tree structure after giving the request result;

[0053] The memory reclaimer is used to execute the memory reclaim request and maintain the Cartesian tree structure after giving the request result;

[0054] The registers include an allocation request register and a recycling request register;

[0055] The allocation request register is used to temporarily store memory allocation requests;

[0056] The recycling request register is used to temporarily store memory recycling requests;

[0057] The dynamic memory manager also includes a memory matching strategy selection module; the memory matching strategy selection module is used to determine the strategy to be selected according to the degree of memory fragmentation and the requested free size.

[0058] The beneficial effects of the present invention are as follows: the present invention performs dynamic memory management based on the Cartesian tree, utilizes the advantage that the Cartesian tree has an order for both attributes, realizes efficient and flexible dynamic memory allocation and recycling, and improves the development efficiency of high-level integrated languages. BRIEF DESCRIPTION OF THE DRAWINGS

[0059] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the accompanying drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings in the following description are only some embodiments of the present invention. For ordinary technicians in this field, other accompanying drawings can be obtained based on these accompanying drawings without paying creative labor.

[0060] Figure 1 It is a schematic diagram of the hardware structure of the preferred Cartesian tree-based high-level integrated dynamic memory management device of the present invention;

[0061] Figure 2 It is a schematic diagram of a search module in a preferred memory allocator of the present invention;

[0062] Figure 3 It is a schematic diagram of a maintenance module in a preferred memory allocator of the present invention;

[0063] Figure 4 It is a schematic diagram of a search module in a preferred memory recycler of the present invention;

[0064] Figure 5 It is a schematic diagram of a maintenance module in a preferred memory recycler of the present invention;

[0065] Figure 6 It is a schematic diagram of the preferred free memory block mapping of the present invention;

[0066] Figure 7 It is a schematic diagram of a preferred Cartesian tree logic structure of the present invention;

[0067] List of reference numerals:

[0068] 1: off-chip storage; 2: bus; 3: FGPA accelerator; 4: data loader; 5: on-chip cache; 6: dynamic memory manager; 7: allocation request register; 8: recycling request register; 9: memory matching strategy selection; 10: allocation cache; 11: memory allocator; 12: memory reclaimer; 13: static storage area; 14: dynamic storage area; 15: Cartesian tree storage area. DETAILED DESCRIPTION

[0069] In order to make the purpose, technical scheme and advantages of the embodiments of the present application clearer, the technical scheme in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, rather than all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in the field without creative work are within the scope of protection of this application. In the absence of conflict, the features in the following embodiments and implementations can be combined with each other.

[0070] The current high-level synthesis does not support dynamic memory management operations, which will result in many high-level language algorithms with good performance being unable to be directly accelerated by FPGA using HLS. This not only severely limits the setting and deployment of existing achievements to the FPGA platform, but also sets a threshold for many researchers who understand various computing processes and optimization methods but are unfamiliar with the details of FPGA. However, the current several solutions cannot provide a memory allocation mode with the efficiency, flexibility and robustness required by FPGA accelerators. All solutions have only made good implementations for one or two of the three. The lack of dynamic memory management greatly limits the design of FPGA hardware accelerators and sets a development threshold for researchers. Therefore, the present invention provides a high-level synthesis dynamic memory management device and method based on Cartesian trees, aiming to provide efficient and flexible dynamic memory management.

[0071] A high-level integrated dynamic memory management method based on a Cartesian tree of the present invention comprises:

[0072] Dynamic memory management is performed based on a Cartesian tree, where the nodes of the Cartesian tree correspond to free memory blocks and are represented by [addr, size], where addr represents the address of the memory block and size represents the size of the memory block.

[0073] According to the memory management request, the target node, i.e., the target free memory block, is found on the Cartesian tree, and the target free memory block is divided or merged; after the operation is completed, the position of the nodes of the Cartesian tree is adjusted to meet the constraints of the Cartesian tree. The memory management request includes a memory allocation request and a memory recovery request; if the memory management request is a memory allocation request, the target free memory block is divided; if the memory management request is a memory recovery request, the target free memory block is merged.

[0074] If the memory management request is a memory allocation request, traverse from the root node of the Cartesian tree to find a node that meets the memory allocation requirement, which is the target node;

[0075] Use any of the following three strategies to find a node that meets the memory allocation requirements:

[0076] The first strategy (worst allocation strategy): Since the Cartesian tree is organized in a decreasing y-axis (i.e., size) manner, the root node stores the information of the largest memory block, and the search result can be obtained immediately from the root node, and the memory block pointed to by the root node is allocated. Specifically, if the root node meets the memory allocation requirements, the root node is directly used as the target node.

[0077] The second strategy (best matching strategy): requires a breadth-first traversal, but since it is impossible to determine which node to start the division from, the search process requires traversing the nodes;

[0078] That is, a node with a minimum size that meets the memory allocation requirement is found; the node with a minimum size that meets the memory allocation requirement is found including:

[0079] Start traversing from the root node of the Cartesian tree, use the root node as the search variable n, and determine whether the size of the current node is greater than the required memory block size. If the size of the current node is less than the required memory block size, the parent node of the current node will be used as the target node; if the current node is equal to the required memory block size, the current node is used as the target node; if the size of the current node is greater than the required memory block size, search for the left and right child nodes of the current node. If the sizes of both child nodes are less than the required memory block size, the current node is used as the target node; if the size of a child node is equal to the required memory block size, the child node is used as the target node; if only one of the child nodes has a size greater than the required memory block size, the larger child node is selected as the new loop variable n in the next loop until the target node is found; if the sizes of both child nodes are greater than the required memory block size, both child nodes are used as the new loop variable n in the next loop until the target node is found, and the two child nodes are taken as the search variables. The target node with the smallest size found by each of them is taken as the final target node.

[0080] The third strategy (better matching strategy): This is a greedy algorithm that recursively selects the currently traversed node and its two child nodes to make the result locally optimal. When the node size can meet the allocation requirements, read the two child nodes of the node. If there are child nodes that can meet the requirements, select the child node with the smallest size that meets the conditions to perform the next round of search. If neither of the two child nodes meets the requirements, the current node is the node to be searched.

[0081] Specifically, traverse from the root node of the Cartesian tree, use the root node as the search variable n, determine whether the size of the current node is greater than the required memory block size, if the size of the current node is less than the required memory block size, the parent node of the current node will be used as the target node; if the current node is equal to the required memory block size, the current node is used as the target node; if the size of the current node is greater than the required memory block size, search for the left and right child nodes of the current node, if the sizes of the two child nodes are both less than the required memory block size, the current node is used as the target node; if there is a child node whose size is equal to the required memory block size, the child node is used as the target node; if only one of the child nodes has a size greater than the required memory block size, select the larger child node as the new loop variable n in the next loop until the target node is found; if the sizes of the two child nodes are both greater than the required memory block size, select the smaller child node as the new loop variable n in the next loop until the target node is found.

[0082] In one embodiment, it also includes a dynamic memory matching strategy selection mechanism;

[0083] The dynamic memory matching strategy selection mechanism includes: using the current heap memory fragmentation degree and the size of the memory allocation application space as measurement indicators to determine the strategy to be selected; the fragmentation degree is calculated by the following formula:

[0084]

[0085] in, It is the size of a free memory block in the heap memory. is the number of nodes in the Cartesian tree; this formula shows the average size of all free memory blocks in the heap memory. The smaller the average size of the free memory block, the more serious the fragmentation problem in the heap.

[0086] If the required memory block size is greater than m times of Frag, the second strategy is sampled, m>1; if the required memory block size is less than Frag, the first strategy is adopted; if the required memory block size is between 1 and m times of the Frag value, the third strategy is adopted.

[0087] In one embodiment, m=4, when the requested memory size capacity is greater than 4 times of Frag, since it is much larger than the size of most nodes, only a small number of upper nodes close to the root node can meet the allocation request, and the best matching strategy (i.e., the second strategy) is adopted at this time. When the memory request size capacity is less than Frag, the nodes that can better meet the requirements are often located on the leaves of the Cartesian tree, and the traversal cost is often large, so the worst matching strategy (i.e., the first strategy) is adopted. When the requested memory size capacity is between 1 and 4 times the Frag value, a better matching strategy (i.e., the third strategy) is adopted.

[0088] If the memory management request is a memory reclaim request, traverse from the root node of the Cartesian tree to find a node adjacent to the released memory block;

[0089] The judgment logic of the node adjacent to the released memory block is: if the sum of the addr value and the size value of the current node is equal to the addr value of the released memory block, the node is marked as pre; if the sum of the addr value and the size value of the released memory block is equal to the addr value of the current node, the node is marked as suc; both the pre node and the suc node are target nodes;

[0090] The fusion operation on the target free memory block includes: if both the pre node and the suc node are found, the size value of the pre node is updated, the suc node is deleted, and the size value of the updated pre node is the sum of the size value of the original pre node, the size value of the suc node and the size value of the released memory block, and the addr value remains unchanged;

[0091] If only one target node is found, that is, one of the pre node and the suc node, the target node is updated, and the size value of the updated target node is the sum of the size value of the original target node and the size value of the released memory block; if the target node is the pre node, the addr value remains unchanged; if the target node is the pre node, the addr value is updated to the original addr value minus the size value of the released memory block;

[0092] If neither the pre node nor the suc node is found, a new node is created based on the addr value and size value of the released memory block.

[0093] The constraints of the Cartesian tree include: the size attribute is ordered by layer and is non-increasing or non-decreasing, and the addr attribute is ordered by sequence and is increasing or decreasing.

[0094] The present invention also provides a high-level integrated dynamic memory management device based on Cartesian trees, comprising an off-chip storage 1, a bus interface 2, an FPGA accelerator 3, a data loader 4, an on-chip cache 5, and a dynamic memory manager 6;

[0095] The off-chip storage 1 is used as extended storage when the BRAM storage resources on the FPGA are insufficient, and is used to store the structure and node information of the Cartesian tree; specifically, the off-chip storage 1 is implemented using DRAM;

[0096] The bus interface 2 is used to connect the accelerator 3 and the dynamic memory manager 6, and provides a unified interface, so not only the FPGA accelerator 3, but also the software processor can access the dynamic memory manager 6 through the bus interface 2 to issue a memory management request. And multiple accelerators 3 can access the memory manager through the bus interface 2 at the same time;

[0097] The FPGA accelerator 3 is used to issue a memory management request;

[0098] The dynamic memory manager 6 is used to accept memory management requests and execute the above method;

[0099] The data loader 4 is used to read the Cartesian tree nodes stored in the off-chip storage 1 into the on-chip cache 5, and write the updated Cartesian tree nodes into the off-chip storage 1; specifically, the data loader 4 is triggered by the memory manager 6 and is responsible for the interaction between the on-chip cache 5 and the off-chip storage 1. Since some Cartesian tree nodes are located in the off-chip memory 1, it is necessary to use the data loader 5 to read the nodes that need to be loaded and calculated into the on-chip cache 5, and write some nodes to the off-chip memory 1 when the on-chip cache 5 is full. The data loader 4 will store or read multiple nodes in blocks.

[0100] The on-chip cache 5 is used to temporarily store the Cartesian tree nodes written by the data loader 5 and the Cartesian tree nodes that need to be calculated recently, and provide them to the dynamic memory manager 6.

[0101] When the FPGA accelerator 3 needs to make a dynamic memory call during operation, it will write the corresponding request to the bus interface 2. The bus interface 2 will first determine the state of the memory manager 6. If it is busy, the request will be temporarily stored in the register. The register will then repeatedly determine the state of the dynamic memory manager 6 until the dynamic memory manager 6 is idle, and the dynamic memory manager 6 will process the request cached in the register.

[0102] In one embodiment, the off-chip memory 1 is divided into a static storage area 13, a dynamic storage area 14 and a Cartesian tree storage area 15;

[0103] The static storage area 13 is used to store part of the Cartesian tree node information and information that will not be released after being written. This part of the memory is often not released after being allocated, such as parameters set in calculations and static arrays;

[0104] The dynamic storage area 14 is a dynamically managed memory area, which is dynamically applied for and released when the program is running, such as dynamic arrays and linked list nodes;

[0105] The Cartesian tree storage area 15 stores Cartesian tree structure information.

[0106] In one embodiment, the dynamic memory manager 6 includes registers, a memory allocator 11, a memory reclaimer 12, and an allocation cache 10;

[0107] The register is used to temporarily store memory management requests;

[0108] The allocation buffer 10 is used to temporarily store the memory management request to be processed so as to quickly respond to the request; the memory management request in the allocation buffer 10 is obtained from the register;

[0109] The memory allocator 11 is used to execute the memory allocation request and maintain the Cartesian tree structure after giving the request result;

[0110] The memory reclaimer 12 is used to execute the memory reclaim request and maintain the Cartesian tree structure after giving the request result;

[0111] The registers include an allocation request register 7 and a recycling request register 8;

[0112] The allocation request register 7 is used to temporarily store memory allocation requests;

[0113] The recycling request register 8 is used to temporarily store memory recycling requests;

[0114] The dynamic memory manager 6 also includes a memory matching strategy selection module 9; the memory matching strategy selection module 9 is used to determine the strategy to be selected according to the degree of memory fragmentation and the requested free size. Therefore, during the execution process, the memory matching strategy selection module 9 will record the length of the memory block allocated or recovered in each successful request, and maintain the data on the degree of heap memory fragmentation.

[0115] The dynamic memory manager 6 is responsible for responding to, allocating or reclaiming the corresponding memory block after receiving the memory management request. When a request is sent but the dynamic memory manager 6 is busy, the dynamic memory manager 6 determines whether the memory management request is a memory allocation request or a memory reclaim request according to the difference of the corresponding flag bits in the signal sent by the FPGA accelerator 3, and caches it in the corresponding register (allocation request register 7 or reclaim request register 8).

[0116] In one embodiment, the memory allocator 11 includes a search module and a maintenance module;

[0117] The search module in the memory allocator 11 is as follows Figure 2 As shown; perform the following process:

[0118] First, the node n setting module sets the root node of the Cartesian tree as the search variable n, and then determines whether the size of the current node is greater than the capacity of the required memory block. If the size of the current node is less than the capacity of the required memory block, the parent node of the current node will be used as the final result, the search phase will end, and the Cartesian tree maintenance phase will be executed ( ). If the size of the current node is equal to the capacity of the required memory block, the current node is used as the target node, the search phase ends, and the maintenance phase of the Cartesian tree is executed. If the size of the current node is greater than the capacity of the required memory block, the search for the child nodes of the current node will continue downward. The lengths of the left child node and the right child node of node n are obtained through the two modules of left child node loading and right child node loading, and compared with the required memory block length capacity respectively ( ). If there is a child node whose size is equal to the capacity of the required memory block, then this child node is selected as the target node, the search phase ends, and the maintenance phase of the Cartesian tree is executed; if only one of the child nodes has a size larger than the capacity of the required memory block, then the larger child node is selected as the new loop variable n in the next loop; if there are two child nodes whose sizes are larger than the capacity ( ), the smaller child node is selected as the new loop variable n in the next loop ( ), and start the next loop; if there is no child node larger than capacity, it means that there is no memory block that meets the requirements in the subtree of the current node, and the loop ends and the current node n enters the maintenance phase of the Cartesian tree ( ).

[0119] The maintenance module in the memory allocator 11 is as follows Figure 3 As shown; perform the following process:

[0120] When the update phase obtains the starting point that needs to be maintained on the Cartesian tree When the node The setup module first modifies the node of and Attribute, indicating that one of the memory blocks has been occupied, and then the left child node loading module and the right child node loading module will be called to obtain the left and right child node information ( ). Then, determine whether there is a child node Attribute ratio Updated Larger values ​​( If not, the maintenance phase ends ( ). If so, you need to upgrade the child node and get the node by loading the module through the parent node The parent node of the algorithm , and trigger the parent node loading module to call the left child node loading module and the right child node loading module to obtain The left and right child nodes of the node are sent to the redirection module for redirection according to the relative size information of the address, and these nodes are reconstructed according to the Cartesian tree construction algorithm ( ). At the same time, the node has a greater The child nodes of the attribute will be selected as nodes The new child nodes and The parent node of ). As this process continues, the node It will gradually reduce the level in the Cartesian tree until there are no more child nodes. Attribute is less than . Then the termination condition is triggered, ending the execution of the maintenance phase ( ).

[0121] It is particularly important to point out that if the node size is reduced to 0, the node will be deleted after it moves to the leaf node position to ensure that the Cartesian tree can meet the constraints.

[0122] The memory recycler 12 includes a search module and a maintenance module;

[0123] The search module in the memory collector 12 is as follows Figure 4 As shown; perform the following process:

[0124] When a request arrives from the accelerator, The module will use the root node of CT as the initial loop variable Then determine whether the memory block released by the accelerator is consistent with the node The free memory blocks represented are adjacent ( and ). The judgment logic of adjacent connections is: if and equal, indicating that the node memory block is before the memory block to be released, so the node can be set to .if and Equal, indicating that the node memory block can be set to According to its With freeing memory The relative sizes between attributes are recorded as or . and The node can be merged with the released storage block. Then, the node selection will continue. The left or right child node of , start the next round of judgment, if the released memory block Node of If the attribute is greater, select the right child node, otherwise select the left child node ( ). Repeat this process until and / or The node is found, or the node Becoming an empty node proves that all possible nodes have been excluded. After the search process is completed, it enters the maintenance phase ( ).

[0125] The maintenance module in the memory collector 12 is as follows Figure 5 As shown; perform the following process:

[0126] like Figure 5 As shown in and All nodes have been found for When , the fusion operation of the memory blocks is started first. These memory blocks will be integrated into and There is a greater In the node with smaller The node can be deleted directly because its memory block has been integrated into other nodes, and its parent node pointer can be directly redirected to its only child node to complete the deletion ( ). Another node that has not been deleted will be marked as , which is the node to be promoted. If only and One of them, mark this unique node as ( ).if and If none of them are found, a new CT node is created and the information of the memory block to be released is recorded in it. Then, this node is inserted into the leaf node traversed by loading the parent node and inserting the child node module. Next, as a new leaf node, and marked as ( ). Subsequently, due to Node The property increases, so Improve the level in CT. This function is realized by the node upward module, which is responsible for the cycle improvement , until the parent node The property is again greater than ( ), it is worth noting that this part of the logic is the same as the downward shift. Multiple functional modules in the memory collector and memory allocator are reused with the memory allocator in implementation to reduce resource consumption.

[0127] The present invention provides a high-level integrated dynamic memory management method based on Cartesian trees, wherein the free memory blocks are mapped as follows: Figure 6 As shown;

[0128] A free memory block is the smallest unit of memory, and is represented by its first address and length. Use [addr, size] to refer to a free memory block. A memory block with a first address of 14 and a length of 3 can be represented as [14, 3], and the same applies to other memory blocks.

[0129] The present invention provides a high-level integrated dynamic memory management method based on Cartesian trees, wherein the Cartesian tree logic structure is as follows: Figure 7 As shown;

[0130] Figure 7 Demonstrates the use of Cartesian trees Figure 6 The example of dynamic memory management of heap memory in the example. The nodes with different patterns in the figure correspond to different free memory blocks. This article uses the first address of the memory block As Axis coordinates, size of memory block As Axis coordinates. All six nodes in the figure use To refer to Figure 6 For example, [18, 6] represents a memory block of 6 MAUs starting from the 18th MAU. In the Cartesian tree, all memory blocks The organization needs to satisfy two constraints: (1) of The attribute is greater than All nodes in the left subtree property, and must be less than All nodes in the right subtree Attributes; or of The attribute is smaller than All nodes in the left subtree Attributes, and must be greater than All nodes in the right subtree Attributes. (2) of The attribute must be greater than or equal to All nodes in the subtree Attributes; or of The property must be less than or equal to All nodes in the subtree Attributes. For example, for the first constraint, of Property 18 is greater than , , of Attributes 0, 7, 14, less than , of Attributes 25 and 29. For the second constraint, you can see the root node in the graph of The attribute is 6, which is the largest free memory block in the heap memory. , and of The minimum attribute. For example, for the first constraint, of Property 7 is greater than of Attribute 0, less than of Property 14. For the second constraint, you can see the root node in the graph of The attribute is 5, which is greater than of Property 3. Node For example, for the first constraint, of Property 25 is less than of Property 29. For the second constraint, you can see the root node in the graph of The property is 3 greater than of Property 2.

[0131] Example 1

[0132] This embodiment introduces a dynamic memory allocation method based on a Cartesian tree.

[0133] S1: Search phase. The main goal of this phase is to find as many resources as possible that can meet the allocation requirements. Has the smallest First, set the loop search variable The initial value is the root node Then the loop search begins. If the node of Greater than allocation request , The two child nodes and Will be considered for replacement with new .

[0134] Specifically, if both child nodes can satisfy the allocation request , then select If only one child node can satisfy the allocation request , then select If there is no child node that can satisfy the requirement, the loop is exited and the current variable This is the node to be searched.

[0135] S2: Maintenance phase. The main goal of this phase is to The length of the middle A piece of memory, and then maintain the Cartesian tree structure to reduce of The node is descended to avoid the situation where the size of the child node is larger. First, divide the node , remove the allocated memory. Then start looping down the node If there are child nodes Compare If it is larger, a swap operation is performed to move the child node up one level.

[0136] Specifically, if the left child If it is bigger, let the left child node As a parent node Son, let The right subtree is replaced by become The left child node of become New right child node. If the right child node If it is greater, then the right child node is promoted. .

[0137] based on Figure 7 Introducing Cartesian tree execution The processing of memory allocation requests: Different nodes in the graph correspond to different free memory blocks. In the search phase, first set The initial node , with a greater Therefore, we can judge the two child nodes and , With the ability to meet the needs , can be used as a new The above process continues for Execute, but and None of them meet the requirement of 4 MAUs, so the loop is exited. The maintenance phase is then executed. 4 MAUs are allocated to the accelerator and of Update to 11 (i.e. 7+4=11) or keep constant( The updated value depends on which MAUs are divided. Updated to 1 (ie 5-4=1). of is 1 less than , so it is necessary to reduce for , let replace become The maintenance phase is completed and the Cartesian tree still complies with the corresponding constraints.

[0138] Example 2

[0139] This embodiment introduces a dynamic memory recovery method based on a Cartesian tree.

[0140] S1: Search phase. The main purpose of this phase is to find the memory block adjacent to the released memory block. First, set the loop variable The root node of the Cartesian tree ,set up and for They are used to save the front and back adjacent memory blocks respectively. Then the loop starts to check whether the memory block represented by the node is adjacent to the released memory block. If it is the front adjacent, it is set to , and the next adjacent setting is .if Less than , then select The left child of , otherwise select The right child node of and are found, or the full search path has been traversed.

[0141] S2: Update phase. The main goal of this phase is to merge these adjacent memory blocks as much as possible and promote the merged memory blocks until their parent nodes are The attribute is greater than the node. First, perform the node fusion operation. and When all can be found, the smaller node is marked as , marking the larger nodes as . Then delete ,Will The parent node pointer points to its only child node and merges the adjacent addresses into Node. If only and One of them, then mark the found node as , and perform the fusion operation. If and If none is found, a new node will be created and marked as , and insert it as a leaf node. Finally, a loop promotion operation is performed so that of Property is less than its parent node.

[0142] Specifically, during the lifting process, first determine whether lifting is needed, and if lifting is needed, use Replace its parent node , then according to and of Attribute size, sorting The child nodes and Then, point to The new parent node starts the next cycle.

[0143] based on Figure 7 The Cartesian tree shown in the figure introduces the execution , The memory recycling request processing process, Figure 7 Different nodes in the graph correspond to different free memory blocks. In the search phase, first set the node The initial node , since the memory block to be released and are not adjacent, consider searching in the next layer. Therefore, use As new Start the next round of judgment. The memory block indicated by the reclaim request is adjacent to the memory block indicated by the reclaim request, so point to .because Not found yet, so make the next layer of nodes As new Continue to judge. is adjacent to the memory block being released and is located after the memory block to be released, so Set to Next, we will perform the maintenance phase. First, Set to the node to be deleted ,Will Set to the node that needs to be promoted .because There are no child nodes, so delete them directly. Update of The value is 10 (i.e. 5+2+3=10), and the addr value remains unchanged. The value is larger than the size of its parent node, so it needs to be increased .make As the root node, and As Or Set to the node to be deleted ,Will Set to the node that needs to be promoted ,renew of The value is 10 (i.e. 5+2+3=10), update of The value is 7 (i.e. of value), due to The value increases, so it is necessary to increase ,make As the root node, and As The right child node of As The right child node of . At this point, the maintenance phase is completed and the Cartesian tree still satisfies the corresponding constraints.

[0144] If executed , Memory reclaim request, only find Node ,Will Set as Node ,renew of The value is 6 (i.e. 5+1=6), and the addr value remains unchanged. The value is equal to the size of its parent node, so there is no need to increase it. .

[0145] If executed , Memory reclaim request, only find Node ;Will Set as Node ,renew of The value is 4 (i.e. 3+1=4), and the addr value is updated to 13. The value is smaller than the size of its parent node, so it does not need to be promoted. .

[0146] If executed , Memory reclaim request, and If none is found, a new node will be created and marked as , represented as [3, 2]. According to the constraints of the Cartesian tree, the new node is The left child node of As the left child of the new node.

[0147] The above embodiments are only used to illustrate the design ideas and features of the present invention, and their purpose is to enable those skilled in the art to understand the content of the present invention and implement it accordingly. The protection scope of the present invention is not limited to the above embodiments. Therefore, any equivalent changes or modifications made based on the principles and design ideas disclosed by the present invention are within the protection scope of the present invention.

Claims

1. A high-level integrated dynamic memory management method based on Cartesian trees, characterized in that: include: Dynamic memory management is performed based on a Cartesian tree, where the nodes of the Cartesian tree correspond to free memory blocks, represented by [addr, size], where addr represents the address of the memory block and size represents the size of the memory block; According to the memory management request, the target node, that is, the target free memory block, is found on the Cartesian tree, and the target free memory block is divided or merged; after the operation is completed, the position of the Cartesian tree node is adjusted to meet the constraints of the Cartesian tree; The memory management request includes a memory allocation request and a memory reclaim request; If the memory management request is a memory allocation request, a partition operation is performed on the target free memory block; If the memory management request is a memory reclaim request, a fusion operation is performed on the target free memory block; Finding the target node on the Cartesian tree includes: if the memory management request is a memory allocation request, traversing from the root node of the Cartesian tree to find a node that meets the memory allocation requirement, which is the target node; The partitioning operation on the target free memory block includes: subtracting the memory allocation requirement size from the size value of the target node; The constraints of the Cartesian tree include: the size attribute is ordered by layer and non-increasing or non-decreasing, and the addr attribute is ordered by sequence and increasing or decreasing; Use any one or more of the following three strategies to find nodes that meet the memory allocation requirements: The first strategy: If the root node meets the memory allocation requirements, directly use the root node as the target node; Second strategy: Find a node with the smallest size that meets the memory allocation requirement; the finding of a node with the smallest size that meets the memory allocation requirement includes: Start traversing from the root node of the Cartesian tree, use the root node as the search variable n, determine whether the size of the current node is greater than the required memory block size, if the size of the current node is less than the required memory block size, the parent node of the current node will be used as the target node; if the current node is equal to the required memory block size, the current node is used as the target node; if the size of the current node is greater than the required memory block size, search for the left and right child nodes of the current node, if the sizes of the two child nodes are both less than the required memory block size, the current node is used as the target node; if the size of a child node is equal to the required memory block size, the child node is used as the target node; if only one of the child nodes has a size greater than the required memory block size, select the larger child node as the new loop variable n in the next loop until the target node is found; if the sizes of the two child nodes are both greater than the required memory block size, the two child nodes are used as the new loop variable n in the next loop until the target node is found, take the two child nodes as the search variables and the target node with the smallest size found is used as the final target node; The third strategy: start traversing from the root node of the Cartesian tree, use the root node as the search variable n, determine whether the size of the current node is greater than the required memory block size, if the size of the current node is less than the required memory block size, the parent node of the current node will be used as the target node; if the current node is equal to the required memory block size, the current node is used as the target node; if the size of the current node is greater than the required memory block size, search for the left and right child nodes of the current node, if the sizes of both child nodes are less than the required memory block size, the current node is used as the target node; if there is a child node whose size is equal to the required memory block size, the child node is used as the target node; if only one of the child nodes has a size greater than the required memory block size, select the larger child node as the new loop variable n in the next loop until the target node is found; if the sizes of both child nodes are greater than the required memory block size, select the smaller child node as the new loop variable n in the next loop until the target node is found; It also includes a dynamic memory matching strategy selection mechanism; The dynamic memory matching strategy selection mechanism includes: using the current heap memory fragmentation degree and the size of the memory allocation application space as measurement indicators to determine the strategy to be selected; the fragmentation degree is calculated by the following formula: Among them, Size Free is the size of a free memory block in the heap memory, N CT is the number of nodes in the Cartesian tree; If the required memory block size is greater than m times of Frag, the second strategy is sampled, m>1; if the required memory block size is less than Frag, the first strategy is adopted; if the required memory block size is between 1 and m times of the Frag value, the third strategy is adopted.

2. The method according to claim 1, characterized in that The step of finding the target node on the Cartesian tree includes: If the memory management request is a memory reclaim request, traverse from the root node of the Cartesian tree to find a node adjacent to the released memory block; The judgment logic of the node adjacent to the released memory block is: if the sum of the addr value and the size value of the current node is equal to the addr value of the released memory block, the node is marked as pre; if the sum of the addr value and the size value of the released memory block is equal to the addr value of the current node, the node is marked as suc; both the pre node and the suc node are target nodes; The fusion operation on the target free memory block includes: if both the pre node and the suc node are found, the size value of the pre node is updated, the suc node is deleted, and the size value of the updated pre node is the sum of the size value of the original pre node, the size value of the suc node and the size value of the released memory block, and the addr value remains unchanged; If only one target node is found, that is, one of the pre node and the suc node, the target node is updated, and the size value of the updated target node is the sum of the size value of the original target node and the size value of the released memory block; if the target node is the pre node, the addr value remains unchanged; if the target node is the pre node, the addr value is updated to the original addr value minus the size value of the released memory block; If neither the pre node nor the suc node is found, a new node is created based on the addr value and size value of the released memory block.

3. A high-level integrated dynamic memory management device based on Cartesian trees, characterized in that: It includes off-chip storage (1), a bus interface (2), at least one FPGA accelerator (3), a data loader (4), an on-chip cache (5), and a dynamic memory manager (6); The off-chip storage (1) is used as extended storage when the BRAM storage resources on the FPGA are insufficient, and is used to store the structure and node information of the Cartesian tree; The bus interface (2) is used to connect the accelerator (3) and the dynamic memory manager (6); The FPGA accelerator (3) is used to issue a memory management request; The dynamic memory manager (6) is used to accept a memory management request and execute the method according to any one of claims 1 to 2; The data loader (4) is used to read the Cartesian tree nodes stored in the off-chip storage (1) into the on-chip cache (5), and write the updated Cartesian tree nodes into the off-chip storage (1); The on-chip cache (5) is used to temporarily store the Cartesian tree nodes written by the data loader (4) and provide them to the dynamic memory manager (6).

4. The device according to claim 3, characterized in that The off-chip storage (1) is divided into a static storage area (13), a dynamic storage area (14) and a Cartesian tree storage area (15); The static storage area (13) is used to store information that will not be released after being written. The dynamic storage area (14) is a dynamically managed memory area; The Cartesian tree storage area (15) stores Cartesian tree structure information.

5. The device according to claim 3, characterized in that The dynamic memory manager (6) includes a register, a memory allocator (11), a memory reclaimer (12) and an allocation cache (10); The register is used to temporarily store memory management requests; The allocation buffer (10) is used to temporarily store the memory management request to be processed; The memory allocator (11) is used to execute the memory allocation request and maintain the Cartesian tree structure after giving the request result; The memory reclaimer (12) is used to execute the memory reclaim request and maintain the Cartesian tree structure after giving the request result; The registers include an allocation request register (7) and a recycling request register (8); The allocation request register (7) is used to temporarily store memory allocation requests; The recycling request register (8) is used to temporarily store memory recycling requests; The dynamic memory manager (6) further comprises a memory matching strategy selection module (9); the memory matching strategy selection module (9) is used to determine the strategy to be selected according to the degree of memory fragmentation and the requested free space size.

Citation Information

Patent Citations

  • Cache read-write method and device, computer readable storage medium and electronic equipment

    CN112527196A

  • Dynamic memory management device and method oriented to high-level synthesis

    CN115309557A

  • Construction method and device of adaptive Cartesian grid data structure

    CN115470510A

  • Near-memory computing accelerator, dual in-line memory module and computing device

    CN115827211A

  • Adaptive Cartesian grid rapid generation optimization method

    CN118094668A