fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5.  
  6. void dot_crs_cpu(double *b,double *val,double *x,int *row_ind,int *col_ind);
  7.  
  8. int main(void){
  9. int n;
  10. printf("please type dimention n.\n");
  11. //今回のテストデータではn=5を入力
  12. scanf("%d",&n);
  13.  
  14. double *matrix_a;
  15. double *vector_b;
  16. double *vector_x;
  17.  
  18. matrix_a = (double*)malloc(sizeof(double)*(n*n));
  19. vector_b = (double*)malloc(sizeof(double)*n);
  20. vector_x = (double*)malloc(sizeof(double)*n);
  21.  
  22. double *val;
  23. val = (double*)malloc(sizeof(double)*(n*n));
  24.  
  25. static int *row_ind, *col_ind, *row_ptr;
  26. row_ind = (int *)malloc( sizeof( int ) * (n*n) );
  27. col_ind = (int *)malloc( sizeof( int ) * (n*n) );
  28. row_ptr = (int *)malloc( sizeof( int ) * (n*n) );
  29.  
  30. double *p_val=val;
  31. int *p_row_ind = row_ind,*p_col_ind = col_ind;
  32. /*
  33. p_row_ind = (int *)malloc( sizeof( int ) * (n*n) );
  34. p_col_ind = (int *)malloc( sizeof( int ) * (n*n) );
  35. *p_row_ind=row_ind,*p_col_ind=col_ind;
  36. */
  37. int i, j, c;
  38. FILE *fin,*fout;
  39. char inputfile[50],outputfile[50];
  40.  
  41. printf("please type input file name.\n");
  42. scanf("%s",inputfile);
  43. fin = fopen(inputfile ,"r");
  44. if(fin==NULL)
  45. {
  46. printf("no file\n");
  47. exit(1);
  48. }
  49.  
  50. printf("please type output file name.\n");
  51. scanf("%s",outputfile);
  52. fout = fopen(outputfile,"w");
  53. if(fout==NULL)
  54. {
  55. printf("do not maike file : output_sp.dat\n");
  56. exit(1);
  57. }
  58.  
  59. fprintf( fout, "A\n");
  60. for( i = 0 ; i < n ; i++)
  61. {
  62. for( j = 0 ; j < n ; j++)
  63. {
  64. fscanf(fin, "%lf", &matrix_a[n*i+j]);
  65. fprintf(fout, "%5.2f\t", matrix_a[n*i+j]);
  66. }
  67. fprintf( fout, "\n");
  68. }
  69.  
  70. fprintf( fout, "b\n");
  71. for( i = 0 ; i < n ; i++)
  72. {
  73. fscanf(fin, "%lf", &vector_b[i]);
  74. fprintf(fout, "%5.2f\t", vector_b[i]);
  75. fprintf( fout, "\n");
  76. }
  77.  
  78.  
  79. fprintf( fout, "x\n");
  80. for( i = 0 ; i < n ; i++)
  81. {
  82. fscanf(fin, "%lf", &vector_x[i]);
  83. fprintf(fout, "%5.2f\t", vector_x[i]);
  84. fprintf( fout, "\n");
  85. }
  86.  
  87. //ターミナルへ出力
  88. printf("\nmatrix_a\n");
  89. for(i = 0; i < n; i ++)
  90. {
  91. for(j = 0; j < n; j ++)
  92. {
  93. printf("matrix_a[%d][%d] = %lf\n",i,j,matrix_a[n * i + j]);
  94. }
  95. }
  96.  
  97. printf("\nvector_b\n");
  98. for(i = 0; i < n; i ++)
  99. {
  100. printf("matrix_b[%d] = %lf\n",i,vector_b[i]);
  101. }
  102.  
  103. printf("\nvector_x\n");
  104. for(i = 0; i < n; i ++)
  105. {
  106. printf("matrix_x[%d] = %lf\n",i,vector_x[i]);
  107. }
  108.  
  109.  
  110. //matrix_aをCRS形式へ変換
  111. for( i = 0, c = 0 ; i < n ; i ++){
  112. for( j = 0; j < n; j ++){
  113. if(matrix_a[n * i + j]){
  114. *p_val++ = matrix_a[n * i + j];
  115. *p_row_ind++ = i+1;
  116. *p_col_ind++ = j+1;
  117. c++;
  118. }
  119. }
  120. }
  121.  
  122. for(i=0;i<c;i++)
  123. {
  124. if(row_ptr[row_ind[i]-1]==0)
  125. {
  126. row_ptr[row_ind[i]-1]=i+1;
  127. }
  128. }
  129.  
  130. //ターミナルへ出力
  131. printf("\nval =");
  132. for(i=0;i<c;i++){
  133. printf(" %lf", val[i]);
  134. }
  135. /*
  136. printf("\nrow_ind =");
  137. for(i=0;i<c;i++){
  138. printf(" %d", row_ind[i]);
  139. }
  140. */
  141.  
  142. printf("\ncol_ind =");
  143. for(i=0;i<c;i++){
  144. printf(" %d", col_ind[i]);
  145. }
  146.  
  147. printf("\nrow_ptr =");
  148. for(i=0;i<n;i++){
  149. printf(" %d", row_ptr[i]);
  150. }
  151.  
  152.  
  153. dot_crs_cpu(vector_b,vector_x,val,col_ind,row_ptr);
  154.  
  155. printf("\nvector_b\n");
  156. for(i = 0; i < n; i ++)
  157. {
  158. printf("matrix_b[%d] = %lf\n",i,vector_b[i]);
  159. }
  160.  
  161.  
  162. fclose(fin);
  163. fclose(fout);
  164.  
  165. printf("\n");
  166.  
  167. return 0;
  168. }
  169.  
  170. /*test.dat
  171. 1.000000 0.000000 3.000000 0.000000 2.000000
  172. 0.000000 1.000000 0.000000 4.000000 6.000000
  173. 2.000000 5.000000 3.000000 0.000000 0.000000
  174. 0.000000 0.000000 7.000000 8.000000 0.000000
  175. 0.000000 1.000000 0.000000 3.000000 9.000000
  176. */
  177.  
  178. /*output_double
  179.  val = 1.000000 3.000000 2.000000 1.000000 4.000000 6.000000 2.000000 5.000000 3.000000 7.000000 8.000000 1.000000 3.000000 9.000000
  180. row_ind = 1 1 1 2 2 2 3 3 3 4 4 5 5 5
  181. col_ind = 1 3 5 2 4 5 1 2 3 3 4 2 4 5
  182. row_ptr = 1 4 7 10 12
  183.  */
  184.  
  185.  
  186. void dot_crs_cpu(double *b,double *x,double *val,int *col_ind,int *row_ptr){
  187. // Ax = b
  188. int i,j,n;
  189. for(i = 0 ; i < n ; i++) {
  190. ksl b[i] = 0;
  191. for(j = row_ptr[i] ; j < row_ptr[i + 1] ; j++) {
  192. b[i] += val[j] * x[ col_ind[j] ];
  193. printf("vector_b[%d] = %lf",n,b[i]);
  194. }
  195. }
  196. }
  197.  
  198.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function ‘main’:
prog.c:12: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
prog.c:42: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
prog.c:51: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
prog.c:64: warning: ignoring return value of ‘fscanf’, declared with attribute warn_unused_result
prog.c:73: warning: ignoring return value of ‘fscanf’, declared with attribute warn_unused_result
prog.c:82: warning: ignoring return value of ‘fscanf’, declared with attribute warn_unused_result
prog.c: In function ‘dot_crs_cpu’:
prog.c:190: error: ‘ksl’ undeclared (first use in this function)
prog.c:190: error: (Each undeclared identifier is reported only once
prog.c:190: error: for each function it appears in.)
prog.c:190: error: expected ‘;’ before ‘b’
stdout
Standard output is empty