Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentes Révision précédente
Prochaine révision
Révision précédente
bibliotheques_scientifiques [2015/02/25 08:45] – modification externe 127.0.0.1bibliotheques_scientifiques [2016/12/15 14:34] (Version actuelle) – [BLAS] admin
Ligne 16: Ligne 16:
 **BLAS 1**  : opérations de type vecteur-vecteur **BLAS 1**  : opérations de type vecteur-vecteur
  
-                   y  y x+                   y = alpha.x + y
 **BLAS 2**  : opérations de type vecteur-matrice **BLAS 2**  : opérations de type vecteur-matrice
  
-                     y   y A+                     y = alpha.A.x + beta.y
  
 **BLAS 3** : opérations de type matrice-matrice **BLAS 3** : opérations de type matrice-matrice
  
-                      C  C A B+                      C + alpha.A.B + beta.C
  
  
Ligne 52: Ligne 52:
 $ gfortran stod.f -o stod -lblas $ gfortran stod.f -o stod -lblas
 </code> </code>
 +
 +===== Appel de DGEMM depuis C/C++=====
 +
 +<file c demm.c>
 +#include <stdio.h>
 +#include <stdlib.h>
 + 
 +#include <errno.h> 
 +
 +extern void dgemm_(const char*, const char*, int*, int*,int*, double*, double*, int*, double*, int*, double*, double*, int*);
 +
 +
 +int main(int argc, char **argv){
 + 
 +  int i,j;
 +  double one = 1.0;
 +  double zero = 0.0;
 +
 +  int n = 1024;
 +
 +  if(argc==2)
 +  {
 +      n=atoi(argv[1]);
 +  }
 +
 +
 +
 +  double *A = (double*)malloc(n*n*sizeof(double));
 +
 +  double *B = (double*)malloc(n*n*sizeof(double));
 +
 +  double *C = (double*)malloc(n*n*sizeof(double));
 +
 +  if(C==NULL || B==NULL || A==NULL){
 +
 +      fprintf(stderr, "Error allocating matrix");
 +
 +      if(A!=NULL) free(A);
 +      if(B!=NULL) free(B);
 +      if(B!=NULL) free(C);
 +
 +      return -1;
 +   }
 +
 +  for(i = 0; i < n*n; i++) {
 +      A[i] = i;
 +      B[i] = i;
 +  }
 + 
 +  //make BLAS 3  matrix matrix operation
 +  //C = C + alpha.A.B + beta.C
 +  dgemm_("N", "N", &n, &n, &n, &one, A, &n, B, &n, &zero, C, &n);
 +
 + // #elif defined(CBLAS) || defined(MKL) || defined(VECLIB)
 +  
 +
 +
 +for(i=n-2; i<n;i++){
 +      
 +   for(j=n-2;j<n;j++){
 +               
 +              printf(" %5.1f ", C[j+n*i]);
 +       }
 +     printf("\n");  
 +}
 +
 +
 + free(A);
 + free(B);
 + free(C);
 +
 +  return(0);
 +}
 +
 +
 +</file>
 +
  
 ====== LAPACK ====== ====== LAPACK ======
Ligne 62: Ligne 139:
  
 **Exemple : ** appel de la routine ''DGESV'' pour résoudre un système linéaire. **Exemple : ** appel de la routine ''DGESV'' pour résoudre un système linéaire.
-<code fortran> +<file fortran randomsys.f90 >
-!­­­­­Appel a LAPACK pour retrouver la solution V tq A*V=U +
-  INFO = 0 +
-  NRHS = 1 +
-  LDA  = N +
-  LDB  = N +
-  CALL DGESV (N, NRHS, A, LDA, IPIV, U, LDB, INFO)+
  
-</code>+program randomsys 
 +    implicit none 
 +    real(kind=8), dimension(:),allocatable :: x,b 
 +    real(kind=8), dimension(:,:),allocatable :: a 
 +    real(kind=8) :: err 
 +    integer :: i, info, lda, ldb, nrhs, n 
 +    integer, dimension(:), allocatable :: ipiv 
 + 
 +    ! initialize random number generator seed 
 +    ! if you remove this, the same numbers will be generated each 
 +    ! time you run this code. 
 +    call init_random_seed()   
 + 
 +    print *, "Input n ... " 
 +    read *, n 
 + 
 +    allocate(a(n,n)) 
 +    allocate(b(n)) 
 +    allocate(x(n)) 
 +    allocate(ipiv(n)) 
 + 
 +    call random_number(a) 
 +    call random_number(x) 
 +    b = matmul(a,x) ! compute RHS 
 +     
 +    nrhs = 1 ! number of right hand sides in b 
 +    lda = n  ! leading dimension of a 
 +    ldb = n  ! leading dimension of b 
 + 
 +    call dgesv(n, nrhs, a, lda, ipiv, b, ldb, info) 
 + 
 +    ! Note: the solution is returned in b 
 +    ! and a has been changed. 
 + 
 +    ! compare computed solution to original x: 
 +    print *, "         x          computed       rel. error" 
 +    do i=1,n 
 +        err = abs(x(i)-b(i))/abs(x(i)) 
 +        print '(3d16.6)', x(i),b(i),err 
 +        enddo 
 + 
 +    deallocate(a,b,ipiv) 
 + 
 +end program randomsys 
 + 
 +</file>
  
 **Compilation :** **Compilation :**
 <code> <code>
-$ gfortran appli.f -o appli -lblas -llapack+$ gfortran randomsys.f90 -o appli -lblas -llapack
 </code> </code>
  
  • bibliotheques_scientifiques.1424853941.txt.gz
  • Dernière modification : 2016/03/18 12:32
  • (modification externe)