mapFolding 0.3.1__tar.gz → 0.3.3__tar.gz
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.
- {mapfolding-0.3.1 → mapfolding-0.3.3}/PKG-INFO +2 -2
- {mapfolding-0.3.1 → mapfolding-0.3.3}/README.md +1 -1
- {mapfolding-0.3.1 → mapfolding-0.3.3}/mapFolding/mapFolding.egg-info/PKG-INFO +2 -2
- {mapfolding-0.3.1 → mapfolding-0.3.3}/mapFolding/mapFolding.egg-info/SOURCES.txt +6 -3
- {mapfolding-0.3.1 → mapfolding-0.3.3}/mapFolding/mapFolding.egg-info/top_level.txt +1 -0
- mapfolding-0.3.3/mapFolding/someAssemblyRequired/__init__.py +2 -0
- mapfolding-0.3.3/mapFolding/someAssemblyRequired/generalizeSourceCode.py +121 -0
- mapfolding-0.3.3/mapFolding/someAssemblyRequired/makeJob.py +19 -0
- {mapfolding-0.3.1 → mapfolding-0.3.3}/mapFolding/someAssemblyRequired/synthesizeJob.py +35 -35
- mapfolding-0.3.3/mapFolding/someAssemblyRequired/synthesizeModules.py +78 -0
- {mapfolding-0.3.1/mapFolding/someAssemblyRequired → mapfolding-0.3.3/mapFolding/syntheticModules}/countInitialize.py +0 -1
- {mapfolding-0.3.1/mapFolding/someAssemblyRequired → mapfolding-0.3.3/mapFolding/syntheticModules}/countParallel.py +0 -1
- {mapfolding-0.3.1/mapFolding/someAssemblyRequired → mapfolding-0.3.3/mapFolding/syntheticModules}/countSequential.py +0 -1
- {mapfolding-0.3.1 → mapfolding-0.3.3}/pyproject.toml +2 -1
- {mapfolding-0.3.1 → mapfolding-0.3.3}/tests/test_oeis.py +7 -1
- {mapfolding-0.3.1 → mapfolding-0.3.3}/tests/test_other.py +6 -6
- {mapfolding-0.3.1 → mapfolding-0.3.3}/tests/test_tasks.py +9 -6
- mapfolding-0.3.1/mapFolding/someAssemblyRequired/synthesizeModules.py +0 -187
- {mapfolding-0.3.1 → mapfolding-0.3.3}/mapFolding/benchmarks/benchmarking.py +0 -0
- {mapfolding-0.3.1 → mapfolding-0.3.3}/mapFolding/mapFolding.egg-info/dependency_links.txt +0 -0
- {mapfolding-0.3.1 → mapfolding-0.3.3}/mapFolding/mapFolding.egg-info/entry_points.txt +0 -0
- {mapfolding-0.3.1 → mapfolding-0.3.3}/mapFolding/mapFolding.egg-info/requires.txt +0 -0
- {mapfolding-0.3.1 → mapfolding-0.3.3}/mapFolding/reference/flattened.py +0 -0
- {mapfolding-0.3.1 → mapfolding-0.3.3}/mapFolding/reference/hunterNumba.py +0 -0
- {mapfolding-0.3.1 → mapfolding-0.3.3}/mapFolding/reference/irvineJavaPort.py +0 -0
- {mapfolding-0.3.1 → mapfolding-0.3.3}/mapFolding/reference/jax.py +0 -0
- {mapfolding-0.3.1 → mapfolding-0.3.3}/mapFolding/reference/lunnan.py +0 -0
- {mapfolding-0.3.1 → mapfolding-0.3.3}/mapFolding/reference/lunnanNumpy.py +0 -0
- {mapfolding-0.3.1 → mapfolding-0.3.3}/mapFolding/reference/lunnanWhile.py +0 -0
- {mapfolding-0.3.1 → mapfolding-0.3.3}/mapFolding/reference/rotatedEntryPoint.py +0 -0
- {mapfolding-0.3.1 → mapfolding-0.3.3}/mapFolding/reference/total_countPlus1vsPlusN.py +0 -0
- {mapfolding-0.3.1 → mapfolding-0.3.3}/mapFolding/someAssemblyRequired/getLLVMforNoReason.py +0 -0
- {mapfolding-0.3.1/mapFolding/someAssemblyRequired → mapfolding-0.3.3/mapFolding/syntheticModules}/__init__.py +0 -0
- {mapfolding-0.3.1 → mapfolding-0.3.3}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.2
|
|
2
2
|
Name: mapFolding
|
|
3
|
-
Version: 0.3.
|
|
3
|
+
Version: 0.3.3
|
|
4
4
|
Summary: Count distinct ways to fold a map (or a strip of stamps)
|
|
5
5
|
Author-email: Hunter Hogan <HunterHogan@pm.me>
|
|
6
6
|
License: CC-BY-NC-4.0
|
|
@@ -52,7 +52,7 @@ The directory [mapFolding/reference](https://github.com/hunterhogan/mapFolding/b
|
|
|
52
52
|
- [hunterNumba.py](https://github.com/hunterhogan/mapFolding/blob/main/mapFolding/reference), a one-size-fits-all, self-contained, reasonably fast, contemporary algorithm that is nevertheless infected by _noobaceae ignorancium_, and
|
|
53
53
|
- miscellaneous notes.
|
|
54
54
|
|
|
55
|
-
[](https://pypi.org/project/mapFolding/) [](https://pypi.org/project/mapFolding/) [](https://github.com/hunterhogan/mapFolding/actions/workflows/pythonTests.yml) [](https://youtu.be/g6f_miE91mk&t=4)   
|
|
56
56
|
|
|
57
57
|
## Simple, easy usage based on OEIS IDs
|
|
58
58
|
|
|
@@ -14,7 +14,7 @@ The directory [mapFolding/reference](https://github.com/hunterhogan/mapFolding/b
|
|
|
14
14
|
- [hunterNumba.py](https://github.com/hunterhogan/mapFolding/blob/main/mapFolding/reference), a one-size-fits-all, self-contained, reasonably fast, contemporary algorithm that is nevertheless infected by _noobaceae ignorancium_, and
|
|
15
15
|
- miscellaneous notes.
|
|
16
16
|
|
|
17
|
-
[](https://pypi.org/project/mapFolding/) [](https://pypi.org/project/mapFolding/) [](https://github.com/hunterhogan/mapFolding/actions/workflows/pythonTests.yml) [](https://youtu.be/g6f_miE91mk&t=4)   
|
|
18
18
|
|
|
19
19
|
## Simple, easy usage based on OEIS IDs
|
|
20
20
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.2
|
|
2
2
|
Name: mapFolding
|
|
3
|
-
Version: 0.3.
|
|
3
|
+
Version: 0.3.3
|
|
4
4
|
Summary: Count distinct ways to fold a map (or a strip of stamps)
|
|
5
5
|
Author-email: Hunter Hogan <HunterHogan@pm.me>
|
|
6
6
|
License: CC-BY-NC-4.0
|
|
@@ -52,7 +52,7 @@ The directory [mapFolding/reference](https://github.com/hunterhogan/mapFolding/b
|
|
|
52
52
|
- [hunterNumba.py](https://github.com/hunterhogan/mapFolding/blob/main/mapFolding/reference), a one-size-fits-all, self-contained, reasonably fast, contemporary algorithm that is nevertheless infected by _noobaceae ignorancium_, and
|
|
53
53
|
- miscellaneous notes.
|
|
54
54
|
|
|
55
|
-
[](https://pypi.org/project/mapFolding/) [](https://pypi.org/project/mapFolding/) [](https://github.com/hunterhogan/mapFolding/actions/workflows/pythonTests.yml) [](https://youtu.be/g6f_miE91mk&t=4)   
|
|
56
56
|
|
|
57
57
|
## Simple, easy usage based on OEIS IDs
|
|
58
58
|
|
|
@@ -17,12 +17,15 @@ mapFolding/reference/lunnanWhile.py
|
|
|
17
17
|
mapFolding/reference/rotatedEntryPoint.py
|
|
18
18
|
mapFolding/reference/total_countPlus1vsPlusN.py
|
|
19
19
|
mapFolding/someAssemblyRequired/__init__.py
|
|
20
|
-
mapFolding/someAssemblyRequired/
|
|
21
|
-
mapFolding/someAssemblyRequired/countParallel.py
|
|
22
|
-
mapFolding/someAssemblyRequired/countSequential.py
|
|
20
|
+
mapFolding/someAssemblyRequired/generalizeSourceCode.py
|
|
23
21
|
mapFolding/someAssemblyRequired/getLLVMforNoReason.py
|
|
22
|
+
mapFolding/someAssemblyRequired/makeJob.py
|
|
24
23
|
mapFolding/someAssemblyRequired/synthesizeJob.py
|
|
25
24
|
mapFolding/someAssemblyRequired/synthesizeModules.py
|
|
25
|
+
mapFolding/syntheticModules/__init__.py
|
|
26
|
+
mapFolding/syntheticModules/countInitialize.py
|
|
27
|
+
mapFolding/syntheticModules/countParallel.py
|
|
28
|
+
mapFolding/syntheticModules/countSequential.py
|
|
26
29
|
tests/test_oeis.py
|
|
27
30
|
tests/test_other.py
|
|
28
31
|
tests/test_tasks.py
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
from mapFolding import datatypeLargeDEFAULT, datatypeMediumDEFAULT, datatypeSmallDEFAULT
|
|
2
|
+
import ast
|
|
3
|
+
|
|
4
|
+
class RecursiveInlinerWithEnum(ast.NodeTransformer):
|
|
5
|
+
"""Process AST nodes to inline functions and substitute enum values.
|
|
6
|
+
Also handles function decorators during inlining."""
|
|
7
|
+
|
|
8
|
+
def __init__(self, dictionaryFunctions, dictionaryEnumValues):
|
|
9
|
+
self.dictionaryFunctions = dictionaryFunctions
|
|
10
|
+
self.dictionaryEnumValues = dictionaryEnumValues
|
|
11
|
+
self.processed = set()
|
|
12
|
+
|
|
13
|
+
def inlineFunctionBody(self, functionName):
|
|
14
|
+
if functionName in self.processed:
|
|
15
|
+
return None
|
|
16
|
+
|
|
17
|
+
self.processed.add(functionName)
|
|
18
|
+
inlineDefinition = self.dictionaryFunctions[functionName]
|
|
19
|
+
# Recursively process the function body
|
|
20
|
+
for node in ast.walk(inlineDefinition):
|
|
21
|
+
self.visit(node)
|
|
22
|
+
return inlineDefinition
|
|
23
|
+
|
|
24
|
+
def visit_Attribute(self, node):
|
|
25
|
+
# Substitute enum identifiers (e.g., indexMy.leaf1ndex.value)
|
|
26
|
+
if isinstance(node.value, ast.Attribute) and isinstance(node.value.value, ast.Name):
|
|
27
|
+
enumPath = f"{node.value.value.id}.{node.value.attr}.{node.attr}"
|
|
28
|
+
if enumPath in self.dictionaryEnumValues:
|
|
29
|
+
return ast.Constant(value=self.dictionaryEnumValues[enumPath])
|
|
30
|
+
return self.generic_visit(node)
|
|
31
|
+
|
|
32
|
+
def visit_Call(self, node):
|
|
33
|
+
callNode = self.generic_visit(node)
|
|
34
|
+
if isinstance(callNode, ast.Call) and isinstance(callNode.func, ast.Name) and callNode.func.id in self.dictionaryFunctions:
|
|
35
|
+
inlineDefinition = self.inlineFunctionBody(callNode.func.id)
|
|
36
|
+
if (inlineDefinition and inlineDefinition.body):
|
|
37
|
+
lastStmt = inlineDefinition.body[-1]
|
|
38
|
+
if isinstance(lastStmt, ast.Return) and lastStmt.value is not None:
|
|
39
|
+
return self.visit(lastStmt.value)
|
|
40
|
+
elif isinstance(lastStmt, ast.Expr) and lastStmt.value is not None:
|
|
41
|
+
return self.visit(lastStmt.value)
|
|
42
|
+
return None
|
|
43
|
+
return callNode
|
|
44
|
+
|
|
45
|
+
def visit_Expr(self, node):
|
|
46
|
+
if isinstance(node.value, ast.Call):
|
|
47
|
+
if isinstance(node.value.func, ast.Name) and node.value.func.id in self.dictionaryFunctions:
|
|
48
|
+
inlineDefinition = self.inlineFunctionBody(node.value.func.id)
|
|
49
|
+
if inlineDefinition:
|
|
50
|
+
return [self.visit(stmt) for stmt in inlineDefinition.body]
|
|
51
|
+
return self.generic_visit(node)
|
|
52
|
+
|
|
53
|
+
def findRequiredImports(node):
|
|
54
|
+
"""Find all modules that need to be imported based on AST analysis.
|
|
55
|
+
NOTE: due to hardcoding, this is a glorified regex. No, wait, this is less versatile than regex."""
|
|
56
|
+
requiredImports = set()
|
|
57
|
+
|
|
58
|
+
class ImportFinder(ast.NodeVisitor):
|
|
59
|
+
def visit_Name(self, node):
|
|
60
|
+
if node.id in {'numba'}:
|
|
61
|
+
requiredImports.add(node.id)
|
|
62
|
+
self.generic_visit(node)
|
|
63
|
+
|
|
64
|
+
def visitDecorator(self, node):
|
|
65
|
+
if isinstance(node, ast.Call) and isinstance(node.func, ast.Name):
|
|
66
|
+
if node.func.id == 'jit':
|
|
67
|
+
requiredImports.add('numba')
|
|
68
|
+
self.generic_visit(node)
|
|
69
|
+
|
|
70
|
+
ImportFinder().visit(node)
|
|
71
|
+
return requiredImports
|
|
72
|
+
|
|
73
|
+
def generateImports(requiredImports):
|
|
74
|
+
"""Generate import statements based on required modules."""
|
|
75
|
+
importStatements = {'import numba', 'from mapFolding import indexMy, indexTrack'}
|
|
76
|
+
|
|
77
|
+
importMapping = {
|
|
78
|
+
'numba': 'import numba',
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
for moduleName in sorted(requiredImports):
|
|
82
|
+
if moduleName in importMapping:
|
|
83
|
+
importStatements.add(importMapping[moduleName])
|
|
84
|
+
|
|
85
|
+
return '\n'.join(importStatements)
|
|
86
|
+
|
|
87
|
+
def makeInlineFunction(sourceCode, targetFunctionName, dictionaryEnumValues, skipEnum=False, **keywordArguments):
|
|
88
|
+
datatypeLarge = keywordArguments.get('datatypeLarge', datatypeLargeDEFAULT)
|
|
89
|
+
datatypeMedium = keywordArguments.get('datatypeMedium', datatypeMediumDEFAULT)
|
|
90
|
+
datatypeSmall = keywordArguments.get('datatypeSmall', datatypeSmallDEFAULT)
|
|
91
|
+
if skipEnum:
|
|
92
|
+
dictionaryEnumValues = {}
|
|
93
|
+
dictionaryParsed = ast.parse(sourceCode)
|
|
94
|
+
dictionaryFunctions = {
|
|
95
|
+
element.name: element
|
|
96
|
+
for element in dictionaryParsed.body
|
|
97
|
+
if isinstance(element, ast.FunctionDef)
|
|
98
|
+
}
|
|
99
|
+
nodeTarget = dictionaryFunctions[targetFunctionName]
|
|
100
|
+
nodeInliner = RecursiveInlinerWithEnum(dictionaryFunctions, dictionaryEnumValues)
|
|
101
|
+
nodeInlined = nodeInliner.visit(nodeTarget)
|
|
102
|
+
ast.fix_missing_locations(nodeInlined)
|
|
103
|
+
callableInlinedDecorators = [decorator for decorator in nodeInlined.decorator_list]
|
|
104
|
+
|
|
105
|
+
requiredImports = findRequiredImports(nodeInlined)
|
|
106
|
+
importStatements = generateImports(requiredImports)
|
|
107
|
+
importsRequired = importStatements
|
|
108
|
+
dictionaryDecoratorsNumba={
|
|
109
|
+
'countInitialize':
|
|
110
|
+
f'@numba.jit((numba.{datatypeSmall}[:,:,::1], numba.{datatypeMedium}[::1], numba.{datatypeSmall}[::1], numba.{datatypeMedium}[:,::1]), parallel=False, boundscheck=False, cache=True, error_model="numpy", fastmath=True, looplift=False, nogil=True, nopython=True)\n',
|
|
111
|
+
'countParallel':
|
|
112
|
+
f'@numba.jit((numba.{datatypeSmall}[:,:,::1], numba.{datatypeLarge}[::1], numba.{datatypeMedium}[::1], numba.{datatypeSmall}[::1], numba.{datatypeMedium}[:,::1]), parallel=True, boundscheck=False, cache=True, error_model="numpy", fastmath=True, looplift=False, nogil=True, nopython=True)\n',
|
|
113
|
+
'countSequential':
|
|
114
|
+
f'@numba.jit((numba.{datatypeSmall}[:,:,::1], numba.{datatypeLarge}[::1], numba.{datatypeMedium}[::1], numba.{datatypeSmall}[::1], numba.{datatypeMedium}[:,::1]), parallel=False, boundscheck=False, cache=True, error_model="numpy", fastmath=True, looplift=False, nogil=True, nopython=True)\n',
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
lineNumbaDecorator = dictionaryDecoratorsNumba[targetFunctionName]
|
|
118
|
+
|
|
119
|
+
# inlinedCode = ast.unparse(ast.Module(body=[nodeInlined], type_ignores=[]))
|
|
120
|
+
callableInlined = lineNumbaDecorator + ast.unparse(nodeInlined)
|
|
121
|
+
return (callableInlined, callableInlinedDecorators, importsRequired)
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
from mapFolding import getPathFilenameFoldsTotal
|
|
2
|
+
from mapFolding import outfitCountFolds
|
|
3
|
+
from typing import Any, Optional, Sequence, Type
|
|
4
|
+
import pathlib
|
|
5
|
+
import pickle
|
|
6
|
+
|
|
7
|
+
def makeStateJob(listDimensions: Sequence[int], **keywordArguments: Optional[Type[Any]]) -> pathlib.Path:
|
|
8
|
+
from syntheticModules import countInitialize
|
|
9
|
+
stateUniversal = outfitCountFolds(listDimensions, computationDivisions=None, CPUlimit=None, **keywordArguments)
|
|
10
|
+
countInitialize(stateUniversal['connectionGraph'], stateUniversal['gapsWhere'], stateUniversal['my'], stateUniversal['track'])
|
|
11
|
+
|
|
12
|
+
pathFilenameChopChop = getPathFilenameFoldsTotal(stateUniversal['mapShape'])
|
|
13
|
+
suffix = pathFilenameChopChop.suffix
|
|
14
|
+
pathJob = pathlib.Path(str(pathFilenameChopChop)[0:-len(suffix)])
|
|
15
|
+
pathJob.mkdir(parents=True, exist_ok=True)
|
|
16
|
+
pathFilenameJob = pathJob / 'stateJob.pkl'
|
|
17
|
+
|
|
18
|
+
pathFilenameJob.write_bytes(pickle.dumps(stateUniversal))
|
|
19
|
+
return pathFilenameJob
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
from mapFolding import getPathFilenameFoldsTotal
|
|
2
|
-
from mapFolding import make_dtype,
|
|
3
|
-
from mapFolding import
|
|
4
|
-
from someAssemblyRequired import
|
|
5
|
-
from typing import
|
|
1
|
+
from mapFolding import getPathFilenameFoldsTotal
|
|
2
|
+
from mapFolding import make_dtype, datatypeLargeDEFAULT, datatypeMediumDEFAULT, datatypeSmallDEFAULT, datatypeModuleDEFAULT
|
|
3
|
+
from mapFolding import computationState
|
|
4
|
+
from someAssemblyRequired import makeStateJob
|
|
5
|
+
from typing import Optional
|
|
6
6
|
import more_itertools
|
|
7
7
|
import inspect
|
|
8
|
+
import importlib
|
|
9
|
+
import importlib.util
|
|
8
10
|
import numpy
|
|
9
11
|
import pathlib
|
|
10
12
|
import pickle
|
|
@@ -12,20 +14,7 @@ import python_minifier
|
|
|
12
14
|
|
|
13
15
|
identifierCallableLaunch = "goGoGadgetAbsurdity"
|
|
14
16
|
|
|
15
|
-
def
|
|
16
|
-
stateUniversal = outfitCountFolds(listDimensions, computationDivisions=None, CPUlimit=None, **keywordArguments)
|
|
17
|
-
countInitialize(stateUniversal['connectionGraph'], stateUniversal['gapsWhere'], stateUniversal['my'], stateUniversal['track'])
|
|
18
|
-
|
|
19
|
-
pathFilenameChopChop = getPathFilenameFoldsTotal(stateUniversal['mapShape'])
|
|
20
|
-
suffix = pathFilenameChopChop.suffix
|
|
21
|
-
pathJob = pathlib.Path(str(pathFilenameChopChop)[0:-len(suffix)])
|
|
22
|
-
pathJob.mkdir(parents=True, exist_ok=True)
|
|
23
|
-
pathFilenameJob = pathJob / 'stateJob.pkl'
|
|
24
|
-
|
|
25
|
-
pathFilenameJob.write_bytes(pickle.dumps(stateUniversal))
|
|
26
|
-
return pathFilenameJob
|
|
27
|
-
|
|
28
|
-
def convertNDArrayToStr(arrayTarget: numpy.ndarray, identifierName: str) -> str:
|
|
17
|
+
def makeStrRLEcompacted(arrayTarget: numpy.ndarray, identifierName: str) -> str:
|
|
29
18
|
def process_nested_array(arraySlice):
|
|
30
19
|
if isinstance(arraySlice, numpy.ndarray) and arraySlice.ndim > 1:
|
|
31
20
|
return [process_nested_array(arraySlice[index]) for index in range(arraySlice.shape[0])]
|
|
@@ -54,22 +43,25 @@ def convertNDArrayToStr(arrayTarget: numpy.ndarray, identifierName: str) -> str:
|
|
|
54
43
|
return f"{identifierName} = numpy.array({stringMinimized}, dtype=numpy.{arrayTarget.dtype})"
|
|
55
44
|
|
|
56
45
|
def writeModuleWithNumba(listDimensions, **keywordArguments: Optional[str]) -> pathlib.Path:
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
46
|
+
datatypeLarge = keywordArguments.get('datatypeLarge', datatypeLargeDEFAULT)
|
|
47
|
+
datatypeMedium = keywordArguments.get('datatypeMedium', datatypeMediumDEFAULT)
|
|
48
|
+
datatypeSmall = keywordArguments.get('datatypeSmall', datatypeSmallDEFAULT)
|
|
49
|
+
datatypeModule = keywordArguments.get('datatypeModule', datatypeModuleDEFAULT)
|
|
60
50
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
51
|
+
dtypeLarge = make_dtype(datatypeLarge, datatypeModule) # type: ignore
|
|
52
|
+
dtypeMedium = make_dtype(datatypeMedium, datatypeModule) # type: ignore
|
|
53
|
+
dtypeSmall = make_dtype(datatypeSmall, datatypeModule) # type: ignore
|
|
64
54
|
|
|
65
|
-
pathFilenameJob = makeStateJob(listDimensions, dtypeLarge =
|
|
55
|
+
pathFilenameJob = makeStateJob(listDimensions, dtypeLarge = dtypeLarge, dtypeMedium = dtypeMedium, dtypeSmall = dtypeSmall)
|
|
66
56
|
stateJob: computationState = pickle.loads(pathFilenameJob.read_bytes())
|
|
67
57
|
pathFilenameFoldsTotal = getPathFilenameFoldsTotal(stateJob['mapShape'], pathFilenameJob.parent)
|
|
68
58
|
|
|
69
|
-
|
|
59
|
+
from syntheticModules import countSequential
|
|
60
|
+
algorithmSource = countSequential
|
|
61
|
+
codeSource = inspect.getsource(algorithmSource)
|
|
70
62
|
|
|
71
63
|
# forceinline=True might actually be useful
|
|
72
|
-
parametersNumba = f"numba.types.{
|
|
64
|
+
parametersNumba = f"numba.types.{datatypeLarge}(), \
|
|
73
65
|
cache=True, \
|
|
74
66
|
nopython=True, \
|
|
75
67
|
fastmath=True, \
|
|
@@ -96,14 +88,14 @@ no_cpython_wrapper=False, \
|
|
|
96
88
|
ImaIndent = ' '
|
|
97
89
|
linesDataDynamic = """"""
|
|
98
90
|
linesDataDynamic = "\n".join([linesDataDynamic
|
|
99
|
-
, ImaIndent + f"foldsTotal = numba.types.{
|
|
100
|
-
, ImaIndent +
|
|
101
|
-
, ImaIndent +
|
|
91
|
+
, ImaIndent + f"foldsTotal = numba.types.{datatypeLarge}(0)"
|
|
92
|
+
, ImaIndent + makeStrRLEcompacted(stateJob['foldGroups'], 'foldGroups')
|
|
93
|
+
, ImaIndent + makeStrRLEcompacted(stateJob['gapsWhere'], 'gapsWhere')
|
|
102
94
|
])
|
|
103
95
|
|
|
104
96
|
linesDataStatic = """"""
|
|
105
97
|
linesDataStatic = "\n".join([linesDataStatic
|
|
106
|
-
, ImaIndent +
|
|
98
|
+
, ImaIndent + makeStrRLEcompacted(stateJob['connectionGraph'], 'connectionGraph')
|
|
107
99
|
])
|
|
108
100
|
|
|
109
101
|
my = stateJob['my']
|
|
@@ -127,7 +119,7 @@ no_cpython_wrapper=False, \
|
|
|
127
119
|
elif 'track[indexTrack.' in lineSource:
|
|
128
120
|
# leafAbove = track[indexTrack.leafAbove.value]
|
|
129
121
|
identifier, statement = lineSource.split('=')
|
|
130
|
-
lineSource = ImaIndent +
|
|
122
|
+
lineSource = ImaIndent + makeStrRLEcompacted(eval(statement.strip()), identifier.strip())
|
|
131
123
|
|
|
132
124
|
linesAlgorithm = "\n".join([linesAlgorithm
|
|
133
125
|
, lineSource
|
|
@@ -163,8 +155,16 @@ if __name__ == '__main__':
|
|
|
163
155
|
return pathFilenameDestination
|
|
164
156
|
|
|
165
157
|
if __name__ == '__main__':
|
|
166
|
-
listDimensions = [
|
|
158
|
+
listDimensions = [6,6]
|
|
167
159
|
datatypeLarge = 'int64'
|
|
168
160
|
datatypeMedium = 'uint8'
|
|
169
161
|
datatypeSmall = datatypeMedium
|
|
170
|
-
writeModuleWithNumba(listDimensions, datatypeLarge=datatypeLarge, datatypeMedium=datatypeMedium, datatypeSmall=datatypeSmall)
|
|
162
|
+
pathFilenameModule = writeModuleWithNumba(listDimensions, datatypeLarge=datatypeLarge, datatypeMedium=datatypeMedium, datatypeSmall=datatypeSmall)
|
|
163
|
+
# Induce numba.jit compilation
|
|
164
|
+
moduleSpec = importlib.util.spec_from_file_location(pathFilenameModule.stem, pathFilenameModule)
|
|
165
|
+
if moduleSpec is None:
|
|
166
|
+
raise ImportError(f"Could not load module specification from {pathFilenameModule}")
|
|
167
|
+
module = importlib.util.module_from_spec(moduleSpec)
|
|
168
|
+
if moduleSpec.loader is None:
|
|
169
|
+
raise ImportError(f"Could not load module from {moduleSpec}")
|
|
170
|
+
moduleSpec.loader.exec_module(module)
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
from mapFolding import indexMy, indexTrack, getAlgorithmSource
|
|
2
|
+
from mapFolding import datatypeLargeDEFAULT, datatypeMediumDEFAULT, datatypeSmallDEFAULT
|
|
3
|
+
from someAssemblyRequired import makeInlineFunction
|
|
4
|
+
import pathlib
|
|
5
|
+
import inspect
|
|
6
|
+
|
|
7
|
+
algorithmSource = getAlgorithmSource()
|
|
8
|
+
|
|
9
|
+
def getDictionaryEnumValues():
|
|
10
|
+
dictionaryEnumValues = {}
|
|
11
|
+
for enumIndex in [indexMy, indexTrack]:
|
|
12
|
+
for memberName, memberValue in enumIndex._member_map_.items():
|
|
13
|
+
dictionaryEnumValues[f"{enumIndex.__name__}.{memberName}.value"] = memberValue.value
|
|
14
|
+
return dictionaryEnumValues
|
|
15
|
+
|
|
16
|
+
def unpackArrays(codeInlined: str, callableTarget: str) -> str:
|
|
17
|
+
dictionaryReplaceScalars = {
|
|
18
|
+
'my[indexMy.dimensionsTotal.value]': 'dimensionsTotal',
|
|
19
|
+
'my[indexMy.dimensionsUnconstrained.value]': 'dimensionsUnconstrained',
|
|
20
|
+
'my[indexMy.gap1ndex.value]': 'gap1ndex',
|
|
21
|
+
'my[indexMy.gap1ndexCeiling.value]': 'gap1ndexCeiling',
|
|
22
|
+
'my[indexMy.indexDimension.value]': 'indexDimension',
|
|
23
|
+
# 'my[indexMy.indexLeaf.value]': 'indexLeaf',
|
|
24
|
+
'my[indexMy.indexMiniGap.value]': 'indexMiniGap',
|
|
25
|
+
'my[indexMy.leaf1ndex.value]': 'leaf1ndex',
|
|
26
|
+
'my[indexMy.leafConnectee.value]': 'leafConnectee',
|
|
27
|
+
# 'my[indexMy.taskDivisions.value]': 'taskDivisions',
|
|
28
|
+
'my[indexMy.taskIndex.value]': 'taskIndex',
|
|
29
|
+
# 'foldGroups[-1]': 'leavesTotal',
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
dictionaryReplaceArrays = {
|
|
33
|
+
"track[indexTrack.leafAbove.value, ": 'leafAbove[',
|
|
34
|
+
"track[indexTrack.leafBelow.value, ": 'leafBelow[',
|
|
35
|
+
'track[indexTrack.countDimensionsGapped.value, ': 'countDimensionsGapped[',
|
|
36
|
+
'track[indexTrack.gapRangeStart.value, ': 'gapRangeStart[',
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
ImaIndent = " "
|
|
40
|
+
linesInitialize = """"""
|
|
41
|
+
|
|
42
|
+
for find, replace in dictionaryReplaceScalars.items():
|
|
43
|
+
linesInitialize += f"{ImaIndent}{replace} = {find}\n"
|
|
44
|
+
codeInlined = codeInlined.replace(find, replace)
|
|
45
|
+
|
|
46
|
+
for find, replace in dictionaryReplaceArrays.items():
|
|
47
|
+
linesInitialize += f"{ImaIndent}{replace[0:-1]} = {find[0:-2]}]\n"
|
|
48
|
+
codeInlined = codeInlined.replace(find, replace)
|
|
49
|
+
|
|
50
|
+
ourGuyOnTheInside = " doFindGaps = True\n"
|
|
51
|
+
linesInitialize = ourGuyOnTheInside + linesInitialize
|
|
52
|
+
|
|
53
|
+
codeInlined = codeInlined.replace(ourGuyOnTheInside, linesInitialize)
|
|
54
|
+
|
|
55
|
+
return codeInlined
|
|
56
|
+
|
|
57
|
+
def inlineMapFoldingNumba(**keywordArguments):
|
|
58
|
+
datatypeLarge = keywordArguments.get('datatypeLarge', datatypeLargeDEFAULT)
|
|
59
|
+
datatypeMedium = keywordArguments.get('datatypeMedium', datatypeMediumDEFAULT)
|
|
60
|
+
datatypeSmall = keywordArguments.get('datatypeSmall', datatypeSmallDEFAULT)
|
|
61
|
+
dictionaryEnumValues = getDictionaryEnumValues()
|
|
62
|
+
codeSource = inspect.getsource(algorithmSource)
|
|
63
|
+
pathFilenameAlgorithm = pathlib.Path(inspect.getfile(algorithmSource))
|
|
64
|
+
|
|
65
|
+
listCallables = [ 'countInitialize', 'countParallel', 'countSequential', ]
|
|
66
|
+
|
|
67
|
+
listPathFilenamesDestination: list[pathlib.Path] = []
|
|
68
|
+
for callableTarget in listCallables:
|
|
69
|
+
skipEnum = (callableTarget == 'countInitialize')
|
|
70
|
+
skipEnum = (callableTarget == 'countSequential')
|
|
71
|
+
pathFilenameDestination = pathFilenameAlgorithm.parent / "syntheticModules" / pathFilenameAlgorithm.with_stem(callableTarget).name
|
|
72
|
+
codeInlined, callableInlinedDecorators, importsRequired = makeInlineFunction(codeSource, callableTarget, dictionaryEnumValues, skipEnum, datatypeLarge=datatypeLarge, datatypeMedium=datatypeMedium, datatypeSmall=datatypeSmall)
|
|
73
|
+
codeUnpacked = unpackArrays(codeInlined, callableTarget)
|
|
74
|
+
pathFilenameDestination.write_text(importsRequired + "\n" + codeUnpacked)
|
|
75
|
+
listPathFilenamesDestination.append(pathFilenameDestination)
|
|
76
|
+
|
|
77
|
+
if __name__ == '__main__':
|
|
78
|
+
inlineMapFoldingNumba()
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
from mapFolding import indexMy, indexTrack
|
|
2
2
|
import numba
|
|
3
|
-
|
|
4
3
|
@numba.jit((numba.uint8[:,:,::1], numba.uint8[::1], numba.uint8[::1], numba.uint8[:,::1]), parallel=False, boundscheck=False, cache=True, error_model="numpy", fastmath=True, looplift=False, nogil=True, nopython=True)
|
|
5
4
|
def countInitialize(connectionGraph, gapsWhere, my, track):
|
|
6
5
|
while my[7] > 0:
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
from mapFolding import indexMy, indexTrack
|
|
2
2
|
import numba
|
|
3
|
-
|
|
4
3
|
@numba.jit((numba.uint8[:,:,::1], numba.int64[::1], numba.uint8[::1], numba.uint8[::1], numba.uint8[:,::1]), parallel=True, boundscheck=False, cache=True, error_model="numpy", fastmath=True, looplift=False, nogil=True, nopython=True)
|
|
5
4
|
def countParallel(connectionGraph, foldGroups, gapsWherePARALLEL, myPARALLEL, trackPARALLEL):
|
|
6
5
|
for indexSherpa in numba.prange(myPARALLEL[9]):
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
from mapFolding import indexMy, indexTrack
|
|
2
2
|
import numba
|
|
3
|
-
|
|
4
3
|
@numba.jit((numba.uint8[:,:,::1], numba.int64[::1], numba.uint8[::1], numba.uint8[::1], numba.uint8[:,::1]), parallel=False, boundscheck=False, cache=True, error_model="numpy", fastmath=True, looplift=False, nogil=True, nopython=True)
|
|
5
4
|
def countSequential(connectionGraph, foldGroups, gapsWhere, my, track):
|
|
6
5
|
doFindGaps = True
|
|
@@ -49,7 +49,7 @@ readme = { file = "README.md", content-type = "text/markdown" }
|
|
|
49
49
|
requires-python = ">=3.10,<3.14"
|
|
50
50
|
scripts = { getOEISids = "mapFolding.oeis:getOEISids", clearOEIScache = "mapFolding.oeis:clearOEIScache", OEIS_for_n = "mapFolding.oeis:OEIS_for_n" }
|
|
51
51
|
urls = { Homepage = "https://github.com/hunterhogan/mapFolding", Donate = "https://www.patreon.com/integrated" }
|
|
52
|
-
version = "0.3.
|
|
52
|
+
version = "0.3.3"
|
|
53
53
|
|
|
54
54
|
[tool.coverage]
|
|
55
55
|
report = { exclude_lines = [
|
|
@@ -62,6 +62,7 @@ run = { branch = true, concurrency = [
|
|
|
62
62
|
"thread",
|
|
63
63
|
], data_file = "tests/coverage/.coverage", omit = [
|
|
64
64
|
"tests/*",
|
|
65
|
+
"mapFolding/someAssemblyRequired/*",
|
|
65
66
|
"Z0Z_*",
|
|
66
67
|
], parallel = true, source = [
|
|
67
68
|
"mapFolding",
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
from tests.conftest import *
|
|
2
1
|
from contextlib import redirect_stdout
|
|
3
2
|
from datetime import datetime, timedelta
|
|
3
|
+
from mapFolding.oeis import _getFilenameOEISbFile, _getOEISidValues, _parseBFileOEIS, _validateOEISid
|
|
4
|
+
from tests.conftest import *
|
|
4
5
|
from typing import Optional, Tuple, Union
|
|
5
6
|
import io
|
|
6
7
|
import os
|
|
@@ -10,9 +11,14 @@ import random
|
|
|
10
11
|
import re as regex
|
|
11
12
|
import unittest
|
|
12
13
|
import unittest.mock
|
|
14
|
+
import urllib
|
|
13
15
|
import urllib.error
|
|
14
16
|
import urllib.request
|
|
15
17
|
|
|
18
|
+
def test_algorithmSourceSequential(oeisID: str, useAlgorithmDirectly):
|
|
19
|
+
for n in settingsOEIS[oeisID]['valuesTestValidation']:
|
|
20
|
+
standardComparison(settingsOEIS[oeisID]['valuesKnown'][n], oeisIDfor_n, oeisID, n)
|
|
21
|
+
|
|
16
22
|
def test_aOFn_calculate_value(oeisID: str):
|
|
17
23
|
for n in settingsOEIS[oeisID]['valuesTestValidation']:
|
|
18
24
|
standardComparison(settingsOEIS[oeisID]['valuesKnown'][n], oeisIDfor_n, oeisID, n)
|
|
@@ -1,17 +1,16 @@
|
|
|
1
|
-
import
|
|
2
|
-
from Z0Z_tools import intInnit
|
|
1
|
+
from contextlib import redirect_stdout
|
|
3
2
|
from tests.conftest import *
|
|
4
|
-
from tests.pythons_idiotic_namespace import *
|
|
5
3
|
from typing import List, Optional, Any
|
|
4
|
+
from Z0Z_tools import intInnit
|
|
5
|
+
import io
|
|
6
6
|
import itertools
|
|
7
7
|
import numba
|
|
8
8
|
import numpy
|
|
9
|
+
import pathlib
|
|
9
10
|
import pytest
|
|
10
11
|
import random
|
|
11
12
|
import sys
|
|
12
13
|
import unittest.mock
|
|
13
|
-
import io
|
|
14
|
-
from contextlib import redirect_stdout
|
|
15
14
|
|
|
16
15
|
@pytest.mark.parametrize("listDimensions,expected_intInnit,expected_parseListDimensions,expected_validateListDimensions,expected_getLeavesTotal", [
|
|
17
16
|
(None, ValueError, ValueError, ValueError, ValueError), # None instead of list
|
|
@@ -87,7 +86,8 @@ def test_countFolds_writeFoldsTotal(
|
|
|
87
86
|
foldsTotalExpected = foldsValue * getLeavesTotal(listDimensionsTestFunctionality)
|
|
88
87
|
mock_countFolds = mockFoldingFunction(foldsValue, listDimensionsTestFunctionality)
|
|
89
88
|
|
|
90
|
-
|
|
89
|
+
# TODO move this to conftest. I shouldn't have such a specific value buried in a test: SSOT
|
|
90
|
+
with unittest.mock.patch("mapFolding.dispatcher._countFolds", side_effect=mock_countFolds):
|
|
91
91
|
returned = countFolds(listDimensionsTestFunctionality, pathishWriteFoldsTotal=pathWriteTarget)
|
|
92
92
|
|
|
93
93
|
# standardComparison(foldsValue, lambda: returned) # Check return value
|
|
@@ -5,20 +5,23 @@ from typing import List, Dict, Tuple
|
|
|
5
5
|
# TODO add a test. `C` = number of logical cores available. `n = C + 1`. Ensure that `[2,n]` is computed correctly.
|
|
6
6
|
# Or, probably smarter: limit the number of cores, then run a test with C+1.
|
|
7
7
|
|
|
8
|
-
def
|
|
9
|
-
standardComparison(foldsTotalKnown[tuple(
|
|
8
|
+
def test_algorithmSourceParallel(listDimensionsTestParallelization: List[int], foldsTotalKnown: Dict[Tuple[int, ...], int], useAlgorithmDirectly) -> None:
|
|
9
|
+
standardComparison(foldsTotalKnown[tuple(listDimensionsTestParallelization)], countFolds, listDimensionsTestParallelization, None, 'maximum')
|
|
10
|
+
|
|
11
|
+
def test_countFoldsComputationDivisionsInvalid(listDimensionsTestFunctionality: List[int]) -> None:
|
|
12
|
+
standardComparison(ValueError, countFolds, listDimensionsTestFunctionality, None, {"wrong": "value"})
|
|
13
|
+
|
|
14
|
+
def test_countFoldsComputationDivisionsMaximum(listDimensionsTestParallelization: List[int], foldsTotalKnown: Dict[Tuple[int, ...], int]) -> None:
|
|
15
|
+
standardComparison(foldsTotalKnown[tuple(listDimensionsTestParallelization)], countFolds, listDimensionsTestParallelization, None, 'maximum')
|
|
10
16
|
|
|
11
17
|
@pytest.mark.parametrize("nameOfTest,callablePytest", PytestFor_defineConcurrencyLimit())
|
|
12
|
-
def
|
|
18
|
+
def test_defineConcurrencyLimit(nameOfTest, callablePytest):
|
|
13
19
|
callablePytest()
|
|
14
20
|
|
|
15
21
|
# @pytest.mark.parametrize("CPUlimitParameter", [{"invalid": True}, ["weird"]])
|
|
16
22
|
# def test_countFolds_cpuLimitOopsie(listDimensionsTestFunctionality: List[int], CPUlimitParameter: Dict[str, bool] | List[str]) -> None:
|
|
17
23
|
# standardComparison((AttributeError or ValueError), countFolds, listDimensionsTestFunctionality, None, 'cpu', CPUlimitParameter)
|
|
18
24
|
|
|
19
|
-
def test_countFolds_invalid_computationDivisions(listDimensionsTestFunctionality: List[int]) -> None:
|
|
20
|
-
standardComparison(ValueError, countFolds, listDimensionsTestFunctionality, None, {"wrong": "value"})
|
|
21
|
-
|
|
22
25
|
@pytest.mark.parametrize("computationDivisions, concurrencyLimit, listDimensions, expectedTaskDivisions", [
|
|
23
26
|
(None, 4, [9, 11], 0),
|
|
24
27
|
("maximum", 4, [7, 11], 77),
|
|
@@ -1,187 +0,0 @@
|
|
|
1
|
-
from mapFolding import indexMy, indexTrack, theDao, datatypeMedium, datatypeLarge, datatypeSmall
|
|
2
|
-
import ast
|
|
3
|
-
import pathlib
|
|
4
|
-
import inspect
|
|
5
|
-
|
|
6
|
-
algorithmSource = theDao
|
|
7
|
-
|
|
8
|
-
dictionaryDecorators={
|
|
9
|
-
'countInitialize':
|
|
10
|
-
f'@numba.jit((numba.{datatypeSmall}[:,:,::1], numba.{datatypeMedium}[::1], numba.{datatypeSmall}[::1], numba.{datatypeMedium}[:,::1]), parallel=False, boundscheck=False, cache=True, error_model="numpy", fastmath=True, looplift=False, nogil=True, nopython=True)\n',
|
|
11
|
-
'countParallel':
|
|
12
|
-
f'@numba.jit((numba.{datatypeSmall}[:,:,::1], numba.{datatypeLarge}[::1], numba.{datatypeMedium}[::1], numba.{datatypeSmall}[::1], numba.{datatypeMedium}[:,::1]), parallel=True, boundscheck=False, cache=True, error_model="numpy", fastmath=True, looplift=False, nogil=True, nopython=True)\n',
|
|
13
|
-
'countSequential':
|
|
14
|
-
f'@numba.jit((numba.{datatypeSmall}[:,:,::1], numba.{datatypeLarge}[::1], numba.{datatypeMedium}[::1], numba.{datatypeSmall}[::1], numba.{datatypeMedium}[:,::1]), parallel=False, boundscheck=False, cache=True, error_model="numpy", fastmath=True, looplift=False, nogil=True, nopython=True)\n',
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
def getDictionaryEnumValues():
|
|
18
|
-
dictionaryEnumValues = {}
|
|
19
|
-
for enumIndex in [indexMy, indexTrack]:
|
|
20
|
-
for memberName, memberValue in enumIndex._member_map_.items():
|
|
21
|
-
dictionaryEnumValues[f"{enumIndex.__name__}.{memberName}.value"] = memberValue.value
|
|
22
|
-
return dictionaryEnumValues
|
|
23
|
-
|
|
24
|
-
class RecursiveInlinerWithEnum(ast.NodeTransformer):
|
|
25
|
-
"""Process AST nodes to inline functions and substitute enum values.
|
|
26
|
-
Also handles function decorators during inlining."""
|
|
27
|
-
|
|
28
|
-
def __init__(self, dictionaryFunctions, dictionaryEnumValues):
|
|
29
|
-
self.dictionaryFunctions = dictionaryFunctions
|
|
30
|
-
self.dictionaryEnumValues = dictionaryEnumValues
|
|
31
|
-
self.processed = set()
|
|
32
|
-
|
|
33
|
-
def inlineFunctionBody(self, functionName):
|
|
34
|
-
if functionName in self.processed:
|
|
35
|
-
return None
|
|
36
|
-
|
|
37
|
-
self.processed.add(functionName)
|
|
38
|
-
inlineDefinition = self.dictionaryFunctions[functionName]
|
|
39
|
-
# Recursively process the function body
|
|
40
|
-
for node in ast.walk(inlineDefinition):
|
|
41
|
-
self.visit(node)
|
|
42
|
-
return inlineDefinition
|
|
43
|
-
|
|
44
|
-
def visit_Attribute(self, node):
|
|
45
|
-
# Substitute enum identifiers (e.g., indexMy.leaf1ndex.value)
|
|
46
|
-
if isinstance(node.value, ast.Attribute) and isinstance(node.value.value, ast.Name):
|
|
47
|
-
enumPath = f"{node.value.value.id}.{node.value.attr}.{node.attr}"
|
|
48
|
-
if enumPath in self.dictionaryEnumValues:
|
|
49
|
-
return ast.Constant(value=self.dictionaryEnumValues[enumPath])
|
|
50
|
-
return self.generic_visit(node)
|
|
51
|
-
|
|
52
|
-
def visit_Call(self, node):
|
|
53
|
-
callNode = self.generic_visit(node)
|
|
54
|
-
if isinstance(callNode, ast.Call) and isinstance(callNode.func, ast.Name) and callNode.func.id in self.dictionaryFunctions:
|
|
55
|
-
inlineDefinition = self.inlineFunctionBody(callNode.func.id)
|
|
56
|
-
if (inlineDefinition and inlineDefinition.body):
|
|
57
|
-
lastStmt = inlineDefinition.body[-1]
|
|
58
|
-
if isinstance(lastStmt, ast.Return) and lastStmt.value is not None:
|
|
59
|
-
return self.visit(lastStmt.value)
|
|
60
|
-
elif isinstance(lastStmt, ast.Expr) and lastStmt.value is not None:
|
|
61
|
-
return self.visit(lastStmt.value)
|
|
62
|
-
return None
|
|
63
|
-
return callNode
|
|
64
|
-
|
|
65
|
-
def visit_Expr(self, node):
|
|
66
|
-
if isinstance(node.value, ast.Call):
|
|
67
|
-
if isinstance(node.value.func, ast.Name) and node.value.func.id in self.dictionaryFunctions:
|
|
68
|
-
inlineDefinition = self.inlineFunctionBody(node.value.func.id)
|
|
69
|
-
if inlineDefinition:
|
|
70
|
-
return [self.visit(stmt) for stmt in inlineDefinition.body]
|
|
71
|
-
return self.generic_visit(node)
|
|
72
|
-
|
|
73
|
-
def findRequiredImports(node):
|
|
74
|
-
"""Find all modules that need to be imported based on AST analysis.
|
|
75
|
-
NOTE: due to hardcoding, this is a glorified regex. No, wait, this is less versatile than regex."""
|
|
76
|
-
requiredImports = set()
|
|
77
|
-
|
|
78
|
-
class ImportFinder(ast.NodeVisitor):
|
|
79
|
-
def visit_Name(self, node):
|
|
80
|
-
if node.id in {'numba'}:
|
|
81
|
-
requiredImports.add(node.id)
|
|
82
|
-
self.generic_visit(node)
|
|
83
|
-
|
|
84
|
-
def visitDecorator(self, node):
|
|
85
|
-
if isinstance(node, ast.Call) and isinstance(node.func, ast.Name):
|
|
86
|
-
if node.func.id == 'jit':
|
|
87
|
-
requiredImports.add('numba')
|
|
88
|
-
self.generic_visit(node)
|
|
89
|
-
|
|
90
|
-
ImportFinder().visit(node)
|
|
91
|
-
return requiredImports
|
|
92
|
-
|
|
93
|
-
def generateImports(requiredImports):
|
|
94
|
-
"""Generate import statements based on required modules."""
|
|
95
|
-
importStatements = {'import numba', 'from mapFolding import indexMy, indexTrack'}
|
|
96
|
-
|
|
97
|
-
importMapping = {
|
|
98
|
-
'numba': 'import numba',
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
for moduleName in sorted(requiredImports):
|
|
102
|
-
if moduleName in importMapping:
|
|
103
|
-
importStatements.add(importMapping[moduleName])
|
|
104
|
-
|
|
105
|
-
return '\n'.join(importStatements)
|
|
106
|
-
|
|
107
|
-
def inlineFunctions(sourceCode, targetFunctionName, dictionaryEnumValues, skipEnum=False):
|
|
108
|
-
if skipEnum:
|
|
109
|
-
dictionaryEnumValues = {}
|
|
110
|
-
dictionaryParsed = ast.parse(sourceCode)
|
|
111
|
-
dictionaryFunctions = {
|
|
112
|
-
element.name: element
|
|
113
|
-
for element in dictionaryParsed.body
|
|
114
|
-
if isinstance(element, ast.FunctionDef)
|
|
115
|
-
}
|
|
116
|
-
nodeTarget = dictionaryFunctions[targetFunctionName]
|
|
117
|
-
nodeInliner = RecursiveInlinerWithEnum(dictionaryFunctions, dictionaryEnumValues)
|
|
118
|
-
nodeInlined = nodeInliner.visit(nodeTarget)
|
|
119
|
-
ast.fix_missing_locations(nodeInlined)
|
|
120
|
-
|
|
121
|
-
requiredImports = findRequiredImports(nodeInlined)
|
|
122
|
-
importStatements = generateImports(requiredImports)
|
|
123
|
-
|
|
124
|
-
lineNumbaDecorator = dictionaryDecorators[targetFunctionName]
|
|
125
|
-
inlinedCode = importStatements + '\n\n' + lineNumbaDecorator + ast.unparse(ast.Module(body=[nodeInlined], type_ignores=[]))
|
|
126
|
-
return inlinedCode
|
|
127
|
-
|
|
128
|
-
def unpackArrays(codeInlined: str, callableTarget: str) -> str:
|
|
129
|
-
dictionaryReplaceScalars = {
|
|
130
|
-
'my[indexMy.dimensionsTotal.value]': 'dimensionsTotal',
|
|
131
|
-
'my[indexMy.dimensionsUnconstrained.value]': 'dimensionsUnconstrained',
|
|
132
|
-
'my[indexMy.gap1ndex.value]': 'gap1ndex',
|
|
133
|
-
'my[indexMy.gap1ndexCeiling.value]': 'gap1ndexCeiling',
|
|
134
|
-
'my[indexMy.indexDimension.value]': 'indexDimension',
|
|
135
|
-
# 'my[indexMy.indexLeaf.value]': 'indexLeaf',
|
|
136
|
-
'my[indexMy.indexMiniGap.value]': 'indexMiniGap',
|
|
137
|
-
'my[indexMy.leaf1ndex.value]': 'leaf1ndex',
|
|
138
|
-
'my[indexMy.leafConnectee.value]': 'leafConnectee',
|
|
139
|
-
# 'my[indexMy.taskDivisions.value]': 'taskDivisions',
|
|
140
|
-
'my[indexMy.taskIndex.value]': 'taskIndex',
|
|
141
|
-
# 'foldGroups[-1]': 'leavesTotal',
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
dictionaryReplaceArrays = {
|
|
145
|
-
"track[indexTrack.leafAbove.value, ": 'leafAbove[',
|
|
146
|
-
"track[indexTrack.leafBelow.value, ": 'leafBelow[',
|
|
147
|
-
'track[indexTrack.countDimensionsGapped.value, ': 'countDimensionsGapped[',
|
|
148
|
-
'track[indexTrack.gapRangeStart.value, ': 'gapRangeStart[',
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
ImaIndent = " "
|
|
152
|
-
linesInitialize = """"""
|
|
153
|
-
|
|
154
|
-
for find, replace in dictionaryReplaceScalars.items():
|
|
155
|
-
linesInitialize += f"{ImaIndent}{replace} = {find}\n"
|
|
156
|
-
codeInlined = codeInlined.replace(find, replace)
|
|
157
|
-
|
|
158
|
-
for find, replace in dictionaryReplaceArrays.items():
|
|
159
|
-
linesInitialize += f"{ImaIndent}{replace[0:-1]} = {find[0:-2]}]\n"
|
|
160
|
-
codeInlined = codeInlined.replace(find, replace)
|
|
161
|
-
|
|
162
|
-
ourGuyOnTheInside = " doFindGaps = True\n"
|
|
163
|
-
linesInitialize = ourGuyOnTheInside + linesInitialize
|
|
164
|
-
|
|
165
|
-
codeInlined = codeInlined.replace(ourGuyOnTheInside, linesInitialize)
|
|
166
|
-
|
|
167
|
-
return codeInlined
|
|
168
|
-
|
|
169
|
-
def Z0Z_inlineMapFolding():
|
|
170
|
-
dictionaryEnumValues = getDictionaryEnumValues()
|
|
171
|
-
codeSource = inspect.getsource(algorithmSource)
|
|
172
|
-
pathFilenameAlgorithm = pathlib.Path(inspect.getfile(algorithmSource))
|
|
173
|
-
|
|
174
|
-
listCallables = [ 'countInitialize', 'countParallel', 'countSequential', ]
|
|
175
|
-
|
|
176
|
-
listPathFilenamesDestination: list[pathlib.Path] = []
|
|
177
|
-
for callableTarget in listCallables:
|
|
178
|
-
skipEnum = (callableTarget == 'countInitialize')
|
|
179
|
-
skipEnum = (callableTarget == 'countSequential')
|
|
180
|
-
pathFilenameDestination = pathFilenameAlgorithm.parent / "someAssemblyRequired" / pathFilenameAlgorithm.with_stem(callableTarget).name
|
|
181
|
-
codeInlined = inlineFunctions(codeSource, callableTarget, dictionaryEnumValues, skipEnum)
|
|
182
|
-
codeUnpacked = unpackArrays(codeInlined, callableTarget)
|
|
183
|
-
pathFilenameDestination.write_text(codeUnpacked)
|
|
184
|
-
listPathFilenamesDestination.append(pathFilenameDestination)
|
|
185
|
-
|
|
186
|
-
if __name__ == '__main__':
|
|
187
|
-
Z0Z_inlineMapFolding()
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|