Skip to content

Commit

Permalink
Merge pull request #164 from MolSSI-MDI/poll
Browse files Browse the repository at this point in the history
Poll
  • Loading branch information
taylor-a-barnes authored Jun 27, 2024
2 parents 686690a + c724d59 commit 1df32e5
Show file tree
Hide file tree
Showing 23 changed files with 334 additions and 32 deletions.
2 changes: 1 addition & 1 deletion MDI_Library/__init__.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
MDI_TCP, MDI_MPI, MDI_LINK, MDI_PLUGIN, MDI_TEST, \
MDI_DRIVER, MDI_ENGINE, \
MDI_MAJOR_VERSION, MDI_MINOR_VERSION, MDI_PATCH_VERSION, \
MDI_Init, MDI_Accept_Communicator, \
MDI_Init, MDI_Check_for_communicator, MDI_Accept_Communicator, \
MDI_Send, MDI_Recv, MDI_Send_Command, MDI_Recv_Command, \
MDI_Conversion_Factor, MDI_Get_Role, \
MDI_MPI_get_world_comm, \
Expand Down
19 changes: 17 additions & 2 deletions MDI_Library/mdi.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,21 @@ int MDI_Initialized(int* flag)
return 0;
}

/*! \brief Check if a new communicator is available
*
* The function returns whether there is currently a new communicator that can be accepted via MDI_Accept_communicator.
* If new communicators are available, the function returns \p 1. Otherwise, it returns \p 0.
*
*/
int MDI_Check_for_communicator(int* flag)
{
if ( codes.initialized == 0 ) {
mdi_error("MDI_Check_for_communicator called but MDI has not been initialized");
return 1;
}
return general_check_communicator(flag);
}

/*! \brief Accept a new MDI communicator
*
* The function returns an MDI_Comm that describes a connection between two codes.
Expand All @@ -294,14 +309,14 @@ int MDI_Accept_communicator(MDI_Comm* comm)
int ret;

if ( codes.initialized == 0 ) {
mdi_error("MDI_Accept_Communicator called but MDI has not been initialized");
mdi_error("MDI_Accept_communicator called but MDI has not been initialized");
return 1;
}
*comm = general_accept_communicator();

ret = mdi_debug("[MDI:MDI_Accept_communicator] Comm: %d\n", *comm);
if ( ret != 0 ) {
mdi_error("Error in MDI_Accept_Communicator: mdi_debug failed");
mdi_error("Error in MDI_Accept_communicator: mdi_debug failed");
return ret;
}

Expand Down
1 change: 1 addition & 0 deletions MDI_Library/mdi.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ DllExport extern const int MDI_ENGINE;
// functions for handling MDI communication
DllExport int MDI_Init(int* argc, char ***argv);
DllExport int MDI_Initialized(int* flag);
DllExport int MDI_Check_for_communicator(int* flag);
DllExport int MDI_Accept_Communicator(MDI_Comm* comm);
DllExport int MDI_Accept_communicator(MDI_Comm* comm);
DllExport int MDI_Send(const void* buf, int count, MDI_Datatype datatype, MDI_Comm comm);
Expand Down
14 changes: 14 additions & 0 deletions MDI_Library/mdi.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,20 @@ def MDI_MPI_set_world_comm(new_comm):
global intra_code_comm
intra_code_comm = new_comm

# MDI_Check_Command_Exists
mdi.MDI_Check_for_communicator.argtypes = [ctypes.POINTER(ctypes.c_int)]
mdi.MDI_Check_for_communicator.restype = ctypes.c_int
def MDI_Check_for_communicator():
arg_size = ctypes.sizeof(ctypes.c_int)
flag = (ctypes.c_int*arg_size)()

ret = mdi.MDI_Check_for_communicator(flag)
if ret != 0:
raise Exception("MDI Error: MDI_Check_Command_Exists failed")
flag_cast = ctypes.cast(flag, ctypes.POINTER(ctypes.c_int)).contents

return flag_cast.value

# MDI_Accept_Communicator
mdi.MDI_Accept_Communicator.argtypes = [ctypes.POINTER(ctypes.c_int)]
mdi.MDI_Accept_Communicator.restype = ctypes.c_int
Expand Down
22 changes: 22 additions & 0 deletions MDI_Library/mdi_f90.F90
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ FUNCTION MDI_Init_with_options_(options) bind(c, name="MDI_Init_with_options")
INTEGER(KIND=C_INT) :: MDI_Init_with_options_
END FUNCTION MDI_Init_with_options_

FUNCTION MDI_Check_for_communicator_(flag) bind(c, name="MDI_Check_for_communicator")
USE, INTRINSIC :: iso_c_binding
TYPE(C_PTR), VALUE :: flag
INTEGER(KIND=C_INT) :: MDI_Check_for_communicator_
END FUNCTION MDI_Check_for_communicator_

FUNCTION MDI_Accept_Communicator_(comm) bind(c, name="MDI_Accept_Communicator")
USE, INTRINSIC :: iso_c_binding
TYPE(C_PTR), VALUE :: comm
Expand Down Expand Up @@ -508,6 +514,22 @@ SUBROUTINE MDI_Init(foptions, ierr)

END SUBROUTINE MDI_Init

SUBROUTINE MDI_Check_for_communicator(flag, ierr)
USE, INTRINSIC :: ISO_C_BINDING
IMPLICIT NONE
#if MDI_WINDOWS
!GCC$ ATTRIBUTES DLLEXPORT :: MDI_Check_for_communicator
!DEC$ ATTRIBUTES DLLEXPORT :: MDI_Check_for_communicator
#endif
INTEGER, INTENT(OUT) :: flag
INTEGER, INTENT(OUT) :: ierr

INTEGER(KIND=C_INT), TARGET :: cbuf

ierr = MDI_Check_for_communicator_(c_loc(cbuf))
flag = cbuf
END SUBROUTINE MDI_Check_for_communicator

SUBROUTINE MDI_Accept_Communicator(communicator, ierr)
USE, INTRINSIC :: ISO_C_BINDING
IMPLICIT NONE
Expand Down
67 changes: 46 additions & 21 deletions MDI_Library/mdi_general.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ int general_init(const char* options) {
ret = get_current_code(&this_code);
if ( ret != 0 ) {
mdi_error("Error in general_init: get_current_code failed");
return 1;
return ret;
}

// Create method objects for each supported method
Expand Down Expand Up @@ -376,6 +376,31 @@ int general_init(const char* options) {
}


/*! \brief Check if a new communicator is available
*
* The function returns whether there is currently a new communicator that can be accepted via MDI_Accept_communicator.
* If new communicators are available, the function returns \p 1. Otherwise, it returns \p 0.
*
*/
int general_check_communicator(int* flag) {
int ret;

code* this_code;
ret = get_current_code(&this_code);
if ( ret != 0 ) {
mdi_error("Error in general_check_communicator: get_current_code failed");
return ret;
}
method* selected_method;
ret = get_method(codes.current_key, this_code->selected_method_id, &selected_method);
if ( ret != 0 ) {
mdi_error("Error in general_check_communicator: get_method failed");
return ret;
}
return selected_method->on_check_communicator(flag);
}


