Changeset 3080 in Sophya for trunk/SophyaProg/Tests


Ignore:
Timestamp:
Sep 16, 2006, 2:39:50 PM (19 years ago)
Author:
ansari
Message:

Amelioration/extension programme test de threads zthr.cc , Reza 16/09/2006

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/SophyaProg/Tests/zthr.cc

    r2615 r3080  
    11#include "sopnamsp.h"
    22#include "zthread.h"
     3#include "resusage.h"
    34
    45#include <iostream>
     6#include <vector>
     7
     8#include "tmatrix.h"
     9#include "tarrinit.h"
    510
    611#include <stdlib.h>
    712#include <stdio.h>
    813
    9 
     14/* -------------------------------------------------
     15  Programme de test des classes de threads de SOPHYA
     16  SOPHYA::ZThread SOPHYA::ZMutex ...
     17  Exemples d'execution
     18  csh> time zthr mtx 2 500
     19  csh> time zthr arr 2 500
     20*/
     21 
    1022#include <time.h>
    1123#include <unistd.h>
     
    1426
    1527
    16 void fun2(void * arg)
     28// --- Structure d'argument pour fonction d'execution dans les threads de test
     29typedef struct  {
     30  int thid;
     31  int M;
     32} ztarg;
     33
     34// --- fonction de test simple avec boucle de sleep
     35void funzt(void *arg)
    1736{
    1837time_t t0, t1;
    1938int i;
    20 char *strg;
    21 strg = (char *)arg;
     39
     40ztarg * za = (ztarg *)arg;
    2241
    2342t0 = time(NULL);
    24 printf("**** Entry to fun2 (arg= %s) ***** \n", strg);
    25 int imax = atoi(strg);
     43printf("+++++ funzt(ThId=%d) Entry to funzt (za.M=%d) +++++\n", za->thid, za->M);
     44int imax = za->M;
    2645for(i=0; i<imax; i++)
    2746  {
    28   sleep(4);
     47  sleep(3);
    2948  t1 = time(NULL)-t0;
    30   printf("######## Fun2 [%d]  Dt= %d \n", i, (int)t1);
     49  printf("++funzt(ThId=%d)  Dt= %d \n", za->thid, (int)t1);
    3150  }
    3251
     
    3453}
    3554
    36 void fun1(void *arg)
     55// --- fonction de test simple avec calcul matriciel
     56void mtx_funzt(void *arg)
    3757{
    38 time_t t0, t1;
    39 int i;
     58  ztarg * za = (ztarg *)arg;
     59  cout << ">>>> mtx-funzt(ThId=" << za->thid << ") - Matrix size= " << za->M << endl;
     60 
     61  sa_size_t m = za->M;
     62  Matrix a1(m,m), a2(m,m), mxprod;
     63  a1 = RandomSequence(RandomSequence::Gaussian, 0., 4.);
     64  a2 = RandomSequence(RandomSequence::Gaussian, 0., 3.);
     65  char buff[128];
     66  sprintf(buff, "mtx-funzt(ThId=%d) EndOfInit", za->thid);
     67  PrtTim(buff);
     68  mxprod = a1*a2;
     69  sprintf(buff, "mtx-funzt(ThId=%d) EndOfMxProd", za->thid);
     70  PrtTim(buff);
     71  return;
     72}
     73// --- fonction de test simple avec calcul matriciel
     74void arr_funzt(void *arg)
     75{
     76  ztarg * za = (ztarg *)arg;
     77  cout << ">>>> arr-funzt(ThId=" << za->thid << ") - Matrix size= " << za->M << endl;
     78 
     79  sa_size_t m = za->M;
     80  TMatrix<int_4> a1(m,m), a2(m,m), ares;
     81  a1 = RegularSequence(1.,1.);
     82  a2 = RegularSequence(5.,3.);
     83  char buff[128];
     84  sprintf(buff, "arr-funzt(ThId=%d) EndOfInit", za->thid);
     85  PrtTim(buff);
     86  ares = 4*a1*12*a2;
     87  sprintf(buff, "arr-funzt(ThId=%d) EndOfOper", za->thid);
     88  PrtTim(buff);
     89  return;
     90}
    4091
    41 char * strg = (char *)arg;
    4292
    43 t0 = time(NULL);
    44 printf("++++ Entry to fun1 (arg= %s) +++++ \n", strg);
    45 int imax = atoi(strg);
    46 for(i=0; i<imax; i++)
    47   {
    48   sleep(3);
    49   t1 = time(NULL)-t0;
    50   printf("%%%% Fun1 [%d]  Dt= %d \n", i, (int)t1);
    51   }
    52 
    53 return;
    54 }
    5593
    5694class CountLock : public ZMutex {
     
    63101
    64102
     103static int N = 1;
     104static int M = 5;
     105
    65106int main(int narg, char *arg[])
    66107
     
    68109
    69110  if (narg < 4) {
    70     cout << " Usage: zthr N1 N2 NLock" << endl;
    71     exit(1);
     111    cout << " Usage: zthr select N LM" << endl;
     112    cout << "  select= sl -> simple loop with sleep " << endl;
     113    cout << "  select= mtx -> matrix init and multiply mx1*mx2" << endl;
     114    cout << "  select= arr -> array/matrix init and operation c1*a1+c2*a2 " << endl;
     115    cout << "  select= clk -> Mutex lock count  " << endl;
     116    cout << "  N= Number of threads (sl/mtx) or CountLock " << endl;
     117    cout << "  LM = Loop limit (sl) or Matrix size (mtx) " << endl;
     118    return(1);
    72119  }
    73120
    74   cout << " zthr-main() arg[1] = " << arg[1] << " arg[2]= " << arg[2] << endl;
     121  string sel = arg[1];
     122  if ((sel != "sl") && (sel != "mtx") && (sel != "arr") && (sel != "clk")) {
     123    cout << "zthr/erreur argument sel (!= sl / mtx / arr / clk) " << endl;
     124    return 2;
     125  }
     126
     127  //--  Decodage arguments
     128  N = atoi(arg[2]);
     129  M = atoi(arg[3]);
     130  cout << "zthr/Info: select=" << sel << " N=" << N << " M= " << M << endl;
     131 
     132 
    75133  InitTim();
     134  SophyaInit();
    76135
    77   CountLock clk;
    78   int kk;
    79   for(kk=0; kk<atoi(arg[3]); kk++) {
    80     clk.Count();
     136  int rc = 0;
     137  try {
     138    ResourceUsage res;
     139    if ((sel == "mtx") || (sel == "arr") || (sel == "sl")) {
     140      vector<ztarg *> vza;
     141      vector<ZThread *> vzth;
     142      for(int i=0; i<N; i++) {
     143        cout << "*****zthr: Creating Thread " << i+1 << " /" << N << endl;
     144        ZThread * pzt = new ZThread();
     145        ztarg* zap = new ztarg;
     146        vzth.push_back(pzt);
     147        zap->thid = i+1;  zap->M = M;
     148        vza.push_back(zap);
     149        if (sel == "mtx")  pzt->setAction(mtx_funzt, vza[i]);
     150        else if (sel == "arr")  pzt->setAction(arr_funzt, vza[i]);
     151        else pzt->setAction(funzt, vza[i]);
     152      }
     153      cout << "***zthr: Starting threads ... " << endl;
     154      PrtTim("***zthr/StarThr");
     155      for(int i=0; i<N; i++) vzth[i]->start();
     156      sleep(1);
     157      cout << "***ResourceUsage before thr[i].join()" << endl;
     158      cout << res;
     159      cout << "***zthr Joining Threads ..." << endl;
     160      for(int i=0; i<N; i++) vzth[i]->join();
     161      cout << "***zthr Threads Z1 ... Z" << N << " Finished OK" << endl;
     162      cout << res;
     163      for(int i=0; i<N; i++) {
     164        delete vzth[i];
     165        delete vza[i];
     166      }
     167    }
     168    else {
     169      PrtTim("BeginOfCount");
     170      CountLock clk;
     171      int kk;
     172      for(kk=0; kk<atoi(arg[3]); kk++) {
     173        clk.Count();
     174      }
     175      cout << " End CountLock-Test Count= " << clk.Count() << endl;
     176    }
    81177  }
    82   cout << " End Count= " << clk.Count() << endl;
    83   PrtTim("EndOfCount1 ");
    84 
    85   cout << ">>> Creating Thread Z1 and Z2 " << endl;
    86   ZThread zt1;
    87   zt1.setAction(fun1, arg[1]);
    88   ZThread zt2;
    89   zt2.setAction(fun2, arg[2]);
    90   cout << ">>> Starting Thread Z1 and Z2 " << endl;
    91   zt1.start();
    92   zt2.start();
    93 
    94   PrtTim("BeginOfCount2 ");
    95   for(kk=0; kk<atoi(arg[3]); kk++) {
    96     clk.Count();
    97   }
    98   cout << " End Count= " << clk.Count() << endl;
    99   PrtTim("EndOfCount2 ");
    100 
    101   //  sleep(5);
    102   cout << ">>> Joining Thread Z1 and Z2 " << endl;
    103 
    104   zt1.join();
    105   zt2.join();
    106   cout << ">>> Thread Z1 and Z2 Finished " << endl;
    107  
    108   return(0);
     178  catch (PThrowable exc) {
     179    cerr << "zthr: catched Exception " << exc.Msg() << endl;
     180    rc = 77;
     181  } 
     182  catch (...) {
     183    cerr << " catched unknown (...) exception (lpk.cc) " << endl;
     184    rc = 78;
     185  }
     186 
     187  return(rc);
    109188 
    110189}
     190
     191
     192
Note: See TracChangeset for help on using the changeset viewer.