Molssi Driver Interface Library
mdi_global.h
Go to the documentation of this file.
1 
6 #ifndef MDI_GLOBAL
7 #define MDI_GLOBAL
8 
9 #include <mpi.h>
10 
11 #ifdef _WIN32
12  #include <winsock2.h>
13  #define sock_t SOCKET
14 #else
15  #define sock_t int
16 #endif
17 
18 #ifdef _WIN32
19  #define mdi_strdup _strdup
20 #else
21  #define mdi_strdup strdup
22 #endif
23 
24 // Hard-coded values
25 #define PLUGIN_PATH_LENGTH 2048
26 
27 // Defined languages
28 #define MDI_LANGUAGE_C 1
29 #define MDI_LANGUAGE_FORTRAN 2
30 #define MDI_LANGUAGE_PYTHON 3
31 
32 // MDI version numbers
33 #define MDI_MAJOR_VERSION_ 1
34 #define MDI_MINOR_VERSION_ 4
35 #define MDI_PATCH_VERSION_ 19
36 
37 // length of an MDI command in characters
38 #define MDI_COMMAND_LENGTH_ 256
39 
40 // length of an MDI name in characters
41 #define MDI_NAME_LENGTH_ 256
42 
43 // length of an MDI label in characters
44 #define MDI_LABEL_LENGTH_ 64
45 
46 // value of a null communicator
47 #define MDI_COMM_NULL_ 0
48 
49 // MDI data types
50 #define MDI_INT_ 1
51 #define MDI_INT8_T_ 7
52 #define MDI_INT16_T_ 8
53 #define MDI_INT32_T_ 9
54 #define MDI_INT64_T_ 10
55 #define MDI_UINT8_T_ 11
56 #define MDI_UINT16_T_ 12
57 #define MDI_UINT32_T_ 13
58 #define MDI_UINT64_T_ 14
59 #define MDI_DOUBLE_ 2
60 #define MDI_CHAR_ 3
61 #define MDI_FLOAT_ 4
62 #define MDI_BYTE_ 6
63 
64 // MDI communication types
65 #define MDI_TCP_ 1
66 #define MDI_MPI_ 2
67 #define MDI_LINK_ 3
68 #define MDI_TEST_ 4
69 
70 // MDI role types
71 #define MDI_DRIVER_ 1
72 #define MDI_ENGINE_ 2
73 
74 // MDI Typedefs
75 typedef int MDI_Comm_Type;
76 typedef int MDI_Datatype_Type;
77 typedef int (*MDI_execute_command_type)(void*, MDI_Comm_Type, void*);
78 
79 /*
80 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
81 !!!!!!!!!!!!!!!!! ABI WARNING !!!!!!!!!!!!!!!!!!!!
82 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
83 
84 Changing this structure will affect the plugin ABI
85 
86 */
87 typedef struct dynamic_array_struct {
89  unsigned char* data;
91  size_t stride;
93  size_t capacity;
95  size_t size; //number of elements actually stored
97  size_t current_key; //number of elements actually stored
100 } vector;
101 
102 
103 /*
104 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
105 !!!!!!!!!!!!!!!!! ABI WARNING !!!!!!!!!!!!!!!!!!!!
106 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
107 
108 Changing this structure will affect the plugin ABI
109 
110 */
111 typedef struct element_struct {
113  void* data;
115  size_t id;
116 } element;
117 
118 
119 typedef struct method_struct {
121  int (*on_selection)();
122  int (*on_accept_communicator)();
123  int (*on_send_command)(const char*, MDI_Comm_Type, int* skip_flag);
124  int (*after_send_command)(const char*, MDI_Comm_Type);
125  int (*on_recv_command)(MDI_Comm_Type);
127  int id;
130 } method;
131 
132 typedef struct communicator_struct {
134  int mdi_version[3];
138  void* method_data;
140  int (*send)(const void*, int, MDI_Datatype_Type, MDI_Comm_Type, int);
142  int (*recv)(void*, int, MDI_Datatype_Type, MDI_Comm_Type, int);
144  int (*delete)(void*);
146  size_t code_id;
148  sock_t sockfd;
152  MDI_Comm_Type id;
159 } communicator;
160 
161 typedef struct node_struct {
163  char name[MDI_COMMAND_LENGTH_];
168 } node;
169 
170 typedef struct code_struct {
172  char name[MDI_NAME_LENGTH_];
174  char role[MDI_NAME_LENGTH_];
184  char* plugin_path;
194  char* hostname;
196  int (*driver_callback_f90)(void*);
198  int (*execute_command_wrapper)(const char*, MDI_Comm_Type, void*);
200  MDI_execute_command_type execute_command;
202  int (*language_on_destroy)(int);
204  int (*mpi4py_recv_callback)(void*, int, int, int, MDI_Comm_Type);
206  int (*mpi4py_send_callback)(void*, int, int, int, MDI_Comm_Type);
208  int (*mpi4py_allgather_callback)(void*, void*);
210  int (*mpi4py_gather_names_callback)(void*, void*, int*, int*);
212  int (*mpi4py_split_callback)(int, int, MDI_Comm_Type, int);
214  int (*mpi4py_rank_callback)(int);
216  int (*mpi4py_size_callback)(int);
220  int (*py_launch_plugin_callback)(void*, void*, void*, int);
222  MPI_Comm intra_MPI_comm;
224  sock_t tcp_socket;
235  size_t id;
241  int language;
259  int port;
262 } code;
263 
264 
265 
269 extern vector codes;
270 
271 
272 
273 int vector_init(vector* v, size_t stride);
274 int vector_push_back(vector* v, void* element);
275 int vector_get(vector* v, int index, void** element);
276 int vector_delete(vector* v, int index);
277 int vector_free(vector* v);
278 
279 int get_node_index(vector* v, const char* node_name, int* node_index);
280 int get_command_index(node* n, const char* command_name, int* command_index);
281 int get_callback_index(node* n, const char* callback_name, int* callback_index);
282 int free_node_vector(vector* v);
283 
284 int new_communicator(size_t code_id, int method, MDI_Comm_Type* comm_id_ptr);
285 int get_communicator(size_t code_id, MDI_Comm_Type comm_id, communicator** comm_ptr);
286 int delete_communicator(size_t code_id, MDI_Comm_Type comm_id);
287 
288 int new_code(size_t* code_id);
289 int get_code(size_t code_id, code** ret_code);
290 int delete_code(size_t code_id);
291 int get_current_code(code** this_code_ptr);
292 
293 int new_method(size_t code_id, int method_id, int* id_ptr);
294 int get_method(size_t code_id, int method_id, method** method_ptr);
295 int delete_method(size_t code_id, int method_id);
297 
299 int file_exists(const char* file_name, int* flag);
300 
302 int communicator_delete(void* comm);
303 
304 void mdi_error(const char* message);
305 void mdi_warning(const char* message);
306 int mdi_debug(const char* message, ...);
307 
309 int datatype_info(MDI_Datatype_Type datatype, size_t* size, MDI_Datatype_Type* base);
310 
312 int datatype_mpitype(MDI_Datatype_Type datatype, MPI_Datatype* mpitype);
313 
315 int convert_buf_datatype(void* recvbuf_in, MDI_Datatype_Type recvtype,
316  void* sendbuf_in, MDI_Datatype_Type sendtype,
317  int count);
318 
319 #endif
get_node_index
int get_node_index(vector *v, const char *node_name, int *node_index)
Determine the index of a node within a vector of nodes.
Definition: mdi_global.c:163
communicator_delete
int communicator_delete(void *comm)
Dummy function for method-specific deletion operations for communicator deletion.
Definition: mdi_global.c:763
code_struct::plugin_argc_ptr
int * plugin_argc_ptr
Argument count for plugin command-line options.
Definition: mdi_global.h:186
vector_free
int vector_free(vector *v)
Free all data associated with a vector.
Definition: mdi_global.c:127
dynamic_array_struct::capacity
size_t capacity
Total number of elements that can be stored by this vector.
Definition: mdi_global.h:93
code_struct::role
char role[MDI_NAME_LENGTH_]
Role of the driver/engine.
Definition: mdi_global.h:174
node_struct::commands
vector * commands
Vector containing all the commands supported at this node.
Definition: mdi_global.h:165
code_struct::returned_comms
int returned_comms
The number of communicator handles that have been returned by MDI_Accept_Connection()
Definition: mdi_global.h:237
communicator_struct
Definition: mdi_global.h:132
communicator_struct::sockfd
sock_t sockfd
For communicators using the TCP communicatiom method, the socket descriptor (WINDOWS)
Definition: mdi_global.h:148
code_struct::shared_state_from_driver
void * shared_state_from_driver
Shared plugin state, received from the driver during plugin calculations.
Definition: mdi_global.h:192
code_struct::mpi4py_size_callback
int(* mpi4py_size_callback)(int)
Python callback pointer for MPI_Comm_size.
Definition: mdi_global.h:216
free_node_vector
int free_node_vector(vector *v)
Determine the index of a callback within a node.
Definition: mdi_global.c:239
communicator_struct::method_data
void * method_data
Method-specific information for this communicator.
Definition: mdi_global.h:138
mdi_error
void mdi_error(const char *message)
Print error message.
Definition: mdi_global.c:799
code_struct::name
char name[MDI_NAME_LENGTH_]
Name of the driver/engine.
Definition: mdi_global.h:172
vector_get
int vector_get(vector *v, int index, void **element)
Return a pointer to an element of a vector.
Definition: mdi_global.c:147
communicator_struct::send
int(* send)(const void *, int, MDI_Datatype_Type, MDI_Comm_Type, int)
Function pointer for method-specific send operations.
Definition: mdi_global.h:140
code_struct::comms
vector * comms
Vector containing all communicators associated with this code.
Definition: mdi_global.h:180
communicator_struct::recv
int(* recv)(void *, int, MDI_Datatype_Type, MDI_Comm_Type, int)
Function pointer for method-specific receive operations.
Definition: mdi_global.h:142
code_struct::mpi4py_allgather_callback
int(* mpi4py_allgather_callback)(void *, void *)
Python callback pointer for the initial MPI allgather.
Definition: mdi_global.h:208
new_communicator
int new_communicator(size_t code_id, int method, MDI_Comm_Type *comm_id_ptr)
Create a new communicator structure and add it to the list of communicators Returns the handle of the...
Definition: mdi_global.c:619
dynamic_array_struct::initialized
int initialized
Flag whether the vector has been initialized.
Definition: mdi_global.h:99
code_struct::mpi_initialized
int mpi_initialized
Flag whether this code has previously initialized MPI.
Definition: mdi_global.h:255
file_exists
int file_exists(const char *file_name, int *flag)
Check whether a file exists.
Definition: mdi_global.c:773
code_struct::mpi4py_split_callback
int(* mpi4py_split_callback)(int, int, MDI_Comm_Type, int)
Python callback pointer for MPI_Comm_split.
Definition: mdi_global.h:212
code_struct::intra_MPI_comm
MPI_Comm intra_MPI_comm
MPI intra-communicator that spans all ranks associated with this code.
Definition: mdi_global.h:222
element_struct
Definition: mdi_global.h:111
code_struct::mpi4py_send_callback
int(* mpi4py_send_callback)(void *, int, int, int, MDI_Comm_Type)
Python callback pointer for MPI_Send.
Definition: mdi_global.h:206
code_struct::language_on_destroy
int(* language_on_destroy)(int)
Function pointer to the language-specific destructor function.
Definition: mdi_global.h:202
code_struct::plugin_argv_ptr
char *** plugin_argv_ptr
Argument vector for plugin command-line options.
Definition: mdi_global.h:188
method_struct::id
int id
ID of this method.
Definition: mdi_global.h:127
communicator_struct::name_length
int name_length
The value of MDI_NAME_LENGTH for the connected code.
Definition: mdi_global.h:156
vector_delete
int vector_delete(vector *v, int index)
Remove an element from a vector.
Definition: mdi_global.c:93
mdi_debug
int mdi_debug(const char *message,...)
Print a debug message.
Definition: mdi_global.c:820
dynamic_array_struct::current_key
size_t current_key
If the array has a currently active component, this is that value.
Definition: mdi_global.h:97
get_code
int get_code(size_t code_id, code **ret_code)
Get a code from a code handle Returns a pointer to the code.
Definition: mdi_global.c:359
code_struct::language
int language
Native language of this code.
Definition: mdi_global.h:241
code_struct::debug_mode
int debug_mode
Flag for whether this code is running in debug mode.
Definition: mdi_global.h:261
communicator_struct::command_length
int command_length
The value of MDI_COMMAND_LENGTH for the connected code.
Definition: mdi_global.h:158
code_struct::ipi_compatibility
int ipi_compatibility
Flag for whether MDI is running in i-PI compatibility mode.
Definition: mdi_global.h:249
get_communicator
int get_communicator(size_t code_id, MDI_Comm_Type comm_id, communicator **comm_ptr)
Get a communicator from a communicator handle Returns a pointer to the communicator.
Definition: mdi_global.c:660
code_struct::world_rank
int world_rank
Rank of this process within MPI_COMM_WORLD.
Definition: mdi_global.h:233
get_callback_index
int get_callback_index(node *n, const char *callback_name, int *callback_index)
Determine the index of a callback within a node.
Definition: mdi_global.c:216
element_struct::id
size_t id
ID of this element.
Definition: mdi_global.h:115
get_current_code
int get_current_code(code **this_code_ptr)
Get the currently active code Returns a pointer to the code.
Definition: mdi_global.c:387
code_struct::tcp_initialized
int tcp_initialized
Flag whether this code has previously initialized TCP.
Definition: mdi_global.h:253
new_code
int new_code(size_t *code_id)
Create a new code structure and add it to the list of codes Returns the index of the new code.
Definition: mdi_global.c:267
get_command_index
int get_command_index(node *n, const char *command_name, int *command_index)
Determine the index of a command within a node.
Definition: mdi_global.c:189
dynamic_array_struct::stride
size_t stride
Size of each element.
Definition: mdi_global.h:91
dynamic_array_struct::data
unsigned char * data
The elements stored by this vector.
Definition: mdi_global.h:89
communicator_struct::id
MDI_Comm_Type id
MDI_Comm handle that corresponds to this communicator.
Definition: mdi_global.h:152
communicator_struct::is_accepted
int is_accepted
Indicate whether this communicator has been accepted yet.
Definition: mdi_global.h:154
delete_communicator
int delete_communicator(size_t code_id, MDI_Comm_Type comm_id)
Delete a communicator.
Definition: mdi_global.c:694
code_struct::hostname
char * hostname
Hostname of the driver.
Definition: mdi_global.h:194
dynamic_array_struct::size
size_t size
Number of elements actually stored.
Definition: mdi_global.h:95
code_struct::id
size_t id
Handle for this code.
Definition: mdi_global.h:235
code_struct::driver_callback_f90
int(* driver_callback_f90)(void *)
Function pointer to the actual callback for Fortran drivers.
Definition: mdi_global.h:196
vector_push_back
int vector_push_back(vector *v, void *element)
Append a new element to the end of the vector.
Definition: mdi_global.c:60
node_struct::name
char name[MDI_COMMAND_LENGTH_]
Name of the node.
Definition: mdi_global.h:163
delete_method
int delete_method(size_t code_id, int method_id)
Delete a method Returns 0 on success.
Definition: mdi_global.c:540
code_struct::execute_command_obj
void * execute_command_obj
Pointer to the class object that is passed to any call to execute_command.
Definition: mdi_global.h:176
code_struct::intra_rank
int intra_rank
Rank of this process within its associated code.
Definition: mdi_global.h:243
method_struct::on_selection
int(* on_selection)()
Function pointer for method initialization work.
Definition: mdi_global.h:121
new_method
int new_method(size_t code_id, int method_id, int *id_ptr)
Create a new method structure and add it to the vector of methods Returns the handle of the new metho...
Definition: mdi_global.c:482
communicator_struct::code_id
size_t code_id
Handle for the id of the associated code.
Definition: mdi_global.h:146
element_struct::data
void * data
The actual data for this element.
Definition: mdi_global.h:113
get_method
int get_method(size_t code_id, int method_id, method **method_ptr)
Get a method from a method handle Returns a pointer to the method.
Definition: mdi_global.c:507
communicator_struct::nodes
vector * nodes
The nodes supported by the connected code.
Definition: mdi_global.h:136
code_struct::is_library
int is_library
Flag whether this code is being used as a library 0: Not a library 1: Is an ENGINE library,...
Definition: mdi_global.h:229
code_struct::mpi4py_gather_names_callback
int(* mpi4py_gather_names_callback)(void *, void *, int *, int *)
Python callback pointer for gathering names.
Definition: mdi_global.h:210
mdi_warning
void mdi_warning(const char *message)
Print warning message.
Definition: mdi_global.c:810
code_struct::next_comm
int next_comm
The handle of the next communicator.
Definition: mdi_global.h:239
convert_buf_datatype
int convert_buf_datatype(void *recvbuf_in, MDI_Datatype_Type recvtype, void *sendbuf_in, MDI_Datatype_Type sendtype, int count)
Convert a buffer from one datatype to another.
Definition: mdi_global.c:964
code_struct
Definition: mdi_global.h:170
code_struct::methods
vector * methods
Vector containing all supported methods.
Definition: mdi_global.h:182
code_struct::world_size
int world_size
Size of MPI_COMM_WORLD.
Definition: mdi_global.h:231
vector_init
int vector_init(vector *v, size_t stride)
Initialize memory allocation for a vector structure.
Definition: mdi_global.c:34
datatype_info
int datatype_info(MDI_Datatype_Type datatype, size_t *size, MDI_Datatype_Type *base)
Get information about an MDI_Datatype.
Definition: mdi_global.c:847
code_struct::port
int port
Port over which the driver will listen.
Definition: mdi_global.h:259
code_struct::execute_command_wrapper
int(* execute_command_wrapper)(const char *, MDI_Comm_Type, void *)
Function pointer to the language-specific wrapper for the execute_command function.
Definition: mdi_global.h:198
code_struct::mpi4py_recv_callback
int(* mpi4py_recv_callback)(void *, int, int, int, MDI_Comm_Type)
Python callback pointer for MPI_Recv.
Definition: mdi_global.h:204
communicator_struct::method_id
int method_id
Communication method used by this communicator.
Definition: mdi_global.h:150
code_struct::tcp_socket
sock_t tcp_socket
Socket for TCP connections.
Definition: mdi_global.h:224
free_methods_vector
int free_methods_vector(vector *v)
Free the memory associated with a methods vector.
Definition: mdi_global.c:595
delete_code
int delete_code(size_t code_id)
Delete a code Returns 0 on success.
Definition: mdi_global.c:401
dynamic_array_struct
Definition: mdi_global.h:87
code_struct::execute_command
MDI_execute_command_type execute_command
Function pointer to the generic execute_command_function.
Definition: mdi_global.h:200
code_struct::called_set_execute_command_func
int called_set_execute_command_func
Flag whether this code has called set_execute_command_func.
Definition: mdi_global.h:245
code_struct::nodes
vector * nodes
Vector containing all nodes supported by this code.
Definition: mdi_global.h:178
codes
vector codes
Vector containing all codes that have been initiailized on this rank. Typically, this will only inclu...
Definition: mdi_global.c:22
method_struct::method_id
int method_id
Communication method.
Definition: mdi_global.h:129
code_struct::plugin_path
char * plugin_path
Path to the plugins available to this code.
Definition: mdi_global.h:184
code_struct::selected_method_id
int selected_method_id
ID of the method being used for inter-code communication.
Definition: mdi_global.h:247
code_struct::py_launch_plugin_callback
int(* py_launch_plugin_callback)(void *, void *, void *, int)
Python callback pointer for MPI_Comm_barrier.
Definition: mdi_global.h:220
method_struct
Definition: mdi_global.h:119
code_struct::plugin_unedited_options_ptr
char ** plugin_unedited_options_ptr
Unedited command-line options for currently running plugin.
Definition: mdi_global.h:190
code_struct::initialized_mpi
int initialized_mpi
Flag for whether MDI called MPI_Init.
Definition: mdi_global.h:251
code_struct::mpi4py_rank_callback
int(* mpi4py_rank_callback)(int)
Python callback pointer for MPI_Comm_rank.
Definition: mdi_global.h:214
node_struct
Definition: mdi_global.h:161
datatype_mpitype
int datatype_mpitype(MDI_Datatype_Type datatype, MPI_Datatype *mpitype)
Get the MPI datatype that corresponds to an MDI datatype.
Definition: mdi_global.c:915
node_struct::callbacks
vector * callbacks
Vector containing all the callbacks associated with this node.
Definition: mdi_global.h:167
code_struct::mpi4py_barrier_callback
int(* mpi4py_barrier_callback)(int)
Python callback pointer for MPI_Comm_barrier.
Definition: mdi_global.h:218
code_struct::test_initialized
int test_initialized
Flag whether this code has previously initialized TEST.
Definition: mdi_global.h:257
communicator_struct::mdi_version
int mdi_version[3]
The MDI version of the connected code.
Definition: mdi_global.h:134