Skip to content

Commit a669374

Browse files
GeorgeWebkbenzie
andauthored
[SYCL][HIP] Initial HIP mem_advise implementation. (#10697)
Adds initial implementation for `mem_advise` to the HIP adapter. --------- Co-authored-by: Kenneth Benzie (Benie) <[email protected]>
1 parent 7331d28 commit a669374

File tree

7 files changed

+131
-69
lines changed

7 files changed

+131
-69
lines changed

sycl/include/sycl/detail/pi.h

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,10 @@
146146
// 14.37 Added piextUSMImportExternalPointer and piextUSMReleaseImportedPointer.
147147
// 14.38 Change PI_MEM_ADVICE_* values to flags for use in bitwise operations.
148148
// 14.39 Added PI_EXT_INTEL_DEVICE_INFO_ESIMD_SUPPORT device info query.
149+
// 14.40 Add HIP _pi_mem_advice alises to match the PI_MEM_ADVICE_CUDA* ones.
149150

150151
#define _PI_H_VERSION_MAJOR 14
151-
#define _PI_H_VERSION_MINOR 39
152+
#define _PI_H_VERSION_MINOR 40
152153

153154
#define _PI_STRING_HELPER(a) #a
154155
#define _PI_CONCAT(a, b) _PI_STRING_HELPER(a.b)
@@ -580,6 +581,29 @@ typedef enum {
580581
PI_MEM_ADVICE_UNKNOWN = 0x7FFFFFFF,
581582
} _pi_mem_advice;
582583

584+
// HIP _pi_mem_advice aliases
585+
static constexpr _pi_mem_advice PI_MEM_ADVICE_HIP_SET_READ_MOSTLY =
586+
PI_MEM_ADVICE_CUDA_SET_READ_MOSTLY;
587+
static constexpr _pi_mem_advice PI_MEM_ADVICE_HIP_UNSET_READ_MOSTLY =
588+
PI_MEM_ADVICE_CUDA_UNSET_READ_MOSTLY;
589+
static constexpr _pi_mem_advice PI_MEM_ADVICE_HIP_SET_PREFERRED_LOCATION =
590+
PI_MEM_ADVICE_CUDA_SET_PREFERRED_LOCATION;
591+
static constexpr _pi_mem_advice PI_MEM_ADVICE_HIP_UNSET_PREFERRED_LOCATION =
592+
PI_MEM_ADVICE_CUDA_UNSET_PREFERRED_LOCATION;
593+
static constexpr _pi_mem_advice PI_MEM_ADVICE_HIP_SET_ACCESSED_BY =
594+
PI_MEM_ADVICE_CUDA_SET_ACCESSED_BY;
595+
static constexpr _pi_mem_advice PI_MEM_ADVICE_HIP_UNSET_ACCESSED_BY =
596+
PI_MEM_ADVICE_CUDA_UNSET_ACCESSED_BY;
597+
static constexpr _pi_mem_advice PI_MEM_ADVICE_HIP_SET_PREFERRED_LOCATION_HOST =
598+
PI_MEM_ADVICE_CUDA_SET_PREFERRED_LOCATION_HOST;
599+
static constexpr _pi_mem_advice
600+
PI_MEM_ADVICE_HIP_UNSET_PREFERRED_LOCATION_HOST =
601+
PI_MEM_ADVICE_CUDA_UNSET_PREFERRED_LOCATION_HOST;
602+
static constexpr _pi_mem_advice PI_MEM_ADVICE_HIP_SET_ACCESSED_BY_HOST =
603+
PI_MEM_ADVICE_CUDA_SET_ACCESSED_BY_HOST;
604+
static constexpr _pi_mem_advice PI_MEM_ADVICE_HIP_UNSET_ACCESSED_BY_HOST =
605+
PI_MEM_ADVICE_CUDA_UNSET_ACCESSED_BY_HOST;
606+
583607
typedef enum {
584608
PI_IMAGE_CHANNEL_ORDER_A = 0x10B1,
585609
PI_IMAGE_CHANNEL_ORDER_R = 0x10B0,

sycl/plugins/unified_runtime/CMakeLists.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ if(SYCL_PI_UR_USE_FETCH_CONTENT)
5757
include(FetchContent)
5858

5959
set(UNIFIED_RUNTIME_REPO "https://github.com/oneapi-src/unified-runtime.git")
60-
# commit 2d42d77d5b931b4558842bad32cf9dad80e17b3c
61-
# Merge: 9f14fedb 2a960baf
60+
# commit 12a67f56c3c5d08cfac0852d552b4e5fe0452d04
61+
# Merge: 2b7b827c c10968f5
6262
# Author: Kenneth Benzie (Benie) <[email protected]>
63-
# Date: Mon Jan 8 13:40:40 2024 +0000
64-
# Merge pull request #1229 from oneapi-src/revert-1228-revert-984-ext_oneapi_queue_priority-hip
65-
# Revert "Revert "[HIP] Implement ext_oneapi_queue_priority""
66-
set(UNIFIED_RUNTIME_TAG 2d42d77d5b931b4558842bad32cf9dad80e17b3c)
63+
# Date: Tue Jan 9 10:53:32 2024 +0000
64+
# Merge pull request #1027 from GeorgeWeb/georgi/hip_memadvise
65+
# [SYCL][HIP] Implement mem_advise for HIP
66+
set(UNIFIED_RUNTIME_TAG 12a67f56c3c5d08cfac0852d552b4e5fe0452d04)
6767

6868
if(SYCL_PI_UR_OVERRIDE_FETCH_CONTENT_REPO)
6969
set(UNIFIED_RUNTIME_REPO "${SYCL_PI_UR_OVERRIDE_FETCH_CONTENT_REPO}")

sycl/plugins/unified_runtime/pi2ur.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3358,6 +3358,18 @@ inline pi_result piextUSMEnqueueMemAdvise(pi_queue Queue, const void *Ptr,
33583358
if (Advice & PI_MEM_ADVICE_CUDA_UNSET_PREFERRED_LOCATION) {
33593359
UrAdvice |= UR_USM_ADVICE_FLAG_CLEAR_PREFERRED_LOCATION;
33603360
}
3361+
if (Advice & PI_MEM_ADVICE_CUDA_SET_ACCESSED_BY) {
3362+
UrAdvice |= UR_USM_ADVICE_FLAG_SET_ACCESSED_BY_DEVICE;
3363+
}
3364+
if (Advice & PI_MEM_ADVICE_CUDA_UNSET_ACCESSED_BY) {
3365+
UrAdvice |= UR_USM_ADVICE_FLAG_CLEAR_ACCESSED_BY_DEVICE;
3366+
}
3367+
if (Advice & PI_MEM_ADVICE_CUDA_SET_ACCESSED_BY_HOST) {
3368+
UrAdvice |= UR_USM_ADVICE_FLAG_SET_ACCESSED_BY_HOST;
3369+
}
3370+
if (Advice & PI_MEM_ADVICE_CUDA_UNSET_ACCESSED_BY_HOST) {
3371+
UrAdvice |= UR_USM_ADVICE_FLAG_CLEAR_ACCESSED_BY_HOST;
3372+
}
33613373
if (Advice & PI_MEM_ADVICE_RESET) {
33623374
UrAdvice |= UR_USM_ADVICE_FLAG_DEFAULT;
33633375
}

sycl/test-e2e/DiscardEvents/discard_events_usm.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
// The test checks that the last parameter is `nullptr` for all PI calls that
66
// should discard events.
77
// {{0|0000000000000000}} is required for various output on Linux and Windows.
8+
// NOTE: piextUSMEnqueuePrefetch and piextUSMEnqueueMemAdvise in the CUDA and
9+
// HIP backends may return a warning result on Windows with error-code
10+
// -996 (PI_ERROR_PLUGIN_SPECIFIC_ERROR) if USM managed memory is not
11+
// supported or if unsupported advice flags are used for the latter API.
12+
// Since it is a warning it is safe to ignore for this test.
813
//
914
// Everything that follows TestQueueOperations()
1015
// CHECK: ---> piextUSMEnqueueMemset(
@@ -30,7 +35,7 @@
3035
// CHECK-NEXT: pi_event * : {{0|0000000000000000}}[ nullptr ]
3136
//
3237
// CHECK: ---> piextUSMEnqueueMemAdvise(
33-
// CHECK: ) ---> pi_result : PI_SUCCESS
38+
// CHECK: ) ---> pi_result : {{PI_SUCCESS|-996}}
3439
// CHECK-NEXT: [out]pi_event * : {{0|0000000000000000}}[ nullptr ]
3540
//
3641
// CHECK: ---> piEnqueueKernelLaunch(
@@ -75,7 +80,7 @@
7580
// CHECK-NEXT: pi_event * : {{0|0000000000000000}}[ nullptr ]
7681
//
7782
// CHECK: ---> piextUSMEnqueueMemAdvise(
78-
// CHECK: ) ---> pi_result : PI_SUCCESS
83+
// CHECK: ) ---> pi_result : {{PI_SUCCESS|-996}}
7984
// CHECK-NEXT: [out]pi_event * : {{0|0000000000000000}}[ nullptr ]
8085
//
8186
// CHECK: ---> piEnqueueKernelLaunch(

sycl/test-e2e/DiscardEvents/discard_events_usm_ooo_queue.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
// The test checks that the last parameter is not `nullptr` for all PI calls
66
// that should discard events.
77
// {{0|0000000000000000}} is required for various output on Linux and Windows.
8-
// NOTE: piextUSMEnqueuePrefetch in the CUDA backend may return a warning
9-
// result on Windows with error-code -996
10-
// (PI_ERROR_PLUGIN_SPECIFIC_ERROR). Since it is a warning it is safe to
11-
// ignore for this test.
8+
// NOTE: piextUSMEnqueuePrefetch and piextUSMEnqueueMemAdvise in the CUDA and
9+
// HIP backends may return a warning result on Windows with error-code
10+
// -996 (PI_ERROR_PLUGIN_SPECIFIC_ERROR) if USM managed memory is not
11+
// supported or if unsupported advice flags are used for the latter API.
12+
// Since it is a warning it is safe to ignore for this test.
1213
//
1314
// Everything that follows TestQueueOperations()
1415
// CHECK: ---> piextUSMEnqueueMemset(
@@ -40,7 +41,7 @@
4041
//
4142
// CHECK: ---> piextUSMEnqueueMemAdvise(
4243
// CHECK-NOT: pi_event * : {{0|0000000000000000}}[ nullptr ]
43-
// CHECK: ---> pi_result : PI_SUCCESS
44+
// CHECK: ---> pi_result : {{PI_SUCCESS|-996}}
4445
//
4546
// CHECK: ---> piEnqueueEventsWaitWithBarrier(
4647
// CHECK-NOT: pi_event * : {{0|0000000000000000}}[ nullptr ]
@@ -97,7 +98,7 @@
9798
//
9899
// CHECK: ---> piextUSMEnqueueMemAdvise(
99100
// CHECK-NOT: pi_event * : {{0|0000000000000000}}[ nullptr ]
100-
// CHECK: ---> pi_result : PI_SUCCESS
101+
// CHECK: ---> pi_result : {{PI_SUCCESS|-996}}
101102
//
102103
// CHECK: ---> piEnqueueEventsWaitWithBarrier(
103104
// CHECK-NOT: pi_event * : {{0|0000000000000000}}[ nullptr ]

sycl/test-e2e/USM/memadvise_cuda.cpp

Lines changed: 0 additions & 54 deletions
This file was deleted.

sycl/test-e2e/USM/memadvise_flags.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// RUN: %{build} -o %t1.out
2+
// REQUIRES: cuda || hip_amd
3+
// RUN: %{run} %t1.out
4+
5+
//==---------------- memadvise_flags.cpp -----------------------------------==//
6+
//
7+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
8+
// See https://llvm.org/LICENSE.txt for license information.
9+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include <iostream>
14+
#include <sycl/sycl.hpp>
15+
#include <vector>
16+
17+
using namespace sycl;
18+
19+
int main() {
20+
const size_t size = 100;
21+
queue q;
22+
auto dev = q.get_device();
23+
auto ctx = q.get_context();
24+
if (!dev.get_info<info::device::usm_shared_allocations>()) {
25+
std::cout << "Shared USM is not supported. Skipping test." << std::endl;
26+
return 0;
27+
}
28+
29+
void *ptr = malloc_shared(size, dev, ctx);
30+
if (ptr == nullptr) {
31+
std::cout << "Allocation failed!" << std::endl;
32+
return -1;
33+
}
34+
35+
bool isCuda = dev.get_backend() == sycl::backend::ext_oneapi_cuda;
36+
bool isHip = dev.get_backend() == sycl::backend::ext_oneapi_hip;
37+
38+
std::vector<int> valid_advices;
39+
if (isCuda) {
40+
valid_advices.emplace_back(PI_MEM_ADVICE_CUDA_SET_READ_MOSTLY);
41+
valid_advices.emplace_back(PI_MEM_ADVICE_CUDA_UNSET_READ_MOSTLY);
42+
valid_advices.emplace_back(PI_MEM_ADVICE_CUDA_SET_PREFERRED_LOCATION);
43+
valid_advices.emplace_back(PI_MEM_ADVICE_CUDA_UNSET_PREFERRED_LOCATION);
44+
valid_advices.emplace_back(PI_MEM_ADVICE_CUDA_SET_ACCESSED_BY);
45+
valid_advices.emplace_back(PI_MEM_ADVICE_CUDA_UNSET_ACCESSED_BY);
46+
valid_advices.emplace_back(PI_MEM_ADVICE_CUDA_SET_PREFERRED_LOCATION_HOST);
47+
valid_advices.emplace_back(
48+
PI_MEM_ADVICE_CUDA_UNSET_PREFERRED_LOCATION_HOST);
49+
valid_advices.emplace_back(PI_MEM_ADVICE_CUDA_SET_ACCESSED_BY_HOST);
50+
valid_advices.emplace_back(PI_MEM_ADVICE_CUDA_UNSET_ACCESSED_BY_HOST);
51+
} else if (isHip) {
52+
valid_advices.emplace_back(PI_MEM_ADVICE_HIP_SET_READ_MOSTLY);
53+
valid_advices.emplace_back(PI_MEM_ADVICE_HIP_UNSET_READ_MOSTLY);
54+
valid_advices.emplace_back(PI_MEM_ADVICE_HIP_SET_PREFERRED_LOCATION);
55+
valid_advices.emplace_back(PI_MEM_ADVICE_HIP_UNSET_PREFERRED_LOCATION);
56+
valid_advices.emplace_back(PI_MEM_ADVICE_HIP_SET_ACCESSED_BY);
57+
valid_advices.emplace_back(PI_MEM_ADVICE_HIP_UNSET_ACCESSED_BY);
58+
valid_advices.emplace_back(PI_MEM_ADVICE_HIP_SET_PREFERRED_LOCATION_HOST);
59+
valid_advices.emplace_back(PI_MEM_ADVICE_HIP_UNSET_PREFERRED_LOCATION_HOST);
60+
valid_advices.emplace_back(PI_MEM_ADVICE_HIP_SET_ACCESSED_BY_HOST);
61+
valid_advices.emplace_back(PI_MEM_ADVICE_HIP_UNSET_ACCESSED_BY_HOST);
62+
} else {
63+
// Skip
64+
return 0;
65+
}
66+
67+
for (int advice : valid_advices) {
68+
q.mem_advise(ptr, size, advice);
69+
}
70+
71+
q.wait_and_throw();
72+
std::cout << "Test passed." << std::endl;
73+
return 0;
74+
}

0 commit comments

Comments
 (0)