├── clpy ├── __init__.pxd ├── core │ ├── __init__.pxd │ ├── include │ │ ├── clpy │ │ │ ├── complex │ │ │ │ ├── README.md │ │ │ │ ├── cpow.h │ │ │ │ └── cproj.h │ │ │ └── complex.cuh │ │ ├── cuda_stub.hpp │ │ └── cupy │ │ │ └── carray.hpp │ ├── flags.pyx │ └── internal.pxd ├── testing │ ├── bufio.pxd │ ├── bufio.pyx │ ├── parameterized.py │ └── attr.py ├── backend │ ├── __init__.pxd │ ├── cuda │ │ ├── __init__.pxd │ │ ├── memory_hook.pxd │ │ ├── device.pxd │ │ ├── memory_hooks │ │ │ └── __init__.py │ │ ├── profiler.pxd │ │ ├── cupy_common.h │ │ ├── common.pxd │ │ ├── nccl.pxd │ │ ├── cupy_stdint.h │ │ ├── cupy_cuComplex.h │ │ ├── function.pxd │ │ ├── nvrtc.pxd │ │ ├── pinned_memory.pxd │ │ ├── cupy_thrust.h │ │ ├── curand.pxd │ │ ├── cupy_nvrtc.h │ │ ├── cusparse.pxd │ │ ├── profiler.pyx │ │ └── driver.pxd │ ├── opencl │ │ ├── __init__.py │ │ ├── clblast │ │ │ └── __init__.py │ │ ├── types.pxd │ │ ├── .gitignore │ │ ├── common_decl.pxi │ │ ├── exceptions.pxd │ │ ├── env.pxd │ │ ├── random.pxd │ │ └── utility.pxd │ ├── ultima │ │ ├── __init__.py │ │ ├── compiler.pxd │ │ └── exceptions.py │ ├── compiler.pxd │ ├── device.pxd │ ├── common.pxd │ ├── compiler.pyx │ ├── pinned_memory.pxd │ └── function.pxd ├── _version.py ├── ext │ ├── __init__.py │ └── scatter.py ├── internal.py ├── prof │ └── __init__.py ├── __main__.py ├── io │ ├── rawfile.py │ ├── __init__.py │ ├── text.py │ └── formatting.py ├── math │ ├── special.py │ ├── __init__.py │ ├── ufunc.py │ ├── hyperbolic.py │ └── rounding.py ├── padding │ └── __init__.py ├── binary │ ├── __init__.py │ └── elementwise.py ├── statistics │ ├── correlation.py │ └── __init__.py ├── sorting │ ├── __init__.py │ └── count.py ├── indexing │ ├── __init__.py │ ├── insert.py │ └── indexing.py ├── creation │ ├── __init__.py │ └── matrix.py ├── logic │ ├── type_test.py │ ├── __init__.py │ ├── comparison.py │ ├── ops.py │ ├── content.py │ └── truth.py ├── random │ ├── permutations.py │ └── __init__.py ├── manipulation │ ├── add_remove.py │ ├── __init__.py │ ├── kind.py │ ├── shape.py │ └── transpose.py ├── sparse │ ├── util.py │ └── __init__.py └── linalg │ ├── __init__.py │ └── util.py ├── cupy_alias ├── cuda │ ├── nccl.py │ ├── cublas.py │ └── __init__.py ├── __init__.py ├── cudnn.py ├── io │ ├── npz.py │ ├── __init__.py │ ├── text.py │ ├── rawfile.py │ └── formatting.py ├── core │ ├── __init__.py │ └── fusion.py ├── cusparse.py ├── ext │ ├── __init__.py │ └── scatter.py ├── internal.py ├── math │ ├── __init__.py │ ├── explog.py │ ├── misc.py │ ├── ufunc.py │ ├── window.py │ ├── floating.py │ ├── rounding.py │ ├── special.py │ ├── sumprod.py │ ├── arithmetic.py │ ├── hyperbolic.py │ └── trigonometric.py ├── prof │ ├── __init__.py │ └── time_range.py ├── binary │ ├── __init__.py │ ├── packing.py │ └── elementwise.py ├── linalg │ ├── __init__.py │ ├── util.py │ ├── einsum.py │ ├── norms.py │ ├── solve.py │ ├── product.py │ ├── eigenvalue.py │ └── decomposition.py ├── logic │ ├── __init__.py │ ├── ops.py │ ├── truth.py │ ├── content.py │ ├── comparison.py │ └── type_test.py ├── padding │ ├── pad.py │ └── __init__.py ├── random │ ├── __init__.py │ ├── sample.py │ ├── generator.py │ ├── distributions.py │ └── permutations.py ├── sparse │ ├── __init__.py │ ├── base.py │ ├── coo.py │ ├── csc.py │ ├── csr.py │ ├── data.py │ ├── dia.py │ ├── util.py │ ├── construct.py │ └── compressed.py ├── backend │ ├── __init__.py │ ├── stream.py │ ├── opencl │ │ └── __init__.py │ └── ultima │ │ └── __init__.py ├── creation │ ├── __init__.py │ ├── basic.py │ ├── matrix.py │ ├── ranges.py │ └── from_data.py ├── indexing │ ├── __init__.py │ ├── insert.py │ ├── generate.py │ └── indexing.py ├── sorting │ ├── __init__.py │ ├── count.py │ ├── sort.py │ └── search.py ├── testing │ ├── __init__.py │ ├── array.py │ ├── attr.py │ ├── helper.py │ ├── random.py │ ├── condition.py │ ├── hypothesis.py │ └── parameterized.py ├── statistics │ ├── __init__.py │ ├── order.py │ ├── meanvar.py │ ├── histogram.py │ └── correlation.py └── manipulation │ ├── __init__.py │ ├── basic.py │ ├── dims.py │ ├── join.py │ ├── kind.py │ ├── shape.py │ ├── split.py │ ├── tiling.py │ ├── add_remove.py │ ├── rearrange.py │ └── transpose.py ├── install ├── __init__.py └── utils.py ├── tests ├── clpy_tests │ ├── __init__.py │ ├── ext_tests │ │ ├── __init__.py │ │ └── test_scatter.py │ ├── io_tests │ │ ├── __init__.py │ │ ├── test_text.py │ │ ├── test_rawfile.py │ │ ├── test_formatting.py │ │ └── test_npz.py │ ├── binary_tests │ │ ├── __init__.py │ │ ├── test_elementwise.py │ │ └── test_packing.py │ ├── core_tests │ │ ├── __init__.py │ │ ├── test_ndarray_contiguity.py │ │ ├── test_ndarray_owndata.py │ │ ├── test_core.py │ │ ├── test_flags.py │ │ ├── test_scan.py │ │ ├── test_carray.py │ │ ├── test_ndarray_get.py │ │ └── test_function.py │ ├── creation_tests │ │ ├── __init__.py │ │ └── test_matrix.py │ ├── cuda_tests │ │ ├── __init__.py │ │ ├── memory_hooks_tests │ │ │ ├── __init__.py │ │ │ ├── test_line_profile.py │ │ │ └── test_debug_print.py │ │ ├── test_cusolver.py │ │ ├── test_nvtx.py │ │ ├── test_stream.py │ │ ├── test_profile.py │ │ ├── test_compiler.py │ │ └── test_curand.py │ ├── indexing_tests │ │ ├── __init__.py │ │ └── test_insert.py │ ├── linalg_tests │ │ └── __init__.py │ ├── logic_tests │ │ ├── __init__.py │ │ ├── test_type_test.py │ │ ├── test_comparison.py │ │ ├── test_content.py │ │ └── test_ops.py │ ├── math_tests │ │ ├── __init__.py │ │ ├── test_special.py │ │ ├── test_window.py │ │ ├── test_rounding.py │ │ ├── test_hyperbolic.py │ │ ├── test_trigonometric.py │ │ ├── test_explog.py │ │ └── test_floating.py │ ├── padding_tests │ │ └── __init__.py │ ├── prof_tests │ │ └── __init__.py │ ├── random_tests │ │ ├── __init__.py │ │ ├── test_random.py │ │ ├── test_distributions.py │ │ └── test_permutations.py │ ├── sorting_tests │ │ └── __init__.py │ ├── sparse_tests │ │ ├── __init__.py │ │ └── test_construct.py │ ├── statics_tests │ │ ├── __init__.py │ │ └── test_correlation.py │ ├── testing_tests │ │ ├── __init__.py │ │ └── test_parameterized.py │ ├── manipulation_tests │ │ ├── __init__.py │ │ ├── test_add_remove.py │ │ ├── test_transpose.py │ │ ├── test_tiling.py │ │ └── test_split.py │ ├── opencl_tests │ │ ├── test_api.py │ │ ├── test_elementwise.py │ │ ├── headercvt_tests │ │ │ ├── test_headercvt_funcdecl.py │ │ │ └── test_headercvt_preproc_defines.py │ │ ├── test_rollaxis.py │ │ ├── ultima_tests │ │ │ ├── test_cindexer.py │ │ │ ├── test_cast.py │ │ │ └── test_half.py │ │ ├── test_sample_rand.py │ │ ├── test_atomicAdd.py │ │ ├── test_linalg.py │ │ └── test_exception.py │ └── test_numpy_interop.py ├── example_tests │ ├── __init__.py │ ├── test_gemm.py │ ├── example_test.py │ ├── test_gmm.py │ └── test_kmeans.py └── install_tests │ ├── __init__.py │ ├── test_utils.py │ └── test_build.py ├── docs ├── requirements.txt ├── image │ └── cupy_logo_1000px.png └── source │ ├── tutorial │ └── index.rst │ ├── reference │ ├── pad.rst │ ├── ext.rst │ ├── generic.rst │ ├── kernel.rst │ ├── memoize.rst │ ├── prof.rst │ ├── indexing.rst │ ├── sorting.rst │ ├── routines.rst │ ├── statistics.rst │ ├── io.rst │ ├── index.rst │ ├── binary.rst │ ├── creation.rst │ ├── logic.rst │ ├── sparse.rst │ ├── linalg.rst │ ├── cuda.rst │ ├── random.rst │ └── ndarray.rst │ ├── index.rst │ ├── _templates │ └── autosummary │ │ └── class.rst │ └── _static │ └── css │ └── modified_theme.css ├── headercvt ├── stub.c ├── .gitignore └── Makefile ├── ultima ├── .gitignore └── Makefile ├── .pep8 ├── .flake8.cython ├── MANIFEST.in ├── .github └── CONTRIBUTING.md ├── .coveragerc ├── readthedocs.yml ├── setup.cfg ├── .gitignore ├── docker ├── python2 │ └── Dockerfile └── python3 │ └── Dockerfile ├── .travis.yml ├── examples └── gemm │ └── utils.py ├── jenkins └── post_comment.sh └── CONTRIBUTOR_CLPY.md /clpy/__init__.pxd: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /clpy/core/__init__.pxd: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /clpy/testing/bufio.pxd: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /cupy_alias/cuda/nccl.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /install/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /clpy/backend/__init__.pxd: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /cupy_alias/cuda/cublas.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/clpy_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /clpy/backend/cuda/__init__.pxd: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /clpy/backend/opencl/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/example_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/install_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/clpy_tests/ext_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/clpy_tests/io_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /clpy/backend/opencl/clblast/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/requirements.txt: -------------------------------------------------------------------------------- 1 | Cython==0.26.1 2 | -------------------------------------------------------------------------------- /tests/clpy_tests/binary_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/clpy_tests/core_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/clpy_tests/creation_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/clpy_tests/cuda_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/clpy_tests/indexing_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/clpy_tests/linalg_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/clpy_tests/logic_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/clpy_tests/math_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/clpy_tests/padding_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/clpy_tests/prof_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/clpy_tests/random_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/clpy_tests/sorting_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/clpy_tests/sparse_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/clpy_tests/statics_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/clpy_tests/testing_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /clpy/_version.py: -------------------------------------------------------------------------------- 1 | __version__ = '2.1.0rc1' 2 | -------------------------------------------------------------------------------- /headercvt/stub.c: -------------------------------------------------------------------------------- 1 | 2 | # include 3 | -------------------------------------------------------------------------------- /tests/clpy_tests/manipulation_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /ultima/.gitignore: -------------------------------------------------------------------------------- 1 | !ultima.cpp 2 | ultima 3 | -------------------------------------------------------------------------------- /.pep8: -------------------------------------------------------------------------------- 1 | [pep8] 2 | exclude=.eggs,*.egg,build 3 | 4 | -------------------------------------------------------------------------------- /clpy/backend/opencl/types.pxd: -------------------------------------------------------------------------------- 1 | include "types.pxi" 2 | -------------------------------------------------------------------------------- /cupy_alias/__init__.py: -------------------------------------------------------------------------------- 1 | from clpy import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/cudnn.py: -------------------------------------------------------------------------------- 1 | from clpy.cudnn import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/io/npz.py: -------------------------------------------------------------------------------- 1 | from clpy.io.npz import * # NOQA 2 | -------------------------------------------------------------------------------- /tests/clpy_tests/cuda_tests/memory_hooks_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /clpy/backend/cuda/memory_hook.pxd: -------------------------------------------------------------------------------- 1 | cpdef get_memory_hooks() 2 | -------------------------------------------------------------------------------- /cupy_alias/core/__init__.py: -------------------------------------------------------------------------------- 1 | from clpy.core import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/cusparse.py: -------------------------------------------------------------------------------- 1 | from clpy.cusparse import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/ext/__init__.py: -------------------------------------------------------------------------------- 1 | from clpy.ext import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/internal.py: -------------------------------------------------------------------------------- 1 | from clpy.internal import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/io/__init__.py: -------------------------------------------------------------------------------- 1 | from clpy.io import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/io/text.py: -------------------------------------------------------------------------------- 1 | from clpy.io.text import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/math/__init__.py: -------------------------------------------------------------------------------- 1 | from clpy.math import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/prof/__init__.py: -------------------------------------------------------------------------------- 1 | from clpy.prof import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/binary/__init__.py: -------------------------------------------------------------------------------- 1 | from clpy.binary import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/core/fusion.py: -------------------------------------------------------------------------------- 1 | from clpy.core.fusion import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/cuda/__init__.py: -------------------------------------------------------------------------------- 1 | from clpy.backend import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/ext/scatter.py: -------------------------------------------------------------------------------- 1 | from clpy.ext.scatter import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/io/rawfile.py: -------------------------------------------------------------------------------- 1 | from clpy.io.rawfile import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/linalg/__init__.py: -------------------------------------------------------------------------------- 1 | from clpy.linalg import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/linalg/util.py: -------------------------------------------------------------------------------- 1 | from clpy.linalg.util import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/logic/__init__.py: -------------------------------------------------------------------------------- 1 | from clpy.logic import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/logic/ops.py: -------------------------------------------------------------------------------- 1 | from clpy.logic.ops import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/logic/truth.py: -------------------------------------------------------------------------------- 1 | from clpy.logic.truth import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/math/explog.py: -------------------------------------------------------------------------------- 1 | from clpy.math.explog import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/math/misc.py: -------------------------------------------------------------------------------- 1 | from clpy.math.misc import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/math/ufunc.py: -------------------------------------------------------------------------------- 1 | from clpy.math.ufunc import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/math/window.py: -------------------------------------------------------------------------------- 1 | from clpy.math.window import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/padding/pad.py: -------------------------------------------------------------------------------- 1 | from clpy.padding.pad import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/random/__init__.py: -------------------------------------------------------------------------------- 1 | from clpy.random import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/sparse/__init__.py: -------------------------------------------------------------------------------- 1 | from clpy.sparse import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/sparse/base.py: -------------------------------------------------------------------------------- 1 | from clpy.sparse.base import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/sparse/coo.py: -------------------------------------------------------------------------------- 1 | from clpy.sparse.coo import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/sparse/csc.py: -------------------------------------------------------------------------------- 1 | from clpy.sparse.csc import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/sparse/csr.py: -------------------------------------------------------------------------------- 1 | from clpy.sparse.csr import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/sparse/data.py: -------------------------------------------------------------------------------- 1 | from clpy.sparse.data import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/sparse/dia.py: -------------------------------------------------------------------------------- 1 | from clpy.sparse.dia import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/sparse/util.py: -------------------------------------------------------------------------------- 1 | from clpy.sparse.util import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/backend/__init__.py: -------------------------------------------------------------------------------- 1 | from clpy.backend import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/creation/__init__.py: -------------------------------------------------------------------------------- 1 | from clpy.creation import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/indexing/__init__.py: -------------------------------------------------------------------------------- 1 | from clpy.indexing import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/io/formatting.py: -------------------------------------------------------------------------------- 1 | from clpy.io.formatting import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/linalg/einsum.py: -------------------------------------------------------------------------------- 1 | from clpy.linalg.einsum import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/linalg/norms.py: -------------------------------------------------------------------------------- 1 | from clpy.linalg.norms import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/linalg/solve.py: -------------------------------------------------------------------------------- 1 | from clpy.linalg.solve import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/logic/content.py: -------------------------------------------------------------------------------- 1 | from clpy.logic.content import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/math/floating.py: -------------------------------------------------------------------------------- 1 | from clpy.math.floating import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/math/rounding.py: -------------------------------------------------------------------------------- 1 | from clpy.math.rounding import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/math/special.py: -------------------------------------------------------------------------------- 1 | from clpy.math.special import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/math/sumprod.py: -------------------------------------------------------------------------------- 1 | from clpy.math.sumprod import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/padding/__init__.py: -------------------------------------------------------------------------------- 1 | from clpy.padding import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/random/sample.py: -------------------------------------------------------------------------------- 1 | from clpy.random.sample import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/sorting/__init__.py: -------------------------------------------------------------------------------- 1 | from clpy.sorting import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/sorting/count.py: -------------------------------------------------------------------------------- 1 | from clpy.sorting.count import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/sorting/sort.py: -------------------------------------------------------------------------------- 1 | from clpy.sorting.sort import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/testing/__init__.py: -------------------------------------------------------------------------------- 1 | from clpy.testing import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/testing/array.py: -------------------------------------------------------------------------------- 1 | from clpy.testing.array import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/testing/attr.py: -------------------------------------------------------------------------------- 1 | from clpy.testing.attr import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/backend/stream.py: -------------------------------------------------------------------------------- 1 | from clpy.backend.stream import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/binary/packing.py: -------------------------------------------------------------------------------- 1 | from clpy.binary.packing import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/creation/basic.py: -------------------------------------------------------------------------------- 1 | from clpy.creation.basic import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/creation/matrix.py: -------------------------------------------------------------------------------- 1 | from clpy.creation.matrix import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/creation/ranges.py: -------------------------------------------------------------------------------- 1 | from clpy.creation.ranges import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/indexing/insert.py: -------------------------------------------------------------------------------- 1 | from clpy.indexing.insert import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/linalg/product.py: -------------------------------------------------------------------------------- 1 | from clpy.linalg.product import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/logic/comparison.py: -------------------------------------------------------------------------------- 1 | from clpy.logic.comparison import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/logic/type_test.py: -------------------------------------------------------------------------------- 1 | from clpy.logic.type_test import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/math/arithmetic.py: -------------------------------------------------------------------------------- 1 | from clpy.math.arithmetic import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/math/hyperbolic.py: -------------------------------------------------------------------------------- 1 | from clpy.math.hyperbolic import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/prof/time_range.py: -------------------------------------------------------------------------------- 1 | from clpy.prof.time_range import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/random/generator.py: -------------------------------------------------------------------------------- 1 | from clpy.random.generator import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/sorting/search.py: -------------------------------------------------------------------------------- 1 | from clpy.sorting.search import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/sparse/construct.py: -------------------------------------------------------------------------------- 1 | from clpy.sparse.construct import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/statistics/__init__.py: -------------------------------------------------------------------------------- 1 | from clpy.statistics import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/statistics/order.py: -------------------------------------------------------------------------------- 1 | from clpy.statistics.order import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/testing/helper.py: -------------------------------------------------------------------------------- 1 | from clpy.testing.helper import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/testing/random.py: -------------------------------------------------------------------------------- 1 | from clpy.testing.random import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/backend/opencl/__init__.py: -------------------------------------------------------------------------------- 1 | from clpy.backend.opencl import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/backend/ultima/__init__.py: -------------------------------------------------------------------------------- 1 | from clpy.backend.ultima import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/binary/elementwise.py: -------------------------------------------------------------------------------- 1 | from clpy.binary.elementwise import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/creation/from_data.py: -------------------------------------------------------------------------------- 1 | from clpy.creation.from_data import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/indexing/generate.py: -------------------------------------------------------------------------------- 1 | from clpy.indexing.generate import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/indexing/indexing.py: -------------------------------------------------------------------------------- 1 | from clpy.indexing.indexing import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/linalg/eigenvalue.py: -------------------------------------------------------------------------------- 1 | from clpy.linalg.eigenvalue import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/manipulation/__init__.py: -------------------------------------------------------------------------------- 1 | from clpy.manipulation import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/manipulation/basic.py: -------------------------------------------------------------------------------- 1 | from clpy.manipulation.basic import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/manipulation/dims.py: -------------------------------------------------------------------------------- 1 | from clpy.manipulation.dims import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/manipulation/join.py: -------------------------------------------------------------------------------- 1 | from clpy.manipulation.join import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/manipulation/kind.py: -------------------------------------------------------------------------------- 1 | from clpy.manipulation.kind import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/manipulation/shape.py: -------------------------------------------------------------------------------- 1 | from clpy.manipulation.shape import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/manipulation/split.py: -------------------------------------------------------------------------------- 1 | from clpy.manipulation.split import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/math/trigonometric.py: -------------------------------------------------------------------------------- 1 | from clpy.math.trigonometric import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/sparse/compressed.py: -------------------------------------------------------------------------------- 1 | from clpy.sparse.compressed import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/statistics/meanvar.py: -------------------------------------------------------------------------------- 1 | from clpy.statistics.meanvar import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/testing/condition.py: -------------------------------------------------------------------------------- 1 | from clpy.testing.condition import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/testing/hypothesis.py: -------------------------------------------------------------------------------- 1 | from clpy.testing.hypothesis import * # NOQA 2 | -------------------------------------------------------------------------------- /headercvt/.gitignore: -------------------------------------------------------------------------------- 1 | !headercvt.cpp 2 | headercvt 3 | *.pxi 4 | not_handled.txt 5 | -------------------------------------------------------------------------------- /cupy_alias/linalg/decomposition.py: -------------------------------------------------------------------------------- 1 | from clpy.linalg.decomposition import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/manipulation/tiling.py: -------------------------------------------------------------------------------- 1 | from clpy.manipulation.tiling import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/random/distributions.py: -------------------------------------------------------------------------------- 1 | from clpy.random.distributions import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/random/permutations.py: -------------------------------------------------------------------------------- 1 | from clpy.random.permutations import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/statistics/histogram.py: -------------------------------------------------------------------------------- 1 | from clpy.statistics.histogram import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/testing/parameterized.py: -------------------------------------------------------------------------------- 1 | from clpy.testing.parameterized import * # NOQA 2 | -------------------------------------------------------------------------------- /clpy/backend/opencl/.gitignore: -------------------------------------------------------------------------------- 1 | func_decl.pxi 2 | types.pxi 3 | preprocessor_defines.pxi 4 | -------------------------------------------------------------------------------- /cupy_alias/manipulation/add_remove.py: -------------------------------------------------------------------------------- 1 | from clpy.manipulation.add_remove import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/manipulation/rearrange.py: -------------------------------------------------------------------------------- 1 | from clpy.manipulation.rearrange import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/manipulation/transpose.py: -------------------------------------------------------------------------------- 1 | from clpy.manipulation.transpose import * # NOQA 2 | -------------------------------------------------------------------------------- /cupy_alias/statistics/correlation.py: -------------------------------------------------------------------------------- 1 | from clpy.statistics.correlation import * # NOQA 2 | -------------------------------------------------------------------------------- /clpy/backend/ultima/__init__.py: -------------------------------------------------------------------------------- 1 | from clpy.backend.ultima.compiler import exec_ultima # NOQA 2 | -------------------------------------------------------------------------------- /clpy/backend/ultima/compiler.pxd: -------------------------------------------------------------------------------- 1 | cpdef str exec_ultima(str source, str _clpy_header_include=*) 2 | -------------------------------------------------------------------------------- /clpy/backend/opencl/common_decl.pxi: -------------------------------------------------------------------------------- 1 | include "preprocessor_defines.pxi" 2 | 3 | include "types.pxi" 4 | -------------------------------------------------------------------------------- /clpy/ext/__init__.py: -------------------------------------------------------------------------------- 1 | # "NOQA" to suppress flake8 warning 2 | from clpy.ext import scatter # NOQA 3 | -------------------------------------------------------------------------------- /clpy/internal.py: -------------------------------------------------------------------------------- 1 | def prod(args, init=1): 2 | for arg in args: 3 | init *= arg 4 | return init 5 | -------------------------------------------------------------------------------- /docs/image/cupy_logo_1000px.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fixstars/clpy/HEAD/docs/image/cupy_logo_1000px.png -------------------------------------------------------------------------------- /.flake8.cython: -------------------------------------------------------------------------------- 1 | [flake8] 2 | filename = *.pyx,*.px* 3 | exclude = .eggs,*.egg,build 4 | ignore = E901,E225,E226,E227 5 | -------------------------------------------------------------------------------- /clpy/backend/opencl/exceptions.pxd: -------------------------------------------------------------------------------- 1 | include "common_decl.pxi" 2 | 3 | cpdef void check_status(cl_int status) except * 4 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | recursive-include clpy *.pyx *.pxd *.pxi *.h 2 | include clpy_setup_build.py 3 | recursive-include install *.py 4 | -------------------------------------------------------------------------------- /.github/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | Please refer to [our Contribution Guide](https://docs-cupy.chainer.org/en/stable/contribution.html). 2 | 3 | -------------------------------------------------------------------------------- /docs/source/tutorial/index.rst: -------------------------------------------------------------------------------- 1 | Tutorial 2 | ======== 3 | 4 | .. toctree:: 5 | :maxdepth: 1 6 | 7 | basic 8 | kernel 9 | -------------------------------------------------------------------------------- /clpy/core/include/clpy/complex/README.md: -------------------------------------------------------------------------------- 1 | These files are copied from thrust project and are modified. 2 | 3 | http://thrust.github.io/ -------------------------------------------------------------------------------- /clpy/prof/__init__.py: -------------------------------------------------------------------------------- 1 | from clpy.prof.time_range import time_range # NOQA 2 | from clpy.prof.time_range import TimeRangeDecorator # NOQA 3 | -------------------------------------------------------------------------------- /.coveragerc: -------------------------------------------------------------------------------- 1 | [run] 2 | plugins = Cython.Coverage 3 | include = cupy/*,examples/* 4 | 5 | [report] 6 | exclude_lines = 7 | if __name__ == .__main__.: 8 | -------------------------------------------------------------------------------- /readthedocs.yml: -------------------------------------------------------------------------------- 1 | name: chainer 2 | type: sphinx 3 | base: docs/source 4 | requirements_file: docs/requirements.txt 5 | python: 6 | setup_py_install: true 7 | -------------------------------------------------------------------------------- /clpy/__main__.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | if len(sys.argv) < 2: 4 | sys.exit() 5 | 6 | sys.argv.pop(0) 7 | with open(sys.argv[0]) as f: 8 | exec(f.read()) 9 | -------------------------------------------------------------------------------- /clpy/io/rawfile.py: -------------------------------------------------------------------------------- 1 | # flake8: NOQA 2 | # "flake8: NOQA" to suppress warning "H104 File contains nothing but comments" 3 | 4 | # TODO(okuta): Implement fromfile 5 | -------------------------------------------------------------------------------- /docs/source/reference/pad.rst: -------------------------------------------------------------------------------- 1 | Padding 2 | ================================ 3 | 4 | .. autosummary:: 5 | :toctree: generated/ 6 | :nosignatures: 7 | 8 | cupy.pad 9 | -------------------------------------------------------------------------------- /docs/source/reference/ext.rst: -------------------------------------------------------------------------------- 1 | External Functions 2 | ================== 3 | 4 | .. autosummary:: 5 | :toctree: generated/ 6 | :nosignatures: 7 | 8 | cupy.scatter_add 9 | -------------------------------------------------------------------------------- /clpy/backend/compiler.pxd: -------------------------------------------------------------------------------- 1 | import function 2 | cimport function 3 | 4 | cpdef function.Module compile_with_cache( 5 | str source, tuple options=*, arch=*, cache_dir=*, extra_source=*) 6 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [flake8] 2 | exclude = .eggs,*.egg,build,caffe_pb2.py,caffe_pb3.py,docs,.git 3 | 4 | [tool:pytest] 5 | filterwarnings= ignore::FutureWarning 6 | error::DeprecationWarning 7 | -------------------------------------------------------------------------------- /clpy/math/special.py: -------------------------------------------------------------------------------- 1 | # flake8: NOQA 2 | # "flake8: NOQA" to suppress warning "H104 File contains nothing but comments" 3 | 4 | # TODO(okuta): Implement i0 5 | 6 | 7 | # TODO(okuta): Implement sinc 8 | -------------------------------------------------------------------------------- /tests/clpy_tests/io_tests/test_text.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from clpy import testing 4 | 5 | 6 | @testing.gpu 7 | class TestText(unittest.TestCase): 8 | 9 | _multiprocess_can_split_ = True 10 | -------------------------------------------------------------------------------- /docs/source/reference/generic.rst: -------------------------------------------------------------------------------- 1 | NumPy-CuPy Generic Code Support 2 | =============================== 3 | 4 | .. autosummary:: 5 | :toctree: generated/ 6 | :nosignatures: 7 | 8 | cupy.get_array_module 9 | -------------------------------------------------------------------------------- /docs/source/reference/kernel.rst: -------------------------------------------------------------------------------- 1 | Custom kernels 2 | ============== 3 | 4 | .. autosummary:: 5 | :toctree: generated/ 6 | :nosignatures: 7 | 8 | cupy.ElementwiseKernel 9 | cupy.ReductionKernel 10 | -------------------------------------------------------------------------------- /tests/clpy_tests/io_tests/test_rawfile.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from clpy import testing 4 | 5 | 6 | @testing.gpu 7 | class TestRawfile(unittest.TestCase): 8 | 9 | _multiprocess_can_split_ = True 10 | -------------------------------------------------------------------------------- /tests/clpy_tests/math_tests/test_special.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from clpy import testing 4 | 5 | 6 | @testing.gpu 7 | class TestSpecial(unittest.TestCase): 8 | 9 | _multiprocess_can_split_ = True 10 | -------------------------------------------------------------------------------- /docs/source/reference/memoize.rst: -------------------------------------------------------------------------------- 1 | Kernel binary memoization 2 | ========================= 3 | 4 | .. autosummary:: 5 | :toctree: generated/ 6 | :nosignatures: 7 | 8 | cupy.memoize 9 | cupy.clear_memo 10 | -------------------------------------------------------------------------------- /tests/clpy_tests/logic_tests/test_type_test.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from clpy import testing 4 | 5 | 6 | @testing.gpu 7 | class TestTypeTest(unittest.TestCase): 8 | 9 | _multiprocess_can_split_ = True 10 | -------------------------------------------------------------------------------- /clpy/core/include/cuda_stub.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #define __global__ __attribute((annotate("cu_global"))) 4 | #define __device__ __attribute((annotate("cu_device"))) 5 | #define __shared__ __attribute((annotate("cu_shared"))) 6 | -------------------------------------------------------------------------------- /clpy/padding/__init__.py: -------------------------------------------------------------------------------- 1 | # Functions from the following NumPy document 2 | # http://docs.scipy.org/doc/numpy/reference/routines.padding.html 3 | 4 | # "NOQA" to suppress flake8 warning 5 | from clpy.padding import pad # NOQA 6 | -------------------------------------------------------------------------------- /tests/clpy_tests/statics_tests/test_correlation.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from clpy import testing 4 | 5 | 6 | @testing.gpu 7 | class TestCorrelation(unittest.TestCase): 8 | 9 | _multiprocess_can_split_ = True 10 | -------------------------------------------------------------------------------- /tests/clpy_tests/manipulation_tests/test_add_remove.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from clpy import testing 4 | 5 | 6 | @testing.gpu 7 | class TestAddRemove(unittest.TestCase): 8 | 9 | _multiprocess_can_split_ = True 10 | -------------------------------------------------------------------------------- /tests/example_tests/test_gemm.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from example_tests import example_test 4 | 5 | 6 | class TestGEMM(unittest.TestCase): 7 | 8 | def test_sgemm(self): 9 | example_test.run_example('gemm/sgemm.py') 10 | -------------------------------------------------------------------------------- /docs/source/reference/prof.rst: -------------------------------------------------------------------------------- 1 | Profiling 2 | ========= 3 | 4 | time range 5 | ---------- 6 | 7 | .. autosummary:: 8 | :toctree: generated/ 9 | :nosignatures: 10 | 11 | cupy.prof.TimeRangeDecorator 12 | cupy.prof.time_range 13 | -------------------------------------------------------------------------------- /clpy/backend/device.pxd: -------------------------------------------------------------------------------- 1 | cpdef int get_device_id() 2 | cpdef get_cublas_handle() 3 | 4 | cdef class Device: 5 | cdef: 6 | public int id 7 | list _device_stack 8 | 9 | cpdef use(self) 10 | cpdef synchronize(self) 11 | -------------------------------------------------------------------------------- /clpy/backend/cuda/device.pxd: -------------------------------------------------------------------------------- 1 | cpdef int get_device_id() except * 2 | cpdef get_cublas_handle() 3 | 4 | cdef class Device: 5 | cdef: 6 | public int id 7 | list _device_stack 8 | 9 | cpdef use(self) 10 | cpdef synchronize(self) 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | *.egg/ 3 | *.pyc 4 | *.pyo 5 | *.cpp 6 | *.so 7 | build 8 | \#*\# 9 | .\#* 10 | .coverage 11 | .eggs/ 12 | _readthedocs_build 13 | /TAGS 14 | /docs/source/reference/**/generated 15 | /tags 16 | clpy.egg-info/ 17 | dist/ 18 | htmlcov/ 19 | .idea/ 20 | -------------------------------------------------------------------------------- /clpy/backend/opencl/env.pxd: -------------------------------------------------------------------------------- 1 | include "common_decl.pxi" 2 | 3 | cdef cl_context get_context() 4 | cdef cl_command_queue get_command_queue() 5 | cpdef int get_device_id() 6 | cpdef set_device_id(int id) 7 | cdef cl_device_id* get_devices() 8 | cdef cl_device_id get_device() 9 | -------------------------------------------------------------------------------- /clpy/backend/ultima/exceptions.py: -------------------------------------------------------------------------------- 1 | class UltimaRuntimeError(RuntimeError): 2 | def __init__(self, status, stderr=''): 3 | self.status = status 4 | super(UltimaRuntimeError, self).__init__( 5 | 'Return code:%d\nSTDERR:\n%s' % (status, stderr)) 6 | -------------------------------------------------------------------------------- /clpy/binary/__init__.py: -------------------------------------------------------------------------------- 1 | # Functions from the following NumPy document 2 | # http://docs.scipy.org/doc/numpy/reference/routines.bitwise.html 3 | 4 | # "NOQA" to suppress flake8 warning 5 | from clpy.binary import elementwise # NOQA 6 | from clpy.binary import packing # NOQA 7 | -------------------------------------------------------------------------------- /clpy/statistics/correlation.py: -------------------------------------------------------------------------------- 1 | # flake8: NOQA 2 | # "flake8: NOQA" to suppress warning "H104 File contains nothing but comments" 3 | 4 | # TODO(okuta): Implement corrcoef 5 | 6 | 7 | # TODO(okuta): Implement correlate 8 | 9 | 10 | # TODO(okuta): Implement cov 11 | -------------------------------------------------------------------------------- /docker/python2/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM nvidia/cuda:7.5-cudnn5-devel 2 | 3 | RUN apt-get update -y && \ 4 | apt-get install -y --no-install-recommends \ 5 | python-dev \ 6 | python-pip && \ 7 | rm -rf /var/lib/apt/lists/* /var/cache/apt/archives/* 8 | 9 | RUN pip install cupy==2.1.0 10 | -------------------------------------------------------------------------------- /docker/python3/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM nvidia/cuda:7.5-cudnn5-devel 2 | 3 | RUN apt-get update -y && \ 4 | apt-get install -y --no-install-recommends \ 5 | python3-dev \ 6 | python3-pip && \ 7 | rm -rf /var/lib/apt/lists/* /var/cache/apt/archives/* 8 | 9 | RUN pip3 install cupy==2.1.0 10 | -------------------------------------------------------------------------------- /clpy/sorting/__init__.py: -------------------------------------------------------------------------------- 1 | # Functions from the following NumPy document 2 | # http://docs.scipy.org/doc/numpy/reference/routines.sort.html 3 | 4 | # "NOQA" to suppress flake8 warning 5 | from clpy.sorting import count # NOQA 6 | from clpy.sorting import search # NOQA 7 | from clpy.sorting import sort # NOQA 8 | -------------------------------------------------------------------------------- /clpy/binary/elementwise.py: -------------------------------------------------------------------------------- 1 | from clpy import core 2 | 3 | 4 | bitwise_and = core.bitwise_and 5 | 6 | 7 | bitwise_or = core.bitwise_or 8 | 9 | 10 | bitwise_xor = core.bitwise_xor 11 | 12 | 13 | invert = core.invert 14 | 15 | 16 | left_shift = core.left_shift 17 | 18 | 19 | right_shift = core.right_shift 20 | -------------------------------------------------------------------------------- /tests/clpy_tests/cuda_tests/test_cusolver.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from cupy import cuda 4 | 5 | 6 | class TestCusolver(unittest.TestCase): 7 | 8 | def test_cusolver_enabled(self): 9 | self.assertEqual(cuda.runtime.runtimeGetVersion() >= 8000, 10 | cuda.cusolver_enabled) 11 | -------------------------------------------------------------------------------- /clpy/indexing/__init__.py: -------------------------------------------------------------------------------- 1 | # Functions from the following NumPy document 2 | # http://docs.scipy.org/doc/numpy/reference/routines.indexing.html 3 | 4 | # "NOQA" to suppress flake8 warning 5 | from clpy.indexing import generate # NOQA 6 | from clpy.indexing import indexing # NOQA 7 | from clpy.indexing import insert # NOQA 8 | -------------------------------------------------------------------------------- /clpy/io/__init__.py: -------------------------------------------------------------------------------- 1 | # Functions from the following NumPy document 2 | # http://docs.scipy.org/doc/numpy/reference/routines.io.html 3 | 4 | # "NOQA" to suppress flake8 warning 5 | from clpy.io import formatting # NOQA 6 | from clpy.io import npz # NOQA 7 | from clpy.io import rawfile # NOQA 8 | from clpy.io import text # NOQA 9 | -------------------------------------------------------------------------------- /clpy/backend/cuda/memory_hooks/__init__.py: -------------------------------------------------------------------------------- 1 | from clpy.cuda.memory_hooks import debug_print # NOQA 2 | from clpy.cuda.memory_hooks import line_profile # NOQA 3 | 4 | # import class and function 5 | from clpy.cuda.memory_hooks.debug_print import DebugPrintHook # NOQA 6 | from clpy.cuda.memory_hooks.line_profile import LineProfileHook # NOQA 7 | -------------------------------------------------------------------------------- /clpy/backend/cuda/profiler.pxd: -------------------------------------------------------------------------------- 1 | cdef extern from *: 2 | ctypedef int OutputMode 'cudaOutputMode_t' 3 | 4 | 5 | cpdef enum: 6 | cudaKeyValuePair = 0 7 | cudaCSV = 1 8 | 9 | cpdef void initialize( 10 | str config_file, str output_file, int output_mode) except * 11 | cpdef void start() except * 12 | cpdef void stop() except * 13 | -------------------------------------------------------------------------------- /docs/source/reference/indexing.rst: -------------------------------------------------------------------------------- 1 | Indexing Routines 2 | ================= 3 | 4 | .. autosummary:: 5 | :toctree: generated/ 6 | :nosignatures: 7 | 8 | cupy.c_ 9 | cupy.r_ 10 | cupy.nonzero 11 | cupy.where 12 | cupy.ix_ 13 | cupy.take 14 | cupy.choose 15 | cupy.diag 16 | cupy.diagonal 17 | cupy.fill_diagonal 18 | -------------------------------------------------------------------------------- /clpy/creation/__init__.py: -------------------------------------------------------------------------------- 1 | # Functions from the following NumPy document 2 | # http://docs.scipy.org/doc/numpy/reference/routines.array-creation.html 3 | 4 | # "NOQA" to suppress flake8 warning 5 | from clpy.creation import basic # NOQA 6 | from clpy.creation import from_data # NOQA 7 | from clpy.creation import matrix # NOQA 8 | from clpy.creation import ranges # NOQA 9 | -------------------------------------------------------------------------------- /clpy/io/text.py: -------------------------------------------------------------------------------- 1 | # flake8: NOQA 2 | # "flake8: NOQA" to suppress warning "H104 File contains nothing but comments" 3 | 4 | 5 | # TODO(okuta): Implement loadtxt 6 | 7 | 8 | # TODO(okuta): Implement savetxt 9 | 10 | 11 | # TODO(okuta): Implement genfromtxt 12 | 13 | 14 | # TODO(okuta): Implement fromregex 15 | 16 | 17 | # TODO(okuta): Implement fromstring 18 | -------------------------------------------------------------------------------- /clpy/logic/type_test.py: -------------------------------------------------------------------------------- 1 | # flake8: NOQA 2 | # "flake8: NOQA" to suppress warning "H104 File contains nothing but comments" 3 | 4 | # TODO(okuta): Implement iscomplex 5 | 6 | 7 | # TODO(okuta): Implement iscomplexobj 8 | 9 | 10 | # TODO(okuta): Implement isfortran 11 | 12 | 13 | # TODO(okuta): Implement isreal 14 | 15 | 16 | # TODO(okuta): Implement isrealobj 17 | -------------------------------------------------------------------------------- /clpy/statistics/__init__.py: -------------------------------------------------------------------------------- 1 | # Functions from the following NumPy document 2 | # http://docs.scipy.org/doc/numpy/reference/routines.statistics.html 3 | 4 | # "NOQA" to suppress flake8 warning 5 | from clpy.statistics import correlation # NOQA 6 | from clpy.statistics import histogram # NOQA 7 | from clpy.statistics import meanvar # NOQA 8 | from clpy.statistics import order # NOQA 9 | -------------------------------------------------------------------------------- /clpy/random/permutations.py: -------------------------------------------------------------------------------- 1 | from clpy.random import generator 2 | 3 | # TODO(okuta): Implement permutation 4 | 5 | 6 | def shuffle(a): 7 | """Shuffles an array. 8 | 9 | Args: 10 | a (clpy.ndarray): The array to be shuffled. 11 | 12 | .. seealso:: :func:`numpy.random.shuffle` 13 | 14 | """ 15 | rs = generator.get_random_state() 16 | return rs.shuffle(a) 17 | -------------------------------------------------------------------------------- /clpy/logic/__init__.py: -------------------------------------------------------------------------------- 1 | # Functions from the following NumPy document 2 | # http://docs.scipy.org/doc/numpy/reference/routines.logic.html 3 | 4 | # "NOQA" to suppress flake8 warning 5 | from clpy.logic import comparison # NOQA 6 | from clpy.logic import content # NOQA 7 | from clpy.logic import ops # NOQA 8 | from clpy.logic import truth # NOQA 9 | from clpy.logic import type_test # NOQA 10 | -------------------------------------------------------------------------------- /docs/source/reference/sorting.rst: -------------------------------------------------------------------------------- 1 | Sorting, Searching, and Counting 2 | ================================ 3 | 4 | .. autosummary:: 5 | :toctree: generated/ 6 | :nosignatures: 7 | 8 | cupy.sort 9 | cupy.lexsort 10 | cupy.argsort 11 | cupy.argmax 12 | cupy.argmin 13 | cupy.partition 14 | cupy.count_nonzero 15 | cupy.nonzero 16 | cupy.flatnonzero 17 | cupy.where 18 | -------------------------------------------------------------------------------- /tests/clpy_tests/opencl_tests/test_api.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import unittest 4 | 5 | 6 | class TestApi(unittest.TestCase): 7 | """test class of OpenCL API""" 8 | 9 | def test_import(self): 10 | self.assertTrue(True) # Always OK if no exeption from import 11 | 12 | # TODO(LWisteria): Implement more case 13 | 14 | 15 | if __name__ == "__main__": 16 | unittest.main() 17 | -------------------------------------------------------------------------------- /tests/install_tests/test_utils.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from install import utils 4 | 5 | 6 | class TestPrintWarning(unittest.TestCase): 7 | 8 | def test_print_warning(self): 9 | utils.print_warning('This is a test.') 10 | 11 | 12 | class TestSearchOnPath(unittest.TestCase): 13 | 14 | def test_exec_not_found(self): 15 | self.assertIsNone(utils.search_on_path(['no_such_exec'])) 16 | -------------------------------------------------------------------------------- /clpy/manipulation/add_remove.py: -------------------------------------------------------------------------------- 1 | # flake8: NOQA 2 | # "flake8: NOQA" to suppress warning "H104 File contains nothing but comments" 3 | 4 | # TODO(okuta): Implement delete 5 | 6 | 7 | # TODO(okuta): Implement insert 8 | 9 | 10 | # TODO(okuta): Implement append 11 | 12 | 13 | # TODO(okuta): Implement resize 14 | 15 | 16 | # TODO(okuta): Implement trim_zeros 17 | 18 | 19 | # TODO(okuta): Implement unique 20 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | 3 | cache: 4 | - pip 5 | 6 | python: 7 | - "3.6" 8 | 9 | install: 10 | - pip install -U pip wheel 11 | - pip install cython 12 | - pip install autopep8==1.4.0 hacking==1.0.0 13 | 14 | script: 15 | - flake8 16 | - flake8 --config=.flake8.cython 17 | - autopep8 -r . --global-config .pep8 --diff | tee check_autopep8 18 | - test ! -s check_autopep8 19 | 20 | sudo: false 21 | -------------------------------------------------------------------------------- /clpy/logic/comparison.py: -------------------------------------------------------------------------------- 1 | from clpy import core 2 | 3 | 4 | # TODO(okuta): Implement allclose 5 | 6 | 7 | # TODO(okuta): Implement isclose 8 | 9 | 10 | # TODO(okuta): Implement array_equal 11 | 12 | 13 | # TODO(okuta): Implement array_equiv 14 | 15 | 16 | greater = core.greater 17 | 18 | 19 | greater_equal = core.greater_equal 20 | 21 | 22 | less = core.less 23 | 24 | 25 | less_equal = core.less_equal 26 | 27 | 28 | equal = core.equal 29 | 30 | 31 | not_equal = core.not_equal 32 | -------------------------------------------------------------------------------- /tests/clpy_tests/math_tests/test_window.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from clpy import testing 4 | 5 | 6 | @testing.parameterize( 7 | *testing.product({ 8 | 'm': [0, 1, -1, 1024], 9 | 'name': ['blackman', 'hamming', 'hanning'], 10 | }) 11 | ) 12 | class TestWindow(unittest.TestCase): 13 | 14 | _multiprocess_can_split_ = True 15 | 16 | @testing.numpy_clpy_allclose(atol=1e-5) 17 | def test_window(self, xp): 18 | return getattr(xp, self.name)(self.m) 19 | -------------------------------------------------------------------------------- /clpy/backend/cuda/cupy_common.h: -------------------------------------------------------------------------------- 1 | #ifndef INCLUDE_GUARD_CUPY_CUDA_COMMON_H 2 | #define INCLUDE_GUARD_CUPY_CUDA_COMMON_H 3 | 4 | typedef char cpy_byte; 5 | typedef unsigned char cpy_ubyte; 6 | typedef short cpy_short; 7 | typedef unsigned short cpy_ushort; 8 | typedef int cpy_int; 9 | typedef unsigned int cpy_uint; 10 | typedef long long cpy_long; 11 | typedef unsigned long long cpy_ulong; 12 | typedef float cpy_float; 13 | typedef double cpy_double; 14 | 15 | #endif // INCLUDE_GUARD_CUPY_CUDA_COMMON_H 16 | -------------------------------------------------------------------------------- /clpy/backend/cuda/common.pxd: -------------------------------------------------------------------------------- 1 | # distutils: language = c++ 2 | 3 | cdef extern from '../cuda/cupy_common.h': # thru parent to import in core 4 | ctypedef char cpy_byte 5 | ctypedef unsigned char cpy_ubyte 6 | ctypedef short cpy_short 7 | ctypedef unsigned short cpy_ushort 8 | ctypedef int cpy_int 9 | ctypedef unsigned int cpy_uint 10 | ctypedef long long cpy_long 11 | ctypedef unsigned long long cpy_ulong 12 | ctypedef float cpy_float 13 | ctypedef double cpy_double 14 | -------------------------------------------------------------------------------- /tests/clpy_tests/ext_tests/test_scatter.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy 4 | 5 | import clpy 6 | from clpy import testing 7 | 8 | 9 | @testing.gpu 10 | class TestScatter(unittest.TestCase): 11 | 12 | def test_scatter_add(self): 13 | a = clpy.zeros((3,), dtype=numpy.float32) 14 | i = clpy.array([1, 1], numpy.int32) 15 | v = clpy.array([2., 1.], dtype=numpy.float32) 16 | clpy.scatter_add(a, i, v) 17 | testing.assert_array_equal(a, clpy.array([0, 3, 0])) 18 | -------------------------------------------------------------------------------- /clpy/backend/common.pxd: -------------------------------------------------------------------------------- 1 | # distutils: language = c++ 2 | 3 | ''' 4 | cdef extern from '../cuda/cupy_common.h': # thru parent to import in core 5 | ctypedef char cpy_byte 6 | ctypedef unsigned char cpy_ubyte 7 | ctypedef short cpy_short 8 | ctypedef unsigned short cpy_ushort 9 | ctypedef int cpy_int 10 | ctypedef unsigned int cpy_uint 11 | ctypedef long long cpy_long 12 | ctypedef unsigned long long cpy_ulong 13 | ctypedef float cpy_float 14 | ctypedef double cpy_double 15 | ''' 16 | -------------------------------------------------------------------------------- /docs/source/reference/routines.rst: -------------------------------------------------------------------------------- 1 | -------- 2 | Routines 3 | -------- 4 | 5 | The following pages describe NumPy-compatible routines. 6 | These functions cover a subset of 7 | `NumPy routines `_. 8 | 9 | .. module:: cupy 10 | 11 | .. toctree:: 12 | :maxdepth: 2 13 | 14 | creation 15 | manipulation 16 | binary 17 | indexing 18 | io 19 | linalg 20 | logic 21 | math 22 | pad 23 | random 24 | sorting 25 | statistics 26 | ext 27 | -------------------------------------------------------------------------------- /ultima/Makefile: -------------------------------------------------------------------------------- 1 | ultima: ultima.cpp 2 | clang++ -std=c++1y -D_GLIBCXX_USE_CXX11_ABI=$(use_cxx11_abi) -Wall -Wextra -O3 -pedantic-errors $< -lclangTooling -lclangFrontendTool -lclangFrontend -lclangDriver -lclangSerialization -lclangCodeGen -lclangParse -lclangSema -lclangStaticAnalyzerFrontend -lclangStaticAnalyzerCheckers -lclangStaticAnalyzerCore -lclangAnalysis -lclangARCMigrate -lclangRewrite -lclangEdit -lclangAST -lclangLex -lclangBasic -lclang -lclangAST `llvm-config --libs --system-libs` -fno-rtti -o $@ 3 | 4 | clean: 5 | rm ultima 2>/dev/null || true 6 | -------------------------------------------------------------------------------- /clpy/backend/cuda/nccl.pxd: -------------------------------------------------------------------------------- 1 | """ 2 | Wrapper for NCCL: Optimized primiteive for collective multi-GPU communication 3 | """ 4 | cpdef enum: 5 | NCCL_SUM = 0 6 | NCCL_PROD = 1 7 | NCCL_MAX = 2 8 | NCCL_MIN = 3 9 | 10 | NCCL_INT8 = 0 11 | NCCL_CHAR = 0 12 | NCCL_UINT8 = 1 13 | NCCL_INT32 = 2 14 | NCCL_INT = 2 15 | NCCL_UINT32 = 3 16 | NCCL_INT64 = 4 17 | NCCL_UINT64 = 5 18 | NCCL_FLOAT16 = 6 19 | NCCL_HALF = 6 20 | NCCL_FLOAT32 = 7 21 | NCCL_FLOAT = 7 22 | NCCL_FLOAT64 = 8 23 | NCCL_DOUBLE = 8 24 | -------------------------------------------------------------------------------- /clpy/math/__init__.py: -------------------------------------------------------------------------------- 1 | # Functions from the following NumPy document 2 | # http://docs.scipy.org/doc/numpy/reference/routines.math.html 3 | 4 | # "NOQA" to suppress flake8 warning 5 | from clpy.math import arithmetic # NOQA 6 | from clpy.math import explog # NOQA 7 | from clpy.math import floating # NOQA 8 | from clpy.math import hyperbolic # NOQA 9 | from clpy.math import misc # NOQA 10 | from clpy.math import rounding # NOQA 11 | from clpy.math import special # NOQA 12 | from clpy.math import sumprod # NOQA 13 | from clpy.math import trigonometric # NOQA 14 | -------------------------------------------------------------------------------- /tests/clpy_tests/random_tests/test_random.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from clpy import random 4 | from clpy import testing 5 | 6 | 7 | @testing.gpu 8 | class TestResetSeed(unittest.TestCase): 9 | 10 | @testing.for_float_dtypes(no_float16=True) 11 | def test_reset_seed(self, dtype): 12 | rs = random.get_random_state() 13 | rs.seed(0) 14 | l1 = rs.rand(10, dtype=dtype) 15 | 16 | rs = random.get_random_state() 17 | rs.seed(0) 18 | l2 = rs.rand(10, dtype=dtype) 19 | 20 | testing.assert_array_equal(l1, l2) 21 | -------------------------------------------------------------------------------- /tests/example_tests/example_test.py: -------------------------------------------------------------------------------- 1 | import os 2 | import subprocess 3 | import sys 4 | 5 | 6 | def run_example(path, *args): 7 | examples_path = os.path.join( 8 | os.path.dirname(__file__), '..', '..', 'examples') 9 | fullpath = os.path.join(examples_path, path) 10 | 11 | try: 12 | return subprocess.check_output( 13 | (sys.executable, fullpath) + args, 14 | stderr=subprocess.STDOUT) 15 | except subprocess.CalledProcessError as e: 16 | print('Original error message:') 17 | print(e.output.decode('utf-8')) 18 | raise 19 | -------------------------------------------------------------------------------- /docs/source/index.rst: -------------------------------------------------------------------------------- 1 | ============================================ 2 | CuPy -- NumPy-like API accelerated with CUDA 3 | ============================================ 4 | 5 | This is the `CuPy `_ documentation. 6 | 7 | .. module:: cupy 8 | 9 | .. toctree:: 10 | :maxdepth: 1 11 | 12 | overview 13 | tutorial/index 14 | reference/index 15 | 16 | .. toctree:: 17 | :maxdepth: 1 18 | :caption: Development 19 | 20 | compatibility 21 | contribution 22 | 23 | .. toctree:: 24 | :maxdepth: 1 25 | :caption: Misc Notes 26 | 27 | install 28 | license 29 | -------------------------------------------------------------------------------- /docs/source/reference/statistics.rst: -------------------------------------------------------------------------------- 1 | Statistics 2 | ========== 3 | 4 | Order statistics 5 | ---------------- 6 | 7 | .. autosummary:: 8 | :toctree: generated/ 9 | :nosignatures: 10 | 11 | cupy.amin 12 | cupy.amax 13 | cupy.nanmin 14 | cupy.nanmax 15 | 16 | 17 | Means and variances 18 | ------------------- 19 | 20 | .. autosummary:: 21 | :toctree: generated/ 22 | :nosignatures: 23 | 24 | cupy.mean 25 | cupy.var 26 | cupy.std 27 | 28 | 29 | Histograms 30 | ---------- 31 | 32 | .. autosummary:: 33 | :toctree: generated/ 34 | :nosignatures: 35 | 36 | cupy.bincount 37 | -------------------------------------------------------------------------------- /tests/clpy_tests/core_tests/test_ndarray_contiguity.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from clpy import testing 4 | 5 | 6 | @testing.gpu 7 | class TestArrayContiguity(unittest.TestCase): 8 | 9 | _multiprocess_can_split_ = True 10 | 11 | def test_is_contiguous(self): 12 | a = testing.shaped_arange((2, 3, 4)) 13 | self.assertTrue(a.flags.c_contiguous) 14 | b = a.transpose(2, 0, 1) 15 | self.assertFalse(b.flags.c_contiguous) 16 | c = a[::-1] 17 | self.assertFalse(c.flags.c_contiguous) 18 | d = a[:, :, ::2] 19 | self.assertFalse(d.flags.c_contiguous) 20 | -------------------------------------------------------------------------------- /install/utils.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | 4 | def print_warning(*lines): 5 | print('**************************************************') 6 | for line in lines: 7 | print('*** WARNING: %s' % line) 8 | print('**************************************************') 9 | 10 | 11 | def get_path(key): 12 | return os.environ.get(key, '').split(os.pathsep) 13 | 14 | 15 | def search_on_path(filenames): 16 | for p in get_path('PATH'): 17 | for filename in filenames: 18 | full = os.path.join(p, filename) 19 | if os.path.exists(full): 20 | return os.path.abspath(full) 21 | return None 22 | -------------------------------------------------------------------------------- /docs/source/reference/io.rst: -------------------------------------------------------------------------------- 1 | Input and Output 2 | ================ 3 | 4 | NPZ files 5 | --------- 6 | 7 | .. autosummary:: 8 | :toctree: generated/ 9 | :nosignatures: 10 | 11 | cupy.load 12 | cupy.save 13 | cupy.savez 14 | cupy.savez_compressed 15 | 16 | 17 | String formatting 18 | ----------------- 19 | 20 | .. autosummary:: 21 | :toctree: generated/ 22 | :nosignatures: 23 | 24 | cupy.array_repr 25 | cupy.array_str 26 | 27 | 28 | Base-n representations 29 | ---------------------- 30 | 31 | .. autosummary:: 32 | :toctree: generated/ 33 | :nosignatures: 34 | 35 | cupy.binary_repr 36 | cupy.base_repr 37 | -------------------------------------------------------------------------------- /docs/source/reference/index.rst: -------------------------------------------------------------------------------- 1 | .. _cupy_reference: 2 | 3 | **************** 4 | Reference Manual 5 | **************** 6 | 7 | This is the official reference of CuPy, a multi-dimensional array on CUDA with a subset of NumPy interface. 8 | 9 | 10 | Indices and tables 11 | ================== 12 | 13 | * :ref:`genindex` 14 | * :ref:`modindex` 15 | 16 | Reference 17 | ========= 18 | 19 | .. module:: cupy 20 | 21 | .. toctree:: 22 | :maxdepth: 2 23 | 24 | ndarray 25 | ufunc 26 | routines 27 | sparse 28 | generic 29 | cuda 30 | memoize 31 | kernel 32 | testing 33 | prof 34 | environment 35 | difference 36 | -------------------------------------------------------------------------------- /tests/clpy_tests/core_tests/test_ndarray_owndata.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from clpy import core 4 | from clpy import testing 5 | 6 | 7 | @testing.gpu 8 | class TestArrayOwndata(unittest.TestCase): 9 | 10 | _multiprocess_can_split_ = True 11 | 12 | def setUp(self): 13 | self.a = core.ndarray(()) 14 | 15 | def test_original_array(self): 16 | self.assertTrue(self.a.flags.owndata) 17 | 18 | def test_view_array(self): 19 | v = self.a.view() 20 | self.assertFalse(v.flags.owndata) 21 | 22 | def test_reshaped_array(self): 23 | r = self.a.reshape(()) 24 | self.assertFalse(r.flags.owndata) 25 | -------------------------------------------------------------------------------- /clpy/backend/cuda/cupy_stdint.h: -------------------------------------------------------------------------------- 1 | #ifndef INCLUDE_GUARD_CUPY_STDINT_H 2 | #define INCLUDE_GUARD_CUPY_STDINT_H 3 | 4 | #if defined(_MSC_VER) && (_MSC_VER < 1600) 5 | typedef signed __int8 int8_t; 6 | typedef signed __int16 int16_t; 7 | typedef signed __int32 int32_t; 8 | typedef signed __int64 int64_t; 9 | typedef unsigned __int8 uint8_t; 10 | typedef unsigned __int16 uint16_t; 11 | typedef unsigned __int32 uint32_t; 12 | typedef unsigned __int64 uint64_t; 13 | 14 | #else // #if defined(_MSC_VER) && (_MSC_VER < 1600) 15 | #include 16 | #endif // #if defined(_MSC_VER) && (_MSC_VER < 1600) 17 | 18 | #endif // #if INCLUDE_GUARD_CUPY_STDINT_H 19 | 20 | -------------------------------------------------------------------------------- /clpy/manipulation/__init__.py: -------------------------------------------------------------------------------- 1 | # Functions from the following NumPy document 2 | # http://docs.scipy.org/doc/numpy/reference/routines.array-manipulation.html 3 | 4 | # "NOQA" to suppress flake8 warning 5 | from clpy.manipulation import add_remove # NOQA 6 | from clpy.manipulation import basic # NOQA 7 | from clpy.manipulation import dims # NOQA 8 | from clpy.manipulation import join # NOQA 9 | from clpy.manipulation import kind # NOQA 10 | from clpy.manipulation import rearrange # NOQA 11 | from clpy.manipulation import shape # NOQA 12 | from clpy.manipulation import split # NOQA 13 | from clpy.manipulation import tiling # NOQA 14 | from clpy.manipulation import transpose # NOQA 15 | -------------------------------------------------------------------------------- /tests/clpy_tests/core_tests/test_core.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import clpy 4 | from clpy.core import core 5 | 6 | 7 | class TestGetStridesForNocopyReshape(unittest.TestCase): 8 | 9 | def test_different_size(self): 10 | a = core.ndarray((2, 3)) 11 | self.assertEqual(core._get_strides_for_nocopy_reshape(a, (1, 5)), 12 | []) 13 | 14 | def test_one(self): 15 | a = core.ndarray((1,), dtype=clpy.int32) 16 | self.assertEqual(core._get_strides_for_nocopy_reshape(a, (1, 1, 1)), 17 | [4, 4, 4]) 18 | 19 | def test_normal(self): 20 | # TODO(nno): write test for normal case 21 | pass 22 | -------------------------------------------------------------------------------- /tests/clpy_tests/io_tests/test_formatting.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy 4 | 5 | import clpy 6 | from clpy import testing 7 | 8 | 9 | @testing.gpu 10 | class TestFormatting(unittest.TestCase): 11 | 12 | _multiprocess_can_split_ = True 13 | 14 | def test_array_repr(self): 15 | a = testing.shaped_arange((2, 3, 4), clpy) 16 | b = testing.shaped_arange((2, 3, 4), numpy) 17 | self.assertEqual(clpy.array_repr(a), numpy.array_repr(b)) 18 | 19 | def test_array_str(self): 20 | a = testing.shaped_arange((2, 3, 4), clpy) 21 | b = testing.shaped_arange((2, 3, 4), numpy) 22 | self.assertEqual(clpy.array_str(a), numpy.array_str(b)) 23 | -------------------------------------------------------------------------------- /docs/source/reference/binary.rst: -------------------------------------------------------------------------------- 1 | Binary Operations 2 | ================= 3 | 4 | Elementwise bit operations 5 | -------------------------- 6 | 7 | .. autosummary:: 8 | :toctree: generated/ 9 | :nosignatures: 10 | 11 | cupy.bitwise_and 12 | cupy.bitwise_or 13 | cupy.bitwise_xor 14 | cupy.invert 15 | cupy.left_shift 16 | cupy.right_shift 17 | 18 | 19 | Bit packing 20 | ----------- 21 | 22 | .. autosummary:: 23 | :toctree: generated/ 24 | :nosignatures: 25 | 26 | cupy.packbits 27 | cupy.unpackbits 28 | 29 | 30 | Output formatting 31 | ----------------- 32 | 33 | .. autosummary:: 34 | :toctree: generated/ 35 | :nosignatures: 36 | 37 | cupy.binary_repr 38 | -------------------------------------------------------------------------------- /tests/clpy_tests/testing_tests/test_parameterized.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from clpy import testing 4 | 5 | 6 | @testing.parameterize( 7 | {'actual': {'a': [1, 2], 'b': [3, 4, 5]}, 8 | 'expect': [{'a': 1, 'b': 3}, {'a': 1, 'b': 4}, {'a': 1, 'b': 5}, 9 | {'a': 2, 'b': 3}, {'a': 2, 'b': 4}, {'a': 2, 'b': 5}]}, 10 | {'actual': {'a': [1, 2]}, 'expect': [{'a': 1}, {'a': 2}]}, 11 | {'actual': {'a': [1, 2], 'b': []}, 'expect': []}, 12 | {'actual': {'a': []}, 'expect': []}, 13 | {'actual': {}, 'expect': [{}]}) 14 | class ProductTest(unittest.TestCase): 15 | 16 | def test_product(self): 17 | self.assertListEqual(testing.product(self.actual), self.expect) 18 | -------------------------------------------------------------------------------- /clpy/backend/cuda/cupy_cuComplex.h: -------------------------------------------------------------------------------- 1 | // This file is a stub header file of cuda for Read the Docs. 2 | 3 | #ifndef INCLUDE_GUARD_CUPY_COMPLEX_H 4 | #define INCLUDE_GUARD_CUPY_COMPLEX_H 5 | 6 | #ifndef CUPY_NO_CUDA 7 | 8 | #include 9 | 10 | #else // #ifndef CUPY_NO_CUDA 11 | 12 | extern "C" { 13 | 14 | /////////////////////////////////////////////////////////////////////////////// 15 | // cuComplex.h 16 | /////////////////////////////////////////////////////////////////////////////// 17 | 18 | struct cuComplex{ 19 | float x, y; 20 | }; 21 | 22 | struct cuDoubleComplex{ 23 | double x, y; 24 | }; 25 | 26 | } // extern "C" 27 | 28 | #endif // #ifndef CUPY_NO_CUDA 29 | #endif // #ifndef INCLUDE_GUARD_CUPY_COMPLEX_H 30 | -------------------------------------------------------------------------------- /tests/clpy_tests/cuda_tests/test_nvtx.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from cupy import cuda 4 | from cupy.testing import attr 5 | 6 | 7 | @unittest.skipUnless(cuda.nvtx_enabled, 'nvtx is not installed') 8 | class TestNVTX(unittest.TestCase): 9 | 10 | @attr.gpu 11 | def test_Mark(self): 12 | cuda.nvtx.Mark("test:Mark", 0) 13 | 14 | @attr.gpu 15 | def test_MarkC(self): 16 | cuda.nvtx.MarkC("test:MarkC", 0xFF000000) 17 | 18 | @attr.gpu 19 | def test_RangePush(self): 20 | cuda.nvtx.RangePush("test:RangePush", 1) 21 | cuda.nvtx.RangePop() 22 | 23 | @attr.gpu 24 | def test_RangePushC(self): 25 | cuda.nvtx.RangePushC("test:RangePushC", 0xFF000000) 26 | cuda.nvtx.RangePop() 27 | -------------------------------------------------------------------------------- /tests/clpy_tests/cuda_tests/test_stream.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from cupy import cuda 4 | from cupy import testing 5 | from cupy.testing import attr 6 | 7 | 8 | class TestStream(unittest.TestCase): 9 | 10 | @attr.gpu 11 | def test_get_and_add_callback(self): 12 | N = 100 13 | cupy_arrays = [testing.shaped_random((2, 3)) for _ in range(N)] 14 | 15 | stream = cuda.Stream.null 16 | out = [] 17 | for i in range(N): 18 | numpy_array = cupy_arrays[i].get(stream=stream) 19 | stream.add_callback( 20 | lambda _, __, t: out.append(t[0]), 21 | (i, numpy_array)) 22 | 23 | stream.synchronize() 24 | self.assertEqual(out, list(range(N))) 25 | -------------------------------------------------------------------------------- /clpy/manipulation/kind.py: -------------------------------------------------------------------------------- 1 | from clpy import core 2 | 3 | 4 | # TODO(okuta): Implement asfarray 5 | 6 | 7 | def asfortranarray(a, dtype=None): 8 | """Return an array laid out in Fortran order in memory. 9 | 10 | Args: 11 | a (~clpy.ndarray): The input array. 12 | dtype (str or dtype object, optional): By default, the data-type is 13 | inferred from the input data. 14 | 15 | Returns: 16 | ~clpy.ndarray: The input `a` in Fortran, or column-major, order. 17 | 18 | .. seealso:: :func:`numpy.asfortranarray` 19 | 20 | """ 21 | return core.asfortranarray(a, dtype) 22 | 23 | 24 | # TODO(okuta): Implement asarray_chkfinite 25 | 26 | 27 | # TODO(okuta): Implement asscalar 28 | 29 | 30 | # TODO(okuta): Implement require 31 | -------------------------------------------------------------------------------- /clpy/backend/cuda/function.pxd: -------------------------------------------------------------------------------- 1 | cdef class CPointer: 2 | cdef void* ptr 3 | 4 | 5 | cdef class Function: 6 | 7 | cdef: 8 | public Module module 9 | public size_t ptr 10 | 11 | cpdef linear_launch(self, size_t size, args, size_t shared_mem=*, 12 | size_t block_max_size=*, stream=*) 13 | 14 | 15 | cdef class Module: 16 | 17 | cdef: 18 | public size_t ptr 19 | 20 | cpdef load_file(self, str filename) 21 | cpdef load(self, bytes cubin) 22 | cpdef get_global_var(self, str name) 23 | cpdef get_function(self, str name) 24 | 25 | 26 | cdef class LinkState: 27 | 28 | cdef: 29 | public size_t ptr 30 | 31 | cpdef add_ptr_data(self, unicode data, unicode name) 32 | cpdef bytes complete(self) 33 | -------------------------------------------------------------------------------- /tests/clpy_tests/opencl_tests/test_elementwise.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy 4 | 5 | import clpy 6 | from clpy import testing 7 | 8 | 9 | @testing.gpu 10 | class TestUltima(unittest.TestCase): 11 | 12 | def test_implicit_conversion_with_constructor(self): 13 | x = clpy.core.array(numpy.array([1])) 14 | y = clpy.ElementwiseKernel( 15 | 'T x', 16 | 'T y', 17 | 'test t = 3; y = t.v', 18 | 'test_implicit_conversion_with_constructor', 19 | preamble=''' 20 | struct test{ 21 | int v; 22 | test(int v_):v(v_+1){} 23 | }; 24 | ''')(x) 25 | self.assertTrue(y[0] == 4) 26 | 27 | 28 | if __name__ == "__main__": 29 | unittest.main() 30 | -------------------------------------------------------------------------------- /tests/clpy_tests/core_tests/test_flags.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from clpy.core import flags 4 | 5 | 6 | class TestFlags(unittest.TestCase): 7 | 8 | def setUp(self): 9 | self.flags = flags.Flags(1, 2, 3) 10 | 11 | def test_c_contiguous(self): 12 | self.assertEqual(1, self.flags['C_CONTIGUOUS']) 13 | 14 | def test_f_contiguous(self): 15 | self.assertEqual(2, self.flags['F_CONTIGUOUS']) 16 | 17 | def test_owndata(self): 18 | self.assertEqual(3, self.flags['OWNDATA']) 19 | 20 | def test_key_error(self): 21 | with self.assertRaises(KeyError): 22 | self.flags['unknown key'] 23 | 24 | def test_repr(self): 25 | self.assertEqual(''' C_CONTIGUOUS : 1 26 | F_CONTIGUOUS : 2 27 | OWNDATA : 3''', repr(self.flags)) 28 | -------------------------------------------------------------------------------- /clpy/backend/compiler.pyx: -------------------------------------------------------------------------------- 1 | import functools 2 | import operator 3 | cimport function 4 | 5 | cimport clpy.backend.opencl.env 6 | cimport clpy.backend.opencl.utility 7 | 8 | cpdef function.Module compile_with_cache( 9 | str source, tuple options=(), arch=None, cache_dir=None, 10 | extra_source=None): 11 | options += (' -cl-fp32-correctly-rounded-divide-sqrt', ) 12 | optionStr = functools.reduce(operator.add, options) 13 | 14 | device = clpy.backend.opencl.env.get_device() 15 | program = clpy.backend.opencl.utility.CreateProgram( 16 | [source.encode('utf-8')], 17 | clpy.backend.opencl.env.get_context(), 18 | 1, 19 | &device, 20 | optionStr.encode('utf-8')) 21 | cdef function.Module module = function.Module() 22 | module.set(program) 23 | return module 24 | -------------------------------------------------------------------------------- /tests/install_tests/test_build.py: -------------------------------------------------------------------------------- 1 | from distutils import ccompiler 2 | from distutils import sysconfig 3 | import unittest 4 | 5 | import pytest 6 | 7 | from install import build 8 | 9 | 10 | class TestCheckVersion(unittest.TestCase): 11 | 12 | def setUp(self): 13 | self.compiler = ccompiler.new_compiler() 14 | sysconfig.customize_compiler(self.compiler) 15 | self.settings = build.get_compiler_setting() 16 | 17 | @pytest.mark.gpu 18 | def test_check_opencl_version(self): 19 | self.assertTrue(build.check_opencl_version( 20 | self.compiler, self.settings)) 21 | 22 | ''' 23 | @pytest.mark.gpu 24 | @pytest.mark.cudnn 25 | def test_check_cudnn_version(self): 26 | self.assertTrue(build.check_cudnn_version( 27 | self.compiler, self.settings)) 28 | ''' 29 | -------------------------------------------------------------------------------- /clpy/backend/opencl/random.pxd: -------------------------------------------------------------------------------- 1 | from clpy.core.core cimport ndarray 2 | 3 | cdef class clrandGenerator: 4 | cdef ndarray a 5 | cdef ndarray b 6 | cdef ndarray c 7 | cdef ndarray d 8 | cdef ndarray counter 9 | cdef int inner_state_size 10 | 11 | 12 | cpdef clrandGenerator createGenerator() 13 | cpdef setPseudoRandomGeneratorSeed( 14 | clrandGenerator generator, unsigned long long seed 15 | ) 16 | 17 | cpdef generate(clrandGenerator generator, ndarray array) 18 | 19 | cpdef generateUniform(clrandGenerator generator, ndarray array) 20 | cpdef generateUniformDouble(clrandGenerator generator, ndarray array) 21 | 22 | cpdef generateNormal( 23 | clrandGenerator generator, ndarray array, float loc, float scale 24 | ) 25 | cpdef generateNormalDouble( 26 | clrandGenerator generator, ndarray array, float loc, float scale 27 | ) 28 | -------------------------------------------------------------------------------- /tests/clpy_tests/indexing_tests/test_insert.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from clpy import testing 4 | 5 | 6 | @testing.parameterize(*testing.product({ 7 | 'shape': [(3, 3), (2, 2, 2), (3, 5), (5, 3)], 8 | 'val': [1, 0], 9 | 'wrap': [True, False], 10 | })) 11 | @testing.gpu 12 | class TestInsert(unittest.TestCase): 13 | 14 | _multiprocess_can_split_ = True 15 | 16 | @testing.for_all_dtypes() 17 | @testing.numpy_clpy_array_equal() 18 | def test_fill_diagonal(self, xp, dtype): 19 | a = testing.shaped_arange(self.shape, xp, dtype) 20 | xp.fill_diagonal(a, val=self.val, wrap=self.wrap) 21 | return a 22 | 23 | @testing.for_all_dtypes() 24 | @testing.numpy_clpy_raises() 25 | def test_1darray(self, xp, dtype): 26 | a = testing.shaped_arange(5, xp, dtype) 27 | xp.fill_diagonal(a, val=self.val, wrap=self.wrap) 28 | -------------------------------------------------------------------------------- /clpy/core/flags.pyx: -------------------------------------------------------------------------------- 1 | # distutils: language = c++ 2 | 3 | 4 | class Flags(object): 5 | 6 | def __init__(self, c_contiguous, f_contiguous, owndata): 7 | self.c_contiguous = c_contiguous 8 | self.f_contiguous = f_contiguous 9 | self.owndata = owndata 10 | 11 | def __getitem__(self, name): 12 | if name == 'C_CONTIGUOUS': 13 | return self.c_contiguous 14 | elif name == 'F_CONTIGUOUS': 15 | return self.f_contiguous 16 | elif name == 'OWNDATA': 17 | return self.owndata 18 | else: 19 | raise KeyError('%s is not defined for clpy.ndarray.flags' % name) 20 | 21 | def __repr__(self): 22 | t = ' %s : %s' 23 | ret = [] 24 | for name in 'C_CONTIGUOUS', 'F_CONTIGUOUS', 'OWNDATA': 25 | ret.append(t % (name, self[name])) 26 | return '\n'.join(ret) 27 | -------------------------------------------------------------------------------- /clpy/backend/cuda/nvrtc.pxd: -------------------------------------------------------------------------------- 1 | 2 | ############################################################################### 3 | # Types 4 | ############################################################################### 5 | 6 | cdef extern from *: 7 | ctypedef int Result 'nvrtcResult' 8 | 9 | ctypedef void* Program 'nvrtcProgram' 10 | 11 | 12 | cpdef check_status(int status) 13 | 14 | cpdef tuple getVersion() 15 | 16 | ############################################################################### 17 | # Program 18 | ############################################################################### 19 | 20 | cpdef size_t createProgram(unicode src, unicode name, headers, 21 | include_names) except * 22 | cpdef destroyProgram(size_t prog) 23 | cpdef compileProgram(size_t prog, options) 24 | cpdef unicode getPTX(size_t prog) 25 | cpdef unicode getProgramLog(size_t prog) 26 | -------------------------------------------------------------------------------- /clpy/backend/opencl/utility.pxd: -------------------------------------------------------------------------------- 1 | include "common_decl.pxi" 2 | 3 | ############################################################################### 4 | # helpers 5 | cdef cl_uint GetDeviceMemBaseAddrAlign(cl_device_id device) 6 | cdef GetDeviceAddressBits(cl_device_id device) 7 | cpdef GetDeviceMaxMemoryAllocation(int device_id) 8 | 9 | ############################################################################### 10 | # utility 11 | cdef void SetKernelArgLocalMemory(cl_kernel kernel, arg_index, size_t size) 12 | cdef is_valid_kernel_name(name) 13 | cdef cl_program CreateProgram(sources, cl_context context, num_devices, 14 | cl_device_id* devices_ptrs, 15 | options=*) except * 16 | cdef str GetProgramBuildLog(cl_program program, cl_device_id device) 17 | 18 | cpdef size_t eventRecord() except * 19 | cpdef void eventSynchronize() except * 20 | -------------------------------------------------------------------------------- /clpy/backend/pinned_memory.pxd: -------------------------------------------------------------------------------- 1 | 2 | cdef class PinnedMemoryPointer: 3 | 4 | cdef: 5 | readonly object mem 6 | readonly size_t ptr 7 | Py_ssize_t _shape[1] 8 | Py_ssize_t _strides[1] 9 | 10 | cpdef Py_ssize_t size(self) 11 | 12 | 13 | cpdef _add_to_watch_list(event, obj) 14 | 15 | 16 | cpdef PinnedMemoryPointer alloc_pinned_memory(Py_ssize_t size) 17 | 18 | 19 | cpdef set_pinned_memory_allocator(allocator=*) 20 | 21 | 22 | cdef class PinnedMemoryPool: 23 | 24 | cdef: 25 | object _alloc 26 | dict _in_use 27 | object _free 28 | object __weakref__ 29 | object _weakref 30 | object _lock 31 | Py_ssize_t _allocation_unit_size 32 | 33 | cpdef PinnedMemoryPointer malloc(self, Py_ssize_t size) 34 | cpdef free(self, size_t ptr, Py_ssize_t size) 35 | cpdef free_all_blocks(self) 36 | cpdef n_free_blocks(self) 37 | -------------------------------------------------------------------------------- /clpy/backend/cuda/pinned_memory.pxd: -------------------------------------------------------------------------------- 1 | 2 | cdef class PinnedMemoryPointer: 3 | 4 | cdef: 5 | readonly object mem 6 | readonly size_t ptr 7 | Py_ssize_t _shape[1] 8 | Py_ssize_t _strides[1] 9 | 10 | cpdef Py_ssize_t size(self) 11 | 12 | 13 | cpdef _add_to_watch_list(event, obj) 14 | 15 | 16 | cpdef PinnedMemoryPointer alloc_pinned_memory(Py_ssize_t size) 17 | 18 | 19 | cpdef set_pinned_memory_allocator(allocator=*) 20 | 21 | 22 | cdef class PinnedMemoryPool: 23 | 24 | cdef: 25 | object _alloc 26 | dict _in_use 27 | object _free 28 | object __weakref__ 29 | object _weakref 30 | object _lock 31 | Py_ssize_t _allocation_unit_size 32 | 33 | cpdef PinnedMemoryPointer malloc(self, Py_ssize_t size) 34 | cpdef free(self, size_t ptr, Py_ssize_t size) 35 | cpdef free_all_blocks(self) 36 | cpdef n_free_blocks(self) 37 | -------------------------------------------------------------------------------- /clpy/testing/bufio.pyx: -------------------------------------------------------------------------------- 1 | cimport clpy.backend.opencl.api 2 | import clpy.backend.opencl.api 3 | cimport clpy.backend.opencl.env 4 | from clpy.backend.memory cimport Buf 5 | 6 | cpdef writebuf(Buf buffer_to_write, n_bytes, host_ptr, offset=0): 7 | cdef size_t host_ptr_sizet = host_ptr 8 | clpy.backend.opencl.api.EnqueueWriteBuffer( 9 | clpy.backend.opencl.env.get_command_queue(), 10 | buffer_to_write.ptr, 11 | clpy.backend.opencl.api.BLOCKING, 12 | offset, 13 | n_bytes, 14 | host_ptr_sizet) 15 | 16 | cpdef readbuf(Buf buffer_to_read, n_bytes, host_ptr, offset=0): 17 | cdef size_t host_ptr_sizet = host_ptr 18 | clpy.backend.opencl.api.EnqueueReadBuffer( 19 | clpy.backend.opencl.env.get_command_queue(), 20 | buffer_to_read.ptr, 21 | clpy.backend.opencl.api.BLOCKING, 22 | offset, 23 | n_bytes, 24 | host_ptr_sizet) 25 | -------------------------------------------------------------------------------- /tests/clpy_tests/opencl_tests/headercvt_tests/test_headercvt_funcdecl.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import headercvt_test_utils as util 4 | 5 | 6 | class TestHeadercvtFuncDecl(unittest.TestCase): 7 | def setUp(self): 8 | util.check_existence_of_headercvt() 9 | 10 | @util.with_temp_wd 11 | def test_headercvt_funcdecl_accept_case(self, wd): 12 | util.kick_headercvt_and_get_results(wd, """ 13 | void clSomeFunction(int, void *); 14 | """) 15 | self.assertTrue(util.compile_with(wd, "clSomeFunction(10, 0)")) 16 | 17 | @util.with_temp_wd 18 | def test_headercvt_funcdecl_decline_case(self, wd): 19 | results = util.kick_headercvt_and_get_results(wd, """ 20 | void SomeFunction(int, void *); 21 | """) 22 | self.assertTrue(not util.contains( 23 | results["func_decl"], "SomeFunction")) 24 | self.assertTrue(util.compile_with(wd, "")) 25 | -------------------------------------------------------------------------------- /tests/clpy_tests/opencl_tests/headercvt_tests/test_headercvt_preproc_defines.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import headercvt_test_utils as util 4 | 5 | 6 | class TestHeadercvtPreprocDefines(unittest.TestCase): 7 | def setUp(self): 8 | util.check_existence_of_headercvt() 9 | 10 | @util.with_temp_wd 11 | def test_headercvt_preproc_define_accept_case(self, wd): 12 | util.kick_headercvt_and_get_results(wd, """ 13 | #define CL_SOME_VALUE 1 14 | """) 15 | self.assertTrue(util.compile_with(wd, "print(CL_SOME_VALUE)")) 16 | 17 | @util.with_temp_wd 18 | def test_headercvt_preproc_define_decline_case(self, wd): 19 | results = util.kick_headercvt_and_get_results(wd, """ 20 | #define SOME_VALUE 1 21 | """) 22 | self.assertTrue(not util.contains( 23 | results["preprocessor_defines"], "SOME_VALUE")) 24 | self.assertTrue(util.compile_with(wd, "")) 25 | -------------------------------------------------------------------------------- /clpy/backend/function.pxd: -------------------------------------------------------------------------------- 1 | cimport clpy.backend.opencl.api 2 | cimport clpy.backend.opencl.types 3 | 4 | cdef class CPointer: 5 | cdef void* ptr 6 | 7 | 8 | cdef class Function: 9 | 10 | cdef: 11 | Module module 12 | clpy.backend.opencl.types.cl_kernel kernel 13 | 14 | cpdef linear_launch(self, size_t size, args, size_t local_mem=*, 15 | size_t local_size=*) 16 | 17 | 18 | cdef class Module: 19 | 20 | cdef: 21 | clpy.backend.opencl.types.cl_program program 22 | 23 | cpdef load_file(self, str filename) 24 | cpdef load(self, bytes cubin) 25 | cpdef get_global_var(self, str name) 26 | cpdef get_function(self, str name) 27 | cdef set(self, clpy.backend.opencl.types.cl_program program) 28 | 29 | 30 | cdef class LinkState: 31 | 32 | cdef: 33 | public size_t ptr 34 | 35 | cpdef add_ptr_data(self, unicode data, unicode name) 36 | cpdef bytes complete(self) 37 | -------------------------------------------------------------------------------- /clpy/core/include/clpy/complex.cuh: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | using thrust::complex; 6 | using thrust::conj; 7 | using thrust::arg; 8 | 9 | using thrust::exp; 10 | using thrust::log; 11 | using thrust::log10; 12 | using thrust::sin; 13 | using thrust::cos; 14 | using thrust::tan; 15 | using thrust::sinh; 16 | using thrust::cosh; 17 | using thrust::tanh; 18 | using thrust::asinh; 19 | using thrust::acosh; 20 | using thrust::atanh; 21 | using thrust::asin; 22 | using thrust::acos; 23 | using thrust::atan; 24 | 25 | template __device__ bool isnan(complex x) { 26 | return isnan(x.real()) || isnan(x.imag()); 27 | } 28 | template __device__ bool isinf(complex x) { 29 | return isinf(x.real()) || isinf(x.imag()); 30 | } 31 | template __device__ bool isfinite(complex x) { 32 | return isfinite(x.real()) && isfinite(x.imag()); 33 | } 34 | 35 | // ToDo: assignment operator for complex = T2 for T2 all types 36 | -------------------------------------------------------------------------------- /headercvt/Makefile: -------------------------------------------------------------------------------- 1 | build: headercvt 2 | 3 | headercvt: headercvt.cpp 4 | clang++ -std=c++1y -D_GLIBCXX_USE_CXX11_ABI=$(use_cxx11_abi) -Wall -Wextra -O3 -pedantic-errors $< -lclangTooling -lclangFrontendTool -lclangFrontend -lclangDriver -lclangSerialization -lclangCodeGen -lclangParse -lclangSema -lclangStaticAnalyzerFrontend -lclangStaticAnalyzerCheckers -lclangStaticAnalyzerCore -lclangAnalysis -lclangEdit -lclangAST -lclangLex -lclangBasic -lclang `llvm-config --libs --system-libs` -fno-rtti -o $@ 5 | 6 | 7 | 8 | func_decl.pxi: headercvt 9 | ./headercvt stub.c -- $(CLPY_HEADERCVT_INCLUDE_DIRS) 10 | preprocessor_defines.pxi: 11 | types.pxi: 12 | 13 | 14 | 15 | DEPLOY_PATH=../clpy/backend/opencl 16 | 17 | $(DEPLOY_PATH)/%.pxi: %.pxi 18 | cp -f $< $@ 19 | 20 | deploy: \ 21 | $(DEPLOY_PATH)/func_decl.pxi \ 22 | $(DEPLOY_PATH)/preprocessor_defines.pxi \ 23 | $(DEPLOY_PATH)/types.pxi 24 | 25 | 26 | clean: 27 | rm headercvt 2>/dev/null || true 28 | rm *.pxi 2>/dev/null || true 29 | -------------------------------------------------------------------------------- /tests/clpy_tests/logic_tests/test_comparison.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from clpy import testing 4 | 5 | 6 | @testing.gpu 7 | class TestComparison(unittest.TestCase): 8 | 9 | _multiprocess_can_split_ = True 10 | 11 | @testing.for_all_dtypes(no_complex=True) 12 | @testing.numpy_clpy_allclose(atol=1e-5) 13 | def check_binary(self, name, xp, dtype): 14 | a = testing.shaped_arange((2, 3), xp, dtype) 15 | b = testing.shaped_reverse_arange((2, 3), xp, dtype) 16 | return getattr(xp, name)(a, b) 17 | 18 | def test_greater(self): 19 | self.check_binary('greater') 20 | 21 | def test_greater_equal(self): 22 | self.check_binary('greater_equal') 23 | 24 | def test_less(self): 25 | self.check_binary('less') 26 | 27 | def test_less_equal(self): 28 | self.check_binary('less_equal') 29 | 30 | def test_not_equal(self): 31 | self.check_binary('not_equal') 32 | 33 | def test_equal(self): 34 | self.check_binary('equal') 35 | -------------------------------------------------------------------------------- /tests/clpy_tests/logic_tests/test_content.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy 4 | 5 | from clpy import testing 6 | 7 | 8 | @testing.gpu 9 | class TestContent(unittest.TestCase): 10 | 11 | _multiprocess_can_split_ = True 12 | 13 | @testing.for_dtypes('fd') 14 | @testing.numpy_clpy_array_equal() 15 | def check_unary_inf(self, name, xp, dtype): 16 | a = xp.array([-3, numpy.inf, -1, -numpy.inf, 0, 1, 2], 17 | dtype=dtype) 18 | return getattr(xp, name)(a) 19 | 20 | @testing.for_dtypes('fd') 21 | @testing.numpy_clpy_array_equal() 22 | def check_unary_nan(self, name, xp, dtype): 23 | a = xp.array( 24 | [-3, numpy.NAN, -1, numpy.NAN, 0, numpy.NAN, numpy.inf], 25 | dtype=dtype) 26 | return getattr(xp, name)(a) 27 | 28 | def test_isfinite(self): 29 | self.check_unary_inf('isfinite') 30 | 31 | def test_isinf(self): 32 | self.check_unary_inf('isinf') 33 | 34 | def test_isnan(self): 35 | self.check_unary_nan('isnan') 36 | -------------------------------------------------------------------------------- /tests/clpy_tests/cuda_tests/test_profile.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import mock 4 | 5 | from cupy import cuda 6 | 7 | 8 | class TestProfile(unittest.TestCase): 9 | 10 | def test_profile(self): 11 | start_patch = mock.patch('cupy.cuda.profiler.start') 12 | stop_patch = mock.patch('cupy.cuda.profiler.stop') 13 | with start_patch as start, stop_patch as stop: 14 | with cuda.profile(): 15 | pass 16 | start.assert_called_once_with() 17 | stop.assert_called_once_with() 18 | 19 | def test_err_case(self): 20 | start_patch = mock.patch('cupy.cuda.profiler.start') 21 | stop_patch = mock.patch('cupy.cuda.profiler.stop') 22 | with start_patch as start, stop_patch as stop: 23 | try: 24 | with cuda.profile(): 25 | raise Exception() 26 | except Exception: 27 | # ignore 28 | pass 29 | start.assert_called_once_with() 30 | stop.assert_called_once_with() 31 | -------------------------------------------------------------------------------- /clpy/math/ufunc.py: -------------------------------------------------------------------------------- 1 | from clpy import core 2 | 3 | 4 | def create_math_ufunc(math_name, nargs, name, doc): 5 | assert 1 <= nargs <= 2 6 | if nargs == 1: 7 | return core.create_ufunc( 8 | name, (('b->e', 'out0 = convert_float_to_half(%s((float)in0))' 9 | % math_name), 10 | ('B->e', 'out0 = convert_float_to_half(%s((float)in0))' 11 | % math_name), 12 | 'f->f', 'd->d', 'F->F', 'D->D'), 13 | 'out0 = %s(in0)' % math_name, doc=doc) 14 | else: 15 | return core.create_ufunc( 16 | name, (('bb->e', 'out0 = convert_float_to_half' 17 | '(%s((float)in0, (float)in1))' 18 | % math_name), 19 | ('BB->e', 'out0 = convert_float_to_half' 20 | '(%s((float)in0, (float)in1))' 21 | % math_name), 22 | 'ff->f', 'dd->d', 'FF->F', 'DD->D'), 23 | 'out0 = %s(in0, in1)' % math_name, doc=doc) 24 | -------------------------------------------------------------------------------- /docs/source/reference/creation.rst: -------------------------------------------------------------------------------- 1 | Array Creation Routines 2 | ======================= 3 | 4 | Basic creation routines 5 | ----------------------- 6 | 7 | .. autosummary:: 8 | :toctree: generated/ 9 | :nosignatures: 10 | 11 | cupy.empty 12 | cupy.empty_like 13 | cupy.eye 14 | cupy.identity 15 | cupy.ones 16 | cupy.ones_like 17 | cupy.zeros 18 | cupy.zeros_like 19 | cupy.full 20 | cupy.full_like 21 | 22 | 23 | Creation from other data 24 | ------------------------ 25 | 26 | .. autosummary:: 27 | :toctree: generated/ 28 | :nosignatures: 29 | 30 | cupy.array 31 | cupy.asarray 32 | cupy.asanyarray 33 | cupy.ascontiguousarray 34 | cupy.copy 35 | 36 | 37 | Numerical ranges 38 | ---------------- 39 | 40 | .. autosummary:: 41 | :toctree: generated/ 42 | :nosignatures: 43 | 44 | cupy.arange 45 | cupy.linspace 46 | cupy.logspace 47 | cupy.meshgrid 48 | 49 | 50 | Matrix creation 51 | --------------- 52 | 53 | .. autosummary:: 54 | :toctree: generated/ 55 | :nosignatures: 56 | 57 | cupy.diag 58 | cupy.diagflat 59 | -------------------------------------------------------------------------------- /tests/clpy_tests/logic_tests/test_ops.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from clpy import testing 4 | 5 | 6 | @testing.gpu 7 | class TestOps(unittest.TestCase): 8 | 9 | _multiprocess_can_split_ = True 10 | 11 | @testing.for_all_dtypes(no_complex=True) 12 | @testing.numpy_clpy_allclose(atol=1e-5) 13 | def check_unary(self, name, xp, dtype): 14 | a = testing.shaped_arange((2, 3), xp, dtype) 15 | return getattr(xp, name)(a) 16 | 17 | @testing.for_all_dtypes(no_complex=True) 18 | @testing.numpy_clpy_allclose(atol=1e-5) 19 | def check_binary(self, name, xp, dtype): 20 | a = testing.shaped_arange((2, 3), xp, dtype) 21 | b = testing.shaped_reverse_arange((2, 3), xp, dtype) 22 | return getattr(xp, name)(a, b) 23 | 24 | def test_logical_and(self): 25 | self.check_binary('logical_and') 26 | 27 | def test_logical_or(self): 28 | self.check_binary('logical_or') 29 | 30 | def test_logical_xor(self): 31 | self.check_binary('logical_xor') 32 | 33 | def test_logical_not(self): 34 | self.check_unary('logical_not') 35 | -------------------------------------------------------------------------------- /clpy/core/internal.pxd: -------------------------------------------------------------------------------- 1 | from libcpp cimport vector 2 | 3 | cpdef Py_ssize_t prod(args, Py_ssize_t init=*) except * 4 | 5 | cpdef Py_ssize_t prod_ssize_t( 6 | vector.vector[Py_ssize_t]& arr, Py_ssize_t init=*) 7 | 8 | cpdef tuple get_size(object size) 9 | 10 | cpdef bint vector_equal( 11 | vector.vector[Py_ssize_t]& x, vector.vector[Py_ssize_t]& y) 12 | 13 | cdef void get_reduced_dims( 14 | vector.vector[Py_ssize_t]& shape, vector.vector[Py_ssize_t]& strides, 15 | Py_ssize_t itemsize, vector.vector[Py_ssize_t]& reduced_shape, 16 | vector.vector[Py_ssize_t]& reduced_strides) 17 | 18 | cpdef vector.vector[Py_ssize_t] get_contiguous_strides( 19 | vector.vector[Py_ssize_t]& shape, Py_ssize_t itemsize, 20 | bint is_c_contiguous) except * 21 | 22 | cpdef bint get_c_contiguity( 23 | vector.vector[Py_ssize_t]& shape, vector.vector[Py_ssize_t]& strides, 24 | Py_ssize_t itemsize) except * 25 | 26 | cpdef vector.vector[Py_ssize_t] infer_unknown_dimension( 27 | vector.vector[Py_ssize_t]& shape, Py_ssize_t size) except * 28 | 29 | cpdef slice complete_slice(slice slc, Py_ssize_t dim) 30 | -------------------------------------------------------------------------------- /clpy/sparse/util.py: -------------------------------------------------------------------------------- 1 | import clpy 2 | import clpy.sparse.base 3 | 4 | 5 | _preamble_atomic_add = ''' 6 | #if __CUDA_ARCH__ < 600 7 | __device__ double atomicAdd(double* address, double val) { 8 | unsigned long long* address_as_ull = 9 | (unsigned long long*)address; 10 | unsigned long long old = *address_as_ull, assumed; 11 | do { 12 | assumed = old; 13 | old = atomicCAS(address_as_ull, assumed, 14 | __double_as_longlong(val + 15 | __longlong_as_double(assumed))); 16 | } while (assumed != old); 17 | return __longlong_as_double(old); 18 | } 19 | #endif 20 | ''' 21 | 22 | 23 | def isintlike(x): 24 | try: 25 | return bool(int(x) == x) 26 | except (TypeError, ValueError): 27 | return False 28 | 29 | 30 | def isscalarlike(x): 31 | return clpy.isscalar(x) or (clpy.sparse.base.isdense(x) and x.ndim == 0) 32 | 33 | 34 | def isshape(x): 35 | if not isinstance(x, tuple) or len(x) != 2: 36 | return False 37 | m, n = x 38 | return isintlike(m) and isintlike(n) 39 | -------------------------------------------------------------------------------- /docs/source/reference/logic.rst: -------------------------------------------------------------------------------- 1 | Logic Functions 2 | =============== 3 | 4 | Truth value testing 5 | ------------------- 6 | 7 | .. autosummary:: 8 | :toctree: generated/ 9 | :nosignatures: 10 | 11 | cupy.all 12 | cupy.any 13 | 14 | 15 | Infinities and NaNs 16 | ------------------- 17 | 18 | .. autosummary:: 19 | :toctree: generated/ 20 | :nosignatures: 21 | 22 | cupy.isfinite 23 | cupy.isinf 24 | cupy.isnan 25 | 26 | 27 | Array type testing 28 | ------------------ 29 | 30 | .. autosummary:: 31 | :toctree: generated/ 32 | :nosignatures: 33 | 34 | cupy.isscalar 35 | 36 | 37 | 38 | Logic operations 39 | ---------------- 40 | 41 | .. autosummary:: 42 | :toctree: generated/ 43 | :nosignatures: 44 | 45 | cupy.logical_and 46 | cupy.logical_or 47 | cupy.logical_not 48 | cupy.logical_xor 49 | 50 | 51 | Comparison operations 52 | --------------------- 53 | 54 | .. autosummary:: 55 | :toctree: generated/ 56 | :nosignatures: 57 | 58 | cupy.greater 59 | cupy.greater_equal 60 | cupy.less 61 | cupy.less_equal 62 | cupy.equal 63 | cupy.not_equal 64 | -------------------------------------------------------------------------------- /clpy/backend/cuda/cupy_thrust.h: -------------------------------------------------------------------------------- 1 | #ifndef INCLUDE_GUARD_CUPY_CUDA_THRUST_H 2 | #define INCLUDE_GUARD_CUPY_CUDA_THRUST_H 3 | 4 | #ifndef CUPY_NO_CUDA 5 | 6 | namespace clpy { 7 | 8 | namespace thrust { 9 | 10 | template void _sort(void *, size_t *, const std::vector&); 11 | 12 | template void _lexsort(size_t *, void *, size_t, size_t); 13 | 14 | template void _argsort(size_t *, void *, void *, const std::vector&); 15 | 16 | } // namespace thrust 17 | 18 | } // namespace clpy 19 | 20 | #else // CUPY_NO_CUDA 21 | 22 | #include "cupy_common.h" 23 | 24 | namespace clpy { 25 | 26 | namespace thrust { 27 | 28 | template void _sort(void *, size_t *, const std::vector&) { return; } 29 | 30 | template void _lexsort(size_t *, void *, size_t, size_t) { return; } 31 | 32 | template void _argsort(size_t *, void *, void *, const std::vector&) { return; } 33 | 34 | } // namespace thrust 35 | 36 | } // namespace clpy 37 | 38 | #endif // #ifndef CUPY_NO_CUDA 39 | 40 | #endif // INCLUDE_GUARD_CUPY_CUDA_THRUST_H 41 | -------------------------------------------------------------------------------- /clpy/linalg/__init__.py: -------------------------------------------------------------------------------- 1 | # Functions from the following NumPy document 2 | # http://docs.scipy.org/doc/numpy/reference/routines.linalg.html 3 | 4 | # "NOQA" to suppress flake8 warning 5 | from clpy.linalg import decomposition # NOQA 6 | from clpy.linalg import eigenvalue # NOQA 7 | from clpy.linalg import einsum # NOQA 8 | from clpy.linalg import norms # NOQA 9 | from clpy.linalg.norms import det # NOQA 10 | from clpy.linalg.norms import matrix_rank # NOQA 11 | from clpy.linalg.norms import norm # NOQA 12 | from clpy.linalg.norms import slogdet # NOQA 13 | from clpy.linalg import product # NOQA 14 | from clpy.linalg import solve # NOQA 15 | 16 | from clpy.linalg.decomposition import cholesky # NOQA 17 | from clpy.linalg.decomposition import qr # NOQA 18 | from clpy.linalg.decomposition import svd # NOQA 19 | 20 | from clpy.linalg.eigenvalue import eigh # NOQA 21 | from clpy.linalg.eigenvalue import eigvalsh # NOQA 22 | 23 | from clpy.linalg.solve import inv # NOQA 24 | from clpy.linalg.solve import pinv # NOQA 25 | from clpy.linalg.solve import solve # NOQA 26 | from clpy.linalg.solve import tensorsolve # NOQA 27 | -------------------------------------------------------------------------------- /clpy/testing/parameterized.py: -------------------------------------------------------------------------------- 1 | import itertools 2 | import sys 3 | import unittest 4 | 5 | 6 | def _gen_case(base, module, i, param): 7 | cls_name = '%s_param_%d' % (base.__name__, i) 8 | 9 | def __str__(self): 10 | name = base.__str__(self) 11 | return '%s parameter: %s' % (name, param) 12 | 13 | mb = dict(param) 14 | 15 | mb['__str__'] = __str__ 16 | cls = type(cls_name, (base,), mb) 17 | setattr(module, cls_name, cls) 18 | 19 | 20 | def _gen_cases(name, base, params): 21 | module = sys.modules[name] 22 | for i, param in enumerate(params): 23 | _gen_case(base, module, i, param) 24 | 25 | 26 | def parameterize(*params): 27 | def f(klass): 28 | assert issubclass(klass, unittest.TestCase) 29 | _gen_cases(klass.__module__, klass, params) 30 | # Remove original base class 31 | return None 32 | return f 33 | 34 | 35 | def product(parameter): 36 | keys = sorted(parameter) 37 | values = [parameter[key] for key in keys] 38 | values_product = itertools.product(*values) 39 | return [dict(zip(keys, vals)) for vals in values_product] 40 | -------------------------------------------------------------------------------- /clpy/logic/ops.py: -------------------------------------------------------------------------------- 1 | from clpy import core 2 | 3 | logical_and = core.create_comparison( 4 | 'logical_and', '&&', 5 | '''Computes the logical AND of two arrays. 6 | 7 | .. seealso:: :data:`numpy.logical_and` 8 | 9 | ''') 10 | 11 | 12 | logical_or = core.create_comparison( 13 | 'logical_or', '||', 14 | '''Computes the logical OR of two arrays. 15 | 16 | .. seealso:: :data:`numpy.logical_or` 17 | 18 | ''') 19 | 20 | 21 | logical_not = core.create_ufunc( 22 | 'clpy_logical_not', 23 | ('?->?', 'b->?', 'B->?', 'h->?', 'H->?', 'i->?', 'I->?', 'l->?', 'L->?', 24 | 'q->?', 'Q->?', 'f->?', 'd->?'), 25 | 'out0 = !in0', 26 | doc='''Computes the logical NOT of an array. 27 | 28 | .. seealso:: :data:`numpy.logical_not` 29 | 30 | ''') 31 | 32 | 33 | logical_xor = core.create_ufunc( 34 | 'clpy_logical_xor', 35 | ('??->?', 'bb->?', 'BB->?', 'hh->?', 'HH->?', 'ii->?', 'II->?', 'll->?', 36 | 'LL->?', 'qq->?', 'QQ->?', 'ff->?', 'dd->?'), 37 | 'out0 = !in0 != !in1', 38 | doc='''Computes the logical XOR of two arrays. 39 | 40 | .. seealso:: :data:`numpy.logical_xor` 41 | 42 | ''') 43 | -------------------------------------------------------------------------------- /tests/example_tests/test_gmm.py: -------------------------------------------------------------------------------- 1 | import os 2 | import shutil 3 | import tempfile 4 | import unittest 5 | 6 | import six 7 | 8 | from clpy import testing 9 | 10 | from example_tests import example_test 11 | 12 | 13 | @testing.with_requires('matplotlib') 14 | class TestGMM(unittest.TestCase): 15 | 16 | def test_gmm(self): 17 | output = example_test.run_example('gmm/gmm.py', '--num', '10') 18 | six.assertRegex( 19 | self, output.decode('utf-8'), 20 | r'Running CPU\.\.\.\s+train_accuracy : [0-9\.]+\s+' + 21 | r'test_accuracy : [0-9\.]+\s+CPU : [0-9\.]+ sec\s+' + 22 | r'Running GPU\.\.\.\s+train_accuracy : [0-9\.]+\s+' + 23 | r'test_accuracy : [0-9\.]+\s+GPU : [0-9\.]+ sec') 24 | 25 | def test_output_image(self): 26 | dir_path = tempfile.mkdtemp() 27 | try: 28 | image_path = os.path.join(dir_path, 'gmm.png') 29 | example_test.run_example( 30 | 'gmm/gmm.py', '--num', '10', '-o', image_path) 31 | self.assertTrue(os.path.exists(image_path)) 32 | finally: 33 | shutil.rmtree(dir_path, ignore_errors=True) 34 | -------------------------------------------------------------------------------- /examples/gemm/utils.py: -------------------------------------------------------------------------------- 1 | import clpy as cp 2 | 3 | 4 | import os 5 | 6 | 7 | def include_path(): 8 | return os.path.join(cp.__path__[0], "..", "clpy", "core", "include") 9 | 10 | 11 | @cp.util.memoize(for_each_device=True) 12 | def load_kernel(kernel_name, code, options=()): 13 | assert isinstance(options, tuple) 14 | kernel_code = cp.backend.compile_with_cache(code, options=options) 15 | return kernel_code.get_function(kernel_name) 16 | 17 | 18 | def read_code(code_filename, params): 19 | with open(code_filename, 'r') as f: 20 | code = f.read() 21 | for k, v in params.items(): 22 | code = '#define ' + k + ' ' + str(v) + '\n' + code 23 | return code 24 | 25 | 26 | # TODO(shusukeueda): 27 | # ClPy does not support cp.backend.Event (clpy/backend/stream.py) 28 | def benchmark(func, args, n_run): 29 | times = [] 30 | for _ in range(n_run): 31 | start = cp.backend.Event() 32 | end = cp.backend.Event() 33 | start.record() 34 | func(*args) 35 | end.record() 36 | end.synchronize() 37 | times.append(cp.backend.get_elapsed_time(start, end)) # milliseconds 38 | return times 39 | -------------------------------------------------------------------------------- /tests/clpy_tests/cuda_tests/test_compiler.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import six 4 | 5 | from cupy.cuda import compiler 6 | from cupy import testing 7 | 8 | 9 | @testing.gpu 10 | class TestNvrtcStderr(unittest.TestCase): 11 | 12 | def test(self): 13 | # An error message contains the file name `kern.cu` 14 | with six.assertRaisesRegex(self, compiler.CompileException, 'kern.cu'): 15 | compiler.compile_using_nvrtc('a') 16 | 17 | 18 | class TestIsValidKernelName(unittest.TestCase): 19 | 20 | def test_valid(self): 21 | self.assertTrue(compiler.is_valid_kernel_name('valid_name_1')) 22 | 23 | def test_empyt(self): 24 | self.assertFalse(compiler.is_valid_kernel_name('')) 25 | 26 | def test_start_with_digit(self): 27 | self.assertFalse(compiler.is_valid_kernel_name('0_invalid')) 28 | 29 | def test_new_line(self): 30 | self.assertFalse(compiler.is_valid_kernel_name('invalid\nname')) 31 | 32 | def test_symbol(self): 33 | self.assertFalse(compiler.is_valid_kernel_name('invalid$name')) 34 | 35 | def test_space(self): 36 | self.assertFalse(compiler.is_valid_kernel_name('invalid name')) 37 | -------------------------------------------------------------------------------- /tests/clpy_tests/random_tests/test_distributions.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import clpy 4 | from clpy.random import distributions 5 | from clpy import testing 6 | 7 | 8 | @testing.parameterize(*testing.product({ 9 | 'shape': [(4, 3, 2), (3, 2)], 10 | 'loc_shape': [(), (3, 2)], 11 | 'scale_shape': [(), (3, 2)], 12 | }) 13 | ) 14 | @testing.gpu 15 | class TestDistributions(unittest.TestCase): 16 | 17 | _multiprocess_can_split_ = True 18 | 19 | def check_distribution(self, dist_func, loc_dtype, scale_dtype, dtype): 20 | loc = clpy.ones(self.loc_shape, dtype=loc_dtype) 21 | scale = clpy.ones(self.scale_shape, dtype=scale_dtype) 22 | out = dist_func(loc, scale, self.shape, dtype) 23 | self.assertEqual(self.shape, out.shape) 24 | self.assertEqual(out.dtype, dtype) 25 | 26 | @clpy.testing.for_float_dtypes('dtype', no_float16=True) 27 | @clpy.testing.for_float_dtypes('loc_dtype') 28 | @clpy.testing.for_float_dtypes('scale_dtype') 29 | def test_normal(self, loc_dtype, scale_dtype, dtype): 30 | self.check_distribution(distributions.normal, 31 | loc_dtype, scale_dtype, dtype) 32 | -------------------------------------------------------------------------------- /tests/clpy_tests/math_tests/test_rounding.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from clpy import testing 4 | 5 | 6 | @testing.gpu 7 | class TestRounding(unittest.TestCase): 8 | 9 | _multiprocess_can_split_ = True 10 | 11 | @testing.for_all_dtypes(no_complex=True) 12 | @testing.numpy_clpy_allclose(atol=1e-5) 13 | def check_unary(self, name, xp, dtype): 14 | a = testing.shaped_arange((2, 3), xp, dtype) 15 | return getattr(xp, name)(a) 16 | 17 | @testing.for_dtypes(['?', 'b', 'h', 'i', 'q', 'f', 'd']) 18 | @testing.numpy_clpy_allclose(atol=1e-5) 19 | def check_unary_negative(self, name, xp, dtype): 20 | a = xp.array([-3, -2, -1, 1, 2, 3], dtype=dtype) 21 | return getattr(xp, name)(a) 22 | 23 | def test_rint(self): 24 | self.check_unary('rint') 25 | 26 | def test_rint_negative(self): 27 | self.check_unary_negative('rint') 28 | 29 | def test_floor(self): 30 | self.check_unary('floor') 31 | 32 | def test_ceil(self): 33 | self.check_unary('ceil') 34 | 35 | def test_trunc(self): 36 | self.check_unary('trunc') 37 | 38 | def test_fix(self): 39 | self.check_unary('fix') 40 | -------------------------------------------------------------------------------- /clpy/logic/content.py: -------------------------------------------------------------------------------- 1 | from clpy import core 2 | 3 | 4 | def _create_float_test_ufunc(name, doc): 5 | return core.create_ufunc( 6 | 'clpy_' + name, 7 | (('b->?', 'out0 = %s((float)in0)' % name), 8 | ('B->?', 'out0 = %s((float)in0)' % name), 9 | 'f->?', 'd->?', 'F->?', 'D->?', 10 | ), 'out0 = %s(in0)' % name, 11 | doc=doc) 12 | 13 | 14 | isfinite = _create_float_test_ufunc( 15 | 'isfinite', 16 | '''Tests finiteness elementwise. 17 | 18 | Each element of returned array is ``True`` only if the corresponding 19 | element of the input is finite (i.e. not an infinity nor NaN). 20 | 21 | .. seealso:: :data:`numpy.isfinite` 22 | 23 | ''') 24 | 25 | 26 | isinf = _create_float_test_ufunc( 27 | 'isinf', 28 | '''Tests if each element is the positive or negative infinity. 29 | 30 | .. seealso:: :data:`numpy.isinf` 31 | 32 | ''') 33 | 34 | 35 | isnan = _create_float_test_ufunc( 36 | 'isnan', 37 | '''Tests if each element is a NaN. 38 | 39 | .. seealso:: :data:`numpy.isnan` 40 | 41 | ''') 42 | 43 | 44 | # TODO(okuta): Implement isneginf 45 | 46 | 47 | # TODO(okuta): Implement isposinf 48 | -------------------------------------------------------------------------------- /tests/clpy_tests/creation_tests/test_matrix.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from clpy import testing 4 | 5 | 6 | @testing.gpu 7 | class TestMatrix(unittest.TestCase): 8 | 9 | _multiprocess_can_split_ = True 10 | 11 | @testing.numpy_clpy_array_equal() 12 | def test_diag1(self, xp): 13 | a = testing.shaped_arange((3, 3), xp) 14 | return xp.diag(a) 15 | 16 | @testing.numpy_clpy_array_equal() 17 | def test_diag2(self, xp): 18 | a = testing.shaped_arange((3, 3), xp) 19 | return xp.diag(a, 1) 20 | 21 | @testing.numpy_clpy_array_equal() 22 | def test_diag3(self, xp): 23 | a = testing.shaped_arange((3, 3), xp) 24 | return xp.diag(a, -2) 25 | 26 | @testing.numpy_clpy_array_equal() 27 | def test_diagflat1(self, xp): 28 | a = testing.shaped_arange((3, 3), xp) 29 | return xp.diagflat(a) 30 | 31 | @testing.numpy_clpy_array_equal() 32 | def test_diagflat2(self, xp): 33 | a = testing.shaped_arange((3, 3), xp) 34 | return xp.diagflat(a, 1) 35 | 36 | @testing.numpy_clpy_array_equal() 37 | def test_diagflat3(self, xp): 38 | a = testing.shaped_arange((3, 3), xp) 39 | return xp.diagflat(a, -2) 40 | -------------------------------------------------------------------------------- /tests/clpy_tests/opencl_tests/test_rollaxis.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import unittest 4 | 5 | import clpy 6 | import numpy 7 | 8 | 9 | class TestRollaxis(unittest.TestCase): 10 | """test class of rollaxis""" 11 | 12 | def test_import(self): 13 | self.assertTrue(True) # Always OK if no exeption from import 14 | 15 | def test_2_3_matrix(self): 16 | npA = numpy.array([[1, 2, 3], [4, 5, 6]]) 17 | expectedA = numpy.rollaxis(npA, 1, 0) 18 | clpA = clpy.array([[1, 2, 3], [4, 5, 6]]) 19 | actualA = clpy.rollaxis(clpA, 1, 0) 20 | self.assertTrue(numpy.allclose(expectedA, actualA.get())) 21 | 22 | def test_2_3_4_matrix(self): 23 | npA = numpy.array([[[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6]], [ 24 | [4, 5, 6, 7], [5, 6, 7, 8], [6, 7, 8, 9]]]) 25 | expectedA = numpy.rollaxis(npA, 1, 0) 26 | clpA = clpy.array([[[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6]], [ 27 | [4, 5, 6, 7], [5, 6, 7, 8], [6, 7, 8, 9]]]) 28 | actualA = clpy.rollaxis(clpA, 1, 0) 29 | self.assertTrue(numpy.allclose(expectedA, actualA.get())) 30 | 31 | 32 | if __name__ == "__main__": 33 | unittest.main() 34 | -------------------------------------------------------------------------------- /clpy/backend/cuda/curand.pxd: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Types 3 | ############################################################################### 4 | 5 | cdef extern from *: 6 | ctypedef int Ordering 'curandOrdering_t' 7 | ctypedef int RngType 'curandRngType_t' 8 | 9 | ctypedef void* Generator 'curandGenerator_t' 10 | 11 | 12 | ############################################################################### 13 | # Enum 14 | ############################################################################### 15 | 16 | cpdef enum: 17 | CURAND_RNG_PSEUDO_DEFAULT = 100 18 | CURAND_RNG_PSEUDO_XORWOW = 101 19 | CURAND_RNG_PSEUDO_MRG32K3A = 121 20 | CURAND_RNG_PSEUDO_MTGP32 = 141 21 | CURAND_RNG_PSEUDO_MT19937 = 142 22 | CURAND_RNG_PSEUDO_PHILOX4_32_10 = 161 23 | CURAND_RNG_QUASI_DEFAULT = 200 24 | CURAND_RNG_QUASI_SOBOL32 = 201 25 | CURAND_RNG_QUASI_SCRAMBLED_SOBOL32 = 202 26 | CURAND_RNG_QUASI_SOBOL64 = 203 27 | CURAND_RNG_QUASI_SCRAMBLED_SOBOL64 = 204 28 | 29 | CURAND_ORDERING_PSEUDO_BEST = 100 30 | CURAND_ORDERING_PSEUDO_DEFAULT = 101 31 | CURAND_ORDERING_PSEUDO_SEEDED = 102 32 | CURAND_ORDERING_QUASI_DEFAULT = 201 33 | -------------------------------------------------------------------------------- /docs/source/reference/sparse.rst: -------------------------------------------------------------------------------- 1 | ------------- 2 | Sparse matrix 3 | ------------- 4 | 5 | CuPy supports sparse matrices using `cuSPARSE `_. 6 | These matrices have the same interfaces of `SciPy's sparse matrices `_. 7 | 8 | .. module:: cupy.sparse 9 | 10 | 11 | Sparse matrix classes 12 | --------------------- 13 | 14 | .. autosummary:: 15 | :toctree: generated/ 16 | :nosignatures: 17 | 18 | cupy.sparse.coo_matrix 19 | cupy.sparse.csr_matrix 20 | cupy.sparse.csc_matrix 21 | cupy.sparse.dia_matrix 22 | cupy.sparse.spmatrix 23 | 24 | 25 | Functions 26 | --------- 27 | 28 | Building sparse matrices 29 | ~~~~~~~~~~~~~~~~~~~~~~~~ 30 | 31 | .. autosummary:: 32 | :toctree: generated/ 33 | :nosignatures: 34 | 35 | cupy.sparse.eye 36 | cupy.sparse.identity 37 | 38 | 39 | Identifying sparse matrices 40 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~ 41 | 42 | .. autosummary:: 43 | :toctree: generated/ 44 | :nosignatures: 45 | 46 | cupy.sparse.issparse 47 | cupy.sparse.isspmatrix 48 | cupy.sparse.isspmatrix_csc 49 | cupy.sparse.isspmatrix_csr 50 | cupy.sparse.isspmatrix_coo 51 | cupy.sparse.isspmatrix_dia 52 | -------------------------------------------------------------------------------- /clpy/sorting/count.py: -------------------------------------------------------------------------------- 1 | from clpy import core 2 | 3 | 4 | def count_nonzero(a, axis=None): 5 | """Counts the number of non-zero values in the array. 6 | 7 | .. note:: 8 | 9 | :func:`numpy.count_nonzero` returns `int` value when `axis=None`, 10 | but :func:`clpy.count_nonzero` returns zero-dimensional array to reduce 11 | CPU-GPU synchronization. 12 | 13 | Args: 14 | a (clpy.ndarray): The array for which to count non-zeros. 15 | axis (int or tuple, optional): Axis or tuple of axes along which to 16 | count non-zeros. Default is None, meaning that non-zeros will be 17 | counted along a flattened version of ``a`` 18 | Returns: 19 | clpy.ndarray of int: Number of non-zero values in the array 20 | along a given axis. Otherwise, the total number of non-zero values 21 | in the array is returned. 22 | """ 23 | 24 | return _count_nonzero(a, axis=axis) 25 | 26 | 27 | _count_nonzero = core.create_reduction_func( 28 | 'clpy_count_nonzero', 29 | ('?->l', 'B->l', 'h->l', 'H->l', 'i->l', 'I->l', 'l->l', 'L->l', 30 | 'q->l', 'Q->l', 'f->l', 'd->l', 'F->l', 'D->l'), 31 | ('in0 != type_in0_data(0)', 'a + b', 'out0 = a', None), 0) 32 | -------------------------------------------------------------------------------- /jenkins/post_comment.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -ex 3 | 4 | ERRORS_FILENAME=$WORKSPACE/erros.log 5 | 6 | # This script is kicked with $1=0 7 | # only if "bash build_and_test.sh" has exited successfully. 8 | if [[ $1 -eq 0 ]]; then 9 | BODY="Test (commit ${GIT_COMMIT}) passed on *$(uname -n)*." 10 | EVENT="APPROVE" 11 | else 12 | N_ERRORFILE_LINES=$(cat ${ERRORS_FILENAME} | wc -l) 13 | N_CROP=50 14 | BODY="Test (commit ${GIT_COMMIT}) failed on *$(uname -n)*. 15 | \`\`\` 16 | $(head -n ${N_CROP} ${ERRORS_FILENAME}) 17 | \`\`\`" 18 | EVENT="REQUEST_CHANGES" 19 | 20 | # If the error file is too long, 21 | # mention that there are more lines. 22 | if [[ $N_ERRORFILE_LINES -gt $N_CROP ]]; then 23 | BODY="$BODY 24 | 25 | ... and more $(($N_ERRORFILE_LINES - $N_CROP)) lines" 26 | fi 27 | 28 | fi 29 | 30 | 31 | # BODY may contain [",\]. Escape it with jq's raw input option (-R). 32 | # jq puts the escaped string from stdin onto the place of ".". 33 | echo "${BODY}" | 34 | jq -sR "{ 35 | \"commit_id\": \"${ghprbActualCommit}\", 36 | \"body\": . , 37 | \"event\": \"${EVENT}\" 38 | }" | 39 | curl -u jenkins-maekawa:${access_token} -XPOST "https://api.github.com/repos/fixstars/clpy/pulls/${ghprbPullId}/reviews" -d @- 40 | -------------------------------------------------------------------------------- /CONTRIBUTOR_CLPY.md: -------------------------------------------------------------------------------- 1 | # ClPy's contributors 2 | 3 | Alphabetically sorted: 4 | 5 | * Akira Tanaka (National Institute of Advanced Industrial Science and Technology) 6 | * Kenjiro Taura (The University of Tokyo Taura Group) 7 | * Naoki Yoshifuji (Fixstars Corporation) 8 | * Naoya Sakabe (Fixstars Corporation) 9 | * Ryousei Takano (National Institute of Advanced Industrial Science and Technology) 10 | * Tomoya Sakai (Fixstars Corporation) 11 | * Toru Asaka (Fixstars Corporation) 12 | * Toru Fukaya (Fixstars Corporation) 13 | * Tsutomu Ikegami (National Institute of Advanced Industrial Science and Technology) 14 | * Yoriyuki Kitta (Fixstars Corporation) 15 | * Yoshiki Imaizumi (Fixstars Corporation) 16 | * Yuki Ito (Fixstars Corporation) 17 | * Yuya Kawabata (Fixstars Corporation) 18 | 19 | and [original cupy's contributors and others](https://github.com/fixstars/clpy/graphs/contributors). 20 | 21 | ## Acknowledgment 22 | 23 | * This work is partially based on results obtained from a project commissioned by the New Energy and Industrial Technology Development Organization (NEDO), Japan. 24 | * The machine to develop [multiple GPU support](https://github.com/fixstars/clpy/pull/160) is voluntarily provided by [SAKURA Internet Inc.](https://www.sakura.ad.jp/koukaryoku/). 25 | -------------------------------------------------------------------------------- /docs/source/_templates/autosummary/class.rst: -------------------------------------------------------------------------------- 1 | {{ fullname }} 2 | {{ underline }} 3 | 4 | .. currentmodule:: {{ module }} 5 | 6 | .. autoclass:: {{ objname }} 7 | 8 | .. 9 | Methods 10 | 11 | {% block methods %} 12 | 13 | .. rubric:: Methods 14 | 15 | .. 16 | Special methods 17 | 18 | {% for item in ('__call__', '__enter__', '__exit__', '__getitem__', '__setitem__', '__len__', '__next__', '__iter__', '__copy__') %} 19 | {% if item in all_methods %} 20 | .. automethod:: {{ item }} 21 | {% endif %} 22 | {%- endfor %} 23 | 24 | .. 25 | Ordinary methods 26 | 27 | {% for item in methods %} 28 | {% if item not in ('__init__',) %} 29 | .. automethod:: {{ item }} 30 | {% endif %} 31 | {%- endfor %} 32 | 33 | .. 34 | Special methods 35 | 36 | {% for item in ('__eq__', '__ne__', '__lt__', '__le__', '__gt__', '__ge__', '__nonzero__', '__bool__') %} 37 | {% if item in all_methods %} 38 | .. automethod:: {{ item }} 39 | {% endif %} 40 | {%- endfor %} 41 | {% endblock %} 42 | 43 | .. 44 | Atributes 45 | 46 | {% block attributes %} {% if attributes %} 47 | 48 | .. rubric:: Attributes 49 | 50 | {% for item in attributes %} 51 | .. autoattribute:: {{ item }} 52 | {%- endfor %} 53 | {% endif %} {% endblock %} 54 | -------------------------------------------------------------------------------- /docs/source/reference/linalg.rst: -------------------------------------------------------------------------------- 1 | Linear Algebra 2 | ============== 3 | 4 | Matrix and vector products 5 | -------------------------- 6 | 7 | .. autosummary:: 8 | :toctree: generated/ 9 | :nosignatures: 10 | 11 | cupy.dot 12 | cupy.vdot 13 | cupy.inner 14 | cupy.outer 15 | cupy.matmul 16 | cupy.tensordot 17 | cupy.einsum 18 | cupy.kron 19 | 20 | Decompositions 21 | -------------- 22 | 23 | .. autosummary:: 24 | :toctree: generated/ 25 | :nosignatures: 26 | 27 | cupy.linalg.cholesky 28 | cupy.linalg.qr 29 | cupy.linalg.svd 30 | 31 | Matrix eigenvalues 32 | ------------------ 33 | 34 | .. autosummary:: 35 | :toctree: generated/ 36 | :nosignatures: 37 | 38 | cupy.linalg.eigh 39 | cupy.linalg.eigvalsh 40 | 41 | Norms etc. 42 | ---------- 43 | 44 | .. autosummary:: 45 | :toctree: generated/ 46 | :nosignatures: 47 | 48 | cupy.linalg.det 49 | cupy.linalg.norm 50 | cupy.linalg.matrix_rank 51 | cupy.linalg.slogdet 52 | cupy.trace 53 | 54 | 55 | Solving linear equations 56 | -------------------------- 57 | 58 | .. autosummary:: 59 | :toctree: generated/ 60 | :nosignatures: 61 | 62 | cupy.linalg.solve 63 | cupy.linalg.tensorsolve 64 | cupy.linalg.inv 65 | cupy.linalg.pinv 66 | -------------------------------------------------------------------------------- /clpy/backend/cuda/cupy_nvrtc.h: -------------------------------------------------------------------------------- 1 | // This file is a stub header file of nvrtc for Read the Docs. 2 | 3 | #ifndef INCLUDE_GUARD_CUPY_NVRTC_H 4 | #define INCLUDE_GUARD_CUPY_NVRTC_H 5 | 6 | #ifndef CUPY_NO_CUDA 7 | 8 | #include 9 | 10 | #else // #ifndef CUPY_NO_CUDA 11 | 12 | extern "C" { 13 | 14 | typedef enum { 15 | NVRTC_SUCCESS = 0, 16 | } nvrtcResult; 17 | 18 | typedef struct _nvrtcProgram *nvrtcProgram; 19 | 20 | const char *nvrtcGetErrorString(...) { 21 | return NULL; 22 | } 23 | 24 | nvrtcResult nvrtcVersion(...) { 25 | return NVRTC_SUCCESS; 26 | } 27 | 28 | nvrtcResult nvrtcCreateProgram(...) { 29 | return NVRTC_SUCCESS; 30 | } 31 | 32 | nvrtcResult nvrtcDestroyProgram(...) { 33 | return NVRTC_SUCCESS; 34 | } 35 | 36 | nvrtcResult nvrtcCompileProgram(...) { 37 | return NVRTC_SUCCESS; 38 | } 39 | 40 | nvrtcResult nvrtcGetPTXSize(...) { 41 | return NVRTC_SUCCESS; 42 | } 43 | 44 | nvrtcResult nvrtcGetPTX(...) { 45 | return NVRTC_SUCCESS; 46 | } 47 | 48 | nvrtcResult nvrtcGetProgramLogSize(...) { 49 | return NVRTC_SUCCESS; 50 | } 51 | 52 | nvrtcResult nvrtcGetProgramLog(...) { 53 | return NVRTC_SUCCESS; 54 | } 55 | 56 | } 57 | 58 | #endif // #ifndef CUPY_NO_CUDA 59 | 60 | #endif // #ifndef INCLUDE_GUARD_CUPY_NVRTC_H 61 | -------------------------------------------------------------------------------- /clpy/math/hyperbolic.py: -------------------------------------------------------------------------------- 1 | from clpy.math import ufunc 2 | 3 | 4 | sinh = ufunc.create_math_ufunc( 5 | 'sinh', 1, 'clpy_sinh', 6 | '''Elementwise hyperbolic sine function. 7 | 8 | .. seealso:: :data:`numpy.sinh` 9 | 10 | ''') 11 | 12 | 13 | cosh = ufunc.create_math_ufunc( 14 | 'cosh', 1, 'clpy_cosh', 15 | '''Elementwise hyperbolic cosine function. 16 | 17 | .. seealso:: :data:`numpy.cosh` 18 | 19 | ''') 20 | 21 | 22 | tanh = ufunc.create_math_ufunc( 23 | 'tanh', 1, 'clpy_tanh', 24 | '''Elementwise hyperbolic tangent function. 25 | 26 | .. seealso:: :data:`numpy.tanh` 27 | 28 | ''') 29 | 30 | 31 | arcsinh = ufunc.create_math_ufunc( 32 | 'asinh', 1, 'clpy_arcsinh', 33 | '''Elementwise inverse of hyperbolic sine function. 34 | 35 | .. seealso:: :data:`numpy.arcsinh` 36 | 37 | ''') 38 | 39 | 40 | arccosh = ufunc.create_math_ufunc( 41 | 'acosh', 1, 'clpy_arccosh', 42 | '''Elementwise inverse of hyperbolic cosine function. 43 | 44 | .. seealso:: :data:`numpy.arccosh` 45 | 46 | ''') 47 | 48 | 49 | arctanh = ufunc.create_math_ufunc( 50 | 'atanh', 1, 'clpy_arctanh', 51 | '''Elementwise inverse of hyperbolic tangent function. 52 | 53 | .. seealso:: :data:`numpy.arctanh` 54 | 55 | ''') 56 | -------------------------------------------------------------------------------- /tests/clpy_tests/binary_tests/test_elementwise.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from clpy import testing 4 | 5 | 6 | @testing.gpu 7 | class TestElementwise(unittest.TestCase): 8 | 9 | _multiprocess_can_split_ = True 10 | 11 | @testing.for_int_dtypes() 12 | @testing.numpy_clpy_array_equal() 13 | def check_unary_int(self, name, xp, dtype): 14 | a = xp.array([-3, -2, -1, 0, 1, 2, 3], dtype=dtype) 15 | return getattr(xp, name)(a) 16 | 17 | @testing.for_int_dtypes() 18 | @testing.numpy_clpy_array_equal() 19 | def check_binary_int(self, name, xp, dtype): 20 | a = xp.array([-3, -2, -1, 0, 1, 2, 3], dtype=dtype) 21 | b = xp.array([0, 1, 2, 3, 4, 5, 6], dtype=dtype) 22 | return getattr(xp, name)(a, b) 23 | 24 | def test_bitwise_and(self): 25 | self.check_binary_int('bitwise_and') 26 | 27 | def test_bitwise_or(self): 28 | self.check_binary_int('bitwise_or') 29 | 30 | def test_bitwise_xor(self): 31 | self.check_binary_int('bitwise_xor') 32 | 33 | def test_invert(self): 34 | self.check_unary_int('invert') 35 | 36 | def test_left_shift(self): 37 | self.check_binary_int('left_shift') 38 | 39 | def test_right_shift(self): 40 | self.check_binary_int('right_shift') 41 | -------------------------------------------------------------------------------- /tests/clpy_tests/math_tests/test_hyperbolic.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from clpy import testing 4 | 5 | 6 | @testing.gpu 7 | class TestHyperbolic(unittest.TestCase): 8 | 9 | _multiprocess_can_split_ = True 10 | 11 | @testing.for_all_dtypes() 12 | @testing.numpy_clpy_allclose(atol=1e-5) 13 | def check_unary(self, name, xp, dtype): 14 | a = testing.shaped_arange((2, 3), xp, dtype) 15 | return getattr(xp, name)(a) 16 | 17 | @testing.for_dtypes(['f', 'd']) 18 | @testing.numpy_clpy_allclose(atol=1e-5) 19 | def check_unary_unit(self, name, xp, dtype): 20 | a = xp.array([0.2, 0.4, 0.6, 0.8], dtype=dtype) 21 | return getattr(xp, name)(a) 22 | 23 | def test_sinh(self): 24 | self.check_unary('sinh') 25 | 26 | def test_cosh(self): 27 | self.check_unary('cosh') 28 | 29 | def test_tanh(self): 30 | self.check_unary('tanh') 31 | 32 | def test_arcsinh(self): 33 | self.check_unary('arcsinh') 34 | 35 | @testing.for_dtypes(['f', 'd']) 36 | @testing.numpy_clpy_allclose(atol=1e-5) 37 | def test_arccosh(self, xp, dtype): 38 | a = xp.array([1, 2, 3], dtype=dtype) 39 | return xp.arccosh(a) 40 | 41 | def test_arctanh(self): 42 | self.check_unary_unit('arctanh') 43 | -------------------------------------------------------------------------------- /clpy/backend/cuda/cusparse.pxd: -------------------------------------------------------------------------------- 1 | cdef extern from *: 2 | ctypedef int IndexBase 'cusparseIndexBase_t' 3 | ctypedef int Status 'cusparseStatus_t' 4 | 5 | ctypedef void* Handle 'cusparseHandle_t' 6 | 7 | ctypedef void* MatDescr 'cusparseMatDescr_t' 8 | 9 | ctypedef int Direction 'cusparseDirection_t' 10 | 11 | ctypedef int MatrixType 'cusparseMatrixType_t' 12 | 13 | ctypedef int Operation 'cusparseOperation_t' 14 | 15 | ctypedef int PointerMode 'cusparsePointerMode_t' 16 | 17 | ctypedef int Action 'cusparseAction_t' 18 | 19 | 20 | cpdef enum: 21 | CUSPARSE_POINTER_MODE_HOST = 0 22 | CUSPARSE_POINTER_MODE_DEVICE = 1 23 | 24 | CUSPARSE_ACTION_SYMBOLIC = 0 25 | CUSPARSE_ACTION_NUMERIC = 1 26 | 27 | CUSPARSE_INDEX_BASE_ZERO = 0 28 | CUSPARSE_INDEX_BASE_ONE = 1 29 | 30 | CUSPARSE_MATRIX_TYPE_GENERAL = 0 31 | CUSPARSE_MATRIX_TYPE_SYMMETRIC = 1 32 | CUSPARSE_MATRIX_TYPE_HERMITIAN = 2 33 | CUSPARSE_MATRIX_TYPE_TRIANGULAR = 3 34 | 35 | CUSPARSE_OPERATION_NON_TRANSPOSE = 0 36 | CUSPARSE_OPERATION_TRANSPOSE = 1 37 | CUSPARSE_OPERATION_CONJUGATE_TRANSPOSE = 2 38 | 39 | CUSPARSE_DIRECTION_ROW = 0 40 | CUSPARSE_DIRECTION_COLUMN = 1 41 | 42 | 43 | cpdef size_t create() except * 44 | cpdef void destroy(size_t handle) 45 | -------------------------------------------------------------------------------- /docs/source/_static/css/modified_theme.css: -------------------------------------------------------------------------------- 1 | @import url('theme.css'); 2 | 3 | /* Main background color modification */ 4 | .btn-info, .wy-menu-vertical a:active, .wy-side-nav-search, .wy-side-nav-search img, .wy-nav .wy-menu-vertical a:hover, .wy-nav-top img, .wy-tray-item-info, .wy-side-nav-search, .wy-dropdown-menu > dd > a:hover, .wy-dropdown-menu a:hover, .wy-nav-top { 5 | background-color: #70c162 !important; 6 | } 7 | 8 | .wy-side-nav-search input[type=text] { 9 | border-color: #72a424 !important; 10 | } 11 | 12 | .rst-content .note .admonition-title { 13 | background-color: #c9c9c9 !important; 14 | } 15 | 16 | .rst-content .note { 17 | background-color: #e3e3e3 !important; 18 | } 19 | 20 | 21 | /* code style */ 22 | .rst-content table.longtable.docutils code { 23 | font-size: 100% !important; 24 | background-color: transparent !important; 25 | border: none !important; 26 | } 27 | 28 | .rst-content a.reference code { 29 | color: #3399cc !important; 30 | } 31 | 32 | .rst-content a.reference code:hover { 33 | text-decoration: underline !important; 34 | color: #3366cc !important; 35 | } 36 | 37 | 38 | /* rubric */ 39 | .rst-content .class .rubric { 40 | color: #333333; 41 | background-color: #aaeeaa; 42 | padding: 0.2em 0 0.2em 1em; 43 | border-top: solid 0.3em #66cc66; 44 | } 45 | -------------------------------------------------------------------------------- /tests/example_tests/test_kmeans.py: -------------------------------------------------------------------------------- 1 | import os 2 | import shutil 3 | import tempfile 4 | import unittest 5 | 6 | import six 7 | 8 | from clpy import testing 9 | 10 | from example_tests import example_test 11 | 12 | 13 | @testing.with_requires('matplotlib') 14 | class TestKmeans(unittest.TestCase): 15 | 16 | def test_default(self): 17 | output = example_test.run_example( 18 | 'kmeans/kmeans.py', '-m', '1', '--num', '10') 19 | six.assertRegex( 20 | self, output.decode('utf-8'), 21 | r' CPU : [0-9\.]+ sec\s+GPU : [0-9\.]+ sec') 22 | 23 | def test_custom_kernel(self): 24 | output = example_test.run_example( 25 | 'kmeans/kmeans.py', '-m', '1', '--num', '10', 26 | '--use-custom-kernel') 27 | six.assertRegex( 28 | self, output.decode('utf-8'), 29 | r' CPU : [0-9\.]+ sec\s+GPU : [0-9\.]+ sec') 30 | 31 | def test_result_image(self): 32 | dir_path = tempfile.mkdtemp() 33 | try: 34 | image_path = os.path.join(dir_path, 'kmeans.png') 35 | example_test.run_example( 36 | 'kmeans/kmeans.py', '-m', '1', '--num', '10', '-o', image_path) 37 | self.assertTrue(os.path.exists(image_path)) 38 | finally: 39 | shutil.rmtree(dir_path, ignore_errors=True) 40 | -------------------------------------------------------------------------------- /tests/clpy_tests/opencl_tests/ultima_tests/test_cindexer.py: -------------------------------------------------------------------------------- 1 | # flake8: noqa 2 | # TODO(vorj): When we will meet flake8 3.7.0+, 3 | # we should ignore only W291 for whole file 4 | # using --per-file-ignores . 5 | 6 | import clpy 7 | import unittest 8 | 9 | 10 | class TestUltimaCIndexer(unittest.TestCase): 11 | 12 | def test_cindexer_argument_mutation(self): 13 | x = clpy.backend.ultima.exec_ultima('', '#include ') + ''' 14 | void f(CIndexer_2 ind) 15 | { 16 | } 17 | '''[1:] 18 | y = clpy.backend.ultima.exec_ultima( 19 | ''' 20 | void f(CIndexer<2> ind){} 21 | ''', 22 | '#include ') 23 | self.maxDiff = None 24 | self.assertEqual(x, y) 25 | 26 | def test_cindexer_member_function(self): 27 | x = clpy.backend.ultima.exec_ultima('', '#include ') + ''' 28 | void f(CIndexer_2 ind) 29 | { 30 | ind_size; 31 | } 32 | '''[1:] 33 | y = clpy.backend.ultima.exec_ultima( 34 | ''' 35 | void f(CIndexer<2> ind){ 36 | ind.size(); 37 | } 38 | ''', 39 | '#include ') 40 | self.maxDiff = None 41 | self.assertEqual(x, y) 42 | 43 | 44 | if __name__ == "__main__": 45 | unittest.main() 46 | -------------------------------------------------------------------------------- /clpy/linalg/util.py: -------------------------------------------------------------------------------- 1 | from numpy import linalg 2 | 3 | import clpy 4 | from clpy import core 5 | 6 | 7 | def _assert_clpy_array(*arrays): 8 | for a in arrays: 9 | if not isinstance(a, clpy.core.ndarray): 10 | raise linalg.LinAlgError( 11 | 'clpy.linalg only supports clpy.core.ndarray') 12 | 13 | 14 | def _assert_rank2(*arrays): 15 | for a in arrays: 16 | if a.ndim != 2: 17 | raise linalg.LinAlgError( 18 | '{}-dimensional array given. Array must be ' 19 | 'two-dimensional'.format(a.ndim)) 20 | 21 | 22 | def _assert_nd_squareness(*arrays): 23 | for a in arrays: 24 | if max(a.shape[-2:]) != min(a.shape[-2:]): 25 | raise linalg.LinAlgError( 26 | 'Last 2 dimensions of the array must be square') 27 | 28 | 29 | _tril_kernel = core.ElementwiseKernel( 30 | 'int64 k', 'S x', 31 | 'x = (_ind.get()[1] - _ind.get()[0] <= k) ? x : 0', 32 | 'tril_kernel', 33 | reduce_dims=False 34 | ) 35 | 36 | 37 | def _tril(x, k=0): 38 | _tril_kernel(k, x) 39 | return x 40 | 41 | 42 | _triu_kernel = core.ElementwiseKernel( 43 | 'int64 k', 'S x', 44 | 'x = (_ind.get()[1] - _ind.get()[0] >= k) ? x : 0', 45 | 'triu_kernel', 46 | reduce_dims=False 47 | ) 48 | 49 | 50 | def _triu(x, k=0): 51 | _triu_kernel(k, x) 52 | return x 53 | -------------------------------------------------------------------------------- /docs/source/reference/cuda.rst: -------------------------------------------------------------------------------- 1 | Low-Level CUDA Support 2 | ====================== 3 | 4 | Device management 5 | ----------------- 6 | 7 | .. autosummary:: 8 | :toctree: generated/ 9 | :nosignatures: 10 | 11 | cupy.cuda.Device 12 | 13 | 14 | Memory management 15 | ----------------- 16 | 17 | .. autosummary:: 18 | :toctree: generated/ 19 | :nosignatures: 20 | 21 | cupy.cuda.Memory 22 | cupy.cuda.MemoryPointer 23 | cupy.cuda.alloc 24 | cupy.cuda.set_allocator 25 | cupy.cuda.MemoryPool 26 | 27 | 28 | Memory hook 29 | ----------- 30 | 31 | .. autosummary:: 32 | :toctree: generated/ 33 | :nosignatures: 34 | 35 | cupy.cuda.MemoryHook 36 | cupy.cuda.memory_hooks.DebugPrintHook 37 | cupy.cuda.memory_hooks.LineProfileHook 38 | 39 | Streams and events 40 | ------------------ 41 | 42 | .. autosummary:: 43 | :toctree: generated/ 44 | :nosignatures: 45 | 46 | cupy.cuda.Stream 47 | cupy.cuda.Event 48 | cupy.cuda.get_elapsed_time 49 | 50 | 51 | Profiler 52 | -------- 53 | 54 | .. autosummary:: 55 | :toctree: generated/ 56 | :nosignatures: 57 | 58 | cupy.cuda.profile 59 | cupy.cuda.profiler.initialize 60 | cupy.cuda.profiler.start 61 | cupy.cuda.profiler.stop 62 | cupy.cuda.nvtx.Mark 63 | cupy.cuda.nvtx.MarkC 64 | cupy.cuda.nvtx.RangePush 65 | cupy.cuda.nvtx.RangePushC 66 | cupy.cuda.nvtx.RangePop 67 | -------------------------------------------------------------------------------- /tests/clpy_tests/cuda_tests/test_curand.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy 4 | 5 | import cupy 6 | from cupy.cuda import curand 7 | 8 | 9 | class TestGenerateNormal(unittest.TestCase): 10 | 11 | def setUp(self): 12 | self.generator = curand.createGenerator( 13 | curand.CURAND_RNG_PSEUDO_DEFAULT) 14 | 15 | def test_invalid_argument_normal_float(self): 16 | out = cupy.empty((1,), dtype=numpy.float32) 17 | with self.assertRaises(ValueError): 18 | curand.generateNormal( 19 | self.generator, out.data.ptr, 1, 0.0, 1.0) 20 | 21 | def test_invalid_argument_normal_double(self): 22 | out = cupy.empty((1,), dtype=numpy.float64) 23 | with self.assertRaises(ValueError): 24 | curand.generateNormalDouble( 25 | self.generator, out.data.ptr, 1, 0.0, 1.0) 26 | 27 | def test_invalid_argument_log_normal_float(self): 28 | out = cupy.empty((1,), dtype=numpy.float32) 29 | with self.assertRaises(ValueError): 30 | curand.generateLogNormal( 31 | self.generator, out.data.ptr, 1, 0.0, 1.0) 32 | 33 | def test_invalid_argument_log_normal_double(self): 34 | out = cupy.empty((1,), dtype=numpy.float64) 35 | with self.assertRaises(ValueError): 36 | curand.generateLogNormalDouble( 37 | self.generator, out.data.ptr, 1, 0.0, 1.0) 38 | -------------------------------------------------------------------------------- /clpy/logic/truth.py: -------------------------------------------------------------------------------- 1 | import clpy 2 | 3 | 4 | def all(a, axis=None, out=None, keepdims=False): 5 | """Tests whether all array elements along a given axis evaluate to True. 6 | 7 | Args: 8 | a (clpy.ndarray): Input array. 9 | axis (int or tuple of ints): Along which axis to compute all. 10 | The flattened array is used by default. 11 | out (clpy.ndarray): Output array. 12 | keepdims (bool): If ``True``, the axis is remained as an axis of 13 | size one. 14 | 15 | Returns: 16 | clpy.ndarray: An array reduced of the input array along the axis. 17 | 18 | .. seealso:: :func:`numpy.all` 19 | 20 | """ 21 | assert isinstance(a, clpy.ndarray) 22 | return a.all(axis=axis, out=out, keepdims=keepdims) 23 | 24 | 25 | def any(a, axis=None, out=None, keepdims=False): 26 | """Tests whether any array elements along a given axis evaluate to True. 27 | 28 | Args: 29 | a (clpy.ndarray): Input array. 30 | axis (int or tuple of ints): Along which axis to compute all. 31 | The flattened array is used by default. 32 | out (clpy.ndarray): Output array. 33 | keepdims (bool): If ``True``, the axis is remained as an axis of 34 | size one. 35 | 36 | Returns: 37 | clpy.ndarray: An array reduced of the input array along the axis. 38 | 39 | .. seealso:: :func:`numpy.any` 40 | 41 | """ 42 | assert isinstance(a, clpy.ndarray) 43 | return a.any(axis=axis, out=out, keepdims=keepdims) 44 | -------------------------------------------------------------------------------- /clpy/sparse/__init__.py: -------------------------------------------------------------------------------- 1 | # from clpy.sparse.base import issparse # NOQA 2 | # from clpy.sparse.base import isspmatrix # NOQA 3 | from clpy.sparse.base import spmatrix # NOQA 4 | # from clpy.sparse.coo import coo_matrix # NOQA 5 | # from clpy.sparse.coo import isspmatrix_coo # NOQA 6 | # from clpy.sparse.csc import csc_matrix # NOQA 7 | # from clpy.sparse.csc import isspmatrix_csc # NOQA 8 | # from clpy.sparse.csr import csr_matrix # NOQA 9 | # from clpy.sparse.csr import isspmatrix_csr # NOQA 10 | # from clpy.sparse.dia import dia_matrix # NOQA 11 | # from clpy.sparse.dia import isspmatrix_dia # NOQA 12 | 13 | # from clpy.sparse.construct import eye # NOQA 14 | # from clpy.sparse.construct import identity # NOQA 15 | # from clpy.sparse.construct import spdiags # NOQA 16 | 17 | # TODO(unno): implement bsr_matrix 18 | # TODO(unno): implement dok_matrix 19 | # TODO(unno): implement lil_matrix 20 | 21 | # TODO(unno): implement kron 22 | # TODO(unno): implement kronsum 23 | # TODO(unno): implement diags 24 | # TODO(unno): implement block_diag 25 | # TODO(unno): implement tril 26 | # TODO(unno): implement triu 27 | # TODO(unno): implement bmat 28 | # TODO(unno): implement hstack 29 | # TODO(unno): implement vstack 30 | # TODO(unno): implement rand 31 | # TODO(unno): implement random 32 | 33 | # TODO(unno): implement save_npz 34 | # TODO(unno): implement load_npz 35 | 36 | # TODO(unno): implement find 37 | 38 | # TODO(unno): implement isspmatrix_bsr(x) 39 | # TODO(unno): implement isspmatrix_lil(x) 40 | # TODO(unno): implement isspmatrix_dok(x) 41 | -------------------------------------------------------------------------------- /tests/clpy_tests/random_tests/test_permutations.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import clpy 4 | from clpy import testing 5 | 6 | 7 | @testing.gpu 8 | class TestPermutations(unittest.TestCase): 9 | 10 | _multiprocess_can_split_ = True 11 | 12 | 13 | @testing.gpu 14 | class TestShuffle(unittest.TestCase): 15 | 16 | _multiprocess_can_split_ = True 17 | 18 | # Test ranks 19 | 20 | @testing.numpy_clpy_raises() 21 | def test_shuffle_zero_dim(self, xp): 22 | a = testing.shaped_random((), xp) 23 | xp.random.shuffle(a) 24 | 25 | # Test same values 26 | 27 | @testing.for_all_dtypes(no_float16=True, no_bool=True, no_complex=True) 28 | def test_shuffle_sort_1dim(self, dtype): 29 | a = clpy.arange(10, dtype=dtype) 30 | b = clpy.copy(a) 31 | clpy.random.shuffle(a) 32 | testing.assert_allclose(clpy.sort(a), b) 33 | 34 | @testing.for_all_dtypes(no_float16=True, no_bool=True, no_complex=True) 35 | def test_shuffle_sort_ndim(self, dtype): 36 | a = clpy.arange(15, dtype=dtype).reshape(5, 3) 37 | b = clpy.copy(a) 38 | clpy.random.shuffle(a) 39 | testing.assert_allclose(clpy.sort(a, axis=0), b) 40 | 41 | # Test seed 42 | 43 | @testing.for_all_dtypes() 44 | def test_shuffle_seed1(self, dtype): 45 | a = testing.shaped_random((10,), clpy, dtype) 46 | b = clpy.copy(a) 47 | clpy.random.seed(0) 48 | clpy.random.shuffle(a) 49 | clpy.random.seed(0) 50 | clpy.random.shuffle(b) 51 | testing.assert_allclose(a, b) 52 | -------------------------------------------------------------------------------- /clpy/manipulation/shape.py: -------------------------------------------------------------------------------- 1 | def reshape(a, newshape): 2 | """Returns an array with new shape and same elements. 3 | 4 | It tries to return a view if possible, otherwise returns a copy. 5 | 6 | This function currently does not support ``order`` option. 7 | 8 | Args: 9 | a (clpy.ndarray): Array to be reshaped. 10 | newshape (int or tuple of ints): The new shape of the array to return. 11 | If it is an integer, then it is treated as a tuple of length one. 12 | It should be compatible with ``a.size``. One of the elements can be 13 | -1, which is automatically replaced with the appropriate value to 14 | make the shape compatible with ``a.size``. 15 | 16 | Returns: 17 | clpy.ndarray: A reshaped view of ``a`` if possible, otherwise a copy. 18 | 19 | .. seealso:: :func:`numpy.reshape` 20 | 21 | """ 22 | # TODO(beam2d): Support ordering option 23 | # TODO(okuta): check type 24 | return a.reshape(newshape) 25 | 26 | 27 | def ravel(a): 28 | """Returns a flattened array. 29 | 30 | It tries to return a view if possible, otherwise returns a copy. 31 | 32 | This function currently does not support ``order`` option. 33 | 34 | Args: 35 | a (clpy.ndarray): Array to be flattened. 36 | 37 | Returns: 38 | clpy.ndarray: A flattened view of ``a`` if possible, otherwise a copy. 39 | 40 | .. seealso:: :func:`numpy.ravel` 41 | 42 | """ 43 | # TODO(beam2d): Support ordering option 44 | # TODO(okuta): check type 45 | return a.ravel() 46 | -------------------------------------------------------------------------------- /clpy/manipulation/transpose.py: -------------------------------------------------------------------------------- 1 | from clpy import core 2 | 3 | 4 | def rollaxis(a, axis, start=0): 5 | """Moves the specified axis backwards to the given place. 6 | 7 | Args: 8 | a (clpy.ndarray): Array to move the axis. 9 | axis (int): The axis to move. 10 | start (int): The place to which the axis is moved. 11 | 12 | Returns: 13 | clpy.ndarray: A view of ``a`` that the axis is moved to ``start``. 14 | 15 | .. seealso:: :func:`numpy.rollaxis` 16 | 17 | """ 18 | return core.rollaxis(a, axis, start) 19 | 20 | 21 | def swapaxes(a, axis1, axis2): 22 | """Swaps the two axes. 23 | 24 | Args: 25 | a (clpy.ndarray): Array to swap the axes. 26 | axis1 (int): The first axis to swap. 27 | axis2 (int): The second axis to swap. 28 | 29 | Returns: 30 | clpy.ndarray: A view of ``a`` that the two axes are swapped. 31 | 32 | .. seealso:: :func:`numpy.swapaxes` 33 | 34 | """ 35 | # TODO(okuta): check type 36 | return a.swapaxes(axis1, axis2) 37 | 38 | 39 | def transpose(a, axes=None): 40 | """Permutes the dimensions of an array. 41 | 42 | Args: 43 | a (clpy.ndarray): Array to permute the dimensions. 44 | axes (tuple of ints): Permutation of the dimensions. This function 45 | reverses the shape by default. 46 | 47 | Returns: 48 | clpy.ndarray: A view of ``a`` that the dimensions are permuted. 49 | 50 | .. seealso:: :func:`numpy.transpose` 51 | 52 | """ 53 | # TODO(okuta): check type 54 | return a.transpose(axes) 55 | -------------------------------------------------------------------------------- /docs/source/reference/random.rst: -------------------------------------------------------------------------------- 1 | .. module:: cupy.random 2 | 3 | Random Sampling (``cupy.random``) 4 | ================================= 5 | 6 | CuPy's random number generation routines are based on cuRAND. 7 | They cover a small fraction of :mod:`numpy.random`. 8 | 9 | The big difference of :mod:`cupy.random` from :mod:`numpy.random` is that :mod:`cupy.random` supports ``dtype`` option for most functions. 10 | This option enables us to generate float32 values directly without any space overhead. 11 | 12 | 13 | Sample random data 14 | ------------------ 15 | 16 | .. autosummary:: 17 | :toctree: generated/ 18 | :nosignatures: 19 | 20 | cupy.random.choice 21 | cupy.random.rand 22 | cupy.random.randn 23 | cupy.random.randint 24 | cupy.random.random_integers 25 | cupy.random.random_sample 26 | cupy.random.random 27 | cupy.random.ranf 28 | cupy.random.sample 29 | cupy.random.bytes 30 | 31 | 32 | Distributions 33 | ------------- 34 | 35 | .. autosummary:: 36 | :toctree: generated/ 37 | :nosignatures: 38 | 39 | cupy.random.gumbel 40 | cupy.random.lognormal 41 | cupy.random.normal 42 | cupy.random.standard_normal 43 | cupy.random.uniform 44 | 45 | 46 | Random number generator 47 | ----------------------- 48 | 49 | .. autosummary:: 50 | :toctree: generated/ 51 | :nosignatures: 52 | 53 | cupy.random.seed 54 | cupy.random.get_random_state 55 | cupy.random.RandomState 56 | 57 | 58 | Permutations 59 | ------------ 60 | 61 | .. autosummary:: 62 | :toctree: generated/ 63 | :nosignatures: 64 | 65 | cupy.random.shuffle 66 | -------------------------------------------------------------------------------- /clpy/random/__init__.py: -------------------------------------------------------------------------------- 1 | import numpy 2 | 3 | 4 | def bytes(length): 5 | """Returns random bytes. 6 | 7 | .. seealso:: :func:`numpy.random.bytes` 8 | """ 9 | return numpy.bytes(length) 10 | 11 | 12 | from clpy.random import distributions # NOQA 13 | from clpy.random import generator # NOQA 14 | from clpy.random import permutations # NOQA 15 | from clpy.random import sample as sample_ # NOQA 16 | 17 | 18 | # import class and function 19 | from clpy.random.distributions import gumbel # NOQA 20 | from clpy.random.distributions import lognormal # NOQA 21 | from clpy.random.distributions import normal # NOQA 22 | from clpy.random.distributions import standard_normal # NOQA 23 | from clpy.random.distributions import uniform # NOQA 24 | from clpy.random.generator import get_random_state # NOQA 25 | from clpy.random.generator import RandomState # NOQA 26 | from clpy.random.generator import reset_states # NOQA 27 | from clpy.random.generator import seed # NOQA 28 | from clpy.random.permutations import shuffle # NOQA 29 | from clpy.random.sample import choice # NOQA 30 | from clpy.random.sample import multinomial # NOQA 31 | from clpy.random.sample import rand # NOQA 32 | from clpy.random.sample import randint # NOQA 33 | from clpy.random.sample import randn # NOQA 34 | from clpy.random.sample import random_integers # NOQA 35 | from clpy.random.sample import random_sample # NOQA 36 | from clpy.random.sample import random_sample as random # NOQA 37 | from clpy.random.sample import random_sample as ranf # NOQA 38 | from clpy.random.sample import random_sample as sample # NOQA 39 | -------------------------------------------------------------------------------- /clpy/math/rounding.py: -------------------------------------------------------------------------------- 1 | from clpy import core 2 | from clpy.math import ufunc 3 | 4 | # TODO(okuta): Implement around 5 | 6 | 7 | # TODO(beam2d): Implement it 8 | # round_ = around 9 | 10 | 11 | rint = ufunc.create_math_ufunc( 12 | 'rint', 1, 'clpy_rint', 13 | '''Rounds each element of an array to the nearest integer. 14 | 15 | .. seealso:: :data:`numpy.rint` 16 | 17 | ''') 18 | 19 | 20 | floor = ufunc.create_math_ufunc( 21 | 'floor', 1, 'clpy_floor', 22 | '''Rounds each element of an array to its floor integer. 23 | 24 | .. seealso:: :data:`numpy.floor` 25 | 26 | ''') 27 | 28 | 29 | ceil = ufunc.create_math_ufunc( 30 | 'ceil', 1, 'clpy_ceil', 31 | '''Rounds each element of an array to its ceiling integer. 32 | 33 | .. seealso:: :data:`numpy.ceil` 34 | 35 | ''') 36 | 37 | 38 | trunc = ufunc.create_math_ufunc( 39 | 'trunc', 1, 'clpy_trunc', 40 | '''Rounds each element of an array towards zero. 41 | 42 | .. seealso:: :data:`numpy.trunc` 43 | 44 | ''') 45 | 46 | 47 | fix = core.create_ufunc( 48 | 'clpy_fix', 49 | (('b->e', 'out0 = convert_float_to_half((in0 >= 0)' 50 | ' ? floor((float)in0)' 51 | ' : ceil((float)in0))'), 52 | ('B->e', 'out0 = convert_float_to_half((in0 >= 0)' 53 | ' ? floor((float)in0)' 54 | ' : ceil((float)in0))'), 55 | 'f->f', 'd->d'), 56 | 'out0 = (in0 >= 0.0) ? floor(in0): ceil(in0)', 57 | doc='''If given value x is positive, it return floor(x). 58 | Else, it return ceil(x). 59 | .. seealso:: :data:`numpy.fix` 60 | 61 | ''') 62 | -------------------------------------------------------------------------------- /tests/clpy_tests/opencl_tests/test_sample_rand.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy 4 | 5 | from clpy import random 6 | 7 | 8 | class TestRandomSample(unittest.TestCase): 9 | 10 | def test_rand_valid_range(self): 11 | n = 10000 12 | ar = random.rand(n, dtype=numpy.float32) 13 | result = ar.get() 14 | 15 | ones = numpy.ones(n) 16 | zeros = numpy.zeros(n) 17 | # TODO(nsakabe-fixstars): 18 | # Fix the order of greater_equal's arguments 19 | # and operands of and (not `all`, but `all()) 20 | self.assertTrue(numpy.greater_equal( 21 | zeros, result).all and numpy.less(result, ones).all()) 22 | 23 | def test_rand_call_twice(self): 24 | # diffrent length of array than other test case is required 25 | n = 10 26 | random.rand(n, dtype=numpy.float32) 27 | ar = random.rand(n, dtype=numpy.float32) 28 | 29 | result = ar.get() 30 | 31 | ones = numpy.ones(n) 32 | zeros = numpy.zeros(n) 33 | 34 | # TODO(nsakabe-fixstars): 35 | # Fix the order of greater_equal's arguments 36 | # and operands of and (not `all`, but `all()) 37 | self.assertTrue(numpy.greater_equal( 38 | zeros, result).all and numpy.less(result, ones).all()) 39 | 40 | def test_rand_generate_different_result(self): 41 | n = 100 42 | a = random.rand(n, dtype=numpy.float32) 43 | b = random.rand(n, dtype=numpy.float32) 44 | 45 | self.assertFalse(numpy.allclose(a.get(), b.get())) 46 | 47 | 48 | if __name__ == "__main__": 49 | unittest.main() 50 | -------------------------------------------------------------------------------- /tests/clpy_tests/binary_tests/test_packing.py: -------------------------------------------------------------------------------- 1 | import numpy 2 | import unittest 3 | 4 | from clpy import testing 5 | 6 | 7 | @testing.gpu 8 | class TestPacking(unittest.TestCase): 9 | 10 | _multiprocess_can_split_ = True 11 | 12 | @testing.with_requires('numpy>=1.10') 13 | @testing.for_int_dtypes() 14 | @testing.numpy_clpy_array_equal() 15 | def check_packbits(self, data, xp, dtype): 16 | # Note numpy <= 1.9 raises an Exception when an input array is bool. 17 | # See https://github.com/numpy/numpy/issues/5377 18 | a = xp.array(data, dtype=dtype) 19 | return xp.packbits(a) 20 | 21 | @testing.numpy_clpy_array_equal() 22 | def check_unpackbits(self, data, xp): 23 | a = xp.array(data, dtype=xp.uint8) 24 | return xp.unpackbits(a) 25 | 26 | def test_packbits(self): 27 | self.check_packbits([0]) 28 | self.check_packbits([1]) 29 | self.check_packbits([0, 1]) 30 | self.check_packbits([1, 0, 1, 1, 0, 1, 1, 1]) 31 | self.check_packbits([1, 0, 1, 1, 0, 1, 1, 1, 1]) 32 | self.check_packbits(numpy.arange(24).reshape((2, 3, 4)) % 2) 33 | 34 | @testing.with_requires('numpy>=1.12') 35 | def test_packbits_empty(self): 36 | # Note packbits of numpy <= 1.11 has a bug against empty arrays. 37 | # See https://github.com/numpy/numpy/issues/8324 38 | self.check_packbits([]) 39 | 40 | def test_unpackbits(self): 41 | self.check_unpackbits([]) 42 | self.check_unpackbits([0]) 43 | self.check_unpackbits([1]) 44 | self.check_unpackbits([255]) 45 | self.check_unpackbits([100, 200, 123, 213]) 46 | -------------------------------------------------------------------------------- /clpy/io/formatting.py: -------------------------------------------------------------------------------- 1 | import numpy 2 | 3 | import clpy 4 | 5 | 6 | def array_repr(arr, max_line_width=None, precision=None, suppress_small=None): 7 | """Returns the string representation of an array. 8 | 9 | Args: 10 | arr (array_like): Input array. It should be able to feed to 11 | :func:`clpy.asnumpy`. 12 | max_line_width (int): The maximum number of line lengths. 13 | precision (int): Floating point precision. It uses the current printing 14 | precision of NumPy. 15 | suppress_small (bool): If ``True``, very small numbers are printed as 16 | zeros 17 | 18 | Returns: 19 | str: The string representation of ``arr``. 20 | 21 | .. seealso:: :func:`numpy.array_repr` 22 | 23 | """ 24 | return numpy.array_repr(clpy.asnumpy(arr), max_line_width, precision, 25 | suppress_small) 26 | 27 | 28 | def array_str(arr, max_line_width=None, precision=None, suppress_small=None): 29 | """Returns the string representation of the content of an array. 30 | 31 | Args: 32 | arr (array_like): Input array. It should be able to feed to 33 | :func:`clpy.asnumpy`. 34 | max_line_width (int): The maximum number of line lengths. 35 | precision (int): Floating point precision. It uses the current printing 36 | precision of NumPy. 37 | suppress_small (bool): If ``True``, very small number are printed as 38 | zeros. 39 | 40 | .. seealso:: :func:`numpy.array_str` 41 | 42 | """ 43 | return numpy.array_str(clpy.asnumpy(arr), max_line_width, precision, 44 | suppress_small) 45 | -------------------------------------------------------------------------------- /tests/clpy_tests/opencl_tests/test_atomicAdd.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy as np 4 | 5 | import clpy 6 | 7 | 8 | class TestAtomicAdd(unittest.TestCase): 9 | """test atomicAdd function""" 10 | 11 | def test_float32(self): 12 | size = 128 13 | dtype = np.float32 14 | npX = np.arange(size, dtype=dtype) 15 | npY = np.flip(np.arange(size, dtype=dtype) * 2, -1) 16 | 17 | x = clpy.array(npX, dtype=dtype) 18 | y = clpy.array(npY, dtype=dtype) 19 | 20 | kernel = clpy.core.ElementwiseKernel( 21 | 'raw T x, T y', 22 | 'T z', 23 | ''' 24 | atomicAdd(&x[i], y); 25 | z = x[i]; 26 | ''', 27 | 'test_atomicAdd' 28 | ) 29 | z = kernel(x, y) 30 | 31 | actual = z.get() 32 | expected = npX + npY 33 | 34 | self.assertTrue(np.allclose(actual, expected)) 35 | 36 | def test_float32_conflict(self): 37 | size = 128 38 | dtype = np.float32 39 | npX = np.arange(size, dtype=dtype) 40 | npZ = np.arange(size, dtype=dtype) 41 | 42 | x = clpy.array(npX, dtype=dtype) 43 | z = clpy.array(npZ, dtype=dtype) 44 | 45 | kernel = clpy.core.ElementwiseKernel( 46 | 'T x', 47 | 'raw T z', 48 | ''' 49 | atomicAdd(&z[0], x); 50 | ''', 51 | 'test_atomicAdd' 52 | ) 53 | kernel(x, z) 54 | 55 | actual = z.get() 56 | npZ[0] += np.sum(npX) 57 | expected = npZ 58 | 59 | self.assertTrue(np.allclose(actual, expected)) 60 | 61 | 62 | if __name__ == "__main__": 63 | unittest.main() 64 | -------------------------------------------------------------------------------- /tests/clpy_tests/core_tests/test_scan.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | import unittest 4 | 5 | import clpy 6 | from clpy import backend 7 | from clpy import testing 8 | 9 | 10 | @testing.gpu 11 | class TestScan(unittest.TestCase): 12 | 13 | @testing.for_all_dtypes() 14 | def test_scan(self, dtype): 15 | element_num = 10000 16 | 17 | if dtype in {clpy.int8, clpy.uint8, clpy.float16}: 18 | element_num = 100 19 | 20 | a = clpy.ones((element_num,), dtype=dtype) 21 | prefix_sum = clpy.core.core.scan(a) 22 | expect = clpy.arange(start=1, stop=element_num + 1).astype(dtype) 23 | 24 | testing.assert_array_equal(prefix_sum, expect) 25 | 26 | def test_check_1d_array(self): 27 | with self.assertRaises(TypeError): 28 | a = clpy.zeros((2, 2)) 29 | clpy.core.core.scan(a) 30 | 31 | @testing.multi_gpu(2) 32 | def test_multi_gpu(self): 33 | with backend.Device(0): 34 | a = clpy.zeros((10,)) 35 | clpy.core.core.scan(a) 36 | with backend.Device(1): 37 | a = clpy.zeros((10,)) 38 | clpy.core.core.scan(a) 39 | 40 | @testing.for_all_dtypes() 41 | def test_scan_out(self, dtype): 42 | element_num = 10000 43 | 44 | if dtype in {clpy.int8, clpy.uint8, clpy.float16}: 45 | element_num = 100 46 | 47 | a = clpy.ones((element_num,), dtype=dtype) 48 | b = clpy.zeros_like(a) 49 | clpy.core.core.scan(a, b) 50 | expect = clpy.arange(start=1, stop=element_num + 1).astype(dtype) 51 | 52 | testing.assert_array_equal(b, expect) 53 | 54 | clpy.core.core.scan(a, a) 55 | testing.assert_array_equal(a, expect) 56 | -------------------------------------------------------------------------------- /tests/clpy_tests/core_tests/test_carray.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import clpy 4 | from clpy import testing 5 | 6 | 7 | class TestCArray(unittest.TestCase): 8 | 9 | def test_size(self): 10 | x = clpy.arange(3).astype('i') 11 | y = clpy.ElementwiseKernel( 12 | 'raw int32 x', 'int32 y', 'y = x.size()', 'test_carray_size', 13 | )(x, size=1) 14 | self.assertEqual(int(y[0]), 3) 15 | 16 | def test_shape(self): 17 | x = clpy.arange(6).reshape((2, 3)).astype('i') 18 | y = clpy.ElementwiseKernel( 19 | 'raw int32 x', 'int32 y', 'y = x.shape()[i]', 'test_carray_shape', 20 | )(x, size=2) 21 | testing.assert_array_equal(y, (2, 3)) 22 | 23 | def test_strides(self): 24 | x = clpy.arange(6).reshape((2, 3)).astype('i') 25 | y = clpy.ElementwiseKernel( 26 | 'raw int32 x', 'int32 y', 'y = x.strides()[i]', 27 | 'test_carray_strides', 28 | )(x, size=2) 29 | testing.assert_array_equal(y, (12, 4)) 30 | 31 | def test_getitem_int(self): 32 | x = clpy.arange(24).reshape((2, 3, 4)).astype('i') 33 | y = clpy.empty_like(x) 34 | y = clpy.ElementwiseKernel( 35 | 'raw T x', 'int32 y', 'y = x[i]', 'test_carray_getitem_int', 36 | )(x, y) 37 | testing.assert_array_equal(y, x) 38 | 39 | def test_getitem_idx(self): 40 | x = clpy.arange(24).reshape((2, 3, 4)).astype('i') 41 | y = clpy.empty_like(x) 42 | y = clpy.ElementwiseKernel( 43 | 'raw T x', 'int32 y', 44 | 'ptrdiff_t idx[] = {i / 12, i / 4 % 3, i % 4}; y = x[idx]', 45 | 'test_carray_getitem_idx', 46 | )(x, y) 47 | testing.assert_array_equal(y, x) 48 | -------------------------------------------------------------------------------- /tests/clpy_tests/sparse_tests/test_construct.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy 4 | 5 | from clpy import testing 6 | 7 | 8 | @testing.parameterize(*testing.product({ 9 | 'dtype': [numpy.float32, numpy.float64], 10 | 'format': ['csr', 'csc', 'coo'], 11 | 'm': [3], 12 | 'n': [None, 3, 2], 13 | 'k': [0, 1], 14 | })) 15 | @testing.with_requires('scipy') 16 | class TestEye(unittest.TestCase): 17 | 18 | @testing.numpy_clpy_allclose(sp_name='sp') 19 | def test_eye(self, xp, sp): 20 | x = sp.eye( 21 | self.m, n=self.n, k=self.k, dtype=self.dtype, format=self.format) 22 | self.assertIsInstance(x, sp.spmatrix) 23 | self.assertEqual(x.format, self.format) 24 | return x.toarray() 25 | 26 | 27 | @testing.parameterize(*testing.product({ 28 | 'dtype': [numpy.float32, numpy.float64], 29 | 'format': ['csr', 'csc', 'coo'], 30 | })) 31 | @testing.with_requires('scipy') 32 | class TestIdentity(unittest.TestCase): 33 | 34 | @testing.numpy_clpy_allclose(sp_name='sp') 35 | def test_eye(self, xp, sp): 36 | x = sp.identity(3, dtype=self.dtype, format=self.format) 37 | self.assertIsInstance(x, sp.spmatrix) 38 | self.assertEqual(x.format, self.format) 39 | return x.toarray() 40 | 41 | 42 | @testing.parameterize(*testing.product({ 43 | 'dtype': [numpy.float32, numpy.float64], 44 | })) 45 | @testing.with_requires('scipy') 46 | class TestSpdiags(unittest.TestCase): 47 | 48 | @testing.numpy_clpy_allclose(sp_name='sp') 49 | def test_spdiags(self, xp, sp): 50 | data = xp.arange(12, dtype=self.dtype).reshape(3, 4) 51 | diags = xp.array([0, -1, 2], dtype='i') 52 | x = sp.spdiags(data, diags, 3, 4) 53 | return x.toarray() 54 | -------------------------------------------------------------------------------- /tests/clpy_tests/math_tests/test_trigonometric.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from clpy import testing 4 | 5 | 6 | @testing.gpu 7 | class TestTrigonometric(unittest.TestCase): 8 | 9 | _multiprocess_can_split_ = True 10 | 11 | @testing.for_all_dtypes(no_complex=True) 12 | @testing.numpy_clpy_allclose(atol=1e-5) 13 | def check_unary(self, name, xp, dtype): 14 | a = testing.shaped_arange((2, 3), xp, dtype) 15 | return getattr(xp, name)(a) 16 | 17 | @testing.for_all_dtypes(no_complex=True) 18 | @testing.numpy_clpy_allclose(atol=1e-5) 19 | def check_binary(self, name, xp, dtype): 20 | a = testing.shaped_arange((2, 3), xp, dtype) 21 | b = testing.shaped_reverse_arange((2, 3), xp, dtype) 22 | return getattr(xp, name)(a, b) 23 | 24 | @testing.for_dtypes(['f', 'd']) 25 | @testing.numpy_clpy_allclose(atol=1e-5) 26 | def check_unary_unit(self, name, xp, dtype): 27 | a = xp.array([0.2, 0.4, 0.6, 0.8], dtype=dtype) 28 | return getattr(xp, name)(a) 29 | 30 | def test_sin(self): 31 | self.check_unary('sin') 32 | 33 | def test_cos(self): 34 | self.check_unary('cos') 35 | 36 | def test_tan(self): 37 | self.check_unary('tan') 38 | 39 | def test_arcsin(self): 40 | self.check_unary_unit('arcsin') 41 | 42 | def test_arccos(self): 43 | self.check_unary_unit('arccos') 44 | 45 | def test_arctan(self): 46 | self.check_unary('arctan') 47 | 48 | def test_arctan2(self): 49 | self.check_binary('arctan2') 50 | 51 | def test_hypot(self): 52 | self.check_binary('hypot') 53 | 54 | def test_deg2rad(self): 55 | self.check_unary('deg2rad') 56 | 57 | def test_rad2deg(self): 58 | self.check_unary('rad2deg') 59 | -------------------------------------------------------------------------------- /tests/clpy_tests/cuda_tests/memory_hooks_tests/test_line_profile.py: -------------------------------------------------------------------------------- 1 | import six 2 | import unittest 3 | 4 | from cupy.cuda import memory 5 | from cupy.cuda import memory_hooks 6 | from cupy import testing 7 | 8 | 9 | @testing.gpu 10 | class TestLineProfileHook(unittest.TestCase): 11 | 12 | def setUp(self): 13 | self.pool = memory.MemoryPool() 14 | 15 | def test_print_report(self): 16 | hook = memory_hooks.LineProfileHook() 17 | p = self.pool.malloc(1000) 18 | del p 19 | with hook: 20 | p1 = self.pool.malloc(1000) 21 | p2 = self.pool.malloc(2000) 22 | del p1 23 | del p2 24 | io = six.StringIO() 25 | hook.print_report(file=io) 26 | actual = io.getvalue() 27 | expect = r'\A_root \(3\.00KB, 2\.00KB\)' 28 | six.assertRegex(self, actual, expect) 29 | expect = r'.*\.py:[0-9]+:test_print_report \(1\.00KB, 0\.00B\)' 30 | six.assertRegex(self, actual, expect) 31 | expect = r'.*\.py:[0-9]+:test_print_report \(2\.00KB, 2\.00KB\)' 32 | six.assertRegex(self, actual, expect) 33 | 34 | def test_print_report_max_depth(self): 35 | hook = memory_hooks.LineProfileHook(max_depth=1) 36 | with hook: 37 | p = self.pool.malloc(1000) 38 | del p 39 | io = six.StringIO() 40 | hook.print_report(file=io) 41 | actual = io.getvalue() 42 | self.assertEqual(2, len(actual.split('\n'))) 43 | 44 | hook = memory_hooks.LineProfileHook(max_depth=2) 45 | with hook: 46 | p = self.pool.malloc(1000) 47 | del p 48 | io = six.StringIO() 49 | hook.print_report(file=io) 50 | actual = io.getvalue() 51 | self.assertEqual(3, len(actual.split('\n'))) 52 | -------------------------------------------------------------------------------- /clpy/indexing/insert.py: -------------------------------------------------------------------------------- 1 | import numpy 2 | 3 | # TODO(okuta): Implement place 4 | 5 | 6 | # TODO(okuta): Implement put 7 | 8 | 9 | # TODO(okuta): Implement putmask 10 | 11 | 12 | def fill_diagonal(a, val, wrap=False): 13 | """Fills the main diagonal of the given array of any dimensionality. 14 | 15 | For an array `a` with ``a.ndim > 2``, the diagonal is the list of 16 | locations with indices ``a[i, i, ..., i]`` all identical. This function 17 | modifies the input array in-place, it does not return a value. 18 | 19 | Args: 20 | a (clpy.ndarray): The array, at least 2-D. 21 | val (scalar): The value to be written on the diagonal. 22 | Its type must be compatible with that of the array a. 23 | wrap (bool): If specified, the diagonal is "wrapped" after N columns. 24 | This affects only tall matrices. 25 | 26 | Examples 27 | -------- 28 | >>> a = clpy.zeros((3, 3), int) 29 | >>> clpy.fill_diagonal(a, 5) 30 | >>> a 31 | array([[5, 0, 0], 32 | [0, 5, 0], 33 | [0, 0, 5]]) 34 | 35 | .. seealso:: :func:`numpy.fill_diagonal` 36 | """ 37 | # The followings are imported from the original numpy 38 | if a.ndim < 2: 39 | raise ValueError('array must be at least 2-d') 40 | end = None 41 | if a.ndim == 2: 42 | step = a.shape[1] + 1 43 | if not wrap: 44 | end = a.shape[1] * a.shape[1] 45 | else: 46 | if not numpy.alltrue(numpy.diff(a.shape) == 0): 47 | raise ValueError('All dimensions of input must be of equal length') 48 | step = 1 + numpy.cumprod(a.shape[:-1]).sum() 49 | 50 | # Since the current clpy does not support a.flat, 51 | # we use a.ravel() instead of a.flat 52 | a.ravel()[:end:step] = val 53 | -------------------------------------------------------------------------------- /clpy/testing/attr.py: -------------------------------------------------------------------------------- 1 | import os 2 | import unittest 3 | 4 | 5 | try: 6 | import pytest 7 | _error = None 8 | except ImportError as e: 9 | _error = e 10 | 11 | 12 | def is_available(): 13 | return _error is None 14 | 15 | 16 | def check_available(): 17 | if _error is not None: 18 | raise RuntimeError('''\ 19 | {} is not available. 20 | 21 | Reason: {}: {}'''.format(__name__, type(_error).__name__, _error)) 22 | 23 | 24 | def get_error(): 25 | return _error 26 | 27 | 28 | if _error is None: 29 | _gpu_limit = int(os.getenv('CLPY_TEST_GPU_LIMIT', '-1')) 30 | 31 | cudnn = pytest.mark.cudnn 32 | slow = pytest.mark.slow 33 | 34 | else: 35 | def _dummy_callable(*args, **kwargs): 36 | check_available() 37 | assert False # Not reachable 38 | 39 | cudnn = _dummy_callable 40 | slow = _dummy_callable 41 | 42 | 43 | def multi_gpu(gpu_num): 44 | """Decorator to indicate number of GPUs required to run the test. 45 | 46 | Tests can be annotated with this decorator (e.g., ``@multi_gpu(2)``) to 47 | declare number of GPUs required to run. When running tests, if 48 | ``CLPY_TEST_GPU_LIMIT`` environment variable is set to value greater 49 | than or equals to 0, test cases that require GPUs more than the limit will 50 | be skipped. 51 | """ 52 | 53 | check_available() 54 | return unittest.skipIf( 55 | 0 <= _gpu_limit and _gpu_limit < gpu_num, 56 | reason='{} GPUs required'.format(gpu_num)) 57 | 58 | 59 | def gpu(f): 60 | """Decorator to indicate that GPU is required to run the test. 61 | 62 | Tests can be annotated with this decorator (e.g., ``@gpu``) to 63 | declare that one GPU is required to run. 64 | """ 65 | 66 | check_available() 67 | return multi_gpu(1)(pytest.mark.gpu(f)) 68 | -------------------------------------------------------------------------------- /tests/clpy_tests/opencl_tests/ultima_tests/test_cast.py: -------------------------------------------------------------------------------- 1 | # flake8: noqa 2 | # TODO(vorj): When we will meet flake8 3.7.0+, 3 | # we should ignore only W291 for whole file 4 | # using --per-file-ignores . 5 | 6 | import clpy 7 | import unittest 8 | 9 | 10 | class TestUltimaCastConversion(unittest.TestCase): 11 | 12 | def test_function_style_cast(self): 13 | x = ''' 14 | void f() 15 | { 16 | (int)(3.F); 17 | } 18 | ''' 19 | y = clpy.backend.ultima.exec_ultima( 20 | ''' 21 | void f(){ 22 | int(3.F); 23 | } 24 | ''') 25 | self.assertEqual(x[1:], y) 26 | 27 | def test_static_cast(self): 28 | x = ''' 29 | void f() 30 | { 31 | (int)(3.F); 32 | } 33 | ''' 34 | y = clpy.backend.ultima.exec_ultima( 35 | ''' 36 | void f(){ 37 | static_cast(3.F); 38 | } 39 | ''') 40 | self.assertEqual(x[1:], y) 41 | 42 | def test_const_cast(self): 43 | x = ''' 44 | void f() 45 | { 46 | const int a = 3; 47 | (int *)(&a); 48 | } 49 | ''' 50 | y = clpy.backend.ultima.exec_ultima( 51 | ''' 52 | void f(){ 53 | const int a = 3; 54 | const_cast(&a); 55 | } 56 | ''') 57 | self.assertEqual(x[1:], y) 58 | 59 | def test_reinterpret_cast(self): 60 | x = ''' 61 | void f() 62 | { 63 | int a = 3; 64 | (float *)(&a); 65 | } 66 | ''' 67 | y = clpy.backend.ultima.exec_ultima( 68 | ''' 69 | void f(){ 70 | int a = 3; 71 | reinterpret_cast(&a); 72 | } 73 | ''') 74 | self.assertEqual(x[1:], y) 75 | 76 | 77 | if __name__ == "__main__": 78 | unittest.main() 79 | -------------------------------------------------------------------------------- /tests/clpy_tests/manipulation_tests/test_transpose.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import clpy 4 | from clpy import testing 5 | 6 | 7 | @testing.gpu 8 | class TestTranspose(unittest.TestCase): 9 | 10 | _multiprocess_can_split_ = True 11 | 12 | @testing.numpy_clpy_array_equal() 13 | def test_rollaxis(self, xp): 14 | a = testing.shaped_arange((2, 3, 4), xp) 15 | return xp.rollaxis(a, 2) 16 | 17 | def test_rollaxis_failure(self): 18 | a = testing.shaped_arange((2, 3, 4)) 19 | with self.assertRaises(ValueError): 20 | clpy.rollaxis(a, 3) 21 | 22 | @testing.numpy_clpy_array_equal() 23 | def test_swapaxes(self, xp): 24 | a = testing.shaped_arange((2, 3, 4), xp) 25 | return xp.swapaxes(a, 2, 0) 26 | 27 | def test_swapaxes_failure(self): 28 | a = testing.shaped_arange((2, 3, 4)) 29 | with self.assertRaises(ValueError): 30 | clpy.swapaxes(a, 3, 0) 31 | 32 | @testing.numpy_clpy_array_equal() 33 | def test_transpose(self, xp): 34 | a = testing.shaped_arange((2, 3, 4), xp) 35 | return a.transpose(-1, 0, 1) 36 | 37 | @testing.numpy_clpy_array_equal() 38 | def test_transpose_empty(self, xp): 39 | a = testing.shaped_arange((2, 3, 4), xp) 40 | return a.transpose() 41 | 42 | @testing.numpy_clpy_array_equal() 43 | def test_transpose_none(self, xp): 44 | a = testing.shaped_arange((2, 3, 4), xp) 45 | return a.transpose(None) 46 | 47 | @testing.numpy_clpy_array_equal() 48 | def test_external_transpose(self, xp): 49 | a = testing.shaped_arange((2, 3, 4), xp) 50 | return xp.transpose(a, (-1, 0, 1)) 51 | 52 | @testing.numpy_clpy_array_equal() 53 | def test_external_transpose_all(self, xp): 54 | a = testing.shaped_arange((2, 3, 4), xp) 55 | return xp.transpose(a) 56 | -------------------------------------------------------------------------------- /tests/clpy_tests/opencl_tests/test_linalg.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import unittest 4 | 5 | import clpy 6 | import numpy 7 | 8 | 9 | class TestProduct(unittest.TestCase): 10 | """test class of linalg""" 11 | 12 | def test_vdot(self): 13 | npA = numpy.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype='float32') 14 | npB = numpy.array([[1, 2, 3], [7, 8, 9], [4, 5, 6]], dtype='float32') 15 | expectedC = numpy.vdot(npA, npB) 16 | 17 | clpA = clpy.array(npA) 18 | clpB = clpy.array(npB) 19 | actualC = clpy.vdot(clpA, clpB) 20 | 21 | self.assertTrue(numpy.allclose(expectedC, actualC.get())) 22 | 23 | def test_dot(self): 24 | npA = numpy.array([1, 2, 3], dtype='float32') 25 | npB = numpy.array([1, 2, 3], dtype='float32') 26 | expectedC = numpy.dot(npA, npB) 27 | 28 | clpA = clpy.array(npA) 29 | clpB = clpy.array(npB) 30 | actualC = clpy.dot(clpA, clpB) 31 | 32 | self.assertTrue(numpy.allclose(expectedC, actualC.get())) 33 | 34 | def test_tensordot(self): 35 | npA = numpy.array([[[1, 2, 3], [4, 5, 6], [7, 8, 9]], 36 | [[10, 11, 12], [13, 14, 15], [16, 17, 18]], 37 | [[19, 20, 21], [22, 23, 24], [25, 26, 27]]], 38 | dtype='float32') 39 | npB = numpy.array([[[1, 2, 3], [4, 5, 6], [7, 8, 9]], 40 | [[10, 11, 12], [13, 14, 15], [16, 17, 18]], 41 | [[19, 20, 21], [22, 23, 24], [25, 26, 27]]], 42 | dtype='float32') 43 | expectedC = numpy.tensordot(npA, npB) 44 | 45 | clpA = clpy.array(npA) 46 | clpB = clpy.array(npB) 47 | actualC = clpy.tensordot(clpA, clpB) 48 | 49 | self.assertTrue(numpy.allclose(expectedC, actualC.get())) 50 | 51 | 52 | if __name__ == "__main__": 53 | unittest.main() 54 | -------------------------------------------------------------------------------- /tests/clpy_tests/math_tests/test_explog.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy 4 | 5 | from clpy import testing 6 | 7 | 8 | @testing.gpu 9 | class TestExplog(unittest.TestCase): 10 | 11 | _multiprocess_can_split_ = True 12 | 13 | @testing.for_all_dtypes() 14 | @testing.numpy_clpy_allclose(atol=1e-5) 15 | def check_unary(self, name, xp, dtype, no_complex=False): 16 | if no_complex: 17 | if numpy.dtype(dtype).kind == 'c': 18 | return xp.array(True) 19 | a = testing.shaped_arange((2, 3), xp, dtype) 20 | return getattr(xp, name)(a) 21 | 22 | @testing.for_all_dtypes() 23 | @testing.numpy_clpy_allclose(atol=1e-5) 24 | def check_binary(self, name, xp, dtype, no_complex=False): 25 | if no_complex: 26 | if numpy.dtype(dtype).kind == 'c': 27 | return xp.array(True) 28 | a = testing.shaped_arange((2, 3), xp, dtype) 29 | b = testing.shaped_reverse_arange((2, 3), xp, dtype) 30 | return getattr(xp, name)(a, b) 31 | 32 | def test_exp(self): 33 | self.check_unary('exp') 34 | 35 | def test_expm1(self): 36 | self.check_unary('expm1', no_complex=True) 37 | 38 | def test_exp2(self): 39 | self.check_unary('exp2') 40 | 41 | def test_log(self): 42 | with testing.NumpyError(divide='ignore'): 43 | self.check_unary('log') 44 | 45 | def test_log10(self): 46 | with testing.NumpyError(divide='ignore'): 47 | self.check_unary('log10') 48 | 49 | def test_log2(self): 50 | with testing.NumpyError(divide='ignore'): 51 | self.check_unary('log2', no_complex=True) 52 | 53 | def test_log1p(self): 54 | self.check_unary('log1p', no_complex=True) 55 | 56 | def test_logaddexp(self): 57 | self.check_binary('logaddexp', no_complex=True) 58 | 59 | def test_logaddexp2(self): 60 | self.check_binary('logaddexp2', no_complex=True) 61 | -------------------------------------------------------------------------------- /tests/clpy_tests/math_tests/test_floating.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy 4 | 5 | import clpy 6 | from clpy import testing 7 | 8 | 9 | @testing.gpu 10 | class TestFloating(unittest.TestCase): 11 | 12 | _multiprocess_can_split_ = True 13 | 14 | @testing.for_all_dtypes() 15 | @testing.numpy_clpy_allclose(atol=1e-5) 16 | def check_unary(self, name, xp, dtype, no_complex=False): 17 | if no_complex and numpy.dtype(dtype).kind == 'c': 18 | return dtype(True) 19 | a = testing.shaped_arange((2, 3), xp, dtype) 20 | return getattr(xp, name)(a) 21 | 22 | @testing.for_all_dtypes(no_complex=True) 23 | @testing.numpy_clpy_allclose(atol=1e-5) 24 | def check_binary(self, name, xp, dtype): 25 | a = testing.shaped_arange((2, 3), xp, dtype) 26 | b = testing.shaped_reverse_arange((2, 3), xp, dtype) 27 | return getattr(xp, name)(a, b) 28 | 29 | def test_signbit(self): 30 | self.check_unary('signbit', no_complex=True) 31 | 32 | def test_copysign(self): 33 | self.check_binary('copysign') 34 | 35 | @testing.for_float_dtypes(name='ftype') 36 | @testing.for_dtypes(['i', 'l'], name='itype') 37 | @testing.numpy_clpy_allclose() 38 | def test_ldexp(self, xp, ftype, itype): 39 | a = xp.array([-3, -2, -1, 0, 1, 2, 3], dtype=ftype) 40 | b = xp.array([-3, -2, -1, 0, 1, 2, 3], dtype=itype) 41 | return xp.ldexp(a, b) 42 | 43 | @testing.for_float_dtypes() 44 | def test_frexp(self, dtype): 45 | numpy_a = numpy.array([-300, -20, -10, -1, 0, 1, 10, 20, 300], 46 | dtype=dtype) 47 | numpy_b, numpy_c = numpy.frexp(numpy_a) 48 | 49 | clpy_a = clpy.array(numpy_a) 50 | clpy_b, clpy_c = clpy.frexp(clpy_a) 51 | 52 | testing.assert_allclose(clpy_b, numpy_b) 53 | testing.assert_array_equal(clpy_c, numpy_c) 54 | 55 | def test_nextafter(self): 56 | self.check_binary('nextafter') 57 | -------------------------------------------------------------------------------- /clpy/creation/matrix.py: -------------------------------------------------------------------------------- 1 | import numpy 2 | 3 | import clpy 4 | 5 | 6 | def diag(v, k=0): 7 | """Returns a diagonal or a diagonal array. 8 | 9 | Args: 10 | v (array-like): Array or array-like object. 11 | k (int): Index of diagonals. Zero indicates the main diagonal, a 12 | positive value an upper diagonal, and a negative value a lower 13 | diagonal. 14 | 15 | Returns: 16 | clpy.ndarray: If ``v`` indicates a 1-D array, then it returns a 2-D 17 | array with the specified diagonal filled by ``v``. If ``v`` indicates a 18 | 2-D array, then it returns the specified diagonal of ``v``. In latter 19 | case, if ``v`` is a :class:`clpy.ndarray` object, then its view is 20 | returned. 21 | 22 | .. seealso:: :func:`numpy.diag` 23 | 24 | """ 25 | if isinstance(v, clpy.ndarray): 26 | if v.ndim == 1: 27 | size = v.size + abs(k) 28 | ret = clpy.zeros((size, size), dtype=v.dtype) 29 | ret.diagonal(k)[:] = v 30 | return ret 31 | else: 32 | return v.diagonal(k) 33 | else: 34 | return clpy.array(numpy.diag(v, k)) 35 | 36 | 37 | def diagflat(v, k=0): 38 | """Creates a diagonal array from the flattened input. 39 | 40 | Args: 41 | v (array-like): Array or array-like object. 42 | k (int): Index of diagonals. See :func:`clpy.diag` for detail. 43 | 44 | Returns: 45 | clpy.ndarray: A 2-D diagonal array with the diagonal copied from ``v``. 46 | 47 | """ 48 | if isinstance(v, clpy.ndarray): 49 | return clpy.diag(v.ravel(), k) 50 | else: 51 | return clpy.diag(numpy.ndarray(v).ravel(), k) 52 | 53 | 54 | # TODO(okuta): Implement tri 55 | 56 | 57 | # TODO(okuta): Implement tril 58 | 59 | 60 | # TODO(okuta): Implement triu 61 | 62 | 63 | # TODO(okuta): Implement vander 64 | 65 | 66 | # TODO(okuta): Implement mat 67 | 68 | 69 | # TODO(okuta): Implement bmat 70 | -------------------------------------------------------------------------------- /tests/clpy_tests/test_numpy_interop.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy 4 | 5 | import clpy 6 | from clpy import testing 7 | 8 | 9 | try: 10 | import scipy.sparse 11 | scipy_available = True 12 | except ImportError: 13 | scipy_available = False 14 | 15 | 16 | @testing.gpu 17 | class TestGetArrayModule(unittest.TestCase): 18 | 19 | def test_get_array_module_1(self): 20 | n1 = numpy.array([2], numpy.float32) 21 | c1 = clpy.array([2], numpy.float32) 22 | csr1 = clpy.sparse.csr_matrix((5, 3), dtype=numpy.float32) 23 | 24 | self.assertIs(numpy, clpy.get_array_module()) 25 | self.assertIs(numpy, clpy.get_array_module(n1)) 26 | self.assertIs(clpy, clpy.get_array_module(c1)) 27 | self.assertIs(clpy, clpy.get_array_module(csr1)) 28 | 29 | self.assertIs(numpy, clpy.get_array_module(n1, n1)) 30 | self.assertIs(clpy, clpy.get_array_module(c1, c1)) 31 | self.assertIs(clpy, clpy.get_array_module(csr1, csr1)) 32 | 33 | self.assertIs(clpy, clpy.get_array_module(n1, csr1)) 34 | self.assertIs(clpy, clpy.get_array_module(csr1, n1)) 35 | self.assertIs(clpy, clpy.get_array_module(c1, n1)) 36 | self.assertIs(clpy, clpy.get_array_module(n1, c1)) 37 | self.assertIs(clpy, clpy.get_array_module(c1, csr1)) 38 | self.assertIs(clpy, clpy.get_array_module(csr1, c1)) 39 | 40 | if scipy_available: 41 | csrn1 = scipy.sparse.csr_matrix((5, 3), dtype=numpy.float32) 42 | 43 | self.assertIs(numpy, clpy.get_array_module(csrn1)) 44 | self.assertIs(clpy, clpy.get_array_module(csrn1, csr1)) 45 | self.assertIs(clpy, clpy.get_array_module(csr1, csrn1)) 46 | self.assertIs(clpy, clpy.get_array_module(c1, csrn1)) 47 | self.assertIs(clpy, clpy.get_array_module(csrn1, c1)) 48 | self.assertIs(numpy, clpy.get_array_module(n1, csrn1)) 49 | self.assertIs(numpy, clpy.get_array_module(csrn1, n1)) 50 | -------------------------------------------------------------------------------- /clpy/core/include/clpy/complex/cpow.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | namespace thrust { 6 | 7 | template 8 | __device__ inline complex pow(const complex& z, 9 | const complex& exponent) { 10 | const T absz = abs(z); 11 | if (absz == 0) { 12 | return complex(0, 0); 13 | } 14 | const T real = exponent.real(); 15 | const T imag = exponent.imag(); 16 | const T argz = arg(z); 17 | T r = ::pow(absz, real); 18 | T theta = real * argz; 19 | if (imag != 0) { 20 | r *= ::exp(-imag * argz); 21 | theta += imag * ::log(absz); 22 | } 23 | return complex(r * cos(theta), r * sin(theta)); 24 | } 25 | 26 | template 27 | __device__ inline complex pow(const complex& z, const T& exponent) { 28 | return pow(z, complex(exponent)); 29 | } 30 | 31 | template 32 | __device__ inline complex pow(const T& x, const complex& exponent) { 33 | return pow(complex(x), exponent); 34 | } 35 | 36 | template 37 | __device__ inline complex::type> pow( 38 | const complex& z, const complex& exponent) { 39 | typedef typename _select_greater_type::type PromotedType; 40 | return pow(complex(z), complex(exponent)); 41 | } 42 | 43 | template 44 | __device__ inline complex::type> pow( 45 | const complex& z, const U& exponent) { 46 | typedef typename _select_greater_type::type PromotedType; 47 | return pow(complex(z), PromotedType(exponent)); 48 | } 49 | 50 | template 51 | __device__ inline complex::type> pow( 52 | const T& x, const complex& exponent) { 53 | typedef typename _select_greater_type::type PromotedType; 54 | return pow(PromotedType(x), complex(exponent)); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /clpy/backend/cuda/profiler.pyx: -------------------------------------------------------------------------------- 1 | # distutils: language = c++ 2 | 3 | """Thin wrapper of cuda profiler.""" 4 | from clpy.cuda cimport runtime 5 | 6 | 7 | cdef extern from "cupy_cuda.h" nogil: 8 | runtime.Error cudaProfilerInitialize(const char *configFile, 9 | const char *outputFile, 10 | int outputMode) 11 | runtime.Error cudaProfilerStart() 12 | runtime.Error cudaProfilerStop() 13 | 14 | 15 | cpdef void initialize(str config_file, 16 | str output_file, 17 | int output_mode) except *: 18 | """Initialize the CUDA profiler. 19 | 20 | This function initialize the CUDA profiler. See the CUDA document for 21 | detail. 22 | 23 | Args: 24 | config_file (str): Name of the configuration file. 25 | output_file (str): Name of the coutput file. 26 | output_mode (int): ``clpy.cuda.profiler.cudaKeyValuePair`` or 27 | ``clpy.cuda.profiler.cudaCSV``. 28 | """ 29 | cdef bytes b_config_file = config_file.encode() 30 | cdef bytes b_output_file = output_file.encode() 31 | status = cudaProfilerInitialize(b_config_file, 32 | b_output_file, 33 | output_mode) 34 | runtime.check_status(status) 35 | 36 | 37 | cpdef void start() except *: 38 | """Enable profiling. 39 | 40 | A user can enable CUDA profiling. When an error occurs, it raises an 41 | exception. 42 | 43 | See the CUDA document for detail. 44 | """ 45 | status = cudaProfilerStart() 46 | runtime.check_status(status) 47 | 48 | 49 | cpdef void stop() except *: 50 | """Disable profiling. 51 | 52 | A user can disable CUDA profiling. When an error occurs, it raises an 53 | exception. 54 | 55 | See the CUDA document for detail. 56 | """ 57 | status = cudaProfilerStop() 58 | runtime.check_status(status) 59 | -------------------------------------------------------------------------------- /clpy/core/include/cupy/carray.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #ifdef __ULTIMA 4 | #include 5 | #endif 6 | 7 | // CArray 8 | template 9 | class CArray { 10 | public: 11 | static const int ndim = _ndim; 12 | int size() const; 13 | 14 | const ptrdiff_t* shape() const; 15 | 16 | const ptrdiff_t* strides() const; 17 | 18 | template 19 | T& operator[](const Int (&idx)[ndim]); 20 | 21 | template 22 | const T& operator[](const Int (&idx)[ndim]) const; 23 | 24 | T& operator[](ptrdiff_t i); 25 | 26 | const T& operator[](ptrdiff_t i) const; 27 | }; 28 | 29 | #ifdef __ULTIMA 30 | namespace __ultima_detail{ 31 | templatestruct cindexer_; 32 | template<>struct cindexer_<0>{typedef CIndexer_0 type;}; 33 | template<>struct cindexer_<1>{typedef CIndexer_1 type;}; 34 | template<>struct cindexer_<2>{typedef CIndexer_2 type;}; 35 | template<>struct cindexer_<3>{typedef CIndexer_3 type;}; 36 | template<>struct cindexer_<4>{typedef CIndexer_4 type;}; 37 | template<>struct cindexer_<5>{typedef CIndexer_5 type;}; 38 | template<>struct cindexer_<6>{typedef CIndexer_6 type;}; 39 | template<>struct cindexer_<7>{typedef CIndexer_7 type;}; 40 | template<>struct cindexer_<8>{typedef CIndexer_8 type;}; 41 | template<>struct cindexer_<9>{typedef CIndexer_9 type;}; 42 | template<>struct cindexer_<10>{typedef CIndexer_10 type;}; 43 | template<>struct cindexer_<11>{typedef CIndexer_11 type;}; 44 | template<>struct cindexer_<12>{typedef CIndexer_12 type;}; 45 | template<>struct cindexer_<13>{typedef CIndexer_13 type;}; 46 | template<>struct cindexer_<14>{typedef CIndexer_14 type;}; 47 | template<>struct cindexer_<15>{typedef CIndexer_15 type;}; 48 | } 49 | #endif 50 | 51 | template 52 | class CIndexer { 53 | public: 54 | static const int ndim = _ndim; 55 | ptrdiff_t size() const; 56 | 57 | void set(ptrdiff_t i); 58 | 59 | const ptrdiff_t* get() const; 60 | 61 | #ifdef __ULTIMA 62 | const typename __ultima_detail::cindexer_<_ndim>::type* operator&()const; 63 | #endif 64 | }; 65 | 66 | -------------------------------------------------------------------------------- /clpy/indexing/indexing.py: -------------------------------------------------------------------------------- 1 | def take(a, indices, axis=None, out=None): 2 | """Takes elements of an array at specified indices along an axis. 3 | 4 | This is an implementation of "fancy indexing" at single axis. 5 | 6 | This function does not support ``mode`` option. 7 | 8 | Args: 9 | a (clpy.ndarray): Array to extract elements. 10 | indices (int or array-like): Indices of elements that this function 11 | takes. 12 | axis (int): The axis along which to select indices. The flattened input 13 | is used by default. 14 | out (clpy.ndarray): Output array. If provided, it should be of 15 | appropriate shape and dtype. 16 | 17 | Returns: 18 | clpy.ndarray: The result of fancy indexing. 19 | 20 | .. seealso:: :func:`numpy.take` 21 | 22 | """ 23 | # TODO(okuta): check type 24 | return a.take(indices, axis, out) 25 | 26 | 27 | def choose(a, choices, out=None, mode='raise'): 28 | return a.choose(choices, out, mode) 29 | 30 | 31 | # TODO(okuta): Implement compress 32 | 33 | 34 | def diagonal(a, offset=0, axis1=0, axis2=1): 35 | """Returns specified diagonals. 36 | 37 | This function extracts the diagonals along two specified axes. The other 38 | axes are not changed. This function returns a writable view of this array 39 | as NumPy 1.10 will do. 40 | 41 | Args: 42 | a (clpy.ndarray): Array from which the diagonals are taken. 43 | offset (int): Index of the diagonals. Zero indicates the main 44 | diagonals, a positive value upper diagonals, and a negative value 45 | lower diagonals. 46 | axis1 (int): The first axis to take diagonals from. 47 | axis2 (int): The second axis to take diagonals from. 48 | 49 | Returns: 50 | clpy.ndarray: A view of the diagonals of ``a``. 51 | 52 | .. seealso:: :func:`numpy.diagonal` 53 | 54 | """ 55 | # TODO(okuta): check type 56 | return a.diagonal(offset, axis1, axis2) 57 | 58 | 59 | # TODO(okuta): Implement select 60 | -------------------------------------------------------------------------------- /tests/clpy_tests/manipulation_tests/test_tiling.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from clpy import testing 4 | 5 | 6 | @testing.parameterize( 7 | {'repeats': 0, 'axis': None}, 8 | {'repeats': 2, 'axis': None}, 9 | {'repeats': 2, 'axis': 1}, 10 | {'repeats': 2, 'axis': -1}, 11 | {'repeats': [0, 0, 0], 'axis': 1}, 12 | {'repeats': [1, 2, 3], 'axis': 1}, 13 | {'repeats': [1, 2, 3], 'axis': -2}, 14 | ) 15 | @testing.gpu 16 | class TestRepeat(unittest.TestCase): 17 | 18 | _multiprocess_can_split_ = True 19 | 20 | @testing.numpy_clpy_array_equal() 21 | def test_array_repeat(self, xp): 22 | x = testing.shaped_arange((2, 3, 4), xp) 23 | return xp.repeat(x, self.repeats, self.axis) 24 | 25 | 26 | @testing.parameterize( 27 | {'repeats': -3, 'axis': None}, 28 | {'repeats': [-3, -3], 'axis': 0}, 29 | {'repeats': [1, 2, 3], 'axis': None}, 30 | {'repeats': [1, 2], 'axis': 1}, 31 | ) 32 | @testing.gpu 33 | class TestRepeatFailure(unittest.TestCase): 34 | 35 | _multiprocess_can_split_ = True 36 | 37 | @testing.numpy_clpy_raises() 38 | def test_repeat_failure(self, xp): 39 | x = testing.shaped_arange((2, 3, 4), xp) 40 | xp.repeat(x, -3) 41 | 42 | 43 | @testing.parameterize( 44 | {'reps': 0}, 45 | {'reps': 1}, 46 | {'reps': 2}, 47 | {'reps': (0, 1)}, 48 | {'reps': (2, 3)}, 49 | {'reps': (2, 3, 4, 5)}, 50 | ) 51 | @testing.gpu 52 | class TestTile(unittest.TestCase): 53 | 54 | _multiprocess_can_split_ = True 55 | 56 | @testing.numpy_clpy_array_equal() 57 | def test_array_tile(self, xp): 58 | x = testing.shaped_arange((2, 3, 4), xp) 59 | return xp.tile(x, self.reps) 60 | 61 | 62 | @testing.parameterize( 63 | {'reps': -1}, 64 | {'reps': (-1, -2)}, 65 | ) 66 | @testing.gpu 67 | class TestTileFailure(unittest.TestCase): 68 | 69 | _multiprocess_can_split_ = True 70 | 71 | @testing.numpy_clpy_raises() 72 | def test_tile_failure(self, xp): 73 | x = testing.shaped_arange((2, 3, 4), xp) 74 | xp.tile(x, -3) 75 | -------------------------------------------------------------------------------- /docs/source/reference/ndarray.rst: -------------------------------------------------------------------------------- 1 | Multi-Dimensional Array (ndarray) 2 | ================================= 3 | 4 | :class:`cupy.ndarray` is the CuPy counterpart of NumPy :class:`numpy.ndarray`. 5 | It provides an intuitive interface for a fixed-size multidimensional array which resides 6 | in a CUDA device. 7 | 8 | For the basic concept of ``ndarray``\s, please refer to the `NumPy documentation `_. 9 | 10 | 11 | .. autosummary:: 12 | :toctree: generated/ 13 | :nosignatures: 14 | 15 | cupy.ndarray 16 | 17 | 18 | Code compatibility features 19 | --------------------------- 20 | 21 | :class:`cupy.ndarray` is designed to be interchangeable with :class:`numpy.ndarray` in terms of code compatibility as much as possible. 22 | But occasionally, you will need to know whether the arrays you're handling are :class:`cupy.ndarray` or :class:`numpy.ndarray`. 23 | One example is when invoking module-level functions such as :meth:`cupy.sum` or :meth:`numpy.sum`. 24 | In such situations, :meth:`cupy.get_array_module` can be used. 25 | 26 | .. autosummary:: 27 | :toctree: generated/ 28 | :nosignatures: 29 | 30 | cupy.get_array_module 31 | 32 | 33 | Conversion to/from NumPy arrays 34 | ------------------------------- 35 | 36 | :class:`cupy.ndarray` and :class:`numpy.ndarray` are not implicitly convertible to each other. 37 | That means, NumPy functions cannot take :class:`cupy.ndarray`\s as inputs, and vice versa. 38 | 39 | - To convert :class:`numpy.ndarray` to :class:`cupy.ndarray`, use :meth:`cupy.array` or :meth:`cupy.asarray`. 40 | - To convert :class:`cupy.ndarray` to :class:`numpy.ndarray`, use :meth:`cupy.asnumpy` or :meth:`cupy.ndarray.get`. 41 | 42 | Note that converting between :class:`cupy.ndarray` and :class:`numpy.ndarray` incurs data transfer between 43 | the host (CPU) device and the GPU device, which is costly in terms of performance. 44 | 45 | 46 | .. autosummary:: 47 | :toctree: generated/ 48 | :nosignatures: 49 | 50 | cupy.array 51 | cupy.asarray 52 | cupy.asnumpy 53 | -------------------------------------------------------------------------------- /tests/clpy_tests/cuda_tests/memory_hooks_tests/test_debug_print.py: -------------------------------------------------------------------------------- 1 | import json 2 | import six 3 | import unittest 4 | 5 | import cupy.cuda 6 | from cupy.cuda import memory 7 | from cupy.cuda import memory_hooks 8 | from cupy import testing 9 | 10 | 11 | @testing.gpu 12 | class TestDebugPrintHook(unittest.TestCase): 13 | 14 | def setUp(self): 15 | self.io = six.StringIO() 16 | self.hook = memory_hooks.DebugPrintHook(file=self.io) 17 | self.pool = memory.MemoryPool() 18 | 19 | def test_print(self): 20 | device_id = 0 21 | size = 1 22 | unit = 512 23 | with cupy.cuda.Device(device_id): 24 | with self.hook: 25 | mem = self.pool.malloc(size) 26 | ptr1, pmem1 = mem.ptr, id(mem.mem) 27 | del mem 28 | mem = self.pool.malloc(size) 29 | ptr2, pmem2 = mem.ptr, id(mem.mem) 30 | del mem 31 | actual_lines = self.io.getvalue().splitlines() 32 | 33 | expect = {'hook': 'alloc', 'device_id': device_id, 34 | 'mem_size': unit, 'mem_ptr': ptr1} 35 | self.assertEqual(expect, json.loads(actual_lines[0])) 36 | 37 | expect = {'hook': 'malloc', 'device_id': device_id, 'size': size, 38 | 'mem_size': unit, 'mem_ptr': ptr1, 'pmem_id': hex(pmem1)} 39 | self.assertEqual(expect, json.loads(actual_lines[1])) 40 | 41 | expect = {'hook': 'free', 'device_id': device_id, 42 | 'mem_size': unit, 'mem_ptr': ptr1, 'pmem_id': hex(pmem1)} 43 | self.assertEqual(expect, json.loads(actual_lines[2])) 44 | 45 | expect = {'hook': 'malloc', 'device_id': device_id, 'size': size, 46 | 'mem_size': unit, 'mem_ptr': ptr2, 'pmem_id': hex(pmem2)} 47 | self.assertEqual(expect, json.loads(actual_lines[3])) 48 | 49 | expect = {'hook': 'free', 'device_id': device_id, 50 | 'mem_size': unit, 'mem_ptr': ptr2, 'pmem_id': hex(pmem2)} 51 | self.assertEqual(expect, json.loads(actual_lines[4])) 52 | -------------------------------------------------------------------------------- /tests/clpy_tests/opencl_tests/ultima_tests/test_half.py: -------------------------------------------------------------------------------- 1 | # flake8: noqa 2 | # TODO(vorj): When we will meet flake8 3.7.0+, 3 | # we should ignore only W291 for whole file 4 | # using --per-file-ignores . 5 | 6 | import clpy 7 | import unittest 8 | 9 | 10 | class TestUltimaHalfTrick(unittest.TestCase): 11 | 12 | def test_type_half(self): 13 | x = clpy.backend.ultima.exec_ultima('', '#include ') + (''' 14 | __clpy__half f() 15 | { 16 | __clpy__half a;constructor___clpy__half___left_paren____clpy__half_float__right_paren__(&a, 42.F); 17 | return a; 18 | } 19 | ''')[1:] 20 | y = clpy.backend.ultima.exec_ultima( 21 | ''' 22 | half f(){ 23 | half a = 42.f; 24 | return a; 25 | } 26 | ''', 27 | '#include ') 28 | self.assertEqual(x, y) 29 | 30 | def test_variable_named_half(self): 31 | x = ''' 32 | void f() 33 | { 34 | int __clpy__half = 1 / 2; 35 | } 36 | ''' 37 | y = clpy.backend.ultima.exec_ultima( 38 | ''' 39 | void f(){ 40 | int half = 1/2; 41 | } 42 | ''') 43 | self.assertEqual(x[1:], y) 44 | 45 | def test_argument_named_half(self): 46 | x = ''' 47 | void f(int __clpy__half) 48 | { 49 | } 50 | ''' 51 | y = clpy.backend.ultima.exec_ultima( 52 | ''' 53 | void f(int half){} 54 | ''') 55 | self.assertEqual(x[1:], y) 56 | 57 | def test_clpy_half(self): 58 | x = clpy.backend.ultima.exec_ultima('', '#include ') + (''' 59 | void f() 60 | { 61 | int __clpy__half = 42; 62 | } 63 | ''')[1:] 64 | y = clpy.backend.ultima.exec_ultima( 65 | ''' 66 | void f(){ 67 | int __clpy__half = 42; 68 | } 69 | ''', 70 | '#include ') 71 | self.assertEqual(x, y) 72 | 73 | 74 | if __name__ == "__main__": 75 | unittest.main() 76 | -------------------------------------------------------------------------------- /tests/clpy_tests/io_tests/test_npz.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import six 4 | 5 | import clpy 6 | from clpy import testing 7 | 8 | 9 | @testing.gpu 10 | class TestNpz(unittest.TestCase): 11 | 12 | _multiprocess_can_split_ = True 13 | 14 | @testing.for_all_dtypes() 15 | def test_save_load(self, dtype): 16 | a = testing.shaped_arange((2, 3, 4), dtype=dtype) 17 | sio = six.BytesIO() 18 | clpy.save(sio, a) 19 | s = sio.getvalue() 20 | sio.close() 21 | 22 | sio = six.BytesIO(s) 23 | b = clpy.load(sio) 24 | sio.close() 25 | 26 | testing.assert_array_equal(a, b) 27 | 28 | @testing.for_all_dtypes() 29 | def check_savez(self, savez, dtype): 30 | a1 = testing.shaped_arange((2, 3, 4), dtype=dtype) 31 | a2 = testing.shaped_arange((3, 4, 5), dtype=dtype) 32 | 33 | sio = six.BytesIO() 34 | savez(sio, a1, a2) 35 | s = sio.getvalue() 36 | sio.close() 37 | 38 | sio = six.BytesIO(s) 39 | with clpy.load(sio) as d: 40 | b1 = d['arr_0'] 41 | b2 = d['arr_1'] 42 | sio.close() 43 | 44 | testing.assert_array_equal(a1, b1) 45 | testing.assert_array_equal(a2, b2) 46 | 47 | def test_savez(self): 48 | self.check_savez(clpy.savez) 49 | 50 | def test_savez_compressed(self): 51 | self.check_savez(clpy.savez_compressed) 52 | 53 | @testing.for_all_dtypes() 54 | def test_pickle(self, dtype): 55 | a = testing.shaped_arange((2, 3, 4), dtype=dtype) 56 | s = six.moves.cPickle.dumps(a) 57 | b = six.moves.cPickle.loads(s) 58 | testing.assert_array_equal(a, b) 59 | 60 | @testing.for_all_dtypes() 61 | def test_dump(self, dtype): 62 | a = testing.shaped_arange((2, 3, 4), dtype=dtype) 63 | 64 | sio = six.BytesIO() 65 | a.dump(sio) 66 | s = sio.getvalue() 67 | sio.close() 68 | 69 | sio = six.BytesIO(s) 70 | b = clpy.load(sio) 71 | sio.close() 72 | 73 | testing.assert_array_equal(a, b) 74 | -------------------------------------------------------------------------------- /clpy/core/include/clpy/complex/cproj.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008-2013 NVIDIA Corporation 3 | * Copyright 2013 Filipe RNC Maia 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | #pragma once 19 | 20 | #include 21 | #include 22 | 23 | namespace thrust { 24 | namespace detail { 25 | namespace complex { 26 | 27 | using thrust::complex; 28 | 29 | __device__ inline complex cprojf(const complex& z) { 30 | if (!isinf(z.real()) && !isinf(z.imag())) { 31 | return z; 32 | } else { 33 | // std::numeric_limits::infinity() doesn't run on the GPU 34 | return complex(infinity(), copysignf(0.0, z.imag())); 35 | } 36 | } 37 | 38 | __device__ inline complex cproj(const complex& z) { 39 | if (!isinf(z.real()) && !isinf(z.imag())) { 40 | return z; 41 | } else { 42 | // numeric_limits::infinity() doesn't run on the GPU 43 | return complex(infinity(), copysign(0.0, z.imag())); 44 | } 45 | } 46 | } 47 | } 48 | 49 | template 50 | __device__ inline thrust::complex proj(const thrust::complex& z) { 51 | return detail::complex::cproj(z); 52 | } 53 | 54 | template <> 55 | __device__ inline thrust::complex proj( 56 | const thrust::complex& z) { 57 | return detail::complex::cproj(z); 58 | } 59 | 60 | template <> 61 | __device__ inline thrust::complex proj(const thrust::complex& z) { 62 | return detail::complex::cprojf(z); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /tests/clpy_tests/opencl_tests/test_exception.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy 4 | import re 5 | import six 6 | 7 | import clpy 8 | from clpy import testing 9 | 10 | from clpy.backend.opencl.exceptions import OpenCLProgramBuildError 11 | from clpy.backend.ultima.exceptions import UltimaRuntimeError 12 | 13 | 14 | @testing.gpu 15 | class TestBuildExceptions(unittest.TestCase): 16 | 17 | def test_undeclared_identifier(self): 18 | with six.assertRaisesRegex(self, UltimaRuntimeError, 19 | 'undeclared identifier'): 20 | x = clpy.core.array(numpy.array([1], dtype="float32")) 21 | clpy.ElementwiseKernel( 22 | 'T x', 23 | '', 24 | 'undeclared_identifier', 25 | 'use_of_undeclared_indentifier')(x) 26 | 27 | def test_opencl_error(self): 28 | for id in range(clpy.backend.opencl.env.num_devices): 29 | with clpy.backend.Device(id): 30 | pattern = re.compile( 31 | 'CL_BUILD_PROGRAM_FAILURE .*Device#%d' % id, 32 | re.DOTALL) 33 | with six.assertRaisesRegex(self, 34 | OpenCLProgramBuildError, 35 | pattern): 36 | x = clpy.core.array(numpy.array([1], dtype="float32")) 37 | clpy.ElementwiseKernel( 38 | 'T x', 39 | '', 40 | '__global T t;', 41 | 'test')(x) 42 | 43 | # def test_assign_to_const_qualified_variable(self): 44 | # with six.assertRaisesRegex(self, OpenCLProgramBuildError, 45 | # 'cannot assign|is not assignable'): 46 | # x = clpy.core.array(numpy.array([1], dtype="float32")) 47 | # clpy.ElementwiseKernel( 48 | # 'T x', 49 | # 'T y', 50 | # 'x = y', 51 | # 'assign_to_const_qualified_variable')(x) 52 | 53 | 54 | if __name__ == "__main__": 55 | unittest.main() 56 | -------------------------------------------------------------------------------- /tests/clpy_tests/core_tests/test_ndarray_get.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import clpy 4 | from clpy import backend 5 | from clpy import testing 6 | import numpy 7 | from numpy import testing as np_testing 8 | 9 | 10 | @testing.gpu 11 | class TestArrayGet(unittest.TestCase): 12 | 13 | _multiprocess_can_split_ = True 14 | 15 | def setUp(self): 16 | self.stream = backend.Stream.null 17 | 18 | def check_get(self, f, stream): 19 | a_gpu = f(clpy) 20 | a_cpu = a_gpu.get(stream) 21 | if stream: 22 | stream.synchronize() 23 | b_cpu = f(numpy) 24 | np_testing.assert_array_equal(a_cpu, b_cpu) 25 | 26 | @testing.for_all_dtypes() 27 | def test_contiguous_array(self, dtype): 28 | def contiguous_array(xp): 29 | return testing.shaped_arange((3,), xp=xp, dtype=dtype) 30 | self.check_get(contiguous_array, None) 31 | 32 | @testing.for_all_dtypes() 33 | def test_non_contiguous_array(self, dtype): 34 | def non_contiguous_array(xp): 35 | return testing.shaped_arange((3,), xp=xp, dtype=dtype)[0::2] 36 | self.check_get(non_contiguous_array, None) 37 | 38 | @testing.for_all_dtypes() 39 | def test_contiguous_array_stream(self, dtype): 40 | def contiguous_array(xp): 41 | return testing.shaped_arange((3,), xp=xp, dtype=dtype) 42 | self.check_get(contiguous_array, self.stream) 43 | 44 | @testing.for_all_dtypes() 45 | def test_non_contiguous_array_stream(self, dtype): 46 | def non_contiguous_array(xp): 47 | return testing.shaped_arange((3,), xp=xp, dtype=dtype)[0::2] 48 | self.check_get(non_contiguous_array, self.stream) 49 | 50 | @testing.multi_gpu(2) 51 | @testing.for_all_dtypes() 52 | def test_get_multigpu(self, dtype): 53 | with backend.Device(1): 54 | src = testing.shaped_arange((2, 3), xp=clpy, dtype=dtype) 55 | src = clpy.asfortranarray(src) 56 | with backend.Device(0): 57 | dst = src.get() 58 | expected = testing.shaped_arange((2, 3), xp=numpy, dtype=dtype) 59 | np_testing.assert_array_equal(dst, expected) 60 | -------------------------------------------------------------------------------- /clpy/ext/scatter.py: -------------------------------------------------------------------------------- 1 | def scatter_add(a, slices, value): 2 | """Adds given values to specified elements of an array. 3 | 4 | It adds ``value`` to the specified elements of ``a``. 5 | If all of the indices target different locations, the operation of 6 | :func:`scatter_add` is equivalent to ``a[slices] = a[slices] + value``. 7 | If there are multiple elements targeting the same location, 8 | :func:`scatter_add` uses all of these values for addition. On the other 9 | hand, ``a[slices] = a[slices] + value`` only adds the contribution from one 10 | of the indices targeting the same location. 11 | 12 | Note that just like an array indexing, negative indices are interpreted as 13 | counting from the end of an array. 14 | 15 | Also note that :func:`scatter_add` behaves identically 16 | to :func:`numpy.add.at`. 17 | 18 | Example 19 | ------- 20 | >>> import numpy 21 | >>> import clpy 22 | >>> a = clpy.zeros((6,), dtype=numpy.float32) 23 | >>> i = clpy.array([1, 0, 1]) 24 | >>> v = clpy.array([1., 1., 1.]) 25 | >>> clpy.scatter_add(a, i, v); 26 | >>> a 27 | array([ 1., 2., 0., 0., 0., 0.], dtype=float32) 28 | 29 | Args: 30 | a (ndarray): An array that gets added. 31 | slices: It is integer, slices, ellipsis, numpy.newaxis, 32 | integer array-like, boolean array-like or tuple of them. 33 | It works for slices used for 34 | :func:`clpy.ndarray.__getitem__` and 35 | :func:`clpy.ndarray.__setitem__`. 36 | v (array-like): Values to increment ``a`` at referenced locations. 37 | 38 | .. note:: 39 | It only supports types that are supported by CUDA's atomicAdd when 40 | an integer array is included in ``slices``. 41 | The supported types are ``numpy.float32``, ``numpy.int32``, 42 | ``numpy.uint32``, ``numpy.uint64`` and ``numpy.ulonglong``. 43 | 44 | .. note:: 45 | :func:`scatter_add` does not raise an error when indices exceed size of 46 | axes. Instead, it wraps indices. 47 | 48 | .. seealso:: :func:`numpy.add.at`. 49 | 50 | """ 51 | a.scatter_add(slices, value) 52 | -------------------------------------------------------------------------------- /clpy/backend/cuda/driver.pxd: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Types 3 | ############################################################################### 4 | 5 | cdef extern from *: 6 | ctypedef int Device 'CUdevice' 7 | ctypedef int Result 'CUresult' 8 | 9 | ctypedef void* Context 'CUcontext' 10 | ctypedef void* Deviceptr 'CUdeviceptr' 11 | ctypedef void* Event 'struct CUevent_st*' 12 | ctypedef void* Function 'struct CUfunc_st*' 13 | ctypedef void* Module 'struct CUmod_st*' 14 | ctypedef void* Stream 'struct CUstream_st*' 15 | ctypedef void* LinkState 'CUlinkState' 16 | 17 | ctypedef int CUjit_option 'CUjit_option' 18 | ctypedef int CUjitInputType 'CUjitInputType' 19 | 20 | cpdef enum: 21 | CU_JIT_INPUT_CUBIN = 0 22 | CU_JIT_INPUT_PTX = 1 23 | CU_JIT_INPUT_FATBINARY = 2 24 | CU_JIT_INPUT_OBJECT = 3 25 | CU_JIT_INPUT_LIBRARY = 4 26 | 27 | 28 | ############################################################################### 29 | # Context management 30 | ############################################################################### 31 | 32 | cpdef size_t ctxGetCurrent() except * 33 | 34 | ############################################################################### 35 | # Module load and kernel execution 36 | ############################################################################### 37 | 38 | cpdef size_t linkCreate() except * 39 | cpdef linkAddData(size_t state, int input_type, bytes data, unicode name) 40 | cpdef bytes linkComplete(size_t state) 41 | cpdef linkDestroy(size_t state) 42 | cpdef size_t moduleLoad(str filename) except * 43 | cpdef size_t moduleLoadData(bytes image) except * 44 | cpdef moduleUnload(size_t module) 45 | cpdef size_t moduleGetFunction(size_t module, str funcname) except * 46 | cpdef size_t moduleGetGlobal(size_t module, str varname) except * 47 | cpdef launchKernel( 48 | size_t f, unsigned int grid_dim_x, unsigned int grid_dim_y, 49 | unsigned int grid_dim_z, unsigned int block_dim_x, 50 | unsigned int block_dim_y, unsigned int block_dim_z, 51 | unsigned int shared_mem_bytes, size_t stream, size_t kernel_params, 52 | size_t extra) 53 | -------------------------------------------------------------------------------- /tests/clpy_tests/manipulation_tests/test_split.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from clpy import testing 4 | 5 | 6 | @testing.gpu 7 | class TestSplit(unittest.TestCase): 8 | 9 | _multiprocess_can_split_ = True 10 | 11 | @testing.numpy_clpy_array_equal() 12 | def test_array_split1(self, xp): 13 | a = testing.shaped_arange((3, 11), xp) 14 | split = xp.array_split(a, 4, 1) 15 | return xp.concatenate(split, 1) 16 | 17 | @testing.numpy_clpy_array_equal() 18 | def test_array_split2(self, xp): 19 | a = testing.shaped_arange((3, 11), xp) 20 | split = xp.array_split(a, 4, -1) 21 | return xp.concatenate(split, -1) 22 | 23 | @testing.numpy_clpy_array_equal() 24 | def test_array_spliti_empty(self, xp): 25 | a = testing.shaped_arange((3, 11), xp) 26 | split = xp.array_split(a, []) 27 | return xp.concatenate(split, -1) 28 | 29 | @testing.numpy_clpy_array_equal() 30 | def test_dsplit(self, xp): 31 | a = testing.shaped_arange((3, 3, 12), xp) 32 | split = xp.dsplit(a, 4) 33 | return xp.dstack(split) 34 | 35 | @testing.numpy_clpy_array_equal() 36 | def test_hsplit_vectors(self, xp): 37 | a = testing.shaped_arange((12,), xp) 38 | split = xp.hsplit(a, 4) 39 | return xp.hstack(split) 40 | 41 | @testing.numpy_clpy_array_equal() 42 | def test_hsplit(self, xp): 43 | a = testing.shaped_arange((3, 12), xp) 44 | split = xp.hsplit(a, 4) 45 | return xp.hstack(split) 46 | 47 | @testing.numpy_clpy_array_equal() 48 | def test_split_by_sections1(self, xp): 49 | a = testing.shaped_arange((3, 11), xp) 50 | split = xp.split(a, (2, 4, 9), 1) 51 | return xp.concatenate(split, 1) 52 | 53 | @testing.numpy_clpy_array_equal() 54 | def test_split_by_sections2(self, xp): 55 | a = testing.shaped_arange((3, 11), xp) 56 | split = xp.split(a, (2, 4, 9), -1) 57 | return xp.concatenate(split, -1) 58 | 59 | @testing.numpy_clpy_array_equal() 60 | def test_vsplit(self, xp): 61 | a = testing.shaped_arange((12, 3), xp) 62 | split = xp.vsplit(a, 4) 63 | return xp.vstack(split) 64 | -------------------------------------------------------------------------------- /tests/clpy_tests/core_tests/test_function.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy 4 | 5 | import clpy 6 | from clpy.core import core 7 | from clpy import testing 8 | 9 | 10 | def _compile_func(kernel_name, code): 11 | module = core.compile_with_cache(code) 12 | return module.get_function(kernel_name) 13 | 14 | 15 | @testing.gpu 16 | class TestFunction(unittest.TestCase): 17 | 18 | def test_python_scalar(self): 19 | code = ''' 20 | __kernel void test_kernel( 21 | __global const double* a, CArray_2 ai, 22 | double b, 23 | __global double* x, CArray_2 xi) { 24 | int i = get_local_id(0); 25 | x[get_CArrayIndexI_2(&xi, i)/sizeof(double)] 26 | = a[get_CArrayIndexI_2(&ai, i)/sizeof(double)] + b; 27 | } 28 | ''' 29 | 30 | a_cpu = numpy.arange(24, dtype=numpy.float64).reshape((4, 6)) 31 | a = clpy.array(a_cpu) 32 | b = float(2) 33 | x = clpy.empty_like(a) 34 | 35 | func = _compile_func('test_kernel', code) # NOQA 36 | 37 | func.linear_launch(a.size, (a, b, x)) 38 | 39 | expected = a_cpu + b 40 | testing.assert_array_equal(x, expected) 41 | 42 | def test_numpy_scalar(self): 43 | code = ''' 44 | __kernel void test_kernel( 45 | __global const double* a, CArray_2 ai, 46 | double b, 47 | __global double* x, CArray_2 xi) { 48 | int i = get_local_id(0); 49 | x[get_CArrayIndexI_2(&xi, i)/sizeof(double)] 50 | = a[get_CArrayIndexI_2(&ai, i)/sizeof(double)] + b; 51 | } 52 | ''' 53 | 54 | a_cpu = numpy.arange(24, dtype=numpy.float64).reshape((4, 6)) 55 | a = clpy.array(a_cpu) 56 | b = numpy.float64(2) 57 | x = clpy.empty_like(a) 58 | 59 | func = _compile_func('test_kernel', code) # NOQA 60 | 61 | func.linear_launch(a.size, (a, b, x)) 62 | 63 | expected = a_cpu + b 64 | testing.assert_array_equal(x, expected) 65 | 66 | def test_not_supported_scalar(self): 67 | code = ''' 68 | __kernel void test_kernel(char a) { 69 | } 70 | ''' 71 | 72 | a = "2" 73 | 74 | func = _compile_func('test_kernel', code) # NOQA 75 | 76 | with self.assertRaises(TypeError): 77 | func.linear_launch(1, a) 78 | --------------------------------------------------------------------------------