Ask Your Question
0

help me to make my program calculate in parallel or Cython?

asked 2016-06-11 17:39:28 +0200

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

Hi everyone,

I'm a beginner in sage and python and I have a short program that calculates from five randomly parameter, they generate a sequence then I checked if this result exists in Sloane then I put my list of generated parameter in a pickle. I want to make my program calculates in parallel because when I put the number of iteration very big he remained for hours to give the result.

import time 
import pickle from os.path 
import isfile

def bino(i,j):
         return factorial(i)/(factorial(j)*factorial(i-j))

def sequ(nb,n):
start_time = time.time()
SloaneEncyclopedia.load()
        if isfile("/home/user/Desktop/Sage program/fileS.pkl"):
               with open('/home/user/Desktop/Sage program/fileS.pkl', 'rb') as pickle_load:
                   lst=pickle.load(pickle_load)
        else:
            lst=[]                  
        for T in range(nb+1):

                p=randint(0,2)
                r=randint(0,3)
                q=randint(-3,3)
                x=randint(-6,12)
                if(x==0):
                        x=1

                y=randint(-6,12)
                if(y==0):
                        y=1

                tx=[]
                arc=[]
                Sum=0
                tup=(p,q,r,x,y)

                if (tup in lst)== False:
                    lst.append(tup)
                    if(r+q)>0:
                               for i in range(n+1):
                                    for k in range(int((i-p)/(r+q))+1):
                                           Sum+=bino((i-q*k),(p+r*k))*pow(y,(p+r*k))*pow(x,(i-p-(r+q)*k))
                                    tx.append(int(Sum))
                                    Sum=0

                               if tx!=[]:

                                   res=SloaneEncyclopedia.find(tx, maxresults=9)

                                   if res!=[]:
                                      arc+=[("p= "+str(p),"q= "+str(q),"r= "+str(r),"x= "+str(x),"y= "+str(y)),res]
                                   else : 
                                        continue 
                               else :
                                   continue
                    else :
                       continue
                    print(arc)


                    with open('/home/user/Desktop/Sage program/ Found4.txt', 'r') as myfile:
                        myfile.write("\n".join(map(lambda x: str(x), arc))+"\n")

                else:
                    continue
        with open('/home/user/Desktop/Sage program/fileS.pkl', 'wb') as pickle_file:
                pickle.dump(lst, pickle_file, protocol=pickle.HIGHEST_PROTOCOL)
        print(len(lst))
        print("- %s seconds -" % (time.time() - start_time))

Thank you.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2016-06-14 00:56:47 +0200

tmonteil gravatar image

You can have a look at the @paralleldecorator : http://doc.sagemath.org/html/en/refer...

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-06-11 17:39:28 +0200

Seen: 257 times

Last updated: Jun 14 '16