#include <stdio.h>
#include <stdlib.h>
void dot_crs_cpu( double * b, double * val, double * x, int * row_ind, int * col_ind) ;
int main( void ) {
int n;
printf ( "please type dimention n.\n " ) ; //今回のテストデータではn=5を入力
double * matrix_a;
double * vector_b;
double * vector_x;
matrix_a
= ( double * ) malloc ( sizeof ( double ) * ( n
* n
) ) ; vector_b
= ( double * ) malloc ( sizeof ( double ) * n
) ; vector_x
= ( double * ) malloc ( sizeof ( double ) * n
) ;
double * val;
val
= ( double * ) malloc ( sizeof ( double ) * ( n
* n
) ) ;
static int * row_ind, * col_ind, * row_ptr;
row_ind
= ( int * ) malloc ( sizeof ( int ) * ( n
* n
) ) ; col_ind
= ( int * ) malloc ( sizeof ( int ) * ( n
* n
) ) ; row_ptr
= ( int * ) malloc ( sizeof ( int ) * ( n
* n
) ) ;
double * p_val= val;
int * p_row_ind = row_ind,* p_col_ind = col_ind;
/*
p_row_ind = (int *)malloc( sizeof( int ) * (n*n) );
p_col_ind = (int *)malloc( sizeof( int ) * (n*n) );
*p_row_ind=row_ind,*p_col_ind=col_ind;
*/
int i, j, c;
FILE * fin,* fout;
char inputfile[ 50 ] , outputfile[ 50 ] ;
printf ( "please type input file name.\n " ) ; fin
= fopen ( inputfile
, "r" ) ; if ( fin== NULL)
{
}
printf ( "please type output file name.\n " ) ; fout
= fopen ( outputfile
, "w" ) ; if ( fout== NULL)
{
printf ( "do not maike file : output_sp.dat\n " ) ; }
for ( i = 0 ; i < n ; i++ )
{
for ( j = 0 ; j < n ; j++ )
{
fscanf ( fin
, "%lf" , & matrix_a
[ n
* i
+ j
] ) ; fprintf ( fout
, "%5.2f\t " , matrix_a
[ n
* i
+ j
] ) ; }
}
for ( i = 0 ; i < n ; i++ )
{
fscanf ( fin
, "%lf" , & vector_b
[ i
] ) ; fprintf ( fout
, "%5.2f\t " , vector_b
[ i
] ) ; }
for ( i = 0 ; i < n ; i++ )
{
fscanf ( fin
, "%lf" , & vector_x
[ i
] ) ; fprintf ( fout
, "%5.2f\t " , vector_x
[ i
] ) ; }
//ターミナルへ出力
for ( i = 0 ; i < n; i ++ )
{
for ( j = 0 ; j < n; j ++ )
{
printf ( "matrix_a[%d][%d] = %lf\n " , i
, j
, matrix_a
[ n
* i
+ j
] ) ; }
}
for ( i = 0 ; i < n; i ++ )
{
printf ( "matrix_b[%d] = %lf\n " , i
, vector_b
[ i
] ) ; }
for ( i = 0 ; i < n; i ++ )
{
printf ( "matrix_x[%d] = %lf\n " , i
, vector_x
[ i
] ) ; }
//matrix_aをCRS形式へ変換
for ( i = 0 , c = 0 ; i < n ; i ++ ) {
for ( j = 0 ; j < n; j ++ ) {
if ( matrix_a[ n * i + j] ) {
* p_val++ = matrix_a[ n * i + j] ;
* p_row_ind++ = i+ 1 ;
* p_col_ind++ = j+ 1 ;
c++;
}
}
}
for ( i= 0 ; i< c; i++ )
{
if ( row_ptr[ row_ind[ i] - 1 ] == 0 )
{
row_ptr[ row_ind[ i] - 1 ] = i+ 1 ;
}
}
//ターミナルへ出力
for ( i= 0 ; i< c; i++ ) {
}
/*
printf("\nrow_ind =");
for(i=0;i<c;i++){
printf(" %d", row_ind[i]);
}
*/
for ( i= 0 ; i< c; i++ ) {
}
for ( i= 0 ; i< n; i++ ) {
}
dot_crs_cpu( vector_b, vector_x, val, col_ind, row_ptr) ;
for ( i = 0 ; i < n; i ++ )
{
printf ( "matrix_b[%d] = %lf\n " , i
, vector_b
[ i
] ) ; }
return 0 ;
}
/*test.dat
1.000000 0.000000 3.000000 0.000000 2.000000
0.000000 1.000000 0.000000 4.000000 6.000000
2.000000 5.000000 3.000000 0.000000 0.000000
0.000000 0.000000 7.000000 8.000000 0.000000
0.000000 1.000000 0.000000 3.000000 9.000000
*/
/*output_double
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
row_ind = 1 1 1 2 2 2 3 3 3 4 4 5 5 5
col_ind = 1 3 5 2 4 5 1 2 3 3 4 2 4 5
row_ptr = 1 4 7 10 12
*/
void dot_crs_cpu( double * b, double * x, double * val, int * col_ind, int * row_ptr) {
// Ax = b
int i, j, n;
for ( i = 0 ; i < n ; i++ ) {
ksl b[ i] = 0 ;
for ( j = row_ptr[ i] ; j < row_ptr[ i + 1 ] ; j++ ) {
b[ i] += val[ j] * x[ col_ind[ j] ] ;
printf ( "vector_b[%d] = %lf" , n
, b
[ i
] ) ; }
}
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCgoKdm9pZCBkb3RfY3JzX2NwdShkb3VibGUgKmIsZG91YmxlICp2YWwsZG91YmxlICp4LGludCAqcm93X2luZCxpbnQgKmNvbF9pbmQpOwoKaW50IG1haW4odm9pZCl7CmludCBuOwpwcmludGYoInBsZWFzZSB0eXBlIGRpbWVudGlvbiBuLlxuIik7Ci8v5LuK5Zue44Gu44OG44K544OI44OH44O844K/44Gn44Gvbj0144KS5YWl5YqbCnNjYW5mKCIlZCIsJm4pOwoKZG91YmxlICptYXRyaXhfYTsKZG91YmxlICp2ZWN0b3JfYjsKZG91YmxlICp2ZWN0b3JfeDsKCm1hdHJpeF9hID0gKGRvdWJsZSopbWFsbG9jKHNpemVvZihkb3VibGUpKihuKm4pKTsKdmVjdG9yX2IgPSAoZG91YmxlKiltYWxsb2Moc2l6ZW9mKGRvdWJsZSkqbik7CnZlY3Rvcl94ID0gKGRvdWJsZSopbWFsbG9jKHNpemVvZihkb3VibGUpKm4pOwoKZG91YmxlICp2YWw7CnZhbCA9IChkb3VibGUqKW1hbGxvYyhzaXplb2YoZG91YmxlKSoobipuKSk7CgpzdGF0aWMgaW50ICpyb3dfaW5kLCAqY29sX2luZCwgKnJvd19wdHI7CnJvd19pbmQgPSAoaW50ICopbWFsbG9jKCBzaXplb2YoIGludCApICogKG4qbikgKTsKY29sX2luZCA9IChpbnQgKiltYWxsb2MoIHNpemVvZiggaW50ICkgKiAobipuKSApOwpyb3dfcHRyID0gKGludCAqKW1h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FbbippK2pdKTsKICAgICAgICAgICAgZnByaW50Zihmb3V0LCAiJTUuMmZcdCIsIG1hdHJpeF9hW24qaStqXSk7CiAgICAgICAgfQogICAgICAgIGZwcmludGYoIGZvdXQsICJcbiIpOwogICAgfQoKCWZwcmludGYoIGZvdXQsICJiXG4iKTsKICAgIGZvciggaSA9IDAgOyBpIDwgbiA7IGkrKykKICAgIHsKICAgICAgICBmc2NhbmYoZmluLCAiJWxmIiwgJnZlY3Rvcl9iW2ldKTsKICAgICAgICBmcHJpbnRmKGZvdXQsICIlNS4yZlx0IiwgdmVjdG9yX2JbaV0pOwogICAgICAgIGZwcmludGYoIGZvdXQsICJcbiIpOwogICAgfQogCiAKICAgIGZwcmludGYoIGZvdXQsICJ4XG4iKTsKICAgIGZvciggaSA9IDAgOyBpIDwgbiA7IGkrKykKICAgIHsKICAgICAgICBmc2NhbmYoZmluLCAiJWxmIiwgJnZlY3Rvcl94W2ldKTsKICAgICAgICBmcHJpbnRmKGZvdXQsICIlNS4yZlx0IiwgdmVjdG9yX3hbaV0pOwogICAgICAgIGZwcmludGYoIGZvdXQsICJcbiIpOwogICAgfQoKCS8v44K/44O844Of44OK44Or44G45Ye65YqbCglwcmludGYoIlxubWF0cml4X2FcbiIpOwoJZm9yKGkgPSAwOyBpIDwgbjsgaSArKykKICAgIHsKICAgICAgICBmb3IoaiA9IDA7IGogPCBuOyBqICsrKQogICAgICAgIHsKICAgICAgICAgIHByaW50ZigibWF0cml4X2FbJWRdWyVkXSA9ICVsZlxuIixpLGosbWF0cml4X2FbbiAqIGkgKyBqXSk7CgkJfQoJfQoKCXByaW50ZigiXG52ZWN0b3JfYlxuIik7Cglmb3IoaSA9IDA7IGkgPCBuOyBpICsrKQogICAgewogICAgICAgICAgcHJpbnRmKCJtYXRyaXhfYlslZF0gPSAlbGZcbiIsaSx2ZWN0b3JfYltpXSk7Cgl9CgoJcHJpbnRmKCJcbnZlY3Rvcl94XG4iKTsKCWZvcihpID0gMDsgaSA8IG47IG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