-
Notifications
You must be signed in to change notification settings - Fork 14
Introduce ModEM getrusage module to record high-water memory mark #44
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
Open
MiCurry
wants to merge
1
commit into
magnetotellurics:main
Choose a base branch
from
MiCurry:modem-getrusage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| #include <sys/resource.h> | ||
| #include <stdio.h> | ||
|
|
||
| /* It should be noted that the below function might have different | ||
| * results depending on the system you are on. POSIX doesn't guarantee | ||
| * ru_maxrss even be implemented. | ||
| * | ||
| * It's on most systems, but your results may very. | ||
| */ | ||
|
|
||
| /* | ||
| * To call this C function from Fortran, use this interface: | ||
| * | ||
| * interface | ||
| * subroutine get_maxrss() result(maxrss) bind(c) | ||
| * use iso_c_binding, only : c_long | ||
| * integer (c_long), intent(out) :: maxrss | ||
| * end subroutine get_maxrss | ||
| * end interface | ||
| */ | ||
| void get_maxrss(long *maxrss_bytes) { | ||
|
|
||
| int conversion; | ||
| struct rusage usage; | ||
|
|
||
| #if defined(__APPLE_) || defined(__MACH__) | ||
| // BSD's ru_maxrss (used by Apple) is in bytes | ||
| conversion = 1.0; | ||
| #elif __linux__ | ||
| // Linux's ru_maxrss is in KiB | ||
| conversion = 1000.0; | ||
| #else | ||
| conversion = 1.0; | ||
| #endif | ||
|
|
||
| getrusage(RUSAGE_SELF, &usage); | ||
| *maxrss_bytes = usage.ru_maxrss / conversion; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| ! module ModEM_memory | ||
| ! | ||
| ! Use getrusage to track memory high-water mark. | ||
| ! | ||
| ! This module contains helpful routines to call and | ||
| ! log the results of ru_maxrss by calling the | ||
| ! getrusage system call. | ||
| ! | ||
| ! The public routines print ru_maxrss, which is | ||
| ! a high-water mark of memory. More information on | ||
| ! getrusage can be found by in `man 2 getrusage`. | ||
| ! | ||
|
|
||
| module ModEM_memory | ||
|
|
||
| use utilities | ||
| use iso_c_binding, only : c_long | ||
| #ifdef MPI | ||
| use mpi | ||
| #endif | ||
| use ModEM_logger | ||
|
|
||
| implicit none | ||
|
|
||
| private | ||
|
|
||
| integer :: task_id | ||
|
|
||
| public ModEM_memory_print_report | ||
| public ModEM_memory_log_report | ||
| #ifdef MPI | ||
| public ModEM_memory_get_all | ||
| #endif | ||
|
|
||
| contains | ||
|
|
||
| subroutine ModEM_memory_create_log_fname() | ||
|
|
||
| end subroutine | ||
|
|
||
| subroutine ModEM_memory_get_maxrss(maxrss_bytes) | ||
|
|
||
| implicit none | ||
|
|
||
| integer, intent(out) :: maxrss_bytes | ||
| integer (c_long) :: maxrss_bytes_c | ||
|
|
||
| #ifdef MODEM_MEMORY | ||
| interface | ||
| subroutine get_maxrss(maxrss_bytes) bind(c) | ||
| use iso_c_binding, only : c_long | ||
| integer (c_long), intent(out) :: maxrss_bytes | ||
| end subroutine get_maxrss | ||
| end interface | ||
|
|
||
| call get_maxrss(maxrss_bytes_c) | ||
| maxrss_bytes = maxrss_bytes_c | ||
| #else | ||
| write(0,*) "ERROR: ModEM was not built with the ModEM_getrusage.c file!" | ||
| write(0,*) "ERROR: In order to use ModEM_memory routines you must" | ||
| write(0,*) "ERROR: build ModEM with CMake and specify '-DMODEM_MEMORY=on'" | ||
| call ModEM_abort() | ||
| #endif | ||
|
|
||
| end subroutine ModEM_memory_get_maxrss | ||
|
|
||
| subroutine ModEM_memory_print_report(message) | ||
|
|
||
| implicit none | ||
|
|
||
| integer :: maxrss | ||
| character (len=*), intent(in) :: message | ||
| real :: maxrss_bytes, maxrss_kb, maxrss_mb, maxrss_gb | ||
| character (len=*), parameter :: LOG_MSG_FMT = "(I4.4, A, A, A, F18.1, A, F18.1, A, F18.1, A)" | ||
| character (len=512) :: log_message | ||
|
|
||
| call ModEM_memory_get_maxrss(maxrss) | ||
| call ModEM_memory_convert_maxrss(maxrss, maxrss_kb, maxrss_mb, maxrss_gb) | ||
|
|
||
| write(log_message, LOG_MSG_FMT) task_id, ' - ', trim(message), ", ", maxrss_kb, ' kb ', maxrss_mb, ' mb ', maxrss_gb, ' gb' | ||
| write(6,*) trim(log_message) | ||
|
|
||
| end subroutine ModEM_memory_print_report | ||
|
|
||
| subroutine ModEM_memory_convert_maxrss(maxrss_bytes, maxrss_kb, maxrss_mb, maxrss_gb) | ||
|
|
||
| implicit none | ||
|
|
||
| integer :: maxrss_bytes | ||
| real, intent(out) :: maxrss_kb | ||
| real, intent(out) :: maxrss_mb | ||
| real, intent(out) :: maxrss_gb | ||
|
|
||
| maxrss_kb = maxrss_bytes / 1.0 | ||
| maxrss_mb = maxrss_kb / 1000.0 | ||
| maxrss_gb = maxrss_mb / 1000.1 | ||
|
|
||
| end subroutine ModEM_memory_convert_maxrss | ||
|
|
||
| subroutine ModEM_memory_log_report(message) | ||
|
|
||
| implicit none | ||
|
|
||
| character (len=*), intent(in) :: message | ||
| character (len=*), parameter :: LOG_MSG_FMT = "(A, A, F18.1, A, F18.1, A, F18.1, A)" | ||
| integer :: maxrss | ||
| real :: maxrss_kb, maxrss_mb, maxrss_gb | ||
| character (len=512) :: log_message | ||
|
|
||
| call ModEM_memory_get_maxrss(maxrss) | ||
|
|
||
| call ModEM_memory_convert_maxrss(maxrss, maxrss_kb, maxrss_mb, maxrss_gb) | ||
| write(log_message, LOG_MSG_FMT) trim(message), ", ", maxrss_kb, ' kb ', maxrss_mb, ' mb ', maxrss_gb, ' gb' | ||
| call ModEM_log(log_message, mainOnly=.false., flush_log=.true.) | ||
|
|
||
| end subroutine ModEM_memory_log_report | ||
|
|
||
| #ifdef MPI | ||
| subroutine ModEM_memory_get_all(message) | ||
|
|
||
| implicit none | ||
|
|
||
| character (len=*), intent(in) :: message | ||
| character (len=*), parameter :: LOG_MSG_FMT = "(A, A, F18.1, A, F18.1, A, F18.1, A)" | ||
| character (len=512) :: log_message | ||
|
|
||
| integer :: maxrss, global_maxrss | ||
| real :: maxrss_kb, maxrss_mb, maxrss_gb | ||
|
|
||
| call ModEM_memory_get_maxrss(maxrss) | ||
|
|
||
| call MPI_reduce(maxrss, global_maxrss, 1, MPI_INTEGER, MPI_SUM, 0, MPI_COMM_WORLD, ierr) | ||
|
|
||
| if (taskid == 0) then | ||
| call ModEM_memory_convert_maxrss(global_maxrss, maxrss_kb, maxrss_mb, maxrss_gb) | ||
| write(log_message, LOG_MSG_FMT) trim(message), ", ", maxrss_kb, ' kb ', maxrss_mb, ' mb ', maxrss_gb, ' gb' | ||
| call ModEM_log(log_message, mainOnly=.false., flush_log=.true.) | ||
| end if | ||
|
|
||
| end subroutine ModEM_memory_get_all | ||
| #endif | ||
|
|
||
| end module ModEM_memory |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need to double check this conversion is right...