|
| 1 | +# Copyright 2020, 2021 Intel Corporation |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import dpctl |
| 16 | +import numpy as np |
| 17 | +from numba import float32 |
| 18 | + |
| 19 | +import numba_dpex as dpex |
| 20 | + |
| 21 | + |
| 22 | +def no_arg_barrier_support(): |
| 23 | + """ |
| 24 | + This example demonstrates the usage of numba_dpex's ``barrier`` |
| 25 | + intrinsic function. The ``barrier`` function is usable only inside |
| 26 | + a ``kernel`` and is equivalent to OpenCL's ``barrier`` function. |
| 27 | + """ |
| 28 | + |
| 29 | + @dpex.kernel |
| 30 | + def twice(A): |
| 31 | + i = dpex.get_global_id(0) |
| 32 | + d = A[i] |
| 33 | + # no argument defaults to global mem fence |
| 34 | + dpex.barrier() |
| 35 | + A[i] = d * 2 |
| 36 | + |
| 37 | + N = 10 |
| 38 | + arr = np.arange(N).astype(np.float32) |
| 39 | + print(arr) |
| 40 | + |
| 41 | + # Use the environment variable SYCL_DEVICE_FILTER to change the default device. |
| 42 | + # See https://github.com/intel/llvm/blob/sycl/sycl/doc/EnvironmentVariables.md#sycl_device_filter. |
| 43 | + device = dpctl.select_default_device() |
| 44 | + print("Using device ...") |
| 45 | + device.print_device_info() |
| 46 | + |
| 47 | + with dpctl.device_context(device): |
| 48 | + twice[N, dpex.DEFAULT_LOCAL_SIZE](arr) |
| 49 | + |
| 50 | + # the output should be `arr * 2, i.e. [0, 2, 4, 6, ...]` |
| 51 | + print(arr) |
| 52 | + |
| 53 | + |
| 54 | +def local_memory(): |
| 55 | + """ |
| 56 | + This example demonstrates the usage of numba-dpex's `local.array` |
| 57 | + intrinsic function. The function is used to create a static array |
| 58 | + allocated on the devices local address space. |
| 59 | + """ |
| 60 | + blocksize = 10 |
| 61 | + |
| 62 | + @dpex.kernel |
| 63 | + def reverse_array(A): |
| 64 | + lm = dpex.local.array(shape=10, dtype=float32) |
| 65 | + i = dpex.get_global_id(0) |
| 66 | + |
| 67 | + # preload |
| 68 | + lm[i] = A[i] |
| 69 | + # barrier local or global will both work as we only have one work group |
| 70 | + dpex.barrier(dpex.CLK_LOCAL_MEM_FENCE) # local mem fence |
| 71 | + # write |
| 72 | + A[i] += lm[blocksize - 1 - i] |
| 73 | + |
| 74 | + arr = np.arange(blocksize).astype(np.float32) |
| 75 | + print(arr) |
| 76 | + |
| 77 | + # Use the environment variable SYCL_DEVICE_FILTER to change the default device. |
| 78 | + # See https://github.com/intel/llvm/blob/sycl/sycl/doc/EnvironmentVariables.md#sycl_device_filter. |
| 79 | + device = dpctl.select_default_device() |
| 80 | + print("Using device ...") |
| 81 | + device.print_device_info() |
| 82 | + |
| 83 | + with dpctl.device_context(device): |
| 84 | + reverse_array[blocksize, dpex.DEFAULT_LOCAL_SIZE](arr) |
| 85 | + |
| 86 | + # the output should be `orig[::-1] + orig, i.e. [9, 9, 9, ...]`` |
| 87 | + print(arr) |
| 88 | + |
| 89 | + |
| 90 | +def main(): |
| 91 | + no_arg_barrier_support() |
| 92 | + local_memory() |
| 93 | + |
| 94 | + print("Done...") |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + main() |
| 99 | + |
0 commit comments