Rule fast lookup method, device and computer equipment based on CUDA
By obtaining the constant string of the rule, compiling it into a boolean array, and using CUDA for multi-threaded matching, the problem of low rule search efficiency is solved, and fast matching of data and rules is achieved.
Patent Information
- Application Number
- CN202211287133.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-10-20
- Publication Date
- 2026-03-03
- Estimated Expiration
- 2042-10-20
AI Technical Summary
Existing rule-based lookup methods are inefficient when dealing with large amounts of data and cannot achieve fast matching.
By obtaining the constant strings of the rules, a string matching algorithm is used to compile them into a boolean array, and CUDA is used for multi-threaded matching to determine whether the string to be matched contains all the constant strings.
It improves the efficiency of rule lookup and enables rapid matching of data and rules.
Smart Images

Figure CN115577076B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of data processing, and in particular to a method, apparatus, and computer device for fast rule lookup based on CUDA. Background Technology
[0002] Log auditing systems typically involve scenarios such as log retrieval and log parsing. In these scenarios, log data needs to be matched with rules in the system, and the data needs to be processed according to the matching rules. As the number of rules in the system increases, it is necessary to quickly find one or more rules that correspond to the data in order to process the data efficiently.
[0003] Current rule lookup methods sort rules in the system based on their activity level, where activity is the product of the reciprocal of the rule lookup time and the number of successful rule matches. The input data is then matched against the rules sequentially, and if the data meets the conditions of a particular rule, that rule is applied. However, this method essentially still involves traversing through the rules for matching. With larger data lengths, the time required for matching rules increases, resulting in low rule lookup efficiency.
[0004] There is currently no effective solution to the problem that related technologies cannot quickly match data with rules. Summary of the Invention
[0005] This embodiment provides a CUDA-based method, apparatus, and computer device for fast rule lookup, in order to solve the problem in related technologies that data cannot be quickly matched with rules.
[0006] Firstly, this embodiment provides a CUDA-based method for fast rule lookup, the method comprising:
[0007] Obtain the constant string for each rule, and compile the constant string using a string matching algorithm to obtain the first boolean array of single non-repeating characters in the constant string;
[0008] Obtain the string to be matched from the data to be matched, and create a second boolean array based on the string to be matched;
[0009] Based on CUDA, the first boolean array and the second boolean array are subjected to multi-threaded matching processing. When the string to be matched contains all the constant strings, the rule matches the data successfully.
[0010] In some embodiments, the step of compiling the constant string using a string matching algorithm to obtain a first boolean array of single non-repeating characters in the constant string includes:
[0011] The constant string is compiled using a string matching algorithm to obtain a third Boolean array of the constant string;
[0012] Based on the third Boolean array, each character in the constant string is extracted and compiled to obtain the first Boolean array of the single non-repeating character in the constant string.
[0013] In some embodiments, compiling the constant string using the Shift-Or algorithm to obtain a third Boolean array of the constant string includes:
[0014] Create a fourth boolean array based on the maximum length value of the constant string;
[0015] Set the boolean value of the fourth boolean array to true;
[0016] By modifying the Boolean values of the fourth Boolean array using the Shift-Or algorithm, the third Boolean array of the constant string is obtained.
[0017] In some embodiments, creating a second boolean array based on the string to be matched includes:
[0018] Based on the string to be matched, create a second boolean array with the same length as the first boolean array;
[0019] Set the boolean value of the second boolean array to false.
[0020] In some embodiments, the CUDA-based multi-threaded matching process between the first boolean array and the second boolean array includes:
[0021] Based on CUDA, extract a single character from the string to be matched;
[0022] If a single non-repeating character exists that is identical to the single character, then the first Boolean array corresponding to the single non-repeating character is retrieved;
[0023] The first boolean array and the second boolean array are matched using a multi-threaded process.
[0024] In some embodiments, the multi-threaded matching process of the retrieved first boolean array and the second boolean array includes:
[0025] Perform a parallel OR operation on the first Boolean array and the second Boolean array;
[0026] Perform multi-threaded shifting on the retrieved first and second boolean arrays.
[0027] In some embodiments, the rule matching the data successfully when the string to be matched contains all of the constant strings includes:
[0028] Record the number of times the string to be matched successfully matches the constant string, and obtain a count value;
[0029] Determine whether the count value is the same as the number of constant strings;
[0030] If they are the same, then the rule and the data are successfully matched.
[0031] Secondly, this embodiment provides a CUDA-based rule-fast lookup device, the device comprising:
[0032] The compilation module obtains the constant string for each rule and compiles the constant string using a string matching algorithm to obtain the first boolean array of single non-repeating characters in the constant string;
[0033] Create a module to obtain the string to be matched from the data to be matched, and create a second boolean array based on the string to be matched;
[0034] The matching module, based on CUDA, performs multi-threaded matching processing on the first boolean array and the second boolean array. When the string to be matched contains all the constant strings, the rule matches the data successfully.
[0035] Thirdly, this embodiment provides a computer device including a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, it implements the CUDA-based rule-based fast lookup method described in the first aspect above.
[0036] Fourthly, this embodiment provides a storage medium storing a computer program that, when executed by a processor, implements the CUDA-based rule-based fast lookup method described in the first aspect above.
[0037] Compared with related technologies, the CUDA-based rule fast lookup method, apparatus, and computer device provided in this embodiment obtains the constant string of each rule and compiles the constant string using a string matching algorithm to obtain a first boolean array of single non-repeating characters in the constant string. Further, it obtains the string to be matched of the data to be matched and creates a second boolean array based on the string to be matched. Based on CUDA, it performs multi-threaded matching processing on the first and second boolean arrays. When the string to be matched contains all the constant strings, the rule and data are successfully matched. This solves the problem of not being able to quickly match data and rules, and improves the efficiency of rule lookup.
[0038] Details of one or more embodiments of this application are set forth in the following drawings and description to make other features, objects and advantages of this application more readily apparent. Attached Figure Description
[0039] The accompanying drawings, which are included to provide a further understanding of this application and form part of this application, illustrate exemplary embodiments and are used to explain this application, but do not constitute an undue limitation of this application. In the drawings:
[0040] Figure 1 This is a hardware structure block diagram of a terminal device for a CUDA-based rule-based fast search method provided in one embodiment of this application;
[0041] Figure 2 This is a flowchart of a CUDA-based rule-based fast lookup method provided in an embodiment of this application;
[0042] Figure 3 This is a flowchart illustrating the compilation process of a CUDA-based fast rule lookup method provided in one embodiment of this application.
[0043] Figure 4 This is a flowchart illustrating the matching process of a CUDA-based rule-based fast search method provided in one embodiment of this application.
[0044] Figure 5 This is a schematic diagram of the multi-threaded matching principle of a CUDA-based fast rule search method provided in an embodiment of this application;
[0045] Figure 6 This is a preferred flowchart of a CUDA-based rule-based fast lookup method provided in an embodiment of this application;
[0046] Figure 7 This is a structural block diagram of a CUDA-based rule-fast lookup device provided in one embodiment of this application.
[0047] In the diagram: 10, compile module; 20, create module; 30, match module. Detailed Implementation
[0048] To better understand the purpose, technical solution, and advantages of this application, the application is described and illustrated below in conjunction with the accompanying drawings and embodiments.
[0049] Unless otherwise defined, the technical or scientific terms used in this application shall have the general meaning as understood by one of ordinary skill in the art to which this application pertains. Words such as “a,” “an,” “an,” “the,” “the,” and “these,” used in this application, do not indicate quantitative limitation and may be singular or plural. The terms “comprising,” “including,” “having,” and any variations thereof used in this application are intended to cover non-exclusive inclusion; for example, a process, method, system, product, or device that comprises a series of steps or modules (units) is not limited to the listed steps or modules (units) but may include steps or modules (units) not listed, or may include other steps or modules (units) inherent to such processes, methods, products, or devices. The terms “connected,” “linked,” and “coupled,” used in this application, are not limited to physical or mechanical connections but may include electrical connections, whether direct or indirect. The term “multiple” used in this application refers to two or more. The "and / or" operator describes the relationship between related objects, indicating that three relationships can exist. For example, "A and / or B" can represent three cases: A alone, A and B simultaneously, and B alone. Typically, the character " / " indicates that the objects before and after it are in an "or" relationship. The terms "first," "second," and "third," etc., used in this application are merely for distinguishing similar objects and do not represent a specific ordering of the objects.
[0050] The method embodiments provided in this example can be executed on a terminal, computer, or similar computing device. For example, it can run on a terminal. Figure 1 This is a hardware structure block diagram of the terminal for the CUDA-based rule-based fast lookup method in this embodiment. For example... Figure 1 As shown, a terminal may include one or more ( Figure 1 Only one is shown in the diagram. A processor 102 and a memory 104 for storing data are also included. The processor 102 may be, but is not limited to, a microprocessor (MCU) or a programmable logic device (FPGA). The terminal may also include a transmission device 106 for communication functions and an input / output device 108. Those skilled in the art will understand that… Figure 1 The structure shown is for illustrative purposes only and does not limit the structure of the terminal described above. For example, the terminal may also include components that are larger than... Figure 1 The more or fewer components shown, or having the same Figure 1 The different configurations shown are illustrated.
[0051] The memory 104 can be used to store computer programs, such as application software programs and modules, like the computer program corresponding to the CUDA-based rule-based fast lookup method in this embodiment. The processor 102 executes various functional applications and data processing by running the computer programs stored in the memory 104, thus implementing the aforementioned method. The memory 104 may include high-speed random access memory and non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some instances, the memory 104 may further include memory remotely located relative to the processor 102, and these remote memories can be connected to the terminal via a network. Examples of such networks include, but are not limited to, the Internet, corporate intranets, local area networks, mobile communication networks, and combinations thereof.
[0052] The transmission device 106 is used to receive or send data via a network. This network includes a wireless network provided by the terminal's communication provider. In one example, the transmission device 106 includes a Network Interface Controller (NIC), which can connect to other network devices via a base station to communicate with the Internet. In another example, the transmission device 106 can be a Radio Frequency (RF) module used for wireless communication with the Internet.
[0053] This embodiment provides a CUDA-based method for fast rule lookup. Figure 2 This is a flowchart of the CUDA-based rule-based fast lookup method in this embodiment, as shown below. Figure 2 As shown, the process includes the following steps:
[0054] Step S210: Obtain the constant string for each rule, and compile the constant string using a string matching algorithm to obtain the first boolean array of single non-repeating characters in the constant string.
[0055] It's important to know that a Boolean array is an array whose elements are Boolean variables. Boolean variables are the definers of logical variables, and the possible values of Boolean variables are true and false. Generally, the constant true is understood to be equivalent to the integer constant 1, while the constant false is understood to be equivalent to the integer constant 0.
[0056] Specifically, the constant string of each rule is extracted and a queue containing constant strings is generated. The constant strings in the queue are compiled using a string matching algorithm to obtain the first boolean array of single non-repeating characters in the constant strings.
[0057] Step S220: Obtain the string to be matched from the data to be matched, and create a second boolean array based on the string to be matched.
[0058] Step S230: Based on CUDA, perform multi-threaded matching on the first boolean array and the second boolean array. If the string to be matched contains all constant strings, then the rule and the data are successfully matched.
[0059] It's important to know that the Compute Unified Device Architecture (CUDA) is a general-purpose parallel computing architecture. This architecture includes the CUDA instruction set architecture and the parallel computing engine inside the Graphics Processing Unit (GPU), enabling GPUs to solve complex computational problems. Therefore, based on CUDA, it is possible to quickly match data with rules and match data with multiple rules simultaneously.
[0060] Current rule lookup methods sort rules in the system based on their activity level, where activity is the product of the reciprocal of the rule lookup time and the number of successful rule matches. The input data is then matched against the rules sequentially, and if the data meets the conditions of a particular rule, that rule is applied. However, this method essentially still involves traversing through the rules for matching. With larger data lengths, the time required for matching rules increases, resulting in low rule lookup efficiency. This application optimizes the matching logic between data and rules based on existing technologies. By determining whether the string to be matched can be successfully matched with the constant string of each rule, it further determines whether the rule and data are successfully matched. Specifically, the constant string of each rule is obtained, and the constant string is compiled using a string matching algorithm to obtain a first boolean array of single non-repeating characters in the constant string. The string to be matched of the data to be matched is obtained, and a second boolean array is created based on the string to be matched. Based on CUDA, multi-threaded matching processing is performed on the first boolean array and the second boolean array. When the string to be matched contains all the constant strings, the rule and data are successfully matched. This solves the problem of not being able to quickly match data and rules and improves the efficiency of rule lookup.
[0061] In some embodiments, a string matching algorithm is used to compile a constant string to obtain a first boolean array of single non-repeating characters in the constant string, including the following steps:
[0062] Step S211: The constant string is compiled using a string matching algorithm to obtain the third Boolean array of the constant string;
[0063] Step S212: Based on the third Boolean array, extract each character from the constant string and compile it to obtain the first Boolean array of single non-repeating characters in the constant string.
[0064] Specifically, such as Figure 3 As shown, the constant string is compiled using a string matching algorithm to obtain a third Boolean array of the constant string. The compilation process of this third Boolean array is as follows:
[0065] Step S301: Read the constant strings in the queue one by one, calculate the number of constant strings in the queue, and denot it as count;
[0066] Step S302: Calculate the length of each constant string;
[0067] Step S303: Calculate the length of the longest constant string in the queue, denoted as maxLength;
[0068] Step S304: Create a fourth boolean array with length count*maxLength, denoted as defaultBitArray;
[0069] Step S305: Initialize the Boolean value of the fourth Boolean array to true;
[0070] Step S306: Set the value of pos to 0, where pos represents the position of the constant string in the queue;
[0071] Step S307: Extract the constant string at position pos in the queue, and record the length of this constant string as length. pos And calculate the length. pos The absolute value of the difference between length and maxLength is denoted as lengthDif. pos ;
[0072] Step S308, if lengthDif pos If the value is 0, proceed to step S312; otherwise, proceed to step S309.
[0073] Step S309, set the value of rlength to 0;
[0074] Step S310: Set the value of defaultBitArray[rlength*count+pos] to false;
[0075] Step S311, if rlength <lengthDif pos If the result is positive, then increment rlength by 1 and proceed to step S310; otherwise, proceed to step S312.
[0076] Step S312, if pos < count, increment pos by 1 and then execute step S307; otherwise, execute step S313.
[0077] Furthermore, based on the third boolean array, extract each character in the constant string and compile it to obtain the first boolean array of single non-repeating characters in the constant string. The compilation process of this first boolean array is as follows:
[0078] Step S313, create a mapping relationship table between the constant string and the first boolean array, denoted as charToBitArray;
[0079] Step S314, set the value of pos to 0;
[0080] Step S315, extract the constant string constString at position pos in the queue;
[0081] Step S316, set the value of posch to 0;
[0082] Step S317, let ch = constString[posch];
[0083] Step S318, if ch exists in charToBitArray, execute step S321; otherwise, execute step S319;
[0084] Step S319, create a boolean array with a length of count * maxLength, whose values are the same as those of defaultBitArray, denoted as chBitArray;
[0085] Step S320, let charToBitArray[ch] = chBitArray;
[0086] Step S321, make chBitArray = charToBitArray[ch];
[0087] Step S322, set the value of chBitArray[(posch + lengthDif pos ) * count + pos] to false;
[0088] Step S323, if posch < length pos , increment posch by 1 and then execute step S317; otherwise, execute step S324;
[0089] Step S324, if pos < count, increment pos by 1 and then execute step S315; otherwise, execute step S325;
[0090] Step S325: Compilation complete.
[0091] In this embodiment, the constant string is first compiled to obtain a third Boolean array of the constant string. Based on the third Boolean array, each character in the constant string is extracted and compiled to obtain a first Boolean array of a single non-repeating character in the constant string, thereby realizing the pre-compilation of the rules.
[0092] In some embodiments, the constant string is compiled using the Shift-Or algorithm to obtain a third Boolean array of the constant string, including the following steps:
[0093] Create a fourth boolean array based on the maximum length of the constant string;
[0094] Set the boolean value of the fourth boolean array to true;
[0095] By using the Shift-Or algorithm, the Boolean values of the fourth Boolean array are modified accordingly to obtain the third Boolean array of constant strings.
[0096] Specifically, the number of constant strings in the queue and the maximum length of each constant string are calculated. The product of the number and the maximum length is used as the length of the fourth boolean array, and the fourth boolean array is initialized by setting its boolean value to true.
[0097] It's important to know that the Shift-Or algorithm is a prefix-based string matching algorithm that uses bitwise operations. Its principle is to record the prefix matching of the pattern string through a mask, and to build an index relationship between characters and arrays through the Shift-Or algorithm.
[0098] In this embodiment, a fourth Boolean array is created based on the maximum length of the constant string, and the Boolean value of the fourth Boolean array is set to true. Furthermore, the Boolean value of the fourth Boolean array is modified accordingly using the Shift-Or algorithm to obtain the third Boolean array of the constant string.
[0099] In some embodiments, creating a second boolean array based on the string to be matched includes the following steps:
[0100] Step S221: Based on the string to be matched, create a second boolean array with the same length as the first boolean array;
[0101] Step S222: Set the Boolean value of the second Boolean array to false.
[0102] Specifically, such as Figure 4 As shown, a second boolean array is created based on the string to be matched. The creation process of this second boolean array is as follows:
[0103] Step S401: Set the initial value of matchTimes to 0;
[0104] Step S402: Record the length of the string to be matched as lengthstr;
[0105] Step S403: Generate a queue containing constant strings, calculate the number of constant strings in the queue, and record it as count;
[0106] Step S404: Calculate the length of the constant strings in the queue and store it in the array lengths;
[0107] Step S405: Create a second boolean array with a length of count*maxLength, denoted as stateBitArray, and set the boolean value of the second boolean array to false;
[0108] It is important to know that the length of the first boolean array is determined based on the number of constant strings in the queue and their maximum length, and the length of the second boolean array is the same as that of the first boolean array.
[0109] In this embodiment, a second boolean array with the same length as the first boolean array is created based on the string to be matched, and the boolean value of the second boolean array is set to false, thereby realizing the creation and initialization of the second boolean array.
[0110] In some embodiments, based on CUDA, multi-threaded matching processing is performed on the first boolean array and the second boolean array, including the following steps:
[0111] Step S231: Based on CUDA, extract a single character from the string to be matched;
[0112] Step S232: If there is a single non-repeating character that is the same as the single character, then retrieve the first Boolean array corresponding to the single non-repeating character;
[0113] Step S233: Perform multi-threaded matching processing on the retrieved first Boolean array and second Boolean array.
[0114] Specifically, based on CUDA, a multi-threaded matching process is performed on the first boolean array and the second boolean array. The multi-threaded matching process is as follows:
[0115] Step S406: Set the value of pos to 0;
[0116] Step S407: Extract the character at position pos in the string to be matched, and denote it as ch;
[0117] Step S408, determine whether ch exists in charToBitArray;
[0118] Step S409, if it exists, set chBitArray to charToBitArray[ch];
[0119] Step S410, otherwise set chBitArray to defaultBitArray;
[0120] Step S411, execute stateBitArray |= chBitArray;
[0121] Step S412, set the value of loop to 0, where loop represents the position in the array lengths;
[0122] Step S413, determine whether lengths[loop] is equal to pos + 1. If it is equal, execute Step S414; otherwise execute Step S416;
[0123] Step S414, determine whether stateBitArray[count * maxLength - loop] is false. If it is, execute Step S415; otherwise execute Step S416;
[0124] Step S415, increment matchTimes by 1 and execute S417;
[0125] Step S416, if loop < count, increment loop by 1 and then execute Step S413; otherwise execute Step S417;
[0126] Step S417, if pos < lengthstr, increment pos by 1 and then execute Step S409; otherwise execute Step S418;
[0127] Step S418, the matching ends.
[0128] Further, in Step S407, extract a single character from the string to be matched, determine whether there is a single non - repeating character that is the same as the single character. If there is, retrieve the first boolean array corresponding to the single non - repeating character; if not, retrieve the third boolean array.
[0129] It should be noted that matchTimes is used to record the number of times the string to be matched successfully matches the constant string in the queue, and the corresponding first boolean array is retrieved through Step S409. In Step S411, an OR operation is performed on the retrieved first boolean array and the second boolean array in parallel.
[0130] In this embodiment, based on CUDA, a single character is extracted from the string to be matched. When a single non-repeating character exists that is the same as the single character, the first boolean array corresponding to the single non-repeating character is retrieved, and the retrieved first boolean array and the second boolean array are subjected to multi-threaded matching processing, thereby completing the matching process between the string to be matched and the constant strings in the queue. Furthermore, through the parallel computing architecture of CUDA, fast matching between boolean arrays is achieved to improve the efficiency of rule lookup.
[0131] In some embodiments, multi-threaded matching is performed on the retrieved first and second boolean arrays, including the following steps:
[0132] Perform a parallel OR operation on the first and second Boolean arrays retrieved.
[0133] Perform multi-threaded shifting on the retrieved first and second Boolean arrays.
[0134] Specifically, the string to be matched contains several individual characters. In the first boolean array that has been compiled, the first boolean array corresponding to the same character is retrieved in turn. The retrieved first boolean array and the second boolean array are ORed in parallel. That is, multiple pairs of boolean values are ORed simultaneously between the two boolean arrays. The boolean values are logical values true or false. Therefore, in the process of ORing, the result is false only when all boolean values are false, and the result is true in other cases.
[0135] It is important to know that the first and second boolean arrays are subjected to multi-threaded shifting. This multi-threaded shifting process refers to shifting each boolean value two positions to the right in the boolean array obtained by the OR operation, and adding false in the empty positions to obtain the boolean array that has completed multi-threaded matching.
[0136] Furthermore, such as Figure 5 As shown, based on the parallel computing architecture CUDA, the matching process of two Boolean arrays is achieved through a concurrent mode using multi-threaded matching. Unlike traditional microprocessors (Central Processing Unit, or CPU), the right shift process based on CUDA can be implemented through code to avoid loop operations.
[0137] In this embodiment, based on the CUDA architecture, the single-threaded calculation and shifting process between the first and second Boolean arrays is optimized into parallel calculation of OR operations between multiple pairs of Boolean values and the simultaneous shifting of multiple Boolean values, thereby achieving fast matching of the string to be matched and the constant string.
[0138] In some embodiments, the rule matches the data successfully when the string to be matched contains all constant strings, including the following steps:
[0139] Record the number of times the string to be matched successfully matches the constant string, and obtain the count value;
[0140] Determine if the count value is the same as the number of constant strings;
[0141] If they match, then the rule and data are successfully matched.
[0142] Specifically, it checks whether the boolean array that has completed the matching has reached the length of the constant string. If the boolean array has reached the length of the constant string, it checks the specific position of the character in the boolean array. Since false represents a successful match, it checks the specific position of false in the boolean array.
[0143] Furthermore, the number of right shifts that have occurred is calculated. This number represents the position in the string to be matched where a match was successfully made. For example, if the number of right shifts is 2, it means that the constant string was successfully matched at position 2 in the string to be matched.
[0144] In this embodiment, the number of times the string to be matched successfully matches the constant string is recorded to obtain a count value. It is then determined whether the count value is the same as the number of constant strings. If they are the same, the rule and data are considered to have matched successfully. This transforms the determination of whether the rule and data match successfully into the determination of whether the number of times the string to be matched successfully matches the constant string is the same as the number of constant strings, thereby improving the accuracy of determining whether the rule and data match successfully.
[0145] The following describes and illustrates this embodiment through application examples.
[0146] Specifically, taking the constant strings "ab" and "abc" in the queue, and the string to be matched as "cabc", the matching process is as follows:
[0147] Calculate the number of constant strings in the queue, i.e., count = 2, and calculate the length of the longest constant string in the queue, i.e. maxLength = 3. Create a fourth boolean array with a length of 6, initialize its boolean value to true, and denote it as defaultBitArray, as shown in Table 1.
[0148] Table 1
[0149] true true true true true true
[0150] Calculate rlength*count+pos sequentially. When pos is 0, rlength is 0, so rlength*count+pos is 0. Set the boolean value at position 0 in defaultBitArray to false, as shown in Table 2. When pos is 1, lengthDif pos If the value is 0, then the fourth boolean array does not need to be modified.
[0151] Table 2
[0152] false true true true true true
[0153] Create a mapping table from individual characters in the constant string to the array. Take out the constant strings in the queue one by one. The first constant string is ab. Extract the first character of ab as 'a'. However, the character 'a' does not exist in the mapping table, so create a new array. Its boolean value is the same as the modified fourth boolean array, as shown in Table 3.
[0154] Table 3
[0155] false true true true true true
[0156] If the extracted character is the first character of the first constant string, then the values of pos and posch are both 0, and lengthDif pos Since it is 1, we calculate (posch + lengthDif) pos The result of )*count+pos is 2. Modify the value at the corresponding position to false, as shown in Table 4.
[0157] Table 4
[0158] a false true false true true true
[0159] Extract the second character 'b' from the first constant string. If character 'b' does not exist in the mapping table, then retrieve the boolean array corresponding to character 'a' in the mapping table. Since the value of 'pos' is 1, the value of 'posch' is 0, and lengthDif... pos The value is 0, so we calculate (posch + lengthDif) pos The result of count + pos is 1. The value at the corresponding position is changed to false. Then, the single characters of the constant strings in the queue are extracted one by one. Finally, the first boolean arrays of characters a, b and c are generated and saved to charToBitArray, as shown in Table 5.
[0160] Table 5
[0161] a false true false true true true b false true true false false true c false true true true true false
[0162] Based on the number of constant strings in the queue, i.e. count is 2, and the length of the longest constant string in the queue, i.e. maxLength is 3, create a second boolean array of length 6, initialize its boolean value to false, and denote it as stateBitArray, as shown in Table 6.
[0163] Table 6
[0164] false false false false false false
[0165] Extract the first character 'c' from the string to be matched, retrieve the second boolean array corresponding to 'c' from charToBitArray, perform a bitwise OR operation between it and stateBitArray to obtain a newly generated stateBitArray, denoted as newBitArray, and shift the newly generated stateBitArray right by 2 bits as the stateBitArray for the next multi-threaded matching process, as shown in Table 7.
[0166] Table 7
[0167]
[0168]
[0169] Continue to extract each character of the string "cabc" to be matched, retrieve the second boolean array corresponding to each character from charToBitArray, and perform the corresponding OR operation and shift, as shown in Tables 8 to 11.
[0170] Table 8
[0171]
[0172] Table 9
[0173]
[0174] When the length of the constant string "ab" is reached, the 5th position of the boolean array is false, which means that the constant string has successfully matched the string to be matched. At this time, the number of right shifts is 2, which means that the match was successful at the 2nd position of the string to be matched.
[0175] Table 10
[0176]
[0177] When the length of the constant string "abc" is reached, the 6th position of the boolean array is false, which means that the constant string has successfully matched the string to be matched. At this time, the number of right shifts is 3, which means that the match was successful at the 3rd position of the string to be matched.
[0178] Table 11
[0179]
[0180] After completing the multi-threaded matching process for each character, the value of matchTimes is found to be 2, which is the same as the number of constant strings count. This indicates that the data corresponding to the string to be matched has successfully matched the above rules.
[0181] The present embodiment will now be described and illustrated through preferred embodiments.
[0182] Figure 6 This is a preferred flowchart of the CUDA-based rule-based fast lookup method in this embodiment, as follows: Figure 6 As shown, this CUDA-based fast rule lookup method includes the following steps:
[0183] Step S610: Obtain the constant string for each rule;
[0184] Step S620: The constant string is compiled using the Shift-Or algorithm to obtain the third Boolean array of the constant string;
[0185] Step S630: Based on the third Boolean array, extract each character from the constant string and compile it to obtain the first Boolean array of a single non-repeating character in the constant string;
[0186] Step S640: Based on the string to be matched, create a second boolean array with the same length as the first boolean array, and set the boolean value of the second boolean array to false;
[0187] Step S650: Based on CUDA, extract a single character from the string to be matched;
[0188] Step S660: If there is a single non-repeating character that is the same as the single character, then retrieve the first Boolean array corresponding to the single non-repeating character;
[0189] Step S670: Perform multi-threaded matching processing on the retrieved first Boolean array and second Boolean array;
[0190] Step S680: Record the number of times the string to be matched successfully matches the constant string, and obtain the count value;
[0191] Step S690: Determine whether the count value is the same as the number of constant strings. If they are the same, the rule and data match successfully.
[0192] In this embodiment, the Shift-Or algorithm is used to compile the constant strings in the queue, resulting in a third Boolean array of constant strings. Based on the third Boolean array, each character in the constant string is extracted and compiled to obtain a first Boolean array of single unique characters in the constant string, completing the pre-compilation of the rules. A second Boolean array with the same length as the first Boolean array is created and initialized. Further, a single character in the string to be matched is extracted. If a single unique character exists that is the same as the single character, the first Boolean array corresponding to the single unique character is retrieved. Based on CUDA, multi-threaded matching processing is performed on the retrieved first Boolean array and the second Boolean array. The number of times the string to be matched and the constant string are successfully matched is recorded to obtain a count value. It is determined whether the count value is the same as the number of constant strings to determine whether the rule and the data are successfully matched. It can be seen that by constructing the index relationship between the string and the array through the Shift-Or algorithm, multi-threaded matching processing based on CUDA is implemented, improving the efficiency of rule lookup.
[0193] It should be noted that the steps shown in the above process or in the flowchart of the accompanying figures can be executed in a computer system such as a set of computer-executable instructions, and although a logical order is shown in the flowchart, in some cases the steps shown or described may be executed in a different order than that shown here.
[0194] This embodiment also provides a CUDA-based rule-based fast lookup device, which is used to implement the above embodiments and preferred embodiments; details already described will not be repeated. The terms "module," "unit," "subunit," etc., used below can refer to combinations of software and / or hardware that implement a predetermined function. Although the device described in the following embodiments is preferably implemented in software, hardware implementation, or a combination of software and hardware, is also possible and contemplated.
[0195] Figure 7 This is a structural block diagram of the CUDA-based rule-based fast lookup device in this embodiment, as shown below. Figure 7 As shown, the device includes: a compilation module 10, a creation module 20, and a matching module 30;
[0196] Compilation module 10 obtains the constant string for each rule and compiles the constant string using a string matching algorithm to obtain the first boolean array of a single non-repeating character in the constant string;
[0197] Create module 20 to obtain the string to be matched from the data to be matched, and create a second boolean array based on the string to be matched;
[0198] Matching module 30, based on CUDA, performs multi-threaded matching on the first and second boolean arrays. When the string to be matched contains all constant strings, the rule and data match successfully.
[0199] The apparatus provided in this embodiment obtains a constant string for each rule and compiles it using a string matching algorithm to obtain a first boolean array of single non-repeating characters in the constant string. Further, it obtains the string to be matched for the data to be matched and creates a second boolean array based on this string. Using CUDA, it performs multi-threaded matching on the first and second boolean arrays. When the string to be matched contains all the constant strings, the rule and data are successfully matched. This solves the problem of not being able to quickly match data with rules and improves the efficiency of rule lookup.
[0200] In some of these embodiments, Figure 7 Based on this, the device also includes an extraction module, which is used to compile the constant string using a string matching algorithm to obtain a third Boolean array of the constant string; based on the third Boolean array, it extracts each character in the constant string and compiles it to obtain a first Boolean array of single non-repeating characters in the constant string.
[0201] In some of these embodiments, Figure 7 Based on this, the device also includes an algorithm module, which is used to create a fourth Boolean array based on the maximum length of the constant string; set the Boolean value of the fourth Boolean array to true; and modify the Boolean value of the fourth Boolean array accordingly using the Shift-Or algorithm to obtain a third Boolean array of the constant string.
[0202] In some of these embodiments, Figure 7 In addition to the above, the device also includes an initialization module for creating a second boolean array with the same length as the first boolean array based on the string to be matched; and setting the boolean value of the second boolean array to false.
[0203] In some of these embodiments, Figure 7 Based on this, the device also includes a retrieval module, which is used to extract a single character from the string to be matched based on CUDA; when there is a single non-repeating character that is the same as the single character, the first Boolean array corresponding to the single non-repeating character is retrieved; and the retrieved first Boolean array and second Boolean array are subjected to multi-threaded matching processing.
[0204] In some of these embodiments, Figure 7 Based on this, the device also includes an execution module for performing a parallel OR operation on the first and second Boolean arrays and performing multi-threaded shift operations on the first and second Boolean arrays.
[0205] In some of these embodiments, Figure 7 In addition to the above, the device also includes a judgment module, which records the number of times the string to be matched successfully matches the constant string and obtains a count value; it judges whether the count value is the same as the number of constant strings; if they are the same, the rule and the data are successfully matched.
[0206] It should be noted that the above modules can be functional modules or program modules, and can be implemented through software or hardware. For modules implemented through hardware, the above modules can reside in the same processor; or the above modules can be located in different processors in any combination.
[0207] This embodiment also provides a computer device, including a memory and a processor, wherein the memory stores a computer program and the processor is configured to run the computer program to perform the steps in any of the above method embodiments.
[0208] Optionally, the computer device may further include a transmission device and an input / output device, wherein the transmission device is connected to the processor and the input / output device is connected to the processor.
[0209] It should be noted that the specific examples in this embodiment can refer to the examples described in the above embodiments and optional implementations, and will not be repeated in this embodiment.
[0210] Furthermore, in conjunction with the CUDA-based fast rule lookup method provided in the above embodiments, this embodiment can also provide a storage medium for implementation. This storage medium stores a computer program; when executed by a processor, this computer program implements any of the CUDA-based fast rule lookup methods described in the above embodiments.
[0211] It should be understood that the specific embodiments described herein are merely illustrative of the application and not intended to limit it. All other embodiments derived by those skilled in the art based on the embodiments provided in this application without inventive effort are within the scope of protection of this application.
[0212] Obviously, the accompanying drawings are merely some examples or embodiments of this application. Those skilled in the art can apply this application to other similar situations based on these drawings without any creative effort. Furthermore, it is understood that although the work done in this development process may be complex and lengthy, for those skilled in the art, certain design, manufacturing, or production modifications made based on the technical content disclosed in this application are merely conventional technical means and should not be considered as insufficient disclosure of this application.
[0213] The term "embodiment" in this application refers to a specific feature, structure, or characteristic described in connection with an embodiment that may be included in at least one embodiment of this application. The appearance of this phrase in various places in the specification does not necessarily imply the same embodiment, nor does it imply that it is mutually exclusive with or independent of other embodiments. It will be clearly or implicitly understood by those skilled in the art that the embodiments described in this application may be combined with other embodiments without conflict.
[0214] The embodiments described above are merely illustrative of several implementation methods of this application, and while the descriptions are relatively specific and detailed, they should not be construed as limiting the scope of patent protection. It should be noted that those skilled in the art can make various modifications and improvements without departing from the concept of this application, and these all fall within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the appended claims.
Claims
1. A CUDA-based method for fast rule lookup, characterized in that, The method includes: Obtain the constant string for each rule, and compile the constant string using a string matching algorithm to obtain the first boolean array of single non-repeating characters in the constant string; The step of compiling the constant string using a string matching algorithm to obtain a first Boolean array of single non-repeating characters in the constant string includes: compiling the constant string using a string matching algorithm to obtain a third Boolean array of the constant string; and extracting each character from the constant string based on the third Boolean array and compiling it to obtain the first Boolean array of single non-repeating characters in the constant string. The step of compiling the constant string using a string matching algorithm to obtain the third Boolean array of the constant string includes: creating a fourth Boolean array based on the maximum length value of the constant string; setting the Boolean value of the fourth Boolean array to true; and modifying the Boolean value of the fourth Boolean array accordingly using the Shift-Or algorithm to obtain the third Boolean array of the constant string. Obtain the string to be matched from the data to be matched, and create a second boolean array based on the string to be matched; The step of creating a second boolean array based on the string to be matched includes: creating a second boolean array with the same length as the first boolean array based on the string to be matched; and setting the boolean value of the second boolean array to false. Based on CUDA, the first boolean array and the second boolean array are subjected to multi-threaded matching processing. When the string to be matched contains all the constant strings, the rule matches the data successfully. The step of performing multi-threaded matching processing on the first boolean array and the second boolean array based on CUDA includes: extracting a single character from the string to be matched based on CUDA; if there is a single non-repeating character that is the same as the single character, then retrieving the first boolean array corresponding to the single non-repeating character; and performing multi-threaded matching processing on the retrieved first boolean array and the second boolean array. The step of determining that the rule matches the data successfully when the string to be matched contains all the constant strings includes: recording the number of times the string to be matched matches the constant strings to obtain a count value; determining whether the count value is the same as the number of constant strings; if they are the same, then the rule matches the data successfully.
2. The CUDA-based rule-based fast lookup method according to claim 1, characterized in that, The multi-threaded matching process for the retrieved first boolean array and the second boolean array includes: Perform a parallel OR operation on the first Boolean array and the second Boolean array; Perform multi-threaded shifting on the retrieved first and second boolean arrays.
3. A CUDA-based rule-based fast lookup device, characterized in that, The device includes: The compilation module obtains the constant string for each rule and compiles the constant string using a string matching algorithm to obtain the first boolean array of single non-repeating characters in the constant string; The compilation module compiles the constant string using a string matching algorithm to obtain a third Boolean array of the constant string; based on the third Boolean array, it extracts each character from the constant string and compiles it to obtain a first Boolean array of the single non-repeating character in the constant string. The compilation module creates a fourth boolean array based on the maximum length of the constant string; sets the boolean value of the fourth boolean array to true; and modifies the boolean value of the fourth boolean array accordingly using the Shift-Or algorithm to obtain the third boolean array of the constant string. Create a module to obtain the string to be matched from the data to be matched, and create a second boolean array based on the string to be matched; The creation module creates a second boolean array with the same length as the first boolean array based on the string to be matched; and sets the boolean value of the second boolean array to false. The matching module, based on CUDA, performs multi-threaded matching processing on the first boolean array and the second boolean array. When the string to be matched contains all the constant strings, the rule matches the data successfully. The matching module, based on CUDA, extracts a single character from the string to be matched; if a single non-repeating character exists that is identical to the single character, the first boolean array corresponding to the single non-repeating character is retrieved; and multi-threaded matching processing is performed on the retrieved first boolean array and the second boolean array. The matching module records the number of times the string to be matched successfully matches the constant string, and obtains a count value; it then determines whether the count value is the same as the number of constant strings; if they are the same, the rule matches the data successfully.
4. A computer device, comprising a memory and a processor, characterized in that, The memory stores a computer program, and the processor is configured to run the computer program to perform the steps of the CUDA-based rule-fast lookup method as described in any one of claims 1 to 2.
5. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by the processor, it implements the steps of the CUDA-based fast rule lookup method as described in any one of claims 1 to 2.
Citation Information
Patent Citations
Matching method and device based on TCAM (ternary content addressable memory) for NFA (non-deterministic finite automaton)
CN103226551A
Quick parallel character string matching method and system based on GPU stream
CN112883245A