Changeset 3185 in Sophya for trunk/SophyaProg/Tests/zthr.cc


Ignore:
Timestamp:
Feb 20, 2007, 4:24:29 PM (19 years ago)
Author:
ansari
Message:

extension programme zthr.cc (sync/syncp) , Reza 20/02/2007

File:
1 edited

Legend:

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

    r3178 r3185  
    3131// --- Structure d'argument pour fonction d'execution dans les threads de test
    3232typedef struct  {
    33   int_4 thid;
    34   int_4 M;
     33  int_4 thid, NTh;
     34  int_4 M, VSz;
    3535} ztarg;
    3636
     
    120120  ~MTVecPool()   { }
    121121  // Renvoie un pointeur de vecteur pour thread tid
    122   TVector<int_8>*  GetVec(uint_4 tid, uint_4& idx)
    123   {
    124     if (tid >= _nth) ParmError("MTVecPool::getvec() tid > _nth");
     122  TVector<int_8>*  GetVecP(uint_4 tid, uint_4& idx)
     123  {
     124    if (tid >= _nth) ParmError("MTVecPool::GetVecP() tid > _nth");
     125    //DBG  cout << "DBG-GetVecP(tid= " << tid << ")" << endl;
     126    if (tid == 0) {
     127      mex.lock();
     128      St_VecPool stv;
     129      idx = _vecs.size();
     130      stv.vv = _vmx.Column(idx);
     131      stv.busy = true;
     132      stv.stat = 0;
     133      _vecs.push_back(stv);
     134      mex.unlock();
     135      //DBG cout << "DBG-GetVecP(tid= " << tid << ") -> Idx=" << idx << " VecSz=" << &(_vecs[idx].vv) << endl;
     136      return (&(_vecs[idx].vv));
     137    }
     138    else {
     139      mex.lock();
     140      bool found = false;
     141      while (!found) {
     142        for(uint_4 k=0; k<_vecs.size(); k++) {
     143          if ( (_vecs[k].stat == tid) && (! _vecs[k].busy) ) {
     144            found = true;  idx = k;
     145            _vecs[k].stat = tid; _vecs[k].busy = true;
     146            break;
     147          }
     148        }
     149        if (found) {
     150          mex.unlock();
     151          //DBG  cout << "DBG-GetVecP(tid= " << tid << ") -> nv=" << hex << rv << dec << endl;
     152          return (&(_vecs[idx].vv));
     153        }
     154        else {
     155          mex.broadcast();
     156          mex.wait();
     157        }
     158      }
     159    }
     160  }
     161  // Renvoie un vecteur pour thread tid
     162  TVector<int_8>  GetVec(uint_4 tid, uint_4& idx)
     163  {
     164    if (tid >= _nth) ParmError("MTVecPool::GetVec() tid > _nth");
    125165    //DBG  cout << "DBG-GetVec(tid= " << tid << ")" << endl;
    126166    if (tid == 0) {
     
    134174      mex.unlock();
    135175      //DBG cout << "DBG-GetVec(tid= " << tid << ") -> Idx=" << idx << " VecSz=" << &(_vecs[idx].vv) << endl;
    136       return (&(_vecs[idx].vv));
     176      return (_vecs[idx].vv);
    137177    }
    138178    else {
     
    150190          mex.unlock();
    151191          //DBG  cout << "DBG-GetVec(tid= " << tid << ") -> nv=" << hex << rv << dec << endl;
    152           return (&(_vecs[idx].vv));
     192          return (_vecs[idx].vv);
    153193        }
    154194        else {
     
    207247static MTVecPool* mtvp = NULL;
    208248
     249// --- fonction de test avec synchronisation entre threads en utilisant pointeur de vecteurs
     250void syncp_funzt(void *arg)
     251{
     252  ztarg * za = (ztarg *)arg;
     253  cout << ">>>> syncp_funzt(ThId=" << za->thid << ") - NVec/NLoop= " << za->M << endl;
     254 
     255  if (mtvp == NULL)
     256    throw NullPtrError("syncp_funzt: MTVecPool* mtvp = NULL");
     257
     258  int_4 L = za->M;
     259  int_4 VS = za->VSz;
     260  int_8 p2 = 1;
     261  uint_4 k, ii, tid;
     262  tid = za->thid;
     263  for(k=0; k<tid; k++) p2 *= 2;
     264
     265  char buff[128];
     266  sprintf(buff, "syncp_funzt(ThId=%d) StarOfLoop", za->thid);
     267  PrtTim(buff);
     268  uint_4 idx;
     269  for(k=0; k<L; k++) {
     270    *(mtvp->GetVecP(tid, idx)) += p2;
     271    //DBG cout << "DBG-syncp_funzt(tid=" << tid << ", idx=" << idx << endl;
     272      mtvp->RetVec(idx);
     273  }
     274  sprintf(buff, "syncp_funzt(ThId=%d) EndOfLoop", za->thid);
     275  PrtTim(buff);
     276  return;
     277}
    209278// --- fonction de test avec synchronisation entre threads
    210279void sync_funzt(void *arg)
     
    217286
    218287  int_4 L = za->M;
     288  int_4 VS = za->VSz;
    219289  int_8 p2 = 1;
    220   uint_4 k, tid;
     290  uint_4 k, ii, tid;
    221291  tid = za->thid;
    222292  for(k=0; k<tid; k++) p2 *= 2;
     
    227297  uint_4 idx;
    228298  for(k=0; k<L; k++) {
    229     *(mtvp->GetVec(tid, idx)) += p2;
     299    mtvp->GetVec(tid, idx) += p2;
    230300    //DBG cout << "DBG-sync_funzt(tid=" << tid << ", idx=" << idx << endl;
    231301    mtvp->RetVec(idx);
     
    247317static int N = 1;
    248318static int M = 5;
    249 static int VSZ = 128;
     319static int VSZ = 32;
    250320
    251321int main(int narg, char *arg[])
     
    260330    cout << "  select= clk -> Mutex lock count  " << endl;
    261331    cout << "  select= sync -> Thread synchronisation using ZMutex" << endl;
     332    cout << "  select= syncp -> Thread synchronisation using ZMutex , Vector pointers" << endl;
    262333    cout << "  N= Number of threads (sl/mtx) or CountLock " << endl;
    263334    cout << "  LM = Loop limit (sl/sync) or Matrix size (mtx) " << endl;
    264     cout << "  Sz = Vector size for select=sync (default=256) " << endl;
     335    cout << "  Sz = Vector size for select=sync,syncp (default=32) " << endl;
    265336    return(1);
    266337  }
     
    268339  string sel = arg[1];
    269340  if ((sel != "sl") && (sel != "mtx") && (sel != "arr") &&
    270       (sel != "sync") && (sel != "clk")) {
     341      (sel != "sync") && (sel != "syncp") && (sel != "clk")) {
    271342    cout << "zthr/erreur argument sel (!= sl / mtx / arr / clk) " << endl;
    272343    return 2;
     
    287358  try {
    288359    ResourceUsage res(ResourceUsage::RU_All);
    289     if ((sel == "mtx") || (sel == "arr") || (sel == "sl") || (sel == "sync") ) {
    290       if (sel == "sync") mtvp = new MTVecPool(N,VSZ,M);
     360    if ((sel == "mtx") || (sel == "arr") || (sel == "sl") || (sel == "sync") || (sel == "syncp")) {
     361      if ( (sel == "sync") || (sel == "syncp")) mtvp = new MTVecPool(N,VSZ,M);
    291362      vector<ztarg *> vza;
    292363      vector<ZThread *> vzth;
     
    298369        // ATTENTION : il faut que le thid = 0 ... N-1 (et pas 1)
    299370        zap->thid = i;  zap->M = M;
     371        zap->NTh = N;   zap->VSz = VSZ;
    300372        vza.push_back(zap);
    301373        if (sel == "mtx")  pzt->setAction(mtx_funzt, vza[i]);
    302374        else if (sel == "arr")  pzt->setAction(arr_funzt, vza[i]);
    303375        else if (sel == "sync")  pzt->setAction(sync_funzt, vza[i]);
     376        else if (sel == "syncp")  pzt->setAction(syncp_funzt, vza[i]);
    304377        else pzt->setAction(funzt, vza[i]);
    305378      }
     
    313386      for(int i=0; i<N; i++) vzth[i]->join();
    314387      cout << "***zthr Threads Z1 ... Z" << N << " Finished OK" << endl;
     388      cout << " zthr/Resusage: getDataSize() = " << res.getDataSize() << "  getStackSize()="
     389           << res.getStackSize() << endl;
    315390      cout << res;
    316       cout << " Resu: getDataSize() = " << res.getDataSize() << "  getStackSize()="
    317            << res.getStackSize() << endl;
    318391      for(int i=0; i<N; i++) {
    319392        delete vzth[i];
Note: See TracChangeset for help on using the changeset viewer.