def main(): array_one = genMatrix() array_two = genMatrix2() start_time = time.time() arr = multiply_matrices(array_two) print((time.time() - start_time)) printSubarray(arr)
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
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)
#!/usr/bin/env python3 import argparse import numpy as np import matrixUtils import time a = matrixUtils.genMatrix() b = matrixUtils.genMatrix2() c = np.zeros((len(a), len(a))) start = time.time() for i in range(len(a)): for j in range(len(b[0])): for k in range(len(b)): c[i][j] += a[i][k] * b[k][j] stop = time.time() print("time: ", (stop - start)) matrixUtils.printSubarray(c, len(c))
#get world communicator comm = MPI.COMM_WORLD #Read the file containing the test matrix. testGraph = readMatrixFromFile("fwTest.txt") #Read the file with the correct answers. refGraph = readMatrixFromFile("fwTestResult.txt") startTime = time.time() if comm.Get_rank() == 0: print("\nInput matrices:\n\n") print("Test Matrix:") mu.printSubarray(testGraph, size=6) #New line print() print("\nTarget Matrix:") mu.printSubarray(refGraph, size=6) #New line print() #Useful formulas size = len(testGraph) rowsPerThread = size // comm.Get_size() threadsPerRow = comm.Get_size() / size startRow = rowsPerThread * comm.Get_rank() endRow = rowsPerThread * (comm.Get_rank() + 1)
matrixA = matrixUtils.genMatrix2(size=250, value=7) matrixB = matrixUtils.genMatrix2(size=250, value=6) if len(sys.argv) != 2: usage() elif str.lower(sys.argv[1]) == 'serial': #Multiply the matrices startTime = time.time() result = multiplySerial(matrixA, matrixB) runningTimeSerial = time.time() - startTime #Print results print("MatrixA shape: %s" % str(matrixA.shape)) print("MatrixA subarray:") matrixUtils.printSubarray(matrixA, size=4) print("\nMatrixB shape: %s" % str(matrixA.shape)) print("MatrixB subarray:") matrixUtils.printSubarray(matrixB, size=4) expResultShape = (matrixA.shape[0], matrixB.shape[1]) print("\nExpected result matrix shape: %s" % str(expResultShape)) print("Actual result matrix shape: %s" % str(result.shape)) print("Computation Time: %.3f seconds" % runningTimeSerial) print("Result matrix subarray:") matrixUtils.printSubarray(result, size=4) print("\n") elif str.lower(sys.argv[1]) == 'parallel': #Run in parallel with 4 threads and be verbose.
import matrixUtils import time #making size of quare matrix modular size = 256 #generating matricies using the utils matrix_1 = matrixUtils.genMatrix(size, 2) matrix_2 = matrixUtils.genMatrix2(size, 3) #creating a same size matrix filled with 0s matrix_sol = [[0] * size for i in range(size)] #starting timer start = time.time() #nested for loops because I don't understand the syntax for python list comprehension for i in range(size): for j in range(size): for k in range(size): matrix_sol[i][j] += matrix_1[i][k] * matrix_2[k][j] #stopping timer stop_time = time.time() - start print("time: ") print("%s seconds" % stop_time) print("\n") #print solution matrixUtils.printSubarray(matrix_sol)
def printSubMatrix(self): matrixUtils.printSubarray(self.matrix, 10)