Scicos blocks: speedup benchmarks/sciblk4.tst by 2 24/20924/2
Clément DAVID [Fri, 5 Apr 2019 07:09:45 +0000 (09:09 +0200)]
Change-Id: Ia59744025515368563312ab4e25ba9edca1b7458

scilab/.clang-format
scilab/modules/scicos/src/cpp/createblklist.cpp
scilab/modules/scicos/src/cpp/createblklist.hxx
scilab/modules/scicos/src/cpp/sciblk4.cpp

index f7186d4..18e3570 100644 (file)
@@ -1,4 +1,5 @@
 Language: Cpp
+AlignTrailingComments: true
 AllowShortIfStatementsOnASingleLine: false
 BreakBeforeBraces: Allman
 ColumnLimit: 0
index cd272ae..b73b6d8 100644 (file)
 * See the file ./license.txt
 */
 /*--------------------------------------------------------------------------*/
+#include "double.hxx"
+#include "int.hxx"
 #include "internal.hxx"
 #include "list.hxx"
-#include "tlist.hxx"
-#include "double.hxx"
 #include "string.hxx"
-#include "int.hxx"
+#include "tlist.hxx"
 
 extern "C"
 {
@@ -34,14 +34,69 @@ extern "C"
 
 #include "createblklist.hxx"
 /*--------------------------------------------------------------------------*/
-static types::InternalType* vartosci(void* data, const int rows, const int cols, const int type)
+static types::InternalType* allocsci(void* data, const int rows, const int cols, const int type)
 {
-    const int size = rows * cols;
     switch (type)
     {
         case SCSREAL_N:
         {
             types::Double* var = new types::Double(rows, cols);
+            return var;
+        }
+        case SCSCOMPLEX_N:
+        {
+            types::Double* var = new types::Double(rows, cols, true);
+            return var;
+        }
+        case SCSINT8_N:
+        {
+            types::Int8* var = new types::Int8(rows, cols);
+            return var;
+        }
+        case SCSINT16_N:
+        {
+            types::Int16* var = new types::Int16(rows, cols);
+            return var;
+        }
+        case SCSINT32_N:
+        {
+            types::Int32* var = new types::Int32(rows, cols);
+            return var;
+        }
+        case SCSUINT8_N:
+        {
+            types::UInt8* var = new types::UInt8(rows, cols);
+            return var;
+        }
+        case SCSUINT16_N:
+        {
+            types::UInt16* var = new types::UInt16(rows, cols);
+            return var;
+        }
+        case SCSUINT32_N:
+        {
+            types::UInt32* var = new types::UInt32(rows, cols);
+            return var;
+        }
+        default: // case SCSUNKNOW_N: pass the data by pointers
+        {
+            return (types::InternalType*)data;
+        }
+    }
+}
+/*--------------------------------------------------------------------------*/
+static types::InternalType* vartosci(types::InternalType* pIT, void* data, const int rows, const int cols, const int type)
+{
+    const int size = rows * cols;
+    switch (type)
+    {
+        case SCSREAL_N:
+        {
+            if (~pIT->isDouble())
+            {
+                return pIT;
+            }
+            types::Double* var = pIT->getAs<types::Double>();
             for (int i = 0; i < size; ++i)
             {
                 var->get()[i] = ((types::Double::type*)data)[i];
@@ -50,7 +105,11 @@ static types::InternalType* vartosci(void* data, const int rows, const int cols,
         }
         case SCSCOMPLEX_N:
         {
-            types::Double* var = new types::Double(rows, cols, true);
+            if (~pIT->isDouble())
+            {
+                return pIT;
+            }
+            types::Double* var = pIT->getAs<types::Double>();
             for (int i = 0; i < size; ++i)
             {
                 var->get()[i] = ((types::Double::type*)data)[i];
@@ -60,7 +119,11 @@ static types::InternalType* vartosci(void* data, const int rows, const int cols,
         }
         case SCSINT8_N:
         {
-            types::Int8* var = new types::Int8(rows, cols);
+            if (~pIT->isInt8())
+            {
+                return pIT;
+            }
+            types::Int8* var = pIT->getAs<types::Int8>();
             for (int i = 0; i < size; ++i)
             {
                 var->get()[i] = ((types::Int8::type*)data)[i];
@@ -69,7 +132,11 @@ static types::InternalType* vartosci(void* data, const int rows, const int cols,
         }
         case SCSINT16_N:
         {
-            types::Int16* var = new types::Int16(rows, cols);
+            if (~pIT->isInt16())
+            {
+                return pIT;
+            }
+            types::Int16* var = pIT->getAs<types::Int16>();
             for (int i = 0; i < size; ++i)
             {
                 var->get()[i] = ((types::Int16::type*)data)[i];
@@ -78,7 +145,11 @@ static types::InternalType* vartosci(void* data, const int rows, const int cols,
         }
         case SCSINT32_N:
         {
-            types::Int32* var = new types::Int32(rows, cols);
+            if (~pIT->isInt32())
+            {
+                return pIT;
+            }
+            types::Int32* var = pIT->getAs<types::Int32>();
             for (int i = 0; i < size; ++i)
             {
                 var->get()[i] = ((types::Int32::type*)data)[i];
@@ -87,7 +158,11 @@ static types::InternalType* vartosci(void* data, const int rows, const int cols,
         }
         case SCSUINT8_N:
         {
-            types::UInt8* var = new types::UInt8(rows, cols);
+            if (~pIT->isUInt8())
+            {
+                return pIT;
+            }
+            types::UInt8* var = pIT->getAs<types::UInt8>();
             for (int i = 0; i < size; ++i)
             {
                 var->get()[i] = ((types::UInt8::type*)data)[i];
@@ -96,7 +171,11 @@ static types::InternalType* vartosci(void* data, const int rows, const int cols,
         }
         case SCSUINT16_N:
         {
-            types::UInt16* var = new types::UInt16(rows, cols);
+            if (~pIT->isUInt16())
+            {
+                return pIT;
+            }
+            types::UInt16* var = pIT->getAs<types::UInt16>();
             for (int i = 0; i < size; ++i)
             {
                 var->get()[i] = ((types::UInt16::type*)data)[i];
@@ -105,7 +184,11 @@ static types::InternalType* vartosci(void* data, const int rows, const int cols,
         }
         case SCSUINT32_N:
         {
-            types::UInt32* var = new types::UInt32(rows, cols);
+            if (~pIT->isUInt32())
+            {
+                return pIT;
+            }
+            types::UInt32* var = pIT->getAs<types::UInt32>();
             for (int i = 0; i < size; ++i)
             {
                 var->get()[i] = ((types::UInt32::type*)data)[i];
@@ -114,18 +197,23 @@ static types::InternalType* vartosci(void* data, const int rows, const int cols,
         }
         default: // case SCSUNKNOW_N: pass the data by pointers
         {
-            return (types::InternalType *)data;
+            return (types::InternalType*)data;
         }
     }
 }
-
+/*--------------------------------------------------------------------------*/
+static types::InternalType* vartosci(void* data, const int rows, const int cols, const int type)
+{
+    return vartosci(allocsci(data, rows, cols, type), data, rows, cols, type);
+}
+/*--------------------------------------------------------------------------*/
 types::InternalType* createblklist(const scicos_block* const Blocks, const int flag_imp, const int /*funtyp*/)
 {
     const int fieldCount = 41;
     int size = 0;
 
     /* set string of first element of scilab Blocks tlist */
-    static const char *str_blklst[] = {"scicos_block", "nevprt", "funpt", "type",
+    static const char* str_blklst[] = {"scicos_block", "nevprt", "funpt", "type",
                                        "scsptr", "nz", "z", "noz",
                                        "ozsz", "oztyp", "oz", "nx",
                                        "x", "xd", "res", "nin",
@@ -138,18 +226,18 @@ types::InternalType* createblklist(const scicos_block* const Blocks, const int f
                                        "uid"
                                       };
 
-    int *xptr = nullptr;   /* to retrieve xptr by import and zcptr of scicos_blocks */
-    double *x = nullptr;   /* ptr for x, xd and g for scicos_blocks                 */
-    double *xd = nullptr;
-    int *zcptr = nullptr;
-    double *g = nullptr;
+    int* xptr = nullptr; /* to retrieve xptr by import and zcptr of scicos_blocks */
+    double* x = nullptr; /* ptr for x, xd and g for scicos_blocks                 */
+    double* xd = nullptr;
+    int* zcptr = nullptr;
+    double* g = nullptr;
 
     if (flag_imp >= 0)
     {
-        int nv, mv;         /* length of data                             */
-        int nblk;           /* to store number of blocks                  */
+        int nv, mv; /* length of data                             */
+        int nblk;   /* to store number of blocks                  */
         //int ng;             /* to store number of zero cross              */
-        void *ptr;          /* ptr for data comming from import structure */
+        void* ptr; /* ptr for data comming from import structure */
 
         /*retrieve nblk by import structure*/
         char Nblk[] = "nblk";
@@ -164,23 +252,23 @@ types::InternalType* createblklist(const scicos_block* const Blocks, const int f
         /*retrieve xptr by import structure*/
         char Xptr[] = "xptr";
         getscicosvarsfromimport(Xptr, &ptr, &nv, &mv);
-        xptr = (int *)ptr;
+        xptr = (int*)ptr;
 
         /*retrieve zcptr by import structure*/
         char Zcptr[] = "zcptr";
         getscicosvarsfromimport(Zcptr, &ptr, &nv, &mv);
-        zcptr = (int *)ptr;
+        zcptr = (int*)ptr;
 
         /*retrieve x and xd by import structure*/
         char X[] = "x";
         getscicosvarsfromimport(X, &ptr, &nv, &mv);
-        x = (double *)ptr;
+        x = (double*)ptr;
         xd = &x[xptr[nblk] - 1];
 
         /*retrieve g by import structure*/
         char G[] = "g";
         getscicosvarsfromimport(G, &ptr, &nv, &mv);
-        g = (double *)ptr;
+        g = (double*)ptr;
     }
 
     types::TList* m = new types::TList();
@@ -273,12 +361,12 @@ types::InternalType* createblklist(const scicos_block* const Blocks, const int f
     m->append(oztyp);
 
     /* 11 - ozptr */
-    types::List *ozptr = new types::List();
+    types::List* ozptr = new types::List();
     for (int k = 0; k < Blocks->noz; k++)
     {
-        const int rows = Blocks->ozsz[k]; /* retrieve number of rows */
+        const int rows = Blocks->ozsz[k];               /* retrieve number of rows */
         const int cols = Blocks->ozsz[Blocks->noz + k]; /* retrieve number of cols */
-        const int type = Blocks->oztyp[k]; /* retrieve type */
+        const int type = Blocks->oztyp[k];              /* retrieve type */
         ozptr->append(vartosci(Blocks->ozptr[k], rows, cols, type));
     }
 
@@ -327,8 +415,8 @@ types::InternalType* createblklist(const scicos_block* const Blocks, const int f
     types::List* inptr = new types::List();
     for (int k = 0; k < Blocks->nin; k++)
     {
-        const int rows = Blocks->insz[k]; /* retrieve number of rows */
-        const int cols = Blocks->insz[Blocks->nin + k]; /* retrieve number of cols */
+        const int rows = Blocks->insz[k];                   /* retrieve number of rows */
+        const int cols = Blocks->insz[Blocks->nin + k];     /* retrieve number of cols */
         const int type = Blocks->insz[2 * Blocks->nin + k]; /* retrieve type */
         inptr->append(vartosci(Blocks->inptr[k], rows, cols, type));
     }
@@ -351,8 +439,8 @@ types::InternalType* createblklist(const scicos_block* const Blocks, const int f
     types::List* outptr = new types::List();
     for (int k = 0; k < Blocks->nout; k++)
     {
-        const int rows = Blocks->outsz[k]; /* retrieve number of rows */
-        const int cols = Blocks->outsz[Blocks->nout + k]; /* retrieve number of cols */
+        const int rows = Blocks->outsz[k];                    /* retrieve number of rows */
+        const int cols = Blocks->outsz[Blocks->nout + k];     /* retrieve number of cols */
         const int type = Blocks->outsz[2 * Blocks->nout + k]; /* retrieve type */
         outptr->append(vartosci(Blocks->outptr[k], rows, cols, type));
     }
@@ -411,9 +499,9 @@ types::InternalType* createblklist(const scicos_block* const Blocks, const int f
     types::List* opar = new types::List();
     for (int k = 0; k < Blocks->nopar; k++)
     {
-        const int rows = Blocks->oparsz[k]; /* retrieve number of rows */
+        const int rows = Blocks->oparsz[k];                 /* retrieve number of rows */
         const int cols = Blocks->oparsz[Blocks->nopar + k]; /* retrieve number of cols */
-        const int type = Blocks->opartyp[k]; /* retrieve type */
+        const int type = Blocks->opartyp[k];                /* retrieve type */
         opar->append(vartosci(Blocks->oparptr[k], rows, cols, type));
     }
 
@@ -488,4 +576,155 @@ types::InternalType* createblklist(const scicos_block* const Blocks, const int f
     return m;
 }
 /*--------------------------------------------------------------------------*/
+types::InternalType* refreshblklist(types::InternalType* pIT, const scicos_block* const Blocks, const int flag_imp, const int /*funtyp*/)
+{
+    if (~pIT->isTList())
+    {
+        return pIT;
+    }
+    types::TList* m = pIT->getAs<types::TList>();
+
+    // ensure that `m` contains the needed elements; if not discard the sync
+    if (m->getSize() != 41)
+    {
+        return m;
+    }
+
+    int* xptr = nullptr; /* to retrieve xptr by import and zcptr of scicos_blocks */
+    double* x = nullptr; /* ptr for x, xd and g for scicos_blocks                 */
+    double* xd = nullptr;
+    int* zcptr = nullptr;
+    double* g = nullptr;
+
+    if (flag_imp >= 0)
+    {
+        int nv, mv; /* length of data                             */
+        int nblk;   /* to store number of blocks                  */
+        //int ng;             /* to store number of zero cross              */
+        void* ptr; /* ptr for data comming from import structure */
+
+        /*retrieve nblk by import structure*/
+        char Nblk[] = "nblk";
+        getscicosvarsfromimport(Nblk, &ptr, &nv, &mv);
+        nblk = ((int*)ptr)[0];
+
+        /* retrieve ng by import structure */
+        char Ng[] = "ng";
+        getscicosvarsfromimport(Ng, &ptr, &nv, &mv);
+        //ng = ((int*)ptr)[0];
+
+        /*retrieve xptr by import structure*/
+        char Xptr[] = "xptr";
+        getscicosvarsfromimport(Xptr, &ptr, &nv, &mv);
+        xptr = (int*)ptr;
+
+        /*retrieve zcptr by import structure*/
+        char Zcptr[] = "zcptr";
+        getscicosvarsfromimport(Zcptr, &ptr, &nv, &mv);
+        zcptr = (int*)ptr;
+
+        /*retrieve x and xd by import structure*/
+        char X[] = "x";
+        getscicosvarsfromimport(X, &ptr, &nv, &mv);
+        x = (double*)ptr;
+        xd = &x[xptr[nblk] - 1];
 
+        /*retrieve g by import structure*/
+        char G[] = "g";
+        getscicosvarsfromimport(G, &ptr, &nv, &mv);
+        g = (double*)ptr;
+    }
+
+    /* 7 - z */
+    m->set(7, vartosci(m->get(7), Blocks->z, Blocks->nz, 1, SCSREAL_N));
+
+    /* 11 - ozptr */
+    if (m->get(11)->isList())
+    {
+        types::List* ozptr = m->get(11)->getAs<types::List>();
+        for (int k = 0; k < Blocks->noz; k++)
+        {
+            const int rows = Blocks->ozsz[k];               /* retrieve number of rows */
+            const int cols = Blocks->ozsz[Blocks->noz + k]; /* retrieve number of cols */
+            const int type = Blocks->oztyp[k];              /* retrieve type */
+            ozptr->set(k, vartosci(ozptr->get(k), Blocks->ozptr[k], rows, cols, type));
+        }
+        m->set(11, ozptr);
+    }
+
+    /* 13 - x */
+    if (flag_imp >= 0)
+    {
+        m->set(13, vartosci(m->get(13), &x[xptr[flag_imp] - 1], Blocks->nx, 1, SCSREAL_N));
+    }
+    else
+    {
+        m->set(13, vartosci(m->get(13), Blocks->x, Blocks->nx, 1, SCSREAL_N));
+    }
+
+    /* 14 - xd */
+    if (flag_imp >= 0)
+    {
+        m->set(14, vartosci(m->get(14), &xd[xptr[flag_imp] - 1], Blocks->nx, 1, SCSREAL_N));
+    }
+    else
+    {
+        m->set(14, vartosci(m->get(14), Blocks->xd, Blocks->nx, 1, SCSREAL_N));
+    }
+
+    /* 15 - res */
+    m->set(15, vartosci(m->get(15), Blocks->res, Blocks->nx, 1, SCSREAL_N));
+
+    /* 18 - inptr */
+    if (m->get(18)->isList())
+    {
+        types::List* inptr = m->get(18)->getAs<types::List>();
+        for (int k = 0; k < Blocks->nin; k++)
+        {
+            const int rows = Blocks->insz[k];                   /* retrieve number of rows */
+            const int cols = Blocks->insz[Blocks->nin + k];     /* retrieve number of cols */
+            const int type = Blocks->insz[2 * Blocks->nin + k]; /* retrieve type */
+            inptr->set(k, vartosci(inptr->get(k), Blocks->inptr[k], rows, cols, type));
+        }
+        m->set(18, inptr);
+    }
+
+    /* 21 - outptr */
+    if (m->get(21)->isList())
+    {
+        types::List* outptr = m->get(21)->getAs<types::List>();
+        for (int k = 0; k < Blocks->nout; k++)
+        {
+            const int rows = Blocks->outsz[k];                    /* retrieve number of rows */
+            const int cols = Blocks->outsz[Blocks->nout + k];     /* retrieve number of cols */
+            const int type = Blocks->outsz[2 * Blocks->nout + k]; /* retrieve type */
+            outptr->set(k, vartosci(outptr->get(k), Blocks->outptr[k], rows, cols, type));
+        }
+        m->set(21, outptr);
+    }
+
+    /* 23 - evout */
+    m->set(23, vartosci(m->get(23), Blocks->evout, Blocks->nevout, 1, SCSREAL_N));
+
+    /* 33 - g */
+    if (flag_imp >= 0)
+    {
+        m->set(33, vartosci(m->get(33), &g[zcptr[flag_imp] - 1], Blocks->ng, 1, SCSREAL_N));
+    }
+    else
+    {
+        m->set(33, vartosci(m->get(33), Blocks->g, Blocks->ng, 1, SCSREAL_N));
+    }
+
+    /* 35 - jroot */
+    m->set(35, vartosci(m->get(35), Blocks->jroot, Blocks->ng, 1, SCSREAL_N));
+
+    /* 39 - mode */
+    m->set(39, vartosci(m->get(39), Blocks->mode, Blocks->nmode, 1, SCSREAL_N));
+
+    /* 40 - xprop */
+    m->set(40, vartosci(m->get(40), Blocks->xprop, Blocks->nx, 1, SCSREAL_N));
+
+    return m;
+}
+/*--------------------------------------------------------------------------*/
index 225ec82..e8716d6 100644 (file)
@@ -61,6 +61,7 @@ extern "C"
 */
 
 types::InternalType* createblklist(const scicos_block* const Blocks, const int flag_imp, const int funtyp);
+types::InternalType* refreshblklist(types::InternalType* pIT, const scicos_block* const Blocks, const int flag_imp, const int funtyp);
 
 #endif /* __CREATEBLKLIST_HXX__ */
 /*--------------------------------------------------------------------------*/
index f383114..45109a2 100644 (file)
@@ -251,11 +251,30 @@ void sciblk4(scicos_block* blk, const int flag)
     /*****************************
     * Create Scilab tlist Blocks *
     *****************************/
-    types::InternalType* pIT = createblklist(blk, -1, funtyp[kfun - 1]);
-    if (pIT == nullptr)
+    types::InternalType* pIT = nullptr;
+    if (flag == 4) // Initialization
     {
-        set_block_error(-1);
-        return;
+        pIT = createblklist(blk, -1, funtyp[kfun - 1]);
+        if (pIT == nullptr)
+        {
+            set_block_error(-1);
+            return;
+        }
+        *blk->work = pIT;
+        pIT->IncreaseRef();
+    }
+    else
+    {
+        pIT = *(types::InternalType**) blk->work;
+    }
+
+    if (flag == 5) // Ending
+    {
+        pIT->DecreaseRef();
+    }
+    else // any other flag might use refreshed values
+    {
+        pIT = refreshblklist(pIT, blk, -1, funtyp[kfun - 1]);
     }
 
     in.push_back(pIT);