title: OpenCL command queue (CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE) not working (MacOS)
body: <p>Working through the examples and source code from Fixstars.  Specifically, I'm trying the last bit of code in chapter 5 (two moving averages - aka Golden Cross):</p>
  
  <p><a href="http://www.fixstars.com/en/opencl/book/OpenCLProgrammingBook/opencl-programming-practice/" rel="nofollow noreferrer">http://www.fixstars.com/en/opencl/book/OpenCLProgrammingBook/opencl-programming-practice/</a></p>
  
  <p>The code is available here:</p>
  
  <p><a href="http://www.fixstars.com/en/opencl/book/sample/" rel="nofollow noreferrer">http://www.fixstars.com/en/opencl/book/sample/</a></p>
  
  <p>I'll post the specific example below.  But the short of it is that by setting up the command queue as follows:</p>
  
  <pre><code>command_queue = clCreateCommandQueue(context, device_id, CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE, &amp;ret);
  </code></pre>
  
  <p>results in an invalid command_queue (clError).  So, the return data is not processed properly.  IE., it's all zeroes.</p>
  
  <p>However, if I set up the code to calculate only one moving average, without <code>CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE</code>, then things work.  Correspondingly, the Fixstars code prior to this chapter all seems to work fine.</p>
  
  <p>I'm working on a brand new MacBook Pro (retina) which has the NVIDIA chip.  So I'm wondering if it's some issue with NVIDIAs implementation or some other aspect.</p>
  
  <p>Regardless, it's a show stopper for me as what I'm eventually trying to do is fire off multiple (identical) processes each with different parameters, essentially the same thing this example is trying to do by calculating two stock price moving averages simultaneously.</p>
  
  <p>Code snippets below.  I've added a debug printout function to their source.  So I see the following as my program executes:</p>
  
  <pre>
  Return code from clGetPlatformIDs: Success!
  Return code from clGetDeviceIDs: Success!
  Return code from clCreateContext: Success!
  Return code from clCreateCommandQueue: Invalid value
  Return code from clBuildProgram: Success!
  Return code from clCreateKernel(13): Success!
  Return code from clCreateKernel(26): Success!
  Return code from clEnqueueTask(13): Invalid command queue
  Return code from clEnqueueTask(26): Invalid command queue
  result[25]:[0] (0.000000,0.000000)[0] (0.000000,0.000000)[0] (0.000000,0.000000)[0] (0.000000,0.000000)
  result[26]:[0] (0.000000,0.000000)[0] (0.000000,0.000000)[0] (0.000000,0.000000)[0] (0.000000,0.000000)
  result[27]:[0] (0.000000,0.000000)[0] (0.000000,0.000000)[0] (0.000000,0.000000)[0] (0.000000,0.000000)
  result[28]:[0] (0.000000,0.000000)[0] (0.000000,0.000000)[0] (0.000000,0.000000)[0] (0.000000,0.000000)
  </pre>
  
  <p>... with the rest of the data being all zeros as well.  I'm compiling with:</p>
  
  <pre><code>gcc -O2 -c moving_average_vec4p.c&lt;br&gt;
  gcc moving_average_vec4p.o -o moving_average_vec4p -framework opencl
  </code></pre>
  
  <p>---- (HOST CODE) moving_average_vec4p.c ----</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #ifdef __APPLE__
  #include &lt;OpenCL/opencl.h&gt;
  #else
  #include &lt;CL/cl.h&gt;
  #endif
  #include &lt;stdio.h&gt;
  
  #define NAME_NUM (4) /* Number of stocks */
  #define DATA_NUM (100) /* Number of data to process for each stock */
  
  /* Read Stock data */
  int stock_array_4[NAME_NUM*DATA_NUM]= {
  #include "stock_array_4.txt"
  };
  
  /* Moving average width */
  #define WINDOW_SIZE_13 (13)
  #define WINDOW_SIZE_26 (26)
  
  #define MAX_SOURCE_SIZE (0x100000)
  
  /* DT: added to aid in debugging */
  
  void printCLError (int err) {
    switch (err) {
    case CL_SUCCESS:                            printf("Success!\n"); break;
    case CL_DEVICE_NOT_FOUND:                   printf("Device not found.\n"); break;
    case CL_DEVICE_NOT_AVAILABLE:               printf("Device not available\n"); break;
    case CL_COMPILER_NOT_AVAILABLE:             printf("Compiler not available\n"); break;
    case CL_MEM_OBJECT_ALLOCATION_FAILURE:      printf("Memory object allocation failure\n"); break;
    case CL_OUT_OF_RESOURCES:                   printf("Out of resources\n"); break;
    case CL_OUT_OF_HOST_MEMORY:                 printf("Out of host memory\n"); break;
    case CL_PROFILING_INFO_NOT_AVAILABLE:       printf("Profiling information not available\n"); break;
    case CL_MEM_COPY_OVERLAP:                   printf("Memory copy overlap\n"); break;
    case CL_IMAGE_FORMAT_MISMATCH:              printf("Image format mismatch\n"); break;
    case CL_IMAGE_FORMAT_NOT_SUPPORTED:         printf("Image format not supported\n"); break;
    case CL_BUILD_PROGRAM_FAILURE:              printf("Program build failure\n"); break;
    case CL_MAP_FAILURE:                        printf("Map failure\n"); break;
    case CL_INVALID_VALUE:                      printf("Invalid value\n"); break;
    case CL_INVALID_DEVICE_TYPE:                printf("Invalid device type\n"); break;
    case CL_INVALID_PLATFORM:                   printf("Invalid platform\n"); break;
    case CL_INVALID_DEVICE:                     printf("Invalid device\n"); break;
    case CL_INVALID_CONTEXT:                    printf("Invalid context\n"); break;
    case CL_INVALID_QUEUE_PROPERTIES:           printf("Invalid queue properties\n"); break;
    case CL_INVALID_COMMAND_QUEUE:              printf("Invalid command queue\n"); break;
    case CL_INVALID_HOST_PTR:                   printf("Invalid host pointer\n"); break;
    case CL_INVALID_MEM_OBJECT:                 printf("Invalid memory object\n"); break;
    case CL_INVALID_IMAGE_FORMAT_DESCRIPTOR:    printf("Invalid image format descriptor\n"); break;
    case CL_INVALID_IMAGE_SIZE:                 printf("Invalid image size\n"); break;
    case CL_INVALID_SAMPLER:                    printf("Invalid sampler\n"); break;
    case CL_INVALID_BINARY:                     printf("Invalid binary\n"); break;
    case CL_INVALID_BUILD_OPTIONS:              printf("Invalid build options\n"); break;
    case CL_INVALID_PROGRAM:                    printf("Invalid program\n"); break;
    case CL_INVALID_PROGRAM_EXECUTABLE:         printf("Invalid program executable\n"); break;
    case CL_INVALID_KERNEL_NAME:                printf("Invalid kernel name\n"); break;
    case CL_INVALID_KERNEL_DEFINITION:          printf("Invalid kernel definition\n"); break;
    case CL_INVALID_KERNEL:                     printf("Invalid kernel\n"); break;
    case CL_INVALID_ARG_INDEX:                  printf("Invalid argument index\n"); break;
    case CL_INVALID_ARG_VALUE:                  printf("Invalid argument value\n"); break;
    case CL_INVALID_ARG_SIZE:                   printf("Invalid argument size\n"); break;
    case CL_INVALID_KERNEL_ARGS:                printf("Invalid kernel arguments\n"); break;
    case CL_INVALID_WORK_DIMENSION:             printf("Invalid work dimension\n"); break;
    case CL_INVALID_WORK_GROUP_SIZE:            printf("Invalid work group size\n"); break;
    case CL_INVALID_WORK_ITEM_SIZE:             printf("Invalid work item size\n"); break;
    case CL_INVALID_GLOBAL_OFFSET:              printf("Invalid global offset\n"); break;
    case CL_INVALID_EVENT_WAIT_LIST:            printf("Invalid event wait list\n"); break;
    case CL_INVALID_EVENT:                      printf("Invalid event\n"); break;
    case CL_INVALID_OPERATION:                  printf("Invalid operation\n"); break;
    case CL_INVALID_GL_OBJECT:                  printf("Invalid OpenGL object\n"); break;
    case CL_INVALID_BUFFER_SIZE:                printf("Invalid buffer size\n"); break;
    case CL_INVALID_MIP_LEVEL:                  printf("Invalid mip-map level\n"); break;
    default: printf("Unknown\n");
    }
  }
  
  
  int main(void)
  {
    cl_platform_id platform_id = NULL;
    cl_uint ret_num_platforms;
    cl_device_id device_id = NULL;
    cl_uint ret_num_devices;
    cl_context context = NULL;
    cl_command_queue command_queue = NULL;
    cl_mem memobj_in = NULL;
    cl_mem memobj_out13 = NULL;
    cl_mem memobj_out26 = NULL;
    cl_program program = NULL;
    cl_kernel kernel13 = NULL;
    cl_kernel kernel26 = NULL;
    cl_event event13, event26;
    size_t kernel_code_size;
    char *kernel_src_str;
    float *result13;
    float *result26;
    cl_int ret;
    FILE *fp;
  
    int window_num_13 = (int)WINDOW_SIZE_13;
    int window_num_26 = (int)WINDOW_SIZE_26;
    int point_num = (NAME_NUM * DATA_NUM);
    int data_num = (int)DATA_NUM;
    int name_num = (int)NAME_NUM;
  
    int i, j;
    /* Allocate space to read in kernel code */
    kernel_src_str = (char *)malloc(MAX_SOURCE_SIZE);
  
    /* Allocate space for the result on the host side */
    result13 = (float *)malloc(point_num*sizeof(float)); /* average over13 weeks */
    result26 = (float *)malloc(point_num*sizeof(float)); /* average over26 weeks */
  
    /* Get Platform */
    ret = clGetPlatformIDs(1, &amp;platform_id, &amp;ret_num_platforms);
    printf("Return code from clGetPlatformIDs: ");
    printCLError(ret);
  
    /* Get Device */
    ret = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_DEFAULT, 1, &amp;device_id,
                 &amp;ret_num_devices);
    printf("Return code from clGetDeviceIDs: ");
    printCLError(ret);
  
    /* Create Context */
    context = clCreateContext( NULL, 1, &amp;device_id, NULL, NULL, &amp;ret);
    printf("Return code from clCreateContext: ");
    printCLError(ret);
  
    /* Create Command Queue */
  
    // DT:  this seems to break it (ie., output is all zeros)
    command_queue = clCreateCommandQueue(context, device_id, CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE, &amp;ret);
    printf("Return code from clCreateCommandQueue: ");
    printCLError(ret);
  
    /* Read kernel source code */
    fp = fopen("moving_average_vec4.cl", "r");
    kernel_code_size = fread(kernel_src_str, 1, MAX_SOURCE_SIZE, fp);
    fclose(fp);
  
    /* Create Program Object */
    program = clCreateProgramWithSource(context, 1, (const char **)&amp;kernel_src_str,
                        (const size_t *)&amp;kernel_code_size, &amp;ret);
  
    /* Compile kernel */
    ret = clBuildProgram(program, 1, &amp;device_id, NULL, NULL, NULL);
    printf("Return code from clBuildProgram: ");
    printCLError(ret);
  
    /* Create kernel */
    kernel13 = clCreateKernel(program, "moving_average_vec4", &amp;ret); /* 13 weeks */
    printf("Return code from clCreateKernel(13): ");
    printCLError(ret);
  
    kernel26 = clCreateKernel(program, "moving_average_vec4", &amp;ret); /* 26 weeks */
    printf("Return code from clCreateKernel(26): ");
    printCLError(ret);
  
    /* Create buffer for the input data on the device */
    memobj_in = clCreateBuffer(context, CL_MEM_READ_WRITE,
                   point_num * sizeof(int), NULL, &amp;ret);
  
    /* Create buffer for the result on the device */
    memobj_out13 = clCreateBuffer(context, CL_MEM_READ_WRITE,
                  point_num * sizeof(float), NULL, &amp;ret); /* 13 weeks */
    memobj_out26 = clCreateBuffer(context, CL_MEM_READ_WRITE,
                  point_num * sizeof(float), NULL, &amp;ret); /* 26 weeks */
  
    /* Copy input data to the global memory on the device*/
    ret = clEnqueueWriteBuffer(command_queue, memobj_in, CL_TRUE, 0, point_num * sizeof(int), stock_array_4, 0, NULL, NULL);
  
    /* Set Kernel Arguments (13 weeks) */
    ret = clSetKernelArg(kernel13, 0, sizeof(cl_mem), (void *)&amp;memobj_in);
    ret = clSetKernelArg(kernel13, 1, sizeof(cl_mem), (void *)&amp;memobj_out13);
    ret = clSetKernelArg(kernel13, 2, sizeof(int), (void *)&amp;data_num);
    ret = clSetKernelArg(kernel13, 3, sizeof(int), (void *)&amp;window_num_13);
  
    /* Submit task to compute the moving average over 13 weeks */
  
    ret = clEnqueueTask(command_queue, kernel13, 0, NULL, NULL);
    printf("Return code from clEnqueueTask(13): ");
    printCLError(ret);
  
    /* Set Kernel Arguments (26 weeks) */
    ret = clSetKernelArg(kernel26, 0, sizeof(cl_mem), (void *)&amp;memobj_in);
    ret = clSetKernelArg(kernel26, 1, sizeof(cl_mem), (void *)&amp;memobj_out26);
    ret = clSetKernelArg(kernel26, 2, sizeof(int), (void *)&amp;data_num);
    ret = clSetKernelArg(kernel26, 3, sizeof(int), (void *)&amp;window_num_26);
  
    /* Submit task to compute the moving average over 26 weeks */
    ret = clEnqueueTask(command_queue, kernel26, 0, NULL, &amp;event26);
    printf("Return code from clEnqueueTask(26): ");
    printCLError(ret);
  
    // DT:  doesn't seem to help ... ;-(
    ret = clFinish(command_queue);
  
    /* Copy result for the 13 weeks moving average from device to host */
    ret = clEnqueueReadBuffer(command_queue, memobj_out13, CL_TRUE, 0, point_num * sizeof(float), result13, 1, &amp;event13, NULL);
  
    /* Copy result for the 26 weeks moving average from device to host */
    ret = clEnqueueReadBuffer(command_queue, memobj_out26, CL_TRUE, 0, point_num * sizeof(float), result26, 1, &amp;event26, NULL);
  
  
    /* OpenCL Object Finalization */
    ret = clReleaseKernel(kernel13);
    ret = clReleaseKernel(kernel26);
    ret = clReleaseProgram(program);
    ret = clReleaseMemObject(memobj_in);
    ret = clReleaseMemObject(memobj_out13);
    ret = clReleaseMemObject(memobj_out26);
    ret = clReleaseCommandQueue(command_queue);
    ret = clReleaseContext(context);
  
  
    /* Display results */
  
    /* DT: also added printout to see if actual numeric values are passing through */
  
    for (i=window_num_26-1; i &lt; data_num; i++) {
      printf("result[%d]:", i );
      for (j=0; j &lt; name_num; j++ ) {
        /* Display whether the 13 week average is greater */
        printf( "[%d] (%f,%f)", (result13[i*NAME_NUM+j] &gt; result26[i*NAME_NUM+j]),result13[i*NAME_NUM+j],result26[i*NAME_NUM+j] );
      }
      printf("\n");
    }
  
    /* Deallocate memory on the host */
    free(result13);
    free(result26);
    free(kernel_src_str);
  
    return 0;
  }
  </code></pre>
  
  <p>---- (OPENCL KERNEL CODE) moving_average_vec4.cl ----</p>
  
  <pre><code>__kernel void moving_average_vec4(__global int4 *values,
                    __global float4 *average,
                    int length,
                    int width)
  {
    int i;
    int4 add_value; /* A vector to hold 4 components */
  
    /* Compute sum for the first "width" elements for 4 stocks */
    add_value = (int4)0;
    for (i=0; i &lt; width; i++) {
      add_value += values[i];
    }
    average[width-1] = convert_float4(add_value);
  
    /* Compute sum for the (width)th ~ (length-1)th elements for 4 stocks */
    for (i=width; i &lt; length; i++) {
      add_value = add_value - values[i-width] + values[i];
      average[i] = convert_float4(add_value);
    }
  
    /* Insert zeros to 0th ~ (width-2)th element for 4 stocks*/
    for (i=0; i &lt; width-1; i++) {
      average[i] = (float4)(1.1f);
    }
  
    /* Compute average of (width-1) ~ (length-1) elements for 4 stocks  */
    for (i=width-1; i &lt; length; i++) {
      average[i] /= (float4)width;
    }
  }
  </code></pre>