Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion dotnet/DDS_Core/DDS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public class DDS
/// via <c>SolverContext</c> and <c>SolverConfig</c>.
/// </para>
/// </remarks>
/// <param name="userThreads">Maximum number of threads to use.</param>
/// <param name="userThreads">Ignored; retained for backward compatibility.</param>
// The underlying C symbol SetMaxThreads was renamed to
// InitializeStaticMemory; this P/Invoke still targets the deprecated alias.
[Obsolete("Use SolverContext instead.")]
public void SetMaxThreads(int userThreads) => DdsNative.SetMaxThreads(userThreads);

Expand Down
2 changes: 2 additions & 0 deletions dotnet/DDS_Core/Native/DdsNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ public static extern int dds_calc_par( SolverContextHandle ctx
#endregion

#region ====== Configuration and Resource Management ======
// The C symbol SetMaxThreads is deprecated and now a thin alias of
// InitializeStaticMemory; the userThreads argument is ignored.
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern void SetMaxThreads( int userThreads);

Expand Down
4 changes: 0 additions & 4 deletions examples/dd_table_for_deal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,6 @@ auto main(int argc, char * argv[]) -> int
DdTableResults table;
char line[80];

#if defined(__linux) || defined(__APPLE__)
SetMaxThreads(0);
#endif

const int res = CalcDDtablePBN(tableDealPBN, &table);
if (res != RETURN_NO_FAULT)
{
Expand Down
2 changes: 1 addition & 1 deletion examples/migration_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

void solve_legacy(const Deal& deal)
{
SetMaxThreads(4);
InitializeStaticMemory();
SetResources(2000, 4);

FutureTricks fut;
Expand Down
99 changes: 90 additions & 9 deletions library/src/api/dll.h
Original file line number Diff line number Diff line change
Expand Up @@ -429,20 +429,29 @@ struct DDSInfo


/**
* @brief Set the maximum number of threads used by the solver.
* @brief Initialise the solver's static memory.
*
* @deprecated In the modern C++ API, thread count is controlled by the
* embedding application (typically one SolverContext per worker
* thread). New code should create/destroy SolverContext instances
* in the application rather than calling this function.
* Allocates the transposition-table memory pools, registers scheduler and
* thread-manager state, and performs one-time lookup-table initialisation.
* This does NOT control the number of worker threads — use the
* SolveAllBoardsN / CalcAllTablesN family for per-call thread caps.
*/
EXTERN_C DLLEXPORT auto STDCALL InitializeStaticMemory() -> void;

/**
* @brief Deprecated alias of InitializeStaticMemory().
*
* @deprecated Use InitializeStaticMemory(); the thread count argument is
* ignored (internal batch threading was removed). In the modern
* C++ API, thread count is controlled by the embedding application
* (typically one SolverContext per worker thread), or per call via
* the SolveAllBoardsN / CalcAllTablesN family.
* See docs/api_migration.md for modern C++ API examples.
*
* @param userThreads Maximum number of threads to use
* @param userThreads Ignored; retained for backward compatibility.
*
* This function is part of the legacy C API and is maintained for backward
* compatibility. It has no direct equivalent in the modern API, where both
* threading and TT memory limits are configured via SolverContext and
* SolverConfig on a per-instance basis.
* compatibility. It simply forwards to InitializeStaticMemory().
*/
EXTERN_C DLLEXPORT auto STDCALL SetMaxThreads(
int userThreads) -> void;
Expand Down Expand Up @@ -542,6 +551,17 @@ EXTERN_C DLLEXPORT auto STDCALL CalcDDtable(
struct DdTableDeal tableDeal,
struct DdTableResults * tablep) -> int;

/**
* @brief CalcDDtable with an explicit worker-thread cap.
*
* @param maxThreads Maximum worker threads; <= 0 selects the automatic
* (hardware_concurrency) default.
*/
EXTERN_C DLLEXPORT auto STDCALL CalcDDtableN(
struct DdTableDeal tableDeal,
struct DdTableResults * tablep,
int maxThreads) -> int;

/**
* @brief Calculate the double dummy table for a PBN Deal.
*
Expand All @@ -553,6 +573,17 @@ EXTERN_C DLLEXPORT auto STDCALL CalcDDtablePBN(
struct DdTableDealPBN tableDealPBN,
struct DdTableResults * tablep) -> int;

/**
* @brief CalcDDtablePBN with an explicit worker-thread cap.
*
* @param maxThreads Maximum worker threads; <= 0 selects the automatic
* (hardware_concurrency) default.
*/
EXTERN_C DLLEXPORT auto STDCALL CalcDDtablePBNN(
struct DdTableDealPBN tableDealPBN,
struct DdTableResults * tablep,
int maxThreads) -> int;

/**
* @brief Calculate double dummy tables for multiple deals.
*
Expand All @@ -570,6 +601,20 @@ EXTERN_C DLLEXPORT auto STDCALL CalcAllTables(
struct DdTablesRes * resp,
struct AllParResults * presp) -> int;

/**
* @brief CalcAllTables with an explicit worker-thread cap.
*
* @param maxThreads Maximum worker threads; <= 0 selects the automatic
* (hardware_concurrency) default.
*/
EXTERN_C DLLEXPORT auto STDCALL CalcAllTablesN(
struct DdTableDeals const * dealsp,
int mode,
int const trumpFilter[DDS_STRAINS],
struct DdTablesRes * resp,
struct AllParResults * presp,
int maxThreads) -> int;

/**
* @brief Calculate double dummy tables for multiple PBN deals.
*
Expand All @@ -587,6 +632,20 @@ EXTERN_C DLLEXPORT auto STDCALL CalcAllTablesPBN(
struct DdTablesRes * resp,
struct AllParResults * presp) -> int;

/**
* @brief CalcAllTablesPBN with an explicit worker-thread cap.
*
* @param maxThreads Maximum worker threads; <= 0 selects the automatic
* (hardware_concurrency) default.
*/
EXTERN_C DLLEXPORT auto STDCALL CalcAllTablesPBNN(
struct DdTableDealsPBN const * dealsp,
int mode,
int const trumpFilter[DDS_STRAINS],
struct DdTablesRes * resp,
struct AllParResults * presp,
int maxThreads) -> int;

/**
* @brief Solve multiple bridge deals in PBN format.
*
Expand All @@ -598,10 +657,32 @@ EXTERN_C DLLEXPORT auto STDCALL SolveAllBoards(
struct BoardsPBN const * bop,
struct SolvedBoards * solvedp) -> int;

/**
* @brief SolveAllBoards with an explicit worker-thread cap.
*
* @param maxThreads Maximum worker threads; <= 0 selects the automatic
* (hardware_concurrency) default.
*/
EXTERN_C DLLEXPORT auto STDCALL SolveAllBoardsN(
struct BoardsPBN const * bop,
struct SolvedBoards * solvedp,
int maxThreads) -> int;

EXTERN_C DLLEXPORT auto STDCALL SolveAllBoardsBin(
struct Boards const * bop,
struct SolvedBoards * solvedp) -> int;

/**
* @brief SolveAllBoardsBin with an explicit worker-thread cap.
*
* @param maxThreads Maximum worker threads; <= 0 selects the automatic
* (hardware_concurrency) default.
*/
EXTERN_C DLLEXPORT auto STDCALL SolveAllBoardsBinN(
struct Boards const * bop,
struct SolvedBoards * solvedp,
int maxThreads) -> int;

EXTERN_C DLLEXPORT auto STDCALL SolveAllBoardsSeq(
struct BoardsPBN const * bop,
struct SolvedBoards * solvedp) -> int;
Expand Down
76 changes: 59 additions & 17 deletions library/src/calc_tables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/

#include "calc_tables.hpp"
#include <thread>
#include <vector>

#include <pbn.hpp>
Expand All @@ -27,7 +26,8 @@ extern Scheduler scheduler;
// Legacy overload (creates temporary context)
auto calc_all_boards_n(
Boards * bop,
SolvedBoards * solvedp) -> int;
SolvedBoards * solvedp,
int max_threads = 0) -> int;


auto calc_single_common_internal(
Expand Down Expand Up @@ -109,7 +109,8 @@ auto calc_all_boards_n(
// Legacy overload: parallel across boards, one SolverContext per worker.
auto calc_all_boards_n(
Boards * bop,
SolvedBoards * solvedp) -> int
SolvedBoards * solvedp,
int max_threads) -> int
{
const int n = bop->no_of_boards;
if (n > MAXNOOFBOARDS)
Expand All @@ -120,8 +121,7 @@ auto calc_all_boards_n(

START_BLOCK_TIMER;

const int nthreads = std::max(1,
std::min(static_cast<int>(std::thread::hardware_concurrency()), n));
const int nthreads = resolve_worker_count(max_threads, n);

int err = RETURN_NO_FAULT;
if (nthreads <= 1)
Expand Down Expand Up @@ -160,9 +160,10 @@ auto calc_all_boards_n(



int STDCALL CalcDDtable(
int STDCALL CalcDDtableN(
DdTableDeal tableDeal,
DdTableResults * tablep)
DdTableResults * tablep,
int maxThreads)
{
Deal dl;
Boards bo;
Expand Down Expand Up @@ -191,7 +192,7 @@ int STDCALL CalcDDtable(
ind++;
}

int res = calc_all_boards_n(&bo, &solved);
int res = calc_all_boards_n(&bo, &solved, maxThreads);
if (res != 1)
return res;

Expand All @@ -211,12 +212,21 @@ int STDCALL CalcDDtable(
}


int STDCALL CalcAllTables(
int STDCALL CalcDDtable(
DdTableDeal tableDeal,
DdTableResults * tablep)
{
return CalcDDtableN(tableDeal, tablep, 0);
}


int STDCALL CalcAllTablesN(
DdTableDeals const * dealsp,
int mode,
int const trumpFilter[5],
DdTablesRes * resp,
AllParResults * presp)
AllParResults * presp,
int maxThreads)
{
/* mode = 0: par calculation, vulnerability None
mode = 1: par calculation, vulnerability All
Expand Down Expand Up @@ -278,7 +288,7 @@ int STDCALL CalcAllTables(

bo.no_of_boards = lastIndex + 1;

int res = calc_all_boards_n(&bo, &solved);
int res = calc_all_boards_n(&bo, &solved, maxThreads);
if (res != 1)
return res;

Expand Down Expand Up @@ -316,12 +326,24 @@ int STDCALL CalcAllTables(
}


int STDCALL CalcAllTablesPBN(
DdTableDealsPBN const * dealsp,
int STDCALL CalcAllTables(
DdTableDeals const * dealsp,
int mode,
int const trumpFilter[5],
DdTablesRes * resp,
AllParResults * presp)
{
return CalcAllTablesN(dealsp, mode, trumpFilter, resp, presp, 0);
}


int STDCALL CalcAllTablesPBNN(
DdTableDealsPBN const * dealsp,
int mode,
int const trumpFilter[5],
DdTablesRes * resp,
AllParResults * presp,
int maxThreads)
{
DdTableDeals dls;
for (int k = 0; k < dealsp->no_of_tables; k++)
Expand All @@ -330,24 +352,44 @@ int STDCALL CalcAllTablesPBN(

dls.no_of_tables = dealsp->no_of_tables;

int res = CalcAllTables(&dls, mode, trumpFilter, resp, presp);
int res = CalcAllTablesN(&dls, mode, trumpFilter, resp, presp, maxThreads);
return res;
}


int STDCALL CalcDDtablePBN(
int STDCALL CalcAllTablesPBN(
DdTableDealsPBN const * dealsp,
int mode,
int const trumpFilter[5],
DdTablesRes * resp,
AllParResults * presp)
{
return CalcAllTablesPBNN(dealsp, mode, trumpFilter, resp, presp, 0);
}


int STDCALL CalcDDtablePBNN(
DdTableDealPBN tableDealPBN,
DdTableResults * tablep)
DdTableResults * tablep,
int maxThreads)
{
DdTableDeal tableDeal;
if (convert_from_pbn(tableDealPBN.cards, tableDeal.cards) != 1)
return RETURN_PBN_FAULT;

int res = CalcDDtable(tableDeal, tablep);
int res = CalcDDtableN(tableDeal, tablep, maxThreads);
return res;
}


int STDCALL CalcDDtablePBN(
DdTableDealPBN tableDealPBN,
DdTableResults * tablep)
{
return CalcDDtablePBNN(tableDealPBN, tablep, 0);
}


void detect_calc_duplicates(
const Boards& bds,
vector<int>& uniques,
Expand Down
Loading
Loading