简体   繁体   English

将数字添加到数组值时,由于执行期间发生错误,命令缓冲区的Swift Metal Shader执行被中止

[英]Swift Metal Shader Execution of the command buffer was aborted due to an error during execution when adding a number to an array value

I'm trying some very simple algorithm using metal GPU acceleration to calculate some values in an array. 我正在尝试使用金属GPU加速的一些非常简单的算法来计算数组中的某些值。 The shader throws an error under some conditions I will explain. 在某些情况下,着色器将引发错误。 Error: Execution of the command buffer was aborted due to an error during execution. 错误:由于执行期间发生错误,命令缓冲区的执行被中止。 Ignored (for causing prior/excessive GPU errors) (IOAF code 4) 被忽略(导致先前/过度的GPU错误)(IOAF代码4)

The shader only throws this error when adding a value to the existing value at an index of the array. 仅当将值添加到数组索引处的现有值时,着色器才会引发此错误。 Example: 例:

This will not cause an error: 这不会导致错误:

    kernel void shader (device int *wPointsIntensity [[buffer(0)]],
                const device uint *wPointsXCoord [[buffer(1)]],
                const device uint *wPointsYCoord [[buffer(2)]],
                device float *pixelSignalIntensity [[buffer(3)]],
                device float *pixelDistance [[buffer(4)]],
                const device uint& noOfPoints [[ buffer(5) ]],
                const device uint& width [[ buffer(6) ]],
                const device uint& height [[ buffer(7) ]],
                uint id [[ thread_position_in_grid ]]) {

//this does not throw error
for (uint wpIndex = 0; wpIndex < noOfPoints; wpIndex++) {
    for (uint heightIndex = 0; heightIndex < height; heightIndex++) {
        for (uint widthIndex = 0; widthIndex < width; widthIndex++) {

            uint pixelIndex = heightIndex * width + widthIndex;
            pixelDistance[pixelIndex] = float(pixelIndex);
            pixelSignalIntensity[pixelIndex] = float(pixelIndex);
}}}}

While if you change 如果你改变

pixelDistance[pixelIndex] = float(pixelIndex); pixelDistance [pixelIndex] = float(pixelIndex); with

pixelDistance[pixelIndex] += float(pixelIndex); pixelDistance [pixelIndex] + = float(pixelIndex);

It will throw an error. 它将引发错误。

Here is the swift code: 这是快速代码:

