Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <pthread.h>
  5. // These two function are not ansi C so they do not appear from the
  6. // libstd.h header if the gcc option -std=c99 option is used.
  7. // I should see if there is a safe way to include them from stdlib.h
  8. // and not place them explicitly here, which is bad style.
  9. void srand48(long int seedval);
  10. double drand48(void);
  11.  
  12.  
  13. int main(int argc, char *argv[])
  14. {
  15. int n=4; // problenm size
  16. double *a; // pointer to transfromation array, space to be malloced
  17. double *b; // pointer to transfromation vector, space to be malloced
  18. int seed=10; // seed for srand48() / drand48()
  19. double *t; // pointer to solution vector, space to be malloced
  20. double *t1; // pointer to next itteraton of solution vector, space to be malloced
  21. double *ttemp; // used to swap t1 and t at each itteration
  22. int itt_max=5; // number of itterations to preform
  23. int itt; // current itteration
  24. int i, j; // indices into arrays
  25. double sum; // computes the inner products for A * t
  26. double error; // max | t1[i] - t[i] |
  27. double errori; // | t1[i] - t[i] |
  28. char ch; // for error checking on command line args.
  29.  
  30. // New Variables
  31. tcount = 2; // Number of threads, default 2
  32. char* logfile; // File to log too, default will be STDOUT
  33. FILE * log; // File descriptor for our log file
  34.  
  35.  
  36. if( argc == 6 )
  37. {
  38. 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[
  39. 4],"%d %[^ /t]", &tcount, &ch) != 1) || (sscanf(argv[5],"%s", &logfile, &ch) != 1) )
  40. {
  41. fprintf(stderr," ERROR : useage: %s [ <n> <seed> <itt_max>]\n", argv[0]);
  42. return(1);
  43. }
  44. }
  45. else if(argc != 1 )
  46. {
  47. fprintf(stderr," ERROR : useage: %s [ <n> <seed> <itt_max>]\n", argv[0]);
  48. return(1);
  49. }
  50.  
  51. if( n<1 )
  52. {
  53. fprintf(stderr," ERROR : n must be positive\n");
  54. return(1);
  55. }
  56.  
  57. if( (a=(double *)malloc(sizeof(double)*n*n)) == NULL )
  58. {
  59. fprintf(stderr," ERROR : malloc for a failed\n");
  60. return(1);
  61. }
  62.  
  63. if( (b=(double *)malloc(sizeof(double)*n)) == NULL )
  64. {
  65. fprintf(stderr," ERROR : malloc for b failed\n");
  66. return(1);
  67. }
  68.  
  69. if( (t=(double *)malloc(sizeof(double)*n)) == NULL )
  70. {
  71. fprintf(stderr," ERROR : malloc for t failed\n");
  72. return(1);
  73. }
  74.  
  75. if( (t1=(double *)malloc(sizeof(double)*n)) == NULL )
  76. {
  77. fprintf(stderr," ERROR : malloc for t1 failed\n");
  78. return(1);
  79. }
  80.  
  81. // Open Log File
  82. if(logfile != null)
  83. log = fopen(logfile,"w+");
  84. else
  85. log = fopen(STDOUT,"w+");
  86.  
  87. // Generate matrix a with | eigenvalues | < 1
  88. srand48((long int)seed);
  89.  
  90. fprintf(log,"\n a=\n");
  91. for(i=0; i< n; i++)
  92. {
  93. for(j=0; j< n; j++)
  94. {
  95. *(a+n*i+j) = 1.999 * (drand48() - 0.5) / n;
  96. fprintf(log,"%10.6f ", *(a+n*i+j) );
  97. }
  98. fprintf(log,"\n");
  99. }
  100.  
  101.  
  102. // Generate vector b
  103. fprintf(log,"\n b=\n");
  104. for(i=0; i< n; i++)
  105. {
  106. b[i] = 10.0 * drand48();
  107. printf(log,"%10.6f ", b[i]);
  108. }
  109. fprintf(log,"\n");
  110.  
  111. // Initialize t
  112. for(i=0; i< n; i++)
  113. {
  114. t[i] = b[i];
  115. }
  116.  
  117. // Creat our threads
  118. pthread_t thread[tcount];
  119.  
  120. // Do stuff!
  121. fprintf(log,"\n itt error\n");
  122. for(itt=0; itt<=itt_max; itt++)
  123. {
  124. error=0.0;
  125. for(i=0; i< n; i++)
  126. {
  127. sum = 0.0;
  128. for(j=0; j< n; j++)
  129. {
  130. sum += *(a+n*i+j) * t[j];
  131. }
  132. t1[i] = sum + b[i];
  133. errori = fabs(t1[i]-t[i]);
  134. if(errori > error)
  135. {
  136. error=errori;
  137. }
  138. }
  139. ttemp = t1;
  140. t1 = t;
  141. t = ttemp;
  142. fprintf(log,"%5d %14.6e\n", itt, error);
  143. }
  144.  
  145. return(0);
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement