Adding OpenCL to a JUCE project for easy GPU access - Working Method Included

Got it working. :slight_smile:

Full Workflow:

I post the full workflow I used to convert the OpenCL Wrapper project into JUCE as I presume the OpenCL Wrapper may be updated over time but likely not directly disappear. Similarly things may change in JUCE. So simply sharing the files without the workflow is likely less safe for future reference.

As long as this forum, the OpenCL Wrapper, and JUCE continue to exist this should work with perhaps minor alterations over time.

EDITING OPEN CL WRAPPER PROJECT FOR JUCE

  1. Clone OpenCL Wrapper here: https://github.com/ProjectPhysX/OpenCL-Wrapper
  2. Open project in Visual Studio 2022 and ensure runs to start with.
  3. Move all files inside OpenCL-Wrapper/src/OpenCL/include/CL/ to OpenCL-Wrapper/src/ so everything shares a same directory except the lib file.
  4. Right click project, select properties > C/C++ > remove the src/OpenCL/include/CL/ directory as an external include folder (as we have now moved the files out of here) - forget where exactly this setting is but it is there
  5. Select “Show All Files” to see the new files in Visual Studio as per https://stackoverflow.com/a/69812456, click them and set include in project to “true” so you can see them.
  6. Keep trying to build and fix #include errors from moving files until solved.
  7. Ensure can still build project.
  8. Open utilities.hpp and right click any to_string function there, rename to cl_to_string, and select ALL possible functions that come up to rename this way (due to conflict otherwise with std::to_string once you bring over to JUCE).
  9. Ensure can still build project (should still all work perfectly)

ADDING TO JUCE

  1. Copy all these .h/.hpp/.cpp files from the modified OpenCL Wrapper project into a folder like OpenCL inside your Projucer project directory and drag this folder into your Projucer File Directory to add it to project.
  2. Copy OpenCL.lib into your Builds/VisualStudio2022 folder and add it in the Projucer Exporters Visual Studio 2022 “External Libraries to Link” as OpenCL.lib.
  3. Comment out with /* */ the contents of the function print_message of utilities.hpp as this will break in JUCE now once ported over.
  4. Open anywhere in your JUCE project code in Visual Studio and add
    #include "OpenCL/opencl.hpp"
  5. Run this test code taken from the Open CL Wrapper main.cpp in that page (with modification made to DBG output):
	//=====================
	//TEST OPENCL
	//===================
	DBG("START OPENCL TEST");
	Device device(select_device_with_most_flops()); // compile OpenCL C code for the fastest available device

	const uint N = 1024u; // size of vectors
	Memory<float> A(device, N); // allocate memory on both host and device
	Memory<float> B(device, N);
	Memory<float> C(device, N);

	Kernel add_kernel(device, N, "add_kernel", A, B, C); // kernel that runs on the device

	for (uint n = 0u; n < N; n++) {
		A[n] = 3.0f; // initialize memory
		B[n] = 2.0f;
		C[n] = 1.0f;
	}

	DBG("Value before kernel execution: C[0] = " + cl_to_string(C[0]));

	A.write_to_device(); // copy data from host memory to device memory
	B.write_to_device();
	add_kernel.run(); // run add_kernel on the device
	C.read_from_device(); // copy data from device memory to host memory

	DBG("Value after kernel execution: C[0] = " + cl_to_string(C[0]));
	
	//===================
  1. You will see the debugged math performed on the GPU successfully.

:slight_smile:

2 Likes