Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0-size allocations for owned fields #25

Merged
merged 2 commits into from
Feb 8, 2024
Merged

0-size allocations for owned fields #25

merged 2 commits into from
Feb 8, 2024

Conversation

awnawab
Copy link
Collaborator

@awnawab awnawab commented Feb 6, 2024

Throughout the IFS, we initialize 0-sized owned fields (a workaround for when a particular field is meant to be disabled). Whilst these 0-sized allocations are safe using the Fortran ALLOCATE statement, they cause errors in C_MALLOC. This PR adds safety checks so that 0-sized allocs can be handled safely using Fortran ALLOCATE/DEALLOCATE.

We need C_MALLOC because there is an upper limit on registering Fortran allocated memory in page-locked memory (which is lower than the OS limit).

@pmarguinaud
Copy link
Collaborator

Hello Ahmad,

Why do not you do something like this in c_malloc ?

void c_malloc (size_t siz, void ** ptr)
{
  if (siz == 0)
    siz = 1;
  *ptr = malloc (siz);
}

This would avoid all the logic with SAFE_ALLOC.

@awnawab
Copy link
Collaborator Author

awnawab commented Feb 8, 2024

Thanks a lot for the feedback @pmarguinaud! When I tried your suggestion, I was getting "free invalid pointer" errors. These probably arise from when we cast the c pointer as a Fortran pointer. Sadly I think we have to do the allocation/deallocation directly using Fortran for 0-sized allocs. My original implementation was admittedly a little clumsy, I have simplified it now and have reverted the change to FIELD_RANKSUFF_MODULE and FIELD_BASIC_MODULE. Please take a look at it again when you have a few spare moments 😄

@pmarguinaud pmarguinaud merged commit a8009e9 into main Feb 8, 2024
7 checks passed
@awnawab awnawab deleted the naan-safe-alloc branch February 13, 2024 08:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants