1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
|
/*============================================================================
* Wrapper file used to generate the Python bindings for ple_coupling functions
*============================================================================*/
/*
This file is part of the "Parallel Location and Exchange" library,
intended to provide mesh or particle-based code coupling services.
Copyright (C) 2005-2019 EDF S.A.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <Python.h>
#include "mpi4py/mpi4py.h"
/*----------------------------------------------------------------------------
* Standard C library headers
*----------------------------------------------------------------------------*/
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*----------------------------------------------------------------------------
* Local headers
*----------------------------------------------------------------------------*/
#include "ple_defs.h"
#include "ple_config_defs.h"
#include "ple_coupling.h"
/*----------------------------------------------------------------------------*/
#ifdef __cplusplus
extern "C" {
#if 0
} /* Fake brace to force Emacs auto-indentation back to column 0 */
#endif
#endif /* __cplusplus */
/*============================================================================
* Static local variables
*============================================================================*/
static int _n_sets = 0;
static ple_coupling_mpi_set_t **_mpi_sets;
/*----------------------------------------------------------------------------*/
/*============================================================================
* Python bindings for the C functions which will be called by Python
*============================================================================*/
/*----------------------------------------------------------------------------*/
/*!
* \brief Build a group id within a communicator based on its name
*
* \param [in] Comm mpi4py communicator.
* \param [in] group_name name associated with current group.
*
* \return id associated with local name.
*/
/*----------------------------------------------------------------------------*/
static PyObject *
pyple_coupling_mpi_name_to_id(PyObject *self, PyObject *args)
{
PyObject *py_comm = NULL;
MPI_Comm *comm_p = NULL;
const char *group_name = "";
int group_name_size = 0;
/* We verify that the correct arguments are provided.
* format is of the form "var1_typevar2_type..varn_type:function_name"
* function_name is used so that in case of a traceback error Python will
* know it failed here.
* If a CPython function returns NULL, it means it failed.
*/
if (!PyArg_ParseTuple(args, "Os#:ple_coupling_mpi_name_to_id",
&py_comm, &group_name, &group_name_size))
return NULL;
/* Retrieve the C MPI_Comm from the mpi4py.Comm */
comm_p = PyMPIComm_Get(py_comm);
if (comm_p == NULL)
return NULL;
/* Call the C function */
int new_id = ple_coupling_mpi_name_to_id(*comm_p, group_name);
/* Return a Python value */
return Py_BuildValue("i", new_id);
}
/*----------------------------------------------------------------------------*/
/*!
* \brief Discover other applications in a set with a common communicator.
*
* \param[in] sync_flag 1 if application is to be synchronized at each
* time step, 0 if independent from others.
* \param[in] app_type name of current application type (software name).
* \param[in] app_name name of current application (data/case name).
* \param[in] base_comm communicator associated with all applications.
* \param[in] app_comm communicator associated with local application.
*
* \return PLE coupling MPI set info id within the static local array
*/
/*----------------------------------------------------------------------------*/
static PyObject *
pyple_coupling_mpi_set_create(PyObject *self, PyObject *args)
{
int sync_flags = 0;
const char *app_type = "";
const char *app_name = "";
PyObject *py_base_comm = NULL;
PyObject *py_app_comm = NULL;
MPI_Comm *base_comm_p = NULL;
MPI_Comm *app_comm_p = NULL;
/* We verify that the correct arguments are provided.
* format is of the form "var1_typevar2_type..varn_type:function_name"
* function_name is used so that in case of a traceback error Python will
* know it failed here.
* If a CPython function returns NULL, it means it failed.
*/
if (!PyArg_ParseTuple(args, "issOO:ple_coupling_mpi_set_create",
&sync_flags,
&app_type,
&app_name,
&py_base_comm,
&py_app_comm))
return NULL;
/* Retrieve the C MPI_Comm from the mpi4py.Comm */
base_comm_p = PyMPIComm_Get(py_base_comm);
app_comm_p = PyMPIComm_Get(py_app_comm);
if (base_comm_p == NULL || app_comm_p == NULL)
return NULL;
/* Call the C function */
if (_n_sets == 0)
PLE_MALLOC(_mpi_sets, 1, ple_coupling_mpi_set_t *);
else
PLE_REALLOC(_mpi_sets, _n_sets + 1, ple_coupling_mpi_set_t *);
_mpi_sets[_n_sets] = ple_coupling_mpi_set_create(sync_flags,
app_type,
app_name,
*base_comm_p,
*app_comm_p);
_n_sets++;
int ret_index = _n_sets - 1;
return Py_BuildValue("i", ret_index);
}
/*----------------------------------------------------------------------------*/
/*!
* \brief Free an PLE coupling MPI set info structure.
*
* \param[in] index id of the set to free
*/
/*----------------------------------------------------------------------------*/
static PyObject *
pyple_coupling_mpi_set_destroy(PyObject *self, PyObject *args)
{
int set_index = 0;
if (!PyArg_ParseTuple(args, "i:ple_coupling_mpi_set_destroy", &set_index))
return NULL;
ple_coupling_mpi_set_destroy(&_mpi_sets[set_index]);
Py_INCREF(Py_None);
return Py_None;
}
/*----------------------------------------------------------------------------*/
/*!
* \brief Return application information in set's common communicator.
*
* \param[in] index index of the PLE coupling MPI set info structure.
* \param[in] app_id application id
*
* \return application information structure as a dictionnary.
*/
/*----------------------------------------------------------------------------*/
static PyObject *
pyple_coupling_mpi_set_get_info(PyObject *self, PyObject *args)
{
int set_index = 0;
int app_id = 0;
/* We verify that the correct arguments are provided.
* format is of the form "var1_typevar2_type..varn_type:function_name"
* function_name is used so that in case of a traceback error Python will
* know it failed here.
* If a CPython function returns NULL, it means it failed.
*/
if (!PyArg_ParseTuple(args, "ii:ple_coupling_mpi_set_get_info",
&set_index, &app_id))
return NULL;
/* Call the C function */
ple_coupling_mpi_set_info_t ai =
ple_coupling_mpi_set_get_info(_mpi_sets[set_index], app_id);
/* Return a Python dictionary */
return Py_BuildValue("{sisisissss}",
"status", ai.status,
"root_rank", ai.root_rank,
"n_ranks", ai.n_ranks,
"app_type", ai.app_type,
"app_name", ai.app_name);
}
/*----------------------------------------------------------------------------*/
/*!
* \brief Return the number of applications in a coupled set.
*
* \param[in] index index of the PLE coupling MPI set info structure.
*
* \return number of application in set's common communicator.
*/
/*----------------------------------------------------------------------------*/
static PyObject *
pyple_coupling_mpi_set_n_apps(PyObject *self, PyObject *args)
{
int set_index = 0;
/* We verify that the correct arguments are provided.
* format is of the form "var1_typevar2_type..varn_type:function_name"
* function_name is used so that in case of a traceback error Python will
* know it failed here.
* If a CPython function returns NULL, it means it failed.
*/
if (!PyArg_ParseTuple(args, "i:ple_coupling_mpi_set_n_apps", &set_index))
return NULL;
/* Call the C function */
int n_apps = ple_coupling_mpi_set_n_apps(_mpi_sets[set_index]);
/* Return a Python value */
return Py_BuildValue("i", n_apps);
}
/*----------------------------------------------------------------------------*/
/*!
* \brief Return the id of the local application in a coupled set.
*
* \param[in] index index of the PLE coupling MPI set info structure.
*
* \return id of the local application in set's common communicator.
*/
/*----------------------------------------------------------------------------*/
static PyObject *
pyple_coupling_mpi_set_get_app_id(PyObject *self, PyObject *args)
{
int set_index = 0;
/* We verify that the correct arguments are provided.
* format is of the form "var1_typevar2_type..varn_type:function_name"
* function_name is used so that in case of a traceback error Python will
* know it failed here.
* If a CPython function returns NULL, it means it failed.
*/
if (!PyArg_ParseTuple(args, "i:ple_coupling_mpi_set_get_app_id", &set_index))
return NULL;
/* Call the C function */
int app_id = ple_coupling_mpi_set_get_app_id(_mpi_sets[set_index]);
/* Return a Python value */
return Py_BuildValue("i", app_id);
}
/*----------------------------------------------------------------------------*/
/*!
* \brief Synchronize applications in a set.
*
* \param[in] index index of the PLE coupling MPI set info structure.
* \param[in] sync_flags app sync flags
* \param[in] time_step app time step
*/
/*----------------------------------------------------------------------------*/
static PyObject *
pyple_coupling_mpi_set_synchronize(PyObject *self, PyObject *args)
{
int set_index = 0;
int sync_flag = 0;
double time_step = 0.;
/* We verify that the correct arguments are provided.
* format is of the form "var1_typevar2_type..varn_type:function_name"
* function_name is used so that in case of a traceback error Python will
* know it failed here.
* If a CPython function returns NULL, it means it failed.
*/
if (!PyArg_ParseTuple(args, "iid:ple_coupling_mpi_set_synchronize",
&set_index, &sync_flag, &time_step))
return NULL;
/* Call the C function */
ple_coupling_mpi_set_synchronize(_mpi_sets[set_index], sync_flag, time_step);
/* Return a Python value */
Py_INCREF(Py_None);
return Py_None;
}
/*----------------------------------------------------------------------------*/
/*!
* \brief Get status of applications in a set.
*
* \param[in] index index of the PLE coupling MPI set info structure.
*
* \return a python list of status flags
*/
/*----------------------------------------------------------------------------*/
static PyObject *
pyple_coupling_mpi_set_get_status(PyObject *self, PyObject *args)
{
int set_index = 0;
/* We verify that the correct arguments are provided.
* format is of the form "var1_typevar2_type..varn_type:function_name"
* function_name is used so that in case of a traceback error Python will
* know it failed here.
* If a CPython function returns NULL, it means it failed.
*/
if (!PyArg_ParseTuple(args, "i:ple_coupling_mpi_set_get_status", &set_index))
return NULL;
/* Call the C function */
const int *set_status = ple_coupling_mpi_set_get_status(_mpi_sets[set_index]);
const int n_apps = ple_coupling_mpi_set_n_apps(_mpi_sets[set_index]);
PyObject *status_list = PyList_New(n_apps);
for (int i = 0; i < n_apps; i++)
PyList_SetItem(status_list, i, set_status[i]);
/* Return a Python list */
return status_list;
}
/*----------------------------------------------------------------------------*/
/*!
* \brief Get time steps in a set.
*
* \param[in] index index of the PLE coupling MPI set info structure.
*
* \return a python list of the set's time steps
*/
/*----------------------------------------------------------------------------*/
static PyObject *
pyple_coupling_mpi_set_get_timestep(PyObject *self, PyObject *args)
{
int set_index = 0;
/* We verify that the correct arguments are provided.
* format is of the form "var1_typevar2_type..varn_type:function_name"
* function_name is used so that in case of a traceback error Python will
* know it failed here.
* If a CPython function returns NULL, it means it failed.
*/
if (!PyArg_ParseTuple(args, "i:ple_coupling_mpi_set_get_timestep", &set_index))
return NULL;
/* Call the C function */
const double *set_dt = ple_coupling_mpi_set_get_timestep(_mpi_sets[set_index]);
const int n_apps = ple_coupling_mpi_set_n_apps(_mpi_sets[set_index]);
PyObject *dt_list = PyList_New(n_apps);
for (int i = 0; i < n_apps; i++)
PyList_SetItem(dt_list, i, PyFloat_FromDouble(set_dt[i]));
/* Return a Python list */
return dt_list;
}
/*----------------------------------------------------------------------------*/
/*!
* \brief Dump printout of an PLE coupling MPI set info structure.
*
* \param[in] index index of the PLE coupling MPI set info structure.
*/
/*----------------------------------------------------------------------------*/
static PyObject *
pyple_coupling_mpi_set_dump(PyObject *self, PyObject *args)
{
int set_index = 0;
/* We verify that the correct arguments are provided.
* format is of the form "var1_typevar2_type..varn_type:function_name"
* function_name is used so that in case of a traceback error Python will
* know it failed here.
* If a CPython function returns NULL, it means it failed.
*/
if (!PyArg_ParseTuple(args, "i:ple_coupling_mpi_set_dump", &set_index))
return NULL;
/* Call the C function */
ple_coupling_mpi_set_dump(_mpi_sets[set_index]);
/* Return a Python value */
Py_INCREF(Py_None);
return Py_None;
}
/*----------------------------------------------------------------------------*/
/*!
* \brief Create an intracommunicator from a local and distant communicator
* within a base communicator.
*
* \param[in] base_comm communicator associated with both applications
* \param[in] app_comm communicator associated with local application
* \param[in] distant_root rank of distant group leader in base_comm
* \param[in] new_comm pointer to new communicator
* \param[in] local_range first and past-the last ranks of local application
* in new communicator
* \param[in] distant_range first and past-the last ranks of distant
* application in new communicator
*/
/*----------------------------------------------------------------------------*/
static PyObject *
pyple_coupling_mpi_intracomm_create(PyObject *self, PyObject *args)
{
PyObject *py_base_comm = NULL;
PyObject *py_app_comm = NULL;
PyObject *py_new_comm = NULL;
int distant_root = 0;
PyObject *llist = NULL;
PyObject *dlist = NULL;
/* We verify that the correct arguments are provided.
* format is of the form "var1_typevar2_type..varn_type:function_name"
* function_name is used so that in case of a traceback error Python will
* know it failed here.
* If a CPython function returns NULL, it means it failed.
*/
if (!PyArg_ParseTuple(args, "OOiOO!O!:ple_coupling_mpi_intracomm_create",
&py_base_comm, &py_app_comm,
&distant_root, &py_new_comm,
&PyList_Type, &llist,
&PyList_Type, &dlist))
return NULL;
MPI_Comm *base_comm_p = NULL;
MPI_Comm *app_comm_p = NULL;
MPI_Comm *new_comm_p = NULL;
base_comm_p = PyMPIComm_Get(py_base_comm);
app_comm_p = PyMPIComm_Get(py_app_comm);
new_comm_p = PyMPIComm_Get(py_new_comm);
int lranks[2] = {-1, -1};
int dranks[2] = {-1, -1};
/* Call the C function */
ple_coupling_mpi_intracomm_create(*base_comm_p,
*app_comm_p,
distant_root,
new_comm_p,
lranks,
dranks);
for (int i = 0; i < 2; i++) {
PyList_SetItem(llist, i, Py_BuildValue("i",lranks[i]));
PyList_SetItem(dlist, i, Py_BuildValue("i",dranks[i]));
}
/* Return a Python value */
Py_INCREF(Py_None);
return Py_None;
}
/*----------------------------------------------------------------------------*/
/*!
* \brief Make the PLE_COUPLING_* flags available in python
*/
/*----------------------------------------------------------------------------*/
static PyObject *
pyple_coupling_get_masks(PyObject *self, PyObject *args)
{
/* Return a Python dictionary */
return Py_BuildValue("{sisisisisisisisisisisisisisisi}",
"INIT", PLE_COUPLING_INIT,
"NO_SYNC", PLE_COUPLING_NO_SYNC,
"STOP", PLE_COUPLING_STOP,
"LAST", PLE_COUPLING_LAST,
"NEW_ITERATION", PLE_COUPLING_NEW_ITERATION,
"REDO_ITERATION", PLE_COUPLING_REDO_ITERATION,
"TS_MIN", PLE_COUPLING_TS_MIN,
"TS_LEADER", PLE_COUPLING_TS_LEADER,
"UNSTEADY", PLE_COUPLING_UNSTEADY,
"STEADY", PLE_COUPLING_STEADY,
"CONVERGED", PLE_COUPLING_CONVERGED,
"USER_1", PLE_COUPLING_USER_1,
"USER_2", PLE_COUPLING_USER_2,
"USER_3", PLE_COUPLING_USER_3,
"USER_4", PLE_COUPLING_USER_4);
}
/*----------------------------------------------------------------------------*/
/*!
* \brief Free all the ple_coupling_mpi_set_t pointers created.
*/
/*----------------------------------------------------------------------------*/
static PyObject *
pyple_coupling_mpi_set_destroy_all(PyObject *self, PyObject *args)
{
for (int i = 0; i < _n_sets; i++)
ple_coupling_mpi_set_destroy(&_mpi_sets[i]);
PLE_FREE(_mpi_sets);
/* Return a Python value */
Py_INCREF(Py_None);
return Py_None;
}
/*============================================================================
* Define the list of methods available for Python.
* Two first arguments are the function's Python name and a pointer to
* the CPython function which will be called.
*============================================================================*/
static struct PyMethodDef
pyple_coupling_methods[] = {
{"coupling_mpi_name_to_id",
(PyCFunction)pyple_coupling_mpi_name_to_id,
METH_VARARGS,
NULL},
{"coupling_mpi_set_create",
(PyCFunction)pyple_coupling_mpi_set_create,
METH_VARARGS,
NULL},
{"coupling_mpi_set_destroy",
(PyCFunction)pyple_coupling_mpi_set_destroy,
METH_VARARGS,
NULL},
{"coupling_mpi_set_get_info",
(PyCFunction)pyple_coupling_mpi_set_get_info,
METH_VARARGS,
NULL},
{"coupling_mpi_set_n_apps",
(PyCFunction)pyple_coupling_mpi_set_n_apps,
METH_VARARGS,
NULL},
{"coupling_mpi_set_get_app_id",
(PyCFunction)pyple_coupling_mpi_set_get_app_id,
METH_VARARGS,
NULL},
{"coupling_mpi_set_synchronize",
(PyCFunction)pyple_coupling_mpi_set_synchronize,
METH_VARARGS,
NULL},
{"coupling_mpi_set_get_status",
(PyCFunction)pyple_coupling_mpi_set_get_status,
METH_VARARGS,
NULL},
{"coupling_mpi_set_get_timestep",
(PyCFunction)pyple_coupling_mpi_set_get_timestep,
METH_VARARGS,
NULL},
{"coupling_mpi_set_dump",
(PyCFunction)pyple_coupling_mpi_set_dump,
METH_VARARGS,
NULL},
{"coupling_mpi_intracomm_create",
(PyCFunction)pyple_coupling_mpi_intracomm_create,
METH_VARARGS,
NULL},
{"coupling_get_masks",
(PyCFunction)pyple_coupling_get_masks,
METH_VARARGS,
NULL},
{"coupling_mpi_set_destroy_all",
(PyCFunction)pyple_coupling_mpi_set_destroy_all,
METH_VARARGS,
NULL},
{NULL, NULL, 0, NULL}
};
/*============================================================================
* Define the python module specific functions needed for it to be imported
*============================================================================*/
#if PY_MAJOR_VERSION < 3
/* --- Python 2 --- */
PyMODINIT_FUNC initlibpyplecoupling(void)
{
PyObject *m = NULL;
/* Initialize mpi4py C-API */
if (import_mpi4py() < 0) goto bad;
/* Module initialization */
m = Py_InitModule("libpyplecoupling", pyple_coupling_methods);
if (m == NULL) goto bad;
return;
bad:
return;
}
#else
/* --- Python 3 --- */
static struct PyModuleDef libpyplecoupling_module = {
PyModuleDef_HEAD_INIT,
"libpyplecoupling", /* m_name */
NULL, /* m_doc */
-1, /* m_size */
pyple_coupling_methods /* m_methods */,
NULL, /* m_reload */
NULL, /* m_traverse */
NULL, /* m_clear */
NULL /* m_free */
};
PyMODINIT_FUNC
PyInit_libpyplecoupling(void)
{
PyObject *m = NULL;
/* Initialize mpi4py's C-API */
if (import_mpi4py() < 0) goto bad;
/* Module initialization */
m = PyModule_Create(&libpyplecoupling_module);
if (m == NULL) goto bad;
return m;
bad:
return NULL;
}
#endif
/*----------------------------------------------------------------------------*/
#ifdef __cplusplus
}
#endif /* __cplusplus */
|