Ask Your Question
2

How to use many cpus to compute in sage

asked 2017-03-19 01:43:32 +0100

Lee gravatar image

updated 2017-03-19 01:49:22 +0100

kcrisman gravatar image

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 flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2017-03-19 10:22:03 +0100

tmonteil gravatar image

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:

edit flag offensive delete link more

Comments

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?

Lee gravatar imageLee ( 2017-03-20 11:33:45 +0100 )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)
tmonteil gravatar imagetmonteil ( 2017-03-29 09:22:19 +0100 )edit

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: 2017-03-19 01:39:50 +0100

Seen: 286 times

Last updated: Mar 19 '17