|
| 1 | +# include <stdio.h> |
| 2 | +# include <stdlib.h> |
| 3 | +# include <iostream> |
| 4 | +# include <fstream> |
| 5 | +# include <math.h> |
| 6 | +//# define NOP 5 /*NOP is the number of processors*/ |
| 7 | + |
| 8 | +using namespace std; |
| 9 | + |
| 10 | +int pNOP[4]={2,3,4,5}; //added line 9 and 10 instead of line 5 |
| 11 | +int NOP; |
| 12 | +float shapeparameter[3] = {0.5,1,2}; |
| 13 | +float ccr[5] = {0.1,0.5,1,5,10}; |
| 14 | +int *wid,*h,v=0; |
| 15 | +FILE *fp; |
| 16 | + |
| 17 | +fstream output_file; //for the input in heft.c |
| 18 | + |
| 19 | +int task(); /*Declaration of all the functions*/ |
| 20 | +float ratio(); |
| 21 | +float graphshape(); |
| 22 | +int avgcomp(); |
| 23 | +int width(float,int); |
| 24 | +int out_degree(int); |
| 25 | +int below(int); |
| 26 | +float compcost(int); |
| 27 | +int commcost(int ,float); |
| 28 | +void output(int*,float*); |
| 29 | + |
| 30 | +int main() |
| 31 | + { |
| 32 | + //srand(time(NULL)); |
| 33 | + int n,k,wDAG,height,nog,*outdegree,i,r,j,temp,*cost; |
| 34 | + float alpha,ccratio,*comp; |
| 35 | + |
| 36 | + fp=fopen("RandomTaskGRaphs.txt","w"); /*Output File*/ |
| 37 | + output_file.open("Input.txt",ios::out); |
| 38 | + |
| 39 | + printf("How many graphs you want to generate?\n"); |
| 40 | + scanf("%d",&nog); |
| 41 | + |
| 42 | + for(k=0;k<nog;k++) |
| 43 | + { |
| 44 | + v=0; |
| 45 | + NOP=pNOP[rand()%4]; //generate number of Processors |
| 46 | + n=task(); |
| 47 | + printf("%d",n); |
| 48 | + |
| 49 | + ccratio=ratio(); |
| 50 | + alpha=graphshape(); |
| 51 | + wDAG=avgcomp(); |
| 52 | + |
| 53 | + height=(int)(sqrt(n)/alpha); |
| 54 | + |
| 55 | + fprintf(fp,"\n\nGraph # %d \nn=%d, alpha=%f,CC Ratio=%f ,Average Computational Cost=%d,\theight=%d \n\n\n",k+1,n,alpha,ccratio,wDAG,height); |
| 56 | + |
| 57 | + h=(int*)calloc(height,sizeof(int)); |
| 58 | + for(i=0;i<height;i++) /*Storing no of edges in each level into an array*/ |
| 59 | + { |
| 60 | + h[i]=width(alpha,n); |
| 61 | + fprintf(fp,"width of level %d=%d\n",i+1,h[i]); |
| 62 | + v=v+h[i]; |
| 63 | + } |
| 64 | + |
| 65 | + fprintf(fp,"\nFinal number of vertices=%d\n\n",v); |
| 66 | + v=v/2;//jsut for fun |
| 67 | + |
| 68 | + output_file<<v<<" "<<NOP<<endl; //First line of heft_input |
| 69 | + cost =(int*)calloc(v*v,sizeof(int)); /*Communication Cost matrix*/ |
| 70 | + outdegree =(int*)calloc(v,sizeof(int)); /*Array to store outdegree of each vertex*/ |
| 71 | + comp =(float*)calloc(v*NOP,sizeof(float)); /*Computational Cost matrix*/ |
| 72 | + |
| 73 | + for(i=0;i<v*v;i++) |
| 74 | + cost[i]=-1; |
| 75 | + |
| 76 | + for(i=0;i<v;i++) /*Storing outdegree of each vertex in an array*/ |
| 77 | + { |
| 78 | + outdegree[i]=out_degree(i); |
| 79 | + fprintf(fp,"outdegree of %d=%d\n",i+1,outdegree[i]); |
| 80 | + } |
| 81 | + |
| 82 | + for(i=0;i<v;i++) |
| 83 | + { |
| 84 | + for(j=0;j<=outdegree[i];j++) |
| 85 | + { |
| 86 | + if(below(i+1)) |
| 87 | + temp=(rand()%below(i+1)+(v-below(i+1)));/*Randomly selecting vertices to be connected with given vertex*/ |
| 88 | + cost[i+temp*v]=commcost(wDAG,ccratio); /*Storing the value of Communication cost between Vertices*/ |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + for(i=0;i<NOP;i++) |
| 93 | + for(j=0;j<v;j++) |
| 94 | + comp[i+j*NOP]=compcost(wDAG); /*Storing the value of Computation cost of a process for each task*/ |
| 95 | + output(cost,comp); |
| 96 | + |
| 97 | + } |
| 98 | + fclose(fp); |
| 99 | + output_file.close(); |
| 100 | + cout<<"\nYour Graphs have been saved in text file named RandomGraphs.txt"<<endl; |
| 101 | + return 123; |
| 102 | + } |
| 103 | + |
| 104 | +int task() /*Function to randomly generate number of tasks*/ |
| 105 | + { |
| 106 | + int r,x; |
| 107 | + r=rand()%10; |
| 108 | + |
| 109 | + x=(r+1)*10; |
| 110 | + return(x); |
| 111 | + } |
| 112 | + |
| 113 | +float ratio() /*Function to randomly determine Communication to Computation Ratio*/ |
| 114 | + { |
| 115 | + int temp; |
| 116 | + temp=rand()%5; |
| 117 | + return(ccr[temp]); |
| 118 | + } |
| 119 | + |
| 120 | +float graphshape() /*Function to randomly determine Shape Parameter*/ |
| 121 | + { |
| 122 | + int temp; |
| 123 | + temp=rand()%3; |
| 124 | + return(shapeparameter[temp]); |
| 125 | + } |
| 126 | + |
| 127 | +int avgcomp() /*Function to randomly determine average computational time*/ |
| 128 | + { |
| 129 | + int r,cos; |
| 130 | + r=rand()%4; |
| 131 | + cos=(r+1)*10; |
| 132 | + return(cos); |
| 133 | + } |
| 134 | + |
| 135 | +int width(float alpha,int x) /*Function to randomly determine number of vertices at each level*/ |
| 136 | + { |
| 137 | + int i,mean,temp; |
| 138 | + mean=(int)(alpha*sqrt(x)); |
| 139 | + wid=(int*)calloc(2*mean-1,sizeof(int)); |
| 140 | + for(i=0;i<(2*mean-1);i++) |
| 141 | + wid[i]=i+1; |
| 142 | + temp=rand()%(2*mean-1); |
| 143 | + return(wid[temp]); |
| 144 | + } |
| 145 | + |
| 146 | +int out_degree(int i) /*Function to randomly determine number of outgoing edges*/ |
| 147 | + { |
| 148 | + int r,temp; |
| 149 | + temp=below(i+1); |
| 150 | + if(!temp)return 0; |
| 151 | + else return((rand()%temp)+1); |
| 152 | + } |
| 153 | + |
| 154 | +int below(int i) /*Function to Calculate number of vertices at level lower than any of the given vetex*/ |
| 155 | + { |
| 156 | + int j=0,temp=0; |
| 157 | + while(temp<i) |
| 158 | + temp=temp+h[j++]; |
| 159 | + return(v-temp); |
| 160 | + } |
| 161 | + |
| 162 | +float compcost(int wDAG) /*Function to randomly determine Computation cost of any Task*/ |
| 163 | + { |
| 164 | + int r; |
| 165 | + float cos; |
| 166 | + cos=(rand()%(2*wDAG)+1); |
| 167 | + return(cos); |
| 168 | + } |
| 169 | + |
| 170 | +int commcost(int wDAG,float ccratio) /*Function to randomly determine Communication cost between any two given Tasks*/ |
| 171 | + { |
| 172 | + int cos,cDAG; |
| 173 | + |
| 174 | + cDAG=int(wDAG*ccratio); |
| 175 | + cos=(rand()%cDAG+1); |
| 176 | + return(cos); |
| 177 | + } |
| 178 | + |
| 179 | +void output(int *cost,float *comp) /*Function to Print output in a Text File*/ |
| 180 | + { |
| 181 | + int i,j; |
| 182 | + fprintf(fp,"\nComputation matrix is\n\n"); |
| 183 | + fprintf(fp,"---------\t"); |
| 184 | + |
| 185 | + for(i=0;i<NOP;i++) |
| 186 | + fprintf(fp,"*Pr%d*\t",(i+1)); |
| 187 | + |
| 188 | + fprintf(fp,"\n"); |
| 189 | + |
| 190 | + for(j=0;j<v;j++) |
| 191 | + { |
| 192 | + fprintf(fp,"Task # %d\t",(j+1)); |
| 193 | + for(i=0;i<NOP;i++) |
| 194 | + { |
| 195 | + fprintf(fp,"%d\t",(int)(comp[j+i*v]+1)); |
| 196 | + output_file<<(int)(comp[j+i*v]+1)<<"\t"; //computation matrix of tasks and processors |
| 197 | + } |
| 198 | + output_file<<endl; |
| 199 | + fprintf(fp,"\n"); |
| 200 | + } |
| 201 | + // data communication rate between the processors |
| 202 | + for(int q=0;q<NOP;++q) |
| 203 | + { |
| 204 | + for(int w=0;w<NOP;++w) |
| 205 | + { |
| 206 | + int p=1; |
| 207 | + if(q==w) p=0; |
| 208 | + output_file<<p<<" "; |
| 209 | + } |
| 210 | + output_file<<endl; |
| 211 | + } |
| 212 | + |
| 213 | + |
| 214 | + |
| 215 | + fprintf(fp,"\nCommunictaion matrix is\n\n"); |
| 216 | + fprintf(fp,"--From / To---\t"); |
| 217 | + for(i=0;i<v;i++) |
| 218 | + fprintf(fp,"#%d\t",i+1); |
| 219 | + |
| 220 | + fprintf(fp,"\n"); |
| 221 | + for(i=0;i<v;i++) |
| 222 | + { |
| 223 | + fprintf(fp,"From Task # %d\t",(i+1)); |
| 224 | + for(j=0;j<v;j++) |
| 225 | + { |
| 226 | + fprintf(fp,"%d\t",cost[i+j*v]); |
| 227 | + output_file<<cost[i+j*v]<<"\t"; //communication matrix between the tasks |
| 228 | + } |
| 229 | + output_file<<endl; |
| 230 | + fprintf(fp,"\n"); |
| 231 | + } |
| 232 | + cout<<endl; |
| 233 | + } |
0 commit comments