Skip to content

Updates for C-language output#53

Merged
yantosca merged 15 commits intodevfrom
c-lang
Jun 28, 2022
Merged

Updates for C-language output#53
yantosca merged 15 commits intodevfrom
c-lang

Conversation

@yantosca
Copy link
Copy Markdown
Contributor

@yantosca yantosca commented Jun 27, 2022

This PR fixes several issues that were preventing KPP from generating C-language output code. Namely:

  • Change /* */ comment headers to //, which reads much more clearly

  • Restored and updated the drv/general.c and drv/general_adj.c driver programs

  • Added updates to gen.c:

    • Where the _Function file is generated, we now wrap Fortran90 optional arguments with an if ( useLang == F90_LANG ). This prevents a compiler error when the target language is C.
    • Where Vdot is added to _Function, we need to loop over elements of the Vdot array if the target language is C. We can only use array notation for Fortran90.
  • Added updates to code_c.c:

    • In routine C_FunctionStart, set fncPrototipe=0. This will declare each C-language function so that arrays will have dimensions included in the function interface. This will avoid a compilation error.
  • I have also reorganized the ./ci_tests subfolders.

    • Each subfolder begins with either C_ or F90_, to denote the target language. (The minimum version test folder has been renamed to X_minver so that it will come last.
    • Also, the names of all subfolders in ci-tests are now specified in a single file that is sourced by the other CI test scripts. This will make it easier to add new CI tests.

NOTE: At present, the C-language code fails when trying to use a mechanism other than small_strato. I think this is due to a Makefile issue that happens when e.g. Hessian is turned off but the Makefile looks for the Hessian files. I can implement a similar fix as was done for Fortran90.

yantosca added 7 commits June 27, 2022 11:30
src/code.c
- Update author list

src/code_c.c
- Update bprintf call to print out //-style commments

Signed-off-by: Bob Yantosca <[email protected]>
drv/general.c
- C-language driver program

drv/general_adj.c
- C-language adjoint driver program

Signed-off-by: Bob Yantosca <[email protected]>
src/code.c
- In WriteDelim, extend the number of ~ characters printed

src/code_c.c
- Fix error in previous commit in call to bprintf, remove the
  2nd parameter (LINE_LENGTH) and add a \n to the format.

Signed-off-by: Bob Yantosca <[email protected]>
src/gen.c
- When generating the _Function file, wrap Fortran90 optional arguments
  in an "if ( useLang == F90_LANG )".  This prevents a compiler error
  when the target language is C
- In the equation for Vdot, we need to loop over elements of the Vdot
  array if the target language is C.  We can only use array notation
  for Fortran90.
- Trim trailing whitespace

src/code_c.c
- In routine C_FunctionStart, set fncPrototipe=0.  This will declare each
  C-language function so that arrays will have dimensions included in
  the function interface.  This will avoid a compilation error.
- Trim trailing whitespace

Signed-off-by: Bob Yantosca <[email protected]>
drv/general.c
drv/general_adj.c
- Add more comments for clarity
- Trimmed trailing whitespace

Signed-off-by: Bob Yantosca <[email protected]>
We have renamed the continuous integration test folders in ci_tests
so that each test begins with either a C_ or F90_, to denote the
target language of KPP-generated code.  The minimum version test has
been renamed to X_minver.

./ci-pipelines/ci-testing-list.sh
- Added the names of all the CI test folders in this file, which is
  source-d by the other scripts.  The list of folders is stored in
  the ALL_TESTS bash variable.

./ci-pipelines/ci-manual-cleanup-script.sh
./ci-pipelines/ci-manual-testing-script.sh
./ci-pipelines/ci-testing-script.sh
- These scripts now issue a "source ./ci-testing-list.sh" command in
  order to get the ALL_TESTS variable (which contains the list of
  CI test folders).

Signed-off-by: Bob Yantosca <[email protected]>
This commit brings the updates from the "cleanup_util" and "dev"
branches branch (plus some documentation updates) into the C-language
development steream.

Resolved conflicts in:
(1) .ci-pipelines/ci-manual-cleanup-script.sh
(2) .ci-pipelines/ci-manual-testing-script.sh
(3) .ci-pipelines/ci-testing-script.sh

Signed-off-by: Bob Yantosca <[email protected]>
@yantosca yantosca added code / structural Related to structural source code updates target-languages Related to the language options for KPP-generated code labels Jun 27, 2022
@yantosca yantosca self-assigned this Jun 27, 2022
@yantosca yantosca added this to the 3.0.0 milestone Jun 27, 2022
yantosca added 3 commits June 27, 2022 17:07
…gets

util/Makefile_f90
util/Makefile_upper_f90
- Remove *.o and *.mod files instead of KPP_ROOT*.o and KPP_ROOT*.mod
  in the clean and distclean targets.  This will prevent issues where
  files are not removed due to case-insensitivity.

Signed-off-by: Bob Yantosca <[email protected]>
./ci-tests/F90_ros_autoreduce
- Renamed (was originally ros_autoreduce)

./ci-pipelines/ci-testing/list.sh
- Add F90_ros_autoreduce to list of tests

drv/general_autoreduce
- Set ICNTRL(15) = -1 to prevent rate constants from being updated
  within the integration step.  This slows the autoreduce code down.

Signed-off-by: Bob Yantosca <[email protected]>
util/Makefile_c
- Typo: Remove KPP_ROOT*.map (was KPP_ROOT_*.map)

util/Makefile.f90
util/Makefile_upper_F90
- Now remove *.o and *.mod in clean and distclean targets, in order
  to avoid uppercase/lowercase ambiguity

Signed-off-by: Bob Yantosca <[email protected]>
@RolfSander
Copy link
Copy Markdown
Contributor

Looks good.

Thanks for given the CI tests a nice structure!

Copy link
Copy Markdown
Contributor Author

Still debugging a seg fault when I try to build a larger mechanism than small_strato. It seems to be in C_InitDeclare when we pass it the EQN_NAMES. Will dig into this a bit.

@yantosca
Copy link
Copy Markdown
Contributor Author

gdb output says:

Program received signal SIGSEGV, Segmentation fault.
0x00002aaaaaf60f76 in __mempcpy_sse2 () from /lib64/libc.so.6
(gdb) where
#0  0x00002aaaaaf60f76 in __mempcpy_sse2 () from /lib64/libc.so.6
#1  0x00002aaaaaf4e06c in _IO_default_xsputn () from /lib64/libc.so.6
#2  0x00002aaaaaf1e033 in vfprintf () from /lib64/libc.so.6
#3  0x00002aaaaaf424cb in vsprintf () from /lib64/libc.so.6
#4  0x0000000000401b55 in bprintf (fmt=0x425ce4 "\"%s\"") at code.c:202
#5  0x0000000000404878 in C_InitDeclare (v=90, n=235, values=0x7ffffffda350)
    at code_c.c:333
#6  0x000000000040dd4d in GenerateMonitorData () at gen.c:525
#7  0x000000000041998c in Generate () at gen.c:3629
#8  0x000000000041c66e in main (argc=2, argv=0x7fffffffb938) at kpp.c:640

@yantosca
Copy link
Copy Markdown
Contributor Author

@RolfSander, I think I see what is going on. In routine C_InitDeclare I put in a debug statement that only executes when the variable EQN_NAMES is passed:

  // debug
  if ( v==90 ) {
    printf("\n");
    for (i=0; i<n; i++) printf( "\n%d, %s\n", i, cval[i] );
    exit(1);
  }

which gives this output:

0,               NO2 --> O3P + NO                                                                                                                                                                                                                                                                                 O3P + AIR + O2 --> O3                                                                                                                                                                                                                                                                                             O3P + O3 --> 2 O2                                                                                                                                                                                                                                                                                     O3P + NO + AIR --> NO2                                                                                                                                                                                                                                                                                           O3P + NO2 --> NO                                                                                                                                                                                                                                                                                            O3P + NO2 --> NO3                                                                                                                                                                                                                                                                                             O3 + NO --> NO2

so for each index i, the cval[i] contains the list of equations from i to the end of the file. This is causing the seg fault as we eventually run out of memory.

This is called from GenerateMonitorData in gen.c here:

  p = bufeqn;
  for (i = 0; i < EqnNr; i++) {
    EqnString(i, p);
    seqn[i] = p;
    p += MAX_EQNLEN;
  }
  InitDeclare( EQN_NAMES, EqnNr, (void*)seqn );

If you have any ideas then let me know. Thanks!

@RolfSander
Copy link
Copy Markdown
Contributor

I'm sorry but I don't know how to solve this problem.

Maybe the easiest quick&dirty fix is to deactive EQN_NAMES for the C
language? I think that would be okay because I don't even know a single
KPP user who creates output in C.

@yantosca
Copy link
Copy Markdown
Contributor Author

yantosca commented Jun 28, 2022

Found a solution!

  switch( var->type ) {
    case VELM: bprintf( "  %s  %s[] = {\n%5s", C_types[var->baseType], var->name, " " );
               for( i = 0; i < n; i++ ) {
                 switch( var->baseType ) {
                   case INT:
		     bprintf( "%3d",  ival[i] );
		     maxCols=12;
		     break;
                   case DOUBLE:
                   case REAL:
		     bprintf( "%5lg", dval[i] );
		     maxCols=8;
		     break;
                   case STRING:
		     bprintf( "\"%s\"", cval[i] );
		     maxCols=8;
		     break;
		   case DOUBLESTRING:
		     // Bug fix: To avoid a segmentation fault, copy the
		     // string from cval[i] to string variable thisEqn
		     // before passing it to bprintf.
		     //  -- Bob Yantosca (28 Jun 2022)
		     strncpy( thisEqn, cval[i], MAX_EQNLEN+1 );
		     bprintf( "\"%s\"", thisEqn );
		     maxCols=1;
		     break;
                 }

yantosca added 4 commits June 28, 2022 14:45
In routine C_InitDeclare, in the switch statement for VELM and
DOUBLESTRING, we first copy the cval[i] to a temporary string variable
before passing that to bprintf.  This avoids a segmentation fault.

Also removed some leftover debug printout.

Signed-off-by: Bob Yantosca <[email protected]>
This is needed in order to generate C-language code for the
models/saprc99.eqn or models/saprcnov.eqn mechanisms.

Signed-off-by: Bob Yantosca <[email protected]>
drv/general.c
- Use floating point variables for TSTART and TEND
- Fixed a typo ("3600/0" should be "3600.0")

drv/general_adj.c
- Use floating point constants 100.0 and 3600.0

models/saprcnov.def
- Remove tabs
- Trim trailing whitespace
- Align entries in INLINE blocks

Signed-off-by: Bob Yantosca <[email protected]>
./ci-pipelines/ci-testing-list.sh
- Added C_rosadj to the ALL_TESTS variable

./ci_tests/C_rk/C_rk.kpp
- Bug fix: #LANGUAGE should be C, not Fortran90

./ci-tests/C_rosadj/C_rosadj.kpp
- Added KPP definition file for the rosenbrock_adj test in C

Signed-off-by: Bob Yantosca <[email protected]>
@yantosca yantosca marked this pull request as ready for review June 28, 2022 18:58
@yantosca
Copy link
Copy Markdown
Contributor Author

I've since fixed the issue above. For now I only use the small_strato mechanism for all the C-language integration tests. KPP will now generate code with the other mechanisms (e.g. saprcnov.eqn) though. Good enough for the sake of continuity and for the KPP 3.0.0 version.

@yantosca yantosca requested review from RolfSander and jimmielin June 28, 2022 19:01
Copy link
Copy Markdown
Contributor

@RolfSander RolfSander left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great that you found a solution. Yes, go ahead and merge!

@yantosca yantosca merged commit a8ec3be into dev Jun 28, 2022
@yantosca yantosca deleted the c-lang branch June 28, 2022 21:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

code / structural Related to structural source code updates target-languages Related to the language options for KPP-generated code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants