Skip to content

Commit 341a302

Browse files
author
jianrui geng
committed
Refactor i-PI socket driver into source_relax
Route calculation=socket through Relax_Driver, keep ESolver lifecycle in driver_run, and add source_relax socket transport tests.
1 parent 2dea783 commit 341a302

11 files changed

Lines changed: 431 additions & 172 deletions

File tree

source/CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -477,9 +477,7 @@ add_library(
477477
driver
478478
OBJECT
479479
source_main/driver.cpp
480-
source_main/driver_run.cpp
481-
source_main/driver_ipi.cpp
482-
source_main/ipi_socket.cpp)
480+
source_main/driver_run.cpp)
483481

484482
list(APPEND device_srcs
485483
source_pw/module_pwdft/kernels/nonlocal_op.cpp

source/source_main/driver.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ class Driver
3636

3737
// the actual calculations
3838
void driver_run();
39-
void driver_ipi_run();
4039

4140
// Init harewares according to Input parameters
4241
void init_hardware();

source/source_main/driver_run.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,7 @@ void Driver::driver_run()
3939
ModuleBase::TITLE("Driver", "driver_run");
4040

4141
const std::string cal = PARAM.inp.calculation;
42-
if (cal == "socket")
43-
{
44-
this->driver_ipi_run();
45-
return;
46-
}
42+
const bool socket_mode = (cal == "socket");
4743

4844
//! 1: setup cell and atom information
4945
// this warning should not be here, mohan 2024-05-22
@@ -66,12 +62,20 @@ void Driver::driver_run()
6662
unitcell::check_atomic_stru(ucell, PARAM.inp.min_dist_coef);
6763

6864
//! 2: initialize the ESolver (depends on a set-up ucell after `setup_cell`)
65+
Input_para esolver_inp = PARAM.inp;
66+
Input_para driver_inp = PARAM.inp;
67+
if (socket_mode)
68+
{
69+
esolver_inp.calculation = "scf";
70+
driver_inp.calculation = cal;
71+
}
72+
6973
this->init_hardware();
7074

71-
ModuleESolver::ESolver* p_esolver = ModuleESolver::init_esolver(PARAM.inp, ucell);
75+
ModuleESolver::ESolver* p_esolver = ModuleESolver::init_esolver(esolver_inp, ucell);
7276

7377
//! 3: initialize Esolver and fill json-structure
74-
p_esolver->before_all_runners(ucell, PARAM.inp);
78+
p_esolver->before_all_runners(ucell, esolver_inp);
7579

7680
// this Json part should be moved to before_all_runners, mohan 2024-05-12
7781
#ifdef __RAPIDJSON
@@ -83,10 +87,10 @@ void Driver::driver_run()
8387
{
8488
Run_MD::md_line(ucell, p_esolver, PARAM);
8589
}
86-
else if (cal == "scf" || cal == "relax" || cal == "cell-relax" || cal == "nscf")
90+
else if (cal == "scf" || cal == "relax" || cal == "cell-relax" || cal == "nscf" || cal == "socket")
8791
{
8892
Relax_Driver rl_driver;
89-
rl_driver.relax_driver(p_esolver, ucell, PARAM.inp, GlobalV::ofs_running);
93+
rl_driver.relax_driver(p_esolver, ucell, driver_inp, GlobalV::ofs_running);
9094
}
9195
else if (cal == "get_s")
9296
{

source/source_relax/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ add_library(
22
relax
33
OBJECT
44
relax_data.cpp
5+
ipi_socket.cpp
6+
socket_driver.cpp
57
cg_base.cpp
68
relax_driver.cpp
79
relax_sync.cpp
Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "ipi_socket.h"
1+
#include "source_relax/ipi_socket.h"
22

33
#include <arpa/inet.h>
44
#include <cerrno>
@@ -41,6 +41,10 @@ std::string padded_header(const std::string& header)
4141
}
4242
} // namespace
4343

44+
IpiSocketClosed::IpiSocketClosed(const std::string& message) : std::runtime_error(message)
45+
{
46+
}
47+
4448
IpiSocket::~IpiSocket()
4549
{
4650
this->close();
@@ -127,7 +131,28 @@ void IpiSocket::close()
127131
std::string IpiSocket::read_header()
128132
{
129133
char header[IPI_HEADER_LEN];
130-
this->read_exact(header, sizeof(header));
134+
std::size_t done = 0;
135+
while (done < sizeof(header))
136+
{
137+
const ssize_t nread = ::recv(fd_, header + done, sizeof(header) - done, 0);
138+
if (nread == 0)
139+
{
140+
if (done == 0)
141+
{
142+
throw IpiSocketClosed("i-PI socket closed before next header");
143+
}
144+
throw std::runtime_error("i-PI socket closed while reading header");
145+
}
146+
if (nread < 0)
147+
{
148+
if (errno == EINTR)
149+
{
150+
continue;
151+
}
152+
throw std::runtime_error(errno_message("i-PI socket header read failed"));
153+
}
154+
done += static_cast<std::size_t>(nread);
155+
}
131156
return trim_header(header);
132157
}
133158

@@ -198,7 +223,7 @@ void IpiSocket::read_exact(void* data, std::size_t nbytes)
198223
const ssize_t nread = ::recv(fd_, cursor + done, nbytes - done, 0);
199224
if (nread == 0)
200225
{
201-
throw std::runtime_error("i-PI socket closed while reading");
226+
throw IpiSocketClosed("i-PI socket closed while reading");
202227
}
203228
if (nread < 0)
204229
{
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@
22
#define ABACUS_IPI_SOCKET_H
33

44
#include <cstddef>
5+
#include <stdexcept>
56
#include <string>
67
#include <vector>
78

9+
class IpiSocketClosed : public std::runtime_error
10+
{
11+
public:
12+
explicit IpiSocketClosed(const std::string& message);
13+
};
14+
815
class IpiSocket
916
{
1017
public:

source/source_relax/relax_driver.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "relax_driver.h"
2+
#include "socket_driver.h"
23
#include "source_base/global_file.h"
34
#include "source_io/module_output/cif_io.h"
45
#include "source_io/module_json/output_info.h"
@@ -17,6 +18,14 @@ void Relax_Driver::relax_driver(
1718
ModuleBase::TITLE("Relax_Driver", "relax_driver");
1819
ModuleBase::timer::start("Relax_Driver", "relax_driver");
1920

21+
if (inp.calculation == "socket")
22+
{
23+
Socket_Driver socket_driver;
24+
socket_driver.socket_driver(p_esolver, ucell, inp, ofs_running);
25+
ModuleBase::timer::end("Relax_Driver", "relax_driver");
26+
return;
27+
}
28+
2029
this->init_relax(ucell.nat, inp);
2130

2231
// steps[0]: istep (main iteration step)

0 commit comments

Comments
 (0)