gumath 0.2.0dev5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. checksums.yaml +7 -0
  2. data/CONTRIBUTING.md +61 -0
  3. data/Gemfile +5 -0
  4. data/History.md +0 -0
  5. data/README.md +5 -0
  6. data/Rakefile +105 -0
  7. data/ext/ruby_gumath/examples.c +126 -0
  8. data/ext/ruby_gumath/extconf.rb +97 -0
  9. data/ext/ruby_gumath/functions.c +106 -0
  10. data/ext/ruby_gumath/gufunc_object.c +79 -0
  11. data/ext/ruby_gumath/gufunc_object.h +55 -0
  12. data/ext/ruby_gumath/gumath/AUTHORS.txt +5 -0
  13. data/ext/ruby_gumath/gumath/INSTALL.txt +42 -0
  14. data/ext/ruby_gumath/gumath/LICENSE.txt +29 -0
  15. data/ext/ruby_gumath/gumath/MANIFEST.in +3 -0
  16. data/ext/ruby_gumath/gumath/Makefile.in +62 -0
  17. data/ext/ruby_gumath/gumath/README.rst +20 -0
  18. data/ext/ruby_gumath/gumath/config.guess +1530 -0
  19. data/ext/ruby_gumath/gumath/config.h.in +52 -0
  20. data/ext/ruby_gumath/gumath/config.sub +1782 -0
  21. data/ext/ruby_gumath/gumath/configure +5049 -0
  22. data/ext/ruby_gumath/gumath/configure.ac +167 -0
  23. data/ext/ruby_gumath/gumath/doc/_static/copybutton.js +66 -0
  24. data/ext/ruby_gumath/gumath/doc/conf.py +26 -0
  25. data/ext/ruby_gumath/gumath/doc/gumath/functions.rst +62 -0
  26. data/ext/ruby_gumath/gumath/doc/gumath/index.rst +26 -0
  27. data/ext/ruby_gumath/gumath/doc/index.rst +45 -0
  28. data/ext/ruby_gumath/gumath/doc/libgumath/data-structures.rst +130 -0
  29. data/ext/ruby_gumath/gumath/doc/libgumath/functions.rst +78 -0
  30. data/ext/ruby_gumath/gumath/doc/libgumath/index.rst +25 -0
  31. data/ext/ruby_gumath/gumath/doc/libgumath/kernels.rst +41 -0
  32. data/ext/ruby_gumath/gumath/doc/releases/index.rst +11 -0
  33. data/ext/ruby_gumath/gumath/install-sh +527 -0
  34. data/ext/ruby_gumath/gumath/libgumath/Makefile.in +170 -0
  35. data/ext/ruby_gumath/gumath/libgumath/Makefile.vc +160 -0
  36. data/ext/ruby_gumath/gumath/libgumath/apply.c +201 -0
  37. data/ext/ruby_gumath/gumath/libgumath/extending/bfloat16.c +130 -0
  38. data/ext/ruby_gumath/gumath/libgumath/extending/examples.c +176 -0
  39. data/ext/ruby_gumath/gumath/libgumath/extending/graph.c +393 -0
  40. data/ext/ruby_gumath/gumath/libgumath/extending/pdist.c +140 -0
  41. data/ext/ruby_gumath/gumath/libgumath/extending/quaternion.c +156 -0
  42. data/ext/ruby_gumath/gumath/libgumath/func.c +177 -0
  43. data/ext/ruby_gumath/gumath/libgumath/gumath.h +205 -0
  44. data/ext/ruby_gumath/gumath/libgumath/kernels/binary.c +547 -0
  45. data/ext/ruby_gumath/gumath/libgumath/kernels/unary.c +449 -0
  46. data/ext/ruby_gumath/gumath/libgumath/nploops.c +219 -0
  47. data/ext/ruby_gumath/gumath/libgumath/tbl.c +223 -0
  48. data/ext/ruby_gumath/gumath/libgumath/thread.c +175 -0
  49. data/ext/ruby_gumath/gumath/libgumath/xndloops.c +130 -0
  50. data/ext/ruby_gumath/gumath/python/extending.py +24 -0
  51. data/ext/ruby_gumath/gumath/python/gumath/__init__.py +74 -0
  52. data/ext/ruby_gumath/gumath/python/gumath/_gumath.c +577 -0
  53. data/ext/ruby_gumath/gumath/python/gumath/examples.c +93 -0
  54. data/ext/ruby_gumath/gumath/python/gumath/functions.c +77 -0
  55. data/ext/ruby_gumath/gumath/python/gumath/pygumath.h +95 -0
  56. data/ext/ruby_gumath/gumath/python/test_gumath.py +405 -0
  57. data/ext/ruby_gumath/gumath/setup.py +298 -0
  58. data/ext/ruby_gumath/gumath/vcbuild/INSTALL.txt +36 -0
  59. data/ext/ruby_gumath/gumath/vcbuild/vcbuild32.bat +21 -0
  60. data/ext/ruby_gumath/gumath/vcbuild/vcbuild64.bat +21 -0
  61. data/ext/ruby_gumath/gumath/vcbuild/vcclean.bat +10 -0
  62. data/ext/ruby_gumath/gumath/vcbuild/vcdistclean.bat +11 -0
  63. data/ext/ruby_gumath/include/gumath.h +205 -0
  64. data/ext/ruby_gumath/include/ruby_gumath.h +41 -0
  65. data/ext/ruby_gumath/lib/libgumath.a +0 -0
  66. data/ext/ruby_gumath/lib/libgumath.so +1 -0
  67. data/ext/ruby_gumath/lib/libgumath.so.0 +1 -0
  68. data/ext/ruby_gumath/lib/libgumath.so.0.2.0dev3 +0 -0
  69. data/ext/ruby_gumath/ruby_gumath.c +295 -0
  70. data/ext/ruby_gumath/ruby_gumath.h +41 -0
  71. data/ext/ruby_gumath/ruby_gumath_internal.h +45 -0
  72. data/ext/ruby_gumath/util.c +68 -0
  73. data/ext/ruby_gumath/util.h +48 -0
  74. data/gumath.gemspec +47 -0
  75. data/lib/gumath.rb +7 -0
  76. data/lib/gumath/version.rb +5 -0
  77. data/lib/ruby_gumath.so +0 -0
  78. metadata +206 -0
