OpenCL (Open Coumputer Language) for FreeBASIC .

Headers, Bindings, Libraries for use with FreeBASIC, Please include example of use to help ensure they are tested and usable.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: OpenCL (Open Coumputer Language) for FreeBASIC .

Post by D.J.Peters »

WOW you can crash a modern OS with a tiny wrong OpenCL code :-)

I made an typo so the GPU was reading / writing from wrong address (on the GPU memory not the HOST memory)

I got funky pixels over the complete desktop and the OS crashed (no keyboard no mouse nothing)

I can't believe that an user task (fbc compiled program) can kill a complete Windows 10 64-bit OS !

Than I reboot in Linux Ubuntu 64-bit and run the same OpenCL kernel with same result a total crash.

Joshy
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: OpenCL (Open Coumputer Language) for FreeBASIC .

Post by D.J.Peters »

Code: Select all

CL("kernel void shader (global uchar4 * pPixels, uint uWidth, uint uHeight, float fRuntime)")
CL("{")
CL("  uint  id = get_global_id(0);")
CL("  uint  uY = id / uWidth;")
CL("  uint  uX = id % uWidth;")
CL("  float fX = uX/(float)uWidth;")
CL("  float fY = uY/(float)uHeight;")
CL("  uchar r8 = (uchar)(fX*255.f);")
CL("  uchar g8 = (uchar)(fY*255.f);") 
CL("  uchar b8 = (uchar)(255.f * (0.5f + 0.5f*sin(fRuntime)));")
' !!! this simple line crashed both Windows 10 and Linux !!!
'CL("  pPixels[uX+uWidth*uY] = (uchar4)(b8,g8,r8,255);")
' this was the right
CL("  pPixels[id] = (uchar4)(b8,g8,r8,255);")
CL("}")
#endif
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: OpenCL (Open Coumputer Language) for FreeBASIC .

Post by D.J.Peters »

In the past if I compiled a 64-bit OpenCL binary and executed it on 64-bit Windows it reported only a 32-bit GPU! Looks like I got fixed this issue. :-)

Joshy

output from old "_oop_work_in_progress.bas"
info32.txt wrote:platform
--------
Profile : FULL_PROFILE
Version : OpenCL 1.2 CUDA 9.1.84
Name : NVIDIA CUDA
Vendor : NVIDIA Corporation
Extensions : cl_khr_global_int32_base_atomics cl_khr_global_int32_extended_atomics cl_khr_local_int32_base_atomics cl_khr_local_int32_extended_atomics cl_khr_fp64 cl_khr_byte_addressable_store cl_khr_icd cl_khr_gl_sharing cl_nv_compiler_options cl_nv_device_attribute_query cl_nv_pragma_unroll cl_nv_d3d10_sharing cl_khr_d3d10_sharing cl_nv_d3d11_sharing cl_nv_copy_opts cl_nv_create_buffer
number of devices : 1

device
-------
Profile : FULL_PROFILE
Version : OpenCL 1.1 CUDA
Name : GeForce GTX 470
Vendor : NVIDIA Corporation
OpenCL C version : OpenCL C 1.1
Extensions : cl_khr_global_int32_base_atomics cl_khr_global_int32_extended_atomics cl_khr_local_int32_base_atomics cl_khr_local_int32_extended_atomics cl_khr_fp64 cl_khr_byte_addressable_store cl_khr_icd cl_khr_gl_sharing cl_nv_compiler_options cl_nv_device_attribute_query cl_nv_pragma_unroll cl_nv_d3d10_sharing cl_khr_d3d10_sharing cl_nv_d3d11_sharing cl_nv_copy_opts cl_nv_create_buffer
Type : 32-bit GPU !!! :-) !!!
MaxComputeUnits : 14
MaxWorkGroupSize : 1024
MaxWorkItemDimensions: 3
MaxWorkItemSizes(0) : 1024
MaxWorkItemSizes(1) : 1024
MaxWorkItemSizes(2) : 64
getMaxClockFrequency : 1312 MHz
getGlobalMemSize : 1280 MB
getMaxMemAllocSize : 320 MB
info64.txt wrote:platform
--------
Profile : FULL_PROFILE
Version : OpenCL 1.2 CUDA 9.1.84
Name : NVIDIA CUDA
Vendor : NVIDIA Corporation
Extensions : cl_khr_global_int32_base_atomics cl_khr_global_int32_extended_atomics cl_khr_local_int32_base_atomics cl_khr_local_int32_extended_atomics cl_khr_fp64 cl_khr_byte_addressable_store cl_khr_icd cl_khr_gl_sharing cl_nv_compiler_options cl_nv_device_attribute_query cl_nv_pragma_unroll cl_nv_d3d10_sharing cl_khr_d3d10_sharing cl_nv_d3d11_sharing cl_nv_copy_opts cl_nv_create_buffer
number of devices : 1

device
-------
Profile : FULL_PROFILE
Version : OpenCL 1.1 CUDA
Name : GeForce GTX 470
Vendor : NVIDIA Corporation
OpenCL C version : OpenCL C 1.1
Extensions : cl_khr_global_int32_base_atomics cl_khr_global_int32_extended_atomics cl_khr_local_int32_base_atomics cl_khr_local_int32_extended_atomics cl_khr_fp64 cl_khr_byte_addressable_store cl_khr_icd cl_khr_gl_sharing cl_nv_compiler_options cl_nv_device_attribute_query cl_nv_pragma_unroll cl_nv_d3d10_sharing cl_khr_d3d10_sharing cl_nv_d3d11_sharing cl_nv_copy_opts cl_nv_create_buffer
Type : 64-bit GPU !!! :-) !!!
MaxComputeUnits : 14
MaxWorkGroupSize : 1024
MaxWorkItemDimensions: 3
MaxWorkItemSizes(0) : 1024
MaxWorkItemSizes(1) : 1024
MaxWorkItemSizes(2) : 64
getMaxClockFrequency : 1312 MHz
getGlobalMemSize : 1280 MB
getMaxMemAllocSize : 320 MB
12val12newakk
Posts: 35
Joined: Nov 14, 2019 17:04

