Results 1 to 3 of 3

Thread: Chapter 2, example 3: Checking a context's reference count

  1. #1
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,146
    Rep Power
    735

    Chapter 2, example 3: Checking a context's reference count

    The third strange new word from OpenCL world is "context". We already learned there can be 1 or more OpenCL platforms on your PC, each containing 1 or more hardware devices.

    When we prepare calculation using OpenCL, we can select which devices will be used for it.

    It is like picking fruit in shop to basket. You could buy all the fruit in theory, but sometimes apples and oranges are just fine for a little snack (this is the deepest thought of day ).

    The basket is called context.

    The bad news is that one context can contain devices from single platform.

    So if you have PC with 4 NVIDIA GPUs, and 4 Intel CPUs, you can:
    • Create context containing 1-4 Nvidia GPUs
    • Create context containing 1-4 Intel CPUs

    But you cannot:

    • Create context containing 1-4 Nvidia GPUs and 1-4 Intel CPUs


    This is most probably due to major architectural differences between the platforms.

    The example below picks first platform, first device, and examines reference count.

    Wait what? Another new word?

    In a way ... yes. Context reference count says, on how many places in your computation do you use the context.
    This is typical for case, when you have main code, and some 3rd party library, which relies on the same context as well.

    To keep track of how many computations share the reference to given context, you can manually increase/decrease the reference count using clRetainContext/clReleaseContext.

    So what I learn?
    How to check reference count of OpenCL context.

    So what I need to run it?
    You will need the latest ThinBASIC and OpenCL headers to run it + of course modern GPU or CPU.

    Code listing (full code in attachement!)
    Uses "Console"
    
    #INCLUDE "%APP_INCLUDEPATH%/cl/cl.tBasicU"
    
    Function TBMain()
      
       /* Host/device data structures */
       tcl_platform_id platform
       tcl_device_id   device
       tcl_context     context
       tcl_int         ErrCl
       tcl_uint        ref_count
    
       /* Access the first installed platform */
       ErrCl = clGetPlatformIDs(1, platform, Byval Null)
       if (ErrCl < 0) Then
          pError("Couldn't find any platforms")
          APP_SetReturnCode(1) : WaitKey : Exit Function
       End If
    
       /* Access the first available device */
       ErrCl = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, device, ByVal NULL)
       If (ErrCl = CL_DEVICE_NOT_FOUND) Then
          ErrCl = clGetDeviceIDs(platform, CL_DEVICE_TYPE_CPU, 1, device, ByVal NULL)
       End If    
       
       if (ErrCl < 0) Then
          pError("Couldn't find any devices")
          APP_SetReturnCode(1) : WaitKey : Exit Function
       End If
                          
       /* Create the context */
       context = clCreateContext(ByVal NULL, 1, device, ByVal NULL, ByVal NULL, ErrCl)
       if (ErrCl < 0) Then
          pError("Couldn't create a context")
          APP_SetReturnCode(1) : WaitKey : Exit Function   
       End If      
                
       /* Determine the reference count */
       ErrCl = clGetContextInfo(context, CL_CONTEXT_REFERENCE_COUNT,     
             SizeOf(ref_count), VarPtr(ref_count), ByVal NULL)            
       If (ErrCl < 0) Then        
          pError("Couldn't read the reference count.")
          APP_SetReturnCode(1) : WaitKey : Exit Function
       End If
       PrintL StrFormat$("Initial reference count: {1}", ref_count)
    
       /* Update and display the reference count */
       clRetainContext(context)                        
       clGetContextInfo(context, CL_CONTEXT_REFERENCE_COUNT,         
             SizeOf(ref_count), VarPtr(ref_count), ByVal NULL)            
       PrintL StrFormat$("Reference count: {1}", ref_count)            
       
       clReleaseContext(context)                        
       clGetContextInfo(context, CL_CONTEXT_REFERENCE_COUNT,         
             SizeOf(ref_count), VarPtr(ref_count), ByVal NULL)            
       PrintL StrFormat$("Reference count: {1}", ref_count)            
    
       clReleaseContext(context)                                                
       
       PrintL "Press any key to continue..."   
       WaitKey
       APP_SetReturnCode(0)    
       
    End Function
    
    Function pError( sError As String )
    
      Console_SetTextAttribute(%CONSOLE_FOREGROUND_RED | %CONSOLE_FOREGROUND_INTENSITY)
      PrintL sError
      Console_SetTextAttribute(%CONSOLE_FOREGROUND_RED | %CONSOLE_FOREGROUND_GREEN | %CONSOLE_FOREGROUND_BLUE)
      
    End Function
    
    Petr
    Attached Images Attached Images
    Attached Files Attached Files
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  2. #2
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    68
    Posts
    3,865
    Rep Power
    404
    Thanks Petr for another fine example and explanation.

    I have been reading up on OpenGL 4 and the world of graphics programming as we just learned is so different now. But the speed improvements are incredible and so it is worth learning the new way.
    I think as I learn shaders more, it will make it easier to pickup OpenCL.

    I have seen some amazing performance demos on youtube using the GPU's for real time Terrain generation and manipulation and also modeling incredible amounts of particles.
    It is not as fun to learn the new stuff as the old pipeline and not as intuitive for us humans as the old pipeline. We need to adapt to the machines now instead of the old way where it adapted to us

  3. #3
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,146
    Rep Power
    735
    We need to adapt to the machines now instead of the old way where it adapted to us
    That is so true! And so wrong

    But there is a way out of the circle - once you master the difficult technology, it is time to create wrapper usable for human beings. I still dream of ThinBASIC PARALEL FUNCTION() : ... : END FUNCTION block, which would do the ugly initialization and parameter passing under the hood. Or at least some parser to generate code for it automagically. I already did some work on it, it could be one of the possible directions to simplify the coders life


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

Similar Threads

  1. Chapter 2, example 2: Testing device extensions
    By Petr Schreiber in forum OpenCL in Action by Matthew Scarpino
    Replies: 3
    Last Post: 23-09-2012, 00:46
  2. Chapter 2, example 1: Testing platform extensions
    By Petr Schreiber in forum OpenCL in Action by Matthew Scarpino
    Replies: 4
    Last Post: 21-09-2012, 08:31
  3. My word count program is vaporware
    By sandyrepope in forum thinBasic General
    Replies: 17
    Last Post: 31-05-2007, 20:04

Members who have read this thread: 0

There are no members to list at the moment.

Tags for this Thread

Posting Permissions

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