From 5e63860cc2f61d86e1b2ccd443a67bf75614d755 Mon Sep 17 00:00:00 2001
From: "Kurt A. O'Hearn" <ohearnk@msu.edu>
Date: Fri, 2 Feb 2018 14:27:58 -0500
Subject: [PATCH] sPuReMD: more memory management changes.

---
 sPuReMD/src/init_md.c | 114 ++++++++++++++++++++++++++++--------------
 sPuReMD/src/lin_alg.c |   7 +--
 sPuReMD/src/lookup.c  |  18 ++++---
 sPuReMD/src/restart.c |  44 +++++++++++-----
 4 files changed, 122 insertions(+), 61 deletions(-)

diff --git a/sPuReMD/src/init_md.c b/sPuReMD/src/init_md.c
index 96243efe..3e814309 100644
--- a/sPuReMD/src/init_md.c
+++ b/sPuReMD/src/init_md.c
@@ -345,22 +345,33 @@ void Init_Workspace( reax_system *system, control_params *control,
     if ( control->cm_solver_pre_comp_type == ICHOLT_PC ||
             control->cm_solver_pre_comp_type == ILUT_PAR_PC )
     {
-        workspace->droptol  = (real *) calloc( system->N_cm, sizeof( real ) );
+        workspace->droptol = (real *) scalloc( system->N_cm, sizeof( real ),
+                "Init_Workspace::workspace->droptol" );
     }
     //TODO: check if unused
-    //workspace->w        = (real *) calloc( cm_lin_sys_size, sizeof( real ) );
+    //workspace->w        = (real *) scalloc( cm_lin_sys_size, sizeof( real ),
+    //"Init_Workspace::workspace->droptol" );
     //TODO: check if unused
-    workspace->b        = (real *) calloc( system->N_cm * 2, sizeof( real ) );
-    workspace->b_s      = (real *) calloc( system->N_cm, sizeof( real ) );
-    workspace->b_t      = (real *) calloc( system->N_cm, sizeof( real ) );
-    workspace->b_prc    = (real *) calloc( system->N_cm * 2, sizeof( real ) );
-    workspace->b_prm    = (real *) calloc( system->N_cm * 2, sizeof( real ) );
-    workspace->s        = (real**) calloc( 5, sizeof( real* ) );
-    workspace->t        = (real**) calloc( 5, sizeof( real* ) );
+    workspace->b = (real *) scalloc( system->N_cm * 2, sizeof( real ),
+            "Init_Workspace::workspace->b" );
+    workspace->b_s = (real *) scalloc( system->N_cm, sizeof( real ),
+            "Init_Workspace::workspace->b_s" );
+    workspace->b_t = (real *) scalloc( system->N_cm, sizeof( real ),
+            "Init_Workspace::workspace->b_t" );
+    workspace->b_prc = (real *) scalloc( system->N_cm * 2, sizeof( real ),
+            "Init_Workspace::workspace->b_prc" );
+    workspace->b_prm = (real *) scalloc( system->N_cm * 2, sizeof( real ),
+            "Init_Workspace::workspace->b_prm" );
+    workspace->s = (real**) scalloc( 5, sizeof( real* ),
+            "Init_Workspace::workspace->s" );
+    workspace->t = (real**) scalloc( 5, sizeof( real* ),
+            "Init_Workspace::workspace->t" );
     for ( i = 0; i < 5; ++i )
     {
-        workspace->s[i] = (real *) calloc( system->N_cm, sizeof( real ) );
-        workspace->t[i] = (real *) calloc( system->N_cm, sizeof( real ) );
+        workspace->s[i] = (real *) scalloc( system->N_cm, sizeof( real ),
+                "Init_Workspace::workspace->s[i]" );
+        workspace->t[i] = (real *) scalloc( system->N_cm, sizeof( real ),
+                "Init_Workspace::workspace->t[i]" );
     }
 
     switch ( control->charge_method )
@@ -422,40 +433,62 @@ void Init_Workspace( reax_system *system, control_params *control,
         /* GMRES storage */
         case GMRES_S:
         case GMRES_H_S:
-            workspace->y  = (real *)  calloc( control->cm_solver_restart + 1, sizeof( real ) );
-            workspace->z  = (real *)  calloc( control->cm_solver_restart + 1, sizeof( real ) );
-            workspace->g  = (real *)  calloc( control->cm_solver_restart + 1, sizeof( real ) );
-            workspace->h  = (real **) calloc( control->cm_solver_restart + 1, sizeof( real*) );
-            workspace->hs = (real *)  calloc( control->cm_solver_restart + 1, sizeof( real ) );
-            workspace->hc = (real *)  calloc( control->cm_solver_restart + 1, sizeof( real ) );
-            workspace->rn = (real **) calloc( control->cm_solver_restart + 1, sizeof( real*) );
-            workspace->v  = (real **) calloc( control->cm_solver_restart + 1, sizeof( real*) );
+            workspace->y = (real *) scalloc( control->cm_solver_restart + 1, sizeof( real ),
+                    "Init_Workspace::workspace->y" );
+            workspace->z = (real *) scalloc( control->cm_solver_restart + 1, sizeof( real ),
+                    "Init_Workspace::workspace->z" );
+            workspace->g = (real *) scalloc( control->cm_solver_restart + 1, sizeof( real ),
+                    "Init_Workspace::workspace->g" );
+            workspace->h = (real **) scalloc( control->cm_solver_restart + 1, sizeof( real*),
+                    "Init_Workspace::workspace->h" );
+            workspace->hs = (real *) scalloc( control->cm_solver_restart + 1, sizeof( real ),
+                    "Init_Workspace::workspace->hs" );
+            workspace->hc = (real *) scalloc( control->cm_solver_restart + 1, sizeof( real ),
+                    "Init_Workspace::workspace->hc" );
+            workspace->rn = (real **) scalloc( control->cm_solver_restart + 1, sizeof( real*),
+                    "Init_Workspace::workspace->rn" );
+            workspace->v = (real **) scalloc( control->cm_solver_restart + 1, sizeof( real*),
+                    "Init_Workspace::workspace->v" );
 
             for ( i = 0; i < control->cm_solver_restart + 1; ++i )
             {
-                workspace->h[i]  = (real *) calloc( control->cm_solver_restart + 1, sizeof( real ) );
-                workspace->rn[i] = (real *) calloc( system->N_cm * 2, sizeof( real ) );
-                workspace->v[i]  = (real *) calloc( system->N_cm, sizeof( real ) );
+                workspace->h[i] = (real *) scalloc( control->cm_solver_restart + 1, sizeof( real ),
+                        "Init_Workspace::workspace->h[i]" );
+                workspace->rn[i] = (real *) scalloc( system->N_cm * 2, sizeof( real ),
+                        "Init_Workspace::workspace->rn[i]" );
+                workspace->v[i] = (real *) scalloc( system->N_cm, sizeof( real ),
+                        "Init_Workspace::workspace->v[i]" );
             }
 
-            workspace->r = (real *) calloc( system->N_cm, sizeof( real ) );
-            workspace->d = (real *) calloc( system->N_cm, sizeof( real ) );
-            workspace->q = (real *) calloc( system->N_cm, sizeof( real ) );
-            workspace->p = (real *) calloc( system->N_cm, sizeof( real ) );
+            workspace->r = (real *) scalloc( system->N_cm, sizeof( real ),
+                    "Init_Workspace::workspace->r" );
+            workspace->d = (real *) scalloc( system->N_cm, sizeof( real ),
+                    "Init_Workspace::workspace->d" );
+            workspace->q = (real *) scalloc( system->N_cm, sizeof( real ),
+                    "Init_Workspace::workspace->q" );
+            workspace->p = (real *) scalloc( system->N_cm, sizeof( real ),
+                    "Init_Workspace::workspace->p" );
             break;
 
         /* CG storage */
         case CG_S:
-            workspace->r = (real *) calloc( system->N_cm, sizeof( real ) );
-            workspace->d = (real *) calloc( system->N_cm, sizeof( real ) );
-            workspace->q = (real *) calloc( system->N_cm, sizeof( real ) );
-            workspace->p = (real *) calloc( system->N_cm, sizeof( real ) );
+            workspace->r = (real *) scalloc( system->N_cm, sizeof( real ),
+                    "Init_Workspace::workspace->r" );
+            workspace->d = (real *) scalloc( system->N_cm, sizeof( real ),
+                    "Init_Workspace::workspace->d" );
+            workspace->q = (real *) scalloc( system->N_cm, sizeof( real ),
+                    "Init_Workspace::workspace->q" );
+            workspace->p = (real *) scalloc( system->N_cm, sizeof( real ),
+                    "Init_Workspace::workspace->p" );
             break;
 
         case SDM_S:
-            workspace->r = (real *) calloc( system->N_cm, sizeof( real ) );
-            workspace->d = (real *) calloc( system->N_cm, sizeof( real ) );
-            workspace->q = (real *) calloc( system->N_cm, sizeof( real ) );
+            workspace->r = (real *) scalloc( system->N_cm, sizeof( real ),
+                    "Init_Workspace::workspace->r" );
+            workspace->d = (real *) scalloc( system->N_cm, sizeof( real ),
+                    "Init_Workspace::workspace->d" );
+            workspace->q = (real *) scalloc( system->N_cm, sizeof( real ),
+                    "Init_Workspace::workspace->q" );
             break;
 
         default:
@@ -480,8 +513,10 @@ void Init_Workspace( reax_system *system, control_params *control,
     /* storage for analysis */
     if ( control->molec_anal || control->diffusion_coef )
     {
-        workspace->mark = (int *) calloc( system->N, sizeof(int) );
-        workspace->old_mark = (int *) calloc( system->N, sizeof(int) );
+        workspace->mark = (int *) scalloc( system->N, sizeof(int),
+                "Init_Workspace::workspace->mark" );
+        workspace->old_mark = (int *) scalloc( system->N, sizeof(int),
+                "Init_Workspace::workspace->old_mark" );
     }
     else
     {
@@ -490,7 +525,8 @@ void Init_Workspace( reax_system *system, control_params *control,
 
     if ( control->diffusion_coef )
     {
-        workspace->x_old = (rvec *) calloc( system->N, sizeof( rvec ) );
+        workspace->x_old = (rvec *) scalloc( system->N, sizeof( rvec ),
+                "Init_Workspace::workspace->x_old" );
     }
     else
     {
@@ -563,8 +599,10 @@ void Init_Lists( reax_system *system, control_params *control,
 
     Generate_Neighbor_Lists(system, control, data, workspace, lists, out_control);
     Htop = 0;
-    hb_top = (int*) calloc( system->N, sizeof(int) );
-    bond_top = (int*) calloc( system->N, sizeof(int) );
+    hb_top = (int*) scalloc( system->N, sizeof(int),
+            "Init_Lists::hb_top" );
+    bond_top = (int*) scalloc( system->N, sizeof(int),
+            "Init_Lists::bond_top" );
     num_3body = 0;
     Estimate_Storage_Sizes( system, control, lists, &Htop,
             hb_top, bond_top, &num_3body );
diff --git a/sPuReMD/src/lin_alg.c b/sPuReMD/src/lin_alg.c
index 01cb1798..32ec6741 100644
--- a/sPuReMD/src/lin_alg.c
+++ b/sPuReMD/src/lin_alg.c
@@ -1894,11 +1894,8 @@ void Transpose( const sparse_matrix * const A, sparse_matrix * const A_t )
 {
     unsigned int i, j, pj, *A_t_top;
 
-    if ( (A_t_top = (unsigned int*) calloc( A->n + 1, sizeof(unsigned int))) == NULL )
-    {
-        fprintf( stderr, "Not enough space for matrix tranpose. Terminating...\n" );
-        exit( INSUFFICIENT_MEMORY );
-    }
+    A_t_top = (unsigned int*) scalloc( A->n + 1, sizeof(unsigned int),
+            "Transpose::A_t_top" );
 
     memset( A_t->start, 0, (A->n + 1) * sizeof(unsigned int) );
 
diff --git a/sPuReMD/src/lookup.c b/sPuReMD/src/lookup.c
index 7990169c..7ec372f4 100644
--- a/sPuReMD/src/lookup.c
+++ b/sPuReMD/src/lookup.c
@@ -271,12 +271,18 @@ void Make_LR_Lookup_Table( reax_system *system, control_params *control )
 
     num_atom_types = system->reaxprm.num_atom_types;
     dr = control->r_cut / control->tabulate;
-    h = (real*) calloc( (control->tabulate + 1), sizeof(real) );
-    fh = (real*) calloc( (control->tabulate + 1), sizeof(real) );
-    fvdw = (real*) calloc( (control->tabulate + 1), sizeof(real) );
-    fCEvd = (real*) calloc( (control->tabulate + 1), sizeof(real) );
-    fele = (real*) calloc( (control->tabulate + 1), sizeof(real) );
-    fCEclmb = (real*) calloc( (control->tabulate + 1), sizeof(real) );
+    h = (real*) scalloc( (control->tabulate + 1), sizeof(real),
+            "Make_LR_Lookup_Table::h" );
+    fh = (real*) scalloc( (control->tabulate + 1), sizeof(real),
+            "Make_LR_Lookup_Table::fh" );
+    fvdw = (real*) scalloc( (control->tabulate + 1), sizeof(real),
+            "Make_LR_Lookup_Table::fvdw" );
+    fCEvd = (real*) scalloc( (control->tabulate + 1), sizeof(real),
+            "Make_LR_Lookup_Table::fCEvd" );
+    fele = (real*) scalloc( (control->tabulate + 1), sizeof(real),
+            "Make_LR_Lookup_Table::fele" );
+    fCEclmb = (real*) scalloc( (control->tabulate + 1), sizeof(real),
+            "Make_LR_Lookup_Table::fCEclmb" );
 
     /* allocate Long-Range LookUp Table space based on
        number of atom types in the ffield file */
diff --git a/sPuReMD/src/restart.c b/sPuReMD/src/restart.c
index 0f7b1c5b..82474a8b 100644
--- a/sPuReMD/src/restart.c
+++ b/sPuReMD/src/restart.c
@@ -20,7 +20,9 @@
   ----------------------------------------------------------------------*/
 
 #include "restart.h"
+
 #include "box.h"
+#include "tool_box.h"
 #include "vector.h"
 
 
@@ -101,17 +103,25 @@ void Read_Binary_Restart( char *fname, reax_system *system,
 #endif
 
     /* memory allocations for atoms, atom maps, bond restrictions */
-    system->atoms = (reax_atom*) calloc( system->N, sizeof(reax_atom) );
+    system->atoms = (reax_atom*) scalloc( system->N, sizeof(reax_atom),
+            "Read_Binary_Restart::system->atoms" );
 
-    workspace->map_serials = (int*) calloc( MAX_ATOM_ID, sizeof(int) );
+    workspace->map_serials = (int*) scalloc( MAX_ATOM_ID, sizeof(int),
+            "Read_Binary_Restart::workspace->map_serials" );
     for ( i = 0; i < MAX_ATOM_ID; ++i )
         workspace->map_serials[i] = -1;
 
-    workspace->orig_id = (int*) calloc( system->N, sizeof(int) );
-    workspace->restricted  = (int*) calloc( system->N, sizeof(int) );
-    workspace->restricted_list = (int**) calloc( system->N, sizeof(int*) );
+    workspace->orig_id = (int*) scalloc( system->N, sizeof(int),
+            "Read_Binary_Restart::workspace->orig_id" );
+    workspace->restricted  = (int*) scalloc( system->N, sizeof(int),
+            "Read_Binary_Restart::workspace->restricted" );
+    workspace->restricted_list = (int**) scalloc( system->N, sizeof(int*),
+            "Read_Binary_Restart::workspace->restricted_list" );
     for ( i = 0; i < system->N; ++i )
-        workspace->restricted_list[i] = (int*) calloc( MAX_RESTRICT, sizeof(int) );
+    {
+        workspace->restricted_list[i] = (int*) scalloc( MAX_RESTRICT, sizeof(int),
+                "Read_Binary_Restart::workspace->restricted_list[i]" );
+    }
 
     for ( i = 0; i < system->N; ++i )
     {
@@ -207,17 +217,27 @@ void Read_ASCII_Restart( char *fname, reax_system *system,
 #endif
 
     /* memory allocations for atoms, atom maps, bond restrictions */
-    system->atoms = (reax_atom*) calloc( system->N, sizeof(reax_atom) );
+    system->atoms = (reax_atom*) scalloc( system->N, sizeof(reax_atom),
+            "Read_ASCII_Restart::system->atoms" );
 
-    workspace->map_serials = (int*) calloc( MAX_ATOM_ID, sizeof(int) );
+    workspace->map_serials = (int*) scalloc( MAX_ATOM_ID, sizeof(int),
+            "Read_ASCII_Restart::workspace->map_serials" );
     for ( i = 0; i < MAX_ATOM_ID; ++i )
+    {
         workspace->map_serials[i] = -1;
+    }
 
-    workspace->orig_id = (int*) calloc( system->N, sizeof(int) );
-    workspace->restricted  = (int*) calloc( system->N, sizeof(int) );
-    workspace->restricted_list = (int**) calloc( system->N, sizeof(int*) );
+    workspace->orig_id = (int*) scalloc( system->N, sizeof(int),
+            "Read_ASCII_Restart::workspace->orig_id" );
+    workspace->restricted  = (int*) scalloc( system->N, sizeof(int),
+            "Read_ASCII_Restart::workspace->restricted" );
+    workspace->restricted_list = (int**) scalloc( system->N, sizeof(int*),
+            "Read_ASCII_Restart::workspace->restricted_list" );
     for ( i = 0; i < system->N; ++i )
-        workspace->restricted_list[i] = (int*) calloc( MAX_RESTRICT, sizeof(int) );
+    {
+        workspace->restricted_list[i] = (int*) scalloc( MAX_RESTRICT, sizeof(int),
+                "Read_ASCII_Restart::workspace->restricted_list[i]" );
+    }
 
     for ( i = 0; i < system->N; ++i )
     {
-- 
GitLab