分类 未分类 下的文章

Kokkos 是 C++ library

hierarchy :
device, host-parallel, host-serial

同步:
Kokkos::fence()

两个问题:

  1. memory space : device / host
  2. memory layout : LayoutLeft / LayoutRight

DualView : 维护在 device memory 上的 Kokkos::View 和其在 host memory 上的 Kokkos::View mirror,同时维护在两个不同 memory space 的 data

  • template argument:DataType, Layout, Device
  • using view_type = Kokkos::DualView<Scalar**, Kokkos::LayoutLeft, Device>

原子操作相关

scatter-add :两个粒子共享邻居,当两个粒子同时更新邻居时可能造成 race
使用 data replication V.S. 使用 atomic operation 解决 race 问题
ScatterView : 在编译时透明地选择处理原子操作方法,对于 CPU 使用 data replication,对于 GPU 使用 atomic operation

`Kokkos::Experimental::contribute(View &dest, Kokkos::Experimental::ScatterView
const &src) 将 ScatterView 的 reduction 结果放回到 dest,可能在 Kokkos::parallel_reduce()` 后调用

检查 Kokkos 属性

判断 layout :

  if (std::is_same<typename decltype(fpair->f)::traits::array_layout, Kokkos::LayoutLeft>::value) {
    printf("array fpair->f is LayoutLeft\n");
  }
  if (std::is_same<typename decltype(fpair->f)::traits::array_layout, Kokkos::LayoutRight>::value) {
    printf("array fpair->f is LayoutRight\n");
  }

获取 stride :

  int strides[2];
  (fpair->x).stride(strides);
  printf("array fpair->x stride : (%d, %d)\n", strides[0], strides[1]);

Access traits

RandomAccess : Kokkos::MemoryTraits<Kokkos::RandomAccess> ,当在 Cuda execution space 中执行时, 如果对于在 CudaSpaceCudaUVMSpace 中的 const View,Kokkos 会使用 texture fetch 访问

Unmanaged View : Kokkos::MemoryTraits<Kokkos::Unmanaged>, 对于一个 raw pointer, Kokkos 不进行 reference counting 和 deallocation

19 年时候学OOP(?)时候的post,更新一下记录一下遇到的新的设计模式:

std::enable_if

用 std::enable_if 增加一个新的函数参数
利用 SFINAE (Substitution Failure Is Not An Error)
模板替换失败不会报错,只会使对应模板不存在

template<int val>
void func(int x, typename std::enable_if<val, int>::type y) {
    printf("in func yes %d\n", x);
}

template<int val>
void func(int x, typename std::enable_if<!val, int>::type y) {
    printf("in func no %d\n", x);
}

int main() {
    func<0>(123, 123);
    
}

当 enable_if 为 false 时,std::enable_if<!val, int> 不存在 type 参数,模板替换错误,此时函数不存在

https://www.runoob.com/design-pattern/design-pattern-intro.html

委托模式


class I /*interface*/ {
public:
    virtual void f() = 0;
    virtual void g() = 0;
};

class A : public I {
public:
    void f(){std::cout << "A::f()" << std::endl;}
    void g(){std::cout << "A::g()" << std::endl;}
};

class B : public I {
public:
    void f(){std::cout << "B::f()" << std::endl;}
    void g(){std::cout << "B::g()" << std::endl;}
};


class C : public I {
public:
    C() { m_i = new A();/*delegation*/ }

    void f(){ m_i->f(); }
    void g(){ m_i->g(); }

    // normal attributes
    void toA(){ m_i = new A(); }
    void toB(){ m_i = new B(); }

private:
    I* m_i;
}

工厂模式

public class ShapeFactory {

   //使用 getShape 方法获取形状类型的对象
   public Shape getShape(String shapeType){
      if(shapeType == null){
         return null;
      }        
      if(shapeType.equalsIgnoreCase("CIRCLE")){
         return new Circle();
      } else if(shapeType.equalsIgnoreCase("RECTANGLE")){
         return new Rectangle();
      } else if(shapeType.equalsIgnoreCase("SQUARE")){
         return new Square();
      }
      return null;
   }
}

CRTP

静态多态



template 
struct base
{
    void interface()
    {
         // ...
         static_cast(this)->implementation();
         // ...
    }
};
 
struct derived : base
{
     void implementation()
     {
         // ...
     }
};

visitor模式

将数据与对数据的操作分离


//前向声明,访问者
class Visitor;

//被访问的抽象类,只有一个接口,用来接受访问者
class Object
{
public:
    virtual void accept(Visitor&v) = 0;
};

//具体的被访问的对象,学生。
class StudentA :public Object
{
    std::string name;
    int score;
public:
    StudentA(std::string name_);
    int getScore();
    std::string getName()
    {
        return name;
    }
    void setScore(int num);
    virtual void StudentA::accept(Visitor & v)
    {
        v.visit(this);
    }
};

//抽象访问者,只有一个接口,访问.
class Visitor
{
public:
    virtual void visit(Object* obj)=0;

};

//具体的访问者一,班主任,实现访问(学生并给学分)
class ClassLeader :public Visitor
{
public:
    virtual void visit(Object* obj);
};
//具体的访问者二,校长,实现访问(学生并批评不及格的学生)
class HeadMaster :public Visitor
{
public:
    virtual void visit(Object* obj);
};

observer模式

一些观察者观察一个数据,当数据被改动时通知所有观察者


public class Subject {
   
   private List observers 
      = new ArrayList();
   private int state;
 
   public int getState() {
      return state;
   }
 
   public void setState(int state) {
      this.state = state;
      notifyAllObservers();
   }
 
   public void attach(Observer observer){
      observers.add(observer);      
   }
 
   public void notifyAllObservers(){
      for (Observer observer : observers) {
         observer.update();
      }
   }  
}
public abstract class Observer {
   protected Subject subject;
   public abstract void update();
}
public class BinaryObserver extends Observer{
 
   public BinaryObserver(Subject subject){
      this.subject = subject;
      this.subject.attach(this);
   }
 
   @Override
   public void update() {
      System.out.println( "Binary String: " 
      + Integer.toBinaryString( subject.getState() ) ); 
   }
}

A100

peak FP64 : 9.7TFLOPS
peak FP32 : 19.5 TFLOPS
peak FP16 : 78 TFLOPS
peak TF32 tensor core : 156 TFLOPS

192KB L1 cache (shared memory) / SM
40MB L2 cache
40GB 主存, 1555GB/s 带宽

PCIe 4 : 31.5GB/s

在地学系的 centos 7 CPU 集群上配 slurm
登录节点 192.168.100.10
计算节点 192.168.100.2[1-8]

slurm 依赖 munge

yum update
yum install epel-release
yum install -y munge munge-libs munge-devel

拷贝 munge-key :

scp data:/etc/munge/munge.key /etc/munge/
chown munge: /etc/munge/munge.key
chmod 400 /etc/munge/munge.key

安装其他依赖:

yum install -y openssl openssl-devel pam-devel numactl numactl-devel hwloc hwloc-devel lua lua-devel readline-devel rrdtool-devel ncurses-devel man2html libibmad libibumad 
yum install -y perl-ExtUtils-MakeMaker
yum install -y mysql
yum install -y perl-PCP-PMDA

从头编译 slurm :

tar -xvjf slurm-18.08.2.tar.bz2
cd slurm-18.08.2/
./configure --prefix=/usr --sysconfdir=/etc/slurm --with-munge=/usr/bin/munge
make -j12
make install 
make install-contrib

拷贝 slurmd 服务 :

cp etc/slurmd.service /etc/systemd/system

登录节点启动服务 slurmctld
计算节点启动服务 slurmd

slurmd log 路径默认为 /var/log/slurmd.log

slurm 配置文件为 /etc/slurm/slurm.conf,需保证所有节点的配置文件相同

配置 /etc/slurm/cgroup.conf 如下:

CgroupAutomount=yes
ConstrainCores=no
ConstrainRAMSpace=no

新建 slurm 用户:

groupadd -g 888 slurm
useradd  -m -c "SLURM workload manager" -d /var/lib/slurm -u 888 -g slurm  -s /bin/bash slurm

使用 slurm 运行 slurmd.service

/etc/systemd/system/slurmd.service 中设置

[Service]
User=slurm

slurm 生成配置:https://slurm.schedmd.com/configurator.html

slurmd -Dvvv # 调试启动
slurmd -C # 查看节点配置
scontrol show node [node] # 查看 node 状态
scontrol update NodeName=[node] State=UNDRAIN # 将 node 状态设为 undrain     sf

slurmdPidFile 默认位于 /var/run/slurmd.pid,路径
/var/run 目录只有 root 有写权限,且 /var/run 是 tmpfs
新建 /var/run/slurm,权限设置为 0770
/etc/tmpfiles.d/slurm.conf 中添加

d /var/run/slurm 0770 root slurm -

开机后自动设置路径
修改 /etc/systemd/system/slurmd.service 中 PIDFile 为 /var/run/slurm/slurmd.pid

然后 systemctl daemon-reload