                        numberOfImages: Int = 1) {
     let slices = ((featureChannels + 3)/4) * numberOfImages
 
-    let h = pipeline.threadExecutionWidth
-    let w = pipeline.maxTotalThreadsPerThreadgroup / h
+    let w = pipeline.threadExecutionWidth
+    let h = pipeline.maxTotalThreadsPerThreadgroup / w
     let d = 1
     let threadGroupSize = MTLSizeMake(w, h, d)
 

}

/**
  Creates a pipeline for a compute kernel.
*/
public func makeFunction(device: MTLDevice, name: String,
                         constantValues: MTLFunctionConstantValues? = nil,

    Dispatches a compute kernel on a 2-dimensional grid.
    
    - Parameters:
      - width: the first dimension
      - height: the second dimension
  */
  public func dispatch(pipeline: MTLComputePipelineState, width: Int, height: Int) {
    let w = pipeline.threadExecutionWidth
    let h = pipeline.maxTotalThreadsPerThreadgroup / w

    let threadGroupSize = MTLSizeMake(w, h, 1)

    let threadGroups = MTLSizeMake(
      (width  + threadGroupSize.width  - 1) / threadGroupSize.width,
      (height + threadGroupSize.height - 1) / threadGroupSize.height, 1)

    setComputePipelineState(pipeline)
    dispatchThreadgroups(threadGroups, threadsPerThreadgroup: threadGroupSize)
