Detect common computational patterns.
(self, code: str)
| 133 | } |
| 134 | |
| 135 | def _detect_patterns(self, code: str) -> List[str]: |
| 136 | """Detect common computational patterns.""" |
| 137 | patterns = [] |
| 138 | |
| 139 | pattern_checks = { |
| 140 | "reduction": [r'__syncthreads', r'for.*>>=\s*1', r'shared\[.*threadIdx'], |
| 141 | "tiling": [r'TILE_SIZE', r'tile', r'__shared__.*\[.*\]\[.*\]'], |
| 142 | "matrix multiplication": [r'\.x\].*\[.*\.y', r'row.*col', r'\.y\].*\[.*\.x'], |
| 143 | "stencil": [r'[-+]\s*1\]', r'[+-]\s*blockDim', r'neighbor'], |
| 144 | "scan/prefix sum": [r'inclusive.*scan', r'exclusive.*scan', r'prefix'], |
| 145 | "transpose": [r'\.y\]\[.*\.x', r'\.x\]\[.*\.y'], |
| 146 | "convolution": [r'filter', r'kernel.*\[.*\]', r'conv'], |
| 147 | "parallel reduction": [r'atomicAdd', r'warp.*reduce', r'__shfl'] |
| 148 | } |
| 149 | |
| 150 | for pattern_name, indicators in pattern_checks.items(): |
| 151 | if sum(1 for ind in indicators if re.search(ind, code, re.IGNORECASE)) >= 2: |
| 152 | patterns.append(pattern_name) |
| 153 | |
| 154 | return patterns |
| 155 | |
| 156 | def _estimate_complexity(self, code: str) -> str: |
| 157 | """Estimate computational complexity.""" |