Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <pthread.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. //#define MAX_SIZE 4096
  8. #define MAX_SIZE 16
  9.  
  10. typedef double matrix[MAX_SIZE][MAX_SIZE];
  11. matrix A; /*matrix A*/
  12. int N; /*matrix size*/
  13. int maxnum; /*max number of element*/
  14. char *Init; /*matrix init type*/
  15. int PRINT; /*print or not*/
  16. double b[MAX_SIZE]; /*vector b*/
  17. double y[MAX_SIZE]; /*vector y*/
  18.  
  19. typedef struct _Data
  20. {
  21. int row,col;
  22.  
  23. }Data;
  24. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  25.  
  26. //int Read_Options(int argc, char **argv);
  27. void Init_Default();
  28. void *function(void *arg);
  29. void Init_Matrix();
  30. void Print_Matrix();
  31. int Read_Options(int, char**);
  32. void work(void);
  33.  
  34. int main(int argc, char *argv[])
  35. {
  36. Init_Default();
  37. Read_Options(argc, argv);
  38. Init_Matrix();
  39. /*
  40. int NUM_THREADS, i;
  41. NUM_THREADS=8;
  42.  
  43. pthread_t threads[NUM_THREADS];
  44. int threadsInt[NUM_THREADS];
  45. Data dat[NUM_THREADS];
  46.  
  47. int step;
  48. step=MAX_SIZE/8;
  49. for(i=0;i<NUM_THREADS;i++)
  50. {
  51. dat[i].col=i*step;
  52. dat[i].row=i*step;
  53. threadsInt[i] = pthread_create(&threads[i], NULL, function, &dat[i]);
  54.  
  55. if(threadsInt[i])
  56. {
  57. fprintf(stderr,"Error - pthread_create() return code: %d\n",threadsInt[i]);
  58. exit(EXIT_FAILURE);
  59. }
  60. }
  61.  
  62. int k;
  63. for(k=0;k<NUM_THREADS;k++)
  64. {
  65. pthread_join(threads[k], NULL);
  66. }
  67. */
  68. printf("\nprintInnanWork : %d", PRINT);
  69. work();
  70. printf("\nPrintEfterWOrk : %d", PRINT);
  71.  
  72. if(PRINT==1)
  73. Print_Matrix();
  74.  
  75. return 0;
  76. }
  77.  
  78. void work(void)
  79. {
  80. int i,j,k;
  81. printf("print i work : %d", PRINT);
  82. for(k=0;k<N;k++)
  83. {
  84. for(j=k+1;j<N;j++)
  85. A[k][j]=A[k][j]/A[k][k];
  86. y[k]=b[k]/A[k][k];
  87. A[k][k]=1.0;
  88. printf("\nPRint mitt i work : %d\n", PRINT);
  89. for(i=k+1;i<N;i++)
  90. for(j=k+1;j<N;j++)
  91. A[i][j]=A[i][j]-A[i][k]*A[k][j];
  92. b[i]=b[i]-A[i][k]*y[k];
  93. A[i][k]=0.0;
  94. }
  95. printf("print i slutet utav work : %d", PRINT);
  96. }
  97.  
  98. void *function(void *arg)
  99. {
  100. pthread_mutex_lock(&mutex);
  101. Data *test =(Data*)arg;
  102.  
  103. int step;
  104. step=MAX_SIZE/8;
  105. int i, j;
  106.  
  107. for(i=test->col;i<(test->col+step);i++)
  108. {
  109. printf("i: %d\n", i);
  110. }
  111. //printf("row & col: %d \t %d\n", test->row, test->col);
  112. pthread_mutex_unlock(&mutex);
  113. }
  114.  
  115. void Init_Default()
  116. {
  117. /* N=2048;
  118. Init = "rand";
  119. maxnum=15.0;
  120. PRINT=0;
  121. */
  122.  
  123. N=16;
  124. Init="fast";
  125. maxnum=15.0;
  126. PRINT=0;
  127. }
  128.  
  129. void Init_Matrix()
  130. {
  131. int i,j;
  132.  
  133. printf("\nsize = %d,x%d", N, N);
  134. printf("\nmaxnum = %d\n", maxnum);
  135. printf("Init =%s\n", Init);
  136. printf("Initializing matrix..");
  137.  
  138. if(strcmp(Init, "rand")==0)
  139. {
  140. for(i=0;i<N;i++)
  141. {
  142. for(j=0;j<N;j++)
  143. {
  144. if(i==j) /*diagonal dominance*/
  145. A[i][j]=(double)(rand() % maxnum) + 5.0;
  146. else
  147. A[i][j]=(double)(rand() % maxnum) +1.0;
  148. }
  149. }
  150. }
  151.  
  152. if(strcmp(Init, "fast")==0)
  153. {
  154. for(i=0;i<N;i++)
  155. {
  156. for(j=0;j<N;j++)
  157. {
  158. if(i==j) /*diagonal dominance*/
  159. A[i][j]=5.0;
  160. else
  161. A[i][j]=2.0;
  162. }
  163. }
  164. }
  165.  
  166. /*Initialize vector b & y */
  167. for(i=0;i<N;i++)
  168. {
  169. b[i]=2.0;
  170. y[i]=1.0;
  171. }
  172.  
  173. printf("Done\n\n");
  174. /*
  175. if(PRINT==1)
  176. Print_Matrix();
  177. */
  178. }
  179.  
  180. void Print_Matrix()
  181. {
  182. int i,j;
  183. printf("\ni print matrix\n");
  184. printf("Matrix A:\n");
  185. for(i=0;i<N;i++)
  186. {
  187. printf("[");
  188. for(j=0;j<N;j++)
  189. {
  190. printf("%5.2f,", A[i][j]);
  191. }
  192. printf("]\n");
  193. }
  194.  
  195. printf("Vector B:\n[");
  196. for(j=0;j<N;j++)
  197. printf("%5.2f,",b[j]);
  198. printf("]\n");
  199.  
  200. printf("Vector Y:\n[");
  201. for(j=0;j<N;j++)
  202. printf("%5.2f,",y[j]);
  203. printf("]\n");
  204. printf("\n\n");
  205.  
  206. }
  207.  
  208. int Read_Options(int argc, char **argv)
  209. {
  210. char *prog;
  211.  
  212. prog =*argv;
  213. while(++argv, --argc > 0)
  214. if(**argv == '-')
  215. {
  216. switch(*++*argv)
  217. {
  218. case 'n':
  219. --argc;
  220. N = atoi(*++argv);
  221. break;
  222.  
  223. case 'h':
  224. printf("\nHELP: try sor -u \n\n");
  225. exit(0);
  226. break;
  227.  
  228. case 'u':
  229. printf("\nUsage: sor [-n problemsize]\n");
  230. printf(" [-D] show default values\n");
  231. printf(" [-h] help \n");
  232. printf(" [-I init_type] fast/rand\n");
  233. printf(" [-m maxnum] max random nr\n");
  234. printf(" [-P print_switch] 0/1\n");
  235. exit(0);
  236. break;
  237.  
  238. case 'D':
  239. printf("\nDefault: n =%d", N);
  240. printf("\n Init =rand");
  241. printf("\n maxnum =5");
  242. printf("\n P =0\n\n");
  243. exit(0);
  244. break;
  245.  
  246. case 'I':
  247. --argc;
  248. Init = *++argv;
  249. break;
  250.  
  251. case 'm':
  252. --argc;
  253. maxnum=atoi(*++argv);
  254. break;
  255.  
  256. case 'p':
  257. --argc;
  258. PRINT=atoi(*++argv);
  259. break;
  260.  
  261. default:
  262. printf("%s: ignored option: -%s\n", prog, *argv);
  263. printf("Help: try %s -u \n\n", prog);
  264. break;
  265. }
  266. }
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement