# help me to make my program calculate in parallel or Cython? 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()
if isfile("/home/user/Desktop/Sage program/fileS.pkl"):
with open('/home/user/Desktop/Sage program/fileS.pkl', 'rb') as 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 close merge delete

Sort by » oldest newest most voted

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

more