Re: OpenCL (Open Coumputer Language) for FreeBASIC .

Post by 12val12newakk »

Is it possible to move the opencl kernel to a separate C language file to get away from writing
strSource &= !" .... \n" ?
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: OpenCL (Open Coumputer Language) for FreeBASIC .

Post by D.J.Peters »

12val12newakk wrote: Jul 13, 2022 17:32 Is it possible to move the opencl kernel to a separate C language file to get away from writing
strSource &= !" .... \n" ?
The source code is a string of course you can load it from file also.
pseudo code I'm not at home ATM

Code: Select all

var Code = ""
if open("mykernal.c",for input as #1) then
  print "error: can't read *.c file!"
  beep : sleep : end 1
end if

while not EOF(1)
  var aLine=""
  line input #1,aLine
  Code &= aLine
wend 
close #1
Code &= chr(0)  ' be sure it's NULL terminate C string
' create program object in context from sourcecode
dim as cl_program program = clCreateProgramWithSource(context,1,strptr(Code),NULL,@status)
...
Last edited by D.J.Peters on Jul 15, 2022 6:47, edited 1 time in total.
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: OpenCL (Open Coumputer Language) for FreeBASIC .

Post by srvaldez »

you can also do it like this

Code: Select all

dim as integer fnum 

fnum = freefile()
if open ("GameOfLife.c", for input, as #fnum) <>0 then
	print "can't open file" 
	end
endif
	code = input(lof(fnum), fnum)
close fnum

Code &= chr(0)
12val12newakk
Posts: 35
Joined: Nov 14, 2019 17:04

Re: OpenCL (Open Coumputer Language) for FreeBASIC .

Post by 12val12newakk »

thanks D.J.Peters and jerry srvaldez
another question
if there are several openCl device (Pcie 1 slot + Pcie 2 slot + InternalGPU in_processor) in the system
how to run multiple instances of programs on different devices?
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: OpenCL (Open Coumputer Language) for FreeBASIC .

Post by D.J.Peters »

You have to enumerate your devices and create a context from multiply devices (device ID's)
see at: clCreateContext() in the cl.bi file or the documentation in the /doc folder.

Joshy
12val12newakk
Posts: 35
Joined: Nov 14, 2019 17:04

Re: OpenCL (Open Coumputer Language) for FreeBASIC .

Post by 12val12newakk »

Code: Select all

' OpenCL related initialization
' Create Context, Device list, Command Queue
' Create OpenCL memory buffer objects
'  CL code, compile, link CL source
' Build program and kernel objects
sub initializeCL                                             
  print "initializeCL"                                       
  dim as cl_int status = 0                                   
  dim as size_t deviceListSize                               
                                                             
  dim as cl_uint numPlatforms                                
  status = clGetPlatformIDs(0, NULL, @numPlatforms)          
  if (status<>CL_SUCCESS) or (numPlatforms<1) then           
    print "Error: Getting Platforms. (clGetPlatformsIDs)"    
    return                                                   
  end if                                                     
                                                             
  dim as cl_platform_id platform                             
  status = clGetPlatformIDs(1, @platform, NULL)              
  if (status<>CL_SUCCESS) then                               
    print "Error: Getting Platform Ids.(clGetPlatformsIDs)"  
    return                                                   
  end if                                                     
                                                             
                                                             
  dim as cl_context_properties cps(2)                        
  cps(0) = CL_CONTEXT_PLATFORM                               
  cps(1) = cast(cl_context_properties, platform)             
  cps(2) = NULL                                              
  ' Create an OpenCL context from platform ID                
  g_context = clCreateContextFromType(@cps(0), _             
                                      CL_DEVICE_TYPE_GPU, _  
                                      NULL, _                
                                      NULL, _                
                                      @status)               
  if (status<>CL_SUCCESS) then                               
    print "Error: Creating Context. (clCreateContextFromType)"  
    return                                                   
  end if                                                     
                                                             
  ' First, get the size of device list data                  
  status = clGetContextInfo(g_context, _                     
                            CL_CONTEXT_DEVICES, _            
                            0, _                             
                            NULL, _                          
                            @deviceListSize)                 
  if(status <> CL_SUCCESS) then                              
    print "Error: Getting Context Info (device list size, clGetContextInfo)"   
    return                                                   
  else                                                       
    print "Info: deviceListSize = " & deviceListSize         
  end if                                                     
                                                             
  ' Detect OpenCL devices                                    
  g_devices = callocate(deviceListSize)                      
  ' Now, get the device list data                            
  status = clGetContextInfo(g_context, _                     
                            CL_CONTEXT_DEVICES, _            
                            deviceListSize, _                
                            g_devices, _                     
                            NULL)                            
  if (status<>CL_SUCCESS) then                               
    print "Error: Getting Context Info (device list, clGetContextInfo)"     
    return                                                   
  end if                                                     
                                                             
  ' Create an OpenCL command queue                           
  g_commandQueue = clCreateCommandQueue(g_context, _         
                                        g_devices[0], _      
                                        0, _                 
                                        @status)             
  if (status<>CL_SUCCESS) then                                
    print "Creating Command Queue. (clCreateCommandQueue)"   
    return                                                   
  end if     
this code uses (selects) the video card to which the monitor is connected.. how to select another
(secondary)?
Post Reply