Ask Your Question

dzhy444's profile - activity

2022-07-14 03:20:04 +0200 received badge  Popular Question (source)
2021-04-24 17:28:31 +0200 received badge  Famous Question (source)
2021-04-24 17:28:31 +0200 received badge  Notable Question (source)
2020-11-26 13:04:04 +0200 received badge  Popular Question (source)
2020-05-27 04:36:10 +0200 commented answer Python MultiProcessing module stuck for long outputs

Thank you for your answer! Now I have found that someone had the same problem with Python. [https://stackoverflow.com/questions/3...]

2020-05-27 04:31:50 +0200 received badge  Supporter (source)
2020-05-27 04:31:48 +0200 received badge  Scholar (source)
2020-05-26 16:39:05 +0200 commented question Python MultiProcessing module stuck for long outputs

@tmonteil Thank you for your suggestion. I have attached my code with an example.

2020-05-26 16:38:06 +0200 received badge  Editor (source)
2020-05-24 15:38:40 +0200 asked a question Python MultiProcessing module stuck for long outputs

I am using SageMath 9.0 and I tried to do paralle computation in two ways

1) parallel decoration built in SageMath;

2) the MultiProcessing module of Python.

When using paralell decoration, everything works fine. When using MultiProcessing module for the same problem with the same input, everything works fine for short output, but there is a problem when the output is long. SageMath gets stuck after the computation if the output is long. I monitored the CPU usage, it peaks at first and then returns to zero, which means that the computation is complete. However, the output still does not appear.

What puzzles me is that the problem depends on the length of output, not the time of computation. For the same computation, once I add a line to manually set the output to be something short, or extract a small part of the original output, then the computation no longer gets stck in the end, and that small part agrees with the original answer.

I would like to know if there is any hidden parameter to prevent the Python MultiProcessing module from producing long outputs in Sagemath.

P.S. Following the suggestion of @tmonteil, I attached my code with an example.

def f(n):
    return 2 ^ (n ^ n)


def g(n):
    return factor(2 ^ (n ^ n))


def run(function, parameters):
    from multiprocessing import Process, Queue

    def target_function(x, queue):
        queue.put(function(*x))

    results = list()

    if __name__ == "__main__":
        queue = Queue()
        processes = [Process(target=target_function, args=(i, queue)) for i in parameters]
        for p in processes:
            p.start()
        for p in processes:
            p.join()
        results = [queue.get() for p in processes]

    return results

On my computer, the command

run(f,[(3,),(4,),(5,),(6,)])

works fine but the command

run(f,[(7,)])

gets stuck. On the other hand, the command

run(g,[(3,),(4,),(5,),(6,),(7,),(8,),(9,),(10,)])

works fine. Note that the function g does strictly more jobs than f, but its answers are much shorter.

2020-02-21 13:58:13 +0200 received badge  Student (source)
2020-02-21 13:15:35 +0200 asked a question Parallel computation for different functions

For a single function with a list of inputs, the @parallel decoration can be used to do parallel computation. I am wandering whether it is possible to do parallel computation for different functions.

A simple is example is to calculate the difference f-g of two independent functions f and g. How can I ask SageMath to simultaneously compute the values of f and g, and then calculate the difference?

My time measurements suggest that when calculating the difference f-g, SageMath actually calculates f and g one after another, and then take the difference.

I can think of a naive approach using the @parallel decoration. I can create a function whose input variable is the name of the functions I want to compute simultaneously. Then I use a list of function names as input to get a generator of outputs. This may work if the final result doesn't depend on the order of the outputs, but does not work if the order matters, for example when taking the difference.

In general, suppose I have a flow chart for the computation, in other words, I already know which jobs can be parallelized and how the information flows to the next stage. Then what is the best way to implement the flow chart?