Skip to content

Commit e06af59

Browse files
committed
add sentinel values for enums and remove duplication
1 parent 75030b5 commit e06af59

8 files changed

Lines changed: 258 additions & 158 deletions

File tree

dpctl/_backend.pxd

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ cdef extern from "syclinterface/dpctl_sycl_enum_types.h":
106106

107107
ctypedef enum _partition_affinity_domain_type \
108108
"DPCTLPartitionAffinityDomainType":
109+
_PARTITION_AFFINITY_DOMAIN_UNKNOWN \
110+
"DPCTL_PARTITION_AFFINITY_DOMAIN_UNKNOWN",
109111
_not_applicable "not_applicable",
110112
_numa "numa",
111113
_L4_cache "L4_cache",
@@ -131,17 +133,20 @@ cdef extern from "syclinterface/dpctl_sycl_enum_types.h":
131133
_MEM_CACHE_TYPE_READ_WRITE "DPCTL_MEM_CACHE_TYPE_READ_WRITE"
132134

133135
ctypedef enum _local_mem_type "DPCTLLocalMemType":
136+
_LOCAL_MEM_TYPE_UNKNOWN "DPCTL_LOCAL_MEM_TYPE_UNKNOWN"
134137
_LOCAL_MEM_TYPE_NONE "DPCTL_LOCAL_MEM_TYPE_NONE"
135138
_LOCAL_MEM_TYPE_LOCAL "DPCTL_LOCAL_MEM_TYPE_LOCAL"
136139
_LOCAL_MEM_TYPE_GLOBAL "DPCTL_LOCAL_MEM_TYPE_GLOBAL"
137140

138141
ctypedef enum _partition_property_type "DPCTLPartitionPropertyType":
142+
_PARTITION_UNKNOWN "DPCTL_PARTITION_UNKNOWN"
139143
_PARTITION_NO_PARTITION "DPCTL_PARTITION_NO_PARTITION"
140144
_PARTITION_EQUALLY "DPCTL_PARTITION_EQUALLY"
141145
_PARTITION_BY_COUNTS "DPCTL_PARTITION_BY_COUNTS"
142146
_PARTITION_BY_AFFINITY_DOMAIN "DPCTL_PARTITION_BY_AFFINITY_DOMAIN"
143147

144148
ctypedef enum _fp_config_type "DPCTLFPConfigType":
149+
_FP_UNKNOWN "DPCTL_FP_UNKNOWN"
145150
_FP_DENORM "DPCTL_FP_DENORM"
146151
_FP_INF_NAN "DPCTL_FP_INF_NAN"
147152
_FP_ROUND_TO_NEAREST "DPCTL_FP_ROUND_TO_NEAREST"
@@ -152,13 +157,15 @@ cdef extern from "syclinterface/dpctl_sycl_enum_types.h":
152157
_FP_SOFT_FLOAT "DPCTL_FP_SOFT_FLOAT"
153158

154159
ctypedef enum _memory_order_type "DPCTLMemoryOrderType":
160+
_MEMORY_ORDER_UNKNOWN "DPCTL_MEMORY_ORDER_UNKNOWN"
155161
_MEMORY_ORDER_RELAXED "DPCTL_MEMORY_ORDER_RELAXED"
156162
_MEMORY_ORDER_ACQUIRE "DPCTL_MEMORY_ORDER_ACQUIRE"
157163
_MEMORY_ORDER_RELEASE "DPCTL_MEMORY_ORDER_RELEASE"
158164
_MEMORY_ORDER_ACQ_REL "DPCTL_MEMORY_ORDER_ACQ_REL"
159165
_MEMORY_ORDER_SEQ_CST "DPCTL_MEMORY_ORDER_SEQ_CST"
160166

161167
ctypedef enum _memory_scope_type "DPCTLMemoryScopeType":
168+
_MEMORY_SCOPE_UNKNOWN "DPCTL_MEMORY_SCOPE_UNKNOWN"
162169
_MEMORY_SCOPE_WORK_ITEM "DPCTL_MEMORY_SCOPE_WORK_ITEM"
163170
_MEMORY_SCOPE_SUB_GROUP "DPCTL_MEMORY_SCOPE_SUB_GROUP"
164171
_MEMORY_SCOPE_WORK_GROUP "DPCTL_MEMORY_SCOPE_WORK_GROUP"

dpctl/_sycl_device.pyx

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@ cdef tuple _to_enum_tuple(
214214
):
215215
"""
216216
Converts an array of DPCTL enum values into a tuple of ``enum_type``s
217+
218+
The DPCTL enums reserve value 0 for an unrecognized value, so a DPCTL
219+
value of ``n`` corresponds to the ``n``-th member of ``enum_type``, whose
220+
members are numbered from 1 by ``enum.auto()``.
217221
"""
218222
cdef list res = []
219223
cdef size_t i
@@ -222,9 +226,10 @@ cdef tuple _to_enum_tuple(
222226
return ()
223227
try:
224228
for i in range(arr_len):
225-
if arr[i] < 0:
229+
try:
230+
res.append(enum_type(arr[i]))
231+
except ValueError:
226232
raise RuntimeError(f"Unrecognized {descr} reported")
227-
res.append(enum_type(arr[i] + 1))
228233
finally:
229234
DPCTLInt_Array_Delete(arr)
230235

@@ -2336,6 +2341,10 @@ cdef class SyclDevice(_SyclDevice):
23362341
Returns:
23372342
:class:`dpctl.partition_property`:
23382343
The partition property that was used to create this device.
2344+
2345+
Raises:
2346+
RuntimeError:
2347+
If an unrecognized partition property is reported by runtime.
23392348
"""
23402349
cdef _partition_property_type ppTy = (
23412350
DPCTLDevice_GetPartitionTypeProperty(self._device_ref)
@@ -2348,7 +2357,7 @@ cdef class SyclDevice(_SyclDevice):
23482357
return partition_property.partition_by_counts
23492358
elif ppTy == _partition_property_type._PARTITION_BY_AFFINITY_DOMAIN:
23502359
return partition_property.partition_by_affinity_domain
2351-
return partition_property.no_partition
2360+
raise RuntimeError("Unrecognized partition property reported")
23522361

23532362
@property
23542363
def partition_type_affinity_domain(self):
@@ -2359,6 +2368,11 @@ cdef class SyclDevice(_SyclDevice):
23592368
Returns:
23602369
str:
23612370
The affinity domain string.
2371+
2372+
Raises:
2373+
RuntimeError:
2374+
If an unrecognized partition affinity domain is reported by
2375+
runtime.
23622376
"""
23632377
cdef _partition_affinity_domain_type padTy = (
23642378
DPCTLDevice_GetPartitionTypeAffinityDomain(self._device_ref)
@@ -2377,7 +2391,9 @@ cdef class SyclDevice(_SyclDevice):
23772391
return "L1_cache"
23782392
elif padTy == _partition_affinity_domain_type._next_partitionable:
23792393
return "next_partitionable"
2380-
return "not_applicable"
2394+
raise RuntimeError(
2395+
"Unrecognized partition affinity domain reported"
2396+
)
23812397

23822398
@property
23832399
def half_fp_config(self):
@@ -2560,6 +2576,11 @@ cdef class SyclDevice(_SyclDevice):
25602576
Returns:
25612577
Tuple[str]:
25622578
Tuple of supported affinity domain names.
2579+
2580+
Raises:
2581+
RuntimeError:
2582+
If an unrecognized partition affinity domain is reported by
2583+
runtime.
25632584
"""
25642585
cdef int *arr = NULL
25652586
cdef size_t arr_len = 0
@@ -2580,15 +2601,20 @@ cdef class SyclDevice(_SyclDevice):
25802601
arr = DPCTLDevice_GetPartitionAffinityDomains(
25812602
self._device_ref, &arr_len
25822603
)
2583-
if arr is not NULL and arr_len > 0:
2604+
if arr is NULL:
2605+
return ()
2606+
try:
25842607
res = []
25852608
for i in range(arr_len):
2586-
res.append(_pad_map.get(arr[i], "not_applicable"))
2587-
DPCTLInt_Array_Delete(arr)
2588-
return tuple(res)
2589-
if arr is not NULL:
2609+
if arr[i] not in _pad_map:
2610+
raise RuntimeError(
2611+
"Unrecognized partition affinity domain reported"
2612+
)
2613+
res.append(_pad_map[arr[i]])
2614+
finally:
25902615
DPCTLInt_Array_Delete(arr)
2591-
return ()
2616+
2617+
return tuple(res)
25922618

25932619
cdef cpp_bool equals(self, SyclDevice other):
25942620
""" Returns ``True`` if the :class:`dpctl.SyclDevice` argument has the

libsyclinterface/helper/include/dpctl_utils_helper.h

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,82 @@ DPCTL_DPCTLPartitionAffinityDomainTypeToSycl(
172172
* @param PartitionAffinityDomain sycl::info::partition_affinity_domain to be
173173
* converted to DPCTLPartitionAffinityDomainType enum.
174174
* @return A DPCTLPartitionAffinityDomainType enum value for the input
175-
* sycl::info::partition_affinity_domain enum value.
176-
* @throws runtime_error
175+
* sycl::info::partition_affinity_domain enum value, or
176+
* DPCTL_PARTITION_AFFINITY_DOMAIN_UNKNOWN if the value is not recognized.
177177
*/
178178
DPCTL_API
179179
DPCTLPartitionAffinityDomainType DPCTL_SyclPartitionAffinityDomainToDPCTLType(
180180
sycl::info::partition_affinity_domain PartitionAffinityDomain);
181181

182+
/*!
183+
* @brief Converts a sycl::info::fp_config enum value to corresponding
184+
* DPCTLFPConfigType enum value.
185+
*
186+
* @param FPConfig sycl::info::fp_config to be converted to
187+
* DPCTLFPConfigType enum.
188+
* @return A DPCTLFPConfigType enum value for the input
189+
* sycl::info::fp_config enum value, or DPCTL_FP_UNKNOWN if the value is
190+
* not recognized.
191+
*/
192+
DPCTL_API
193+
DPCTLFPConfigType DPCTL_SyclFPConfigToDPCTLType(sycl::info::fp_config FPConfig);
194+
195+
/*!
196+
* @brief Converts a sycl::info::local_mem_type enum value to corresponding
197+
* DPCTLLocalMemType enum value.
198+
*
199+
* @param LocalMemType sycl::info::local_mem_type to be converted to
200+
* DPCTLLocalMemType enum.
201+
* @return A DPCTLLocalMemType enum value for the input
202+
* sycl::info::local_mem_type enum value, or DPCTL_LOCAL_MEM_TYPE_UNKNOWN if
203+
* the value is not recognized.
204+
*/
205+
DPCTL_API
206+
DPCTLLocalMemType
207+
DPCTL_SyclLocalMemTypeToDPCTLType(sycl::info::local_mem_type LocalMemType);
208+
209+
/*!
210+
* @brief Converts a sycl::memory_order enum value to corresponding
211+
* DPCTLMemoryOrderType enum value.
212+
*
213+
* @param MemoryOrder sycl::memory_order to be converted to
214+
* DPCTLMemoryOrderType enum.
215+
* @return A DPCTLMemoryOrderType enum value for the input
216+
* sycl::memory_order enum value, or DPCTL_MEMORY_ORDER_UNKNOWN if the value
217+
* is not recognized.
218+
*/
219+
DPCTL_API
220+
DPCTLMemoryOrderType
221+
DPCTL_SyclMemoryOrderToDPCTLType(sycl::memory_order MemoryOrder);
222+
223+
/*!
224+
* @brief Converts a sycl::memory_scope enum value to corresponding
225+
* DPCTLMemoryScopeType enum value.
226+
*
227+
* @param MemoryScope sycl::memory_scope to be converted to
228+
* DPCTLMemoryScopeType enum.
229+
* @return A DPCTLMemoryScopeType enum value for the input
230+
* sycl::memory_scope enum value, or DPCTL_MEMORY_SCOPE_UNKNOWN if the value
231+
* is not recognized.
232+
*/
233+
DPCTL_API
234+
DPCTLMemoryScopeType
235+
DPCTL_SyclMemoryScopeToDPCTLType(sycl::memory_scope MemoryScope);
236+
237+
/*!
238+
* @brief Converts a sycl::info::partition_property enum value to corresponding
239+
* DPCTLPartitionPropertyType enum value.
240+
*
241+
* @param PartitionProperty sycl::info::partition_property to be converted
242+
* to DPCTLPartitionPropertyType enum.
243+
* @return A DPCTLPartitionPropertyType enum value for the input
244+
* sycl::info::partition_property enum value, or DPCTL_PARTITION_UNKNOWN if
245+
* the value is not recognized.
246+
*/
247+
DPCTL_API
248+
DPCTLPartitionPropertyType DPCTL_SyclPartitionPropertyToDPCTLType(
249+
sycl::info::partition_property PartitionProperty);
250+
182251
/*!
183252
* @brief Converts a DPCTLPeerAccessType enum value to its corresponding
184253
* sycl::ext::oneapi::peer_access enum value.

libsyclinterface/helper/source/dpctl_utils_helper.cpp

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "dpctl_utils_helper.h"
2727
#include "Config/dpctl_config.h"
2828
#include <sstream>
29+
#include <stdexcept>
2930
#include <string>
3031

3132
using namespace sycl;
@@ -466,7 +467,101 @@ DPCTLPartitionAffinityDomainType DPCTL_SyclPartitionAffinityDomainToDPCTLType(
466467
case info::partition_affinity_domain::next_partitionable:
467468
return DPCTLPartitionAffinityDomainType::next_partitionable;
468469
default:
469-
throw std::runtime_error("Unsupported partition_affinity_domain type");
470+
return DPCTLPartitionAffinityDomainType::
471+
DPCTL_PARTITION_AFFINITY_DOMAIN_UNKNOWN;
472+
}
473+
}
474+
475+
DPCTLFPConfigType DPCTL_SyclFPConfigToDPCTLType(info::fp_config FPConfig)
476+
{
477+
switch (FPConfig) {
478+
case info::fp_config::denorm:
479+
return DPCTLFPConfigType::DPCTL_FP_DENORM;
480+
case info::fp_config::inf_nan:
481+
return DPCTLFPConfigType::DPCTL_FP_INF_NAN;
482+
case info::fp_config::round_to_nearest:
483+
return DPCTLFPConfigType::DPCTL_FP_ROUND_TO_NEAREST;
484+
case info::fp_config::round_to_zero:
485+
return DPCTLFPConfigType::DPCTL_FP_ROUND_TO_ZERO;
486+
case info::fp_config::round_to_inf:
487+
return DPCTLFPConfigType::DPCTL_FP_ROUND_TO_INF;
488+
case info::fp_config::fma:
489+
return DPCTLFPConfigType::DPCTL_FP_FMA;
490+
case info::fp_config::correctly_rounded_divide_sqrt:
491+
return DPCTLFPConfigType::DPCTL_FP_CORRECTLY_ROUNDED_DIVIDE_SQRT;
492+
case info::fp_config::soft_float:
493+
return DPCTLFPConfigType::DPCTL_FP_SOFT_FLOAT;
494+
default:
495+
return DPCTLFPConfigType::DPCTL_FP_UNKNOWN;
496+
}
497+
}
498+
499+
DPCTLLocalMemType
500+
DPCTL_SyclLocalMemTypeToDPCTLType(info::local_mem_type LocalMemType)
501+
{
502+
switch (LocalMemType) {
503+
case info::local_mem_type::none:
504+
return DPCTLLocalMemType::DPCTL_LOCAL_MEM_TYPE_NONE;
505+
case info::local_mem_type::local:
506+
return DPCTLLocalMemType::DPCTL_LOCAL_MEM_TYPE_LOCAL;
507+
case info::local_mem_type::global:
508+
return DPCTLLocalMemType::DPCTL_LOCAL_MEM_TYPE_GLOBAL;
509+
default:
510+
return DPCTLLocalMemType::DPCTL_LOCAL_MEM_TYPE_UNKNOWN;
511+
}
512+
}
513+
514+
DPCTLMemoryOrderType DPCTL_SyclMemoryOrderToDPCTLType(memory_order MemoryOrder)
515+
{
516+
switch (MemoryOrder) {
517+
case memory_order::relaxed:
518+
return DPCTLMemoryOrderType::DPCTL_MEMORY_ORDER_RELAXED;
519+
case memory_order::acquire:
520+
return DPCTLMemoryOrderType::DPCTL_MEMORY_ORDER_ACQUIRE;
521+
case memory_order::release:
522+
return DPCTLMemoryOrderType::DPCTL_MEMORY_ORDER_RELEASE;
523+
case memory_order::acq_rel:
524+
return DPCTLMemoryOrderType::DPCTL_MEMORY_ORDER_ACQ_REL;
525+
case memory_order::seq_cst:
526+
return DPCTLMemoryOrderType::DPCTL_MEMORY_ORDER_SEQ_CST;
527+
default:
528+
return DPCTLMemoryOrderType::DPCTL_MEMORY_ORDER_UNKNOWN;
529+
}
530+
}
531+
532+
DPCTLMemoryScopeType DPCTL_SyclMemoryScopeToDPCTLType(memory_scope MemoryScope)
533+
{
534+
switch (MemoryScope) {
535+
case memory_scope::work_item:
536+
return DPCTLMemoryScopeType::DPCTL_MEMORY_SCOPE_WORK_ITEM;
537+
case memory_scope::sub_group:
538+
return DPCTLMemoryScopeType::DPCTL_MEMORY_SCOPE_SUB_GROUP;
539+
case memory_scope::work_group:
540+
return DPCTLMemoryScopeType::DPCTL_MEMORY_SCOPE_WORK_GROUP;
541+
case memory_scope::device:
542+
return DPCTLMemoryScopeType::DPCTL_MEMORY_SCOPE_DEVICE;
543+
case memory_scope::system:
544+
return DPCTLMemoryScopeType::DPCTL_MEMORY_SCOPE_SYSTEM;
545+
default:
546+
return DPCTLMemoryScopeType::DPCTL_MEMORY_SCOPE_UNKNOWN;
547+
}
548+
}
549+
550+
DPCTLPartitionPropertyType DPCTL_SyclPartitionPropertyToDPCTLType(
551+
info::partition_property PartitionProperty)
552+
{
553+
switch (PartitionProperty) {
554+
case info::partition_property::no_partition:
555+
return DPCTLPartitionPropertyType::DPCTL_PARTITION_NO_PARTITION;
556+
case info::partition_property::partition_equally:
557+
return DPCTLPartitionPropertyType::DPCTL_PARTITION_EQUALLY;
558+
case info::partition_property::partition_by_counts:
559+
return DPCTLPartitionPropertyType::DPCTL_PARTITION_BY_COUNTS;
560+
case info::partition_property::partition_by_affinity_domain:
561+
return DPCTLPartitionPropertyType::DPCTL_PARTITION_BY_AFFINITY_DOMAIN;
562+
default:
563+
// TODO: investigate ext_intel_partition_by_cslice extension
564+
return DPCTLPartitionPropertyType::DPCTL_PARTITION_UNKNOWN;
470565
}
471566
}
472567

libsyclinterface/include/syclinterface/dpctl_sycl_enum_types.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ typedef enum
143143
*/
144144
typedef enum
145145
{
146+
DPCTL_PARTITION_AFFINITY_DOMAIN_UNKNOWN = 0,
146147
not_applicable,
147148
numa,
148149
L4_cache,
@@ -194,13 +195,15 @@ typedef enum
194195

195196
typedef enum
196197
{
198+
DPCTL_LOCAL_MEM_TYPE_UNKNOWN = 0,
197199
DPCTL_LOCAL_MEM_TYPE_NONE,
198200
DPCTL_LOCAL_MEM_TYPE_LOCAL,
199201
DPCTL_LOCAL_MEM_TYPE_GLOBAL
200202
} DPCTLLocalMemType;
201203

202204
typedef enum
203205
{
206+
DPCTL_PARTITION_UNKNOWN = 0,
204207
DPCTL_PARTITION_NO_PARTITION,
205208
DPCTL_PARTITION_EQUALLY,
206209
DPCTL_PARTITION_BY_COUNTS,
@@ -209,6 +212,7 @@ typedef enum
209212

210213
typedef enum
211214
{
215+
DPCTL_FP_UNKNOWN = 0,
212216
DPCTL_FP_DENORM,
213217
DPCTL_FP_INF_NAN,
214218
DPCTL_FP_ROUND_TO_NEAREST,
@@ -221,6 +225,7 @@ typedef enum
221225

222226
typedef enum
223227
{
228+
DPCTL_MEMORY_ORDER_UNKNOWN = 0,
224229
DPCTL_MEMORY_ORDER_RELAXED,
225230
DPCTL_MEMORY_ORDER_ACQUIRE,
226231
DPCTL_MEMORY_ORDER_RELEASE,
@@ -230,6 +235,7 @@ typedef enum
230235

231236
typedef enum
232237
{
238+
DPCTL_MEMORY_SCOPE_UNKNOWN = 0,
233239
DPCTL_MEMORY_SCOPE_WORK_ITEM,
234240
DPCTL_MEMORY_SCOPE_SUB_GROUP,
235241
DPCTL_MEMORY_SCOPE_WORK_GROUP,

0 commit comments

Comments
 (0)