Exemplo n.º 1
0
        private void f(TcpListener listener, TcpClient client_)
        {
            bool tmpWorking = true;

            while (tmpWorking)
            {
                client.ReceiveBufferSize = NetworkBuffer.receiveSendBufferSize;
                client.SendBufferSize    = NetworkBuffer.receiveSendBufferSize;
                byte[] buffer = new byte[NetworkBuffer.receiveSendBufferSize];

                //---read incoming stream---
                int bytesRead  = NetworkBuffer.receiveSendBufferSize;
                int counter    = 0;
                int length     = -1;
                int bytesTotal = 0;
                nb.resetExceptArrays();
                NetworkStream nwStream = client.GetStream();
                while (!nwStream.DataAvailable)
                {
                    Thread.Sleep(1);
                }
                while (nwStream.CanRead && bytesRead > 0 && (length == -1 || bytesTotal < length))
                {
                    bytesRead = nwStream.Read(buffer, 0, NetworkBuffer.receiveSendBufferSize);


                    if (length == -1)
                    {
                        length = nb.readLengthOfBuffer(buffer);
                    }
                    if (length == 0)
                    {
                        Console.WriteLine("Server: couldn't receive buffer.");
                        return;
                    }
                    else
                    {
                        if (serverBuffer.Length < length)
                        {
                            serverBuffer = new byte[length];
                        }
                        Buffer.BlockCopy(buffer, 0, serverBuffer, bytesTotal, bytesRead);
                        bytesTotal += bytesRead;
                    }
                }

                List <NetworkBuffer.HashIndisSiraDizi> arrays = nb.oku(serverBuffer, "server");
                if (nb.bufferCommand() == NetworkBuffer.SETUP)
                {
                    string   deviceTypes            = new string((char[])arrays[0].backingArray);
                    string   kernelsString          = new string((char[])arrays[1].backingArray);
                    string[] kernelNamesStringArray = (new string((char[])arrays[2].backingArray)).Split(" ".ToCharArray());
                    int      localRAnge             = ((int[])arrays[3].backingArray)[0];
                    int      numberOfGPUsToUse      = ((int[])arrays[4].backingArray)[0];
                    bool     GPU_STREAM             = ((bool[])arrays[5].backingArray)[0];
                    int      MAX_CPU = ((int[])arrays[6].backingArray)[0];
                    openclSystemToCrunchNumbers = new Cores(
                        deviceTypes, kernelsString,
                        kernelNamesStringArray, false, localRAnge,
                        numberOfGPUsToUse, GPU_STREAM,
                        MAX_CPU);
                    if (openclSystemToCrunchNumbers.errorCode() != 0)
                    {
                        Console.WriteLine("Compiling error!");
                        Console.WriteLine(openclSystemToCrunchNumbers.errorMessage());
                        openclSystemToCrunchNumbers.dispose();
                        return;
                    }

                    NetworkBuffer nbAnswer    = new NetworkBuffer(NetworkBuffer.ANSWER_SUCCESS);
                    byte[]        bytesToSend = nbAnswer.buf();
                    int           pos         = 0;
                    while (pos < bytesToSend.Length)
                    {
                        int l = Math.Min(bytesToSend.Length - pos, client.ReceiveBufferSize);
                        nwStream.Write(bytesToSend, pos, l);
                        pos += l;
                    }
                    Console.WriteLine("--------------------------------------------------");
                }
                else if (nb.bufferCommand() == NetworkBuffer.COMPUTE)
                {
                    string[] kernelNamesString_ = new string((char[])arrays[0].backingArray).Split(" ".ToCharArray());
                    int      numberOfSteps      = ((int[])arrays[1].backingArray)[0];
                    string   stepFunction       = new string((char[])arrays[2].backingArray);

                    int numberOfArrays = ((int[])arrays[3].backingArray)[0];



                    string[] readWrite = new string[numberOfArrays];
                    for (int i = 0; i < numberOfArrays; i++)
                    {
                        readWrite[i] = new string((char[])arrays[numberOfArrays + 4 + i].backingArray);
                    }


                    int[] arrayElementsPerWorkItem = new int[numberOfArrays];
                    for (int i = 0; i < numberOfArrays; i++)
                    {
                        arrayElementsPerWorkItem[i] = ((int[])arrays[numberOfArrays * 2 + 4].backingArray)[i];
                    }

                    int      totalGlobalRange      = ((int[])arrays[numberOfArrays * 2 + 5].backingArray)[0];
                    int      computeId             = ((int[])arrays[numberOfArrays * 2 + 6].backingArray)[0];
                    int      globalRangeOffset     = ((int[])arrays[numberOfArrays * 2 + 7].backingArray)[0];
                    bool     pipelineEnabled       = ((bool[])arrays[numberOfArrays * 2 + 8].backingArray)[0];
                    int      pipelineNumberOfBlobs = ((int[])arrays[numberOfArrays * 2 + 9].backingArray)[0];
                    bool     pipelineType          = ((bool[])arrays[numberOfArrays * 2 + 10].backingArray)[0];
                    object[] tmpArrays             = new object[numberOfArrays];
                    int[]    tmpHashValues         = new int[numberOfArrays];
                    for (int o = 0; o < numberOfArrays; o++)
                    {
                        tmpArrays[o]     = arrays[4 + o].backingArray;
                        tmpHashValues[o] = arrays[4 + o].hash_;
                        if (!d0.ContainsKey(tmpHashValues[o]))
                        {
                            d0.Add(tmpHashValues[o], tmpArrays[o]);
                            d1.Add(tmpArrays[o], tmpHashValues[o]);
                        }
                    }

                    openclSystemToCrunchNumbers.compute(kernelNamesString_, numberOfSteps, stepFunction, tmpArrays, readWrite,
                                                        arrayElementsPerWorkItem, totalGlobalRange, computeId,
                                                        globalRangeOffset, pipelineEnabled, pipelineNumberOfBlobs, pipelineType);
                    openclSystemToCrunchNumbers.performanceReport(computeId);


                    // todo: "true ||" must be deleted but then array update is not working on same array because hash values don't match
                    //            /
                    //           /
                    //          /
                    //         /
                    //        |
                    //        v
                    if (true || nbComputeAnswer == null)
                    {
                        nbComputeAnswer = new NetworkBuffer(NetworkBuffer.ANSWER_COMPUTE_COMPLETE);
                        for (int m = 0; m < numberOfArrays; m++)
                        {
                            if (tmpArrays[m].GetType() == typeof(float[]))
                            {
                                nbComputeAnswer.addArray((float[])tmpArrays[m], tmpHashValues[m],
                                                         globalRangeOffset, totalGlobalRange, arrayElementsPerWorkItem[m]);
                            }
                            else if (tmpArrays[m].GetType() == typeof(int[]))
                            {
                                nbComputeAnswer.addComputeSteps((int[])tmpArrays[m], tmpHashValues[m]);
                            }
                            else if (tmpArrays[m].GetType() == typeof(byte[]))
                            {
                                nbComputeAnswer.addArray((byte[])tmpArrays[m], tmpHashValues[m]);
                            }
                            else if (tmpArrays[m].GetType() == typeof(char[]))
                            {
                                nbComputeAnswer.addCompute((char[])tmpArrays[m], tmpHashValues[m]);
                            }
                            else if (tmpArrays[m].GetType() == typeof(double[]))
                            {
                                nbComputeAnswer.addArray((double[])tmpArrays[m], tmpHashValues[m]);
                            }
                            else if (tmpArrays[m].GetType() == typeof(long[]))
                            {
                                nbComputeAnswer.addArray((long[])tmpArrays[m], tmpHashValues[m]);
                            }
                        }
                    }
                    else
                    {
                        for (int m = 0; m < numberOfArrays; m++)
                        {
                            if (tmpArrays[m].GetType() == typeof(float[]))
                            {
                                nbComputeAnswer.update((float[])tmpArrays[m], tmpHashValues[m],
                                                       globalRangeOffset, totalGlobalRange, arrayElementsPerWorkItem[m]);
                            }
                            else if (tmpArrays[m].GetType() == typeof(int[]))
                            {
                                nbComputeAnswer.update((int[])tmpArrays[m], tmpHashValues[m]);
                            }
                            else if (tmpArrays[m].GetType() == typeof(byte[]))
                            {
                                nbComputeAnswer.update((byte[])tmpArrays[m], tmpHashValues[m]);
                            }
                            else if (tmpArrays[m].GetType() == typeof(char[]))
                            {
                                nbComputeAnswer.update((char[])tmpArrays[m], tmpHashValues[m]);
                            }
                            else if (tmpArrays[m].GetType() == typeof(double[]))
                            {
                                nbComputeAnswer.update((double[])tmpArrays[m], tmpHashValues[m]);
                            }
                            else if (tmpArrays[m].GetType() == typeof(long[]))
                            {
                                nbComputeAnswer.update((long[])tmpArrays[m], tmpHashValues[m]);
                            }
                        }
                    }

                    byte[] bytesToSend = nbComputeAnswer.buf();
                    int    pos         = 0;
                    while (pos < bytesToSend.Length)
                    {
                        int l = Math.Min(bytesToSend.Length - pos, NetworkBuffer.receiveSendBufferSize);
                        nwStream.Write(bytesToSend, pos, l);
                        pos += l;
                    }
                }
                else if (nb.bufferCommand() == NetworkBuffer.DISPOSE)
                {
                    Console.WriteLine("Server-side Cekirdekler API is being deleted");
                    openclSystemToCrunchNumbers.dispose();

                    NetworkBuffer nbAnswer    = new NetworkBuffer(NetworkBuffer.ANSWER_DELETED);
                    byte[]        bytesToSend = nbAnswer.buf();
                    int           pos         = 0;
                    while (pos < bytesToSend.Length)
                    {
                        Console.WriteLine("server: writing segment");
                        int l = Math.Min(bytesToSend.Length - pos, NetworkBuffer.receiveSendBufferSize);
                        nwStream.Write(bytesToSend, pos, l);
                        pos += l;
                        Console.WriteLine("server: segment written: " + pos + "  " + bytesToSend.Length);
                    }
                    Console.WriteLine("--------------------------------------------------");
                    tmpWorking = false;
                    dispose();
                }
                else if (nb.bufferCommand() == NetworkBuffer.SERVER_STOP)
                {
                    Console.WriteLine("stopping server");
                    NetworkBuffer nbAnswer    = new NetworkBuffer(NetworkBuffer.ANSWER_STOPPED);
                    byte[]        bytesToSend = nbAnswer.buf();
                    int           pos         = 0;
                    while (pos < bytesToSend.Length)
                    {
                        Console.WriteLine("server: writing segment");
                        int l = Math.Min(bytesToSend.Length - pos, NetworkBuffer.receiveSendBufferSize);
                        nwStream.Write(bytesToSend, pos, l);
                        pos += l;
                        Console.WriteLine("server: segment written: " + pos + "  " + bytesToSend.Length);
                    }
                    Console.WriteLine("--------------------------------------------------");
                    tmpWorking = false;
                    server.stop();
                }
                else if (nb.bufferCommand() == NetworkBuffer.SERVER_CONTROL)
                {
                    Console.WriteLine("controlling server");
                    NetworkBuffer nbAnswer    = new NetworkBuffer(NetworkBuffer.ANSWER_CONTROL);
                    byte[]        bytesToSend = nbAnswer.buf();
                    int           pos         = 0;
                    while (pos < bytesToSend.Length)
                    {
                        Console.WriteLine("server: writing segment");
                        int l = Math.Min(bytesToSend.Length - pos, NetworkBuffer.receiveSendBufferSize);
                        nwStream.Write(bytesToSend, pos, l);
                        pos += l;
                        Console.WriteLine("server: segment written: " + pos + "  " + bytesToSend.Length);
                    }
                    Console.WriteLine("--------------------------------------------------");
                }
                else if (nb.bufferCommand() == NetworkBuffer.SERVER_NUMBER_OF_DEVICES)
                {
                    Console.WriteLine("receiving number of devices in server");
                    NetworkBuffer nbAnswer    = new NetworkBuffer(NetworkBuffer.ANSWER_NUMBER_OF_DEVICES);
                    int[]         numDevices_ = new int[1];
                    numDevices_[0] = openclSystemToCrunchNumbers.numberOfDevices();
                    nbAnswer.addComputeSteps(numDevices_, numDevices_.GetHashCode());
                    byte[] bytesToSend = nbAnswer.buf();
                    int    pos         = 0;
                    while (pos < bytesToSend.Length)
                    {
                        Console.WriteLine("server: writing segment");
                        int l = Math.Min(bytesToSend.Length - pos, NetworkBuffer.receiveSendBufferSize);
                        nwStream.Write(bytesToSend, pos, l);
                        pos += l;
                        Console.WriteLine("server: segment written: " + pos + "  " + bytesToSend.Length);
                    }
                    Console.WriteLine("--------------------------------------------------");
                }
            }
        }
        public void compute(string[] kernelNameStringArray = null,
                            int numberOfSteps = 0, string stepFunction    = "",
                            object[] arrays_  = null, string[] readWrite_ = null,
                            int[] arrayElementsPerWorkItem = null,
                            int totalGlobalRange_          = 1024, int computeId_     = 1,
                            int offsetValueForGlobalRange  = 0, bool pipelineEnabled_ = false,
                            int pipelineBlobCount          = 4, bool pipelineType     = Cores.PIPELINE_EVENT)
        {
            pipelineEnabled        = pipelineEnabled_;
            this.pipelineBlobCount = pipelineBlobCount;
            if (stepsNum == null || stepsNum.Length != clients.Count)
            {
                stepsNum = new int[clients.Count];
                for (int i = 0; i < clients.Count; i++)
                {
                    stepsNum[i] = clients[i].numDevices() * localRangeValue;
                    if (pipelineEnabled_)
                    {
                        stepsNum[i] *= pipelineBlobCount;
                    }
                }
            }
            if (swCompute == null)
            {
                swCompute = new Stopwatch();
            }
            if (offsetValues == null || offsetValues.Length != clients.Count)
            {
                offsetValues = new int[clients.Count];
            }
            if (swMainframe == null)
            {
                swMainframe = new Stopwatch();
            }
            if (timers == null)
            {
                timers = new Stopwatch[clients.Count];
                for (int i = 0; i < clients.Count; i++)
                {
                    timers[i] = new Stopwatch();
                }
            }
            if (latestGlobalRanges == null)
            {
                latestGlobalRanges = new Dictionary <int, int[]>();
            }
            if (latestTimings == null)
            {
                latestTimings = new Dictionary <int, double[]>();
                if (latestTimings.ContainsKey(computeId_))
                {
                    for (int i = 0; i < clients.Count; i++)
                    {
                        latestTimings[computeId_][i] = 0.01;
                    }
                }
                else
                {
                    latestTimings.Add(computeId_, new double[clients.Count]);

                    for (int i = 0; i < clients.Count; i++)
                    {
                        latestTimings[computeId_][i] = 0.01;
                    }
                }
            }

            if (loadBalancers == null)
            {
                loadBalancers = new Dictionary <int, ClusterLoadBalancer>();
            }
            if (mainframeThreads == null)
            {
                mainframeThreads = new Dictionary <int, int>();
            }
            if (mainframeTimings == null)
            {
                mainframeTimings = new Dictionary <int, double>();
            }
            if (!loadBalancers.ContainsKey(computeId_))
            {
                loadBalancers.Add(computeId_, new ClusterLoadBalancer());
            }


            if (!latestGlobalRanges.ContainsKey(computeId_))
            {
                int[] tmpRanges = new int[clients.Count];
                if (mainframeThreads.ContainsKey(computeId_))
                {
                    mainframeThreads[computeId_] = loadBalancers[computeId_].dengeleEsit(totalGlobalRange_, tmpRanges, stepsNum);
                }
                else
                {
                    mainframeThreads.Add(computeId_, loadBalancers[computeId_].dengeleEsit(totalGlobalRange_, tmpRanges, stepsNum));
                }
                latestGlobalRanges.Add(computeId_, tmpRanges);
            }
            else
            {
                if (clients.Count != latestGlobalRanges[computeId_].Length)
                {
                    int[] tmpRanges = new int[clients.Count];

                    if (mainframeThreads.ContainsKey(computeId_))
                    {
                        mainframeThreads[computeId_] = loadBalancers[computeId_].dengeleEsit(totalGlobalRange_, tmpRanges, stepsNum);
                    }
                    else
                    {
                        mainframeThreads.Add(computeId_, loadBalancers[computeId_].dengeleEsit(totalGlobalRange_, tmpRanges, stepsNum));
                    }

                    latestGlobalRanges[computeId_] = tmpRanges;
                }
                else
                {
                    if (mainframeThreads.ContainsKey(computeId_))
                    {
                        mainframeThreads[computeId_] =
                            loadBalancers[computeId_].balanceOnPerformances(latestTimings[computeId_],
                                                                            totalGlobalRange_, latestGlobalRanges[computeId_],
                                                                            stepsNum, mainframeThreads[computeId_],
                                                                            mainframeTimings[computeId_]);
                    }
                    else
                    {
                        mainframeThreads.Add(computeId_, loadBalancers[computeId_].balanceOnPerformances(latestTimings[computeId_],
                                                                                                         totalGlobalRange_, latestGlobalRanges[computeId_],
                                                                                                         stepsNum, mainframeThreads[computeId_],
                                                                                                         mainframeTimings[computeId_]));
                    }
                }
            }

            int tmpRef = offsetValueForGlobalRange;

            for (int i = 0; i < clients.Count; i++)
            {
                offsetValues[i] = tmpRef;
                tmpRef         += latestGlobalRanges[computeId_][i];
            }


            swCompute.Reset();
            swCompute.Start();
            Parallel.For(0, clients.Count + 1, i => {
                if (i < clients.Count)
                {
                    timers[i].Reset();
                    timers[i].Start();
                    clients[i].compute(
                        kernelNameStringArray,
                        numberOfSteps, stepFunction,
                        arrays_, readWrite_,
                        arrayElementsPerWorkItem,
                        latestGlobalRanges[computeId_][i], computeId_,
                        offsetValues[i], pipelineEnabled_,
                        pipelineBlobCount, pipelineType);
                    timers[i].Stop();

                    if (latestTimings.ContainsKey(computeId_))
                    {
                        latestTimings[computeId_][i] = 0.01 + ((double)timers[i].ElapsedMilliseconds);
                    }
                    else
                    {
                        latestTimings.Add(computeId_, new double[clients.Count]);

                        latestTimings[computeId_][i] = 0.01 + ((double)timers[i].ElapsedMilliseconds);
                    }
                }
                else
                {
                    swMainframe.Reset();
                    swMainframe.Start();
                    if (mainframeThreads[computeId_] > 0)
                    {
                        mainframeComputer.compute(kernelNameStringArray,
                                                  numberOfSteps, stepFunction,
                                                  arrays_, readWrite_,
                                                  arrayElementsPerWorkItem,
                                                  mainframeThreads[computeId_], computeId_,
                                                  tmpRef, pipelineEnabled_,
                                                  pipelineBlobCount, pipelineType);
                    }
                    swMainframe.Stop();
                    if (mainframeTimings.ContainsKey(computeId_))
                    {
                        mainframeTimings[computeId_] = 0.01 + (double)swMainframe.ElapsedMilliseconds;
                    }
                    else
                    {
                        mainframeTimings.Add(computeId_, 0.01 + (double)swMainframe.ElapsedMilliseconds);
                    }
                }
            });
            swCompute.Stop();
            Console.WriteLine("Hesap süresi=" + swCompute.ElapsedMilliseconds + "ms");
        }