title: How could I speed up my OpenCL algorithm?
body: <p>I successfully write a sorting algorithm based on OpenCL and C. But I found it executed a little bit slow, how can I change my code to make it much faster?</p>
  <p>A section of my code:</p>
  <pre class="lang-c prettyprint-override"><code>size_t local_work_size = ((size_t)array_size &gt; max_work_group_size) ? max_work_group_size : (size_t)array_size;
  size_t num_work_groups = (array_size + local_work_size - 1) / local_work_size;
  size_t global_work_size = num_work_groups * local_work_size;
      
  // half = array_size / 2;
  for (int i = 0; i &lt; half; i++)
  {
      err = clEnqueueNDRangeKernel(command_queue, even_kernel, 1, NULL, &amp;global_work_size, &amp;local_work_size, 0, NULL, NULL);
      check_err(err, &quot;clEnqueueNDRangeKernel even&quot;);
      err = clEnqueueNDRangeKernel(command_queue, odd_kernel, 1, NULL, &amp;global_work_size, &amp;local_work_size, 0, NULL, NULL);
      check_err(err, &quot;clEnqueueNDRangeKernel odd&quot;);
  }
  
  </code></pre>
  <p>Here is my OpenCL code:</p>
  <pre class="lang-cl prettyprint-override"><code>__kernel void odd_sort(
      __global int *array, 
      int array_size
      )
  {
      int index = get_global_id(0);
      index = index * 2 + 1;
  
      if (index &lt; (array_size - 1)) {
          if (array[index] &gt; array[index + 1]){
              int tmp = array[index];
              array[index] = array[index + 1];
              array[index + 1] = tmp;
          }
      }
  }
  
  __kernel void even_sort(
      __global int *array, 
      int array_size
      )
  {
      int index = get_global_id(0);
      index = index * 2;
  
      if (index &lt; (array_size - 1)) {
          if (array[index] &gt; array[index + 1]) {
              int tmp = array[index];
              array[index] = array[index + 1];
              array[index + 1] = tmp;
          }
      }
  }
  </code></pre>