public static void main(String args[]) 
		throws InterruptedException, IOException 
	{
		int m = 0, n = 0, p = 0;
		int numThreads = 1;
		Thread[] threads = null;
		FileWriter fstream = null;
		BufferedWriter out = null;
		try {
			m = Integer.parseInt(args[0]);
			n = Integer.parseInt(args[1]);
			p = Integer.parseInt(args[2]);
			numThreads = Integer.parseInt(args[3]);
			threads = new Thread[numThreads];
			fstream = new FileWriter(args[4]);
			out = new BufferedWriter(fstream);
			if (m <= 0 || n <= 0 || p <= 0)
				throw new Exception();
		}
		catch (Exception e) {
			System.err.println(e.getMessage());
			System.err.println(
				"usage: m n p numThreads filename\n\n" + 
				"Multiply a matrix A (m x n) with a matrix B (n x p).\n" +
				"A and B will be randomly generated and the results saved " +
				"to a matlab script that can be run to verify results.\n"
			);
			System.exit(-1);
		}
		// Allocate the arrays
		float[][] A = new float[m][n];
		float[][] B = new float[n][p];
		MatrixMultiply mm = new MatrixMultiply(A,B);

		long start = System.currentTimeMillis();

		// Start the threads
		for (int i=0; i<numThreads; i++) {
			threads[i] = new Thread(mm);
			threads[i].start();
		}

		// Wait for threads to finish
		for (int i=0; i<numThreads; i++) 
			threads[i].join();
	
		float duration = (System.currentTimeMillis() - start);
		System.out.println("Done in " + duration / 1000 + "s");

		// Write matlab script that can be run to check correctness
		out.write("A = ");
		writeMatrix(out,A);
		out.write(";\n");
		out.write("B = ");
		writeMatrix(out,B);
		out.write(";\n");
		out.write("result = ");
		mm.writeOutputMatrix(out);
		out.write(";\n");
		out.write(
			"if (abs(A*B - result) <= 1e-2)\n" + 
			"	disp('matrices multiplied correctly');\n" +
			"else\n" +
			"	disp('matrices not multiplied correctly');\n" + 
			"end\n"
		);
		out.close();
	}