예제 #1
0
def main():
    """
    Used for running as a script
    """

    parser = argparse.ArgumentParser(
        description='Generate a 2d matrix and save it to a file.')
    parser.add_argument('-e',
                        '--serial',
                        action='store_true',
                        help='Tests the serial algorithm')
    parser.add_argument('-p',
                        '--parallel',
                        action='store_true',
                        help='Tests the parallel algorithm')
    parser.add_argument(
        '-n',
        '--num_of_threads',
        default=1,
        type=int,
        help='Number of threads used in the parallel algorithm')
    parser.add_argument('-s',
                        '--size',
                        default=700,
                        type=int,
                        help='Size of the 2d matrix to generate')
    parser.add_argument('-v',
                        '--value',
                        default=1,
                        type=int,
                        help='The value with which to fill the array with')
    parser.add_argument(
        '-f',
        '--filename',
        default='output.txt',
        type=str,
        help='The name of the file to save the matrix in (optional)')

    args = parser.parse_args()

    matrixA = mu.genMatrix2(args.size, args.value)
    matrixB = mu.genMatrix2(args.size, args.value)

    if args.parallel is not None:
        matrixC = parallelTest(matrixA, matrixB, args.num_of_threads)
    else:
        matrixC = serialTest(matrixA, matrixB)

    if args.filename is not None:
        print(f'Writing matrix to {args.filename}')
        mu.writeToFile(matrixC, args.filename)

        print(f'Testing file')
        mu.printSubarray(mu.readFromFile(args.filename))
    else:
        mu.printSubarray(matrixC)
    return
예제 #2
0
def calculate_path(graph, expected_result):
    '''
    Calculates the shortest path for all points
    '''

    comm       = MPI.COMM_WORLD
    rank       = comm.Get_rank()
    size       = comm.Get_size()
    print("size: ", size)
    print("rank: ", rank)
    rows       = len(graph)
    rows_per_thread = rows / size
    start      = int(rows_per_thread * rank)
    end        = int(rows_per_thread * (rank + 1))
    startTime  = timer()

    for i in range(rows):
        owner = int((size/rows) * i )
        graph[i] = comm.bcast(graph[i], root=owner)

        for k in range(start, end):
            for j in range(rows):
                graph[k][j] = min(graph[k][j], graph[k][i] + graph[i][j])

    # Bring everything together
    if rank is 0:
        for i in range(end, rows):
            owner = int((size/rows) * i)
            graph[i] = comm.recv(source=owner, tag=i)
        endTime = timer()
        mu.writeToFile(graph, "result.txt")
        if (graph == expected_result):
            mu.printSubarray(expected_result)
            print("Success!")
            print("Total Time: ", endTime - startTime)
        else:
            print("Try again!")
    else:
        for i in range(start, end):
            comm.send(graph[i], dest = 0, tag = i)
def main():
    parser = argparse.ArgumentParser(
        description='Generates the matrix of the shortest paths of a graph.')
    parser.add_argument('-g',
                        '--graph',
                        type=str,
                        help='graph file matrix of numbers for input')
    parser.add_argument(
        '-o',
        '--outputfile',
        help='output filename. matrix of shortest paths saved here')
    args = parser.parse_args()

    if (args.graph is None):
        print('Missing Graph file. Try --help')
    else:
        print(args.graph)
        finished_matrix, rank = run(matrixUtils.readFromFile(args.graph))
        if (args.outputfile is None):
            matrixUtils.printSubarray(finished_matrix)
        else:
            if (rank == 0):
                print(f'Writing matrix to {args.outputfile}')
                matrixUtils.writeToFile(finished_matrix, args.outputfile)
예제 #4
0
from matrixUtils import genMatrix, genRandomMatrix, writeToFile, multiplyMatrix

#Debugger
import numpy as np

if __name__ == "__main__":

    A = genMatrix(4, 100)
    B = genMatrix(4, 2)

    result = multiplyMatrix(A, B)
    result2 = np.matmul(A, B)

    print(result)
    print(result2)
    writeToFile(result, "result.txt")
 def writeToFile(self):
     matrixUtils.writeToFile(self.matrix, "output.txt")