Intel® oneAPI Data Parallel C++
Support for Intel® oneAPI DPC++ Compiler, Intel® oneAPI DPC++ Library, Intel® DPC++ Compatibility Tool, and GDB*
Announcements
Welcome to the Intel Community. If you get an answer you like, please mark it as an Accepted Solution to help others. Thank you!
For the latest information on Intel’s response to the Log4j/Log4Shell vulnerability, please see Intel-SA-00646
280 Discussions

Simple example to Define function than call it from parallel_for kernal

pril
Novice
352 Views

Hi 
Thanks for DPC++ It's great,  Is there  a simple example for  :

Creating a simple  void function like  this and calling it from the  parallel_for kernal ?




#include <CL/sycl.hpp>
#include <iostream>

using namespace sycl;

 

Void Mycustomfunction( int avariable ){

  int myoutput=avariable+2;

  return myoutput;

}

int main(){

int N =5;

queue q ;

int *data = malloc_shared<int>(N, q);

for(int i = 0; N < 5; i++) {
data[i]=i;
}

q.parallel_for(range<1>(N), [=] (id<1> i){

data[i]=Mycustomfunction(i);

}).wait();

}

 

 

 

 

 

0 Kudos
1 Solution
GouthamK_Intel
Moderator
337 Views

Hi Prilvesh Krishna,

>>Thanks for DPC++ It's great,  

Thank you for the kind feedback, we are glad to know that you liked DPC++.

>>Creating a simple void function like this and calling it from the parallel_for kernal ?

You can achieve the above scenario in two ways.

1) Use accessor datatype instead of int* variable inside the function definition /declaration.

Example:

void Mycustomfunction(cl::sycl::accessor<int, 1, cl::sycl::access::mode::write> p, int x);

2) Typecast accessor address (aa) to (int *) inside the kernel and use int * in function definition/ declaration.

Example:

void Mycustomfunction(int *p, int x);

Also, I'm attaching the complete working code sample(with both methods) below as per your requirement.

#include<iostream>
#include<CL/sycl.hpp>

using namespace std;
using namespace sycl;

//void Mycustomfunction(int *p, int x); For Method 2 uncomment this
void Mycustomfunction(cl::sycl::accessor<int, 1, cl::sycl::access::mode::write> p, int x);

int main()
{
   const int N=5;
    int a[N];
    for(int i=0;i<N;i++)
    {
        a[i]=i;
    }
    cpu_selector device_selector;
    queue q(device_selector);
    {
        buffer<int ,1> a_buff(a,range<1> (N));
        q.submit([&](handler &h)
        {
            auto aa=a_buff.get_access<access::mode::write>(h);
            h.parallel_for(range<1>{N},[=](id<1> item)
            {
                int index=item;
                Mycustomfunction(aa,index);
                 //Mycustomfunction((int *)&aa[0], index); For Method 2 uncomment this
            });
      });
    }
    for(int i=0;i<N;i++)
    {
        cout<<a[i]<<"\t";
    }
    cout<<"\n"<<std::endl;
}

void Mycustomfunction(cl::sycl::accessor<int, 1, cl::sycl::access::mode::write> p, int x){
        p[x]=x+2;
}

/* For Method 2 uncomment this
void Mycustomfunction(int *p,int x)
{
    p[x]=x+2;
}
*/

Please feel free to reach out to us if you are facing any further issues in running this code.

Have a Good day!

 

Thanks & Regards

Goutham

View solution in original post

3 Replies
GouthamK_Intel
Moderator
338 Views

Hi Prilvesh Krishna,

>>Thanks for DPC++ It's great,  

Thank you for the kind feedback, we are glad to know that you liked DPC++.

>>Creating a simple void function like this and calling it from the parallel_for kernal ?

You can achieve the above scenario in two ways.

1) Use accessor datatype instead of int* variable inside the function definition /declaration.

Example:

void Mycustomfunction(cl::sycl::accessor<int, 1, cl::sycl::access::mode::write> p, int x);

2) Typecast accessor address (aa) to (int *) inside the kernel and use int * in function definition/ declaration.

Example:

void Mycustomfunction(int *p, int x);

Also, I'm attaching the complete working code sample(with both methods) below as per your requirement.

#include<iostream>
#include<CL/sycl.hpp>

using namespace std;
using namespace sycl;

//void Mycustomfunction(int *p, int x); For Method 2 uncomment this
void Mycustomfunction(cl::sycl::accessor<int, 1, cl::sycl::access::mode::write> p, int x);

int main()
{
   const int N=5;
    int a[N];
    for(int i=0;i<N;i++)
    {
        a[i]=i;
    }
    cpu_selector device_selector;
    queue q(device_selector);
    {
        buffer<int ,1> a_buff(a,range<1> (N));
        q.submit([&](handler &h)
        {
            auto aa=a_buff.get_access<access::mode::write>(h);
            h.parallel_for(range<1>{N},[=](id<1> item)
            {
                int index=item;
                Mycustomfunction(aa,index);
                 //Mycustomfunction((int *)&aa[0], index); For Method 2 uncomment this
            });
      });
    }
    for(int i=0;i<N;i++)
    {
        cout<<a[i]<<"\t";
    }
    cout<<"\n"<<std::endl;
}

void Mycustomfunction(cl::sycl::accessor<int, 1, cl::sycl::access::mode::write> p, int x){
        p[x]=x+2;
}

/* For Method 2 uncomment this
void Mycustomfunction(int *p,int x)
{
    p[x]=x+2;
}
*/

Please feel free to reach out to us if you are facing any further issues in running this code.

Have a Good day!

 

Thanks & Regards

Goutham

View solution in original post

pril
Novice
327 Views

Thank you   this example  works great .

GouthamK_Intel
Moderator
310 Views

Hi Prilvesh Krishna, 

Thanks for the confirmation!

Glad to know that your issue is resolved.

As this issue has been resolved, we will no longer respond to this thread. 

If you require any additional assistance from Intel, please start a new thread. 

Any further interaction in this thread will be considered community only. 

Have a Good day.


Thanks & Regards

Goutham


Reply