Ask Your Question

Revision history [back]

Running a for cycle in parallel

I have an algorithm that I need to speed up. I would like to parallelize computations

the main part I need to speed up is the following

  for r2 in Subsets(V2,k-4):
         check2(fuse(v,r2),k)

this gives as output three values b1, b2, b3 that are global and for any input check2 can either leave them unchanged or increase them by 1. I tried looking into @parallel but it doens't seem to fit my requirements.

How can I parallelize that (I can use up to 16 cores)?

Running a for cycle in parallel

I have an algorithm that I need to speed up. I would like to parallelize computations

the main part I need to speed up is the following

  for r2 in Subsets(V2,k-4):
         check2(fuse(v,r2),k)

this gives as output three values b1, b2, b3 that are global and for any input check2 can either leave them unchanged or increase them by 1. I tried looking into @parallel but it doens't seem to fit my requirements.

How can I parallelize that (I can use up to 16 cores)?

This is the whole code:

Running a for cycle in parallel

I have an algorithm that I need to speed up. I would like to parallelize computations

the main part I need to speed up is the following

  for r2 in Subsets(V2,k-4):
         check2(fuse(v,r2),k)

this gives as output three values b1, b2, b3 that are global and for any input check2 can either leave them unchanged or increase them by 1. I tried looking into @parallel but it doens't seem to fit my requirements.

How can I parallelize that (I can use up to 16 cores)?

