1

I'm trying to do the following: XX + XX^t + 4yy here's my following code:

I have created matrices first, then started to calculate the time taken for the process to complete without the multiprocessing. Then I introduced multiprocessing and calculated the time again

#!/usr/bin/env python3

from multiprocessing import Process
import numpy as np
import random
import time


def createMatrices(n):
    global x,y
    for i in range(n):
        row = []
        for j in range(n):
            row.append(random.randint(0, 256))
        x[i] = row
        
    for i in range(n):
        row = []
        for j in range(n):
            row.append(random.randint(0, 256))
        y[i] = row
        

def transposeMatrix(X):
    result = X.transpose()
    return result

def mat_mul(mat1, mat2,n):
    res = np.dot(mat1,mat2)
    return res


if __name__ == '__main__':
    for x in (2**p for p in range(3, 15)):
        n = x
        x = np.empty([n,n])
        y = np.empty([n,n])
        createMatrices(n)
        '''
        print("-"*50)
        print(x)
        print("-"*50)
        print("-"*50)
        print(y)
        print("-"*50)
        '''
        total_time=0
        sum = []
        #X*X
        start = time.time()
        sum.append(mat_mul(x,x,n).tolist())
        
        #X*XT
        sum.append(mat_mul(x, transposeMatrix(x),n))
        
        #4*y*y
        sum.append(4*mat_mul(y,y,n).tolist())
        end = time.time()
        
        total_time = end-start
        '''
        print("1st: ")
        print(total_time)
        '''
        
        total_time2 = 0
        start2 = time.time()
        p1 = Process(target = mat_mul, args=(x,x,n))
        p1.start()
        p2 = Process(target = mat_mul, args=(x, transposeMatrix(x), n))
        p2.start()
        p3 = Process(target = mat_mul, args=(y,y,n))
        p3.start()
        
        
        p1.join()
        p2.join()
        p3.join()
        end2 = time.time()
        total_time2 = end2-start2
        
        '''
        print("2nd:")
        print(total_time2)
        '''
        
        
        print(n,total_time,total_time2)

This is my run result:

8 9.059906005859375e-06 0.28221797943115234
16 8.511543273925781e-05 0.3175086975097656
32 8.7738037109375e-05 0.34047412872314453
64 0.00018310546875 0.33080291748046875
128 0.012358903884887695 0.28963184356689453
256 0.003584146499633789 0.3264491558074951
512 0.026476621627807617 0.2882118225097656
1024 0.08968615531921387 0.40580010414123535
2048 0.5845317840576172 1.2362430095672607
4096 3.2829248905181885 4.272820949554443
8192 21.27417492866516 32.87949204444885

2^14 taking forever to execute.

Am I doing something wrong in using multiprocessing? Is there somehting to do with my machine itself? I'm using macbook air M1. how would I correct myself? Thank you.

Prashanthv
  • 109
  • 7

0 Answers0