File: dt.c

package info (click to toggle)
simgrid 4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 39,192 kB
  • sloc: cpp: 124,913; ansic: 66,744; python: 8,560; java: 6,773; fortran: 6,079; f90: 5,123; xml: 4,587; sh: 2,194; perl: 1,436; makefile: 111; lisp: 49; javascript: 7; sed: 6
file content (711 lines) | stat: -rw-r--r-- 21,031 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
/*************************************************************************
 *                                                                       *
 *    N  A  S   P A R A L L E L   B E N C H M A R K S  3.3     *
 *                                     *
 *                  D T       *
 *                                     *
 *************************************************************************
 *                                     *
 *   This benchmark is part of the NAS Parallel Benchmark 3.3 suite.   *
 *                                     *
 *   Permission to use, copy, distribute and modify this software    *
 *   for any purpose with or without fee is hereby granted.  We      *
 *   request, however, that all derived work reference the NAS       *
 *   Parallel Benchmarks 3.3. This software is provided "as is"      *
 *   without express or implied warranty.                *
 *                                     *
 *   Information on NPB 3.3, including the technical report, the     *
 *   original specifications, source code, results and information     *
 *   on how to submit new results, is available at:            *
 *                                     *
 *      http:  www.nas.nasa.gov/Software/NPB             *
 *                                     *
 *   Send comments or suggestions to  npb@nas.nasa.gov           *
 *   Send bug reports to        npb-bugs@nas.nasa.gov        *
 *                                     *
 *     NAS Parallel Benchmarks Group                 *
 *     NASA Ames Research Center                   *
 *     Mail Stop: T27A-1                       *
 *     Moffett Field, CA   94035-1000                *
 *                                     *
 *     E-mail:  npb@nas.nasa.gov                   *
 *     Fax:   (650) 604-3957                     *
 *                                     *
 *************************************************************************
 *                                     *
 *   Author: M. Frumkin         *       *
 *                                     *
 *************************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "DGraph.h"
#include "smpi/mpi.h"
#include "nas_common.h"
#include "simgrid/instr.h" //TRACE_

int timer_on=0,timers_tot=64;
double start[64], elapsed[64];

char class;
int nprocs;
int num_samples;
int deviation;
int num_sources;

static int verify(char *bmname,double rnm2){
  double verify_value=0.0;
  double epsilon=1.0E-8;
  int verified=-1;
  if (class != 'U') {
    if(class=='S') {
     if(strstr(bmname,"BH")){
       verify_value=30892725.0;
     }else if(strstr(bmname,"WH")){
       verify_value=67349758.0;
     }else if(strstr(bmname,"SH")){
       verify_value=58875767.0;
     }else{
       fprintf(stderr,"No such benchmark as %s.\n",bmname);
     }
     verified = 0;
    }else if(class=='W') {
      if(strstr(bmname,"BH")){
        verify_value = 4102461.0;
      }else if(strstr(bmname,"WH")){
        verify_value = 204280762.0;
      }else if(strstr(bmname,"SH")){
        verify_value = 186944764.0;
      }else{
        fprintf(stderr,"No such benchmark as %s.\n",bmname);
      }
      verified = 0;
    }else if(class=='A') {
      if(strstr(bmname,"BH")){
        verify_value = 17809491.0;
      }else if(strstr(bmname,"WH")){
        verify_value = 1289925229.0;
      }else if(strstr(bmname,"SH")){
        verify_value = 610856482.0;
      }else{
        fprintf(stderr,"No such benchmark as %s.\n",bmname);
      }
      verified = 0;
    }else if(class=='B') {
      if(strstr(bmname,"BH")){
        verify_value = 4317114.0;
      }else if(strstr(bmname,"WH")){
        verify_value = 7877279917.0;
      }else if(strstr(bmname,"SH")){
        verify_value = 1836863082.0;
      }else{
        fprintf(stderr,"No such benchmark as %s.\n",bmname);
        verified = 0;
      }
    }else if(class=='C' || class == 'D') {
        verify_value = 0.0;
    }else{
      fprintf(stderr,"No such class as %c.\n",class);
    }
    fprintf(stderr," %s L2 Norm = %f\n",bmname,rnm2);
    if(verified==-1){
      fprintf(stderr," No verification was performed.\n");
    }else if( rnm2 - verify_value < epsilon && rnm2 - verify_value > -epsilon) {  /* abs here does not work on ALTIX */
      verified = 1;
      fprintf(stderr," Deviation = %f\n",(rnm2 - verify_value));
    }else{
      verified = 0;
      fprintf(stderr," The correct verification value = %f\n",verify_value);
      fprintf(stderr," Got value = %f\n",rnm2);
    }
  }else{
    verified = -1;
  }
  return  verified;
}

static int ipowMod(int a,long long int n,int md){
  int seed=1;
  int q=a;
  int r=1;
  int exp = n;
  if(n<0){
    fprintf(stderr,"ipowMod: exponent must be nonnegative exp=%lld\n",n);
    exp=-n; /* temp fix */
  }
  if(md<=0){
    fprintf(stderr,"ipowMod: module must be positive mod=%d",md);
    return 1;
  }
  if(n==0)
    return 1;
  while(exp>1){
    int n2 = exp/2;
    if (n2*2==n){
      seed = (q*q)%md;
      q=seed;
      exp = n2;
    }else{
      seed = (r*q)%md;
      r=seed;
      exp = exp -1;
    }
  }
  seed = (r*q)%md;
  return seed;
}

static DGraph *buildSH(const char cls){
/* Nodes of the graph must be topologically sorted to avoid MPI deadlock. */
  DGraph *dg;
  unsigned int numSources=num_sources; /* must be power of 2 */
  unsigned int numOfLayers=0;
  unsigned int tmpS=numSources>>1;
  int firstLayerNode=0;
  DGArc *ar=NULL;
  DGNode *nd=NULL;
  unsigned int mask=0x0;
  int ndid=0,ndoff=0;
  unsigned int i=0;
  unsigned int j=0;
  char nm[BLOCK_SIZE];

  snprintf(nm,BLOCK_SIZE,"DT_SH.%c",cls);
  dg=newDGraph(nm);

  while(tmpS>1){
  numOfLayers++;
  tmpS>>=1;
  }
  for(i=0;i<numSources;i++){
    snprintf(nm, BLOCK_SIZE, "Source.%u", i);
    nd = newNode(nm);
    AttachNode(dg, nd);
  }
  for(j=0;j<numOfLayers;j++){
    mask=0x00000001<<j;
    for(i=0;i<numSources;i++){
      snprintf(nm, BLOCK_SIZE, "Comparator.%u", (i + j * firstLayerNode));
      nd=newNode(nm);
      AttachNode(dg,nd);
      ndoff=i&(~mask);
      ndid=firstLayerNode+ndoff;
      ar=newArc(dg->node[ndid],nd);
      AttachArc(dg,ar);
      ndoff+=mask;
      ndid=firstLayerNode+ndoff;
      ar=newArc(dg->node[ndid],nd);
      AttachArc(dg,ar);
    }
    firstLayerNode+=numSources;
  }
  mask=0x00000001<<numOfLayers;
  for(i=0;i<numSources;i++){
    snprintf(nm, BLOCK_SIZE, "Sink.%u", i);
    nd=newNode(nm);
    AttachNode(dg,nd);
    ndoff=i&(~mask);
    ndid=firstLayerNode+ndoff;
    ar=newArc(dg->node[ndid],nd);
    AttachArc(dg,ar);
    ndoff+=mask;
    ndid=firstLayerNode+ndoff;
    ar=newArc(dg->node[ndid],nd);
    AttachArc(dg,ar);
  }
  return dg;
}

static DGraph *buildWH(const char cls){
/*  Nodes of the graph must be topologically sorted to avoid MPI deadlock. */
  int i=0,j=0;
  int numSources=num_sources,maxInDeg=4;
  int numLayerNodes=numSources,firstLayerNode=0;
  int totComparators=0;
  int numPrevLayerNodes=numLayerNodes;
  int id=0,sid=0;
  DGraph *dg;
  DGNode *nd=NULL,*source=NULL,*tmp=NULL,*snd=NULL;
  DGArc *ar=NULL;
  char nm[BLOCK_SIZE];

  snprintf(nm,BLOCK_SIZE,"DT_WH.%c",cls);
  dg=newDGraph(nm);

  for(i=0;i<numSources;i++){
    snprintf(nm,BLOCK_SIZE,"Sink.%d",i);
    nd=newNode(nm);
    AttachNode(dg,nd);
  }
  totComparators=0;
  while(numLayerNodes>maxInDeg){
    numLayerNodes=numLayerNodes/maxInDeg;
    if(numLayerNodes*maxInDeg<numPrevLayerNodes)
      numLayerNodes++;
    for(i=0;i<numLayerNodes;i++){
      snprintf(nm,BLOCK_SIZE,"Comparator.%d",totComparators);
      totComparators++;
      nd=newNode(nm);
      id=AttachNode(dg,nd);
      for(j=0;j<maxInDeg;j++){
        sid=i*maxInDeg+j;
        if(sid>=numPrevLayerNodes)
          break;
        snd=dg->node[firstLayerNode+sid];
        ar=newArc(dg->node[id],snd);
        AttachArc(dg,ar);
      }
    }
    firstLayerNode+=numPrevLayerNodes;
    numPrevLayerNodes=numLayerNodes;
  }
  source=newNode((char*)"Source");
  AttachNode(dg,source);
  for(i=0;i<numPrevLayerNodes;i++){
    nd=dg->node[firstLayerNode+i];
    ar=newArc(source,nd);
    AttachArc(dg,ar);
  }

  for(i=0;i<dg->numNodes/2;i++){  /* Topological sorting */
    tmp=dg->node[i];
    dg->node[i]=dg->node[dg->numNodes-1-i];
    dg->node[i]->id=i;
    dg->node[dg->numNodes-1-i]=tmp;
    dg->node[dg->numNodes-1-i]->id=dg->numNodes-1-i;
  }
return dg;
}

static DGraph *buildBH(const char cls){
/* Nodes of the graph must be topologically sorted to avoid MPI deadlock.*/
  int i=0,j=0;
  int numSources=num_sources,maxInDeg=4;
  int numLayerNodes=numSources,firstLayerNode=0;
  DGraph *dg;
  DGNode *nd=NULL, *snd=NULL, *sink=NULL;
  DGArc *ar=NULL;
  int totComparators=0;
  int numPrevLayerNodes=numLayerNodes;
  int id=0, sid=0;
  char nm[BLOCK_SIZE];

  snprintf(nm,BLOCK_SIZE,"DT_BH.%c",cls);
  dg=newDGraph(nm);

  for(i=0;i<numSources;i++){
    snprintf(nm,BLOCK_SIZE,"Source.%d",i);
    nd=newNode(nm);
    AttachNode(dg,nd);
  }
  while(numLayerNodes>maxInDeg){
    numLayerNodes=numLayerNodes/maxInDeg;
    if(numLayerNodes*maxInDeg<numPrevLayerNodes)
      numLayerNodes++;
    for(i=0;i<numLayerNodes;i++){
      snprintf(nm,BLOCK_SIZE,"Comparator.%d",totComparators);
      totComparators++;
      nd=newNode(nm);
      id=AttachNode(dg,nd);
      for(j=0;j<maxInDeg;j++){
        sid=i*maxInDeg+j;
        if(sid>=numPrevLayerNodes)
          break;
        snd=dg->node[firstLayerNode+sid];
        ar=newArc(snd,dg->node[id]);
        AttachArc(dg,ar);
      }
    }
    firstLayerNode+=numPrevLayerNodes;
    numPrevLayerNodes=numLayerNodes;
  }
  sink=newNode((char*)"Sink");
  AttachNode(dg,sink);
  for(i=0;i<numPrevLayerNodes;i++){
    nd=dg->node[firstLayerNode+i];
    ar=newArc(nd,sink);
    AttachArc(dg,ar);
  }
  return dg;
}

typedef struct{
  int len;
  double* val;
} Arr;

static Arr *newArr(int len){
  Arr *arr=(Arr *)malloc(sizeof(Arr)); //Arr *arr=(Arr *)SMPI_SHARED_MALLOC(sizeof(Arr));
  arr->len=len;
  arr->val=(double *)malloc(len*sizeof(double)); //arr->val=(double *)SMPI_SHARED_MALLOC(len*sizeof(double));
  return arr;
}

static double CheckVal(Arr *feat){
  double csum=0.0;
  for(int i=0;i<feat->len;i++){
    csum+=feat->val[i]*feat->val[i]/feat->len; /* The truncation does not work since result will be 0 for large len  */
  }
  return csum;
}

static int GetFNumDPar(int* mean, int* stdev){
  *mean=num_samples;
  *stdev=deviation;
  return 0;
}

static int GetFeatureNum(char *mbname,int id){
  double tran=314159265.0;
  double A=2*id+1;
  double denom=randlc(&tran,&A);
  char cval='S';
  int mean=num_samples,stdev=128;
  int rtfs=0,len=0;
  GetFNumDPar(&mean,&stdev);
  rtfs=ipowMod((int)(1/denom)*(int)cval,(long long int) (2*id+1),2*stdev);
  if(rtfs<0) rtfs=-rtfs;
  len=mean-stdev+rtfs;
  return len;
}

static Arr* RandomFeatures(char *bmname,int fdim,int id){
  int len=GetFeatureNum(bmname,id)*fdim;
  Arr* feat=newArr(len);
  int nxg=2,nyg=2,nzg=2,nfg=5;
  int nx=421,ny=419,nz=1427,nf=3527;
  long long int expon=(len*(id+1))%3141592;
  int seedx=ipowMod(nxg,expon,nx), seedy=ipowMod(nyg,expon,ny), seedz=ipowMod(nzg,expon,nz),seedf=ipowMod(nfg,expon,nf);

  if(timer_on){
    timer_clear(id+1);
    timer_start(id+1);
  }
  for(int i=0;i<len;i+=fdim){
    seedx=(seedx*nxg)%nx;
    seedy=(seedy*nyg)%ny;
    seedz=(seedz*nzg)%nz;
    seedf=(seedf*nfg)%nf;
    feat->val[i]=seedx;
    feat->val[i+1]=seedy;
    feat->val[i+2]=seedz;
    feat->val[i+3]=seedf;
  }
  if(timer_on){
  timer_stop(id+1);
  fprintf(stderr,"** RandomFeatures time in node %d = %f\n",id,timer_read(id+1));
  }
  return feat;
}

static void Resample(Arr *a,int blen){
  long long int i=0,j=0,jlo=0,jhi=0;
  double avval=0.0;
  double *nval=(double *)malloc(blen*sizeof(double));
  //double *nval=(double *)SMPI_SHARED_MALLOC(blen*sizeof(double));
  for(i=0;i<blen;i++) nval[i]=0.0;
  for(i=1;i<a->len-1;i++){
    jlo=(int)(0.5*(2*i-1)*(blen/a->len));
    jhi=(int)(0.5*(2*i+1)*(blen/a->len));

    avval=a->val[i]/(jhi-jlo+1);
    for(j=jlo;j<=jhi;j++){
    nval[j]+=avval;
    }
  }
  nval[0]=a->val[0];
  nval[blen-1]=a->val[a->len-1];
  free(a->val); //SMPI_SHARED_FREE(a->val);
  a->val=nval;
  a->len=blen;
}

