Improving the Performance in the Statistical Redistribution of Message Symbols using Architectural patterns for Parallel Programming

* Created by: Ramses Alexander Coraspe Valdez

* Created on: November 22, 2019

In [0]:
import timeit as tiempo
from functools import reduce
import numpy as np
from collections import Counter
import pandas as pd
import multiprocessing as mp
In [0]:
class code_message:

    def __init__(self, m, k1, k2):        
        self.m =m
        self.k1= k1
        self.k2= k2                  
    
    def code(self, s, pos): 
        s, key = change_distribution(s);                                            
        return (pos, s, key);                                
    
    #geometric descomposition 
    def transform(self, k):

       keys=[];        

        pool = mp.Pool(processes=8)        
        results = [pool.apply_async(self.code, args=(self.m[x:x+kr], x)) for x in range(0, len(self.m), kr)]
        output = [p.get() for p in results]

        #Update each section of the message after receiving the results                

        return self.m, keys        
In [0]:
class optimal_matrix:

    def __init__(self, n):
        self.n = n
    
    def find_optimal_matrix(self):
        noise = False
        if self.is_prime():            
            self.n = self.n + 1
            noise = True
        return self.get_size_matrix(self.factors(self.n), noise);

    #I get the optimized array size always leaving a bit as noise
    def get_size_matrix(self, factors, noise):        
        return factors[len(factors) - 1], factors[len(factors) - 2], noise
   
    #Check if a number is prime
    def is_prime(self):
        if self.n < 3 or self.n % 2 == 0:
            return self.n == 2
        else:
            return not any(self.n % i == 0 for i in range(3, int(self.n**0.5 + 2), 2))

    #Calculation of all factors
    def factors(self, f):    
        return reduce(list.__add__, ([i, f//i] for i in range(1, int(f**0.5) + 1) if f % i == 0));
In [0]:
if __name__ == '__main__':

    inicio = tiempo.default_timer()
    with open('images\Lenna.png', 'rb') as r:                                                           
              original= bytearray(r.read())

    originalVector_int = [x for x in original]

    om = optimal_matrix(len(originalVector_int));

    k1, k2, noise= om.find_optimal_matrix();    
    if(noise): 
        originalVector_int.append(0)

    print(len(originalVector_int))

    cc= code_message(originalVector_int,k1, k2);

    newVector, key = cc.transform(k2);

    fin = tiempo.default_timer()
    print("running time: " + format(fin-inicio, '.8f'))