Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.31 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 *error;
  75.     pthread_mutex_t *errorLock;
  76. };
  77. void *doTheStuff(void *datas)
  78. {
  79.     double  sum;    // computes the inner products for A * t
  80.     double  errori; // | t1[i] - t[i] |
  81.     int i, j;       // indices into arrays
  82.    
  83.     int tid;
  84.     int itt;
  85.     int n;
  86.     int tcount;
  87.     double *a;
  88.     double *b;
  89.     double *t;
  90.     double *t1;
  91.     double *error;
  92.     pthread_mutex_t *errorLock;
  93.    
  94.     struct theDatas *localDatas;
  95.    
  96.     localDatas = (struct theDatas *) datas;
  97.     tid = localDatas->tid;
  98.     itt = localDatas->itt;
  99.     tcount = localDatas->tcount;
  100.     n = localDatas->n;
  101.     a = localDatas->a;
  102.     b = localDatas->b;
  103.     t = localDatas->t;
  104.     t1 = localDatas->t1;
  105.     error = localDatas->error;
  106.     errorLock = localDatas->errorLock;
  107.    
  108.    
  109.     //printf("I'm thread %d!\n",tid);
  110.    
  111.     *error=0.0;
  112.     for(i=0; i< n; i += tcount)
  113.     {
  114.         //printf("Thread %d Processing Row %d!\n",tid,i);
  115.         sum = 0.0;
  116.         //printf("n = %d\n",n);
  117.         for(j=0; j<n; j++)
  118.         {
  119.             //printf("Thread %d Processing Row %d Col %d!\n",tid,i,j);
  120.             sum += *(a+n*i+j) * t[j];
  121.         }
  122.         //printf("0 ");
  123.         t1[i] = sum + b[i];
  124.         //printf("1 ");
  125.         errori = fabs(t1[i]-t[i]);
  126.         //printf("2 ");
  127.         pthread_mutex_lock(&errorLock);
  128.         if(errori > error)
  129.         {
  130.             error=errori;
  131.         }
  132.         pthread_mutex_unlock(&errorLock);
  133.         //printf("3 ");
  134.     }
  135.    
  136.     pthread_exit(NULL);
  137. }
  138.  
  139. int main(int argc, char *argv[])
  140. {
  141. int n=4;        // problenm size
  142. double  *a;     // pointer to transfromation array, space to be malloced
  143. double  *b;     // pointer to transfromation vector, space to be malloced
  144. int seed=10;    // seed for srand48() / drand48()
  145. double *t;     // pointer to solution vector, space to be malloced
  146. double *t1;    // pointer to solution vector, space to be malloced
  147. int itt_max=5;  // number of itterations to preform
  148. int itt;        // current itteration
  149. char ch;        // for error checking on command line args.
  150. int i, j;       // indices into arrays
  151. double  *ttemp; // used to swap t1 and t at each itteration
  152. double  error;  // max | t1[i] - t[i] |
  153.  
  154. // New Variables
  155. int tcount = 4; // Number of threads, default 2
  156. char* logfile=NULL; // File to log too, default will be STDOUT
  157. FILE * log;     // File descriptor for our log file
  158. int tc;         // thread counter
  159. int rc;
  160.  
  161. if( argc == 4 )
  162. {
  163.     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) )
  164.     {
  165.         fprintf(stderr," ERROR : useage: %s [ <n> <seed> <itt_max>]\n", argv[0]);
  166.         return(1);
  167.     }
  168. }
  169. else if(argc == 5)
  170. {
  171.     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) || (sscanf(argv[4],"%d %[^ /t]", &tcount, &ch) != 1))
  172.     {
  173.         fprintf(stderr," ERROR : useage: %s [ <n> <seed> <itt_max>]\n", argv[0]);
  174.         return(1);
  175.     }
  176. }
  177. else if(argc == 6)
  178. {
  179.     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) || (sscanf(argv[4],"%d %[^ /t]", &tcount, &ch) != 1) || (sscanf(argv[5],"%s", logfile) != 1))
  180.     {
  181.         fprintf(stderr," ERROR : useage: %s [ <n> <seed> <itt_max>]\n", argv[0]);
  182.         return(1);
  183.     }
  184. }
  185. else if(argc != 1 )
  186. {
  187.     fprintf(stderr," ERROR : useage: %s [ <n> <seed> <itt_max>]\n", argv[0]);
  188.     return(1);
  189. }
  190.  
  191. if( n<1 )
  192. {
  193.     fprintf(stderr," ERROR : n must be positive\n");
  194.     return(1);
  195. }
  196.  
  197. if( (a=(double *)malloc(sizeof(double)*n*n)) == NULL )
  198. {
  199.     fprintf(stderr," ERROR : malloc for a failed\n");
  200.     return(1);
  201. }
  202.  
  203. if( (b=(double *)malloc(sizeof(double)*n)) == NULL )
  204. {
  205.     fprintf(stderr," ERROR : malloc for b failed\n");
  206.     return(1);
  207. }
  208.  
  209. if( (t=(double *)malloc(sizeof(double)*n)) == NULL )
  210. {
  211.     fprintf(stderr," ERROR : malloc for t failed\n");
  212.     return(1);
  213. }
  214. if( (t1=(double *)malloc(sizeof(double)*n)) == NULL )
  215. {
  216.     fprintf(stderr," ERROR : malloc for t1 failed\n");
  217.     return(1);
  218. }
  219.  
  220. // Open Log File
  221. /*printf("%s\n",logfile);
  222. if(logfile != NULL)
  223. {
  224.     log = fopen(logfile,O_APPEND | O_WRONLY);
  225.     dup2(log,stdout);
  226. }*/
  227.  
  228. // Generate matrix a with | eigenvalues | < 1
  229. srand48((long int)seed);
  230.  
  231. printf("\n  a=\n");
  232. for(i=0; i< n; i++)
  233. {
  234.     for(j=0; j< n; j++)
  235.     {
  236.         *(a+n*i+j) = 1.999 * (drand48() - 0.5) / n;
  237.         printf("%10.6f ", *(a+n*i+j) );
  238.     }
  239.     printf("\n");
  240. }
  241.  
  242.  
  243. // Generate vector b
  244. printf("\n  b=\n");
  245. for(i=0; i< n; i++)
  246. {
  247.     b[i] = 10.0 * drand48();
  248.     printf("%10.6f ", b[i]);
  249. }
  250. printf("\n");
  251.  
  252. // Initialize t
  253. for(i=0; i< n; i++)
  254. {
  255.     t[i] = b[i];
  256. }
  257.  
  258. // Thread stuff!
  259. pthread_t thread[tcount];
  260. struct theDatas someData[tcount];
  261. pthread_attr_t attr;
  262. void *status;
  263. pthread_mutex_t errorLock = PTHREAD_MUTEX_INITIALIZER;
  264.  
  265.  
  266. pthread_attr_init(&attr);
  267. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  268.  
  269. // Do stuff!
  270. printf("\n  itt  error\n");
  271.  
  272. for(itt=0; itt<=itt_max; itt++)
  273. {
  274.     for(tc=0; tc<tcount; tc++)
  275.     {  
  276.         someData[tc].tid = tc;
  277.         someData[tc].tcount = tcount;
  278.         someData[tc].itt = itt;
  279.         someData[tc].n = n;
  280.         someData[tc].a = a;
  281.         someData[tc].b = b;
  282.         someData[tc].t = t;
  283.         someData[tc].t1 = t1;
  284.         someData[tc].error = error;
  285.         someData[tc].errorLock = errorLock;
  286.        
  287. //      printf("Spawning thread %d of %d\n",tc,tcount-1);
  288.        
  289.         pthread_create(&thread[tc], NULL, doTheStuff, (void *) &someData[tc]);
  290.     }
  291.     for(tc=0; tc<tcount; tc++)
  292.         rc = pthread_join(thread[tc], &status);
  293.    
  294.     ttemp = t1;
  295.     t1 = t;
  296.     t = ttemp;
  297.     printf("%5d %14.6e\n", itt, error);
  298. }
  299.  
  300. return(0);
  301. }
  302.  
  303. // Output
  304. gcc -Wall -pedantic -std=c99 -o matmul_heap matmul_heap.c -lm -lpthread -fstack-protector-all -g
  305. matmul_heap.c: In function ‘doTheStuff’:
  306. matmul_heap.c:127: warning: passing argument 1 of ‘pthread_mutex_lock’ from incompatible pointer type
  307. matmul_heap.c:128: error: invalid operands to binary > (have ‘double’ and ‘double *)
  308. matmul_heap.c:130: error: incompatible types in assignment
  309. matmul_heap.c:132: warning: passing argument 1 of ‘pthread_mutex_unlock’ from incompatible pointer type
  310. matmul_heap.c: In function ‘main’:
  311. matmul_heap.c:284: error: incompatible types in assignment
  312. matmul_heap.c:285: error: incompatible types in assignment
  313. matmul_heap.c:157: warning: unused variable ‘log
  314. make: *** [matmul_heap] Error 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement