|
| 1 | +# Data Parallel Control (dpctl) |
| 2 | +# |
| 3 | +# Copyright 2020-2022 Intel Corporation |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +# distutils: language = c++ |
| 18 | +# cython: language_level=3 |
| 19 | +# cython: linetrace=True |
| 20 | + |
| 21 | +from libcpp cimport bool as cpp_bool |
| 22 | + |
| 23 | +from dpctl.tensor._usmarray cimport ( |
| 24 | + USM_ARRAY_C_CONTIGUOUS, |
| 25 | + USM_ARRAY_F_CONTIGUOUS, |
| 26 | + USM_ARRAY_WRITEABLE, |
| 27 | + usm_ndarray, |
| 28 | +) |
| 29 | + |
| 30 | + |
| 31 | +cdef cpp_bool _check_bit(int flag, int mask): |
| 32 | + return (flag & mask) == mask |
| 33 | + |
| 34 | + |
| 35 | +cdef class Flags: |
| 36 | + """Helper class to represent flags of :class:`dpctl.tensor.usm_ndarray`.""" |
| 37 | + cdef int flags_ |
| 38 | + cdef usm_ndarray arr_ |
| 39 | + |
| 40 | + def __cinit__(self, usm_ndarray arr, int flags): |
| 41 | + self.arr_ = arr |
| 42 | + self.flags_ = flags |
| 43 | + |
| 44 | + @property |
| 45 | + def flags(self): |
| 46 | + return self.flags_ |
| 47 | + |
| 48 | + @property |
| 49 | + def c_contiguous(self): |
| 50 | + return _check_bit(self.flags_, USM_ARRAY_C_CONTIGUOUS) |
| 51 | + |
| 52 | + @property |
| 53 | + def f_contiguous(self): |
| 54 | + return _check_bit(self.flags_, USM_ARRAY_F_CONTIGUOUS) |
| 55 | + |
| 56 | + @property |
| 57 | + def writable(self): |
| 58 | + return _check_bit(self.flags_, USM_ARRAY_WRITEABLE) |
| 59 | + |
| 60 | + @property |
| 61 | + def fc(self): |
| 62 | + return ( |
| 63 | + _check_bit(self.flags_, USM_ARRAY_C_CONTIGUOUS) |
| 64 | + and _check_bit(self.flags_, USM_ARRAY_F_CONTIGUOUS) |
| 65 | + ) |
| 66 | + |
| 67 | + @property |
| 68 | + def forc(self): |
| 69 | + return ( |
| 70 | + _check_bit(self.flags_, USM_ARRAY_C_CONTIGUOUS) |
| 71 | + or _check_bit(self.flags_, USM_ARRAY_F_CONTIGUOUS) |
| 72 | + ) |
| 73 | + |
| 74 | + @property |
| 75 | + def fnc(self): |
| 76 | + return ( |
| 77 | + _check_bit(self.flags_, USM_ARRAY_C_CONTIGUOUS) |
| 78 | + and not _check_bit(self.flags_, USM_ARRAY_F_CONTIGUOUS) |
| 79 | + ) |
| 80 | + |
| 81 | + @property |
| 82 | + def contiguous(self): |
| 83 | + return self.forc |
| 84 | + |
| 85 | + def __getitem__(self, name): |
| 86 | + if name in ["C_CONTIGUOUS", "C"]: |
| 87 | + return self.c_contiguous |
| 88 | + elif name in ["F_CONTIGUOUS", "F"]: |
| 89 | + return self.f_contiguous |
| 90 | + elif name == "WRITABLE": |
| 91 | + return self.writable |
| 92 | + elif name == "FC": |
| 93 | + return self.fc |
| 94 | + elif name == "CONTIGUOUS": |
| 95 | + return self.forc |
| 96 | + |
| 97 | + def __repr__(self): |
| 98 | + out = [] |
| 99 | + for name in "C_CONTIGUOUS", "F_CONTIGUOUS", "WRITABLE": |
| 100 | + out.append(" {} : {}".format(name, self[name])) |
| 101 | + return '\n'.join(out) |
| 102 | + |
| 103 | + def __eq__(self, other): |
| 104 | + cdef Flags other_ |
| 105 | + if isinstance(other, self.__class__): |
| 106 | + other_ = <Flags>other |
| 107 | + return self.flags_ == other_.flags_ |
| 108 | + elif isinstance(other, int): |
| 109 | + return self.flags_ == <int>other |
| 110 | + else: |
| 111 | + return False |
0 commit comments