Results 1 to 4 of 4

Thread: Chapter 2, example 4: Creating a program from file

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,153
    Rep Power
    736

    Chapter 2, example 4: Creating a program from file

    We have learned what platform, device and context is. The term I would like to present today is "program".

    This name is a bit misleading, because in OpenCL, the "program" thing is more similar to what we know as "library of functions".
    The OpenCL "program" does not get executed, does not have main function and other features you would expect.

    Instead, it is collection of small functions, called kernels.

    While the previous terms (platform/device/context) were managed using function calls, the OpenCL program is written in special form of something I would not hesitate call "C on steroids".
    It is language based on C99 standard, but enhanced with goodies such as vector data types. The bad news for BASIC programmers is that even this version of C language still has semicolons and scary syntax for FOR cycles (gripe intented ).

    The program needs to be built first to make possible to call computations stored inside (execute kernels).

    As a source for building the program you can pass string, but it is more comfortable to store sources in separate file and load them from file.

    I must admit in this part it was nice to see ThinBASIC does not complicate things. In C original, the code to load the program file looks like this:
    program_handle = fopen(file_name[i], "r");
    if(program_handle == NULL) {
      perror("Couldn't find the program file");
      exit(1);   
    }
    fseek(program_handle, 0, SEEK_END);
    program_size[i] = ftell(program_handle);
    rewind(program_handle);
    program_buffer[i] = (char*)malloc(program_size[i]+1);
    program_buffer[i][program_size[i]] = '\0';
    fread(program_buffer[i], sizeof(char), program_size[i], program_handle);
    fclose(program_handle);
    
    In thinBASIC, this code does the job:
    program_buffer(i) = FILE_Load(file_name(i)) 
    program_size(i)   = Len(program_buffer(i))
          
    If(program_size(i) = 0) Then
      Printl("Couldn't find the program file")
      APP_SetReturnCode(1) : WaitKey : Exit Function   
    End If
    
    The code attached to this post takes two OpenCL C source code files, and tries to build one program from them.

    For demonstrative purposes, this operation fails - to show, how you can read back the OpenCL C errors. This is a thing you will experience soon, so better to get ready for it right now
    If you would like to see successful compilation, just rename the kernel function in file bad.cl to something other than "good", which will fix the name conflict.

    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
    Last edited by Petr Schreiber; 28-09-2012 at 09:44.
    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 1: Creating and distributing a matrix-vector multiplication kernel
    By Petr Schreiber in forum OpenCL in Action by Matthew Scarpino
    Replies: 7
    Last Post: 11-09-2013, 11:30
  2. 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
  3. 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
  4. Example: Section 4.3 (page 28), My First OGL Program (second program)
    By kryton9 in forum ThinBASIC programming in OpenGL/TBGL
    Replies: 2
    Last Post: 26-02-2010, 06:01
  5. Creating .lib file from a Dll
    By MikeTrader in forum 3rd party tools
    Replies: 3
    Last Post: 22-10-2009, 12:57

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
  •