Skip to content
Open
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
24 changes: 24 additions & 0 deletions cmake/modules/FindLibRI.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,30 @@ if(LibRI_FOUND AND NOT TARGET LibRI::LibRI)
find_package(LibComm REQUIRED)
find_package(cereal REQUIRED CONFIG)

# A distribution-packaged cereal may resolve its install prefix to "/", so
# the imported target advertises the non-existent include directory
# "/include". CMake rejects such a path once the target is linked directly
# (as LibRI::LibRI does), so repair it with the headers found on disk.
if(TARGET cereal::cereal)
get_target_property(_cereal_include_dirs cereal::cereal
INTERFACE_INCLUDE_DIRECTORIES)
set(_cereal_include_dirs_valid TRUE)
foreach(_cereal_include_dir IN LISTS _cereal_include_dirs)
if(NOT IS_DIRECTORY "${_cereal_include_dir}")
set(_cereal_include_dirs_valid FALSE)
endif()
endforeach()
if(NOT _cereal_include_dirs_valid)
find_path(LIBRI_CEREAL_INCLUDE_DIR cereal/version.hpp)
if(NOT LIBRI_CEREAL_INCLUDE_DIR)
message(FATAL_ERROR "cereal headers were not found")
endif()
set_target_properties(
cereal::cereal
PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${LIBRI_CEREAL_INCLUDE_DIR}")
endif()
endif()

add_library(LibRI::LibRI INTERFACE IMPORTED)
set_target_properties(
LibRI::LibRI
Expand Down
2 changes: 2 additions & 0 deletions source/Makefile.Objects
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ OBJS_BASE=assoc_laguerre.o\
mathzone_add1.o\
matrix.o\
matrix3.o\
memory_budget.o\
memory_recorder.o\
mymath.o\
para_gemm.o\
Expand Down Expand Up @@ -911,6 +912,7 @@ OBJS_LCAO=evolve_elec.o\
lcao_allocate.o\
lcao_set_mat2d.o\
lcao_init_basis.o\
lcao_memory_check.o\
setup_exx.o\
setup_deepks.o\
allocate_dm.o\
Expand Down
1 change: 1 addition & 0 deletions source/source_base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ add_library(
mathzone_add1.cpp
matrix.cpp
matrix3.cpp
memory_budget.cpp
memory_recorder.cpp
mymath.cpp
opt_cg.cpp
Expand Down
88 changes: 44 additions & 44 deletions source/source_base/complexmatrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ namespace ModuleBase
ComplexMatrix::ComplexMatrix(const int nrows, const int ncols, const bool flag_zero)
:nr(nrows),
nc(ncols),
size(nrows*ncols),
size(0),
c(nullptr)
{
size = matrix_size(nr, nc);
if( size )
{
c = new std::complex<double>[size];
Expand All @@ -29,7 +30,7 @@ ComplexMatrix::ComplexMatrix(const int nrows, const int ncols, const bool flag_z
// zero out the ComplexMatrix
void ComplexMatrix::zero_out(void)
{
for (int i=0; i<size; i++) c[i] = std::complex<double>(0.0,0.0);
for (std::size_t i=0; i<size; i++) c[i] = std::complex<double>(0.0,0.0);
}

/*
Expand Down Expand Up @@ -69,13 +70,14 @@ ComplexMatrix::ComplexMatrix( ComplexMatrix && m1 )
ComplexMatrix::ComplexMatrix(const matrix &m)
:nr(m.nr),
nc(m.nc),
size(m.nr*m.nc),
size(0),
c(nullptr)
{
size = matrix_size(nr, nc);
if( size )
{
c = new std::complex<double>[size];
for( int i=0; i<size; ++i)
for( std::size_t i=0; i<size; ++i)
{
c[i] = m.c[i];
}
Expand All @@ -95,35 +97,30 @@ ComplexMatrix::~ComplexMatrix()
// reallocate memory for Complex Matrix
void ComplexMatrix::create(const int nr_in, const int nc_in, const bool flag_zero)
{
if( nr_in && nc_in )
{
if(c)
{
const int size_in=nr_in*nc_in;
if( size_in!=nr*nc )
{
delete[] c;
c = new std::complex<double>[size_in];
}
}
else
{
c = new std::complex<double>[nr_in * nc_in];
}

nr = nr_in;
nc = nc_in;
size = nr*nc;
if(flag_zero) zero_out();
}
else
const std::size_t new_size = matrix_size(nr_in, nc_in);
if( new_size == 0 )
{
if(c) delete[] c;
c = nullptr;
nr = nr_in;
nc = nc_in;
size = nr*nc;
size = 0;
return;
}

if( c == nullptr || new_size != size )
{
// Allocate before releasing the old buffer so a bad_alloc leaves the
// matrix unchanged and the caller can still report the failure.
std::complex<double>* new_data = new std::complex<double>[new_size];
delete[] c;
c = new_data;
}

nr = nr_in;
nc = nc_in;
size = new_size;
if(flag_zero) zero_out();
}

void ComplexMatrix::set_as_identity_matrix(void)
Expand All @@ -132,8 +129,8 @@ void ComplexMatrix::set_as_identity_matrix(void)
{
for(int j=0; j<nc; j++)
{
if(i==j) c[nc * i + j] = std::complex<double>(1.0, 0.0);
else c[nc * i + j] = std::complex<double>(0.0, 0.0);
if(i==j) c[matrix_index(i, j, nc)] = std::complex<double>(1.0, 0.0);
else c[matrix_index(i, j, nc)] = std::complex<double>(0.0, 0.0);
}
}
return;
Expand Down Expand Up @@ -197,36 +194,39 @@ ComplexMatrix operator*(const ComplexMatrix &m1, const ComplexMatrix &m2)
ComplexMatrix operator*(const std::complex<double> &c,const ComplexMatrix &m)
{
ComplexMatrix sm(m);
for (int i=0 ;i<m.size; i++) sm.c[i] *= c;
for (std::size_t i=0 ;i<m.size; i++) sm.c[i] *= c;
return sm;
}

// ComplexMatrix scalar
ComplexMatrix operator*(const ComplexMatrix &m,const std::complex<double> &c)
{
ComplexMatrix sm(m);
for (int i = 0;i < m.size;i++) sm.c[i] *= c;
for (std::size_t i = 0;i < m.size;i++) sm.c[i] *= c;
return sm;
}

ComplexMatrix operator*(const double &r,const ComplexMatrix &m)
{
ComplexMatrix sm(m);
for(int i=0; i<m.size; i++) sm.c[i]*= r;
for(std::size_t i=0; i<m.size; i++) sm.c[i]*= r;
return sm;
}

ComplexMatrix operator*(const ComplexMatrix &m,const double &r)
{
ComplexMatrix sm(m);
for (int i=0; i<m.size; i++) sm.c[i] *= r;
for (std::size_t i=0; i<m.size; i++) sm.c[i] *= r;
return sm;
}

ComplexMatrix& ComplexMatrix::operator=(const ComplexMatrix &m)
{
this->create(m.nr, m.nc, false);
memcpy( c, m.c, size*sizeof(std::complex<double>) );
if( size )
{
memcpy( c, m.c, size*sizeof(std::complex<double>) );
}
return *this;
}

Expand All @@ -243,29 +243,29 @@ ComplexMatrix& ComplexMatrix::operator=( ComplexMatrix && m )

ComplexMatrix& ComplexMatrix::operator*=(const std::complex<double> &s)
{
for (int i = 0;i < this->size;i++) c[i] *= s;
for (std::size_t i = 0;i < this->size;i++) c[i] *= s;
return *this;
}

// Accumulate to a ComplexMatrix in place
ComplexMatrix& ComplexMatrix::operator+=(const ComplexMatrix &m)
{
for(int i=0; i<size; i++) this->c[i] += m.c[i];
for(std::size_t i=0; i<size; i++) this->c[i] += m.c[i];
return *this;
}

// decumulate to a ComplexMatrix in place
ComplexMatrix& ComplexMatrix::operator-=(const ComplexMatrix &m)
{
for(int i=0; i<size; i++) this->c[i] -= m.c[i];
for(std::size_t i=0; i<size; i++) this->c[i] -= m.c[i];
return *this;
}

// Peize Lin add 2017-03-29
matrix ComplexMatrix::real() const
{
matrix m(nr,nc,false);
for( int i=0; i<this->size; ++i) m.c[i] = c[i].real();
for( std::size_t i=0; i<this->size; ++i) m.c[i] = c[i].real();
return m;
}

Expand All @@ -285,7 +285,7 @@ void scale_accumulate(const std::complex<double> &s,
{
assert(min.nr == mout.nr);
assert(min.nc == mout.nc);
for (int j=0; j<min.size; j++)
for (std::size_t j=0; j<min.size; j++)
{
mout.c[j] += s * min.c[j];
}
Expand Down Expand Up @@ -318,7 +318,7 @@ void scaled_sum(const std::complex<double> &s1,
assert(m1.nc == m2.nc);
assert(m1.nc == mout.nc);

for(int i=0; i<m1.size; i++)
for(std::size_t i=0; i<m1.size; i++)
{
mout.c[i] = s1 * m1.c[i] + s2 * m2.c[i];
}
Expand Down Expand Up @@ -348,7 +348,7 @@ double abs2_row(const ComplexMatrix &m,const int ir)
std::complex<double> z;
for(int ic=0;ic<m.nc;ic++)
{
z = m.c[ m.nc*ir + ic];
z = m.c[ matrix_index(ir, ic, m.nc) ];
r += z.real()*z.real() + z.imag()*z.imag();
}
return r;
Expand All @@ -360,7 +360,7 @@ double abs2_column(const ComplexMatrix &m,const int ic)
std::complex<double> z;
for(int ir=0;ir<m.nr;ir++)
{
z = m.c[ m.nc*ir + ic ];
z = m.c[ matrix_index(ir, ic, m.nc) ];
r += z.real()*z.real() + z.imag()*z.imag();
}
return r;
Expand All @@ -372,7 +372,7 @@ double abs2(const ComplexMatrix &m)
double r=0.0;
std::complex<double> z;

for (int i = 0;i < m.size;i++)
for (std::size_t i = 0;i < m.size;i++)
{
z = m.c[i];
r += z.real() * z.real() + z.imag() * z.imag();
Expand Down Expand Up @@ -408,7 +408,7 @@ ComplexMatrix transpose(const ComplexMatrix &m, const bool &conjugate)
ComplexMatrix conj(const ComplexMatrix &m)
{
ComplexMatrix cm( m.nr, m.nc, false );
for(int i=0; i!=m.size; ++i)
for(std::size_t i=0; i!=m.size; ++i)
cm.c[i] = conj(m.c[i]);
return cm;
}
Expand Down
8 changes: 5 additions & 3 deletions source/source_base/complexmatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ class ComplexMatrix

int nr=0;
int nc=0;
int size=0;
/// Element count. Kept in size_t because an LCAO matrix can exceed
/// INT_MAX elements (issue #8342: NLOCAL = 377884 gives ~1.4e11).
std::size_t size=0;
Comment thread
mohanchen marked this conversation as resolved.
Resolved
std::complex<double> *c=nullptr;

ComplexMatrix(): nr(0), nc(0), size(0), c(nullptr){}
Expand All @@ -34,12 +36,12 @@ class ComplexMatrix
std::complex<double> &operator()(const int ir,const int ic)
{
assert(ir>=0); assert(ir<nr); assert(ic>=0); assert(ic<nc);
return c[ir*nc+ic];//mohan modify in-line 2007-10-1
return c[matrix_index(ir, ic, nc)];//mohan modify in-line 2007-10-1
}
const std::complex<double> &operator()(const int ir,const int ic)const
{
assert(ir>=0); assert(ir<nr); assert(ic>=0); assert(ic<nc);
return c[ir*nc+ic];//mohan modify in-line 2007-10-13
return c[matrix_index(ir, ic, nc)];//mohan modify in-line 2007-10-13
}

ComplexMatrix& operator*=(const std::complex<double> &s);
Expand Down
Loading
Loading