Multiply

Package: Libraries.Compute.MatrixTransform · Inherits from: Libraries.Language.Object, Libraries.Compute.MatrixTransform.MatrixTransform

This is a MatrixTransform class to perform a matrix multiply operation on two matrices. Transform multiplies on the CPU. TransformInHardware multiplies on the graphics card (GameStateManager:GetComputeDevice), with or without a game, and is much faster for large matrices: about 45 times the CPU's speed at 2,048 x 2,048 on a recent Mac. Its answers equal Transform's, bit for bit, on every graphics card: numbers go to the card as the 64-bit numbers they are, and every add and multiply rounds as the CPU's does. How the card works with them depends on what it has (SetNumberMode; AUTOMATIC, the default, picks the first that fits): NUMBER_64 GLSL double: cards with 64-bit numbers. EMULATED_NUMBER_64 IEEE 754 worked out with 64-bit integers: cards with 64-bit integers, Apple's included. EMULATED_32_NUMBER_64 the same with 32-bit integers: every card. DOUBLE_NUMBER_32 two 32-bit numbers each, about 48 bits: only when asked for. Its answers agree with the CPU's to about 14 significant digits, and it can't hold numbers past about 1e38 or products below 1e-30. When there is no graphics card that can compute, or the matrices are too large for it, TransformInHardware multiplies on the CPU instead (RanInHardware says which), and logs why. The graphics card's program and memory are kept between calls, so keep one Multiply for many multiplies; Release lets them go. Attribute Returns A new matrix after the operation is performed.

Example Code

use Libraries.Compute.Matrix
use Libraries.Compute.MatrixTransform.Multiply

class Main
    action Main
        Matrix matrix
        matrix:Fill(2, 3, 10.0)
        Matrix matrix2
        matrix2:Fill(3, 2, 2.0)
        Multiply multiply
        multiply:SetValue(matrix2)
        Matrix result = multiply:Transform(matrix)
        output result:ToText()
    end
end

Variables

  • integer EMULATED_32_NUMBER_64: 64-bit numbers, IEEE 754 emulated with 32-bit integers: the CPU's answers; every graphics card.
  • integer AUTOMATIC: Choose by the graphics card: the first of NUMBER_64, EMULATED_NUMBER_64 and EMULATED_32_NUMBER_64 it can run.
  • integer EMULATED_NUMBER_64: 64-bit numbers, IEEE 754 emulated with 64-bit integers: the CPU's answers; cards with 64-bit integers.
  • integer NUMBER_64: 64-bit numbers in GLSL double: the CPU's answers; cards with 64-bit numbers.
  • integer DOUBLE_NUMBER_32: Two 32-bit numbers per number: about 14 digits; every graphics card.

Actions

CalculateBufferSize(Libraries.Compute.Matrix matrix1, Libraries.Compute.Matrix matrix2)

This action sets the width and height of TransformInHardware's work groups. It is halved until the graphics card allows it. 0, the default, picks 16.

Parameters

Returns

integer

Example

use Libraries.Compute.MatrixTransform.Multiply

class Main
    action Main
        Multiply multiply
        multiply:SetTileSize(32)
    end
end

GetHardwareCutoff()

4 positions in the buffer for constants

Returns

integer

GetNumberMode()

This action returns the number mode set by SetNumberMode: AUTOMATIC unless one was set.

Returns

integer: AUTOMATIC, DOUBLE_NUMBER_32, NUMBER_64, EMULATED_NUMBER_64 or EMULATED_32_NUMBER_64.

Example

use Libraries.Compute.MatrixTransform.Multiply

class Main
    action Main
        Multiply multiply
        output multiply:GetNumberMode()
    end
end

GetNumberModeUsed()

This action returns the number mode the last TransformInHardware that ran on the graphics card used, after AUTOMATIC chose one: AUTOMATIC if none has run there yet.

Returns

integer: DOUBLE_NUMBER_32, NUMBER_64, EMULATED_NUMBER_64, EMULATED_32_NUMBER_64, or AUTOMATIC if none has run.

Example

use Libraries.Compute.Matrix
use Libraries.Compute.MatrixTransform.Multiply

class Main
    action Main
        Matrix matrix
        matrix:Fill(64, 64, 2.0)
        Multiply multiply
        multiply:SetValue(matrix)
        Matrix result = multiply:TransformInHardware(matrix)
        if multiply:RanInHardware()
            output multiply:GetNumberModeUsed()
        end
    end
end

GetTileSize()

This action returns the work group width and height the last TransformInHardware used.

Returns

integer: The tile size.

Example

use Libraries.Compute.MatrixTransform.Multiply

class Main
    action Main
        Multiply multiply
        output multiply:GetTileSize()
    end
end

GetValueMatrix()

This method gets the matrix of values that the transform function will apply. Attribute Parameter The value to apply in the transform.

Returns

Libraries.Compute.Matrix

Example

use Libraries.Compute.Matrix
use Libraries.Compute.MatrixTransform.Multiply

class Main
    action Main
        Matrix matrix2
        matrix2:Fill(3, 2, 2.0)
        Multiply multiply
        multiply:SetValue(matrix2)
        output multiply:GetValueMatrix():ToText()
    end
end

IsTiled()

This action says whether TransformInHardware uses tiles.

Returns

boolean: true if it uses tiles.

Example

use Libraries.Compute.MatrixTransform.Multiply

class Main
    action Main
        Multiply multiply
        output multiply:IsTiled()
    end
end

RanInHardware()

This action says whether the last TransformInHardware ran on the graphics card, or fell back to the CPU.

Returns

boolean: true if the graphics card did the last multiply.

Example

use Libraries.Compute.Matrix
use Libraries.Compute.MatrixTransform.Multiply

class Main
    action Main
        Matrix matrix
        matrix:Fill(64, 64, 2.0)
        Multiply multiply
        multiply:SetValue(matrix)
        Matrix result = multiply:TransformInHardware(matrix)
        output multiply:RanInHardware()
    end
end

Release()

This action lets go of what TransformInHardware keeps on the graphics card between calls: its program, memory and commands. The next call makes them again. The graphics card itself is the program's and stays.

Example

use Libraries.Compute.Matrix
use Libraries.Compute.MatrixTransform.Multiply

class Main
    action Main
        Matrix matrix
        matrix:Fill(64, 64, 2.0)
        Multiply multiply
        multiply:SetValue(matrix)
        Matrix result = multiply:TransformInHardware(matrix)
        multiply:Release()
    end
end

SetHardwareCutoff(integer hardwareCutoff)

This action says whether TransformInHardware uses tiles.

Parameters

  • hardwareCutoff integer

Example

use Libraries.Compute.MatrixTransform.Multiply

class Main
    action Main
        Multiply multiply
        output multiply:IsTiled()
    end
end

SetNumberMode(integer mode)

This action sets how TransformInHardware works with numbers on the graphics card. AUTOMATIC (the default) picks NUMBER_64 if the card has 64-bit numbers, otherwise EMULATED_NUMBER_64 if it has 64-bit integers, otherwise EMULATED_32_NUMBER_64: all three give Transform's answer bit for bit. DOUBLE_NUMBER_32 gives about 14 digits and is used only when set here. A mode the card can't run (NUMBER_64 without 64-bit numbers, EMULATED_NUMBER_64 without 64-bit integers) makes TransformInHardware use the CPU, and log why.

Parameters

  • mode integer: AUTOMATIC, DOUBLE_NUMBER_32, NUMBER_64, EMULATED_NUMBER_64 or EMULATED_32_NUMBER_64.

Example

use Libraries.Compute.MatrixTransform.Multiply

class Main
    action Main
        Multiply multiply
        multiply:SetNumberMode(multiply:EMULATED_32_NUMBER_64)
    end
end

SetTileSize(integer size)

This action sets the width and height of TransformInHardware's work groups. It is halved until the graphics card allows it. 0, the default, picks 16.

Parameters

  • size integer: The width and height, or 0 for the default.

Example

use Libraries.Compute.MatrixTransform.Multiply

class Main
    action Main
        Multiply multiply
        multiply:SetTileSize(32)
    end
end

SetTiled(boolean yes)

This action chooses how TransformInHardware divides the work: true loads tiles of both matrices into each work group's shared memory, which is faster from about 1,024 x 1,024 up; false (the default) reads from memory directly, which is as fast or faster for smaller matrices.

Parameters

  • yes boolean: true to use tiles.

Example

use Libraries.Compute.MatrixTransform.Multiply

class Main
    action Main
        Multiply multiply
        multiply:SetTiled(true)
    end
end

SetValue(Libraries.Compute.Matrix values)

This method sets the a matrix of values for the transform function to apply. Attribute Parameter The value matrix to apply in the transform.

Parameters

Example

use Libraries.Compute.Matrix
use Libraries.Compute.MatrixTransform.Multiply

class Main
    action Main
        Matrix matrix2
        matrix2:Fill(3, 2, 2.0)
        Multiply multiply
        multiply:SetValue(matrix2)
        output multiply:GetValueMatrix():ToText()
    end
end

Transform(Libraries.Compute.Matrix matrix)

This is a MatrixTransform class to perform a matrix multiply operation on two matrices. Attribute Returns A new matrix after the operation is performed.

Parameters

Returns

Libraries.Compute.Matrix

Example

use Libraries.Compute.Matrix
use Libraries.Compute.MatrixTransform.Multiply

class Main
    action Main
        Matrix matrix
        matrix:Fill(2, 3, 10.0)
        Matrix matrix2
        matrix2:Fill(3, 2, 2.0)
        Multiply multiply
        multiply:SetValue(matrix2)
        Matrix result = multiply:Transform(matrix)
        output result:ToText()
    end
end

TransformInHardware(Libraries.Compute.Matrix matrix)

This action multiplies matrix by the value matrix on the graphics card, in the number mode SetNumberMode says (AUTOMATIC, the default, gives Transform's answer bit for bit on every card). It uses the CPU instead (and logs why) when there is no graphics card that can compute, when the matrices don't fit the graphics card, when the mode asked for needs something the card doesn't have, or, with DOUBLE_NUMBER_32 only, when a number or product is too large or too small to hold as two 32-bit numbers.

Parameters

Returns

Libraries.Compute.Matrix: A new matrix: matrix times the value matrix.

Example

use Libraries.Compute.Matrix
use Libraries.Compute.MatrixTransform.Multiply

class Main
    action Main
        Matrix matrix
        matrix:Fill(512, 512, 1.5)
        Multiply multiply
        multiply:SetValue(matrix)
        Matrix result = multiply:TransformInHardware(matrix)
        output result:Get(0, 0)
        multiply:Release()
    end
end

TransformInSoftware(Libraries.Compute.Matrix matrix)

This is a MatrixTransform class to perform a matrix multiply operation on two matrices. Attribute Returns A new matrix after the operation is performed.

Parameters

Returns

Libraries.Compute.Matrix

Example

use Libraries.Compute.Matrix
use Libraries.Compute.MatrixTransform.Multiply

class Main
    action Main
        Matrix matrix
        matrix:Fill(2, 3, 10.0)
        Matrix matrix2
        matrix2:Fill(3, 2, 2.0)
        Multiply multiply
        multiply:SetValue(matrix2)
        Matrix result = multiply:Transform(matrix)
        output result:ToText()
    end
end

Inherited Actions

3 actions from Libraries.Language.Object