Xojo Conferences

Platforms to show: All Mac Windows Linux Cross-Platform

/MacFrameworks/OpenCL/OpenCL HelloWorld
Required plugins for this example: MBS MacFrameworks Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacFrameworks/OpenCL/OpenCL HelloWorld
This example is the version from Fri, 29th Jan 2015.
Project "OpenCL HelloWorld.rbp"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class Window1 Inherits Window
Control List Inherits Listbox
ControlInstance List Inherits Listbox
End Control
EventHandler Sub Open() // OpenCL example. // based on an example from Apple // http://developer.apple.com/library/mac/#documentation/Performance/Conceptual/OpenCL_MacProgGuide/Example:Hello,World/Example:Hello,World.html // //////////////////////////////////////////////////////////////////////////////// // Use a static data size for simplicity // const DATA_SIZE = 1024 //////////////////////////////////////////////////////////////////////////////// // Simple compute kernel that computes the square of an input array. // dim KernelSource as string = EndOfLine+_ "__kernel void square( "+EndOfLine+_ " __global float* input, "+EndOfLine+_ " __global float* output, "+EndOfLine+_ " const unsigned int count) "+EndOfLine+_ "{ "+EndOfLine+_ " int i = get_global_id(0); "+EndOfLine+_ " if(i < count) "+EndOfLine+_ " output[i] = input[i] * input[i]; "+EndOfLine+_ "} "+EndOfLine+_ "" //////////////////////////////////////////////////////////////////////////////// // Get data on which to operate // dim outputdata as new MemoryBlock(DATA_SIZE*4) dim inputdata as new MemoryBlock(DATA_SIZE*4) for i as integer = 0 to DATA_SIZE-1 inputdata.SingleValue(i*4)=rnd next // Get an ID for the device dim devices(-1) as CLDeviceMBS = OpenCLMBS.AllDevices(CLDeviceMBS.kDeviceTypeGPU) if UBound(devices)=-1 then MsgBox "no devices found." Return end if dim device as CLDeviceMBS = devices(0) // we use first one // Create a context // dim context as new CLContextMBS(device, CLContextMBS.kErrorModeLogMessagesToSystemLog) // Create a command queue // dim queue as new CLCommandQueueMBS(context, device, 0) // Create the compute program from the source buffer // dim program as new CLProgramMBS(context, KernelSource) // Build the program executable // program.BuildProgram if program.lasterror <> 0 then MsgBox "Error: Failed to build program executable"+EndOfLine+EndOfLine+Program.BuildLog(device) Return end if // take a look on the code 'dim binarysizes(-1) as UInt64 = Program.BinarySizes 'dim binaries(-1) as string = Program.Binaries // Create the compute kernel in the program we wish to run // dim kernel as new clKernelMBS(program, "square") // Create the input and output arrays in device memory for our calculation dim input as new CLMemMBS(context, CLMEMMBS.kMemoryReadOnly, inputdata.Size) dim output as new CLMemMBS(context, CLMEMMBS.kMemoryWriteOnly, outputdata.Size) // Write our data set into the input array in device memory queue.EnqueueWriteBuffer(input, 0, inputdata.Size, inputdata) // Set the arguments to our compute kernel // kernel.SetKernelArgMem(0, input) kernel.SetKernelArgMem(1, output) kernel.SetKernelArgInt32(2, DATA_SIZE) // Get the maximum work-group size for executing the kernel on the device dim localsize as integer = kernel.GetKernelWorkGroupSize(Device) // Execute the kernel over the entire range of the data set // dim globalsize as integer = DATA_SIZE queue.EnqueueNDRangeKernel(kernel, globalsize, localsize) // Wait for the command queue to get serviced before reading back results queue.finish // Read the results from the device // queue.EnqueueReadBuffer(output, 0, outputdata.size, outputdata) // Show result for i as integer = 0 to DATA_SIZE-1 List.AddRow str(i+1) List.Cell(List.LastIndex,1) = str(inputdata.SingleValue(i*4)) List.Cell(List.LastIndex,2) = str(outputdata.SingleValue(i*4)) next End EventHandler
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
End Project

See also:

Feedback, Comments & Corrections

The items on this page are in the following plugins: MBS MacFrameworks Plugin.

MBS Xojo Plugins