title: Performance of metal function multiple call
body: <p>i make rigid body simulation for iPhone/iPad with using Apple Metal. To do this, i need to make many calls of kernel functions, and i see, that it takes a long time, opposite to CUDA for example. 
  I implemented Metal kernel function call, like it describes in Apple tutorial</p>
  
  <pre><code>let commandQueue = device.newCommandQueue()
  
  var commandBuffers:[MTLCommandBuffer]=[]
  var gpuPrograms:[MTLFunction]=[]
  var computePipelineFilters:[MTLComputePipelineState]=[]
  var computeCommandEncoders:[MTLComputeCommandEncoder]=[]
  
  //here i fill all arrays for my command queue
  //and next i execute it 
  
  let threadsPerGroup = MTLSize(width:1,height:1,depth:1)
  let numThreadgroups = MTLSize(width:threadsAmount, height:1, depth:1)
  
  for computeCommandEncoder in computeCommandEncoders
  {
      computeCommandEncoder.dispatchThreadgroups(numThreadgroups, threadsPerThreadgroup: threadsPerGroup)
  }
  
  for computeCommandEncoder in computeCommandEncoders
  {
      computeCommandEncoder.endEncoding()
  }
  
  for commandBuffer in commandBuffers
  {
      commandBuffer.enqueue()
  }
  
  for commandBuffer in commandBuffers
  {
      commandBuffer.commit()
  }
  
  for commandBuffer in commandBuffers
  {
      commandBuffer.waitUntilCompleted()
  }
  </code></pre>
  
  <p>I am do up to few dozens metal kernel functions every frame, and it works too slow. I tested it with empty kernel functions - and it shows me, that the problem are in Swift part of execution. I mean, when i want to execute kernel function in CUDA, i just call it like usual function and it works very fast. But here i must make many actions for every execution of every function every frame. May be i don't know something, but i want create all additional objects one time, and then just make something like </p>
  
  <pre><code> commandQueue.execute()
  </code></pre>
  
  <p>to execute all kernel functions. </p>
  
  <p>Am i rights in my actions to execute many kernel functions, or there is some other way to do it faster?</p>