├── .gitignore ├── LICENSE.rst ├── README.rst ├── pymagma ├── __init__.py ├── cuda.py ├── cudadrv.py ├── cudart.py └── magma.py └── setup.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | 5 | # C extensions 6 | *.so 7 | 8 | # Distribution / packaging 9 | bin/ 10 | build/ 11 | develop-eggs/ 12 | dist/ 13 | eggs/ 14 | lib/ 15 | lib64/ 16 | parts/ 17 | sdist/ 18 | var/ 19 | *.egg-info/ 20 | .installed.cfg 21 | *.egg 22 | 23 | # Installer logs 24 | pip-log.txt 25 | pip-delete-this-directory.txt 26 | 27 | # Unit test / coverage reports 28 | .tox/ 29 | .coverage 30 | .cache 31 | 32 | # Translations 33 | *.mo 34 | 35 | # Mr Developer 36 | .mr.developer.cfg 37 | .project 38 | .pydevproject 39 | 40 | # Django stuff: 41 | *.log 42 | *.pot 43 | 44 | # Sphinx documentation 45 | docs/_build/ 46 | 47 | # Examples directory 48 | examples/ 49 | 50 | # Backup files 51 | *~ 52 | #*# -------------------------------------------------------------------------------- /LICENSE.rst: -------------------------------------------------------------------------------- 1 | .. -*- rst -*- 2 | 3 | License 4 | ======= 5 | Copyright (c) 2016, Carlo Holly. 6 | All rights reserved. 7 | 8 | Redistribution and use in source and binary forms, with or without 9 | modification, are permitted provided that the following conditions are 10 | met: 11 | 12 | * Redistributions of source code must retain the above copyright 13 | notice, this list of conditions and the following disclaimer. 14 | * Redistributions in binary form must reproduce the above 15 | copyright notice, this list of conditions and the following 16 | disclaimer in the documentation and/or other materials provided 17 | with the distribution. 18 | * Neither the name of Carlo Holly nor the names of any 19 | contributors may be used to endorse or promote products derived 20 | from this software without specific prior written permission. 21 | 22 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 25 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | .. -*- rst -*- 2 | 3 | pymagma 4 | _______ 5 | 6 | A Python interface to the MAGMA libraries. 7 | This package is developed to provide an python interface to GPU accelerated matrix and vector operations - especially the sparse-iter functions from MAGMA. It provides easy access to iterative sparse solvers on the GPU. Therefore, two classes magma_matrix and magma_vector are added. Objects of these classes can be created with a scipy sparse matrix or numpy array, respectively and the class can be used to automatically transfer the magma matrix to GPU memory space and internally call functions corresponding to the present data type (z, c, d or s). The package currently supports MAGMA-2.0.2. It has not been tested for other versions of the MAGMA libraries. Parts of the code are taken from `scikit-cuda `_ written by `Lev Givon `_. 8 | 9 | The package is written and maintained by `Carlo Holly `_ for my research at RWTH Aachen University at the `Chair for Laser Technology `_. 10 | 11 | Prerequisites 12 | _____________ 13 | 14 | The MAGMA-2.0.2 libraries (libmagma.so and libmagma_sparse.so on a Linux system) have to be installed on your system. To download and install the libraries visit `this site `_. 15 | 16 | Installation 17 | ____________ 18 | 19 | To install pymagma use... 20 | 21 | Contact 22 | _______ 23 | 24 | This project is maintained by Carlo Holly. Please contact me via carlo.holly@rwth-aachen.de. 25 | 26 | Citing 27 | ______ 28 | 29 | If you make use of pymagma in your research, please cite it. The BibTeX reference is 30 | 31 | @article{Holly_pymagma, 32 | author = {Carlo Holly and Lev E. Givon}, 33 | title = {pymagma 0.0.1 - a {Python} interface to the {MAGMA} libraries}, 34 | month = August, 35 | year = 2016, 36 | doi = "", 37 | url = "", 38 | } 39 | 40 | License 41 | _______ 42 | 43 | This software is licensed under the `BSD License `_. 44 | See the included `LICENSE`_ file for more information. 45 | 46 | .. _LICENSE: LICENSE.rst 47 | -------------------------------------------------------------------------------- /pymagma/__init__.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2016, Carlo Holly. 3 | # All rights reserved. 4 | # 5 | 6 | #!/usr/bin/env python 7 | 8 | __version_info__ = ('0', '0', '1') 9 | __version__ = '.'.join(__version_info__) 10 | 11 | from cuda import * 12 | from magma import * -------------------------------------------------------------------------------- /pymagma/cuda.py: -------------------------------------------------------------------------------- 1 | # This file is taken from scikits.cuda (https://github.com/lebedov/scikit-cuda) 2 | # 3 | # Copyright (c) 2009-2015, Lev Givon. 4 | # All rights reserved. 5 | # 6 | # Redistribution and use in source and binary forms, with or without 7 | # modification, are permitted provided that the following conditions are 8 | # met: 9 | # 10 | # * Redistributions of source code must retain the above copyright 11 | # notice, this list of conditions and the following disclaimer. 12 | # * Redistributions in binary form must reproduce the above 13 | # copyright notice, this list of conditions and the following 14 | # disclaimer in the documentation and/or other materials provided 15 | # with the distribution. 16 | # * Neither the name of Lev Givon nor the names of any 17 | # contributors may be used to endorse or promote products derived 18 | # from this software without specific prior written permission. 19 | # 20 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | #!/usr/bin/env python 33 | 34 | """ 35 | Python interface to CUDA functions. 36 | """ 37 | 38 | from __future__ import absolute_import 39 | 40 | from .cudart import * 41 | from .cudadrv import * -------------------------------------------------------------------------------- /pymagma/cudadrv.py: -------------------------------------------------------------------------------- 1 | # This file is taken from scikits.cuda (https://github.com/lebedov/scikit-cuda) 2 | # 3 | # Copyright (c) 2009-2015, Lev Givon. 4 | # All rights reserved. 5 | # 6 | # Redistribution and use in source and binary forms, with or without 7 | # modification, are permitted provided that the following conditions are 8 | # met: 9 | # 10 | # * Redistributions of source code must retain the above copyright 11 | # notice, this list of conditions and the following disclaimer. 12 | # * Redistributions in binary form must reproduce the above 13 | # copyright notice, this list of conditions and the following 14 | # disclaimer in the documentation and/or other materials provided 15 | # with the distribution. 16 | # * Neither the name of Lev Givon nor the names of any 17 | # contributors may be used to endorse or promote products derived 18 | # from this software without specific prior written permission. 19 | # 20 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | #!/usr/bin/env python 33 | 34 | """ 35 | Python interface to CUDA driver functions. 36 | """ 37 | 38 | import sys, ctypes 39 | 40 | # Load library: 41 | if 'linux' in sys.platform: 42 | _libcuda_libname_list = ['libcuda.so'] 43 | elif sys.platform == 'darwin': 44 | _libcuda_libname_list = ['libcuda.dylib'] 45 | elif sys.platform == 'win32': 46 | _libcuda_libname_list = ['cuda.dll', 'nvcuda.dll'] 47 | else: 48 | raise RuntimeError('unsupported platform') 49 | 50 | # Print understandable error message when library cannot be found: 51 | _libcuda = None 52 | for _libcuda_libname in _libcuda_libname_list: 53 | try: 54 | if sys.platform == 'win32': 55 | _libcuda = ctypes.windll.LoadLibrary(_libcuda_libname) 56 | else: 57 | _libcuda = ctypes.cdll.LoadLibrary(_libcuda_libname) 58 | except OSError: 59 | pass 60 | else: 61 | break 62 | if _libcuda == None: 63 | raise OSError('CUDA driver library not found') 64 | 65 | # Exceptions corresponding to various CUDA driver errors: 66 | 67 | class CUDA_ERROR(Exception): 68 | """CUDA error.""" 69 | pass 70 | 71 | class CUDA_ERROR_INVALID_VALUE(CUDA_ERROR): 72 | pass 73 | 74 | class CUDA_ERROR_OUT_OF_MEMORY(CUDA_ERROR): 75 | pass 76 | 77 | class CUDA_ERROR_NOT_INITIALIZED(CUDA_ERROR): 78 | pass 79 | 80 | class CUDA_ERROR_DEINITIALIZED(CUDA_ERROR): 81 | pass 82 | 83 | class CUDA_ERROR_PROFILER_DISABLED(CUDA_ERROR): 84 | pass 85 | 86 | class CUDA_ERROR_PROFILER_NOT_INITIALIZED(CUDA_ERROR): 87 | pass 88 | 89 | class CUDA_ERROR_PROFILER_ALREADY_STARTED(CUDA_ERROR): 90 | pass 91 | 92 | class CUDA_ERROR_PROFILER_ALREADY_STOPPED(CUDA_ERROR): 93 | pass 94 | 95 | class CUDA_ERROR_NO_DEVICE(CUDA_ERROR): 96 | pass 97 | 98 | class CUDA_ERROR_INVALID_DEVICE(CUDA_ERROR): 99 | pass 100 | 101 | class CUDA_ERROR_INVALID_IMAGE(CUDA_ERROR): 102 | pass 103 | 104 | class CUDA_ERROR_INVALID_CONTEXT(CUDA_ERROR): 105 | pass 106 | 107 | class CUDA_ERROR_CONTEXT_ALREADY_CURRENT(CUDA_ERROR): 108 | pass 109 | 110 | class CUDA_ERROR_MAP_FAILED(CUDA_ERROR): 111 | pass 112 | 113 | class CUDA_ERROR_UNMAP_FAILED(CUDA_ERROR): 114 | pass 115 | 116 | class CUDA_ERROR_ARRAY_IS_MAPPED(CUDA_ERROR): 117 | pass 118 | 119 | class CUDA_ERROR_ALREADY_MAPPED(CUDA_ERROR): 120 | pass 121 | 122 | class CUDA_ERROR_NO_BINARY_FOR_GPU(CUDA_ERROR): 123 | pass 124 | 125 | class CUDA_ERROR_ALREADY_ACQUIRED(CUDA_ERROR): 126 | pass 127 | 128 | class CUDA_ERROR_NOT_MAPPED(CUDA_ERROR): 129 | pass 130 | 131 | class CUDA_ERROR_NOT_MAPPED_AS_ARRAY(CUDA_ERROR): 132 | pass 133 | 134 | class CUDA_ERROR_NOT_MAPPED_AS_POINTER(CUDA_ERROR): 135 | pass 136 | 137 | class CUDA_ERROR_ECC_UNCORRECTABLE(CUDA_ERROR): 138 | pass 139 | 140 | class CUDA_ERROR_UNSUPPORTED_LIMIT(CUDA_ERROR): 141 | pass 142 | 143 | class CUDA_ERROR_CONTEXT_ALREADY_IN_USE(CUDA_ERROR): 144 | pass 145 | 146 | class CUDA_ERROR_PEER_ACCESS_UNSUPPORTED(CUDA_ERROR): 147 | pass 148 | 149 | class CUDA_ERROR_INVALID_PTX(CUDA_ERROR): 150 | pass 151 | 152 | class CUDA_ERROR_INVALID_GRAPHICS_CONTEXT(CUDA_ERROR): 153 | pass 154 | 155 | class CUDA_ERROR_INVALID_SOURCE(CUDA_ERROR): 156 | pass 157 | 158 | class CUDA_ERROR_FILE_NOT_FOUND(CUDA_ERROR): 159 | pass 160 | 161 | class CUDA_ERROR_SHARED_OBJECT_SYMBOL_NOT_FOUND(CUDA_ERROR): 162 | pass 163 | 164 | class CUDA_ERROR_SHARED_OBJECT_INIT_FAILED(CUDA_ERROR): 165 | pass 166 | 167 | class CUDA_ERROR_OPERATING_SYSTEM(CUDA_ERROR): 168 | pass 169 | 170 | class CUDA_ERROR_INVALID_HANDLE(CUDA_ERROR): 171 | pass 172 | 173 | class CUDA_ERROR_NOT_FOUND(CUDA_ERROR): 174 | pass 175 | 176 | class CUDA_ERROR_NOT_READY(CUDA_ERROR): 177 | pass 178 | 179 | class CUDA_ERROR_ILLEGAL_ADDRESS(CUDA_ERROR): 180 | pass 181 | 182 | class CUDA_ERROR_LAUNCH_OUT_OF_RESOURCES(CUDA_ERROR): 183 | pass 184 | 185 | class CUDA_ERROR_LAUNCH_TIMEOUT(CUDA_ERROR): 186 | pass 187 | 188 | class CUDA_ERROR_LAUNCH_INCOMPATIBLE_TEXTURING(CUDA_ERROR): 189 | pass 190 | 191 | class CUDA_ERROR_PEER_ACCESS_ALREADY_ENABLED(CUDA_ERROR): 192 | pass 193 | 194 | class CUDA_ERROR_PEER_ACCESS_NOT_ENABLED(CUDA_ERROR): 195 | pass 196 | 197 | class CUDA_ERROR_PRIMARY_CONTEXT_ACTIVE(CUDA_ERROR): 198 | pass 199 | 200 | class CUDA_ERROR_CONTEXT_IS_DESTROYED(CUDA_ERROR): 201 | pass 202 | 203 | class CUDA_ERROR_ASSERT(CUDA_ERROR): 204 | pass 205 | 206 | class CUDA_ERROR_TOO_MANY_PEERS(CUDA_ERROR): 207 | pass 208 | 209 | class CUDA_ERROR_HOST_MEMORY_ALREADY_REGISTERED(CUDA_ERROR): 210 | pass 211 | 212 | class CUDA_ERROR_HOST_MEMORY_NOT_REGISTERED(CUDA_ERROR): 213 | pass 214 | 215 | class CUDA_ERROR_HARDWARE_STACK_ERROR(CUDA_ERROR): 216 | pass 217 | 218 | class CUDA_ERROR_ILLEGAL_INSTRUCTION(CUDA_ERROR): 219 | pass 220 | 221 | class CUDA_ERROR_MISALIGNED_ADDRESS(CUDA_ERROR): 222 | pass 223 | 224 | class CUDA_ERROR_INVALID_ADDRESS_SPACE(CUDA_ERROR): 225 | pass 226 | 227 | class CUDA_ERROR_INVALID_PC(CUDA_ERROR): 228 | pass 229 | 230 | class CUDA_ERROR_LAUNCH_FAILED(CUDA_ERROR): 231 | pass 232 | 233 | class CUDA_ERROR_NOT_PERMITTED(CUDA_ERROR): 234 | pass 235 | 236 | class CUDA_ERROR_NOT_SUPPORTED(CUDA_ERROR): 237 | pass 238 | 239 | class CUDA_ERROR_UNKNOWN(CUDA_ERROR): 240 | pass 241 | 242 | CUDA_EXCEPTIONS = { 243 | 1: CUDA_ERROR_INVALID_VALUE, 244 | 2: CUDA_ERROR_OUT_OF_MEMORY, 245 | 3: CUDA_ERROR_NOT_INITIALIZED, 246 | 4: CUDA_ERROR_DEINITIALIZED, 247 | 5: CUDA_ERROR_PROFILER_DISABLED, 248 | 6: CUDA_ERROR_PROFILER_NOT_INITIALIZED, 249 | 7: CUDA_ERROR_PROFILER_ALREADY_STARTED, 250 | 8: CUDA_ERROR_PROFILER_ALREADY_STOPPED, 251 | 100: CUDA_ERROR_NO_DEVICE, 252 | 101: CUDA_ERROR_INVALID_DEVICE, 253 | 200: CUDA_ERROR_INVALID_IMAGE, 254 | 201: CUDA_ERROR_INVALID_CONTEXT, 255 | 202: CUDA_ERROR_CONTEXT_ALREADY_CURRENT, 256 | 205: CUDA_ERROR_MAP_FAILED, 257 | 206: CUDA_ERROR_UNMAP_FAILED, 258 | 207: CUDA_ERROR_ARRAY_IS_MAPPED, 259 | 208: CUDA_ERROR_ALREADY_MAPPED, 260 | 209: CUDA_ERROR_NO_BINARY_FOR_GPU, 261 | 210: CUDA_ERROR_ALREADY_ACQUIRED, 262 | 211: CUDA_ERROR_NOT_MAPPED, 263 | 212: CUDA_ERROR_NOT_MAPPED_AS_ARRAY, 264 | 213: CUDA_ERROR_NOT_MAPPED_AS_POINTER, 265 | 214: CUDA_ERROR_ECC_UNCORRECTABLE, 266 | 215: CUDA_ERROR_UNSUPPORTED_LIMIT, 267 | 216: CUDA_ERROR_CONTEXT_ALREADY_IN_USE, 268 | 217: CUDA_ERROR_PEER_ACCESS_UNSUPPORTED, 269 | 218: CUDA_ERROR_INVALID_PTX, 270 | 219: CUDA_ERROR_INVALID_GRAPHICS_CONTEXT, 271 | 300: CUDA_ERROR_INVALID_SOURCE, 272 | 301: CUDA_ERROR_FILE_NOT_FOUND, 273 | 302: CUDA_ERROR_SHARED_OBJECT_SYMBOL_NOT_FOUND, 274 | 303: CUDA_ERROR_SHARED_OBJECT_INIT_FAILED, 275 | 304: CUDA_ERROR_OPERATING_SYSTEM, 276 | 400: CUDA_ERROR_INVALID_HANDLE, 277 | 500: CUDA_ERROR_NOT_FOUND, 278 | 600: CUDA_ERROR_NOT_READY, 279 | 700: CUDA_ERROR_ILLEGAL_ADDRESS, 280 | 701: CUDA_ERROR_LAUNCH_OUT_OF_RESOURCES, 281 | 702: CUDA_ERROR_LAUNCH_TIMEOUT, 282 | 703: CUDA_ERROR_LAUNCH_INCOMPATIBLE_TEXTURING, 283 | 704: CUDA_ERROR_PEER_ACCESS_ALREADY_ENABLED, 284 | 705: CUDA_ERROR_PEER_ACCESS_NOT_ENABLED, 285 | 708: CUDA_ERROR_PRIMARY_CONTEXT_ACTIVE, 286 | 709: CUDA_ERROR_CONTEXT_IS_DESTROYED, 287 | 710: CUDA_ERROR_ASSERT, 288 | 711: CUDA_ERROR_TOO_MANY_PEERS, 289 | 712: CUDA_ERROR_HOST_MEMORY_ALREADY_REGISTERED, 290 | 713: CUDA_ERROR_HOST_MEMORY_NOT_REGISTERED, 291 | 714: CUDA_ERROR_HARDWARE_STACK_ERROR, 292 | 715: CUDA_ERROR_ILLEGAL_INSTRUCTION, 293 | 716: CUDA_ERROR_MISALIGNED_ADDRESS, 294 | 717: CUDA_ERROR_INVALID_ADDRESS_SPACE, 295 | 718: CUDA_ERROR_INVALID_PC, 296 | 719: CUDA_ERROR_LAUNCH_FAILED, 297 | 800: CUDA_ERROR_NOT_PERMITTED, 298 | 801: CUDA_ERROR_NOT_SUPPORTED, 299 | 999: CUDA_ERROR_UNKNOWN 300 | } 301 | 302 | def cuCheckStatus(status): 303 | """ 304 | Raise CUDA exception. 305 | Raise an exception corresponding to the specified CUDA driver 306 | error code. 307 | Parameters 308 | ---------- 309 | status : int 310 | CUDA driver error code. 311 | See Also 312 | -------- 313 | CUDA_EXCEPTIONS 314 | """ 315 | 316 | if status != 0: 317 | try: 318 | raise CUDA_EXCEPTIONS[status] 319 | except KeyError: 320 | raise CUDA_ERROR 321 | 322 | 323 | CU_POINTER_ATTRIBUTE_CONTEXT = 1 324 | CU_POINTER_ATTRIBUTE_MEMORY_TYPE = 2 325 | CU_POINTER_ATTRIBUTE_DEVICE_POINTER = 3 326 | CU_POINTER_ATTRIBUTE_HOST_POINTER = 4 327 | 328 | _libcuda.cuPointerGetAttribute.restype = int 329 | _libcuda.cuPointerGetAttribute.argtypes = [ctypes.c_void_p, 330 | ctypes.c_int, 331 | ctypes.c_uint] 332 | def cuPointerGetAttribute(attribute, ptr): 333 | data = ctypes.c_void_p() 334 | status = _libcuda.cuPointerGetAttribute(data, attribute, ptr) 335 | cuCheckStatus(status) 336 | return data -------------------------------------------------------------------------------- /pymagma/cudart.py: -------------------------------------------------------------------------------- 1 | # This file is taken from scikits.cuda (https://github.com/lebedov/scikit-cuda) 2 | # 3 | # Copyright (c) 2009-2015, Lev Givon. 4 | # All rights reserved. 5 | # 6 | # Redistribution and use in source and binary forms, with or without 7 | # modification, are permitted provided that the following conditions are 8 | # met: 9 | # 10 | # * Redistributions of source code must retain the above copyright 11 | # notice, this list of conditions and the following disclaimer. 12 | # * Redistributions in binary form must reproduce the above 13 | # copyright notice, this list of conditions and the following 14 | # disclaimer in the documentation and/or other materials provided 15 | # with the distribution. 16 | # * Neither the name of Lev Givon nor the names of any 17 | # contributors may be used to endorse or promote products derived 18 | # from this software without specific prior written permission. 19 | # 20 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | #!/usr/bin/env python 33 | 34 | """ 35 | Python interface to CUDA runtime functions. 36 | """ 37 | 38 | import atexit, ctypes, platform, re, sys, warnings 39 | import numpy as np 40 | 41 | # Load library: 42 | _version_list = [7.5, 7.0, 6.5, 6.0, 5.5, 5.0, 4.0] 43 | if 'linux' in sys.platform: 44 | _libcudart_libname_list = ['libcudart.so'] + \ 45 | ['libcudart.so.%s' % v for v in _version_list] 46 | elif sys.platform == 'darwin': 47 | _libcudart_libname_list = ['libcudart.dylib'] 48 | elif sys.platform == 'win32': 49 | if sys.maxsize > 2**32: 50 | _libcudart_libname_list = ['cudart.dll'] + \ 51 | ['cudart64_%s.dll' % int(10*v) for v in _version_list] 52 | else: 53 | _libcudart_libname_list = ['cudart.dll'] + \ 54 | ['cudart32_%s.dll' % int(10*v) for v in _version_list] 55 | else: 56 | raise RuntimeError('unsupported platform') 57 | 58 | # Print understandable error message when library cannot be found: 59 | _libcudart = None 60 | for _libcudart_libname in _libcudart_libname_list: 61 | try: 62 | if sys.platform == 'win32': 63 | _libcudart = ctypes.windll.LoadLibrary(_libcudart_libname) 64 | else: 65 | #_libcudart = ctypes.cdll.LoadLibrary(_libcudart_libname) 66 | # ctypes.RTLD_GLOBAL is necessary to resolve the symbols which are otherwise undefined in the dynamically loaded library libpycusp.dylib, sine it links to libcudart.dylib 67 | _libcudart = ctypes.CDLL(_libcudart_libname, ctypes.RTLD_GLOBAL) 68 | except OSError: 69 | pass 70 | else: 71 | break 72 | if _libcudart == None: 73 | raise OSError('CUDA runtime library not found') 74 | 75 | # Code adapted from PARRET: 76 | def POINTER(obj): 77 | """ 78 | Create ctypes pointer to object. 79 | Notes 80 | ----- 81 | This function converts None to a real NULL pointer because of bug 82 | in how ctypes handles None on 64-bit platforms. 83 | """ 84 | 85 | p = ctypes.POINTER(obj) 86 | if not isinstance(p.from_param, classmethod): 87 | def from_param(cls, x): 88 | if x is None: 89 | return cls() 90 | else: 91 | return x 92 | p.from_param = classmethod(from_param) 93 | 94 | return p 95 | 96 | # Classes corresponding to CUDA vector structures: 97 | class float2(ctypes.Structure): 98 | _fields_ = [ 99 | ('x', ctypes.c_float), 100 | ('y', ctypes.c_float) 101 | ] 102 | 103 | class cuFloatComplex(float2): 104 | @property 105 | def value(self): 106 | return complex(self.x, self.y) 107 | 108 | class double2(ctypes.Structure): 109 | _fields_ = [ 110 | ('x', ctypes.c_double), 111 | ('y', ctypes.c_double) 112 | ] 113 | 114 | class cuDoubleComplex(double2): 115 | @property 116 | def value(self): 117 | return complex(self.x, self.y) 118 | 119 | def gpuarray_ptr(g): 120 | """ 121 | Return ctypes pointer to data in GPUAarray object. 122 | """ 123 | 124 | addr = int(g.gpudata) 125 | if g.dtype == np.int8: 126 | return ctypes.cast(addr, POINTER(ctypes.c_byte)) 127 | if g.dtype == np.uint8: 128 | return ctypes.cast(addr, POINTER(ctypes.c_ubyte)) 129 | if g.dtype == np.int16: 130 | return ctypes.cast(addr, POINTER(ctypes.c_short)) 131 | if g.dtype == np.uint16: 132 | return ctypes.cast(addr, POINTER(ctypes.c_ushort)) 133 | if g.dtype == np.int32: 134 | return ctypes.cast(addr, POINTER(ctypes.c_int)) 135 | if g.dtype == np.uint32: 136 | return ctypes.cast(addr, POINTER(ctypes.c_uint)) 137 | if g.dtype == np.int64: 138 | return ctypes.cast(addr, POINTER(ctypes.c_long)) 139 | if g.dtype == np.uint64: 140 | return ctypes.cast(addr, POINTER(ctypes.c_ulong)) 141 | if g.dtype == np.float32: 142 | return ctypes.cast(addr, POINTER(ctypes.c_float)) 143 | elif g.dtype == np.float64: 144 | return ctypes.cast(addr, POINTER(ctypes.c_double)) 145 | elif g.dtype == np.complex64: 146 | return ctypes.cast(addr, POINTER(cuFloatComplex)) 147 | elif g.dtype == np.complex128: 148 | return ctypes.cast(addr, POINTER(cuDoubleComplex)) 149 | else: 150 | raise ValueError('unrecognized type') 151 | 152 | _libcudart.cudaGetErrorString.restype = ctypes.c_char_p 153 | _libcudart.cudaGetErrorString.argtypes = [ctypes.c_int] 154 | def cudaGetErrorString(e): 155 | """ 156 | Retrieve CUDA error string. 157 | Return the string associated with the specified CUDA error status 158 | code. 159 | Parameters 160 | ---------- 161 | e : int 162 | Error number. 163 | Returns 164 | ------- 165 | s : str 166 | Error string. 167 | """ 168 | 169 | return _libcudart.cudaGetErrorString(e) 170 | 171 | # Generic CUDA error: 172 | class cudaError(Exception): 173 | """CUDA error.""" 174 | pass 175 | 176 | # Exceptions corresponding to various CUDA runtime errors: 177 | class cudaErrorMissingConfiguration(cudaError): 178 | __doc__ = _libcudart.cudaGetErrorString(1) 179 | pass 180 | 181 | class cudaErrorMemoryAllocation(cudaError): 182 | __doc__ = _libcudart.cudaGetErrorString(2) 183 | pass 184 | 185 | class cudaErrorInitializationError(cudaError): 186 | __doc__ = _libcudart.cudaGetErrorString(3) 187 | pass 188 | 189 | class cudaErrorLaunchFailure(cudaError): 190 | __doc__ = _libcudart.cudaGetErrorString(4) 191 | pass 192 | 193 | class cudaErrorPriorLaunchFailure(cudaError): 194 | __doc__ = _libcudart.cudaGetErrorString(5) 195 | pass 196 | 197 | class cudaErrorLaunchTimeout(cudaError): 198 | __doc__ = _libcudart.cudaGetErrorString(6) 199 | pass 200 | 201 | class cudaErrorLaunchOutOfResources(cudaError): 202 | __doc__ = _libcudart.cudaGetErrorString(7) 203 | pass 204 | 205 | class cudaErrorInvalidDeviceFunction(cudaError): 206 | __doc__ = _libcudart.cudaGetErrorString(8) 207 | pass 208 | 209 | class cudaErrorInvalidConfiguration(cudaError): 210 | __doc__ = _libcudart.cudaGetErrorString(9) 211 | pass 212 | 213 | class cudaErrorInvalidDevice(cudaError): 214 | __doc__ = _libcudart.cudaGetErrorString(10) 215 | pass 216 | 217 | class cudaErrorInvalidValue(cudaError): 218 | __doc__ = _libcudart.cudaGetErrorString(11) 219 | pass 220 | 221 | class cudaErrorInvalidPitchValue(cudaError): 222 | __doc__ = _libcudart.cudaGetErrorString(12) 223 | pass 224 | 225 | class cudaErrorInvalidSymbol(cudaError): 226 | __doc__ = _libcudart.cudaGetErrorString(13) 227 | pass 228 | 229 | class cudaErrorMapBufferObjectFailed(cudaError): 230 | __doc__ = _libcudart.cudaGetErrorString(14) 231 | pass 232 | 233 | class cudaErrorUnmapBufferObjectFailed(cudaError): 234 | __doc__ = _libcudart.cudaGetErrorString(15) 235 | pass 236 | 237 | class cudaErrorInvalidHostPointer(cudaError): 238 | __doc__ = _libcudart.cudaGetErrorString(16) 239 | pass 240 | 241 | class cudaErrorInvalidDevicePointer(cudaError): 242 | __doc__ = _libcudart.cudaGetErrorString(17) 243 | pass 244 | 245 | class cudaErrorInvalidTexture(cudaError): 246 | __doc__ = _libcudart.cudaGetErrorString(18) 247 | pass 248 | 249 | class cudaErrorInvalidTextureBinding(cudaError): 250 | __doc__ = _libcudart.cudaGetErrorString(19) 251 | pass 252 | 253 | class cudaErrorInvalidChannelDescriptor(cudaError): 254 | __doc__ = _libcudart.cudaGetErrorString(20) 255 | pass 256 | 257 | class cudaErrorInvalidMemcpyDirection(cudaError): 258 | __doc__ = _libcudart.cudaGetErrorString(21) 259 | pass 260 | 261 | class cudaErrorTextureFetchFailed(cudaError): 262 | __doc__ = _libcudart.cudaGetErrorString(23) 263 | pass 264 | 265 | class cudaErrorTextureNotBound(cudaError): 266 | __doc__ = _libcudart.cudaGetErrorString(24) 267 | pass 268 | 269 | class cudaErrorSynchronizationError(cudaError): 270 | __doc__ = _libcudart.cudaGetErrorString(25) 271 | pass 272 | 273 | class cudaErrorInvalidFilterSetting(cudaError): 274 | __doc__ = _libcudart.cudaGetErrorString(26) 275 | pass 276 | 277 | class cudaErrorInvalidNormSetting(cudaError): 278 | __doc__ = _libcudart.cudaGetErrorString(27) 279 | pass 280 | 281 | class cudaErrorMixedDeviceExecution(cudaError): 282 | __doc__ = _libcudart.cudaGetErrorString(28) 283 | pass 284 | 285 | class cudaErrorCudartUnloading(cudaError): 286 | __doc__ = _libcudart.cudaGetErrorString(29) 287 | pass 288 | 289 | class cudaErrorUnknown(cudaError): 290 | __doc__ = _libcudart.cudaGetErrorString(30) 291 | pass 292 | 293 | class cudaErrorNotYetImplemented(cudaError): 294 | __doc__ = _libcudart.cudaGetErrorString(31) 295 | pass 296 | 297 | class cudaErrorMemoryValueTooLarge(cudaError): 298 | __doc__ = _libcudart.cudaGetErrorString(32) 299 | pass 300 | 301 | class cudaErrorInvalidResourceHandle(cudaError): 302 | __doc__ = _libcudart.cudaGetErrorString(33) 303 | pass 304 | 305 | class cudaErrorNotReady(cudaError): 306 | __doc__ = _libcudart.cudaGetErrorString(34) 307 | pass 308 | 309 | class cudaErrorInsufficientDriver(cudaError): 310 | __doc__ = _libcudart.cudaGetErrorString(35) 311 | pass 312 | 313 | class cudaErrorSetOnActiveProcess(cudaError): 314 | __doc__ = _libcudart.cudaGetErrorString(36) 315 | pass 316 | 317 | class cudaErrorInvalidSurface(cudaError): 318 | __doc__ = _libcudart.cudaGetErrorString(37) 319 | pass 320 | 321 | class cudaErrorNoDevice(cudaError): 322 | __doc__ = _libcudart.cudaGetErrorString(38) 323 | pass 324 | 325 | class cudaErrorECCUncorrectable(cudaError): 326 | __doc__ = _libcudart.cudaGetErrorString(39) 327 | pass 328 | 329 | class cudaErrorSharedObjectSymbolNotFound(cudaError): 330 | __doc__ = _libcudart.cudaGetErrorString(40) 331 | pass 332 | 333 | class cudaErrorSharedObjectInitFailed(cudaError): 334 | __doc__ = _libcudart.cudaGetErrorString(41) 335 | pass 336 | 337 | class cudaErrorUnsupportedLimit(cudaError): 338 | __doc__ = _libcudart.cudaGetErrorString(42) 339 | pass 340 | 341 | class cudaErrorDuplicateVariableName(cudaError): 342 | __doc__ = _libcudart.cudaGetErrorString(43) 343 | pass 344 | 345 | class cudaErrorDuplicateTextureName(cudaError): 346 | __doc__ = _libcudart.cudaGetErrorString(44) 347 | pass 348 | 349 | class cudaErrorDuplicateSurfaceName(cudaError): 350 | __doc__ = _libcudart.cudaGetErrorString(45) 351 | pass 352 | 353 | class cudaErrorDevicesUnavailable(cudaError): 354 | __doc__ = _libcudart.cudaGetErrorString(46) 355 | pass 356 | 357 | class cudaErrorInvalidKernelImage(cudaError): 358 | __doc__ = _libcudart.cudaGetErrorString(47) 359 | pass 360 | 361 | class cudaErrorNoKernelImageForDevice(cudaError): 362 | __doc__ = _libcudart.cudaGetErrorString(48) 363 | pass 364 | 365 | class cudaErrorIncompatibleDriverContext(cudaError): 366 | __doc__ = _libcudart.cudaGetErrorString(49) 367 | pass 368 | 369 | class cudaErrorPeerAccessAlreadyEnabled(cudaError): 370 | __doc__ = _libcudart.cudaGetErrorString(50) 371 | pass 372 | 373 | class cudaErrorPeerAccessNotEnabled(cudaError): 374 | __doc__ = _libcudart.cudaGetErrorString(51) 375 | pass 376 | 377 | class cudaErrorDeviceAlreadyInUse(cudaError): 378 | __doc__ = _libcudart.cudaGetErrorString(54) 379 | pass 380 | 381 | class cudaErrorProfilerDisabled(cudaError): 382 | __doc__ = _libcudart.cudaGetErrorString(55) 383 | pass 384 | 385 | class cudaErrorProfilerNotInitialized(cudaError): 386 | __doc__ = _libcudart.cudaGetErrorString(56) 387 | pass 388 | 389 | class cudaErrorProfilerAlreadyStarted(cudaError): 390 | __doc__ = _libcudart.cudaGetErrorString(57) 391 | pass 392 | 393 | class cudaErrorProfilerAlreadyStopped(cudaError): 394 | __doc__ = _libcudart.cudaGetErrorString(58) 395 | pass 396 | 397 | class cudaErrorAssert(cudaError): 398 | __doc__ = _libcudart.cudaGetErrorString(59) 399 | pass 400 | 401 | class cudaErrorTooManyPeers(cudaError): 402 | __doc__ = _libcudart.cudaGetErrorString(60) 403 | pass 404 | 405 | class cudaErrorHostMemoryAlreadyRegistered(cudaError): 406 | __doc__ = _libcudart.cudaGetErrorString(61) 407 | pass 408 | 409 | class cudaErrorHostMemoryNotRegistered(cudaError): 410 | __doc__ = _libcudart.cudaGetErrorString(62) 411 | pass 412 | 413 | class cudaErrorOperatingSystem(cudaError): 414 | __doc__ = _libcudart.cudaGetErrorString(63) 415 | pass 416 | 417 | class cudaErrorPeerAccessUnsupported(cudaError): 418 | __doc__ = _libcudart.cudaGetErrorString(64) 419 | pass 420 | 421 | class cudaErrorLaunchMaxDepthExceeded(cudaError): 422 | __doc__ = _libcudart.cudaGetErrorString(65) 423 | pass 424 | 425 | class cudaErrorLaunchFileScopedTex(cudaError): 426 | __doc__ = _libcudart.cudaGetErrorString(66) 427 | pass 428 | 429 | class cudaErrorLaunchFileScopedSurf(cudaError): 430 | __doc__ = _libcudart.cudaGetErrorString(67) 431 | pass 432 | 433 | class cudaErrorSyncDepthExceeded(cudaError): 434 | __doc__ = _libcudart.cudaGetErrorString(68) 435 | pass 436 | 437 | class cudaErrorLaunchPendingCountExceeded(cudaError): 438 | __doc__ = _libcudart.cudaGetErrorString(69) 439 | pass 440 | 441 | class cudaErrorNotPermitted(cudaError): 442 | __doc__ = _libcudart.cudaGetErrorString(70) 443 | pass 444 | 445 | class cudaErrorNotSupported(cudaError): 446 | __doc__ = _libcudart.cudaGetErrorString(71) 447 | pass 448 | 449 | class cudaErrorHardwareStackError(cudaError): 450 | __doc__ = _libcudart.cudaGetErrorString(72) 451 | pass 452 | 453 | class cudaErrorIllegalInstruction(cudaError): 454 | __doc__ = _libcudart.cudaGetErrorString(73) 455 | pass 456 | 457 | class cudaErrorMisalignedAddress(cudaError): 458 | __doc__ = _libcudart.cudaGetErrorString(74) 459 | pass 460 | 461 | class cudaErrorInvalidAddressSpace(cudaError): 462 | __doc__ = _libcudart.cudaGetErrorString(75) 463 | pass 464 | 465 | class cudaErrorInvalidPc(cudaError): 466 | __doc__ = _libcudart.cudaGetErrorString(76) 467 | pass 468 | 469 | class cudaErrorIllegalAddress(cudaError): 470 | __doc__ = _libcudart.cudaGetErrorString(77) 471 | pass 472 | 473 | class cudaErrorInvalidPtx(cudaError): 474 | __doc__ = _libcudart.cudaGetErrorString(78) 475 | pass 476 | 477 | class cudaErrorInvalidGraphicsContext(cudaError): 478 | __doc__ = _libcudart.cudaGetErrorString(79) 479 | 480 | class cudaErrorStartupFailure(cudaError): 481 | __doc__ = _libcudart.cudaGetErrorString(127) 482 | pass 483 | 484 | cudaExceptions = { 485 | 1: cudaErrorMissingConfiguration, 486 | 2: cudaErrorMemoryAllocation, 487 | 3: cudaErrorInitializationError, 488 | 4: cudaErrorLaunchFailure, 489 | 5: cudaErrorPriorLaunchFailure, 490 | 6: cudaErrorLaunchTimeout, 491 | 7: cudaErrorLaunchOutOfResources, 492 | 8: cudaErrorInvalidDeviceFunction, 493 | 9: cudaErrorInvalidConfiguration, 494 | 10: cudaErrorInvalidDevice, 495 | 11: cudaErrorInvalidValue, 496 | 12: cudaErrorInvalidPitchValue, 497 | 13: cudaErrorInvalidSymbol, 498 | 14: cudaErrorMapBufferObjectFailed, 499 | 15: cudaErrorUnmapBufferObjectFailed, 500 | 16: cudaErrorInvalidHostPointer, 501 | 17: cudaErrorInvalidDevicePointer, 502 | 18: cudaErrorInvalidTexture, 503 | 19: cudaErrorInvalidTextureBinding, 504 | 20: cudaErrorInvalidChannelDescriptor, 505 | 21: cudaErrorInvalidMemcpyDirection, 506 | 22: cudaError, 507 | 23: cudaErrorTextureFetchFailed, 508 | 24: cudaErrorTextureNotBound, 509 | 25: cudaErrorSynchronizationError, 510 | 26: cudaErrorInvalidFilterSetting, 511 | 27: cudaErrorInvalidNormSetting, 512 | 28: cudaErrorMixedDeviceExecution, 513 | 29: cudaErrorCudartUnloading, 514 | 30: cudaErrorUnknown, 515 | 31: cudaErrorNotYetImplemented, 516 | 32: cudaErrorMemoryValueTooLarge, 517 | 33: cudaErrorInvalidResourceHandle, 518 | 34: cudaErrorNotReady, 519 | 35: cudaErrorInsufficientDriver, 520 | 36: cudaErrorSetOnActiveProcess, 521 | 37: cudaErrorInvalidSurface, 522 | 38: cudaErrorNoDevice, 523 | 39: cudaErrorECCUncorrectable, 524 | 40: cudaErrorSharedObjectSymbolNotFound, 525 | 41: cudaErrorSharedObjectInitFailed, 526 | 42: cudaErrorUnsupportedLimit, 527 | 43: cudaErrorDuplicateVariableName, 528 | 44: cudaErrorDuplicateTextureName, 529 | 45: cudaErrorDuplicateSurfaceName, 530 | 46: cudaErrorDevicesUnavailable, 531 | 47: cudaErrorInvalidKernelImage, 532 | 48: cudaErrorNoKernelImageForDevice, 533 | 49: cudaErrorIncompatibleDriverContext, 534 | 50: cudaErrorPeerAccessAlreadyEnabled, 535 | 51: cudaErrorPeerAccessNotEnabled, 536 | 52: cudaError, 537 | 53: cudaError, 538 | 54: cudaErrorDeviceAlreadyInUse, 539 | 55: cudaErrorProfilerDisabled, 540 | 56: cudaErrorProfilerNotInitialized, 541 | 57: cudaErrorProfilerAlreadyStarted, 542 | 58: cudaErrorProfilerAlreadyStopped, 543 | 59: cudaErrorAssert, 544 | 60: cudaErrorTooManyPeers, 545 | 61: cudaErrorHostMemoryAlreadyRegistered, 546 | 62: cudaErrorHostMemoryNotRegistered, 547 | 63: cudaErrorOperatingSystem, 548 | 64: cudaErrorPeerAccessUnsupported, 549 | 65: cudaErrorLaunchMaxDepthExceeded, 550 | 66: cudaErrorLaunchFileScopedTex, 551 | 67: cudaErrorLaunchFileScopedSurf, 552 | 68: cudaErrorSyncDepthExceeded, 553 | 69: cudaErrorLaunchPendingCountExceeded, 554 | 70: cudaErrorNotPermitted, 555 | 71: cudaErrorNotSupported, 556 | 72: cudaErrorHardwareStackError, 557 | 73: cudaErrorIllegalInstruction, 558 | 74: cudaErrorMisalignedAddress, 559 | 75: cudaErrorInvalidAddressSpace, 560 | 76: cudaErrorInvalidPc, 561 | 77: cudaErrorIllegalAddress, 562 | 78: cudaErrorInvalidPtx, 563 | 79: cudaErrorInvalidGraphicsContext, 564 | 127: cudaErrorStartupFailure 565 | } 566 | 567 | def cudaCheckStatus(status): 568 | """ 569 | Raise CUDA exception. 570 | Raise an exception corresponding to the specified CUDA runtime error 571 | code. 572 | Parameters 573 | ---------- 574 | status : int 575 | CUDA runtime error code. 576 | See Also 577 | -------- 578 | cudaExceptions 579 | """ 580 | 581 | if status != 0: 582 | try: 583 | raise cudaExceptions[status] 584 | except KeyError: 585 | raise cudaError('unknown CUDA error %s' % status) 586 | 587 | # Memory allocation functions (adapted from pystream): 588 | _libcudart.cudaMalloc.restype = int 589 | _libcudart.cudaMalloc.argtypes = [ctypes.POINTER(ctypes.c_void_p), 590 | ctypes.c_size_t] 591 | def cudaMalloc(count, ctype=None): 592 | """ 593 | Allocate device memory. 594 | Allocate memory on the device associated with the current active 595 | context. 596 | Parameters 597 | ---------- 598 | count : int 599 | Number of bytes of memory to allocate 600 | ctype : _ctypes.SimpleType, optional 601 | ctypes type to cast returned pointer. 602 | Returns 603 | ------- 604 | ptr : ctypes pointer 605 | Pointer to allocated device memory. 606 | """ 607 | 608 | ptr = ctypes.c_void_p() 609 | status = _libcudart.cudaMalloc(ctypes.byref(ptr), count) 610 | cudaCheckStatus(status) 611 | if ctype != None: 612 | ptr = ctypes.cast(ptr, ctypes.POINTER(ctype)) 613 | return ptr 614 | 615 | _libcudart.cudaFree.restype = int 616 | _libcudart.cudaFree.argtypes = [ctypes.c_void_p] 617 | def cudaFree(ptr): 618 | """ 619 | Free device memory. 620 | Free allocated memory on the device associated with the current active 621 | context. 622 | Parameters 623 | ---------- 624 | ptr : ctypes pointer 625 | Pointer to allocated device memory. 626 | """ 627 | 628 | status = _libcudart.cudaFree(ptr) 629 | cudaCheckStatus(status) 630 | 631 | _libcudart.cudaMallocPitch.restype = int 632 | _libcudart.cudaMallocPitch.argtypes = [ctypes.POINTER(ctypes.c_void_p), 633 | ctypes.POINTER(ctypes.c_size_t), 634 | ctypes.c_size_t, ctypes.c_size_t] 635 | def cudaMallocPitch(pitch, rows, cols, elesize): 636 | """ 637 | Allocate pitched device memory. 638 | Allocate pitched memory on the device associated with the current active 639 | context. 640 | Parameters 641 | ---------- 642 | pitch : int 643 | Pitch for allocation. 644 | rows : int 645 | Requested pitched allocation height. 646 | cols : int 647 | Requested pitched allocation width. 648 | elesize : int 649 | Size of memory element. 650 | Returns 651 | ------- 652 | ptr : ctypes pointer 653 | Pointer to allocated device memory. 654 | """ 655 | 656 | ptr = ctypes.c_void_p() 657 | status = _libcudart.cudaMallocPitch(ctypes.byref(ptr), 658 | ctypes.c_size_t(pitch), cols*elesize, 659 | rows) 660 | cudaCheckStatus(status) 661 | return ptr, pitch 662 | 663 | # Memory copy modes: 664 | cudaMemcpyHostToHost = 0 665 | cudaMemcpyHostToDevice = 1 666 | cudaMemcpyDeviceToHost = 2 667 | cudaMemcpyDeviceToDevice = 3 668 | cudaMemcpyDefault = 4 669 | 670 | _libcudart.cudaMemcpy.restype = int 671 | _libcudart.cudaMemcpy.argtypes = [ctypes.c_void_p, ctypes.c_void_p, 672 | ctypes.c_size_t, ctypes.c_int] 673 | def cudaMemcpy_htod(dst, src, count): 674 | """ 675 | Copy memory from host to device. 676 | Copy data from host memory to device memory. 677 | Parameters 678 | ---------- 679 | dst : ctypes pointer 680 | Device memory pointer. 681 | src : ctypes pointer 682 | Host memory pointer. 683 | count : int 684 | Number of bytes to copy. 685 | """ 686 | 687 | status = _libcudart.cudaMemcpy(dst, src, 688 | ctypes.c_size_t(count), 689 | cudaMemcpyHostToDevice) 690 | cudaCheckStatus(status) 691 | 692 | def cudaMemcpy_dtoh(dst, src, count): 693 | """ 694 | Copy memory from device to host. 695 | Copy data from device memory to host memory. 696 | Parameters 697 | ---------- 698 | dst : ctypes pointer 699 | Host memory pointer. 700 | src : ctypes pointer 701 | Device memory pointer. 702 | count : int 703 | Number of bytes to copy. 704 | """ 705 | 706 | status = _libcudart.cudaMemcpy(dst, src, 707 | ctypes.c_size_t(count), 708 | cudaMemcpyDeviceToHost) 709 | cudaCheckStatus(status) 710 | 711 | _libcudart.cudaMemGetInfo.restype = int 712 | _libcudart.cudaMemGetInfo.argtypes = [ctypes.c_void_p, 713 | ctypes.c_void_p] 714 | def cudaMemGetInfo(): 715 | """ 716 | Return the amount of free and total device memory. 717 | Returns 718 | ------- 719 | free : long 720 | Free memory in bytes. 721 | total : long 722 | Total memory in bytes. 723 | """ 724 | 725 | free = ctypes.c_size_t() 726 | total = ctypes.c_size_t() 727 | status = _libcudart.cudaMemGetInfo(ctypes.byref(free), 728 | ctypes.byref(total)) 729 | cudaCheckStatus(status) 730 | return free.value, total.value 731 | 732 | _libcudart.cudaSetDevice.restype = int 733 | _libcudart.cudaSetDevice.argtypes = [ctypes.c_int] 734 | def cudaSetDevice(dev): 735 | """ 736 | Set current CUDA device. 737 | Select a device to use for subsequent CUDA operations. 738 | Parameters 739 | ---------- 740 | dev : int 741 | Device number. 742 | """ 743 | 744 | status = _libcudart.cudaSetDevice(dev) 745 | cudaCheckStatus(status) 746 | 747 | # Added on 2016.06.22, C. Holly 748 | _libcudart.cudaDeviceReset.restype = int 749 | _libcudart.cudaDeviceReset.argtypes = [] 750 | def cudaDeviceReset(): 751 | """ 752 | Reset current CUDA device. 753 | """ 754 | 755 | status = _libcudart.cudaDeviceReset() 756 | cudaCheckStatus(status) 757 | 758 | 759 | _libcudart.cudaGetDevice.restype = int 760 | _libcudart.cudaGetDevice.argtypes = [ctypes.POINTER(ctypes.c_int)] 761 | def cudaGetDevice(): 762 | """ 763 | Get current CUDA device. 764 | Return the identifying number of the device currently used to 765 | process CUDA operations. 766 | Returns 767 | ------- 768 | dev : int 769 | Device number. 770 | """ 771 | 772 | dev = ctypes.c_int() 773 | status = _libcudart.cudaGetDevice(ctypes.byref(dev)) 774 | cudaCheckStatus(status) 775 | return dev.value 776 | 777 | _libcudart.cudaDriverGetVersion.restype = int 778 | _libcudart.cudaDriverGetVersion.argtypes = [ctypes.POINTER(ctypes.c_int)] 779 | def cudaDriverGetVersion(): 780 | """ 781 | Get installed CUDA driver version. 782 | Return the version of the installed CUDA driver as an integer. If 783 | no driver is detected, 0 is returned. 784 | Returns 785 | ------- 786 | version : int 787 | Driver version. 788 | """ 789 | 790 | version = ctypes.c_int() 791 | status = _libcudart.cudaDriverGetVersion(ctypes.byref(version)) 792 | cudaCheckStatus(status) 793 | return version.value 794 | 795 | try: 796 | _cudart_version = str(cudaDriverGetVersion()) 797 | except: 798 | _cudart_version = '9999' 799 | 800 | class _cudart_version_req(object): 801 | """ 802 | Decorator to replace function with a placeholder that raises an exception 803 | if the installed CUDA Runtime version is not greater than `v`. 804 | """ 805 | 806 | def __init__(self, v): 807 | self.vs = str(v) 808 | if isinstance(v, int): 809 | major = str(v) 810 | minor = '0' 811 | else: 812 | major, minor = re.search('(\d+)\.(\d+)', self.vs).groups() 813 | self.vi = major.ljust(2, '0')+minor.ljust(2, '0') 814 | 815 | def __call__(self,f): 816 | def f_new(*args,**kwargs): 817 | raise NotImplementedError('CUDART '+self.vs+' required') 818 | f_new.__doc__ = f.__doc__ 819 | 820 | if _cudart_version >= self.vi: 821 | return f 822 | else: 823 | return f_new 824 | 825 | # Memory types: 826 | cudaMemoryTypeHost = 1 827 | cudaMemoryTypeDevice = 2 828 | 829 | class cudaPointerAttributes(ctypes.Structure): 830 | _fields_ = [ 831 | ('memoryType', ctypes.c_int), 832 | ('device', ctypes.c_int), 833 | ('devicePointer', ctypes.c_void_p), 834 | ('hostPointer', ctypes.c_void_p) 835 | ] 836 | 837 | _libcudart.cudaPointerGetAttributes.restype = int 838 | _libcudart.cudaPointerGetAttributes.argtypes = [ctypes.c_void_p, 839 | ctypes.c_void_p] 840 | def cudaPointerGetAttributes(ptr): 841 | """ 842 | Get memory pointer attributes. 843 | Returns attributes of the specified pointer. 844 | Parameters 845 | ---------- 846 | ptr : ctypes pointer 847 | Memory pointer to examine. 848 | Returns 849 | ------- 850 | memory_type : int 851 | Memory type; 1 indicates host memory, 2 indicates device 852 | memory. 853 | device : int 854 | Number of device associated with pointer. 855 | Notes 856 | ----- 857 | This function only works with CUDA 4.0 and later. 858 | """ 859 | 860 | attributes = cudaPointerAttributes() 861 | status = \ 862 | _libcudart.cudaPointerGetAttributes(ctypes.byref(attributes), ptr) 863 | cudaCheckStatus(status) 864 | return attributes.memoryType, attributes.device -------------------------------------------------------------------------------- /pymagma/magma.py: -------------------------------------------------------------------------------- 1 | # Parts of this file are taken from scikits.cuda (https://github.com/lebedov/scikit-cuda) 2 | # For these parts the following holds: 3 | # 4 | # Copyright (c) 2009-2015, Lev Givon. 5 | # All rights reserved. 6 | # 7 | # The additional sparse-iter functions were added for the pymagma project. 8 | # For these parts the following holds: 9 | # 10 | # Copyright (c) 2016, Carlo Holly. 11 | # All rights reserved. 12 | # 13 | # Redistribution and use in source and binary forms, with or without 14 | # modification, are permitted provided that the following conditions are 15 | # met: 16 | # 17 | # * Redistributions of source code must retain the above copyright 18 | # notice, this list of conditions and the following disclaimer. 19 | # * Redistributions in binary form must reproduce the above 20 | # copyright notice, this list of conditions and the following 21 | # disclaimer in the documentation and/or other materials provided 22 | # with the distribution. 23 | # * Neither the name of Lev Givon nor the names of any 24 | # contributors may be used to endorse or promote products derived 25 | # from this software without specific prior written permission. 26 | # 27 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 28 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 29 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 30 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 31 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 32 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 33 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 34 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 35 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 36 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 37 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 | 39 | #!/usr/bin/env python 40 | 41 | """ 42 | Python interface to MAGMA toolkit. 43 | """ 44 | 45 | from __future__ import absolute_import, division, print_function 46 | 47 | import sys, inspect 48 | import ctypes 49 | import atexit 50 | import numpy as np 51 | import scipy as sp 52 | from numpy.ctypeslib import ndpointer 53 | from . import cuda 54 | 55 | # Structures, type definitions and constants 56 | 57 | real_Double_t = ctypes.c_double 58 | magma_int_t = ctypes.c_int 59 | magma_index_t = ctypes.c_int 60 | p_magma_index_t = ndpointer(ctypes.c_int, flags="C_CONTIGUOUS") 61 | p_magmaDoubleComplex = ndpointer(np.complex128, flags="C_CONTIGUOUS") 62 | # TODO: Add definition of cusparseSolveAnalysisInfo 63 | cusparseSolveAnalysisInfo_t = ctypes.c_void_p #ctypes.POINTER(cusparseSolveAnalysisInfo) 64 | 65 | MAGMA_SUCCESS = 0 66 | HAVE_CUBLAS = 1 67 | 68 | 69 | class magma_uplo_t: 70 | MagmaUpper = 121 71 | MagmaLower = 122 72 | MagmaFull = 123 # lascl, laset 73 | MagmaHessenberg = 124 # lascl 74 | 75 | 76 | class magma_storage_t: 77 | Magma_CSR = 611 78 | Magma_ELLPACKT = 612 79 | Magma_ELL = 613 80 | Magma_DENSE = 614 81 | Magma_BCSR = 615 82 | Magma_CSC = 616 83 | Magma_HYB = 617 84 | Magma_COO = 618 85 | Magma_ELLRT = 619 86 | Magma_SPMVFUNCTION = 620 87 | Magma_SELLP = 621 88 | Magma_ELLD = 622 89 | Magma_CSRLIST = 623 90 | Magma_CSRD = 624 91 | Magma_CSRL = 627 92 | Magma_CSRU = 628 93 | Magma_CSRCOO = 629 94 | Magma_CUCSR = 630 95 | 96 | 97 | class magma_solver_type: 98 | Magma_CG = 431 99 | Magma_CGMERGE = 432 100 | Magma_GMRES = 433 101 | Magma_BICGSTAB = 434 102 | Magma_BICGSTABMERGE = 435 103 | Magma_BICGSTABMERGE2 = 436 104 | Magma_JACOBI = 437 105 | Magma_GS = 438 106 | Magma_ITERREF = 439 107 | Magma_BCSRLU = 440 108 | Magma_PCG = 441 109 | Magma_PGMRES = 442 110 | Magma_PBICGSTAB = 443 111 | Magma_PASTIX = 444 112 | Magma_ILU = 445 113 | Magma_ICC = 446 114 | Magma_AILU = 447 115 | Magma_AICC = 448 116 | Magma_BAITER = 449 117 | Magma_LOBPCG = 450 118 | Magma_NONE = 451 119 | Magma_FUNCTION = 452 120 | Magma_IDR = 453 121 | Magma_PIDR = 454 122 | Magma_CGS = 455 123 | Magma_PCGS = 456 124 | Magma_CGSMERGE = 457 125 | Magma_PCGSMERGE = 458 126 | Magma_TFQMR = 459 127 | Magma_PTFQMR = 460 128 | Magma_TFQMRMERGE = 461 129 | Magma_PTFQMRMERGE = 462 130 | Magma_QMR = 463 131 | Magma_PQMR = 464 132 | Magma_QMRMERGE = 465 133 | Magma_PQMRMERGE = 466 134 | Magma_BOMBARD = 490 135 | Magma_BOMBARDMERGE = 491 136 | Magma_PCGMERGE = 492 137 | Magma_BAITERO = 493 138 | Magma_IDRMERGE = 494 139 | Magma_PBICGSTABMERGE = 495 140 | Magma_AICT = 496 141 | Magma_CUSTOMIC = 497 142 | Magma_CUSTOMILU = 498 143 | Magma_PIDRMERGE = 499 144 | Magma_BICG = 500 145 | Magma_BICGMERGE = 501 146 | Magma_PBICG = 502 147 | Magma_PBICGMERGE = 503 148 | Magma_LSQR = 504 149 | 150 | 151 | class magma_location_t: 152 | Magma_CPU = 571 153 | Magma_DEV = 572 154 | 155 | 156 | class magma_ortho_t: 157 | Magma_CGSO = 561 158 | Magma_FUSED_CGSO = 562 159 | Magma_MGSO = 563 160 | 161 | 162 | class magma_scale_t: 163 | Magma_NOSCALE = 511 164 | Magma_UNITROW = 512 165 | Magma_UNITDIAG = 513 166 | 167 | 168 | class magma_precision: 169 | Magma_DCOMPLEX = 501 170 | Magma_FCOMPLEX = 502 171 | Magma_DOUBLE = 503 172 | Magma_FLOAT = 504 173 | 174 | 175 | class _uval(ctypes.Union): 176 | _fields_ = [('val', ctypes.c_void_p), 177 | ('dval', ctypes.c_void_p)] 178 | 179 | 180 | class _udiag(ctypes.Union): 181 | _fields_ = [('diag', ctypes.c_void_p), 182 | ('ddiag', ctypes.c_void_p)] 183 | 184 | 185 | class _urow(ctypes.Union): 186 | _fields_ = [('row', ctypes.c_void_p), 187 | ('drow', ctypes.c_void_p)] 188 | 189 | 190 | class _urowidx(ctypes.Union): 191 | _fields_ = [('rowidx', ctypes.c_void_p), 192 | ('drowidx', ctypes.c_void_p)] 193 | 194 | 195 | class _ucol(ctypes.Union): 196 | _fields_ = [('col', ctypes.c_void_p), 197 | ('dcol', ctypes.c_void_p)] 198 | 199 | 200 | class _ulist(ctypes.Union): 201 | _fields_ = [('list', ctypes.c_void_p), 202 | ('dlist', ctypes.c_void_p)] 203 | 204 | 205 | class magma_t_matrix(ctypes.Structure): 206 | _anonymous_ = ('uval','udiag','urow','urowidx','ucol','ulist') 207 | _fields_ = [('storage_type', ctypes.c_int), 208 | ('memory_location', ctypes.c_int), 209 | ('sym', ctypes.c_int), 210 | ('diagorder_type', ctypes.c_int), 211 | ('fill_mode', ctypes.c_int), 212 | ('num_rows', ctypes.c_int), 213 | ('num_cols', ctypes.c_int), 214 | ('nnz', ctypes.c_int), 215 | ('max_nnz_row', ctypes.c_int), 216 | ('diameter', ctypes.c_int), 217 | ('true_nnz', ctypes.c_int), 218 | ('uval', _uval), 219 | ('udiag', _udiag), 220 | ('urow', _urow), 221 | ('urowidx', _urowidx), 222 | ('ucol', _ucol), 223 | ('ulist', _ulist), 224 | ('blockinfo', ctypes.c_void_p), 225 | ('blocksize', ctypes.c_int), 226 | ('numblocks', ctypes.c_int), 227 | ('alignment', ctypes.c_int), 228 | ('major', ctypes.c_int), 229 | ('ld', ctypes.c_int) 230 | ] 231 | 232 | def __init__(self, storage_type): 233 | super(magma_t_matrix, self).__init__(storage_type) 234 | 235 | 236 | # For the python implementation the c_void_p in the structures can point to any data type. 237 | # Thus, it is not necessary to implement the matrix structures for other data types explicitly. 238 | # We introduce magma_t_matrix and magma_t_vector, where the 't' stands for the corresponding 239 | # type which can be z, c, d or s. 240 | magma_z_matrix = magma_t_matrix 241 | magma_c_matrix = magma_t_matrix 242 | magma_d_matrix = magma_t_matrix 243 | magma_s_matrix = magma_t_matrix 244 | 245 | magma_z_vector = magma_z_matrix 246 | magma_c_vector = magma_c_matrix 247 | magma_d_vector = magma_d_matrix 248 | magma_s_vector = magma_s_matrix 249 | 250 | 251 | class magma_z_solver_par(ctypes.Structure): 252 | _fields_ = [('solver', ctypes.c_int), # solver type 253 | ('version', ctypes.c_int), # sometimes there are different versions 254 | ('atol', ctypes.c_double), # absolute residual stopping criterion 255 | ('rtol', ctypes.c_double), # relative residual stopping criterion 256 | ('maxiter', ctypes.c_int), # upper iteration limit 257 | ('restart', ctypes.c_int), # for GMRES 258 | ('ortho', ctypes.c_int), # for GMRES 259 | ('numiter', ctypes.c_int), # feedback: number of needed iterations 260 | ('spmv_count', ctypes.c_int), # feedback: number of needed SpMV - can be different to iteration count 261 | ('init_res', ctypes.c_double), # feedback: initial residual 262 | ('final_res', ctypes.c_double), # feedback: final residual 263 | ('iter_res', ctypes.c_double), # feedback: iteratively computed residual 264 | ('runtime', real_Double_t), # feedback: runtime needed 265 | ('res_vec', ctypes.POINTER(real_Double_t)), # feedback: array containing residuals 266 | ('timing', ctypes.POINTER(real_Double_t)), # feedback: detailed timing 267 | ('verbose', ctypes.c_int), # print residual ever 'verbose' iterations 268 | ('num_eigenvalues', ctypes.c_int), # number of EV for eigensolvers 269 | ('ev_length', ctypes.c_int), # needed for framework 270 | ('eigenvalues', ctypes.POINTER(ctypes.c_double)), # feedback: array containing eigenvalues 271 | ('eigenvectors', ctypes.POINTER(cuda.cuDoubleComplex)), # feedback: array containing eigenvectors 272 | ('info', ctypes.c_int) # feedback: did the solver converge etc 273 | ] 274 | 275 | 276 | class magma_c_solver_par(ctypes.Structure): 277 | _fields_ = [('solver', ctypes.c_int), # solver type 278 | ('version', ctypes.c_int), # sometimes there are different versions 279 | ('atol', ctypes.c_float), # absolute residual stopping criterion 280 | ('rtol', ctypes.c_float), # relative residual stopping criterion 281 | ('maxiter', ctypes.c_int), # upper iteration limit 282 | ('restart', ctypes.c_int), # for GMRES 283 | ('ortho', ctypes.c_int), # for GMRES 284 | ('numiter', ctypes.c_int), # feedback: number of needed iterations 285 | ('spmv_count', ctypes.c_int), # feedback: number of needed SpMV - can be different to iteration count 286 | ('init_res', ctypes.c_float), # feedback: initial residual 287 | ('final_res', ctypes.c_float), # feedback: final residual 288 | ('iter_res', ctypes.c_float), # feedback: iteratively computed residual 289 | ('runtime', real_Double_t), # feedback: runtime needed 290 | ('res_vec', ctypes.POINTER(real_Double_t)), # feedback: array containing residuals 291 | ('timing', ctypes.POINTER(real_Double_t)), # feedback: detailed timing 292 | ('verbose', ctypes.c_int), # print residual ever 'verbose' iterations 293 | ('num_eigenvalues', ctypes.c_int), # number of EV for eigensolvers 294 | ('ev_length', ctypes.c_int), # needed for framework 295 | ('eigenvalues', ctypes.POINTER(ctypes.c_float)), # feedback: array containing eigenvalues 296 | ('eigenvectors', ctypes.POINTER(cuda.cuFloatComplex)), # feedback: array containing eigenvectors 297 | ('info', ctypes.c_int) # feedback: did the solver converge etc 298 | ] 299 | 300 | 301 | class magma_d_solver_par(ctypes.Structure): 302 | _fields_ = [('solver', ctypes.c_int), # solver type 303 | ('version', ctypes.c_int), # sometimes there are different versions 304 | ('atol', ctypes.c_double), # absolute residual stopping criterion 305 | ('rtol', ctypes.c_double), # relative residual stopping criterion 306 | ('maxiter', ctypes.c_int), # upper iteration limit 307 | ('restart', ctypes.c_int), # for GMRES 308 | ('ortho', ctypes.c_int), # for GMRES 309 | ('numiter', ctypes.c_int), # feedback: number of needed iterations 310 | ('spmv_count', ctypes.c_int), # feedback: number of needed SpMV - can be different to iteration count 311 | ('init_res', ctypes.c_double), # feedback: initial residual 312 | ('final_res', ctypes.c_double), # feedback: final residual 313 | ('iter_res', ctypes.c_double), # feedback: iteratively computed residual 314 | ('runtime', real_Double_t), # feedback: runtime needed 315 | ('res_vec', ctypes.POINTER(real_Double_t)), # feedback: array containing residuals 316 | ('timing', ctypes.POINTER(real_Double_t)), # feedback: detailed timing 317 | ('verbose', ctypes.c_int), # print residual ever 'verbose' iterations 318 | ('num_eigenvalues', ctypes.c_int), # number of EV for eigensolvers 319 | ('ev_length', ctypes.c_int), # needed for framework 320 | ('eigenvalues', ctypes.POINTER(ctypes.c_double)), # feedback: array containing eigenvalues 321 | ('eigenvectors', ctypes.POINTER(cuda.cuDoubleComplex)), # feedback: array containing eigenvectors 322 | ('info', ctypes.c_int) # feedback: did the solver converge etc 323 | ] 324 | 325 | 326 | class magma_s_solver_par(ctypes.Structure): 327 | _fields_ = [('solver', ctypes.c_int), # solver type 328 | ('version', ctypes.c_int), # sometimes there are different versions 329 | ('atol', ctypes.c_float), # absolute residual stopping criterion 330 | ('rtol', ctypes.c_float), # relative residual stopping criterion 331 | ('maxiter', ctypes.c_int), # upper iteration limit 332 | ('restart', ctypes.c_int), # for GMRES 333 | ('ortho', ctypes.c_int), # for GMRES 334 | ('numiter', ctypes.c_int), # feedback: number of needed iterations 335 | ('spmv_count', ctypes.c_int), # feedback: number of needed SpMV - can be different to iteration count 336 | ('init_res', ctypes.c_float), # feedback: initial residual 337 | ('final_res', ctypes.c_float), # feedback: final residual 338 | ('iter_res', ctypes.c_float), # feedback: iteratively computed residual 339 | ('runtime', real_Double_t), # feedback: runtime needed 340 | ('res_vec', ctypes.POINTER(real_Double_t)), # feedback: array containing residuals 341 | ('timing', ctypes.POINTER(real_Double_t)), # feedback: detailed timing 342 | ('verbose', ctypes.c_int), # print residual ever 'verbose' iterations 343 | ('num_eigenvalues', ctypes.c_int), # number of EV for eigensolvers 344 | ('ev_length', ctypes.c_int), # needed for framework 345 | ('eigenvalues', ctypes.POINTER(ctypes.c_float)), # feedback: array containing eigenvalues 346 | ('eigenvectors', ctypes.POINTER(cuda.cuFloatComplex)), # feedback: array containing eigenvectors 347 | ('info', ctypes.c_int) # feedback: did the solver converge etc 348 | ] 349 | 350 | 351 | class magma_z_preconditioner(ctypes.Structure): 352 | _fields_ = [('solver', ctypes.c_int), 353 | ('levels', ctypes.c_int), 354 | ('sweeps', ctypes.c_int), 355 | ('format', ctypes.c_int), 356 | ('atol', ctypes.c_double), 357 | ('rtol', ctypes.c_double), 358 | ('maxiter', ctypes.c_int), 359 | ('restart', ctypes.c_int), 360 | ('numiter', ctypes.c_int), 361 | ('spmv_count', ctypes.c_int), 362 | ('init_res', ctypes.c_double), 363 | ('final_res', ctypes.c_double), 364 | ('runtime', real_Double_t), # feedback: preconditioner runtime needed 365 | ('setuptime', real_Double_t), # feedback: preconditioner setup time needed 366 | ('M', magma_z_matrix), 367 | ('L', magma_z_matrix), 368 | ('LT', magma_z_matrix), 369 | ('U', magma_z_matrix), 370 | ('UT', magma_z_matrix), 371 | ('LD', magma_z_matrix), 372 | ('UD', magma_z_matrix), 373 | ('d', magma_z_matrix), 374 | ('d2', magma_z_matrix), 375 | ('work1', magma_z_matrix), 376 | ('work2', magma_z_matrix), 377 | ('int_array_1', ctypes.POINTER(ctypes.c_int)), 378 | ('int_array_2', ctypes.POINTER(ctypes.c_int)), 379 | ('cuinfo', cusparseSolveAnalysisInfo_t), 380 | ('cuinfoL', cusparseSolveAnalysisInfo_t), 381 | ('cuinfoLT', cusparseSolveAnalysisInfo_t), 382 | ('cuinfoU', cusparseSolveAnalysisInfo_t), 383 | ('cuinfoUT', cusparseSolveAnalysisInfo_t) 384 | # TODO: 385 | #if defined(HAVE_PASTIX) 386 | # pastix_data_t* pastix_data; 387 | # magma_int_t* iparm; 388 | # double* dparm; 389 | #endif 390 | ] 391 | 392 | 393 | class magma_c_preconditioner(ctypes.Structure): 394 | _fields_ = [('solver', ctypes.c_int), 395 | ('levels', ctypes.c_int), 396 | ('sweeps', ctypes.c_int), 397 | ('format', ctypes.c_int), 398 | ('atol', ctypes.c_float), 399 | ('rtol', ctypes.c_float), 400 | ('maxiter', ctypes.c_int), 401 | ('restart', ctypes.c_int), 402 | ('numiter', ctypes.c_int), 403 | ('spmv_count', ctypes.c_int), 404 | ('init_res', ctypes.c_float), 405 | ('final_res', ctypes.c_float), 406 | ('runtime', real_Double_t), # feedback: preconditioner runtime needed 407 | ('setuptime', real_Double_t), # feedback: preconditioner setup time needed 408 | ('M', magma_c_matrix), 409 | ('L', magma_c_matrix), 410 | ('LT', magma_c_matrix), 411 | ('U', magma_c_matrix), 412 | ('UT', magma_c_matrix), 413 | ('LD', magma_c_matrix), 414 | ('UD', magma_c_matrix), 415 | ('d', magma_c_matrix), 416 | ('d2', magma_c_matrix), 417 | ('work1', magma_c_matrix), 418 | ('work2', magma_c_matrix), 419 | ('int_array_1', ctypes.POINTER(ctypes.c_int)), 420 | ('int_array_2', ctypes.POINTER(ctypes.c_int)), 421 | ('cuinfo', cusparseSolveAnalysisInfo_t), 422 | ('cuinfoL', cusparseSolveAnalysisInfo_t), 423 | ('cuinfoLT', cusparseSolveAnalysisInfo_t), 424 | ('cuinfoU', cusparseSolveAnalysisInfo_t), 425 | ('cuinfoUT', cusparseSolveAnalysisInfo_t) 426 | # TODO: 427 | #if defined(HAVE_PASTIX) 428 | # pastix_data_t* pastix_data; 429 | # magma_int_t* iparm; 430 | # float* dparm; 431 | #endif 432 | ] 433 | 434 | class magma_d_preconditioner(ctypes.Structure): 435 | _fields_ = [('solver', ctypes.c_int), 436 | ('levels', ctypes.c_int), 437 | ('sweeps', ctypes.c_int), 438 | ('format', ctypes.c_int), 439 | ('atol', ctypes.c_double), 440 | ('rtol', ctypes.c_double), 441 | ('maxiter', ctypes.c_int), 442 | ('restart', ctypes.c_int), 443 | ('numiter', ctypes.c_int), 444 | ('spmv_count', ctypes.c_int), 445 | ('init_res', ctypes.c_double), 446 | ('final_res', ctypes.c_double), 447 | ('runtime', real_Double_t), # feedback: preconditioner runtime needed 448 | ('setuptime', real_Double_t), # feedback: preconditioner setup time needed 449 | ('M', magma_d_matrix), 450 | ('L', magma_d_matrix), 451 | ('LT', magma_d_matrix), 452 | ('U', magma_d_matrix), 453 | ('UT', magma_d_matrix), 454 | ('LD', magma_d_matrix), 455 | ('UD', magma_d_matrix), 456 | ('d', magma_d_matrix), 457 | ('d2', magma_d_matrix), 458 | ('work1', magma_d_matrix), 459 | ('work2', magma_d_matrix), 460 | ('int_array_1', ctypes.POINTER(ctypes.c_int)), 461 | ('int_array_2', ctypes.POINTER(ctypes.c_int)), 462 | ('cuinfo', cusparseSolveAnalysisInfo_t), 463 | ('cuinfoL', cusparseSolveAnalysisInfo_t), 464 | ('cuinfoLT', cusparseSolveAnalysisInfo_t), 465 | ('cuinfoU', cusparseSolveAnalysisInfo_t), 466 | ('cuinfoUT', cusparseSolveAnalysisInfo_t) 467 | # TODO: 468 | #if defined(HAVE_PASTIX) 469 | # pastix_data_t* pastix_data; 470 | # magma_int_t* iparm; 471 | # double* dparm; 472 | #endif 473 | ] 474 | 475 | class magma_s_preconditioner(ctypes.Structure): 476 | _fields_ = [('solver', ctypes.c_int), 477 | ('levels', ctypes.c_int), 478 | ('sweeps', ctypes.c_int), 479 | ('format', ctypes.c_int), 480 | ('atol', ctypes.c_float), 481 | ('rtol', ctypes.c_float), 482 | ('maxiter', ctypes.c_int), 483 | ('restart', ctypes.c_int), 484 | ('numiter', ctypes.c_int), 485 | ('spmv_count', ctypes.c_int), 486 | ('init_res', ctypes.c_float), 487 | ('final_res', ctypes.c_float), 488 | ('runtime', real_Double_t), # feedback: preconditioner runtime needed 489 | ('setuptime', real_Double_t), # feedback: preconditioner setup time needed 490 | ('M', magma_s_matrix), 491 | ('L', magma_s_matrix), 492 | ('LT', magma_s_matrix), 493 | ('U', magma_s_matrix), 494 | ('UT', magma_s_matrix), 495 | ('LD', magma_s_matrix), 496 | ('UD', magma_s_matrix), 497 | ('d', magma_s_matrix), 498 | ('d2', magma_s_matrix), 499 | ('work1', magma_s_matrix), 500 | ('work2', magma_s_matrix), 501 | ('int_array_1', ctypes.POINTER(ctypes.c_int)), 502 | ('int_array_2', ctypes.POINTER(ctypes.c_int)), 503 | ('cuinfo', cusparseSolveAnalysisInfo_t), 504 | ('cuinfoL', cusparseSolveAnalysisInfo_t), 505 | ('cuinfoLT', cusparseSolveAnalysisInfo_t), 506 | ('cuinfoU', cusparseSolveAnalysisInfo_t), 507 | ('cuinfoUT', cusparseSolveAnalysisInfo_t) 508 | # TODO: 509 | #if defined(HAVE_PASTIX) 510 | # pastix_data_t* pastix_data; 511 | # magma_int_t* iparm; 512 | # float* dparm; 513 | #endif 514 | ] 515 | 516 | 517 | class magma_zopts(ctypes.Structure): 518 | _fields_ = [('solver_par', magma_z_solver_par), 519 | ('precond_par', magma_z_preconditioner), 520 | ('input_format', ctypes.c_int), 521 | ('blocksize', ctypes.c_int), 522 | ('alignment', ctypes.c_int), 523 | ('output_format', ctypes.c_int), 524 | ('input_location', ctypes.c_int), 525 | ('output_location', ctypes.c_int), 526 | ('scaling', ctypes.c_int) 527 | ] 528 | 529 | 530 | class magma_copts(ctypes.Structure): 531 | _fields_ = [('solver_par', magma_c_solver_par), 532 | ('precond_par', magma_c_preconditioner), 533 | ('input_format', ctypes.c_int), 534 | ('blocksize', ctypes.c_int), 535 | ('alignment', ctypes.c_int), 536 | ('output_format', ctypes.c_int), 537 | ('input_location', ctypes.c_int), 538 | ('output_location', ctypes.c_int), 539 | ('scaling', ctypes.c_int) 540 | ] 541 | 542 | 543 | class magma_dopts(ctypes.Structure): 544 | _fields_ = [('solver_par', magma_d_solver_par), 545 | ('precond_par', magma_d_preconditioner), 546 | ('input_format', ctypes.c_int), 547 | ('blocksize', ctypes.c_int), 548 | ('alignment', ctypes.c_int), 549 | ('output_format', ctypes.c_int), 550 | ('input_location', ctypes.c_int), 551 | ('output_location', ctypes.c_int), 552 | ('scaling', ctypes.c_int) 553 | ] 554 | 555 | 556 | class magma_sopts(ctypes.Structure): 557 | _fields_ = [('solver_par', magma_s_solver_par), 558 | ('precond_par', magma_s_preconditioner), 559 | ('input_format', ctypes.c_int), 560 | ('blocksize', ctypes.c_int), 561 | ('alignment', ctypes.c_int), 562 | ('output_format', ctypes.c_int), 563 | ('input_location', ctypes.c_int), 564 | ('output_location', ctypes.c_int), 565 | ('scaling', ctypes.c_int) 566 | ] 567 | 568 | 569 | def magma_zopts_default(): 570 | opts = magma_zopts() 571 | opts.solver_par = magma_z_solver_par_default() 572 | opts.precond_par = magma_z_preconditioner_default() 573 | opts.input_format = magma_storage_t.Magma_CSR 574 | opts.blocksize = 32 575 | opts.alignment = 1 576 | opts.output_format = magma_storage_t.Magma_CUCSR 577 | opts.input_location = magma_location_t.Magma_CPU 578 | opts.output_location = magma_location_t.Magma_CPU 579 | opts.scaling = magma_scale_t.Magma_NOSCALE 580 | 581 | return opts 582 | 583 | 584 | def magma_copts_default(): 585 | opts = magma_copts() 586 | opts.solver_par = magma_c_solver_par_default() 587 | opts.precond_par = magma_c_preconditioner_default() 588 | opts.input_format = magma_storage_t.Magma_CSR 589 | opts.blocksize = 32 590 | opts.alignment = 1 591 | opts.output_format = magma_storage_t.Magma_CUCSR 592 | opts.input_location = magma_location_t.Magma_CPU 593 | opts.output_location = magma_location_t.Magma_CPU 594 | opts.scaling = magma_scale_t.Magma_NOSCALE 595 | 596 | return opts 597 | 598 | 599 | def magma_dopts_default(): 600 | opts = magma_dopts() 601 | opts.solver_par = magma_d_solver_par_default() 602 | opts.precond_par = magma_d_preconditioner_default() 603 | opts.input_format = magma_storage_t.Magma_CSR 604 | opts.blocksize = 32 605 | opts.alignment = 1 606 | opts.output_format = magma_storage_t.Magma_CUCSR 607 | opts.input_location = magma_location_t.Magma_CPU 608 | opts.output_location = magma_location_t.Magma_CPU 609 | opts.scaling = magma_scale_t.Magma_NOSCALE 610 | 611 | return opts 612 | 613 | 614 | def magma_sopts_default(): 615 | opts = magma_sopts() 616 | opts.solver_par = magma_s_solver_par_default() 617 | opts.precond_par = magma_s_preconditioner_default() 618 | opts.input_format = magma_storage_t.Magma_CSR 619 | opts.blocksize = 32 620 | opts.alignment = 1 621 | opts.output_format = magma_storage_t.Magma_CUCSR 622 | opts.input_location = magma_location_t.Magma_CPU 623 | opts.output_location = magma_location_t.Magma_CPU 624 | opts.scaling = magma_scale_t.Magma_NOSCALE 625 | 626 | return opts 627 | 628 | 629 | def magma_z_solver_par_default(): 630 | solver_par = magma_z_solver_par() 631 | solver_par.solver = magma_solver_type.Magma_CGMERGE 632 | solver_par.version = 0 633 | solver_par.atol = 1e-16 634 | solver_par.rtol = 1e-10 635 | solver_par.maxiter = 1000 636 | solver_par.restart = 50 637 | solver_par.verbose = 0 638 | solver_par.num_eigenvalues = 0 639 | 640 | return solver_par 641 | 642 | 643 | def magma_c_solver_par_default(): 644 | solver_par = magma_c_solver_par() 645 | solver_par.solver = magma_solver_type.Magma_CGMERGE 646 | solver_par.version = 0 647 | solver_par.atol = 1e-8 648 | solver_par.rtol = 1e-5 649 | solver_par.maxiter = 1000 650 | solver_par.restart = 50 651 | solver_par.verbose = 0 652 | solver_par.num_eigenvalues = 0 653 | 654 | return solver_par 655 | 656 | 657 | def magma_d_solver_par_default(): 658 | solver_par = magma_d_solver_par() 659 | solver_par.solver = magma_solver_type.Magma_CGMERGE 660 | solver_par.version = 0 661 | solver_par.atol = 1e-16 662 | solver_par.rtol = 1e-10 663 | solver_par.maxiter = 1000 664 | solver_par.restart = 50 665 | solver_par.verbose = 0 666 | solver_par.num_eigenvalues = 0 667 | 668 | return solver_par 669 | 670 | 671 | def magma_s_solver_par_default(): 672 | solver_par = magma_s_solver_par() 673 | solver_par.solver = magma_solver_type.Magma_CGMERGE 674 | solver_par.version = 0 675 | solver_par.atol = 1e-8 676 | solver_par.rtol = 1e-5 677 | solver_par.maxiter = 1000 678 | solver_par.restart = 50 679 | solver_par.verbose = 0 680 | solver_par.num_eigenvalues = 0 681 | 682 | return solver_par 683 | 684 | 685 | def magma_z_preconditioner_default(): 686 | precond_par = magma_z_preconditioner() 687 | precond_par.solver = magma_solver_type.Magma_NONE 688 | precond_par.levels = 0 689 | precond_par.sweeps = 5 690 | precond_par.format = magma_precision.Magma_DCOMPLEX 691 | precond_par.atol = 1e-16 692 | precond_par.rtol = 1e-10 693 | precond_par.maxiter = 100 694 | precond_par.restart = 10 695 | 696 | return precond_par 697 | 698 | 699 | def magma_c_preconditioner_default(): 700 | precond_par = magma_c_preconditioner() 701 | precond_par.solver = magma_solver_type.Magma_NONE 702 | precond_par.levels = 0 703 | precond_par.sweeps = 5 704 | precond_par.format = magma_precision.Magma_FCOMPLEX 705 | precond_par.atol = 1e-8 706 | precond_par.rtol = 1e-5 707 | precond_par.maxiter = 100 708 | precond_par.restart = 10 709 | 710 | return precond_par 711 | 712 | 713 | def magma_d_preconditioner_default(): 714 | precond_par = magma_d_preconditioner() 715 | precond_par.solver = magma_solver_type.Magma_NONE 716 | precond_par.levels = 0 717 | precond_par.sweeps = 5 718 | precond_par.format = magma_precision.Magma_DCOMPLEX 719 | precond_par.atol = 1e-16 720 | precond_par.rtol = 1e-10 721 | precond_par.maxiter = 100 722 | precond_par.restart = 10 723 | 724 | return precond_par 725 | 726 | 727 | def magma_s_preconditioner_default(): 728 | precond_par = magma_s_preconditioner() 729 | precond_par.solver = magma_solver_type.Magma_NONE 730 | precond_par.levels = 0 731 | precond_par.sweeps = 5 732 | precond_par.format = magma_precision.Magma_FCOMPLEX 733 | precond_par.atol = 1e-8 734 | precond_par.rtol = 1e-5 735 | precond_par.maxiter = 100 736 | precond_par.restart = 10 737 | 738 | return precond_par 739 | 740 | 741 | class magma_queue(ctypes.Structure): 742 | _fields_ = [('own__', ctypes.c_int), 743 | ('device__', ctypes.c_int), 744 | #if HAVE_CUBLAS 745 | ('stream__', ctypes.c_void_p), # cudaStream_t 746 | ('cublas__', ctypes.c_void_p), # cusparseHandle_t 747 | ('cusparse__', ctypes.c_void_p) # cusparseHandle_t 748 | ] 749 | 750 | def __int__(self): 751 | self.stream__.value = None 752 | self.cublas__.value = None 753 | self.cusparse__.value = None 754 | 755 | 756 | """ 757 | magma_queue_t is a pointer to a magma_queue structure. 758 | """ 759 | magma_queue_t = ctypes.POINTER(magma_queue) 760 | 761 | 762 | # Load MAGMA library: 763 | if 'linux' in sys.platform: 764 | _libmagma_libname_list = ['libmagma.so'] 765 | elif sys.platform == 'darwin': 766 | _libmagma_libname_list = ['libmagma.so', 'libmagma.dylib'] 767 | elif sys.platform == 'win32': 768 | _libmagma_libname_list = ['magma.dll'] 769 | else: 770 | raise RuntimeError('unsupported platform') 771 | 772 | _load_err = '' 773 | for _lib in _libmagma_libname_list: 774 | try: 775 | #_libmagma = ctypes.cdll.LoadLibrary(_lib) 776 | _libmagma = ctypes.CDLL(_lib, ctypes.RTLD_GLOBAL) 777 | except OSError: 778 | _load_err += ('' if _load_err == '' else ', ') + _lib 779 | else: 780 | _load_err = '' 781 | break 782 | if _load_err: 783 | raise OSError('%s not found' % _load_err) 784 | 785 | # Load MAGMA sparse library: 786 | if 'linux' in sys.platform: 787 | _libmagma_sparse_libname_list = ['libmagma_sparse.so'] 788 | elif sys.platform == 'darwin': 789 | _libmagma_sparse_libname_list = ['libmagma_sparse.so', 'libmagma_sparse.dylib'] 790 | elif sys.platform == 'win32': 791 | _libmagma_sparse_libname_list = ['magma_sparse.dll'] 792 | else: 793 | raise RuntimeError('unsupported platform') 794 | 795 | _load_err = '' 796 | for _lib in _libmagma_sparse_libname_list: 797 | try: 798 | _libmagma_sparse = ctypes.CDLL(_lib, ctypes.RTLD_GLOBAL) 799 | except OSError: 800 | _load_err += ('' if _load_err == '' else ', ') + _lib 801 | else: 802 | _load_err = '' 803 | break 804 | if _load_err: 805 | raise OSError('%s not found' % _load_err) 806 | 807 | # Exceptions corresponding to various MAGMA errors: 808 | _libmagma.magma_strerror.restype = ctypes.c_char_p 809 | _libmagma.magma_strerror.argtypes = [ctypes.c_int] 810 | # MAGMA below 1.4.0 uses "L" and "U" to select upper/lower triangular 811 | # matrices, MAGMA 1.5+ uses numeric constants. This dict will be filled 812 | # in magma_init() and will convert between the two modes accordingly 813 | _uplo_conversion = {} 814 | def magma_strerror(error): 815 | """ 816 | Return string corresponding to specified MAGMA error code. 817 | """ 818 | 819 | return _libmagma.magma_strerror(error) 820 | 821 | 822 | class MagmaError(Exception): 823 | def __init__(self, status, info=None): 824 | self._status = status 825 | self._info = info 826 | errstr = "%s (Code: %d)" % (magma_strerror(status), status) 827 | super(MagmaError,self).__init__(errstr) 828 | 829 | 830 | def magmaCheckStatus(status): 831 | """ 832 | Raise an exception corresponding to the specified MAGMA status code. 833 | """ 834 | 835 | if status != 0: 836 | raise MagmaError(status) 837 | 838 | # Utility functions: 839 | _libmagma.magma_version.argtypes = [ctypes.c_void_p, 840 | ctypes.c_void_p, ctypes.c_void_p] 841 | 842 | 843 | def magma_version(): 844 | """ 845 | Get MAGMA version. 846 | """ 847 | majv = ctypes.c_int() 848 | minv = ctypes.c_int() 849 | micv = ctypes.c_int() 850 | _libmagma.magma_version(ctypes.byref(majv), 851 | ctypes.byref(minv), ctypes.byref(micv)) 852 | return (majv.value, minv.value, micv.value) 853 | 854 | 855 | _libmagma.magma_uplo_const.restype = ctypes.c_int 856 | _libmagma.magma_uplo_const.argtypes = [ctypes.c_char] 857 | _libmagma.magma_init.restype = int 858 | def magma_init(): 859 | """ 860 | Initialize MAGMA. 861 | """ 862 | 863 | global _uplo_conversion 864 | status = _libmagma.magma_init() 865 | magmaCheckStatus(status) 866 | v = magma_version() 867 | if v >= (1, 5, 0): 868 | _uplo_conversion.update({"L": _libmagma.magma_uplo_const(b"L"), 869 | "l": _libmagma.magma_uplo_const(b"l"), 870 | "U": _libmagma.magma_uplo_const(b"U"), 871 | "u": _libmagma.magma_uplo_const(b"u")}) 872 | else: 873 | _uplo_conversion.update({"L": "L", "l": "l", "U": "u", "u": "u"}) 874 | 875 | _libmagma.magma_finalize.restype = int 876 | def magma_finalize(): 877 | """ 878 | Finalize MAGMA. 879 | """ 880 | 881 | status = _libmagma.magma_finalize() 882 | magmaCheckStatus(status) 883 | 884 | _libmagma.magma_getdevice_arch.restype = int 885 | def magma_getdevice_arch(): 886 | """ 887 | Get device architecture. 888 | """ 889 | 890 | return _libmagma.magma_getdevice_arch() 891 | 892 | _libmagma.magma_getdevice.argtypes = [ctypes.c_void_p] 893 | def magma_getdevice(): 894 | """ 895 | Get current device used by MAGMA. 896 | """ 897 | 898 | dev = ctypes.c_int() 899 | _libmagma.magma_getdevice(ctypes.byref(dev)) 900 | return dev.value 901 | 902 | _libmagma.magma_setdevice.argtypes = [ctypes.c_int] 903 | def magma_setdevice(dev): 904 | """ 905 | Get current device used by MAGMA. 906 | """ 907 | 908 | _libmagma.magma_setdevice(dev) 909 | 910 | def magma_device_sync(): 911 | """ 912 | Synchronize device used by MAGMA. 913 | """ 914 | 915 | _libmagma.magma_device_sync() 916 | 917 | # TODO: test this function 918 | # _libmagma.magma_queue_sync_internal.restype = int 919 | # _libmagma.magma_queue_sync_internal.argtypes = [magma_queue_t, 920 | # ctypes.c_char_p, 921 | # ctypes.c_char_p, 922 | # ctypes.c_int] 923 | # def magma_queue_sync_internal(queue, func, outfile, line): 924 | # """ 925 | # Internally calls cudaStreamSynchronize. 926 | # """ 927 | # 928 | # _libmagma.magma_queue_sync_internal(queue, func, outfile, int(line)) 929 | 930 | 931 | # BLAS routines 932 | 933 | # ISAMAX, IDAMAX, ICAMAX, IZAMAX 934 | _libmagma.magma_isamax.restype = int 935 | _libmagma.magma_isamax.argtypes = [ctypes.c_int, 936 | ctypes.c_void_p, 937 | ctypes.c_int] 938 | def magma_isamax(n, dx, incx): 939 | """ 940 | Index of maximum magnitude element. 941 | """ 942 | 943 | return _libmagma.magma_isamax(n, int(dx), incx) 944 | 945 | _libmagma.magma_idamax.restype = int 946 | _libmagma.magma_idamax.argtypes = [ctypes.c_int, 947 | ctypes.c_void_p, 948 | ctypes.c_int] 949 | def magma_idamax(n, dx, incx): 950 | """ 951 | Index of maximum magnitude element. 952 | """ 953 | 954 | return _libmagma.magma_idamax(n, int(dx), incx) 955 | 956 | _libmagma.magma_icamax.restype = int 957 | _libmagma.magma_icamax.argtypes = [ctypes.c_int, 958 | ctypes.c_void_p, 959 | ctypes.c_int] 960 | def magma_icamax(n, dx, incx): 961 | """ 962 | Index of maximum magnitude element. 963 | """ 964 | 965 | return _libmagma.magma_icamax(n, int(dx), incx) 966 | 967 | _libmagma.magma_izamax.restype = int 968 | _libmagma.magma_izamax.argtypes = [ctypes.c_int, 969 | ctypes.c_void_p, 970 | ctypes.c_int] 971 | def magma_izamax(n, dx, incx): 972 | """ 973 | Index of maximum magnitude element. 974 | """ 975 | 976 | return _libmagma.magma_izamax(n, int(dx), incx) 977 | 978 | # ISAMIN, IDAMIN, ICAMIN, IZAMIN 979 | _libmagma.magma_isamin.restype = int 980 | _libmagma.magma_isamin.argtypes = [ctypes.c_int, 981 | ctypes.c_void_p, 982 | ctypes.c_int] 983 | def magma_isamin(n, dx, incx): 984 | """ 985 | Index of minimum magnitude element. 986 | """ 987 | 988 | return _libmagma.magma_isamin(n, int(dx), incx) 989 | 990 | _libmagma.magma_idamin.restype = int 991 | _libmagma.magma_idamin.argtypes = [ctypes.c_int, 992 | ctypes.c_void_p, 993 | ctypes.c_int] 994 | def magma_idamin(n, dx, incx): 995 | """ 996 | Index of minimum magnitude element. 997 | """ 998 | 999 | return _libmagma.magma_idamin(n, int(dx), incx) 1000 | 1001 | _libmagma.magma_icamin.restype = int 1002 | _libmagma.magma_icamin.argtypes = [ctypes.c_int, 1003 | ctypes.c_void_p, 1004 | ctypes.c_int] 1005 | def magma_icamin(n, dx, incx): 1006 | """ 1007 | Index of minimum magnitude element. 1008 | """ 1009 | 1010 | return _libmagma.magma_icamin(n, int(dx), incx) 1011 | 1012 | _libmagma.magma_izamin.restype = int 1013 | _libmagma.magma_izamin.argtypes = [ctypes.c_int, 1014 | ctypes.c_void_p, 1015 | ctypes.c_int] 1016 | def magma_izamin(n, dx, incx): 1017 | """ 1018 | Index of minimum magnitude element. 1019 | """ 1020 | 1021 | return _libmagma.magma_izamin(n, int(dx), incx) 1022 | 1023 | # SASUM, DASUM, SCASUM, DZASUM 1024 | _libmagma.magma_sasum.restype = int 1025 | _libmagma.magma_sasum.argtypes = [ctypes.c_int, 1026 | ctypes.c_void_p, 1027 | ctypes.c_int] 1028 | def magma_sasum(n, dx, incx): 1029 | """ 1030 | Sum of absolute values of vector. 1031 | """ 1032 | 1033 | return _libmagma.magma_sasum(n, int(dx), incx) 1034 | 1035 | _libmagma.magma_dasum.restype = int 1036 | _libmagma.magma_dasum.argtypes = [ctypes.c_int, 1037 | ctypes.c_void_p, 1038 | ctypes.c_int] 1039 | def magma_dasum(n, dx, incx): 1040 | """ 1041 | Sum of absolute values of vector. 1042 | """ 1043 | 1044 | return _libmagma.magma_dasum(n, int(dx), incx) 1045 | 1046 | _libmagma.magma_scasum.restype = int 1047 | _libmagma.magma_scasum.argtypes = [ctypes.c_int, 1048 | ctypes.c_void_p, 1049 | ctypes.c_int] 1050 | def magma_scasum(n, dx, incx): 1051 | """ 1052 | Sum of absolute values of vector. 1053 | """ 1054 | 1055 | return _libmagma.magma_scasum(n, int(dx), incx) 1056 | 1057 | _libmagma.magma_dzasum.restype = int 1058 | _libmagma.magma_dzasum.argtypes = [ctypes.c_int, 1059 | ctypes.c_void_p, 1060 | ctypes.c_int] 1061 | def magma_dzasum(n, dx, incx): 1062 | """ 1063 | Sum of absolute values of vector. 1064 | """ 1065 | 1066 | return _libmagma.magma_dzasum(n, int(dx), incx) 1067 | 1068 | # SAXPY, DAXPY, CAXPY, ZAXPY 1069 | _libmagma.magma_saxpy.restype = int 1070 | _libmagma.magma_saxpy.argtypes = [ctypes.c_int, 1071 | ctypes.c_float, 1072 | ctypes.c_void_p, 1073 | ctypes.c_int, 1074 | ctypes.c_void_p, 1075 | ctypes.c_int] 1076 | def magma_saxpy(n, alpha, dx, incx, dy, incy): 1077 | """ 1078 | Vector addition. 1079 | """ 1080 | 1081 | _libmagma.magma_saxpy(n, alpha, int(dx), incx, int(dy), incy) 1082 | 1083 | _libmagma.magma_daxpy.restype = int 1084 | _libmagma.magma_daxpy.argtypes = [ctypes.c_int, 1085 | ctypes.c_double, 1086 | ctypes.c_void_p, 1087 | ctypes.c_int, 1088 | ctypes.c_void_p, 1089 | ctypes.c_int] 1090 | def magma_daxpy(n, alpha, dx, incx, dy, incy): 1091 | """ 1092 | Vector addition. 1093 | """ 1094 | 1095 | _libmagma.magma_daxpy(n, alpha, int(dx), incx, int(dy), incy) 1096 | 1097 | _libmagma.magma_caxpy.restype = int 1098 | _libmagma.magma_caxpy.argtypes = [ctypes.c_int, 1099 | cuda.cuFloatComplex, 1100 | ctypes.c_void_p, 1101 | ctypes.c_int, 1102 | ctypes.c_void_p, 1103 | ctypes.c_int] 1104 | def magma_caxpy(n, alpha, dx, incx, dy, incy): 1105 | """ 1106 | Vector addition. 1107 | """ 1108 | 1109 | _libmagma.magma_caxpy(n, ctypes.byref(cuda.cuFloatComplex(alpha.real, 1110 | alpha.imag)), 1111 | int(dx), incx, int(dy), incy) 1112 | 1113 | _libmagma.magma_zaxpy.restype = int 1114 | _libmagma.magma_zaxpy.argtypes = [ctypes.c_int, 1115 | cuda.cuDoubleComplex, 1116 | ctypes.c_void_p, 1117 | ctypes.c_int, 1118 | ctypes.c_void_p, 1119 | ctypes.c_int] 1120 | def magma_zaxpy(n, alpha, dx, incx, dy, incy): 1121 | """ 1122 | Vector addition. 1123 | """ 1124 | 1125 | _libmagma.magma_zaxpy(n, ctypes.byref(cuda.cuDoubleComplex(alpha.real, 1126 | alpha.imag)), 1127 | int(dx), incx, int(dy), incy) 1128 | 1129 | # SCOPY, DCOPY, CCOPY, ZCOPY 1130 | _libmagma.magma_scopy.restype = int 1131 | _libmagma.magma_scopy.argtypes = [ctypes.c_int, 1132 | ctypes.c_void_p, 1133 | ctypes.c_int, 1134 | ctypes.c_void_p, 1135 | ctypes.c_int] 1136 | def magma_scopy(n, dx, incx, dy, incy): 1137 | """ 1138 | Vector copy. 1139 | """ 1140 | 1141 | _libmagma.magma_scopy(n, int(dx), incx, int(dy), incy) 1142 | 1143 | _libmagma.magma_dcopy.restype = int 1144 | _libmagma.magma_dcopy.argtypes = [ctypes.c_int, 1145 | ctypes.c_void_p, 1146 | ctypes.c_int, 1147 | ctypes.c_void_p, 1148 | ctypes.c_int] 1149 | def magma_dcopy(n, dx, incx, dy, incy): 1150 | """ 1151 | Vector copy. 1152 | """ 1153 | 1154 | _libmagma.magma_dcopy(n, int(dx), incx, int(dy), incy) 1155 | 1156 | _libmagma.magma_ccopy.restype = int 1157 | _libmagma.magma_ccopy.argtypes = [ctypes.c_int, 1158 | ctypes.c_void_p, 1159 | ctypes.c_int, 1160 | ctypes.c_void_p, 1161 | ctypes.c_int] 1162 | def magma_ccopy(n, dx, incx, dy, incy): 1163 | """ 1164 | Vector copy. 1165 | """ 1166 | 1167 | _libmagma.magma_ccopy(n, int(dx), incx, int(dy), incy) 1168 | 1169 | _libmagma.magma_zcopy.restype = int 1170 | _libmagma.magma_zcopy.argtypes = [ctypes.c_int, 1171 | ctypes.c_void_p, 1172 | ctypes.c_int, 1173 | ctypes.c_void_p, 1174 | ctypes.c_int] 1175 | def magma_zcopy(n, dx, incx, dy, incy): 1176 | """ 1177 | Vector copy. 1178 | """ 1179 | 1180 | _libmagma.magma_zcopy(n, int(dx), incx, int(dy), incy) 1181 | 1182 | # SDOT, DDOT, CDOTU, CDOTC, ZDOTU, ZDOTC 1183 | _libmagma.magma_sdot.restype = ctypes.c_float 1184 | _libmagma.magma_sdot.argtypes = [ctypes.c_int, 1185 | ctypes.c_void_p, 1186 | ctypes.c_int, 1187 | ctypes.c_void_p, 1188 | ctypes.c_int] 1189 | def magma_sdot(n, dx, incx, dy, incy): 1190 | """ 1191 | Vector dot product. 1192 | """ 1193 | 1194 | return _libmagma.magma_sdot(n, int(dx), incx, int(dy), incy) 1195 | 1196 | _libmagma.magma_ddot.restype = ctypes.c_double 1197 | _libmagma.magma_ddot.argtypes = [ctypes.c_int, 1198 | ctypes.c_void_p, 1199 | ctypes.c_int, 1200 | ctypes.c_void_p, 1201 | ctypes.c_int] 1202 | def magma_ddot(n, dx, incx, dy, incy): 1203 | """ 1204 | Vector dot product. 1205 | """ 1206 | 1207 | return _libmagma.magma_ddot(n, int(dx), incx, int(dy), incy) 1208 | 1209 | _libmagma.magma_cdotc.restype = cuda.cuFloatComplex 1210 | _libmagma.magma_cdotc.argtypes = [ctypes.c_int, 1211 | ctypes.c_void_p, 1212 | ctypes.c_int, 1213 | ctypes.c_void_p, 1214 | ctypes.c_int] 1215 | def magma_cdotc(n, dx, incx, dy, incy): 1216 | """ 1217 | Vector dot product. 1218 | """ 1219 | 1220 | return _libmagma.magma_cdotc(n, int(dx), incx, int(dy), incy) 1221 | 1222 | _libmagma.magma_cdotu.restype = cuda.cuFloatComplex 1223 | _libmagma.magma_cdotu.argtypes = [ctypes.c_int, 1224 | ctypes.c_void_p, 1225 | ctypes.c_int, 1226 | ctypes.c_void_p, 1227 | ctypes.c_int] 1228 | def magma_cdotu(n, dx, incx, dy, incy): 1229 | """ 1230 | Vector dot product. 1231 | """ 1232 | 1233 | return _libmagma.magma_cdotu(n, int(dx), incx, int(dy), incy) 1234 | 1235 | _libmagma.magma_zdotc.restype = cuda.cuDoubleComplex 1236 | _libmagma.magma_zdotc.argtypes = [ctypes.c_int, 1237 | ctypes.c_void_p, 1238 | ctypes.c_int, 1239 | ctypes.c_void_p, 1240 | ctypes.c_int] 1241 | def magma_zdotc(n, dx, incx, dy, incy): 1242 | """ 1243 | Vector dot product. 1244 | """ 1245 | 1246 | return _libmagma.magma_zdotc(n, int(dx), incx, int(dy), incy) 1247 | 1248 | _libmagma.magma_zdotu.restype = cuda.cuDoubleComplex 1249 | _libmagma.magma_zdotu.argtypes = [ctypes.c_int, 1250 | ctypes.c_void_p, 1251 | ctypes.c_int, 1252 | ctypes.c_void_p, 1253 | ctypes.c_int] 1254 | def magma_zdotu(n, dx, incx, dy, incy): 1255 | """ 1256 | Vector dot product. 1257 | """ 1258 | 1259 | return _libmagma.magma_zdotu(n, int(dx), incx, int(dy), incy) 1260 | 1261 | # SNRM2, DNRM2, SCNRM2, DZNRM2 1262 | _libmagma.magma_snrm2.restype = ctypes.c_float 1263 | _libmagma.magma_snrm2.argtypes = [ctypes.c_int, 1264 | ctypes.c_void_p, 1265 | ctypes.c_int] 1266 | def magma_snrm2(n, dx, incx): 1267 | """ 1268 | Euclidean norm (2-norm) of vector. 1269 | """ 1270 | 1271 | return _libmagma.magma_snrm2(n, int(dx), incx) 1272 | 1273 | _libmagma.magma_dnrm2.restype = ctypes.c_double 1274 | _libmagma.magma_dnrm2.argtypes = [ctypes.c_int, 1275 | ctypes.c_void_p, 1276 | ctypes.c_int] 1277 | def magma_dnrm2(n, dx, incx): 1278 | """ 1279 | Euclidean norm (2-norm) of vector. 1280 | """ 1281 | 1282 | return _libmagma.magma_dnrm2(n, int(dx), incx) 1283 | 1284 | _libmagma.magma_scnrm2.restype = ctypes.c_float 1285 | _libmagma.magma_scnrm2.argtypes = [ctypes.c_int, 1286 | ctypes.c_void_p, 1287 | ctypes.c_int] 1288 | def magma_scnrm2(n, dx, incx): 1289 | """ 1290 | Euclidean norm (2-norm) of vector. 1291 | """ 1292 | 1293 | return _libmagma.magma_scnrm2(n, int(dx), incx) 1294 | 1295 | _libmagma.magma_dznrm2.restype = ctypes.c_double 1296 | _libmagma.magma_dznrm2.argtypes = [ctypes.c_int, 1297 | ctypes.c_void_p, 1298 | ctypes.c_int] 1299 | def magma_dznrm2(n, dx, incx): 1300 | """ 1301 | Euclidean norm (2-norm) of vector. 1302 | """ 1303 | 1304 | return _libmagma.magma_dznrm2(n, int(dx), incx) 1305 | 1306 | # SROT, DROT, CROT, CSROT, ZROT, ZDROT 1307 | _libmagma.magma_srot.argtypes = [ctypes.c_int, 1308 | ctypes.c_void_p, 1309 | ctypes.c_int, 1310 | ctypes.c_void_p, 1311 | ctypes.c_int, 1312 | ctypes.c_float, 1313 | ctypes.c_float] 1314 | def magma_srot(n, dx, incx, dy, incy, dc, ds): 1315 | """ 1316 | Apply a rotation to vectors. 1317 | """ 1318 | 1319 | _libmagma.magma_srot(n, int(dx), incx, int(dy), incy, dc, ds) 1320 | 1321 | # SROTM, DROTM 1322 | _libmagma.magma_srotm.argtypes = [ctypes.c_int, 1323 | ctypes.c_void_p, 1324 | ctypes.c_int, 1325 | ctypes.c_void_p, 1326 | ctypes.c_int, 1327 | ctypes.c_void_p] 1328 | def magma_srotm(n, dx, incx, dy, incy, param): 1329 | """ 1330 | Apply a real modified Givens rotation. 1331 | """ 1332 | 1333 | _libmagma.magma_srotm(n, int(dx), incx, int(dy), incy, param) 1334 | 1335 | # SROTMG, DROTMG 1336 | _libmagma.magma_srotmg.argtypes = [ctypes.c_void_p, 1337 | ctypes.c_void_p, 1338 | ctypes.c_void_p, 1339 | ctypes.c_void_p, 1340 | ctypes.c_void_p] 1341 | def magma_srotmg(d1, d2, x1, y1, param): 1342 | """ 1343 | Construct a real modified Givens rotation matrix. 1344 | """ 1345 | 1346 | _libmagma.magma_srotmg(int(d1), int(d2), int(x1), int(y1), param) 1347 | 1348 | # SSCAL, DSCAL, CSCAL, CSCAL, CSSCAL, ZSCAL, ZDSCAL 1349 | _libmagma.magma_sscal.argtypes = [ctypes.c_int, 1350 | ctypes.c_float, 1351 | ctypes.c_void_p, 1352 | ctypes.c_int] 1353 | def magma_sscal(n, alpha, dx, incx): 1354 | """ 1355 | Scale a vector by a scalar. 1356 | """ 1357 | 1358 | _libmagma.magma_sscal(n, alpha, int(dx), incx) 1359 | 1360 | # SSWAP, DSWAP, CSWAP, ZSWAP 1361 | _libmagma.magma_sswap.argtypes = [ctypes.c_int, 1362 | ctypes.c_void_p, 1363 | ctypes.c_int, 1364 | ctypes.c_void_p, 1365 | ctypes.c_int] 1366 | def magma_sswap(n, dA, ldda, dB, lddb): 1367 | """ 1368 | Swap vectors. 1369 | """ 1370 | 1371 | _libmagma.magma_sswap(n, int(dA), ldda, int(dB), lddb) 1372 | 1373 | # SGEMV, DGEMV, CGEMV, ZGEMV 1374 | _libmagma.magma_sgemv.argtypes = [ctypes.c_char, 1375 | ctypes.c_int, 1376 | ctypes.c_int, 1377 | ctypes.c_float, 1378 | ctypes.c_void_p, 1379 | ctypes.c_int, 1380 | ctypes.c_void_p, 1381 | ctypes.c_int, 1382 | ctypes.c_float, 1383 | ctypes.c_void_p, 1384 | ctypes.c_int] 1385 | def magma_sgemv(trans, m, n, alpha, dA, ldda, dx, incx, beta, 1386 | dy, incy): 1387 | """ 1388 | Matrix-vector product for general matrix. 1389 | """ 1390 | 1391 | _libmagma.magma_sgemv(trans, m, n, alpha, int(dA), ldda, dx, incx, 1392 | beta, int(dy), incy) 1393 | 1394 | # SGER, DGER, CGERU, CGERC, ZGERU, ZGERC 1395 | _libmagma.magma_sger.argtypes = [ctypes.c_int, 1396 | ctypes.c_int, 1397 | ctypes.c_float, 1398 | ctypes.c_void_p, 1399 | ctypes.c_int, 1400 | ctypes.c_void_p, 1401 | ctypes.c_int, 1402 | ctypes.c_void_p, 1403 | ctypes.c_int] 1404 | def magma_sger(m, n, alpha, dx, incx, dy, incy, dA, ldda): 1405 | """ 1406 | Rank-1 operation on real general matrix. 1407 | """ 1408 | 1409 | _libmagma.magma_sger(m, n, alpha, int(dx), incx, int(dy), incy, 1410 | int(dA), ldda) 1411 | 1412 | # SSYMV, DSYMV, CSYMV, ZSYMV 1413 | _libmagma.magma_ssymv.argtypes = [ctypes.c_char, 1414 | ctypes.c_int, 1415 | ctypes.c_float, 1416 | ctypes.c_void_p, 1417 | ctypes.c_int, 1418 | ctypes.c_void_p, 1419 | ctypes.c_int, 1420 | ctypes.c_float, 1421 | ctypes.c_void_p, 1422 | ctypes.c_int] 1423 | def magma_ssymv(uplo, n, alpha, dA, ldda, dx, incx, beta, dy, incy): 1424 | _libmagma.magma_ssymv(uplo, n, alpha, int(dA), ldda, int(dx), incx, beta, 1425 | int(dy), incy) 1426 | 1427 | # SSYR, DSYR, CSYR, ZSYR 1428 | _libmagma.magma_ssyr.argtypes = [ctypes.c_char, 1429 | ctypes.c_int, 1430 | ctypes.c_float, 1431 | ctypes.c_void_p, 1432 | ctypes.c_int, 1433 | ctypes.c_void_p, 1434 | ctypes.c_int] 1435 | def magma_ssyr(uplo, n, alpha, dx, incx, dA, ldda): 1436 | _libmagma.magma_ssyr(uplo, n, alpha, int(dx), incx, int(dA), ldda) 1437 | 1438 | # SSYR2, DSYR2, CSYR2, ZSYR2 1439 | _libmagma.magma_ssyr2.argtypes = [ctypes.c_char, 1440 | ctypes.c_int, 1441 | ctypes.c_float, 1442 | ctypes.c_void_p, 1443 | ctypes.c_int, 1444 | ctypes.c_void_p, 1445 | ctypes.c_int, 1446 | ctypes.c_void_p, 1447 | ctypes.c_int] 1448 | def magma_ssyr2(uplo, n, alpha, dx, incx, dy, incy, dA, ldda): 1449 | _libmagma.magma_ssyr2(uplo, n, alpha, int(dx), incx, 1450 | int(dy), incy, int(dA), ldda) 1451 | 1452 | # STRMV, DTRMV, CTRMV, ZTRMV 1453 | _libmagma.magma_strmv.argtypes = [ctypes.c_char, 1454 | ctypes.c_char, 1455 | ctypes.c_char, 1456 | ctypes.c_int, 1457 | ctypes.c_void_p, 1458 | ctypes.c_int, 1459 | ctypes.c_void_p, 1460 | ctypes.c_int] 1461 | def magma_strmv(uplo, trans, diag, n, 1462 | dA, ldda, dx, incx): 1463 | _libmagma.magma_strmv(uplo, trans, diag, n, 1464 | int(dA), ldda, int(dx), incx) 1465 | 1466 | # STRSV, DTRSV, CTRSV, ZTRSV 1467 | _libmagma.magma_strsv.argtypes = [ctypes.c_char, 1468 | ctypes.c_char, 1469 | ctypes.c_char, 1470 | ctypes.c_int, 1471 | ctypes.c_void_p, 1472 | ctypes.c_int, 1473 | ctypes.c_void_p, 1474 | ctypes.c_int] 1475 | def magma_strsv(uplo, trans, diag, n, 1476 | dA, ldda, dx, incx): 1477 | _libmagma.magma_strsv(uplo, trans, diag, n, 1478 | int(dA), ldda, int(dx), incx) 1479 | 1480 | # SGEMM, DGEMM, CGEMM, ZGEMM 1481 | _libmagma.magma_sgemm.argtypes = [ctypes.c_char, 1482 | ctypes.c_char, 1483 | ctypes.c_int, 1484 | ctypes.c_int, 1485 | ctypes.c_int, 1486 | ctypes.c_float, 1487 | ctypes.c_void_p, 1488 | ctypes.c_int, 1489 | ctypes.c_void_p, 1490 | ctypes.c_int, 1491 | ctypes.c_float, 1492 | ctypes.c_void_p, 1493 | ctypes.c_int] 1494 | def magma_sgemm(transA, transB, m, n, k, alpha, dA, ldda, dB, lddb, beta, 1495 | dC, lddc): 1496 | _libmagma.magma_sgemm(transA, transB, m, n, k, alpha, 1497 | int(dA), ldda, int(dB), lddb, 1498 | beta, int(dC), lddc) 1499 | 1500 | _libmagma.magma_zgemm.argtypes = [ctypes.c_char, 1501 | ctypes.c_char, 1502 | ctypes.c_int, 1503 | ctypes.c_int, 1504 | ctypes.c_int, 1505 | ctypes.c_float, 1506 | ctypes.c_void_p, 1507 | ctypes.c_int, 1508 | ctypes.c_void_p, 1509 | ctypes.c_int, 1510 | ctypes.c_float, 1511 | ctypes.c_void_p, 1512 | ctypes.c_int] 1513 | def magma_zgemm(transA, transB, m, n, k, alpha, dA, ldda, dB, lddb, beta, 1514 | dC, lddc): 1515 | _libmagma.magma_zgemm(transA, transB, m, n, k, alpha, 1516 | int(dA), ldda, int(dB), lddb, 1517 | beta, int(dC), lddc) 1518 | 1519 | 1520 | # SSYMM, DSYMM, CSYMM, ZSYMM 1521 | _libmagma.magma_ssymm.argtypes = [ctypes.c_char, 1522 | ctypes.c_char, 1523 | ctypes.c_int, 1524 | ctypes.c_int, 1525 | ctypes.c_float, 1526 | ctypes.c_void_p, 1527 | ctypes.c_int, 1528 | ctypes.c_void_p, 1529 | ctypes.c_int, 1530 | ctypes.c_float, 1531 | ctypes.c_void_p, 1532 | ctypes.c_int] 1533 | def magma_ssymm(side, uplo, m, n, alpha, dA, ldda, dB, lddb, beta, 1534 | dC, lddc): 1535 | _libmagma.magma_ssymm(side, uplo, m, n, alpha, 1536 | int(dA), ldda, int(dB), lddb, 1537 | beta, int(dC), lddc) 1538 | 1539 | # SSYRK, DSYRK, CSYRK, ZSYRK 1540 | _libmagma.magma_ssyrk.argtypes = [ctypes.c_char, 1541 | ctypes.c_char, 1542 | ctypes.c_int, 1543 | ctypes.c_int, 1544 | ctypes.c_float, 1545 | ctypes.c_void_p, 1546 | ctypes.c_int, 1547 | ctypes.c_float, 1548 | ctypes.c_void_p, 1549 | ctypes.c_int] 1550 | def magma_ssyrk(uplo, trans, n, k, alpha, dA, ldda, beta, 1551 | dC, lddc): 1552 | _libmagma.magma_ssyrk(uplo, trans, n, k, alpha, 1553 | int(dA), ldda, beta, int(dC), lddc) 1554 | 1555 | # SSYR2K, DSYR2K, CSYR2K, ZSYR2K 1556 | _libmagma.magma_ssyr2k.argtypes = [ctypes.c_char, 1557 | ctypes.c_char, 1558 | ctypes.c_int, 1559 | ctypes.c_int, 1560 | ctypes.c_float, 1561 | ctypes.c_void_p, 1562 | ctypes.c_int, 1563 | ctypes.c_void_p, 1564 | ctypes.c_int, 1565 | ctypes.c_float, 1566 | ctypes.c_void_p, 1567 | ctypes.c_int] 1568 | def magma_ssyr2k(uplo, trans, n, k, alpha, dA, ldda, 1569 | dB, lddb, beta, dC, lddc): 1570 | _libmagma.magma_ssyr2k(uplo, trans, n, k, alpha, 1571 | int(dA), ldda, int(dB), lddb, 1572 | beta, int(dC), lddc) 1573 | 1574 | # STRMM, DTRMM, CTRMM, ZTRMM 1575 | _libmagma.magma_strmm.argtypes = [ctypes.c_char, 1576 | ctypes.c_char, 1577 | ctypes.c_char, 1578 | ctypes.c_char, 1579 | ctypes.c_int, 1580 | ctypes.c_int, 1581 | ctypes.c_float, 1582 | ctypes.c_void_p, 1583 | ctypes.c_int, 1584 | ctypes.c_void_p, 1585 | ctypes.c_int] 1586 | def magma_strmm(side, uplo, trans, diag, m, n, alpha, dA, ldda, 1587 | dB, lddb): 1588 | _libmagma.magma_strmm(uplo, trans, diag, m, n, alpha, 1589 | int(dA), ldda, int(dB), lddb) 1590 | 1591 | # STRSM, DTRSM, CTRSM, ZTRSM 1592 | _libmagma.magma_strsm.argtypes = [ctypes.c_char, 1593 | ctypes.c_char, 1594 | ctypes.c_char, 1595 | ctypes.c_char, 1596 | ctypes.c_int, 1597 | ctypes.c_int, 1598 | ctypes.c_float, 1599 | ctypes.c_void_p, 1600 | ctypes.c_int, 1601 | ctypes.c_void_p, 1602 | ctypes.c_int] 1603 | def magma_strsm(side, uplo, trans, diag, m, n, alpha, dA, ldda, 1604 | dB, lddb): 1605 | _libmagma.magma_strsm(uplo, trans, diag, m, n, alpha, 1606 | int(dA), ldda, int(dB), lddb) 1607 | 1608 | 1609 | # Auxiliary routines: 1610 | _libmagma.magma_vec_const.restype = int 1611 | _libmagma.magma_vec_const.argtypes = [ctypes.c_char] 1612 | def magma_vec_const(job): 1613 | return _libmagma.magma_vec_const(job) 1614 | 1615 | _libmagma.magma_get_spotrf_nb.restype = int 1616 | _libmagma.magma_get_spotrf_nb.argtypes = [ctypes.c_int] 1617 | def magma_get_spotrf_nb(m): 1618 | return _libmagma.magma_get_spotrf_nb(m) 1619 | 1620 | _libmagma.magma_get_sgetrf_nb.restype = int 1621 | _libmagma.magma_get_sgetrf_nb.argtypes = [ctypes.c_int] 1622 | def magma_get_sgetrf_nb(m): 1623 | return _libmagma.magma_get_sgetrf_nb(m) 1624 | 1625 | _libmagma.magma_get_sgetri_nb.restype = int 1626 | _libmagma.magma_get_sgetri_nb.argtypes = [ctypes.c_int] 1627 | def magma_get_sgetri_nb(m): 1628 | return _libmagma.magma_get_sgetri_nb(m) 1629 | 1630 | _libmagma.magma_get_sgeqp3_nb.restype = int 1631 | _libmagma.magma_get_sgeqp3_nb.argtypes = [ctypes.c_int] 1632 | def magma_get_sgeqp3_nb(m): 1633 | return _libmagma.magma_get_sgeqp3_nb(m) 1634 | 1635 | _libmagma.magma_get_sgeqrf_nb.restype = int 1636 | _libmagma.magma_get_sgeqrf_nb.argtypes = [ctypes.c_int] 1637 | def magma_get_sgeqrf_nb(m): 1638 | return _libmagma.magma_get_sgeqrf_nb(m) 1639 | 1640 | _libmagma.magma_get_sgeqlf_nb.restype = int 1641 | _libmagma.magma_get_sgeqlf_nb.argtypes = [ctypes.c_int] 1642 | def magma_get_sgeqlf_nb(m): 1643 | return _libmagma.magma_get_sgeqlf_nb(m) 1644 | 1645 | _libmagma.magma_get_sgehrd_nb.restype = int 1646 | _libmagma.magma_get_sgehrd_nb.argtypes = [ctypes.c_int] 1647 | def magma_get_sgehrd_nb(m): 1648 | return _libmagma.magma_get_sgehrd_nb(m) 1649 | 1650 | _libmagma.magma_get_ssytrd_nb.restype = int 1651 | _libmagma.magma_get_ssytrd_nb.argtypes = [ctypes.c_int] 1652 | def magma_get_ssytrd_nb(m): 1653 | return _libmagma.magma_get_ssytrd_nb(m) 1654 | 1655 | _libmagma.magma_get_sgelqf_nb.restype = int 1656 | _libmagma.magma_get_sgelqf_nb.argtypes = [ctypes.c_int] 1657 | def magma_get_sgelqf_nb(m): 1658 | return _libmagma.magma_get_sgelqf_nb(m) 1659 | 1660 | _libmagma.magma_get_sgebrd_nb.restype = int 1661 | _libmagma.magma_get_sgebrd_nb.argtypes = [ctypes.c_int] 1662 | def magma_get_sgebrd_nb(m): 1663 | return _libmagma.magma_get_sgebrd_nb(m) 1664 | 1665 | _libmagma.magma_get_ssygst_nb.restype = int 1666 | _libmagma.magma_get_ssygst_nb.argtypes = [ctypes.c_int] 1667 | def magma_get_ssygst_nb(m): 1668 | return _libmagma.magma_get_ssgyst_nb(m) 1669 | 1670 | _libmagma.magma_get_sgesvd_nb.restype = int 1671 | _libmagma.magma_get_sgesvd_nb.argtypes = [ctypes.c_int] 1672 | def magma_get_sgesvd_nb(m): 1673 | return _libmagma.magma_get_sgesvd_nb(m) 1674 | 1675 | _libmagma.magma_get_dgesvd_nb.restype = int 1676 | _libmagma.magma_get_dgesvd_nb.argtypes = [ctypes.c_int] 1677 | def magma_get_dgesvd_nb(m): 1678 | return _libmagma.magma_get_dgesvd_nb(m) 1679 | 1680 | _libmagma.magma_get_cgesvd_nb.restype = int 1681 | _libmagma.magma_get_cgesvd_nb.argtypes = [ctypes.c_int] 1682 | def magma_get_cgesvd_nb(m): 1683 | return _libmagma.magma_get_cgesvd_nb(m) 1684 | 1685 | _libmagma.magma_get_zgesvd_nb.restype = int 1686 | _libmagma.magma_get_zgesvd_nb.argtypes = [ctypes.c_int] 1687 | def magma_get_zgesvd_nb(m): 1688 | return _libmagma.magma_get_zgesvd_nb(m) 1689 | 1690 | _libmagma.magma_get_ssygst_nb_m.restype = int 1691 | _libmagma.magma_get_ssygst_nb_m.argtypes = [ctypes.c_int] 1692 | def magma_get_ssygst_nb_m(m): 1693 | return _libmagma.magma_get_ssgyst_nb_m(m) 1694 | 1695 | _libmagma.magma_get_sbulge_nb.restype = int 1696 | _libmagma.magma_get_sbulge_nb.argtypes = [ctypes.c_int] 1697 | def magma_get_sbulge_nb(m): 1698 | return _libmagma.magma_get_sbulge_nb(m) 1699 | 1700 | _libmagma.magma_get_dsytrd_nb.restype = int 1701 | _libmagma.magma_get_dsytrd_nb.argtypes = [ctypes.c_int] 1702 | def magma_get_dsytrd_nb(m): 1703 | return _libmagma.magma_get_dsytrd_nb(m) 1704 | 1705 | # LAPACK routines 1706 | 1707 | # SGEBRD, DGEBRD, CGEBRD, ZGEBRD 1708 | _libmagma.magma_sgebrd.restype = int 1709 | _libmagma.magma_sgebrd.argtypes = [ctypes.c_int, 1710 | ctypes.c_int, 1711 | ctypes.c_void_p, 1712 | ctypes.c_int, 1713 | ctypes.c_void_p, 1714 | ctypes.c_void_p, 1715 | ctypes.c_void_p, 1716 | ctypes.c_void_p, 1717 | ctypes.c_void_p, 1718 | ctypes.c_int, 1719 | ctypes.c_void_p] 1720 | def magma_sgebrd(m, n, A, lda, d, e, tauq, taup, work, lwork, info): 1721 | """ 1722 | Reduce matrix to bidiagonal form. 1723 | """ 1724 | 1725 | status = _libmagma.magma_sgebrd.argtypes(m, n, int(A), lda, 1726 | int(d), int(e), 1727 | int(tauq), int(taup), 1728 | int(work), int(lwork), 1729 | int(info)) 1730 | magmaCheckStatus(status) 1731 | 1732 | # SGEHRD2, DGEHRD2, CGEHRD2, ZGEHRD2 1733 | _libmagma.magma_sgehrd2.restype = int 1734 | _libmagma.magma_sgehrd2.argtypes = [ctypes.c_int, 1735 | ctypes.c_int, 1736 | ctypes.c_int, 1737 | ctypes.c_void_p, 1738 | ctypes.c_int, 1739 | ctypes.c_void_p, 1740 | ctypes.c_void_p, 1741 | ctypes.c_int, 1742 | ctypes.c_void_p] 1743 | def magma_sgehrd2(n, ilo, ihi, A, lda, tau, 1744 | work, lwork, info): 1745 | """ 1746 | Reduce matrix to upper Hessenberg form. 1747 | """ 1748 | 1749 | status = _libmagma.magma_sgehrd2(n, ilo, ihi, int(A), lda, 1750 | int(tau), int(work), 1751 | lwork, int(info)) 1752 | magmaCheckStatus(status) 1753 | 1754 | # SGEHRD, DGEHRD, CGEHRD, ZGEHRD 1755 | _libmagma.magma_sgehrd.restype = int 1756 | _libmagma.magma_sgehrd.argtypes = [ctypes.c_int, 1757 | ctypes.c_int, 1758 | ctypes.c_int, 1759 | ctypes.c_void_p, 1760 | ctypes.c_int, 1761 | ctypes.c_void_p, 1762 | ctypes.c_void_p, 1763 | ctypes.c_int, 1764 | ctypes.c_void_p] 1765 | def magma_sgehrd(n, ilo, ihi, A, lda, tau, 1766 | work, lwork, dT, info): 1767 | """ 1768 | Reduce matrix to upper Hessenberg form (fast algorithm). 1769 | """ 1770 | 1771 | status = _libmagma.magma_sgehrd(n, ilo, ihi, int(A), lda, 1772 | int(tau), int(work), 1773 | lwork, int(dT), int(info)) 1774 | magmaCheckStatus(status) 1775 | 1776 | # SGELQF, DGELQF, CGELQF, ZGELQF 1777 | _libmagma.magma_sgelqf.restype = int 1778 | _libmagma.magma_sgelqf.argtypes = [ctypes.c_int, 1779 | ctypes.c_int, 1780 | ctypes.c_void_p, 1781 | ctypes.c_int, 1782 | ctypes.c_void_p, 1783 | ctypes.c_void_p, 1784 | ctypes.c_int, 1785 | ctypes.c_void_p] 1786 | def magma_sgelqf(m, n, A, lda, tau, work, lwork, info): 1787 | 1788 | """ 1789 | LQ factorization. 1790 | """ 1791 | 1792 | status = _libmagma.magma_sgelqf(m, n, int(A), lda, 1793 | int(tau), int(work), 1794 | lwork, int(info)) 1795 | magmaCheckStatus(status) 1796 | 1797 | # SGEQRF, DGEQRF, CGEQRF, ZGEQRF 1798 | _libmagma.magma_sgeqrf.restype = int 1799 | _libmagma.magma_sgeqrf.argtypes = [ctypes.c_int, 1800 | ctypes.c_int, 1801 | ctypes.c_void_p, 1802 | ctypes.c_int, 1803 | ctypes.c_void_p, 1804 | ctypes.c_void_p, 1805 | ctypes.c_int, 1806 | ctypes.c_void_p] 1807 | def magma_sgeqrf(m, n, A, lda, tau, work, lwork, info): 1808 | 1809 | """ 1810 | QR factorization. 1811 | """ 1812 | 1813 | status = _libmagma.magma_sgeqrf(m, n, int(A), lda, 1814 | int(tau), int(work), 1815 | lwork, int(info)) 1816 | magmaCheckStatus(status) 1817 | 1818 | # SGEQRF, DGEQRF, CGEQRF, ZGEQRF (ooc) 1819 | _libmagma.magma_sgeqrf_ooc.restype = int 1820 | _libmagma.magma_sgeqrf_ooc.argtypes = [ctypes.c_int, 1821 | ctypes.c_int, 1822 | ctypes.c_void_p, 1823 | ctypes.c_int, 1824 | ctypes.c_void_p, 1825 | ctypes.c_void_p, 1826 | ctypes.c_int, 1827 | ctypes.c_void_p] 1828 | def magma_sgeqrf_ooc(m, n, A, lda, tau, work, lwork, info): 1829 | 1830 | """ 1831 | QR factorization (ooc). 1832 | """ 1833 | 1834 | status = _libmagma.magma_sgeqrf_ooc(m, n, int(A), lda, 1835 | int(tau), int(work), 1836 | lwork, int(info)) 1837 | magmaCheckStatus(status) 1838 | 1839 | # SGESV, DGESV, CGESV, ZGESV 1840 | _libmagma.magma_sgesv.restype = int 1841 | _libmagma.magma_sgesv.argtypes = [ctypes.c_int, 1842 | ctypes.c_int, 1843 | ctypes.c_void_p, 1844 | ctypes.c_int, 1845 | ctypes.c_void_p, 1846 | ctypes.c_void_p, 1847 | ctypes.c_int, 1848 | ctypes.c_void_p] 1849 | def magma_sgesv(n, nhrs, A, lda, ipiv, B, ldb, info): 1850 | 1851 | """ 1852 | Solve system of linear equations. 1853 | """ 1854 | 1855 | status = _libmagma.magma_sgesv(n, nhrs, int(A), lda, 1856 | int(ipiv), int(B), 1857 | ldb, int(info)) 1858 | magmaCheckStatus(status) 1859 | 1860 | # SGETRF, DGETRF, CGETRF, ZGETRF 1861 | _libmagma.magma_sgetrf.restype = int 1862 | _libmagma.magma_sgetrf.argtypes = [ctypes.c_int, 1863 | ctypes.c_int, 1864 | ctypes.c_void_p, 1865 | ctypes.c_int, 1866 | ctypes.c_void_p, 1867 | ctypes.c_void_p] 1868 | def magma_sgetrf(m, n, A, lda, ipiv, info): 1869 | 1870 | """ 1871 | LU factorization. 1872 | """ 1873 | 1874 | status = _libmagma.magma_sgetrf(m, n, int(A), lda, 1875 | int(ipiv), int(info)) 1876 | magmaCheckStatus(status) 1877 | 1878 | ## SGETRF2, DGETRF2, CGETRF2, ZGETRF2 1879 | #_libmagma.magma_sgetrf2.restype = int 1880 | #_libmagma.magma_sgetrf2.argtypes = [ctypes.c_int, 1881 | # ctypes.c_int, 1882 | # ctypes.c_void_p, 1883 | # ctypes.c_int, 1884 | # ctypes.c_void_p, 1885 | # ctypes.c_void_p] 1886 | #def magma_sgetrf2(m, n, A, lda, ipiv, info): 1887 | # 1888 | # """ 1889 | # LU factorization (multi-GPU). 1890 | # """ 1891 | # 1892 | # status = _libmagma.magma_sgetrf2(m, n, int(A), lda, 1893 | # int(ipiv), int(info)) 1894 | # magmaCheckStatus(status) 1895 | 1896 | # SGEEV, DGEEV, CGEEV, ZGEEV 1897 | _libmagma.magma_sgeev.restype = int 1898 | _libmagma.magma_sgeev.argtypes = [ctypes.c_char, 1899 | ctypes.c_char, 1900 | ctypes.c_int, 1901 | ctypes.c_void_p, 1902 | ctypes.c_int, 1903 | ctypes.c_void_p, 1904 | ctypes.c_void_p, 1905 | ctypes.c_int, 1906 | ctypes.c_void_p, 1907 | ctypes.c_int, 1908 | ctypes.c_void_p, 1909 | ctypes.c_int, 1910 | ctypes.c_void_p, 1911 | ctypes.c_void_p] 1912 | def magma_sgeev(jobvl, jobvr, n, a, lda, 1913 | w, vl, ldvl, vr, ldvr, work, lwork, rwork, info): 1914 | 1915 | """ 1916 | Compute eigenvalues and eigenvectors. 1917 | """ 1918 | 1919 | status = _libmagma.magma_sgeev(jobvl, jobvr, n, int(a), lda, 1920 | int(w), int(vl), ldvl, int(vr), ldvr, 1921 | int(work), lwork, int(rwork), int(info)) 1922 | magmaCheckStatus(status) 1923 | 1924 | # SGESVD, DGESVD, CGESVD, ZGESVD 1925 | _libmagma.magma_sgesvd.restype = int 1926 | _libmagma.magma_sgesvd.argtypes = [ctypes.c_int, 1927 | ctypes.c_int, 1928 | ctypes.c_int, 1929 | ctypes.c_int, 1930 | ctypes.c_void_p, 1931 | ctypes.c_int, 1932 | ctypes.c_void_p, 1933 | ctypes.c_void_p, 1934 | ctypes.c_int, 1935 | ctypes.c_void_p, 1936 | ctypes.c_int, 1937 | ctypes.c_void_p, 1938 | ctypes.c_int, 1939 | ctypes.c_void_p] 1940 | def magma_sgesvd(jobu, jobvt, m, n, a, lda, s, u, ldu, vt, ldvt, work, lwork, 1941 | info): 1942 | """ 1943 | SVD decomposition. 1944 | """ 1945 | 1946 | status = _libmagma.magma_sgesvd(jobu, jobvt, m, n, 1947 | int(a), lda, int(s), int(u), ldu, 1948 | int(vt), ldvt, int(work), lwork, 1949 | int(info)) 1950 | magmaCheckStatus(status) 1951 | 1952 | _libmagma.magma_dgesvd.restype = int 1953 | _libmagma.magma_dgesvd.argtypes = [ctypes.c_int, 1954 | ctypes.c_int, 1955 | ctypes.c_int, 1956 | ctypes.c_int, 1957 | ctypes.c_void_p, 1958 | ctypes.c_int, 1959 | ctypes.c_void_p, 1960 | ctypes.c_void_p, 1961 | ctypes.c_int, 1962 | ctypes.c_void_p, 1963 | ctypes.c_int, 1964 | ctypes.c_void_p, 1965 | ctypes.c_int, 1966 | ctypes.c_void_p] 1967 | def magma_dgesvd(jobu, jobvt, m, n, a, lda, s, u, ldu, vt, ldvt, work, lwork, 1968 | info): 1969 | """ 1970 | SVD decomposition. 1971 | """ 1972 | 1973 | status = _libmagma.magma_dgesvd(jobu, jobvt, m, n, 1974 | int(a), lda, int(s), int(u), ldu, 1975 | int(vt), ldvt, int(work), lwork, 1976 | int(info)) 1977 | magmaCheckStatus(status) 1978 | 1979 | _libmagma.magma_cgesvd.restype = int 1980 | _libmagma.magma_cgesvd.argtypes = [ctypes.c_int, 1981 | ctypes.c_int, 1982 | ctypes.c_int, 1983 | ctypes.c_int, 1984 | ctypes.c_void_p, 1985 | ctypes.c_int, 1986 | ctypes.c_void_p, 1987 | ctypes.c_void_p, 1988 | ctypes.c_int, 1989 | ctypes.c_void_p, 1990 | ctypes.c_int, 1991 | ctypes.c_void_p, 1992 | ctypes.c_int, 1993 | ctypes.c_void_p] 1994 | def magma_cgesvd(jobu, jobvt, m, n, a, lda, s, u, ldu, vt, ldvt, work, lwork, 1995 | rwork, info): 1996 | """ 1997 | SVD decomposition. 1998 | """ 1999 | 2000 | status = _libmagma.magma_cgesvd(jobu, jobvt, m, n, 2001 | int(a), lda, int(s), int(u), ldu, 2002 | int(vt), ldvt, int(work), lwork, 2003 | int(rwork), 2004 | int(info)) 2005 | magmaCheckStatus(status) 2006 | 2007 | _libmagma.magma_zgesvd.restype = int 2008 | _libmagma.magma_zgesvd.argtypes = [ctypes.c_int, 2009 | ctypes.c_int, 2010 | ctypes.c_int, 2011 | ctypes.c_int, 2012 | ctypes.c_void_p, 2013 | ctypes.c_int, 2014 | ctypes.c_void_p, 2015 | ctypes.c_void_p, 2016 | ctypes.c_int, 2017 | ctypes.c_void_p, 2018 | ctypes.c_int, 2019 | ctypes.c_void_p, 2020 | ctypes.c_int, 2021 | ctypes.c_void_p, 2022 | ctypes.c_void_p] 2023 | def magma_zgesvd(jobu, jobvt, m, n, a, lda, s, u, ldu, vt, ldvt, work, lwork, 2024 | rwork, info): 2025 | """ 2026 | SVD decomposition. 2027 | """ 2028 | 2029 | status = _libmagma.magma_zgesvd(jobu, jobvt, m, n, 2030 | int(a), lda, int(s), int(u), ldu, 2031 | int(vt), ldvt, int(work), lwork, 2032 | int(rwork), int(info)) 2033 | magmaCheckStatus(status) 2034 | 2035 | # SGESDD, DGESDD, CGESDD, ZGESDD 2036 | _libmagma.magma_sgesdd.restype = int 2037 | _libmagma.magma_sgesdd.argtypes = [ctypes.c_int, 2038 | ctypes.c_int, 2039 | ctypes.c_int, 2040 | ctypes.c_void_p, 2041 | ctypes.c_int, 2042 | ctypes.c_void_p, 2043 | ctypes.c_void_p, 2044 | ctypes.c_int, 2045 | ctypes.c_void_p, 2046 | ctypes.c_int, 2047 | ctypes.c_void_p, 2048 | ctypes.c_int, 2049 | ctypes.c_void_p, 2050 | ctypes.c_void_p] 2051 | def magma_sgesdd(jobz, m, n, a, lda, s, u, ldu, vt, ldvt, work, lwork, 2052 | iwork, info): 2053 | """ 2054 | SDD decomposition. 2055 | """ 2056 | 2057 | status = _libmagma.magma_sgesdd(jobz, m, n, 2058 | int(a), lda, int(s), int(u), ldu, 2059 | int(vt), ldvt, int(work), lwork, 2060 | int(iwork), int(info)) 2061 | magmaCheckStatus(status) 2062 | 2063 | _libmagma.magma_dgesdd.restype = int 2064 | _libmagma.magma_dgesdd.argtypes = [ctypes.c_int, 2065 | ctypes.c_int, 2066 | ctypes.c_int, 2067 | ctypes.c_void_p, 2068 | ctypes.c_int, 2069 | ctypes.c_void_p, 2070 | ctypes.c_void_p, 2071 | ctypes.c_int, 2072 | ctypes.c_void_p, 2073 | ctypes.c_int, 2074 | ctypes.c_void_p, 2075 | ctypes.c_int, 2076 | ctypes.c_void_p, 2077 | ctypes.c_void_p] 2078 | def magma_dgesdd(jobz, m, n, a, lda, s, u, ldu, vt, ldvt, work, lwork, 2079 | iwork, info): 2080 | """ 2081 | SDD decomposition. 2082 | """ 2083 | 2084 | status = _libmagma.magma_dgesdd(jobz, m, n, 2085 | int(a), lda, int(s), int(u), ldu, 2086 | int(vt), ldvt, int(work), lwork, 2087 | int(iwork), int(info)) 2088 | magmaCheckStatus(status) 2089 | 2090 | _libmagma.magma_cgesdd.restype = int 2091 | _libmagma.magma_cgesdd.argtypes = [ctypes.c_int, 2092 | ctypes.c_int, 2093 | ctypes.c_int, 2094 | ctypes.c_void_p, 2095 | ctypes.c_int, 2096 | ctypes.c_void_p, 2097 | ctypes.c_void_p, 2098 | ctypes.c_int, 2099 | ctypes.c_void_p, 2100 | ctypes.c_int, 2101 | ctypes.c_void_p, 2102 | ctypes.c_int, 2103 | ctypes.c_void_p, 2104 | ctypes.c_void_p, 2105 | ctypes.c_void_p] 2106 | def magma_cgesdd(jobz, m, n, a, lda, s, u, ldu, vt, ldvt, work, lwork, 2107 | rwork, iwork, info): 2108 | """ 2109 | SDD decomposition. 2110 | """ 2111 | 2112 | status = _libmagma.magma_cgesdd(jobz, m, n, 2113 | int(a), lda, int(s), int(u), ldu, 2114 | int(vt), ldvt, int(work), lwork, 2115 | int(rwork), int(iwork), int(info)) 2116 | magmaCheckStatus(status) 2117 | 2118 | _libmagma.magma_zgesdd.restype = int 2119 | _libmagma.magma_zgesdd.argtypes = [ctypes.c_int, 2120 | ctypes.c_int, 2121 | ctypes.c_int, 2122 | ctypes.c_void_p, 2123 | ctypes.c_int, 2124 | ctypes.c_void_p, 2125 | ctypes.c_void_p, 2126 | ctypes.c_int, 2127 | ctypes.c_void_p, 2128 | ctypes.c_int, 2129 | ctypes.c_void_p, 2130 | ctypes.c_int, 2131 | ctypes.c_void_p, 2132 | ctypes.c_void_p, 2133 | ctypes.c_void_p] 2134 | def magma_zgesdd(jobz, m, n, a, lda, s, u, ldu, vt, ldvt, work, lwork, 2135 | rwork, iwork, info): 2136 | """ 2137 | SDD decomposition. 2138 | """ 2139 | 2140 | status = _libmagma.magma_zgesdd(jobz, m, n, 2141 | int(a), lda, int(s), int(u), ldu, 2142 | int(vt), ldvt, int(work), lwork, 2143 | int(rwork), int(iwork), int(info)) 2144 | magmaCheckStatus(status) 2145 | 2146 | 2147 | 2148 | # SPOSV, DPOSV, CPOSV, ZPOSV 2149 | _libmagma.magma_sposv_gpu.restype = int 2150 | _libmagma.magma_sposv_gpu.argtypes = [ctypes.c_int, 2151 | ctypes.c_int, 2152 | ctypes.c_int, 2153 | ctypes.c_void_p, 2154 | ctypes.c_int, 2155 | ctypes.c_void_p, 2156 | ctypes.c_int, 2157 | ctypes.c_void_p] 2158 | def magma_sposv_gpu(uplo, n, nhrs, a_gpu, lda, b_gpu, ldb): 2159 | """ 2160 | Solve linear system with positive semidefinite coefficient matrix. 2161 | """ 2162 | uplo = _uplo_conversion[uplo] 2163 | info = ctypes.c_int() 2164 | status = _libmagma.magma_sposv_gpu(uplo, n, nhrs, int(a_gpu), lda, 2165 | int(b_gpu), ldb, ctypes.byref(info)) 2166 | magmaCheckStatus(status) 2167 | 2168 | _libmagma.magma_dposv_gpu.restype = int 2169 | _libmagma.magma_dposv_gpu.argtypes = _libmagma.magma_sposv_gpu.argtypes 2170 | def magma_dposv_gpu(uplo, n, nhrs, a_gpu, lda, b_gpu, ldb): 2171 | """ 2172 | Solve linear system with positive semidefinite coefficient matrix. 2173 | """ 2174 | uplo = _uplo_conversion[uplo] 2175 | info = ctypes.c_int() 2176 | status = _libmagma.magma_dposv_gpu(uplo, n, nhrs, int(a_gpu), lda, 2177 | int(b_gpu), ldb, ctypes.byref(info)) 2178 | magmaCheckStatus(status) 2179 | 2180 | 2181 | _libmagma.magma_cposv_gpu.restype = int 2182 | _libmagma.magma_cposv_gpu.argtypes = _libmagma.magma_sposv_gpu.argtypes 2183 | def magma_cposv_gpu(uplo, n, nhrs, a_gpu, lda, b_gpu, ldb): 2184 | """ 2185 | Solve linear system with positive semidefinite coefficient matrix. 2186 | """ 2187 | uplo = _uplo_conversion[uplo] 2188 | info = ctypes.c_int() 2189 | status = _libmagma.magma_cposv_gpu(uplo, n, nhrs, int(a_gpu), lda, 2190 | int(b_gpu), ldb, ctypes.byref(info)) 2191 | magmaCheckStatus(status) 2192 | 2193 | 2194 | _libmagma.magma_zposv_gpu.restype = int 2195 | _libmagma.magma_zposv_gpu.argtypes = _libmagma.magma_sposv_gpu.argtypes 2196 | def magma_zposv_gpu(uplo, n, nhrs, a_gpu, lda, b_gpu, ldb): 2197 | """ 2198 | Solve linear system with positive semidefinite coefficient matrix. 2199 | """ 2200 | uplo = _uplo_conversion[uplo] 2201 | info = ctypes.c_int() 2202 | status = _libmagma.magma_zposv_gpu(uplo, n, nhrs, int(a_gpu), lda, 2203 | int(b_gpu), ldb, ctypes.byref(info)) 2204 | magmaCheckStatus(status) 2205 | 2206 | 2207 | # SGESV, DGESV, CGESV, ZGESV 2208 | _libmagma.magma_sgesv_gpu.restype = int 2209 | _libmagma.magma_sgesv_gpu.argtypes = [ctypes.c_int, 2210 | ctypes.c_int, 2211 | ctypes.c_void_p, 2212 | ctypes.c_int, 2213 | ctypes.c_void_p, 2214 | ctypes.c_void_p, 2215 | ctypes.c_int, 2216 | ctypes.c_void_p] 2217 | def magma_sgesv_gpu(n, nhrs, A, lda, ipiv, B, ldb): 2218 | """ 2219 | Solve system of linear equations. 2220 | """ 2221 | info = ctypes.c_int() 2222 | status = _libmagma.magma_sgesv_gpu(n, nhrs, int(A), lda, 2223 | int(ipiv), int(B), 2224 | ldb, ctypes.byref(info)) 2225 | magmaCheckStatus(status) 2226 | 2227 | 2228 | _libmagma.magma_dgesv_gpu.restype = int 2229 | _libmagma.magma_dgesv_gpu.argtypes = _libmagma.magma_sgesv_gpu.argtypes 2230 | def magma_dgesv_gpu(n, nhrs, A, lda, ipiv, B, ldb): 2231 | """ 2232 | Solve system of linear equations. 2233 | """ 2234 | info = ctypes.c_int() 2235 | status = _libmagma.magma_dgesv_gpu(n, nhrs, int(A), lda, 2236 | int(ipiv), int(B), 2237 | ldb, ctypes.byref(info)) 2238 | magmaCheckStatus(status) 2239 | 2240 | 2241 | _libmagma.magma_cgesv_gpu.restype = int 2242 | _libmagma.magma_cgesv_gpu.argtypes = _libmagma.magma_sgesv_gpu.argtypes 2243 | def magma_cgesv_gpu(n, nhrs, A, lda, ipiv, B, ldb): 2244 | """ 2245 | Solve system of linear equations. 2246 | """ 2247 | info = ctypes.c_int() 2248 | status = _libmagma.magma_cgesv_gpu(n, nhrs, int(A), lda, 2249 | int(ipiv), int(B), 2250 | ldb, ctypes.byref(info)) 2251 | magmaCheckStatus(status) 2252 | 2253 | 2254 | _libmagma.magma_zgesv_gpu.restype = int 2255 | _libmagma.magma_zgesv_gpu.argtypes = _libmagma.magma_sgesv_gpu.argtypes 2256 | def magma_zgesv_gpu(n, nhrs, A, lda, ipiv, B, ldb): 2257 | """ 2258 | Solve system of linear equations. 2259 | """ 2260 | info = ctypes.c_int() 2261 | status = _libmagma.magma_zgesv_gpu(n, nhrs, int(A), lda, 2262 | int(ipiv), int(B), 2263 | ldb, ctypes.byref(info)) 2264 | magmaCheckStatus(status) 2265 | 2266 | 2267 | _libmagma.magma_sgesv_nopiv_gpu.restype = int 2268 | _libmagma.magma_sgesv_nopiv_gpu.argtypes = [ctypes.c_int, 2269 | ctypes.c_int, 2270 | ctypes.c_void_p, 2271 | ctypes.c_int, 2272 | ctypes.c_void_p, 2273 | ctypes.c_int, 2274 | ctypes.c_void_p] 2275 | def magma_sgesv_nopiv_gpu(n, nhrs, A, lda, B, ldb): 2276 | """ 2277 | Solve system of linear equations. 2278 | """ 2279 | info = ctypes.c_int() 2280 | status = _libmagma.magma_sgesv_nopiv_gpu(n, nhrs, int(A), lda, 2281 | int(B), ldb, ctypes.byref(info)) 2282 | magmaCheckStatus(status) 2283 | 2284 | _libmagma.magma_dgesv_nopiv_gpu.restype = int 2285 | _libmagma.magma_dgesv_nopiv_gpu.argtypes = _libmagma.magma_sgesv_nopiv_gpu.argtypes 2286 | def magma_dgesv_nopiv_gpu(n, nhrs, A, lda, B, ldb): 2287 | """ 2288 | Solve system of linear equations. 2289 | """ 2290 | info = ctypes.c_int() 2291 | status = _libmagma.magma_dgesv_nopiv_gpu(n, nhrs, int(A), lda, 2292 | int(B), ldb, ctypes.byref(info)) 2293 | magmaCheckStatus(status) 2294 | 2295 | _libmagma.magma_cgesv_nopiv_gpu.restype = int 2296 | _libmagma.magma_cgesv_nopiv_gpu.argtypes = _libmagma.magma_sgesv_nopiv_gpu.argtypes 2297 | def magma_cgesv_nopiv_gpu(n, nhrs, A, lda, B, ldb): 2298 | """ 2299 | Solve system of linear equations. 2300 | """ 2301 | info = ctypes.c_int() 2302 | status = _libmagma.magma_cgesv_nopiv_gpu(n, nhrs, int(A), lda, 2303 | int(B), ldb, ctypes.byref(info)) 2304 | magmaCheckStatus(status) 2305 | 2306 | 2307 | _libmagma.magma_zgesv_nopiv_gpu.restype = int 2308 | _libmagma.magma_zgesv_nopiv_gpu.argtypes = _libmagma.magma_sgesv_nopiv_gpu.argtypes 2309 | def magma_zgesv_nopiv_gpu(n, nhrs, A, lda, B, ldb): 2310 | """ 2311 | Solve system of linear equations. 2312 | """ 2313 | info = ctypes.c_int() 2314 | status = _libmagma.magma_zgesv_nopiv_gpu(n, nhrs, int(A), lda, 2315 | int(B), ldb, ctypes.byref(info)) 2316 | magmaCheckStatus(status) 2317 | 2318 | # SPOTRF, DPOTRF, CPOTRF, ZPOTRF 2319 | _libmagma.magma_spotrf_gpu.restype = int 2320 | _libmagma.magma_spotrf_gpu.argtypes = [ctypes.c_int, 2321 | ctypes.c_int, 2322 | ctypes.c_void_p, 2323 | ctypes.c_int, 2324 | ctypes.c_void_p] 2325 | def magma_spotrf_gpu(uplo, n, A, lda): 2326 | """ 2327 | Cholesky factorization of positive symmetric matrix. 2328 | """ 2329 | uplo = _uplo_conversion[uplo] 2330 | info = ctypes.c_int() 2331 | status = _libmagma.magma_spotrf_gpu(uplo, n, int(A), lda, ctypes.byref(info)) 2332 | magmaCheckStatus(status) 2333 | 2334 | 2335 | _libmagma.magma_dpotrf_gpu.restype = int 2336 | _libmagma.magma_dpotrf_gpu.argtypes = _libmagma.magma_spotrf_gpu.argtypes 2337 | def magma_dpotrf_gpu(uplo, n, A, lda): 2338 | """ 2339 | Cholesky factorization of positive symmetric matrix. 2340 | """ 2341 | uplo = _uplo_conversion[uplo] 2342 | info = ctypes.c_int() 2343 | status = _libmagma.magma_dpotrf_gpu(uplo, n, int(A), lda, ctypes.byref(info)) 2344 | magmaCheckStatus(status) 2345 | 2346 | 2347 | _libmagma.magma_cpotrf_gpu.restype = int 2348 | _libmagma.magma_cpotrf_gpu.argtypes = _libmagma.magma_spotrf_gpu.argtypes 2349 | def magma_cpotrf_gpu(uplo, n, A, lda): 2350 | """ 2351 | Cholesky factorization of positive symmetric matrix. 2352 | """ 2353 | uplo = _uplo_conversion[uplo] 2354 | info = ctypes.c_int() 2355 | status = _libmagma.magma_cpotrf_gpu(uplo, n, int(A), lda, ctypes.byref(info)) 2356 | magmaCheckStatus(status) 2357 | 2358 | 2359 | _libmagma.magma_zpotrf_gpu.restype = int 2360 | _libmagma.magma_zpotrf_gpu.argtypes = _libmagma.magma_zpotrf_gpu.argtypes 2361 | def magma_zpotrf_gpu(uplo, n, A, lda): 2362 | """ 2363 | Cholesky factorization of positive symmetric matrix. 2364 | """ 2365 | uplo = _uplo_conversion[uplo] 2366 | info = ctypes.c_int() 2367 | status = _libmagma.magma_zpotrf_gpu(uplo, n, int(A), lda, ctypes.byref(info)) 2368 | magmaCheckStatus(status) 2369 | 2370 | 2371 | # SPOTRI, DPOTRI, CPOTRI, ZPOTRI 2372 | _libmagma.magma_spotri_gpu.restype = int 2373 | _libmagma.magma_spotri_gpu.argtypes = [ctypes.c_int, 2374 | ctypes.c_int, 2375 | ctypes.c_void_p, 2376 | ctypes.c_int, 2377 | ctypes.c_void_p] 2378 | def magma_spotri_gpu(uplo, n, A, lda): 2379 | """ 2380 | Inverse using the Cholesky factorization of positive symmetric matrix. 2381 | """ 2382 | uplo = _uplo_conversion[uplo] 2383 | info = ctypes.c_int() 2384 | status = _libmagma.magma_spotri_gpu(uplo, n, int(A), lda, ctypes.byref(info)) 2385 | magmaCheckStatus(status) 2386 | 2387 | 2388 | _libmagma.magma_dpotri_gpu.restype = int 2389 | _libmagma.magma_dpotri_gpu.argtypes = _libmagma.magma_spotri_gpu.argtypes 2390 | def magma_dpotri_gpu(uplo, n, A, lda): 2391 | """ 2392 | Inverse using the Cholesky factorization of positive symmetric matrix. 2393 | """ 2394 | uplo = _uplo_conversion[uplo] 2395 | info = ctypes.c_int() 2396 | status = _libmagma.magma_dpotri_gpu(uplo, n, int(A), lda, ctypes.byref(info)) 2397 | magmaCheckStatus(status) 2398 | 2399 | 2400 | _libmagma.magma_cpotri_gpu.restype = int 2401 | _libmagma.magma_cpotri_gpu.argtypes = _libmagma.magma_spotri_gpu.argtypes 2402 | def magma_cpotri_gpu(uplo, n, A, lda): 2403 | """ 2404 | Inverse using the Cholesky factorization of positive symmetric matrix. 2405 | """ 2406 | uplo = _uplo_conversion[uplo] 2407 | info = ctypes.c_int() 2408 | status = _libmagma.magma_cpotri_gpu(uplo, n, int(A), lda, ctypes.byref(info)) 2409 | magmaCheckStatus(status) 2410 | 2411 | 2412 | _libmagma.magma_zpotri_gpu.restype = int 2413 | _libmagma.magma_zpotri_gpu.argtypes = _libmagma.magma_spotri_gpu.argtypes 2414 | def magma_zpotri_gpu(uplo, n, A, lda): 2415 | """ 2416 | Inverse using the Cholesky factorization of positive symmetric matrix. 2417 | """ 2418 | uplo = _uplo_conversion[uplo] 2419 | info = ctypes.c_int() 2420 | status = _libmagma.magma_zpotri_gpu(uplo, n, int(A), lda, ctypes.byref(info)) 2421 | magmaCheckStatus(status) 2422 | 2423 | 2424 | # SGETRF, DGETRF, CGETRF, ZGETRF 2425 | _libmagma.magma_sgetrf_gpu.restype = int 2426 | _libmagma.magma_sgetrf_gpu.argtypes = [ctypes.c_int, 2427 | ctypes.c_int, 2428 | ctypes.c_void_p, 2429 | ctypes.c_int, 2430 | ctypes.c_void_p, 2431 | ctypes.c_void_p] 2432 | def magma_sgetrf_gpu(n, m, A, lda, ipiv): 2433 | """ 2434 | LU factorization. 2435 | """ 2436 | info = ctypes.c_int() 2437 | status = _libmagma.magma_sgetrf_gpu(n, m, int(A), lda, 2438 | int(ipiv), ctypes.byref(info)) 2439 | magmaCheckStatus(status) 2440 | 2441 | 2442 | _libmagma.magma_dgetrf_gpu.restype = int 2443 | _libmagma.magma_dgetrf_gpu.argtypes = _libmagma.magma_sgetrf_gpu.argtypes 2444 | def magma_dgetrf_gpu(n, m, A, lda, ipiv): 2445 | """ 2446 | LU factorization. 2447 | """ 2448 | info = ctypes.c_int() 2449 | status = _libmagma.magma_dgetrf_gpu(n, m, int(A), lda, 2450 | int(ipiv), ctypes.byref(info)) 2451 | magmaCheckStatus(status) 2452 | 2453 | 2454 | _libmagma.magma_cgetrf_gpu.restype = int 2455 | _libmagma.magma_cgetrf_gpu.argtypes = _libmagma.magma_sgetrf_gpu.argtypes 2456 | def magma_cgetrf_gpu(n, m, A, lda, ipiv): 2457 | """ 2458 | LU factorization. 2459 | """ 2460 | info = ctypes.c_int() 2461 | status = _libmagma.magma_cgetrf_gpu(n, m, int(A), lda, 2462 | int(ipiv), ctypes.byref(info)) 2463 | magmaCheckStatus(status) 2464 | 2465 | 2466 | _libmagma.magma_zgetrf_gpu.restype = int 2467 | _libmagma.magma_zgetrf_gpu.argtypes = _libmagma.magma_sgetrf_gpu.argtypes 2468 | def magma_zgetrf_gpu(n, m, A, lda, ipiv): 2469 | """ 2470 | LU factorization. 2471 | """ 2472 | info = ctypes.c_int() 2473 | status = _libmagma.magma_zgetrf_gpu(n, m, int(A), lda, 2474 | int(ipiv), ctypes.byref(info)) 2475 | magmaCheckStatus(status) 2476 | 2477 | # SSYEVD, DSYEVD 2478 | _libmagma.magma_ssyevd_gpu.restype = int 2479 | _libmagma.magma_ssyevd_gpu.argtypes = [ctypes.c_int, 2480 | ctypes.c_int, 2481 | ctypes.c_int, 2482 | ctypes.c_void_p, 2483 | ctypes.c_int, 2484 | ctypes.c_void_p, 2485 | ctypes.c_void_p, 2486 | ctypes.c_int, 2487 | ctypes.c_void_p, 2488 | ctypes.c_int, 2489 | ctypes.c_void_p, 2490 | ctypes.c_int, 2491 | ctypes.c_void_p] 2492 | def magma_ssyevd_gpu(jobz, uplo, n, dA, ldda, w, wA, ldwa, work, lwork, iwork, 2493 | liwork): 2494 | """ 2495 | Compute eigenvalues of real symmetric matrix. 2496 | """ 2497 | 2498 | uplo = _uplo_conversion[uplo] 2499 | info = ctypes.c_int() 2500 | status = _libmagma.magma_ssyevd_gpu(jobz, uplo, n, int(dA), ldda, 2501 | int(w), int(wA), ldwa, int(work), 2502 | lwork, int(iwork), liwork, ctypes.byref(info)) 2503 | magmaCheckStatus(status) 2504 | 2505 | _libmagma.magma_dsyevd_gpu.restype = int 2506 | _libmagma.magma_dsyevd_gpu.argtypes = [ctypes.c_int, 2507 | ctypes.c_int, 2508 | ctypes.c_int, 2509 | ctypes.c_void_p, 2510 | ctypes.c_int, 2511 | ctypes.c_void_p, 2512 | ctypes.c_void_p, 2513 | ctypes.c_int, 2514 | ctypes.c_void_p, 2515 | ctypes.c_int, 2516 | ctypes.c_void_p, 2517 | ctypes.c_int, 2518 | ctypes.c_void_p] 2519 | def magma_dsyevd_gpu(jobz, uplo, n, dA, ldda, w, wA, ldwa, work, lwork, iwork, 2520 | liwork): 2521 | """ 2522 | Compute eigenvalues of real symmetric matrix. 2523 | """ 2524 | 2525 | uplo = _uplo_conversion[uplo] 2526 | info = ctypes.c_int() 2527 | status = _libmagma.magma_dsyevd_gpu(jobz, uplo, n, int(dA), ldda, 2528 | int(w), int(wA), ldwa, int(work), 2529 | lwork, int(iwork), liwork, ctypes.byref(info)) 2530 | magmaCheckStatus(status) 2531 | 2532 | # SYMMETRIZE 2533 | _libmagma.magmablas_ssymmetrize.restype = int 2534 | _libmagma.magmablas_ssymmetrize.argtypes = [ctypes.c_int, 2535 | ctypes.c_int, 2536 | ctypes.c_void_p, 2537 | ctypes.c_int] 2538 | def magmablas_ssymmetrize(uplo, n, A, lda): 2539 | """ 2540 | Symmetrize a triangular matrix. 2541 | """ 2542 | uplo = _uplo_conversion[uplo] 2543 | status = _libmagma.magmablas_ssymmetrize(uplo, n, int(A), lda) 2544 | magmaCheckStatus(status) 2545 | 2546 | 2547 | _libmagma.magmablas_dsymmetrize.restype = int 2548 | _libmagma.magmablas_dsymmetrize.argtypes = _libmagma.magmablas_ssymmetrize.argtypes 2549 | def magmablas_dsymmetrize(uplo, n, A, lda): 2550 | """ 2551 | Symmetrize a triangular matrix. 2552 | """ 2553 | uplo = _uplo_conversion[uplo] 2554 | status = _libmagma.magmablas_dsymmetrize(uplo, n, int(A), lda) 2555 | magmaCheckStatus(status) 2556 | 2557 | 2558 | _libmagma.magmablas_csymmetrize.restype = int 2559 | _libmagma.magmablas_csymmetrize.argtypes = _libmagma.magmablas_ssymmetrize.argtypes 2560 | def magmablas_csymmetrize(uplo, n, A, lda): 2561 | """ 2562 | Symmetrize a triangular matrix. 2563 | """ 2564 | uplo = _uplo_conversion[uplo] 2565 | status = _libmagma.magmablas_csymmetrize(uplo, n, int(A), lda) 2566 | magmaCheckStatus(status) 2567 | 2568 | 2569 | _libmagma.magmablas_zsymmetrize.restype = int 2570 | _libmagma.magmablas_zsymmetrize.argtypes = _libmagma.magmablas_ssymmetrize.argtypes 2571 | def magmablas_zsymmetrize(uplo, n, A, lda): 2572 | """ 2573 | Symmetrize a triangular matrix. 2574 | """ 2575 | uplo = _uplo_conversion[uplo] 2576 | status = _libmagma.magmablas_zsymmetrize(uplo, n, int(A), lda) 2577 | magmaCheckStatus(status) 2578 | 2579 | 2580 | _libmagma_sparse.magma_zmfree.restype = int 2581 | _libmagma_sparse.magma_zmfree.argtypes = [ctypes.POINTER(magma_z_matrix), magma_queue_t] 2582 | def magma_zmfree(A, queue): 2583 | status = _libmagma_sparse.magma_zmfree(ctypes.byref(A), queue) 2584 | magmaCheckStatus(status) 2585 | 2586 | 2587 | _libmagma_sparse.magma_cmfree.restype = int 2588 | _libmagma_sparse.magma_cmfree.argtypes = [ctypes.POINTER(magma_c_matrix), magma_queue_t] 2589 | def magma_cmfree(A, queue): 2590 | status = _libmagma_sparse.magma_cmfree(ctypes.byref(A), queue) 2591 | magmaCheckStatus(status) 2592 | 2593 | 2594 | _libmagma_sparse.magma_dmfree.restype = int 2595 | _libmagma_sparse.magma_dmfree.argtypes = [ctypes.POINTER(magma_d_matrix), magma_queue_t] 2596 | def magma_dmfree(A, queue): 2597 | status = _libmagma_sparse.magma_dmfree(ctypes.byref(A), queue) 2598 | magmaCheckStatus(status) 2599 | 2600 | 2601 | _libmagma_sparse.magma_smfree.restype = int 2602 | _libmagma_sparse.magma_smfree.argtypes = [ctypes.POINTER(magma_s_matrix), magma_queue_t] 2603 | def magma_smfree(A, queue): 2604 | status = _libmagma_sparse.magma_smfree(ctypes.byref(A), queue) 2605 | magmaCheckStatus(status) 2606 | 2607 | 2608 | _libmagma_sparse.magma_zcsrset.restype = int 2609 | _libmagma_sparse.magma_zcsrset.argtypes = [ctypes.c_int, 2610 | ctypes.c_int, 2611 | ndpointer(ctypes.c_int, flags="C_CONTIGUOUS"), 2612 | ndpointer(ctypes.c_int, flags="C_CONTIGUOUS"), 2613 | ndpointer(np.complex128, flags="C_CONTIGUOUS"), 2614 | ctypes.POINTER(magma_z_matrix), 2615 | magma_queue_t] 2616 | def magma_zcsrset(m, n, row, col, val, A, queue): 2617 | #TODO: check that dtype is np.int32. np.ndarray(row, dtype=np.int32) 2618 | status = _libmagma_sparse.magma_zcsrset(m, n, row, col, val, ctypes.byref(A), queue) 2619 | magmaCheckStatus(status) 2620 | 2621 | 2622 | _libmagma_sparse.magma_ccsrset.restype = int 2623 | _libmagma_sparse.magma_ccsrset.argtypes = [ctypes.c_int, 2624 | ctypes.c_int, 2625 | ndpointer(ctypes.c_int, flags="C_CONTIGUOUS"), 2626 | ndpointer(ctypes.c_int, flags="C_CONTIGUOUS"), 2627 | ndpointer(np.complex64, flags="C_CONTIGUOUS"), 2628 | ctypes.POINTER(magma_c_matrix), 2629 | magma_queue_t] 2630 | def magma_ccsrset(m, n, row, col, val, A, queue): 2631 | #TODO: check that dtype is np.int32. np.ndarray(row, dtype=np.int32) 2632 | status = _libmagma_sparse.magma_ccsrset(m, n, row, col, val, ctypes.byref(A), queue) 2633 | magmaCheckStatus(status) 2634 | 2635 | 2636 | _libmagma_sparse.magma_dcsrset.restype = int 2637 | _libmagma_sparse.magma_dcsrset.argtypes = [ctypes.c_int, 2638 | ctypes.c_int, 2639 | ndpointer(ctypes.c_int, flags="C_CONTIGUOUS"), 2640 | ndpointer(ctypes.c_int, flags="C_CONTIGUOUS"), 2641 | ndpointer(ctypes.c_double, flags="C_CONTIGUOUS"), 2642 | ctypes.POINTER(magma_d_matrix), 2643 | magma_queue_t] 2644 | def magma_dcsrset(m, n, row, col, val, A, queue): 2645 | #TODO: check that dtype is np.int32. np.ndarray(row, dtype=np.int32) 2646 | status = _libmagma_sparse.magma_dcsrset(m, n, row, col, val, ctypes.byref(A), queue) 2647 | magmaCheckStatus(status) 2648 | 2649 | 2650 | _libmagma_sparse.magma_scsrset.restype = int 2651 | _libmagma_sparse.magma_scsrset.argtypes = [ctypes.c_int, 2652 | ctypes.c_int, 2653 | ndpointer(ctypes.c_int, flags="C_CONTIGUOUS"), 2654 | ndpointer(ctypes.c_int, flags="C_CONTIGUOUS"), 2655 | ndpointer(ctypes.c_float, flags="C_CONTIGUOUS"), 2656 | ctypes.POINTER(magma_s_matrix), 2657 | magma_queue_t] 2658 | def magma_scsrset(m, n, row, col, val, A, queue): 2659 | #TODO: check that dtype is np.int32. np.ndarray(row, dtype=np.int32) 2660 | status = _libmagma_sparse.magma_scsrset(m, n, row, col, val, ctypes.byref(A), queue) 2661 | magmaCheckStatus(status) 2662 | 2663 | 2664 | _libmagma_sparse.magma_zcsrget.restype = int 2665 | _libmagma_sparse.magma_zcsrget.argtypes = [magma_d_matrix, 2666 | ctypes.POINTER(magma_int_t), 2667 | ctypes.POINTER(magma_int_t), 2668 | ctypes.POINTER(ctypes.POINTER(ctypes.c_int)), 2669 | ctypes.POINTER(ctypes.POINTER(ctypes.c_int)), 2670 | #ctypes.POINTER(ndpointer(np.complex128, flags="C_CONTIGUOUS")), 2671 | ctypes.POINTER(ctypes.c_void_p), 2672 | magma_queue_t] 2673 | def magma_zcsrget(A, queue): 2674 | m = ctypes.c_int() 2675 | n = ctypes.c_int() 2676 | val_p = ctypes.c_void_p() 2677 | #val_p = ctypes.POINTER(ndpointer(np.complex128, flags="C_CONTIGUOUS"))() 2678 | row_p = ctypes.POINTER(ctypes.c_int)() 2679 | col_p = ctypes.POINTER(ctypes.c_int)() 2680 | 2681 | status = _libmagma_sparse.magma_zcsrget(A, ctypes.byref(m), ctypes.byref(n), ctypes.byref(row_p), ctypes.byref(col_p), ctypes.byref(val_p), queue) 2682 | magmaCheckStatus(status) 2683 | 2684 | val_p = ctypes.cast(val_p, ndpointer(np.complex128, ndim=1, shape = (A.nnz,), flags="C_CONTIGUOUS")) 2685 | 2686 | val = np.ctypeslib.as_array(val_p, shape = (A.nnz,)) 2687 | row = np.ctypeslib.as_array(row_p, shape = (A.nnz,)) 2688 | col = np.ctypeslib.as_array(col_p, shape = (A.nnz,)) 2689 | 2690 | return m.value, n.value, row, col, val 2691 | 2692 | 2693 | _libmagma_sparse.magma_ccsrget.restype = int 2694 | _libmagma_sparse.magma_ccsrget.argtypes = [magma_c_matrix, 2695 | ctypes.POINTER(magma_int_t), 2696 | ctypes.POINTER(magma_int_t), 2697 | ctypes.POINTER(ctypes.POINTER(ctypes.c_int)), 2698 | ctypes.POINTER(ctypes.POINTER(ctypes.c_int)), 2699 | ctypes.POINTER(ctypes.c_void_p), 2700 | magma_queue_t] 2701 | def magma_ccsrget(A, queue): 2702 | m = ctypes.c_int() 2703 | n = ctypes.c_int() 2704 | val_p = ctypes.c_void_p() 2705 | row_p = ctypes.POINTER(ctypes.c_int)() 2706 | col_p = ctypes.POINTER(ctypes.c_int)() 2707 | 2708 | status = _libmagma_sparse.magma_ccsrget(A, ctypes.byref(m), ctypes.byref(n), ctypes.byref(row_p), ctypes.byref(col_p), ctypes.byref(val_p), queue) 2709 | magmaCheckStatus(status) 2710 | 2711 | val_p = ctypes.cast(val_p, ndpointer(np.complex64, ndim=1, shape = (A.nnz,), flags="C_CONTIGUOUS")) 2712 | 2713 | val = np.ctypeslib.as_array(val_p, shape = (A.nnz,)) 2714 | row = np.ctypeslib.as_array(row_p, shape = (A.nnz,)) 2715 | col = np.ctypeslib.as_array(col_p, shape = (A.nnz,)) 2716 | 2717 | return m.value, n.value, row, col, val 2718 | 2719 | 2720 | _libmagma_sparse.magma_dcsrget.restype = int 2721 | _libmagma_sparse.magma_dcsrget.argtypes = [magma_d_matrix, 2722 | ctypes.POINTER(magma_int_t), 2723 | ctypes.POINTER(magma_int_t), 2724 | ctypes.POINTER(ctypes.POINTER(ctypes.c_int)), 2725 | ctypes.POINTER(ctypes.POINTER(ctypes.c_int)), 2726 | ctypes.POINTER(ctypes.POINTER(ctypes.c_double)), 2727 | magma_queue_t] 2728 | def magma_dcsrget(A, queue): 2729 | m = ctypes.c_int() 2730 | n = ctypes.c_int() 2731 | val_p = ctypes.POINTER(ctypes.c_double)() 2732 | row_p = ctypes.POINTER(ctypes.c_int)() 2733 | col_p = ctypes.POINTER(ctypes.c_int)() 2734 | 2735 | status = _libmagma_sparse.magma_dcsrget(A, ctypes.byref(m), ctypes.byref(n), ctypes.byref(row_p), ctypes.byref(col_p), ctypes.byref(val_p), queue) 2736 | magmaCheckStatus(status) 2737 | 2738 | val = np.ctypeslib.as_array(val_p, shape = (A.nnz,)) 2739 | row = np.ctypeslib.as_array(row_p, shape = (A.nnz,)) 2740 | col = np.ctypeslib.as_array(col_p, shape = (A.nnz,)) 2741 | 2742 | return m.value, n.value, row, col, val 2743 | 2744 | 2745 | _libmagma_sparse.magma_scsrget.restype = int 2746 | _libmagma_sparse.magma_scsrget.argtypes = [magma_s_matrix, 2747 | ctypes.POINTER(magma_int_t), 2748 | ctypes.POINTER(magma_int_t), 2749 | ctypes.POINTER(ctypes.POINTER(ctypes.c_int)), 2750 | ctypes.POINTER(ctypes.POINTER(ctypes.c_int)), 2751 | ctypes.POINTER(ctypes.POINTER(ctypes.c_float)), 2752 | magma_queue_t] 2753 | def magma_scsrget(A, queue): 2754 | m = ctypes.c_int() 2755 | n = ctypes.c_int() 2756 | val_p = ctypes.POINTER(ctypes.c_float)() 2757 | row_p = ctypes.POINTER(ctypes.c_int)() 2758 | col_p = ctypes.POINTER(ctypes.c_int)() 2759 | 2760 | status = _libmagma_sparse.magma_scsrget(A, ctypes.byref(m), ctypes.byref(n), ctypes.byref(row_p), ctypes.byref(col_p), ctypes.byref(val_p), queue) 2761 | magmaCheckStatus(status) 2762 | 2763 | val = np.ctypeslib.as_array(val_p, shape = (A.nnz,)) 2764 | row = np.ctypeslib.as_array(row_p, shape = (A.nnz,)) 2765 | col = np.ctypeslib.as_array(col_p, shape = (A.nnz,)) 2766 | 2767 | return m.value, n.value, row, col, val 2768 | 2769 | 2770 | _libmagma_sparse.magma_zvset.restype = int 2771 | _libmagma_sparse.magma_zvset.argtypes = [ctypes.c_int, 2772 | ctypes.c_int, 2773 | ndpointer(np.complex128, flags="C_CONTIGUOUS"), 2774 | ctypes.POINTER(magma_z_matrix), 2775 | magma_queue_t] 2776 | def magma_zvset(m, n, val, v, queue): 2777 | status = _libmagma_sparse.magma_zvset(m, n, val, ctypes.byref(v), queue) 2778 | magmaCheckStatus(status) 2779 | 2780 | 2781 | _libmagma_sparse.magma_cvset.restype = int 2782 | _libmagma_sparse.magma_cvset.argtypes = [ctypes.c_int, 2783 | ctypes.c_int, 2784 | ndpointer(np.complex64, flags="C_CONTIGUOUS"), 2785 | ctypes.POINTER(magma_c_matrix), 2786 | magma_queue_t] 2787 | def magma_cvset(m, n, val, v, queue): 2788 | status = _libmagma_sparse.magma_cvset(m, n, val, ctypes.byref(v), queue) 2789 | magmaCheckStatus(status) 2790 | 2791 | 2792 | _libmagma_sparse.magma_dvset.restype = int 2793 | _libmagma_sparse.magma_dvset.argtypes = [ctypes.c_int, 2794 | ctypes.c_int, 2795 | ndpointer(ctypes.c_double, flags="C_CONTIGUOUS"), 2796 | ctypes.POINTER(magma_d_matrix), 2797 | magma_queue_t] 2798 | def magma_dvset(m, n, val, v, queue): 2799 | status = _libmagma_sparse.magma_dvset(m, n, val, ctypes.byref(v), queue) 2800 | magmaCheckStatus(status) 2801 | 2802 | 2803 | _libmagma_sparse.magma_svset.restype = int 2804 | _libmagma_sparse.magma_svset.argtypes = [ctypes.c_int, 2805 | ctypes.c_int, 2806 | ndpointer(ctypes.c_float, flags="C_CONTIGUOUS"), 2807 | ctypes.POINTER(magma_s_matrix), 2808 | magma_queue_t] 2809 | def magma_svset(m, n, val, v, queue): 2810 | status = _libmagma_sparse.magma_svset(m, n, val, ctypes.byref(v), queue) 2811 | magmaCheckStatus(status) 2812 | 2813 | 2814 | _libmagma_sparse.magma_zvget.restype = int 2815 | _libmagma_sparse.magma_zvget.argtypes = [magma_z_matrix, 2816 | ctypes.POINTER(magma_int_t), 2817 | ctypes.POINTER(magma_int_t), 2818 | ctypes.POINTER(ctypes.c_void_p), 2819 | magma_queue_t] 2820 | def magma_zvget(v, queue): 2821 | m = ctypes.c_int() 2822 | n = ctypes.c_int() 2823 | val_p = ctypes.c_void_p() 2824 | 2825 | status = _libmagma_sparse.magma_zvget(v, ctypes.byref(m), ctypes.byref(n), ctypes.byref(val_p), queue) 2826 | magmaCheckStatus(status) 2827 | 2828 | val_p = ctypes.cast(val_p, ndpointer(np.complex128, ndim=1, shape = (v.nnz,), flags="C_CONTIGUOUS")) 2829 | val = np.ctypeslib.as_array(val_p, shape = (v.nnz,)) 2830 | 2831 | return m.value, n.value, val 2832 | 2833 | 2834 | _libmagma_sparse.magma_cvget.restype = int 2835 | _libmagma_sparse.magma_cvget.argtypes = [magma_c_matrix, 2836 | ctypes.POINTER(magma_int_t), 2837 | ctypes.POINTER(magma_int_t), 2838 | ctypes.POINTER(ctypes.c_void_p), 2839 | magma_queue_t] 2840 | def magma_cvget(v, queue): 2841 | m = ctypes.c_int() 2842 | n = ctypes.c_int() 2843 | val_p = ctypes.c_void_p() 2844 | 2845 | status = _libmagma_sparse.magma_cvget(v, ctypes.byref(m), ctypes.byref(n), ctypes.byref(val_p), queue) 2846 | magmaCheckStatus(status) 2847 | 2848 | val_p = ctypes.cast(val_p, ndpointer(np.complex64, ndim=1, shape = (v.nnz,), flags="C_CONTIGUOUS")) 2849 | val = np.ctypeslib.as_array(val_p, shape = (v.nnz,)) 2850 | 2851 | return m.value, n.value, val 2852 | 2853 | 2854 | _libmagma_sparse.magma_dvget.restype = int 2855 | _libmagma_sparse.magma_dvget.argtypes = [magma_d_matrix, 2856 | ctypes.POINTER(magma_int_t), 2857 | ctypes.POINTER(magma_int_t), 2858 | ctypes.POINTER(ctypes.POINTER(ctypes.c_double)), 2859 | magma_queue_t] 2860 | def magma_dvget(v, queue): 2861 | m = ctypes.c_int() 2862 | n = ctypes.c_int() 2863 | val_p = ctypes.POINTER(ctypes.c_double)() 2864 | 2865 | status = _libmagma_sparse.magma_dvget(v, ctypes.byref(m), ctypes.byref(n), ctypes.byref(val_p), queue) 2866 | magmaCheckStatus(status) 2867 | 2868 | val = np.ctypeslib.as_array(val_p, shape = (v.nnz,)) 2869 | 2870 | return m.value, n.value, val 2871 | 2872 | 2873 | _libmagma_sparse.magma_svget.restype = int 2874 | _libmagma_sparse.magma_svget.argtypes = [magma_s_matrix, 2875 | ctypes.POINTER(magma_int_t), 2876 | ctypes.POINTER(magma_int_t), 2877 | ctypes.POINTER(ctypes.POINTER(ctypes.c_float)), 2878 | magma_queue_t] 2879 | def magma_svget(v, queue): 2880 | m = ctypes.c_int() 2881 | n = ctypes.c_int() 2882 | val_p = ctypes.POINTER(ctypes.c_float)() 2883 | 2884 | status = _libmagma_sparse.magma_svget(v, ctypes.byref(m), ctypes.byref(n), ctypes.byref(val_p), queue) 2885 | magmaCheckStatus(status) 2886 | 2887 | val = np.ctypeslib.as_array(val_p, shape = (v.nnz,)) 2888 | 2889 | return m.value, n.value, val 2890 | 2891 | 2892 | _libmagma_sparse.magma_zvinit.restype = int 2893 | _libmagma_sparse.magma_zvinit.argtypes = [ctypes.POINTER(magma_z_matrix), 2894 | ctypes.c_int, 2895 | ctypes.c_int, 2896 | ctypes.c_int, 2897 | cuda.cuDoubleComplex, 2898 | magma_queue_t] 2899 | def magma_zvinit(x, memory_location, num_rows, num_cols, values, queue): 2900 | status = _libmagma_sparse.magma_zvinit(ctypes.byref(x), memory_location, num_rows, num_cols, values, queue) 2901 | magmaCheckStatus(status) 2902 | 2903 | 2904 | _libmagma_sparse.magma_cvinit.restype = int 2905 | _libmagma_sparse.magma_cvinit.argtypes = [ctypes.POINTER(magma_c_matrix), 2906 | ctypes.c_int, 2907 | ctypes.c_int, 2908 | ctypes.c_int, 2909 | cuda.cuFloatComplex, 2910 | magma_queue_t] 2911 | def magma_cvinit(x, memory_location, num_rows, num_cols, values, queue): 2912 | status = _libmagma_sparse.magma_cvinit(ctypes.byref(x), memory_location, num_rows, num_cols, values, queue) 2913 | magmaCheckStatus(status) 2914 | 2915 | 2916 | _libmagma_sparse.magma_dvinit.restype = int 2917 | _libmagma_sparse.magma_dvinit.argtypes = [ctypes.POINTER(magma_d_matrix), 2918 | ctypes.c_int, 2919 | ctypes.c_int, 2920 | ctypes.c_int, 2921 | ctypes.c_double, 2922 | magma_queue_t] 2923 | def magma_dvinit(x, memory_location, num_rows, num_cols, values, queue): 2924 | status = _libmagma_sparse.magma_dvinit(ctypes.byref(x), memory_location, num_rows, num_cols, values, queue) 2925 | magmaCheckStatus(status) 2926 | 2927 | 2928 | _libmagma_sparse.magma_svinit.restype = int 2929 | _libmagma_sparse.magma_svinit.argtypes = [ctypes.POINTER(magma_s_matrix), 2930 | ctypes.c_int, 2931 | ctypes.c_int, 2932 | ctypes.c_int, 2933 | ctypes.c_float, 2934 | magma_queue_t] 2935 | def magma_svinit(x, memory_location, num_rows, num_cols, values, queue): 2936 | status = _libmagma_sparse.magma_svinit(ctypes.byref(x), memory_location, num_rows, num_cols, values, queue) 2937 | magmaCheckStatus(status) 2938 | 2939 | 2940 | _libmagma_sparse.magma_zmtransfer.restype = int 2941 | _libmagma_sparse.magma_zmtransfer.argtypes = [magma_z_matrix, 2942 | ctypes.POINTER(magma_z_matrix), 2943 | ctypes.c_int, 2944 | ctypes.c_int, 2945 | magma_queue_t] 2946 | def magma_zmtransfer(A, B, src, dst, queue): 2947 | status = _libmagma_sparse.magma_zmtransfer(A, ctypes.byref(B), src, dst, queue) 2948 | magmaCheckStatus(status) 2949 | 2950 | 2951 | _libmagma_sparse.magma_cmtransfer.restype = int 2952 | _libmagma_sparse.magma_cmtransfer.argtypes = [magma_c_matrix, 2953 | ctypes.POINTER(magma_c_matrix), 2954 | ctypes.c_int, 2955 | ctypes.c_int, 2956 | magma_queue_t] 2957 | def magma_cmtransfer(A, B, src, dst, queue): 2958 | status = _libmagma_sparse.magma_cmtransfer(A, ctypes.byref(B), src, dst, queue) 2959 | magmaCheckStatus(status) 2960 | 2961 | 2962 | _libmagma_sparse.magma_dmtransfer.restype = int 2963 | _libmagma_sparse.magma_dmtransfer.argtypes = [magma_d_matrix, 2964 | ctypes.POINTER(magma_d_matrix), 2965 | ctypes.c_int, 2966 | ctypes.c_int, 2967 | magma_queue_t] 2968 | def magma_dmtransfer(A, B, src, dst, queue): 2969 | status = _libmagma_sparse.magma_dmtransfer(A, ctypes.byref(B), src, dst, queue) 2970 | magmaCheckStatus(status) 2971 | 2972 | 2973 | _libmagma_sparse.magma_smtransfer.restype = int 2974 | _libmagma_sparse.magma_smtransfer.argtypes = [magma_s_matrix, 2975 | ctypes.POINTER(magma_s_matrix), 2976 | ctypes.c_int, 2977 | ctypes.c_int, 2978 | magma_queue_t] 2979 | def magma_smtransfer(A, B, src, dst, queue): 2980 | status = _libmagma_sparse.magma_smtransfer(A, ctypes.byref(B), src, dst, queue) 2981 | magmaCheckStatus(status) 2982 | 2983 | 2984 | _libmagma_sparse.magma_zmscale.restype = int 2985 | _libmagma_sparse.magma_zmscale.argtypes = [ctypes.POINTER(magma_z_matrix), 2986 | ctypes.c_int, 2987 | magma_queue_t] 2988 | def magma_zmscale(A, scaling, queue): 2989 | _libmagma_sparse.magma_zmscale(ctypes.byref(A), scaling, queue) 2990 | 2991 | 2992 | _libmagma_sparse.magma_cmscale.restype = int 2993 | _libmagma_sparse.magma_cmscale.argtypes = [ctypes.POINTER(magma_c_matrix), 2994 | ctypes.c_int, 2995 | magma_queue_t] 2996 | def magma_cmscale(A, scaling, queue): 2997 | _libmagma_sparse.magma_cmscale(ctypes.byref(A), scaling, queue) 2998 | 2999 | 3000 | _libmagma_sparse.magma_dmscale.restype = int 3001 | _libmagma_sparse.magma_dmscale.argtypes = [ctypes.POINTER(magma_d_matrix), 3002 | ctypes.c_int, 3003 | magma_queue_t] 3004 | def magma_dmscale(A, scaling, queue): 3005 | _libmagma_sparse.magma_dmscale(ctypes.byref(A), scaling, queue) 3006 | 3007 | 3008 | _libmagma_sparse.magma_smscale.restype = int 3009 | _libmagma_sparse.magma_smscale.argtypes = [ctypes.POINTER(magma_s_matrix), 3010 | ctypes.c_int, 3011 | magma_queue_t] 3012 | def magma_smscale(A, scaling, queue): 3013 | _libmagma_sparse.magma_smscale(ctypes.byref(A), scaling, queue) 3014 | 3015 | 3016 | _libmagma_sparse.magma_zmconvert.restype = int 3017 | _libmagma_sparse.magma_zmconvert.argtypes = [magma_z_matrix, 3018 | ctypes.POINTER(magma_z_matrix), 3019 | ctypes.c_int, 3020 | ctypes.c_int, 3021 | magma_queue_t] 3022 | def magma_zmconvert(A, B, old_format, new_format, queue): 3023 | status = _libmagma_sparse.magma_zmconvert(A, ctypes.byref(B), old_format, new_format, queue) 3024 | magmaCheckStatus(status) 3025 | 3026 | 3027 | _libmagma_sparse.magma_cmconvert.restype = int 3028 | _libmagma_sparse.magma_cmconvert.argtypes = [magma_c_matrix, 3029 | ctypes.POINTER(magma_c_matrix), 3030 | ctypes.c_int, 3031 | ctypes.c_int, 3032 | magma_queue_t] 3033 | def magma_cmconvert(A, B, old_format, new_format, queue): 3034 | status = _libmagma_sparse.magma_cmconvert(A, ctypes.byref(B), old_format, new_format, queue) 3035 | magmaCheckStatus(status) 3036 | 3037 | 3038 | _libmagma_sparse.magma_dmconvert.restype = int 3039 | _libmagma_sparse.magma_dmconvert.argtypes = [magma_d_matrix, 3040 | ctypes.POINTER(magma_d_matrix), 3041 | ctypes.c_int, 3042 | ctypes.c_int, 3043 | magma_queue_t] 3044 | def magma_dmconvert(A, B, old_format, new_format, queue): 3045 | status = _libmagma_sparse.magma_dmconvert(A, ctypes.byref(B), old_format, new_format, queue) 3046 | magmaCheckStatus(status) 3047 | 3048 | 3049 | _libmagma_sparse.magma_smconvert.restype = int 3050 | _libmagma_sparse.magma_smconvert.argtypes = [magma_s_matrix, 3051 | ctypes.POINTER(magma_s_matrix), 3052 | ctypes.c_int, 3053 | ctypes.c_int, 3054 | magma_queue_t] 3055 | def magma_smconvert(A, B, old_format, new_format, queue): 3056 | status = _libmagma_sparse.magma_smconvert(A, ctypes.byref(B), old_format, new_format, queue) 3057 | magmaCheckStatus(status) 3058 | 3059 | 3060 | _libmagma_sparse.magma_zm_5stencil.restype = int 3061 | _libmagma_sparse.magma_zm_5stencil.argtypes = [ctypes.c_int, 3062 | ctypes.POINTER(magma_z_matrix), 3063 | magma_queue_t] 3064 | def magma_zm_5stencil(laplace_size, A, queue): 3065 | status = _libmagma_sparse.magma_zm_5stencil(laplace_size, ctypes.byref(A), queue) 3066 | magmaCheckStatus(status) 3067 | 3068 | 3069 | _libmagma_sparse.magma_cm_5stencil.restype = int 3070 | _libmagma_sparse.magma_cm_5stencil.argtypes = [ctypes.c_int, 3071 | ctypes.POINTER(magma_c_matrix), 3072 | magma_queue_t] 3073 | def magma_cm_5stencil(laplace_size, A, queue): 3074 | status = _libmagma_sparse.magma_cm_5stencil(laplace_size, ctypes.byref(A), queue) 3075 | magmaCheckStatus(status) 3076 | 3077 | 3078 | _libmagma_sparse.magma_dm_5stencil.restype = int 3079 | _libmagma_sparse.magma_dm_5stencil.argtypes = [ctypes.c_int, 3080 | ctypes.POINTER(magma_d_matrix), 3081 | magma_queue_t] 3082 | def magma_dm_5stencil(laplace_size, A, queue): 3083 | status = _libmagma_sparse.magma_dm_5stencil(laplace_size, ctypes.byref(A), queue) 3084 | magmaCheckStatus(status) 3085 | 3086 | 3087 | _libmagma_sparse.magma_sm_5stencil.restype = int 3088 | _libmagma_sparse.magma_sm_5stencil.argtypes = [ctypes.c_int, 3089 | ctypes.POINTER(magma_s_matrix), 3090 | magma_queue_t] 3091 | def magma_sm_5stencil(laplace_size, A, queue): 3092 | status = _libmagma_sparse.magma_sm_5stencil(laplace_size, ctypes.byref(A), queue) 3093 | magmaCheckStatus(status) 3094 | 3095 | 3096 | _libmagma_sparse.magma_z_spmv.restype = int 3097 | _libmagma_sparse.magma_z_spmv.argtypes = [cuda.cuDoubleComplex, 3098 | magma_z_matrix, 3099 | magma_z_matrix, 3100 | cuda.cuDoubleComplex, 3101 | magma_z_matrix, 3102 | magma_queue_t] 3103 | def magma_z_spmv(alpha, A, x, beta, y, queue): 3104 | status = _libmagma_sparse.magma_z_spmv(alpha, A, x, beta, y, queue) 3105 | magmaCheckStatus(status) 3106 | 3107 | 3108 | _libmagma_sparse.magma_c_spmv.restype = int 3109 | _libmagma_sparse.magma_c_spmv.argtypes = [cuda.cuFloatComplex, 3110 | magma_c_matrix, 3111 | magma_c_matrix, 3112 | cuda.cuFloatComplex, 3113 | magma_c_matrix, 3114 | magma_queue_t] 3115 | def magma_c_spmv(alpha, A, x, beta, y, queue): 3116 | status = _libmagma_sparse.magma_c_spmv(alpha, A, x, beta, y, queue) 3117 | magmaCheckStatus(status) 3118 | 3119 | 3120 | _libmagma_sparse.magma_d_spmv.restype = int 3121 | _libmagma_sparse.magma_d_spmv.argtypes = [ctypes.c_double, 3122 | magma_d_matrix, 3123 | magma_d_matrix, 3124 | ctypes.c_double, 3125 | magma_d_matrix, 3126 | magma_queue_t] 3127 | def magma_d_spmv(alpha, A, x, beta, y, queue): 3128 | status = _libmagma_sparse.magma_d_spmv(alpha, A, x, beta, y, queue) 3129 | magmaCheckStatus(status) 3130 | 3131 | 3132 | _libmagma_sparse.magma_s_spmv.restype = int 3133 | _libmagma_sparse.magma_s_spmv.argtypes = [ctypes.c_float, 3134 | magma_s_matrix, 3135 | magma_s_matrix, 3136 | ctypes.c_float, 3137 | magma_s_matrix, 3138 | magma_queue_t] 3139 | def magma_s_spmv(alpha, A, x, beta, y, queue): 3140 | status = _libmagma_sparse.magma_s_spmv(alpha, A, x, beta, y, queue) 3141 | magmaCheckStatus(status) 3142 | 3143 | 3144 | _libmagma.magma_queue_create_v2_internal.restype = int 3145 | _libmagma.magma_queue_create_v2_internal.argtypes = [ctypes.c_int, ctypes.POINTER(ctypes.POINTER(magma_queue)), ctypes.c_char_p, ctypes.c_char_p, ctypes.c_int] 3146 | def magma_queue_create(device, queue): 3147 | status = _libmagma.magma_queue_create_v2_internal(device, ctypes.byref(queue), 'magma_queue_create', __file__, inspect.currentframe().f_back.f_lineno) 3148 | magmaCheckStatus(status) 3149 | 3150 | 3151 | _libmagma.magma_queue_destroy_internal.restype = int 3152 | _libmagma.magma_queue_destroy_internal.argtypes = [ctypes.POINTER(magma_queue), ctypes.c_char_p, ctypes.c_char_p, ctypes.c_int] 3153 | def magma_queue_destroy(queue): 3154 | status = _libmagma.magma_queue_destroy_internal(queue, 'magma_queue_destroy', __file__, inspect.currentframe().f_back.f_lineno) 3155 | magmaCheckStatus(status) 3156 | 3157 | 3158 | _libmagma.magma_queue_get_device.restype = int 3159 | _libmagma.magma_queue_get_device.argtypes = [ctypes.POINTER(magma_queue)] 3160 | def magma_queue_get_device(queue): 3161 | return _libmagma.magma_queue_get_device(queue) 3162 | 3163 | 3164 | _libmagma_sparse.magma_zsolverinfo_init.restype = int 3165 | _libmagma_sparse.magma_zsolverinfo_init.argtypes = [ctypes.POINTER(magma_z_solver_par), 3166 | ctypes.POINTER(magma_z_preconditioner), 3167 | magma_queue_t] 3168 | def magma_zsolverinfo_init(solver_par, precond, queue): 3169 | status = _libmagma_sparse.magma_zsolverinfo_init(ctypes.byref(solver_par), ctypes.byref(precond), queue) 3170 | magmaCheckStatus(status) 3171 | 3172 | 3173 | _libmagma_sparse.magma_csolverinfo_init.restype = int 3174 | _libmagma_sparse.magma_csolverinfo_init.argtypes = [ctypes.POINTER(magma_c_solver_par), 3175 | ctypes.POINTER(magma_c_preconditioner), 3176 | magma_queue_t] 3177 | def magma_csolverinfo_init(solver_par, precond, queue): 3178 | status = _libmagma_sparse.magma_csolverinfo_init(ctypes.byref(solver_par), ctypes.byref(precond), queue) 3179 | magmaCheckStatus(status) 3180 | 3181 | 3182 | _libmagma_sparse.magma_dsolverinfo_init.restype = int 3183 | _libmagma_sparse.magma_dsolverinfo_init.argtypes = [ctypes.POINTER(magma_d_solver_par), 3184 | ctypes.POINTER(magma_d_preconditioner), 3185 | magma_queue_t] 3186 | def magma_dsolverinfo_init(solver_par, precond, queue): 3187 | status = _libmagma_sparse.magma_dsolverinfo_init(ctypes.byref(solver_par), ctypes.byref(precond), queue) 3188 | magmaCheckStatus(status) 3189 | 3190 | 3191 | _libmagma_sparse.magma_ssolverinfo_init.restype = int 3192 | _libmagma_sparse.magma_ssolverinfo_init.argtypes = [ctypes.POINTER(magma_s_solver_par), 3193 | ctypes.POINTER(magma_s_preconditioner), 3194 | magma_queue_t] 3195 | def magma_ssolverinfo_init(solver_par, precond, queue): 3196 | status = _libmagma_sparse.magma_ssolverinfo_init(ctypes.byref(solver_par), ctypes.byref(precond), queue) 3197 | magmaCheckStatus(status) 3198 | 3199 | 3200 | _libmagma_sparse.magma_zeigensolverinfo_init.restype = int 3201 | _libmagma_sparse.magma_zeigensolverinfo_init.argtypes = [ctypes.POINTER(magma_z_solver_par), 3202 | magma_queue_t] 3203 | def magma_zeigensolverinfo_init(solver_par, queue): 3204 | status = _libmagma_sparse.magma_zeigensolverinfo_init(ctypes.byref(solver_par), queue) 3205 | magmaCheckStatus(status) 3206 | 3207 | 3208 | _libmagma_sparse.magma_ceigensolverinfo_init.restype = int 3209 | _libmagma_sparse.magma_ceigensolverinfo_init.argtypes = [ctypes.POINTER(magma_c_solver_par), 3210 | magma_queue_t] 3211 | def magma_ceigensolverinfo_init(solver_par, queue): 3212 | status = _libmagma_sparse.magma_ceigensolverinfo_init(ctypes.byref(solver_par), queue) 3213 | magmaCheckStatus(status) 3214 | 3215 | 3216 | _libmagma_sparse.magma_deigensolverinfo_init.restype = int 3217 | _libmagma_sparse.magma_deigensolverinfo_init.argtypes = [ctypes.POINTER(magma_d_solver_par), 3218 | magma_queue_t] 3219 | def magma_deigensolverinfo_init(solver_par, queue): 3220 | status = _libmagma_sparse.magma_deigensolverinfo_init(ctypes.byref(solver_par), queue) 3221 | magmaCheckStatus(status) 3222 | 3223 | 3224 | _libmagma_sparse.magma_seigensolverinfo_init.restype = int 3225 | _libmagma_sparse.magma_seigensolverinfo_init.argtypes = [ctypes.POINTER(magma_s_solver_par), 3226 | magma_queue_t] 3227 | def magma_seigensolverinfo_init(solver_par, queue): 3228 | status = _libmagma_sparse.magma_seigensolverinfo_init(ctypes.byref(solver_par), queue) 3229 | magmaCheckStatus(status) 3230 | 3231 | 3232 | _libmagma_sparse.magma_zsolverinfo.restype = int 3233 | _libmagma_sparse.magma_zsolverinfo.argtypes = [ctypes.POINTER(magma_z_solver_par), 3234 | ctypes.POINTER(magma_z_preconditioner), 3235 | magma_queue_t] 3236 | def magma_zsolverinfo(solver_par, precond_par, queue): 3237 | status = _libmagma_sparse.magma_zsolverinfo(ctypes.byref(solver_par), ctypes.byref(precond_par), queue) 3238 | magmaCheckStatus(status) 3239 | 3240 | 3241 | _libmagma_sparse.magma_csolverinfo.restype = int 3242 | _libmagma_sparse.magma_csolverinfo.argtypes = [ctypes.POINTER(magma_c_solver_par), 3243 | ctypes.POINTER(magma_c_preconditioner), 3244 | magma_queue_t] 3245 | def magma_csolverinfo(solver_par, precond_par, queue): 3246 | status = _libmagma_sparse.magma_csolverinfo(ctypes.byref(solver_par), ctypes.byref(precond_par), queue) 3247 | magmaCheckStatus(status) 3248 | 3249 | 3250 | _libmagma_sparse.magma_dsolverinfo.restype = int 3251 | _libmagma_sparse.magma_dsolverinfo.argtypes = [ctypes.POINTER(magma_d_solver_par), 3252 | ctypes.POINTER(magma_d_preconditioner), 3253 | magma_queue_t] 3254 | def magma_dsolverinfo(solver_par, precond_par, queue): 3255 | status = _libmagma_sparse.magma_dsolverinfo(ctypes.byref(solver_par), ctypes.byref(precond_par), queue) 3256 | magmaCheckStatus(status) 3257 | 3258 | 3259 | _libmagma_sparse.magma_ssolverinfo.restype = int 3260 | _libmagma_sparse.magma_ssolverinfo.argtypes = [ctypes.POINTER(magma_s_solver_par), 3261 | ctypes.POINTER(magma_s_preconditioner), 3262 | magma_queue_t] 3263 | def magma_ssolverinfo(solver_par, precond_par, queue): 3264 | status = _libmagma_sparse.magma_ssolverinfo(ctypes.byref(solver_par), ctypes.byref(precond_par), queue) 3265 | magmaCheckStatus(status) 3266 | 3267 | 3268 | _libmagma_sparse.magma_zsolverinfo_free.restype = int 3269 | _libmagma_sparse.magma_zsolverinfo_free.argtypes = [ctypes.POINTER(magma_z_solver_par), 3270 | ctypes.POINTER(magma_z_preconditioner), 3271 | magma_queue_t] 3272 | def magma_zsolverinfo_free(solver_par, precond, queue): 3273 | status = _libmagma_sparse.magma_zsolverinfo_free(ctypes.byref(solver_par), ctypes.byref(precond), queue) 3274 | magmaCheckStatus(status) 3275 | 3276 | 3277 | _libmagma_sparse.magma_csolverinfo_free.restype = int 3278 | _libmagma_sparse.magma_csolverinfo_free.argtypes = [ctypes.POINTER(magma_c_solver_par), 3279 | ctypes.POINTER(magma_c_preconditioner), 3280 | magma_queue_t] 3281 | def magma_csolverinfo_free(solver_par, precond, queue): 3282 | status = _libmagma_sparse.magma_csolverinfo_free(ctypes.byref(solver_par), ctypes.byref(precond), queue) 3283 | magmaCheckStatus(status) 3284 | 3285 | 3286 | _libmagma_sparse.magma_dsolverinfo_free.restype = int 3287 | _libmagma_sparse.magma_dsolverinfo_free.argtypes = [ctypes.POINTER(magma_d_solver_par), 3288 | ctypes.POINTER(magma_d_preconditioner), 3289 | magma_queue_t] 3290 | def magma_dsolverinfo_free(solver_par, precond, queue): 3291 | status = _libmagma_sparse.magma_dsolverinfo_free(ctypes.byref(solver_par), ctypes.byref(precond), queue) 3292 | magmaCheckStatus(status) 3293 | 3294 | 3295 | _libmagma_sparse.magma_ssolverinfo_free.restype = int 3296 | _libmagma_sparse.magma_ssolverinfo_free.argtypes = [ctypes.POINTER(magma_s_solver_par), 3297 | ctypes.POINTER(magma_s_preconditioner), 3298 | magma_queue_t] 3299 | def magma_ssolverinfo_free(solver_par, precond, queue): 3300 | status = _libmagma_sparse.magma_ssolverinfo_free(ctypes.byref(solver_par), ctypes.byref(precond), queue) 3301 | magmaCheckStatus(status) 3302 | 3303 | 3304 | _libmagma_sparse.magma_z_solver.restype = int 3305 | _libmagma_sparse.magma_z_solver.argtypes = [magma_z_matrix, 3306 | magma_z_matrix, 3307 | ctypes.POINTER(magma_z_matrix), 3308 | ctypes.POINTER(magma_zopts), 3309 | magma_queue_t] 3310 | def magma_z_solver(A, b, x, opts, queue): 3311 | return _libmagma_sparse.magma_z_solver(A, b, ctypes.byref(x), ctypes.byref(opts), queue) 3312 | 3313 | 3314 | _libmagma_sparse.magma_c_solver.restype = int 3315 | _libmagma_sparse.magma_c_solver.argtypes = [magma_c_matrix, 3316 | magma_c_matrix, 3317 | ctypes.POINTER(magma_c_matrix), 3318 | ctypes.POINTER(magma_copts), 3319 | magma_queue_t] 3320 | def magma_c_solver(A, b, x, opts, queue): 3321 | return _libmagma_sparse.magma_c_solver(A, b, ctypes.byref(x), ctypes.byref(opts), queue) 3322 | 3323 | 3324 | _libmagma_sparse.magma_d_solver.restype = int 3325 | _libmagma_sparse.magma_d_solver.argtypes = [magma_d_matrix, 3326 | magma_d_matrix, 3327 | ctypes.POINTER(magma_d_matrix), 3328 | ctypes.POINTER(magma_dopts), 3329 | magma_queue_t] 3330 | def magma_d_solver(A, b, x, opts, queue): 3331 | return _libmagma_sparse.magma_d_solver(A, b, ctypes.byref(x), ctypes.byref(opts), queue) 3332 | 3333 | 3334 | _libmagma_sparse.magma_s_solver.restype = int 3335 | _libmagma_sparse.magma_s_solver.argtypes = [magma_s_matrix, 3336 | magma_s_matrix, 3337 | ctypes.POINTER(magma_s_matrix), 3338 | ctypes.POINTER(magma_sopts), 3339 | magma_queue_t] 3340 | def magma_s_solver(A, b, x, opts, queue): 3341 | return _libmagma_sparse.magma_s_solver(A, b, ctypes.byref(x), ctypes.byref(opts), queue) 3342 | 3343 | 3344 | _libmagma_sparse.magma_z_precondsetup.restype = int 3345 | _libmagma_sparse.magma_z_precondsetup.argtypes = [magma_z_matrix, 3346 | magma_z_matrix, 3347 | ctypes.POINTER(magma_z_solver_par), 3348 | ctypes.POINTER(magma_z_preconditioner), 3349 | magma_queue_t] 3350 | def magma_z_precondsetup(A, b, solver, precond, queue): 3351 | status = _libmagma_sparse.magma_z_precondsetup(A, b, ctypes.byref(solver), ctypes.byref(precond), queue) 3352 | magmaCheckStatus(status) 3353 | 3354 | 3355 | _libmagma_sparse.magma_c_precondsetup.restype = int 3356 | _libmagma_sparse.magma_c_precondsetup.argtypes = [magma_c_matrix, 3357 | magma_c_matrix, 3358 | ctypes.POINTER(magma_c_solver_par), 3359 | ctypes.POINTER(magma_c_preconditioner), 3360 | magma_queue_t] 3361 | def magma_c_precondsetup(A, b, solver, precond, queue): 3362 | status = _libmagma_sparse.magma_c_precondsetup(A, b, ctypes.byref(solver), ctypes.byref(precond), queue) 3363 | magmaCheckStatus(status) 3364 | 3365 | 3366 | _libmagma_sparse.magma_d_precondsetup.restype = int 3367 | _libmagma_sparse.magma_d_precondsetup.argtypes = [magma_d_matrix, 3368 | magma_d_matrix, 3369 | ctypes.POINTER(magma_d_solver_par), 3370 | ctypes.POINTER(magma_d_preconditioner), 3371 | magma_queue_t] 3372 | def magma_d_precondsetup(A, b, solver, precond, queue): 3373 | status = _libmagma_sparse.magma_d_precondsetup(A, b, ctypes.byref(solver), ctypes.byref(precond), queue) 3374 | magmaCheckStatus(status) 3375 | 3376 | 3377 | _libmagma_sparse.magma_s_precondsetup.restype = int 3378 | _libmagma_sparse.magma_s_precondsetup.argtypes = [magma_s_matrix, 3379 | magma_s_matrix, 3380 | ctypes.POINTER(magma_s_solver_par), 3381 | ctypes.POINTER(magma_s_preconditioner), 3382 | magma_queue_t] 3383 | def magma_s_precondsetup(A, b, solver, precond, queue): 3384 | status = _libmagma_sparse.magma_s_precondsetup(A, b, ctypes.byref(solver), ctypes.byref(precond), queue) 3385 | magmaCheckStatus(status) 3386 | 3387 | 3388 | def free(A): 3389 | if isinstance(A, magma_vector): 3390 | cuda.cudaFree(A.pointer) 3391 | elif isinstance(A, magma_matrix): 3392 | if A.dtype == np.complex128: 3393 | magma_zmfree(A.magma_t_matrix, A.queue) 3394 | elif A.dtype == np.complex64: 3395 | magma_cmfree(A.magma_t_matrix, A.queue) 3396 | elif A.dtype == np.float64: 3397 | magma_dmfree(A.magma_t_matrix, A.queue) 3398 | elif A.dtype == np.float32: 3399 | magma_smfree(A.magma_t_matrix, A.queue) 3400 | else: 3401 | raise ValueError('free: Type not supported.') 3402 | 3403 | 3404 | def magma_free(A): 3405 | free(A) 3406 | 3407 | 3408 | def magma_csrset(m, n, indptr, indices, val, A): 3409 | """ 3410 | A is a magma_matrix 3411 | m is int 3412 | n is int 3413 | val is ndarray of type dtype 3414 | indptr is ndarray of type int32 3415 | indices is ndarray of type int32 3416 | """ 3417 | 3418 | if val.dtype == np.complex128: 3419 | magma_zcsrset(m, n, indptr, indices, val, A.magma_t_matrix, A.queue) 3420 | elif val.dtype == np.complex64: 3421 | magma_ccsrset(m, n, indptr, indices, val, A.magma_t_matrix, A.queue) 3422 | elif val.dtype == np.float64: 3423 | magma_dcsrset(m, n, indptr, indices, val, A.magma_t_matrix, A.queue) 3424 | elif val.dtype == np.float32: 3425 | magma_scsrset(m, n, indptr, indices, val, A.magma_t_matrix, A.queue) 3426 | else: 3427 | raise ValueError('magma_csrset: Type not supported.') 3428 | 3429 | 3430 | # TODO: 3431 | #def magma_csrget(A, queue): 3432 | 3433 | 3434 | def magma_solver(A, b, x, opts): 3435 | """ 3436 | A is magma_matrix 3437 | b is magma_matrix 3438 | x is magma_matrix 3439 | opts is magma_xopts with x = z, c, d, s 3440 | queue is of type magma_queue_t 3441 | """ 3442 | 3443 | if A.dtype == np.complex128: 3444 | return magma_z_solver(A.magma_t_matrix, b.magma_t_matrix, x.magma_t_matrix, opts, A.queue) 3445 | elif A.dtype == np.complex64: 3446 | return magma_c_solver(A.magma_t_matrix, b.magma_t_matrix, x.magma_t_matrix, opts, A.queue) 3447 | elif A.dtype == np.float64: 3448 | return magma_d_solver(A.magma_t_matrix, b.magma_t_matrix, x.magma_t_matrix, opts, A.queue) 3449 | elif A.dtype == np.float32: 3450 | return magma_s_solver(A.magma_t_matrix, b.magma_t_matrix, x.magma_t_matrix, opts, A.queue) 3451 | else: 3452 | raise ValueError('magma_solver: Type not supported.') 3453 | 3454 | 3455 | def magma_precondsetup(A, b, solver, precond): 3456 | """ 3457 | A is magma_matrix 3458 | b is magma_matrix 3459 | solver is magma_x_solver_par with x = z, c, d, s 3460 | precond is magma_x_preconditioner with x = z, c, d, s 3461 | queue is of type magma_queue_t 3462 | """ 3463 | 3464 | if A.dtype == np.complex128: 3465 | magma_z_precondsetup(A.magma_t_matrix, b.magma_t_matrix, solver, precond, A.queue) 3466 | elif A.dtype == np.complex64: 3467 | magma_c_precondsetup(A.magma_t_matrix, b.magma_t_matrix, solver, precond, A.queue) 3468 | elif A.dtype == np.float64: 3469 | magma_d_precondsetup(A.magma_t_matrix, b.magma_t_matrix, solver, precond, A.queue) 3470 | elif A.dtype == np.float32: 3471 | magma_s_precondsetup(A.magma_t_matrix, b.magma_t_matrix, solver, precond, A.queue) 3472 | else: 3473 | raise ValueError('magma_precondsetup: Type not supported.') 3474 | 3475 | 3476 | def magma_solverinfo(solver_par, precond_par, queue): 3477 | """ 3478 | solver_par is magma_x_solver_par with x = z, c, d, s 3479 | precond is magma_x_preconditioner with x = z, c, d, s 3480 | queue is of type magma_queue_t 3481 | """ 3482 | 3483 | if isinstance(solver_par, magma_z_solver_par): 3484 | magma_zsolverinfo(solver_par, precond_par, queue) 3485 | elif isinstance(solver_par, magma_c_solver_par): 3486 | magma_csolverinfo(solver_par, precond_par, queue) 3487 | elif isinstance(solver_par, magma_d_solver_par): 3488 | magma_dsolverinfo(solver_par, precond_par, queue) 3489 | elif isinstance(solver_par, magma_s_solver_par): 3490 | magma_ssolverinfo(solver_par, precond_par, queue) 3491 | else: 3492 | raise ValueError('magma_solverinfo: Type not supported.') 3493 | 3494 | 3495 | def magma_solverinfo_init(solver_par, precond, queue): 3496 | """ 3497 | solver_par is magma_x_solver_par with x = z, c, d, s 3498 | precond is magma_x_preconditioner with x = z, c, d, s 3499 | queue is of type magma_queue_t 3500 | """ 3501 | 3502 | if isinstance(solver_par, magma_z_solver_par): 3503 | magma_zsolverinfo_init(solver_par, precond, queue) 3504 | elif isinstance(solver_par, magma_c_solver_par): 3505 | magma_csolverinfo_init(solver_par, precond, queue) 3506 | elif isinstance(solver_par, magma_d_solver_par): 3507 | magma_dsolverinfo_init(solver_par, precond, queue) 3508 | elif isinstance(solver_par, magma_s_solver_par): 3509 | magma_ssolverinfo_init(solver_par, precond, queue) 3510 | else: 3511 | raise ValueError('magma_solverinfo_init: Type not supported.') 3512 | 3513 | 3514 | def magma_solverinfo_free(solver_par, precond, queue): 3515 | """ 3516 | solver_par is magma_x_solver_par with x = z, c, d, s 3517 | precond is magma_x_preconditioner with x = z, c, d, s 3518 | queue is of type magma_queue_t 3519 | """ 3520 | 3521 | if isinstance(solver_par, magma_z_solver_par): 3522 | magma_zsolverinfo_free(solver_par, precond, queue) 3523 | elif isinstance(solver_par, magma_c_solver_par): 3524 | magma_csolverinfo_free(solver_par, precond, queue) 3525 | elif isinstance(solver_par, magma_d_solver_par): 3526 | magma_dsolverinfo_free(solver_par, precond, queue) 3527 | elif isinstance(solver_par, magma_s_solver_par): 3528 | magma_ssolverinfo_free(solver_par, precond, queue) 3529 | else: 3530 | raise ValueError('magma_solverinfo_free: Type not supported.') 3531 | 3532 | 3533 | def magma_opts_default(dtype = np.complex128): 3534 | if dtype == np.complex128: 3535 | return magma_zopts_default() 3536 | elif dtype == np.complex64: 3537 | return magma_copts_default() 3538 | elif dtype == np.float64: 3539 | return magma_dopts_default() 3540 | elif dtype == np.float32: 3541 | return magma_sopts_default() 3542 | else: 3543 | raise ValueError('magma_opts_default: Type not supported.') 3544 | 3545 | 3546 | def magma_mconvert(A, B, old_format, new_format): 3547 | """ 3548 | A is a magma_matrix 3549 | B is a magma_matrix 3550 | old_format is a int 3551 | new_format is a int 3552 | queue is of type magma_queue_t 3553 | """ 3554 | 3555 | if A.dtype == np.complex128: 3556 | magma_zmconvert(A.magma_t_matrix, B.magma_t_matrix, old_format, new_format, A.queue) 3557 | elif A.dtype == np.complex64: 3558 | magma_cmconvert(A.magma_t_matrix, B.magma_t_matrix, old_format, new_format, A.queue) 3559 | elif A.dtype == np.float64: 3560 | magma_dmconvert(A.magma_t_matrix, B.magma_t_matrix, old_format, new_format, A.queue) 3561 | elif A.dtype == np.float32: 3562 | magma_smconvert(A.magma_t_matrix, B.magma_t_matrix, old_format, new_format, A.queue) 3563 | else: 3564 | raise ValueError('magma_mconvert: Type not supported.') 3565 | 3566 | 3567 | def magma_mscale(A, scaling): 3568 | """ 3569 | A is magma_matrix 3570 | queue is of type magma_queue_t 3571 | """ 3572 | 3573 | if A.dtype == np.complex128: 3574 | magma_zmscale(A.magma_t_matrix, scaling, A.queue) 3575 | elif A.dtype == np.complex64: 3576 | magma_cmscale(A.magma_t_matrix, scaling, A.queue) 3577 | elif A.dtype == np.float64: 3578 | magma_dmscale(A.magma_t_matrix, scaling, A.queue) 3579 | elif A.dtype == np.float32: 3580 | magma_smscale(A.magma_t_matrix, scaling, A.queue) 3581 | else: 3582 | raise ValueError('magma_mscale: Type not supported.') 3583 | 3584 | 3585 | def magma_mtransfer(A, B, src, dst): 3586 | """ 3587 | A is magma_matrix 3588 | B is magma_matrix 3589 | src is from magma_location_t 3590 | dst is from magma_location_t 3591 | queue is of type magma_queue_t 3592 | """ 3593 | 3594 | if A.dtype == np.complex128: 3595 | magma_zmtransfer(A.magma_t_matrix, B.magma_t_matrix, src, dst, A.queue) 3596 | elif A.dtype == np.complex64: 3597 | magma_cmtransfer(A.magma_t_matrix, B.magma_t_matrix, src, dst, A.queue) 3598 | elif A.dtype == np.float64: 3599 | magma_dmtransfer(A.magma_t_matrix, B.magma_t_matrix, src, dst, A.queue) 3600 | elif A.dtype == np.float32: 3601 | magma_smtransfer(A.magma_t_matrix, B.magma_t_matrix, src, dst, A.queue) 3602 | else: 3603 | raise ValueError('magma_mtransfer: Type not supported.') 3604 | 3605 | 3606 | def magma_spmv(alpha, A, x, beta, y): 3607 | """ 3608 | A is magma_matrix, 3609 | x is magma_matrix, 3610 | y is magma_matrix, 3611 | alpha is np.complex128 or np.float64, 3612 | beta is np.complex128 or np.float64, 3613 | queue is of type magma_queue_t. 3614 | """ 3615 | 3616 | if A.dtype == np.complex128: 3617 | alpha = np.complex128(alpha) 3618 | alpha_magma = cuda.cuDoubleComplex() 3619 | alpha_magma.x = alpha.real 3620 | alpha_magma.y = alpha.imag 3621 | 3622 | beta = np.complex128(beta) 3623 | beta_magma = cuda.cuDoubleComplex() 3624 | beta_magma.x = beta.real 3625 | beta_magma.y = beta.imag 3626 | 3627 | magma_z_spmv(alpha_magma, A.magma_t_matrix, x.magma_t_matrix, beta_magma, y.magma_t_matrix, A.queue) 3628 | elif A.dtype == np.complex64: 3629 | alpha = np.complex64(alpha) 3630 | alpha_magma = cuda.cuFloatComplex() 3631 | alpha_magma.x = alpha.real 3632 | alpha_magma.y = alpha.imag 3633 | 3634 | beta = np.complex64(beta) 3635 | beta_magma = cuda.cuFloatComplex() 3636 | beta_magma.x = beta.real 3637 | beta_magma.y = beta.imag 3638 | 3639 | magma_c_spmv(alpha_magma, A.magma_t_matrix, x.magma_t_matrix, beta_magma, y.magma_t_matrix, A.queue) 3640 | elif A.dtype == np.float64: 3641 | magma_d_spmv(alpha, A.magma_t_matrix, x.magma_t_matrix, beta, y.magma_t_matrix, A.queue) 3642 | elif A.dtype == np.float32: 3643 | magma_s_spmv(alpha, A.magma_t_matrix, x.magma_t_matrix, beta, y.magma_t_matrix, A.queue) 3644 | else: 3645 | raise ValueError('magma_spmv: Type not supported.') 3646 | 3647 | 3648 | def magma_vget(v): 3649 | """ 3650 | v is of type magma_matrix 3651 | queue is of type magma_queue_t 3652 | """ 3653 | 3654 | if v.dtype == np.complex128: 3655 | return magma_zvget(v.magma_t_matrix, v.queue) 3656 | elif v.dtype == np.complex64: 3657 | return magma_cvget(v.magma_t_matrix, v.queue) 3658 | elif v.dtype == np.float64: 3659 | return magma_dvget(v.magma_t_matrix, v.queue) 3660 | elif v.dtype == np.float32: 3661 | return magma_svget(v.magma_t_matrix, v.queue) 3662 | else: 3663 | raise ValueError('magma_vget: Type not supported.') 3664 | 3665 | 3666 | def magma_vcopy(n, x, incx, y, incy): 3667 | """ 3668 | n is the array length asint 3669 | x is a magma_matrix 3670 | incx is an int 3671 | y is a magma_matrix 3672 | incy is an int 3673 | """ 3674 | 3675 | if x.dtype == np.complex128: 3676 | magma_zcopy( n, x.magma_t_matrix.dval, incx, y.magma_t_matrix.dval, incy) 3677 | elif x.dtype == np.complex64: 3678 | magma_ccopy( n, x.magma_t_matrix.dval, incx, y.magma_t_matrix.dval, incy) 3679 | elif x.dtype == np.float64: 3680 | magma_dcopy( n, x.magma_t_matrix.dval, incx, y.magma_t_matrix.dval, incy) 3681 | elif x.dtype == np.float32: 3682 | magma_scopy( n, x.magma_t_matrix.dval, incx, y.magma_t_matrix.dval, incy) 3683 | else: 3684 | raise ValueError('magma_vcopy: Type not supported.') 3685 | 3686 | 3687 | def magma_vinit(x, memory_location, num_rows, num_cols, values): 3688 | """ 3689 | x is a magma_matrix 3690 | memory_location is int 3691 | num_rows is int 3692 | num_cols is int 3693 | values is np.complex128 or np.float64 3694 | queue is of type magma_queue_t 3695 | """ 3696 | 3697 | if x.dtype == np.complex128: 3698 | values_magma = cuda.cuDoubleComplex() 3699 | values_magma.x = values.real 3700 | values_magma.y = values.imag 3701 | 3702 | magma_zvinit(x.magma_t_matrix, memory_location, num_rows, num_cols, values_magma, x.queue) 3703 | elif x.dtype == np.complex64: 3704 | values_magma = cuda.cuFloatComplex() 3705 | values_magma.x = values.real 3706 | values_magma.y = values.imag 3707 | 3708 | magma_cvinit(x.magma_t_matrix, memory_location, num_rows, num_cols, values_magma, x.queue) 3709 | elif x.dtype == np.float64: 3710 | magma_dvinit(x.magma_t_matrix, memory_location, num_rows, num_cols, values, x.queue) 3711 | elif x.dtype == np.float32: 3712 | magma_svinit(x.magma_t_matrix, memory_location, num_rows, num_cols, values, x.queue) 3713 | else: 3714 | raise ValueError('magma_vinit: Type not supported.') 3715 | 3716 | 3717 | class magma_matrix: 3718 | 3719 | def __init__(self, 3720 | A = None, 3721 | queue = None, 3722 | memory_location = magma_location_t.Magma_DEV, 3723 | storage = magma_storage_t.Magma_CSR, 3724 | dtype = np.complex128, 3725 | blocksize = 32, 3726 | alignment = 1): 3727 | 3728 | """ 3729 | A is a scipy sparse matrix or a ndarray. 3730 | If A is a ndarray, then a (m,1) sparse matrix is created from the array. 3731 | """ 3732 | 3733 | self.memory_location = memory_location 3734 | self.magma_t_matrix = magma_t_matrix(magma_storage_t.Magma_CSR) 3735 | self.storage = storage 3736 | self.dtype = dtype 3737 | self.queue = queue 3738 | self.blocksize = blocksize 3739 | self.alignment = alignment 3740 | 3741 | if A is not None: 3742 | self.dtype = A.dtype 3743 | 3744 | A_CPU = magma_t_matrix(magma_storage_t.Magma_CSR) 3745 | A_CPU.memory_location = magma_location_t.Magma_CPU 3746 | 3747 | if isinstance(A, np.ndarray): 3748 | if self.dtype == np.complex128: 3749 | magma_zvset(A.size, 1, A, A_CPU, queue) 3750 | magma_zmtransfer(A_CPU, self.magma_t_matrix, magma_location_t.Magma_CPU, memory_location, self.queue) 3751 | elif self.dtype == np.complex64: 3752 | magma_cvset(A.size, 1, A, A_CPU, queue) 3753 | magma_cmtransfer(A_CPU, self.magma_t_matrix, magma_location_t.Magma_CPU, memory_location, self.queue) 3754 | elif self.dtype == np.float64: 3755 | magma_dvset(A.size, 1, A, A_CPU, queue) 3756 | magma_dmtransfer(A_CPU, self.magma_t_matrix, magma_location_t.Magma_CPU, memory_location, self.queue) 3757 | elif self.dtype == np.float32: 3758 | magma_svset(A.size, 1, A, A_CPU, queue) 3759 | magma_smtransfer(A_CPU, self.magma_t_matrix, magma_location_t.Magma_CPU, memory_location, self.queue) 3760 | else: 3761 | raise ValueError('__init__: Type not supported.') 3762 | 3763 | else: 3764 | A_CONV = magma_t_matrix(self.storage) 3765 | A_CONV.memory_location = magma_location_t.Magma_CPU 3766 | A_CONV.blocksize = self.blocksize 3767 | A_CONV.alignment = self.alignment 3768 | 3769 | if self.dtype == np.complex128: 3770 | magma_zcsrset(A.shape[0], A.shape[1], A.indptr, A.indices, A.data, A_CPU, self.queue) 3771 | magma_zmconvert(A_CPU, A_CONV, magma_storage_t.Magma_CSR, self.storage, self.queue) 3772 | magma_zmtransfer(A_CONV, self.magma_t_matrix, magma_location_t.Magma_CPU, memory_location, self.queue) 3773 | elif self.dtype == np.complex64: 3774 | magma_ccsrset(A.shape[0], A.shape[1], A.indptr, A.indices, A.data, A_CPU, self.queue) 3775 | magma_cmconvert(A_CPU, A_CONV, magma_storage_t.Magma_CSR, self.storage, self.queue) 3776 | magma_cmtransfer(A_CONV, self.magma_t_matrix, magma_location_t.Magma_CPU, memory_location, self.queue) 3777 | elif self.dtype == np.float64: 3778 | magma_dcsrset(A.shape[0], A.shape[1], A.indptr, A.indices, A.data, A_CPU, self.queue) 3779 | magma_dmconvert(A_CPU, A_CONV, magma_storage_t.Magma_CSR, self.storage, self.queue) 3780 | magma_dmtransfer(A_CONV, self.magma_t_matrix, magma_location_t.Magma_CPU, memory_location, self.queue) 3781 | elif self.dtype == np.float32: 3782 | magma_scsrset(A.shape[0], A.shape[1], A.indptr, A.indices, A.data, A_CPU, self.queue) 3783 | magma_smconvert(A_CPU, A_CONV, magma_storage_t.Magma_CSR, self.storage, self.queue) 3784 | magma_smtransfer(A_CONV, self.magma_t_matrix, magma_location_t.Magma_CPU, memory_location, self.queue) 3785 | else: 3786 | raise ValueError('__init__: Type not supported.') 3787 | 3788 | 3789 | def get_data(self): 3790 | #if self.memory_location == magma_location_t.Magma_DEV: 3791 | A = magma_t_matrix(magma_storage_t.Magma_CSR) 3792 | A.memory_location = magma_location_t.Magma_CPU 3793 | 3794 | if self.dtype == np.complex128: 3795 | magma_zmtransfer(self.magma_t_matrix, A, magma_location_t.Magma_DEV, magma_location_t.Magma_CPU, self.queue) 3796 | m, n, val = magma_zvget(A, self.queue) 3797 | elif self.dtype == np.complex64: 3798 | magma_cmtransfer(self.magma_t_matrix, A, magma_location_t.Magma_DEV, magma_location_t.Magma_CPU, self.queue) 3799 | m, n, val = magma_cvget(A, self.queue) 3800 | elif self.dtype == np.float64: 3801 | magma_dmtransfer(self.magma_t_matrix, A, magma_location_t.Magma_DEV, magma_location_t.Magma_CPU, self.queue) 3802 | m, n, val = magma_dvget(A, self.queue) 3803 | elif self.dtype == np.float32: 3804 | magma_smtransfer(self.magma_t_matrix, A, magma_location_t.Magma_DEV, magma_location_t.Magma_CPU, self.queue) 3805 | m, n, val = magma_svget(A, self.queue) 3806 | else: 3807 | raise ValueError('get_data: Type not supported.') 3808 | 3809 | return val 3810 | 3811 | 3812 | class magma_vector: 3813 | def __init__(self, A, memory_space = 'device'): 3814 | if isinstance(A, np.ndarray): 3815 | self.memory_space = memory_space 3816 | self.size = A.shape[0] 3817 | self.dtype = A.dtype 3818 | self.nbytes = self.size*self.dtype.itemsize 3819 | 3820 | if memory_space == 'device': 3821 | self.pointer = cuda.cudaMalloc(self.nbytes) 3822 | cuda.cudaMemcpy_htod(self.pointer, A.ctypes.data_as(ctypes.c_void_p) , self.nbytes) 3823 | else: 3824 | raise ValueError('magma_vector: not implemented.') 3825 | else: 3826 | raise ValueError('magma_vector: Type not supported for construction.') 3827 | 3828 | def to_ndarray(self): 3829 | a = np.zeros((self.size,), dtype = self.dtype) 3830 | if self.memory_space == 'device': 3831 | cuda.cudaMemcpy_dtoh(a.ctypes.data_as(ctypes.c_void_p), self.pointer, self.nbytes) 3832 | return a 3833 | 3834 | @property 3835 | def dev_ptr(self): 3836 | return self.pointer.value 3837 | 3838 | def __int__(self): 3839 | return self.dev_ptr 3840 | 3841 | def iamax(self, incx = 1): 3842 | """ 3843 | Index of maximum magnitude element. 3844 | """ 3845 | if self.memory_space == 'device': 3846 | if self.dtype == np.complex128: 3847 | return magma_izamax(self.size, self, incx) 3848 | elif self.dtype == np.complex64: 3849 | return magma_icamax(self.size, self, incx) 3850 | elif self.dtype == np.float64: 3851 | return magma_idamax(self.size, self, incx) 3852 | elif self.dtype == np.float32: 3853 | return magma_isamax(self.size, self, incx) 3854 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2016, Carlo Holly. 3 | # All rights reserved. 4 | # 5 | 6 | #!/usr/bin/env python 7 | 8 | from distutils.core import setup 9 | 10 | setup(name='pymagma', 11 | version='0.0.1', 12 | description='A Python interface to the MAGMA libraries', 13 | author='Carlo Holly', 14 | author_email='carlo.holly@rwth-aachen.de', 15 | url='https://github.com/carloholly/pymagma', 16 | packages=['pymagma'] 17 | ) --------------------------------------------------------------------------------