Advertisement
Guest User

Untitled

a guest
May 12th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.28 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "mmio.h"
  4. #include "math.h"
  5. #include <sys/time.h>
  6. #include "omp.h"
  7. #include <unistd.h>
  8.  
  9. double wtime()
  10. {
  11. struct timeval t;
  12. gettimeofday(&t, NULL);
  13. return (double)t.tv_sec + (double)t.tv_usec * 1E-6;
  14. }
  15.  
  16. void quicksort(double* a, double* vindex, int* rindex, int* cindex, int n)
  17. {
  18. int i, j, m;
  19. double p, t, s;
  20. if (n < 2)
  21. return;
  22. p = vindex[n / 2];
  23.  
  24. for (i = 0, j = n - 1;; i++, j--) {
  25. while (vindex[i]<p)
  26. i++;
  27. while (p<vindex[j])
  28. j--;
  29. if (i >= j)
  30. break;
  31. t = a[i];
  32. a[i] = a[j];
  33. a[j] = t;
  34.  
  35. s = vindex[i];
  36. vindex[i] = vindex[j];
  37. vindex[j] = s;
  38.  
  39. m = rindex[i];
  40. rindex[i] = rindex[j];
  41. rindex[j] = m;
  42.  
  43. m = cindex[i];
  44. cindex[i] = cindex[j];
  45. cindex[j] = m;
  46. }
  47. quicksort(a, vindex, rindex, cindex, i);
  48. quicksort(a + i, vindex + i, rindex + i, cindex + i, n - i);
  49. }
  50.  
  51. int main(int argc, char *argv[])
  52. {
  53. int j;
  54. int i;
  55. double t;
  56. int ret_code;
  57. MM_typecode matcode;
  58. FILE *f;
  59. int M, N, nz, *row, *col;
  60. double *val, *vector, *result, *result_t, *check_result, *check_result_t;
  61.  
  62. if (argc < 2)
  63. {
  64. fprintf(stderr, "Usage: %s [martix-market-filename]\n", argv[0]);
  65. exit(1);
  66. }
  67. else
  68. {
  69. if ((f = fopen(argv[1], "r")) == NULL)
  70. exit(1);
  71. }
  72.  
  73. if (mm_read_banner(f, &matcode) != 0)
  74. {
  75. printf("Could not process Matrix Market banner.\n");
  76. exit(1);
  77. }
  78.  
  79. if (mm_is_complex(matcode) && mm_is_matrix(matcode) &&
  80. mm_is_sparse(matcode) )
  81. {
  82. printf("Sorry, this application does not support ");
  83. printf("Market Market type: [%s]\n", mm_typecode_to_str(matcode));
  84. exit(1);
  85. }
  86.  
  87. if ((ret_code = mm_read_mtx_crd_size(f, &M, &N, &nz)) !=0)
  88. exit(1);
  89.  
  90. row = (int *) malloc(nz * sizeof(int));
  91. col = (int *) malloc(nz * sizeof(int));
  92. val = (double *) malloc(nz * sizeof(double));
  93.  
  94. for (int i = 0; i < nz; i++)
  95. {
  96. fscanf(f, "%d %d %lg\n", &row[i], &col[i], &val[i]);
  97. row[i]--;
  98. col[i]--;
  99. }
  100.  
  101. if (f !=stdin) fclose(f);
  102.  
  103. //->> Сортировка строк
  104. double *vIndex;
  105. vIndex = (double*)malloc(nz*sizeof(double));
  106. memset(vIndex, 0, nz*sizeof(double));
  107. for (int i = 0; i < nz; i++)
  108. {
  109. vIndex[i] = (double)row[i] * N + col[i];
  110. if (vIndex[i] < 0)
  111. {
  112. printf("Error!\n");
  113. exit(1);
  114. }
  115. }
  116.  
  117. quicksort(val, vIndex, row, col, nz);
  118.  
  119. //->> Конвертер
  120. int *RowIndex;
  121. RowIndex = (int*)malloc((N + 1)*sizeof(int));
  122. RowIndex[0] = 0;
  123. int row1 = 0;
  124. int num, num2;
  125. i = 0;
  126. for (i = 1; i < nz; i++){
  127. if (row[i] != row[i-1]){
  128. row1++;
  129. RowIndex[row1] = i;
  130. num = row[i] - row[i-1];
  131. if (num > 1){
  132. num2 = row1 + num - 1;
  133. for (int j = row1; j < num2; j++){
  134. row1++;
  135. RowIndex[row1] = i;
  136. }
  137. }
  138. }
  139. }
  140. row1++;
  141. RowIndex[row1] = i;
  142.  
  143. t = wtime();
  144. vector = (double*)malloc(sizeof(double)*nz);
  145. result = (double*)malloc(sizeof(double)*nz);
  146. result_t = (double*)malloc(sizeof(double)*nz);
  147. for (int i = 0; i < N; i++)
  148. {
  149. vector[i] = i + 2.00;
  150. }
  151.  
  152. //************************************************************* умножение *************************************************
  153.  
  154.  
  155.  
  156. #pragma omp parallel for private(i, j)
  157. for(i = 0; i < N; i++ )
  158. {
  159. result[i] = 0;
  160. for (j = RowIndex[i]; j < RowIndex[i + 1]; j++)
  161. result[i] += val[j] * vector[col[j]];
  162.  
  163. }
  164.  
  165.  
  166.  
  167.  
  168. //----------------------------------------------------------------------------------------------------------------------------------------
  169. //************************************************************* ТРАНСПОНИРОВАНИЕ *************************************************
  170.  
  171. for (int i = 0; i < N; ++i)
  172. {
  173. result_t[i] = 0.0;
  174. }
  175.  
  176. #pragma omp parallel for private(i, j)
  177. for (i = 0; i < N; i++)
  178. {
  179.  
  180. for(j = RowIndex[i]; j < RowIndex[i+1]; j++)
  181. {
  182. if (i != col[j])
  183. {
  184.  
  185. result_t[col[j]] += val[j] * vector[i];
  186. }
  187. }
  188. }
  189.  
  190.  
  191. //----------------------------------------------------------------------------------------------------------------------------------------
  192.  
  193. //************************************************************* проверка *************************************************
  194. check_result = (double*)malloc(sizeof(double)*nz);
  195. check_result_t = (double*)malloc(sizeof(double)*nz);
  196. for (i = 0; i < N; i++)
  197. {
  198. check_result[i] = 0;
  199. check_result_t[i] = 0;
  200. for ( j=RowIndex[i]; j<RowIndex[i + 1]; j++)
  201. {
  202. check_result[i] += val[j] * vector[col[j]];
  203. if (i != col[j])
  204. check_result_t[col[j]] += val[j] * vector[i];
  205.  
  206. }
  207. }
  208.  
  209. ///check multiplication
  210. for (int i = 0; i < N; ++i)
  211. {
  212. if (result[i] == check_result[i])
  213. continue;
  214. else
  215. {
  216. printf("error-->>%d\n", i);
  217. printf("check_result_t[%d] = %0.12f\t result[%d] = %0.12f\n",i, check_result[i], i, result[i]);
  218. printf("Vectors not match\n");
  219. break;
  220. }
  221. }
  222.  
  223. ///check transposition
  224. for (int i = 0; i < N; ++i)
  225. {
  226. if (result_t[i] == check_result_t[i])
  227. continue;
  228. else
  229. {
  230. printf("error-->>%d\n", i);
  231. printf("check_result_t[%d] = %0.12f\t result_t[%d] = %0.12f\n",i, check_result_t[i], i, result_t[i]);
  232. printf("Vectors not match\n");
  233. break;
  234. }
  235. }
  236. return 0;
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement