| [1fbac22] | 1 | #ifndef __MPI_CVL__
|
|---|
| [20e1243] | 2 | #define __MPI_CVL__
|
|---|
| [e15d0a3] | 3 |
|
|---|
| [411e0b8] | 4 | //TODO make a Datatype struct, which has a field "int size;" Define one of these objects for MPI_INT, MPI_DOUBLE, etc.
|
|---|
| 5 | //TODO Then provide methods like MPI provides for creating new ones.
|
|---|
| 6 | //TODO then support MPI_Type_contig(datatype, int n).
|
|---|
| 7 |
|
|---|
| [1fbac22] | 8 | #define BCAST_TAG 999
|
|---|
| 9 | #define REDUCE_TAG 998
|
|---|
| 10 |
|
|---|
| 11 | /* Completed definition for mpi-common.h */
|
|---|
| 12 | struct MPI_Status{
|
|---|
| 13 | int MPI_SOURCE;
|
|---|
| 14 | int MPI_TAG;
|
|---|
| 15 | int MPI_ERROR;
|
|---|
| 16 | int size;
|
|---|
| 17 | };
|
|---|
| 18 |
|
|---|
| 19 | /* Definition of CIVL-MPI */
|
|---|
| 20 | typedef enum {
|
|---|
| 21 | __UNINIT,
|
|---|
| 22 | __INIT,
|
|---|
| 23 | __FINALIZED
|
|---|
| 24 | }__MPI_Sys_status__;
|
|---|
| 25 |
|
|---|
| 26 | struct MPI_Request{
|
|---|
| 27 | int id;
|
|---|
| 28 | };
|
|---|
| 29 |
|
|---|
| [20e1243] | 30 | /* Definition of CMPI_Gcomm and MPI_Comm */
|
|---|
| [1fbac22] | 31 | typedef struct CMPI_Gcomm {
|
|---|
| [20e1243] | 32 | $gcomm p2p; // point-to-point communication
|
|---|
| 33 | $gcomm col; // collective communication
|
|---|
| [1fbac22] | 34 | $gbarrier gbarrier;
|
|---|
| 35 | } CMPI_Gcomm;
|
|---|
| [20e1243] | 36 |
|
|---|
| 37 | struct MPI_Comm {
|
|---|
| 38 | $comm p2p; // point-to-point communication
|
|---|
| 39 | $comm col; // collective communication
|
|---|
| [1fbac22] | 40 | $barrier barrier;
|
|---|
| 41 | __MPI_Sys_status__ status;
|
|---|
| [20e1243] | 42 | };
|
|---|
| 43 |
|
|---|
| [2e6fe6f] | 44 | /********************************** State **************************************/
|
|---|
| 45 |
|
|---|
| 46 | /* The number of times the MPI_Wtime function has been called */
|
|---|
| 47 | int CMPI_time_count = 0;
|
|---|
| 48 |
|
|---|
| [e15d0a3] | 49 | /****************************** Helper Functions **********************************/
|
|---|
| 50 | int sizeofDatatype(MPI_Datatype datatype) {
|
|---|
| 51 | switch (datatype) {
|
|---|
| 52 | case MPI_INT:
|
|---|
| 53 | return sizeof(int);
|
|---|
| 54 | case MPI_FLOAT:
|
|---|
| 55 | return sizeof(float);
|
|---|
| 56 | case MPI_DOUBLE:
|
|---|
| 57 | return sizeof(double);
|
|---|
| 58 | case MPI_CHAR:
|
|---|
| 59 | return sizeof(char);
|
|---|
| 60 | default:
|
|---|
| 61 | $assert(0, "Unreachable");
|
|---|
| 62 | }
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | /* Helpers for MPI_Reduce and MPI_Allreduce */
|
|---|
| 66 | void sumInt(int result[], int buf[], int real_count, int nprocs){
|
|---|
| 67 | //init result array
|
|---|
| 68 | $atomic{
|
|---|
| 69 | for(int i=0; i<real_count; i++){
|
|---|
| 70 | result[i] = 0;
|
|---|
| 71 | }
|
|---|
| 72 | //sum up
|
|---|
| 73 | for(int i=0; i<nprocs; i++)
|
|---|
| 74 | for(int j=0; j<real_count; j++)
|
|---|
| 75 | result[j] = result[j] + buf[i * real_count + j];
|
|---|
| 76 | }
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | void sumFloat(float result[], float buf[], int real_count, int nprocs){
|
|---|
| 80 | $atomic{
|
|---|
| 81 | for(int i=0; i<real_count; i++){
|
|---|
| 82 | result[i] = 0;
|
|---|
| 83 | }
|
|---|
| 84 | //sum up
|
|---|
| 85 | for(int i=0; i<nprocs; i++)
|
|---|
| 86 | for(int j=0; j<real_count; j++)
|
|---|
| 87 | result[j] = result[j] + buf[i * real_count + j];
|
|---|
| 88 | }
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | void sumDouble(double result[], double buf[], int real_count, int nprocs){
|
|---|
| 92 | $atomic{
|
|---|
| 93 | for(int i=0; i<real_count; i++){
|
|---|
| 94 | result[i] = 0;
|
|---|
| 95 | }
|
|---|
| 96 | //sum up
|
|---|
| 97 | for(int i=0; i<nprocs; i++)
|
|---|
| 98 | for(int j=0; j<real_count; j++)
|
|---|
| 99 | result[j] = result[j] + buf[i * real_count + j];
|
|---|
| 100 | }
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 | void maxInt(int result[], int buf[], int real_count, int nprocs){
|
|---|
| 105 | //init result arra
|
|---|
| 106 | $atomic{
|
|---|
| 107 | for(int i=0; i<real_count; i++){
|
|---|
| 108 | result[i] = buf[i * real_count];
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | for(int i=0; i<nprocs; i++)
|
|---|
| 112 | for(int j=0; j<real_count; j++){
|
|---|
| 113 | if(buf[i * real_count + j] > result[j])
|
|---|
| 114 | result[j] = buf[i * real_count + j];
|
|---|
| 115 | }
|
|---|
| 116 | }
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | void maxFloat(float result[], float buf[], int real_count, int nprocs){
|
|---|
| 120 | //init result array
|
|---|
| 121 | $atom{
|
|---|
| 122 | for(int i=0; i<real_count; i++){
|
|---|
| 123 | result[i] = buf[i * real_count];
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | for(int i=0; i<nprocs; i++)
|
|---|
| 127 | for(int j=0; j<real_count; j++){
|
|---|
| 128 | if(buf[i * real_count + j] > result[j])
|
|---|
| 129 | result[j] = buf[i * real_count + j];
|
|---|
| 130 | }
|
|---|
| 131 | }
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | void maxDouble(double result[], double buf[], int real_count, int nprocs){
|
|---|
| 135 | //init result array
|
|---|
| 136 | $atom{
|
|---|
| 137 | for(int i=0; i<real_count; i++){
|
|---|
| 138 | result[i] = buf[i * real_count];
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | for(int i=0; i<nprocs; i++)
|
|---|
| 142 | for(int j=0; j<real_count; j++){
|
|---|
| 143 | if(buf[i * real_count + j] > result[j])
|
|---|
| 144 | result[j] = buf[i * real_count + j];
|
|---|
| 145 | }
|
|---|
| 146 | }
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | void minInt(int result[], int buf[], int real_count, int nprocs){
|
|---|
| 150 | //init result array
|
|---|
| 151 | $atom{
|
|---|
| 152 | for(int i=0; i<real_count; i++){
|
|---|
| 153 | result[i] = buf[i * real_count];
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | for(int i=0; i<nprocs; i++)
|
|---|
| 157 | for(int j=0; j<real_count; j++){
|
|---|
| 158 | if(buf[i * real_count + j] < result[j])
|
|---|
| 159 | result[j] = buf[i * real_count + j];
|
|---|
| 160 | }
|
|---|
| 161 | }
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | void minFloat(float result[], float buf[], int real_count, int nprocs){
|
|---|
| 165 | //init result array
|
|---|
| 166 | $atom{
|
|---|
| 167 | for(int i=0; i<real_count; i++){
|
|---|
| 168 | result[i] = buf[i * real_count];
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | for(int i=0; i<nprocs; i++)
|
|---|
| 172 | for(int j=0; j<real_count; j++){
|
|---|
| 173 | if(buf[i * real_count + j] < result[j])
|
|---|
| 174 | result[j] = buf[i * real_count + j];
|
|---|
| 175 | }
|
|---|
| 176 | }
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | void minDouble(double result[], double buf[], int real_count, int nprocs){
|
|---|
| 180 | //init result array
|
|---|
| 181 | $atom{
|
|---|
| 182 | for(int i=0; i<real_count; i++){
|
|---|
| 183 | result[i] = buf[i * real_count];
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | for(int i=0; i<nprocs; i++)
|
|---|
| 187 | for(int j=0; j<real_count; j++){
|
|---|
| 188 | if(buf[i * real_count + j] < result[j])
|
|---|
| 189 | result[j] = buf[i * real_count + j];
|
|---|
| 190 | }
|
|---|
| 191 | }
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | /************************** MPI LIB Implementations *******************************/
|
|---|
| [2e6fe6f] | 195 |
|
|---|
| 196 | $abstract double CMPI_time(int count);
|
|---|
| 197 |
|
|---|
| 198 | double MPI_Wtime() {
|
|---|
| 199 | double result = CMPI_time(CMPI_time_count);
|
|---|
| 200 |
|
|---|
| 201 | CMPI_time_count++;
|
|---|
| 202 | return result;
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| [e15d0a3] | 205 | CMPI_Gcomm CMPI_Gcomm_create($scope scope, int size) {
|
|---|
| 206 | CMPI_Gcomm result;
|
|---|
| 207 |
|
|---|
| 208 | result.p2p = $gcomm_create(scope, size);
|
|---|
| 209 | result.col = $gcomm_create(scope, size);
|
|---|
| [1fbac22] | 210 | result.gbarrier = $gbarrier_create(scope, size);
|
|---|
| [e15d0a3] | 211 | return result;
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | void CMPI_Gcomm_destroy(CMPI_Gcomm gc) {
|
|---|
| 215 | $gcomm_destroy(gc.p2p);
|
|---|
| 216 | $gcomm_destroy(gc.col);
|
|---|
| [1fbac22] | 217 | $gbarrier_destroy(gc.gbarrier);
|
|---|
| [e15d0a3] | 218 | }
|
|---|
| 219 |
|
|---|
| [1fbac22] | 220 | MPI_Comm CMPI_Comm_create($scope scope, CMPI_Gcomm gc, int rank) {
|
|---|
| [e15d0a3] | 221 | MPI_Comm result;
|
|---|
| 222 |
|
|---|
| 223 | result.p2p = $comm_create(scope, gc.p2p, rank);
|
|---|
| 224 | result.col = $comm_create(scope, gc.col, rank);
|
|---|
| [1fbac22] | 225 | result.barrier = $barrier_create(scope, gc.gbarrier, rank);
|
|---|
| [c574547] | 226 | result.status = __UNINIT;
|
|---|
| [e15d0a3] | 227 | return result;
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| [1fbac22] | 230 | void CMPI_Comm_destroy(MPI_Comm comm) {
|
|---|
| [e15d0a3] | 231 | $comm_destroy(comm.p2p);
|
|---|
| 232 | $comm_destroy(comm.col);
|
|---|
| [1fbac22] | 233 | $barrier_destroy(comm.barrier);
|
|---|
| [e15d0a3] | 234 | }
|
|---|
| 235 |
|
|---|
| [c574547] | 236 | int __MPI_Init(MPI_Comm *comm) {
|
|---|
| 237 | comm->status = __INIT;
|
|---|
| [e15d0a3] | 238 | return 0;
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| [c574547] | 241 | int __MPI_Finalize(MPI_Comm *comm) {
|
|---|
| 242 | comm->status = __FINALIZED;
|
|---|
| [e15d0a3] | 243 | return 0;
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | int MPI_Comm_size(MPI_Comm comm, int *size) {
|
|---|
| [411e0b8] | 247 | $assert(comm.status == __INIT, "MPI_Comm_size() cannot be invoked without MPI_Init() being called before.\n");
|
|---|
| [e15d0a3] | 248 | *size = $comm_size(comm.p2p);
|
|---|
| 249 | return 0;
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | int MPI_Comm_rank(MPI_Comm comm, int *rank) {
|
|---|
| [411e0b8] | 253 | $assert(comm.status == __INIT, "MPI_Comm_rank() cannot be invoked without MPI_Init() being called before.\n");
|
|---|
| [e15d0a3] | 254 | *rank = $comm_place(comm.p2p);
|
|---|
| 255 | return 0;
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 |
|
|---|
| 259 | int CMPI_Send(void *buf, int count, MPI_Datatype datatype, int dest,
|
|---|
| 260 | int tag, $comm comm) {
|
|---|
| 261 | if (dest >= 0) {
|
|---|
| 262 | int size = count*sizeofDatatype(datatype);
|
|---|
| 263 | int place = $comm_place(comm);
|
|---|
| 264 | $message out = $message_pack(place, dest, tag, buf, size);
|
|---|
| 265 |
|
|---|
| 266 | $comm_enqueue(comm, out);
|
|---|
| 267 | }
|
|---|
| 268 | return 0;
|
|---|
| 269 | }
|
|---|
| 270 |
|
|---|
| 271 | int MPI_Send(void *buf, int count, MPI_Datatype datatype, int dest,
|
|---|
| 272 | int tag, MPI_Comm comm) {
|
|---|
| [411e0b8] | 273 | $assert(comm.status == __INIT, "MPI_Send() cannot be invoked without MPI_Init() being called before.\n");
|
|---|
| [e15d0a3] | 274 | return CMPI_Send(buf, count, datatype, dest, tag, comm.p2p);
|
|---|
| 275 | }
|
|---|
| 276 |
|
|---|
| 277 |
|
|---|
| 278 | int CMPI_Recv(void *buf, int count, MPI_Datatype datatype, int source,
|
|---|
| 279 | int tag, $comm comm, MPI_Status *status) {
|
|---|
| 280 | if (source >= 0) {
|
|---|
| 281 | $message in = $comm_dequeue(comm, source, tag);
|
|---|
| 282 | int size = count*sizeofDatatype(datatype);
|
|---|
| 283 |
|
|---|
| 284 | $message_unpack(in, buf, size);
|
|---|
| 285 | if (status != MPI_STATUS_IGNORE) {
|
|---|
| 286 | status->size = $message_size(in);
|
|---|
| 287 | status->MPI_SOURCE = $message_source(in);
|
|---|
| 288 | status->MPI_TAG = $message_tag(in);
|
|---|
| 289 | status->MPI_ERROR = 0;
|
|---|
| 290 | }
|
|---|
| 291 | }
|
|---|
| 292 | return 0;
|
|---|
| 293 | }
|
|---|
| 294 |
|
|---|
| 295 | int MPI_Recv(void *buf, int count, MPI_Datatype datatype, int source,
|
|---|
| 296 | int tag, MPI_Comm comm, MPI_Status *status) {
|
|---|
| [411e0b8] | 297 | $assert(comm.status == __INIT, "MPI_Recv() cannot be invoked without MPI_Init() being called before.\n");
|
|---|
| [e15d0a3] | 298 | return CMPI_Recv(buf, count, datatype, source, tag, comm.p2p, status);
|
|---|
| 299 | }
|
|---|
| 300 |
|
|---|
| 301 | int MPI_Get_count(MPI_Status *status, MPI_Datatype datatype, int *count) {
|
|---|
| [c574547] | 302 | //$assert(__my_status == __INIT, "MPI status is not INIT.\n");
|
|---|
| [e15d0a3] | 303 | *count = status->size/sizeofDatatype(datatype);
|
|---|
| 304 | return 0;
|
|---|
| 305 | }
|
|---|
| 306 |
|
|---|
| 307 | int MPI_Sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
|
|---|
| 308 | int dest, int sendtag,
|
|---|
| 309 | void *recvbuf, int recvcount, MPI_Datatype recvtype,
|
|---|
| 310 | int source, int recvtag,
|
|---|
| 311 | MPI_Comm comm, MPI_Status *status) {
|
|---|
| [411e0b8] | 312 | $assert(comm.status == __INIT, "MPI_Sendrecv() cannot be invoked without MPI_Init() being called before.\n");
|
|---|
| [e15d0a3] | 313 | MPI_Send(sendbuf, sendcount, sendtype, dest, sendtag, comm);
|
|---|
| 314 | MPI_Recv(recvbuf, recvcount, recvtype, source, recvtag, comm, status);
|
|---|
| 315 | return 0;
|
|---|
| 316 | }
|
|---|
| 317 |
|
|---|
| 318 | /* Broadcasts a message from root to everyone else.
|
|---|
| 319 | * Need to use a differnt comm.
|
|---|
| 320 | */
|
|---|
| 321 | int MPI_Bcast(void *buf, int count, MPI_Datatype datatype, int root,
|
|---|
| 322 | MPI_Comm comm) {
|
|---|
| 323 | int place = $comm_place(comm.col);
|
|---|
| 324 |
|
|---|
| [411e0b8] | 325 | $assert(comm.status == __INIT, "MPI_Bcast() cannot be invoked without MPI_Init() being called before.\n");
|
|---|
| [74e1869] | 326 | place = $comm_place(comm.col);
|
|---|
| [e15d0a3] | 327 | if (place == root) {
|
|---|
| 328 | int nprocs = $comm_size(comm.col);
|
|---|
| 329 |
|
|---|
| 330 | for (int i=0; i<nprocs; i++)
|
|---|
| 331 | if (i != root)
|
|---|
| 332 | CMPI_Send(buf, count, datatype, i, BCAST_TAG, comm.col);
|
|---|
| 333 | } else {
|
|---|
| 334 | CMPI_Recv(buf, count, datatype, root, BCAST_TAG, comm.col,
|
|---|
| 335 | MPI_STATUS_IGNORE);
|
|---|
| 336 | }
|
|---|
| 337 | return 0;
|
|---|
| 338 | }
|
|---|
| 339 |
|
|---|
| 340 | /* Reduces values on all processes to a single value */
|
|---|
| 341 | int MPI_Reduce(void* sendbuf, void* recvbuf, int count, MPI_Datatype datatype,
|
|---|
| 342 | MPI_Op op, int root, MPI_Comm comm){
|
|---|
| [74e1869] | 343 | int place;
|
|---|
| [e15d0a3] | 344 | MPI_Status status;
|
|---|
| 345 |
|
|---|
| [411e0b8] | 346 | $assert(comm.status == __INIT, "MPI_Reduce() cannot be invoked without MPI_Init() being called before.\n");
|
|---|
| [74e1869] | 347 | place = $comm_place(comm.col);
|
|---|
| [e15d0a3] | 348 | //non-root
|
|---|
| 349 | CMPI_Send(sendbuf, count, datatype, root, REDUCE_TAG, comm.col);
|
|---|
| 350 | if(place != root)
|
|---|
| 351 | return 0;
|
|---|
| 352 | else{
|
|---|
| 353 | //root
|
|---|
| 354 | int nprocs = $comm_size(comm.col);
|
|---|
| 355 | int real_count = -1; //the real count gotton from MPI_Status
|
|---|
| 356 | //void ** buf; //Buffer stores data from other processes
|
|---|
| 357 | void * recv; //Buffer used in MPI_Recv()
|
|---|
| 358 | void * results; //Array of results
|
|---|
| 359 | $scope here = $root;
|
|---|
| 360 | struct Bundle_buf{
|
|---|
| 361 | int I[nprocs * count];
|
|---|
| 362 | float F[nprocs * count];
|
|---|
| 363 | double D[nprocs * count];
|
|---|
| 364 | } buf;
|
|---|
| 365 |
|
|---|
| 366 | switch(datatype){
|
|---|
| 367 | case MPI_INT :
|
|---|
| 368 | recv = (int *)$malloc(here, count * sizeof(int));
|
|---|
| 369 | results = (int *)$malloc(here, count * sizeof(int));
|
|---|
| 370 | break;
|
|---|
| 371 | case MPI_FLOAT :
|
|---|
| 372 | recv = (float *)$malloc(here, count * sizeof(float));
|
|---|
| 373 | results = (float *)$malloc(here, count * sizeof(float));
|
|---|
| 374 | break;
|
|---|
| 375 | case MPI_DOUBLE :
|
|---|
| 376 | recv = (double *)$malloc(here, count * sizeof(double));
|
|---|
| 377 | results = (double *)$malloc(here, count * sizeof(double));
|
|---|
| 378 | break;
|
|---|
| 379 | }
|
|---|
| 380 | for(int i=0; i<nprocs; i++){
|
|---|
| 381 | CMPI_Recv(recv, count, datatype, i, REDUCE_TAG, comm.col,
|
|---|
| 382 | &status);
|
|---|
| 383 | MPI_Get_count(&status, datatype, &real_count);
|
|---|
| 384 | $assert(real_count == count);
|
|---|
| 385 |
|
|---|
| 386 | switch(datatype){
|
|---|
| 387 | case MPI_INT :
|
|---|
| 388 | for(int j=0; j<real_count; j++)
|
|---|
| 389 | buf.I[i * real_count + j] = ((int *)recv)[j];
|
|---|
| 390 | break;
|
|---|
| 391 | case MPI_FLOAT :
|
|---|
| 392 | for(int j=0; j<real_count; j++)
|
|---|
| 393 | buf.F[i * real_count + j] = ((float *)recv)[j];
|
|---|
| 394 | break;
|
|---|
| 395 | case MPI_DOUBLE :
|
|---|
| 396 | for(int j=0; j<real_count; j++)
|
|---|
| 397 | buf.D[i * real_count + j] = ((double *)recv)[j];
|
|---|
| 398 | break;
|
|---|
| 399 | }
|
|---|
| 400 | }
|
|---|
| 401 |
|
|---|
| 402 | //operations
|
|---|
| 403 | switch(datatype){
|
|---|
| 404 | case MPI_INT:
|
|---|
| 405 | if(op == MPI_SUM)
|
|---|
| 406 | sumInt(results, buf.I, real_count, nprocs);
|
|---|
| 407 | if(op == MPI_MAX)
|
|---|
| 408 | maxInt(results, buf.I, real_count, nprocs);
|
|---|
| 409 | if(op == MPI_MIN)
|
|---|
| 410 | minInt(results, buf.I, real_count, nprocs);
|
|---|
| 411 | break;
|
|---|
| 412 | case MPI_FLOAT:
|
|---|
| 413 | if(op == MPI_SUM)
|
|---|
| 414 | sumFloat(results, buf.F, real_count, nprocs);
|
|---|
| 415 | if(op == MPI_MAX)
|
|---|
| 416 | maxFloat(results, buf.F, real_count, nprocs);
|
|---|
| 417 | if(op == MPI_MIN)
|
|---|
| 418 | minFloat(results, buf.F, real_count, nprocs);
|
|---|
| 419 | break;
|
|---|
| 420 | case MPI_DOUBLE:
|
|---|
| 421 | if(op == MPI_SUM)
|
|---|
| 422 | sumDouble(results, buf.D, real_count, nprocs);
|
|---|
| 423 | if(op == MPI_MAX)
|
|---|
| 424 | maxDouble(results, buf.D, real_count, nprocs);
|
|---|
| 425 | if(op == MPI_MIN)
|
|---|
| 426 | minDouble(results, buf.D, real_count, nprocs);
|
|---|
| 427 | break;
|
|---|
| 428 | }
|
|---|
| [80bbea5] | 429 | MPI_Sendrecv(results, real_count, datatype,
|
|---|
| [e15d0a3] | 430 | place, 0,
|
|---|
| [80bbea5] | 431 | recvbuf, real_count, datatype,
|
|---|
| [e15d0a3] | 432 | place, 0,
|
|---|
| 433 | comm, &status);
|
|---|
| 434 | $free(recv);
|
|---|
| 435 | $free(results);
|
|---|
| 436 | }
|
|---|
| 437 | return 0;
|
|---|
| 438 | }
|
|---|
| 439 |
|
|---|
| 440 | /* Combines values from all processes and distributes the result back to all processes */
|
|---|
| 441 | /* default root is 0 */
|
|---|
| 442 | int MPI_Allreduce(void* sendbuf, void* recvbuf, int count, MPI_Datatype datatype,
|
|---|
| 443 | MPI_Op op, MPI_Comm comm){
|
|---|
| [74e1869] | 444 | int place;
|
|---|
| 445 | int root;
|
|---|
| [e15d0a3] | 446 | MPI_Status status;
|
|---|
| [74e1869] | 447 |
|
|---|
| [411e0b8] | 448 | $assert(comm.status == __INIT, "MPI_Allreduce() cannot be invoked without MPI_Init() being called before.\n");
|
|---|
| [74e1869] | 449 | place = $comm_place(comm.col);
|
|---|
| 450 | root = 0;
|
|---|
| [1fbac22] | 451 | MPI_Reduce(sendbuf, recvbuf, count, datatype, op, root, comm);
|
|---|
| 452 | if(place == root)
|
|---|
| 453 | MPI_Sendrecv(recvbuf, count, datatype, place, 0,
|
|---|
| 454 | recvbuf, count, datatype, place, 0,
|
|---|
| 455 | comm, &status);
|
|---|
| 456 | MPI_Bcast(recvbuf, count, datatype, root, comm);
|
|---|
| [e15d0a3] | 457 | return 0;
|
|---|
| 458 | }
|
|---|
| [20e1243] | 459 |
|
|---|
| [1fbac22] | 460 | int MPI_Barrier(MPI_Comm comm){
|
|---|
| 461 |
|
|---|
| 462 | $assert(comm.status == __INIT, "MPI_Allreduce() cannot be invoked without MPI_Init() being called before.\n");
|
|---|
| 463 | $barrier_call(comm.barrier);
|
|---|
| 464 | }
|
|---|
| [20e1243] | 465 | #endif
|
|---|