From 8370ad19ef39a10af6f91e7e84fc1d7859a967b3 Mon Sep 17 00:00:00 2001
From: "Kurt A. O'Hearn" <ohearnku@msu.edu>
Date: Wed, 14 Jul 2021 17:13:40 -0400
Subject: [PATCH] sPuReMD: ensure matrix rows are sorted.

---
 sPuReMD/src/lin_alg.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/sPuReMD/src/lin_alg.c b/sPuReMD/src/lin_alg.c
index 69b69b6..c40baca 100644
--- a/sPuReMD/src/lin_alg.c
+++ b/sPuReMD/src/lin_alg.c
@@ -113,13 +113,15 @@ static int compare_matrix_entry(const void *v1, const void *v2)
 void Sort_Matrix_Rows( sparse_matrix * const A )
 {
     unsigned int i, j, si, ei;
+    size_t temp_size;
     sparse_matrix_entry *temp;
 
 #if defined(_OPENMP)
 //    #pragma omp parallel default(none) private(i, j, si, ei, temp) shared(stderr)
 #endif
     {
-        temp = smalloc( sizeof(sparse_matrix_entry) * (A->n + 1), __FILE__, __LINE__ );
+        temp = NULL;
+        temp_size = 0;
 
         /* sort each row of A using column indices */
 #if defined(_OPENMP)
@@ -130,6 +132,16 @@ void Sort_Matrix_Rows( sparse_matrix * const A )
             si = A->start[i];
             ei = A->start[i + 1];
 
+            if ( temp_size < ei - si )
+            {
+                if ( temp != NULL )
+                {
+                    sfree( temp, __FILE__, __LINE__ );
+                }
+                temp = smalloc( sizeof(sparse_matrix_entry) * (ei - si), __FILE__, __LINE__ );
+                temp_size = ei - si;
+            }
+
             for ( j = 0; j < (ei - si); ++j )
             {
                 temp[j].j = A->j[si + j];
-- 
GitLab