我正在尝试使用 MPSMatrixRandom 为我的应用程序生成一些随机整数数据,我有两个问题。
MPSMatrixRandomMTGP32 和 MPSMatrixRandomPhilox 有什么区别?
我知道这两个着色器使用不同的算法,但是它们之间有什么区别?这两种算法的性能或输出是否不同,如果有,有何不同?
您可以使用什么代码来实现这些着色器?
我尝试自己实现它们,但我的应用程序总是崩溃并出现模糊的错误消息。我希望看到一个正确完成的示例实现。
我正在尝试使用 MPSMatrixRandom 为我的应用程序生成一些随机整数数据,我有两个问题。
MPSMatrixRandomMTGP32 和 MPSMatrixRandomPhilox 有什么区别?
我知道这两个着色器使用不同的算法,但是它们之间有什么区别?这两种算法的性能或输出是否不同,如果有,有何不同?
您可以使用什么代码来实现这些着色器?
我尝试自己实现它们,但我的应用程序总是崩溃并出现模糊的错误消息。我希望看到一个正确完成的示例实现。
这是一个示例,演示如何使用这两个内核生成随机矩阵:
import Foundation
import Metal
import MetalPerformanceShaders
let device = MTLCreateSystemDefaultDevice()!
let commandQueue = device.makeCommandQueue()!
let rows = 8
let columns = 8
let matrixDescriptor = MPSMatrixDescriptor(rows: rows,
columns: columns,
rowBytes: MemoryLayout<Float>.stride * columns,
dataType: .float32)
let mtMatrix = MPSMatrix(device: device, descriptor: matrixDescriptor)
let phMatrix = MPSMatrix(device: device, descriptor: matrixDescriptor)
let distribution = MPSMatrixRandomDistributionDescriptor.uniformDistributionDescriptor(withMinimum: -1.0, maximum: 1.0)
let mtKernel = MPSMatrixRandomMTGP32(device: device,
destinationDataType: .float32,
seed: 0,
distributionDescriptor: distribution)
let phKernel = MPSMatrixRandomPhilox(device: device,
destinationDataType: .float32,
seed: 0,
distributionDescriptor: distribution)
let commandBuffer = commandQueue.makeCommandBuffer()!
mtKernel.encode(commandBuffer: commandBuffer, destinationMatrix: mtMatrix)
phKernel.encode(commandBuffer: commandBuffer, destinationMatrix: phMatrix)
#if os(macOS)
mtMatrix.synchronize(on: commandBuffer)
phMatrix.synchronize(on: commandBuffer)
#endif
commandBuffer.commit()
commandBuffer.waitUntilCompleted() // Only necessary to ensure GPU->CPU sync for display
print("Mersenne Twister values:")
let mtValues = mtMatrix.data.contents().assumingMemoryBound(to: Float.self)
for row in 0..<rows {
for col in 0..<columns {
print("\(mtValues[row * columns + col])", terminator: " ")
}
print("")
}
print("")
print("Philox values:")
let phValues = phMatrix.data.contents().assumingMemoryBound(to: Float.self)
for row in 0..<rows {
for col in 0..<columns {
print("\(phValues[row * columns + col])", terminator: " ")
}
print("")
}
我无法评论这些生成器的统计特性;我建议您参考评论中提到的文件。