From 10286ffee81e73ccb1e5b7510894aa751142f6cb Mon Sep 17 00:00:00 2001
From: Florent Pruvost <florent.pruvost@inria.fr>
Date: Wed, 11 Feb 2015 12:27:05 +0000
Subject: [PATCH] in MORSE_Desc_Create_User we can use the matrix in mat field
 without allocating it --> avoid to free it in this case, users could need it
 and will manage deallocation

---
 control/control.c      |  2 +-
 control/descriptor.c   | 11 ++++++++++-
 include/morse_struct.h |  1 +
 3 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/control/control.c b/control/control.c
index 39199bc06..7810c2763 100644
--- a/control/control.c
+++ b/control/control.c
@@ -122,7 +122,7 @@ int MORSE_InitPar(int nworkers, int ncudas, int nthreads_per_worker)
       int flag = 0, provided = 0;
       MPI_Initialized( &flag );
       if ( !flag ) {
-	MPI_Init_thread( NULL, NULL, MPI_THREAD_MULTIPLE, &provided );
+          MPI_Init_thread( NULL, NULL, MPI_THREAD_MULTIPLE, &provided );
       }
     }
 #endif
diff --git a/control/descriptor.c b/control/descriptor.c
index 174229ea8..b0ed10144 100644
--- a/control/descriptor.c
+++ b/control/descriptor.c
@@ -66,6 +66,7 @@ MORSE_desc_t morse_desc_init(MORSE_enum dtyp, int mb, int nb, int bsiz,
     desc.id = nbdesc; nbdesc++;
     desc.occurences = 0;
     desc.use_mat = 1;
+    desc.alloc_mat = 1;
     desc.register_mat = 1;
 
 #if defined(CHAMELEON_USE_MPI)
@@ -157,6 +158,7 @@ MORSE_desc_t morse_desc_init_user(MORSE_enum dtyp, int mb, int nb, int bsiz,
     desc.id = nbdesc; nbdesc++;
     desc.occurences = 0;
     desc.use_mat = 1;
+    desc.alloc_mat = 1;
     desc.register_mat = 1;
 
 #if defined(CHAMELEON_USE_MPI)
@@ -316,7 +318,9 @@ int morse_desc_mat_free( MORSE_desc_t *desc )
 
     RUNTIME_desc_destroy( desc );
 
-    if (desc->mat != NULL && desc->use_mat == 1) {
+    if (desc->mat != NULL  &&
+        desc->use_mat == 1 &&
+        desc->alloc_mat == 1) {
 #ifndef CHAMELEON_SIMULATION
         free(desc->mat);
 #endif
@@ -417,6 +421,8 @@ int MORSE_Desc_Create(MORSE_desc_t **desc, void *mat, MORSE_enum dtyp, int mb, i
 
     } else {
         (**desc).mat = mat;
+        /* memory of the matrix is handle by users */
+        (**desc).alloc_mat = 0;
     }
 
     /* Create scheduler structure like registering data */
@@ -511,6 +517,9 @@ int MORSE_Desc_Create_User(MORSE_desc_t **desc, void *mat, MORSE_enum dtyp, int
     /* if the user gives a pointer to the overall data (tiles) we can use it */
     (**desc).use_mat = (mat == NULL) ? 0 : 1;
 
+    /* memory of the matrix is handle by users */
+    (**desc).alloc_mat = 0;
+
     /* users data can have multiple forms: let him register tiles */
     (**desc).register_mat = 0;
 
diff --git a/include/morse_struct.h b/include/morse_struct.h
index 6c2ca7d4c..c84b7f88d 100644
--- a/include/morse_struct.h
+++ b/include/morse_struct.h
@@ -102,6 +102,7 @@ struct morse_desc_s {
                       // submatrix desc (occurances>1) to avoid unregistering
                       // GPU data twice
     int use_mat;      // 1 if we have a pointer to the overall data mat - else 0
+    int alloc_mat;    // 1 if we handle the allocation of mat - else 0
     int register_mat; // 1 if we have to register mat - else 0 (handled by the application)
     int myrank;       // MPI rank of the descriptor
     void *schedopt;   // scheduler (QUARK|StarPU) specific structure
-- 
GitLab