MAGMA  1.2.0
MatrixAlgebraonGPUandMulticoreArchitectures
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
testing_zgetrf_gpu_f.f90
Go to the documentation of this file.
1 !
2 ! -- MAGMA (version 1.2.0) --
3 ! Univ. of Tennessee, Knoxville
4 ! Univ. of California, Berkeley
5 ! Univ. of Colorado, Denver
6 ! May 2012
7 !
8 ! @precisions normal z -> c d s
9 !
11 
12  use magma
13 
14  external cublas_init, cublas_set_matrix, cublas_get_matrix
15  external cublas_shutdown, cublas_alloc
16  external zlange, zgemm, zgesv, dlamch
17 
18  double precision zlange, dlamch
19  integer cublas_alloc
20 
21  double precision :: rnumber(2), anorm, bnorm, rnorm, xnorm
22  double precision, allocatable :: work(:)
23  complex*16, allocatable :: h_a(:), h_b(:), h_x(:)
24  magma_devptr_t :: devptra, devptrb
25  integer, allocatable :: ipiv(:)
26 
27  complex*16 :: zone, mzone
28  integer :: i, n, info, stat, lda
29  integer :: size_of_elt, nrhs
30  real(kind=8) :: flops, t, tstart, tend
31 
32  parameter( nrhs = 1, zone = 1., mzone = -1. )
33 
34  call cublas_init()
35 
36  n = 2048
37  lda = n
38  ldda = ((n+31)/32)*32
39  size_of_elt = sizeof_complex_16
40 
41 !------ Allocate CPU memory
42  allocate(h_a(lda*n))
43  allocate(h_b(lda*nrhs))
44  allocate(h_x(lda*nrhs))
45  allocate(work(n))
46  allocate(ipiv(n))
47 
48 !------ Allocate GPU memory
49  stat = cublas_alloc(ldda*n, size_of_elt, devptra)
50  if (stat .ne. 0) then
51  write(*,*) "device memory allocation failed"
52  stop
53  endif
54 
55  stat = cublas_alloc(ldda*nrhs, size_of_elt, devptrb)
56  if (stat .ne. 0) then
57  write(*,*) "device memory allocation failed"
58  stop
59  endif
60 
61 !---- Initializa the matrix
62  do i=1,lda*n
63  call random_number(rnumber)
64  h_a(i) = rnumber(1)
65  end do
66 
67  do i=1,lda*nrhs
68  call random_number(rnumber)
69  h_b(i) = rnumber(1)
70  end do
71  h_x(:) = h_b(:)
72 
73 !---- devPtrA = h_A
74  call cublas_set_matrix(n, n, size_of_elt, h_a, lda, devptra, ldda)
75 
76 !---- devPtrB = h_B
77  call cublas_set_matrix(n, nrhs, size_of_elt, h_b, lda, devptrb, ldda)
78 
79 !---- Call magma LU ----------------
80  call magma_wtime_f(tstart)
81  call magmaf_zgetrf_gpu(n, n, devptra, ldda, ipiv, info)
82  call magma_wtime_f(tend)
83 
84  if ( info .ne. 0 ) then
85  write(*,*) "Info : ", info
86  end if
87 
88 !---- Call magma solve -------------
89  call magmaf_zgetrs_gpu('n', n, nrhs, devptra, ldda, ipiv, devptrb, ldda, info)
90 
91  if ( info .ne. 0 ) then
92  write(*,*) "Info : ", info
93  end if
94 
95 !---- h_X = devptrB
96  call cublas_get_matrix(n, nrhs, size_of_elt, devptrb, ldda, h_x, lda)
97 
98 !---- Compare the two results ------
99  anorm = zlange('I', n, n, h_a, lda, work)
100  bnorm = zlange('I', n, nrhs, h_b, lda, work)
101  xnorm = zlange('I', n, nrhs, h_x, lda, work)
102  call zgemm('n', 'n', n, nrhs, n, zone, h_a, lda, h_x, lda, mzone, h_b, lda)
103  rnorm = zlange('I', n, nrhs, h_b, lda, work)
104 
105  write(*,*)
106  write(*,* ) 'Solving A x = b using LU factorization:'
107  write(*,105) ' || A || = ', anorm
108  write(*,105) ' || b || = ', bnorm
109  write(*,105) ' || x || = ', xnorm
110  write(*,105) ' || b - A x || = ', rnorm
111 
112  flops = 2. * n * n * n / 3.
113  t = tend - tstart
114 
115  write(*,*) ' Gflops = ', flops / t / 1e9
116  write(*,*)
117 
118  rnorm = rnorm / ( (anorm*xnorm+bnorm) * n * dlamch('E') )
119 
120  if ( rnorm > 60. ) then
121  write(*,105) ' Solution is suspicious, ', rnorm
122  else
123  write(*,105) ' Solution is CORRECT'
124  end if
125 
126 !---- Free CPU memory
127  deallocate(h_a, h_x, h_b, work, ipiv)
128 
129 !---- Free GPU memory
130  call cublas_free(devptra)
131  call cublas_free(devptrb)
132  call cublas_shutdown()
133 
134  105 format((a35,es10.3))
135 
136  end