18 #include "boost/tokenizer.hpp"
22 #include "Version.hpp"
55 #ifndef AD_FORTRAN_SPECIFIC
75 flags(0), nDerivOrder(0), prefix(
"ad_"), debugLevel(0), quiet(false),
76 msgContext(
"ADIC"), openMPflag(false), sparsehandlingflag(false),
77 myStaticAnnotationsFlag(false), max_axpy(0),myTemplateSgFile_p(0), adolcflag(false)
85 std::string a_mapModel, std::string a_headerFileName,
199 StringList::iterator iter;
201 if((fname.find(*iter)!=std::string::npos ))
242 std::string str(psz);
293 std::string disclaimer;
294 std::ostringstream disc;
296 struct tm* ptm = localtime(&t);
297 char szTodayDate[50];
298 strftime(szTodayDate, 50,
"%m/%d/%y %H:%M:%S", ptm);
301 #ifndef AD_FORTRAN_SPECIFIC
302 <<
"/************************** DISCLAIMER ********************************/\n"
304 <<
"/* This file was generated on " << szTodayDate
305 <<
" by */\n" <<
"/* ADIC version "
309 <<
"/* ADIC was prepared as an account of work sponsored by an */\n"
310 <<
"/* agency of the United States Government and the University of */\n"
311 <<
"/* Chicago. NEITHER THE AUTHOR(S), THE UNITED STATES GOVERNMENT */\n"
312 <<
"/* NOR ANY AGENCY THEREOF, NOR THE UNIVERSITY OF CHICAGO, INCLUDING */\n"
313 <<
"/* ANY OF THEIR EMPLOYEES OR OFFICERS, MAKES ANY WARRANTY, EXPRESS */\n"
314 <<
"/* OR IMPLIED, OR ASSUMES ANY LEGAL LIABILITY OR RESPONSIBILITY FOR */\n"
315 <<
"/* THE ACCURACY, COMPLETENESS, OR USEFULNESS OF ANY INFORMATION OR */\n"
316 <<
"/* PROCESS DISCLOSED, OR REPRESENTS THAT ITS USE WOULD NOT INFRINGE */\n"
317 <<
"/* PRIVATELY OWNED RIGHTS. */\n"
319 <<
"/**********************************************************************/\n";
321 <<
"! ************************** DISCLAIMER *************************\n"
322 <<
"! This file was generated on " << szTodayDate <<
" by something \n"
323 <<
"! ***************************************************************\n";
326 disclaimer = disc.str();
330 #ifdef AD_FORTRAN_SPECIFIC
332 static SgType* ourActiveType_p(0);
334 return ourActiveType_p;
336 const char * name=
"active";
338 SgClassDefinition* classDefinition =
new SgClassDefinition();
339 classDefinition->setCaseInsensitive(
true);
340 SgDerivedTypeStatement* classDeclaration =
new SgDerivedTypeStatement(name,SgClassDeclaration::e_struct,NULL,classDefinition);
341 classDeclaration->set_definingDeclaration(classDeclaration);
342 SgDerivedTypeStatement* nondefiningClassDeclaration =
new SgDerivedTypeStatement(name,SgClassDeclaration::e_struct,NULL,NULL);
343 nondefiningClassDeclaration->set_parent(scope);
344 if (nondefiningClassDeclaration->get_type () == NULL)
345 nondefiningClassDeclaration->set_type (SgClassType::createType(nondefiningClassDeclaration));
346 classDeclaration->set_type(nondefiningClassDeclaration->get_type());
347 classDeclaration->set_firstNondefiningDeclaration(nondefiningClassDeclaration);
348 nondefiningClassDeclaration->set_firstNondefiningDeclaration(nondefiningClassDeclaration);
349 nondefiningClassDeclaration->set_definingDeclaration(classDeclaration);
350 nondefiningClassDeclaration->setForward();
351 classDefinition->set_declaration(classDeclaration);
352 classDeclaration->set_scope(scope);
353 nondefiningClassDeclaration->set_scope(scope);
354 classDeclaration->set_parent(scope);
355 SgClassSymbol* classSymbol =
new SgClassSymbol(nondefiningClassDeclaration);
356 scope->insert_symbol(name,classSymbol);
358 return ourActiveType_p=classDeclaration->get_type();
369 std::ostringstream buf;
377 buf <<
"/*! \\file\n"
379 <<
" \\brief Preaccumulation macros for dense gradient arrays.\n"
381 <<
" Copyright (c) 2004--2011, UChicago Argonne, LLC <br>\n"
382 <<
" All rights reserved. <br>\n"
383 <<
" See $ROSE2XAIF_DIR/Copyright.txt for details. <br>\n"
386 <<
"#ifndef __AD_GRAD_SAXPY_N_DENSE_H\n"
387 <<
"#define __AD_GRAD_SAXPY_N_DENSE_H\n"
388 <<
"\n#ifndef ADIC_GRAD_LENGTH";
390 buf <<
"\n#include \"ad_types.h\"";
392 buf <<
"\n#include \"ad_types.hpp\"";
394 <<
"\n#include \"ad_grad_length_types.h\""
398 <<
"#ifdef __cplusplus\n"
399 <<
"extern \"C\" {\n"
402 for (i = 1; i <= this->
max_axpy; ++i) {
404 buf <<
"#define ADIC_Sax_" << i <<
"( \\" << std::endl;
405 for (j = 1; j <= i; ++j) {
406 buf <<
"\t\t\t__adic_a" << j <<
", __adic_x" << j;
407 if (j < i) buf <<
", \\" << std::endl;
409 buf <<
", __adic_tgt) {\\" << std::endl;
411 buf <<
"\tint __adic_iCtr; double *__adic_gradz = DERIV_grad(__adic_tgt), \\" << std::endl;
412 for (j = 1; j <= i; ++j) {
413 buf <<
"\t\t\t*__adic_grad" << j <<
" = DERIV_grad(__adic_x" << j <<
")";
414 if (j < i) buf <<
", \\" << std::endl;
415 else buf <<
"; \\" << std::endl;
418 buf <<
"\tfor (__adic_iCtr = 0; __adic_iCtr < __ADIC_GradSize(); __adic_iCtr++) { \\" << std::endl;
419 buf <<
"\t\t__adic_gradz[__adic_iCtr] = ";
420 for (j = 1; j <= i; ++j) {
421 buf <<
"(__adic_a" << j <<
") * __adic_grad" << j <<
"[__adic_iCtr]";
422 if (j < i) buf <<
" + \\" << std::endl <<
"\t\t\t";
423 else buf <<
"; \\" << std::endl;
425 buf <<
"\t} \\" << std::endl;
427 buf <<
"}" << std::endl << std::endl;
432 buf <<
"#ifdef __cplusplus" << std::endl <<
"} /* closing brace for extern \"C\" */" << std::endl;
433 buf <<
"#endif" << std::endl <<
"#endif /* #ifndef __AD_GRAD_SAXPY_N_DENSE_H */" << std::endl;
435 hfile.open(
"ad_grad_saxpy-n_dense.h");
447 std::ostringstream buf;
455 buf <<
"/*! \\file\n"
457 <<
" \\brief Preaccumulation macros for dense gradient arrays.\n"
459 <<
" Copyright (c) 2004--2011, UChicago Argonne, LLC <br>\n"
460 <<
" All rights reserved. <br>\n"
461 <<
" See $ROSE2XAIF_DIR/Copyright.txt for details. <br>\n"
464 <<
"#ifndef __AD_GRAD_SAXPY_N_SPARSE_H\n"
465 <<
"#define __AD_GRAD_SAXPY_N_SPARSE_H\n"
466 <<
"\n#ifdef ADIC_SPARSE_NO_GRAD"
467 <<
"\n#include \"noderiv_sparslinc.h\""
468 <<
"\n#include \"noderiv_sparsderiv.hpp\""
470 <<
"\n#ifdef ADIC_SPARSE"
471 <<
"\n#include \"sparslinc.h\""
472 <<
"\n#include \"sparsderiv.hpp\""
475 <<
"#ifdef __cplusplus\n"
476 <<
"extern \"C\" {\n"
479 for (i = 1; i <= this->
max_axpy; ++i) {
481 buf <<
"#define ADIC_Sax_" << i <<
"( \\" << std::endl;
482 for (j = 1; j <= i; ++j) {
483 buf <<
"\t\t\t__adic_a" << j <<
", __adic_x" << j;
484 if (j < i) buf <<
", \\" << std::endl;
486 buf <<
", __adic_tgt) {\\" << std::endl;
488 buf<<
"\t(__adic_tgt).indexSet = (__adic_x1).indexSet; \\" << std::endl;
491 buf<<
"\tstd::set<int>::iterator srcSetIter; \\" << std::endl;
493 for (j = 2; j <= i; ++j) {
494 buf <<
"\tsrcSetIter = (__adic_x" << j <<
").indexSet.begin(); \\" << std::endl;
495 buf <<
"\tfor (; srcSetIter != (__adic_x" << j <<
").indexSet.end(); srcSetIter++) { \\" << std::endl;
496 buf <<
"\t\t(__adic_tgt).indexSet.insert(*srcSetIter); \\" << std::endl;
497 buf <<
"\t} \\" << std::endl;
500 buf <<
"}" << std::endl << std::endl;
504 buf <<
"#ifdef __cplusplus" << std::endl <<
"} /* closing brace for extern \"C\" */" << std::endl;
505 buf <<
"#endif" << std::endl <<
"#endif /* #ifndef __AD_GRAD_SAXPY_N_SPARSE_H */" << std::endl;
507 hfile.open(
"ad_grad_saxpy-n_sparse.h");
515 std::ostringstream buf;
523 buf <<
"/*! \\file\n"
525 <<
" \\brief Registration of ADOL-C external functions.\n"
527 <<
" Copyright (c) 2004--2011, UChicago Argonne, LLC <br>\n"
528 <<
" All rights reserved. <br>\n"
529 <<
" See $ROSE2XAIF_DIR/Copyright.txt for details. <br>\n"
532 <<
"#ifndef __AD_REGISTER_EXTERNAL_FUNCTIONS_H\n"
533 <<
"#define __AD_REGISTER_EXTERNAL_FUNCTIONS_H\n"
534 <<
"\n#include <adolc/externfcts.h>"
535 <<
"\n#include \"ad_types.hpp\""
539 buf <<
"int "<<(*iter)<<
"(int n,double *yin,int m,double *yout); " << std::endl;
540 buf <<
"int ad_"<<(*iter)<<
"(int n,DERIV_TYPE *yin,int m,DERIV_TYPE *yout); " << std::endl;
541 buf <<
"ADOLC_ext_fct zosf_"<<(*iter)<<
"; " << std::endl;
542 buf <<
"ADOLC_ext_fct_fos_forward fosf_"<<(*iter)<<
"; " << std::endl;
543 buf <<
"ADOLC_ext_fct_fov_forward fovf_"<<(*iter)<<
"; " << std::endl;
545 buf <<
"ext_diff_fct* reg_ext_fct_"<<(*iter)<<
"(int n, int m);" << std::endl;
549 buf <<
"#endif /* #ifndef __AD_REGISTER_EXTERNAL_FUNCTIONS_H */" << std::endl;
551 hfile.open(
"ad_register_external_functions.h");
559 std::ostringstream buf;
560 buf <<
"int zosf_"<<functionname<<
"(int n, double *x, int m, double *y){ " << std::endl;
561 buf <<
"\t"<<
"return "<<functionname<<
"(n, x, m, y);"<< std::endl;
562 buf <<
"}" << std::endl << std::endl;
569 std::ostringstream buf;
570 buf <<
"int fosf_"<<functionname<<
"(int n, double *dp_x, double *dp_X, int m, double *dp_y, double *dp_Y){ " << std::endl;
571 buf <<
"\t"<<
"double **dpp_X = &dp_X;"<< std::endl;
572 buf <<
"\t"<<
"double **dpp_Y = &dp_Y;"<< std::endl;
573 buf <<
"\t"<<
"fovf_"<<functionname<<
"(n, dp_x, 1, dpp_X, m, dp_y, dpp_Y);"<< std::endl;
574 buf <<
"\t"<<
"return 0;"<< std::endl;
575 buf <<
"}" << std::endl << std::endl;
582 std::ostringstream buf;
583 buf <<
"int fovf_"<<functionname<<
"(int n, double *dp_x, int p, double **dpp_X, int m, double *dp_y, double **dpp_Y){ " << std::endl;
584 buf <<
"\t"<<
"int i,j;" << std::endl;
585 buf <<
"\t"<<
"ADIC_Init(p);" << std::endl;
586 buf <<
"\t"<<
"DERIV_TYPE *ad_x, *ad_y;" << std::endl;
587 buf <<
"\t"<<
"ad_x = new DERIV_TYPE[n]; " << std::endl;
588 buf <<
"\t"<<
"ad_y = new DERIV_TYPE[m]; " << std::endl;
589 buf <<
"\t"<<
"// Set indpendent variables " << std::endl;
590 buf <<
"\t"<<
"ADIC_SetForwardMode();" << std::endl;
591 buf <<
"\t"<<
"ADIC_SetIndepArrayFromSeed(ad_x,n,dpp_X);" << std::endl;
592 buf <<
"\t"<<
"ADIC_SetDepArray(ad_y,m);" << std::endl;
593 buf <<
"\t"<<
"ADIC_SetIndepDone();" << std::endl;
594 buf <<
"\t"<<
"// Initialize the value of the independent variable ad_x" << std::endl;
595 buf <<
"\t"<<
"for(i = 0; i < n; i++)" << std::endl;
596 buf <<
"\t"<<
"\tDERIV_val(ad_x[i]) = dp_x[i];" << std::endl;
597 buf <<
"\t"<<
"for(i = 0; i < m; i++)" << std::endl;
598 buf <<
"\t"<<
"\tDERIV_val(ad_y[i]) = 0.0;" << std::endl;
599 buf <<
"\t"<<
"// Invoke AD function " << std::endl;
600 buf <<
"\t"<<
"ad_"<<functionname<<
"(n, ad_x, m, ad_y);" << std::endl;
601 buf <<
"\t"<<
"for(i = 0; i < m; i++)" << std::endl;
602 buf <<
"\t"<<
"\tdp_y[i] = DERIV_val(ad_y[i]);" << std::endl;
603 buf <<
"\t"<<
"for(i = 0; i < m; i++)" << std::endl;
604 buf <<
"\t"<<
"\tfor(j = 0; j < p ; j++)" << std::endl;
605 buf <<
"\t"<<
"\t\tdpp_Y[i][j] = DERIV_grad(ad_y[i])[j];" << std::endl;
606 buf <<
"\t"<<
"delete []ad_y;" << std::endl;
607 buf <<
"\t"<<
"delete []ad_x;" << std::endl;
608 buf <<
"\t"<<
"ADIC_Finalize();" << std::endl;
609 buf <<
"\t"<<
"return 0;" << std::endl;
610 buf <<
"}" << std::endl << std::endl;
617 std::ostringstream buf;
618 buf <<
"ext_diff_fct * reg_ext_fct_"<<functionname<<
"(int n, int m){ " << std::endl;
619 buf <<
"\tdouble *yp, *ynewp, *ydp, *ynewdp, **ydpp, **ynewdpp;" << std::endl;
620 buf <<
"\t// register external function and set up pointers" << std::endl;
621 buf <<
"\text_diff_fct *edf = reg_ext_fct("<<functionname<<
");"<< std::endl;
622 buf <<
"\t// information for Zero-Order-Scalar (=zos) forward"<< std::endl;
623 buf <<
"\t// allocate memory for edf structure from n and m"<< std::endl;
624 buf <<
"\typ = myalloc1(n);"<< std::endl;
625 buf <<
"\tynewp = myalloc1(m);"<< std::endl;
627 buf <<
"\tydp = myalloc1(n);"<< std::endl;
628 buf <<
"\tynewdp = myalloc1(m);"<< std::endl;
630 buf <<
"\tydpp = myalloc2(n,n); // second number is number of tangent directions"<< std::endl;
631 buf <<
"\tynewdpp = myalloc2(m,n);"<< std::endl;
633 buf <<
"\tedf->zos_forward = zosf_"<<functionname<<
";"<< std::endl;
634 buf <<
"\tedf->dp_x = yp;"<< std::endl;
635 buf <<
"\tedf->dp_y = ynewp;"<< std::endl;
636 buf <<
"\t// information for First-Order-Scalar (=fos) forward"<< std::endl;
637 buf <<
"\tedf->fos_forward = fosf_"<<functionname<<
";"<< std::endl;
638 buf <<
"\tedf->dp_X = ydp;"<< std::endl;
639 buf <<
"\tedf->dp_Y = ynewdp;"<< std::endl;
640 buf <<
"\t// information for First-Order-Vector (=fov) forward"<< std::endl;
641 buf <<
"\tedf->fov_forward = fovf_"<<functionname<<
";"<< std::endl;
642 buf <<
"\tedf->dpp_X = ydpp;"<< std::endl;
643 buf <<
"\tedf->dpp_Y = ynewdpp;"<< std::endl;
644 buf <<
"\treturn edf;"<< std::endl;
645 buf <<
"}" << std::endl << std::endl;
653 std::ostringstream buf;
661 buf <<
"/*! \\file\n"
663 <<
" \\brief Registration of ADOL-C external functions.\n"
665 <<
" Copyright (c) 2004--2011, UChicago Argonne, LLC <br>\n"
666 <<
" All rights reserved. <br>\n"
667 <<
" See $ROSE2XAIF_DIR/Copyright.txt for details. <br>\n"
672 buf <<
"#include \"ad_types.hpp\"" <<std::endl;
673 buf <<
"#include \"ad_register_external_functions.h\"" <<std::endl;
674 buf <<
"#include <adolc/adalloc.h>" <<std::endl;
683 hfile.open(
"ad_register_external_functions.c");