/*! \brief Accept a new MDI communicator
*
* The function returns an MDI_Comm that describes a connection between two codes.
Expand All @@ -389,7 +414,7 @@ int general_accept_communicator() {
ret = get_current_code(&this_code);
if ( ret != 0 ) {
mdi_error("Error in general_accept_communicator: get_current_code failed");
return 1;
return ret;
}
method* selected_method;
ret = get_method(codes.current_key, this_code->selected_method_id, &selected_method);
Expand Down Expand Up @@ -422,7 +447,7 @@ int general_send(const void* buf, int count, MDI_Datatype datatype, MDI_Comm com
ret = get_current_code(&this_code);
if ( ret != 0 ) {
mdi_error("Error in general_send: get_current_code failed");
return 1;
return ret;
}
communicator* this;
ret = get_communicator(codes.current_key, comm, &this);
Expand Down Expand Up @@ -486,7 +511,7 @@ int general_recv(void* buf, int count, MDI_Datatype datatype, MDI_Comm comm) {
ret = get_current_code(&this_code);
if ( ret != 0 ) {
mdi_error("Error in general_recv: get_current_code failed");
return 1;
return ret;
}
communicator* this;
ret = get_communicator(codes.current_key, comm, &this);
Expand Down Expand Up @@ -597,7 +622,7 @@ int general_send_command(const char* buf, MDI_Comm comm) {
ret = get_current_code(&this_code);
if ( ret != 0 ) {
mdi_error("Error in general_send_command: get_current_code failed");
return 1;
return ret;
}
method* selected_method;
ret = get_method(codes.current_key, this_code->selected_method_id, &selected_method);
Expand Down Expand Up @@ -676,7 +701,7 @@ int general_builtin_command(const char* buf, MDI_Comm comm, int* flag) {
ret = get_current_code(&this_code);
if ( ret != 0 ) {
mdi_error("Error in general_builtin_command: get_current_code failed");
return 1;
return ret;
}

// check if this command corresponds to one of MDI's standard built-in commands
Expand Down Expand Up @@ -756,7 +781,7 @@ int general_recv_command(char* buf, MDI_Comm comm) {
ret = get_current_code(&this_code);
if ( ret != 0 ) {
mdi_error("Error in general_recv_command: get_current_code failed");
return 1;
return ret;
}
communicator* this;
ret = get_communicator(codes.current_key, comm, &this);
Expand All @@ -782,7 +807,7 @@ int general_recv_command(char* buf, MDI_Comm comm) {
ret = get_current_code(&this_code);
if ( ret != 0 ) {
mdi_error("Error in general_recv_command: second get_current_code failed");
return 1;
return ret;
}

// only receive on rank 0
Expand Down Expand Up @@ -841,7 +866,7 @@ int register_node(vector* node_vec, const char* node_name)
ret = get_current_code(&this_code);
if ( ret != 0 ) {
mdi_error("Error in register_node: get_current_code failed");
return 1;
return ret;
}
if ( this_code->intra_rank != 0 ) {
return 0;
Expand All @@ -858,7 +883,7 @@ int register_node(vector* node_vec, const char* node_name)
ret = get_node_index(node_vec, node_name, &node_index);
if ( ret != 0 ) {
mdi_error("Error in register_node: get_node_index failed");
return 1;
return ret;
}
if ( node_index != -1 ) {
mdi_error("This node is already registered");
Expand Down Expand Up @@ -902,7 +927,7 @@ int register_command(vector* node_vec, const char* node_name, const char* comman
ret = get_current_code(&this_code);
if ( ret != 0 ) {
mdi_error("Error in register_command: get_current_code failed");
return 1;
return ret;
}
if ( this_code->intra_rank != 0 ) {
return 0;
Expand All @@ -925,7 +950,7 @@ int register_command(vector* node_vec, const char* node_name, const char* comman
ret = get_node_index(node_vec, node_name, &node_index);
if ( ret != 0 ) {
mdi_error("Error in register_command: get_node_index failed");
return 1;
return ret;
}
if ( node_index == -1 ) {
mdi_error("Attempting to register a command on an unregistered node");
Expand All @@ -939,7 +964,7 @@ int register_command(vector* node_vec, const char* node_name, const char* comman
ret = get_command_index(target_node, command_name, &command_index);
if ( ret != 0 ) {
mdi_error("Error in register_command: get_command_index failed");
return 1;
return ret;
}
if ( command_index != -1 ) {
mdi_error("This command is already registered for this node");
Expand Down Expand Up @@ -979,7 +1004,7 @@ int register_callback(vector* node_vec, const char* node_name, const char* callb
ret = get_current_code(&this_code);
if ( ret != 0 ) {
mdi_error("Error in register_callback: get_current_code failed");
return 1;
return ret;
}
if ( this_code->intra_rank != 0 ) {
return 0;
Expand All @@ -1002,7 +1027,7 @@ int register_callback(vector* node_vec, const char* node_name, const char* callb
ret = get_node_index(node_vec, node_name, &node_index);
if ( ret != 0 ) {
mdi_error("Error in register_callback: get_node_index failed");
return 1;
return ret;
}
if ( node_index == -1 ) {
mdi_error("Attempting to register a callback on an unregistered node");
Expand All @@ -1016,7 +1041,7 @@ int register_callback(vector* node_vec, const char* node_name, const char* callb
ret = get_callback_index(target_node, callback_name, &callback_index);
if ( ret != 0 ) {
mdi_error("Error in register_callback: get_callback_index failed");
return 1;
return ret;
}
if ( callback_index != -1 ) {
mdi_error("This callback is already registered for this node");
Expand Down Expand Up @@ -1051,7 +1076,7 @@ int send_command_list(MDI_Comm comm) {
ret = get_current_code(&this_code);
if ( ret != 0 ) {
mdi_error("Error in send_command_list: get_current_code failed");
return 1;
return ret;
}
communicator* this_comm;
ret = get_communicator(codes.current_key, comm, &this_comm);
Expand Down Expand Up @@ -1247,7 +1272,7 @@ int send_node_list(MDI_Comm comm) {
ret = get_current_code(&this_code);
if ( ret != 0 ) {
mdi_error("Error in send_node_list: get_current_code failed");
return 1;
return ret;
}
communicator* this_comm;
ret = get_communicator(codes.current_key, comm, &this_comm);
Expand Down Expand Up @@ -1315,7 +1340,7 @@ int send_ncommands(MDI_Comm comm) {
ret = get_current_code(&this_code);
if ( ret != 0 ) {
mdi_error("Error in send_ncommands: get_current_code failed");
return 1;
return ret;
}
if ( this_code->intra_rank != 0 ) {
mdi_error("Attempting to send command information from the incorrect rank");
Expand Down Expand Up @@ -1352,7 +1377,7 @@ int send_ncallbacks(MDI_Comm comm) {
ret = get_current_code(&this_code);
if ( ret != 0 ) {
mdi_error("Error in send_ncallbacks: get_current_code failed");
return 1;
return ret;
}
if ( this_code->intra_rank != 0 ) {
mdi_error("Attempting to send callback information from the incorrect rank");
Expand Down Expand Up @@ -1389,7 +1414,7 @@ int send_nnodes(MDI_Comm comm) {
ret = get_current_code(&this_code);
if ( ret != 0 ) {
mdi_error("Error in send_nnodes: get_current_code failed");
return 1;
return ret;
}
if ( this_code->intra_rank != 0 ) {
mdi_error("Attempting to send callback information from the incorrect rank");
Expand Down
1 change: 1 addition & 0 deletions MDI_Library/mdi_general.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

int general_init_code();
int general_init(const char* options);
int general_check_communicator(int* flag);
int general_accept_communicator();
int general_send(const void* buf, int count, MDI_Datatype datatype, MDI_Comm comm);
int general_recv(void* buf, int count, MDI_Datatype datatype, MDI_Comm comm);
Expand Down
1 change: 1 addition & 0 deletions MDI_Library/mdi_global.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ typedef struct element_struct {
typedef struct method_struct {
/*! \brief Function pointer for method initialization work */
int (*on_selection)();
int (*on_check_communicator)(int* flag);
int (*on_accept_communicator)();
int (*on_send_command)(const char*, MDI_Comm_Type, int* skip_flag);
int (*after_send_command)(const char*, MDI_Comm_Type);
Expand Down
Loading

0 comments on commit 1df32e5

Please sign in to comment.