Compile and Execute Sample SYCL Code

This example shows the steps to compile and execute sample SYCL code that demonstrates basic offloading to a device.

  1. Consider the following sample SYCL program, captured in the file simple-sycl-app.cpp:
    #include <CL/sycl.hpp>
    
    int main() {
      // Creating SYCL queue
      cl::sycl::queue Queue;
    
      // Creating buffer of 4 ints
      cl::sycl::buffer<cl::sycl::cl_int, 1> Buffer(4);
    
      // Size of index space for kernel
      cl::sycl::range<1> NumOfWorkItems{Buffer.get_count()};
    
      // Submitting command group to queue
      Queue.submit([&](cl::sycl::handler &cgh) {
        // Getting write only access to the buffer on a device
        auto Accessor = Buffer.get_access<cl::sycl::access::mode::write>(cgh);
        // Executing kernel
        cgh.parallel_for<class FillBuffer>(
            NumOfWorkItems, [=](cl::sycl::id<1> WIid) {
              // Fill buffer with indexes
              Accessor[WIid] = (cl::sycl::cl_int)WIid.get(0);
            });
      });
    
      // Getting read only access to the buffer on the host
      const auto HostAccessor = Buffer.get_access<cl::sycl::access::mode::read>();
    
      // Check that the results are correct
      bool MismatchFound = false;
      for (size_t I = 0; I < Buffer.get_count(); ++I) {
        if (HostAccessor[I] != I) {
          std::cout << "The result is incorrect for element: " << I
                    << " , expected: " << I << " , got: " << HostAccessor[I]
                    << std::endl;
          MismatchFound = true;
        }
      }
    
      if (!MismatchFound) {
        std::cout << "The results are correct!" << std::endl;
      }
    
      return MismatchFound;
    }
    
  2. Build the simple-sycl-app, with SYCL enabled.

    Linux:

    icpx -fsycl simple-sycl-app.cpp -o simple-sycl-app

    Windows:

    icx -fsycl simple-sycl-app.cpp /Fesimple-sycl-app.exe
  3. Run the simple-sycl-app from the command line.

    Linux:

    ./simple-sycl-app

    Windows:

    simple-sycl-app.exe
  4. You will see the following output:

    The results are correct!

Next Steps