# How to use many cpus to compute in sage

There is an server with 48 cpus in my office. I want to use all of them to compute the following codes in sage. How can I do that?

%time

import itertools

def is_matrix_integral(M):

for i,j in itertools.product(range(2),range(2)):
if M[i][j] not in ZZ:
return False
return True

def Gamma(M):

if is_matrix_integral(M) and M[1][0]%243==0 and M.det()==1:
return True
return False

p=4

T=Matrix([[2,-1],[9,-4]])*Matrix([[4,0],[0,9]])

W=Matrix([[0,1],[-243,0]])

S=Matrix([[1,1/3],[0,1]])

A=Matrix([[1,0],[81,1]])

B=-1/(243)*W*A^(-1)*W*A

C=Matrix([[1,1/9],[-27,-2]])

e=Matrix([[1,0],[0,-1]])

def Q(a,c):

return T*Matrix([[a,a+c],[c,-a]])*T^(-1)*e

I=[-100000..-1]+[1..100000]

for i,j in itertools.product([1..100000],I):

if sqrt(i*i+i*j+j*j) in QQ:
for k in [0,1,2]:
L=Q(i,j)*W*Q(i,j)^(-1)*W^(-1)*B^k
if Gamma(L):
print x,Q(i,j),Q(i,j).det(),i,j
for l in [0,1,2]:
M=Q(i,j)*B*Q(i,j)^(-1)*B^l
if Gamma(M):
print x,x,M,M.det(),i,j,k,l
break

edit retag close merge delete

Sort by ยป oldest newest most voted

If the commands run inside your big loop are independent, you can run them independently using the @parallel decorator. You will have to run the commands of your loop as a function, and replace the print statements by some return statements. Indeed, thing printed in a subprocess will not be visible.

See:

more

Thank you very much! I modified the code into following

@parallel(ncpus=47)

def P(i,j):

if sqrt(i*i+i*j+j*j) in QQ:
for k in [0,1,2]:
L=Q(i,j)*W*Q(i,j)^(-1)*W^(-1)*B^k
if Gamma(L):
for l in [0,1,2]:
M=Q(i,j)*B*Q(i,j)^(-1)*B^l
if Gamma(M):
return i,j


I=[1..1000]

for i,j in itertools.product([1..100],I): P(i,j)

but seems no effect. What is the reason?

( 2017-03-20 11:33:45 +0200 )edit

Your for loop is not parallel. You have to do something like (no tested):

sage: L = list(itertools.product([1..100],[1..1000]))
sage: P(L)

( 2017-03-29 09:22:19 +0200 )edit