Intel® oneAPI DPC++/C++ Compiler
Talk to fellow users of Intel® oneAPI DPC++/C++ Compiler and companion tools like Intel® oneAPI DPC++ Library, Intel® DPC++ Compatibility Tool, and Intel® Distribution for GDB*
838 토론

Simple example to Define function than call it from parallel_for kernal

pril
초보자
2,637 조회수

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 포인트
1 솔루션
GouthamK_Intel
중재자
2,622 조회수

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

원본 게시물의 솔루션 보기

0 포인트
3 응답
GouthamK_Intel
중재자
2,623 조회수

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

0 포인트
pril
초보자
2,613 조회수

Thank you   this example  works great .

0 포인트
GouthamK_Intel
중재자
2,596 조회수

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


0 포인트
응답