PETSc - MatMultScale? Matrix X vector X scalar -
PETSc - MatMultScale? Matrix X vector X scalar -
i'm using petsc , wanted like,
i know can do:
mat vec x,y matmult(a,x,y) vecscale(y,0.5)
i curious if there function of these in 1 shot. seems save loop.
matmultscale(a,x,0.5,y)
does such function exist?
this function (or close) not seems in list of functions operating on mat. brief reply question be...no.
if utilize $y=\frac12 ax$, solution scale matrix 1 time all, using matscale(a,0.5);
.
would such function useful ? 1 way check utilize -log_summary
alternative of petsc, profiling information. if matrix dense, see time spent in matmult()
much larger time spent in vecscale()
. question meaningful if sparce matrix handled, few non-null terms per line.
here code test it, using 2xidentity matrix :
static char help[] = "tests solving linear scheme on 0 0 matrix.\n\n"; #include <petscksp.h> #undef __funct__ #define __funct__ "main" int main(int argc,char **args) { vec x, y; mat a; petscreal alpha=0.5; petscerrorcode ierr; petscint n=42; petscinitialize(&argc,&args,(char*)0,help); ierr = petscoptionsgetint(null,"-n",&n,null);chkerrq(ierr); /* create vector*/ ierr = veccreate(petsc_comm_world,&x);chkerrq(ierr); ierr = vecsetsizes(x,petsc_decide,n);chkerrq(ierr); ierr = vecsetfromoptions(x);chkerrq(ierr); ierr = vecduplicate(x,&y);chkerrq(ierr); /* create matrix. when using matcreate(), matrix format can specified @ runtime. performance tuning note: problems of substantial size, preallocation of matrix memory crucial attaining performance. see matrix chapter of users manual details. */ ierr = matcreate(petsc_comm_world,&a);chkerrq(ierr); ierr = matsetsizes(a,petsc_decide,petsc_decide,n,n);chkerrq(ierr); ierr = matsetfromoptions(a);chkerrq(ierr); ierr = matsetup(a);chkerrq(ierr); /* matrix diagonal, 2 times identity should have preallocated, shame */ petscint i,col; petscscalar value=2.0; (i=0; i<n; i++) { col=i; ierr = matsetvalues(a,1,&i,1,&col,&value,insert_values);chkerrq(ierr); } ierr = matassemblybegin(a,mat_final_assembly);chkerrq(ierr); ierr = matassemblyend(a,mat_final_assembly);chkerrq(ierr); /* let's 42 times nil : */ for(i=0;i<42;i++){ ierr = matmult(a,x,y);chkerrq(ierr); ierr = vecscale(y,alpha);chkerrq(ierr); } ierr = vecdestroy(&x);chkerrq(ierr); ierr = vecdestroy(&y);chkerrq(ierr); ierr = matdestroy(&a);chkerrq(ierr); ierr = petscfinalize(); homecoming 0; }
the makefile :
include ${petsc_dir}/conf/variables include ${petsc_dir}/conf/rules include ${petsc_dir}/conf/test clinker=g++ : ex1 ex1 : main.o chkopts ${clinker} -w -o main main.o ${petsc_lib} ${rm} main.o run : mpirun -np 2 main -n 10000000 -log_summary -help -mat_type mpiaij
and here resulting 2 lines of -log_summary
reply question :
event count time (sec) flops --- global --- --- stage --- total max ratio max ratio max ratio mess avg len reduct %t %f %m %l %r %t %f %m %l %r mflop/s ------------------------------------------------------------------------------------------------------------------------ --- event stage 0: main stage vecscale 42 1.0 1.0709e+00 1.0 2.10e+08 1.0 0.0e+00 0.0e+00 0.0e+00 4 50 0 0 0 4 50 0 0 0 392 matmult 42 1.0 5.7360e+00 1.1 2.10e+08 1.0 0.0e+00 0.0e+00 0.0e+00 20 50 0 0 0 20 50 0 0 0 73
so 42 vecscale()
operations took 1 sec while 42 matmult()
operations took 5.7 seconds. suppressing vecscale()
operation speed code 20%, in best case. overhead due loop lower that. guess that's reason why function not exist.
i apologize poor performance of computer (392mflops vecscale()...). curious know happens on yours !
petsc
Comments
Post a Comment