使用 apt 源安装 ROCm 6.0.x 在Ubuntu 22.04.01

从源码编译 rocSolver

本人只操作过单个rocm版本的情景,20240218 ubuntu 22.04.01


1,卸载原先的rocm


https://docs.amd.com/en/docs-5.1.3/deploy/linux/os-native/uninstall.html
 

# Uninstall single-version ROCm packages
sudo apt autoremove rocm-core

# Uninstall Kernel-mode Driver
sudo apt autoremove amdgpu-dkms

# remove apt source
sudo rm /etc/apt/sources.list.d/<rocm_repository-name>.list
sudo rm /etc/apt/sources.list.d/<amdgpu_repository-name>.list
sudo rm /etc/apt/sources.list.d/rocm.list
sudo rm /etc/apt/sources.list.d/amdgpu.list

sudo rm -rf /var/cache/apt/*
sudo apt-get clean all

sudo reboot

2,安装最新的rocm


https://rocm.docs.amd.com/projects/install-on-linux/en/latest/tutorial/quick-start.html#rocm-install-quick

sudo apt install "linux-headers-$(uname -r)" "linux-modules-extra-$(uname -r)"
# See prerequisites. Adding current user to Video and Render groups
sudo usermod -a -G render,video $LOGNAME
wget https://repo.radeon.com/amdgpu-install/6.0.2/ubuntu/jammy/amdgpu-install_6.0.60002-1_all.deb
sudo apt install ./amdgpu-install_6.0.60002-1_all.deb
sudo apt update
sudo apt install amdgpu-dkms
sudo apt install rocm
sudo reboot
sudo amdgpu-install --usecase=graphics,rocm
sudo reboot

3,安装依赖

Clang: ...  'cmath' file not found
找不到 #include_next <cmath>

sudo apt install libstdc++-12-dev

4,示例

4.1 amd官方示例

$ git clone --recursive  https://github.com/amd/rocm-examples.git
$ cd HIP-Basic/device_query
$ make
$ ./hip_device_query

4.2 rocsolver_dgeqrf

ex_rocsolver_dgeqrf.cpp

/
// example.cpp source code //
/

#include <algorithm> // for std::min
#include <stddef.h>  // for size_t
#include <stdio.h>
#include <vector>
#include <hip/hip_runtime_api.h> // for hip functions
#include <rocsolver/rocsolver.h> // for all the rocsolver C interfaces and type declarations

void init_vector(double* A, int n)
{
  for(int i=0; i<n; i++)
    A[i] = (rand()%2000)/1000.0;
}

void print_matrix(double* A, int M, int N, int lda)
{
  for(int i=0; i<M; i++)
  {
    for(int j=0; j<N; j++)
    {
      printf("%7.4f, ", A[i + j*lda]);
    }
    printf("\n");
  }

}

int main() {
  rocblas_int M = 7;
  rocblas_int N = 7;
  rocblas_int lda = M;

  // here is where you would initialize M, N and lda with desired values

  rocblas_handle handle;
  rocblas_create_handle(&handle);

  size_t size_A = size_t(lda) * N;          // the size of the array for the matrix
  size_t size_piv = size_t(std::min(M, N)); // the size of array for the Householder scalars

  std::vector<double> hA(size_A);      // creates array for matrix in CPU
  std::vector<double> hIpiv(size_piv); // creates array for householder scalars in CPU

  init_vector(hA.data(), size_A);
  memset(hIpiv.data(), 0, size_piv*sizeof(double));

  print_matrix(hA.data(), M, N, lda);

  double *dA, *dIpiv;
  hipMalloc(&dA, sizeof(double)*size_A);      // allocates memory for matrix in GPU
  hipMalloc(&dIpiv, sizeof(double)*size_piv); // allocates memory for scalars in GPU

  // here is where you would initialize matrix A (array hA) with input data
  // note: matrices must be stored in column major format,
  //       i.e. entry (i,j) should be accessed by hA[i + j*lda]

  // copy data to GPU
  hipMemcpy(dA, hA.data(), sizeof(double)*size_A, hipMemcpyHostToDevice);
  // compute the QR factorization on the GPU
  rocsolver_dgeqrf(handle, M, N, dA, lda, dIpiv);
  // copy the results back to CPU
  hipMemcpy(hA.data(), dA, sizeof(double)*size_A, hipMemcpyDeviceToHost);
  hipMemcpy(hIpiv.data(), dIpiv, sizeof(double)*size_piv, hipMemcpyDeviceToHost);

  printf("\nR =\n");
  print_matrix(hA.data(), M, N, lda);
  printf("\ntau=\n");
  print_matrix(hIpiv.data(), 1, N, 1);

  // the results are now in hA and hIpiv, so you can use them here

  hipFree(dA);                        // de-allocate GPU memory
  hipFree(dIpiv);
  rocblas_destroy_handle(handle);     // destroy handle
}

Makefile:

EXE := ex_rocsolver_dgeqrf

all: $(EXE)

INC :=  -I /opt/rocm/include -D__HIP_PLATFORM_AMD__
LD_FLAGS := -L /opt/rocm/lib -lamdhip64 -lrocblas -lrocsolver

ex_rocsolver_dgeqrf.o: ex_rocsolver_dgeqrf.cpp
        g++ $< $(INC) -c -o $@

ex_rocsolver_dgeqrf: ex_rocsolver_dgeqrf.o
        g++ $< $(LD_FLAGS) -o $@


.PHONY: clean
clean:
        ${RM} *.o $(EXE)

运行效果:

使用matlab对结果做验证:

相关推荐

  1. ubuntu 使用 apt 安装、卸载 mysql

    2024-02-19 11:24:04       33 阅读
  2. Ubuntu上配置(安装使用)Nginx

    2024-02-19 11:24:04       34 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-19 11:24:04       91 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-19 11:24:04       97 阅读
  3. 在Django里面运行非项目文件

    2024-02-19 11:24:04       78 阅读
  4. Python语言-面向对象

    2024-02-19 11:24:04       88 阅读

热门阅读

  1. 【开源】C++ 周期任务调度的思想和实现

    2024-02-19 11:24:04       40 阅读
  2. 银行的金额大小写转换

    2024-02-19 11:24:04       47 阅读
  3. sql语句创建数据库

    2024-02-19 11:24:04       54 阅读
  4. 【c++】斐波那契数列

    2024-02-19 11:24:04       45 阅读
  5. 「计算机网络」物理层

    2024-02-19 11:24:04       50 阅读