/// <summary> /// <para>prepares devices and compiles kernels in the kernel string</para> /// <para>does optionally pipelined kernel execution load balancing between multiple devices</para> /// </summary> /// <param name="cpuGpu">AcceleratorType.CPU|AcceleratorType.GPU or similar</param> /// <param name="kernelString">something like: @"multi-line C# string that has multiple kernel definitions"</param> /// <param name="numberofCPUCoresToUseAsDeviceFission">AcceleratorType.CPU uses number of threads for an N-core CPU(between 1 and N-1)(-1 means N-1)</param> /// <param name="numberOfGPUsToUse">AcceleratorType.GPU uses number of GPUs equal to this parameter. Between 1 and N(-1 means N)</param> /// <param name="stream">devices that share RAM with CPU will not do extra copies. Devices that don't share RAM will directly access RAM and reduce number of copies</param> /// <param name="noPipelining">disables extra command queue allocation, can't enable driver-driven pipelining later. Useful for device to device pipelining with many stages.</param> public ClNumberCruncher(AcceleratorType cpuGpu, string kernelString, int numberofCPUCoresToUseAsDeviceFission = -1, int numberOfGPUsToUse = -1, bool stream = true, bool noPipelining = false) { bool defaultQueue = false; if (kernelString.Contains("enqueue_kernel(")) { defaultQueue = true; } repeatCount = 1; numberOfErrorsHappened = 0; StringBuilder cpuGpu_ = new StringBuilder(""); if (((int)cpuGpu & ((int)AcceleratorType.CPU)) > 0) { cpuGpu_.Append("cpu "); } if (((int)cpuGpu & ((int)AcceleratorType.GPU)) > 0) { cpuGpu_.Append("gpu "); } if (((int)cpuGpu & ((int)AcceleratorType.ACC)) > 0) { cpuGpu_.Append("acc "); } List <string> kernelNames_ = new List <string>(); // extracting patterns kernel _ _ _ void _ _ name _ _ ( string kernelVoidRegex = "(kernel[\\s]+void[\\s]+[a-zA-Z\\d_]+[^\\(])"; Regex regex = new Regex(kernelVoidRegex); MatchCollection match = regex.Matches(kernelString); for (int i = 0; i < match.Count; i++) { // extracting name Regex rgx = new Regex("([\\s]+[a-zA-Z\\d_]+)"); MatchCollection mc = rgx.Matches(match[i].Value.Trim()); kernelNames_.Add(mc[mc.Count - 1].Value.Trim()); } if (kernelNames_.Count == 0) { Console.WriteLine("Error: no kernel definitions are found in string. Kernel string: \n" + kernelString); errorNotification = 1; return; } numberCruncher = new Cores(cpuGpu_.ToString(), kernelString, kernelNames_.ToArray(), defaultQueue, 256, numberOfGPUsToUse, stream, numberofCPUCoresToUseAsDeviceFission, noPipelining); if (numberCruncher.errorCode() != 0) { errorMessage_ = numberCruncher.errorMessage(); Console.WriteLine(numberCruncher.errorMessage()); errorNotification = numberCruncher.errorCode(); numberCruncher.dispose(); numberOfErrorsHappened++; return; } }
/// <summary> /// <para>prepares devices and compiles kernels in the kernel string</para> /// <para>does optionally pipelined kernel execution load balancing between multiple devices</para> /// </summary> /// <param name="devicesForGPGPU">one or more devices for GPGPU</param> /// <param name="kernelString">something like: @"multi-line C# string that has multiple kernel definitions"</param> /// <param name="noPipelining">disables extra command queue allocation, can't enable driver-driven pipelining later. Useful for device to device pipelining with many stages.</param> /// <param name="computeQueueConcurrency">max number of command queues to send commands asynchronously, max=16, min=1</param> public ClNumberCruncher(ClDevices devicesForGPGPU, string kernelString, bool noPipelining = false, int computeQueueConcurrency = 16) { bool defaultQueue = false; if (kernelString.Contains("enqueue_kernel")) { defaultQueue = true; } repeatCount = 1; numberOfErrorsHappened = 0; List <string> kernelNames_ = new List <string>(); // extracting patterns kernel _ _ _ void _ _ name _ _ ( string kernelVoidRegex = "(kernel[\\s]+void[\\s]+[a-zA-Z\\d_]+[^\\(])"; Regex regex = new Regex(kernelVoidRegex); MatchCollection match = regex.Matches(kernelString); for (int i = 0; i < match.Count; i++) { // extracting name Regex rgx = new Regex("([\\s]+[a-zA-Z\\d_]+)"); MatchCollection mc = rgx.Matches(match[i].Value.Trim()); kernelNames_.Add(mc[mc.Count - 1].Value.Trim()); } if (kernelNames_.Count == 0) { Console.WriteLine("Error: no kernel definitions are found in string. Kernel string: \n" + kernelString); errorNotification = 1; return; } numberCruncher = new Cores(devicesForGPGPU, kernelString, kernelNames_.ToArray(), defaultQueue, computeQueueConcurrency, noPipelining); if (numberCruncher.errorCode() != 0) { errorMessage_ = numberCruncher.errorMessage(); Console.WriteLine(numberCruncher.errorMessage()); errorNotification = numberCruncher.errorCode(); numberCruncher.dispose(); numberOfErrorsHappened++; return; } }
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("--------------------------------------------------"); } } }