コード例 #1
0
        static void Main(string[] args)
        {
            Console.CancelKeyPress += delegate {
                Console.WriteLine("Ctrl+C - Exitting");
                if (gc != null && gc.IsConnected)
                {
                    gc.GrinClose();
                }
                if (cuda != null && !cuda.HasExited)
                {
                    cuda.Kill();
                }

                Environment.Exit(0);
            };

            System.Console.InputEncoding = System.Text.Encoding.ASCII;
            DateTime s      = DateTime.Now;
            var      parser = new SimpleCommandLineParser();

            parser.Parse(args);

            if (parser.Contains("t"))
            {
                Test = true;

                /*
                 *  src/cuckaroo$ ./cuda29 -n 77
                 *  GeForce GTX 1070 with 8119MB @ 256 bits x 4004MHz
                 *  Looking for 42-cycle on cuckaroo29("",77) with 50% edges, 64*64 buckets, 176 trims, and 64 thread blocks.
                 *  Using 6976MB of global memory.
                 *  nonce 77 k0 k1 k2 k3 f4956dc403730b01 e6d45de39c2a5a3e cbf626a8afee35f6 4307b94b1a0c9980
                 *  Seeding completed in 355 + 131 ms
                 *  67962 edges after trimming
                 *    8-cycle found
                 *    12-cycle found
                 *    42-cycle found
                 *  findcycles edges 67962 time 121 ms total 1044 ms
                 *  Time: 1044 ms
                 *  Solution 7d86f6 30eca94 4c4e3b8 5fdc721 70dd206 737c0cd 7b3b464 7dfd358 9038cc2 913872c b0a40a6 b50ea02 b52718b b58c806 d3a1049 d4f4485 e1083cf e267035 e531581 eb1e9bf ef7c556 11037141 11b20da7 11c73af0 136af3d4 13a9f961 13b4b0d9 146a4fed 161015fe 16125cb0 1653304f 18157684 18c2fc5b 18d4a39e 1962c64d 1bb64237 1c46b245 1d40bc3d 1d49d47c 1d8a0e1d 1e80fdc8 1fb4541e
                 *  Verified with cyclehash 6d6545ca75f63e13c428a5e495e548e3c573b15af8841951599ba037d2f2ef58
                 *  1 total solutions
                 */
            }

            try
            {
                if (parser.Contains("a"))
                {
                    string remote = parser.Arguments["a"][0];
                    if (remote.Contains(":"))
                    {
                        gc = new GrinConeeect(remote.Split(':')[0], int.Parse(remote.Split(':')[1])); // user specified port
                    }
                    else
                    {
                        gc = new GrinConeeect(remote, 13416); // default port
                    }
                    gc.statistics = statistics;
                }
                else
                {
                    Console.WriteLine("Please specify options: [-d <cuda device>] -a <node IP>");
                    return;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error parsing remote address:port" + ex.Message);
            }


            if (!gc.IsConnected)
            {
                Console.WriteLine("Unable to connect to node.");
                return;
            }

            if (parser.Contains("l"))
            {
                string login = parser.Arguments["l"][0];
                string pwd   = parser.Contains("p") ? parser.Arguments["p"][0] : "";

                gc.SendLogin(login, pwd);
            }

            Console.Write("Waiting for next block, this may take a bit");
            int wcnt = 0;

            while (true)
            {
                if (gc.CurrentJob != null && gc.IsConnected)
                {
                    break;
                }

                if (Canceled)
                {
                    return;
                }

                Console.Write(".");

                if ((++wcnt % 30) == 0)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("\nMake sure you are running 'grin wallet listen' on your node! \n");
                    Console.ResetColor();
                }

                Task.Delay(500).Wait();
            }
            Console.WriteLine();

            try
            {
                if (!Directory.Exists("edges"))
                {
                    Directory.CreateDirectory("edges");
                }
            }
            catch { }


            {
                try
                {
                    nonce  = 0;
                    reps   = 0;
                    device = parser.Contains("d") ? UInt64.Parse(parser.Arguments["d"][0]) : 0;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Unable to parse arguments: " + ex.Message);
                }

                Console.WriteLine("Starting CUDA solver process on device " + device.ToString());

                try
                {
                    bool lowMem = parser.Contains("lm");

                    if (lowMem)
                    {
                        Console.WriteLine("8GB version switch is deprecated");
                    }

                    cuda = Process.Start(new ProcessStartInfo()
                    {
                        FileName               = "Cudacka.exe",
                        Arguments              = device.ToString(),
                        CreateNoWindow         = true,
                        RedirectStandardError  = true,
                        RedirectStandardInput  = true,
                        RedirectStandardOutput = true,
                        StandardErrorEncoding  = Encoding.ASCII,
                        StandardOutputEncoding = Encoding.ASCII,
                        UseShellExecute        = false
                    });

                    cuda.ErrorDataReceived  += Cuda_ErrorDataReceived;
                    cuda.OutputDataReceived += Cuda_OutputDataReceived;
                    cuda.BeginOutputReadLine();
                    cuda.BeginErrorReadLine();

                    Task.Delay(5000).Wait();

                    if (cuda.HasExited)
                    {
                        Console.WriteLine("C++ trimmer self-terminated!");
                    }

                    if (tstate != TrimmerState.Ready)
                    {
                        Console.WriteLine("C++ trimmer not in ready state!");

                        if (CppInitText.Trim() == "")
                        {
                            Console.WriteLine("Console output redirection failed! Lets try to use sockets for OpenCL miner instead...");
                        }
                        else
                        {
                            Console.WriteLine("Unexpected data from C++ (expected #r): " + CppInitText);
                        }
                    }

                    SupressCudaConsole = true;

                    if (!cuda.HasExited && tstate == TrimmerState.Ready)
                    {
                        s = DateTime.Now;

                        UInt64 jobId  = 0;
                        UInt64 height = 0;
                        UInt64 dif    = 0;
                        byte[] header;

                        //for (ulong i = 0; i < reps; i++)
                        while (!Canceled)
                        {
                            if (gc.lastComm.AddMinutes(30) < DateTime.Now)
                            {
                                gc.WaitForJob = true;
                            }

                            if (gc.WaitForJob | !gc.IsConnected)
                            {
                                Task.Delay(100).Wait();
                                Console.Write(".");
                                continue;
                            }

                            DateTime a = DateTime.Now;

                            GetSols();

                            jobId  = gc.CurrentJob.job_id;
                            height = (UInt64)gc.CurrentJob.height;
                            dif    = (UInt64)gc.CurrentJob.difficulty;
                            header = gc.CurrentJob.GetHeader();

                            UInt64 hnonce = (UInt64)(long)rnd.Next() | ((UInt64)(long)rnd.Next() << 32);
                            var    bytes  = BitConverter.GetBytes(hnonce).Reverse().ToArray();
                            //Array.Copy(bytes, 0, header, header.Length - 8, 8);
                            header = header.Concat(bytes).ToArray();
                            var    hash   = new Crypto.Blake2B(256);
                            byte[] blaked = hash.ComputeHash(header);
                            //blaked = hash.ComputeHash(blaked); -- testnet2 bug

                            if (Test)
                            {
                                k0 = 0xf4956dc403730b01L;
                                k1 = 0xe6d45de39c2a5a3eL;
                                k2 = 0xcbf626a8afee35f6L;
                                k3 = 0x4307b94b1a0c9980L;
                            }
                            else
                            {
                                k0 = BitConverter.ToUInt64(blaked, 0);
                                k1 = BitConverter.ToUInt64(blaked, 8);
                                k2 = BitConverter.ToUInt64(blaked, 16);
                                k3 = BitConverter.ToUInt64(blaked, 24);
                            }

                            if (statistics.graphs % 100 == 0)
                            {
                                Console.WriteLine("Graphs: {0}, Trims: {1}, Shares: {2}, Mined: {3}", statistics.graphs, statistics.edgesets, statistics.solutions, statistics.mined);
                                if (statistics.solutions > 0)
                                {
                                    Console.WriteLine("Graphs per Solution: {0}", statistics.graphs / statistics.solutions);
                                }
                                if (statistics.graphs > 0)
                                {
                                    Console.WriteLine("GPS(Graphs/Second): {0:F2}", (float)statistics.graphs / (0.1 + (DateTime.Now - s).TotalSeconds));
                                }
                            }

                            statistics.graphs++;
                            cuda.StandardInput.WriteLine(string.Format("#t {0} {1} {2} {3} {4}", k0, k1, k2, k3, 0));

                            bool notify = false;
                            for (int w = 0; w < 500; w++)
                            {
                                if (tstate != TrimmerState.Trimming)
                                {
                                    Task.Delay(1).Wait();
                                }
                                else
                                {
                                    break;
                                }

                                if (w > 100 && !notify)
                                {
                                    notify = true;
                                    Console.WriteLine("Warning: No response from trimmer to trim command for > 100ms");
                                }
                            }

                            if (tstate == TrimmerState.Trimming)
                            {
                                try
                                {
                                    timeout = false;
                                    bool reported = false;

                                    for (int t = 0; t <= 10000; t++)
                                    {
                                        if (t == 10000)
                                        {
                                            timeout = true;
                                        }

                                        if (tstate != TrimmerState.Terminated)
                                        {
                                            if (tstate == TrimmerState.Ready && !reported)
                                            {
                                                trimfailed = 0;
                                                reported   = true;
                                                Console.ForegroundColor = ConsoleColor.Magenta;
                                                Console.WriteLine("Trimmed in " + Math.Round((DateTime.Now - a).TotalMilliseconds).ToString() + "ms");
                                                Console.ResetColor();
                                            }

                                            if (tstate == TrimmerState.Ready)
                                            {
                                                UInt64 _k0 = k0, _k1 = k1, _k2 = k2, _k3 = k3, _nonce = hnonce;

                                                if (activeCyclers < 4)
                                                {
                                                    Task.Run(() =>
                                                    {
                                                        try
                                                        {
                                                            activeCyclers++;

                                                            statistics.edgesets++;
                                                            CGraph g = new CGraph();
                                                            g.SetHeader(_nonce, _k0, _k1, _k2, _k3, height, dif, jobId);
                                                            g.SetEdges(edges);
                                                            g.FindSolutions(42, solutions);
                                                        }
                                                        catch
                                                        {
                                                        }
                                                        finally
                                                        {
                                                            activeCyclers--;
                                                        }
                                                    });
                                                }
                                                else
                                                {
                                                    Console.WriteLine("CPU overloaded, dropping tasks, CPU bottleneck!");
                                                }


                                                break;
                                            }
                                        }

                                        Task.Delay(1).Wait();
                                    }

                                    if (timeout)
                                    {
                                        Console.WriteLine("CUDA trimmer timeout");
                                        break;
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine("Error while waiting for trimmed edges: " + ex.Message);
                                }
                            }
                            else
                            {
                                Console.WriteLine("CUDA trimmer not responding");

                                try
                                {
                                    if (cuda.HasExited)
                                    {
                                        Console.WriteLine("CUDA trimmer thread terminated itself with code " + cuda.ExitCode.ToString());
                                    }
                                    else
                                    {
                                        Console.WriteLine("CUDA trimmer stuck in " + tstate.ToString());
                                    }
                                }
                                catch { }

                                if (trimfailed++ > 3)
                                {
                                    break;
                                }
                            }
                        }

                        if (activeCyclers > 0)
                        {
                            Task.Delay(500).Wait();
                        }

                        if (solutions.Count > 0)
                        {
                            for (int i = 0; i < 10; i++)
                            {
                                if (tstate != TrimmerState.Ready)
                                {
                                    Task.Delay(200).Wait();
                                }
                            }

                            GetSols();

                            Task.Delay(500).Wait();
                        }


                        cuda.StandardInput.WriteLine("#e");
                    }
                    else
                    {
                        Console.WriteLine("CUDA launch error");
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error while starting CUDA solver: " + ex.Message);
                }
                finally
                {
                    try
                    {
                        if (cuda != null && !cuda.HasExited)
                        {
                            cuda.Kill();
                        }
                    }
                    catch { }
                }
            }


            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.WriteLine("Finished in " + Math.Round((DateTime.Now - s).TotalMilliseconds).ToString() + "ms");
            Console.ResetColor();
        }