[PYTHON] Solving higher-order equations with integer type (no multiple solution) Manager

Manager edition

Until the last time Solving high equations with integer type, the parallel processing part used the pool of multiprocessing. Each process will be returned in a random order at the end. It would be nice to simply process in parallel, but there was a loss in processing time due to the need to rearrange the order. So this time, I rewrote the program using the manager of multiprocessing. The calculation time has been reduced to less than half because there is no need to rearrange the work twice.

In the previous Solving high equations with integer type, the programs for the 10th and 10000th equations were rewritten and shown below. It is a Raspberry Pi 4B memory 2G model and uses Thonny of Raspberry Pi OS with DESKTOP (2020-DEC-02, 32bit version OS). No disc etc. is connected to USB. Swap is the default SD card 100MB.

Decimal equation

eq-10ji-int-manager.py


import time
from multiprocessing import Manager, Process

def f0(list0, r_f, ji_su, y_zoom, X_init, X_end, Ksu):
    Ypre=0
    for X in range( int(X_init), int(X_end)+1 ):
        Y=0
        for j in range(0, ji_su +1):
            Y += Ksu[j]*(X**j)
        if X == int(X_init):
            Ypre=Y
            list0.pop(0)
        if Ypre * Y > 0:
            Ypre=Y
        else:
            if Y == 0:
                Y=Y
                list0.append(( X/r_f, Y/(r_f**(ji_su-y_zoom)) ))
            else:
                Ypre=Y
                list0.append(( X/r_f, Y/(r_f**(ji_su-y_zoom)) ))

def f1(list1, r_f, ji_su, y_zoom, X_init, X_end, Ksu):
    Ypre=0
    for X in range( int(X_init), int(X_end)+1 ):
        Y=0
        for j in range(0, ji_su +1):
            Y += Ksu[j]*(X**j)
        if X == int(X_init):
            Ypre=Y
            list1.pop(0)
        if Ypre * Y > 0:
            Ypre=Y
        else:
            if Y == 0:
                Y=Y
                list1.append(( X/r_f, Y/(r_f**(ji_su-y_zoom)) ))
            else:
                Ypre=Y
                list1.append(( X/r_f, Y/(r_f**(ji_su-y_zoom)) ))

def f2(list2, r_f, ji_su, y_zoom, X_init, X_end, Ksu):
    Ypre=0
    for X in range( int(X_init), int(X_end)+1 ):
        Y=0
        for j in range(0, ji_su +1):
            Y += Ksu[j]*(X**j)
        if X == int(X_init):
            Ypre=Y
            list2.pop(0)
        if Ypre * Y > 0:
            Ypre=Y
        else:
            if Y == 0:
                Y=Y
                list2.append(( X/r_f, Y/(r_f**(ji_su-y_zoom)) ))
            else:
                Ypre=Y
                list2.append(( X/r_f, Y/(r_f**(ji_su-y_zoom)) ))

def f3(list3, r_f, ji_su, y_zoom, X_init, X_end, Ksu):
    Ypre=0
    for X in range( int(X_init), int(X_end)+1 ):
        Y=0
        for j in range(0, ji_su +1):
            Y += Ksu[j]*(X**j)
        if X == int(X_init):
            Ypre=Y
            list3.pop(0)
        if Ypre * Y > 0:
            Ypre=Y
        else:
            if Y == 0:
                Y=Y
                list3.append(( X/r_f, Y/(r_f**(ji_su-y_zoom)) ))
            else:
                Ypre=Y
                list3.append(( X/r_f, Y/(r_f**(ji_su-y_zoom)) ))



