Results 1 to 3 of 3

Thread: OpenCL doesn't work with multiple host threads?

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    20

    OpenCL doesn't work with multiple host threads?

    I write an OpenCL program for running on CPUs, so basically the host and the device are essentially the same. When I run my program using 1 CPU thread, everything works fine, but when I use more than 1 thread to call to the device (1 thread creates context, another builds program), it dies at clBuildProgram.

    I use AMD SDK v2.5 which has OpenCL 1.1. Khornos cliams that OpenCL 1.1 has Host-thread safety, enabling OpenCL commands to be enqueued from multiple host threads. What can I do to make this work? Please help.

    I use CUDA OpenCL on another machine with a GPU device, and it works fine with multiple host threads.

    The followings are my codes.

    1 thread version (work):
    Code :
    cl_context cxGPUContext;
     
    const char *ProgramSource = 
     
    "// :::: main\n"
    "#pragma OPENCL EXTENSION cl_khr_fp64 : enable\n"
    "__kernel void kernel_main( __global float* _region__pb_rv, __global float* _region_x, int h, int w, int dim_d0_begin, int dim_d0_end, int dim_d1_begin, int dim_d1_end, int dim__pb_rv_d0, int dim_x_d0 ) {\n"
    "unsigned int _r3_x = get_global_id( 0 );\n"
    "unsigned int _r3_y = get_global_id( 1 );\n"
    "int i = _r3_x;\n"
    "int j = _r3_y;\n"
    "if( _r3_x >= dim_d0_begin && _r3_x < dim_d0_end && _r3_y >= dim_d1_begin && _r3_y < dim_d1_end ) {\n"
    "unsigned int idx__pb_rv = (_r3_x+(dim__pb_rv_d0*(_r3_y+0)));\n"
    "unsigned int idx_x = (_r3_x+(dim_x_d0*(_r3_y+0)));\n"
    "#define RETURN(x) _region__pb_rv[idx__pb_rv] = x; return\n"
    "RETURN ( _region_x [ idx_x ] );\n"
    "}\n"
    "}\n"
    "";
     
    int main(int argc, const char** argv)
    {
        cl_platform_id cpPlatform = NULL;
        cl_uint ciDeviceCount = 0;
        cl_device_id device_id;
        cl_int ciErrNum = CL_SUCCESS;
        cl_uint numPlatforms;
     
        ciErrNum = clGetPlatformIDs(0, NULL, &numPlatforms);
        if(ciErrNum != CL_SUCCESS)
        {
            printf("Error: Failed to num platforms!\n");
            return ciErrNum;
        }
     
        if (0 < numPlatforms) 
        {
            cl_platform_id* platforms = new cl_platform_id[numPlatforms];
            ciErrNum = clGetPlatformIDs(numPlatforms, platforms, NULL);
            if(ciErrNum != CL_SUCCESS)
            {
              printf("Error: Failed to get platform id!\n");
              return ciErrNum;
            }
                for (unsigned i = 0; i < numPlatforms; ++i) 
                {
                    char pbuf[100];
                    ciErrNum = clGetPlatformInfo(platforms[i],
                                               CL_PLATFORM_VENDOR,
                                               sizeof(pbuf),
                                               pbuf,
                                               NULL);
     
                    if(ciErrNum != CL_SUCCESS)
                    {
                      printf("Error: Failed to get platform info!\n");
                      return ciErrNum;
                    }
     
                    cpPlatform = platforms[i];
                    if (!strcmp(pbuf, "Advanced Micro Devices, Inc.")) 
                    {
                        printf("platform %d\n", i);
                        break;
                    }
                }
            delete[] platforms;
        }
     
        if(NULL == cpPlatform)
        {
            printf("NULL platform found so Exiting Application.\n");
            return 0;
        }
     
        ciErrNum = clGetDeviceIDs(cpPlatform, CL_DEVICE_TYPE_CPU, 0, NULL, &ciDeviceCount);    
        if (ciErrNum != CL_SUCCESS)
        {
            printf("Error: Failed to get num devices!\n");
            return ciErrNum;
        }
     
        ciErrNum = clGetDeviceIDs(cpPlatform, CL_DEVICE_TYPE_CPU, 1, &device_id, NULL);
        if (ciErrNum != CL_SUCCESS)
        {
            printf("Error: Failed to get device id!\n");
            return ciErrNum;
        }
     
        cxGPUContext = clCreateContext(0, 1, &device_id, NULL, NULL, &ciErrNum);
        if (ciErrNum != CL_SUCCESS)
        {
            printf("Error: Failed to create OpenCL context!\n");
            return ciErrNum;
        }
     
     
        cl_program cpProgram = clCreateProgramWithSource(cxGPUContext,1, (const char **) &ProgramSource, NULL, &ciErrNum);
        if (ciErrNum != CL_SUCCESS)
        {
            printf("Error: Failed to create program\n");
        }
        printf("clCreateProgramWithSource_main = %d success!\n", cpProgram);
     
        ciErrNum = clBuildProgram(cpProgram, 0, NULL, "-cl-fast-relaxed-math", NULL, NULL);
        if (ciErrNum != CL_SUCCESS)
        {
            printf("Error: Failed to build program\n");
        }
        printf("clBuildProgram_main = %d success!\n", cpProgram);
     
        return 0;
    }

    2 threads version (fail):
    Code :
    cl_context cxGPUContext;
     
    void mainLoop();
     
    extern "C" void *startGpuManager(void* /*arg*/) {
      //try {
        mainLoop();
      //}catch(petabricks::DynamicScheduler::CleanExitException e){}
      return NULL;
    }
     
    const char *ProgramSource = 
     
    "// :::: main\n"
    "#pragma OPENCL EXTENSION cl_khr_fp64 : enable\n"
    "__kernel void kernel_main( __global float* _region__pb_rv, __global float* _region_x, int h, int w, int dim_d0_begin, int dim_d0_end, int dim_d1_begin, int dim_d1_end, int dim__pb_rv_d0, int dim_x_d0 ) {\n"
    "unsigned int _r3_x = get_global_id( 0 );\n"
    "unsigned int _r3_y = get_global_id( 1 );\n"
    "int i = _r3_x;\n"
    "int j = _r3_y;\n"
    "if( _r3_x >= dim_d0_begin && _r3_x < dim_d0_end && _r3_y >= dim_d1_begin && _r3_y < dim_d1_end ) {\n"
    "unsigned int idx__pb_rv = (_r3_x+(dim__pb_rv_d0*(_r3_y+0)));\n"
    "unsigned int idx_x = (_r3_x+(dim_x_d0*(_r3_y+0)));\n"
    "#define RETURN(x) _region__pb_rv[idx__pb_rv] = x; return\n"
    "RETURN ( _region_x [ idx_x ] );\n"
    "}\n"
    "}\n"
    "";
     
    int main(int argc, const char** argv)
    {
        cl_platform_id cpPlatform = NULL;
        cl_uint ciDeviceCount = 0;
        cl_device_id device_id;
        cl_int ciErrNum = CL_SUCCESS;
        cl_uint numPlatforms;
     
        ciErrNum = clGetPlatformIDs(0, NULL, &numPlatforms);
        if(ciErrNum != CL_SUCCESS)
        {
            printf("Error: Failed to num platforms!\n");
            return ciErrNum;
        }
        printf("num platforms = %d\n", numPlatforms);
        if (0 < numPlatforms) 
        {
            cl_platform_id* platforms = new cl_platform_id[numPlatforms];
            ciErrNum = clGetPlatformIDs(numPlatforms, platforms, NULL);
            if(ciErrNum != CL_SUCCESS)
            {
              printf("Error: Failed to get platform id!\n");
              return ciErrNum;
            }
                for (unsigned i = 0; i < numPlatforms; ++i) 
                {
                    char pbuf[100];
                    ciErrNum = clGetPlatformInfo(platforms[i],
                                               CL_PLATFORM_VENDOR,
                                               sizeof(pbuf),
                                               pbuf,
                                               NULL);
     
                    if(ciErrNum != CL_SUCCESS)
                    {
                      printf("Error: Failed to get platform info!\n");
                      return ciErrNum;
                    }
     
                    cpPlatform = platforms[i];
                    if (!strcmp(pbuf, "Advanced Micro Devices, Inc.")) 
                    {
                        printf("platform %d\n", i);
                        break;
                    }
                }
            delete[] platforms;
        }
     
        if(NULL == cpPlatform)
        {
            printf("NULL platform found so Exiting Application.\n");
            return 0;
        }
     
        ciErrNum = clGetDeviceIDs(cpPlatform, CL_DEVICE_TYPE_CPU, 0, NULL, &ciDeviceCount);    
        if (ciErrNum != CL_SUCCESS)
        {
            printf("Error: Failed to get num devices!\n");
            return ciErrNum;
        }
        printf("num devices = %d\n", ciDeviceCount);
        ciErrNum = clGetDeviceIDs(cpPlatform, CL_DEVICE_TYPE_CPU, 1, &device_id, NULL);
        if (ciErrNum != CL_SUCCESS)
        {
            printf("Error: Failed to get device id!\n");
            return ciErrNum;
        }
     
        cxGPUContext = clCreateContext(0, 1, &device_id, NULL, NULL, &ciErrNum);
        if (ciErrNum != CL_SUCCESS)
        {
            printf("Error: Failed to create OpenCL context!\n");
            return ciErrNum;
        }
     
        pthread_t _thread;
        pthread_attr_t attr;
        pthread_attr_init(&attr);
        pthread_attr_setdetachstate(&attr, 0);
        pthread_create(&_thread, &attr, startGpuManager, NULL);
        pthread_attr_destroy(&attr);
     
     
        return 0;
    }
     
    void mainLoop() {
        cl_int ciErrNum = CL_SUCCESS;
     
        // Program Setup
        cl_program cpProgram = clCreateProgramWithSource(cxGPUContext,1, (const char **) &ProgramSource, NULL, &ciErrNum);
        if (ciErrNum != CL_SUCCESS)
        {
            printf("Error: Failed to create program\n");
        }
        printf("clCreateProgramWithSource = %d success!\n", cpProgram);
     
        // build the program
        ciErrNum = clBuildProgram(cpProgram, 0, NULL, "-cl-fast-relaxed-math", NULL, NULL);
        if (ciErrNum != CL_SUCCESS)
        {
            printf("Error: Failed to build program\n");
        }
        printf("clBuildProgram = %d success!\n", cpProgram);
    }

  2. #2
    Senior Member
    Join Date
    May 2010
    Location
    Toronto, Canada
    Posts
    845

    Re: OpenCL doesn't work with multiple host threads?

    What steps do you take to ensure that cxGPUContext is initialized by the time mainLoop() starts to run? Is it possible that cxGPUContext is uninitialized when the second thread calls clBuildProgram()?
    Disclaimer: Employee of Qualcomm Canada. Any opinions expressed here are personal and do not necessarily reflect the views of my employer. LinkedIn profile.

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    20

    Re: OpenCL doesn't work with multiple host threads?

    I figure it out. There is actually a problem with incompatibility between the library that my group implemented and AMD/intel SDK. We suspected that they use new and malloc/ delete/free interchangeably. Thank you for the reply anyway!

Similar Threads

  1. Replies: 5
    Last Post: 02-06-2011, 05:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •