Skip to content

Commit

Permalink
[SYCL][USM] Fix USM malloc_shared and free to handle zero byte (#1273)
Browse files Browse the repository at this point in the history
Enum variables were too commonly used by users.
This kind of conflicts cannot be avoided 100%, but we can minimize the chance by using the prefix SYCL_

Signed-off-by: Byoungro So <byoungro.so@intel.com>
  • Loading branch information
bso-intel authored Mar 17, 2020
1 parent 20aa83e commit d596593
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
6 changes: 6 additions & 0 deletions sycl/source/detail/usm/usm_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ namespace usm {
void *alignedAllocHost(size_t Alignment, size_t Size, const context &Ctxt,
alloc Kind) {
void *RetVal = nullptr;
if (Size == 0)
return nullptr;
if (Ctxt.is_host()) {
if (!Alignment) {
// worst case default
Expand Down Expand Up @@ -72,6 +74,8 @@ void *alignedAllocHost(size_t Alignment, size_t Size, const context &Ctxt,
void *alignedAlloc(size_t Alignment, size_t Size, const context &Ctxt,
const device &Dev, alloc Kind) {
void *RetVal = nullptr;
if (Size == 0)
return nullptr;
if (Ctxt.is_host()) {
if (Kind == alloc::unknown) {
RetVal = nullptr;
Expand Down Expand Up @@ -126,6 +130,8 @@ void *alignedAlloc(size_t Alignment, size_t Size, const context &Ctxt,
}

void free(void *Ptr, const context &Ctxt) {
if (Ptr == nullptr)
return;
if (Ctxt.is_host()) {
// need to use alignedFree here for Windows
detail::OSUtil::alignedFree(Ptr);
Expand Down
49 changes: 49 additions & 0 deletions sycl/test/regression/usm_malloc_shared.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// RUN: %clangxx -fsycl %s -o %t.out
// RUN: env SYCL_DEVICE_TYPE=HOST %t.out
// RUN: %CPU_RUN_PLACEHOLDER %t.out

// This test checks if users will successfully allocate 160, 0, and -16 bytes of
// shared memory, and also test user can call free() without worrying about
// nullptr or invalid memory descriptor returned from malloc.

#include <CL/sycl.hpp>
#include <iostream>
#include <stdlib.h>
using namespace cl::sycl;

int main(int argc, char *argv[]) {
auto exception_handler = [](cl::sycl::exception_list exceptions) {
for (std::exception_ptr const &e : exceptions) {
try {
std::rethrow_exception(e);
} catch (cl::sycl::exception const &e) {
std::cout << "Caught asynchronous SYCL "
"exception:\n"
<< e.what() << std::endl;
}
}
};

queue myQueue(default_selector{}, exception_handler);
std::cout << "Device: " << myQueue.get_device().get_info<info::device::name>()
<< std::endl;

double *ia = (double *)malloc_shared(160, myQueue);
double *ja = (double *)malloc_shared(0, myQueue);
double *result = (double *)malloc_shared(-16, myQueue);

assert(ia != nullptr);
assert(ja == nullptr);
assert(result == nullptr);

std::cout << "ia : " << ia << " ja: " << ja << " result : " << result
<< std::endl;

// followings should not throw CL_INVALID_VALUE
cl::sycl::free(ia, myQueue);
cl::sycl::free(nullptr, myQueue);
cl::sycl::free(ja, myQueue);
cl::sycl::free(result, myQueue);

return 0;
}

0 comments on commit d596593

Please sign in to comment.