title: How to create OpenCL command queue?
body: <p>I'm trying to learn OpenCL but I can't even make a simple kernel to work.</p>
  
  <p>The code below comes from the book "OpenCL Programming by Example", which I modified, modified, modified... and still, I have no clues what's the problem.</p>
  
  <p>Every time I execute the program in my PC (AMD Athlon 5350 APU with Radeon R3), it prints the result as "0.0000".
  If I run the same executable, in my other machine (which is a clone of this HD, so everything is the same) with a NVIDIA 1080 TI, the program outputs "3.000"  as the result.</p>
  
  <p>I noticed a warning in the compiler output, so I  changed the obsolete clCreateCommandQueue call to clCreateCommandQueueWithProperties().</p>
  
  <p>And now... it just segfaults (with a printf() test I know it segfaults during/after the clCreateCommandQueueWithProperties).</p>
  
  <p>On the system with the NVIDIA GPU, it just works.</p>
  
  <p>What am I missing?</p>
  
  <pre><code>#include &lt;stdint.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;time.h&gt;
  #include &lt;malloc.h&gt;
  
  #include &lt;CL/cl.h&gt;
  
  #define VECTOR_NS 4096
  #define VECTOR_SIZE (VECTOR_NS*sizeof(float))
  
  static const char* saxpy_kernel =
      "__kernel void saxpy_kernel(__global float *A, __global float *B, __global float *C)\n"
      "{\n"
          "int index = get_global_id(0);\n"
          "C[0] = 3;\n"
      "}\n"
      ;
  
  int main(void)
  {
      int i;
      float total;
      float* A;
      float* B;
      float* C;
      float* Cmapped;
      cl_mem Acl;
      cl_mem Bcl;
      cl_mem Ccl;
      cl_context context;
      cl_platform_id* platforms;
      cl_uint num_platforms;
      cl_uint num_devices;
      cl_command_queue queue;
      cl_kernel kernel;
      cl_int clStatus;
  
      // Get platform and device information
      platforms = NULL;
      //Set up the Platform
      clStatus = clGetPlatformIDs(0, NULL, &amp;num_platforms);
      platforms = (cl_platform_id *)malloc(sizeof(cl_platform_id)*num_platforms);
      clStatus = clGetPlatformIDs(num_platforms, platforms, NULL);
      //Get the devices list and choose the device you want to run on
      cl_device_id* device_list = NULL;
  
      clStatus = clGetDeviceIDs( platforms[0], CL_DEVICE_TYPE_GPU, 0, NULL, &amp;num_devices);
      device_list = (cl_device_id *) malloc(sizeof(cl_device_id)*num_devices);
  
      clStatus = clGetDeviceIDs( platforms[0], CL_DEVICE_TYPE_GPU, num_devices, device_list, NULL);
      // Create one OpenCL context for each device in the platform
      context = clCreateContext( NULL, num_devices, device_list, NULL, NULL, &amp;clStatus);
  
      /* Create the command queue */
      //queue = clCreateCommandQueue(context, device_list[0], 0, &amp;clStatus);
      queue = clCreateCommandQueueWithProperties(context, device_list[0], NULL, &amp;clStatus);
  
      if(clStatus != CL_SUCCESS){
          fprintf(stderr, "ERROR: failed to execute the kernel: %d.\n", clStatus);
          exit(1);
      }
  
      /* */
      if((A = aligned_alloc(sysconf(_SC_PAGESIZE), VECTOR_SIZE)) == NULL){
          fprintf(stderr, "ERROR: failed to allocate memory.\n");
          exit(1);
      }
      if((B = aligned_alloc(sysconf(_SC_PAGESIZE), VECTOR_SIZE)) == NULL){
          fprintf(stderr, "ERROR: failed to allocate memory.\n");
          exit(1);
      }
      if((C = aligned_alloc(sysconf(_SC_PAGESIZE), VECTOR_SIZE)) == NULL){
          fprintf(stderr, "ERROR: failed to allocate memory.\n");
          exit(1);
      }
  
      /* Initialize it */
      i = 0;
      do {
          A[i] = 1;
          B[i] = 2;
          C[i] = 0;
      } while(++i != VECTOR_NS);
  
      Acl = clCreateBuffer(context, CL_MEM_USE_HOST_PTR | CL_MEM_READ_WRITE, VECTOR_SIZE, A, &amp;clStatus); // CL_MEM_READ_ONLY
      Bcl = clCreateBuffer(context, CL_MEM_USE_HOST_PTR | CL_MEM_READ_WRITE, VECTOR_SIZE, B, &amp;clStatus); // CL_MEM_READ_ONLY
      Ccl = clCreateBuffer(context, CL_MEM_USE_HOST_PTR | CL_MEM_READ_WRITE, VECTOR_SIZE, C, &amp;clStatus); // CL_MEM_WRITE_ONLY
  
      // Create a program from the kernel source
      // Build the program
      cl_program program = clCreateProgramWithSource(context, 1, (const char**)&amp;saxpy_kernel, NULL, &amp;clStatus);
  
      if(clStatus != CL_SUCCESS){
          fprintf(stderr, "ERROR: failed to compile the OpenCL code.\n");
          exit(1);
      }
  
      clStatus = clBuildProgram(program, 1, device_list, NULL, NULL, NULL);
  
      if(clStatus != CL_SUCCESS){
          fprintf(stderr, "ERROR: failed to compile the OpenCL code.\n");
          exit(1);
      }
  
      // Create the OpenCL kernel
      kernel = clCreateKernel(program, "saxpy_kernel", &amp;clStatus);
      // Set the arguments of the kernel
      clSetKernelArg(kernel, 0, sizeof(cl_mem), (void*)&amp;Acl);
      clSetKernelArg(kernel, 1, sizeof(cl_mem), (void*)&amp;Bcl);
      clSetKernelArg(kernel, 2, sizeof(cl_mem), (void*)&amp;Ccl);
  
      // Execute the OpenCL kernel on the list
      size_t global_size = VECTOR_NS; // Process the entire lists
      size_t local_size = 1;
  
      // Process one item at a time
      clStatus = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &amp;global_size, &amp;local_size, 0, NULL, NULL);
  
      if(clStatus != CL_SUCCESS){
          fprintf(stderr, "ERROR: failed to execute the kernel: %d.\n", clStatus);
          exit(1);
      }
  
      //* Clean up and wait for all the comands to complete. */
      clFlush(queue);
  
      /* Display the result to the screen */
      Cmapped = (float*) clEnqueueMapBuffer(queue, Ccl, CL_TRUE, CL_MAP_READ, 0, VECTOR_SIZE, 0, NULL, NULL, &amp;clStatus); // CL_MEM_USE_HOST_PTR
  
      if(clStatus != CL_SUCCESS){
          fprintf(stderr, "ERROR: failed to execute the kernel: %d.\n", clStatus);
          exit(1);
      }
  
      total = 0;
      for(i = 0; i &lt; VECTOR_NS; i++)
          total += C[i];
  
      printf("TOTAL: %f\n", total);
  
      /* Clean up and wait for all the comands to complete. */
      clFlush(queue);
      clFinish(queue);
  
      /* Finally release all OpenCL allocated objects and host buffers. */
      clReleaseKernel(kernel);
      clReleaseProgram(program);
      clReleaseMemObject(Acl);
      clReleaseMemObject(Bcl);
      clReleaseMemObject(Ccl);
      clReleaseCommandQueue(queue);
      clReleaseContext(context);
  
      free(platforms);
      free(device_list);
  
      return 0;
  }
  </code></pre>