@@ -0,0 +1,298 @@
1
+ #
2
+ # BSD 3-Clause License
3
+ #
4
+ # Copyright (c) 2017-2018, plures
5
+ # All rights reserved.
6
+ #
7
+ # Redistribution and use in source and binary forms, with or without
8
+ # modification, are permitted provided that the following conditions are met:
9
+ #
10
+ # 1. Redistributions of source code must retain the above copyright notice,
11
+ # this list of conditions and the following disclaimer.
12
+ #
13
+ # 2. Redistributions in binary form must reproduce the above copyright notice,
14
+ # this list of conditions and the following disclaimer in the documentation
15
+ # and/or other materials provided with the distribution.
16
+ #
17
+ # 3. Neither the name of the copyright holder nor the names of its
18
+ # contributors may be used to endorse or promote products derived from
19
+ # this software without specific prior written permission.
20
+ #
21
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
+ #
32
+
33
+ import sys, os
34
+
35
+ if "bdist_wheel" in sys.argv:
36
+ from setuptools import setup, Extension
37
+ else:
38
+ from distutils.core import setup, Extension
39
+
40
+ from distutils.command.build_ext import build_ext
41
+ from distutils.sysconfig import get_python_lib
42
+ from glob import glob
43
+ import platform
44
+ import subprocess
45
+ import shutil
46
+ import warnings
47
+
48
+
49
+ DESCRIPTION = """\
50
+ Extensible array functions that operate on xnd containers.\
51
+ """
52
+
53
+ LONG_DESCRIPTION = """\
54
+ """
55
+
56
+ warnings.simplefilter("ignore", UserWarning)
57
+
58
+ if sys.platform == "darwin":
59
+ LIBNAME = "libgumath.dylib"
60
+ LIBSONAME = "libgumath.0.dylib"
61
+ LIBSHARED = "libgumath.0.2.0dev3.dylib"
62
+ else:
63
+ LIBNAME = "libgumath.so"
64
+ LIBSONAME = "libgumath.so.0"
65
+ LIBSHARED = "libgumath.so.0.2.0dev3"
66
+ LIBNDTYPES = "libndtypes.so.0.2.0dev3"
67
+ LIBXND = "libxnd.so.0.2.0dev3"
68
+
69
+ if "install" in sys.argv or "bdist_wheel" in sys.argv:
70
+ CONFIGURE_INCLUDES = ["%s/ndtypes" % get_python_lib(),
71
+ "%s/xnd" % get_python_lib()]
72
+ CONFIGURE_LIBS = CONFIGURE_INCLUDES
73
+ INCLUDES = LIBS = CONFIGURE_INCLUDES
74
+ LIBGUMATHDIR = "%s/gumath" % get_python_lib()
75
+ INSTALL_LIBS = True
76
+ elif "conda_install" in sys.argv:
77
+ site = ["%s/ndtypes" % get_python_lib(), "%s/xnd" % get_python_lib()]
78
+ sys_includes = [os.path.join(os.environ['PREFIX'], "include")]
79
+ libdir = "Library/bin" if sys.platform == "win32" else "lib"
80
+ sys_libs = os.path.join(os.environ['PREFIX'], libdir)
81
+ INCLUDES = CONFIGURE_INCLUDES = sys_includes + site
82
+ LIBS = [sys_libs] + site
83
+ LIBGUMATHDIR = "%s/gumath" % get_python_lib()
84
+ INSTALL_LIBS = False
85
+ else:
86
+ CONFIGURE_INCLUDES = ["../python/ndtypes", "../python/xnd"]
87
+ CONFIGURE_LIBS = CONFIGURE_INCLUDES
88
+ INCLUDES = LIBS = CONFIGURE_INCLUDES
89
+ LIBGUMATHDIR = "../python/gumath"
90
+ INSTALL_LIBS = False
91
+
92
+
93
+
94
+ PY_MAJOR = sys.version_info[0]
95
+ PY_MINOR = sys.version_info[1]
96
+ ARCH = platform.architecture()[0]
97
+ BUILD_ALL = \
98
+ "build" in sys.argv or "install" in sys.argv or "bdist_wheel" in sys.argv
99
+
100
+
101
+ if PY_MAJOR < 3:
102
+ raise NotImplementedError(
103
+ "python2 support is not implemented")
104
+
105
+
106
+ def get_module_path():
107
+ pathlist = glob("build/lib.*/")
108
+ if pathlist:
109
+ return pathlist[0]
110
+ raise RuntimeError("cannot find xnd module in build directory")
111
+
112
+ def copy_ext():
113
+ if sys.platform == "win32":
114
+ pathlist = glob("build/lib.*/gumath/*.pyd")
115
+ else:
116
+ pathlist = glob("build/lib.*/gumath/*.so")
117
+ for f in pathlist:
118
+ shutil.copy2(f, "python/gumath")
119
+
120
+ def make_symlinks():
121
+ os.chdir(LIBGUMATHDIR)
122
+ os.chmod(LIBSHARED, 0o755)
123
+ os.system("ln -sf %s %s" % (LIBSHARED, LIBSONAME))
124
+ os.system("ln -sf %s %s" % (LIBSHARED, LIBNAME))
125
+
126
+
127
+ if len(sys.argv) == 3 and sys.argv[1] == "install" and \
128
+ sys.argv[2].startswith("--local"):
129
+ localdir = sys.argv[2].split("=")[1]
130
+ sys.argv = sys.argv[:2] + [
131
+ "--install-base=" + localdir,
132
+ "--install-purelib=" + localdir,
133
+ "--install-platlib=" + localdir,
134
+ "--install-scripts=" + localdir,
135
+ "--install-data=" + localdir,
136
+ "--install-headers=" + localdir]
137
+
138
+ CONFIGURE_INCLUDES = ["%s/ndtypes" % localdir, "%s/xnd" % localdir]
139
+ INCLUDES = LIBS = CONFIGURE_LIBS = CONFIGURE_INCLUDES
140
+ LIBGUMATHDIR = "%s/gumath" % localdir
141
+ INSTALL_LIBS = True
142
+
143
+ if sys.platform == "darwin": # homebrew bug
144
+ sys.argv.append("--prefix=")
145
+
146
+ if len(sys.argv) == 2:
147
+ if sys.argv[1] == 'module':
148
+ sys.argv[1] = 'build'
149
+ if sys.argv[1] == 'module_install' or sys.argv[1] == 'conda_install':
150
+ sys.argv[1] = 'install'
151
+ if sys.argv[1] == 'test':
152
+ module_path = get_module_path()
153
+ python_path = os.getenv('PYTHONPATH')
154
+ path = module_path + ':' + python_path if python_path else module_path
155
+ env = os.environ.copy()
156
+ env['PYTHONPATH'] = path
157
+ ret = subprocess.call([sys.executable, "python/test_gumath.py"], env=env)
158
+ sys.exit(ret)
159
+ elif sys.argv[1] == 'clean':
160
+ shutil.rmtree("build", ignore_errors=True)
161
+ os.chdir("python/gumath")
162
+ shutil.rmtree("__pycache__", ignore_errors=True)
163
+ for f in glob("_gumath*.so"):
164
+ os.remove(f)
165
+ sys.exit(0)
166
+ elif sys.argv[1] == 'distclean':
167
+ if sys.platform == "win32":
168
+ os.chdir("vcbuild")
169
+ os.system("vcdistclean.bat")
170
+ else:
171
+ os.system("make distclean")
172
+ sys.exit(0)
173
+ else:
174
+ pass
175
+
176
+
177
+ def gumath_extensions():
178
+ add_include_dirs = [".", "libgumath", "ndtypes/python/ndtypes", "xnd/python/xnd"] + INCLUDES
179
+ add_library_dirs = ["libgumath", "ndtypes/libndtypes", "xnd/libxnd"] + LIBS
180
+ add_depends = []
181
+
182
+ if sys.platform == "win32":
183
+ add_libraries = ["libndtypes-0.2.0dev3.dll", "libxnd-0.2.0dev3.dll", "libgumath-0.2.0dev3.dll"]
184
+ add_extra_compile_args = ["/DNDT_IMPORT", "/DXND_IMPORT", "/DGM_IMPORT"]
185
+ add_extra_link_args = []
186
+ add_runtime_library_dirs = []
187
+
188
+ if BUILD_ALL:
189
+ from distutils.msvc9compiler import MSVCCompiler
190
+ MSVCCompiler().initialize()
191
+ os.chdir("vcbuild")
192
+ os.environ['LIBNDTYPESINCLUDE'] = os.path.normpath(CONFIGURE_INCLUDES[0])
193
+ os.environ['LIBNDTYPESDIR'] = os.path.normpath(CONFIGURE_LIBS[0])
194
+ os.environ['LIBXNDINCLUDE'] = os.path.normpath(CONFIGURE_INCLUDES[1])
195
+ os.environ['LIBXNDDIR'] = os.path.normpath(CONFIGURE_LIBS[1])
196
+ if ARCH == "64bit":
197
+ os.system("vcbuild64.bat")
198
+ else:
199
+ os.system("vcbuild32.bat")
200
+ os.chdir("..")
201
+ else:
202
+ add_extra_compile_args = ["-Wextra", "-Wno-missing-field-initializers", "-std=c11"]
203
+ if sys.platform == "darwin":
204
+ add_libraries = ["ndtypes", "xnd", "gumath"]
205
+ add_extra_link_args = ["-Wl,-rpath,@loader_path"]
206
+ add_runtime_library_dirs = []
207
+ else:
208
+ add_libraries = [":%s" % LIBNDTYPES, ":%s" % LIBXND, ":%s" % LIBSHARED]
209
+ add_extra_link_args = []
210
+ add_runtime_library_dirs = ["$ORIGIN"]
211
+
212
+ if BUILD_ALL:
213
+ cflags = '"-I%s -I%s"' % tuple(CONFIGURE_INCLUDES)
214
+ ldflags = '"-L%s -L%s"' % tuple(CONFIGURE_LIBS)
215
+ os.system("./configure CFLAGS=%s LDFLAGS=%s && make" % (cflags, ldflags))
216
+
217
+ def gumath_ext():
218
+ sources = ["python/gumath/_gumath.c"]
219
+
220
+ return Extension (
221
+ "gumath._gumath",
222
+ include_dirs = add_include_dirs,
223
+ library_dirs = add_library_dirs,
224
+ depends = add_depends,
225
+ sources = sources,
226
+ libraries = add_libraries,
227
+ extra_compile_args = add_extra_compile_args,
228
+ extra_link_args = add_extra_link_args,
229
+ runtime_library_dirs = add_runtime_library_dirs
230
+ )
231
+
232
+ def functions_ext():
233
+ sources = ["python/gumath/functions.c"]
234
+
235
+ return Extension (
236
+ "gumath.functions",
237
+ include_dirs = add_include_dirs,
238
+ library_dirs = add_library_dirs,
239
+ depends = add_depends,
240
+ sources = sources,
241
+ libraries = add_libraries,
242
+ extra_compile_args = add_extra_compile_args,
243
+ extra_link_args = add_extra_link_args,
244
+ runtime_library_dirs = add_runtime_library_dirs
245
+ )
246
+
247
+ def examples_ext():
248
+ sources = ["python/gumath/examples.c"]
249
+
250
+ return Extension (
251
+ "gumath.examples",
252
+ include_dirs = add_include_dirs,
253
+ library_dirs = add_library_dirs,
254
+ depends = add_depends,
255
+ sources = sources,
256
+ libraries = add_libraries,
257
+ extra_compile_args = add_extra_compile_args,
258
+ extra_link_args = add_extra_link_args,
259
+ runtime_library_dirs = add_runtime_library_dirs
260
+ )
261
+
262
+ return [gumath_ext(), functions_ext(), examples_ext()]
263
+
264
+ setup (
265
+ name = "gumath",
266
+ version = "0.2.0dev3",
267
+ description = DESCRIPTION,
268
+ long_description = LONG_DESCRIPTION,
269
+ url = "https://github.com/plures/gumath",
270
+ author = 'Stefan Krah',
271
+ author_email = 'skrah@bytereef.org',
272
+ license = "BSD License",
273
+ keywords = ["gufuncs", "array computing", "vectorization"],
274
+ platforms = ["Many"],
275
+ classifiers = [
276
+ "Development Status :: 3 - Alpha",
277
+ "Intended Audience :: Developers",
278
+ "License :: OSI Approved :: BSD License",
279
+ "Programming Language :: C",
280
+ "Programming Language :: Python :: 3",
281
+ "Programming Language :: Python :: 3.6",
282
+ "Programming Language :: Python :: 3.7",
283
+ "Operating System :: OS Independent",
284
+ "Topic :: Scientific/Engineering :: Mathematics",
285
+ "Topic :: Software Development"
286
+ ],
287
+ install_requires = ["ndtypes == v0.2.0dev3", "xnd == v0.2.0dev3"],
288
+ package_dir = {"": "python"},
289
+ packages = ["gumath"],
290
+ package_data = {"gumath": ["libgumath*", "gumath.h", "pygumath.h"]
291
+ if INSTALL_LIBS else ["pygumath.h"]},
292
+ ext_modules = gumath_extensions(),
293
+ )
294
+
295
+ copy_ext()
296
+
297
+ if INSTALL_LIBS and sys.platform != "win32" and not "bdist_wheel" in sys.argv:
298
+ make_symlinks()
@@ -0,0 +1,36 @@
1
+
2
+
3
+ libgumath build instructions for Visual Studio
4
+ ==============================================
5
+
6
+
7
+ Requirements
8
+ ------------
9
+
10
+ - Visual Studio 2015 or later.
11
+
12
+
13
+ 64-bit build
14
+ ------------
15
+
16
+ # Set the build environment
17
+ vcvarsall.bat x64
18
+
19
+ # Build: If successful, the static library, the dynamic library, the
20
+ # common header file and two executables for running the unit tests
21
+ # should be in the dist64 directory.
22
+ vcbuild64.bat
23
+
24
+
25
+ 32-bit build
26
+ ------------
27
+
28
+ # Set the build environment
29
+ vcvarsall.bat x86
30
+
31
+ # Build: If successful, the static library, the dynamic library, the
32
+ # common header file and two executables for running the unit tests
33
+ # should be in the dist32 directory.
34
+ vcbuild32.bat
35
+
36
+
@@ -0,0 +1,21 @@
1
+ @ECHO off
2
+
3
+ if not exist dist32 mkdir dist32
4
+ if exist dist32\* del /q dist32\*
5
+
6
+ cd ..\libgumath
7
+ copy /y Makefile.vc Makefile
8
+
9
+ nmake /nologo clean
10
+ nmake /nologo
11
+
12
+ copy /y libgumath-0.2.0dev3.lib ..\vcbuild\dist32
13
+ copy /y libgumath-0.2.0dev3.dll ..\vcbuild\dist32
14
+ copy /y libgumath-0.2.0dev3.dll.lib ..\vcbuild\dist32
15
+ copy /y libgumath-0.2.0dev3.dll.exp ..\vcbuild\dist32
16
+ copy /y gumath.h ..\vcbuild\dist32
17
+
18
+ cd ..\vcbuild
19
+
20
+
21
+
@@ -0,0 +1,21 @@
1
+ @ECHO off
2
+
3
+ if not exist dist64 mkdir dist64
4
+ if exist dist64\* del /q dist64\*
5
+
6
+ cd ..\libgumath
7
+ copy /y Makefile.vc Makefile
8
+
9
+ nmake /nologo clean
10
+ nmake /nologo
11
+
12
+ copy /y libgumath-0.2.0dev3.lib ..\vcbuild\dist64
13
+ copy /y libgumath-0.2.0dev3.dll ..\vcbuild\dist64
14
+ copy /y libgumath-0.2.0dev3.dll.lib ..\vcbuild\dist64
15
+ copy /y libgumath-0.2.0dev3.dll.exp ..\vcbuild\dist64
16
+ copy /y gumath.h ..\vcbuild\dist64
17
+
18
+ cd ..\vcbuild
19
+
20
+
21
+
@@ -0,0 +1,10 @@
1
+ @ECHO off
2
+
3
+ cd ..\libgumath
4
+ if exist Makefile nmake /nologo clean
5
+
6
+ cd ..\vcbuild
7
+ if exist dist64 rd /q /s dist64
8
+ if exist dist32 rd /q /s dist32
9
+
10
+
@@ -0,0 +1,11 @@
1
+ @ECHO off
2
+
3
+ cd ..\libgumath
4
+ if exist Makefile nmake /nologo distclean
5
+
6
+ cd ..\vcbuild
7
+ if exist dist64 rd /q /s dist64
8
+ if exist dist32 rd /q /s dist32
9
+
10
+
11
+
@@ -0,0 +1,205 @@
1
+ /*
2
+ * BSD 3-Clause License
3
+ *
4
+ * Copyright (c) 2017-2018, plures
5
+ * All rights reserved.
6
+ *
7
+ * Redistribution and use in source and binary forms, with or without
8
+ * modification, are permitted provided that the following conditions are met:
9
+ *
10
+ * 1. Redistributions of source code must retain the above copyright notice,
11
+ * this list of conditions and the following disclaimer.
12
+ *
13
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
14
+ * this list of conditions and the following disclaimer in the documentation
15
+ * and/or other materials provided with the distribution.
16
+ *
17
+ * 3. Neither the name of the copyright holder nor the names of its
18
+ * contributors may be used to endorse or promote products derived from
19
+ * this software without specific prior written permission.
20
+ *
21
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
+ */
32
+
33
+
34
+ #ifndef GUMATH_H
35
+ #define GUMATH_H
36
+
37
+ #include "ndtypes.h"
38
+ #include "xnd.h"
39
+
40
+
41
+ #ifdef _MSC_VER
42
+ #if defined (GM_EXPORT)
43
+ #define GM_API __declspec(dllexport)
44
+ #elif defined(GM_IMPORT)
45
+ #define GM_API __declspec(dllimport)
46
+ #else
47
+ #define GM_API
48
+ #endif
49
+
50
+ #ifndef GM_UNUSED
51
+ #define GM_UNUSED
52
+ #endif
53
+
54
+ #include "malloc.h"
55
+ #define ALLOCA(type, name, nmemb) type *name = _alloca(nmemb * sizeof(type))
56
+ #else
57
+ #define GM_API
58
+ #if defined(__GNUC__) && !defined(__INTEL_COMPILER)
59
+ #define GM_UNUSED __attribute__((unused))
60
+ #else
61
+ #define GM_UNUSED
62
+ #endif
63
+
64
+ #define ALLOCA(type, name, nmemb) type name[nmemb]
65
+ #endif
66
+
67
+
68
+ #define GM_MAX_KERNELS 512
69
+
70
+ typedef float float32_t;
71
+ typedef double float64_t;
72
+
73
+
74
+ typedef int (* gm_xnd_kernel_t)(xnd_t stack[], ndt_context_t *ctx);
75
+ typedef int (* gm_strided_kernel_t)(char **args, intptr_t *dimensions, intptr_t *steps, void *data);
76
+
77
+ /* Collection of specialized kernels for a single function signature. */
78
+ typedef struct {
79
+ ndt_t *sig;
80
+ const ndt_constraint_t *constraint;
81
+
82
+ /* Xnd signatures */
83
+ gm_xnd_kernel_t Opt; /* dispatch ensures elementwise, at least 1D, contiguous in last dimensions */
84
+ gm_xnd_kernel_t C; /* dispatch ensures c-contiguous in inner dimensions */
85
+ gm_xnd_kernel_t Fortran; /* dispatch ensures f-contiguous in inner dimensions */
86
+ gm_xnd_kernel_t Xnd; /* selected if non-contiguous or the other fields are NULL */
87
+
88
+ /* NumPy signature */
89
+ gm_strided_kernel_t Strided;
90
+ } gm_kernel_set_t;
91
+
92
+ typedef struct {
93
+ const char *name;
94
+ const char *type;
95
+ const ndt_methods_t *meth;
96
+ } gm_typedef_init_t;
97
+
98
+ typedef struct {
99
+ const char *name;
100
+ const char *sig;
101
+ const ndt_constraint_t *constraint;
102
+
103
+ gm_xnd_kernel_t Opt;
104
+ gm_xnd_kernel_t C;
105
+ gm_xnd_kernel_t Fortran;
106
+ gm_xnd_kernel_t Xnd;
107
+ gm_strided_kernel_t Strided;
108
+ } gm_kernel_init_t;
109
+
110
+ /* Actual kernel selected for application */
111
+ typedef struct {
112
+ uint32_t flag;
113
+ const gm_kernel_set_t *set;
114
+ } gm_kernel_t;
115
+
116
+ /* Multimethod with associated kernels */
117
+ typedef struct gm_func gm_func_t;
118
+ typedef const gm_kernel_set_t *(*gm_typecheck_t)(ndt_apply_spec_t *spec, const gm_func_t *f, const ndt_t *in[], int nin, ndt_context_t *ctx);
119
+ struct gm_func {
120
+ char *name;
121
+ gm_typecheck_t typecheck; /* Experimental optimized type-checking, may be NULL. */
122
+ int nkernels;
123
+ gm_kernel_set_t kernels[GM_MAX_KERNELS];
124
+ };
125
+
126
+
127
+ typedef struct _gm_tbl gm_tbl_t;
128
+
129
+
130
+ /******************************************************************************/
131
+ /* Functions */
132
+ /******************************************************************************/
133
+
134
+ GM_API gm_func_t *gm_func_new(const char *name, ndt_context_t *ctx);
135
+ GM_API void gm_func_del(gm_func_t *f);
136
+
137
+ GM_API gm_func_t *gm_add_func(gm_tbl_t *tbl, const char *name, ndt_context_t *ctx);
138
+ GM_API int gm_add_kernel(gm_tbl_t *tbl, const gm_kernel_init_t *kernel, ndt_context_t *ctx);
139
+ GM_API int gm_add_kernel_typecheck(gm_tbl_t *tbl, const gm_kernel_init_t *kernel, ndt_context_t *ctx, gm_typecheck_t f);
140
+
141
+ GM_API gm_kernel_t gm_select(ndt_apply_spec_t *spec, const gm_tbl_t *tbl, const char *name,
142
+ const ndt_t *in_types[], int nin, const xnd_t args[],
143
+ ndt_context_t *ctx);
144
+ GM_API int gm_apply(const gm_kernel_t *kernel, xnd_t stack[], int outer_dims, ndt_context_t *ctx);
145
+ GM_API int gm_apply_thread(const gm_kernel_t *kernel, xnd_t stack[], int outer_dims, uint32_t flags, const int64_t nthreads, ndt_context_t *ctx);
146
+
147
+
148
+ /******************************************************************************/
149
+ /* NumPy loops */
150
+ /******************************************************************************/
151
+
152
+ GM_API int gm_np_flatten(char **args, const int nargs,
153
+ int64_t *dimensions, int64_t *strides, const xnd_t stack[],
154
+ ndt_context_t *ctx);
155
+
156
+ GM_API int gm_np_convert_xnd(char **args, const int nargs,
157
+ intptr_t *dimensions, const int dims_size,
158
+ intptr_t *steps, const int steps_size,
159
+ xnd_t stack[], const int outer_dims,
160
+ ndt_context_t *ctx);
161
+
162
+ GM_API int gm_np_map(const gm_strided_kernel_t f,
163
+ char **args, int nargs,
164
+ intptr_t *dimensions,
165
+ intptr_t *steps,
166
+ void *data,
167
+ int outer_dims);
168
+
169
+
170
+ /******************************************************************************/
171
+ /* Xnd loops */
172
+ /******************************************************************************/
173
+
174
+ GM_API int gm_xnd_map(const gm_xnd_kernel_t f, xnd_t stack[], const int nargs,
175
+ const int outer_dims, ndt_context_t *ctx);
176
+
177
+
178
+ /******************************************************************************/
179
+ /* Gufunc table */
180
+ /******************************************************************************/
181
+ GM_API gm_tbl_t *gm_tbl_new(ndt_context_t *ctx);
182
+ GM_API void gm_tbl_del(gm_tbl_t *t);
183
+
184
+ GM_API int gm_tbl_add(gm_tbl_t *tbl, const char *key, gm_func_t *value, ndt_context_t *ctx);
185
+ GM_API gm_func_t *gm_tbl_find(const gm_tbl_t *tbl, const char *key, ndt_context_t *ctx);
186
+ GM_API int gm_tbl_map(const gm_tbl_t *tbl, int (*f)(const gm_func_t *, void *state), void *state);
187
+
188
+
189
+ /******************************************************************************/
190
+ /* Library initialization and tables */
191
+ /******************************************************************************/
192
+
193
+ GM_API void gm_init(void);
194
+ GM_API int gm_init_unary_kernels(gm_tbl_t *tbl, ndt_context_t *ctx);
195
+ GM_API int gm_init_binary_kernels(gm_tbl_t *tbl, ndt_context_t *ctx);
196
+ GM_API int gm_init_example_kernels(gm_tbl_t *tbl, ndt_context_t *ctx);
197
+ GM_API int gm_init_bfloat16_kernels(gm_tbl_t *tbl, ndt_context_t *ctx);
198
+ GM_API int gm_init_graph_kernels(gm_tbl_t *tbl, ndt_context_t *ctx);
199
+ GM_API int gm_init_quaternion_kernels(gm_tbl_t *tbl, ndt_context_t *ctx);
200
+ GM_API int gm_init_pdist_kernels(gm_tbl_t *tbl, ndt_context_t *ctx);
201
+
202
+ GM_API void gm_finalize(void);
203
+
204
+
205
+ #endif /* GUMATH_H */