MatrixMultiplyShaderCode
Package: Libraries.Compute.Hardware.Shaders · Inherits from: Libraries.Language.Object
The MatrixMultiplyShaderCode class writes the GLSL compute shader that multiplies two matrices of Quorum numbers on the graphics card, with one of four arithmetics (GetCode(tiled, size, arithmetic)): DOUBLE_NUMBER_32 HardwareCompute's DOUBLE_NUMBER_32 format, two 32-bit numbers each (DoubleNumber32ShaderCode): about 14 digits, every graphics card. NUMBER_64 HardwareCompute's NUMBER_64 format in GLSL double: cards with 64-bit numbers. EMULATED_NUMBER_64 NUMBER_64, IEEE 754 emulated with 64-bit integers: cards with 64-bit integers. EMULATED_32_NUMBER_64 NUMBER_64, IEEE 754 emulated with 32-bit integers: every graphics card. The three NUMBER_64 arithmetics (Number64ShaderCode) round every step as the CPU does, and each thread adds its products in the CPU's order (k = 0, 1, 2 ..., tiled or not), so their answers equal the CPU's bit for bit. Every matrix is stored column by column, one number (two 32-bit numbers, or one 64-bit number) per element. Where the matrices are, and their sizes, reach the shader with each run, as RUN_INTEGER_COUNT integers (HardwareCompute's run integers, Vulkan push constants), in this order: rowsA rows of A, and of the answer colsA columns of A, which are the rows of B colsB columns of B, and of the answer offsetA where A starts in the buffer, counted in numbers offsetB where B starts offsetC where the answer goes Since every run says where its matrices are, one buffer can hold many, and a chain of multiplies can run on the graphics card without coming back between steps, each step reading the one before's answer. Each thread works out one number of the answer. Work groups are size x size threads; run ceil(rowsA / size) by ceil(colsB / size) by 1 of them. Two ways: plain each thread reads its row of A and column of B from memory. A size of 1 is one thread per group. tiled the group loads size x size tiles of A and B into shared memory together, then multiplies from there: each number is read from memory once per tile instead of once per answer. Needs 2 * size * size * 8 bytes of shared memory. Numbers past an edge are loaded as zero, which adds nothing (exactly); with NUMBER_64 only the products inside the matrix are added at all, so padding never changes a sum, not even a zero's sign. A GLSL work group's size is fixed when it is compiled, so the size is part of the source: each size is its own program.
Example Code
use Libraries.Game.GameStateManager
use Libraries.Compute.Hardware.HardwareComputeDevice
use Libraries.Compute.Hardware.HardwareCompute
use Libraries.Compute.Hardware.Shaders.MatrixMultiplyShaderCode
use Libraries.Containers.Array
use Libraries.Containers.Number64BitArray
// a 2 x 2 times a 2 x 2: A at 0, B at 4, the answer at 8, column by column
MatrixMultiplyShaderCode shaderCode
GameStateManager manager
HardwareComputeDevice device = manager:GetComputeDevice()
if device not= undefined
HardwareCompute compute = device:CreateCompute()
Number64BitArray numbers
numbers:SetSize(12)
numbers:Set(0, 1)
numbers:Set(1, 3)
numbers:Set(2, 2)
numbers:Set(3, 4)
numbers:Set(4, 5)
numbers:Set(5, 7)
numbers:Set(6, 6)
numbers:Set(7, 8)
compute:SetRunIntegerCount(shaderCode:RUN_INTEGER_COUNT)
compute:SetProgram("MatrixMultiply", shaderCode:GetCode(false, 16))
compute:SetData(numbers)
Array<integer> place
place:Add(2)
place:Add(2)
place:Add(2)
place:Add(0)
place:Add(4)
place:Add(8)
compute:BeginRuns()
compute:AddRun(1, 1, 1, place)
compute:FinishRuns()
Number64BitArray answer
answer:SetSize(4)
compute:GetValues(8, answer)
// answer holds 19, 43, 22, 50
end
Variables
integer RUN_INTEGER_COUNT: How many integers each run gives the shader, in the order above.integer EMULATED_NUMBER_64: 64-bit numbers, IEEE 754 emulated with 64-bit integers: exact, cards with 64-bit integers.integer NUMBER_64: 64-bit numbers in GLSL double: exact, cards with 64-bit numbers.integer EMULATED_32_NUMBER_64: 64-bit numbers, IEEE 754 emulated with 32-bit integers: exact, every graphics card.integer DOUBLE_NUMBER_32: Two 32-bit numbers per number: about 14 digits, every graphics card.
Actions
GetCode(boolean tiled, integer size)
This action returns the shader's GLSL source, with size x size work groups, tiled or plain. Run it with ceil(rowsA / size) by ceil(colsB / size) by 1 work groups, giving each run RUN_INTEGER_COUNT integers.
Parameters
tiledboolean: true to multiply from tiles in shared memory (faster from about 1,024 square up), false to read from memory.sizeinteger: the work group's width and height: 16 is a good default, 1 is one thread per group.
Returns
text: the GLSL source.
Example
use Libraries.Compute.Hardware.Shaders.MatrixMultiplyShaderCode
MatrixMultiplyShaderCode shaderCode
text grouped = shaderCode:GetCode(false, 16)
text tiled = shaderCode:GetCode(true, 16)
GetCode(boolean tiled, integer size, integer arithmetic)
This action returns the shader's GLSL source with the arithmetic given, with size x size work groups, tiled or plain. DOUBLE_NUMBER_32 needs HardwareCompute's DOUBLE_NUMBER_32 format; the other three need its NUMBER_64 format. Run it with ceil(rowsA / size) by ceil(colsB / size) by 1 work groups, giving each run RUN_INTEGER_COUNT integers.
Parameters
tiledboolean: true to multiply from tiles in shared memory, false to read from memory.sizeinteger: the work group's width and height: 16 is a good default, 1 is one thread per group.arithmeticinteger: DOUBLE_NUMBER_32, NUMBER_64, EMULATED_NUMBER_64 or EMULATED_32_NUMBER_64.
Returns
text: the GLSL source.
Example
use Libraries.Compute.Hardware.Shaders.MatrixMultiplyShaderCode
MatrixMultiplyShaderCode shaderCode
text exact = shaderCode:GetCode(false, 16, shaderCode:EMULATED_32_NUMBER_64)