Linux 下的 metis 与 mt-metis 的安装和使用 #
- 本文的 Linux 包括 VMare 和 windows 下的 WSL 环境下安装 metis 串行和并行
- 本文还讲述了电脑的线程以及和超线程的区别,如何寻找,如何判别,请看下文
装备:
- Ubuntu18.6.4LTS
- gcc7.5.0
- cmake version 3.10.2
metis 的安装 #
别相信网上的一键sudo apt-get install libmetis-dev,有很多问题!!!
- 下载链接->metis-5.1.0.tar.gz
- 解压成文件夹,名字假设为 A
- 打开
A/include/metis.h,根据自己电脑的位数(32or64)选择
// 64位
#define IDXTYPEWIDTH 64
// 32位
#define IDXTYPEWIDTH 32
- 在 A 目录执行以下编译命令
make config
make
sudo make install
可能出现的问题:
- make directory '/home/xxx/metis/build/Linux-x86_64'
- 无伤大雅,没有问题
- make directory '/home/xxx/metis/build/Linux-x86_64'
配置环境
sudo vim /etc/ld.so.conf- 末尾添加
include /usr/local/bin保存 - 运行
sudo ldconfig更新
测试环境
#include <cstddef> /* NULL */
#include <metis.h>
#include <iostream>
#include <vector>
int main(){
idx_t nVertices = 6;//顶点个数
idx_t nEdges = 7;//边的条数
idx_t nWeights = 1;//权重
idx_t nParts = 2;//几类
idx_t objval;
std::vector<idx_t> part(nVertices, 0);
// Indexes of starting points in adjacent array
std::vector<idx_t> xadj = {0,2,5,7,9,12,14};
// Adjacent vertices in consecutive index order
std::vector<idx_t> adjncy = {1,3,0,4,2,1,5,0,4,3,1,5,4,2};
// Weights of vertices
// if all weights are equal then can be set to NULL
std::vector<idx_t> vwgt(nVertices * nWeights, 0);
int ret = METIS_PartGraphKway(&nVertices,& nWeights, xadj.data(), adjncy.data(),
NULL, NULL, NULL, &nParts, NULL,
NULL, NULL, &objval, part.data());
std::cout << ret << std::endl;
for(unsigned part_i = 0; part_i < part.size(); part_i++){
std::cout << part_i << " " << part[part_i] << std::endl;
}
return 0;
}
g++ -std=c++11 test.cpp -o test -lmetis- 结果:
1
0 1
1 0
2 0
3 1
4 1
5 0
**ps:**分割是随机的,只要看你是不是分为 0,1 共 2 类
mt-metis 的安装 #
mt-metis 就是 metis 的多线程版本,能够进行并行计算
- 下载链接->mt-metis-0.7.2.tar.gz
- 解压成文件夹,名字假设为 A
- 打开
A/metis/include/metis.h,根据自己电脑的位数(32or64)选择
// 64位
#define IDXTYPEWIDTH 64
// 32位
#define IDXTYPEWIDTH 32
- 回到 A 目录里,运行编译命令
./configure
make
sudo make install
- 可能的问题
./configure:command not found解决办法:sh configure然后添加执行权限chmod u+x configure- 如果是 wsl 安装,把第一个命令改成
bash ./configure即可
- 使用方法
mtmetis test.graph 2 test.part -t -T n
解释:
- test.graph 是图表的文件,例如下面内容:第一行就是顶点和边的个数,顶点从 1 开始,下面就是每两个顶点连接成的边
10 10 10 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 1 2是分成的类别数量test.part是分割后生成的文件-t能显示 mtmetis 的运行时间-T nn 代表线程数量,默认以全部线程数,不能超过最大的线程数
超线程与线程 #
接着上题,你可能会疑惑该如何查找自己的线程数量为多少?那什么又是超线程呢?
- 以 windows 为例,打开任务管理器-》性能
- 能够看到右下角有内核和逻辑处理器,能发现逻辑处理器=内核*2,这里的逻辑处理器数量就是超线程数量,线程数量=内核数量
- 超线程是 intel 提出的虚拟化处理器,在高性能计算中我们不能使用超线程,因此一定要知道自己最真实的线程数量有多少
- 在 vmare 虚拟机的处理器配置中也用到了超线程,为了规避这种情况,可以设置 1 个处理器,内核数量为真实线程数量(因为每个人就是 1 个 CPU 处理器,x 核,而 intel 使用超线程让内核*2)
- 你可能还看到左侧线程有 3000,4000。刚刚又说电脑只有 n 个总线程,远小于他!xd,可别忘了,线程切换的开销很小,电脑一会就切换了好多线程,所以你看到了有 3,4k。
