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*
836 ディスカッション

Simple example to Define function than call it from parallel_for kernal

pril
初心者
2,608件の閲覧回数

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,593件の閲覧回数

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

元の投稿で解決策を見る

3 返答(返信)
GouthamK_Intel
モデレーター
2,594件の閲覧回数

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

pril
初心者
2,584件の閲覧回数

Thank you   this example  works great .

GouthamK_Intel
モデレーター
2,567件の閲覧回数

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


返信