| 1 |
/* |
| 2 |
* This file is part of MUSIC. |
| 3 |
* Copyright (C) 2008, 2009 INCF |
| 4 |
* |
| 5 |
* MUSIC is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* MUSIC is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
#include "music.hh" |
| 19 |
#if MUSIC_USE_MPI |
| 20 |
|
| 21 |
|
| 22 |
#include <string> |
| 23 |
#include <cstring> |
| 24 |
|
| 25 |
extern "C" { |
| 26 |
|
| 27 |
#include "music-c.h" |
| 28 |
|
| 29 |
/* Setup */ |
| 30 |
|
| 31 |
MUSIC_Setup * |
| 32 |
MUSIC_createSetup (int *argc, char ***argv) |
| 33 |
{ |
| 34 |
return (MUSIC_Setup *) new MUSIC::Setup (*argc, *argv); |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
MUSIC_Setup * |
| 39 |
MUSIC_createSetupThread (int *argc, char ***argv, int required, int *provided) |
| 40 |
{ |
| 41 |
return (MUSIC_Setup *) new MUSIC::Setup (*argc, *argv, required, provided); |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
/* Communicators */ |
| 46 |
|
| 47 |
MPI_Comm |
| 48 |
MUSIC_setupCommunicatorGlue (MUSIC_Setup *setup) |
| 49 |
{ |
| 50 |
MUSIC::Setup* cxxSetup = (MUSIC::Setup *) setup; |
| 51 |
return (MPI_Comm) cxxSetup->communicator (); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
/* Port creation */ |
| 56 |
|
| 57 |
MUSIC_ContOutputPort * |
| 58 |
MUSIC_publishContOutput (MUSIC_Setup *setup, char *id) |
| 59 |
{ |
| 60 |
MUSIC::Setup* cxxSetup = (MUSIC::Setup *) setup; |
| 61 |
return (MUSIC_ContOutputPort *) cxxSetup->publishContOutput (id); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
MUSIC_ContInputPort * |
| 66 |
MUSIC_publishContInput (MUSIC_Setup *setup, char *id) |
| 67 |
{ |
| 68 |
MUSIC::Setup* cxxSetup = (MUSIC::Setup *) setup; |
| 69 |
return (MUSIC_ContInputPort *) cxxSetup->publishContInput(id); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
MUSIC_EventOutputPort * |
| 74 |
MUSIC_publishEventOutput (MUSIC_Setup *setup, char *id) |
| 75 |
{ |
| 76 |
MUSIC::Setup* cxxSetup = (MUSIC::Setup *) setup; |
| 77 |
return (MUSIC_EventOutputPort *) cxxSetup->publishEventOutput(id); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
MUSIC_EventInputPort * |
| 82 |
MUSIC_publishEventInput (MUSIC_Setup *setup, char *id) |
| 83 |
{ |
| 84 |
MUSIC::Setup* cxxSetup = (MUSIC::Setup *) setup; |
| 85 |
return (MUSIC_EventInputPort *) cxxSetup->publishEventInput(id); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
MUSIC_MessageOutputPort * |
| 90 |
MUSIC_publishMessageOutput (MUSIC_Setup *setup, char *id) |
| 91 |
{ |
| 92 |
MUSIC::Setup* cxxSetup = (MUSIC::Setup *) setup; |
| 93 |
return (MUSIC_MessageOutputPort *) cxxSetup->publishMessageOutput(id); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
MUSIC_MessageInputPort * |
| 98 |
MUSIC_publishMessageInput (MUSIC_Setup *setup, char *id) |
| 99 |
{ |
| 100 |
MUSIC::Setup* cxxSetup = (MUSIC::Setup *) setup; |
| 101 |
return (MUSIC_MessageInputPort *) cxxSetup->publishMessageInput(id); |
| 102 |
} |
| 103 |
|
| 104 |
/* |
| 105 |
void |
| 106 |
MUSIC_destroyContOutput (MUSIC_ContOutputPort* Port) |
| 107 |
{ |
| 108 |
delete (MUSIC::ContOutputPort *) Port; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
void |
| 113 |
MUSIC_destroyContInput (MUSIC_ContInputPort* Port) |
| 114 |
{ |
| 115 |
delete (MUSIC::ContInputPort *) Port; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
void |
| 120 |
MUSIC_destroyEventOutput (MUSIC_EventOutputPort* Port) |
| 121 |
{ |
| 122 |
delete (MUSIC::EventOutputPort *) Port; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
void |
| 127 |
MUSIC_destroyEventInput (MUSIC_EventInputPort* Port) |
| 128 |
{ |
| 129 |
delete (MUSIC::EventInputPort *) Port; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
void |
| 134 |
MUSIC_destroyMessageOutput (MUSIC_MessageOutputPort* Port) |
| 135 |
{ |
| 136 |
delete (MUSIC::MessageOutputPort *) Port; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
void |
| 141 |
MUSIC_destroyMessageInput (MUSIC_MessageInputPort* Port) |
| 142 |
{ |
| 143 |
delete (MUSIC::MessageInputPort *) Port; |
| 144 |
} |
| 145 |
*/ |
| 146 |
|
| 147 |
/* General port methods */ |
| 148 |
|
| 149 |
int |
| 150 |
MUSIC_ContOutputPort_isConnected (MUSIC_ContOutputPort *Port) |
| 151 |
{ |
| 152 |
MUSIC::ContOutputPort* cxxPort = (MUSIC::ContOutputPort *) Port; |
| 153 |
return cxxPort->isConnected (); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
int |
| 158 |
MUSIC_ContInputPort_isConnected (MUSIC_ContInputPort *Port) |
| 159 |
{ |
| 160 |
MUSIC::ContInputPort* cxxPort = (MUSIC::ContInputPort *) Port; |
| 161 |
return cxxPort->isConnected (); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
int |
| 166 |
MUSIC_EventOutputPort_isConnected (MUSIC_EventOutputPort *Port) |
| 167 |
{ |
| 168 |
MUSIC::EventOutputPort* cxxPort = (MUSIC::EventOutputPort *) Port; |
| 169 |
return cxxPort->isConnected (); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
int |
| 174 |
MUSIC_EventInputPort_isConnected (MUSIC_EventInputPort *Port) |
| 175 |
{ |
| 176 |
MUSIC::EventInputPort* cxxPort = (MUSIC::EventInputPort *) Port; |
| 177 |
return cxxPort->isConnected (); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
int |
| 182 |
MUSIC_MessageOutputPort_isConnected (MUSIC_MessageOutputPort *Port) |
| 183 |
{ |
| 184 |
MUSIC::MessageOutputPort* cxxPort = (MUSIC::MessageOutputPort *) Port; |
| 185 |
return cxxPort->isConnected (); |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
int |
| 190 |
MUSIC_MessageInputPort_isConnected (MUSIC_MessageInputPort *Port) |
| 191 |
{ |
| 192 |
MUSIC::MessageInputPort* cxxPort = (MUSIC::MessageInputPort *) Port; |
| 193 |
return cxxPort->isConnected (); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
int |
| 198 |
MUSIC_ContOutputPort_hasWidth (MUSIC_ContOutputPort *Port) |
| 199 |
{ |
| 200 |
MUSIC::ContOutputPort* cxxPort = (MUSIC::ContOutputPort *) Port; |
| 201 |
return cxxPort->hasWidth (); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
int |
| 206 |
MUSIC_ContInputPort_hasWidth (MUSIC_ContInputPort *Port) |
| 207 |
{ |
| 208 |
MUSIC::ContInputPort* cxxPort = (MUSIC::ContInputPort *) Port; |
| 209 |
return cxxPort->hasWidth (); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
int |
| 214 |
MUSIC_EventOutputPort_hasWidth (MUSIC_EventOutputPort *Port) |
| 215 |
{ |
| 216 |
MUSIC::EventOutputPort* cxxPort = (MUSIC::EventOutputPort *) Port; |
| 217 |
return cxxPort->hasWidth (); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
int |
| 222 |
MUSIC_EventInputPort_hasWidth (MUSIC_EventInputPort *Port) |
| 223 |
{ |
| 224 |
MUSIC::EventInputPort* cxxPort = (MUSIC::EventInputPort *) Port; |
| 225 |
return cxxPort->hasWidth (); |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
int |
| 230 |
MUSIC_ContOutputPort_width (MUSIC_ContOutputPort *Port) |
| 231 |
{ |
| 232 |
MUSIC::ContOutputPort* cxxPort = (MUSIC::ContOutputPort *) Port; |
| 233 |
return cxxPort->width (); |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
int |
| 238 |
MUSIC_ContInputPort_width (MUSIC_ContInputPort *Port) |
| 239 |
{ |
| 240 |
MUSIC::ContInputPort* cxxPort = (MUSIC::ContInputPort *) Port; |
| 241 |
return cxxPort->width (); |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
int |
| 246 |
MUSIC_EventOutputPort_width (MUSIC_EventOutputPort *Port) |
| 247 |
{ |
| 248 |
MUSIC::EventOutputPort* cxxPort = (MUSIC::EventOutputPort *) Port; |
| 249 |
return cxxPort->width (); |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
int |
| 254 |
MUSIC_EventInputPort_width (MUSIC_EventInputPort *Port) |
| 255 |
{ |
| 256 |
MUSIC::EventInputPort* cxxPort = (MUSIC::EventInputPort *) Port; |
| 257 |
return cxxPort->width (); |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
/* Mapping */ |
| 262 |
|
| 263 |
/* No arguments are optional. */ |
| 264 |
|
| 265 |
void |
| 266 |
MUSIC_ContOutputPort_map (MUSIC_ContOutputPort *Port, |
| 267 |
MUSIC_ContData *dmap, |
| 268 |
int maxBuffered) |
| 269 |
{ |
| 270 |
MUSIC::ContOutputPort* cxxPort = (MUSIC::ContOutputPort *) Port; |
| 271 |
MUSIC::ContData* cxxDmap = (MUSIC::ContData *) dmap; |
| 272 |
cxxPort->map (cxxDmap, maxBuffered); |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
void |
| 277 |
MUSIC_ContInputPort_map (MUSIC_ContInputPort *Port, |
| 278 |
MUSIC_ContData *dmap, |
| 279 |
double delay, |
| 280 |
int maxBuffered, |
| 281 |
int interpolate) |
| 282 |
{ |
| 283 |
MUSIC::ContInputPort* cxxPort = (MUSIC::ContInputPort *) Port; |
| 284 |
MUSIC::ContData* cxxDmap = (MUSIC::ContData *) dmap; |
| 285 |
cxxPort->map (cxxDmap, delay, maxBuffered, interpolate); |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
void |
| 290 |
MUSIC_EventOutputPort_mapGlobalIndex (MUSIC_EventOutputPort *Port, |
| 291 |
MUSIC_IndexMap *indices, |
| 292 |
int maxBuffered) |
| 293 |
{ |
| 294 |
MUSIC::EventOutputPort* cxxPort = (MUSIC::EventOutputPort *) Port; |
| 295 |
MUSIC::IndexMap* cxxIndices = (MUSIC::IndexMap *) indices; |
| 296 |
cxxPort->map (cxxIndices, MUSIC::Index::GLOBAL, maxBuffered); |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
void |
| 301 |
MUSIC_EventOutputPort_mapLocalIndex (MUSIC_EventOutputPort *Port, |
| 302 |
MUSIC_IndexMap *indices, |
| 303 |
int maxBuffered) |
| 304 |
{ |
| 305 |
MUSIC::EventOutputPort* cxxPort = (MUSIC::EventOutputPort *) Port; |
| 306 |
MUSIC::IndexMap* cxxIndices = (MUSIC::IndexMap *) indices; |
| 307 |
cxxPort->map (cxxIndices, MUSIC::Index::LOCAL, maxBuffered); |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
typedef void MUSIC_EventHandler (double t, int id); |
| 312 |
|
| 313 |
void |
| 314 |
MUSIC_EventInputPort_mapGlobalIndex (MUSIC_EventInputPort *Port, |
| 315 |
MUSIC_IndexMap *indices, |
| 316 |
MUSIC_EventHandler *handleEvent, |
| 317 |
double accLatency, |
| 318 |
int maxBuffered) |
| 319 |
{ |
| 320 |
MUSIC::EventInputPort* cxxPort = (MUSIC::EventInputPort *) Port; |
| 321 |
MUSIC::IndexMap* cxxIndices = (MUSIC::IndexMap *) indices; |
| 322 |
MUSIC::EventHandlerGlobalIndexProxy* cxxHandleEvent = |
| 323 |
cxxPort->allocEventHandlerGlobalIndexProxy (handleEvent); |
| 324 |
cxxPort->map (cxxIndices, cxxHandleEvent, accLatency, maxBuffered); |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
void |
| 329 |
MUSIC_EventInputPort_mapLocalIndex (MUSIC_EventInputPort *Port, |
| 330 |
MUSIC_IndexMap *indices, |
| 331 |
MUSIC_EventHandler *handleEvent, |
| 332 |
double accLatency, |
| 333 |
int maxBuffered) |
| 334 |
{ |
| 335 |
MUSIC::EventInputPort* cxxPort = (MUSIC::EventInputPort *) Port; |
| 336 |
MUSIC::IndexMap* cxxIndices = (MUSIC::IndexMap *) indices; |
| 337 |
MUSIC::EventHandlerLocalIndexProxy* cxxHandleEvent = |
| 338 |
cxxPort->allocEventHandlerLocalIndexProxy (handleEvent); |
| 339 |
cxxPort->map (cxxIndices, cxxHandleEvent, accLatency, maxBuffered); |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
void |
| 344 |
MUSIC_MessageOutputPort_map_no_handler (MUSIC_MessageOutputPort *Port) |
| 345 |
{ |
| 346 |
MUSIC::MessageOutputPort* cxxPort = (MUSIC::MessageOutputPort *) Port; |
| 347 |
cxxPort->map (); |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
void |
| 352 |
MUSIC_MessageOutputPort_map (MUSIC_MessageOutputPort *Port, |
| 353 |
int maxBuffered) |
| 354 |
{ |
| 355 |
MUSIC::MessageOutputPort* cxxPort = (MUSIC::MessageOutputPort *) Port; |
| 356 |
cxxPort->map (maxBuffered); |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
typedef void MUSIC_MessageHandler (double t, void *msg, size_t size); |
| 361 |
|
| 362 |
void |
| 363 |
MUSIC_MessageInputPort_map (MUSIC_MessageInputPort *Port, |
| 364 |
MUSIC_MessageHandler *handleMessage, |
| 365 |
double accLatency, |
| 366 |
int maxBuffered) |
| 367 |
{ |
| 368 |
MUSIC::MessageInputPort* cxxPort = (MUSIC::MessageInputPort *) Port; |
| 369 |
MUSIC::MessageHandlerProxy* cxxHandleMessage = |
| 370 |
cxxPort->allocMessageHandlerProxy (handleMessage); |
| 371 |
cxxPort->map (cxxHandleMessage, accLatency, maxBuffered); |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
/* Index maps */ |
| 376 |
|
| 377 |
MUSIC_PermutationIndex * |
| 378 |
MUSIC_createPermutationIndex (int *indices, |
| 379 |
int size) |
| 380 |
{ |
| 381 |
void* data = static_cast<void*> (indices); |
| 382 |
return (MUSIC_PermutationIndex *) |
| 383 |
new MUSIC::PermutationIndex (static_cast<MUSIC::GlobalIndex*> (data), |
| 384 |
size); |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
void |
| 389 |
MUSIC_destroyPermutationIndex (MUSIC_PermutationIndex *Index) |
| 390 |
{ |
| 391 |
delete (MUSIC::PermutationIndex *) Index; |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
MUSIC_LinearIndex * |
| 396 |
MUSIC_createLinearIndex (int baseIndex, |
| 397 |
int size) |
| 398 |
{ |
| 399 |
return (MUSIC_LinearIndex *) new MUSIC::LinearIndex (baseIndex, size); |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
void |
| 404 |
MUSIC_destroyLinearIndex (MUSIC_LinearIndex *Index) |
| 405 |
{ |
| 406 |
delete (MUSIC::LinearIndex *) Index; |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
/* Data maps */ |
| 411 |
|
| 412 |
/* Exception: The map argument can take any type of index map. */ |
| 413 |
|
| 414 |
MUSIC_ArrayData * |
| 415 |
MUSIC_createArrayData (void *buffer, |
| 416 |
MPI_Datatype type, |
| 417 |
void *map) |
| 418 |
{ |
| 419 |
MUSIC::IndexMap* cxxMap = (MUSIC::IndexMap *) map; |
| 420 |
return (MUSIC_ArrayData *) new MUSIC::ArrayData (buffer, type, cxxMap); |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
/* Exception: MUSIC_createLinearArrayData corresponds to |
| 425 |
c++ MUSIC::ArrayData::ArrayData (..., ..., ..., ...) */ |
| 426 |
|
| 427 |
MUSIC_ArrayData * |
| 428 |
MUSIC_createLinearArrayData (void *buffer, |
| 429 |
MPI_Datatype type, |
| 430 |
int baseIndex, |
| 431 |
int size) |
| 432 |
{ |
| 433 |
return (MUSIC_ArrayData *) new MUSIC::ArrayData (buffer, |
| 434 |
type, |
| 435 |
baseIndex, |
| 436 |
size); |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
void |
| 441 |
MUSIC_destroyArrayData (MUSIC_ArrayData *ArrayData) |
| 442 |
{ |
| 443 |
delete (MUSIC::ArrayData *) ArrayData; |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
/* Configuration variables */ |
| 448 |
|
| 449 |
/* Exceptions: Result is char * |
| 450 |
Extra maxlen argument prevents buffer overflow. |
| 451 |
Result is terminated by \0 unless longer than maxlen - 1 */ |
| 452 |
|
| 453 |
int |
| 454 |
MUSIC_configString (MUSIC_Setup *setup, |
| 455 |
char *name, |
| 456 |
char *result, |
| 457 |
size_t maxlen) |
| 458 |
{ |
| 459 |
MUSIC::Setup* cxxSetup = (MUSIC::Setup *) setup; |
| 460 |
std::string cxxResult; |
| 461 |
int res = cxxSetup->config (string (name), &cxxResult); |
| 462 |
strncpy(result, cxxResult.c_str (), maxlen); |
| 463 |
return res; |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
int |
| 468 |
MUSIC_configInt (MUSIC_Setup *setup, char *name, int *result) |
| 469 |
{ |
| 470 |
MUSIC::Setup* cxxSetup = (MUSIC::Setup *) setup; |
| 471 |
return cxxSetup->config (string(name), result); |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
int |
| 476 |
MUSIC_configDouble (MUSIC_Setup *setup, char *name, double *result) |
| 477 |
{ |
| 478 |
MUSIC::Setup* cxxSetup = (MUSIC::Setup *) setup; |
| 479 |
return cxxSetup->config (string(name), result); |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
/* Runtime */ |
| 484 |
|
| 485 |
MUSIC_Runtime * |
| 486 |
MUSIC_createRuntime (MUSIC_Setup *setup, double h) |
| 487 |
{ |
| 488 |
MUSIC::Setup* cxxSetup = (MUSIC::Setup *) setup; |
| 489 |
return (MUSIC_Runtime *) new MUSIC::Runtime (cxxSetup, h); |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
void |
| 494 |
MUSIC_tick (MUSIC_Runtime *runtime) |
| 495 |
{ |
| 496 |
MUSIC::Runtime* cxxRuntime = (MUSIC::Runtime *) runtime; |
| 497 |
cxxRuntime->tick (); |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
double |
| 502 |
MUSIC_time (MUSIC_Runtime *runtime) |
| 503 |
{ |
| 504 |
MUSIC::Runtime* cxxRuntime = (MUSIC::Runtime *) runtime; |
| 505 |
return cxxRuntime->time (); |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
/* Finalization */ |
| 510 |
|
| 511 |
void |
| 512 |
MUSIC_destroyRuntime (MUSIC_Runtime *runtime) |
| 513 |
{ |
| 514 |
MUSIC::Runtime* cxxRuntime = (MUSIC::Runtime *) runtime; |
| 515 |
delete cxxRuntime; |
| 516 |
} |
| 517 |
|
| 518 |
} |
| 519 |
#endif |