This is the whole code:code (minus all the parts that do, I can't publish it all):

P2=[]                                              
def P_2(q,n,S):     %creates the set P2


I2R=[]  
I2S=[]
I3=[]
I3R=[]
I6=[]
I8=[]
def IG(k):      %creates a set of indeces vectors

def coll(x,y,z):    %checks if 3 given tuples are collinear
    A=[x,y,z]
    M = matrix(A)
    if M.determinant()==0:
         return 'true'                                             

def cone(v):     %defines a function to be used in conic()

def conic(x1,x2,x3,x4,x5,x6):    %checks if 6 given tuples are on a conic
A=[cone(x1),cone(x2),cone(x3),cone(x4),cone(x5),cone(x6)]
M = matrix(A)
if M.determinant()==0:
    return 'true'                              

def cube(v):     %defines a function to be used in SIcubic()                    



def dxcube(v):     %defines a function to be used in SIcubic()      

def dycube(v):         %defines a function to be used in SIcubic()      


def dzcube(v):       %defines a function to be used in SIcubic()      


def SIcubic(x1,x2,x3,x4,x5,x6,x7,x8):      %checks if 8 given tuples are on a special cubic
    v1='false'
    v=[x1,x2,x3,x4,x5,x6,x7,x8]
    for a in range(8):
        x=v[a]
        A=[]
        A.append(dxcube(x))
        A.append(dycube(x))
        A.append(dzcube(x))
        for t in range(8):
         if t!=a:
           y=v[t]
           A.append(cube(y))      
        M = matrix(A)
        if M.determinant()==0:
             v1='true'   
    return v1         

b1=0  
b2=0
b3=0  
def check(s,k):       %checks a vector s of lenght k
        global b1
        global b2
        global b3
        IG(k)
        q1=0
        q2=0
        q3=0
        for i3 in I3:
                a0=i3[0]
                a1=i3[1]
                a2=i3[2]
                if coll(s[a0],s[a1],s[a2])=='true':
                        q1=1
                        q2=1
                        q3=1
                        break  
        if q2==0:     
         if k>5:         
          if q==0:
            for i6 in I6:
                a0=i6[0]
                a1=i6[1]
                a2=i6[2]
                a3=i6[3]
                a4=i6[4]
                a5=i6[5]
                if conic(s[a0],s[a1],s[a2],s[a3],s[a4],s[a5])=='true':
                        q2=1
                        q3=1
                        break
        if q3==0:
         if k>7:
          for i8 in I8:
            a0=i8[0]
            a1=i8[1]
            a2=i8[2]
            a3=i8[3]
            a4=i8[4]
            a5=i8[5]
            a6=i8[6]
            a7=i8[7]
            if SIcubic(s[a0],s[a1],s[a2],s[a3],s[a4],s[a5],s[a6],s[a7])=='true':
                 q3=1
                 break          
        b1=b1+q1
        b2=b2+q2
        b3=b3+q3
        return b1
        return b2
        return b3

b1=0    
def check1(s,a):     %checks a vector s and an element a
        global b1
        q1=0
        for i2 in I2S:
                a0=i2[0]
                a1=i2[1]
                if coll(s[a0],s[a1],a)=='true':
                        q1=1
                        break
        b1=b1+q1
        return b1

b1=0    
b2=0  
b3=0  
def check2(s,k):    %checks a vector s of lenght k
        global b1
        global b2
        global b3
        q1=0
        q2=0
        q3=0
        for i3 in I3R:
                a0=i3[0]
                a1=i3[1]
                a2=i3[2]
                if coll(s[a0],s[a1],s[a2])=='true':
                        q1=1
                        q2=1
                        q3=1
                        break         
        if q2==0:         
          if k>5:
            for i6 in I6:
                a0=i6[0]
                a1=i6[1]
                a2=i6[2]
                a3=i6[3]
                a4=i6[4]
                a5=i6[5]
                if conic(s[a0],s[a1],s[a2],s[a3],s[a4],s[a5])=='true':
                        q2=1
                        q3=1
                        break
        if q3==0:
         if k>7: 
          for i8 in I8:
            a0=i8[0]
            a1=i8[1]
            a2=i8[2]
            a3=i8[3]
            a4=i8[4]
            a5=i8[5]
            a6=i8[6]
            a7=i8[7]
            if SIcubic(s[a0],s[a1],s[a2],s[a3],s[a4],s[a5],s[a6],s[a7])=='true':
                 q3=1
                 break
        b1=b1+q1
        b2=b2+q2
        b3=b3+q3
        return b1
        return b2
        return b3 


t=1
def TOT(P,q,n,k):      %computes a polynomial depending on q,n,k,P


def fuse(s,t):        %fuses two vectors into 1 (see repsonse to answere)


sn=[]
def Count(S,q,n,k):     %main function
    IG(k)
    global b1
    global b2
    global b3
    l1=0
    l2=0
    l3=0
    y=q^n
    s=len(S)
    v=[(0,0,1),(0,1,0),(1,0,0),(1,1,1)]
    V1=copy(S)
    V1.remove((1,0,0))
    V1.remove((0,1,0))
    V1.remove((0,0,1))
    V1.remove((1,1,1))
    if k<4:
      b1=0
      for s in Subsets(S,k):
          check(s,k)
      l1=b1*factorial(k)
    if k>4:
      V2=copy(V1)
      for r1 in V1:
          b1=0
          b2=0
          b3=0
          check1(v,r1)
          if b1==1:
             V2.remove(r1)
      s2=len(V2)
      l1=l1+binomial(s-4,k-4)-binomial(s2,k-4)
      l2=l2+binomial(s-4,k-4)-binomial(s2,k-4)
      l3=l3+binomial(s-4,k-4)-binomial(s2,k-4)
      b1=0
      b2=0
      b3=0
      for r2 in Subsets(V2,k-4):
             check2(fuse(v,r2),k)   
      l1=l1+b1 
      l2=l2+b2   
      l3=l3+b3      
      l1=l1*(y^2+y+1)*(y^3-y)*(y^3-y^2)*factorial(k-4)
      l2=l2*(y^2+y+1)*(y^3-y)*(y^3-y^2)*factorial(k-4)
      l3=l3*(y^2+y+1)*(y^3-y)*(y^3-y^2)*factorial(k-4)
    g1=t-l1
    g2=t-l2
    g3=t-l3
    print('There are ', g1/factorial(k) , ' unordered', k,'-tuples in general linear position')
    print('There are ', g1 , ' ordered', k ,'-tuples in general linear position')
    print('There are ', g2/factorial(k) , ' unordered', k,'-tuples in general linear and conic position')
    print('There are ', g2 , ' ordered', k ,'-tuples in general linear and conic position')
    print('There are ', g3/factorial(k) , ' unordered', k,'-tuples in general position')
    print('There are ', g3 , ' ordered', k ,'-tuples in general position')




 def Active(q,n,k):        %every fucntion I need to run reduced into 1
        P2=[]
        P_2(q,n,P2)
        TOT(P2,q,n,k)
        Count(P2,q,n,k)