Repository: wolfpld/tracy
File: examples/OpenCLVectorAdd/OpenCLVectorAdd.cpp (C++)

Code before:
#include <CL/cl.h>

#include <Tracy.hpp>
#include <TracyOpenCL.hpp>

#define CL_ASSERT(err)                                              \
    if((err) != CL_SUCCESS)                                         \
    {                                                               \
        std::cerr << "OpenCL Call Returned " << err << std::endl;   \
        assert(false);                                              \
    }

const char kernelSource[] =
"   void __kernel vectorAdd(global float* C, global float* A, global float* B, int N)  "
"   {                                                                                  "
"       int i = get_global_id(0);                                                      "
"       if (i < N) {                                                                   "
"           C[i] = A[i] + B[i];                                                        "
"       }                                                                              "
"   }                                                                                  ";

int main()
{
    using namespace std::chrono_literals;
    std::cout << "Sleep for 1s to wait profiler connection..." << std::endl;
    std::this_thread::sleep_for(1s);

    cl_platform_id platform;
    cl_device_id device;
    cl_context context;
    cl_command_queue commandQueue;
    cl_kernel vectorAddKernel;
    cl_program program;
    cl_int err;
    cl_mem bufferA, bufferB, bufferC;

    TracyCLCtx tracyCLCtx;

    {
        ZoneScopedN("OpenCL Init");

        cl_uint numPlatforms = 0;
        CL_ASSERT(clGetPlatformIDs(0, nullptr, &numPlatforms));

        if (numPlatforms == 0)
        {
            std::cerr << "Cannot find OpenCL platform to run this application" << std::endl;
            return 1;
        }

        CL_ASSERT(clGetPlatformIDs(1, &platform, nullptr));

Review comment (suggestion):
As far as I'm aware you don't need to do this if click the connect button before running the application. Also, since this is a short-lived application, defining the environment variable `TRACY_NO_EXIT=1` should prevent it from exiting too quickly.

Code after:

#include <Tracy.hpp>
#include <TracyOpenCL.hpp>

#define CL_ASSERT(err)                                              \
    if((err) != CL_SUCCESS)                                         \
    {                                                               \
        std::cerr << "OpenCL Call Returned " << err << std::endl;   \
        assert(false);                                              \
    }

const char kernelSource[] =
"   void __kernel vectorAdd(global float* C, global float* A, global float* B, int N)  "
"   {                                                                                  "
"       int i = get_global_id(0);                                                      "
"       if (i < N) {                                                                   "
"           C[i] = A[i] + B[i];                                                        "
"       }                                                                              "
"   }                                                                                  ";

int main()
{
    cl_platform_id platform;
    cl_device_id device;
    cl_context context;
    cl_command_queue commandQueue;
    cl_kernel vectorAddKernel;
    cl_program program;
    cl_int err;
    cl_mem bufferA, bufferB, bufferC;

    TracyCLCtx tracyCLCtx;

    {
        ZoneScopedN("OpenCL Init");

        cl_uint numPlatforms = 0;
        CL_ASSERT(clGetPlatformIDs(0, nullptr, &numPlatforms));

        if (numPlatforms == 0)
        {
            std::cerr << "Cannot find OpenCL platform to run this application" << std::endl;
            return 1;
        }

        CL_ASSERT(clGetPlatformIDs(1, &platform, nullptr));

        size_t platformNameBufferSize = 0;
        CL_ASSERT(clGetPlatformInfo(platform, CL_PLATFORM_NAME, 0, nullptr, &platformNameBufferSize));
        std::string platformName(platformNameBufferSize, '\0');
        CL_ASSERT(clGetPlatformInfo(platform, CL_PLATFORM_NAME, platformNameBufferSize, &platformName[0], nullptr));