'Refactoring a for loop
as a result of a profiling study, I have found that this function is creating a bottleneck for my code to run faster.
TM1 = TermMap(self.sums, self.tensors)
for xs in product(*tlists):
sign = math.prod([x[1] for x in xs])
newtensors = [permute(t, x[0]) for t, x in zip(other.tensors, xs)]
TM2 = TermMap(other.sums, newtensors)
if TM1 == TM2:
return sign
return None
I would like to refactor in a way that I can either parallelize it, or at least compile it using SWIG or similar.
I tried numba but the jit class is not really working since the code handles many string and symbolic lists which produces a lot of errors in numba.
I have been facing the problem that parallelization using mp processes is complicated since the function is returning a None
and the condition TM1 == TM2 is returning sign within the loop. I don't really know how to handle this, the re write the function that the mp.(i)map approach can be used.
The function TermMap and permute are part of other classes and therefore I don't really see how I can compile the for loop with these functions inside. Any ideas of suggestions to make this part of the code more efficient are welcome.
tlists is a list in this form:
[[((0, 1, 2, 3), 1), ((1, 0, 2, 3), -1), ((0, 1, 3, 2), -1), ((1, 0, 3, 2), 1)], [((0,), 1)], [((0, 1, 2, 3), 1), ((1, 0, 2, 3), -1), ((0, 1, 3, 2), -1), ((1, 0, 3, 2), 1)], [((0,), 1)], [((0, 1), 1)], [((0, 1), 1)], [((0, 1), 1)]]
However, it changes the size since this function is called many times inside another for loop.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