var wPointsValues = [Int32](repeating:0, count: wPoints.count)
var wPointsXLocations = [Int32](repeating:0, count: wPoints.count)
var wPointsYLocations = [Int32](repeating:0, count: wPoints.count)
        for i in 0..<wPoints.count {
            wPointsValues[i] = Int32(wPoints[i].signalIntensity)
            wPointsXLocations[i] = Int32(wPoints[i].location.x)
            wPointsYLocations[i] = Int32(wPoints[i].location.y)
        }

        var numberOfWPoints:Int32 = Int32(wPoints.count)
        var int32Width = Int32(width)
        var int32Height = Int32(height)

        //output arrays

        let numberOfResults = wPoints.count * Int(width) * Int(height)
        var wPointsSignalIntensity = [Float32](repeating:0.0, count: numberOfResults)
        var wPointsDistance = [Float32](repeating:0.0, count: numberOfResults)


        //local variables
        var signalDensity:[Float32] = [Float32](repeating:0.0, count: numberOfResults)
        var signalDistance:[Float32] = [Float32](repeating:0.0, count: numberOfResults)

            //create input buffers
            let inWPointSignalValues = device.makeBuffer(bytes: wPointsValues, length: (MemoryLayout<Int32>.stride * wPoints.count), options: [])
            let inWPointXCoordBuffer = device.makeBuffer(bytes: wPointsXLocations, length: (MemoryLayout<Int32>.stride * wPoints.count), options: [])
            let inWPointYCoordBuffer = device.makeBuffer(bytes: wPointsYLocations, length: (MemoryLayout<Int32>.stride * wPoints.count), options: [])

            //create putput buffers
            let outPixelSignalIntensityBuffer = device.makeBuffer(bytes: wPointsSignalIntensity, length: (MemoryLayout<Float32>.stride * numberOfResults), options: [])
            let outPixelDistanceBuffer = device.makeBuffer(bytes: wPointsDistance, length: (MemoryLayout<Float32>.stride * numberOfResults), options: [])

            let commandBuffer = (mtlCommmandQueue?.makeCommandBuffer())!
            let computeCommandEncoder = (commandBuffer.makeComputeCommandEncoder())!
            computeCommandEncoder.setComputePipelineState(mtlComputePipelineFilter!)
            //set input buffers
            computeCommandEncoder.setBuffer(inWPointSignalValues, offset: 0, index: 0)
            computeCommandEncoder.setBuffer(inWPointXCoordBuffer, offset: 0, index: 1)
            computeCommandEncoder.setBuffer(inWPointYCoordBuffer, offset: 0, index: 2)

            //set output buffers
            computeCommandEncoder.setBuffer(outPixelSignalIntensityBuffer, offset: 0, index: 3)
            computeCommandEncoder.setBuffer(outPixelDistanceBuffer, offset: 0, index: 4)

            //set constants
            computeCommandEncoder.setBytes(&numberOfWPoints, length: MemoryLayout<Int32>.stride, index: 5)
            computeCommandEncoder.setBytes(&int32Width, length: MemoryLayout<Int32>.stride, index: 6)
            computeCommandEncoder.setBytes(&int32Height, length: MemoryLayout<Int32>.stride, index: 7)


            let threadsPerGroup = MTLSize(width:2,height:2,depth:2)
            let numThreadgroups = MTLSize(width:2, height:2, depth:2)
            computeCommandEncoder.dispatchThreadgroups(numThreadgroups, threadsPerThreadgroup: threadsPerGroup)

            let endBufferAllocation = mach_absolute_time()

            print("time for creating and setting buffert: time: \(Double(endBufferAllocation - start) / Double(NSEC_PER_SEC))")
            computeCommandEncoder.endEncoding()

            commandBuffer.commit()
            commandBuffer.waitUntilCompleted()
            let allComplete = mach_absolute_time()
        self.signalDistance = (outPixelDistanceBuffer?.contents())!
        self.signalDensity = (outPixelSignalIntensityBuffer?.contents())!

I had this issue for ages and program crashed intermittently. 我有这个问题很久了,程序间歇性崩溃。 It turned out that I was accessing memory in the kernel that had not been allocated by the buffer. 原来,我正在访问内核中尚未由缓冲区分配的内存。 In the kernel I was doing a for loop 0..<5 (ie output 5 values for each thread) but had not divided the num_threads by 5. 在内核中,我正在执行for循环0 .. <5(即,每个线程输出5个值),但没有将num_threads除以5。

When it didn't crash it was giving the correct answer and no errors except " Execution of the command buffer was aborted due to an error during execution. Caused GPU Hang Error (IOAF code 3)" were ever thrown. 当它没有崩溃时,它给出了正确的答案,没有错误,只抛出了“由于执行过程中的错误导致命令缓冲区的执行中止。导致GPU挂起错误(IOAF代码3)”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 由于执行期间发生错误,命令缓冲区的执行被中止。 导致GPU超时错误(IOAF代码2) - Execution of the command buffer was aborted due to an error during execution. Caused GPU Timeout Error (IOAF code 2) Apple Watch App 上的“命令缓冲区的执行因执行过程中的错误而中止”? - "Execution of the command buffer was aborted due to an error during execution" on Apple Watch App? iOS Metal:命令缓冲区执行顺序 - iOS Metal: command buffer execution order 使用 Metal 间接命令缓冲区时出错:“片段着色器不能与间接命令缓冲区一起使用” - Error when using Metal Indirect Command Buffer: "Fragment shader cannot be used with indirect command buffers" iOS12导致金属命令缓冲区执行错误,渲染是毛病或不会发生 - iOS12 is causing an error on Metal Command Buffer execution, render is glitchy or doesn't occur Swift Playground 执行中止 - Swift playground execution aborted 尝试将制服传递给金属着色器时,“SceneKit:错误,缺少缓冲区[-1/2]” - “SceneKit: error, missing buffer [-1/2]” when trying to pass uniforms to a metal shader 金属着色器在缓冲区中插值 - Metal shader interpolate values in buffer 金属着色器中的数组声明 - Array declaration in metal shader Swift 5 中的金属顶点着色器警告 - Metal Vertex Shader Warning in Swift 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM