<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic SYCL named function objects compile error ( Needs keyword &amp;quot;const&amp;quot; for operator () ) in Intel® oneAPI DPC++/C++ Compiler</title>
    <link>https://community.intel.com/t5/Intel-oneAPI-DPC-C-Compiler/SYCL-named-function-objects-compile-error-Needs-keyword-quot/m-p/1619057#M3994</link>
    <description>&lt;P&gt;Hello,&lt;BR /&gt;&lt;BR /&gt;I am trying to compile a simple SYCL program which runs parallel for using "Named function objects" instead of&amp;nbsp; lambdas. I get a compile error which ultimately is resolved if I add "const" to the end of the overloaded () operator and I am not sure why that is the case. Could someone please point out why the "const" qualifier is needed and if there are any ways around it ?&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="cpp"&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;sycl/sycl.hpp&amp;gt;

#define SIZE 10
#define DIV 12.3

using namespace sycl;

class test_kernel
{
public:
   test_kernel(int* xx, const double yy):x(xx), y(yy)
   {}

   void operator() (nd_item&amp;lt;3&amp;gt; itm)
   {
      int idx = itm.get_global_id(0);
      if(idx &amp;lt; SIZE)
        x[idx] = y/12.3;
   }

private:
   int* x;
   double y;

};

