- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page