summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichał Klichowicz <emkael@tlen.pl>2023-11-05 18:08:51 +0100
committerMichał Klichowicz <emkael@tlen.pl>2023-11-05 18:08:51 +0100
commit50f18e08e71fef4992fb7df32fdee8ac142f181a (patch)
treed3fab74ec9746a6da4d46760e325861b08932c61
parent86b5e6fab4f7d223d30f0b370020a6ba42cb8d05 (diff)
Parallel per-board processing
-rw-r--r--src/BCDDFile.cs107
-rw-r--r--src/DDTable.cs23
2 files changed, 89 insertions, 41 deletions
diff --git a/src/BCDDFile.cs b/src/BCDDFile.cs
index a37ead6..f0e7522 100644
--- a/src/BCDDFile.cs
+++ b/src/BCDDFile.cs
@@ -8,14 +8,19 @@ namespace BCDD
{
public List<String> errors;
private String filename;
+ private PBNFile file;
public static int filesCounter;
public static ManualResetEvent filesCountdown = new ManualResetEvent(false);
+ private int boardCount;
+ private ManualResetEvent boardCountdown;
+
public BCDDFile(String filename)
{
this.errors = new List<String>();
this.filename = filename;
+ this.file = new PBNFile(filename);
}
private void error(String message, String boardNo = "")
@@ -32,53 +37,68 @@ namespace BCDD
Console.WriteLine("ERROR: " + message);
}
+ private void info(String boardNo, String ddTable, ParContract contract)
+ {
+ Console.WriteLine(String.Format("[{0}:{1}] {2} {3}", this.filename, boardNo, ddTable, contract));
+ }
+
+ private void processBoard(object state)
+ {
+ PBNBoard board = state as PBNBoard;
+ DDTable table = new DDTable(board);
+ String boardNo;
+ try
+ {
+ boardNo = board.GetNumber();
+ }
+ catch (FieldNotFoundException)
+ {
+ boardNo = "?";
+ }
+ try
+ {
+ int[,] ddTable = table.GetDDTable();
+ if (ddTable != null)
+ {
+ ParScore par = new ParScore(board);
+ ParContract contract = par.GetParContract(ddTable);
+ board.SaveDDTable(ddTable);
+ board.SaveParContract(contract);
+ this.info(boardNo, DDTable.ShortFormat(ddTable), contract);
+ }
+ else
+ {
+ this.error("unable to determine DD table for board " + boardNo, boardNo);
+ }
+ }
+ catch (DllNotFoundException)
+ {
+ throw;
+ }
+ catch (Exception e)
+ {
+ this.error(e.Message, boardNo);
+ }
+ finally
+ {
+ if (Interlocked.Decrement(ref this.boardCount) == 0)
+ {
+ this.boardCountdown.Set();
+ }
+ }
+ }
+
public void analyze(object state)
{
try
{
- PBNFile file = new PBNFile(filename);
- foreach (PBNBoard board in file.Boards)
+ this.boardCount = this.file.Boards.Count;
+ this.boardCountdown = new ManualResetEvent(false);
+ foreach (PBNBoard board in this.file.Boards)
{
- DDTable table = new DDTable(board);
- String boardNo;
- try
- {
- boardNo = board.GetNumber();
- }
- catch (FieldNotFoundException)
- {
- boardNo = "?";
- }
- try
- {
- int[,] ddTable = table.GetDDTable();
- if (ddTable != null)
- {
- ParScore par = new ParScore(board);
- ParContract contract = par.GetParContract(ddTable);
- board.SaveDDTable(ddTable);
- board.SaveParContract(contract);
- Console.WriteLine("Board " + boardNo);
- DDTable.PrintTable(ddTable);
- Console.WriteLine(contract);
- Console.WriteLine();
- }
- else
- {
- this.error("unable to determine DD table for board " + boardNo, boardNo);
- }
- }
- catch (DllNotFoundException)
- {
- throw;
- }
- catch (Exception e)
- {
- this.error(e.Message, boardNo);
- }
- file.WriteBoard(board);
+ ThreadPool.QueueUserWorkItem(new WaitCallback(this.processBoard), board);
}
- file.Save();
+ boardCountdown.WaitOne();
}
catch (DllNotFoundException)
{
@@ -91,6 +111,11 @@ namespace BCDD
}
finally
{
+ foreach (PBNBoard board in this.file.Boards) {
+ this.file.WriteBoard(board);
+ }
+ Console.WriteLine(String.Format("Saving file {0}", this.filename));
+ this.file.Save();
if (Interlocked.Decrement(ref BCDDFile.filesCounter) == 0)
{
BCDDFile.filesCountdown.Set();
diff --git a/src/DDTable.cs b/src/DDTable.cs
index c2ffcd1..1e21cee 100644
--- a/src/DDTable.cs
+++ b/src/DDTable.cs
@@ -152,5 +152,28 @@ namespace BCDD
Console.WriteLine();
}
}
+
+ public static string ShortFormat(int[,] ddTable)
+ {
+ StringBuilder s = new StringBuilder();
+ for (int i = 0; i < 4; i++)
+ {
+ s.Append(BCalcWrapper.PLAYERS[i]);
+ s.Append(" ");
+ for (int j = 0; j < 5; j++)
+ {
+ s.Append(ddTable[i, j]);
+ if (j < 4)
+ {
+ s.Append("-");
+ }
+ }
+ if (i < 3)
+ {
+ s.Append(" ");
+ }
+ }
+ return s.ToString();
+ }
}
}