Data processing method and device
By splitting the matrix into blocks and using the Montgomery module multiplication instruction for matrix multiplication, the problem of low matrix multiplication operation in the PoseidonHash algorithm is solved, and the operating efficiency and data processing efficiency of the processor are improved.
Patent Information
- Application Number
- CN202110448967.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-04-25
- Publication Date
- 2024-09-03
- Estimated Expiration
- 2041-04-25
AI Technical Summary
The matrix mode multiplication operation efficiency in the PoseidonHash algorithm is low, resulting in a large data processing time consumption, and it is difficult for the existing technology to effectively improve the efficiency of matrix multiplication operation.
By splitting the second matrix into multiple matrix blocks and using the Montgomery modulo multiplication instruction to perform calculations with the first matrix, the matrix multiplication operation process is simplified, the operation complexity is reduced, and the processor operation efficiency is improved.
It significantly improves the processor operation efficiency of matrix multiplication operation, shortens the execution time of the Poseidon Hash algorithm on a single-block processor, and improves data processing efficiency.
Smart Images

Figure CN113296733B
Abstract
Description
Technical Field
[0001] The embodiments of this specification relate to the field of computer technology, and more particularly to a data processing method. One or more embodiments of this specification also relate to a data processing apparatus, a computing device, and a computer-readable storage medium. Background Art
[0002] With the rapid development of computer technology, the PoseidonHash algorithm, as a new hash function, is more widely used in the field of blockchain and privacy protection, thereby improving data security. The core operation in the PoseidonHash algorithm is the modular multiplication of matrices (referred to as matrix multiplication). The modular multiplication operation refers to the operation of multiplying matrices first and then taking the remainder, that is, the modular multiplication of matrices requires multiplication and then division. The operation process is relatively complicated, resulting in low efficiency of modular multiplication of matrices. How to improve the efficiency of modular multiplication of matrices and save processing time is the main problem currently faced. Therefore, when performing modular multiplication of matrices, a more efficient data processing method needs to be provided. Summary of the invention
[0003] In view of this, an embodiment of this specification provides a data processing method. One or more embodiments of this specification also relate to a data processing device, a computing device, and a computer-readable storage medium to solve the technical defects existing in the prior art.
[0004] According to a first aspect of an embodiment of this specification, a data processing method is provided, including:
[0005] S1, determining a first matrix and a second matrix, and splitting the second matrix into a first preset number of matrix blocks;
[0006] S2, calling a Montgomery modular multiplication-addition instruction to operate the elements included in the first matrix and the elements included in the j-th matrix block to obtain a matrix block operation result corresponding to the j-th matrix block, wherein j is equal to 1, and the Montgomery modular multiplication-addition instruction is used to simultaneously implement multiplication and addition operations in the Montgomery domain;
[0007] S3, overwriting the elements in the j-th matrix block with the matrix block operation result corresponding to the j-th matrix block;
[0008] S4, increment j by 1, and continue to execute step S2 until j is equal to the first preset number, to obtain a target matrix after matrix multiplication of the first matrix and the second matrix.
[0009] According to a second aspect of an embodiment of this specification, there is provided a data processing device, including:
[0010] A splitting module is configured to determine a first matrix and a second matrix, and split the second matrix into a first preset number of matrix blocks;
[0011] A calling module is configured to call a Montgomery modular multiplication-addition instruction to operate the elements included in the first matrix and the elements included in the j-th matrix block to obtain a matrix block operation result corresponding to the j-th matrix block, wherein j is equal to 1, and the Montgomery modular multiplication-addition instruction is used to simultaneously implement multiplication and addition operations in the Montgomery domain;
[0012] A covering module, configured to cover the elements in the j-th matrix block with the matrix block operation result corresponding to the j-th matrix block;
[0013] The execution module is configured to increment j by 1 and continue to execute the calling module until j is equal to the first preset number, thereby obtaining a target matrix after matrix multiplication of the first matrix and the second matrix.
[0014] According to a third aspect of an embodiment of this specification, a computing device is provided, including:
[0015] Memory and processor;
[0016] The memory is used to store computer executable instructions, and the processor is used to execute the computer executable instructions to implement:
[0017] S1, determining a first matrix and a second matrix, and splitting the second matrix into a first preset number of matrix blocks;
[0018] S2, calling a Montgomery modular multiplication-addition instruction to operate the elements included in the first matrix and the elements included in the j-th matrix block to obtain a matrix block operation result corresponding to the j-th matrix block, wherein j is equal to 1, and the Montgomery modular multiplication-addition instruction is used to simultaneously implement multiplication and addition operations in the Montgomery domain;
[0019] S3, overwriting the elements in the j-th matrix block with the matrix block operation result corresponding to the j-th matrix block;
[0020] S4, increment j by 1, and continue to execute step S2 until j is equal to the first preset number, to obtain a target matrix after matrix multiplication of the first matrix and the second matrix.
[0021] According to a fourth aspect of the embodiments of this specification, a computer-readable storage medium is provided, which stores computer-executable instructions, and when the instructions are executed by a processor, the steps of any one of the data processing methods are implemented.
[0022] An embodiment of the present specification provides a data processing method, which can first determine a first matrix and a second matrix, and split the second matrix into a first preset number of matrix blocks, and then call the Montgomery modular multiplication and addition instruction to operate the elements included in the first matrix with the elements included in the j-th matrix block to obtain the matrix block operation result corresponding to the j-th matrix block, and use the matrix block operation result corresponding to the j-th matrix block to overwrite the elements in the j-th matrix block, then increase j by 1, and continue to execute the above-mentioned steps of obtaining the matrix block operation result until j is equal to the first preset number, so as to obtain the target matrix after the matrix multiplication operation of the first matrix and the second matrix. In this way, a high-performance matrix modular multiplication algorithm based on Montgomery modular multiplication and addition is provided, which obtains a target matrix after the matrix multiplication operation by splitting the second matrix into multiple matrix blocks and then using the operation results with the first matrix to cover the original elements in the matrix blocks, thereby simplifying the operation process of the matrix multiplication operation and reducing the operation complexity; in addition, the complex operation between the elements included in the first matrix and the elements included in the j-th matrix block can be implemented by calling the Montgomery modular multiplication and addition instruction, thereby obtaining the target matrix after the final matrix multiplication operation, effectively utilizing the advantages of the batch processing of the Montgomery modular multiplication and addition instructions, improving the operating efficiency of the processor performing the matrix multiplication operation, thereby improving the data processing efficiency and saving the operation time of the matrix modular multiplication operation. BRIEF DESCRIPTION OF THE DRAWINGS
[0023] Figure 1 This is a schematic diagram of a data processing scenario provided by an embodiment of this specification;
[0024] Figure 2A is a flow chart of a data processing method provided by an embodiment of this specification;
[0025] Figure 2B is a flow chart of a calculation process provided by an embodiment of this specification;
[0026] Figure 2C is a flow chart of another operation process provided by an embodiment of this specification;
[0027] Figure 2D is a schematic diagram of a calculation process provided by an embodiment of this specification;
[0028] Figure 3A is a flow chart of another data processing method provided by an embodiment of this specification;
[0029] Figure 3B is a schematic diagram of another operation process provided by an embodiment of this specification;
[0030] Figure 4 is a flow chart of another data processing method provided by an embodiment of this specification;
[0031] Figure 5 is a structural schematic diagram of a data processing device provided by an embodiment of this specification;
[0032] Figure 6 It is a structural block diagram of a computing device provided by an embodiment of this specification. DETAILED DESCRIPTION
[0033] Many specific details are described in the following description to facilitate a full understanding of this specification. However, this specification can be implemented in many other ways than those described herein, and those skilled in the art can make similar generalizations without violating the connotation of this specification, so this specification is not limited to the specific implementation disclosed below.
[0034] The terms used in one or more embodiments of this specification are only for the purpose of describing specific embodiments, and are not intended to limit one or more embodiments of this specification. The singular forms of "a", "said" and "the" used in one or more embodiments of this specification and the appended claims are also intended to include plural forms, unless the context clearly indicates other meanings. It should also be understood that the term "and / or" used in one or more embodiments of this specification refers to and includes any or all possible combinations of one or more associated listed items.
[0035] It should be understood that although the terms first, second, etc. may be used to describe various information in one or more embodiments of this specification, this information should not be limited to these terms. These terms are only used to distinguish the same type of information from each other. For example, without departing from the scope of one or more embodiments of this specification, the first may also be referred to as the second, and similarly, the second may also be referred to as the first. Depending on the context, the word "if" as used herein may be interpreted as "at the time of" or "when" or "in response to determining".
[0036] First, the terms involved in one or more embodiments of this specification are explained.
[0037] Blockchain: is a new type of decentralized distributed data system, a database with data "hash verification" function. Blocks are data blocks that are combined into a chain structure in chronological order, and cryptographic algorithms are used to collectively maintain the reliability of the database in a distributed accounting manner. All data blocks are connected in chronological order to form a blockchain, which combines multiple technologies such as consensus mechanism, encryption algorithm, and peer-to-peer transmission.
[0038] Poseidon Hash: A new hash function used in zero-knowledge proof systems. The constraint complexity of a zero-knowledge proof system using Poseidon can be reduced by 8 times compared to Pedersen Hash.
[0039] Zero-knowledge proof: The prover can convince the verifier that a statement is correct without providing any useful information to the verifier.
[0040] Filecoin: It is a decentralized storage solution initiated by Protocol Labs and a blockchain implementation of the IPFS Interstellar File System.
[0041] Instructions: Instructions are the bridge between software and hardware. The design of instructions determines the design complexity and performance of software and hardware.
[0042] Dedicated instructions: instructions for dedicated processors designed for specific application fields, which can accelerate algorithms in specific application fields. The dedicated instructions in the embodiments of this specification are specially designed for the Poseidon Hash algorithm.
[0043] Montgomery modular multiply-add instruction: An instruction designed specifically for the Poseidon Hash algorithm that simultaneously performs Montgomery domain multiplication and addition operations.
[0044] As a new hash function, Poseidon Hash is widely used in the field of blockchain and privacy protection. For example, IPFS / Filecoin blockchain and Loopring projects use Poseidon Hash as the core hash function to improve their security. The core calculation in Poseidon Hash is matrix multiplication. How to improve the execution efficiency of matrix multiplication is the main problem. How to use pipelined modular multiplication operation components and related instructions is the key to improving performance. Therefore, the embodiment of this specification provides a high-performance matrix modular multiplication algorithm based on Montgomery modular multiplication and addition, which effectively utilizes the advantages of dedicated instruction batch processing and greatly improves the operating efficiency of the matrix multiplication operation component.
[0045] In this specification, a data processing method is provided. This specification also relates to a data processing apparatus, a computing device, and a computer-readable storage medium, which are described in detail one by one in the following embodiments.
[0046] Figure 1 A schematic diagram of a data processing scenario provided according to an embodiment of this specification is shown, such as Figure 1As shown, the processor is a processor that performs matrix multiplication operations. The data processing method for matrix multiplication operations provided in the embodiments of this specification can improve the operating efficiency of the processor that performs matrix multiplication operations, thereby improving the data processing efficiency of the processor and saving the operation time of the matrix multiplication operations.
[0047] The execution time of the Poseidon Hash (Precommit2) stage in Filecoin on a single processor is about 20 minutes. The embodiment of this specification provides a high-performance matrix multiplication processing method based on Montgomery modular multiplication and addition, which can shorten the execution time of the Precommit2 stage on a single processor to about 10 minutes. The core calculation of the Poseidon Hash algorithm is the matrix multiplication algorithm. Improving the performance of the matrix multiplication algorithm plays a key role in improving the operating efficiency of the processor.
[0048] It should be noted that the data processing method provided in the embodiments of this specification is applied to the matrix multiplication algorithm. Currently, the matrix multiplication algorithm is involved in multiple scenarios, such as the Poseidon Hash algorithm in the field of blockchain and privacy protection. In the field of privacy protection, when encrypting user data information, the matrix multiplication algorithm may be involved, that is, the user's data information can be converted into a matrix, and then encrypted by matrix multiplication, thereby protecting the user's data security; or when encrypting private pictures uploaded by users, the matrix multiplication algorithm may also be involved, that is, the data in the pictures uploaded by users can be extracted, the data of the pictures can be converted into a matrix, and then encrypted by matrix multiplication, thereby protecting the user's data security. Therefore, matrix multiplication operations are involved in different scenarios, and the data processing method provided in the embodiments of this specification can be applied to matrix multiplication operations involved in various scenarios.
[0049] Figure 2A A flow chart of a data processing method provided according to an embodiment of the present specification is shown, including steps S1 to S4.
[0050] Step S1: determining a first matrix and a second matrix, and splitting the second matrix into a first preset number of matrix blocks.
[0051] Specifically, the first matrix and the second matrix may refer to two matrices waiting for matrix multiplication operations, and the first matrix and the second matrix are both stored in columns. It should be noted that the core calculation in Poseidon Hash is a matrix multiplication operation, and it may be a matrix multiplication operation based on large integer modular multiplication, or it may also be a sparse matrix modular multiplication operation, that is, the elements included in the matrix that needs to be matrix multiplied can be large integers, that is, the length of the element is relatively long, such as the elements included in the matrix that needs to be matrix multiplied can be 256 bits of data. In addition, the first matrix and the second matrix can be small-scale matrices, that is, the rows and columns of the first matrix and the second matrix can be less than a preset threshold. Among them, matrix multiplication refers to the modular multiplication operation of two matrices.
[0052] In addition, since the two matrices can be multiplied, the columns of the first matrix need to be equal to the rows of the second matrix, and since in the embodiment of this specification, the second matrix is split into a first preset number of matrix blocks, and then operations are performed on each row of the data blocks obtained by splitting the first matrix and the second matrix in sequence, the rows of the first matrix and the rows of the second matrix are also the same. In other words, the first matrix is a square matrix, which includes the same rows and columns, and the rows of the second matrix are also the same as the rows of the first matrix.
[0053] For example, the determined first matrix is a 12*12 matrix, and the determined second matrix is a 12*32 matrix.
[0054] It should be noted that the processor that performs Montgomery modular multiplication and addition can be a fully pipelined computing unit. To efficiently utilize the computing unit, there needs to be sufficient multiplication and addition operations that can be executed in parallel. The original matrix multiplication algorithm needs to be optimized to utilize this feature of the computing unit, thereby improving the operating efficiency of the computing unit.
[0055] In an optional implementation of this embodiment, when performing a matrix multiplication operation on the first matrix and the second matrix, after splitting the second matrix into a plurality of matrix blocks, the first matrix can be respectively operated with each matrix block obtained by splitting. When performing operations on the first matrix and the matrix blocks obtained by splitting, the matrix blocks can be stored in a buffer space. In order to improve the space utilization of the buffer space and save storage resource overhead, it is necessary to store as many columns of elements as possible in the buffer space, that is, according to the size of the buffer space, it can be determined how many data blocks the second matrix is split into, so the second matrix is split into a first preset number of matrix blocks. The implementation process can be as follows:
[0056] determining a buffer capacity of the buffer space;
[0057] Determining the number of storage columns of the buffer space for the second matrix according to the buffer capacity;
[0058] Determining the first preset number according to the total number of columns of the second matrix and the number of stored columns;
[0059] The second matrix is split into the first preset number of matrix blocks, each of the matrix blocks includes a second preset number of column elements.
[0060] Specifically, the buffer space is a space used to temporarily store matrix blocks, and the buffer capacity refers to the size of the buffer space. According to the size of the buffer space, the maximum number of columns of the second matrix that the buffer space can store can be determined, that is, the number of storage columns of the second matrix in the buffer space. The total number of columns of the second matrix is divided by the number of storage columns, and the number of data blocks that the second matrix needs to be split can be obtained.
[0061] For example, the second matrix is a 12*32 matrix, that is, the second matrix includes 32 columns of elements. Assuming that the number of storage columns of the buffer space for the second matrix is (that is, the size of the buffer space can store at most) 2 columns of elements, the second matrix can be split into 16 matrix blocks at this time; or, assuming that the number of storage columns of the buffer space for the second matrix is 4 columns of elements, the second matrix can be split into 8 matrix blocks at this time.
[0062] In an optional implementation of this embodiment, after splitting the second matrix into a first preset number of matrix blocks, the matrix blocks to be operated with the first matrix can be stored in a buffer space for subsequent operations, that is, calling a Montgomery modular multiplication-add instruction to operate the elements included in the first matrix with the elements included in the j-th matrix block, and before obtaining the matrix block operation result corresponding to the j-th matrix block, the method further includes:
[0063] The j-th matrix block is stored in a buffer space.
[0064] In an embodiment of the present specification, after determining the first matrix and the second matrix, the second matrix can be split into a first preset number of matrix blocks, and then the matrix blocks to be operated can be stored in a buffer space, so as to facilitate the subsequent operation of the first matrix with the data block alone, and use the operation result with the first matrix to overwrite the original elements in the matrix block, that is, the data blocks stored in the buffer space are updated, and the data stored in the buffer space is continuously used, making full use of the data reuse in the matrix multiplication algorithm, and the number of columns of the matrix blocks stored in the buffer space is the maximum number of matrix columns that can be stored in the buffer space, thereby maximizing the savings in storage resource overhead.
[0065] Step S2: calling the Montgomery modular multiply-add instruction to operate the elements included in the first matrix and the elements included in the j-th matrix block to obtain the matrix block operation result corresponding to the j-th matrix block, where j is equal to 1.
[0066] Among them, the Montgomery modular multiplication and addition instruction is a pre-defined special instruction that can simultaneously implement the multiplication and addition operations of the Montgomery domain. The Montgomery domain is obtained by converting the constant domain through the Montgomery modular multiplication calculation. It should be noted that modular multiplication requires multiplication and division operations, and the operation is relatively complex. The Montgomery algorithm converts modular multiplication into multiplication, addition, shift and other operations.
[0067] Specifically, on the basis of splitting the second matrix into a first preset number of matrix blocks, further, a Montgomery modular multiplication-addition instruction can be called to operate the elements included in the first matrix with the elements included in the j-th matrix block to obtain a matrix block operation result corresponding to the j-th matrix block, where j is equal to 1. In addition, the Montgomery modular multiplication-addition instruction is a predefined special instruction that can simultaneously implement Montgomery domain multiplication and addition operations.
[0068] In an optional implementation of this embodiment, a Montgomery modular multiplication-addition instruction may be pre-defined to implement operations between the first matrix and each matrix block, that is, calling the Montgomery modular multiplication-addition instruction to operate the elements included in the first matrix with the elements included in the j-th matrix block to obtain the matrix block operation result corresponding to the j-th matrix block, further comprising:
[0069] The Montgomery modular multiply-add instruction is set, and the Montgomery modular multiply-add instruction includes an operation type identifier, a first source operand, a second source operand, a third source operand, and a target operand.
[0070] Specifically, the operation type identifier can be the type of operation to be implemented by the Montgomery modular multiply-add instruction, such as the operation type identifier can be a multiplication-add operation, a multiplication operation, or an addition operation; the first source operand, the second source operand, and the third source operand can be the data sources that need to be calculated by the Montgomery modular multiply-add instruction, and the target operand can be the result obtained after performing the corresponding operation, that is, the calculation result.
[0071] In this specification, special instructions for performing operations on the first matrix and the second matrix, namely, Montgomery modular multiplication-add instructions, can be customized in advance. Subsequently, the multiplication and addition operations in the Montgomery domain can be simultaneously implemented through the customized Montgomery modular multiplication-add instructions to perform complex operations between the elements included in the first matrix and the elements included in the j-th matrix block, thereby obtaining the target matrix after the final matrix multiplication operation, effectively utilizing the advantages of batch processing of Montgomery modular multiplication-add instructions, improving the operating efficiency of the processor performing matrix multiplication operations, thereby improving data processing efficiency, and saving matrix operation time for matrix multiplication operations.
[0072] In an optional implementation of this embodiment, the Montgomery modular multiplication-addition instruction is called to operate the elements included in the first matrix and the elements included in the j-th matrix block to obtain the matrix block operation result corresponding to the j-th matrix block. The implementation process may be as follows:
[0073] Reading the j-th matrix block from the buffer space;
[0074] The Montgomery modular multiply-add instruction is called to operate the elements included in the first matrix and the elements included in the read j-th matrix block to obtain a matrix block operation result corresponding to the j-th matrix block.
[0075] It should be noted that after splitting the second matrix into a first preset number of matrix blocks, the matrix blocks to be operated with the first matrix can be stored in the buffer space. Therefore, when it is necessary to operate the first matrix and a certain matrix block, the corresponding data blocks can be obtained from the buffer space and then subsequent operations can be performed.
[0076] Figure 2B is a flowchart of a calculation process provided by an embodiment of the present specification. In an optional implementation of the present embodiment, in the process of calculating the elements included in the first matrix and the elements included in the j-th matrix block, the elements included in the first matrix and the elements included in the j-th matrix block can be calculated row by row, such as Figure 2B As shown, the first matrix includes a second preset number of row elements;
[0077] Correspondingly, the calling of the Montgomery modular multiply-add instruction operates the elements included in the first matrix and the elements included in the j-th matrix block to obtain the matrix block operation result corresponding to the j-th matrix block. The implementation process may include the following steps S21-S25:
[0078] Step S21: setting the initial intermediate results corresponding to the elements of each column in the j-th matrix block, wherein each element included in the initial intermediate results is set to 0.
[0079] Step S22: Calculate all elements of the i-th row of the first matrix and the elements included in the j-th matrix block to obtain the target intermediate result corresponding to the i-th row, where i is equal to 1.
[0080] Step S23: Determine whether i is equal to a second preset number, if not, execute step S24, if yes, execute step S25.
[0081] Step S24: determine the target intermediate result corresponding to the i-th row as the initial intermediate result, increment i by 1, and continue to execute step S22.
[0082] Step S25: Determine the target intermediate result corresponding to the i-th row as the matrix block operation result corresponding to the j-th matrix block.
[0083] It should be noted that, for the first row, the target intermediate result corresponding to the first row can be obtained by operating the elements of the first row of the first matrix and the j-th matrix block. Since there is no data before the first row, it is not necessary to combine with the previous data, so the elements included in the initial intermediate result can be set to 0, and the target intermediate result obtained for the first row can be combined with the initial intermediate result, and the target intermediate result corresponding to the first row can be determined as the initial intermediate result, that is, the initial intermediate result is updated according to the target intermediate result corresponding to the first row, so that when the second row is operated later, the operation result of the first row can be combined. Therefore, for the second row, the target intermediate result corresponding to the second row can be obtained by operating the elements of the second row of the first matrix and the j-th matrix block, and then the initial intermediate result can be updated according to the target intermediate result corresponding to the second row, until the target intermediate result corresponding to the last row is obtained, that is, the matrix block operation result corresponding to the j-th matrix block.
[0084] In another possible implementation, the initial intermediate result may not be set in advance, and the 1st row and the jth matrix block of the first matrix may be directly operated to obtain the target intermediate result corresponding to the 1st row. At this time, the target intermediate result corresponding to the 1st row may be set as the initial intermediate result, and then the 2nd row and the jth matrix block of the first matrix may be operated to obtain the target intermediate result corresponding to the 2nd row. The initial intermediate result may then be updated according to the target intermediate result corresponding to the 2nd row for subsequent operations.
[0085] Figure 2C is a flowchart of another operation process provided by an embodiment of the present specification. In an optional implementation of the present embodiment, in the process of operating all elements of the i-th row of the first matrix with the elements included in the j-th matrix block, the elements included in the first matrix and the elements included in the j-th matrix block may be operated column by column, such as Figure 2C As shown, each of the matrix blocks includes a third preset number of column elements;
[0086] Correspondingly, the operation of operating all elements of the i-th row of the first matrix with the elements included in the j-th matrix block to obtain the target intermediate result corresponding to the i-th row may be implemented as follows:
[0087] S221, multiply all elements of the i-th row of the first matrix by the elements of the i-th row and the k-th column of the j-th matrix block to obtain a reference intermediate result corresponding to the elements of the k-th column, where k is 1;
[0088] S222, adding the reference intermediate result corresponding to the element in the k-th column to the initial intermediate result corresponding to the element in the k-th column to obtain the target intermediate result corresponding to the element in the k-th column;
[0089] S223, determining whether k is equal to a third preset number, if not, incrementing k by 1 and continuing to step S221, if yes, executing step S224;
[0090] S224, determining each of the obtained target intermediate results as the target intermediate result corresponding to the i-th row.
[0091] It should be noted that, for a certain row, all elements of the row can be multiplied with the elements of the first column of the row in the jth matrix block to obtain a reference intermediate result corresponding to the elements of the first column, and then the reference intermediate result corresponding to the elements of the first column is added to the initial intermediate result corresponding to the elements of the first column to obtain the target intermediate result corresponding to the elements of the first column, until the calculation of each column element in the matrix block is completed, the corresponding target intermediate result can be obtained, and the target intermediate result corresponding to each column element obtained at this time is the target intermediate result corresponding to the row.
[0092] In addition, for a certain matrix block, each column element can be pre-set with a corresponding initial intermediate result, so as to facilitate the subsequent addition of the reference intermediate result corresponding to the k-th column element and the initial intermediate result corresponding to the k-th column element to obtain the target intermediate result corresponding to the k-th column element.
[0093] Furthermore, in the process of determining the target intermediate result corresponding to the i-th row as the initial intermediate result, the target intermediate result corresponding to the k-th column of the i-th row can be determined as the initial intermediate result corresponding to the k-th column, that is, the target intermediate result corresponding to a column is used to update the initial intermediate result corresponding to the elements of the column.
[0094] For example, Figure 2D is a schematic diagram of a calculation process provided by an embodiment of this specification, such as Figure 2D As shown, the first matrix is a 3*3 matrix A, the second matrix is a 3*4 matrix B, and the matrix B is split into 2 matrix blocks, each matrix block includes 2 columns of elements, that is, the first preset number is 2, the second preset number is 3, and the third preset number is 2. For the first matrix block, the initial intermediate result 1 corresponding to the first column element of the matrix block and the initial intermediate result 2 corresponding to the second column element of the matrix block are preset, and each element included in the initial intermediate result is set to 0.
[0095] For the elements in the first row (i.e., i is equal to 1), let k be equal to 1, multiply all the elements in the first row of matrix A with the elements in the first row and first column of the matrix block, and obtain the reference intermediate result 1 corresponding to the elements in the first column, add the reference intermediate result 1 and the initial intermediate result 1 to obtain the target intermediate result 1; since the current k is equal to 1, which is not equal to the third preset number, let k be incremented by 1, multiply all the elements in the first row of matrix A with the elements in the first row and second column of the matrix block, and obtain the reference intermediate result 2 corresponding to the elements in the second column, add the reference intermediate result 2 and the initial intermediate result 2 to obtain the target intermediate result 2, and since the current k is equal to the third preset number, the obtained target intermediate result 1 and target intermediate result 2 are determined as the target intermediate results corresponding to the first row.
[0096] Since i is equal to 1 at this time, which is not equal to the second preset number, the target intermediate result corresponding to the first row is determined as the initial intermediate result, that is, the target intermediate result corresponding to the first row and the first column is determined as the initial intermediate result corresponding to the elements of the first column, and the target intermediate result corresponding to the first row and the second column is determined as the initial intermediate result corresponding to the elements of the second column. At this time, the initial intermediate result 1 is the target intermediate result 1, and the initial intermediate result 2 is the target intermediate result 2. Then let i be incremented by 1, multiply all elements of the 2nd row of matrix A by the elements of the 2nd row and 1st column in the matrix block, and obtain the reference intermediate result 3 corresponding to the elements of the 1st column, add the reference intermediate result 3 and the initial intermediate result 1 (target intermediate result 1) to obtain the target intermediate result 3; since the current k is equal to 1, which is not equal to the third preset number, let k be incremented by 1, multiply all elements of the 2nd row of matrix A by the elements of the 2nd row and 2nd column in the matrix block, and obtain the reference intermediate result 4 corresponding to the elements of the 2nd column, add the reference intermediate result 4 and the initial intermediate result 2 (target intermediate result 2) to obtain the target intermediate result 4, and since the current k is equal to the third preset number, the obtained target intermediate result 3 and target intermediate result 4 are determined as the target intermediate results corresponding to the 2nd row.
[0097] Since i is equal to 2 at this time, which is not equal to the second preset number, the target intermediate result corresponding to the second row is determined as the initial intermediate result, that is, the target intermediate result corresponding to the first column of the second row is determined as the initial intermediate result corresponding to the elements of the first column, and the target intermediate result corresponding to the second row and the second column is determined as the initial intermediate result corresponding to the elements of the second column. At this time, the initial intermediate result 1 is the target intermediate result 3, and the initial intermediate result 2 is the target intermediate result 4. Then let i be incremented by 1, multiply all elements of the 3rd row of matrix A by the elements of the 3rd row and 1st column in the matrix block, and obtain the reference intermediate result 5 corresponding to the elements of the 1st column, add the reference intermediate result 5 and the initial intermediate result 1 (target intermediate result 3) to obtain the target intermediate result 5; since the current k is equal to 1, which is not equal to the third preset number, let k be incremented by 1, multiply all elements of the 3rd row of matrix A by the elements of the 3rd row and 2nd column in the matrix block, and obtain the reference intermediate result 6 corresponding to the elements of the 2nd column, add the reference intermediate result 6 and the initial intermediate result 2 (target intermediate result 4) to obtain the target intermediate result 6, and since the current k is equal to the third preset number, the obtained target intermediate result 5 and target intermediate result 6 are determined as the target intermediate results corresponding to the 3rd row.
[0098] Since i is equal to the second preset number at this time, the target intermediate result corresponding to the 3rd row is determined as the matrix block operation result corresponding to the 1st matrix block, that is, the matrix block operation result corresponding to the 1st matrix block at this time is the target intermediate result 5 and the target intermediate result 6.
[0099] By repeating the above operation for the second matrix block, the matrix block operation result corresponding to the second matrix block can be obtained, thereby obtaining the target matrix after the matrix multiplication operation.
[0100] In an optional implementation of this embodiment, a Montgomery modular multiplication-addition instruction is pre-defined, so each of the above-mentioned operation processes can be implemented by calling the Montgomery modular multiplication-addition instruction, that is, all elements of the i-th row of the first matrix are operated with the elements included in the j-th matrix block to obtain the target intermediate result corresponding to the i-th row. The implementation process can be as follows:
[0101] Determine the operation type identifier, the first source operand, the second source operand, and the third source operand according to a calculation process of all elements of the i-th row of the first matrix and elements included in the j-th matrix block;
[0102] Calling the Montgomery modular multiply-add instruction according to the operation type identifier, the first source operand, the second source operand and the third source operand to execute step S221 and step S222;
[0103] The target operand obtained after executing the Montgomery modular multiply-add instruction is the target intermediate result.
[0104] It should be noted that the operation to be performed is the operation of all elements of the i-th row of the first matrix and the elements included in the j-th matrix block. Since the operation process of all elements of the i-th row of the first matrix and the elements included in the j-th matrix block includes the above steps S221 and S222, it is necessary to determine the parameters required in the Montgomery modular multiplication and addition instruction, namely, the operation type identifier, the first source operand, the second source operand and the third source operand according to steps S221 and S222. After the operation type identifier, the first source operand, the second source operand and the third source operand are determined, the Montgomery modular multiplication and addition instruction can be called according to the operation type identifier, the first source operand, the second source operand and the third source operand to execute the operations of the above steps S221 and S222 to obtain the corresponding target intermediate result.
[0105] In an optional implementation of this embodiment, the operation type identifier, the first source operand, the second source operand, and the third source operand are determined according to the operation process of all elements of the i-th row of the first matrix and the elements included in the j-th matrix block. The implementation process may be as follows:
[0106] According to steps S221 and S222 included in the operation process of all elements of the i-th row of the first matrix and elements included in the j-th matrix block, the operation type identifier is determined to be a multiplication-addition operation;
[0107] The initial intermediate result is determined as the first source operand, all elements of the i-th row of the first matrix are determined as the second source operands, and the elements of the i-th row and k-th column in the j-th matrix block are determined as the third source operand.
[0108] It should be noted that, since the above step S221 is an operation step corresponding to a multiplication operation, and step S222 is an operation step corresponding to an addition operation, the operation process of all elements of the i-th row of the first matrix and the elements included in the j-th matrix block includes multiplication and addition operations, and the operation type identifier can be determined as a multiplication-addition operation. In addition, step S221 is to multiply all elements of the i-th row of the first matrix with the elements of the i-th row and k-th column in the j-th matrix block, and at this time, all elements of the i-th row of the first matrix can be determined as the second source operand, and the elements of the i-th row and k-th column in the j-th matrix block can be determined as the third source operand, and step S222 is to add the result of step S221 to the initial intermediate result, so that the initial intermediate result can be determined as the first source operand, and the target operand obtained after executing the Montgomery modular multiplication-addition instruction is the target intermediate result corresponding to the i-th row.
[0109] The embodiments of this specification provide a high-performance matrix multiplication algorithm based on Montgomery modular multiplication and addition, which simplifies the matrix multiplication operation process and reduces the operation complexity by splitting the second matrix into multiple matrix blocks and performing operations on each of the blocks with the first matrix. In addition, a dedicated Montgomery modular multiplication and addition instruction can be customized in advance, and the complex operation between the elements included in the first matrix and the elements included in the jth matrix block can be implemented by calling the Montgomery modular multiplication and addition instruction to obtain the target matrix after the final Montgomery modular multiplication and addition operation, effectively utilizing the advantages of the batch processing of the Montgomery modular multiplication and addition instruction, improving the operating efficiency of the processor performing the matrix multiplication operation, thereby improving the data processing efficiency and saving the operation time of the matrix multiplication operation.
[0110] Step S3: Overwrite the elements in the j-th matrix block with the matrix block operation result corresponding to the j-th matrix block.
[0111] Specifically, on the basis of calling the Montgomery modular multiply-add instruction to operate the elements included in the first matrix with the elements included in the j-th matrix block to obtain the matrix block operation result corresponding to the j-th matrix block, the matrix block operation result corresponding to the j-th matrix block can further overwrite the elements in the j-th matrix block.
[0112] It should be noted that the determined matrix block operation result corresponding to the j-th matrix block may include the target intermediate result corresponding to each column element in the matrix block. Therefore, when the matrix block operation result corresponding to the j-th matrix block is used to cover the elements in the j-th matrix block, the target intermediate result corresponding to the k-th column element in the matrix block operation result corresponding to the j-th matrix block can be used to replace the k-th column element in the j-th matrix block.
[0113] Using the above example, Figure 2D As shown, for the first matrix block, the matrix block operation results obtained are the target intermediate result 5 and the target intermediate result 6. The target intermediate result 5 is the target intermediate result corresponding to the elements in the first column of the first matrix block, and the target intermediate result 6 is the target intermediate result corresponding to the elements in the second column of the first matrix block. Therefore, at this time, the target intermediate result 5 can be used to cover the elements in the first column of the first matrix block, and the target intermediate result 6 can be used to cover the elements in the second column of the first matrix block to obtain the updated first matrix block.
[0114] The embodiments of this specification provide a high-performance matrix modular multiplication algorithm based on Montgomery modular multiplication and addition, which can use the operation results of the matrix block and the first matrix to overwrite the original elements in the matrix block, thereby obtaining the target matrix after the matrix multiplication operation, simplifying the matrix multiplication operation process, reducing the operation complexity, and the algorithm is concise and can be applied to a variety of small-scale matrix multiplication operations, and improve the operating efficiency of the processor performing the matrix multiplication operation, thereby improving data processing efficiency and saving the operation time of the matrix multiplication operation.
[0115] Step S4: Increment j by 1 and continue to execute step S2 until j is equal to the first preset number, thereby obtaining a target matrix after matrix multiplication of the first matrix and the second matrix.
[0116] Specifically, on the basis of covering the elements in the j-th matrix block with the matrix block operation result corresponding to the j-th matrix block, j can be further increased by 1, and the above step S2 can be continued until j is equal to the first preset number, so as to obtain the target matrix after the matrix multiplication operation of the first matrix and the second matrix.
[0117] It should be noted that after covering the elements in the first matrix block to obtain the updated first matrix block, the above operation process can be repeated on the second matrix block to cover the elements in the second matrix block to obtain the updated second matrix block, until all the split matrix blocks are covered. At this time, the operation between the first matrix and the second matrix is completed, and the updated matrix blocks obtained after merging are the target matrix after the matrix multiplication operation of the first matrix and the second matrix.
[0118] An embodiment of the present specification provides a data processing method, which can first determine a first matrix and a second matrix, and split the second matrix into a first preset number of matrix blocks, and then call the Montgomery modular multiplication and addition instruction to operate the elements included in the first matrix with the elements included in the j-th matrix block to obtain the matrix block operation result corresponding to the j-th matrix block, and use the matrix block operation result corresponding to the j-th matrix block to overwrite the elements in the j-th matrix block, then increase j by 1, and continue to execute the above-mentioned steps of obtaining the matrix block operation result until j is equal to the first preset number, so as to obtain the target matrix after the matrix multiplication operation of the first matrix and the second matrix. In this way, a high-performance matrix modular multiplication algorithm based on Montgomery modular multiplication and addition is provided, which obtains a target matrix after the matrix multiplication operation by splitting the second matrix into multiple matrix blocks and then using the operation results with the first matrix to cover the original elements in the matrix blocks, thereby simplifying the operation process of the matrix multiplication operation and reducing the operation complexity; in addition, the complex operation between the elements included in the first matrix and the elements included in the j-th matrix block can be implemented by calling the Montgomery modular multiplication and addition instruction, thereby obtaining the target matrix after the final matrix multiplication operation, effectively utilizing the advantages of the batch processing of the Montgomery modular multiplication and addition instructions, improving the operating efficiency of the processor performing the matrix multiplication operation, thereby improving the data processing efficiency and saving the operation time of the matrix modular multiplication operation.
[0119] Figure 3A A flowchart of another data processing method provided according to an embodiment of this specification is shown. Figure 3A As shown, the method includes:
[0120] Step 302: Determine a first matrix and a second matrix, and split the second matrix into a first preset number of matrix blocks, wherein the first matrix includes a second preset number of row elements, and each of the matrix blocks includes a third preset number of column elements.
[0121] Step 304: Multiply all elements of the 1st row of the first matrix with elements of the 1st row and the kth column of the jth matrix block to obtain a reference intermediate result corresponding to the elements of the kth column, where k is equal to 1 and j is equal to 1.
[0122] Step 306: Determine whether k is equal to the third preset number. If not, increment k by 1 and continue to execute step 304. If so, execute step 308.
[0123] Step 308: Determine the obtained reference intermediate results corresponding to the elements in each column as the initial intermediate results corresponding to the elements in each column.
[0124] Step 310: Set k to 1.
[0125] Step 312: Multiply all elements of the i-th row of the first matrix with the elements of the i-th row and k-th column in the j-th matrix block to obtain a reference intermediate result corresponding to the elements of the k-th column, where i is equal to 2.
[0126] Step 314: Add the reference intermediate result corresponding to the element in the k-th column to the initial intermediate result corresponding to the element in the k-th column to obtain the target intermediate result corresponding to the element in the k-th column.
[0127] Step 316: Determine whether k is equal to the third preset number. If not, increment k by 1 and continue to execute step 312. If so, execute step 318.
[0128] Step 318: Determine the target intermediate result corresponding to the i-th row from each target intermediate result obtained.
[0129] Step 320: Determine whether i is equal to a second preset number, if not, execute step 322, if yes, execute step 324.
[0130] Step 322: determine the target intermediate result corresponding to the i-th row as the initial intermediate result, increment i by 1, and continue to execute step 310.
[0131] Step 324: Determine the target intermediate result corresponding to the i-th row as the matrix block operation result corresponding to the j-th matrix block.
[0132] Step 326: Overwrite the elements in the j-th matrix block with the matrix block operation result corresponding to the j-th matrix block.
[0133] Step 328: Increment j by 1 and return to step 304 until j is equal to the first preset number, thereby obtaining a target matrix after matrix multiplication of the first matrix and the second matrix.
[0134] It should be noted that the present embodiment describes a process in which the initial intermediate result is not set in advance, but the first row of the first matrix and the jth matrix block are directly operated to obtain the target intermediate result corresponding to the first row, and then the target intermediate result corresponding to the first row is set as the initial intermediate result, and then the second row of the first matrix and the jth matrix block are operated to obtain the target intermediate result corresponding to the second row, and then the initial intermediate result is updated according to the target intermediate result corresponding to the second row, and so on, the initial intermediate result is updated according to the target intermediate result corresponding to each row, until the target intermediate result corresponding to the last row is obtained, and the target intermediate result corresponding to the last row is determined as the matrix operation result corresponding to the matrix block.
[0135] For example, Figure 3B is a schematic diagram of another operation process provided by an embodiment of this specification, such as Figure 3B As shown, the first matrix is a 3*3 matrix A, and the second matrix is a 3*4 matrix B. Matrix B is split into 2 matrix blocks, each of which includes 2 columns of elements, that is, the first preset number is 2, the second preset number is 3, and the third preset number is 2. For the first matrix block, and for the first row of elements (that is, i is equal to 1), let k be 1, multiply all elements of the first row of matrix A with the elements of the first row and the first column in the matrix block, and obtain the reference intermediate result 1 corresponding to the elements of the first column, and determine the reference intermediate result 1 as the initial intermediate result 1; since the current k is equal to 1, which is not equal to the third preset number, k is incremented by 1, and all elements of the first row of matrix A are multiplied with the elements of the first row and the second column in the matrix block, and obtain the reference intermediate result 2 corresponding to the elements of the second column, and determine the reference intermediate result 2 as the initial intermediate result 2. Since the current k is equal to the third preset number, the reference intermediate result 1 and the reference intermediate result 2 obtained at this time can be determined as the target intermediate result corresponding to the first row.
[0136] Let i be incremented by 1, now i is equal to 2, multiply all elements of the 2nd row of matrix A with the elements of the 2nd row and 1st column in the matrix block, and obtain the reference intermediate result 3 corresponding to the elements of the 1st column, add the reference intermediate result 3 and the initial intermediate result 1 to obtain the target intermediate result 1; since the current k is equal to 1, which is not equal to the third preset number, let k be incremented by 1, multiply all elements of the 2nd row of matrix A with the elements of the 2nd row and 2nd column in the matrix block, and obtain the reference intermediate result 4 corresponding to the elements of the 2nd column, add the reference intermediate result 4 and the initial intermediate result 2 to obtain the target intermediate result 2, since the current k is equal to the third preset number, the obtained target intermediate result 1 and target intermediate result 2 are determined as the target intermediate results corresponding to the 2nd row.
[0137] Since i is equal to 2 at this time, which is not equal to the second preset number, the target intermediate result corresponding to the second row is determined as the initial intermediate result, that is, the target intermediate result corresponding to the first column of the second row is determined as the initial intermediate result corresponding to the elements of the first column, and the target intermediate result corresponding to the second row and the second column is determined as the initial intermediate result corresponding to the elements of the second column. At this time, the initial intermediate result 1 is the target intermediate result 1, and the initial intermediate result 2 is the target intermediate result 2. Then let i be incremented by 1, multiply all elements of the 3rd row of matrix A by the elements of the 3rd row and 1st column in the matrix block, and obtain the reference intermediate result 5 corresponding to the elements of the 1st column, add the reference intermediate result 5 and the initial intermediate result 1 (target intermediate result 1) to obtain the target intermediate result 3; since the current k is equal to 1, which is not equal to the third preset number, let k be incremented by 1, multiply all elements of the 3rd row of matrix A by the elements of the 3rd row and 2nd column in the matrix block, and obtain the reference intermediate result 6 corresponding to the elements of the 2nd column, add the reference intermediate result 6 and the initial intermediate result 2 (target intermediate result 2) to obtain the target intermediate result 4, and since the current k is equal to the third preset number, the obtained target intermediate result 3 and target intermediate result 4 are determined as the target intermediate results corresponding to the 3rd row.
[0138] Since i is equal to the second preset number at this time, the target intermediate result corresponding to the 3rd row is determined as the matrix block operation result corresponding to the 1st matrix block, that is, the matrix block operation result corresponding to the 1st matrix block at this time is the target intermediate result 3 and the target intermediate result 4.
[0139] The target intermediate result 3 is used to overwrite the elements in the first column of the first matrix block, and the target intermediate result 4 is used to overwrite the elements in the second column of the first matrix block, to obtain the updated first matrix block.
[0140] By repeating the above operation for the second matrix block, the matrix block operation result corresponding to the second matrix block can be obtained, thereby obtaining the target matrix after the matrix multiplication operation.
[0141] In addition, the operation process described in this embodiment is the same as Figure 2A The calculation process described in the embodiment shown is similar, so the details of the implementation method of directly performing the calculation without presetting the initial intermediate result can be referred to the above Figure 2A The embodiments shown in the specification will not be described in detail here.
[0142] An embodiment of the present specification provides a high-performance matrix modular multiplication algorithm based on Montgomery modular multiplication and addition, which obtains a target matrix after the matrix multiplication operation by splitting the second matrix into multiple matrix blocks and then using the operation results with the first matrix to cover the original elements in the matrix blocks, thereby simplifying the operation process of the matrix multiplication operation and reducing the operation complexity; in addition, the Montgomery modular multiplication and addition instruction can be called to implement complex operations between the elements included in the first matrix and the elements included in the jth matrix block, thereby obtaining the target matrix after the final matrix multiplication operation, effectively utilizing the advantages of Montgomery modular multiplication and addition instruction batch processing, improving the operating efficiency of the processor performing the matrix multiplication operation, thereby improving data processing efficiency and saving matrix operation time for matrix multiplication operations.
[0143] Figure 4 A flowchart of another data processing method provided according to an embodiment of this specification is shown. Figure 4 As shown, the method includes:
[0144] Step 402: Determine a first matrix and a second matrix, and split the second matrix into a first preset number of matrix blocks, wherein the first matrix includes a second preset number of row elements, and each of the matrix blocks includes a third preset number of column elements.
[0145] Step 404: setting the initial intermediate results corresponding to the elements of each column in the j-th matrix block, wherein each element included in the initial intermediate result is set to 0, and j is equal to 1.
[0146] Step 406: Multiply all elements of the i-th row of the first matrix with the elements of the i-th row and k-th column of the j-th matrix block to obtain a reference intermediate result corresponding to the elements of the k-th column, where k is equal to 1.
[0147] Step 408: Add the reference intermediate result corresponding to the element in the k-th column to the initial intermediate result corresponding to the element in the k-th column to obtain the target intermediate result corresponding to the element in the k-th column.
[0148] Step 410: Determine whether k is equal to the third preset number. If not, increment k by 1 and continue to execute step 406. If yes, execute step 412.
[0149] Step 412: Determine the target intermediate result corresponding to the i-th row from each of the obtained target intermediate results.
[0150] Step 414: Determine whether i is equal to a second preset number, if not, execute step 416, if yes, execute step 418.
[0151] Step 416: determine the target intermediate result corresponding to the i-th row as the initial intermediate result, increment i by 1, and continue executing step 406.
[0152] Step 418: Determine the target intermediate result corresponding to the i-th row as the matrix block operation result corresponding to the j-th matrix block.
[0153] Step 420: Overwrite the elements in the j-th matrix block with the matrix block operation result corresponding to the j-th matrix block.
[0154] Step 422: Increment j by 1 and return to step 404 until j is equal to the first preset number, thereby obtaining a target matrix after matrix multiplication of the first matrix and the second matrix.
[0155] It should be noted that, for the 1st row, the 1st row elements of the first matrix and the jth matrix block are operated to obtain the target intermediate result corresponding to the 1st row. Since there is no data before the 1st row, there is no need to combine it with the previous data. Therefore, the elements included in the initial intermediate result can be set to 0, and then the target intermediate result obtained for the 1st row can be combined with the initial intermediate result, and the target intermediate result corresponding to the 1st row can be determined as the initial intermediate result, that is, the initial intermediate result is updated according to the target intermediate result corresponding to the 1st row, and so on. After obtaining the target intermediate result corresponding to each row, the initial intermediate result is updated until the target intermediate result corresponding to the last row is obtained.
[0156] In addition, the operation process described in this embodiment is the same as Figure 2A The calculation process described in the embodiment shown is similar, so the details of the implementation method of presetting the initial intermediate result and then performing the calculation can be referred to the above Figure 2A The embodiments shown in the specification will not be described in detail here.
[0157] An embodiment of the present specification provides a high-performance matrix modular multiplication algorithm based on Montgomery modular multiplication and addition, which obtains a target matrix after the matrix multiplication operation by splitting the second matrix into multiple matrix blocks and then using the operation results with the first matrix to cover the original elements in the matrix blocks, thereby simplifying the matrix multiplication operation process and reducing the operation complexity; in addition, the Montgomery modular multiplication and addition instruction can be called to implement complex operations between the elements included in the first matrix and the elements included in the jth matrix block, thereby obtaining the target matrix after the final matrix multiplication operation, effectively utilizing the advantages of Montgomery modular multiplication and addition instruction batch processing, improving the operating efficiency of the processor performing the matrix multiplication operation, thereby improving data processing efficiency and saving the operation time of the matrix multiplication operation.
[0158] Corresponding to the above method embodiment, this specification also provides a data processing device embodiment, Figure 5FIG. 1 is a schematic diagram showing the structure of a data processing device provided by an embodiment of the present specification. Figure 5 As shown, the device comprises:
[0159] A splitting module 502 is configured to determine a first matrix and a second matrix, and split the second matrix into a first preset number of matrix blocks;
[0160] The calling module 504 is configured to call a Montgomery modular multiply-add instruction to operate the elements included in the first matrix and the elements included in the j-th matrix block to obtain a matrix block operation result corresponding to the j-th matrix block, wherein j is equal to 1, and the Montgomery modular multiply-add instruction is used to simultaneously implement multiplication and addition operations in the Montgomery domain;
[0161] A covering module 506 is configured to cover the elements in the j-th matrix block with the matrix block operation result corresponding to the j-th matrix block;
[0162] The execution module 508 is configured to increment j by 1 and continue to execute the calling module until j is equal to the first preset number, thereby obtaining a target matrix after matrix multiplication of the first matrix and the second matrix.
[0163] Optionally, the first matrix includes a second preset number of row elements;
[0164] Accordingly, the calling module 504 further includes:
[0165] A setting submodule is configured to set an initial intermediate result corresponding to each column element in the j-th matrix block, wherein each element included in the initial intermediate result is set to 0;
[0166] an operation submodule configured to operate all elements of the i-th row of the first matrix with elements included in the j-th matrix block to obtain a target intermediate result corresponding to the i-th row, where i is equal to 1;
[0167] a judging submodule, configured to judge whether i is equal to the second preset number, and if not, to execute the first determining submodule, and if so, to execute the second determining submodule;
[0168] A first determination submodule is configured to determine the target intermediate result corresponding to the i-th row as the initial intermediate result, increment i by 1, and continue to run the operation submodule;
[0169] The second determination submodule is configured to determine the target intermediate result corresponding to the i-th row as the matrix block operation result corresponding to the j-th matrix block.
[0170] Optionally, each of the matrix blocks includes a third preset number of column elements;
[0171] Accordingly, the operator module further includes:
[0172] a multiplication subunit configured to multiply all elements of the i-th row of the first matrix by elements of the i-th row and the k-th column of the j-th matrix block to obtain a reference intermediate result corresponding to the elements of the k-th column, where k is equal to 1;
[0173] an adding subunit, configured to add a reference intermediate result corresponding to the element in the k-th column to an initial intermediate result corresponding to the element in the k-th column to obtain a target intermediate result corresponding to the element in the k-th column;
[0174] A judging subunit is configured to judge whether k is equal to the third preset number, if not, k is incremented by 1, and the multiplication subunit is continued to be executed, and if yes, the following determination subunit is executed;
[0175] The determination subunit is configured to determine each of the obtained target intermediate results as the target intermediate result corresponding to the i-th row.
[0176] Optionally, the device further includes a setting module configured to:
[0177] The Montgomery modular multiply-add instruction is set, and the Montgomery modular multiply-add instruction includes an operation type identifier, a first source operand, a second source operand, a third source operand, and a target operand.
[0178] Optionally, the operator module is further configured as:
[0179] Determine the operation type identifier, the first source operand, the second source operand, and the third source operand according to a calculation process of all elements of the i-th row of the first matrix and elements included in the j-th matrix block;
[0180] Calling the Montgomery modular multiply-add instruction according to the operation type identifier, the first source operand, the second source operand, and the third source operand to run the multiplication subunit and the addition subunit;
[0181] The target operand obtained after executing the Montgomery modular multiply-add instruction is the target intermediate result.
[0182] Optionally, the operator module is further configured as:
[0183] According to the multiplication subunit and the addition subunit running in the operation process of all elements of the i-th row of the first matrix and the elements included in the j-th matrix block, the operation type identifier is determined as a multiplication-addition operation;
[0184] The initial intermediate result is determined as the first source operand, all elements of the i-th row of the first matrix are determined as the second source operands, and the elements of the i-th row and k-th column in the j-th matrix block are determined as the third source operand.
[0185] Optionally, the device further includes a storage module configured to:
[0186] Storing the j-th matrix block in a buffer space;
[0187] Accordingly, the calling module 504 is further configured as follows:
[0188] Reading the j-th matrix block from the buffer space;
[0189] The Montgomery modular multiply-add instruction is called to operate the elements included in the first matrix and the elements included in the read j-th matrix block to obtain a matrix block operation result corresponding to the j-th matrix block.
[0190] Optionally, the splitting module 502 is further configured to:
[0191] determining a buffer capacity of the buffer space;
[0192] Determining the number of storage columns of the buffer space for the second matrix according to the buffer capacity;
[0193] Determining the first preset number according to the total number of columns of the second matrix and the number of stored columns;
[0194] The second matrix is split into the first preset number of matrix blocks, each of the matrix blocks includes a second preset number of column elements.
[0195] An embodiment of the present specification provides a data processing device, which obtains a target matrix after matrix multiplication by splitting a second matrix into multiple matrix blocks and then using the operation results with the first matrix to overwrite the original elements in the matrix blocks, thereby simplifying the operation process of the matrix multiplication operation and reducing the operation complexity; in addition, the Montgomery modular multiplication-add instruction can be called to implement complex operations between the elements included in the first matrix and the elements included in the j-th matrix block, thereby obtaining the target matrix after the final matrix multiplication operation, effectively utilizing the advantages of Montgomery modular multiplication-add instruction batch processing, improving the operating efficiency of the processor performing the matrix multiplication operation, thereby improving data processing efficiency and saving the operation time of the matrix multiplication operation.
[0196] Each embodiment in this specification is described in a progressive manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the data processing device embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.
[0197] Figure 6 The block diagram of a computing device 600 according to an embodiment of the present specification is shown. The components of the computing device 600 include but are not limited to a memory 610 and a processor 620. The processor 620 is connected to the memory 610 via a bus 630, and the database 650 is used to store data.
[0198] The computing device 600 also includes an access device 640 that enables the computing device 600 to communicate via one or more networks 660. Examples of these networks include a public switched telephone network (PSTN), a local area network (LAN), a wide area network (WAN), a personal area network (PAN), or a combination of communication networks such as the Internet. The access device 640 may include one or more of any type of network interface (e.g., a network interface card (NIC)) whether wired or wireless, such as an IEEE 802.11 wireless local area network (WLAN) wireless interface, a World Wide Interoperability for Microwave Access (Wi-MAX) interface, an Ethernet interface, a universal serial bus (USB) interface, a cellular network interface, a Bluetooth interface, a near field communication (NFC) interface, and the like.
[0199] In one embodiment of the present specification, the above components of the computing device 600 and Figure 6 Other components not shown in the figure may also be connected to each other, for example, via a bus. It should be understood that Figure 6 The computing device structure block diagram shown is only for the purpose of illustration, and is not intended to limit the scope of this specification. Those skilled in the art can add or replace other components as needed.
[0200] The computing device 600 may be any type of stationary or mobile computing device, including a mobile computer or mobile computing device (e.g., a tablet computer, a personal digital assistant, a laptop computer, a notebook computer, a netbook, etc.), a mobile phone (e.g., a smart phone), a wearable computing device (e.g., a smart watch, smart glasses, etc.), or other types of mobile devices, or a stationary computing device such as a desktop computer or PC. The computing device 600 may also be a mobile or stationary server.
[0201] The processor 620 is used to execute the following computer executable instructions to implement:
[0202] S1, determining a first matrix and a second matrix, and splitting the second matrix into a first preset number of matrix blocks;
[0203] S2, calling a Montgomery modular multiplication-addition instruction to operate the elements included in the first matrix and the elements included in the j-th matrix block to obtain a matrix block operation result corresponding to the j-th matrix block, wherein j is equal to 1, and the Montgomery modular multiplication-addition instruction is used to simultaneously implement multiplication and addition operations in the Montgomery domain;
[0204] S3, overwriting the elements in the j-th matrix block with the matrix block operation result corresponding to the j-th matrix block;
[0205] S4, increment j by 1, and continue to execute step S2 until j is equal to the first preset number, to obtain a target matrix after matrix multiplication of the first matrix and the second matrix.
[0206] Each embodiment in this specification is described in a progressive manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the computing device embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.
[0207] An embodiment of the present specification further provides a computer-readable storage medium storing computer instructions, which, when executed by a processor, are used to implement the steps of any one of the above-mentioned data processing methods.
[0208] Each embodiment in this specification is described in a progressive manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the computer-readable storage medium embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.
[0209] The above is a description of a specific embodiment of the specification. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recorded in the claims can be performed in an order different from that in the embodiments and still achieve the desired results. In addition, the processes depicted in the drawings do not necessarily require the specific order or continuous order shown to achieve the desired results. In some embodiments, multitasking and parallel processing are also possible or may be advantageous.
[0210] The computer instructions include computer program codes, which may be in source code form, object code form, executable files or some intermediate forms, etc. The computer-readable medium may include: any entity or device capable of carrying the computer program code, recording medium, USB flash drive, mobile hard disk, magnetic disk, optical disk, computer memory, read-only memory (ROM), random access memory (RAM), electric carrier signal, telecommunication signal and software distribution medium, etc. It should be noted that the content contained in the computer-readable medium may be appropriately increased or decreased according to the requirements of legislation and patent practice in the jurisdiction. For example, in some jurisdictions, according to legislation and patent practice, computer-readable media do not include electric carrier signals and telecommunication signals.
[0211] It should be noted that, for the above-mentioned method embodiments, for the sake of simplicity of description, they are all expressed as a series of action combinations, but those skilled in the art should be aware that the embodiments of this specification are not limited by the order of the actions described, because according to the embodiments of this specification, some steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily required by the embodiments of this specification.
[0212] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0213] The preferred embodiments of this specification disclosed above are only used to help explain this specification. The optional embodiments do not describe all the details in detail, nor do they limit the invention to only the specific implementation methods described. Obviously, many modifications and changes can be made according to the content of the embodiments of this specification. This specification selects and specifically describes these embodiments in order to better explain the principles and practical applications of the embodiments of this specification, so that technicians in the relevant technical field can well understand and use this specification. This specification is only limited by the claims and their full scope and equivalents.
Claims
1. A data processing method, comprising: S1, determining a first matrix and a second matrix, and splitting the second matrix into a first preset number of matrix blocks, setting parameter j to be the j-th matrix block, setting parameter i to be the i-th row of the first matrix, and setting parameter k to be the i-th row and k-th column of the j-th matrix block, wherein j is equal to 1, k is equal to 1, and i is equal to 1; S2, calling a Montgomery modular multiplication-addition instruction to operate the elements included in the first matrix with the elements included in the j-th matrix block, to obtain a matrix block operation result corresponding to the j-th matrix block, wherein the Montgomery modular multiplication-addition instruction is used to simultaneously implement multiplication and addition operations in the Montgomery domain, and the Montgomery modular multiplication-addition instruction is a predefined special instruction for performing multiplication-addition operations on the elements included in the first matrix with the elements included in the j-th matrix block; S3, overwriting the elements in the j-th matrix block with the matrix block operation result corresponding to the j-th matrix block; S4, increment j by 1, and continue to execute step S2 until j is equal to the first preset number, to obtain a target matrix after matrix multiplication of the first matrix and the second matrix; Before calling the Montgomery modular multiply-add instruction to operate the elements included in the first matrix and the elements included in the j-th matrix block to obtain the matrix block operation result corresponding to the j-th matrix block, the method further includes: The Montgomery modular multiply-add instruction is set, and the Montgomery modular multiply-add instruction includes an operation type identifier, a first source operand, a second source operand, a third source operand and a target operand, wherein an initial intermediate result corresponding to the elements of each column in the j-th matrix block is determined as the first source operand, and each element included in the initial intermediate result is set to 0, all elements of the i-th row of the first matrix are determined as the second source operand, and the elements of the i-th row and the k-th column in the j-th matrix block are determined as the third source operand, and the target operand is a result obtained after operating the first source operand, the second source operand and the third source operand.
2. The data processing method according to claim 1, wherein the first matrix comprises a second preset number of row elements; Correspondingly, the calling of the Montgomery modular multiply-add instruction to operate the elements included in the first matrix and the elements included in the j-th matrix block to obtain the matrix block operation result corresponding to the j-th matrix block includes: S21, setting the initial intermediate result corresponding to each column element in the j-th matrix block, wherein each element included in the initial intermediate result is set to 0; S22, performing operations on all elements of the i-th row of the first matrix and elements included in the j-th matrix block to obtain a target intermediate result corresponding to the i-th row; S23, determining whether i is equal to the second preset number, if not, executing step S24, if yes, executing step S25; S24, determining the target intermediate result corresponding to the i-th row as the initial intermediate result, incrementing i by 1, and continuing to execute step S22; S25, determining the target intermediate result corresponding to the i-th row as the matrix block operation result corresponding to the j-th matrix block.
3. The data processing method according to claim 2, wherein each of the matrix blocks comprises a third preset number of column elements; Correspondingly, the operation of all elements of the i-th row of the first matrix and the elements included in the j-th matrix block to obtain the target intermediate result corresponding to the i-th row includes: S221, multiply all elements of the i-th row of the first matrix by the elements of the i-th row and the k-th column in the j-th matrix block to obtain a reference intermediate result corresponding to the elements of the k-th column, wherein; S222, adding the reference intermediate result corresponding to the element in the k-th column to the initial intermediate result corresponding to the element in the k-th column to obtain the target intermediate result corresponding to the element in the k-th column; S223, determining whether k is equal to the third preset number, if not, incrementing k by 1 and continuing to execute step S221, if yes, executing step S224; S224, determining each of the obtained target intermediate results as the target intermediate result corresponding to the i-th row.
4. The data processing method according to claim 3, wherein the step of operating all elements of the i-th row of the first matrix with elements included in the j-th matrix block to obtain a target intermediate result corresponding to the i-th row comprises: Determine the operation type identifier, the first source operand, the second source operand, and the third source operand according to a calculation process of all elements of the i-th row of the first matrix and elements included in the j-th matrix block; Calling the Montgomery modular multiply-add instruction according to the operation type identifier, the first source operand, the second source operand and the third source operand to execute step S221 and step S222; The target operand obtained after executing the Montgomery modular multiply-add instruction is the target intermediate result.
5. The data processing method according to claim 4, wherein determining the operation type identifier, the first source operand, the second source operand, and the third source operand according to the operation process of all elements of the i-th row of the first matrix and the elements included in the j-th matrix block comprises: According to step S221 and step S222 included in the operation process of all elements of the i-th row of the first matrix and the elements included in the j-th matrix block, the operation type identifier is determined to be a multiplication-addition operation.
6. The data processing method according to any one of claims 1 to 5, before calling the Montgomery modular multiply-add instruction to operate the elements included in the first matrix with the elements included in the j-th matrix block to obtain the matrix block operation result corresponding to the j-th matrix block, it also includes: Storing the j-th matrix block in a buffer space; Correspondingly, calling the Montgomery modular multiply-add instruction to operate the elements included in the first matrix and the elements included in the j-th matrix block to obtain the matrix block operation result corresponding to the j-th matrix block, including: Reading the j-th matrix block from the buffer space; The Montgomery modular multiply-add instruction is called to operate the elements included in the first matrix and the elements included in the read j-th matrix block to obtain a matrix block operation result corresponding to the j-th matrix block.
7. The data processing method according to claim 6, wherein splitting the second matrix into a first preset number of matrix blocks comprises: determining a buffer capacity of the buffer space; Determining the number of storage columns of the buffer space for the second matrix according to the buffer capacity; Determining the first preset number according to the total number of columns of the second matrix and the number of stored columns; The second matrix is split into the first preset number of matrix blocks, each of the matrix blocks includes a second preset number of column elements.
8. A data processing device, comprising: a splitting module configured to determine a first matrix and a second matrix, and split the second matrix into a first preset number of matrix blocks, let parameter j be the j-th matrix block, let parameter i be the i-th row of the first matrix, let parameter k be the i-th row and k-th column of the j-th matrix block, wherein j is equal to 1, k is equal to 1, and i is equal to 1; A calling module is configured to call a Montgomery modular multiplication-addition instruction to perform operations on elements included in the first matrix and elements included in the j-th matrix block to obtain a matrix block operation result corresponding to the j-th matrix block, wherein the Montgomery modular multiplication-addition instruction is used to simultaneously implement multiplication and addition operations in the Montgomery domain, and the Montgomery modular multiplication-addition instruction is a predefined special instruction for performing multiplication-addition operations on elements included in the first matrix and elements included in the j-th matrix block; A covering module, configured to cover the elements in the j-th matrix block with the matrix block operation result corresponding to the j-th matrix block; an execution module configured to increment j by 1, and continue to execute the calling module until j is equal to the first preset number, to obtain a target matrix after matrix multiplication of the first matrix and the second matrix; The data processing device also includes a setting module, which is configured to set the Montgomery modular multiply-add instruction, which includes an operation type identifier, a first source operand, a second source operand, a third source operand and a target operand, wherein an initial intermediate result corresponding to each column element in the jth matrix block is determined as the first source operand, each element included in the initial intermediate result is set to 0, all elements in the i-th row of the first matrix are determined as the second source operand, and the elements in the i-th row and k-th column of the j-th matrix block are determined as the third source operand, and the target operand is a result obtained after operating the first source operand, the second source operand and the third source operand.
9. A computing device comprising: Memory and processor; The memory is used to store computer executable instructions, and the processor is used to execute the computer executable instructions to implement: S1, determining a first matrix and a second matrix, and splitting the second matrix into a first preset number of matrix blocks, setting parameter j to be the j-th matrix block, setting parameter i to be the i-th row of the first matrix, and setting parameter k to be the i-th row and k-th column of the j-th matrix block, wherein j is equal to 1, k is equal to 1, and i is equal to 1; S2, calling a Montgomery modular multiplication-addition instruction to operate the elements included in the first matrix with the elements included in the j-th matrix block, to obtain a matrix block operation result corresponding to the j-th matrix block, wherein the Montgomery modular multiplication-addition instruction is used to simultaneously implement multiplication and addition operations in the Montgomery domain, and the Montgomery modular multiplication-addition instruction is a predefined special instruction for performing multiplication-addition operations on the elements included in the first matrix with the elements included in the j-th matrix block; S3, overwriting the elements in the j-th matrix block with the matrix block operation result corresponding to the j-th matrix block; S4, increment j by 1, and continue to execute step S2 until j is equal to the first preset number, to obtain a target matrix after matrix multiplication of the first matrix and the second matrix; Before calling the Montgomery modular multiply-add instruction to operate the elements included in the first matrix and the elements included in the j-th matrix block to obtain the matrix block operation result corresponding to the j-th matrix block, the method further includes: The Montgomery modular multiply-add instruction is set, and the Montgomery modular multiply-add instruction includes an operation type identifier, a first source operand, a second source operand, a third source operand and a target operand, wherein an initial intermediate result corresponding to the elements of each column in the j-th matrix block is determined as the first source operand, and each element included in the initial intermediate result is set to 0, all elements of the i-th row of the first matrix are determined as the second source operand, and the elements of the i-th row and the k-th column in the j-th matrix block are determined as the third source operand, and the target operand is a result obtained after operating the first source operand, the second source operand and the third source operand.
10. A computer-readable storage medium storing computer-executable instructions, wherein the computer-executable instructions, when executed by a processor, implement the steps of the data processing method according to any one of claims 1 to 7.
