In parallel computing, it is often necessary to call the functions in the SCALAPCK (parallelized lapack) function library to program, here is a brief introduction to the C language how to call the matrix vector multiplication in the SCALAPCK function.
Note: The functions in ScaLAPACK are written in Fortran, and the matrices are stored in columns.
ScaLAPACK links need to be Blas, so make sure Blas, Gfortran is installed on this machine.
The following is an example of matrix vector multiplication (for a simple calculation, the number of processes set in the program is 4):
#include <stdio.h>#include<string.h>#include<stdlib.h>#include"mpi.h"#defineAA (i,j) aa[(i) *m+ (j)]
intMainintargcChar**argv) { intI, J, K;/************ MPI ***************************/ intMyrank_mpi, Nprocs_mpi; Mpi_init (&ARGC, &argv); Mpi_comm_rank (Mpi_comm_world,&Myrank_mpi); Mpi_comm_size (Mpi_comm_world,&nprocs_mpi);/************ Blacs ***************************/ intIctxt, Nprow, Npcol, Myrow, MYCOL,NB; intinfo,itemp; intzero=0, one=1; Nprow=2; Npcol=2; NB=1; Cblacs_pinfo (&myrank_mpi, &Nprocs_mpi); Cblacs_get (-1,0, &ictxt); Cblacs_gridinit (&ictxt,"Row", Nprow, Npcol); Cblacs_gridinfo (Ictxt,&nprow, &npcol, &myrow, &MyCol); intM =4; intdesca[9],descx[9],descy[9]; intMA = Numroc_ (&m, &NB, &myrow, &zero, &Nprow); intNA = Numroc_ (&m, &NB, &mycol, &zero, &Npcol); intNX = Numroc_ (&m, &NB, &myrow, &zero, &Nprow); intmy = Numroc_ (&m, &NB, &myrow, &zero, &Nprow); Descinit_ (Desca,&m, &m, &NB, &NB, &zero, &zero, &ictxt, &ma, &info); Descinit_ (Descx,&m, &one, &NB, &one, &zero, &zero, &ictxt, &nx, &info); Descinit_ (Descy,&m, &one, &NB, &one, &zero, &zero, &ictxt, &my, &info); Double*x = (Double*)malloc(nx*sizeof(Double)); Double*y = (Double*)calloc(My,sizeof(Double)); Double*a = (Double*)malloc(ma*na*sizeof(Double)); intSat,sut; for(i=0; i<ma;i++) for(j=0; j<na;j++) A[j*ma+i]=1.0; for(i=0; i<nx;i++) X[i]=1.0; DoubleAlpha =1.0;DoubleBeta =0.0; Pdgemv_ ("N", &m, &m, &alpha, A, &one, &one, Desca, X, &one, &one,descx, &one, &beta, y, &ONE, &one, Descy, &One ); for(i=0; i<my;i++) printf ("rank=%d,%f \ n", Myrank_mpi,y[i]); Mpi_finalize (); return 0;}
Compile the build executable file:
Mpicc-o PDGEMV pdgemv.c-l/opt/scalapack/scalapck-2.0. 2/-lscalapack-lblas-lgfortran
Run:
4 ./pdgemv
How the C language in Linux system calls functions in ScaLAPACK