#define fielddim 4
static Arr* WindowFilter(Arr *a, Arr* b,int w){
  int i=0,j=0,k=0;
  double rms0=0.0,rms1=0.0,rmsm1=0.0;
  double weight=((double) (w+1))/(w+2);

  w+=1;
  if(timer_on){
    timer_clear(w);
    timer_start(w);
  }
  if(a->len<b->len) Resample(a,b->len);
  if(a->len>b->len) Resample(b,a->len);
  for(i=fielddim;i<a->len-fielddim;i+=fielddim){
    rms0=(a->val[i]-b->val[i])*(a->val[i]-b->val[i]) +(a->val[i+1]-b->val[i+1])*(a->val[i+1]-b->val[i+1])
          +(a->val[i+2]-b->val[i+2])*(a->val[i+2]-b->val[i+2]) +(a->val[i+3]-b->val[i+3])*(a->val[i+3]-b->val[i+3]);
    j=i+fielddim;
    rms1=(a->val[j]-b->val[j])*(a->val[j]-b->val[j]) +(a->val[j+1]-b->val[j+1])*(a->val[j+1]-b->val[j+1])
          +(a->val[j+2]-b->val[j+2])*(a->val[j+2]-b->val[j+2]) +(a->val[j+3]-b->val[j+3])*(a->val[j+3]-b->val[j+3]);
    j=i-fielddim;
    rmsm1=(a->val[j]-b->val[j])*(a->val[j]-b->val[j]) +(a->val[j+1]-b->val[j+1])*(a->val[j+1]-b->val[j+1])
           +(a->val[j+2]-b->val[j+2])*(a->val[j+2]-b->val[j+2]) +(a->val[j+3]-b->val[j+3])*(a->val[j+3]-b->val[j+3]);
    k=0;
    if(rms1<rms0){
      k=1;
      rms0=rms1;
    }
    if(rmsm1<rms0) k=-1;
    if(k==0){
      a->val[i]=weight*b->val[i];
      a->val[i+1]=weight*b->val[i+1];
      a->val[i+2]=weight*b->val[i+2];
      a->val[i+3]=weight*b->val[i+3];
    }else if(k==1){
      j=i+fielddim;
      a->val[i]=weight*b->val[j];
      a->val[i+1]=weight*b->val[j+1];
      a->val[i+2]=weight*b->val[j+2];
      a->val[i+3]=weight*b->val[j+3];
    }else { /*if(k==-1)*/
      j=i-fielddim;
      a->val[i]=weight*b->val[j];
      a->val[i+1]=weight*b->val[j+1];
      a->val[i+2]=weight*b->val[j+2];
      a->val[i+3]=weight*b->val[j+3];
    }
  }
  if(timer_on){
    timer_stop(w);
    fprintf(stderr,"** WindowFilter time in node %d = %f\n",(w-1),timer_read(w));
  }
  return a;
}

static int SendResults(DGraph *dg,DGNode *nd,Arr *feat){
  int i=0,tag=0;
  DGArc *ar=NULL;
  DGNode *head=NULL;
  if(feat == 0)
    return 0;
  TRACE_smpi_set_category ("SendResults");
  for(i=0;i<nd->outDegree;i++){
    ar=nd->outArc[i];
    if(ar->tail ==nd){
      head=ar->head;
      tag=ar->id;
      if(head->address!=nd->address){
        MPI_Send(&feat->len,1,MPI_INT,head->address,tag,MPI_COMM_WORLD);
        MPI_Send(feat->val,feat->len,MPI_DOUBLE,head->address,tag,MPI_COMM_WORLD);
      }
    }
  }
  TRACE_smpi_set_category (NULL);
  return 1;
}

