0

I am learning dpc++ and trying to implement 2d array matrix program. I am stuck in between the program. Please check the blow code and support me. Need help.

#include<CL/sycl.hpp>
#include<stdio.h>
#define N 2

using namespace sycl;
int main(){
int ha[N][N] = {1,2,3,4};
int hb[N][N] = {1,2,3,4};
int hc[N][N];

//printing ha and hb arrays here

//synchronization block start
{
queue q;
buffer my_buf1(ha);
buffer my_buf2(hb);
buffer my_buf3(hc);

q.submit([&](handler &h) {
stream out(1024,256,h);
accessor dev_acs1(my_buf1,h);
accessor dev_acs2(my_buf2,h);
accessor dev_acs3(my_buf3,h);

I am not getting how to write parallel_for function to add ha[i][j] and hb[i][j] and put it in hc[i][j]

h.parallel_for(range{N,N},[=](id<2> idx){
----------------------
});

host_accessor host_hc(my_buf3);
for(int i=0;i<N;i++)
  for(int j=0;j<N;j++)
     std::cout<<host_hc[i][j];
}
return 0;
}
Filburt
  • 17,626
  • 12
  • 64
  • 115

1 Answers1

0

Please find below the code for 2d matrix addition:

#include<CL/sycl.hpp>
#include<stdio.h>
#include<iostream>
#define N 2

using namespace sycl;
int main(){
        int ha[N][N] = {1,2,3,4};
        int hb[N][N] = {1,2,3,4};
        int hc[N][N]={0};

//printing ha and hb arrays here

//synchronization block start
{
queue q;

buffer my_buf1(*ha,range<2>(N,N));
buffer my_buf2(*hb,range<2>(N,N));
buffer my_buf3(*hc,range<2>(N,N));

q.submit([&](handler &h) {
stream out(1024,256,h);
accessor dev_acs1(my_buf1,h,read_only);
accessor dev_acs2(my_buf2,h,read_only);
accessor dev_acs3(my_buf3,h,write_only);
h.parallel_for(range{N,N},[=](id<2> idx){
int i = idx[1];
int j = idx[0];

dev_acs3[i][j]=dev_acs1[i][j] + dev_acs2[i][j]; // (or) dev_acs3[idx]=dev_acs1[idx] + dev_acs2[idx];

});
});

host_accessor host_hc(my_buf3);
for(int i=0;i<N;i++)
  for(int j=0;j<N;j++)
     std::cout<<host_hc[i][j]<<"\n" ;
}
return 0;
}