Пример #1
0
            public void WaitAll(Communicator comm)
            {
                RequestList requests = new RequestList();
                Dictionary <Request, EventWaitHandle> waitHandles = new Dictionary <Request, EventWaitHandle>();
                Dictionary <Request, string>          requestTags = new Dictionary <Request, string>();
                DateTime startTime  = DateTime.UtcNow;
                int      nextMinute = 1;
                var      workItems  = comm.Serialization.WorkItems;

                while (pendingCount > 0 || workItems.Count > 0 || requests.Count > 0)
                {
                    RequestGenerator workItem;
                    if (workItems.TryDequeue(out workItem))
                    {
                        var request = workItem.generator();
                        requests.Add(request);
                        waitHandles.Add(request, workItem.waitHandle);
                        requestTags.Add(request, workItem.Tag);
                    }
                    else
                    {
                        var request = requests.TestAny();
                        if (request != null)
                        {
                            waitHandles[request].Set();
                            waitHandles.Remove(request);
                            requestTags.Remove(request);
                        }
                    }
                    var elapsed = DateTime.UtcNow - startTime;
                    if (elapsed.TotalMinutes > nextMinute)
                    {
                        StringBuilder sb = new StringBuilder();
                        if (requestTags.Count > 0)
                        {
                            sb.Append(": ");
                            bool firstTime = true;
                            foreach (var tag in requestTags.Values)
                            {
                                if (!firstTime)
                                {
                                    sb.Append(",");
                                }
                                sb.Append(tag);
                                firstTime = false;
                            }
                        }
                        comm.Serialization.WaitLogger?.Invoke($"Waiting on {pendingCount} pending tasks, {workItems.Count} work items, {requests.Count} requests{sb.ToString()}");
                        nextMinute++;
                    }
                }
                if (waitHandles.Count != 0)
                {
                    throw new Exception("waitHandles.Count != 0");
                }
            }
Пример #2
0
    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);
            }
        }    
    }
Пример #3
0
        public void go_parallel()
        {
            breaking();

            string input = readInput(TEMP_FILE_PATH + Rank);            //(PATH);

            bool sair = false;

            while (!sair)
            {
                splitMap(input);
                input  = "";
                saidas = new Dictionary <int, string> ();
                sair   = true;
                IEnumerator <string> I = KVPair_OMK_OMV.getIteratorKeys();
                while (I.MoveNext())
                {
                    string key = I.Current;
                    reduce(key, KVPair_OMK_OMV.getList(key));
                    string[] words = valorORV.Split(' ');

                    int task_id = Math.Abs((words[0]).GetHashCode()) % Size;
                    if (task_id == Rank)
                    {
                        input = input + valorORV;
                    }
                    else
                    {
                        if (!saidas.ContainsKey(task_id))
                        {
                            saidas [task_id] = "";
                        }
                        saidas [task_id] = saidas [task_id] + valorORV;
                    }
                    if (sair && chaveORV.Equals("0"))
                    {
                        sair = false;
                    }
                }
                for (int i = 0; i < Size; i++)
                {
                    if (i != Rank)
                    {
                        if (!saidas.ContainsKey(i))
                        {
                            saidas [i] = "";
                        }
                        rlist.Add(worldcomm.ImmediateSend <string> (saidas [i], i, 0));
                    }
                }
                for (int i = 0; i < Size; i++)
                {
                    if (i != Rank)
                    {
                        input = input + worldcomm.Receive <string> (i, 0);
                    }
                }
                rlist.WaitAll();
                int exit = worldcomm.Allreduce <int> (sair ? 1 : 0, MPI.Operation <int> .Min);
                sair = exit == 1;
            }
            Randomize.clearFile(OUTPUT_FILE_PATH + Rank);
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(@OUTPUT_FILE_PATH + Rank, true)){
                file.WriteLine(input);
            }
        }