if __name__=="__main__":
    
    with Manager() as manager:
        list0 = manager.list(range(1))
        list1 = manager.list(range(1))
        list2 = manager.list(range(1))
        list3 = manager.list(range(1))


        ####  Input Start  ####
        xx_from = 0
        xx_to   = 12
        aa00= 3628800.0
        aa01=-10628640.0
        aa02= 12753576.0
        aa03=-8409500.0
        aa04= 3416930.0
        aa05=-902055.0
        aa06= 157773.0
        aa07=-18150.0
        aa08= 1320.0
        aa09=-55.0
        aa10= 1.0
        r_f     = 1000000
        ji_su   = 10
        y_zoom  = 0
        ####  Input End  ####

        ksu=[]
        ksu.append( int((r_f**10)*aa00) )
        ksu.append( int((r_f**9) *aa01) )
        ksu.append( int((r_f**8) *aa02) )
        ksu.append( int((r_f**7) *aa03) )
        ksu.append( int((r_f**6) *aa04) )
        ksu.append( int((r_f**5) *aa05) )
        ksu.append( int((r_f**4) *aa06) )
        ksu.append( int((r_f**3) *aa07) )
        ksu.append( int((r_f**2) *aa08) )
        ksu.append( int((r_f**1) *aa09) )
        ksu.append( int((r_f**0) *aa10) )

        initial_time=time.time()
        print(f"Calcilation Start")
        print(f" Please wait for 2 minutes.")
        Xfrom  = int(r_f*xx_from)
        Xto    = int(r_f*xx_to)
        Xrange = Xto - Xfrom

        p0 = Process(target=f0, args=(list0,r_f,ji_su,y_zoom,Xfrom+0*Xrange/4,Xfrom+1*Xrange/4,ksu))
        p0.start()
        p1 = Process(target=f1, args=(list1,r_f,ji_su,y_zoom,Xfrom+1*Xrange/4,Xfrom+2*Xrange/4,ksu))
        p1.start()
        p2 = Process(target=f2, args=(list2,r_f,ji_su,y_zoom,Xfrom+2*Xrange/4,Xfrom+3*Xrange/4,ksu))
        p2.start()
        p3 = Process(target=f3, args=(list3,r_f,ji_su,y_zoom,Xfrom+3*Xrange/4,Xfrom+4*Xrange/4,ksu))
        p3.start()
        p0.join()
        p1.join()
        p2.join()
        p3.join()
        print(f"Calcilation Finished")
        print(f"")
        print(f"Time :")
        print(str(time.time() - initial_time))
        print(f"Answers :")
        for i in range(len(list0)):
            print(list0[i])
        for i in range(len(list1)):
            print(list1[i])
        for i in range(len(list2)):
            print(list2[i])
        for i in range(len(list3)):
            print(list3[i])
    
        time.sleep(10)

10000th order equation

eq-10000ji-int-manager.py


import time
from multiprocessing import Manager, Process

def f0(List0, R_f, Ji_su, Y_zoom, X_init, X_end, Ksu):
    Ypre=0
    for X in range( int(X_init), int(X_end)+1 ):
        Y=1
        for j in range(   0, 10000):
            Y *= (X-Ksu[j])
        if X == int(X_init):
            Ypre=Y
            List0.pop(0)
        if Ypre * Y > 0:
            Ypre=Y
        else:
            if Y == 0:
                Y=Y
                List0.append(( X/R_f, Y/(R_f**(Ji_su-Y_zoom)) ))
            else:
                Ypre=Y
                List0.append(( X/R_f, Y/(R_f**(Ji_su-Y_zoom)) ))

def f1(List1, R_f, Ji_su, Y_zoom, X_init, X_end, Ksu):
    Ypre=0
    for X in range( int(X_init), int(X_end)+1 ):
        Y=1
        for j in range(   0, 10000):
            Y *= (X-Ksu[j])
        if X == int(X_init):
            Ypre=Y
            List1.pop(0)
        if Ypre * Y > 0:
            Ypre=Y
        else:
            if Y == 0:
                Y=Y
                List1.append(( X/R_f, Y/(R_f**(Ji_su-Y_zoom)) ))
            else:
                Ypre=Y
                List1.append(( X/R_f, Y/(R_f**(Ji_su-Y_zoom)) ))

def f2(List2, R_f, Ji_su, Y_zoom, X_init, X_end, Ksu):
    Ypre=0
    for X in range( int(X_init), int(X_end)+1 ):
        Y=1
        for j in range(   0, 10000):
            Y *= (X-Ksu[j])
        if X == int(X_init):
            Ypre=Y
            List2.pop(0)
        if Ypre * Y > 0:
            Ypre=Y
        else:
            if Y == 0:
                Y=Y
                List2.append(( X/R_f, Y/(R_f**(Ji_su-Y_zoom)) ))
            else:
                Ypre=Y
                List2.append(( X/R_f, Y/(R_f**(Ji_su-Y_zoom)) ))

