1#define  REAL_DATATYPE rk8
2
3! module for producing matrix traces, to be plotted by provided python plotter
4! currently the module is very simple and non-flexible
5! it is only usable for printing the matrix A and possibly its counterpart A_DEV
6! both are assumed to be in block-cyclic distribution
7! At the moment, the output works for double real only
8! To simplify things, a convenience macro (as follows) can be placed in a template file:
9
10! #undef SAVE_MATR
11! #ifdef DOUBLE_PRECISION_REAL
12! #define SAVE_MATR(name, iteration) \
13! call prmat(na,useGpu,a_mat,a_dev,lda,matrixCols,nblk,my_prow,my_pcol,np_rows,np_cols,name,iteration)
14! #else
15! #define SAVE_MATR(name, iteration)
16! #endif
17
18! traces are stored into directory "matrices", that has to be created
19
20module matrix_plot
21
22  contains
23
24    subroutine prmat(na, useGpu, a_mat, a_dev, lda, matrixCols, nblk, my_prow, my_pcol, np_rows, np_cols, name, iteration)
25      use cuda_functions
26      use iso_c_binding
27      use precision
28      implicit none
29      integer, parameter :: out_unit=20
30      character(len = 1025) :: directory = "matrices"
31      character(len = 1024) :: filename
32
33      character(len = *), intent(in)             :: name
34      integer(kind=ik), intent(in)                  :: na, lda, nblk, matrixCols, my_prow, my_pcol, np_rows, np_cols, iteration
35      real(kind=REAL_DATATYPE), intent(in)          :: a_mat(lda,matrixCols)
36      integer(kind=C_intptr_T), intent(in)          :: a_dev
37      logical, intent(in)                           :: useGPU
38
39      integer(kind=ik)                              :: row, col, mpi_rank
40      integer(kind=ik), save                        :: counter = 0
41      real(kind=REAL_DATATYPE)                      :: a_dev_helper(lda,matrixCols)
42      logical                                       :: successCUDA
43      integer(kind=c_size_t), parameter             :: size_of_datatype = size_of_double_real
44
45      mpi_rank = np_rows * my_pcol + my_prow
46
47      ! print a_mat
48      write(filename, "(A,A,I0.4,A,I0.2,A)") trim(directory), "/a_mat-", counter, "-", mpi_rank, ".txt"
49      write(*,*) trim(filename)
50      open(unit=out_unit, file=trim(filename), action="write",status="replace")
51
52      write(out_unit, "(9I5)") na, nblk, lda, matrixCols, my_prow, my_pcol, np_rows, np_cols, iteration
53      write(out_unit, "(A)") name
54      do row = 1, lda
55          write(out_unit, *) a_mat(row, :)
56      end do
57      close(out_unit)
58
59      ! print a_dev
60
61      if(useGpu) then
62        successCUDA = cuda_memcpy(loc(a_dev_helper(1,1)), a_dev, lda * matrixCols * size_of_datatype, cudaMemcpyDeviceToHost)
63
64        write(filename, "(A,A,I0.4,A,I0.2,A)") trim(directory), "/a_dev-", counter, "-", mpi_rank, ".txt"
65        write(*,*) trim(filename)
66        open(unit=out_unit, file=trim(filename), action="write",status="replace")
67
68        write(out_unit, "(9I5)") na, nblk, lda, matrixCols, my_prow, my_pcol, np_rows, np_cols, iteration
69        write(out_unit, "(A)") name
70        do row = 1, lda
71            write(out_unit, *) a_dev_helper(row, :)
72        end do
73        close(out_unit)
74      end if
75
76      counter = counter + 1
77
78    end subroutine
79
80end module
81