static void Main(string[] args) { using (new MPI.Environment(ref args)) { int matrixSize = 50; Intracommunicator comm = Communicator.world; if (comm.Rank == 0) { DateTime startTime = DateTime.Now; MatrixMultiply mp = new MatrixMultiply(comm.Size, matrixSize); //mp.Show(); comm.Send <MatrixMultiply>(mp, 1, 0); Console.WriteLine("Sending MatrixMyltiply"); Matrix res = mp.MultiplyForRank(comm.Rank); comm.Receive <MatrixMultiply>(Communicator.anySource, 0); Console.WriteLine("Recieve MatrixMultiply"); comm.Send <Matrix>(res, 1, 1); Console.WriteLine("Sending Matrix result"); //res = comm.Receive<Matrix>(Communicator.anySource, 1); Console.WriteLine("Recieve Matrix result"); //res.Show(); DateTime endTime = DateTime.Now; Console.WriteLine("Test multiply" + (startTime - endTime).ToString()); } else { MatrixMultiply mp = comm.Receive <MatrixMultiply>(comm.Rank - 1, 0); comm.Send <MatrixMultiply>(mp, (comm.Rank + 1) % comm.Size, 0); Matrix res = mp.MultiplyForRank(comm.Rank); Matrix m = comm.Receive <Matrix>(comm.Rank - 1, 1); comm.Send <Matrix>(m.Append(res), (comm.Rank + 1) % comm.Size, 1); } } }
public void MatrixMultiplicationTest() { BaseOperation operation = new MatrixMultiply(); operation.Matrix1 = new int[, ] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; operation.Matrix2 = new int[, ] { { 1, 2 }, { 3, 4 }, { 5, 6 } }; operation.Calculate(); Assert.AreEqual(new int[, ] { { 22, 28 }, { 49, 64 }, { 76, 100 } }, operation.MatrixResult); }
public void GetMatrixValueFromHistory() { Operations operation = new MatrixMultiply(); operation.Matrix1 = new int[, ] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; operation.Matrix2 = new int[, ] { { 1, 2 }, { 3, 4 }, { 5, 6 } }; operation.Calculate(); operation.SaveDisplayRes(); HistoryManager.AddLog(operation.calculation); var actual = HistoryManager.history.ElementAt(2); var expected = "matrix multiplication result : {1,2,3},{4,5,6},{7,8,9} * {1,2},{3,4},{5,6} = {22,28},{49,64},{76,100}"; Assert.AreEqual(expected, actual); }
public static unsafe void bb2() { Variable w1 = new Variable(new Dimension[] { 20, 10 }, new Shape(500, 300)); w1.Name = "w1"; Variable w2 = new Variable(new Dimension[] { 20, 10 }, new Shape(500, 300)); w2.Name = "w2"; Variable w3 = new Variable(new Dimension[] { 20, 10 }, new Shape(300, 400)); w3.Name = "w3"; Variable w4 = new Variable(new Dimension[] { 20, 10 }, new Shape(500, 400)); w4.Name = "w4"; var d1 = new Subtract(w1, w2); d1.Name = "d1"; var d2 = new MatrixMultiply(d1, w3); d2.Name = "d2"; var sum = new Power(new Subtract(d2, w4), 2); sum.Name = "sum"; //Shape s = Shape.NewShape(2, 10); //Index a = Index.NewIndex(s); sum.PreCheck(); //a.SetZero(); //Console.WriteLine("w: "); //for (int i = 0; i < s.TotalSize; i++, a.Add(1)) // Console.WriteLine(w.GetTerm(a).GetResult()); //a.SetZero(); //Console.WriteLine("w2: "); //for (int i = 0; i < s.TotalSize; i++, a.Add(1)) // Console.WriteLine(w2.GetTerm(a).GetResult()); //a.SetZero(); //Console.WriteLine("sum: "); //for (int i = 0; i < s.TotalSize; i++, a.Add(1)) // Console.WriteLine(sum.GetTerm(a).GetResult()); Hyperparameters.LearningRate = 0.02f; Stopwatch c = new Stopwatch(); for (int i2 = 0; i2 < 10; i2++) { c.Restart(); sum.Minimize(); c.Stop(); Console.WriteLine($"{i2} took {c.ElapsedMilliseconds}ms"); } //sum.PreCheck(); //a.SetZero(); //Console.WriteLine("w: "); //for (int i = 0; i < s.TotalSize; i++, a.Add(1)) // Console.WriteLine(w.GetTerm(a).GetResult()); //a.SetZero(); //Console.WriteLine("w2: "); //for (int i = 0; i < s.TotalSize; i++, a.Add(1)) // Console.WriteLine(w2.GetTerm(a).GetResult()); //a.SetZero(); //Console.WriteLine("sum: "); //for (int i = 0; i < s.TotalSize; i++, a.Add(1)) // Console.WriteLine(sum.GetTerm(a).GetResult()); //Index.Return(a); //Shape.Return(s); }
public override vectorT Evaluate() { matrix r = new MatrixMultiply(VT.ToMatrix(), M, R); return(new(r.Cols, r.ReuseArray())); }
public override vector Evaluate() { matrix r = new MatrixMultiply(M, V.ToMatrix(), R); return(new(r.Rows, r.ReuseArray())); }
static void Main(string[] args) { using (new MPI.Environment(ref args)) { if (args.Length != 1) { return; } const int matrixSize = 50; Intracommunicator comm = Communicator.world; int groupCount = int.Parse(args[0]); if (comm.Rank == 0) { DateTime startTime = DateTime.Now; MatrixMultiply testMultiply = new MatrixMultiply(comm.Size, matrixSize); Matrix result = testMultiply.MultiplyForRank(0); for (int i = 1; i < comm.Size; i++) { result.Append(testMultiply.MultiplyForRank(i)); } DateTime endTime = DateTime.Now; Console.WriteLine("Test multiply" + (startTime - endTime).ToString()); int workers = comm.Size - groupCount - 1; Console.WriteLine("All process count: " + (comm.Size).ToString()); Console.WriteLine("Workers count: " + (workers + groupCount).ToString()); Console.WriteLine("Group count" + (groupCount).ToString()); int workerIndex = groupCount + 1; int managersWithoutWorkers = groupCount; Request[] sendRequest = new Request[groupCount]; for (int i = 0; i < groupCount; i++) { int workersIntheGroup = (int)Math.Floor((double)(workers / managersWithoutWorkers)); MatrixMultiply mp = new MatrixMultiply(workersIntheGroup + 1, matrixSize) { WorkersList = new List <int>(), IsAManager = true }; for (int j = 0; j < workersIntheGroup; j++) { mp.WorkersList.Add(workerIndex); // Console.WriteLine("WorkerID " + workerIndex); workerIndex++; workers--; } managersWithoutWorkers--; Console.WriteLine("Group " + i.ToString() + " has " + (workersIntheGroup + 1).ToString() + "members"); sendRequest[i] = comm.ImmediateSend(mp, i + 1, 0); } Console.WriteLine("Sending the job"); for (int i = 0; i < groupCount; i++) { sendRequest[i].Wait(); } ReceiveRequest[] recieveRequest = new ReceiveRequest[groupCount]; for (int i = 0; i < groupCount; i++) { recieveRequest[i] = comm.ImmediateReceive <Matrix>(i + 1, 0); } Console.WriteLine("Recieve results"); for (int i = 0; i < groupCount; i++) { recieveRequest[i].Wait(); } //for (int i = 0; i < groupCount; i++) // { // var result = recieveRequest[i].GetValue(); // if(result == null) // { // Console.WriteLine("Null get"); // } // Console.WriteLine("Group " + i + " has finished in " + ((TimeSpan)recieveRequest[i].GetValue()).ToString()); //} } else { ReceiveRequest receiveRequest = comm.ImmediateReceive <MatrixMultiply>(Communicator.anySource, 0); receiveRequest.Wait(); MatrixMultiply mp = (MatrixMultiply)receiveRequest.GetValue(); if (mp.IsAManager) { DateTime startTime = DateTime.Now; mp.IsAManager = false; mp.Chef = comm.Rank; Request[] sendRequestToWorker = new Request[mp.WorkersList.Count]; int index = 0; //Console.WriteLine("MAnager " + mp.WorkersList.Count); foreach (int workerId in mp.WorkersList) { mp.workerId = index + 1; sendRequestToWorker[index] = comm.ImmediateSend(mp, workerId, 0); // Console.WriteLine("Worker " + mp.WorkersList.Count + " " + index); index++; } for (int i = 0; i < mp.WorkersList.Count; i++) { sendRequestToWorker[i].Wait(); } var result = mp.MultiplyForRank(0); ReceiveRequest[] receiveRequestFromWorkers = new ReceiveRequest[mp.WorkersList.Count]; for (int i = 0; i < mp.WorkersList.Count; i++) { receiveRequestFromWorkers[i] = comm.ImmediateReceive <Matrix>(mp.WorkersList.ToArray()[i], 0); } for (int i = 0; i < mp.WorkersList.Count; i++) { // Console.WriteLine("Waiting for " + mp.WorkersList.ToArray()[i]); receiveRequestFromWorkers[i].Wait(); } for (int i = 0; i < mp.WorkersList.Count; i++) { result.Append((Matrix)receiveRequestFromWorkers[i].GetValue()); } DateTime finishTime = DateTime.Now; TimeSpan resultTime = (finishTime - startTime); Request sendRequest = comm.ImmediateSend(resultTime, 0, 0); sendRequest.Wait(); Console.WriteLine("Group " + comm.Rank + " has done in " + resultTime); } else { //Console.WriteLine("Worker " + comm.Rank + "manager" + mp.Chef); mp.MultiplyForRank(mp.workerId); Request sendRequest = comm.ImmediateSend(mp.MultiplyForRank(mp.workerId), mp.Chef, 0); sendRequest.Wait(); } } } }