Skip to content
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

[SYCLomatic] Add malloc, free, and temporary allocation mappings #1165

Merged

Conversation

mmichel11
Copy link
Contributor

@mmichel11 mmichel11 commented Aug 14, 2023

In this PR, we add the APIs for malloc, free, and temporary buffers (get_temporary_allocation and release_temporary_allocation). A tagged_pointer type is added that contains a template parameter of type host_sys_tag or device_sys_tag specifying the location of the allocation. This type is returned by malloc and get_temporary_allocation. tagged_pointer currently provides basic pointer functionality and will be improved in the future once we get access to Boost iterator adaptors.

In addition to passing execution policies to the memory allocation APIs, passing a system tag type is also supported in this PR. The implemented functionality is only supported for USM mode. Static asserts are hit if DPCT_USM_LEVEL_NONE is set.

There are two PRs that test this functionality:

Copy link
Contributor

@tomflinda tomflinda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pls add test case in https://github.com/oneapi-src/SYCLomatic-test/tree/SYCLomatic/help_function/src for new added helper APIs in this PR

class tagged_pointer_base {
public:
using pointer = T *;
using difference_type = ::std::make_signed_t<std::size_t>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about use ptrdiff_t?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have switched to using this.

public:
using pointer = T *;
using difference_type = ::std::make_signed_t<std::size_t>;
tagged_pointer_base() = default;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m_ptr is not initialized.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

tagged_pointer() : super_t() {}
tagged_pointer(T *ptr) : super_t(ptr) {}
T &operator[](difference_type idx) { return this->m_ptr[idx]; }
T &operator[](difference_type idx) const { return this->m_ptr[idx]; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should return const T & here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I have made this change.

tagged_pointer &operator-=(difference_type backward) {
this->m_ptr = this->m_ptr - backward;
return *this;
}
Copy link
Contributor

@ziranzha ziranzha Aug 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

operator* and operator-> and pointer type conversion are also needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added this, so we are not relying on conversion to pointer. I also updated the test to check operator* and operator->.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need some type conversion like:

  template<class OtherT>
  operator tagged_pointer<Tag, OtherT>();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this operator is necessary since we already provide a raw pointer conversion to otherT which can then call the tagged_pointer<Tag, OtherT> constructor. This functionality is tested in onedpl_test_tagged_pointer in the test PR.

We also don't provide this operator in similar classes (ex: device_pointer).

oneapi::dpl::execution::is_execution_policy_v<decayed_policy_or_tag_t>;
static constexpr bool is_sys_tag_v =
::std::is_base_of_v<sys_tag, std::decay_t<decayed_policy_or_tag_t>>;
static_assert(is_policy_v || is_sys_tag_v,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is user-defined tag that deriving sys_tag also permitted?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we do not currently support any custom execution policies in oneDPL, I think we should not currently allow user-defined system tags. I have put a stricter check in here to ensure this.


// TODO: Improve this pointer wrapper and make an iterator adaptor
// once we have the Boost library dependency
template <typename Tag, typename T, typename Derived>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we need this base class?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This base class just contains the shared functions / members between the standard tagged_pointer case and the specialization for void pointers. The CRTP template parameter is currently unused. I have removed it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The base class just provide two simple methods:

  operator const T *() const { return m_ptr; }
  operator T *() { return m_ptr; }

I think we can remove it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have made this change.

Signed-off-by: Matthew Michel <[email protected]>
@mmichel11
Copy link
Contributor Author

@tomflinda I have added tests for the system tag internal helper APIs in: https://github.com/oneapi-src/SYCLomatic-test/pull/427/files and a test for the malloc_base helper API in oneapi-src/SYCLomatic-test#426

Copy link
Contributor

@danhoeflinger danhoeflinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, it would be good to get and approval from @ziranzha before merging.

Copy link
Contributor

@tomflinda tomflinda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@zhimingwang36 zhimingwang36 merged commit d2a91f4 into oneapi-src:SYCLomatic Aug 29, 2023
ShengchenJ pushed a commit to ShengchenJ/SYCLomatic that referenced this pull request Sep 27, 2024
ShengchenJ pushed a commit to ShengchenJ/SYCLomatic that referenced this pull request Sep 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants