|
| 1 | +//============================================================== |
| 2 | +// Copyright © Intel Corporation |
| 3 | +// |
| 4 | +// SPDX-License-Identifier: MIT |
| 5 | +// ============================================================= |
| 6 | + |
| 7 | + |
| 8 | +#include <CL/sycl.hpp> |
| 9 | +#include <iomanip> |
| 10 | + |
| 11 | +using namespace sycl; |
| 12 | + |
| 13 | +int main() { |
| 14 | + |
| 15 | + size_t N = 16; |
| 16 | + std::cout << "MATRIX_SIZE : " << N << "x" << N << std::endl; |
| 17 | + |
| 18 | + //# Define vectors for matrices |
| 19 | + std::vector<float> matrix_a(N*N); |
| 20 | + std::vector<float> matrix_b(N*N); |
| 21 | + std::vector<float> matrix_c(N*N); |
| 22 | + std::vector<float> matrix_d(N*N); |
| 23 | + |
| 24 | + //# Initialize matrices with values |
| 25 | + float v1 = 2.f; |
| 26 | + float v2 = 3.f; |
| 27 | + for (int i=0; i<N; i++) |
| 28 | + for (int j=0; j<N; j++){ |
| 29 | + matrix_a[i*N+j] = v1++; |
| 30 | + matrix_b[i*N+j] = v2++; |
| 31 | + matrix_c[i*N+j] = 0.f; |
| 32 | + matrix_d[i*N+j] = 0.f; |
| 33 | + } |
| 34 | + |
| 35 | + //# Define queue with default device for offloading computation |
| 36 | + queue q; |
| 37 | + std::cout << "Offload Device : " << q.get_device().get_info<info::device::name>() << std::endl; |
| 38 | + |
| 39 | + //# Create buffers for matrices |
| 40 | + buffer a(matrix_a); |
| 41 | + buffer b(matrix_b); |
| 42 | + buffer c(matrix_c); |
| 43 | + |
| 44 | + //# Submit command groups to execute on device |
| 45 | + q.submit([&](handler &h){ |
| 46 | + //# Create accessors to copy buffers to the device |
| 47 | + accessor A(a, h, read_only); |
| 48 | + accessor B(b, h, read_only); |
| 49 | + accessor C(c, h, write_only); |
| 50 | + |
| 51 | + //# Define size for ND-range and work-group size |
| 52 | + range<2> global_size(N,N); |
| 53 | + range<2> work_group_size(N,N); |
| 54 | + |
| 55 | + //# Create local accessors |
| 56 | + accessor<float, 2, access::mode::read_write, access::target::local> A_local(range<2>(N, N), h); |
| 57 | + accessor<float, 2, access::mode::read_write, access::target::local> B_local(range<2>(N, N), h); |
| 58 | + |
| 59 | + //# Parallel Compute Matrix Multiplication |
| 60 | + h.parallel_for(nd_range<2>{global_size, work_group_size}, [=](nd_item<2> item){ |
| 61 | + const int i = item.get_global_id(0); |
| 62 | + const int j = item.get_global_id(1); |
| 63 | + const int x = item.get_local_id(0); |
| 64 | + const int y = item.get_local_id(1); |
| 65 | + |
| 66 | + //# copy from global to local memory |
| 67 | + A_local[x][y] = A[i * N + j]; |
| 68 | + B_local[x][y] = B[i * N + j]; |
| 69 | + |
| 70 | + //# barrier to sychronize local memory copy across all work items |
| 71 | + group_barrier(item.get_group()); |
| 72 | + |
| 73 | + //# matrix multiplication computation from local memory |
| 74 | + float temp = 0.f; |
| 75 | + for (int k = 0; k < N; k++) { |
| 76 | + temp += A_local[x][k] * B_local[k][y]; |
| 77 | + } |
| 78 | + C[i*N+j] = temp; |
| 79 | + }); |
| 80 | + }); |
| 81 | + host_accessor ha(c, read_only); |
| 82 | + |
| 83 | + //# Print Output and Verification |
| 84 | + auto FAIL = 0; |
| 85 | + for (int i=0; i<N; i++){ |
| 86 | + for (int j=0; j<N; j++){ |
| 87 | + for(int k=0; k<N; k++){ |
| 88 | + matrix_d[i*N+j] += matrix_a[i*N+k] * matrix_b[k*N+j]; |
| 89 | + } |
| 90 | + if(matrix_d[i*N+j] != matrix_c[i*N+j]) FAIL = 1; |
| 91 | + std::cout << std::setw(6) << matrix_c[i*N+j] << " "; |
| 92 | + } |
| 93 | + std::cout << "\n"; |
| 94 | + } |
| 95 | + if(FAIL == 1) std::cout << "FAIL\n"; else std::cout << "PASS\n"; |
| 96 | + |
| 97 | + return 0; |
| 98 | +} |
| 99 | + |
0 commit comments