/// <summary> /// Method that receives messages /// </summary> private void ReceiveMessages() { while (ContinueExecution) { // Receive next message and handle it ReceiveRequest asyncReceive = null; lock (comm) { // starts an asynchronous receive of the next message // Note: Must be asynchronous otherwise the lock of the comm object may cause a dead lock (unable to send message because we are waiting for one) asyncReceive = comm.ImmediateReceive <Message>(Communicator.anySource, 0); } // Creates a task that waits for the result to arrive. // Note : We must use a task because the Wait function of a task allows for a timeout parameter which we need to validate if the execution is still ongoing Task waitForMessage = new Task(() => { asyncReceive.Wait(); }); waitForMessage.Start(); // waits for a message to be received, stops (within 200ms) if the execution stops while (ContinueExecution && !waitForMessage.Wait(200)) { ; } if (!ContinueExecution) { asyncReceive.Cancel(); // We need to wait for the asyncReceive cancellation because the completion of this thread will close MPI but we need to make sure the cancel is over by then. asyncReceive.Wait(); break; // if the execution is over, the message may not be valid and should be ignored. } messageQueue.Add((Message)asyncReceive.GetValue()); } // Signal the main MapManager thread that this thread is correctly closed and we won't receive any other messages messageQueue.CompleteAdding(); }
static void TestRequests(Intracommunicator comm, RequestList requestList) { int datum = comm.Rank; int expectedDatum = (comm.Rank + comm.Size - 1) % comm.Size; int[] intArraySendBuffer = new int[comm.Rank + 1]; string[] strArraySendBuffer = new string[comm.Rank + 1]; for (int i = 0; i <= comm.Rank; ++i) { intArraySendBuffer[i] = i; strArraySendBuffer[i] = i.ToString(); } int[] intArrayRecvBuffer = new int[expectedDatum + 1]; string[] strArrayRecvBuffer = new string[expectedDatum + 1]; Request[] requests = new Request[8]; requests[0] = comm.ImmediateReceive <int>(Communicator.anySource, 0); requests[1] = comm.ImmediateReceive <string>(Communicator.anySource, 1); requests[2] = comm.ImmediateReceive(Communicator.anySource, 2, intArrayRecvBuffer); requests[3] = comm.ImmediateReceive(Communicator.anySource, 3, strArrayRecvBuffer); requests[4] = comm.ImmediateSend(datum, (comm.Rank + 1) % comm.Size, 0); requests[5] = comm.ImmediateSend(datum.ToString(), (comm.Rank + 1) % comm.Size, 1); requests[6] = comm.ImmediateSend(intArraySendBuffer, (comm.Rank + 1) % comm.Size, 2); requests[7] = comm.ImmediateSend(strArraySendBuffer, (comm.Rank + 1) % comm.Size, 3); if (requestList == null) { // Complete all communications manually bool allDone = false; while (!allDone) { allDone = true; for (int i = 0; i < requests.Length; ++i) { allDone = allDone && requests[i].Test() != null; } } } else { // Use the request list to complete all communications for (int i = 0; i < requests.Length; ++i) { requestList.Add(requests[i]); } requestList.WaitAll(); } ReceiveRequest intRecv = (ReceiveRequest)requests[0]; CompletedStatus intStatus = intRecv.Wait(); if ((int)intRecv.GetValue() != expectedDatum || intStatus.Source != expectedDatum || intStatus.Tag != 0) { System.Console.Error.WriteLine("error in non-blocking receive of integer: got " + (int)intRecv.GetValue() + " from " + intStatus.Source + " on tag " + intStatus.Tag + ", expected " + expectedDatum); MPI.Environment.Abort(-1); } ReceiveRequest strRecv = (ReceiveRequest)requests[1]; CompletedStatus strStatus = strRecv.Wait(); if ((string)strRecv.GetValue() != expectedDatum.ToString() || strStatus.Source != expectedDatum || strStatus.Tag != 1) { System.Console.Error.WriteLine("error in non-blocking receive of string: got " + strRecv.GetValue() + " from " + strStatus.Source + " on tag " + strStatus.Tag + ", expected " + expectedDatum); MPI.Environment.Abort(-1); } ReceiveRequest intArrayRecv = (ReceiveRequest)requests[2]; CompletedStatus intArrayStatus = intArrayRecv.Wait(); if (intArrayRecv.GetValue() != intArrayRecvBuffer || intArrayStatus.Source != expectedDatum || intArrayStatus.Tag != 2) { System.Console.WriteLine("error: received into the wrong integer array"); MPI.Environment.Abort(-1); } for (int i = 0; i <= expectedDatum; ++i) { if (intArrayRecvBuffer[i] != i) { System.Console.WriteLine("error: intArrayRecv[" + i + "] is " + intArrayRecvBuffer[i] + ", expected " + i); MPI.Environment.Abort(-1); } } ReceiveRequest strArrayRecv = (ReceiveRequest)requests[3]; CompletedStatus strArrayStatus = strArrayRecv.Wait(); if (strArrayRecv.GetValue() != strArrayRecvBuffer || strArrayStatus.Source != expectedDatum || strArrayStatus.Tag != 3) { System.Console.WriteLine("error: received into the wrong string array"); MPI.Environment.Abort(-1); } for (int i = 0; i <= expectedDatum; ++i) { if (strArrayRecvBuffer[i] != i.ToString()) { System.Console.WriteLine("error: strArrayRecv[" + i + "] is " + strArrayRecvBuffer[i] + ", expected " + i.ToString()); MPI.Environment.Abort(-1); } } }
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(); } } } }
public override void main() { Console.WriteLine(this.PeerRank + ": HELLO ! I AM RIGHT !!!"); /* // RECEIVE ARRAY * Tuple<int,int> source0 = new Tuple<int,int> (0, this.PeerRank); * string[] s0 = new string[1]; this.Binding.Receive<string> (source0, 0, ref s0); * Console.WriteLine (this.PeerRank + "- RECEIVED right " + s0 + " from " + source0); * * Tuple<int,int> source1 = new Tuple<int,int> (0, this.PeerRank); * string[] s1 = new string[1]; this.Binding.Receive<string> (source1, 1, ref s1); * Console.WriteLine (this.PeerRank + "- RECEIVED right " + s1 + " from " + source1); * * // RECEIVE SINGLETON * Tuple<int,int> source2 = new Tuple<int,int> (0, this.PeerRank); * string s2 = this.Binding.Receive<string> (source2, 2); * Console.WriteLine (this.PeerRank + "- RECEIVED right " + s2 + " from " + source2); * * Tuple<int,int> source3 = new Tuple<int,int> (0, this.PeerRank); * string s3 = this.Binding.Receive<string> (source3, 3); * Console.WriteLine (this.PeerRank + "- RECEIVED right " + s3 + " from " + source3); */ // ------------------------------- // RECEIVE IMMEDIATE ARRAY string[] b0 = new string[1]; Tuple <int, int> source4 = new Tuple <int, int> (0, this.PeerRank); ReceiveRequest r0 = this.Binding.ImmediateReceive <string> (source4, 44, b0); string[] b1 = new string[1]; Tuple <int, int> source5 = new Tuple <int, int> (0, this.PeerRank); ReceiveRequest r1 = this.Binding.ImmediateReceive <string> (source5, 55, b1); // RECEIVE IMMEDIATE SINGLETON Tuple <int, int> source6 = new Tuple <int, int> (0, this.PeerRank); ReceiveRequest r2 = this.Binding.ImmediateReceive <string> (source6, 66); Tuple <int, int> source7 = new Tuple <int, int> (0, this.PeerRank); ReceiveRequest r3 = this.Binding.ImmediateReceive <string> (source7, 77); CompletedStatus t0 = r0.Wait(); string[] s4 = r0.GetValue() as string[]; Console.WriteLine(this.PeerRank + "- RECEIVED right " + b0[0] + " from " + t0.Source + " tag=" + t0.Tag + " cancelled=" + t0.Cancelled + " count=" + t0.Count()); CompletedStatus t1 = r1.Wait(); string[] s5 = r1.GetValue() as string[]; Console.WriteLine(this.PeerRank + "- RECEIVED right " + b1[0] + " from " + t1.Source + " tag=" + t1.Tag + " cancelled=" + t1.Cancelled + " count=" + t1.Count()); CompletedStatus t2 = r2.Wait(); string s6 = r2.GetValue() as string; Console.WriteLine(this.PeerRank + "- RECEIVED right " + s6 + " from " + t2.Source + " tag=" + t2.Tag + " cancelled=" + t2.Cancelled + " count=" + t2.Count()); CompletedStatus t3 = r3.Wait(); string s7 = r3.GetValue() as string; Console.WriteLine(this.PeerRank + "- RECEIVED right " + s7 + " from " + t3.Source + " tag=" + t3.Tag + " cancelled=" + t3.Cancelled + " count=" + t3.Count()); }
static void Main(string[] args) { //using (new MPI.Environment(ref args)) //{ // Intracommunicator comm = Communicator.world; // if (comm.Rank == 0) // { // MatrixMultiply mp = new MatrixMultiply(comm.Size, 2); // mp.Show(); // Request sendRequest = comm.ImmediateSend(mp, 1, 0); // Console.WriteLine("Sending MatrixMyltiply"); // Matrix res = mp.MultiplyForRank(comm.Rank); // sendRequest.Wait(); // ReceiveRequest recieveRequest = comm.ImmediateReceive<MatrixMultiply>(Communicator.anySource, 0); // recieveRequest.Wait(); // Console.WriteLine("Recieve MatrixMultiply"); // sendRequest = comm.ImmediateSend(res, 1, 1); // Console.WriteLine("Sending Matrix result"); // sendRequest.Wait(); // recieveRequest = comm.ImmediateReceive<Matrix>(Communicator.anySource, 1); // recieveRequest.Wait(); // res = (Matrix)recieveRequest.GetValue(); // Console.WriteLine("Recieve Matrix result"); // res.Show(); // } // else // { // ReceiveRequest receiveRequest = comm.ImmediateReceive<MatrixMultiply>(comm.Rank - 1, 0); // receiveRequest.Wait(); // MatrixMultiply mp = (MatrixMultiply)receiveRequest.GetValue(); // Request sendRequest = comm.ImmediateSend(mp, (comm.Rank + 1) % comm.Size, 0); // Matrix res = mp.MultiplyForRank(comm.Rank); // sendRequest.Wait(); // receiveRequest = comm.ImmediateReceive<Matrix>(comm.Rank - 1, 1); // receiveRequest.Wait(); // receiveRequest.GetValue(); // sendRequest = comm.ImmediateSend<Matrix>(((Matrix)receiveRequest.GetValue()).Append(res), (comm.Rank + 1) % comm.Size, 1); // sendRequest.Wait(); // } //} using (new MPI.Environment(ref args)) { const int matrixSize = 50; Intracommunicator comm = Communicator.world; 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()); startTime = DateTime.Now; MatrixMultiply mp = new MatrixMultiply(comm.Size, matrixSize); // mp.Show(); Request[] sendRequest = new Request[comm.Size]; for (int i = 1; i < comm.Size; i++) { sendRequest[i] = comm.ImmediateSend(mp, i, 0); } Console.WriteLine("Sending partly MatrixMyltiply"); Matrix res = mp.MultiplyForRank(comm.Rank); for (int i = 1; i < comm.Size; i++) { sendRequest[i].Wait(); } ReceiveRequest[] recieveRequest = new ReceiveRequest[comm.Size]; for (int i = 1; i < comm.Size; i++) { recieveRequest[i] = comm.ImmediateReceive <Matrix>(i, 1); } Console.WriteLine("Recieve partly MatrixMultiply"); for (int i = 1; i < comm.Size; i++) { recieveRequest[i].Wait(); } for (int i = 1; i < comm.Size; i++) { res.Append((Matrix)recieveRequest[i].GetValue()); } //Console.WriteLine("Result is"); // res.Show(); endTime = DateTime.Now; Console.WriteLine("Parallel multiply" + (startTime - endTime).ToString()); } else { ReceiveRequest receiveRequest = comm.ImmediateReceive <MatrixMultiply>(0, 0); receiveRequest.Wait(); MatrixMultiply mp = (MatrixMultiply)receiveRequest.GetValue(); Request sendRequest = comm.ImmediateSend(mp.MultiplyForRank(comm.Rank), 0, 1); sendRequest.Wait(); } } }