Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.24 KB | None | 0 0
  1. // matmul_heap.c
  2. // CS4540 Fall 2010
  3. // kapenga
  4. //
  5. // This program is one of a set of three programs that show a
  6. // matrix itteration. The difference between the three is the way
  7. // space for the arrays is allocated:
  8. //   matmul_stack uses local (automatic) variables for the arrays,
  9. //        which use space on the stack for the arrays. The array
  10. //        sizes are dynamic.
  11. //   matmul_static uses global (external) variables for the arrays,
  12. //        which uses space on the stack for the arrays. The maximum
  13. //        array sizes are compiled in.
  14. //   matmul_heap uses dynamic (malloced) space for the arrays,
  15. //        which uses space on the heap for the arrays. The array
  16. //        sizes are dynamic.
  17. //
  18. // The following itteretion can be used to solve linear systems
  19. //   t_{i+1} = A t_i + b
  20. // If the itteration converges to t, then t == t_{i+1} == t_i
  21. // So t = A t + b
  22. //   or  (I-a) t = b
  23. //   where, I is the n*n idenity matrix
  24. // There are several important applied problems where convergence
  25. // will take place. One such case is when for
  26. // each row of A ( rows 0 <= i < n)
  27. //             sum(j=0 ... n-1) abs(a[i][j])  < 1.0    
  28. // Then the itteration will converge, assuming no roundoff or overflow.
  29. // Example
  30. // % ./matmul_heap 4 10 5
  31. //
  32. //  a=
  33. //  0.189331   0.147829  -0.009582   0.012830
  34. // -0.020409   0.222627   0.073037   0.042701
  35. //  0.069882   0.228326  -0.001161   0.024936
  36. //  0.116375  -0.100117   0.229832   0.022235
  37. //
  38. //  b=
  39. //  2.411774   9.837874   6.251698   6.576916
  40. //
  41. //  itt  error
  42. //    0   2.878398e+00
  43. //    1   8.266521e-01
  44. //    2   2.688652e-01
  45. //    3   8.817662e-02
  46. //    4   2.832084e-02
  47. //    5   9.015857e-03
  48. //
  49.  
  50. #include <stdio.h>
  51. #include <stdlib.h>
  52. #include <math.h>
  53. #include <pthread.h>
  54. #include <fcntl.h>
  55. #include <unistd.h>
  56.  
  57. // These two function are not ansi C so they do not appear from the
  58. // libstd.h  header if the gcc option -std=c99 option is used.
  59. // I should see if there is a safe way to include them from stdlib.h
  60. // and not place them explicitly here, which is bad style.
  61.  
  62. void srand48(long int seedval);
  63. double drand48(void);
  64.  
  65. struct theDatas{
  66.     int tid;
  67.     int tcount;
  68.     int itt;
  69.     int n;
  70.     double *a;
  71.     double *b;
  72.     double *t;
  73.     double *t1;
  74.     double sum;
  75.     double error;
  76. };
  77. void *doTheStuff(void *datas)
  78. {
  79.     double  sum;    // computes the inner products for A * t
  80.     double  error;  // max | t1[i] - t[i] |
  81.     double  errori; // | t1[i] - t[i] |
  82.     int i, j;       // indices into arrays
  83.    
  84.     int tid;
  85.     int itt;
  86.     int n;
  87.     int tcount;
  88.     double *a;
  89.     double *b;
  90.     double *t;
  91.     double *t1;
  92.    
  93.     struct theDatas *localDatas;
  94.    
  95.     localDatas = (struct theDatas *) datas;
  96.     tid = localDatas->tid;
  97.     itt = localDatas->itt;
  98.     tcount = localDatas->tcount;
  99.     n = localDatas->n;
  100.     a = localDatas->a;
  101.     b = localDatas->b;
  102.     t = localDatas->t;
  103.    
  104.    
  105.    
  106.     printf("I'm thread %d!\n",tid);
  107.    
  108.     error=0.0;
  109.     for(i=0; i< n; i += tcount)
  110.     {
  111.         printf("Thread %d Processing Row %d!\n",tid,i);
  112.         sum = 0.0;
  113.         for(j=0; j< n; j++)
  114.         {
  115.             printf("Thread %d Processing Row %d Col %d!\n",tid,i,j);
  116.             sum += *(a+n*i+j) * t[j];
  117.         }
  118.         t1[i] = sum + b[i];
  119.         errori = fabs(t1[i]-t[i]);
  120.         if(errori > error)
  121.         {
  122.             error=errori;
  123.         }
  124.     }
  125.     localDatas.sum = sum;
  126.     localDatas.error = error;
  127.    
  128.     pthread_exit(NULL);
  129. }
  130.  
  131. int main(int argc, char *argv[])
  132. {
  133. int n=4;        // problenm size
  134. double  *a;     // pointer to transfromation array, space to be malloced
  135. double  *b;     // pointer to transfromation vector, space to be malloced
  136. int seed=10;    // seed for srand48() / drand48()
  137. double *t;     // pointer to solution vector, space to be malloced
  138. double *t1;    // pointer to solution vector, space to be malloced
  139. int itt_max=5;  // number of itterations to preform
  140. int itt;        // current itteration
  141. char ch;        // for error checking on command line args.
  142. int i, j;       // indices into arrays
  143. double  *ttemp; // used to swap t1 and t at each itteration
  144. double  sum;    // computes the inner products for A * t
  145. double  error;  // max | t1[i] - t[i] |
  146. double  errori; // | t1[i] - t[i] |
  147.  
  148. // New Variables
  149. int tcount = 2; // Number of threads, default 2
  150. char* logfile=NULL; // File to log too, default will be STDOUT
  151. FILE * log;     // File descriptor for our log file
  152. int tc;         // thread counter
  153. int rc;
  154.  
  155. if( argc == 4 )
  156. {
  157.     if( (sscanf(argv[1],"%d %[^ /t]", &n, &ch) != 1) || (sscanf(argv[2],"%d %[^ /t]", &seed, &ch) != 1) || (sscanf(argv[3],"%d %[^ /t]", &itt_max, &ch) != 1) )
  158.     {
  159.         fprintf(stderr," ERROR : useage: %s [ <n> <seed> <itt_max>]\n", argv[0]);
  160.         return(1);
  161.     }
  162. }
  163. else if(argc == 5)
  164. {
  165.     if(sscanf(argv[4],"%d %[^ /t]", &tcount, &ch) != 1)
  166.     {
  167.         fprintf(stderr," ERROR : useage: %s [ <n> <seed> <itt_max>]\n", argv[0]);
  168.         return(1);
  169.     }
  170. }
  171. else if(argc == 6)
  172. {
  173.     if(sscanf(argv[5],"%s", logfile) != 1)
  174.     {
  175.         fprintf(stderr," ERROR : useage: %s [ <n> <seed> <itt_max>]\n", argv[0]);
  176.         return(1);
  177.     }
  178. }
  179. else if(argc != 1 )
  180. {
  181.     fprintf(stderr," ERROR : useage: %s [ <n> <seed> <itt_max>]\n", argv[0]);
  182.     return(1);
  183. }
  184.  
  185. if( n<1 )
  186. {
  187.     fprintf(stderr," ERROR : n must be positive\n");
  188.     return(1);
  189. }
  190.  
  191. if( (a=(double *)malloc(sizeof(double)*n*n)) == NULL )
  192. {
  193.     fprintf(stderr," ERROR : malloc for a failed\n");
  194.     return(1);
  195. }
  196.  
  197. if( (b=(double *)malloc(sizeof(double)*n)) == NULL )
  198. {
  199.     fprintf(stderr," ERROR : malloc for b failed\n");
  200.     return(1);
  201. }
  202.  
  203. if( (t=(double *)malloc(sizeof(double)*n)) == NULL )
  204. {
  205.     fprintf(stderr," ERROR : malloc for t failed\n");
  206.     return(1);
  207. }
  208. if( (t1=(double *)malloc(sizeof(double)*n)) == NULL )
  209. {
  210.     fprintf(stderr," ERROR : malloc for t1 failed\n");
  211.     return(1);
  212. }
  213.  
  214. // Open Log File
  215. /*printf("%s\n",logfile);
  216. if(logfile != NULL)
  217. {
  218.     log = fopen(logfile,O_APPEND | O_WRONLY);
  219.     dup2(log,stdout);
  220. }*/
  221.  
  222. // Generate matrix a with | eigenvalues | < 1
  223. srand48((long int)seed);
  224.  
  225. printf("\n  a=\n");
  226. for(i=0; i< n; i++)
  227. {
  228.     for(j=0; j< n; j++)
  229.     {
  230.         *(a+n*i+j) = 1.999 * (drand48() - 0.5) / n;
  231.         printf("%10.6f ", *(a+n*i+j) );
  232.     }
  233.     printf("\n");
  234. }
  235.  
  236.  
  237. // Generate vector b
  238. printf("\n  b=\n");
  239. for(i=0; i< n; i++)
  240. {
  241.     b[i] = 10.0 * drand48();
  242.     printf("%10.6f ", b[i]);
  243. }
  244. printf("\n");
  245.  
  246. // Initialize t
  247. for(i=0; i< n; i++)
  248. {
  249.     t[i] = b[i];
  250. }
  251.  
  252. // Create our thread array
  253. pthread_t thread[tcount];
  254. struct theDatas someData[tcount];
  255. pthread_attr_t attr;
  256. void *status;
  257.  
  258. pthread_attr_init(&attr);
  259. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  260.  
  261. // Do stuff!
  262. printf("\n  itt  error\n");
  263.  
  264. for(itt=0; itt<=itt_max; itt++)
  265. {
  266.     for(tc=0; tc<tcount; tc++)
  267.     {  
  268.         someData[tc].tid = tc;
  269.         someData[tc].tcount = tcount;
  270.         someData[tc].itt = itt;
  271.         someData[tc].n = n;
  272.         someData[tc].a = a;
  273.         someData[tc].b = b;
  274.         someData[tc].t = t;
  275.         someData[tc].t1 = t1;
  276.        
  277.         printf("Spawning thread %d of %d\n",tc,tcount-1);
  278.        
  279.         rc = pthread_create(&thread[tc], NULL, doTheStuff, (void *) &someData[tc]);
  280.         if(rc)
  281.         {
  282.             fprintf(stderr,"Error spawning thread %d (%d)\n",tc,rc);
  283.             return(-1);
  284.         }
  285.        
  286.         /*
  287.         error=0.0;
  288.         for(i=0; i< n; i++)
  289.         {
  290.             sum = 0.0;
  291.             for(j=0; j< n; j++)
  292.             {
  293.                 sum += *(a+n*i+j) * t[j];
  294.             }
  295.             t1[i] = sum + b[i];
  296.             errori = fabs(t1[i]-t[i]);
  297.             if(errori > error)
  298.             {
  299.                 error=errori;
  300.             }
  301.         }
  302.         */
  303.        
  304.     }
  305.     for(tc=0; tc<tcount; tc++)
  306.     {
  307.         rc = pthread_join(thread[tc], &status);
  308.         if(rc)
  309.         {
  310.             fprintf(stderr,"Error joining thread %d (%d)\n",tc,rc);
  311.             return(-1);
  312.         }
  313.          printf("Main: completed join with thread %d having a status of %ld\n",tc,(long)status);
  314.     }
  315.    
  316.     ttemp = t1;
  317.     t1 = t;
  318.     t = ttemp;
  319.     printf("%5d %14.6e\n", itt, error);
  320. }
  321.  
  322. return(0);
  323. }
  324.  
  325. // output
  326. travis@batman:~/cs454/ass2/3[139] 2864 % ./matmul_heap 10 10 10
  327.  
  328.   a=
  329.   0.075732   0.059132  -0.003833   0.005132  -0.008163   0.089051   0.029215   0.017080   0.027953   0.091330
  330.  -0.000464   0.009974   0.046550  -0.040047   0.091933   0.008894  -0.051739   0.096709   0.025021   0.031523
  331.  -0.076330  -0.076081  -0.068221  -0.056610  -0.058096   0.054306  -0.079693  -0.005993  -0.010118   0.029982
  332.   0.026604  -0.027442   0.003887   0.094350  -0.026043  -0.024950   0.087433  -0.075697   0.005936  -0.085100
  333.   0.000359  -0.076407  -0.045923   0.098488   0.055573  -0.081033   0.056287  -0.061312   0.070243   0.071067
  334.   0.087992   0.060836   0.084124   0.019586  -0.049597  -0.007719   0.064988  -0.049935  -0.059120   0.079362
  335.  -0.053771   0.057492  -0.009375  -0.010235   0.046699   0.073086   0.077445  -0.005320  -0.024860  -0.075497
  336.  -0.068074  -0.067880  -0.047793  -0.032344   0.058348  -0.093961   0.054170  -0.085748  -0.051241  -0.079509
  337.   0.095479  -0.007781  -0.090568   0.075227   0.067584  -0.062678  -0.052532   0.001142   0.030670  -0.025270
  338.  -0.067617   0.094996   0.026292   0.094573  -0.012125  -0.029150   0.023878  -0.037532  -0.022051   0.083907
  339.  
  340.   b=
  341.   6.298272   3.537073   8.809945   8.331442   4.834493   5.205376   9.472396   2.323569   1.059944   6.950048
  342.  
  343.   itt  error
  344. Spawning thread 0 of 1
  345. I'm thread 0!
  346. Thread 0 Processing Row 0!
  347. Thread 0 Processing Row 0 Col 0!
  348. Thread 0 Processing Row 0 Col 1!
  349. Thread 0 Processing Row 0 Col 2!
  350. Thread 0 Processing Row 0 Col 3!
  351. Thread 0 Processing Row 0 Col 4!
  352. Thread 0 Processing Row 0 Col 5!
  353. Thread 0 Processing Row 0 Col 6!
  354. Thread 0 Processing Row 0 Col 7!
  355. Thread 0 Processing Row 0 Col 8!
  356. Thread 0 Processing Row 0 Col 9!
  357. [2]    19581 segmentation fault  ./matmul_heap 10 10 10
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement