Ubuntu20.04 安装HPC_SDK加速库

1. NVIDIA HPC SDK 简介

NVIDIA HPC SDK(NVIDIA High Performance Compute Software Development Kit)是一个适用于高性能计算的全面的编译器,库和工具套件。NVIDIA HPC SDK包括通过验证的编译器,库和软件工具,这些工具对于最大化开发人员的工做效率以及HPC应用程序的性能和可移植性相当重要。html

NVIDIA HPC SDK 包括有如下的一些编译器和内容,C,C ++和Fortran编译器经过标准C ++和Fortran,OpenACC指令和CUDA支持GPU加速HPC建模和仿真应用程序。 GPU加速的数学库最大程度地提升了通用HPC算法的性能,优化的通讯库可实现基于标准的多GPU和可扩展的系统编程。 性能分析和调试工具简化了HPC应用程序的移植和优化,而容器化工具能够在本地或云中轻松部署。 经过支持NVIDIA GPU和运行Linux的Arm,OpenPOWER或x86-64 CPU,HPC SDK提供了构建NVIDIA GPU加速的HPC应用程序所需的工具。web

2. 安装

按照官网上的说明,能够直接下载它的tar包进行安装或者是rpm、deb包进行安装。笔者这里的操做系统是Ubuntu20.04,已经安装成功CUDA-10.1库,因此笔者下载了deb包进行安装,包含有两个版本的。算法

wget https://developer.download.nvidia.com/hpc-sdk/20.9/nvhpc-20-9_20.9_amd64.deb \
https://developer.download.nvidia.com/hpc-sdk/20.9/nvhpc-2020_20.9_amd64.deb \
https://developer.download.nvidia.com/hpc-sdk/20.9/nvhpc-20-9-cuda-multi_20.9_amd64.deb
sudo apt-get install ./nvhpc-20-9_20.9_amd64.deb ./nvhpc-2020_20.9_amd64.deb ./nvhpc-20-9-cuda-multi_20.9_amd64.deb

安装的工具包安装到了/opt/nvidia/hpc_sdk文件夹中,编辑文件~/.bashrc编程

nano ~/.bashrc

而后进行环境变量设置bash

export NVARCH=`uname -s`_`uname -m`; 
export NVCOMPILERS=/opt/nvidia/hpc_sdk;
export PATH=$NVCOMPILERS/$NVARCH/20.9/comm_libs/mpi/bin:$PATH
export MANPATH=$MANPATH:$NVCOMPILERS/$NVARCH/20.9/comm_libs/mpi/man

使得环境变量生效ide

source ~/.bashrc

这样就安装成功了HPCSDK加速工具包。svg

3. OpenACC程序测试

如今咱们编写一个很是简单的程序来测试HPC SDK是否能使用。咱们这里使用到了OpenACC加速库。下面是一个C语言写成的一个程序,文件名为test.c工具

#include <stdio.h>
 #include <stdlib.h>
 void vecaddgpu( float *restrict r, float *a, float *b, int n ){ 
 
  
     #pragma acc kernels loop copyin(a[0:n],b[0:n]) copyout(r[0:n])
     for( int i = 0; i < n; ++i ) r[i] = a[i] + b[i];
 }

 int main( int argc, char* argv[] ){ 
 
  
     int n; /* vector length */
     float * a; /* input vector 1 */
     float * b; /* input vector 2 */
     float * r; /* output vector */
     float * e; /* expected output values */
     int i, errs;
     if( argc > 1 ) n = atoi( argv[1] );
     else n = 100000; /* default vector length */
     if( n <= 0 ) n = 100000;
     a = (float*)malloc( n*sizeof(float) );
     b = (float*)malloc( n*sizeof(float) );
     r = (float*)malloc( n*sizeof(float) );
     e = (float*)malloc( n*sizeof(float) );
     for( i = 0; i < n; ++i ){ 
 
  
          a[i] = (float)(i+1);
          b[i] = (float)(1000*i);
     }
     /* compute on the GPU */
     vecaddgpu( r, a, b, n );
     /* compute on the host to compare */
     for( i = 0; i < n; ++i ) e[i] = a[i] + b[i];
     /* compare results */
     errs = 0;
     for( i = 0; i < n; ++i ){ 
 
  
         if( r[i] != e[i] ){ 
 
  
            ++errs;
        }
    }
    printf( "%d errors found\n", errs );
    return errs;
 }

对上述程序进行编译处理:oop

nvc -o test -acc test.c

若是显示下列内容则安装成功性能

0 errors found

fortran90程序,测试文件test.f90

module vecaddmod
  implicit none
 contains
  subroutine vecaddgpu( r, a, b, n )
   real, dimension(:) :: r, a, b
   integer :: n
   integer :: i
!$acc kernels loop copyin(a(1:n),b(1:n)) copyout(r(1:n))
   do i = 1, n
    r(i) = a(i) + b(i)
   enddo
  end subroutine
end module

program main
  use vecaddmod
  implicit none
  integer :: n, i, errs, argcount
  real, dimension(:), allocatable :: a, b, r, e
  character*10 :: arg1
  argcount = command_argument_count()
  n = 1000000  ! default value
  if( argcount >= 1 )then
   call get_command_argument( 1, arg1 )
   read( arg1, '(i)' ) n
   if( n <= 0 ) n = 100000
  endif
  allocate( a(n), b(n), r(n), e(n) )
  do i = 1, n
   a(i) = i
   b(i) = 1000*i
  enddo
  ! compute on the GPU
  call vecaddgpu( r, a, b, n )
  ! compute on the host to compare
  do i = 1, n
   e(i) = a(i) + b(i)
  enddo
  ! compare results
  errs = 0
  do i = 1, n
   if( r(i) /= e(i) )then
     errs = errs + 1
   endif
  enddo
  print *, errs, ' errors found'
  if( errs ) call exit(errs)
end program

编译程序

nvfortran -o test -acc test.f90

若显示

0  errors found

则安装成功

参考

[1] HPC SDK 官方安装向导
[2] OpenAcc官方参考文档