Intel® Advisor User Guide

ID 766448
Date 10/31/2024
Public
Document Table of Contents

Issue: Const Reference to a Host Pointer Used to Initialize a Buffer

When porting applications from C++ to SYCL, these issues may be implicitly present. The convention for passing arguments to a function is defined in C++. If a function requires read-only parameters, they are sent in as const references and any parameter that has to be used for read-write will be without a const. This causes a secondary issue in SYCL algorithms if these const references are used by the algorithm to construct buffers. Since the type of access required to use this data is not known at the time of construction of the buffer, the Intel® oneAPI DPC++/C++ Compiler is conservative and creates copies of const references while creating the buffers. If these are large arrays, the cost incurred is not trivial. This issue only affects the CPU device as everything is communicated through shared memory and the copying of data pointer to the host pointer is not necessary.

The va_const.cpp example demonstrates such an issue and indicates buffer copy in the application that needs to be looked at.

To eliminate this copy, you should change the code sample in the following way:

// Old code:
// The function prototype passed in the read-only buffers as const, which is the
// recommended practive in C++
//
// void vec_add(queue &q, const float A[], const float B[], ...) {

void vec_add(queue &q, float A[], float B[], float C[],
             const int size) {
  ...
}

NOTE:
The recommended practice in C++ is to pass in read-only parameters as const values. However, this causes the Intel® oneAPI DPC++/C++ Compiler to be conservative and create a copy. If you are porting C++ code to SYCL, the static rule-check should help you identify such issues in your application.