Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
don't protect only some paths against NULLity
[simgrid.git] / examples / smpi / NAS / dt.c
index 567ee17..55b29dd 100644 (file)
@@ -104,28 +104,8 @@ static int verify(char *bmname,double rnm2){
         fprintf(stderr,"No such benchmark as %s.\n",bmname);
         verified = 0;
       }
-    }else if(class=='C') {
-      if(strstr(bmname,"BH")){
-        verify_value = 0.0;
-      }else if(strstr(bmname,"WH")){
-        verify_value = 0.0;
-      }else if(strstr(bmname,"SH")){
-        verify_value = 0.0;
-      }else{
-        fprintf(stderr,"No such benchmark as %s.\n",bmname);
-        verified = -1;
-      }
-    }else if(class=='D') {
-      if(strstr(bmname,"BH")){
+    }else if(class=='C' || class == 'D') {
         verify_value = 0.0;
-      }else if(strstr(bmname,"WH")){
-        verify_value = 0.0;
-      }else if(strstr(bmname,"SH")){
-        verify_value = 0.0;
-      }else{
-        fprintf(stderr,"No such benchmark as %s.\n",bmname);
-      }
-      verified = -1;
     }else{
       fprintf(stderr,"No such class as %c.\n",class);
     }
@@ -147,27 +127,30 @@ static int verify(char *bmname,double rnm2){
 }
 
 static int ipowMod(int a,long long int n,int md){
-  int seed=1,q=a,r=1;
+  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);
-    n=-n; /* temp fix */
-    /*  return 1; */
+    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(n>1){
-    int n2 = n/2;
+  if(n==0)
+    return 1;
+  while(exp>1){
+    int n2 = exp/2;
     if (n2*2==n){
       seed = (q*q)%md;
       q=seed;
-      n = n2;
+      exp = n2;
     }else{
       seed = (r*q)%md;
       r=seed;
-      n = n-1;
+      exp = exp -1;
     }
   }
   seed = (r*q)%md;
@@ -177,16 +160,19 @@ static int ipowMod(int a,long long int n,int md){
 static DGraph *buildSH(const char cls){
 /* Nodes of the graph must be topologically sorted to avoid MPI deadlock. */
   DGraph *dg;
-  int numSources=num_sources; /* must be power of 2 */
-  int numOfLayers=0,tmpS=numSources>>1;
+  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;
-  int mask=0x0,ndid=0,ndoff=0;
-  int i=0,j=0;
+  unsigned int mask=0x0;
+  int ndid=0,ndoff=0;
+  unsigned int i=0;
+  unsigned int j=0;
   char nm[BLOCK_SIZE];
 
-  sprintf(nm,"DT_SH.%c",cls);
+  snprintf(nm,BLOCK_SIZE,"DT_SH.%c",cls);
   dg=newDGraph(nm);
 
   while(tmpS>1){
@@ -194,14 +180,14 @@ static DGraph *buildSH(const char cls){
   tmpS>>=1;
   }
   for(i=0;i<numSources;i++){
-  sprintf(nm,"Source.%d",i);
+  snprintf(nm,BLOCK_SIZE,"Source.%d",i);
   nd=newNode(nm);
   AttachNode(dg,nd);
   }
   for(j=0;j<numOfLayers;j++){
     mask=0x00000001<<j;
     for(i=0;i<numSources;i++){
-      sprintf(nm,"Comparator.%d",(i+j*firstLayerNode));
+      snprintf(nm,BLOCK_SIZE,"Comparator.%d",(i+j*firstLayerNode));
       nd=newNode(nm);
       AttachNode(dg,nd);
       ndoff=i&(~mask);
@@ -217,7 +203,7 @@ static DGraph *buildSH(const char cls){
   }
   mask=0x00000001<<numOfLayers;
   for(i=0;i<numSources;i++){
-    sprintf(nm,"Sink.%d",i);
+    snprintf(nm,BLOCK_SIZE,"Sink.%d",i);
     nd=newNode(nm);
     AttachNode(dg,nd);
     ndoff=i&(~mask);
@@ -245,11 +231,11 @@ static DGraph *buildWH(const char cls){
   DGArc *ar=NULL;
   char nm[BLOCK_SIZE];
 
-  sprintf(nm,"DT_WH.%c",cls);
+  snprintf(nm,BLOCK_SIZE,"DT_WH.%c",cls);
   dg=newDGraph(nm);
 
   for(i=0;i<numSources;i++){
-    sprintf(nm,"Sink.%d",i);
+    snprintf(nm,BLOCK_SIZE,"Sink.%d",i);
     nd=newNode(nm);
     AttachNode(dg,nd);
   }
@@ -257,15 +243,17 @@ static DGraph *buildWH(const char cls){
   numPrevLayerNodes=numLayerNodes;
   while(numLayerNodes>maxInDeg){
     numLayerNodes=numLayerNodes/maxInDeg;
-    if(numLayerNodes*maxInDeg<numPrevLayerNodes)numLayerNodes++;
+    if(numLayerNodes*maxInDeg<numPrevLayerNodes)
+      numLayerNodes++;
     for(i=0;i<numLayerNodes;i++){
-      sprintf(nm,"Comparator.%d",totComparators);
+      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;
+        if(sid>=numPrevLayerNodes)
+          break;
         snd=dg->node[firstLayerNode+sid];
         ar=newArc(dg->node[id],snd);
         AttachArc(dg,ar);
@@ -305,25 +293,27 @@ static DGraph *buildBH(const char cls){
   int id=0, sid=0;
   char nm[BLOCK_SIZE];
 
-  sprintf(nm,"DT_BH.%c",cls);
+  snprintf(nm,BLOCK_SIZE,"DT_BH.%c",cls);
   dg=newDGraph(nm);
 
   for(i=0;i<numSources;i++){
-    sprintf(nm,"Source.%d",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++;
+    if(numLayerNodes*maxInDeg<numPrevLayerNodes)
+      numLayerNodes++;
     for(i=0;i<numLayerNodes;i++){
-      sprintf(nm,"Comparator.%d",totComparators);
+      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;
+        if(sid>=numPrevLayerNodes)
+          break;
         snd=dg->node[firstLayerNode+sid];
         ar=newArc(snd,dg->node[id]);
         AttachArc(dg,ar);
@@ -354,13 +344,6 @@ static Arr *newArr(int len){
   return arr;
 }
 
-static void arrShow(Arr* a){
-  if(!a) fprintf(stderr,"-- NULL array\n");
-  else{
-    fprintf(stderr,"-- length=%d\n",a->len);
-  }
-}
-
 static double CheckVal(Arr *feat){
   double csum=0.0;
   for(int i=0;i<feat->len;i++){
@@ -499,16 +482,18 @@ static int SendResults(DGraph *dg,DGNode *nd,Arr *feat){
   int i=0,tag=0;
   DGArc *ar=NULL;
   DGNode *head=NULL;
-  if(!feat) return 0;
+  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) continue;
-    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);
+    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);
@@ -526,25 +511,27 @@ static Arr* CombineStreams(DGraph *dg,DGNode *nd){
   if(nd->inDegree==0) return NULL;
   for(i=0;i<nd->inDegree;i++){
     ar=nd->inArc[i];
-    if(ar->head!=nd) continue;
-    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);
+    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;
+  for(i=0;i<resfeat->len;i++)
+    resfeat->val[i]=((int)resfeat->val[i])/nd->inDegree;
   nd->feat=resfeat;
   return nd->feat;
 }
@@ -641,7 +628,7 @@ static int ProcessNodes(DGraph *dg,int me){
 int main(int argc,char **argv ){
   int my_rank,comm_size;
   int i;
-  DGraph *dg=NULL;
+  DGraph *dg = NULL;
   int verified=0, featnum=0;
   double bytes_sent=2.0,tot_time=0.0;
 
@@ -685,7 +672,7 @@ int main(int argc,char **argv ){
     dg=buildSH(class);
   }
 
-  if(timer_on && dg->numNodes+1>timers_tot){
+  if(timer_on != 0 && dg->numNodes+1>timers_tot){
     timer_on=0;
     if(my_rank==0)
     fprintf(stderr,"Not enough timers. Node timeing is off. \n");
@@ -700,7 +687,7 @@ int main(int argc,char **argv ){
     MPI_Finalize();
     exit(0);
   }
-  for(i=0;i<dg->numNodes;i++){
+  for(i=0; i<dg->numNodes; i++){
     dg->node[i]->address=i;
   }
   if( my_rank == 0 ){