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

Remove strcat_alloc() #4020

Closed
jngrad opened this issue Nov 28, 2020 · 2 comments · Fixed by #4069
Closed

Remove strcat_alloc() #4020

jngrad opened this issue Nov 28, 2020 · 2 comments · Fixed by #4069

Comments

@jngrad
Copy link
Member

jngrad commented Nov 28, 2020

Utils::strcat_alloc() appends a C-string to a char array with manual memory management. This utility function is one of the last consumers of the ESPResSo manual memory management toolset (#2900), the others being EK and LB that are currently being rewritten in the waLBerla branch.

Here is a typical use of Utils::strcat_alloc():

double p3m_mc_time(char **log, const int mesh[3], int cao, ...) {
  char buffer[200];
  sprintf(buffer, "%-4d %-3d cao too large for this mesh\n", mesh[0], cao);
  *log = strcat_alloc(*log, buffer);
}

The same can be achieved with a std::string:

#include <string>
double p3m_mc_time(std::string &log, const int mesh[3], int cao, ...) {
  char buffer[200];
  sprintf(buffer, "%-4d %-3d cao too large for this mesh\n", mesh[0], cao);
  log += buffer;
}

Affected files: p3m.cpp, p3m-dipolar.cpp, mmm1d.cpp to remove calls to Utils::strcat_alloc(), and electrostatics.pxd, magnetostatics.pxd to replace char ** log by string &log from libcpp.string. After that, the utility function and its unit test can be removed.

The sprintf() statements do not need to be modified. They have two modern std::string equivalents, but they cannot be used out-of-the-box: std::format is a C++20 feature, boost::format uses a different syntax. Anyway, these sprintf() calls are found in functions that are probably going to be rewritten in Python in the future, so further beautification isn't worth it.

@fweik
Copy link
Contributor

fweik commented Nov 29, 2020

Shouldn't it first be discussed if a string log is really the best way to report back from the tuning?

@jngrad
Copy link
Member Author

jngrad commented Nov 30, 2020

I would be fine with a logger or a printf. Also, there are 2 memory leaks with the tuning code, but that shouldn't surprise anyone...

diff --git a/src/core/electrostatics_magnetostatics/p3m.cpp b/src/core/electrostatics_magnetostatics/p3m.cpp
index 70918abbc..139cf58d5 100644
--- a/src/core/electrostatics_magnetostatics/p3m.cpp
+++ b/src/core/electrostatics_magnetostatics/p3m.cpp
@@ -1140,4 +1140,12 @@ int p3m_adaptive_tune(char **log) {
           time_best);
   *log = strcat_alloc(*log, b);
+  constexpr size_t N = 500*1024*1024;
+  char buffer[N];
+  for (size_t i = 0; i < N; ++i)
+    buffer[i] = '\n';
+  buffer[N-1] = '\0';
+  printf("(allocating %.0f MB in the core)\n", sizeof(buffer) / 1024. / 1024.);
+  *log = strcat_alloc(*log, buffer);
+  (*log)[0] = '\0';
   return ES_OK;
 }
import os, psutil, espressomd.electrostatics
process = psutil.Process(os.getpid())
ram = lambda: print(f'{process.memory_info().rss / 1024. / 1024.:.0f} MB')
system = espressomd.System(box_l=3*[1], time_step=0.01)
system.part.add(pos=[[0,0,0],[1,0,0]], q=[-1,1])
ram()  # should print 88 MB
p3m = espressomd.electrostatics.P3M(prefactor=1.0, accuracy=1e-2)
system.actors.add(p3m)  # start tuning
ram()  # should print 98 MB

Output:

87 MB
(allocating 500 MB in the core)
1097 MB

@reinaual reinaual self-assigned this Dec 17, 2020
@kodiakhq kodiakhq bot closed this as completed in #4069 Dec 23, 2020
kodiakhq bot added a commit that referenced this issue Dec 23, 2020
Fixes #4020

Description of changes:
- replaced manual string `char*` construct with `std::printf`
- introduced verbose parameter to touched tuning functions and interfaces
- removed `utils/strcat_alloc.hpp` with unit tests
- added missing `handle_errors` call in p3m_gpu_init
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants