Skip to content

Commit 1d61b38

Browse files
committed
Update handling of exceptions
1 parent 7efb7e2 commit 1d61b38

21 files changed

Lines changed: 743 additions & 257 deletions

include/gauxc/basisset.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <stdbool.h>
2121
#include <stddef.h>
2222
#endif
23+
#include <gauxc/types.h>
2324
#include <gauxc/status.h>
2425
#include <gauxc/shell.h>
2526

@@ -32,6 +33,7 @@ namespace GauXC::C {
3233
* @brief GauXC C API BasisSet handle.
3334
*/
3435
typedef struct GauXCBasisSet {
36+
GauXCHeader hdr; ///< Header for internal use.
3537
void* ptr; ///< Pointer to the BasisSet instance.
3638
} GauXCBasisSet;
3739

include/gauxc/functional.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212
#pragma once
1313

14+
#include <gauxc/types.h>
1415
#include <gauxc/status.h>
1516

1617
#ifdef __cplusplus
@@ -466,6 +467,7 @@ enum GauXC_Functional {
466467
* @brief GauXC C API Functional handle.
467468
*/
468469
typedef struct GauXCFunctional {
470+
GauXCHeader hdr; ///< Header for internal use.
469471
void* ptr; ///< Pointer to the Functional instance.
470472
} GauXCFunctional;
471473

include/gauxc/load_balancer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <stddef.h>
2121
#include <stdbool.h>
2222
#endif
23+
#include <gauxc/types.h>
2324
#include <gauxc/status.h>
2425
#include <gauxc/enum.h>
2526
#include <gauxc/runtime_environment.h>
@@ -36,6 +37,7 @@ namespace GauXC::C {
3637
* @brief GauXC C API LoadBalancer handle.
3738
*/
3839
typedef struct GauXCLoadBalancer {
40+
GauXCHeader hdr; ///< Header for internal use.
3941
void* ptr; ///< Pointer to the LoadBalancer instance.
4042
bool owned; ///< Whether this instance owns the LoadBalancer.
4143
} GauXCLoadBalancer;
@@ -54,6 +56,7 @@ extern void gauxc_load_balancer_delete(
5456
* @brief GauXC C API LoadBalancerFactory handle.
5557
*/
5658
typedef struct GauXCLoadBalancerFactory {
59+
GauXCHeader hdr; ///< Header for internal use.
5760
void* ptr; ///< Pointer to the LoadBalancerFactory instance.
5861
} GauXCLoadBalancerFactory;
5962

include/gauxc/matrix.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <stdint.h>
1919
#include <stddef.h>
2020
#endif
21+
#include <gauxc/types.h>
2122
#include <gauxc/status.h>
2223

2324
#ifdef __cplusplus
@@ -29,6 +30,7 @@ extern "C" {
2930
* @brief GauXC C API Matrix handle.
3031
*/
3132
typedef struct GauXCMatrix {
33+
GauXCHeader hdr; ///< Header for internal use.
3234
void* ptr; ///< Pointer to the Matrix instance.
3335
} GauXCMatrix;
3436

include/gauxc/molecular_weights.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <stdint.h>
1919
#include <stdbool.h>
2020
#endif
21+
#include <gauxc/types.h>
2122
#include <gauxc/status.h>
2223
#include <gauxc/enum.h>
2324
#include <gauxc/load_balancer.h>
@@ -39,6 +40,7 @@ typedef struct GauXCMolecularWeightsSettings {
3940
* @brief GauXC C API MolecularWeights handle.
4041
*/
4142
typedef struct GauXCMolecularWeights {
43+
GauXCHeader hdr; ///< Header for internal use.
4244
void* ptr; ///< Pointer to the MolecularWeights instance.
4345
bool owned; ///< Whether this instance owns the MolecularWeights.
4446
} GauXCMolecularWeights;
@@ -69,6 +71,7 @@ extern void gauxc_molecular_weights_modify_weights(
6971
* @brief GauXC C API MolecularWeightsFactory handle.
7072
*/
7173
typedef struct GauXCMolecularWeightsFactory {
74+
GauXCHeader hdr; ///< Header for internal use.
7275
void* ptr; ///< Pointer to the MolecularWeightsFactory instance.
7376
} GauXCMolecularWeightsFactory;
7477

include/gauxc/molecule.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <stdbool.h>
2121
#include <stddef.h>
2222
#endif
23+
#include <gauxc/types.h>
2324
#include <gauxc/status.h>
2425
#include <gauxc/atom.h>
2526

@@ -32,6 +33,7 @@ namespace GauXC::C {
3233
* @brief GauXC C API Molecule handle.
3334
*/
3435
typedef struct GauXCMolecule {
36+
GauXCHeader hdr; ///< Header for internal use.
3537
void* ptr; ///< Pointer to the Molecule instance.
3638
} GauXCMolecule;
3739

include/gauxc/molgrid.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#else
1919
#include <stdint.h>
2020
#endif
21+
#include <gauxc/types.h>
2122
#include <gauxc/status.h>
2223

2324
#ifdef __cplusplus
@@ -29,6 +30,7 @@ namespace GauXC::C {
2930
* @brief GauXC C API MolGrid handle.
3031
*/
3132
typedef struct GauXCMolGrid {
33+
GauXCHeader hdr; ///< Header for internal use.
3234
void* ptr; ///< Pointer to the MolGrid instance.
3335
} GauXCMolGrid;
3436

include/gauxc/types.h

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* GauXC Copyright (c) 2020-2024, The Regents of the University of California,
3+
* through Lawrence Berkeley National Laboratory (subject to receipt of
4+
* any required approvals from the U.S. Dept. of Energy).
5+
*
6+
* (c) 2024-2025, Microsoft Corporation
7+
*
8+
* All rights reserved.
9+
*
10+
* See LICENSE.txt for details
11+
*/
12+
#pragma once
13+
14+
#ifdef __cplusplus
15+
#include <cstddef>
16+
#else
17+
#include <stddef.h>
18+
#endif
19+
#include <gauxc/status.h>
20+
21+
#ifdef __cplusplus
22+
namespace GauXC::C {
23+
extern "C" {
24+
#endif
25+
26+
enum GauXC_Type {
27+
GauXC_Type_Molecule = 1,
28+
GauXC_Type_BasisSet = 2,
29+
GauXC_Type_MolGrid = 3,
30+
GauXC_Type_RuntimeEnvironment = 4,
31+
GauXC_Type_LoadBalancer = 5,
32+
GauXC_Type_LoadBalancerFactory = 6,
33+
GauXC_Type_MolecularWeights = 7,
34+
GauXC_Type_MolecularWeightsFactory = 8,
35+
GauXC_Type_Functional = 9,
36+
GauXC_Type_Integrator = 10,
37+
GauXC_Type_IntegratorFactory = 11,
38+
GauXC_Type_Matrix = 12
39+
};
40+
41+
/**
42+
* @brief GauXC C API Header for all GauXC objects.
43+
*/
44+
typedef struct GauXCHeader {
45+
enum GauXC_Type type; ///< Type of the GauXC object.
46+
} GauXCHeader;
47+
48+
/**
49+
* @brief Delete a GauXC object.
50+
* @param status Status object to capture any errors.
51+
* @param ptr Pointer to the GauXC object to delete.
52+
*/
53+
extern void gauxc_object_delete(
54+
GauXCStatus* status,
55+
void** ptr
56+
);
57+
58+
/**
59+
* @brief Delete all GauXC objects.
60+
* @param status Status object to capture any errors.
61+
* @param ptrs Array of GauXCHeader objects.
62+
* @param nptrs Number of pointers in the array.
63+
*/
64+
inline static void gauxc_objects_delete(
65+
GauXCStatus* status,
66+
void** ptrs,
67+
size_t nptrs
68+
) {
69+
status->code = 0;
70+
for(void** ptr = ptrs; ptr < ptrs + nptrs; ++ptr) {
71+
#ifdef __cplusplus
72+
if(*ptr != nullptr) {
73+
#else
74+
if(*ptr != NULL) {
75+
#endif
76+
gauxc_object_delete( status, ptr );
77+
if(status->code != 0) return;
78+
}
79+
}
80+
}
81+
82+
#ifdef __cplusplus
83+
} // extern "C"
84+
} // namespace GauXC::C
85+
#endif

include/gauxc/util/c_matrix.hpp

Lines changed: 124 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,147 @@
1111
*/
1212
#pragma once
1313

14+
#include <algorithm>
15+
#include <cstddef>
16+
1417
namespace GauXC::detail {
1518

1619
class CMatrix {
1720
public:
1821
using value_type = double;
19-
CMatrix( ) noexcept : rows_(0), cols_(0), data_(nullptr) {}
20-
CMatrix( size_t rows, size_t cols ) noexcept {
21-
rows_ = rows;
22-
cols_ = cols;
23-
data_ = new value_type[ rows * cols ]();
24-
}
22+
23+
/**
24+
* @brief Default constructor creates a 0x0 matrix
25+
*/
26+
CMatrix() noexcept : rows_(0), cols_(0), data_(nullptr) {}
27+
28+
/**
29+
* @brief Construct a new CMatrix object
30+
* @param rows Number of rows
31+
* @param cols Number of columns
32+
*/
33+
CMatrix(size_t rows, size_t cols)
34+
: rows_(rows), cols_(cols), data_(new value_type[rows * cols]()) {}
35+
36+
/**
37+
* @brief Destroy the CMatrix object
38+
*/
2539
~CMatrix() noexcept {
26-
if (data_) delete[] data_;
27-
data_ = nullptr;
40+
delete[] data_; // delete[] on nullptr is safe
41+
}
42+
43+
/**
44+
* @brief Copy constructor
45+
* @param other CMatrix to copy from
46+
*/
47+
CMatrix(const CMatrix& other)
48+
: rows_(other.rows_), cols_(other.cols_), data_(nullptr) {
49+
if (other.data_ && rows_ * cols_ > 0) {
50+
data_ = new value_type[rows_ * cols_];
51+
std::copy(other.data_, other.data_ + rows_ * cols_, data_);
52+
}
2853
}
29-
value_type * data() noexcept { return data_; }
30-
const value_type * data() const noexcept { return data_; }
54+
55+
/**
56+
* @brief Copy assignment operator
57+
* @param other CMatrix to copy from
58+
* @return CMatrix& Reference to this
59+
*/
60+
CMatrix& operator=(const CMatrix& other) {
61+
if (this != &other) {
62+
// Always clean up if sizes differ OR if other has no data
63+
if (rows_ * cols_ != other.rows_ * other.cols_ || !other.data_) {
64+
delete[] data_;
65+
data_ = nullptr;
66+
}
67+
rows_ = other.rows_;
68+
cols_ = other.cols_;
69+
if (other.data_ && rows_ * cols_ > 0) {
70+
if (!data_) {
71+
data_ = new value_type[rows_ * cols_];
72+
}
73+
std::copy(other.data_, other.data_ + rows_ * cols_, data_);
74+
}
75+
}
76+
return *this;
77+
}
78+
79+
/**
80+
* @brief Move constructor
81+
* @param other CMatrix to move from
82+
*/
83+
CMatrix(CMatrix&& other) noexcept
84+
: rows_(other.rows_), cols_(other.cols_), data_(other.data_) {
85+
other.rows_ = 0;
86+
other.cols_ = 0;
87+
other.data_ = nullptr;
88+
}
89+
90+
// Move assignment
91+
/**
92+
* @brief Move assignment operator
93+
* @param other CMatrix to move from
94+
* @return CMatrix& Reference to this
95+
*/
96+
CMatrix& operator=(CMatrix&& other) noexcept {
97+
if (this != &other) {
98+
delete[] data_;
99+
rows_ = other.rows_;
100+
cols_ = other.cols_;
101+
data_ = other.data_;
102+
other.rows_ = 0;
103+
other.cols_ = 0;
104+
other.data_ = nullptr;
105+
}
106+
return *this;
107+
}
108+
109+
/**
110+
* @brief Get pointer to raw data
111+
* @return value_type* Pointer to data
112+
*/
113+
value_type* data() noexcept { return data_; }
114+
/**
115+
* @brief Get pointer to raw data (const)
116+
* @return const value_type* Pointer to data
117+
*/
118+
const value_type* data() const noexcept { return data_; }
119+
/**
120+
* @brief Get number of rows
121+
* @return size_t Number of rows
122+
*/
31123
size_t rows() const noexcept { return rows_; }
124+
/**
125+
* @brief Get number of columns
126+
* @return size_t Number of columns
127+
*/
32128
size_t cols() const noexcept { return cols_; }
129+
130+
/**
131+
* @brief Set all elements to zero
132+
*/
33133
void setZero() noexcept {
34-
for( size_t i = 0; i < rows_ * cols_; ++i )
134+
for (size_t i = 0; i < rows_ * cols_; ++i)
35135
data_[i] = value_type(0);
36136
}
37-
void resize( size_t rows, size_t cols ) noexcept {
38-
if( rows == rows_ && cols == cols_ ) return;
39-
if (data_) delete[] data_;
137+
138+
/**
139+
* @brief Resize the matrix
140+
* @param rows New number of rows
141+
* @param cols New number of columns
142+
*/
143+
void resize(size_t rows, size_t cols) {
144+
if (rows == rows_ && cols == cols_) return;
145+
delete[] data_;
40146
rows_ = rows;
41147
cols_ = cols;
42-
data_ = new value_type[ rows * cols ]();
148+
data_ = new value_type[rows * cols]();
43149
}
44150

45151
private:
46-
size_t rows_;
47-
size_t cols_;
48-
value_type* data_;
152+
size_t rows_ = 0;
153+
size_t cols_ = 0;
154+
value_type* data_ = nullptr;
49155
};
50156

51157
static inline CMatrix* get_matrix_ptr(C::GauXCMatrix matrix) noexcept {

0 commit comments

Comments
 (0)