OGS
ChemistryLib::PhreeqcIOData::PhreeqcInstancePool Class Reference

Detailed Description

Manages a pool of independent IPhreeqc instances for thread-safe parallel chemistry calculations.

Each OpenMP thread uses its own PHREEQC instance to avoid thread-safety issues. All instances are initialized with the same thermodynamic database.

Definition at line 18 of file PhreeqcInstancePool.h.

#include <PhreeqcInstancePool.h>

Public Member Functions

 PhreeqcInstancePool (std::string const &database, int num_instances)
 ~PhreeqcInstancePool ()
 PhreeqcInstancePool (PhreeqcInstancePool const &)=delete
PhreeqcInstancePooloperator= (PhreeqcInstancePool const &)=delete
int getInstanceForThread (int thread_id) const
int size () const
 Returns the number of instances in the pool.

Static Public Member Functions

static int createInstance (std::string const &database)
static void configureForStringIO (int id)

Private Attributes

std::vector< int > instance_ids_

Constructor & Destructor Documentation

◆ PhreeqcInstancePool() [1/2]

ChemistryLib::PhreeqcIOData::PhreeqcInstancePool::PhreeqcInstancePool ( std::string const & database,
int num_instances )

Creates a pool of PHREEQC instances.

Parameters
databasePath to the thermodynamic database file.
num_instancesNumber of instances to create (one per thread).

Definition at line 50 of file PhreeqcInstancePool.cpp.

52{
53 if (num_instances < 1)
54 {
55 OGS_FATAL("PhreeqcInstancePool requires at least 1 instance, got {}.",
56 num_instances);
57 }
58
59 instance_ids_.reserve(num_instances);
60 for (int i = 0; i < num_instances; ++i)
61 {
62 int const id = createInstance(database);
64 instance_ids_.push_back(id);
65 }
66
67 INFO("Created {} PHREEQC instances for parallel chemistry execution.",
68 num_instances);
69}
#define OGS_FATAL(...)
Definition Error.h:10
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:28
static int createInstance(std::string const &database)

References configureForStringIO(), createInstance(), INFO(), instance_ids_, and OGS_FATAL.

Referenced by PhreeqcInstancePool(), and operator=().

◆ ~PhreeqcInstancePool()

ChemistryLib::PhreeqcIOData::PhreeqcInstancePool::~PhreeqcInstancePool ( )

Definition at line 71 of file PhreeqcInstancePool.cpp.

72{
73 for (int const id : instance_ids_)
74 {
75 DestroyIPhreeqc(id);
76 }
77}

References instance_ids_.

◆ PhreeqcInstancePool() [2/2]

ChemistryLib::PhreeqcIOData::PhreeqcInstancePool::PhreeqcInstancePool ( PhreeqcInstancePool const & )
delete

References PhreeqcInstancePool().

Member Function Documentation

◆ configureForStringIO()

void ChemistryLib::PhreeqcIOData::PhreeqcInstancePool::configureForStringIO ( int id)
static

Configures an IPhreeqc instance for string-based I/O: disables file output and enables in-memory buffers for selected output and dump.

Definition at line 35 of file PhreeqcInstancePool.cpp.

36{
37 SetSelectedOutputFileOn(id, 0);
38 if (SetSelectedOutputStringOn(id, 1) != IPQ_OK)
39 {
40 OGS_FATAL("Failed to enable string output for PHREEQC instance {}.",
41 id);
42 }
43 SetDumpFileOn(id, 0);
44 if (SetDumpStringOn(id, 1) != IPQ_OK)
45 {
46 OGS_FATAL("Failed to enable dump string for PHREEQC instance {}.", id);
47 }
48}

References OGS_FATAL.

Referenced by PhreeqcInstancePool().

◆ createInstance()

int ChemistryLib::PhreeqcIOData::PhreeqcInstancePool::createInstance ( std::string const & database)
static

Creates a new IPhreeqc instance and loads the thermodynamic database. Returns the instance id. Fatals on failure.

Definition at line 15 of file PhreeqcInstancePool.cpp.

16{
17 int const id = CreateIPhreeqc();
18 if (id < 0)
19 {
21 "Failed to create PHREEQC instance (IPQ_RESULT error code = {}).",
22 id);
23 }
24 if (LoadDatabase(id, database.c_str()) != IPQ_OK)
25 {
26 OutputErrorString(id);
28 "Failed to load thermodynamic database '{:s}' for PHREEQC "
29 "instance {}.",
30 database, id);
31 }
32 return id;
33}

References OGS_FATAL.

Referenced by PhreeqcInstancePool(), and ChemistryLib::PhreeqcIOData::PhreeqcIO::PhreeqcIO().

◆ getInstanceForThread()

int ChemistryLib::PhreeqcIOData::PhreeqcInstancePool::getInstanceForThread ( int thread_id) const

Returns the PHREEQC instance ID for the given thread.

Parameters
thread_idThe OpenMP thread ID (0-based).
Returns
The IPhreeqc instance ID to use for this thread.

Definition at line 79 of file PhreeqcInstancePool.cpp.

80{
81 if (thread_id < 0 || thread_id >= static_cast<int>(instance_ids_.size()))
82 {
84 "Thread ID {} is out of range [0, {}). This indicates a bug in "
85 "the parallel chemistry implementation.",
86 thread_id, instance_ids_.size());
87 }
88 return instance_ids_[thread_id];
89}

References instance_ids_, and OGS_FATAL.

◆ operator=()

PhreeqcInstancePool & ChemistryLib::PhreeqcIOData::PhreeqcInstancePool::operator= ( PhreeqcInstancePool const & )
delete

References PhreeqcInstancePool().

◆ size()

int ChemistryLib::PhreeqcIOData::PhreeqcInstancePool::size ( ) const
inline

Returns the number of instances in the pool.

Definition at line 38 of file PhreeqcInstancePool.h.

38{ return static_cast<int>(instance_ids_.size()); }

References instance_ids_.

Member Data Documentation

◆ instance_ids_

std::vector<int> ChemistryLib::PhreeqcIOData::PhreeqcInstancePool::instance_ids_
private

The documentation for this class was generated from the following files: