简介
在集群上每个计算节点有三四十个核心,往往使用MPI对任务进行并行计算,计算资源平均分布在多个节点的多个CPU核心上。为了充分利用计算节点剩余的核心,可以将任务再次细分,在MPI多进程的基础上每个进程使用多线程调用空闲核心完成计算。
多线程
源代码
#include <iostream>
#include <thread>
#include <pthread.h>
using namespace std;
// 多线程之pthread_create的参数结构体
typedef struct
{
int myid;
double *matrix;
char name[1024];
}THREADPARAM, *PTHREADPARAM;
// 多线程之std::thread的测试函数
void func(int myid)
{
std::cout << "this is func!" << endl;
// 打印线程ID
std::cout << "myid is : " << myid << " this thread id is " << std::this_thread::get_id() << endl;
}
// 多线程之pthread_create的测试函数,返回值为void*,输入参数的结构体为void*
void* func2(void* pthreadparam)
{
// 解析参数
std::cout << "this is func2! " << "thread id is : " << ((PTHREADPARAM)pthreadparam)->myid <<endl;
}
int main()
{
std::cout << "***************** std::thread start ***************** " << endl;
// 打印线程ID
std::cout << "main thread id = " << std::this_thread::get_id() << std::endl;
// 打印线程总数,即CPU逻辑核心数量
std::cout << "all threads number = " << std::thread::hardware_concurrency() << std::endl;
// 创建线程 std::thread 变量名 (调用函数名,函数所需参数)
std::thread thread1 (func, 1);
std::thread thread2 (func, 2);
std::thread thread3 (func, 3);
std::thread thread4 (func, 4);
// 阻塞线程(等待执行完毕,释放资源)
thread1.join();
thread2.join();
thread3.join();
thread4.join();
std::cout << "***************** std::thread end ***************** " << endl;
std::cout << "***************** pthread start ***************** " << endl;
// 子线程数量
const int THREADNUM = 32;
// 子线程所需参数
THREADPARAM threadparam[THREADNUM];
// 创建子线程
pthread_t pt[THREADNUM];
// 为子线程分配任务
for (int i = 0; i < THREADNUM; i++)
{
// 子线程参数结构体初始化
threadparam[i].myid = i;
// 启动子线程
pthread_create(&pt[i], NULL, func2, (void*)(&threadparam[i]));
}
// 阻塞子线程
for (int i = 0; i < THREADNUM; i++)
{
pthread_join(pt[i], NULL);
}
std::cout << "***************** pthread end ***************** " << endl;
return 0;
}
编译
g++ -o thread-l.out -std=c++11 -lpthread thread-l.cpp