def f3(List3, R_f, Ji_su, Y_zoom, X_init, X_end, Ksu):
    Ypre=0
    for X in range( int(X_init), int(X_end)+1 ):
        Y=1
        for j in range(   0, 10000):
            Y *= (X-Ksu[j])
        if X == int(X_init):
            Ypre=Y
            List3.pop(0)
        if Ypre * Y > 0:
            Ypre=Y
        else:
            if Y == 0:
                Y=Y
                List3.append(( X/R_f, Y/(R_f**(Ji_su-Y_zoom)) ))
            else:
                Ypre=Y
                List3.append(( X/R_f, Y/(R_f**(Ji_su-Y_zoom)) ))



if __name__=="__main__":
    
    with Manager() as manager:

        list0 = manager.list(range(1))
        list1 = manager.list(range(1))
        list2 = manager.list(range(1))
        list3 = manager.list(range(1))

        ####  Input Start  ####
        xx_from = 0.940
        xx_to   = 1.060
        ksu_init = []
        ksu_temp = 0.94999
        for i in range(10000):
            ksu_temp += 0.00001
            ksu_init.append(ksu_temp)
        r_f     = 200000
        ji_su   = 10000
        y_zoom  = 2500
        ####  Input End  ####

        initial_time=time.time()
        print(f"Calcilation Start")
        print(f" Please wait for 15 minutes.")
        Xfrom  = int(r_f*xx_from)
        Xto    = int(r_f*xx_to)
        Xrange = Xto - Xfrom
        ksu=[]
        for i in range(len(ksu_init)):
            ksu.append( int(r_f*ksu_init[i]) )

        p0 = Process(target=f0, args=(list0,r_f,ji_su,y_zoom,Xfrom+0*Xrange/4,Xfrom+1*Xrange/4,ksu))
        p0.start()
        p1 = Process(target=f1, args=(list1,r_f,ji_su,y_zoom,Xfrom+1*Xrange/4,Xfrom+2*Xrange/4,ksu))
        p1.start()
        p2 = Process(target=f2, args=(list2,r_f,ji_su,y_zoom,Xfrom+2*Xrange/4,Xfrom+3*Xrange/4,ksu))
        p2.start()
        p3 = Process(target=f3, args=(list3,r_f,ji_su,y_zoom,Xfrom+3*Xrange/4,Xfrom+4*Xrange/4,ksu))
        p3.start()
        p0.join()
        p1.join()
        p2.join()
        p3.join()
        print(f"Calcilation Finished")
        print(f"")
        print(f"Time :")
        print(str(time.time() - initial_time))
        print(f"Answers :")
        time.sleep(2)
        for i in range(len(list0)):
            print(list0[i])
        for i in range(len(list1)):
            print(list1[i])
        for i in range(len(list2)):
            print(list2[i])
        for i in range(len(list3)):
            print(list3[i])
    
        time.sleep(10)

Thank you for watching until the end.

This time, I agree with the purpose of Qiita and post it, so you are free to copy or modify the published program. There are no copyright issues.

However, when using the Raspberry Pi 4B, a particularly large heat sink is required for the CPU. In the case of this program, where LAN communication is infrequent, the LAN chip does not get hot. However, if the calculation time continues, a considerable amount of power is used as can be seen from the intense heat generated by the CPU. The small black chip behind the power supply UCB C type also gets hot, so you also need a fan flow.

Next time, in "Solving high equations with integer type = Distributed parallel processing =", we plan to try to shorten the calculation time by distributed processing using socket communication of 4 Raspberry Pis.

Recommended Posts

Solving higher-order equations with integer type (no multiple solution) Manager
Solve higher-order equations with integer type (no multiple solutions)
Solving higher-order equations in a 4-unit cluster I (no multiple solutions)
Solve 100th-order equations (no multiple solutions)