Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Diff of /branches/pure-cfg/src/lib/parallel-target/main.c
ViewVC logotype

Diff of /branches/pure-cfg/src/lib/parallel-target/main.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1214, Sat May 14 04:19:19 2011 UTC revision 1215, Sat May 14 05:54:07 2011 UTC
# Line 14  Line 14 
14  #include <Diderot/diderot.h>  #include <Diderot/diderot.h>
15  #include <pthread.h>  #include <pthread.h>
16    
17  #ifdef HAVE_BUILTIN_ATOMIC_OPS  // #ifdef HAVE_BUILTIN_ATOMIC_OPS
18  STATIC_INLINE uint32_t AtomicInc (uint32_t *x)  // STATIC_INLINE uint32_t AtomicInc (uint32_t *x)
19  {  // {
20      return __sync_add_and_fetch(x, 1);  //     return __sync_add_and_fetch(x, 1);
21  }  // }
22  STATIC_INLINE uint32_t AtomicDec (uint32_t *x)  // STATIC_INLINE uint32_t AtomicDec (uint32_t *x)
23  {  // {
24      return __sync_sub_and_fetch(x, 1);  //     return __sync_sub_and_fetch(x, 1);
25  }  // }
26  #else  // #else
27  #  error atomic operations not supported  // #  error atomic operations not supported
28  #endif  // #endif
29    
30  // The number of strands a worker will take for processing at one time  // The number of strands a worker will take for processing at one time
31  #define BLOCK_SIZE 256  #define BLOCK_SIZE 256
# Line 73  Line 73 
73              else {              else {
74                // all other workers are idle, so we can proceed after some initialization                // all other workers are idle, so we can proceed after some initialization
75                  wrld->numIdle = 0;                  wrld->numIdle = 0;
76                  wrld->numAvail = wrld->numActive;                  wrld->numAvail = wrld->numStrands;  // includes inactive strands
77                  wrld->nextStrand = 0;                  wrld->nextStrand = 0;
78                // swap in and out                // swap in and out
79                  void **tmp = wrld->inState;                  void **tmp = wrld->inState;
# Line 92  Line 92 
92        // iterate until there is no more work to do        // iterate until there is no more work to do
93          int blkStart, blkSize;          int blkStart, blkSize;
94          existsStabilizing = false;          existsStabilizing = false;
95            int numDead = 0;
96          do {          do {
97            // grab some work            // grab some work
98              pthread_mutex_lock (&wrld->lock);              pthread_mutex_lock (&wrld->lock);
                 if (wrld->numAvail > 0) {  
99                      blkStart = wrld->nextStrand;                      blkStart = wrld->nextStrand;
100                      blkSize = (wrld->numAvail >= BLOCK_SIZE) ? BLOCK_SIZE : wrld->numAvail;                      blkSize = (wrld->numAvail >= BLOCK_SIZE) ? BLOCK_SIZE : wrld->numAvail;
101                      wrld->numAvail -= blkSize;                      wrld->numAvail -= blkSize;
102                  }                  wrld->nextStrand += blkSize;
103              pthread_mutex_unlock (&wrld->lock);              pthread_mutex_unlock (&wrld->lock);
104            // update the strands            // update the strands
105              for (int i = blkStart;  i < blkStart+blkSize;  i++) {              for (int i = blkStart;  i < blkStart+blkSize;  i++) {
# Line 112  Line 112 
112                          break;                          break;
113                        case DIDEROT_DIE:                        case DIDEROT_DIE:
114                          wrld->status[i] = DIDEROT_DIE;                          wrld->status[i] = DIDEROT_DIE;
115                          AtomicDec(&wrld->numActive);                          numDead++;
116                          break;                          break;
117                        default:                        default:
118                          break;                          break;
119                      }                      }
120                  }                  }
121                    else {
122                        assert ((wrld->status[i] == DIDEROT_STABLE) || (wrld->status[i] == DIDEROT_DIE));
123                    }
124              }              }
125          } while (blkSize > 0);          } while (blkSize > 0);
126    
127        // barrier synchronization        // barrier synchronization
128          pthread_mutex_lock (&wrld->lock);          pthread_mutex_lock (&wrld->lock);
129                wrld->numActive -= numDead;
130              if (wrld->numIdle+1 < wrld->numWorkers) {              if (wrld->numIdle+1 < wrld->numWorkers) {
131                  wrld->numIdle++;                  wrld->numIdle++;
132                  pthread_cond_wait (&wrld->barrier, &wrld->lock);                  pthread_cond_wait (&wrld->barrier, &wrld->lock);
# Line 130  Line 134 
134              else {              else {
135                // all other workers are idle, so we can proceed                // all other workers are idle, so we can proceed
136                  wrld->numIdle = 0;                  wrld->numIdle = 0;
                 wrld->numAvail = wrld->numActive;  
137                  pthread_cond_broadcast (&wrld->barrier);                  pthread_cond_broadcast (&wrld->barrier);
138                    wrld->nSteps++;
139              }              }
140          pthread_mutex_unlock (&wrld->lock);          pthread_mutex_unlock (&wrld->lock);
141    
# Line 144  Line 148 
148              int start = myArg->id * nStrandsPerWorker;              int start = myArg->id * nStrandsPerWorker;
149              int limit = start + nStrandsPerWorker;              int limit = start + nStrandsPerWorker;
150              if (limit > wrld->numStrands) limit = wrld->numStrands;              if (limit > wrld->numStrands) limit = wrld->numStrands;
151                int numStabilized = 0;
152              for (int i = start;  i < limit;  i++) {              for (int i = start;  i < limit;  i++) {
153                  if (wrld->status[i] == DIDEROT_STABILIZE) {                  if (wrld->status[i] == DIDEROT_STABILIZE) {
154                    // copy out to in so that both copies are the stable state                    // copy out to in so that both copies are the stable state
155                      memcpy (wrld->inState[i], wrld->outState[i], Diderot_Strands[0]->stateSzb);                      memcpy (wrld->inState[i], wrld->outState[i], Diderot_Strands[0]->stateSzb);
156                      wrld->status[i] = DIDEROT_STABLE;                      wrld->status[i] = DIDEROT_STABLE;
157                      AtomicDec (&wrld->numActive);                      numStabilized++;
                 }  
158              }              }
159          }          }
160              // adjust the numActive count
161                pthread_mutex_lock (&wrld->lock);
162                    wrld->numActive -= numStabilized;
163                pthread_mutex_unlock (&wrld->lock);
164      }      }
165        } // end while(true)
166    
167  }  }
168    

Legend:
Removed from v.1214  
changed lines
  Added in v.1215

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0