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 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
|
/*************************************************************************/
/* OPARI Version 1.1 */
/* Copyright (C) 2001 */
/* Forschungszentrum Juelich, Zentralinstitut fuer Angewandte Mathematik */
/*************************************************************************/
#include <vector>
using std::vector;
#include <map>
using std::map;
#include <stack>
using std::stack;
#include <string>
using std::string;
using std::getline;
#include <iostream>
using std::cerr;
#include <fstream>
using std::ifstream;
using std::ofstream;
#include <cstdlib>
using std::exit;
#include <cstring>
using std::strcmp;
#include "handler.h"
#include "ompregion.h"
/*
* global data
*/
namespace {
class File {
public:
File(const string& n, int f, int l, Language la)
: name(n), first(f), last(l), lang(la) {}
string name;
unsigned first, last;
Language lang;
};
typedef map<string, phandler_t> htab;
htab table;
vector<OMPRegion*> regions;
stack<OMPRegion*> regStack;
OMPRegion* atomicRegion = 0;
vector<File*> fileList;
Language lang = L_NA;
bool keepSrcInfo = false;
const char* infile = "";
const char* rcfile = "";
}
bool do_transform = true;
/*
* local utility functions and data
*/
namespace {
enum constructs {
C_NONE = 0x0000,
C_ATOMIC = 0x0001,
C_CRITICAL = 0x0002,
C_MASTER = 0x0004,
C_SINGLE = 0x0008,
C_LOCKS = 0x0010,
C_FLUSH = 0x0020,
C_SYNC = 0x00FF,
C_ALL = 0xFFFF
};
unsigned enabled = C_ALL;
unsigned string2construct(const string& str) {
switch ( str[0] ) {
case 'a': if ( str == "atomic" ) return C_ATOMIC;
break;
case 'c': if ( str == "critical" ) return C_CRITICAL;
break;
case 'f': if ( str == "flush" ) return C_FLUSH;
break;
case 'l': if ( str == "locks" ) return C_LOCKS;
break;
case 'm': if ( str == "master" ) return C_MASTER;
break;
case 's': if ( str == "single" ) return C_SINGLE;
if ( str == "sync" ) return C_SYNC;
break;
}
return C_NONE;
}
unsigned IDused = 0;
int nextID() { return ++IDused; }
void generate_call(const char* event, const char* type, int id, ostream& os) {
if ( lang & L_FORTRAN )
os << " call pomp_" << type << "_" << event << "(" << id << ")\n";
else {
if ( strcmp(event, "begin") == 0 ) os << "{ ";
os << "pomp_" << type << "_" << event << "(&omp_rd_" << id << ");";
if ( strcmp(event, "end") == 0 ) os << " }";
os << "\n";
}
}
void generate_pragma(const char* p, ostream& os) {
if ( lang & L_FORTRAN )
os << "!$omp " << p << "\n";
else
os << "#pragma omp " << p << "\n";
}
void generate_barrier(int n, ostream& os) {
generate_call("enter", "barrier", n, os);
generate_pragma("barrier", os);
generate_call("exit", "barrier", n, os);
}
void print_pragma(OMPragma* p, ostream& os) {
if ( p->lines.size() && keepSrcInfo ) {
// print original source location information reset pragma
os << "#line " << p->lineno << " \"" << infile << "\"" << "\n";
}
// print pragma text
for (unsigned i=0; i<p->lines.size(); ++i) os << p->lines[i] << "\n";
}
void print_pragma_plain(OMPragma* p, ostream& os) {
for (unsigned i=0; i<p->lines.size(); ++i) os << p->lines[i] << "\n";
}
void reset_src_info(OMPragma* p, ostream& os) {
os << "#line " << p->lineno+p->lines.size()
<< " \"" << infile << "\"" << "\n";
}
OMPRegion* REnter(OMPragma* p, int n) {
OMPRegion* r = new OMPRegion(p->name, p->filename, n,
p->lineno, p->lineno+p->lines.size()-1);
regions.push_back(r);
regStack.push(r);
return r;
}
OMPRegion* RTop(OMPragma* p) {
if ( regStack.empty() ) {
cerr << infile << ":" << p->lineno
<< ": ERROR: unbalanced pragma/directive nesting\n";
cleanup_and_exit();
} else {
return regStack.top();
}
return 0;
}
int RExit(OMPragma* p) {
OMPRegion* r = RTop(p);
#if defined(__GNUC__) && (__GNUC__ < 3)
if ( p->name[0] != '$' && p->name.substr(3) != r->name ) {
#else
if ( p->name[0] != '$' && p->name.compare(3, string::npos, r->name) != 0 ) {
#endif
cerr << infile << ":" << r->begin_first_line
<< ": ERROR: missing end" << r->name << " directive for "
<< r->name << " directive\n";
cerr << infile << ":" << p->lineno
<< ": ERROR: non-matching " << p->name
<< " directive\n";
cleanup_and_exit();
}
if ( p->lines.size() ) {
r->end_first_line = p->lineno;
r->end_last_line = p->lineno + p->lines.size() - 1;
} else {
// C/C++ $END$ pragma
r->end_first_line = r->end_last_line = p->lineno - p->pline;
}
regStack.pop();
return r->id;
}
void readResourceFile(const char* inf, const char* rcf) {
ifstream rcs(rcf);
if ( !rcs ) {
IDused = 0;
if ( inf ) fileList.push_back(new File(inf, 1, -1, lang));
} else {
string magic;
getline(rcs, magic);
if ( magic != "<OPARIRC 1.0>" ) {
cerr << "ERROR: wrong magic number for " << rcf << "\n";
cleanup_and_exit();
}
rcs >> IDused;
rcs.ignore(9999, '\n'); // ignore rest of line
string line;
while ( getline(rcs, line) ) {
if ( inf && line == inf ) {
cerr << "WARNING: " << inf << " was already processed\n";
} else {
int f, l, la;
rcs >> f >> l >> la;
fileList.push_back(new File(line, f, l, static_cast<Language>(la)));
}
rcs.ignore(9999, '\n'); // ignore rest of line
}
if ( inf ) fileList.push_back(new File(inf, IDused+1, -1, lang));
}
}
void writeTableFile(const char* tabfile) {
ofstream tabs(tabfile);
if ( !tabs ) {
cerr << "ERROR: cannot open opari table file " << tabfile << "\n";
cleanup_and_exit();
}
OMPRegion::generate_header(tabs);
tabs << "\n";
// For Fortran: include generated .opari.inc files containing the
// descriptors
// For C/C++ : generate extern declaration for descriptor (as they
// are part of the C/C++ file
for (unsigned i=0; i<fileList.size(); ++i) {
if ( fileList[i]->lang & L_FORTRAN ) {
tabs << "#include \"" << fileList[i]->name << ".opari.inc\"\n";
} else if ( unsigned d = fileList[i]->first ) {
for (; d<=fileList[i]->last; ++d)
tabs << "extern struct ompregdescr omp_rd_" << d << ";\n";
}
}
tabs << "\n";
// Generate descriptor table
// Note: might contain holes!
tabs << "int POMP_MAX_ID = " << (IDused+1) << ";\n\n";
tabs << "struct ompregdescr* pomp_rd_table[" << (IDused+1) << "] = {\n";
if ( IDused == 0 ) tabs << " 0,\n"; // hole
int id = -1;
for (unsigned i=0; i<fileList.size(); ++i) {
int d = fileList[i]->first;
if ( d ) {
for (int n=id+1; n<d; ++n) tabs << " 0,\n"; // hole
id = fileList[i]->last;
for (; d<=id; ++d) tabs << " &omp_rd_" << d << ",\n";
}
}
tabs << "};\n";
}
}
/*
* OpenMP pragma transformation functions
*/
namespace {
void h_parallel(OMPragma* p, ostream& os) {
int n = nextID();
/*OMPRegion* r =*/ REnter(p, n);
generate_call("fork", "parallel", n, os);
print_pragma(p, os);
generate_call("begin", "parallel", n, os);
if ( keepSrcInfo ) reset_src_info(p, os);
}
void h_endparallel(OMPragma* p, ostream& os) {
int n = RExit(p);
generate_barrier(n, os);
generate_call("end", "parallel", n, os);
print_pragma(p, os);
generate_call("join", "parallel", n, os);
if ( keepSrcInfo ) reset_src_info(p, os);
}
void h_for(OMPragma* p, ostream& os) {
int n = nextID();
OMPRegion* r = REnter(p, n);
if ( ! p->is_nowait() ) {
p->add_nowait();
r->noWaitAdded = true;
}
generate_call("enter", "for", n, os);
print_pragma(p, os);
}
void h_endfor(OMPragma* p, ostream& os) {
OMPRegion* r = RTop(p);
int n = RExit(p);
if ( r->noWaitAdded ) generate_barrier(n, os);
generate_call("exit", "for", n, os);
if ( keepSrcInfo ) reset_src_info(p, os);
}
void h_do(OMPragma* p, ostream& os) {
int n = nextID();
/*OMPRegion* r =*/ REnter(p, n);
generate_call("enter", "do", n, os);
print_pragma(p, os);
}
void h_enddo(OMPragma* p, ostream& os) {
int n = RExit(p);
if ( p->is_nowait() ) {
print_pragma(p, os);
} else {
p->add_nowait();
print_pragma(p, os);
generate_barrier(n, os);
}
generate_call("exit", "do", n, os);
if ( keepSrcInfo ) reset_src_info(p, os);
}
void h_sections_c(OMPragma* p, ostream& os) {
int n = nextID();
OMPRegion* r = REnter(p, n);
if ( ! p->is_nowait() ) {
p->add_nowait();
r->noWaitAdded = true;
}
generate_call("enter", "sections", n, os);
print_pragma(p, os);
}
void h_section_c(OMPragma* p, ostream& os) {
OMPRegion* r = RTop(p);
OMPRegion* s = new OMPRegion(p->name, p->filename, r->id,
p->lineno, p->lineno+p->lines.size()-1);
regStack.push(s);
if ( r->num_sections ) print_pragma_plain(p, os); else print_pragma(p, os);
generate_call("begin", "section", r->id, os);
if ( keepSrcInfo ) reset_src_info(p, os);
r->num_sections++;
}
void h_endsection_c(OMPragma* p, ostream& os) {
OMPRegion* r = RTop(p);
generate_call("end", "section", r->id, os);
if ( keepSrcInfo ) reset_src_info(p, os);
regStack.pop();
}
void h_endsections_c(OMPragma* p, ostream& os) {
OMPRegion* r = RTop(p);
int n = RExit(p);
if ( r->noWaitAdded ) generate_barrier(n, os);
generate_call("exit", "sections", n, os);
if ( keepSrcInfo ) reset_src_info(p, os);
}
void h_sections(OMPragma* p, ostream& os) {
int n = nextID();
/*OMPRegion* r =*/ REnter(p, n);
generate_call("enter", "sections", n, os);
print_pragma(p, os);
}
void h_section(OMPragma* p, ostream& os) {
OMPRegion* r = RTop(p);
if ( r->num_sections ) {
// close last section if necessary
generate_call("end", "section", r->id, os);
}
print_pragma(p, os);
generate_call("begin", "section", r->id, os);
if ( keepSrcInfo ) reset_src_info(p, os);
++(r->num_sections);
}
void h_endsections(OMPragma* p, ostream& os) {
int n = RExit(p);
generate_call("end", "section", n, os);
if ( p->is_nowait() ) {
print_pragma(p, os);
} else {
p->add_nowait();
print_pragma(p, os);
generate_barrier(n, os);
}
generate_call("exit", "sections", n, os);
if ( keepSrcInfo ) reset_src_info(p, os);
}
void h_single_c(OMPragma* p, ostream& os) {
int n = nextID();
OMPRegion* r = REnter(p, n);
if ( ! p->is_nowait() ) {
p->add_nowait();
r->noWaitAdded = true;
}
generate_call("enter", "single", n, os);
print_pragma(p, os);
generate_call("begin", "single", n, os);
if ( keepSrcInfo ) reset_src_info(p, os);
}
void h_endsingle_c(OMPragma* p, ostream& os) {
OMPRegion* r = RTop(p);
int n = RExit(p);
generate_call("end", "single", n, os);
if ( r->noWaitAdded ) generate_barrier(n, os);
generate_call("exit", "single", n, os);
if ( keepSrcInfo ) reset_src_info(p, os);
}
void h_single(OMPragma* p, ostream& os) {
int n = nextID();
/*OMPRegion* r =*/ REnter(p, n);
generate_call("enter", "single", n, os);
print_pragma(p, os);
generate_call("begin", "single", n, os);
if ( keepSrcInfo ) reset_src_info(p, os);
}
void h_endsingle(OMPragma* p, ostream& os) {
int n = RExit(p);
generate_call("end", "single", n, os);
if ( p->is_nowait() ) {
print_pragma(p, os);
} else {
p->add_nowait();
print_pragma(p, os);
generate_barrier(n, os);
}
generate_call("exit", "single", n, os);
if ( keepSrcInfo ) reset_src_info(p, os);
}
void h_master(OMPragma* p, ostream& os) {
int n = nextID();
/*OMPRegion* r =*/ REnter(p, n);
print_pragma(p, os);
generate_call("begin", "master", n, os);
if ( keepSrcInfo ) reset_src_info(p, os);
}
void h_endmaster_c(OMPragma* p, ostream& os) {
int n = RExit(p);
generate_call("end", "master", n, os);
if ( keepSrcInfo ) reset_src_info(p, os);
}
void h_endmaster(OMPragma* p, ostream& os) {
int n = RExit(p);
generate_call("end", "master", n, os);
print_pragma(p, os);
}
void h_critical(OMPragma* p, ostream& os) {
int n = nextID();
OMPRegion* r = REnter(p, n);
r->sub_name = p->find_sub_name();
generate_call("enter", "critical", n, os);
print_pragma(p, os);
generate_call("begin", "critical", n, os);
if ( keepSrcInfo ) reset_src_info(p, os);
}
void h_endcritical(OMPragma* p, ostream& os) {
OMPRegion* r = RTop(p);
int n = RExit(p);
if ( p->name[0] != '$' ) {
string cname = p->find_sub_name();
if ( cname != r->sub_name ) {
cerr << infile << ":" << r->begin_first_line
<< ": ERROR: missing end critical(" << r->sub_name
<< ") directive\n";
cerr << infile << ":" << p->lineno
<< ": ERROR: non-matching end critical(" << cname
<< ") directive\n";
cleanup_and_exit();
}
}
generate_call("end", "critical", n, os);
print_pragma(p, os);
generate_call("exit", "critical", n, os);
if ( keepSrcInfo ) reset_src_info(p, os);
}
void h_parallelfor(OMPragma* p, ostream& os) {
int n = nextID();
/*OMPRegion* r =*/ REnter(p, n);
OMPragma* forPragma = p->split_combined();
forPragma->add_nowait();
generate_call("fork", "parallel", n, os);
print_pragma(p, os); // #omp parallel
generate_call("begin", "parallel", n, os);
generate_call("enter", "for", n, os);
print_pragma(forPragma, os); // #omp for nowait
delete forPragma;
}
void h_endparallelfor(OMPragma* p, ostream& os) {
int n = RExit(p);
generate_barrier(n, os);
generate_call("exit", "for", n, os);
generate_call("end", "parallel", n, os);
generate_call("join", "parallel", n, os);
if ( keepSrcInfo ) reset_src_info(p, os);
}
void h_paralleldo(OMPragma* p, ostream& os) {
int n = nextID();
/*OMPRegion* r =*/ REnter(p, n);
generate_call("fork", "parallel", n, os);
OMPragma* doPragma = p->split_combined();
print_pragma(p, os); // #omp parallel
generate_call("begin", "parallel", n, os);
generate_call("enter", "do", n, os);
print_pragma(doPragma, os); // #omp do
delete doPragma;
}
void h_endparalleldo(OMPragma* p, ostream& os) {
int n = RExit(p);
generate_pragma("end do nowait", os);
generate_barrier(n, os);
generate_call("exit", "do", n, os);
generate_call("end", "parallel", n, os);
generate_pragma("end parallel", os);
generate_call("join", "parallel", n, os);
if ( keepSrcInfo ) reset_src_info(p, os);
}
void h_parallelsections_c(OMPragma* p, ostream& os) {
int n = nextID();
/*OMPRegion* r =*/ REnter(p, n);
OMPragma* secPragma = p->split_combined();
secPragma->add_nowait();
generate_call("fork", "parallel", n, os);
print_pragma(p, os); // #omp parallel
generate_call("begin", "parallel", n, os);
generate_call("enter", "sections", n, os);
print_pragma(secPragma, os); // #omp sections
delete secPragma;
}
void h_endparallelsections_c(OMPragma* p, ostream& os) {
int n = RExit(p);
generate_barrier(n, os);
generate_call("exit", "sections", n, os);
generate_call("end", "parallel", n, os);
generate_call("join", "parallel", n, os);
if ( keepSrcInfo ) reset_src_info(p, os);
}
void h_parallelsections(OMPragma* p, ostream& os) {
int n = nextID();
/*OMPRegion* r =*/ REnter(p, n);
generate_call("fork", "parallel", n, os);
OMPragma* secPragma = p->split_combined();
print_pragma(p, os); // #omp parallel
generate_call("begin", "parallel", n, os);
generate_call("enter", "sections", n, os);
print_pragma(secPragma, os); // #omp sections
delete secPragma;
}
void h_endparallelsections(OMPragma* p, ostream& os) {
int n = RExit(p);
generate_call("end", "section", n, os);
generate_pragma("end sections nowait", os);
generate_barrier(n, os);
generate_call("exit", "sections", n, os);
generate_call("end", "parallel", n, os);
generate_pragma("end parallel", os);
generate_call("join", "parallel", n, os);
if ( keepSrcInfo ) reset_src_info(p, os);
}
void h_barrier(OMPragma* p, ostream& os) {
int n = nextID();
OMPRegion* r = new OMPRegion(p->name, p->filename, n,
p->lineno, p->lineno+p->lines.size()-1);
regions.push_back(r);
generate_call("enter", "barrier", n, os);
print_pragma(p, os);
generate_call("exit", "barrier", n, os);
if ( keepSrcInfo ) reset_src_info(p, os);
}
void h_flush(OMPragma* p, ostream& os) {
int n = nextID();
OMPRegion* r = new OMPRegion(p->name, p->filename, n,
p->lineno, p->lineno+p->lines.size()-1);
regions.push_back(r);
generate_call("enter", "flush", n, os);
print_pragma(p, os);
generate_call("exit", "flush", n, os);
if ( keepSrcInfo ) reset_src_info(p, os);
}
void h_atomic(OMPragma* p, ostream& os) {
int n = nextID();
OMPRegion* r = new OMPRegion(p->name, p->filename, n,
p->lineno, p->lineno+p->lines.size()-1);
regions.push_back(r);
generate_call("enter", "atomic", n, os);
print_pragma(p, os);
atomicRegion = r;
}
/*2.0*/
void h_workshare(OMPragma* p, ostream& os) {
int n = nextID();
/*OMPRegion* r =*/ REnter(p, n);
generate_call("enter", "workshare", n, os);
print_pragma(p, os);
}
/*2.0*/
void h_endworkshare(OMPragma* p, ostream& os) {
int n = RExit(p);
if ( p->is_nowait() ) {
print_pragma(p, os);
generate_call("exit", "workshare", n, os);
} else {
p->add_nowait();
print_pragma(p, os);
generate_barrier(n, os);
generate_call("exit", "workshare", n, os);
}
if ( keepSrcInfo ) reset_src_info(p, os);
}
/*2.0*/
void h_parallelworkshare(OMPragma* p, ostream& os) {
int n = nextID();
/*OMPRegion* r =*/ REnter(p, n);
generate_call("fork", "parallel", n, os);
OMPragma* wsPragma = p->split_combined();
print_pragma(p, os); // #omp parallel
generate_call("begin", "parallel", n, os);
generate_call("enter", "workshare", n, os);
print_pragma(wsPragma, os); // #omp workshare
delete wsPragma;
}
/*2.0*/
void h_endparallelworkshare(OMPragma* p, ostream& os) {
int n = RExit(p);
generate_pragma("end workshare nowait", os);
generate_barrier(n, os);
generate_call("exit", "workshare", n, os);
generate_call("end", "parallel", n, os);
generate_pragma("end parallel", os);
generate_call("join", "parallel", n, os);
if ( keepSrcInfo ) reset_src_info(p, os);
}
/*INST*/
void h_inst(OMPragma* p, ostream& os) {
if ( lang & L_FORTRAN )
os << " call pomp_" << p->name.substr(4) << "()\n";
else
os << "pomp_" << p->name.substr(4) << "();\n";
if ( keepSrcInfo ) reset_src_info(p, os);
}
/*INST*/
void h_instbegin(OMPragma* p, ostream& os) {
int n = nextID();
OMPRegion* r = REnter(p, n);
r->name = "region";
r->sub_name = p->find_sub_name();
if ( lang & L_FORTRAN )
os << " call pomp_begin(" << n << ")\n";
else
os << "pomp_begin(&omp_rd_" << n << ");\n";
if ( keepSrcInfo ) reset_src_info(p, os);
}
/*INST*/
void h_instaltend(OMPragma* p, ostream& os) {
OMPRegion* r = RTop(p);
string cname = p->find_sub_name();
if ( cname != r->sub_name ) {
cerr << infile << ":" << r->begin_first_line
<< ": ERROR: missing inst end(" << r->sub_name
<< ") pragma/directive\n";
cerr << infile << ":" << p->lineno
<< ": ERROR: non-matching inst end(" << cname
<< ") pragma/directive\n";
cleanup_and_exit();
}
if ( lang & L_FORTRAN )
os << " call pomp_end(" << r->id << ")\n";
else
os << "pomp_end(&omp_rd_" << r->id << ");\n";
if ( keepSrcInfo ) reset_src_info(p, os);
}
/*INST*/
void h_instend(OMPragma* p, ostream& os) {
p->name = "endregion";
OMPRegion* r = RTop(p);
string cname = p->find_sub_name();
int n = RExit(p);
if ( cname != r->sub_name ) {
cerr << infile << ":" << r->begin_first_line
<< ": ERROR: missing inst end(" << r->sub_name
<< ") pragma/directive\n";
cerr << infile << ":" << p->lineno
<< ": ERROR: non-matching inst end(" << cname
<< ") pragma/directive\n";
cleanup_and_exit();
}
if ( lang & L_FORTRAN )
os << " call pomp_end(" << n << ")\n";
else
os << "pomp_end(&omp_rd_" << n << ");\n";
if ( keepSrcInfo ) reset_src_info(p, os);
}
/*INST*/
void h_instrument(OMPragma* p, ostream& os) {
do_transform = true;
if ( keepSrcInfo ) reset_src_info(p, os);
}
/*INST*/
void h_noinstrument(OMPragma* p, ostream& os) {
do_transform = false;
if ( keepSrcInfo ) reset_src_info(p, os);
}
void h_cxx_end(OMPragma* p, ostream& os) {
if ( atomicRegion ) {
extra_handler(p->lineno-p->pline, os);
} else {
OMPRegion* r = RTop(p);
phandler_t handler = find_handler("end"+r->name);
handler(p, os);
}
}
}
/*
* External interface functions
*/
void init_handler(const char* inf, const char* rcf, Language l, bool g) {
// remember environment
lang = l;
keepSrcInfo = g;
infile = inf;
rcfile = rcf;
readResourceFile(inf, rcf);
// init handler table
if ( lang & L_FORTRAN ) {
table["do"] = h_do;
table["enddo"] = h_enddo;
table["workshare"] = h_workshare; /*2.0*/
table["endworkshare"] = h_endworkshare; /*2.0*/
table["sections"] = h_sections;
table["section"] = h_section;
table["endsections"] = h_endsections;
if ( enabled & C_SINGLE ) {
table["single"] = h_single;
table["endsingle"] = h_endsingle;
}
if ( enabled & C_MASTER ) {
table["master"] = h_master;
table["endmaster"] = h_endmaster;
}
table["paralleldo"] = h_paralleldo;
table["endparalleldo"] = h_endparalleldo;
table["parallelsections"] = h_parallelsections;
table["endparallelsections"] = h_endparallelsections;
table["parallelworkshare"] = h_parallelworkshare; /*2.0*/
table["endparallelworkshare"] = h_endparallelworkshare; /*2.0*/
} else {
table["for"] = h_for;
table["endfor"] = h_endfor;
table["sections"] = h_sections_c;
table["section"] = h_section_c;
table["endsection"] = h_endsection_c;
table["endsections"] = h_endsections_c;
if ( enabled & C_SINGLE ) {
table["single"] = h_single_c;
table["endsingle"] = h_endsingle_c;
}
if ( enabled & C_MASTER ) {
table["master"] = h_master; // F version OK here
table["endmaster"] = h_endmaster_c; // but not here
}
table["parallelfor"] = h_parallelfor;
table["endparallelfor"] = h_endparallelfor;
table["parallelsections"] = h_parallelsections_c;
table["endparallelsections"] = h_endparallelsections_c;
table["$END$"] = h_cxx_end;
}
table["parallel"] = h_parallel;
table["endparallel"] = h_endparallel;
if ( enabled & C_CRITICAL ) {
table["critical"] = h_critical;
table["endcritical"] = h_endcritical;
}
table["barrier"] = h_barrier;
if ( enabled & C_FLUSH ) {
table["flush"] = h_flush;
}
if ( enabled & C_ATOMIC ) {
table["atomic"] = h_atomic;
}
table["instinit"] = h_inst; /*INST*/
table["instfinalize"] = h_inst; /*INST*/
table["inston"] = h_inst; /*INST*/
table["instoff"] = h_inst; /*INST*/
table["instbegin"] = h_instbegin; /*INST*/
table["instaltend"] = h_instaltend; /*INST*/
table["instend"] = h_instend; /*INST*/
table["instrument"] = h_instrument; /*INST*/
table["noinstrument"] = h_noinstrument; /*INST*/
}
void finalize_handler(const char* incfile, const char* tabfile) {
// check region stack
if ( ! regStack.empty() ) {
cerr << "ERROR: unbalanced pragma/directive nesting\n";
cleanup_and_exit();
}
// update information about current file in file list
File* myFile = fileList.back();
if ( myFile->first > IDused ) {
myFile->first = 0;
myFile->last = 0;
} else {
myFile->last = IDused;
}
// update resource file
ofstream rcs(rcfile);
if ( !rcs ) {
cerr << "ERROR: cannot open opari resource file " << rcfile << "\n";
exit(1);
}
rcs << "<OPARIRC 1.0>\n";
rcs << IDused << "\n";
for (unsigned i=0; i<fileList.size(); ++i) {
rcs << fileList[i]->name << "\n";
rcs << fileList[i]->first << " "
<< fileList[i]->last << " "
<< static_cast<int>(fileList[i]->lang) << "\n";
}
// generate opari include file
ofstream incs(incfile);
if ( !incs ) {
cerr << "ERROR: cannot open opari include file " << incfile << "\n";
exit(1);
}
if ( regions.size() ) {
if ( lang & L_C_OR_CXX ) OMPRegion::generate_header(incs);
for (unsigned i=0; i<regions.size(); ++i)
regions[i]->generate_descr(incs);
}
// generate opari table file
if ( tabfile ) writeTableFile(tabfile);
// free file list
for (unsigned i=0; i<fileList.size(); ++i) {
delete fileList[i];
}
}
void generateTableFile(const char* rcf, const char* tabfile) {
rcfile = rcf;
readResourceFile(0, rcf);
// generate opari table file
writeTableFile(tabfile);
// free file list
for (unsigned i=0; i<fileList.size(); ++i) {
delete fileList[i];
}
}
phandler_t find_handler(const string& pragma) {
htab::iterator it = table.find(pragma);
if ( it != table.end() )
return it->second;
else
return print_pragma;
}
void extra_handler(int lineno, ostream& os) {
if ( atomicRegion ) {
atomicRegion->end_first_line = lineno;
atomicRegion->end_last_line = lineno;
generate_call("exit", "atomic", atomicRegion->id, os);
if ( keepSrcInfo ) {
os << "#line " << (lineno+1) << " \"" << infile << "\"" << "\n";
}
atomicRegion = 0;
}
}
bool set_disabled(const string& constructs) {
string::size_type pos = 0 ;
string::size_type last = 0 ;
unsigned c;
string s;
do {
pos = constructs.find_first_of(",", last);
if ( pos < string::npos ) {
s = constructs.substr(last, pos-last);
if ( (c = string2construct(s)) == C_NONE ) {
cerr << "ERROR: unknown value \'" << s << "\' for option -disable\n";
return true;
} else {
enabled &= ~c;
}
last = pos + 1;
}
} while ( pos < string::npos );
s = constructs.substr(last);
if ( (c = string2construct(s)) == C_NONE ) {
cerr << "ERROR: unknown value \'" << s << "\' for option -disable\n";
return true;
} else {
enabled &= ~c;
}
return false;
}
bool instrument_locks() {
return enabled & C_LOCKS;
}
|