Results 1 to 1 of 1

Thread: Chapter 3, example 1: Buffers and subbuffers

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

    Chapter 3, example 1: Buffers and subbuffers

    After a longer pause, new example based on the mighty book by Mr. Scarpino.

    The chapter 3 explains concept of buffers and subbuffers. These names cover memory objects, which are passed to computing kernel as input or output structures.
    In ThinBASIC programming, you would pass a parameter to function. In OpenCL, as the code runs on other device, you pass pointer to buffer or sub-buffer.

    Buffers are blocks of memory, supported since OpenCL 1.0.
    SubBuffers are smaller blocks of memory, mapped inside buffers, supported since OpenCL 1.1.

    This caused me a bit of headache, because my GPU does not have OpenCL 1.1 support. But my CPU does. That made the original sample stop, because it checked only 1 platform, like this:
    /* Identify a platform */
    clGetPlatformIDs(1, platform, byVal Null)
    
    If this platform was OpenCL 1.0, the example crashed later, making me desperate, howling on the Moon... you know it.

    But there is a way out of the valley of crying programmers - by modifying the proposed create_device routine to something with more power:
    • going through all platforms, all devices
    • checking for OpenCL 1.1 support on device, not platform level
    • in case no device found, it halts early, not later in code


    This results in the birth of monster, but... it will work on more PCs:
    /* Find a GPU or CPU associated with the available platforms */
    Function create_device(minMajorVersion As Long, minMinorVersion As Long, Optional sReason As String) As tcl_device_id
    
       tcl_platform_id platforms()    
       tcl_platform_id platform
       tcl_uint        num_platforms
    
       tcl_device_id   dev
       tcl_int         ErrCl
    
       /* Find number of platforms */
       ErrCL = clGetPlatformIDs(1, ByVal NULL, num_platforms)
       If (ErrCL < 0) Then        
          pError("Couldn't find any platforms.")            
          APP_SetReturnCode(1) : WaitKey : Exit Function
       End If
      
       /* Access all installed platforms */
       ReDim platforms(num_platforms)
       clGetPlatformIDs(num_platforms, platforms, ByVal NULL)        
       
       Long   p
       Long   majorVersion     
       Long   minorVersion
       Dim versionCL As Asciiz * 512
       
       For p = 1 To num_platforms 
         platform = platforms(p)       
         
         /* Access a device */
         ErrCl = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, dev, ByVal NULL)   
         If (ErrCl = CL_DEVICE_NOT_FOUND) Then
            ErrCl = clGetDeviceIDs(platform, CL_DEVICE_TYPE_CPU, 1, dev, ByVal NULL)
         End If
         
         If (ErrCl < 0) Then
            perror("Couldn't access any devices")
            APP_SetReturnCode(1) : WaitKey : Exit Function   
         End If                     
      
         /* Testing for version */    
         clGetDeviceInfo(platform,             
            CL_DEVICE_VERSION, SizeOf(versionCL), ByVal VarPtr(versionCL), ByVal NULL)        
            
         versionCL = Parse$(versionCL, " ", 2)         
         
         majorVersion = Parse$(versionCL, ".", 1)
         minorVersion = Parse$(versionCL, ".", 2)
                
         If majorVersion*1000 + minorVersion*100 >= minMajorVersion*1000 + minMinorVersion*100 Then
            Return dev     
         End If                                     
       Next
        
       /* No device/platform found */
       perror("No device with OpenCL v"+minMajorVersion+"."+minMinorVersion+" found" + IIf$(StrPtr(sReason), $CRLF+sReason, ""))      
       APP_SetReturnCode(1) : WaitKey : Stop
     
            
       Return dev
       
    End Function
    
    The example is a typical learning one - nothing complex, but solid test of creating buffers and subbuffers inside of them, no computation is performed but pointers to memory are reported instead.

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

    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

Similar Threads

  1. Chapter 2, example 5: Searching for a kernel by name
    By Petr Schreiber in forum OpenCL in Action by Matthew Scarpino
    Replies: 0
    Last Post: 03-10-2012, 11:55
  2. Chapter 2, example 4: Creating a program from file
    By Petr Schreiber in forum OpenCL in Action by Matthew Scarpino
    Replies: 3
    Last Post: 02-10-2012, 02:08
  3. Chapter 2, example 3: Checking a context's reference count
    By Petr Schreiber in forum OpenCL in Action by Matthew Scarpino
    Replies: 2
    Last Post: 26-09-2012, 08:33
  4. 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
  5. 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

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
  •