int main()
{
    queue m_que{default_selector_v};
    range&amp;lt;3&amp;gt; global_range(SIZE,1,1);
    range&amp;lt;3&amp;gt; local_range(SIZE,1,1);
    std::cout &amp;lt;&amp;lt; "Device Name: " &amp;lt;&amp;lt; m_que.get_device().get_info&amp;lt;info::device::name&amp;gt;() &amp;lt;&amp;lt; std::endl;
    
    double val = DIV * 1000;
    int* x = malloc_shared&amp;lt;int&amp;gt;(SIZE, m_que);
    
    for(int i =0; i&amp;lt;SIZE; i++)
    {
        x[i] = 10;
    }
    
    m_que.parallel_for(sycl::nd_range&amp;lt;3&amp;gt;{global_range,local_range}, test_kernel{x, val}).wait();
    
    for(int i = 0; i &amp;lt; SIZE; i++)
    {
        if(x[i] != 1000)
        {
            std::cout &amp;lt;&amp;lt; "x[ " &amp;lt;&amp;lt; i &amp;lt;&amp;lt; "] = " &amp;lt;&amp;lt; x[i] &amp;lt;&amp;lt; " instead of " &amp;lt;&amp;lt; int(val/DIV) &amp;lt;&amp;lt; std::endl;
        }
    }
    
    free(x, m_que);
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;BR /&gt;Here is the compile command and error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="bash"&gt;snarayanan@crane:builds$ icpx -fsycl -o submit_to_gpu_complex ../src/submit_to_gpu_complex.cpp
In file included from ../src/submit_to_gpu_complex.cpp:2:
In file included from /usr/people/shared/tools/rocky/9/intel_oneapi/2024.1.0/compiler/2024.1/bin/compiler/../../include/sycl/sycl.hpp:16:
In file included from /usr/people/shared/tools/rocky/9/intel_oneapi/2024.1.0/compiler/2024.1/bin/compiler/../../include/sycl/backend.hpp:27:
/usr/people/shared/tools/rocky/9/intel_oneapi/2024.1.0/compiler/2024.1/bin/compiler/../../include/sycl/handler.hpp:1540:5: error: no matching function for call to object of type 'const test_kernel'
1540 |     KernelFunc(detail::Builder::getElement(detail::declptr&amp;lt;ElementType&amp;gt;()));
|     ^~~~~~~~~~
/usr/people/shared/tools/rocky/9/intel_oneapi/2024.1.0/compiler/2024.1/bin/compiler/../../include/sycl/handler.hpp:1617:10: note: in instantiation of function template specialization 'sycl::handler::kernel_parallel_for&amp;lt;test_kernel, sycl::nd_item&amp;lt;3&amp;gt;, test_kernel&amp;gt;' requested here
1617 |       h-&amp;gt;kernel_parallel_for&amp;lt;TypesToForward..., Props...&amp;gt;(Args...);
|          ^
/usr/people/shared/tools/rocky/9/intel_oneapi/2024.1.0/compiler/2024.1/bin/compiler/../../include/sycl/handler.hpp:1703:29: note: in instantiation of function template specialization 'sycl::handler::KernelPropertiesUnpackerImpl&amp;lt;&amp;gt;::kernel_parallel_for_unpack&amp;lt;test_kernel, sycl::nd_item&amp;lt;3&amp;gt;, test_kernel, test_kernel&amp;gt;' requested here
1703 |           Unpacker.template kernel_parallel_for_unpack&amp;lt;KernelName, ElementType,
|                             ^
/usr/people/shared/tools/rocky/9/intel_oneapi/2024.1.0/compiler/2024.1/bin/compiler/../../include/sycl/handler.hpp:1699:5: note: in instantiation of function template specialization 'sycl::handler::unpack&amp;lt;test_kernel, test_kernel, sycl::ext::oneapi::experimental::properties&amp;lt;std::tuple&amp;lt;&amp;gt;&amp;gt;, false, (lambda at /usr/people/shared/tools/rocky/9/intel_oneapi/2024.1.0/compiler/2024.1/bin/compiler/../../include/sycl/handler.hpp:1702:21)&amp;gt;' requested here
1699 |     unpack&amp;lt;KernelName, KernelType, PropertiesT,
|     ^
/usr/people/shared/tools/rocky/9/intel_oneapi/2024.1.0/compiler/2024.1/bin/compiler/../../include/sycl/handler.hpp:1373:5: note: in instantiation of function template specialization 'sycl::handler::kernel_parallel_for_wrapper&amp;lt;test_kernel, sycl::nd_item&amp;lt;3&amp;gt;, test_kernel, sycl::ext::oneapi::experimental::properties&amp;lt;std::tuple&amp;lt;&amp;gt;&amp;gt;&amp;gt;' requested here
1373 |     kernel_parallel_for_wrapper&amp;lt;NameT, TransformedArgType, KernelType,
|     ^
/usr/people/shared/tools/rocky/9/intel_oneapi/2024.1.0/compiler/2024.1/bin/compiler/../../include/sycl/handler.hpp:2370:5: note: in instantiation of function template specialization 'sycl::handler::parallel_for_impl&amp;lt;sycl::detail::auto_name, test_kernel, 3, sycl::ext::oneapi::experimental::properties&amp;lt;std::tuple&amp;lt;&amp;gt;&amp;gt;&amp;gt;' requested here
2370 |     parallel_for_impl&amp;lt;KernelName&amp;gt;(Range, Properties, std::move(KernelFunc));
|     ^
/usr/people/shared/tools/rocky/9/intel_oneapi/2024.1.0/compiler/2024.1/bin/compiler/../../include/sycl/queue.hpp:2504:24: note: in instantiation of function template specialization 'sycl::handler::parallel_for&amp;lt;sycl::detail::auto_name, test_kernel, sycl::ext::oneapi::experimental::properties&amp;lt;std::tuple&amp;lt;&amp;gt;&amp;gt;, 3&amp;gt;' requested here
2504 |           CGH.template parallel_for&amp;lt;KernelName&amp;gt;(Range, Properties, Rest...);
|                        ^
/usr/people/shared/tools/rocky/9/intel_oneapi/2024.1.0/compiler/2024.1/bin/compiler/../../include/sycl/queue.hpp:2519:12: note: in instantiation of function template specialization 'sycl::queue::parallel_for&amp;lt;sycl::detail::auto_name, 3, sycl::ext::oneapi::experimental::properties&amp;lt;std::tuple&amp;lt;&amp;gt;&amp;gt;, test_kernel &amp;amp;&amp;gt;' requested here
2519 |     return parallel_for&amp;lt;KernelName&amp;gt;(
|            ^
../src/submit_to_gpu_complex.cpp:43:11: note: in instantiation of function template specialization 'sycl::queue::parallel_for&amp;lt;sycl::detail::auto_name, 3, test_kernel&amp;gt;' requested here
43 |     m_que.parallel_for(sycl::nd_range&amp;lt;3&amp;gt;{global_range,local_range}, test_kernel{x, val}).wait();
|           ^
../src/submit_to_gpu_complex.cpp:15:9: note: candidate function not viable: 'this' argument has type 'const test_kernel', but method is not marked const
15 |    void operator() (nd_item&amp;lt;3&amp;gt; itm)
|         ^
1 error generated.&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;DPC++ compiler version:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="bash"&gt;snarayanan@crane:builds$ icpx --version
Intel(R) oneAPI DPC++/C++ Compiler 2024.1.0 (2024.1.0.20240308)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/people/shared/tools/rocky/9/intel_oneapi/2024.1.0/compiler/2024.1/bin/compiler
Configuration file: /usr/people/shared/tools/rocky/9/intel_oneapi/2024.1.0/compiler/2024.1/bin/compiler/../icpx.cfg&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 29 Jul 2024 18:20:48 GMT</pubDate>
    <dc:creator>Sidarth</dc:creator>
    <dc:date>2024-07-29T18:20:48Z</dc:date>
    <item>
      <title>SYCL named function objects compile error ( Needs keyword "const" for operator () )</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-DPC-C-Compiler/SYCL-named-function-objects-compile-error-Needs-keyword-quot/m-p/1619057#M3994</link>
      <description>&lt;P&gt;Hello,&lt;BR /&gt;&lt;BR /&gt;I am trying to compile a simple SYCL program which runs parallel for using "Named function objects" instead of&amp;nbsp; lambdas. I get a compile error which ultimately is resolved if I add "const" to the end of the overloaded () operator and I am not sure why that is the case. Could someone please point out why the "const" qualifier is needed and if there are any ways around it ?&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="cpp"&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;sycl/sycl.hpp&amp;gt;

#define SIZE 10
#define DIV 12.3

using namespace sycl;

class test_kernel
{
public:
   test_kernel(int* xx, const double yy):x(xx), y(yy)
   {}

   void operator() (nd_item&amp;lt;3&amp;gt; itm)
   {
      int idx = itm.get_global_id(0);
      if(idx &amp;lt; SIZE)
        x[idx] = y/12.3;
   }

private:
   int* x;
   double y;

};

int main()
{
    queue m_que{default_selector_v};
    range&amp;lt;3&amp;gt; global_range(SIZE,1,1);
    range&amp;lt;3&amp;gt; local_range(SIZE,1,1);
    std::cout &amp;lt;&amp;lt; "Device Name: " &amp;lt;&amp;lt; m_que.get_device().get_info&amp;lt;info::device::name&amp;gt;() &amp;lt;&amp;lt; std::endl;
    
    double val = DIV * 1000;
    int* x = malloc_shared&amp;lt;int&amp;gt;(SIZE, m_que);
    
    for(int i =0; i&amp;lt;SIZE; i++)
    {
        x[i] = 10;
    }
    
    m_que.parallel_for(sycl::nd_range&amp;lt;3&amp;gt;{global_range,local_range}, test_kernel{x, val}).wait();
    
    for(int i = 0; i &amp;lt; SIZE; i++)
    {
        if(x[i] != 1000)
        {
            std::cout &amp;lt;&amp;lt; "x[ " &amp;lt;&amp;lt; i &amp;lt;&amp;lt; "] = " &amp;lt;&amp;lt; x[i] &amp;lt;&amp;lt; " instead of " &amp;lt;&amp;lt; int(val/DIV) &amp;lt;&amp;lt; std::endl;
        }
    }
    
    free(x, m_que);
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;BR /&gt;Here is the compile command and error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="bash"&gt;snarayanan@crane:builds$ icpx -fsycl -o submit_to_gpu_complex ../src/submit_to_gpu_complex.cpp
In file included from ../src/submit_to_gpu_complex.cpp:2:
In file included from /usr/people/shared/tools/rocky/9/intel_oneapi/2024.1.0/compiler/2024.1/bin/compiler/../../include/sycl/sycl.hpp:16:
In file included from /usr/people/shared/tools/rocky/9/intel_oneapi/2024.1.0/compiler/2024.1/bin/compiler/../../include/sycl/backend.hpp:27:
/usr/people/shared/tools/rocky/9/intel_oneapi/2024.1.0/compiler/2024.1/bin/compiler/../../include/sycl/handler.hpp:1540:5: error: no matching function for call to object of type 'const test_kernel'
1540 |     KernelFunc(detail::Builder::getElement(detail::declptr&amp;lt;ElementType&amp;gt;()));
|     ^~~~~~~~~~
/usr/people/shared/tools/rocky/9/intel_oneapi/2024.1.0/compiler/2024.1/bin/compiler/../../include/sycl/handler.hpp:1617:10: note: in instantiation of function template specialization 'sycl::handler::kernel_parallel_for&amp;lt;test_kernel, sycl::nd_item&amp;lt;3&amp;gt;, test_kernel&amp;gt;' requested here
1617 |       h-&amp;gt;kernel_parallel_for&amp;lt;TypesToForward..., Props...&amp;gt;(Args...);
|          ^
/usr/people/shared/tools/rocky/9/intel_oneapi/2024.1.0/compiler/2024.1/bin/compiler/../../include/sycl/handler.hpp:1703:29: note: in instantiation of function template specialization 'sycl::handler::KernelPropertiesUnpackerImpl&amp;lt;&amp;gt;::kernel_parallel_for_unpack&amp;lt;test_kernel, sycl::nd_item&amp;lt;3&amp;gt;, test_kernel, test_kernel&amp;gt;' requested here
1703 |           Unpacker.template kernel_parallel_for_unpack&amp;lt;KernelName, ElementType,
|                             ^
/usr/people/shared/tools/rocky/9/intel_oneapi/2024.1.0/compiler/2024.1/bin/compiler/../../include/sycl/handler.hpp:1699:5: note: in instantiation of function template specialization 'sycl::handler::unpack&amp;lt;test_kernel, test_kernel, sycl::ext::oneapi::experimental::properties&amp;lt;std::tuple&amp;lt;&amp;gt;&amp;gt;, false, (lambda at /usr/people/shared/tools/rocky/9/intel_oneapi/2024.1.0/compiler/2024.1/bin/compiler/../../include/sycl/handler.hpp:1702:21)&amp;gt;' requested here
1699 |     unpack&amp;lt;KernelName, KernelType, PropertiesT,
|     ^
/usr/people/shared/tools/rocky/9/intel_oneapi/2024.1.0/compiler/2024.1/bin/compiler/../../include/sycl/handler.hpp:1373:5: note: in instantiation of function template specialization 'sycl::handler::kernel_parallel_for_wrapper&amp;lt;test_kernel, sycl::nd_item&amp;lt;3&amp;gt;, test_kernel, sycl::ext::oneapi::experimental::properties&amp;lt;std::tuple&amp;lt;&amp;gt;&amp;gt;&amp;gt;' requested here
1373 |     kernel_parallel_for_wrapper&amp;lt;NameT, TransformedArgType, KernelType,
|     ^
/usr/people/shared/tools/rocky/9/intel_oneapi/2024.1.0/compiler/2024.1/bin/compiler/../../include/sycl/handler.hpp:2370:5: note: in instantiation of function template specialization 'sycl::handler::parallel_for_impl&amp;lt;sycl::detail::auto_name, test_kernel, 3, sycl::ext::oneapi::experimental::properties&amp;lt;std::tuple&amp;lt;&amp;gt;&amp;gt;&amp;gt;' requested here
2370 |     parallel_for_impl&amp;lt;KernelName&amp;gt;(Range, Properties, std::move(KernelFunc));
|     ^
/usr/people/shared/tools/rocky/9/intel_oneapi/2024.1.0/compiler/2024.1/bin/compiler/../../include/sycl/queue.hpp:2504:24: note: in instantiation of function template specialization 'sycl::handler::parallel_for&amp;lt;sycl::detail::auto_name, test_kernel, sycl::ext::oneapi::experimental::properties&amp;lt;std::tuple&amp;lt;&amp;gt;&amp;gt;, 3&amp;gt;' requested here
2504 |           CGH.template parallel_for&amp;lt;KernelName&amp;gt;(Range, Properties, Rest...);
|                        ^
/usr/people/shared/tools/rocky/9/intel_oneapi/2024.1.0/compiler/2024.1/bin/compiler/../../include/sycl/queue.hpp:2519:12: note: in instantiation of function template specialization 'sycl::queue::parallel_for&amp;lt;sycl::detail::auto_name, 3, sycl::ext::oneapi::experimental::properties&amp;lt;std::tuple&amp;lt;&amp;gt;&amp;gt;, test_kernel &amp;amp;&amp;gt;' requested here
2519 |     return parallel_for&amp;lt;KernelName&amp;gt;(
|            ^
../src/submit_to_gpu_complex.cpp:43:11: note: in instantiation of function template specialization 'sycl::queue::parallel_for&amp;lt;sycl::detail::auto_name, 3, test_kernel&amp;gt;' requested here
43 |     m_que.parallel_for(sycl::nd_range&amp;lt;3&amp;gt;{global_range,local_range}, test_kernel{x, val}).wait();
|           ^
../src/submit_to_gpu_complex.cpp:15:9: note: candidate function not viable: 'this' argument has type 'const test_kernel', but method is not marked const
15 |    void operator() (nd_item&amp;lt;3&amp;gt; itm)
|         ^
1 error generated.&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;DPC++ compiler version:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="bash"&gt;snarayanan@crane:builds$ icpx --version
Intel(R) oneAPI DPC++/C++ Compiler 2024.1.0 (2024.1.0.20240308)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/people/shared/tools/rocky/9/intel_oneapi/2024.1.0/compiler/2024.1/bin/compiler
Configuration file: /usr/people/shared/tools/rocky/9/intel_oneapi/2024.1.0/compiler/2024.1/bin/compiler/../icpx.cfg&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jul 2024 18:20:48 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-DPC-C-Compiler/SYCL-named-function-objects-compile-error-Needs-keyword-quot/m-p/1619057#M3994</guid>
      <dc:creator>Sidarth</dc:creator>
      <dc:date>2024-07-29T18:20:48Z</dc:date>
    </item>
    <item>
      <title>Re: SYCL named function objects compile error ( Needs keyword "const" for operator () )</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-DPC-C-Compiler/SYCL-named-function-objects-compile-error-Needs-keyword-quot/m-p/1619464#M3996</link>
      <description>&lt;P&gt;I'm escalating your question to our internal team for further investigation.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jul 2024 19:39:01 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-DPC-C-Compiler/SYCL-named-function-objects-compile-error-Needs-keyword-quot/m-p/1619464#M3996</guid>
      <dc:creator>Alex_Y_Intel</dc:creator>
      <dc:date>2024-07-30T19:39:01Z</dc:date>
    </item>
  </channel>
</rss>

