|
| 1 | +# Copyright (c) 2018, Intel Corporation |
| 2 | +# |
| 3 | +# Redistribution and use in source and binary forms, with or without |
| 4 | +# modification, are permitted provided that the following conditions are met: |
| 5 | +# |
| 6 | +# * Redistributions of source code must retain the above copyright notice, |
| 7 | +# this list of conditions and the following disclaimer. |
| 8 | +# * Redistributions in binary form must reproduce the above copyright |
| 9 | +# notice, this list of conditions and the following disclaimer in the |
| 10 | +# documentation and/or other materials provided with the distribution. |
| 11 | +# * Neither the name of Intel Corporation nor the names of its contributors |
| 12 | +# may be used to endorse or promote products derived from this software |
| 13 | +# without specific prior written permission. |
| 14 | +# |
| 15 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 16 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 18 | +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE |
| 19 | +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 20 | +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 21 | +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 22 | +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 23 | +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | + |
| 26 | +import sys |
| 27 | + |
| 28 | +import mkl |
| 29 | + |
| 30 | + |
| 31 | +def test_mkl_memory_create_malloc(): |
| 32 | + nbytes = 1024 |
| 33 | + mem = mkl.MKLMemory(nbytes) |
| 34 | + assert mem.nbytes == nbytes |
| 35 | + # default alignment is 64 bytes |
| 36 | + assert mem.alignment == 64 |
| 37 | + |
| 38 | + |
| 39 | +def test_mkl_memory_create_calloc(): |
| 40 | + size = 32 |
| 41 | + num = 32 |
| 42 | + nbytes = num * size |
| 43 | + # test creating with mkl_calloc |
| 44 | + mem = mkl.MKLMemory(num, size) |
| 45 | + assert mem.nbytes == nbytes |
| 46 | + # default alignment is 64 bytes |
| 47 | + assert mem.alignment == 64 |
| 48 | + |
| 49 | + |
| 50 | +def test_mkl_memory_create_with_malloc_and_alignment(): |
| 51 | + size = 32 |
| 52 | + num = 32 |
| 53 | + nbytes = num * size |
| 54 | + alignment = 128 |
| 55 | + mem = mkl.MKLMemory(nbytes, alignment=alignment) |
| 56 | + assert mem.nbytes == nbytes |
| 57 | + assert mem.alignment == alignment |
| 58 | + |
| 59 | + |
| 60 | +def test_mkl_memory_create_with_calloc_and_alignment(): |
| 61 | + size = 32 |
| 62 | + num = 32 |
| 63 | + nbytes = num * size |
| 64 | + alignment = 128 |
| 65 | + mem = mkl.MKLMemory(num, size, alignment=alignment) |
| 66 | + assert mem.nbytes == nbytes |
| 67 | + |
| 68 | + |
| 69 | +def test_mkl_memory_create_from_mkl_memory(): |
| 70 | + mem1 = mkl.MKLMemory(1024) |
| 71 | + mem2 = mkl.MKLMemory(mem1) |
| 72 | + assert mem2.nbytes == mem1.nbytes |
| 73 | + |
| 74 | + |
| 75 | +def test_mkl_memory_create_from_mkl_memory_with_alignment(): |
| 76 | + mem1 = mkl.MKLMemory(1024) |
| 77 | + alignment = 128 |
| 78 | + mem2 = mkl.MKLMemory(mem1, alignment=alignment) |
| 79 | + assert mem2.nbytes == mem1.nbytes |
| 80 | + assert mem2.alignment == alignment |
| 81 | + |
| 82 | + |
| 83 | +def test_mkl_memory_propagates_alignment(): |
| 84 | + mem1 = mkl.MKLMemory(1024, alignment=128) |
| 85 | + mem2 = mkl.MKLMemory(mem1) |
| 86 | + assert mem2.nbytes == mem1.nbytes |
| 87 | + assert mem2.alignment == mem1.alignment |
| 88 | + |
| 89 | + |
| 90 | +def test_mkl_memory_properties(): |
| 91 | + nbytes = 1024 |
| 92 | + mem = mkl.MKLMemory(nbytes) |
| 93 | + assert len(mem) == nbytes |
| 94 | + assert type(repr(mem)) is str |
| 95 | + assert type(bytes(mem)) is bytes |
| 96 | + assert sys.getsizeof(mem) >= nbytes |
| 97 | + |
| 98 | + |
| 99 | +def test_buffer_protocol(): |
| 100 | + mem = mkl.MKLMemory(1024) |
| 101 | + mv1 = memoryview(mem) |
| 102 | + assert mv1.nbytes == mem.nbytes |
| 103 | + mv2 = memoryview(mem) |
| 104 | + assert mv1 == mv2 |
| 105 | + |
| 106 | + |
| 107 | +def test_pickling(): |
| 108 | + import pickle |
| 109 | + |
| 110 | + mem = mkl.MKLMemory(1024) |
| 111 | + mv = memoryview(mem) |
| 112 | + for i in range(len(mem)): |
| 113 | + mv[i] = (i % 32) + ord("a") |
| 114 | + |
| 115 | + mem_reconstructed = pickle.loads(pickle.dumps(mem)) |
| 116 | + assert type(mem) is type(mem_reconstructed), "Pickling should preserve type" |
| 117 | + assert ( |
| 118 | + mem.tobytes() == mem_reconstructed.tobytes() |
| 119 | + ), "Pickling should preserve buffer content" |
| 120 | + assert ( |
| 121 | + mem._pointer != mem_reconstructed._pointer |
| 122 | + ), "Pickling/unpickling should be changing pointer" |
| 123 | + |
| 124 | + |
| 125 | +def test_pickling_with_alignment(): |
| 126 | + import pickle |
| 127 | + |
| 128 | + mem = mkl.MKLMemory(1024, alignment=128) |
| 129 | + mem_reconstructed = pickle.loads(pickle.dumps(mem)) |
| 130 | + assert type(mem) is type(mem_reconstructed), "Pickling should preserve type" |
| 131 | + assert ( |
| 132 | + mem.tobytes() == mem_reconstructed.tobytes() |
| 133 | + ), "Pickling should preserve buffer content" |
| 134 | + assert ( |
| 135 | + mem._pointer != mem_reconstructed._pointer |
| 136 | + ), "Pickling/unpickling should be changing pointer" |
| 137 | + assert ( |
| 138 | + mem.alignment == mem_reconstructed.alignment |
| 139 | + ), "Pickling should preserve alignment" |
0 commit comments