static Arr* CombineStreams(DGraph *dg,DGNode *nd){
  Arr *resfeat=newArr(num_samples*fielddim);
  int i=0,len=0,tag=0;
  DGArc *ar=NULL;
  DGNode *tail=NULL;
  MPI_Status status;
  Arr *feat=NULL,*featp=NULL;

  if(nd->inDegree==0) return NULL;
  for(i=0;i<nd->inDegree;i++){
    ar=nd->inArc[i];
    if(ar->head == nd){
      tail=ar->tail;
      if(tail->address!=nd->address){
        len=0;
        tag=ar->id;
        MPI_Recv(&len,1,MPI_INT,tail->address,tag,MPI_COMM_WORLD,&status);
        feat=newArr(len);
        MPI_Recv(feat->val,feat->len,MPI_DOUBLE,tail->address,tag,MPI_COMM_WORLD,&status);
        resfeat=WindowFilter(resfeat,feat,nd->id);
        free(feat);//SMPI_SHARED_FREE(feat);
      }else{
        featp=(Arr *)tail->feat;
        feat=newArr(featp->len);
        memcpy(feat->val,featp->val,featp->len*sizeof(double));
        resfeat=WindowFilter(resfeat,feat,nd->id);
        free(feat);//SMPI_SHARED_FREE(feat);
      }
    }
  }
  for(i=0;i<resfeat->len;i++)
    resfeat->val[i]=((int)resfeat->val[i])/nd->inDegree;
  nd->feat=resfeat;
  return nd->feat;
}

static double Reduce(Arr *a,int w){
  double retv=0.0;
  if(timer_on){
    timer_clear(w);
    timer_start(w);
  }
  retv=(int)(w*CheckVal(a));/* The casting needed for node and array dependent verifcation */
  if(timer_on){
    timer_stop(w);
    fprintf(stderr,"** Reduce time in node %d = %f\n",(w-1),timer_read(w));
  }
  return retv;
}

static double ReduceStreams(DGraph *dg,DGNode *nd){
  double csum=0.0;
  int i=0,len=0,tag=0;
  DGArc *ar=NULL;
  DGNode *tail=NULL;
  Arr *feat=NULL;
  double retv=0.0;

  TRACE_smpi_set_category ("ReduceStreams");

  for(i=0;i<nd->inDegree;i++){
    ar=nd->inArc[i];
    if(ar->head!=nd) continue;
    tail=ar->tail;
    if(tail->address!=nd->address){
      MPI_Status status;
      len=0;
      tag=ar->id;
      MPI_Recv(&len,1,MPI_INT,tail->address,tag,MPI_COMM_WORLD,&status);
      feat=newArr(len);
      MPI_Recv(feat->val,feat->len,MPI_DOUBLE,tail->address,tag,MPI_COMM_WORLD,&status);
      csum+=Reduce(feat,(nd->id+1));
      free(feat);//SMPI_SHARED_FREE(feat);
    }else{
      csum+=Reduce(tail->feat,(nd->id+1));
    }
  }
  if(nd->inDegree>0)csum=(((long long int)csum)/nd->inDegree);
  retv=(nd->id+1)*csum;
  return retv;
}

static int ProcessNodes(DGraph *dg,int me){
  double chksum=0.0;
  Arr *feat=NULL;
  int i=0,verified=0,tag;
  DGNode *nd=NULL;
  double rchksum=0.0;
  MPI_Status status;

  TRACE_smpi_set_category ("ProcessNodes");

  for(i=0;i<dg->numNodes;i++){
    nd=dg->node[i];
    if(nd->address!=me) continue;
    if(strstr(nd->name,"Source")){
      nd->feat=RandomFeatures(dg->name,fielddim,nd->id);
      SendResults(dg,nd,nd->feat);
    }else if(strstr(nd->name,"Sink")){
      chksum=ReduceStreams(dg,nd);
      tag=dg->numArcs+nd->id; /* make these to avoid clash with arc tags */
      MPI_Send(&chksum,1,MPI_DOUBLE,0,tag,MPI_COMM_WORLD);
    }else{
      feat=CombineStreams(dg,nd);
      SendResults(dg,nd,feat);
    }
  }

  TRACE_smpi_set_category ("ProcessNodes");

  if(me==0){ /* Report node */
    rchksum=0.0;
    chksum=0.0;
    for(i=0;i<dg->numNodes;i++){
      nd=dg->node[i];
      if(!strstr(nd->name,"Sink")) continue;
      tag=dg->numArcs+nd->id; /* make these to avoid clash with arc tags */
      MPI_Recv(&rchksum,1,MPI_DOUBLE,nd->address,tag,MPI_COMM_WORLD,&status);
      chksum+=rchksum;
    }
    verified=verify(dg->name,chksum);
  }
  return verified;
}

