       (height + threadGroupSize.height - 1) / threadGroupSize.height,
       (slices + threadGroupSize.depth  - 1) / threadGroupSize.depth)
 
-    //printGrid(threadGroups, threadGroupSize)
+    //printGrid(threadgroups: threadGroups, threadsPerThreadgroup: threadGroupSize)
 
     setComputePipelineState(pipeline)
     dispatchThreadgroups(threadGroups, threadsPerThreadgroup: threadGroupSize)
   }
 
-  func printGrid(_ threadGroups: MTLSize, _ threadGroupSize: MTLSize) {
-    let grid = MTLSizeMake(threadGroups.width  * threadGroupSize.width,
-                           threadGroups.height * threadGroupSize.height,
-                           threadGroups.depth  * threadGroupSize.depth)
-
-    print("threadGroups: \(threadGroups.width)x\(threadGroups.height)x\(threadGroups.depth)"
-        + ", threadsPerGroup: \(threadGroupSize.width)x\(threadGroupSize.height)x\(threadGroupSize.depth)"
+  /**
+    For debugging the threadgroup sizes.
+  */
+  public func printGrid(threadGroups: MTLSize, threadsPerThreadgroup: MTLSize) {
+    let groups = threadGroups
+    let threads = threadsPerThreadgroup
+    let grid = MTLSizeMake(groups.width  * threads.width,
+                           groups.height * threads.height,
+                           groups.depth  * threads.depth)
+
+    print("threadGroups: \(groups.width)x\(groups.height)x\(groups.depth)"
+        + ", threadsPerThreadgroup: \(threads.width)x\(threads.height)x\(threads.depth)"
         + ", grid: \(grid.width)x\(grid.height)x\(grid.depth)")
   }
 

                       numberOfImages: Int = 1) {
    let slices = ((featureChannels + 3)/4) * numberOfImages

    let h = pipeline.threadExecutionWidth
    let w = pipeline.maxTotalThreadsPerThreadgroup / h
    let d = 1
    let threadGroupSize = MTLSizeMake(w, h, d)


    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)
