Skip to content

Commit 33b18fb

Browse files
committed
the segfault caused by device gpu
1 parent 8a3ce55 commit 33b18fb

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

source/module_psi/psi_init.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ PSIInit<T, Device>::PSIInit(const std::string& init_wfc_in,
2727
this->pw_wfc = pw_wfc_in;
2828
}
2929

30+
template <typename T, typename Device>
31+
PSIInit<T, Device>::~PSIInit()
32+
{
33+
if (this->use_psiinitializer)
34+
{
35+
{
36+
this->psi_init->deallocate_psig();
37+
// delete this->psi_init;
38+
// this->psi_init = nullptr;
39+
}
40+
}
41+
}
42+
3043
template <typename T, typename Device>
3144
void PSIInit<T, Device>::prepare_init(Structure_Factor* p_sf,
3245
UnitCell* p_ucell,

source/module_psi/psi_init.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class PSIInit
1717
const std::string& basis_type_in,
1818
const bool& use_psiinitializer_in,
1919
ModulePW::PW_Basis_K* pw_wfc_in);
20-
~PSIInit(){ this->psi_init->deallocate_psig(); }
20+
~PSIInit();
2121

2222
// prepare the wavefunction initialization
2323
void prepare_init(Structure_Factor* p_sf, //< structure factor

source/module_psi/psi_initializer.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@
1111
#include "module_base/parallel_reduce.h"
1212
#endif
1313

14+
template<typename T, typename Device>
15+
PsiInitializer<T, Device>::PsiInitializer()
16+
{
17+
// the following line will simultaneously run by CPU and GPU
18+
// For CPU, this->device will get value base_device::CpuDevice
19+
// For GPU, this->device will get value base_device::GpuDevice
20+
this->device = base_device::get_device_type<Device>(this->ctx);
21+
}
22+
1423
template<typename T, typename Device>
1524
psi::Psi<std::complex<double>>* PsiInitializer<T, Device>::allocate(const bool only_psig)
1625
{
@@ -92,7 +101,7 @@ psi::Psi<std::complex<double>>* PsiInitializer<T, Device>::allocate(const bool o
92101
this->d_psig_ = new psi::Psi<T, Device>(nks_psig,
93102
nbands_actual,
94103
nbasis_actual,
95-
this->pw_wfc_->npwk);
104+
this->pw_wfc_->npwk); // psig can be directly allocate on GPU
96105

97106
double memory_cost_psig =
98107
nks_psig * nbands_actual * this->pw_wfc_->npwk_max * PARAM.globalv.npol * sizeof(T);

source/module_psi/psi_initializer.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class PsiInitializer
6868
public:
6969
// technical notes:
7070
// Polymorphism is used to implement different methods, and achieved by pointers and virtual functions
71-
PsiInitializer() {};
71+
PsiInitializer();
7272
virtual ~PsiInitializer() {};
7373
#ifdef __MPI // MPI additional implementation
7474
/// @brief initialize the PsiInitializer with external data and methods
@@ -197,7 +197,10 @@ class PsiInitializer
197197
// refactored psig, in old version it is of datatype Psi<T, Device>*, use std::shared_ptr to
198198
// avoid memory leak
199199
// std::shared_ptr<psi::Psi<T, Device>> psig_;
200-
200+
201+
Device* ctx = {}; // device information carrier. The ctx can have type DEVICE_CPU or DEVICE_GPU
202+
base_device::DEVICE_CPU* cpu_ctx = {};
203+
base_device::AbacusDevice_t device = {};
201204
psi::Psi<T, Device>* d_psig_ = nullptr; // is it possible to directly operate data on GPU?
202205
private:
203206
int mem_saver_ = 0;

0 commit comments

Comments
 (0)