Код, вычисляющий количество бедрока. Как возможно максимально быстро вычислять? Как сделать MultiThread ещё не придумал.
В мэйне идёт загрузка из файла аргументов, указывающих на: Прогресс, способ вычислений.
public struct Program
{
public static void Main()
{
List<long> args = File.ReadAllText("C:\\p.txt").Split(',').Select(long.Parse).ToList();
Console.WriteLine("Stats was successful. x - " + args[0] + ", State - " + args[4] + "/" + args[7] + ", Count - " + args[1]);
switch (args[8])
{
case 0:
new OneThreadCPU().Start(args);
break;
case 1:
new MoreThreadCPU().Start();
break;
}
}
}
public struct OneThreadCPU
{
private const int MaxBlock = 0x1C9C38;
public void Start(List<long> args)
{
SetThreadAffinityMask(GetCurrentThread(), (UIntPtr)1);
Help h = new Help();
h.Load(new Info((int)args[0], args[1], new Point((int)args[2], (int)args[3], (int)args[4]), new Point((int)args[5], (int)args[6], (int)args[7])));
for (; h.i.x < MaxBlock; h.i.x++)
{
var z = -1875000;
var startTime = DateTime.Now;
for (; z < MaxBlock; z++) h.i.count += h.GetBedrockChunk(h.i.x, z);
h.Save(@"C:\\p.txt", h.i.x + 1, h.i.count, h.i.min, h.i.max, startTime, 0, 0);
}
}
[DllImport("kernel32.dll")]
static extern UIntPtr SetThreadAffinityMask(IntPtr hThread, UIntPtr dwThreadAffinityMask);
[DllImport("kernel32.dll")]
static extern IntPtr GetCurrentThread();
}
public struct Help
{
private const int MaxBlock = 0x1C9C38;
public Info i;
private readonly BedrockGen Bd = new();
private string DateTimes() => "[" + DateTime.Now.ToString("yy.MM.dd.HH:mm:ss") + "] ";
public long GetBedrockChunk(int x, int z) //-1875000,0,0,0,1000,0,0,0,0,0
{
var countBedrockInChunk = 0;
var chunkSeed = Bd.rSFC(x, z);
var vx = x >> 4;
var vz = z >> 4;
for (var y = (byte)1; y < 5; y++)
for (var bx = vx; bx < vx + 16; bx++)
for (var bz = vz; bz < vz + 16; bz++)
if (Bd.bO112(bx, y, bz, chunkSeed))
countBedrockInChunk++;
if (countBedrockInChunk > i.max.count)
{
Send(ConsoleColor.Green,
"Max " + "[" + countBedrockInChunk + "/" + i.max.count + "] [" + i.x + "|" + z
+ "] [" + (i.count + countBedrockInChunk).ToString("#,#", new CultureInfo("de-DE")) + "]");
i.max = new Point(x, z, countBedrockInChunk);
}
else if (countBedrockInChunk < i.min.count)
{
Send(ConsoleColor.Red,
"Min " + "[" + countBedrockInChunk + "/" + i.min.count + "] [" + x + "|" + z
+ "] [" + (i.count + countBedrockInChunk).ToString("#,#", new CultureInfo("de-DE")) + "]");
i.min = new Point(x, z, countBedrockInChunk);
}
return countBedrockInChunk;
}
private void Send(ConsoleColor color, string message)
{
Console.ForegroundColor = color;
Console.WriteLine(DateTimes() + message);
Console.ResetColor();
}
public void Load(Info i)
{
this.i = i;
}
public void Save(string path, int x, long countBedrockArg, Point coordinatesMinBedrockCountArg, Point coordinatesMaxBedrockCountArg, DateTime dateStart, int firstArg, int secondArg)
{
File.WriteAllText(path,
string.Join(',',
new List<long>{
x, countBedrockArg, coordinatesMinBedrockCountArg.x, coordinatesMinBedrockCountArg.z,
coordinatesMinBedrockCountArg.count, coordinatesMaxBedrockCountArg.x,
coordinatesMaxBedrockCountArg.z,
coordinatesMaxBedrockCountArg.count, firstArg, secondArg
}.Select(x => x.ToString())));
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"{DateTimes()}Save [{x}({ Math.Round((float)(100 - (float)Math.Abs(x) / MaxBlock * 100 + (x > 0 ? 50 : 0)), 5, MidpointRounding.AwayFromZero)}%)] [{(new TimeSpan((DateTime.Now - dateStart).Ticks).ToString("mm'm 'ss's 'fff'n'").Replace("00s ", "").Replace("00m ", "").Replace("000n", ""))}] [{i.count.ToString("#,#", new CultureInfo("de-DE"))}]");
Console.ResetColor();
}
}
internal struct BedrockGen
{
private const long q = 0x4F9939F508, w = 0x1EF1565BD5, e = 0x5DEECE66D, g = 0xFFFFFFFFFFFF;
private const int t = 0xF, a1 = 0x10, a2 = 0x11;
internal long rSFC(in int x, in int z) => ((x * q + z * w) ^ e) & g;
private bool r(in long rs, in long a, in long b, in int y) => ((((rs * a + b) & g) >> a2) % 5) >= y;
private int pCICN(in int x, in int y, in int z) => ((z * a1) + x) * 4 + (3 - y);
internal bool bO112(in int x, in byte y, in int z, in long cs)
{
var pi = pCICN(x & t, y - 1, z & t);
return r(cs, A_OW_112[pi], B_OW_112[pi], y);
}
}
public struct Info
{
public int x;
public long count;
public Point min, max;
public Info(int x, long count, Point min, Point max)
{
this.x = x;
this.count = count;
this.min = min;
this.max = max;
}
}
public struct Point
{
public readonly int count;
public readonly int x;
public readonly int z;
public Point(int x, int z, int count)
{
this.x = x;
this.z = z;
this.count = count;
}
}