-
Notifications
You must be signed in to change notification settings - Fork 797
[SYCL] dot_product support. #2609
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e90c0e1
[SYCL] dot_product support.
rdeodhar 8b8d40a
[SYCL] dot_product support.
rdeodhar 5dadfa9
Merge branch 'idot1' of https://github.com/rdeodhar/llvm into idot1
rdeodhar 1b8104d
Merge branch 'idot1' of https://github.com/rdeodhar/llvm into idot1
rdeodhar 81997a3
Merge branch 'idot1' of https://github.com/rdeodhar/llvm into idot1
rdeodhar 2385d2a
[SYCL] Changed dot_product from INTEL namespace to ONEAPI.
rdeodhar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
//==----------- dot_product.hpp ------- SYCL dot-product -------------------==// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// DP4A extension | ||
|
||
#pragma once | ||
|
||
__SYCL_INLINE_NAMESPACE(cl) { | ||
namespace sycl { | ||
namespace intel { | ||
vladimirlaz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
union Us { | ||
char s[4]; | ||
int i; | ||
}; | ||
union Uu { | ||
unsigned char s[4]; | ||
int i; | ||
}; | ||
|
||
int dot_acc(int pa, int pb, int c) { | ||
Us a = *(reinterpret_cast<Us *>(&pa)); | ||
Us b = *(reinterpret_cast<Us *>(&pb)); | ||
return a.s[0] * b.s[0] + a.s[1] * b.s[1] + a.s[2] * b.s[2] + a.s[3] * b.s[3] + | ||
c; | ||
} | ||
|
||
int dot_acc(unsigned int pa, unsigned int pb, int c) { | ||
Uu a = *(reinterpret_cast<Uu *>(&pa)); | ||
Uu b = *(reinterpret_cast<Uu *>(&pb)); | ||
return a.s[0] * b.s[0] + a.s[1] * b.s[1] + a.s[2] * b.s[2] + a.s[3] * b.s[3] + | ||
c; | ||
} | ||
|
||
int dot_acc(int pa, unsigned int pb, int c) { | ||
Us a = *(reinterpret_cast<Us *>(&pa)); | ||
Uu b = *(reinterpret_cast<Uu *>(&pb)); | ||
return a.s[0] * b.s[0] + a.s[1] * b.s[1] + a.s[2] * b.s[2] + a.s[3] * b.s[3] + | ||
c; | ||
} | ||
|
||
int dot_acc(unsigned int pa, int pb, int c) { | ||
Uu a = *(reinterpret_cast<Uu *>(&pa)); | ||
Us b = *(reinterpret_cast<Us *>(&pb)); | ||
return a.s[0] * b.s[0] + a.s[1] * b.s[1] + a.s[2] * b.s[2] + a.s[3] * b.s[3] + | ||
c; | ||
} | ||
|
||
int dot_acc(vec<int8_t, 4> a, vec<int8_t, 4> b, int32_t c) { | ||
return a.s0() * b.s0() + a.s1() * b.s1() + a.s2() * b.s2() + a.s3() * b.s3() + | ||
c; | ||
} | ||
|
||
int dot_acc(vec<uint8_t, 4> a, vec<uint8_t, 4> b, int32_t c) { | ||
return a.s0() * b.s0() + a.s1() * b.s1() + a.s2() * b.s2() + a.s3() * b.s3() + | ||
c; | ||
} | ||
|
||
int dot_acc(vec<uint8_t, 4> a, vec<int8_t, 4> b, int32_t c) { | ||
return a.s0() * b.s0() + a.s1() * b.s1() + a.s2() * b.s2() + a.s3() * b.s3() + | ||
c; | ||
} | ||
|
||
int dot_acc(vec<int8_t, 4> a, vec<uint8_t, 4> b, int32_t c) { | ||
return a.s0() * b.s0() + a.s1() * b.s1() + a.s2() * b.s2() + a.s3() * b.s3() + | ||
c; | ||
} | ||
|
||
} // namespace intel | ||
} // namespace sycl | ||
} // __SYCL_INLINE_NAMESPACE(cl) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,248 @@ | ||
// This test checks dp4a support | ||
// For now we only check fallback support because DG1 hardware is not widespread | ||
|
||
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out | ||
// RUN: env SYCL_DEVICE_TYPE=HOST %t.out | ||
// RUN: %CPU_RUN_PLACEHOLDER %t.out | ||
// RUN: %GPU_RUN_PLACEHOLDER %t.out | ||
// RUN: %ACC_RUN_PLACEHOLDER %t.out | ||
|
||
#include <CL/sycl.hpp> | ||
#include <CL/sycl/INTEL/dot_product.hpp> | ||
#include <iostream> | ||
#include <memory> | ||
#include <stdio.h> | ||
|
||
// Change if tests are added/removed | ||
static int testCount = 4; | ||
static int passCount; | ||
|
||
using namespace cl::sycl; | ||
using namespace cl::sycl::detail::gtl; | ||
using namespace cl::sycl::intel; | ||
|
||
constexpr int RangeLength = 100; | ||
|
||
// Verify 1D array | ||
template <typename T> | ||
static bool verify_1D(const char *name, int X, T *A, T *A_ref) { | ||
int ErrCnt = 0; | ||
|
||
for (int i = 0; i < X; i++) { | ||
if (A_ref[i] != A[i]) { | ||
if (++ErrCnt < 10) { | ||
std::cout << name << " mismatch at " << i << ". Expected " << A_ref[i] | ||
<< " result is " << A[i] << "\n"; | ||
} | ||
} | ||
} | ||
|
||
if (ErrCnt == 0) { | ||
return true; | ||
} | ||
std::cout << " Failed. Failure rate: " << ErrCnt << "/" << X << "(" | ||
<< ErrCnt / (float)X * 100.f << "%)\n"; | ||
return false; | ||
} | ||
|
||
static bool testss(queue &Q) { | ||
int A[RangeLength]; | ||
int B[RangeLength]; | ||
int C[RangeLength]; | ||
int D[RangeLength]; | ||
int D_ref[RangeLength]; | ||
|
||
std::memset(D, 0, RangeLength * sizeof(int)); | ||
std::memset(D_ref, 0, RangeLength * sizeof(int)); | ||
|
||
for (int i = 0; i < RangeLength; i++) { | ||
A[i] = i | (i << 8) | (i << 16) | (i << 24); | ||
B[i] = 0xFFFFFFFF; | ||
C[i] = i; | ||
} | ||
for (int i = 0; i < RangeLength; i++) { | ||
D_ref[i] = (i * -1) + (i * -1) + (i * -1) + (i * -1) + C[i]; | ||
} | ||
|
||
buffer<int, 1> Abuf(A, range<1>(RangeLength)); | ||
buffer<int, 1> Bbuf(B, range<1>(RangeLength)); | ||
buffer<int, 1> Cbuf(C, range<1>(RangeLength)); | ||
buffer<int, 1> Dbuf(D, range<1>(RangeLength)); | ||
|
||
Q.submit([&](handler &cgh) { | ||
auto Ap = Abuf.get_access<access::mode::read>(cgh); | ||
auto Bp = Bbuf.get_access<access::mode::read>(cgh); | ||
auto Cp = Cbuf.get_access<access::mode::read>(cgh); | ||
auto Dp = Dbuf.get_access<access::mode::write>(cgh); | ||
|
||
cgh.parallel_for<class tss>(range<1>(RangeLength), [=](id<1> I) { | ||
Dp[I] = dot_acc(Ap[I], Bp[I], Cp[I]); | ||
}); | ||
}); | ||
const auto HAcc = Dbuf.get_access<cl::sycl::access::mode::read>(); | ||
|
||
return verify_1D("testss D", RangeLength, D, D_ref); | ||
} | ||
|
||
static bool testuu(queue &Q) { | ||
unsigned int A[RangeLength]; | ||
unsigned int B[RangeLength]; | ||
int C[RangeLength]; | ||
int D[RangeLength]; | ||
int D_ref[RangeLength]; | ||
|
||
std::memset(D, 0, RangeLength * sizeof(int)); | ||
std::memset(D_ref, 0, RangeLength * sizeof(int)); | ||
|
||
for (int i = 0; i < RangeLength; i++) { | ||
A[i] = i | (i << 8) | (i << 16) | (i << 24); | ||
B[i] = 0xFFFFFFFF; | ||
C[i] = i; | ||
} | ||
for (int i = 0; i < RangeLength; i++) { | ||
D_ref[i] = (i * 255) + (i * 255) + (i * 255) + (i * 255) + C[i]; | ||
} | ||
|
||
buffer<unsigned int, 1> Abuf(A, range<1>(RangeLength)); | ||
buffer<unsigned int, 1> Bbuf(B, range<1>(RangeLength)); | ||
buffer<int, 1> Cbuf(C, range<1>(RangeLength)); | ||
buffer<int, 1> Dbuf(D, range<1>(RangeLength)); | ||
|
||
Q.submit([&](handler &cgh) { | ||
auto Ap = Abuf.get_access<access::mode::read>(cgh); | ||
auto Bp = Bbuf.get_access<access::mode::read>(cgh); | ||
auto Cp = Cbuf.get_access<access::mode::read>(cgh); | ||
auto Dp = Dbuf.get_access<access::mode::write>(cgh); | ||
|
||
cgh.parallel_for<class tuu>(range<1>(RangeLength), [=](id<1> I) { | ||
Dp[I] = dot_acc(Ap[I], Bp[I], Cp[I]); | ||
}); | ||
}); | ||
const auto HAcc = Dbuf.get_access<cl::sycl::access::mode::read>(); | ||
|
||
return verify_1D("testuu D", RangeLength, D, D_ref); | ||
} | ||
|
||
static bool testsu(queue &Q) { | ||
int A[RangeLength]; | ||
unsigned int B[RangeLength]; | ||
int C[RangeLength]; | ||
int D[RangeLength]; | ||
int D_ref[RangeLength]; | ||
|
||
std::memset(D, 0, RangeLength * sizeof(int)); | ||
std::memset(D_ref, 0, RangeLength * sizeof(int)); | ||
|
||
for (int i = 0; i < RangeLength; i++) { | ||
A[i] = 0xFFFFFFFF; | ||
B[i] = i | (i << 8) | (i << 16) | (i << 24); | ||
C[i] = i; | ||
} | ||
for (int i = 0; i < RangeLength; i++) { | ||
D_ref[i] = (i * -1) + (i * -1) + (i * -1) + (i * -1) + C[i]; | ||
} | ||
|
||
buffer<int, 1> Abuf(A, range<1>(RangeLength)); | ||
buffer<unsigned int, 1> Bbuf(B, range<1>(RangeLength)); | ||
buffer<int, 1> Cbuf(C, range<1>(RangeLength)); | ||
buffer<int, 1> Dbuf(D, range<1>(RangeLength)); | ||
|
||
Q.submit([&](handler &cgh) { | ||
auto Ap = Abuf.get_access<access::mode::read>(cgh); | ||
auto Bp = Bbuf.get_access<access::mode::read>(cgh); | ||
auto Cp = Cbuf.get_access<access::mode::read>(cgh); | ||
auto Dp = Dbuf.get_access<access::mode::write>(cgh); | ||
|
||
cgh.parallel_for<class tsu>(range<1>(RangeLength), [=](id<1> I) { | ||
Dp[I] = dot_acc(Ap[I], Bp[I], Cp[I]); | ||
}); | ||
}); | ||
const auto HAcc = Dbuf.get_access<cl::sycl::access::mode::read>(); | ||
|
||
return verify_1D("testsu D", RangeLength, D, D_ref); | ||
} | ||
|
||
static bool testus(queue &Q) { | ||
unsigned int A[RangeLength]; | ||
int B[RangeLength]; | ||
int C[RangeLength]; | ||
int D[RangeLength]; | ||
int D_ref[RangeLength]; | ||
|
||
std::memset(D, 0, RangeLength * sizeof(int)); | ||
std::memset(D_ref, 0, RangeLength * sizeof(int)); | ||
|
||
for (int i = 0; i < RangeLength; i++) { | ||
A[i] = i | (i << 8) | (i << 16) | (i << 24); | ||
B[i] = 0xFFFFFFFF; | ||
C[i] = i; | ||
} | ||
for (int i = 0; i < RangeLength; i++) { | ||
D_ref[i] = (i * -1) + (i * -1) + (i * -1) + (i * -1) + C[i]; | ||
} | ||
|
||
buffer<unsigned int, 1> Abuf(A, range<1>(RangeLength)); | ||
buffer<int, 1> Bbuf(B, range<1>(RangeLength)); | ||
buffer<int, 1> Cbuf(C, range<1>(RangeLength)); | ||
buffer<int, 1> Dbuf(D, range<1>(RangeLength)); | ||
|
||
Q.submit([&](handler &cgh) { | ||
auto Ap = Abuf.get_access<access::mode::read>(cgh); | ||
auto Bp = Bbuf.get_access<access::mode::read>(cgh); | ||
auto Cp = Cbuf.get_access<access::mode::read>(cgh); | ||
auto Dp = Dbuf.get_access<access::mode::write>(cgh); | ||
|
||
cgh.parallel_for<class tus>(range<1>(RangeLength), [=](id<1> I) { | ||
Dp[I] = dot_acc(Ap[I], Bp[I], Cp[I]); | ||
}); | ||
}); | ||
const auto HAcc = Dbuf.get_access<cl::sycl::access::mode::read>(); | ||
|
||
return verify_1D("testus D", RangeLength, D, D_ref); | ||
} | ||
|
||
bool run_tests() { | ||
queue Q([](exception_list L) { | ||
for (auto ep : L) { | ||
std::rethrow_exception(ep); | ||
} | ||
}); | ||
|
||
passCount = 0; | ||
if (testss(Q)) { | ||
++passCount; | ||
} | ||
if (testuu(Q)) { | ||
++passCount; | ||
} | ||
if (testsu(Q)) { | ||
++passCount; | ||
} | ||
if (testus(Q)) { | ||
++passCount; | ||
} | ||
|
||
auto D = Q.get_device(); | ||
const char *devType = D.is_host() ? "Host" : D.is_cpu() ? "CPU" : "GPU"; | ||
std::cout << passCount << " of " << testCount << " tests passed on " | ||
<< devType << "\n"; | ||
|
||
return (testCount == passCount); | ||
} | ||
|
||
int main(int argc, char *argv[]) { | ||
bool passed = true; | ||
default_selector selector{}; | ||
auto D = selector.select_device(); | ||
const char *devType = D.is_host() ? "Host" : D.is_cpu() ? "CPU" : "GPU"; | ||
std::cout << "Running on device " << devType << " (" | ||
<< D.get_info<cl::sycl::info::device::name>() << ")\n"; | ||
passed &= run_tests(); | ||
|
||
if (!passed) { | ||
std::cout << "FAILED\n"; | ||
return 1; | ||
} | ||
std::cout << "PASSED\n"; | ||
return 0; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.