int main(int argc,char **argv ){
  int my_rank,comm_size;
  int i;
  DGraph *dg = NULL;
  int verified=0, featnum=0;
  double bytes_sent=2.0,tot_time=0.0;

  MPI_Init( &argc, &argv );
  MPI_Comm_rank( MPI_COMM_WORLD, &my_rank );
  MPI_Comm_size( MPI_COMM_WORLD, &comm_size );

  TRACE_smpi_set_category ("begin");
  get_info(argc, argv, &nprocs, &class);
  check_info(DT, nprocs, class);

  if    (class == 'S') { num_samples=1728; deviation=128; num_sources=4; }
  else if (class == 'W') { num_samples=1728*8; deviation=128*2; num_sources=4*2; }
  else if (class == 'A') { num_samples=1728*64; deviation=128*4; num_sources=4*4; }
  else if (class == 'B') { num_samples=1728*512; deviation=128*8; num_sources=4*8; }
  else if (class == 'C') { num_samples=1728*4096; deviation=128*16; num_sources=4*16; }
  else if (class == 'D') { num_samples=1728*4096*8; deviation=128*32; num_sources=4*32; }
  else {
    printf("setparams: Internal error: invalid class type %c\n", class);
    exit(1);
  }

  if(argc!=4 || (strncmp(argv[3],"BH",2)!=0 && strncmp(argv[3],"WH",2)!=0 && strncmp(argv[3],"SH",2)!=0)){
    if(my_rank==0){
    fprintf(stderr,"** Usage: mpirun -np N ../bin/dt.S GraphName\n");
    fprintf(stderr,"** Where \n   - N is integer number of MPI processes\n");
    fprintf(stderr,"   - S is the class S, W, or A \n");
    fprintf(stderr,"   - GraphName is the communication graph name BH, WH, or SH.\n");
    fprintf(stderr,"   - the number of MPI processes N should not be be less than \n");
    fprintf(stderr,"   the number of nodes in the graph\n");
    }
    MPI_Finalize();
    exit(0);
  }

  if(strncmp(argv[3],"BH",2)==0){
    dg=buildBH(class);
  }else if(strncmp(argv[3],"WH",2)==0){
    dg=buildWH(class);
  }else /* (strncmp(argv[3],"SH",2)==0) */ {
    dg=buildSH(class);
  }

  if(timer_on != 0 && dg->numNodes+1>timers_tot){
    timer_on=0;
    if(my_rank==0)
    fprintf(stderr,"Not enough timers. Node timing is off. \n");
  }
  if(dg->numNodes && dg->numNodes>comm_size){
    if(my_rank==0){
    fprintf(stderr,"**  The number of MPI processes should not be less than \n");
    fprintf(stderr,"**  the number of nodes in the graph\n");
    fprintf(stderr,"**  Number of MPI processes = %d\n",comm_size);
    fprintf(stderr,"**  Number nodes in the graph = %d\n",dg->numNodes);
    }
    MPI_Finalize();
    exit(0);
  }
  for(i=0; i<dg->numNodes; i++){
    dg->node[i]->address=i;
  }
  if( my_rank == 0 ){
    printf( "\n\n NAS Parallel Benchmarks 3.3 -- DT Benchmark\n\n" );
    graphShow(dg,0);
    timer_clear(0);
    timer_start(0);
  }
  verified=ProcessNodes(dg,my_rank);
  TRACE_smpi_set_category ("end");

  featnum=num_samples*fielddim;
  bytes_sent=featnum*dg->numArcs;
  bytes_sent/=1048576;
  if(my_rank==0){
    timer_stop(0);
    tot_time=timer_read(0);
    c_print_results( dg->name, class, featnum, 0, 0, dg->numNodes, 0, comm_size, tot_time, bytes_sent/tot_time,
         "bytes transmitted", verified);
  }
  MPI_Finalize();
  return 1;
}