| 1 |
// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root |
| 2 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 4 |
// vi: set et ts=4 sw=2 sts=2: |
| 5 |
/****************************************************************************/ |
| 6 |
/* */ |
| 7 |
/* File: mpi-ppif.c */ |
| 8 |
/* */ |
| 9 |
/* Purpose: parallel processor interface */ |
| 10 |
/* Provides a portable interface to message passing MIMD */ |
| 11 |
/* architectures. PPIF is divided into three parts: */ |
| 12 |
/* */ |
| 13 |
/* (1) Administration */ |
| 14 |
/* (2) Communication */ |
| 15 |
/* (3) Miscellaneous */ |
| 16 |
/* */ |
| 17 |
/* The interface assumes that the parallel machine has */ |
| 18 |
/* the following properties: */ |
| 19 |
/* */ |
| 20 |
/* (1) it is physically connected at least as a 2 or 3 dim. array*/ |
| 21 |
/* (2) it has a fast virtual channel communication mechanism */ |
| 22 |
/* (3) it has an asynchronous communication mechanism */ |
| 23 |
/* */ |
| 24 |
/* MPI Implementation */ |
| 25 |
/* */ |
| 26 |
/* Author: Jens Boenisch */ |
| 27 |
/* Rechenzentrum Uni Stuttgart */ |
| 28 |
/* Universitaet Stuttgart */ |
| 29 |
/* Allmandring 3a */ |
| 30 |
/* 7000 Stuttgart 80 */ |
| 31 |
/* internet: [email protected] */ |
| 32 |
/* */ |
| 33 |
/* History: 17 Aug 1992, begin */ |
| 34 |
/* 18 Feb 1993, Indigo version */ |
| 35 |
/* 02 Jun 1993, Paragon OSF/1 version */ |
| 36 |
/* 14 Sep 1995, MPI version */ |
| 37 |
/* 29 Jan 2003, pV3 concentrator support */ |
| 38 |
/* */ |
| 39 |
/* Remarks: */ |
| 40 |
/* */ |
| 41 |
/****************************************************************************/ |
| 42 |
|
| 43 |
/****************************************************************************/ |
| 44 |
/* */ |
| 45 |
/* include files */ |
| 46 |
/* system include files */ |
| 47 |
/* application include files */ |
| 48 |
/* */ |
| 49 |
/****************************************************************************/ |
| 50 |
|
| 51 |
/* standard C library */ |
| 52 |
#include <config.h> |
| 53 |
#include <sys/types.h> |
| 54 |
#include <cstdio> |
| 55 |
#include <cstdlib> |
| 56 |
#include <ctime> |
| 57 |
#include <cmath> |
| 58 |
|
| 59 |
#include <mpi.h> |
| 60 |
|
| 61 |
#include "ppif.h" |
| 62 |
#include "ppifcontext.hh" |
| 63 |
|
| 64 |
using namespace PPIF; |
| 65 |
|
| 66 |
/****************************************************************************/ |
| 67 |
/* */ |
| 68 |
/* defines in the following order */ |
| 69 |
/* */ |
| 70 |
/* compile time constants defining static data size (i.e. arrays) */ |
| 71 |
/* other constants */ |
| 72 |
/* macros */ |
| 73 |
/* */ |
| 74 |
/****************************************************************************/ |
| 75 |
|
| 76 |
#define MAXT 15 /* maximum number of downtree nodes max */ |
| 77 |
/* log2(P) */ |
| 78 |
|
| 79 |
#define ID_TREE 101 /* channel id: tree */ |
| 80 |
|
| 81 |
#define PPIF_SUCCESS 0 /* Return value for success */ |
| 82 |
#define PPIF_FAILURE 1 /* Return value for failure */ |
| 83 |
|
| 84 |
/****************************************************************************/ |
| 85 |
/* */ |
| 86 |
/* data structures */ |
| 87 |
/* */ |
| 88 |
/****************************************************************************/ |
| 89 |
|
| 90 |
namespace PPIF { |
| 91 |
|
| 92 |
struct VChannel |
| 93 |
{ |
| 94 |
int p; |
| 95 |
int chanid; |
| 96 |
}; |
| 97 |
|
| 98 |
struct Msg |
| 99 |
{ |
| 100 |
MPI_Request req; |
| 101 |
}; |
| 102 |
|
| 103 |
} /* namespace PPIF */ |
| 104 |
|
| 105 |
/****************************************************************************/ |
| 106 |
/* */ |
| 107 |
/* definition of static variables */ |
| 108 |
/* */ |
| 109 |
/****************************************************************************/ |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
/****************************************************************************/ |
| 114 |
/* */ |
| 115 |
/* definition of exported global variables */ |
| 116 |
/* */ |
| 117 |
/****************************************************************************/ |
| 118 |
|
| 119 |
/* id's */ |
| 120 |
int PPIF::me; /* my processor id */ |
| 121 |
int PPIF::master; /* id of master processor */ |
| 122 |
int PPIF::procs; /* number of processors in the network */ |
| 123 |
|
| 124 |
/****************************************************************************/ |
| 125 |
/* */ |
| 126 |
/* routines for handling virtual channels */ |
| 127 |
/* */ |
| 128 |
/****************************************************************************/ |
| 129 |
|
| 130 |
static VChannelPtr NewVChan (int p, int id) |
| 131 |
|
| 132 |
{ |
| 133 |
VChannelPtr myChan = new PPIF::VChannel; |
| 134 |
|
| 135 |
myChan->p = p; |
| 136 |
myChan->chanid = id; |
| 137 |
|
| 138 |
return myChan; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
static void DeleteVChan (VChannelPtr myChan) |
| 143 |
|
| 144 |
{ |
| 145 |
delete myChan; |
| 146 |
} |
| 147 |
|
| 148 |
static std::shared_ptr<PPIF::PPIFContext> ppifContext_; |
| 149 |
|
| 150 |
void PPIF::ppifContext(const std::shared_ptr<PPIFContext>& context) |
| 151 |
{ |
| 152 |
ppifContext_ = context; |
| 153 |
|
| 154 |
me = context->me(); |
| 155 |
master = context->master(); |
| 156 |
procs = context->procs(); |
| 157 |
} |
| 158 |
|
| 159 |
void PPIF::ppifContext(std::nullptr_t) |
| 160 |
{ |
| 161 |
ppifContext_ = nullptr; |
| 162 |
|
| 163 |
me = 0; |
| 164 |
master = 0; |
| 165 |
procs = 1; |
| 166 |
} |
| 167 |
|
| 168 |
const std::shared_ptr<PPIF::PPIFContext>& PPIF::ppifContext() |
| 169 |
{ |
| 170 |
return ppifContext_; |
| 171 |
} |
| 172 |
|
| 173 |
/****************************************************************************/ |
| 174 |
/* */ |
| 175 |
/* Function: InitPPIF */ |
| 176 |
/* */ |
| 177 |
/* Purpose: initialize parallel processor interface */ |
| 178 |
/* set exported variables, allocate tree communication structure */ |
| 179 |
/* */ |
| 180 |
/* Input: void */ |
| 181 |
/* */ |
| 182 |
/* Output: int 0: ok */ |
| 183 |
/* int!=0: error */ |
| 184 |
/* */ |
| 185 |
/****************************************************************************/ |
| 186 |
|
| 187 |
/* |
| 188 |
Factor N into two integers that are as close together as possible |
| 189 |
*/ |
| 190 |
|
| 191 |
static void Factor (int N, int *pn, int *pm) |
| 192 |
|
| 193 |
{ |
| 194 |
int n = (int)ceil (sqrt ((double) N)); |
| 195 |
int m = (int)floor (sqrt ((double) N)); |
| 196 |
|
| 197 |
while (n*m != N) |
| 198 |
{ |
| 199 |
if (n*m < N) n++; |
| 200 |
else m--; |
| 201 |
} |
| 202 |
|
| 203 |
*pn = n; *pm = m; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
void PPIF::InitPPIF(PPIFContext& context) |
| 208 |
{ |
| 209 |
const auto me = context.me(); |
| 210 |
const auto procs = context.procs(); |
| 211 |
|
| 212 |
context.dims_[2] = 1; |
| 213 |
Factor(procs, &context.dims_[0], &context.dims_[1]); |
| 214 |
|
| 215 |
/* tree configuration */ |
| 216 |
|
| 217 |
auto& degree = context.degree_; |
| 218 |
auto& uptree = context.uptree_; |
| 219 |
auto& downtree = context.downtree_; |
| 220 |
|
| 221 |
degree = 0; |
| 222 |
const int sonl = 2*me + 1; |
| 223 |
const int sonr = 2*me + 2; |
| 224 |
|
| 225 |
if (sonl<procs) |
| 226 |
{ |
| 227 |
degree++; |
| 228 |
if (! downtree[0]) /* InitPPIF is being called for the first time */ |
| 229 |
downtree[0] = NewVChan(sonl,ID_TREE); |
| 230 |
} |
| 231 |
else |
| 232 |
{ |
| 233 |
downtree[0] = NULL; |
| 234 |
} |
| 235 |
|
| 236 |
if (sonr<procs) |
| 237 |
{ |
| 238 |
degree++; |
| 239 |
if (! downtree[1]) /* InitPPIF is being called for the first time */ |
| 240 |
downtree[1] = NewVChan(sonr,ID_TREE); |
| 241 |
} |
| 242 |
else |
| 243 |
{ |
| 244 |
downtree[1] = NULL; |
| 245 |
} |
| 246 |
|
| 247 |
if (me>0) |
| 248 |
{ |
| 249 |
if (! uptree) /* InitPPIF is being called for the first time */ |
| 250 |
uptree = NewVChan((me-1)/2,ID_TREE); |
| 251 |
} |
| 252 |
else |
| 253 |
{ |
| 254 |
uptree = NULL; |
| 255 |
} |
| 256 |
|
| 257 |
auto& slvcnt = context.slvcnt_; |
| 258 |
|
| 259 |
int succ=1; |
| 260 |
for(int i=0; i<degree; i++) |
| 261 |
{ |
| 262 |
MPI_Recv ((void *) &(slvcnt[i]), (int) sizeof(int), MPI_BYTE, |
| 263 |
downtree[i]->p, ID_TREE, context.comm(), MPI_STATUS_IGNORE); |
| 264 |
succ += slvcnt[i]; |
| 265 |
} |
| 266 |
if (me>0) |
| 267 |
{ |
| 268 |
MPI_Send ((void *) &succ, (int) sizeof(int), MPI_BYTE, (int)(me-1)/2, ID_TREE, context.comm()); |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
int PPIF::InitPPIF (int *, char ***) |
| 273 |
{ |
| 274 |
auto context = ppifContext(); |
| 275 |
if (not context) |
| 276 |
context = std::make_shared<PPIFContext>(); |
| 277 |
ppifContext(context); |
| 278 |
|
| 279 |
return PPIF_SUCCESS; |
| 280 |
} |
| 281 |
|
| 282 |
void PPIF::ExitPPIF(PPIF::PPIFContext& context) |
| 283 |
{ |
| 284 |
/* Deallocate tree structure */ |
| 285 |
DeleteVChan(context.uptree_); |
| 286 |
context.uptree_ = nullptr; |
| 287 |
/* I currently think that only the first two entries of downtree can contain |
| 288 |
* valid entries, but I am not entirely sure. */ |
| 289 |
DeleteVChan(context.downtree_[0]); |
| 290 |
DeleteVChan(context.downtree_[1]); |
| 291 |
context.downtree_[0] = context.downtree_[1] = nullptr; |
| 292 |
} |
| 293 |
|
| 294 |
int PPIF::ExitPPIF () |
| 295 |
{ |
| 296 |
if (ppifContext()) { |
| 297 |
ppifContext(nullptr); |
| 298 |
} |
| 299 |
|
| 300 |
return PPIF_SUCCESS; |
| 301 |
} |
| 302 |
|
| 303 |
/****************************************************************************/ |
| 304 |
/* */ |
| 305 |
/* Tree oriented functions */ |
| 306 |
/* */ |
| 307 |
/****************************************************************************/ |
| 308 |
|
| 309 |
int PPIF::Broadcast (const PPIFContext& context, void *data, int size) |
| 310 |
{ |
| 311 |
if (MPI_SUCCESS != MPI_Bcast (data, size, MPI_BYTE, context.master(), context.comm()) ) |
| 312 |
return (PPIF_FAILURE); |
| 313 |
|
| 314 |
return (PPIF_SUCCESS); |
| 315 |
} |
| 316 |
|
| 317 |
int PPIF::Concentrate (const PPIFContext& context, void *data, int size) |
| 318 |
{ |
| 319 |
if (not context.isMaster()) |
| 320 |
if (SendSync (context, context.uptree(), data, size) < 0) return (PPIF_FAILURE); |
| 321 |
|
| 322 |
return (PPIF_SUCCESS); |
| 323 |
} |
| 324 |
|
| 325 |
int PPIF::GetConcentrate(const PPIFContext& context, int slave, void *data, int size) |
| 326 |
{ |
| 327 |
if (slave < context.degree()) |
| 328 |
if (RecvSync (context, context.downtree()[slave], data, size) < 0) return (PPIF_FAILURE); |
| 329 |
|
| 330 |
return (PPIF_SUCCESS); |
| 331 |
} |
| 332 |
|
| 333 |
int PPIF::Spread(const PPIFContext& context, int slave, void *data, int size) |
| 334 |
{ |
| 335 |
if (slave < context.degree()) |
| 336 |
if (SendSync(context, context.downtree()[slave], data, size) < 0) return (PPIF_FAILURE); |
| 337 |
|
| 338 |
return (PPIF_SUCCESS); |
| 339 |
} |
| 340 |
|
| 341 |
int PPIF::GetSpread(const PPIFContext& context, void *data, int size) |
| 342 |
{ |
| 343 |
if (not context.isMaster()) |
| 344 |
if (RecvSync(context, context.uptree(), data, size) < 0) return (PPIF_FAILURE); |
| 345 |
|
| 346 |
return (PPIF_SUCCESS); |
| 347 |
} |
| 348 |
|
| 349 |
int PPIF::Synchronize(const PPIFContext& context) |
| 350 |
{ |
| 351 |
if (MPI_SUCCESS != MPI_Barrier (context.comm()) ) return (PPIF_FAILURE); |
| 352 |
|
| 353 |
return (PPIF_SUCCESS); |
| 354 |
} |
| 355 |
|
| 356 |
/****************************************************************************/ |
| 357 |
/* */ |
| 358 |
/* Synchronous communication */ |
| 359 |
/* */ |
| 360 |
/****************************************************************************/ |
| 361 |
|
| 362 |
VChannelPtr PPIF::ConnSync(const PPIFContext&, int p, int id) |
| 363 |
{ |
| 364 |
return NewVChan(p, id); |
| 365 |
} |
| 366 |
|
| 367 |
int PPIF::DiscSync(const PPIFContext&, VChannelPtr v) |
| 368 |
{ |
| 369 |
DeleteVChan(v); |
| 370 |
|
| 371 |
return (0); |
| 372 |
} |
| 373 |
|
| 374 |
int PPIF::SendSync(const PPIFContext& context, VChannelPtr v, void *data, int size) |
| 375 |
{ |
| 376 |
if (MPI_SUCCESS == MPI_Ssend (data, size, MPI_BYTE, |
| 377 |
v->p, v->chanid, context.comm()) ) |
| 378 |
return (size); |
| 379 |
else |
| 380 |
return (-1); |
| 381 |
} |
| 382 |
|
| 383 |
int PPIF::RecvSync(const PPIFContext& context, VChannelPtr v, void *data, int size) |
| 384 |
{ |
| 385 |
int count = -1; |
| 386 |
MPI_Status status; |
| 387 |
|
| 388 |
if (MPI_SUCCESS == MPI_Recv (data, size, MPI_BYTE, |
| 389 |
v->p, v->chanid, context.comm(), &status) ) |
| 390 |
MPI_Get_count (&status, MPI_BYTE, &count); |
| 391 |
|
| 392 |
return (count); |
| 393 |
} |
| 394 |
|
| 395 |
/****************************************************************************/ |
| 396 |
/* */ |
| 397 |
/* Asynchronous communication */ |
| 398 |
/* */ |
| 399 |
/****************************************************************************/ |
| 400 |
|
| 401 |
VChannelPtr PPIF::ConnASync(const PPIFContext&, int p, int id) |
| 402 |
{ |
| 403 |
return NewVChan(p, id); |
| 404 |
} |
| 405 |
|
| 406 |
int PPIF::InfoAConn(const PPIFContext&, const VChannelPtr v) |
| 407 |
{ |
| 408 |
return (v ? 1 : -1); |
| 409 |
} |
| 410 |
|
| 411 |
int PPIF::DiscASync(const PPIFContext&, VChannelPtr v) |
| 412 |
{ |
| 413 |
DeleteVChan (v); |
| 414 |
return (PPIF_SUCCESS); |
| 415 |
} |
| 416 |
|
| 417 |
int PPIF::InfoADisc(const PPIFContext&, const VChannelPtr v) |
| 418 |
{ |
| 419 |
return (true); |
| 420 |
} |
| 421 |
|
| 422 |
msgid PPIF::SendASync(const PPIFContext& context, VChannelPtr v, void *data, int size, int *error) |
| 423 |
{ |
| 424 |
msgid m = new PPIF::Msg; |
| 425 |
|
| 426 |
if (m) |
| 427 |
{ |
| 428 |
if (MPI_SUCCESS == MPI_Isend (data, size, MPI_BYTE, |
| 429 |
v->p, v->chanid, context.comm(), &m->req) ) |
| 430 |
{ |
| 431 |
*error = false; |
| 432 |
return m; |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
*error = true; |
| 437 |
return NULL; |
| 438 |
} |
| 439 |
|
| 440 |
msgid PPIF::RecvASync(const PPIFContext& context, VChannelPtr v, void *data, int size, int *error) |
| 441 |
{ |
| 442 |
msgid m = new PPIF::Msg; |
| 443 |
|
| 444 |
if (m) |
| 445 |
{ |
| 446 |
if (MPI_SUCCESS == MPI_Irecv (data, size, MPI_BYTE, |
| 447 |
v->p, v->chanid, context.comm(), &m->req) ) |
| 448 |
{ |
| 449 |
*error = false; |
| 450 |
return m; |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
*error = true; |
| 455 |
return (NULL); |
| 456 |
} |
| 457 |
|
| 458 |
int PPIF::InfoASend(const PPIFContext&, VChannelPtr v, msgid m) |
| 459 |
{ |
| 460 |
int complete; |
| 461 |
|
| 462 |
if (m) |
| 463 |
{ |
| 464 |
if (MPI_SUCCESS == MPI_Test (&m->req, &complete, MPI_STATUS_IGNORE) ) |
| 465 |
{ |
| 466 |
if (complete) |
| 467 |
delete m; |
| 468 |
|
| 469 |
return (complete); /* complete is true for completed send, false otherwise */ |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
return (-1); /* return -1 for FAILURE */ |
| 474 |
} |
| 475 |
|
| 476 |
int PPIF::InfoARecv(const PPIFContext&, VChannelPtr v, msgid m) |
| 477 |
{ |
| 478 |
int complete; |
| 479 |
|
| 480 |
if (m) |
| 481 |
{ |
| 482 |
if (MPI_SUCCESS == MPI_Test (&m->req, &complete, MPI_STATUS_IGNORE) ) |
| 483 |
{ |
| 484 |
if (complete) |
| 485 |
delete m; |
| 486 |
|
| 487 |
return (complete); /* complete is true for completed receive, false otherwise */ |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
return (-1); /* return -1 for FAILURE */ |
| 492 |
} |