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 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
|
/****************************************************************/
/* Parallel Combinatorial BLAS Library (for Graph Computations) */
/* version 1.6 -------------------------------------------------*/
/* date: 6/15/2017 ---------------------------------------------*/
/* authors: Ariful Azad, Aydin Buluc --------------------------*/
/****************************************************************/
/*
Copyright (c) 2010-2017, The Regents of the University of California
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef _PAR_FRIENDS_EXT_H_
#define _PAR_FRIENDS_EXT_H_
#include "mpi.h"
#include <iostream>
#include "SpParMat.h"
#include "SpParHelper.h"
#include "MPIType.h"
#include "Friends.h"
namespace combblas {
template <class IT, class NT, class DER>
class SpParMat;
/*************************************************************************************************/
/**************************** FRIEND FUNCTIONS FOR PARALLEL CLASSES ******************************/
/************************** EXTENDED SET (NOT COMMONLY USED FUNCTIONS) ***************************/
/*************************************************************************************************/
/**
* Parallel A = B*C routine that uses one-sided MPI-2 features
* General active target syncronization via MPI_Win_Post, MPI_Win_Start, MPI_Win_Complete, MPI_Win_Wait
* Tested on my dual core Macbook with 1,4,9,16,25 MPI processes
* No memory hog: splits the matrix into two along the column, prefetches the next half matrix while computing on the current one
**/
template <typename SR, typename IU, typename NU1, typename NU2, typename UDERA, typename UDERB>
SpParMat<IU,typename promote_trait<NU1,NU2>::T_promote,typename promote_trait<UDERA,UDERB>::T_promote> Mult_AnXBn_ActiveTarget
(const SpParMat<IU,NU1,UDERA> & A, const SpParMat<IU,NU2,UDERB> & B )
{
typedef typename promote_trait<NU1,NU2>::T_promote N_promote;
typedef typename promote_trait<UDERA,UDERB>::T_promote DER_promote;
if(A.getncol() != B.getnrow())
{
std::cout<<"Can not multiply, dimensions does not match"<<std::endl;
MPI_Abort(MPI_COMM_WORLD, DIMMISMATCH);
return SpParMat< IU,N_promote,DER_promote >();
}
int stages, Aoffset, Boffset; // stages = inner dimension of matrix blocks
std::shared_ptr<CommGrid> GridC = ProductGrid((A.commGrid).get(), (B.commGrid).get(), stages, Aoffset, Boffset);
IU C_m = A.spSeq->getnrow();
IU C_n = B.spSeq->getncol();
UDERA A1seq, A2seq;
(A.spSeq)->Split( A1seq, A2seq);
// ABAB: It should be able to perform split/merge with the transpose option [single function call]
const_cast< UDERB* >(B.spSeq)->Transpose();
UDERB B1seq, B2seq;
(B.spSeq)->Split( B1seq, B2seq);
// Create row and column windows (collective operation, i.e. everybody exposes its window to others)
std::vector<MPI_Win> rowwins1, rowwins2, colwins1, colwins2;
SpParHelper::SetWindows((A.commGrid)->GetRowWorld(), A1seq, rowwins1);
SpParHelper::SetWindows((A.commGrid)->GetRowWorld(), A2seq, rowwins2);
SpParHelper::SetWindows((B.commGrid)->GetColWorld(), B1seq, colwins1);
SpParHelper::SetWindows((B.commGrid)->GetColWorld(), B2seq, colwins2);
// ABAB: We can optimize the call to create windows in the absence of passive synchronization
// MPI_Info info;
// MPI_Info_create ( &info );
// MPI_Info_set( info, "no_locks", "true" );
// MPI_Win_create( . . ., info, . . . );
// MPI_Info_free( &info );
IU ** ARecvSizes1 = SpHelper::allocate2D<IU>(UDERA::esscount, stages);
IU ** ARecvSizes2 = SpHelper::allocate2D<IU>(UDERA::esscount, stages);
IU ** BRecvSizes1 = SpHelper::allocate2D<IU>(UDERB::esscount, stages);
IU ** BRecvSizes2 = SpHelper::allocate2D<IU>(UDERB::esscount, stages);
SpParHelper::GetSetSizes( A1seq, ARecvSizes1, (A.commGrid)->GetRowWorld());
SpParHelper::GetSetSizes( A2seq, ARecvSizes2, (A.commGrid)->GetRowWorld());
SpParHelper::GetSetSizes( B1seq, BRecvSizes1, (B.commGrid)->GetColWorld());
SpParHelper::GetSetSizes( B2seq, BRecvSizes2, (B.commGrid)->GetColWorld());
// Remotely fetched matrices are stored as pointers
UDERA * ARecv1, * ARecv2;
UDERB * BRecv1, * BRecv2;
std::vector< SpTuples<IU,N_promote> *> tomerge;
MPI_Group row_group, col_group;
MPI_Comm_group((A.commGrid)->GetRowWorld(), &row_group);
MPI_Comm_group((B.commGrid)->GetColWorld(), &col_group);
int Aself = (A.commGrid)->GetRankInProcRow();
int Bself = (B.commGrid)->GetRankInProcCol();
#ifdef SPGEMMDEBUG
MPI_Barrier(GridC->GetWorld());
SpParHelper::Print("Writing to file\n");
std::ofstream oput;
GridC->OpenDebugFile("deb", oput);
oput << "A1seq: " << A1seq.getnrow() << " " << A1seq.getncol() << " " << A1seq.getnnz() << std::endl;
oput << "A2seq: " << A2seq.getnrow() << " " << A2seq.getncol() << " " << A2seq.getnnz() << std::endl;
oput << "B1seq: " << B1seq.getnrow() << " " << B1seq.getncol() << " " << B1seq.getnnz() << std::endl;
oput << "B2seq: " << B2seq.getnrow() << " " << B2seq.getncol() << " " << B2seq.getnnz() << std::endl;
SpParHelper::Print("Wrote to file\n");
MPI_Barrier(GridC->GetWorld());
#endif
SpParHelper::PostExposureEpoch(Aself, rowwins1, row_group);
SpParHelper::PostExposureEpoch(Aself, rowwins2, row_group);
SpParHelper::PostExposureEpoch(Bself, colwins1, col_group);
SpParHelper::PostExposureEpoch(Bself, colwins2, col_group);
MPI_Barrier(GridC->GetWorld());
SpParHelper::Print("Exposure epochs posted\n");
MPI_Barrier(GridC->GetWorld());
int Aowner = (0+Aoffset) % stages;
int Bowner = (0+Boffset) % stages;
SpParHelper::AccessNFetch(ARecv1, Aowner, rowwins1, row_group, ARecvSizes1);
SpParHelper::AccessNFetch(ARecv2, Aowner, rowwins2, row_group, ARecvSizes2); // Start prefetching next half
for(int j=0; j< rowwins1.size(); ++j) // wait for the first half to complete
rowwins1[j].Complete();
SpParHelper::AccessNFetch(BRecv1, Bowner, colwins1, col_group, BRecvSizes1);
SpParHelper::AccessNFetch(BRecv2, Bowner, colwins2, col_group, BRecvSizes2); // Start prefetching next half
for(int j=0; j< colwins1.size(); ++j)
colwins1[j].Complete();
for(int i = 1; i < stages; ++i)
{
#ifdef SPGEMMDEBUG
SpParHelper::Print("Stage starting\n");
#endif
SpTuples<IU,N_promote> * C_cont = MultiplyReturnTuples<SR>(*ARecv1, *BRecv1, false, true);
#ifdef SPGEMMDEBUG
SpParHelper::Print("Multiplied\n");
#endif
if(!C_cont->isZero())
tomerge.push_back(C_cont);
#ifdef SPGEMMDEBUG
SpParHelper::Print("Pushed back\n");
MPI_Barrier(GridC->GetWorld());
#endif
bool remoteA = false;
bool remoteB = false;
delete ARecv1; // free the memory of the previous first half
for(int j=0; j< rowwins2.size(); ++j) // wait for the previous second half to complete
rowwins2[j].Complete();
SpParHelper::Print("Completed A\n");
delete BRecv1;
for(int j=0; j< colwins2.size(); ++j) // wait for the previous second half to complete
colwins2[j].Complete();
#ifdef SPGEMMDEBUG
SpParHelper::Print("Completed B\n");
MPI_Barrier(GridC->GetWorld());
#endif
Aowner = (i+Aoffset) % stages;
Bowner = (i+Boffset) % stages;
// start fetching the current first half
SpParHelper::AccessNFetch(ARecv1, Aowner, rowwins1, row_group, ARecvSizes1);
SpParHelper::AccessNFetch(BRecv1, Bowner, colwins1, col_group, BRecvSizes1);
#ifdef SPGEMMDEBUG
SpParHelper::Print("Fetched next\n");
MPI_Barrier(GridC->GetWorld());
#endif
// while multiplying the already completed previous second halfs
C_cont = MultiplyReturnTuples<SR>(*ARecv2, *BRecv2, false, true);
if(!C_cont->isZero())
tomerge.push_back(C_cont);
#ifdef SPGEMMDEBUG
SpParHelper::Print("Multiplied and pushed\n");
MPI_Barrier(GridC->GetWorld());
#endif
delete ARecv2; // free memory of the previous second half
delete BRecv2;
for(int j=0; j< rowwins1.size(); ++j) // wait for the current first half to complte
rowwins1[j].Complete();
for(int j=0; j< colwins1.size(); ++j)
colwins1[j].Complete();
#ifdef SPGEMMDEBUG
SpParHelper::Print("Completed next\n");
MPI_Barrier(GridC->GetWorld());
#endif
// start prefetching the current second half
SpParHelper::AccessNFetch(ARecv2, Aowner, rowwins2, row_group, ARecvSizes2);
SpParHelper::AccessNFetch(BRecv2, Bowner, colwins2, col_group, BRecvSizes2);
}
SpTuples<IU,N_promote> * C_cont = MultiplyReturnTuples<SR>(*ARecv1, *BRecv1, false, true);
if(!C_cont->isZero())
tomerge.push_back(C_cont);
delete ARecv1; // free the memory of the previous first half
for(int j=0; j< rowwins2.size(); ++j) // wait for the previous second half to complete
rowwins2[j].Complete();
delete BRecv1;
for(int j=0; j< colwins2.size(); ++j) // wait for the previous second half to complete
colwins2[j].Complete();
C_cont = MultiplyReturnTuples<SR>(*ARecv2, *BRecv2, false, true);
if(!C_cont->isZero())
tomerge.push_back(C_cont);
delete ARecv2;
delete BRecv2;
SpHelper::deallocate2D(ARecvSizes1, UDERA::esscount);
SpHelper::deallocate2D(ARecvSizes2, UDERA::esscount);
SpHelper::deallocate2D(BRecvSizes1, UDERB::esscount);
SpHelper::deallocate2D(BRecvSizes2, UDERB::esscount);
DER_promote * C = new DER_promote(MergeAll<SR>(tomerge, C_m, C_n), false, NULL); // First get the result in SpTuples, then convert to UDER
for(int i=0; i<tomerge.size(); ++i)
{
delete tomerge[i];
}
// MPI_Win_Wait() works like a barrier as it waits for all origins to finish their remote memory operation on "this" window
SpParHelper::WaitNFree(rowwins1);
SpParHelper::WaitNFree(rowwins2);
SpParHelper::WaitNFree(colwins1);
SpParHelper::WaitNFree(colwins2);
(A.spSeq)->Merge(A1seq, A2seq);
(B.spSeq)->Merge(B1seq, B2seq);
MPI_Group_free(&row_group);
MPI_Group_free(&col_group);
const_cast< UDERB* >(B.spSeq)->Transpose(); // transpose back to original
return SpParMat<IU,N_promote,DER_promote> (C, GridC); // return the result object
}
/**
* Parallel A = B*C routine that uses one-sided MPI-2 features
* This function implements an asynchronous 2D algorithm, in the sense that there is no notion of stages.
* \n The process that completes its submatrix update, requests subsequent matrices from their owners w/out waiting to sychronize with other processors
* \n This partially remedies the severe load balancing problem in sparse matrices.
* \n The class uses MPI-2 to achieve one-sided asynchronous communication
* \n The algorithm treats each submatrix as a single block
* \n Local data structure can be any SpMat that has a constructor with array sizes and getarrs() member
* Passive target syncronization via MPI_Win_Lock, MPI_Win_Unlock
* No memory hog: splits the matrix into two along the column, prefetches the next half matrix while computing on the current one
**/
template <typename SR, typename IU, typename NU1, typename NU2, typename UDERA, typename UDERB>
SpParMat<IU,typename promote_trait<NU1,NU2>::T_promote,typename promote_trait<UDERA,UDERB>::T_promote> Mult_AnXBn_PassiveTarget
(const SpParMat<IU,NU1,UDERA> & A, const SpParMat<IU,NU2,UDERB> & B )
{
typedef typename promote_trait<NU1,NU2>::T_promote N_promote;
typedef typename promote_trait<UDERA,UDERB>::T_promote DER_promote;
if(A.getncol() != B.getnrow())
{
std::cout<<"Can not multiply, dimensions does not match"<<std::endl;
MPI_Abort(MPI_COMM_WORLD, DIMMISMATCH);
return SpParMat< IU,N_promote,DER_promote >();
}
int stages, Aoffset, Boffset; // stages = inner dimension of matrix blocks
std::shared_ptr<CommGrid> GridC = ProductGrid((A.commGrid).get(), (B.commGrid).get(), stages, Aoffset, Boffset);
IU C_m = A.spSeq->getnrow();
IU C_n = B.spSeq->getncol();
UDERA A1seq, A2seq;
(A.spSeq)->Split( A1seq, A2seq);
// ABAB: It should be able to perform split/merge with the transpose option [single function call]
const_cast< UDERB* >(B.spSeq)->Transpose();
UDERB B1seq, B2seq;
(B.spSeq)->Split( B1seq, B2seq);
// Create row and column windows (collective operation, i.e. everybody exposes its window to others)
std::vector<MPI_Win> rowwins1, rowwins2, colwins1, colwins2;
SpParHelper::SetWindows((A.commGrid)->GetRowWorld(), A1seq, rowwins1);
SpParHelper::SetWindows((A.commGrid)->GetRowWorld(), A2seq, rowwins2);
SpParHelper::SetWindows((B.commGrid)->GetColWorld(), B1seq, colwins1);
SpParHelper::SetWindows((B.commGrid)->GetColWorld(), B2seq, colwins2);
IU ** ARecvSizes1 = SpHelper::allocate2D<IU>(UDERA::esscount, stages);
IU ** ARecvSizes2 = SpHelper::allocate2D<IU>(UDERA::esscount, stages);
IU ** BRecvSizes1 = SpHelper::allocate2D<IU>(UDERB::esscount, stages);
IU ** BRecvSizes2 = SpHelper::allocate2D<IU>(UDERB::esscount, stages);
SpParHelper::GetSetSizes( A1seq, ARecvSizes1, (A.commGrid)->GetRowWorld());
SpParHelper::GetSetSizes( A2seq, ARecvSizes2, (A.commGrid)->GetRowWorld());
SpParHelper::GetSetSizes( B1seq, BRecvSizes1, (B.commGrid)->GetColWorld());
SpParHelper::GetSetSizes( B2seq, BRecvSizes2, (B.commGrid)->GetColWorld());
// Remotely fetched matrices are stored as pointers
UDERA * ARecv1, * ARecv2;
UDERB * BRecv1, * BRecv2;
std::vector< SpTuples<IU,N_promote> *> tomerge; // sorted triples to be merged
MPI_Group row_group, col_group;
MPI_Comm_group((A.commGrid)->GetRowWorld(), &row_group);
MPI_Comm_group((B.commGrid)->GetColWorld(), &col_group);
int Aself = (A.commGrid)->GetRankInProcRow();
int Bself = (B.commGrid)->GetRankInProcCol();
int Aowner = (0+Aoffset) % stages;
int Bowner = (0+Boffset) % stages;
SpParHelper::LockNFetch(ARecv1, Aowner, rowwins1, row_group, ARecvSizes1);
SpParHelper::LockNFetch(ARecv2, Aowner, rowwins2, row_group, ARecvSizes2); // Start prefetching next half
SpParHelper::LockNFetch(BRecv1, Bowner, colwins1, col_group, BRecvSizes1);
SpParHelper::LockNFetch(BRecv2, Bowner, colwins2, col_group, BRecvSizes2); // Start prefetching next half
// Finish the first halfs
SpParHelper::UnlockWindows(Aowner, rowwins1);
SpParHelper::UnlockWindows(Bowner, colwins1);
for(int i = 1; i < stages; ++i)
{
SpTuples<IU,N_promote> * C_cont = MultiplyReturnTuples<SR>(*ARecv1, *BRecv1, false, true);
if(!C_cont->isZero())
tomerge.push_back(C_cont);
bool remoteA = false;
bool remoteB = false;
delete ARecv1; // free the memory of the previous first half
delete BRecv1;
SpParHelper::UnlockWindows(Aowner, rowwins2); // Finish the second half
SpParHelper::UnlockWindows(Bowner, colwins2);
Aowner = (i+Aoffset) % stages;
Bowner = (i+Boffset) % stages;
// start fetching the current first half
SpParHelper::LockNFetch(ARecv1, Aowner, rowwins1, row_group, ARecvSizes1);
SpParHelper::LockNFetch(BRecv1, Bowner, colwins1, col_group, BRecvSizes1);
// while multiplying the already completed previous second halfs
C_cont = MultiplyReturnTuples<SR>(*ARecv2, *BRecv2, false, true);
if(!C_cont->isZero())
tomerge.push_back(C_cont);
delete ARecv2; // free memory of the previous second half
delete BRecv2;
// wait for the current first half to complte
SpParHelper::UnlockWindows(Aowner, rowwins1);
SpParHelper::UnlockWindows(Bowner, colwins1);
// start prefetching the current second half
SpParHelper::LockNFetch(ARecv2, Aowner, rowwins2, row_group, ARecvSizes2);
SpParHelper::LockNFetch(BRecv2, Bowner, colwins2, col_group, BRecvSizes2);
}
SpTuples<IU,N_promote> * C_cont = MultiplyReturnTuples<SR>(*ARecv1, *BRecv1, false, true);
if(!C_cont->isZero())
tomerge.push_back(C_cont);
delete ARecv1; // free the memory of the previous first half
delete BRecv1;
SpParHelper::UnlockWindows(Aowner, rowwins2);
SpParHelper::UnlockWindows(Bowner, colwins2);
C_cont = MultiplyReturnTuples<SR>(*ARecv2, *BRecv2, false, true);
if(!C_cont->isZero())
tomerge.push_back(C_cont);
delete ARecv2;
delete BRecv2;
SpHelper::deallocate2D(ARecvSizes1, UDERA::esscount);
SpHelper::deallocate2D(ARecvSizes2, UDERA::esscount);
SpHelper::deallocate2D(BRecvSizes1, UDERB::esscount);
SpHelper::deallocate2D(BRecvSizes2, UDERB::esscount);
DER_promote * C = new DER_promote(MergeAll<SR>(tomerge, C_m, C_n), false, NULL); // First get the result in SpTuples, then convert to UDER
for(int i=0; i<tomerge.size(); ++i)
{
delete tomerge[i];
}
SpParHelper::FreeWindows(rowwins1);
SpParHelper::FreeWindows(rowwins2);
SpParHelper::FreeWindows(colwins1);
SpParHelper::FreeWindows(colwins2);
(A.spSeq)->Merge(A1seq, A2seq);
(B.spSeq)->Merge(B1seq, B2seq);
MPI_Group_free(&row_group);
MPI_Group_free(&col_group);
const_cast< UDERB* >(B.spSeq)->Transpose(); // transpose back to original
return SpParMat<IU,N_promote,DER_promote> (C, GridC); // return the result object
}
/**
* Parallel A = B*C routine that uses one-sided MPI-2 features
* Syncronization is through MPI_Win_Fence
* Buggy as of September, 2009
**/
template <typename SR, typename IU, typename NU1, typename NU2, typename UDERA, typename UDERB>
SpParMat<IU,typename promote_trait<NU1,NU2>::T_promote,typename promote_trait<UDERA,UDERB>::T_promote> Mult_AnXBn_Fence
(const SpParMat<IU,NU1,UDERA> & A, const SpParMat<IU,NU2,UDERB> & B )
{
typedef typename promote_trait<NU1,NU2>::T_promote N_promote;
typedef typename promote_trait<UDERA,UDERB>::T_promote DER_promote;
if(A.getncol() != B.getnrow())
{
std::cout<<"Can not multiply, dimensions does not match"<<std::endl;
MPI_Abort(MPI_COMM_WORLD, DIMMISMATCH);
return SpParMat< IU,N_promote,DER_promote >();
}
int stages, Aoffset, Boffset; // stages = inner dimension of matrix blocks
std::shared_ptr<CommGrid> GridC = ProductGrid((A.commGrid).get(), (B.commGrid).get(), stages, Aoffset, Boffset);
std::ofstream oput;
GridC->OpenDebugFile("deb", oput);
const_cast< UDERB* >(B.spSeq)->Transpose();
// set row & col window handles
std::vector<MPI_Win> rowwindows, colwindows;
std::vector<MPI_Win> rowwinnext, colwinnext;
SpParHelper::SetWindows((A.commGrid)->GetRowWorld(), *(A.spSeq), rowwindows);
SpParHelper::SetWindows((B.commGrid)->GetColWorld(), *(B.spSeq), colwindows);
SpParHelper::SetWindows((A.commGrid)->GetRowWorld(), *(A.spSeq), rowwinnext);
SpParHelper::SetWindows((B.commGrid)->GetColWorld(), *(B.spSeq), colwinnext);
IU ** ARecvSizes = SpHelper::allocate2D<IU>(UDERA::esscount, stages);
IU ** BRecvSizes = SpHelper::allocate2D<IU>(UDERB::esscount, stages);
SpParHelper::GetSetSizes( *(A.spSeq), ARecvSizes, (A.commGrid)->GetRowWorld());
SpParHelper::GetSetSizes( *(B.spSeq), BRecvSizes, (B.commGrid)->GetColWorld());
UDERA * ARecv, * ARecvNext;
UDERB * BRecv, * BRecvNext;
std::vector< SpTuples<IU,N_promote> *> tomerge;
// Prefetch first
for(int j=0; j< rowwindows.size(); ++j)
MPI_Win_fence(MPI_MODE_NOPRECEDE, rowwindows[j]);
for(int j=0; j< colwindows.size(); ++j)
MPI_Win_fence(MPI_MODE_NOPRECEDE, colwindows[j]);
for(int j=0; j< rowwinnext.size(); ++j)
MPI_Win_fence(MPI_MODE_NOPRECEDE, rowwinnext[j]);
for(int j=0; j< colwinnext.size(); ++j)
MPI_Win_fence(MPI_MODE_NOPRECEDE, colwinnext[j]);
int Aownind = (0+Aoffset) % stages;
int Bownind = (0+Boffset) % stages;
if(Aownind == (A.commGrid)->GetRankInProcRow())
{
ARecv = A.spSeq; // shallow-copy
}
else
{
std::vector<IU> ess1(UDERA::esscount); // pack essentials to a vector
for(int j=0; j< UDERA::esscount; ++j)
{
ess1[j] = ARecvSizes[j][Aownind];
}
ARecv = new UDERA(); // create the object first
oput << "For A (out), Fetching " << (void*)rowwindows[0] << std::endl;
SpParHelper::FetchMatrix(*ARecv, ess1, rowwindows, Aownind); // fetch its elements later
}
if(Bownind == (B.commGrid)->GetRankInProcCol())
{
BRecv = B.spSeq; // shallow-copy
}
else
{
std::vector<IU> ess2(UDERB::esscount); // pack essentials to a vector
for(int j=0; j< UDERB::esscount; ++j)
{
ess2[j] = BRecvSizes[j][Bownind];
}
BRecv = new UDERB();
oput << "For B (out), Fetching " << (void*)colwindows[0] << std::endl;
SpParHelper::FetchMatrix(*BRecv, ess2, colwindows, Bownind); // No lock version, only get !
}
int Aownprev = Aownind;
int Bownprev = Bownind;
for(int i = 1; i < stages; ++i)
{
Aownind = (i+Aoffset) % stages;
Bownind = (i+Boffset) % stages;
if(i % 2 == 1) // Fetch RecvNext via winnext, fence on Recv via windows
{
if(Aownind == (A.commGrid)->GetRankInProcRow())
{
ARecvNext = A.spSeq; // shallow-copy
}
else
{
std::vector<IU> ess1(UDERA::esscount); // pack essentials to a vector
for(int j=0; j< UDERA::esscount; ++j)
{
ess1[j] = ARecvSizes[j][Aownind];
}
ARecvNext = new UDERA(); // create the object first
oput << "For A, Fetching " << (void*) rowwinnext[0] << std::endl;
SpParHelper::FetchMatrix(*ARecvNext, ess1, rowwinnext, Aownind);
}
if(Bownind == (B.commGrid)->GetRankInProcCol())
{
BRecvNext = B.spSeq; // shallow-copy
}
else
{
std::vector<IU> ess2(UDERB::esscount); // pack essentials to a vector
for(int j=0; j< UDERB::esscount; ++j)
{
ess2[j] = BRecvSizes[j][Bownind];
}
BRecvNext = new UDERB();
oput << "For B, Fetching " << (void*)colwinnext[0] << std::endl;
SpParHelper::FetchMatrix(*BRecvNext, ess2, colwinnext, Bownind); // No lock version, only get !
}
oput << "Fencing " << (void*) rowwindows[0] << std::endl;
oput << "Fencing " << (void*) colwindows[0] << std::endl;
for(int j=0; j< rowwindows.size(); ++j)
MPI_Win_fence(MPI_MODE_NOSTORE, rowwindows[j]); // Synch using "other" windows
for(int j=0; j< colwindows.size(); ++j)
MPI_Win_fence(MPI_MODE_NOSTORE, colwindows[j]);
SpTuples<IU,N_promote> * C_cont = MultiplyReturnTuples<SR>(*ARecv, *BRecv, false, true);
if(!C_cont->isZero())
tomerge.push_back(C_cont);
if(Aownprev != (A.commGrid)->GetRankInProcRow()) delete ARecv;
if(Bownprev != (B.commGrid)->GetRankInProcCol()) delete BRecv;
Aownprev = Aownind;
Bownprev = Bownind;
}
else // fetch to Recv via windows, fence on RecvNext via winnext
{
if(Aownind == (A.commGrid)->GetRankInProcRow())
{
ARecv = A.spSeq; // shallow-copy
}
else
{
std::vector<IU> ess1(UDERA::esscount); // pack essentials to a vector
for(int j=0; j< UDERA::esscount; ++j)
{
ess1[j] = ARecvSizes[j][Aownind];
}
ARecv = new UDERA(); // create the object first
oput << "For A, Fetching " << (void*) rowwindows[0] << std::endl;
SpParHelper::FetchMatrix(*ARecv, ess1, rowwindows, Aownind);
}
if(Bownind == (B.commGrid)->GetRankInProcCol())
{
BRecv = B.spSeq; // shallow-copy
}
else
{
std::vector<IU> ess2(UDERB::esscount); // pack essentials to a vector
for(int j=0; j< UDERB::esscount; ++j)
{
ess2[j] = BRecvSizes[j][Bownind];
}
BRecv = new UDERB();
oput << "For B, Fetching " << (void*)colwindows[0] << std::endl;
SpParHelper::FetchMatrix(*BRecv, ess2, colwindows, Bownind); // No lock version, only get !
}
oput << "Fencing " << (void*) rowwinnext[0] << std::endl;
oput << "Fencing " << (void*) rowwinnext[0] << std::endl;
for(int j=0; j< rowwinnext.size(); ++j)
MPI_Win_fence(MPI_MODE_NOSTORE, rowwinnext[j]); // Synch using "other" windows
for(int j=0; j< colwinnext.size(); ++j)
MPI_Win_fence(MPI_MODE_NOSTORE, colwinnext[j]);
SpTuples<IU,N_promote> * C_cont = MultiplyReturnTuples<SR>(*ARecvNext, *BRecvNext, false, true);
if(!C_cont->isZero())
tomerge.push_back(C_cont);
if(Aownprev != (A.commGrid)->GetRankInProcRow()) delete ARecvNext;
if(Bownprev != (B.commGrid)->GetRankInProcCol()) delete BRecvNext;
Aownprev = Aownind;
Bownprev = Bownind;
}
}
if(stages % 2 == 1) // fence on Recv via windows
{
oput << "Fencing " << (void*) rowwindows[0] << std::endl;
oput << "Fencing " << (void*) colwindows[0] << std::endl;
for(int j=0; j< rowwindows.size(); ++j)
MPI_Win_fence(MPI_MODE_NOSUCCEED, rowwindows[j]); // Synch using "prev" windows
for(int j=0; j< colwindows.size(); ++j)
MPI_Win_fence(MPI_MODE_NOSUCCEED, colwindows[j]);
SpTuples<IU,N_promote> * C_cont = MultiplyReturnTuples<SR>(*ARecv, *BRecv, false, true);
if(!C_cont->isZero())
tomerge.push_back(C_cont);
if(Aownprev != (A.commGrid)->GetRankInProcRow()) delete ARecv;
if(Bownprev != (B.commGrid)->GetRankInProcRow()) delete BRecv;
}
else // fence on RecvNext via winnext
{
oput << "Fencing " << (void*) rowwinnext[0] << std::endl;
oput << "Fencing " << (void*) colwinnext[0] << std::endl;
for(int j=0; j< rowwinnext.size(); ++j)
MPI_Win_fence(MPI_MODE_NOSUCCEED, rowwinnext[j]); // Synch using "prev" windows
for(int j=0; j< colwinnext.size(); ++j)
MPI_Win_fence(MPI_MODE_NOSUCCEED, colwinnext[j]);
SpTuples<IU,N_promote> * C_cont = MultiplyReturnTuples<SR>(*ARecvNext, *BRecvNext, false, true);
if(!C_cont->isZero())
tomerge.push_back(C_cont);
if(Aownprev != (A.commGrid)->GetRankInProcRow()) delete ARecvNext;
if(Bownprev != (B.commGrid)->GetRankInProcRow()) delete BRecvNext;
}
for(int i=0; i< rowwindows.size(); ++i)
{
MPI_Win_free(&rowwindows[i]);
MPI_Win_free(&rowwinnext[i]);
}
for(int i=0; i< colwindows.size(); ++i)
{
MPI_Win_free(&colwindows[i]);
MPI_Win_free(&colwinnext[i]);
}
MPI_Barrier(GridC->GetWorld());
IU C_m = A.spSeq->getnrow();
IU C_n = B.spSeq->getncol();
DER_promote * C = new DER_promote(MergeAll<SR>(tomerge, C_m, C_n), false, NULL); // First get the result in SpTuples, then convert to UDER
for(int i=0; i<tomerge.size(); ++i)
{
delete tomerge[i];
}
SpHelper::deallocate2D(ARecvSizes, UDERA::esscount);
SpHelper::deallocate2D(BRecvSizes, UDERB::esscount);
const_cast< UDERB* >(B.spSeq)->Transpose(); // transpose back to original
return SpParMat<IU,N_promote,DER_promote> (C, GridC); // return the result object
}
}
#endif
|