csr_samples_m.f90 Source File


Source Code

module csr_samples_m
    !! Module specifying sample matrices
    use csrmat_m, only: csrmat_t
    implicit none

    public :: csr_random_init

contains

    subroutine csr_random_init(a, ndim, ncol, nnz)
        !! Fills up matrix a with random values
        type(csrmat_t), intent(out) :: a
        !! CSR matrix
        integer, intent(in) :: ndim, ncol, nnz
        !! number of rows, number of columns, number of nonzero elements

        integer :: row_counter, val_counter, temp_integer
        real :: temp_real

        a%ndim = ndim
        a%ncol = ncol
        a%nnz = nnz

        allocate(a%i(a%ndim + 1))
        allocate(a%j(a%nnz))
        allocate(a%val(a%nnz))

        ! Initialize row indices
        a%i(1) = 1
        do row_counter = 2, a%ndim
            call random_number(temp_real)
            temp_integer = nint(2 * a%nnz * temp_real / a%ncol)
            a%i(row_counter) = a%i(row_counter - 1) + temp_integer
            if (a%i(row_counter) > nnz + 1) then
                a%i(row_counter) = nnz + 1
            endif
        enddo
        a%i(a%ndim+1) = nnz + 1

        ! Initialize column values
        do val_counter = 1, a%nnz
            call random_number(temp_real)
            temp_integer = nint(a%ndim * temp_real)
            a%j(val_counter) = temp_integer
            call random_number(a%val(val_counter))
        enddo

    end subroutine

end module csr_samples_m