# Maintainer: Sven-Hendrik Haase <svenstaro@archlinux.org>
# Maintainer: Torsten Keßler <tpkessler@archlinux.org>
# Contributor: bartus <arch-user-repoᘓbartus.33mail.com>
# Contributor: pingplug <pingplug@foxmail.com>
# Contributor: cornholio <vigo.the.unholy.carpathian@gmail.com>

_pkgname=magma
pkgbase=$_pkgname
pkgname=(magma-cuda magma-hip)
pkgver=2.7.2
pkgrel=4
_pkgdesc="Matrix Algebra on GPU and Multicore Architectures"
arch=('x86_64' 'aarch64')
url="https://icl.utk.edu/magma/"
license=('custom')
depends=('blas' 'lapack')
makedepends=('git' 'cmake' 'ninja' 'python' 'gcc-fortran'
             'cuda'
             'rocm-core' 'hip-runtime-amd' 'hipblas' 'hipsparse')
optdepends=('python: for examples and tests'
            'gcc-fortran: Fortran interface')
_commit=a1625ff4d9bc362906bd01f805dbbe12612953f6  # commit after v2.7.2 with ROCm 6 fixes.
source=("${_pkgname}::git+https://bitbucket.org/icl/magma.git#commit=${_commit}"
        'add_cuda_archs.patch')
sha256sums=('SKIP'
            '1ed3706a65a124ecef71012273687307aac70bfc80bc99a978002dfb0ee000cc')
options=(!lto)

_valid_sm() {
  echo "sm_52 sm_53 sm_60 sm_61 sm_62 sm_70 sm_72 sm_75 sm_80 sm_86 sm_87 sm_89 sm_90"
}

_valid_gfx() {
  #List of GPU targets from rocBLAS
  echo "gfx900 gfx906:xnack- gfx908:xnack- gfx90a:xnack+ gfx90a:xnack- gfx940 gfx941 gfx942 gfx1010 gfx1012 gfx1030 gfx1100 gfx1101 gfx1102"
}

prepare() {
  cp -r "${_pkgname}" "${_pkgname}-${pkgver}-cuda"
  cp -r "${_pkgname}" "${_pkgname}-${pkgver}-hip"

  cd "${_pkgname}-${pkgver}-cuda"
  echo -e "BACKEND = cuda\nFORT = true\nGPU_TARGET=$(_valid_sm)" > make.inc
  make generate
  patch -Np1 -i "${srcdir}/add_cuda_archs.patch"

  cd "${srcdir}"

  cd "${_pkgname}-${pkgver}-hip"
  echo -e "BACKEND = hip\nFORT = true\nGPU_TARGET=$(_valid_gfx)" > make.inc
  make generate
}

build() {
  echo "Build with cuda backend"
  cd "${srcdir}/${_pkgname}-${pkgver}-cuda"

  case $CARCH in
      x86_64)
          _cuda_arch=x86_64
          ;;
      aarch64)
          _cuda_arch=sbsa
          ;;
  esac

  cmake \
    -Bbuild \
    -GNinja \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=/opt/cuda/targets/${_cuda_arch}-linux \
    -DBUILD_SHARED_LIBS=ON \
    -DGPU_TARGET="$(_valid_sm)"
  ninja -C build


  echo "Build with rocm/hip backend"
  cd "${srcdir}/${_pkgname}-${pkgver}-hip"
  local _rocm_ver=$(./tools/get-rocm-version.sh)
  # -fcf-protection is not supported by HIP, see
  # https://docs.amd.com/bundle/ROCm-Compiler-Reference-Guide-v5.5/page/Compiler_Options_and_Features.html#d2e2018
  CXXFLAGS+=" -fcf-protection=none"
  # ROCm version needs to be passed to the compiler since it's not part of the
  # cmake toolchain yet.
  CXXFLAGS+=" -DROCM_VERSION=$_rocm_ver"
  # With ROCm 6.0.0 the header moved from /opt/rocm/include to the subfolder hipsparse.
  # magma still uses the old location.
  CXXFLAGS+=" -isystem /opt/rocm/include/hipsparse"
  cmake \
    -Bbuild \
    -GNinja \
    -DCMAKE_CXX_COMPILER=/opt/rocm/bin/hipcc \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=/opt/rocm \
    -DBUILD_SHARED_LIBS=ON \
    -DMAGMA_ENABLE_HIP=ON \
    -DGPU_TARGET="$(_valid_gfx)"
  ninja -C build
}

_package() {
  DESTDIR="${pkgdir}" ninja -Cbuild install

  local _prefix="$1"
  install -d "${pkgdir}/${_prefix}"/share/magma/example
  cp -r "${srcdir}"/magma/example/* "${pkgdir}/${_prefix}"/share/magma/example/
  install -d "${pkgdir}/${_prefix}"/share/magma/testing
  cp -r "${srcdir}"/magma/testing/* "${pkgdir}/${_prefix}"/share/magma/testing/
  install -Dm644 "${srcdir}"/magma/COPYRIGHT "${pkgdir}"/usr/share/licenses/${pkgname}/LICENSE
  echo "${_prefix}/lib" > "${pkgname}.conf"
  install -Dm644 "${pkgname}.conf" "${pkgdir}"/etc/ld.so.conf.d/"${pkgname}.conf"
}

package_magma-cuda() {
  pkgdesc="${_pkgdesc} (with CUDA)"
  depends+=(cuda)
  provides=(magma)
  replaces=(magma)

  cd "${srcdir}/${_pkgname}-${pkgver}-cuda"
  _package "/opt/cuda"
}

package_magma-hip() {
  pkgdesc="${_pkgdesc} (with ROCm/HIP)"
  depends+=(hip-runtime-amd hipblas hipsparse)
  provides=(hipmagma)
  replaces=(hipmagma)

  cd "${srcdir}/${_pkgname}-${pkgver}-hip"
  _package "/opt/rocm"
}

# vim:set ts=2 sw=2 et:
